Skip to main content

hydro_lang/compile/
compiled.rs

1use dfir_lang::graph::{AsCodeOptions, 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    /// Per-location codegen options, edited by sidecars via
24    /// [`crate::telemetry::Sidecar::edit_as_code_options`]. Locations without an entry use
25    /// [`AsCodeOptions::default`].
26    pub(super) as_code_options: SparseSecondaryMap<LocationKey, AsCodeOptions>,
27
28    pub(super) _phantom: Invariant<'a>,
29}
30
31impl<'a> CompiledFlow<'a> {
32    /// Returns the DFIR graph for the given location.
33    ///
34    /// - `Ok(&partitioned_graph)` if partitioning succeeded.
35    /// - `Err(&PartitionError)` if partitioning failed. The error still exposes the
36    ///   (renderable) flat graph via [`PartitionError::flat_graph`] and the reason via
37    ///   [`PartitionError::diagnostic`], so the graph can be visualized to diagnose the
38    ///   failure — e.g.
39    ///   ```ignore
40    ///   let mermaid = match compiled.dfir_for(&process) {
41    ///       Ok(graph) => graph.to_mermaid(&Default::default()),
42    ///       Err(err) => err.flat_graph.mermaid_string_flat(),
43    ///   };
44    ///   ```
45    pub fn dfir_for(&self, location: &impl Location<'a>) -> Result<&DfirGraph, &PartitionError> {
46        self.dfir
47            .get(Location::id(location).key())
48            .unwrap()
49            .as_ref()
50    }
51
52    /// Returns the DFIR graph (or [`PartitionError`]) for every location.
53    pub fn all_dfir(&self) -> &SecondaryMap<LocationKey, Result<DfirGraph, PartitionError>> {
54        &self.dfir
55    }
56}