Skip to main content

hydro_lang/sim/
flow.rs

1//! Entrypoint for compiling and running Hydro simulations.
2
3use std::cell::RefCell;
4use std::collections::{HashMap, HashSet};
5use std::panic::RefUnwindSafe;
6use std::rc::Rc;
7
8use dfir_lang::graph::{DfirGraph, FlatGraphBuilder, FlatGraphBuilderOutput};
9use libloading::Library;
10use slotmap::SparseSecondaryMap;
11
12use super::builder::SimBuilder;
13use super::compiled::{CompiledSim, CompiledSimInstance};
14use super::graph::{SimDeploy, SimExternal, SimNode, compile_sim, create_sim_graph_trybuild};
15use crate::compile::ir::HydroRoot;
16use crate::location::LocationKey;
17use crate::location::dynamic::LocationId;
18use crate::prelude::Cluster;
19use crate::sim::graph::SimExternalPortRegistry;
20use crate::staging_util::Invariant;
21
22/// A not-yet-compiled simulator for a Hydro program.
23pub struct SimFlow<'a> {
24    pub(crate) ir: Vec<HydroRoot>,
25
26    /// SimNode for each Process.
27    pub(crate) processes: SparseSecondaryMap<LocationKey, SimNode>,
28    /// SimNode for each Cluster.
29    pub(crate) clusters: SparseSecondaryMap<LocationKey, SimNode>,
30    /// SimExternal for each External.
31    pub(crate) externals: SparseSecondaryMap<LocationKey, SimExternal>,
32
33    /// Max size of each cluster.
34    pub(crate) cluster_max_sizes: SparseSecondaryMap<LocationKey, usize>,
35    /// Handle to state handling `external`s' ports.
36    pub(crate) externals_port_registry: Rc<RefCell<SimExternalPortRegistry>>,
37
38    /// When true, the simulator only tests safety properties (not liveness).
39    pub(crate) test_safety_only: bool,
40
41    /// When true, consistency assertions are skipped (treated as identity no-ops).
42    /// When false (default), encountering a consistency assertion panics because
43    /// validating consistency assertions is not yet supported in the simulator.
44    pub(crate) skip_consistency_assertions: bool,
45
46    /// Number of iterations to use for fuzzing, defaults to 8192
47    pub(crate) unit_test_fuzz_iterations: usize,
48
49    pub(crate) _phantom: Invariant<'a>,
50}
51
52impl<'a> SimFlow<'a> {
53    /// Sets the maximum size of the given cluster in the simulation.
54    pub fn with_cluster_size<C>(mut self, cluster: &Cluster<'a, C>, max_size: usize) -> Self {
55        self.cluster_max_sizes.insert(cluster.key, max_size);
56        self
57    }
58
59    /// Opts in to safety-only testing, which is required when using
60    /// [`lossy_delayed_forever`](crate::networking::NetworkingConfig::lossy_delayed_forever)
61    /// networking.
62    ///
63    /// The simulator models dropped messages as indefinitely delayed, which means
64    /// it only tests safety properties—not liveness—since messages may never arrive.
65    /// Calling this method acknowledges that the simulation will not verify that the
66    /// program eventually makes progress.
67    pub fn test_safety_only(mut self) -> Self {
68        self.test_safety_only = true;
69        self
70    }
71
72    /// Opts in to skipping consistency assertions. When enabled, `assert_is_consistent`
73    /// nodes are treated as identity no-ops in the simulator. When disabled (the default),
74    /// encountering a consistency assertion will panic because validating consistency
75    /// assertions is not yet supported in the simulator.
76    pub fn skip_consistency_assertions(mut self) -> Self {
77        self.skip_consistency_assertions = true;
78        self
79    }
80
81    /// Sets the number of fuzz iterations for this test. Overrides the
82    /// the default value of 8192
83    pub fn unit_test_fuzz_iterations(mut self, iterations: usize) -> Self {
84        self.unit_test_fuzz_iterations = iterations;
85        self
86    }
87
88    /// Executes the given closure with a single instance of the compiled simulation.
89    pub fn with_instance<T>(self, thunk: impl FnOnce(CompiledSimInstance) -> T) -> T {
90        self.compiled().with_instance(thunk)
91    }
92
93    /// Uses a fuzzing strategy to explore possible executions of the simulation. The provided
94    /// closure will be repeatedly executed with instances of the Hydro program where the
95    /// batching boundaries, order of messages, and retries are varied.
96    ///
97    /// During development, you should run the test that invokes this function with the `cargo sim`
98    /// command, which will use `libfuzzer` to intelligently explore the execution space. If a
99    /// failure is found, a minimized test case will be produced in a `sim-failures` directory.
100    /// When running the test with `cargo test` (such as in CI), if a reproducer is found it will
101    /// be executed, and if no reproducer is found a small number of random executions will be
102    /// performed.
103    pub fn fuzz(self, thunk: impl AsyncFn() + RefUnwindSafe) {
104        self.compiled().fuzz(thunk)
105    }
106
107    /// Exhaustively searches all possible executions of the simulation. The provided
108    /// closure will be repeatedly executed with instances of the Hydro program where the
109    /// batching boundaries, order of messages, and retries are varied.
110    ///
111    /// Exhaustive searching is feasible when the inputs to the Hydro program are finite and there
112    /// are no dataflow loops that generate infinite messages. Exhaustive searching provides a
113    /// stronger guarantee of correctness than fuzzing, but may take a long time to complete.
114    /// Because no fuzzer is involved, you can run exhaustive tests with `cargo test`.
115    ///
116    /// Returns the number of distinct executions explored.
117    pub fn exhaustive(self, thunk: impl AsyncFnMut() + RefUnwindSafe) -> usize {
118        self.compiled().exhaustive(thunk)
119    }
120
121    /// Compiles the simulation into a dynamically loadable library, and returns a handle to it.
122    pub fn compiled(mut self) -> CompiledSim {
123        use std::collections::BTreeMap;
124
125        use dfir_lang::graph::{eliminate_extra_unions_tees, partition_graph};
126
127        let mut sim_emit = SimBuilder {
128            process_graphs: BTreeMap::new(),
129            cluster_graphs: BTreeMap::new(),
130            process_tick_dfirs: BTreeMap::new(),
131            cluster_tick_dfirs: BTreeMap::new(),
132            extra_stmts_global: vec![],
133            extra_stmts_cluster: BTreeMap::new(),
134            next_hoff_id: 0,
135            test_safety_only: self.test_safety_only,
136            skip_consistency_assertions: self.skip_consistency_assertions,
137        };
138
139        // Ensure the default (0) external is always present.
140        self.externals.insert(
141            LocationKey::FIRST,
142            SimExternal {
143                shared_inner: self.externals_port_registry.clone(),
144            },
145        );
146
147        let mut seen_tees_instantiate: HashMap<_, _> = HashMap::new();
148        let mut seen_cluster_members = HashSet::new();
149        self.ir.iter_mut().for_each(|leaf| {
150            leaf.compile_network::<SimDeploy>(
151                &mut SparseSecondaryMap::new(),
152                &mut seen_tees_instantiate,
153                &mut seen_cluster_members,
154                &self.processes,
155                &self.clusters,
156                &self.externals,
157                &mut (),
158            );
159        });
160
161        let mut seen_tees = HashMap::new();
162        let mut built_tees = HashMap::new();
163        let mut next_stmt_id = 0;
164        for leaf in &mut self.ir {
165            leaf.emit(
166                &mut sim_emit,
167                &mut seen_tees,
168                &mut built_tees,
169                &mut next_stmt_id,
170            );
171        }
172
173        fn build_graphs(
174            graphs: BTreeMap<LocationId, FlatGraphBuilder>,
175        ) -> BTreeMap<LocationId, DfirGraph> {
176            graphs
177                .into_iter()
178                .map(|(l, g)| {
179                    let FlatGraphBuilderOutput { mut flat_graph, .. } =
180                        g.build().expect("Failed to build DFIR flat graph.");
181                    eliminate_extra_unions_tees(&mut flat_graph);
182                    (
183                        l,
184                        partition_graph(flat_graph).expect("Failed to partition (cycle detected)."),
185                    )
186                })
187                .collect()
188        }
189
190        let process_graphs = build_graphs(sim_emit.process_graphs);
191        let cluster_graphs = build_graphs(sim_emit.cluster_graphs);
192        let process_tick_graphs = build_graphs(sim_emit.process_tick_dfirs);
193        let cluster_tick_graphs = build_graphs(sim_emit.cluster_tick_dfirs);
194
195        #[expect(
196            clippy::disallowed_methods,
197            reason = "nondeterministic iteration order, fine for checks"
198        )]
199        for c in self.clusters.keys() {
200            assert!(
201                self.cluster_max_sizes.contains_key(c),
202                "Cluster {:?} missing max size; call with_cluster_size() before compiled()",
203                c
204            );
205        }
206
207        let (bin, trybuild) = create_sim_graph_trybuild(
208            process_graphs,
209            cluster_graphs,
210            self.cluster_max_sizes,
211            process_tick_graphs,
212            cluster_tick_graphs,
213            sim_emit.extra_stmts_global,
214            sim_emit.extra_stmts_cluster,
215        );
216
217        let out = compile_sim(bin, trybuild).unwrap();
218        let lib = unsafe { Library::new(&out).unwrap() };
219
220        CompiledSim {
221            _path: out,
222            lib,
223            externals_port_registry: self.externals_port_registry.take(),
224            unit_test_fuzz_iterations: self.unit_test_fuzz_iterations,
225        }
226    }
227}