def test_replace_exitswitch_by_constant_bug(): class X: pass def constant9(): x = X() x.n = 3 x.n = 9 return x.n def fn(): n = constant9() if n == 1: return 5 elif n == 2: return 6 elif n == 3: return 8 elif n == 4: return -123 elif n == 5: return 12973 else: return n t = TranslationContext() a = t.buildannotator() a.build_types(fn, []) rtyper = t.buildrtyper() rtyper.specialize() graph = t.graphs[0] remove_same_as(graph) merge_if_blocks_once(graph) from rpython.translator.backendopt import malloc, inline inline.auto_inlining(t, 20) malloc.remove_mallocs(t, t.graphs) from rpython.translator import simplify simplify.join_blocks(graph)
def test_switch_on_symbolic(): symb1 = CDefinedIntSymbolic("1", 1) symb2 = CDefinedIntSymbolic("2", 2) symb3 = CDefinedIntSymbolic("3", 3) def fn(x): res = 0 if x == symb1: res += x + 1 elif x == symb2: res += x + 2 elif x == symb3: res += x + 3 res += 1 return res t = TranslationContext() a = t.buildannotator() a.build_types(fn, [int]) rtyper = t.buildrtyper() rtyper.specialize() graph = t.graphs[0] remove_same_as(graph) res = merge_if_blocks_once(graph) assert not res checkgraph(graph)
def test_merge_if_blocks_bug_2(): def fn(): n = llop.same_as(lltype.Signed, 66) if n == 1: return 5 elif n == 2: return 6 elif n == 3: return 8 elif n == 4: return -123 elif n == 5: return 12973 else: return n graph, t = get_graph(fn, []) from rpython.translator.backendopt.removenoops import remove_same_as from rpython.translator.backendopt import merge_if_blocks remove_same_as(graph) merge_if_blocks.merge_if_blocks_once(graph) constant_fold_graph(graph) check_graph(graph, [], 66, t)
def test_merge_if_blocks_bug(): def fn(n): if n == 1: return 5 elif n == 2: return 6 elif n == 3: return 8 elif n == 4: return -123 elif n == 5: return 12973 else: return n graph, t = get_graph(fn, [int]) from rpython.translator.backendopt.removenoops import remove_same_as from rpython.translator.backendopt import merge_if_blocks remove_same_as(graph) merge_if_blocks.merge_if_blocks_once(graph) constant_fold_graph(graph) check_graph(graph, [4], -123, t) check_graph(graph, [9], 9, t)
def do_test_merge(fn, testvalues): t = TranslationContext() a = t.buildannotator() a.build_types(fn, [type(testvalues[0])]) rtyper = t.buildrtyper() rtyper.specialize() graph = tgraphof(t, fn) assert len(list(graph.iterblocks())) == 4 #startblock, blocks, returnblock remove_same_as(graph) merge_if_blocks_once(graph) assert len(graph.startblock.exits) == 4 assert len(list(graph.iterblocks())) == 2 #startblock, returnblock interp = LLInterpreter(rtyper) for i in testvalues: expected = fn(i) actual = interp.eval_graph(graph, [i]) assert actual == expected
def test_merge_passonvars(): def merge(n, m): if n == 1: return m + 1 elif n == 2: return m + 2 elif n == 3: return m + 3 return m + 4 t = TranslationContext() a = t.buildannotator() a.build_types(merge, [int, int]) rtyper = t.buildrtyper() rtyper.specialize() graph = tgraphof(t, merge) assert len(list(graph.iterblocks())) == 8 remove_same_as(graph) merge_if_blocks_once(graph) assert len(graph.startblock.exits) == 4 interp = LLInterpreter(rtyper) for i in range(1, 5): res = interp.eval_graph(graph, [i, 1]) assert res == i + 1