diff --git a/src/copapy/_basic_types.py b/src/copapy/_basic_types.py index f03e259..3f551fe 100644 --- a/src/copapy/_basic_types.py +++ b/src/copapy/_basic_types.py @@ -336,7 +336,7 @@ class CPConstant(Node): self.dtype, self.value = _get_data_and_dtype(value) self.name = 'const_' + self.dtype self.args = tuple() - self.node_hash = hash(value) if constant else id(self) + self.node_hash = hash(value) ^ hash(self.dtype) if constant else id(self) class Write(Node): diff --git a/src/copapy/_compiler.py b/src/copapy/_compiler.py index 124755c..97922bc 100644 --- a/src/copapy/_compiler.py +++ b/src/copapy/_compiler.py @@ -102,19 +102,24 @@ def get_all_dag_edges(nodes: Iterable[Node]) -> Generator[tuple[Node, Node], Non Tuples of (source_node, target_node) representing edges in the DAG """ emitted_edges: set[tuple[Node, Node]] = set() - used_nets: set[Net] = set() + used_nets: dict[Net, Net] = {} node_list: list[Node] = [n for n in nodes] while(node_list): node = node_list.pop() for net in node.args: - if net not in used_nets: - used_nets.add(net) - edge = (net.source, node) - if edge not in emitted_edges: - yield edge - node_list.append(net.source) - emitted_edges.add(edge) + + # In case there is already net with equivalent value use this + if net in used_nets: + net = used_nets[net] + else: + used_nets[net] = net + + edge = (net.source, node) + if edge not in emitted_edges: + yield edge + node_list.append(net.source) + emitted_edges.add(edge) def get_const_nets(nodes: list[Node]) -> list[Net]: