diff --git a/crates/celox/src/simulation.rs b/crates/celox/src/simulation.rs index 839986f734b..b190fbbe557 100644 --- a/crates/celox/src/simulation.rs +++ b/crates/celox/src/simulation.rs @@ -57,6 +57,8 @@ impl std::fmt::Debug for SimulationCheckpoint { /// Manages simulation time, periodic clocks, and an event queue. /// /// The default type parameter uses the host's [`crate::DefaultBackend`]. +/// The builder also offers `build_native`, `build_cranelift`, `build_wasm`, +/// and `build_interpreter`; thread and partition options apply to timed builds. pub struct Simulation { pub(crate) simulator: Simulator, pub(crate) state: SimulationState, @@ -714,7 +716,10 @@ mod process_tests { assert!(sim.is_finished()); assert_eq!(sim.time(), steps); assert_eq!(sim.get(ticks), steps.into()); - assert_eq!(sim.get(count), ((steps + 5) / 10).into()); + assert_eq!( + sim.get_four_state(count), + (((steps + 5) / 10).into(), 0u8.into()) + ); } fn builder(four_state: bool, steps: usize) -> SimulatorBuilder<'static, Simulator> { @@ -724,16 +729,16 @@ mod process_tests { } fn check_all_backends(four_state: bool, steps: usize) { + let timed = || Simulation::from_frontend(design(four_state, steps)).four_state(four_state); + check(timed().build_interpreter().unwrap(), steps as u64); + check(timed().build_cranelift().unwrap(), steps as u64); + check(timed().build_wasm().unwrap(), steps as u64); check( - Simulation::new(builder(four_state, steps).build_interpreter().unwrap()), - steps as u64, - ); - check( - Simulation::new(builder(four_state, steps).build_cranelift().unwrap()), - steps as u64, - ); - check( - Simulation::new(builder(four_state, steps).build_wasm().unwrap()), + timed() + .threads(4) + .parallel_partition(crate::ParallelPartition::Always) + .build_cranelift() + .unwrap(), steps as u64, ); check( @@ -745,8 +750,13 @@ mod process_tests { all(target_arch = "aarch64", not(feature = "x86_64-codegen")) ))] { + check(timed().build_native().unwrap(), steps as u64); check( - Simulation::new(builder(four_state, steps).build_native().unwrap()), + timed() + .threads(4) + .parallel_partition(crate::ParallelPartition::Always) + .build_native() + .unwrap(), steps as u64, ); let image = builder(four_state, steps) diff --git a/crates/celox/src/simulator/builder.rs b/crates/celox/src/simulator/builder.rs index e578ae835fa..ba64dbdfa88 100644 --- a/crates/celox/src/simulator/builder.rs +++ b/crates/celox/src/simulator/builder.rs @@ -1487,7 +1487,7 @@ mod host { /// /// Use [`Simulator::builder()`] or [`Simulation::builder()`](crate::Simulation::builder) /// to obtain the appropriate variant. Both share the same configuration methods; - /// only `.build()` differs in return type. + /// their build methods return the corresponding simulator or timed simulation. pub struct SimulatorBuilder<'a, Target = Simulator> { sources: Vec<(&'a str, &'a Path)>, sv_sources: Vec<(&'a str, &'a Path)>, @@ -2124,7 +2124,9 @@ mod host { _marker: std::marker::PhantomData, } } + } + impl<'a, Target> SimulatorBuilder<'a, Target> { /// Compile SIR, finalize its state layout, and return the typed artifact /// along with the remaining builder state. /// Consumes self. @@ -2265,7 +2267,9 @@ mod host { self.injected_components, )) } + } + impl<'a> SimulatorBuilder<'a, Simulator> { /// Compiles the Veryl source and constructs the simulator. /// Uses a custom native backend when its generated code matches the /// compilation target, and Cranelift for host-side cross-codegen builds. @@ -2927,88 +2931,102 @@ mod host { } } - /// Compiles the Veryl source and constructs the timed simulation wrapper. - pub fn build(mut self) -> Result { - self.options.emit_triggers = true; - self.enforce_native_force_optimizer(); + /// Compiles the design using the host's default backend. + pub fn build(self) -> Result { #[cfg(any( all(target_arch = "x86_64", not(feature = "arm64-codegen")), all(target_arch = "aarch64", not(feature = "x86_64-codegen")) ))] - let layout_mode = crate::backend::memory_layout::MemoryLayoutMode::ElementStrided; + { + self.build_native() + } #[cfg(not(any( all(target_arch = "x86_64", not(feature = "arm64-codegen")), all(target_arch = "aarch64", not(feature = "x86_64-codegen")) )))] - let layout_mode = crate::backend::memory_layout::MemoryLayoutMode::Packed; - let layout_mode = if self.vcd_path.is_some() { - crate::backend::memory_layout::MemoryLayoutMode::Packed - } else { - layout_mode - }; - let (program, warnings) = if let Some(artifact) = &self.frontend_artifact { - compile_frontend_to_sir_with_layout_mode( - artifact, - &self.ignored_loops, - &self.true_loops, - self.options.four_state, - &self.options.trace, - None, - &self.options.optimize_options, - &self.options.diagnostics, - layout_mode, - )? - } else { - compile_hdl_to_sir_with_layout_mode( - &self.sources, - &self.sv_sources, - self.top, - &self.ignored_loops, - &self.true_loops, - self.options.four_state, - &self.options.trace, - None, - self.metadata, - self.clock_type, - self.reset_type, - &self.param_overrides, - &self.options.optimize_options, - &self.options.diagnostics, - &self.injected_components.manifests(), - layout_mode, - !self.options.native_force_support, - self.options.allow_always_ff_function_effects, - )? - }; - let mut laid_out = - program.into_laid_out_with_mode(self.options.four_state, layout_mode); - if self.vcd_path.is_some() { - laid_out.enable_vcd_tracking(); + { + self.build_cranelift() } + } - if self.options.dead_store_policy != DeadStorePolicy::Off { - run_dead_store_elimination(&mut laid_out, &self.live_signals, &self.options); - } - #[cfg(any( - all(target_arch = "x86_64", not(feature = "arm64-codegen")), - all(target_arch = "aarch64", not(feature = "x86_64-codegen")) - ))] - let backend = crate::backend::native::NativeBackend::new(&laid_out, &self.options)?; - #[cfg(not(any( - all(target_arch = "x86_64", not(feature = "arm64-codegen")), - all(target_arch = "aarch64", not(feature = "x86_64-codegen")) - )))] - let backend = crate::backend::JitBackend::new(&laid_out, &self.options, None)?; + /// Compiles a timed simulation using the Cranelift JIT backend. + pub fn build_cranelift(self) -> Result, SimulatorError> { + self.build_with_backend( + crate::backend::memory_layout::MemoryLayoutMode::Packed, + |program, options| { + let mut trace = crate::debug::CompilationTrace::default(); + let wants_trace = options.trace.pre_optimized_clif + || options.trace.post_optimized_clif + || options.trace.native; + let backend = + JitBackend::new(program, options, wants_trace.then_some(&mut trace))?; + if options.trace.output_to_stdout { + trace.print(); + } + Ok(backend) + }, + ) + } + /// Compiles a timed simulation using the host's native backend. + #[cfg(any( + all(target_arch = "x86_64", not(feature = "arm64-codegen")), + all(target_arch = "aarch64", not(feature = "x86_64-codegen")) + ))] + pub fn build_native( + self, + ) -> Result, SimulatorError> + { + self.build_with_backend( + crate::backend::memory_layout::MemoryLayoutMode::ElementStrided, + crate::backend::native::NativeBackend::new, + ) + } + + /// Compiles a timed simulation using the Wasmtime WASM backend. + pub fn build_wasm( + self, + ) -> Result, SimulatorError> + { + self.build_with_backend( + crate::backend::memory_layout::MemoryLayoutMode::Packed, + crate::backend::wasm_runtime::WasmBackend::new, + ) + } + + /// Builds a timed simulation using the interpreter backend. + pub fn build_interpreter( + self, + ) -> Result, SimulatorError> { + self.build_with_backend( + crate::backend::memory_layout::MemoryLayoutMode::Packed, + crate::backend::InterpBackend::new, + ) + } + + fn build_with_backend( + mut self, + layout_mode: crate::backend::memory_layout::MemoryLayoutMode, + compile: impl FnOnce( + &crate::ir::LaidOutProgram, + &SimulatorOptions, + ) -> Result, + ) -> Result, SimulatorError> { + self.options.emit_triggers = true; + let (laid_out, warnings, options, vcd_path, injected_components) = + self.into_laid_out_program(layout_mode)?; + let backend = compile(&laid_out, &options)?; + // Initial kernels must keep their time-zero gates until the timed + // scheduler starts them; ordinary Simulator builders release them. let mut sim = Simulator::with_backend_and_program_for_simulation( backend, laid_out.into_runtime(), warnings, ); - sim.components.set_injected(self.injected_components); - sim.diagnostics = self.options.diagnostics.clone(); - if let Some(path) = self.vcd_path { - let descs = sim.build_vcd_descs(self.options.four_state); + sim.components.set_injected(injected_components); + sim.diagnostics = options.diagnostics.clone(); + if let Some(path) = vcd_path { + let descs = sim.build_vcd_descs(options.four_state); let vcd_writer = crate::VcdWriter::new(path, &descs) .map_err(|_| SimulatorError::from(crate::RuntimeErrorCode::InternalError))?; sim.vcd_writer = Some(vcd_writer); diff --git a/crates/celox/tests/frontends/systemverilog/suite.rs b/crates/celox/tests/frontends/systemverilog/suite.rs index 1d8099b4fb9..f4126b657a6 100644 --- a/crates/celox/tests/frontends/systemverilog/suite.rs +++ b/crates/celox/tests/frontends/systemverilog/suite.rs @@ -221,15 +221,28 @@ fn build(design: &Design, backend: &str) -> Result> { .map(|source| (source.text.as_str(), source.path.as_path())) .collect::>(); if design.timed { - // `Simulation` builds only the default backend. - if backend != "native" { - return Err(format!("no timed simulation for the {backend} backend").into()); - } let builder = design.parameters.iter().fold( Simulation::from_sv_sources(sources, &design.top).four_state(design.four_state), |builder, (name, value)| builder.param(name, *value), ); - return Ok(Box::new(TimedBackend::new(builder.build()?))); + return Ok(match backend { + "native" => Box::new(TimedBackend::new(builder.build_native()?)), + "cranelift" => Box::new(TimedBackend::new(builder.build_cranelift()?)), + "native-parallel" => Box::new(TimedBackend::new( + builder + .threads(4) + .parallel_partition(celox::ParallelPartition::Always) + .build_native()?, + )), + "cranelift-parallel" => Box::new(TimedBackend::new( + builder + .threads(4) + .parallel_partition(celox::ParallelPartition::Always) + .build_cranelift()?, + )), + "wasm" => Box::new(TimedBackend::new(builder.build_wasm()?)), + _ => return Err(format!("unknown backend: {backend}").into()), + }); } let builder = design.parameters.iter().fold( Simulator::from_sv_sources(sources, &design.top).four_state(design.four_state), diff --git a/crates/celox/tests/frontends/systemverilog/timing.rs b/crates/celox/tests/frontends/systemverilog/timing.rs index c33b86cb3c7..291bb088484 100644 --- a/crates/celox/tests/frontends/systemverilog/timing.rs +++ b/crates/celox/tests/frontends/systemverilog/timing.rs @@ -2,8 +2,7 @@ //! processes, driven through the timed `Simulation`. //! //! Every exclusion names what Celox lacks for the case, so the change that -//! adds it removes the exclusion. `Simulation` builds only the default -//! (native) backend, so the other backends are excluded for every case. +//! adds it removes the exclusion. Supported cases run on every backend. macro_rules! timed_cases { () => {}; @@ -21,26 +20,26 @@ macro_rules! timed_cases { } #[test] - #[ignore = "a timed Simulation builds only the default backend"] + $(#[$attr])* fn cranelift() { crate::suite::run_case($case, "cranelift"); } #[cfg(any(target_arch = "x86_64", target_arch = "aarch64"))] #[test] - #[ignore = "a timed Simulation builds only the default backend"] + $(#[$attr])* fn native_parallel() { crate::suite::run_case($case, "native-parallel"); } #[test] - #[ignore = "a timed Simulation builds only the default backend"] + $(#[$attr])* fn cranelift_parallel() { crate::suite::run_case($case, "cranelift-parallel"); } #[test] - #[ignore = "a timed Simulation builds only the default backend"] + $(#[$attr])* fn wasm() { crate::suite::run_case($case, "wasm"); }