def _import_graph(self, graph): mng = manage(graph, weak=True) graphs = set() parents = mng.parents g = graph clone = GraphCloner(total=True, relation='cosmetic') while g: clone.add_clone(g) graphs.add(g) g = parents[g] self.graphs |= {clone[g] for g in graphs} self.focus.add(clone[graph])
def test_clone_inline(): def f(x, y): a = x * x b = y * y c = a + b return c g = parse(f) cl = GraphCloner(clone_constants=False) target = _graph_for_inline() new_params = [ONE, TWO] cl.add_clone(g, target, new_params) # We ask twice to test that this doesn't cause problems cl.add_clone(g, target, new_params) _successful_inlining(cl, g, new_params, target)
def test_clone_recursive(): def f(x, y): a = x * x b = y * y return f(a, b) g = parse(f) cl = GraphCloner(g, clone_constants=True) g2 = cl[g] d1 = set(dfs(g.return_, succ_deeper)) d2 = set(dfs(g2.return_, succ_deeper)) # Both node sets should be disjoint assert d1 & d2 == set() # Now test inlining cl2 = GraphCloner(clone_constants=True) target = _graph_for_inline() new_params = [ONE, TWO] cl2.add_clone(g, target, new_params) _successful_inlining(cl2, g, new_params, target) # The recursive call still refers to the original graph new_nodes = set(dfs(cl2[g.output], succ_deeper)) assert any(node.value is g for node in new_nodes) # Now test that inlining+total will fail cl2 = GraphCloner(total=True, clone_constants=True) target = _graph_for_inline() new_params = [ONE, TWO] with pytest.raises(Exception): cl2.add_clone(g, target, new_params) cl2[g.output]