def test_splitblock(self): old, new = self.b.splitblock('newblock') with self.b.at_front(old): self.b.add(types.Int32, [self.a, self.a]) with self.b.at_end(new): self.b.div(types.Int32, [self.a, self.a]) self.assertEqual(opcodes(self.f), ['add', 'div'])
def test_ssa2(self): mod = from_c(source) f = mod.get_function('func') cfa.run(f) verify(f) codes = opcodes(f) self.assertEqual(codes.count('phi'), 3)
def test_splitblock(self): old, new = self.b.splitblock('newblock') with self.b.at_front(old): self.b.add(self.a, self.a) with self.b.at_end(new): self.b.div(self.a, self.a) self.assertEqual(opcodes(self.f), ['add', 'div'])
def test_replace(self): entry = self.f.get_block('entry') for op in entry: if op.opcode == ops.convert: r, = op.args t = self.b.add(types.Int32, [r, r]) c = self.b.convert(types.Float32, [t], result=op.result) op.replace([t, c]) break cfa.run(self.f) self.assertEqual(opcodes(self.f), ['mul', 'add', 'convert', 'ret'])
def test_ssa(self): mod = from_c(source) f = mod.get_function('func_simple') verify(f) self.assertEqual(opcodes(f.startblock), ['alloca', 'store', 'load', 'gt', 'cbranch']) # SSA CFG = cfa.cfg(f) cfa.ssa(f, CFG) assert len(f.blocks) == 4 blocks = list(f.blocks) self.assertEqual(opcodes(blocks[0]), ['gt', 'cbranch']) self.assertEqual(opcodes(blocks[1]), ['jump']) self.assertEqual(opcodes(blocks[2]), ['jump']) self.assertEqual(opcodes(blocks[3]), ['phi', 'ret']) phi = findop(f, 'phi') iblocks, ivals = phi.args self.assertEqual(sorted(iblocks), sorted([blocks[1], blocks[2]])) self.assertEqual(len(ivals), 2)
def test_ssa(self): mod = from_c(source) f = mod.get_function('func_simple') verify(f) self.assertEqual(opcodes(f.startblock), ['alloca', 'store', 'load', 'gt', 'cbranch']) # SSA CFG = cfa.cfg(f) cfa.ssa(f, CFG) assert len(f.blocks) == 4 blocks = list(f.blocks) self.assertEqual(opcodes(blocks[0]), ['gt', 'cbranch']) self.assertEqual(opcodes(blocks[1]), ['jump']) self.assertEqual(opcodes(blocks[2]), ['jump']) self.assertEqual(opcodes(blocks[3]), ['phi', 'convert', 'ret']) phi = findop(f, 'phi') iblocks, ivals = phi.args self.assertEqual(sorted(iblocks), sorted([blocks[1], blocks[2]])) self.assertEqual(len(ivals), 2)
def test_exc_rewrite(self): func = Function("foo", [], types.Function(types.Void, (), False)) entry = func.new_block("entry") catch_block = func.new_block("catch") b = Builder(func) with b.at_front(entry): b.exc_setup([catch_block]) b.exc_throw(Const(StopIteration, types.Exception)) with b.at_front(catch_block): b.exc_catch([Const(Exception, types.Exception)]) local_exceptions.run(func, {}) self.assertNotIn('exc_throw', opcodes(func))
def test_inline(self): simple = """ #include <pykit_ir.h> int callee(int i) { return i * i; } int caller(int i) { int x = call(callee, list(i)); return x; } """ mod = from_c(simple) func = mod.get_function("caller") [callsite] = findallops(func, 'call') inline.inline(func, callsite) cfa.run(func) verify(func) assert interp.run(func, args=[10]) == 100 assert len(list(func.blocks)) == 1 assert opcodes(func) == ['mul', 'ret']
def test_inline(self): simple = textwrap.dedent(""" #include <pykit_ir.h> int callee(int i) { return i * i; } int caller(int i) { int x = call(callee, list(i)); return x; } """) mod = from_c(simple) func = mod.get_function("caller") [callsite] = findallops(func, 'call') inline.inline(func, callsite) cfa.run(func) verify(func) assert interp.run(func, args=[10]) == 100 assert len(list(func.blocks)) == 1 assert opcodes(func) == ['mul', 'ret']
def test_inline(self): simple = textwrap.dedent(""" #include <pykit_ir.h> int callee(int i) { return i * i; } int caller(int i) { int x = call(callee, list(i)); return x; } """) mod = from_c(simple) func = mod.get_function("caller") [callsite] = findallops(func, 'call') inline.inline(func, callsite) cfa.run(func) verify(func) assert interp.run(func, args=[10]) == 100 assert len(list(func.blocks)) == 1 self.assertEqual([o for o in opcodes(func) if o != 'convert'], ['mul', 'ret'])
def test_normalize(self): mod = from_c(testfunc) func = mod.get_function("func") ret.run(func, fresh_env()) ops = opcodes(func) self.assertEqual(ops.count("ret"), 1, ops)