Skip to main content

hydro_lang/compile/
compiled.rs

1use dfir_lang::graph::{DfirGraph, PartitionError};
2use slotmap::{SecondaryMap, SparseSecondaryMap};
3use syn::Stmt;
4
5use crate::location::{Location, LocationKey};
6use crate::staging_util::Invariant;
7
8pub struct CompiledFlow<'a> {
9    /// The DFIR graph for each location.
10    ///
11    /// Each entry is `Ok(partitioned_graph)` on success, or `Err(PartitionError)` if
12    /// partitioning failed (e.g. an intra-tick cycle). The error still carries the
13    /// renderable flat graph and the diagnostic, so a failed location can be visualized
14    /// and diagnosed rather than aborting the whole compile.
15    pub(super) dfir: SecondaryMap<LocationKey, Result<DfirGraph, PartitionError>>,
16
17    /// Extra statements to be added above the DFIR graph code, for each location.
18    pub(super) extra_stmts: SparseSecondaryMap<LocationKey, Vec<Stmt>>,
19
20    /// `Future` expressions to be run alongside the DFIR graph execution, per-location. See [`crate::telemetry::Sidecar`].
21    pub(super) sidecars: SparseSecondaryMap<LocationKey, Vec<syn::Expr>>,
22
23    pub(super) _phantom: Invariant<'a>,
24}
25
26impl<'a> CompiledFlow<'a> {
27    /// Returns the DFIR graph for the given location.
28    ///
29    /// - `Ok(&partitioned_graph)` if partitioning succeeded.
30    /// - `Err(&PartitionError)` if partitioning failed. The error still exposes the
31    ///   (renderable) flat graph via [`PartitionError::flat_graph`] and the reason via
32    ///   [`PartitionError::diagnostic`], so the graph can be visualized to diagnose the
33    ///   failure — e.g.
34    ///   ```ignore
35    ///   let mermaid = match compiled.dfir_for(&process) {
36    ///       Ok(graph) => graph.to_mermaid(&Default::default()),
37    ///       Err(err) => err.flat_graph.mermaid_string_flat(),
38    ///   };
39    ///   ```
40    pub fn dfir_for(&self, location: &impl Location<'a>) -> Result<&DfirGraph, &PartitionError> {
41        self.dfir
42            .get(Location::id(location).key())
43            .unwrap()
44            .as_ref()
45    }
46
47    /// Returns the DFIR graph (or [`PartitionError`]) for every location.
48    pub fn all_dfir(&self) -> &SecondaryMap<LocationKey, Result<DfirGraph, PartitionError>> {
49        &self.dfir
50    }
51}