@@ -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