1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172
|
# Automatically formatted with yapf (https://github.com/google/yapf)
"""Utility functions for creating and manipulating LLVM 'opt' NPM pipeline objects."""
def fromStr(pipeStr):
"""Create pipeline object from string representation."""
stack = []
curr = []
tok = ''
kind = ''
for c in pipeStr:
if c == ',':
if tok != '':
curr.append([None, tok])
tok = ''
elif c == '(':
stack.append([kind, curr])
kind = tok
curr = []
tok = ''
elif c == ')':
if tok != '':
curr.append([None, tok])
tok = ''
oldKind = kind
oldCurr = curr
[kind, curr] = stack.pop()
curr.append([oldKind, oldCurr])
else:
tok += c
if tok != '':
curr.append([None, tok])
return curr
def toStr(pipeObj):
"""Create string representation of pipeline object."""
res = ''
lastIdx = len(pipeObj) - 1
for i, c in enumerate(pipeObj):
if c[0]:
res += c[0] + '('
res += toStr(c[1])
res += ')'
else:
res += c[1]
if i != lastIdx:
res += ','
return res
def count(pipeObj):
"""Count number of passes (pass-managers excluded) in pipeline object."""
cnt = 0
for c in pipeObj:
if c[0]:
cnt += count(c[1])
else:
cnt += 1
return cnt
def split(pipeObj, splitIndex):
"""Create two new pipeline objects by splitting pipeObj in two directly after pass with index splitIndex."""
def splitInt(src, splitIndex, dstA, dstB, idx):
for s in src:
if s[0]:
dstA2 = []
dstB2 = []
idx = splitInt(s[1], splitIndex, dstA2, dstB2, idx)
dstA.append([s[0], dstA2])
dstB.append([s[0], dstB2])
else:
if idx <= splitIndex:
dstA.append([None, s[1]])
else:
dstB.append([None, s[1]])
idx += 1
return idx
listA = []
listB = []
splitInt(pipeObj, splitIndex, listA, listB, 0)
return [listA, listB]
def remove(pipeObj, removeIndex):
"""Create new pipeline object by removing pass with index removeIndex from pipeObj."""
def removeInt(src, removeIndex, dst, idx):
for s in src:
if s[0]:
dst2 = []
idx = removeInt(s[1], removeIndex, dst2, idx)
dst.append([s[0], dst2])
else:
if idx != removeIndex:
dst.append([None, s[1]])
idx += 1
return idx
dst = []
removeInt(pipeObj, removeIndex, dst, 0)
return dst
def copy(srcPipeObj):
"""Create copy of pipeline object srcPipeObj."""
def copyInt(dst, src):
for s in src:
if s[0]:
dst2 = []
copyInt(dst2, s[1])
dst.append([s[0], dst2])
else:
dst.append([None, s[1]])
dstPipeObj = []
copyInt(dstPipeObj, srcPipeObj)
return dstPipeObj
def prune(srcPipeObj):
"""Create new pipeline object by removing empty pass-managers (those with count = 0) from srcPipeObj."""
def pruneInt(dst, src):
for s in src:
if s[0]:
if count(s[1]):
dst2 = []
pruneInt(dst2, s[1])
dst.append([s[0], dst2])
else:
dst.append([None, s[1]])
dstPipeObj = []
pruneInt(dstPipeObj, srcPipeObj)
return dstPipeObj
if __name__ == "__main__":
import unittest
class Test(unittest.TestCase):
def test_0(self):
pipeStr = 'a,b,A(c,B(d,e),f),g'
pipeObj = fromStr(pipeStr)
self.assertEqual(7, count(pipeObj))
self.assertEqual(pipeObj, pipeObj)
self.assertEqual(pipeObj, prune(pipeObj))
self.assertEqual(pipeObj, copy(pipeObj))
self.assertEqual(pipeStr, toStr(pipeObj))
self.assertEqual(pipeStr, toStr(prune(pipeObj)))
self.assertEqual(pipeStr, toStr(copy(pipeObj)))
[pipeObjA, pipeObjB] = split(pipeObj, 3)
self.assertEqual('a,b,A(c,B(d))', toStr(pipeObjA))
self.assertEqual('A(B(e),f),g', toStr(pipeObjB))
self.assertEqual('b,A(c,B(d,e),f),g', toStr(remove(pipeObj, 0)))
self.assertEqual('a,b,A(c,B(d,e),f)', toStr(remove(pipeObj, 6)))
pipeObjC = remove(pipeObj, 4)
self.assertEqual('a,b,A(c,B(d),f),g', toStr(pipeObjC))
pipeObjC = remove(pipeObjC, 3)
self.assertEqual('a,b,A(c,B(),f),g', toStr(pipeObjC))
pipeObjC = prune(pipeObjC)
self.assertEqual('a,b,A(c,f),g', toStr(pipeObjC))
unittest.main()
exit(0)
|