Skip to content

Commit aeb5d00

Browse files
committed
Export order hints in Python.
1 parent e489a13 commit aeb5d00

File tree

3 files changed

+33
-27
lines changed

3 files changed

+33
-27
lines changed

hugr-core/src/export.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -633,7 +633,7 @@ impl<'a> Context<'a> {
633633
sources = self.make_ports(child, Direction::Outgoing, input.types.len());
634634
input_types = Some(&input.types);
635635

636-
if has_order_edges(&self.hugr, child) {
636+
if has_order_edges(self.hugr, child) {
637637
let key = self.make_term(model::Literal::Nat(child.index() as u64).into());
638638
meta.push(self.make_term_apply(model::ORDER_HINT_INPUT_KEY, &[key]));
639639
}
@@ -642,7 +642,7 @@ impl<'a> Context<'a> {
642642
targets = self.make_ports(child, Direction::Incoming, output.types.len());
643643
output_types = Some(&output.types);
644644

645-
if has_order_edges(&self.hugr, child) {
645+
if has_order_edges(self.hugr, child) {
646646
let key = self.make_term(model::Literal::Nat(child.index() as u64).into());
647647
meta.push(self.make_term_apply(model::ORDER_HINT_OUTPUT_KEY, &[key]));
648648
}
@@ -1106,7 +1106,7 @@ impl<'a> Context<'a> {
11061106
}
11071107

11081108
fn export_node_order_metadata(&mut self, node: Node, meta: &mut Vec<table::TermId>) {
1109-
if has_order_edges(&self.hugr, node) {
1109+
if has_order_edges(self.hugr, node) {
11101110
let key = self.make_term(model::Literal::Nat(node.index() as u64).into());
11111111
meta.push(self.make_term_apply(model::ORDER_HINT_KEY, &[key]));
11121112
}

hugr-core/src/import.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -654,7 +654,7 @@ impl<'a> Context<'a> {
654654

655655
// Check that the node has order ports.
656656
// NOTE: This assumes that a node has an input order port iff it has an output one.
657-
if !child_optype.other_output_port().is_some() {
657+
if child_optype.other_output_port().is_none() {
658658
return Err(OrderHintError::NoOrderPort(*child_id).into());
659659
}
660660

hugr-py/src/hugr/model/export.py

Lines changed: 29 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ def export_node(
7171
meta = self.export_json_meta(node)
7272

7373
# Add an order hint key to the node if necessary
74-
if _needs_order_key(self.hugr, node):
74+
if _has_order_links(self.hugr, node):
7575
meta.append(model.Apply("core.order_hint.key", [model.Literal(node.idx)]))
7676

7777
match node_data.op:
@@ -411,13 +411,27 @@ def export_region_dfg(self, node: Node) -> model.Region:
411411
for i in range(child_data._num_outs)
412412
]
413413

414+
if _has_order_links(self.hugr, child):
415+
meta.append(
416+
model.Apply(
417+
"core.order_hint.input_key", [model.Literal(child.idx)]
418+
)
419+
)
420+
414421
case Output() as op:
415422
target_types = model.List([type.to_model() for type in op.types])
416423
targets = [
417424
self.link_name(InPort(child, i))
418425
for i in range(child_data._num_inps)
419426
]
420427

428+
if _has_order_links(self.hugr, child):
429+
meta.append(
430+
model.Apply(
431+
"core.order_hint.output_key", [model.Literal(child.idx)]
432+
)
433+
)
434+
421435
case _:
422436
child_node = self.export_node(child)
423437

@@ -426,14 +440,13 @@ def export_region_dfg(self, node: Node) -> model.Region:
426440

427441
children.append(child_node)
428442

429-
meta += [
430-
model.Apply(
431-
"core.order_hint.order",
432-
[model.Literal(child.idx), model.Literal(successor.idx)],
433-
)
434-
for successor in self.hugr.outgoing_order_links(child)
435-
if not isinstance(self.hugr[successor].op, Output)
436-
]
443+
meta += [
444+
model.Apply(
445+
"core.order_hint.order",
446+
[model.Literal(child.idx), model.Literal(successor.idx)],
447+
)
448+
for successor in self.hugr.outgoing_order_links(child)
449+
]
437450

438451
signature = model.Apply("core.fn", [source_types, target_types])
439452

@@ -618,19 +631,12 @@ def union(self, a: T, b: T):
618631
self.sizes[a] += self.sizes[b]
619632

620633

621-
def _needs_order_key(hugr: Hugr, node: Node) -> bool:
622-
"""Checks whether the node has any order links for the purposes of
623-
exporting order hint metadata. Order links to `Input` or `Output`
624-
operations are ignored, since they are not present in the model format.
625-
"""
626-
for succ in hugr.outgoing_order_links(node):
627-
succ_op = hugr[succ].op
628-
if not isinstance(succ_op, Output):
629-
return True
630-
631-
for pred in hugr.incoming_order_links(node):
632-
pred_op = hugr[pred].op
633-
if not isinstance(pred_op, Input):
634-
return True
634+
def _has_order_links(hugr: Hugr, node: Node) -> bool:
635+
"""Checks whether the node has any order links."""
636+
for _succ in hugr.outgoing_order_links(node):
637+
return True
638+
639+
for _pred in hugr.incoming_order_links(node):
640+
return True
635641

636642
return False

0 commit comments

Comments
 (0)