@@ -627,41 +627,47 @@ impl<'a> Context<'a> {
627627 let children = self . hugr . children ( node) ;
628628 let mut region_children = BumpVec :: with_capacity_in ( children. size_hint ( ) . 0 - 2 , self . bump ) ;
629629
630- let mut output_node = None ;
631-
632630 for child in children {
633631 match self . hugr . get_optype ( child) {
634632 OpType :: Input ( input) => {
635633 sources = self . make_ports ( child, Direction :: Outgoing , input. types . len ( ) ) ;
636634 input_types = Some ( & input. types ) ;
635+
636+ if has_order_edges ( & self . hugr , child) {
637+ let key = self . make_term ( model:: Literal :: Nat ( child. index ( ) as u64 ) . into ( ) ) ;
638+ meta. push ( self . make_term_apply ( model:: ORDER_HINT_INPUT_KEY , & [ key] ) ) ;
639+ }
637640 }
638641 OpType :: Output ( output) => {
639642 targets = self . make_ports ( child, Direction :: Incoming , output. types . len ( ) ) ;
640643 output_types = Some ( & output. types ) ;
641- output_node = Some ( child) ;
644+
645+ if has_order_edges ( & self . hugr , child) {
646+ let key = self . make_term ( model:: Literal :: Nat ( child. index ( ) as u64 ) . into ( ) ) ;
647+ meta. push ( self . make_term_apply ( model:: ORDER_HINT_OUTPUT_KEY , & [ key] ) ) ;
648+ }
642649 }
643- child_optype => {
650+ _ => {
644651 if let Some ( child_id) = self . export_node_shallow ( child) {
645652 region_children. push ( child_id) ;
646-
647- // Record all order edges that originate from this node in metadata.
648- let successors = child_optype
649- . other_output_port ( )
650- . into_iter ( )
651- . flat_map ( |port| self . hugr . linked_inputs ( child, port) )
652- . map ( |( successor, _) | successor)
653- . filter ( |successor| Some ( * successor) != output_node) ;
654-
655- for successor in successors {
656- let a =
657- self . make_term ( model:: Literal :: Nat ( child. index ( ) as u64 ) . into ( ) ) ;
658- let b = self
659- . make_term ( model:: Literal :: Nat ( successor. index ( ) as u64 ) . into ( ) ) ;
660- meta. push ( self . make_term_apply ( model:: ORDER_HINT_ORDER , & [ a, b] ) ) ;
661- }
662653 }
663654 }
664655 }
656+
657+ // Record all order edges that originate from this node in metadata.
658+ let successors = self
659+ . hugr
660+ . get_optype ( child)
661+ . other_output_port ( )
662+ . into_iter ( )
663+ . flat_map ( |port| self . hugr . linked_inputs ( child, port) )
664+ . map ( |( successor, _) | successor) ;
665+
666+ for successor in successors {
667+ let a = self . make_term ( model:: Literal :: Nat ( child. index ( ) as u64 ) . into ( ) ) ;
668+ let b = self . make_term ( model:: Literal :: Nat ( successor. index ( ) as u64 ) . into ( ) ) ;
669+ meta. push ( self . make_term_apply ( model:: ORDER_HINT_ORDER , & [ a, b] ) ) ;
670+ }
665671 }
666672
667673 for child_id in & region_children {
@@ -1100,21 +1106,7 @@ impl<'a> Context<'a> {
11001106 }
11011107
11021108 fn export_node_order_metadata ( & mut self , node : Node , meta : & mut Vec < table:: TermId > ) {
1103- fn is_relevant_node ( hugr : & Hugr , node : Node ) -> bool {
1104- let optype = hugr. get_optype ( node) ;
1105- !optype. is_input ( ) && !optype. is_output ( )
1106- }
1107-
1108- let optype = self . hugr . get_optype ( node) ;
1109-
1110- let has_order_edges = Direction :: BOTH
1111- . iter ( )
1112- . filter ( |dir| optype. other_port_kind ( * * dir) == Some ( EdgeKind :: StateOrder ) )
1113- . filter_map ( |dir| optype. other_port ( * dir) )
1114- . flat_map ( |port| self . hugr . linked_ports ( node, port) )
1115- . any ( |( other, _) | is_relevant_node ( self . hugr , other) ) ;
1116-
1117- if has_order_edges {
1109+ if has_order_edges ( & self . hugr , node) {
11181110 let key = self . make_term ( model:: Literal :: Nat ( node. index ( ) as u64 ) . into ( ) ) ;
11191111 meta. push ( self . make_term_apply ( model:: ORDER_HINT_KEY , & [ key] ) ) ;
11201112 }
@@ -1229,6 +1221,18 @@ impl Links {
12291221 }
12301222}
12311223
1224+ /// Returns `true` if a node has any incident order edges.
1225+ fn has_order_edges ( hugr : & Hugr , node : Node ) -> bool {
1226+ let optype = hugr. get_optype ( node) ;
1227+ Direction :: BOTH
1228+ . iter ( )
1229+ . filter ( |dir| optype. other_port_kind ( * * dir) == Some ( EdgeKind :: StateOrder ) )
1230+ . filter_map ( |dir| optype. other_port ( * dir) )
1231+ . flat_map ( |port| hugr. linked_ports ( node, port) )
1232+ . next ( )
1233+ . is_some ( )
1234+ }
1235+
12321236#[ cfg( test) ]
12331237mod test {
12341238 use rstest:: { fixture, rstest} ;
0 commit comments