1 # Automatically formatted with yapf (https://github.com/google/yapf)
2 """Utility functions for creating and manipulating LLVM 'opt' NPM pipeline objects."""
6 """Create pipeline object from string representation."""
14 curr
.append([None, tok
])
17 stack
.append([kind
, curr
])
23 curr
.append([None, tok
])
27 [kind
, curr
] = stack
.pop()
28 curr
.append([oldKind
, oldCurr
])
32 curr
.append([None, tok
])
37 """Create string representation of pipeline object."""
39 lastIdx
= len(pipeObj
) - 1
40 for i
, c
in enumerate(pipeObj
):
53 """Count number of passes (pass-managers excluded) in pipeline object."""
63 def split(pipeObj
, splitIndex
):
64 """Create two new pipeline objects by splitting pipeObj in two directly after pass with index splitIndex."""
65 def splitInt(src
, splitIndex
, dstA
, dstB
, idx
):
70 idx
= splitInt(s
[1], splitIndex
, dstA2
, dstB2
, idx
)
71 dstA
.append([s
[0], dstA2
])
72 dstB
.append([s
[0], dstB2
])
75 dstA
.append([None, s
[1]])
77 dstB
.append([None, s
[1]])
83 splitInt(pipeObj
, splitIndex
, listA
, listB
, 0)
87 def remove(pipeObj
, removeIndex
):
88 """Create new pipeline object by removing pass with index removeIndex from pipeObj."""
89 def removeInt(src
, removeIndex
, dst
, idx
):
93 idx
= removeInt(s
[1], removeIndex
, dst2
, idx
)
94 dst
.append([s
[0], dst2
])
96 if idx
!= removeIndex
:
97 dst
.append([None, s
[1]])
102 removeInt(pipeObj
, removeIndex
, dst
, 0)
106 def copy(srcPipeObj
):
107 """Create copy of pipeline object srcPipeObj."""
108 def copyInt(dst
, src
):
113 dst
.append([s
[0], dst2
])
115 dst
.append([None, s
[1]])
118 copyInt(dstPipeObj
, srcPipeObj
)
122 def prune(srcPipeObj
):
123 """Create new pipeline object by removing empty pass-managers (those with count = 0) from srcPipeObj."""
124 def pruneInt(dst
, src
):
130 dst
.append([s
[0], dst2
])
132 dst
.append([None, s
[1]])
135 pruneInt(dstPipeObj
, srcPipeObj
)
139 if __name__
== "__main__":
142 class Test(unittest
.TestCase
):
144 pipeStr
= 'a,b,A(c,B(d,e),f),g'
145 pipeObj
= fromStr(pipeStr
)
147 self
.assertEqual(7, count(pipeObj
))
149 self
.assertEqual(pipeObj
, pipeObj
)
150 self
.assertEqual(pipeObj
, prune(pipeObj
))
151 self
.assertEqual(pipeObj
, copy(pipeObj
))
153 self
.assertEqual(pipeStr
, toStr(pipeObj
))
154 self
.assertEqual(pipeStr
, toStr(prune(pipeObj
)))
155 self
.assertEqual(pipeStr
, toStr(copy(pipeObj
)))
157 [pipeObjA
, pipeObjB
] = split(pipeObj
, 3)
158 self
.assertEqual('a,b,A(c,B(d))', toStr(pipeObjA
))
159 self
.assertEqual('A(B(e),f),g', toStr(pipeObjB
))
161 self
.assertEqual('b,A(c,B(d,e),f),g', toStr(remove(pipeObj
, 0)))
162 self
.assertEqual('a,b,A(c,B(d,e),f)', toStr(remove(pipeObj
, 6)))
164 pipeObjC
= remove(pipeObj
, 4)
165 self
.assertEqual('a,b,A(c,B(d),f),g', toStr(pipeObjC
))
166 pipeObjC
= remove(pipeObjC
, 3)
167 self
.assertEqual('a,b,A(c,B(),f),g', toStr(pipeObjC
))
168 pipeObjC
= prune(pipeObjC
)
169 self
.assertEqual('a,b,A(c,f),g', toStr(pipeObjC
))