Skip to main content

dfir_lang/graph/
meta_graph.rs

1#![warn(missing_docs)]
2
3extern crate proc_macro;
4
5use std::collections::{BTreeMap, BTreeSet};
6use std::fmt::Debug;
7use std::iter::FusedIterator;
8
9use itertools::Itertools;
10use proc_macro2::{Ident, Literal, Span, TokenStream};
11use quote::{ToTokens, format_ident, quote, quote_spanned};
12use serde::{Deserialize, Serialize};
13use slotmap::{Key, SecondaryMap, SlotMap, SparseSecondaryMap};
14use syn::spanned::Spanned;
15
16use super::graph_write::{Dot, GraphWrite, Mermaid};
17use super::ops::{
18    DelayType, OPERATORS, OperatorWriteOutput, WriteContextArgs, find_op_op_constraints,
19    null_write_iterator_fn,
20};
21use super::{
22    CONTEXT, Color, DiMulGraph, GRAPH, GraphEdgeId, GraphLoopId, GraphNode, GraphNodeId,
23    GraphSubgraphId, HANDOFF_NODE_STR, MODULE_BOUNDARY_NODE_STR, OperatorInstance, PortIndexValue,
24    Varname, change_spans, get_operator_generics,
25};
26use crate::diagnostic::{Diagnostic, Diagnostics, Level};
27use crate::pretty_span::{PrettyRowCol, PrettySpan};
28use crate::process_singletons;
29
30/// An abstract "meta graph" representation of a DFIR graph.
31///
32/// Can be with or without subgraph partitioning, stratification, and handoff insertion. This is
33/// the meta graph used for generating Rust source code in macros from DFIR sytnax.
34///
35/// This struct has a lot of methods for manipulating the graph, vaguely grouped together in
36/// separate `impl` blocks. You might notice a few particularly specific arbitray-seeming methods
37/// in here--those are just what was needed for the compilation algorithms. If you need another
38/// method then add it.
39#[derive(Default, Debug, Serialize, Deserialize)]
40pub struct DfirGraph {
41    /// Each node type (operator or handoff).
42    nodes: SlotMap<GraphNodeId, GraphNode>,
43
44    /// Instance data corresponding to each operator node.
45    /// This field will be empty after deserialization.
46    #[serde(skip)]
47    operator_instances: SecondaryMap<GraphNodeId, OperatorInstance>,
48    /// Debugging/tracing tag for each operator node.
49    operator_tag: SecondaryMap<GraphNodeId, String>,
50    /// Graph data structure (two-way adjacency list).
51    graph: DiMulGraph<GraphNodeId, GraphEdgeId>,
52    /// Input and output port for each edge.
53    ports: SecondaryMap<GraphEdgeId, (PortIndexValue, PortIndexValue)>,
54
55    /// Which loop a node belongs to (or none for top-level).
56    node_loops: SecondaryMap<GraphNodeId, GraphLoopId>,
57    /// Which nodes belong to each loop.
58    loop_nodes: SlotMap<GraphLoopId, Vec<GraphNodeId>>,
59    /// For the loop, what is its parent (`None` for top-level).
60    loop_parent: SparseSecondaryMap<GraphLoopId, GraphLoopId>,
61    /// What loops are at the root.
62    root_loops: Vec<GraphLoopId>,
63    /// For the loop, what are its child loops.
64    loop_children: SecondaryMap<GraphLoopId, Vec<GraphLoopId>>,
65
66    /// Which subgraph each node belongs to.
67    node_subgraph: SecondaryMap<GraphNodeId, GraphSubgraphId>,
68
69    /// Which nodes belong to each subgraph.
70    subgraph_nodes: SlotMap<GraphSubgraphId, Vec<GraphNodeId>>,
71
72    /// Resolved singletons varnames references, per node.
73    node_singleton_references: SparseSecondaryMap<GraphNodeId, Vec<Option<GraphNodeId>>>,
74    /// What variable name each graph node belongs to (if any). For debugging (graph writing) purposes only.
75    node_varnames: SparseSecondaryMap<GraphNodeId, Varname>,
76
77    /// Delay type for handoff nodes that represent tick-boundary back-edges.
78    /// Set by `order_subgraphs` for `defer_tick` / `defer_tick_lazy`, either on handoff nodes
79    /// it injects or on existing handoff nodes that it marks as tick-boundary back-edges.
80    handoff_delay_type: SparseSecondaryMap<GraphNodeId, DelayType>,
81}
82
83/// Basic methods.
84impl DfirGraph {
85    /// Create a new empty graph.
86    pub fn new() -> Self {
87        Default::default()
88    }
89}
90
91/// Node methods.
92impl DfirGraph {
93    /// Get a node with its operator instance (if applicable).
94    pub fn node(&self, node_id: GraphNodeId) -> &GraphNode {
95        self.nodes.get(node_id).expect("Node not found.")
96    }
97
98    /// Get the `OperatorInstance` for a given node. Node must be an operator and have an
99    /// `OperatorInstance` present, otherwise will return `None`.
100    ///
101    /// Note that no operator instances will be persent after deserialization.
102    pub fn node_op_inst(&self, node_id: GraphNodeId) -> Option<&OperatorInstance> {
103        self.operator_instances.get(node_id)
104    }
105
106    /// Get the debug variable name attached to a graph node.
107    pub fn node_varname(&self, node_id: GraphNodeId) -> Option<&Varname> {
108        self.node_varnames.get(node_id)
109    }
110
111    /// Get subgraph for node.
112    pub fn node_subgraph(&self, node_id: GraphNodeId) -> Option<GraphSubgraphId> {
113        self.node_subgraph.get(node_id).copied()
114    }
115
116    /// Degree into a node, i.e. the number of predecessors.
117    pub fn node_degree_in(&self, node_id: GraphNodeId) -> usize {
118        self.graph.degree_in(node_id)
119    }
120
121    /// Degree out of a node, i.e. the number of successors.
122    pub fn node_degree_out(&self, node_id: GraphNodeId) -> usize {
123        self.graph.degree_out(node_id)
124    }
125
126    /// Successors, iterator of `(GraphEdgeId, GraphNodeId)` of outgoing edges.
127    pub fn node_successors(
128        &self,
129        src: GraphNodeId,
130    ) -> impl '_
131    + DoubleEndedIterator<Item = (GraphEdgeId, GraphNodeId)>
132    + ExactSizeIterator
133    + FusedIterator
134    + Clone
135    + Debug {
136        self.graph.successors(src)
137    }
138
139    /// Predecessors, iterator of `(GraphEdgeId, GraphNodeId)` of incoming edges.
140    pub fn node_predecessors(
141        &self,
142        dst: GraphNodeId,
143    ) -> impl '_
144    + DoubleEndedIterator<Item = (GraphEdgeId, GraphNodeId)>
145    + ExactSizeIterator
146    + FusedIterator
147    + Clone
148    + Debug {
149        self.graph.predecessors(dst)
150    }
151
152    /// Successor edges, iterator of `GraphEdgeId` of outgoing edges.
153    pub fn node_successor_edges(
154        &self,
155        src: GraphNodeId,
156    ) -> impl '_
157    + DoubleEndedIterator<Item = GraphEdgeId>
158    + ExactSizeIterator
159    + FusedIterator
160    + Clone
161    + Debug {
162        self.graph.successor_edges(src)
163    }
164
165    /// Predecessor edges, iterator of `GraphEdgeId` of incoming edges.
166    pub fn node_predecessor_edges(
167        &self,
168        dst: GraphNodeId,
169    ) -> impl '_
170    + DoubleEndedIterator<Item = GraphEdgeId>
171    + ExactSizeIterator
172    + FusedIterator
173    + Clone
174    + Debug {
175        self.graph.predecessor_edges(dst)
176    }
177
178    /// Successor nodes, iterator of `GraphNodeId`.
179    pub fn node_successor_nodes(
180        &self,
181        src: GraphNodeId,
182    ) -> impl '_
183    + DoubleEndedIterator<Item = GraphNodeId>
184    + ExactSizeIterator
185    + FusedIterator
186    + Clone
187    + Debug {
188        self.graph.successor_vertices(src)
189    }
190
191    /// Predecessor nodes, iterator of `GraphNodeId`.
192    pub fn node_predecessor_nodes(
193        &self,
194        dst: GraphNodeId,
195    ) -> impl '_
196    + DoubleEndedIterator<Item = GraphNodeId>
197    + ExactSizeIterator
198    + FusedIterator
199    + Clone
200    + Debug {
201        self.graph.predecessor_vertices(dst)
202    }
203
204    /// Iterator of node IDs `GraphNodeId`.
205    pub fn node_ids(&self) -> slotmap::basic::Keys<'_, GraphNodeId, GraphNode> {
206        self.nodes.keys()
207    }
208
209    /// Iterator over `(GraphNodeId, &Node)` pairs.
210    pub fn nodes(&self) -> slotmap::basic::Iter<'_, GraphNodeId, GraphNode> {
211        self.nodes.iter()
212    }
213
214    /// Insert a node, assigning the given varname.
215    pub fn insert_node(
216        &mut self,
217        node: GraphNode,
218        varname_opt: Option<Ident>,
219        loop_opt: Option<GraphLoopId>,
220    ) -> GraphNodeId {
221        let node_id = self.nodes.insert(node);
222        if let Some(varname) = varname_opt {
223            self.node_varnames.insert(node_id, Varname(varname));
224        }
225        if let Some(loop_id) = loop_opt {
226            self.node_loops.insert(node_id, loop_id);
227            self.loop_nodes[loop_id].push(node_id);
228        }
229        node_id
230    }
231
232    /// Insert an operator instance for the given node. Panics if already set.
233    pub fn insert_node_op_inst(&mut self, node_id: GraphNodeId, op_inst: OperatorInstance) {
234        assert!(matches!(
235            self.nodes.get(node_id),
236            Some(GraphNode::Operator(_))
237        ));
238        let old_inst = self.operator_instances.insert(node_id, op_inst);
239        assert!(old_inst.is_none());
240    }
241
242    /// Assign all operator instances if not set. Write diagnostic messages/errors into `diagnostics`.
243    pub fn insert_node_op_insts_all(&mut self, diagnostics: &mut Diagnostics) {
244        let mut op_insts = Vec::new();
245        for (node_id, node) in self.nodes() {
246            let GraphNode::Operator(operator) = node else {
247                continue;
248            };
249            if self.node_op_inst(node_id).is_some() {
250                continue;
251            };
252
253            // Op constraints.
254            let Some(op_constraints) = find_op_op_constraints(operator) else {
255                diagnostics.push(Diagnostic::spanned(
256                    operator.path.span(),
257                    Level::Error,
258                    format!("Unknown operator `{}`", operator.name_string()),
259                ));
260                continue;
261            };
262
263            // Input and output ports.
264            let (input_ports, output_ports) = {
265                let mut input_edges: Vec<(&PortIndexValue, GraphNodeId)> = self
266                    .node_predecessors(node_id)
267                    .map(|(edge_id, pred_id)| (self.edge_ports(edge_id).1, pred_id))
268                    .collect();
269                // Ensure sorted by port index.
270                input_edges.sort();
271                let input_ports: Vec<PortIndexValue> = input_edges
272                    .into_iter()
273                    .map(|(port, _pred)| port)
274                    .cloned()
275                    .collect();
276
277                // Collect output arguments (successors).
278                let mut output_edges: Vec<(&PortIndexValue, GraphNodeId)> = self
279                    .node_successors(node_id)
280                    .map(|(edge_id, succ)| (self.edge_ports(edge_id).0, succ))
281                    .collect();
282                // Ensure sorted by port index.
283                output_edges.sort();
284                let output_ports: Vec<PortIndexValue> = output_edges
285                    .into_iter()
286                    .map(|(port, _succ)| port)
287                    .cloned()
288                    .collect();
289
290                (input_ports, output_ports)
291            };
292
293            // Generic arguments.
294            let generics = get_operator_generics(diagnostics, operator);
295            // Generic argument errors.
296            {
297                // Span of `generic_args` (if it exists), otherwise span of the operator name.
298                let generics_span = generics
299                    .generic_args
300                    .as_ref()
301                    .map(Spanned::span)
302                    .unwrap_or_else(|| operator.path.span());
303
304                if !op_constraints
305                    .persistence_args
306                    .contains(&generics.persistence_args.len())
307                {
308                    diagnostics.push(Diagnostic::spanned(
309                        generics.persistence_args_span().unwrap_or(generics_span),
310                        Level::Error,
311                        format!(
312                            "`{}` should have {} persistence lifetime arguments, actually has {}.",
313                            op_constraints.name,
314                            op_constraints.persistence_args.human_string(),
315                            generics.persistence_args.len()
316                        ),
317                    ));
318                }
319                if !op_constraints.type_args.contains(&generics.type_args.len()) {
320                    diagnostics.push(Diagnostic::spanned(
321                        generics.type_args_span().unwrap_or(generics_span),
322                        Level::Error,
323                        format!(
324                            "`{}` should have {} generic type arguments, actually has {}.",
325                            op_constraints.name,
326                            op_constraints.type_args.human_string(),
327                            generics.type_args.len()
328                        ),
329                    ));
330                }
331            }
332
333            op_insts.push((
334                node_id,
335                OperatorInstance {
336                    op_constraints,
337                    input_ports,
338                    output_ports,
339                    singletons_referenced: operator.singletons_referenced.clone(),
340                    generics,
341                    arguments_pre: operator.args.clone(),
342                    arguments_raw: operator.args_raw.clone(),
343                },
344            ));
345        }
346
347        for (node_id, op_inst) in op_insts {
348            self.insert_node_op_inst(node_id, op_inst);
349        }
350    }
351
352    /// Inserts a node between two existing nodes connected by the given `edge_id`.
353    ///
354    /// `edge`: (src, dst, dst_idx)
355    ///
356    /// Before: A (src) ------------> B (dst)
357    /// After:  A (src) -> X (new) -> B (dst)
358    ///
359    /// Returns the ID of X & ID of edge OUT of X.
360    ///
361    /// Note that both the edges will be new and `edge_id` will be removed. Both new edges will
362    /// get the edge type of the original edge.
363    pub fn insert_intermediate_node(
364        &mut self,
365        edge_id: GraphEdgeId,
366        new_node: GraphNode,
367    ) -> (GraphNodeId, GraphEdgeId) {
368        let span = Some(new_node.span());
369
370        // Make corresponding operator instance (if `node` is an operator).
371        let op_inst_opt = 'oc: {
372            let GraphNode::Operator(operator) = &new_node else {
373                break 'oc None;
374            };
375            let Some(op_constraints) = find_op_op_constraints(operator) else {
376                break 'oc None;
377            };
378            let (input_port, output_port) = self.ports.get(edge_id).cloned().unwrap();
379
380            let mut dummy_diagnostics = Diagnostics::new();
381            let generics = get_operator_generics(&mut dummy_diagnostics, operator);
382            assert!(dummy_diagnostics.is_empty());
383
384            Some(OperatorInstance {
385                op_constraints,
386                input_ports: vec![input_port],
387                output_ports: vec![output_port],
388                singletons_referenced: operator.singletons_referenced.clone(),
389                generics,
390                arguments_pre: operator.args.clone(),
391                arguments_raw: operator.args_raw.clone(),
392            })
393        };
394
395        // Insert new `node`.
396        let node_id = self.nodes.insert(new_node);
397        // Insert corresponding `OperatorInstance` if applicable.
398        if let Some(op_inst) = op_inst_opt {
399            self.operator_instances.insert(node_id, op_inst);
400        }
401        // Update edges to insert node within `edge_id`.
402        let (e0, e1) = self
403            .graph
404            .insert_intermediate_vertex(node_id, edge_id)
405            .unwrap();
406
407        // Update corresponding ports.
408        let (src_idx, dst_idx) = self.ports.remove(edge_id).unwrap();
409        self.ports
410            .insert(e0, (src_idx, PortIndexValue::Elided(span)));
411        self.ports
412            .insert(e1, (PortIndexValue::Elided(span), dst_idx));
413
414        (node_id, e1)
415    }
416
417    /// Remove the node `node_id` but preserves and connects the single predecessor and single successor.
418    /// Panics if the node does not have exactly one predecessor and one successor, or is not in the graph.
419    pub fn remove_intermediate_node(&mut self, node_id: GraphNodeId) {
420        assert_eq!(
421            1,
422            self.node_degree_in(node_id),
423            "Removed intermediate node must have one predecessor"
424        );
425        assert_eq!(
426            1,
427            self.node_degree_out(node_id),
428            "Removed intermediate node must have one successor"
429        );
430        assert!(
431            self.node_subgraph.is_empty() && self.subgraph_nodes.is_empty(),
432            "Should not remove intermediate node after subgraph partitioning"
433        );
434
435        assert!(self.nodes.remove(node_id).is_some());
436        let (new_edge_id, (pred_edge_id, succ_edge_id)) =
437            self.graph.remove_intermediate_vertex(node_id).unwrap();
438        self.operator_instances.remove(node_id);
439        self.node_varnames.remove(node_id);
440
441        let (src_port, _) = self.ports.remove(pred_edge_id).unwrap();
442        let (_, dst_port) = self.ports.remove(succ_edge_id).unwrap();
443        self.ports.insert(new_edge_id, (src_port, dst_port));
444    }
445
446    /// Helper method: determine the "color" (pull vs push) of a node based on its in and out degree,
447    /// excluding reference edges. If linear (1 in, 1 out), color is `None`, indicating it can be
448    /// either push or pull.
449    ///
450    /// Note that this does NOT consider `DelayType` barriers (which generally implies `Pull`).
451    pub(crate) fn node_color(&self, node_id: GraphNodeId) -> Option<Color> {
452        if matches!(self.node(node_id), GraphNode::Handoff { .. }) {
453            return Some(Color::Hoff);
454        }
455
456        // TODO(shadaj): this is a horrible hack
457        if let GraphNode::Operator(op) = self.node(node_id)
458            && (op.name_string() == "resolve_futures_blocking"
459                || op.name_string() == "resolve_futures_blocking_ordered")
460        {
461            return Some(Color::Push);
462        }
463
464        // In-degree, excluding ref-edges.
465        let inn_degree = self.node_predecessor_nodes(node_id).len();
466        // Out-degree excluding ref-edges.
467        let out_degree = self.node_successor_nodes(node_id).len();
468
469        match (inn_degree, out_degree) {
470            (0, 0) => None, // Generally should not happen, "Degenerate subgraph detected".
471            (0, 1) => Some(Color::Pull),
472            (1, 0) => Some(Color::Push),
473            (1, 1) => None, // Linear, can be either push or pull.
474            (_many, 0 | 1) => Some(Color::Pull),
475            (0 | 1, _many) => Some(Color::Push),
476            (_many, _to_many) => Some(Color::Comp),
477        }
478    }
479
480    /// Set the operator tag (for debugging/tracing).
481    pub fn set_operator_tag(&mut self, node_id: GraphNodeId, tag: String) {
482        self.operator_tag.insert(node_id, tag);
483    }
484}
485
486/// Singleton references.
487impl DfirGraph {
488    /// Set the singletons referenced for the `node_id` operator. Each reference corresponds to the
489    /// same index in the [`crate::parse::Operator::singletons_referenced`] vec.
490    pub fn set_node_singleton_references(
491        &mut self,
492        node_id: GraphNodeId,
493        singletons_referenced: Vec<Option<GraphNodeId>>,
494    ) -> Option<Vec<Option<GraphNodeId>>> {
495        self.node_singleton_references
496            .insert(node_id, singletons_referenced)
497    }
498
499    /// Gets the singletons referenced by a node. Returns an empty iterator for non-operators and
500    /// operators that do not reference singletons.
501    pub fn node_singleton_references(&self, node_id: GraphNodeId) -> &[Option<GraphNodeId>] {
502        self.node_singleton_references
503            .get(node_id)
504            .map(std::ops::Deref::deref)
505            .unwrap_or_default()
506    }
507}
508
509/// Module methods.
510impl DfirGraph {
511    /// When modules are imported into a flat graph, they come with an input and output ModuleBoundary node.
512    /// The partitioner doesn't understand these nodes and will panic if it encounters them.
513    /// merge_modules removes them from the graph, stitching the input and ouput sides of the ModuleBondaries based on their ports
514    /// For example:
515    ///     source_iter([]) -> \[myport\]ModuleBoundary(input)\[my_port\] -> map(|x| x) -> ModuleBoundary(output) -> null();
516    /// in the above eaxmple, the \[myport\] port will be used to connect the source_iter with the map that is inside of the module.
517    /// The output module boundary has elided ports, this is also used to match up the input/output across the module boundary.
518    pub fn merge_modules(&mut self) -> Result<(), Diagnostic> {
519        let mod_bound_nodes = self
520            .nodes()
521            .filter(|(_nid, node)| matches!(node, GraphNode::ModuleBoundary { .. }))
522            .map(|(nid, _node)| nid)
523            .collect::<Vec<_>>();
524
525        for mod_bound_node in mod_bound_nodes {
526            self.remove_module_boundary(mod_bound_node)?;
527        }
528
529        Ok(())
530    }
531
532    /// see `merge_modules`
533    /// This function removes a singular module boundary from the graph and performs the necessary stitching to fix the graph afterward.
534    /// `merge_modules` calls this function for each module boundary in the graph.
535    fn remove_module_boundary(&mut self, mod_bound_node: GraphNodeId) -> Result<(), Diagnostic> {
536        assert!(
537            self.node_subgraph.is_empty() && self.subgraph_nodes.is_empty(),
538            "Should not remove intermediate node after subgraph partitioning"
539        );
540
541        let mut mod_pred_ports = BTreeMap::new();
542        let mut mod_succ_ports = BTreeMap::new();
543
544        for mod_out_edge in self.node_predecessor_edges(mod_bound_node) {
545            let (pred_port, succ_port) = self.edge_ports(mod_out_edge);
546            mod_pred_ports.insert(succ_port.clone(), (mod_out_edge, pred_port.clone()));
547        }
548
549        for mod_inn_edge in self.node_successor_edges(mod_bound_node) {
550            let (pred_port, succ_port) = self.edge_ports(mod_inn_edge);
551            mod_succ_ports.insert(pred_port.clone(), (mod_inn_edge, succ_port.clone()));
552        }
553
554        if mod_pred_ports.keys().collect::<BTreeSet<_>>()
555            != mod_succ_ports.keys().collect::<BTreeSet<_>>()
556        {
557            // get module boundary node
558            let GraphNode::ModuleBoundary { input, import_expr } = self.node(mod_bound_node) else {
559                panic!();
560            };
561
562            if *input {
563                return Err(Diagnostic {
564                    span: *import_expr,
565                    level: Level::Error,
566                    message: format!(
567                        "The ports into the module did not match. input: {:?}, expected: {:?}",
568                        mod_pred_ports.keys().map(|x| x.to_string()).join(", "),
569                        mod_succ_ports.keys().map(|x| x.to_string()).join(", ")
570                    ),
571                });
572            } else {
573                return Err(Diagnostic {
574                    span: *import_expr,
575                    level: Level::Error,
576                    message: format!(
577                        "The ports out of the module did not match. output: {:?}, expected: {:?}",
578                        mod_succ_ports.keys().map(|x| x.to_string()).join(", "),
579                        mod_pred_ports.keys().map(|x| x.to_string()).join(", "),
580                    ),
581                });
582            }
583        }
584
585        for (port, (pred_edge, pred_port)) in mod_pred_ports {
586            let (succ_edge, succ_port) = mod_succ_ports.remove(&port).unwrap();
587
588            let (src, _) = self.edge(pred_edge);
589            let (_, dst) = self.edge(succ_edge);
590            self.remove_edge(pred_edge);
591            self.remove_edge(succ_edge);
592
593            let new_edge_id = self.graph.insert_edge(src, dst);
594            self.ports.insert(new_edge_id, (pred_port, succ_port));
595        }
596
597        self.graph.remove_vertex(mod_bound_node);
598        self.nodes.remove(mod_bound_node);
599
600        Ok(())
601    }
602}
603
604/// Edge methods.
605impl DfirGraph {
606    /// Get the `src` and `dst` for an edge: `(src GraphNodeId, dst GraphNodeId)`.
607    pub fn edge(&self, edge_id: GraphEdgeId) -> (GraphNodeId, GraphNodeId) {
608        let (src, dst) = self.graph.edge(edge_id).expect("Edge not found.");
609        (src, dst)
610    }
611
612    /// Get the source and destination ports for an edge: `(src &PortIndexValue, dst &PortIndexValue)`.
613    pub fn edge_ports(&self, edge_id: GraphEdgeId) -> (&PortIndexValue, &PortIndexValue) {
614        let (src_port, dst_port) = self.ports.get(edge_id).expect("Edge not found.");
615        (src_port, dst_port)
616    }
617
618    /// Iterator of all edge IDs `GraphEdgeId`.
619    pub fn edge_ids(&self) -> slotmap::basic::Keys<'_, GraphEdgeId, (GraphNodeId, GraphNodeId)> {
620        self.graph.edge_ids()
621    }
622
623    /// Iterator over all edges: `(GraphEdgeId, (src GraphNodeId, dst GraphNodeId))`.
624    pub fn edges(
625        &self,
626    ) -> impl '_
627    + ExactSizeIterator<Item = (GraphEdgeId, (GraphNodeId, GraphNodeId))>
628    + FusedIterator
629    + Clone
630    + Debug {
631        self.graph.edges()
632    }
633
634    /// Insert an edge between nodes thru the given ports.
635    pub fn insert_edge(
636        &mut self,
637        src: GraphNodeId,
638        src_port: PortIndexValue,
639        dst: GraphNodeId,
640        dst_port: PortIndexValue,
641    ) -> GraphEdgeId {
642        let edge_id = self.graph.insert_edge(src, dst);
643        self.ports.insert(edge_id, (src_port, dst_port));
644        edge_id
645    }
646
647    /// Removes an edge and its corresponding ports and edge type info.
648    pub fn remove_edge(&mut self, edge: GraphEdgeId) {
649        let (_src, _dst) = self.graph.remove_edge(edge).unwrap();
650        let (_src_port, _dst_port) = self.ports.remove(edge).unwrap();
651    }
652}
653
654/// Subgraph methods.
655impl DfirGraph {
656    /// Nodes belonging to the given subgraph.
657    pub fn subgraph(&self, subgraph_id: GraphSubgraphId) -> &Vec<GraphNodeId> {
658        self.subgraph_nodes
659            .get(subgraph_id)
660            .expect("Subgraph not found.")
661    }
662
663    /// Iterator over all subgraph IDs.
664    pub fn subgraph_ids(&self) -> slotmap::basic::Keys<'_, GraphSubgraphId, Vec<GraphNodeId>> {
665        self.subgraph_nodes.keys()
666    }
667
668    /// Iterator over all subgraphs, ID and members: `(GraphSubgraphId, Vec<GraphNodeId>)`.
669    pub fn subgraphs(&self) -> slotmap::basic::Iter<'_, GraphSubgraphId, Vec<GraphNodeId>> {
670        self.subgraph_nodes.iter()
671    }
672
673    /// Create a subgraph consisting of `node_ids`. Returns an error if any of the nodes are already in a subgraph.
674    pub fn insert_subgraph(
675        &mut self,
676        node_ids: Vec<GraphNodeId>,
677    ) -> Result<GraphSubgraphId, (GraphNodeId, GraphSubgraphId)> {
678        // Check none are already in subgraphs
679        for &node_id in node_ids.iter() {
680            if let Some(&old_sg_id) = self.node_subgraph.get(node_id) {
681                return Err((node_id, old_sg_id));
682            }
683        }
684        let subgraph_id = self.subgraph_nodes.insert_with_key(|sg_id| {
685            for &node_id in node_ids.iter() {
686                self.node_subgraph.insert(node_id, sg_id);
687            }
688            node_ids
689        });
690
691        Ok(subgraph_id)
692    }
693
694    /// Removes a node from its subgraph. Returns true if the node was in a subgraph.
695    pub fn remove_from_subgraph(&mut self, node_id: GraphNodeId) -> bool {
696        if let Some(old_sg_id) = self.node_subgraph.remove(node_id) {
697            self.subgraph_nodes[old_sg_id].retain(|&other_node_id| other_node_id != node_id);
698            true
699        } else {
700            false
701        }
702    }
703
704    /// Gets the delay type for a handoff node, if set.
705    pub fn handoff_delay_type(&self, node_id: GraphNodeId) -> Option<DelayType> {
706        self.handoff_delay_type.get(node_id).copied()
707    }
708
709    /// Sets the delay type for a handoff node.
710    pub fn set_handoff_delay_type(&mut self, node_id: GraphNodeId, delay_type: DelayType) {
711        self.handoff_delay_type.insert(node_id, delay_type);
712    }
713
714    /// Helper: finds the first index in `subgraph_nodes` where it transitions from pull to push.
715    fn find_pull_to_push_idx(&self, subgraph_nodes: &[GraphNodeId]) -> usize {
716        subgraph_nodes
717            .iter()
718            .position(|&node_id| {
719                self.node_color(node_id)
720                    .is_some_and(|color| Color::Pull != color)
721            })
722            .unwrap_or(subgraph_nodes.len())
723    }
724}
725
726/// Display/output methods.
727impl DfirGraph {
728    /// Helper to generate a deterministic `Ident` for the given node.
729    fn node_as_ident(&self, node_id: GraphNodeId, is_pred: bool) -> Ident {
730        let name = match &self.nodes[node_id] {
731            GraphNode::Operator(_) => format!("op_{:?}", node_id.data()),
732            GraphNode::Handoff { .. } => format!(
733                "hoff_{:?}_{}",
734                node_id.data(),
735                if is_pred { "recv" } else { "send" }
736            ),
737            GraphNode::ModuleBoundary { .. } => panic!(),
738        };
739        let span = match (is_pred, &self.nodes[node_id]) {
740            (_, GraphNode::Operator(operator)) => operator.span(),
741            (true, &GraphNode::Handoff { src_span, .. }) => src_span,
742            (false, &GraphNode::Handoff { dst_span, .. }) => dst_span,
743            (_, GraphNode::ModuleBoundary { .. }) => panic!(),
744        };
745        Ident::new(&name, span)
746    }
747
748    /// Helper to generate the main buffer `Ident` for a handoff node.
749    fn hoff_buf_ident(&self, hoff_id: GraphNodeId, span: Span) -> Ident {
750        Ident::new(&format!("hoff_{:?}_buf", hoff_id.data()), span)
751    }
752
753    /// Helper to generate the back (double-buffer) `Ident` for a handoff node.
754    fn hoff_back_ident(&self, hoff_id: GraphNodeId, span: Span) -> Ident {
755        Ident::new(&format!("hoff_{:?}_back", hoff_id.data()), span)
756    }
757
758    /// For per-node singleton references. Helper to generate a deterministic `Ident` for the given node.
759    fn node_as_singleton_ident(&self, node_id: GraphNodeId, span: Span) -> Ident {
760        Ident::new(&format!("singleton_op_{:?}", node_id.data()), span)
761    }
762
763    /// Resolve the singletons via [`Self::node_singleton_references`] for the given `node_id`.
764    fn helper_resolve_singletons(&self, node_id: GraphNodeId, span: Span) -> Vec<Ident> {
765        self.node_singleton_references(node_id)
766            .iter()
767            .map(|singleton_node_id| {
768                // TODO(mingwei): this `expect` should be caught in error checking
769                self.node_as_singleton_ident(
770                    singleton_node_id
771                        .expect("Expected singleton to be resolved but was not, this is a bug."),
772                    span,
773                )
774            })
775            .collect::<Vec<_>>()
776    }
777
778    /// Returns each subgraph's receive and send handoffs.
779    /// `Map<GraphSubgraphId, (recv handoffs, send handoffs)>`
780    fn helper_collect_subgraph_handoffs(
781        &self,
782    ) -> SecondaryMap<GraphSubgraphId, (Vec<GraphNodeId>, Vec<GraphNodeId>)> {
783        // Get data on handoff src and dst subgraphs.
784        let mut subgraph_handoffs: SecondaryMap<
785            GraphSubgraphId,
786            (Vec<GraphNodeId>, Vec<GraphNodeId>),
787        > = self
788            .subgraph_nodes
789            .keys()
790            .map(|k| (k, Default::default()))
791            .collect();
792
793        // For each handoff node, add it to the `send`/`recv` lists for the corresponding subgraphs.
794        for (hoff_id, node) in self.nodes() {
795            if !matches!(node, GraphNode::Handoff { .. }) {
796                continue;
797            }
798            // Receivers from the handoff. (Should really only be one).
799            for (_edge, succ_id) in self.node_successors(hoff_id) {
800                let succ_sg = self.node_subgraph(succ_id).unwrap();
801                subgraph_handoffs[succ_sg].0.push(hoff_id);
802            }
803            // Senders into the handoff. (Should really only be one).
804            for (_edge, pred_id) in self.node_predecessors(hoff_id) {
805                let pred_sg = self.node_subgraph(pred_id).unwrap();
806                subgraph_handoffs[pred_sg].1.push(hoff_id);
807            }
808        }
809
810        subgraph_handoffs
811    }
812
813    /// Emit this graph as runnable Rust source code tokens that execute inline.
814    /// Generates a flat `async move |df: &mut Context|` closure where subgraph
815    /// blocks are inlined in topological order, using local `Vec<T>` buffers
816    /// instead of runtime handoffs. Each call to the closure runs one tick.
817    ///
818    /// The generated code block evaluates to a `Dfir` instance wrapping the
819    /// closure. Operator prologues run at construction time on the `Context`
820    /// before it is moved into `Dfir::new`. `Dfir` provides the `Context`
821    /// to the closure on each tick run.
822    ///
823    /// # Errors
824    ///
825    /// Returns all diagnostics as `Err(diagnostics)` if any are errors
826    /// (leaving `&mut diagnostics` empty).
827    pub fn as_code(
828        &self,
829        root: &TokenStream,
830        include_type_guards: bool,
831        prefix: TokenStream,
832        diagnostics: &mut Diagnostics,
833    ) -> Result<TokenStream, Diagnostics> {
834        self.as_code_with_options(root, include_type_guards, true, prefix, diagnostics)
835    }
836
837    /// Like [`Self::as_code`], but with `include_meta` controlling whether
838    /// the runtime meta graph + diagnostics JSON blobs are baked into the
839    /// generated `Dfir::new(...)` call.
840    ///
841    /// The simulator calls Dfir::new() on each iteration, and as a part of that
842    /// it does parsing of the metagraph and diganostics blob. One of them causes spans to get allocated,
843    /// each time a span is allocated, some threadlocal u32 is being incremented, and, on a long simulator run,
844    /// the u32 overflows and panics.
845    pub fn as_code_with_options(
846        &self,
847        root: &TokenStream,
848        include_type_guards: bool,
849        include_meta: bool,
850        prefix: TokenStream,
851        diagnostics: &mut Diagnostics,
852    ) -> Result<TokenStream, Diagnostics> {
853        let df = Ident::new(GRAPH, Span::call_site());
854        let context = Ident::new(CONTEXT, Span::call_site());
855
856        // 1. Generate local Vec buffers for each handoff node.
857        let handoff_nodes: Vec<_> = self
858            .nodes
859            .iter()
860            .filter_map(|(node_id, node)| match node {
861                GraphNode::Operator(_) => None,
862                &GraphNode::Handoff { src_span, dst_span } => Some((node_id, (src_span, dst_span))),
863                GraphNode::ModuleBoundary { .. } => panic!(),
864            })
865            .collect();
866
867        let buffer_code: Vec<TokenStream> = handoff_nodes
868            .iter()
869            .map(|&(node_id, (src_span, dst_span))| {
870                let span = src_span.join(dst_span).unwrap_or(src_span);
871                let buf_ident = self.hoff_buf_ident(node_id, span);
872                quote_spanned! {span=>
873                    let mut #buf_ident: Vec<_> = Vec::new();
874                }
875            })
876            .collect();
877
878        // For tick-boundary handoffs (`defer_tick` / `defer_tick_lazy`), declare a
879        // second "back" buffer for double-buffering. At the start of each tick, the
880        // main buffer and back buffer are swapped so the consumer reads last tick's
881        // data while the producer writes to a fresh buffer.
882        let back_buffer_code: Vec<TokenStream> = handoff_nodes
883            .iter()
884            .filter(|(node_id, _)| self.handoff_delay_type(*node_id).is_some())
885            .map(|&(node_id, (src_span, dst_span))| {
886                let span = src_span.join(dst_span).unwrap_or(src_span);
887                let back_ident = self.hoff_back_ident(node_id, span);
888                quote_spanned! {span=>
889                    let mut #back_ident: Vec<_> = Vec::new();
890                }
891            })
892            .collect();
893
894        // 2. Collect subgraph handoffs (same as as_code).
895        let subgraph_handoffs = self.helper_collect_subgraph_handoffs();
896
897        // 3. Sort subgraphs topologically and collect non-lazy defer_tick buffer idents.
898        //
899        // Handoffs marked with a `DelayType` (Tick/TickLazy) are tick-boundary back-edges.
900        // These are excluded from the topo sort (no ordering constraint). Double-buffering
901        // ensures data written by the producer in tick N is only visible to the consumer
902        // in tick N+1, regardless of execution order.
903        //
904        // While iterating handoffs, we also collect buffer idents for non-lazy tick-boundary
905        // edges (defer_tick). When these buffers are non-empty at end of tick, we set
906        // can_start_tick so that run_available continues ticking.
907        let mut defer_tick_buf_idents: Vec<Ident> = Vec::new();
908        let mut back_edge_hoff_ids: BTreeSet<GraphNodeId> = BTreeSet::new();
909        let all_subgraphs = {
910            // Build predecessor map for subgraphs.
911            let mut sg_preds = SecondaryMap::<_, Vec<_>>::with_capacity(self.subgraph_nodes.len());
912            for (hoff_id, node) in self.nodes() {
913                if !matches!(node, GraphNode::Handoff { .. }) {
914                    // Not a handoff; skip.
915                    continue;
916                }
917                assert_eq!(1, self.node_successors(hoff_id).len());
918                assert_eq!(1, self.node_predecessors(hoff_id).len());
919                let (_edge_id, pred) = self.node_predecessors(hoff_id).next().unwrap();
920                let (_edge_id, succ) = self.node_successors(hoff_id).next().unwrap();
921                let pred_sg = self.node_subgraph(pred).unwrap();
922                let succ_sg = self.node_subgraph(succ).unwrap();
923                if pred_sg == succ_sg {
924                    panic!("bug: unexpected subgraph self-handoff cycle");
925                }
926                if let Some(delay_type) = self.handoff_delay_type(hoff_id) {
927                    debug_assert!(matches!(delay_type, DelayType::Tick | DelayType::TickLazy));
928                    // Tick/back-edge handoff: no ordering constraint. Double-buffering
929                    // handles the tick deferral regardless of execution order.
930                    back_edge_hoff_ids.insert(hoff_id);
931
932                    // Non-lazy tick-boundary: defer_tick (not defer_tick_lazy).
933                    if !matches!(delay_type, DelayType::TickLazy) {
934                        defer_tick_buf_idents.push(self.hoff_buf_ident(hoff_id, node.span()));
935                    }
936                } else {
937                    sg_preds.entry(succ_sg).unwrap().or_default().push(pred_sg);
938                }
939            }
940
941            // Include singleton reference edges: if node A references the
942            // singleton output of node B, then A's subgraph must run after B's.
943            for dst_id in self.node_ids() {
944                for src_ref_id in self
945                    .node_singleton_references(dst_id)
946                    .iter()
947                    .copied()
948                    .flatten()
949                {
950                    let src_sg = self
951                        .node_subgraph(src_ref_id)
952                        .expect("bug: singleton ref node must belong to a subgraph");
953                    let dst_sg = self
954                        .node_subgraph(dst_id)
955                        .expect("bug: singleton ref consumer must belong to a subgraph");
956                    if src_sg != dst_sg {
957                        sg_preds.entry(dst_sg).unwrap().or_default().push(src_sg);
958                    }
959                }
960            }
961
962            let topo_sort = super::graph_algorithms::topo_sort(self.subgraph_ids(), |sg_id| {
963                sg_preds.get(sg_id).into_iter().flatten().copied()
964            })
965            .expect("bug: unexpected cycle between subgraphs within the tick");
966
967            topo_sort
968                .into_iter()
969                .map(|sg_id| (sg_id, self.subgraph(sg_id)))
970                .collect::<Vec<_>>()
971        };
972
973        // Generate swap code for tick-boundary (defer_tick / defer_tick_lazy) handoffs.
974        // At the start of each tick, swap the main buffer and back buffer so the
975        // consumer reads last tick's data from the back buffer.
976        let back_edge_swap_code: Vec<TokenStream> = back_edge_hoff_ids
977            .iter()
978            .map(|&hoff_id| {
979                let span = self.nodes[hoff_id].span();
980                let buf_ident = self.hoff_buf_ident(hoff_id, span);
981                let back_ident = self.hoff_back_ident(hoff_id, span);
982                quote_spanned! {span=>
983                    ::std::mem::swap(&mut #buf_ident, &mut #back_ident);
984                }
985            })
986            .collect();
987
988        let mut op_prologue_code = Vec::new();
989        let mut op_tick_end_code = Vec::new();
990        let mut subgraph_blocks = Vec::new();
991        {
992            for &(subgraph_id, subgraph_nodes) in all_subgraphs.iter() {
993                let sg_metrics_ffi = subgraph_id.data().as_ffi();
994                let (recv_hoffs, send_hoffs) = &subgraph_handoffs[subgraph_id];
995
996                // Generate buffer ident helpers for this subgraph's handoffs.
997                let recv_port_idents: Vec<Ident> = recv_hoffs
998                    .iter()
999                    .map(|&hoff_id| self.node_as_ident(hoff_id, true))
1000                    .collect();
1001                let send_port_idents: Vec<Ident> = send_hoffs
1002                    .iter()
1003                    .map(|&hoff_id| self.node_as_ident(hoff_id, false))
1004                    .collect();
1005
1006                // Map handoff node IDs to buffer idents.
1007                let recv_buf_idents: Vec<Ident> = recv_hoffs
1008                    .iter()
1009                    .map(|&hoff_id| self.hoff_buf_ident(hoff_id, self.nodes[hoff_id].span()))
1010                    .collect();
1011                let send_buf_idents: Vec<Ident> = send_hoffs
1012                    .iter()
1013                    .map(|&hoff_id| self.hoff_buf_ident(hoff_id, self.nodes[hoff_id].span()))
1014                    .collect();
1015
1016                // Recv port code: drain from buffer into iterator, tracking if non-empty.
1017                // For back-edge (defer_tick) handoffs, drain from the back buffer instead.
1018                // Also update handoff metrics (measured at recv, not send — see graph.rs).
1019                let recv_port_code: Vec<TokenStream> = recv_port_idents
1020                    .iter()
1021                    .zip(recv_buf_idents.iter())
1022                    .zip(recv_hoffs.iter())
1023                    .map(|((port_ident, buf_ident), &hoff_id)| {
1024                        let hoff_ffi = hoff_id.data().as_ffi();
1025                        // Use call_site span for internal identifiers to avoid
1026                        // hygiene issues when invoked through declarative macros
1027                        // (e.g. dfir_expect_warnings!). TODO(#2781): define these once.
1028                        let work_done = Ident::new("__dfir_work_done", Span::call_site());
1029                        let metrics = Ident::new("__dfir_metrics", Span::call_site());
1030                        // Tick-boundary handoffs drain from the back buffer (double-buffering).
1031                        // (Sending always writes to the regular buffer — no branch needed there.)
1032                        let drain_ident = if back_edge_hoff_ids.contains(&hoff_id) {
1033                            self.hoff_back_ident(hoff_id, buf_ident.span())
1034                        } else {
1035                            buf_ident.clone()
1036                        };
1037                        quote_spanned! {port_ident.span()=>
1038                            {
1039                                let hoff_len = #drain_ident.len();
1040                                if hoff_len > 0 {
1041                                    #work_done = true;
1042                                }
1043                                let hoff_metrics = &#metrics.handoffs[
1044                                    #root::slotmap::KeyData::from_ffi(#hoff_ffi).into()
1045                                ];
1046                                hoff_metrics.total_items_count.update(|x| x + hoff_len);
1047                                hoff_metrics.curr_items_count.set(hoff_len);
1048                            }
1049                            let #port_ident = #root::dfir_pipes::pull::iter(#drain_ident.drain(..));
1050                        }
1051                    })
1052                    .collect();
1053
1054                // Send port code: push into buffer.
1055                let send_port_code: Vec<TokenStream> = send_port_idents
1056                    .iter()
1057                    .zip(send_buf_idents.iter())
1058                    .map(|(port_ident, buf_ident)| {
1059                        quote_spanned! {port_ident.span()=>
1060                            let #port_ident = #root::dfir_pipes::push::vec_push(&mut #buf_ident);
1061                        }
1062                    })
1063                    .collect();
1064
1065                // All nodes in a subgraph should be in the same loop.
1066                let loop_id = self.node_loop(subgraph_nodes[0]);
1067
1068                let mut subgraph_op_iter_code = Vec::new();
1069                let mut subgraph_op_iter_after_code = Vec::new();
1070                {
1071                    let pull_to_push_idx = self.find_pull_to_push_idx(subgraph_nodes);
1072
1073                    let (pull_half, push_half) = subgraph_nodes.split_at(pull_to_push_idx);
1074                    let nodes_iter = pull_half.iter().chain(push_half.iter().rev());
1075
1076                    for (idx, &node_id) in nodes_iter.enumerate() {
1077                        let node = &self.nodes[node_id];
1078                        assert!(
1079                            matches!(node, GraphNode::Operator(_)),
1080                            "Handoffs are not part of subgraphs."
1081                        );
1082                        let op_inst = &self.operator_instances[node_id];
1083
1084                        let op_span = node.span();
1085                        let op_name = op_inst.op_constraints.name;
1086                        // Use op's span for root. #root is expected to be correct, any errors should span back to the op gen.
1087                        let root = change_spans(root.clone(), op_span);
1088                        let op_constraints = OPERATORS
1089                            .iter()
1090                            .find(|op| op_name == op.name)
1091                            .unwrap_or_else(|| panic!("Failed to find op: {}", op_name));
1092
1093                        let ident = self.node_as_ident(node_id, false);
1094
1095                        {
1096                            // TODO clean this up.
1097                            // Collect input arguments (predecessors).
1098                            let mut input_edges = self
1099                                .graph
1100                                .predecessor_edges(node_id)
1101                                .map(|edge_id| (self.edge_ports(edge_id).1, edge_id))
1102                                .collect::<Vec<_>>();
1103                            // Ensure sorted by port index.
1104                            input_edges.sort();
1105
1106                            let inputs = input_edges
1107                                .iter()
1108                                .map(|&(_port, edge_id)| {
1109                                    let (pred, _) = self.edge(edge_id);
1110                                    self.node_as_ident(pred, true)
1111                                })
1112                                .collect::<Vec<_>>();
1113
1114                            // Collect output arguments (successors).
1115                            let mut output_edges = self
1116                                .graph
1117                                .successor_edges(node_id)
1118                                .map(|edge_id| (&self.ports[edge_id].0, edge_id))
1119                                .collect::<Vec<_>>();
1120                            // Ensure sorted by port index.
1121                            output_edges.sort();
1122
1123                            let outputs = output_edges
1124                                .iter()
1125                                .map(|&(_port, edge_id)| {
1126                                    let (_, succ) = self.edge(edge_id);
1127                                    self.node_as_ident(succ, false)
1128                                })
1129                                .collect::<Vec<_>>();
1130
1131                            let is_pull = idx < pull_to_push_idx;
1132
1133                            let singleton_output_ident = &if op_constraints.has_singleton_output {
1134                                self.node_as_singleton_ident(node_id, op_span)
1135                            } else {
1136                                // This ident *should* go unused.
1137                                Ident::new(&format!("{}_has_no_singleton_output", op_name), op_span)
1138                            };
1139
1140                            // There's a bit of dark magic hidden in `Span`s... you'd think it's just a `file:line:column`,
1141                            // but it has one extra bit of info for _name resolution_, used for `Ident`s. `Span::call_site()`
1142                            // has the (unhygienic) resolution we want, an ident is just solely determined by its string name,
1143                            // which is what you'd expect out of unhygienic proc macros like this. Meanwhile, declarative macros
1144                            // use `Span::mixed_site()` which is weird and I don't understand it. It turns out that if you call
1145                            // the dfir syntax proc macro from _within_ a declarative macro then `op_span` will have the
1146                            // bad `Span::mixed_site()` name resolution and cause "Cannot find value `df/context`" errors. So
1147                            // we call `.resolved_at()` to fix resolution back to `Span::call_site()`. -Mingwei
1148                            let df_local = &Ident::new(GRAPH, op_span.resolved_at(df.span()));
1149                            let context = &Ident::new(CONTEXT, op_span.resolved_at(context.span()));
1150
1151                            let singletons_resolved =
1152                                self.helper_resolve_singletons(node_id, op_span);
1153                            let arguments = &process_singletons::postprocess_singletons(
1154                                op_inst.arguments_raw.clone(),
1155                                singletons_resolved.clone(),
1156                            );
1157                            let arguments_handles =
1158                                &process_singletons::postprocess_singletons_handles(
1159                                    op_inst.arguments_raw.clone(),
1160                                    singletons_resolved.clone(),
1161                                );
1162
1163                            let source_tag = 'a: {
1164                                if let Some(tag) = self.operator_tag.get(node_id).cloned() {
1165                                    break 'a tag;
1166                                }
1167
1168                                #[cfg(nightly)]
1169                                if proc_macro::is_available() {
1170                                    let op_span = op_span.unwrap();
1171                                    break 'a format!(
1172                                        "loc_{}_{}_{}_{}_{}",
1173                                        crate::pretty_span::make_source_path_relative(
1174                                            &op_span.file()
1175                                        )
1176                                        .display()
1177                                        .to_string()
1178                                        .replace(|x: char| !x.is_ascii_alphanumeric(), "_"),
1179                                        op_span.start().line(),
1180                                        op_span.start().column(),
1181                                        op_span.end().line(),
1182                                        op_span.end().column(),
1183                                    );
1184                                }
1185
1186                                format!(
1187                                    "loc_nopath_{}_{}_{}_{}",
1188                                    op_span.start().line,
1189                                    op_span.start().column,
1190                                    op_span.end().line,
1191                                    op_span.end().column
1192                                )
1193                            };
1194
1195                            let work_fn = format_ident!(
1196                                "{}__{}__{}",
1197                                ident,
1198                                op_name,
1199                                source_tag,
1200                                span = op_span
1201                            );
1202                            let work_fn_async = format_ident!("{}__async", work_fn, span = op_span);
1203
1204                            let context_args = WriteContextArgs {
1205                                root: &root,
1206                                df_ident: df_local,
1207                                context,
1208                                subgraph_id,
1209                                node_id,
1210                                loop_id,
1211                                op_span,
1212                                op_tag: self.operator_tag.get(node_id).cloned(),
1213                                work_fn: &work_fn,
1214                                work_fn_async: &work_fn_async,
1215                                ident: &ident,
1216                                is_pull,
1217                                inputs: &inputs,
1218                                outputs: &outputs,
1219                                singleton_output_ident,
1220                                op_name,
1221                                op_inst,
1222                                arguments,
1223                                arguments_handles,
1224                            };
1225
1226                            let write_result =
1227                                (op_constraints.write_fn)(&context_args, diagnostics);
1228                            let OperatorWriteOutput {
1229                                write_prologue,
1230                                write_iterator,
1231                                write_iterator_after,
1232                                write_tick_end,
1233                            } = write_result.unwrap_or_else(|()| {
1234                                assert!(
1235                                    diagnostics.has_error(),
1236                                    "Operator `{}` returned `Err` but emitted no diagnostics, this is a bug.",
1237                                    op_name,
1238                                );
1239                                OperatorWriteOutput {
1240                                    write_iterator: null_write_iterator_fn(&context_args),
1241                                    ..Default::default()
1242                                }
1243                            });
1244
1245                            op_prologue_code.push(syn::parse_quote! {
1246                                #[allow(non_snake_case)]
1247                                #[inline(always)]
1248                                fn #work_fn<T>(thunk: impl ::std::ops::FnOnce() -> T) -> T {
1249                                    thunk()
1250                                }
1251
1252                                #[allow(non_snake_case)]
1253                                #[inline(always)]
1254                                async fn #work_fn_async<T>(
1255                                    thunk: impl ::std::future::Future<Output = T>,
1256                                ) -> T {
1257                                    thunk.await
1258                                }
1259                            });
1260                            op_prologue_code.push(write_prologue);
1261                            op_tick_end_code.push(write_tick_end);
1262                            subgraph_op_iter_code.push(write_iterator);
1263
1264                            if include_type_guards {
1265                                let type_guard = if is_pull {
1266                                    quote_spanned! {op_span=>
1267                                        let #ident = {
1268                                            #[allow(non_snake_case)]
1269                                            #[inline(always)]
1270                                            pub fn #work_fn<Item, Input>(input: Input)
1271                                                -> impl #root::dfir_pipes::pull::Pull<Item = Item, Meta = (), CanPend = Input::CanPend, CanEnd = Input::CanEnd>
1272                                            where
1273                                                Input: #root::dfir_pipes::pull::Pull<Item = Item, Meta = ()>,
1274                                            {
1275                                                #root::pin_project_lite::pin_project! {
1276                                                    #[repr(transparent)]
1277                                                    struct Pull<Item, Input: #root::dfir_pipes::pull::Pull<Item = Item>> {
1278                                                        #[pin]
1279                                                        inner: Input
1280                                                    }
1281                                                }
1282
1283                                                impl<Item, Input> #root::dfir_pipes::pull::Pull for Pull<Item, Input>
1284                                                where
1285                                                    Input: #root::dfir_pipes::pull::Pull<Item = Item>,
1286                                                {
1287                                                    type Ctx<'ctx> = Input::Ctx<'ctx>;
1288
1289                                                    type Item = Item;
1290                                                    type Meta = Input::Meta;
1291                                                    type CanPend = Input::CanPend;
1292                                                    type CanEnd = Input::CanEnd;
1293
1294                                                    #[inline(always)]
1295                                                    fn pull(
1296                                                        self: ::std::pin::Pin<&mut Self>,
1297                                                        ctx: &mut Self::Ctx<'_>,
1298                                                    ) -> #root::dfir_pipes::pull::PullStep<Self::Item, Self::Meta, Self::CanPend, Self::CanEnd> {
1299                                                        #root::dfir_pipes::pull::Pull::pull(self.project().inner, ctx)
1300                                                    }
1301
1302                                                    #[inline(always)]
1303                                                    fn size_hint(&self) -> (usize, Option<usize>) {
1304                                                        #root::dfir_pipes::pull::Pull::size_hint(&self.inner)
1305                                                    }
1306                                                }
1307
1308                                                Pull {
1309                                                    inner: input
1310                                                }
1311                                            }
1312                                            #work_fn::<_, _>( #ident )
1313                                        };
1314                                    }
1315                                } else {
1316                                    quote_spanned! {op_span=>
1317                                        let #ident = {
1318                                            #[allow(non_snake_case)]
1319                                            #[inline(always)]
1320                                            pub fn #work_fn<Item, Psh>(psh: Psh) -> impl #root::dfir_pipes::push::Push<Item, (), CanPend = Psh::CanPend>
1321                                            where
1322                                                Psh: #root::dfir_pipes::push::Push<Item, ()>
1323                                            {
1324                                                #root::pin_project_lite::pin_project! {
1325                                                    #[repr(transparent)]
1326                                                    struct PushGuard<Psh> {
1327                                                        #[pin]
1328                                                        inner: Psh,
1329                                                    }
1330                                                }
1331
1332                                                impl<Item, Psh> #root::dfir_pipes::push::Push<Item, ()> for PushGuard<Psh>
1333                                                where
1334                                                    Psh: #root::dfir_pipes::push::Push<Item, ()>,
1335                                                {
1336                                                    type Ctx<'ctx> = Psh::Ctx<'ctx>;
1337
1338                                                    type CanPend = Psh::CanPend;
1339
1340                                                    #[inline(always)]
1341                                                    fn poll_ready(
1342                                                        self: ::std::pin::Pin<&mut Self>,
1343                                                        ctx: &mut Self::Ctx<'_>,
1344                                                    ) -> #root::dfir_pipes::push::PushStep<Self::CanPend> {
1345                                                        #root::dfir_pipes::push::Push::poll_ready(self.project().inner, ctx)
1346                                                    }
1347
1348                                                    #[inline(always)]
1349                                                    fn start_send(
1350                                                        self: ::std::pin::Pin<&mut Self>,
1351                                                        item: Item,
1352                                                        meta: (),
1353                                                    ) {
1354                                                        #root::dfir_pipes::push::Push::start_send(self.project().inner, item, meta)
1355                                                    }
1356
1357                                                    #[inline(always)]
1358                                                    fn poll_flush(
1359                                                        self: ::std::pin::Pin<&mut Self>,
1360                                                        ctx: &mut Self::Ctx<'_>,
1361                                                    ) -> #root::dfir_pipes::push::PushStep<Self::CanPend> {
1362                                                        #root::dfir_pipes::push::Push::poll_flush(self.project().inner, ctx)
1363                                                    }
1364
1365                                                    #[inline(always)]
1366                                                    fn size_hint(
1367                                                        self: ::std::pin::Pin<&mut Self>,
1368                                                        hint: (usize, Option<usize>),
1369                                                    ) {
1370                                                        #root::dfir_pipes::push::Push::size_hint(self.project().inner, hint)
1371                                                    }
1372                                                }
1373
1374                                                PushGuard {
1375                                                    inner: psh
1376                                                }
1377                                            }
1378                                            #work_fn( #ident )
1379                                        };
1380                                    }
1381                                };
1382                                subgraph_op_iter_code.push(type_guard);
1383                            }
1384                            subgraph_op_iter_after_code.push(write_iterator_after);
1385                        }
1386                    }
1387
1388                    {
1389                        // Determine pull and push halves of the `Pivot`.
1390                        let pull_ident = if 0 < pull_to_push_idx {
1391                            self.node_as_ident(subgraph_nodes[pull_to_push_idx - 1], false)
1392                        } else {
1393                            // Entire subgraph is push (with a single recv/pull handoff input).
1394                            recv_port_idents[0].clone()
1395                        };
1396
1397                        #[rustfmt::skip]
1398                        let push_ident = if let Some(&node_id) =
1399                            subgraph_nodes.get(pull_to_push_idx)
1400                        {
1401                            self.node_as_ident(node_id, false)
1402                        } else if 1 == send_port_idents.len() {
1403                            // Entire subgraph is pull (with a single send/push handoff output).
1404                            send_port_idents[0].clone()
1405                        } else {
1406                            diagnostics.push(Diagnostic::spanned(
1407                                pull_ident.span(),
1408                                Level::Error,
1409                                "Degenerate subgraph detected, is there a disconnected `null()` or other degenerate pipeline somewhere?",
1410                            ));
1411                            continue;
1412                        };
1413
1414                        // Pivot span is combination of pull and push spans (or if not possible, just take the push).
1415                        let pivot_span = pull_ident
1416                            .span()
1417                            .join(push_ident.span())
1418                            .unwrap_or_else(|| push_ident.span());
1419                        let pivot_fn_ident = Ident::new(
1420                            &format!("pivot_run_sg_{:?}", subgraph_id.data()),
1421                            pivot_span,
1422                        );
1423                        let root = change_spans(root.clone(), pivot_span);
1424                        subgraph_op_iter_code.push(quote_spanned! {pivot_span=>
1425                            #[inline(always)]
1426                            fn #pivot_fn_ident<Pul, Psh, Item>(pull: Pul, push: Psh)
1427                                -> impl ::std::future::Future<Output = ()>
1428                            where
1429                                Pul: #root::dfir_pipes::pull::Pull<Item = Item>,
1430                                Psh: #root::dfir_pipes::push::Push<Item, Pul::Meta>,
1431                            {
1432                                #root::dfir_pipes::pull::Pull::send_push(pull, push)
1433                            }
1434                            (#pivot_fn_ident)(#pull_ident, #push_ident).await;
1435                        });
1436                    }
1437                };
1438
1439                // Each subgraph block is an async block so it can be individually instrumented.
1440                // Note: this ident is for the subgraph future, not a runtime SubgraphId binding
1441                // (unlike the scheduled path's `sg_ident`).
1442                let sg_fut_ident = subgraph_id.as_ident(Span::call_site());
1443
1444                // Generate send-side curr_items_count updates (after subgraph runs).
1445                let send_metrics_code: Vec<TokenStream> = send_hoffs
1446                    .iter()
1447                    .zip(send_buf_idents.iter())
1448                    .map(|(&hoff_id, buf_ident)| {
1449                        let hoff_ffi = hoff_id.data().as_ffi();
1450                        quote! {
1451                            __dfir_metrics.handoffs[
1452                                #root::slotmap::KeyData::from_ffi(#hoff_ffi).into()
1453                            ].curr_items_count.set(#buf_ident.len());
1454                        }
1455                    })
1456                    .collect();
1457
1458                subgraph_blocks.push(quote! {
1459                    let #sg_fut_ident = async {
1460                        let #context = &#df;
1461                        #( #recv_port_code )*
1462                        #( #send_port_code )*
1463                        #( #subgraph_op_iter_code )*
1464                        #( #subgraph_op_iter_after_code )*
1465                    };
1466                    {
1467                        let sg_metrics = &__dfir_metrics.subgraphs[
1468                            #root::slotmap::KeyData::from_ffi(#sg_metrics_ffi).into()
1469                        ];
1470                        #root::scheduled::metrics::InstrumentSubgraph::new(
1471                            #sg_fut_ident, sg_metrics
1472                        ).await;
1473                        sg_metrics.total_run_count.update(|x| x + 1);
1474                    }
1475                    #( #send_metrics_code )*
1476                });
1477
1478                // Collect per-subgraph prologues into the main prologue lists.
1479                // (They are already pushed above in the operator loop.)
1480            }
1481        }
1482
1483        if diagnostics.has_error() {
1484            return Err(std::mem::take(diagnostics));
1485        }
1486        let _ = diagnostics; // Ensure no more diagnostics may be added after checking for errors.
1487
1488        let (meta_graph_arg, diagnostics_arg) = if include_meta {
1489            let meta_graph_json = serde_json::to_string(&self).unwrap();
1490            let meta_graph_json = Literal::string(&meta_graph_json);
1491
1492            let serde_diagnostics: Vec<_> = diagnostics.iter().map(Diagnostic::to_serde).collect();
1493            let diagnostics_json = serde_json::to_string(&*serde_diagnostics).unwrap();
1494            let diagnostics_json = Literal::string(&diagnostics_json);
1495
1496            (
1497                quote! { Some(#meta_graph_json) },
1498                quote! { Some(#diagnostics_json) },
1499            )
1500        } else {
1501            (quote! { None }, quote! { None })
1502        };
1503
1504        // Generate metrics initialization: one entry per handoff and per subgraph.
1505        let metrics_init_code = {
1506            let handoff_inits = handoff_nodes.iter().map(|&(node_id, _)| {
1507                let ffi = node_id.data().as_ffi();
1508                quote! {
1509                    dfir_metrics.handoffs.insert(
1510                        #root::slotmap::KeyData::from_ffi(#ffi).into(),
1511                        ::std::default::Default::default(),
1512                    );
1513                }
1514            });
1515            let subgraph_inits = all_subgraphs.iter().map(|&(sg_id, _)| {
1516                let ffi = sg_id.data().as_ffi();
1517                quote! {
1518                    dfir_metrics.subgraphs.insert(
1519                        #root::slotmap::KeyData::from_ffi(#ffi).into(),
1520                        ::std::default::Default::default(),
1521                    );
1522                }
1523            });
1524            handoff_inits.chain(subgraph_inits).collect::<Vec<_>>()
1525        };
1526
1527        // Prologues and buffer declarations persist across ticks (outside the closure).
1528        // Subgraph blocks run each tick (inside the closure).
1529        Ok(quote! {
1530            {
1531                #prefix
1532
1533                use #root::{var_expr, var_args};
1534
1535                let __dfir_wake_state = ::std::sync::Arc::new(
1536                    #root::scheduled::context::WakeState::default()
1537                );
1538
1539                let __dfir_metrics = {
1540                    let mut dfir_metrics = #root::scheduled::metrics::DfirMetrics::default();
1541                    #( #metrics_init_code )*
1542                    ::std::rc::Rc::new(dfir_metrics)
1543                };
1544
1545                #[allow(unused_mut)]
1546                let mut #df = #root::scheduled::context::Context::new(
1547                    ::std::clone::Clone::clone(&__dfir_wake_state),
1548                    __dfir_metrics,
1549                );
1550
1551                #( #buffer_code )*
1552                #( #back_buffer_code )*
1553                #( #op_prologue_code )*
1554
1555                // Pre-set to true so the first tick always returns true
1556                // (matching Dfir pre-scheduling behavior). Subsequent ticks
1557                // start false (from take()) and are set true by recv port code
1558                // if any handoff buffer has data.
1559                let mut __dfir_work_done = true;
1560                #[allow(unused_qualifications, unused_mut, unused_variables, clippy::await_holding_refcell_ref, clippy::deref_addrof)]
1561                let __dfir_inline_tick = async move |#df: &mut #root::scheduled::context::Context| {
1562                    let __dfir_metrics = #df.metrics();
1563                    // Double-buffer swap for defer_tick handoffs: move last tick's
1564                    // producer output into the back buffer for the consumer to drain.
1565                    #( #back_edge_swap_code )*
1566                    #( #subgraph_blocks )*
1567
1568                    // For non-lazy defer_tick: if any deferred buffer has data,
1569                    // signal that another tick should run.
1570                    if false #( || !#defer_tick_buf_idents.is_empty() )* {
1571                        #df.schedule_subgraph(true);
1572                    }
1573
1574                    // End-of-tick state reset (e.g. 'tick persistence).
1575                    #( #op_tick_end_code )*
1576
1577                    #df.__end_tick();
1578                    ::std::mem::take(&mut __dfir_work_done)
1579                };
1580                #root::scheduled::context::Dfir::new(
1581                    __dfir_inline_tick,
1582                    #df,
1583                    #meta_graph_arg,
1584                    #diagnostics_arg,
1585                )
1586            }
1587        })
1588    }
1589
1590    /// Color mode (pull vs. push, handoff vs. comp) for nodes. Some nodes can be push *OR* pull;
1591    /// those nodes will not be set in the returned map.
1592    pub fn node_color_map(&self) -> SparseSecondaryMap<GraphNodeId, Color> {
1593        let mut node_color_map: SparseSecondaryMap<GraphNodeId, Color> = self
1594            .node_ids()
1595            .filter_map(|node_id| {
1596                let op_color = self.node_color(node_id)?;
1597                Some((node_id, op_color))
1598            })
1599            .collect();
1600
1601        // Fill in rest via subgraphs.
1602        for sg_nodes in self.subgraph_nodes.values() {
1603            let pull_to_push_idx = self.find_pull_to_push_idx(sg_nodes);
1604
1605            for (idx, node_id) in sg_nodes.iter().copied().enumerate() {
1606                let is_pull = idx < pull_to_push_idx;
1607                node_color_map.insert(node_id, if is_pull { Color::Pull } else { Color::Push });
1608            }
1609        }
1610
1611        node_color_map
1612    }
1613
1614    /// Writes this graph as mermaid into a string.
1615    pub fn to_mermaid(&self, write_config: &WriteConfig) -> String {
1616        let mut output = String::new();
1617        self.write_mermaid(&mut output, write_config).unwrap();
1618        output
1619    }
1620
1621    /// Writes this graph as mermaid into the given `Write`.
1622    pub fn write_mermaid(
1623        &self,
1624        output: impl std::fmt::Write,
1625        write_config: &WriteConfig,
1626    ) -> std::fmt::Result {
1627        let mut graph_write = Mermaid::new(output);
1628        self.write_graph(&mut graph_write, write_config)
1629    }
1630
1631    /// Writes this graph as DOT (graphviz) into a string.
1632    pub fn to_dot(&self, write_config: &WriteConfig) -> String {
1633        let mut output = String::new();
1634        let mut graph_write = Dot::new(&mut output);
1635        self.write_graph(&mut graph_write, write_config).unwrap();
1636        output
1637    }
1638
1639    /// Writes this graph as DOT (graphviz) into the given `Write`.
1640    pub fn write_dot(
1641        &self,
1642        output: impl std::fmt::Write,
1643        write_config: &WriteConfig,
1644    ) -> std::fmt::Result {
1645        let mut graph_write = Dot::new(output);
1646        self.write_graph(&mut graph_write, write_config)
1647    }
1648
1649    /// Write out this graph using the given `GraphWrite`. E.g. `Mermaid` or `Dot.
1650    pub(crate) fn write_graph<W>(
1651        &self,
1652        mut graph_write: W,
1653        write_config: &WriteConfig,
1654    ) -> Result<(), W::Err>
1655    where
1656        W: GraphWrite,
1657    {
1658        fn helper_edge_label(
1659            src_port: &PortIndexValue,
1660            dst_port: &PortIndexValue,
1661        ) -> Option<String> {
1662            let src_label = match src_port {
1663                PortIndexValue::Path(path) => Some(path.to_token_stream().to_string()),
1664                PortIndexValue::Int(index) => Some(index.value.to_string()),
1665                _ => None,
1666            };
1667            let dst_label = match dst_port {
1668                PortIndexValue::Path(path) => Some(path.to_token_stream().to_string()),
1669                PortIndexValue::Int(index) => Some(index.value.to_string()),
1670                _ => None,
1671            };
1672            let label = match (src_label, dst_label) {
1673                (Some(l1), Some(l2)) => Some(format!("{}\n{}", l1, l2)),
1674                (Some(l1), None) => Some(l1),
1675                (None, Some(l2)) => Some(l2),
1676                (None, None) => None,
1677            };
1678            label
1679        }
1680
1681        // Make node color map one time.
1682        let node_color_map = self.node_color_map();
1683
1684        // Write prologue.
1685        graph_write.write_prologue()?;
1686
1687        // Define nodes.
1688        let mut skipped_handoffs = BTreeSet::new();
1689        let mut subgraph_handoffs = <BTreeMap<GraphSubgraphId, Vec<GraphNodeId>>>::new();
1690        for (node_id, node) in self.nodes() {
1691            if matches!(node, GraphNode::Handoff { .. }) {
1692                if write_config.no_handoffs {
1693                    skipped_handoffs.insert(node_id);
1694                    continue;
1695                } else {
1696                    let pred_node = self.node_predecessor_nodes(node_id).next().unwrap();
1697                    let pred_sg = self.node_subgraph(pred_node);
1698                    let succ_node = self.node_successor_nodes(node_id).next().unwrap();
1699                    let succ_sg = self.node_subgraph(succ_node);
1700                    if let Some((pred_sg, succ_sg)) = pred_sg.zip(succ_sg)
1701                        && pred_sg == succ_sg
1702                    {
1703                        subgraph_handoffs.entry(pred_sg).or_default().push(node_id);
1704                    }
1705                }
1706            }
1707            graph_write.write_node_definition(
1708                node_id,
1709                &if write_config.op_short_text {
1710                    node.to_name_string()
1711                } else if write_config.op_text_no_imports {
1712                    // Remove any lines that start with "use" (imports)
1713                    let full_text = node.to_pretty_string();
1714                    let mut output = String::new();
1715                    for sentence in full_text.split('\n') {
1716                        if sentence.trim().starts_with("use") {
1717                            continue;
1718                        }
1719                        output.push('\n');
1720                        output.push_str(sentence);
1721                    }
1722                    output.into()
1723                } else {
1724                    node.to_pretty_string()
1725                },
1726                if write_config.no_pull_push {
1727                    None
1728                } else {
1729                    node_color_map.get(node_id).copied()
1730                },
1731            )?;
1732        }
1733
1734        // Write edges.
1735        for (edge_id, (src_id, mut dst_id)) in self.edges() {
1736            // Handling for if `write_config.no_handoffs` true.
1737            if skipped_handoffs.contains(&src_id) {
1738                continue;
1739            }
1740
1741            let (src_port, mut dst_port) = self.edge_ports(edge_id);
1742            if skipped_handoffs.contains(&dst_id) {
1743                let mut handoff_succs = self.node_successors(dst_id);
1744                assert_eq!(1, handoff_succs.len());
1745                let (succ_edge, succ_node) = handoff_succs.next().unwrap();
1746                dst_id = succ_node;
1747                dst_port = self.edge_ports(succ_edge).1;
1748            }
1749
1750            let label = helper_edge_label(src_port, dst_port);
1751            let delay_type = self
1752                .node_op_inst(dst_id)
1753                .and_then(|op_inst| (op_inst.op_constraints.input_delaytype_fn)(dst_port));
1754            graph_write.write_edge(src_id, dst_id, delay_type, label.as_deref(), false)?;
1755        }
1756
1757        // Write reference edges.
1758        if !write_config.no_references {
1759            for dst_id in self.node_ids() {
1760                for src_ref_id in self
1761                    .node_singleton_references(dst_id)
1762                    .iter()
1763                    .copied()
1764                    .flatten()
1765                {
1766                    let delay_type = Some(DelayType::Stratum);
1767                    let label = None;
1768                    graph_write.write_edge(src_ref_id, dst_id, delay_type, label, true)?;
1769                }
1770            }
1771        }
1772
1773        // The following code is a little bit tricky. Generally, the graph has the hierarchy:
1774        // `loop -> subgraph -> varname -> node`. However, each of these can be disabled via the `write_config`. To
1775        // handle both the enabled and disabled case, this code is structured as a series of nested loops. If the layer
1776        // is disabled, then the HashMap<Option<KEY>, Vec<VALUE>> will only have a single key (`None`) with a
1777        // corresponding `Vec` value containing everything. This way no special handling is needed for the next layer.
1778
1779        // Loop -> Subgraphs
1780        let loop_subgraphs = self.subgraph_ids().map(|sg_id| {
1781            let loop_id = if write_config.no_loops {
1782                None
1783            } else {
1784                self.subgraph_loop(sg_id)
1785            };
1786            (loop_id, sg_id)
1787        });
1788        let loop_subgraphs = into_group_map(loop_subgraphs);
1789        for (loop_id, subgraph_ids) in loop_subgraphs {
1790            if let Some(loop_id) = loop_id {
1791                graph_write.write_loop_start(loop_id)?;
1792            }
1793
1794            // Subgraph -> Varnames.
1795            let subgraph_varnames_nodes = subgraph_ids.into_iter().flat_map(|sg_id| {
1796                self.subgraph(sg_id).iter().copied().map(move |node_id| {
1797                    let opt_sg_id = if write_config.no_subgraphs {
1798                        None
1799                    } else {
1800                        Some(sg_id)
1801                    };
1802                    (opt_sg_id, (self.node_varname(node_id), node_id))
1803                })
1804            });
1805            let subgraph_varnames_nodes = into_group_map(subgraph_varnames_nodes);
1806            for (sg_id, varnames) in subgraph_varnames_nodes {
1807                if let Some(sg_id) = sg_id {
1808                    graph_write.write_subgraph_start(sg_id)?;
1809                }
1810
1811                // Varnames -> Nodes.
1812                let varname_nodes = varnames.into_iter().map(|(varname, node)| {
1813                    let varname = if write_config.no_varnames {
1814                        None
1815                    } else {
1816                        varname
1817                    };
1818                    (varname, node)
1819                });
1820                let varname_nodes = into_group_map(varname_nodes);
1821                for (varname, node_ids) in varname_nodes {
1822                    if let Some(varname) = varname {
1823                        graph_write.write_varname_start(&varname.0.to_string(), sg_id)?;
1824                    }
1825
1826                    // Write all nodes.
1827                    for node_id in node_ids {
1828                        graph_write.write_node(node_id)?;
1829                    }
1830
1831                    if varname.is_some() {
1832                        graph_write.write_varname_end()?;
1833                    }
1834                }
1835
1836                if sg_id.is_some() {
1837                    graph_write.write_subgraph_end()?;
1838                }
1839            }
1840
1841            if loop_id.is_some() {
1842                graph_write.write_loop_end()?;
1843            }
1844        }
1845
1846        // Write epilogue.
1847        graph_write.write_epilogue()?;
1848
1849        Ok(())
1850    }
1851
1852    /// Convert back into surface syntax.
1853    pub fn surface_syntax_string(&self) -> String {
1854        let mut string = String::new();
1855        self.write_surface_syntax(&mut string).unwrap();
1856        string
1857    }
1858
1859    /// Convert back into surface syntax.
1860    pub fn write_surface_syntax(&self, write: &mut impl std::fmt::Write) -> std::fmt::Result {
1861        for (key, node) in self.nodes.iter() {
1862            match node {
1863                GraphNode::Operator(op) => {
1864                    writeln!(write, "{:?} = {};", key.data(), op.to_token_stream())?;
1865                }
1866                GraphNode::Handoff { .. } => {
1867                    writeln!(write, "// {:?} = <handoff>;", key.data())?;
1868                }
1869                GraphNode::ModuleBoundary { .. } => panic!(),
1870            }
1871        }
1872        writeln!(write)?;
1873        for (_e, (src_key, dst_key)) in self.graph.edges() {
1874            writeln!(write, "{:?} -> {:?};", src_key.data(), dst_key.data())?;
1875        }
1876        Ok(())
1877    }
1878
1879    /// Convert into a [mermaid](https://mermaid-js.github.io/) graph. Ignores subgraphs.
1880    pub fn mermaid_string_flat(&self) -> String {
1881        let mut string = String::new();
1882        self.write_mermaid_flat(&mut string).unwrap();
1883        string
1884    }
1885
1886    /// Convert into a [mermaid](https://mermaid-js.github.io/) graph. Ignores subgraphs.
1887    pub fn write_mermaid_flat(&self, write: &mut impl std::fmt::Write) -> std::fmt::Result {
1888        writeln!(write, "flowchart TB")?;
1889        for (key, node) in self.nodes.iter() {
1890            match node {
1891                GraphNode::Operator(operator) => writeln!(
1892                    write,
1893                    "    %% {span}\n    {id:?}[\"{row_col} <tt>{code}</tt>\"]",
1894                    span = PrettySpan(node.span()),
1895                    id = key.data(),
1896                    row_col = PrettyRowCol(node.span()),
1897                    code = operator
1898                        .to_token_stream()
1899                        .to_string()
1900                        .replace('&', "&amp;")
1901                        .replace('<', "&lt;")
1902                        .replace('>', "&gt;")
1903                        .replace('"', "&quot;")
1904                        .replace('\n', "<br>"),
1905                ),
1906                GraphNode::Handoff { .. } => {
1907                    writeln!(write, r#"    {:?}{{"{}"}}"#, key.data(), HANDOFF_NODE_STR)
1908                }
1909                GraphNode::ModuleBoundary { .. } => {
1910                    writeln!(
1911                        write,
1912                        r#"    {:?}{{"{}"}}"#,
1913                        key.data(),
1914                        MODULE_BOUNDARY_NODE_STR
1915                    )
1916                }
1917            }?;
1918        }
1919        writeln!(write)?;
1920        for (_e, (src_key, dst_key)) in self.graph.edges() {
1921            writeln!(write, "    {:?}-->{:?}", src_key.data(), dst_key.data())?;
1922        }
1923        Ok(())
1924    }
1925}
1926
1927/// Loops
1928impl DfirGraph {
1929    /// Iterator over all loop IDs.
1930    pub fn loop_ids(&self) -> slotmap::basic::Keys<'_, GraphLoopId, Vec<GraphNodeId>> {
1931        self.loop_nodes.keys()
1932    }
1933
1934    /// Iterator over all loops, ID and members: `(GraphLoopId, Vec<GraphNodeId>)`.
1935    pub fn loops(&self) -> slotmap::basic::Iter<'_, GraphLoopId, Vec<GraphNodeId>> {
1936        self.loop_nodes.iter()
1937    }
1938
1939    /// Create a new loop context, with the given parent loop (or `None`).
1940    pub fn insert_loop(&mut self, parent_loop: Option<GraphLoopId>) -> GraphLoopId {
1941        let loop_id = self.loop_nodes.insert(Vec::new());
1942        self.loop_children.insert(loop_id, Vec::new());
1943        if let Some(parent_loop) = parent_loop {
1944            self.loop_parent.insert(loop_id, parent_loop);
1945            self.loop_children
1946                .get_mut(parent_loop)
1947                .unwrap()
1948                .push(loop_id);
1949        } else {
1950            self.root_loops.push(loop_id);
1951        }
1952        loop_id
1953    }
1954
1955    /// Get a node's loop context (or `None` for root).
1956    pub fn node_loop(&self, node_id: GraphNodeId) -> Option<GraphLoopId> {
1957        self.node_loops.get(node_id).copied()
1958    }
1959
1960    /// Get a subgraph's loop context (or `None` for root).
1961    pub fn subgraph_loop(&self, subgraph_id: GraphSubgraphId) -> Option<GraphLoopId> {
1962        let &node_id = self.subgraph(subgraph_id).first().unwrap();
1963        let out = self.node_loop(node_id);
1964        debug_assert!(
1965            self.subgraph(subgraph_id)
1966                .iter()
1967                .all(|&node_id| self.node_loop(node_id) == out),
1968            "Subgraph nodes should all have the same loop context."
1969        );
1970        out
1971    }
1972
1973    /// Get a loop context's parent loop context (or `None` for root).
1974    pub fn loop_parent(&self, loop_id: GraphLoopId) -> Option<GraphLoopId> {
1975        self.loop_parent.get(loop_id).copied()
1976    }
1977
1978    /// Get a loop context's child loops.
1979    pub fn loop_children(&self, loop_id: GraphLoopId) -> &Vec<GraphLoopId> {
1980        self.loop_children.get(loop_id).unwrap()
1981    }
1982}
1983
1984/// Configuration for writing graphs.
1985#[derive(Clone, Debug, Default)]
1986#[cfg_attr(feature = "clap-derive", derive(clap::Args))]
1987pub struct WriteConfig {
1988    /// Subgraphs will not be rendered if set.
1989    #[cfg_attr(feature = "clap-derive", arg(long))]
1990    pub no_subgraphs: bool,
1991    /// Variable names will not be rendered if set.
1992    #[cfg_attr(feature = "clap-derive", arg(long))]
1993    pub no_varnames: bool,
1994    /// Will not render pull/push shapes if set.
1995    #[cfg_attr(feature = "clap-derive", arg(long))]
1996    pub no_pull_push: bool,
1997    /// Will not render handoffs if set.
1998    #[cfg_attr(feature = "clap-derive", arg(long))]
1999    pub no_handoffs: bool,
2000    /// Will not render singleton references if set.
2001    #[cfg_attr(feature = "clap-derive", arg(long))]
2002    pub no_references: bool,
2003    /// Will not render loops if set.
2004    #[cfg_attr(feature = "clap-derive", arg(long))]
2005    pub no_loops: bool,
2006
2007    /// Op text will only be their name instead of the whole source.
2008    #[cfg_attr(feature = "clap-derive", arg(long))]
2009    pub op_short_text: bool,
2010    /// Op text will exclude any line that starts with "use".
2011    #[cfg_attr(feature = "clap-derive", arg(long))]
2012    pub op_text_no_imports: bool,
2013}
2014
2015/// Enum for choosing between mermaid and dot graph writing.
2016#[derive(Copy, Clone, Debug)]
2017#[cfg_attr(feature = "clap-derive", derive(clap::Parser, clap::ValueEnum))]
2018pub enum WriteGraphType {
2019    /// Mermaid graphs.
2020    Mermaid,
2021    /// Dot (Graphviz) graphs.
2022    Dot,
2023}
2024
2025/// [`itertools::Itertools::into_group_map`], but for `BTreeMap`.
2026fn into_group_map<K, V>(iter: impl IntoIterator<Item = (K, V)>) -> BTreeMap<K, Vec<V>>
2027where
2028    K: Ord,
2029{
2030    let mut out: BTreeMap<_, Vec<_>> = BTreeMap::new();
2031    for (k, v) in iter {
2032        out.entry(k).or_default().push(v);
2033    }
2034    out
2035}