def test_sccp_dead_loop(self): simple = textwrap.dedent(""" #include <pykit_ir.h> Int32 f(Int32 i) { Int32 x, y, z; x = 2; y = 3; z = 4; while (y < x) { if (y < x) { x = 1; x = x + 1; y = x - z; } } return x + z; } """) mod = from_c(simple) f = mod.get_function("f") cfa.run(f) sccp.run(f) verify(f) verify(f) ops = list(f.ops) assert len(list(f.ops)) == 1 [op] = ops assert op.opcode == 'ret' assert isinstance(op.args[0], Const) self.assertEqual(op.args[0].const, 6)
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_sccp_endless_loop(self): simple = textwrap.dedent(""" #include <pykit_ir.h> Int32 f(Int32 i) { Int32 x, y, z; x = 2; y = 3; z = 4; while (x < y) { if (x < y) { x = 2; } } return x + z; } """) mod = from_c(simple) f = mod.get_function("f") cfa.run(f) sccp.run(f) verify(f) assert len(f.blocks) == 2 start, loop = f.blocks assert start.terminator.opcode == 'jump' assert start.terminator.args[0] == loop assert loop.terminator.opcode == 'jump' assert loop.terminator.args[0] == loop
def test_inline2(self): harder = textwrap.dedent(""" int callee(int i) { (void) print(i); while (i < 10) { i = i + 1; return i * 2; } return i; } int caller() { int x = 4; while (x < 10) { (void) print(x); x = call(callee, list(x)); } return x; } """) mod = from_c(harder) func = mod.get_function("caller") verify(func) result = interp.run(func) [callsite] = findallops(func, 'call') inline.inline(func, callsite) cfa.run(func) verify(func)
def test_swap(self): simple = textwrap.dedent(""" #include <pykit_ir.h> int f(int i) { int x = 1; int y = 2; int tmp; while (i > 0) { tmp = x; x = y; y = tmp; i = i - 1; } return x; } """) mod = from_c(simple) func = mod.get_function("f") cfa.run(func) verify(func) ssa_result1 = interp.run(func, args=[1]) ssa_result2 = interp.run(func, args=[2]) reg2mem.reg2mem(func) verify(func) stack_result1 = interp.run(func, args=[1]) stack_result2 = interp.run(func, args=[2]) self.assertEqual(ssa_result1, stack_result1) self.assertEqual(ssa_result2, stack_result2)
def test_inline2(self): harder = """ int callee(int i) { (void) print(i); while (i < 10) { i = i + 1; return i * 2; } return i; } int caller() { int x = 4; while (x < 10) { (void) print(x); x = call(callee, list(x)); } return x; } """ mod = from_c(harder) func = mod.get_function("caller") verify(func) result = interp.run(func) [callsite] = findallops(func, 'call') inline.inline(func, callsite) cfa.run(func) verify(func)
def test_inline2(self): harder = textwrap.dedent(""" int callee(int i) { (void) print(i); while (i < 10) { i = i + 1; return i * 2; } return i; } int caller() { int x = 4; while (x < 10) { (void) print(x); x = call(callee, list(x)); } return x; } """) mod = from_c(harder) func = mod.get_function("caller") verify(func) result = interp.run(func) [callsite] = findallops(func, 'call') inline.inline(func, callsite) cfa.run(func) verify(func) # TODO: update phi when splitting blocks result2 = interp.run(func) assert result == result2
def test_sccp(self): simple = textwrap.dedent(""" #include <pykit_ir.h> Int32 f(Int32 i) { Int32 x, y, z; x = 2; y = 3; z = 4; if (x < y) x = y; else x = i; return x + z; } """) mod = from_c(simple) f = mod.get_function("f") cfa.run(f) sccp.run(f) verify(f) ops = list(f.ops) assert len(list(f.ops)) == 1 [op] = ops assert op.opcode == 'ret' assert isinstance(op.args[0], Const) self.assertEqual(op.args[0].const, 7)
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_swap_loop(self): simple = textwrap.dedent(""" #include <pykit_ir.h> int f(int i, int j) { int x = 1; int y = 2; int tmp; while (i > 0) { while (j > 0) { tmp = x; x = y; y = tmp; j = j - 1; } i = i - 1; } return x; } """) mod = from_c(simple) func = mod.get_function("f") cfa.run(func) verify(func) ssa_results = [] for i in range(10): for j in range(10): ssa_result = interp.run(func, args=[i, j]) ssa_results.append(ssa_result) #print(func) reg2mem.reg2mem(func) verify(func) #print(func) stack_results = [] for i in range(10): for j in range(10): stack_result = interp.run(func, args=[i, j]) stack_results.append(stack_result)
def pykitcompile(source, funcname=None, cfanalyze=True): m = from_c(source) verify(m) if cfanalyze: for function in m.functions.values(): cfa.run(function) verify(function) verify(m) if len(m.functions) == 1: funcname, = m.functions if funcname: f = m.get_function(funcname) b = Builder(f) entry = f.startblock else: f, b, entry = None, None, None env = environment.fresh_env() return State(m, f, b, entry, env)
def test_ops(self): source = textwrap.dedent(""" #include <pykit_ir.h> Int32 f(Int32 i) { Int32 x; x = 2; if (x < 3) x = x + 1; if (x <= 3) x = x + 1; if (x >= 3) x = x + 1; if (x >= 3) x = x + 1; if (x > 3) x = x + 1; return x; } """) mod = from_c(source) f = mod.get_function("f") remove_convert(f) cfa.run(f) sccp.run(f) verify(f) # print(f) ops = list(f.ops) assert len(list(f.ops)) == 1 [op] = ops assert op.opcode == 'ret' assert isinstance(op.args[0], Const) self.assertEqual(op.args[0].const, 7)
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'])