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."""
66 def splitInt(src
, splitIndex
, dstA
, dstB
, idx
):
71 idx
= splitInt(s
[1], splitIndex
, dstA2
, dstB2
, idx
)
72 dstA
.append([s
[0], dstA2
])
73 dstB
.append([s
[0], dstB2
])
76 dstA
.append([None, s
[1]])
78 dstB
.append([None, s
[1]])
84 splitInt(pipeObj
, splitIndex
, listA
, listB
, 0)
88 def remove(pipeObj
, removeIndex
):
89 """Create new pipeline object by removing pass with index removeIndex from pipeObj."""
91 def removeInt(src
, removeIndex
, dst
, idx
):
95 idx
= removeInt(s
[1], removeIndex
, dst2
, idx
)
96 dst
.append([s
[0], dst2
])
98 if idx
!= removeIndex
:
99 dst
.append([None, s
[1]])
104 removeInt(pipeObj
, removeIndex
, dst
, 0)
108 def copy(srcPipeObj
):
109 """Create copy of pipeline object srcPipeObj."""
111 def copyInt(dst
, src
):
116 dst
.append([s
[0], dst2
])
118 dst
.append([None, s
[1]])
121 copyInt(dstPipeObj
, srcPipeObj
)
125 def prune(srcPipeObj
):
126 """Create new pipeline object by removing empty pass-managers (those with count = 0) from srcPipeObj."""
128 def pruneInt(dst
, src
):
134 dst
.append([s
[0], dst2
])
136 dst
.append([None, s
[1]])
139 pruneInt(dstPipeObj
, srcPipeObj
)
143 if __name__
== "__main__":
146 class Test(unittest
.TestCase
):
148 pipeStr
= "a,b,A(c,B(d,e),f),g"
149 pipeObj
= fromStr(pipeStr
)
151 self
.assertEqual(7, count(pipeObj
))
153 self
.assertEqual(pipeObj
, pipeObj
)
154 self
.assertEqual(pipeObj
, prune(pipeObj
))
155 self
.assertEqual(pipeObj
, copy(pipeObj
))
157 self
.assertEqual(pipeStr
, toStr(pipeObj
))
158 self
.assertEqual(pipeStr
, toStr(prune(pipeObj
)))
159 self
.assertEqual(pipeStr
, toStr(copy(pipeObj
)))
161 [pipeObjA
, pipeObjB
] = split(pipeObj
, 3)
162 self
.assertEqual("a,b,A(c,B(d))", toStr(pipeObjA
))
163 self
.assertEqual("A(B(e),f),g", toStr(pipeObjB
))
165 self
.assertEqual("b,A(c,B(d,e),f),g", toStr(remove(pipeObj
, 0)))
166 self
.assertEqual("a,b,A(c,B(d,e),f)", toStr(remove(pipeObj
, 6)))
168 pipeObjC
= remove(pipeObj
, 4)
169 self
.assertEqual("a,b,A(c,B(d),f),g", toStr(pipeObjC
))
170 pipeObjC
= remove(pipeObjC
, 3)
171 self
.assertEqual("a,b,A(c,B(),f),g", toStr(pipeObjC
))
172 pipeObjC
= prune(pipeObjC
)
173 self
.assertEqual("a,b,A(c,f),g", toStr(pipeObjC
))