Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Make dummy decl names unique #49

Merged
merged 1 commit into from
Oct 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions guppy/hugr/hugr.py
Original file line number Diff line number Diff line change
Expand Up @@ -632,10 +632,16 @@ def remove_dummy_nodes(self) -> "Hugr":
"""Replaces dummy ops with external function calls."""
if self.root is None:
raise ValueError("Dummy node removal requires a module root node")
used_names: dict[str, int] = {}
for n in list(self.nodes()):
if isinstance(n, VNode) and isinstance(n.op, ops.DummyOp):
name = n.op.name
fun_ty = FunctionType(list(n.in_port_types), list(n.out_port_types))
if name in used_names:
used_names[name] += 1
name = f"{name}${used_names[name]}"
else:
used_names[name] = 0
decl = self.add_declare(fun_ty, self.root, name)
n.op = ops.Call()
self.add_edge(decl.out_port(0), n.add_in_port(fun_ty))
Expand Down
41 changes: 41 additions & 0 deletions tests/hugr/test_dummy_nodes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import pytest

from guppy.error import UndefinedPort, InternalGuppyError
from guppy.guppy_types import FunctionType
from guppy.hugr import ops
from guppy.hugr.hugr import Hugr
from guppy.prelude.builtin import BoolType, IntType


def test_single_dummy():
g = Hugr()
defn = g.add_def(FunctionType([IntType()], [IntType()]), g.root, "test")
dfg = g.add_dfg(defn)
inp = g.add_input([IntType()], dfg).out_port(0)
dummy = g.add_node(
ops.DummyOp(name="dummy"), inputs=[inp], output_types=[IntType()], parent=dfg
)
g.add_output([dummy.out_port(0)], parent=dfg)

g.remove_dummy_nodes()
[decl] = [n for n in g.nodes() if isinstance(n.op, ops.FuncDecl)]
assert decl.op.name == "dummy"


def test_unique_names():
g = Hugr()
defn = g.add_def(FunctionType([IntType()], [IntType(), BoolType]), g.root, "test")
dfg = g.add_dfg(defn)
inp = g.add_input([IntType()], dfg).out_port(0)
dummy1 = g.add_node(
ops.DummyOp(name="dummy"), inputs=[inp], output_types=[IntType()], parent=dfg
)
dummy2 = g.add_node(
ops.DummyOp(name="dummy"), inputs=[inp], output_types=[BoolType()], parent=dfg
)
g.add_output([dummy1.out_port(0), dummy2.out_port(0)], parent=dfg)

g.remove_dummy_nodes()
[decl1, decl2] = [n for n in g.nodes() if isinstance(n.op, ops.FuncDecl)]
assert {decl1.op.name, decl2.op.name} == {"dummy", "dummy$1"}