From 0e87db6d9020b7400b1697ae3296177ed062e8e8 Mon Sep 17 00:00:00 2001 From: tignear Date: Fri, 23 Oct 2026 13:17:33 +0900 Subject: [PATCH 1/2] perf(runtime)!: compare counted wait baselines in place A wait term on a counted signal kept the signal's value at the last check as a pair of BigUints: every check read the signal into new BigUints and compared them. Like an uncounted plain signal, it now keeps the signal's plane image (Baseline::RawCounter) and compares it with the state in place. Counter remains for signals without a plane image and for baselines loaded from a state file, which become RawCounter at their first check; a RawCounter saves as the same Counter record. BREAKING CHANGE: celox_runtime::timing::Baseline has a new variant, RawCounter. Change-Id: Icaf42a4570d829af240927beaeafcb3642cf245b --- crates/celox-runtime/src/simulation/wait.rs | 68 ++++++++++++++++----- crates/celox/src/simulator/state_file.rs | 26 +++++--- 2 files changed, 72 insertions(+), 22 deletions(-) diff --git a/crates/celox-runtime/src/simulation/wait.rs b/crates/celox-runtime/src/simulation/wait.rs index f8497a81a6d..1bb95a744e9 100644 --- a/crates/celox-runtime/src/simulation/wait.rs +++ b/crates/celox-runtime/src/simulation/wait.rs @@ -48,9 +48,24 @@ impl SimulationState { for (term, baseline) in terms.iter().zip(&mut waiter.baselines) { match term.counter { Some(counter) => { - *baseline = Baseline::Counter { - seen: backend.get_as(counter), - value: read(backend, term.signal), + let seen = backend.get_as(counter); + match planes(backend, term.signal) { + Some(image) => { + if let Baseline::RawCounter { seen: last, raw } = baseline { + *last = seen; + copy_planes(raw, image, term.signal.width); + } else { + let mut raw = Vec::with_capacity(image.len()); + copy_planes(&mut raw, image, term.signal.width); + *baseline = Baseline::RawCounter { seen, raw }; + } + } + None => { + *baseline = Baseline::Counter { + seen, + value: read(backend, term.signal), + } + } } } None => match planes(backend, term.signal) { @@ -128,20 +143,40 @@ impl SimulationState { continue; } let baseline = &mut waiter.baselines[index]; + // The common term: a plain signal no counter counts. + if let Baseline::Raw(last) = baseline { + let now = state.planes(term.signal).expect("a plain signal"); + wake |= Self::advance(term.kind, last, now, term.signal.width); + continue; + } + let counted = |seen: u32| { + term.counter + .is_some_and(|counter| backend.get_as::(counter) != seen) + }; wake |= match baseline { - Baseline::Counter { seen, value } => { - let counted = term - .counter - .is_some_and(|counter| backend.get_as::(counter) != *seen); - let now = read(backend, term.signal); - let changed = occurs(term.kind, value, &now); - *value = now; - counted || changed - } - Baseline::Raw(last) => { + Baseline::RawCounter { seen, raw } => { let now = state.planes(term.signal).expect("a plain signal"); - Self::advance(term.kind, last, now, term.signal.width) + let changed = Self::advance(term.kind, raw, now, term.signal.width); + counted(*seen) || changed } + Baseline::Counter { seen, value } => match state.planes(term.signal) { + // A baseline loaded from a state file. + Some(now) => { + let seen = *seen; + let mut raw = encode_planes(value, term.signal.width, now.len()); + let changed = + Self::advance(term.kind, &mut raw, now, term.signal.width); + *baseline = Baseline::RawCounter { seen, raw }; + counted(seen) || changed + } + None => { + let now = read(backend, term.signal); + let changed = occurs(term.kind, value, &now); + *value = now; + counted(*seen) || changed + } + }, + Baseline::Raw(_) => unreachable!("tested above"), Baseline::Value(last) => match state.planes(term.signal) { // A baseline loaded from a state file. Some(now) => { @@ -191,7 +226,7 @@ impl SimulationState { } match baseline { // Counters are bumped by the write itself. - Baseline::Counter { .. } => {} + Baseline::Counter { .. } | Baseline::RawCounter { .. } => {} Baseline::Raw(last) => { let now = encode_planes(value, signal.width, last.len()); wake |= Self::advance(term.kind, last, &now, signal.width); @@ -224,6 +259,9 @@ pub enum Baseline { /// (see [`planes`]) with the bits above its width cleared. Checks /// compare it with the state in place. Raw(Vec), + /// Like `Counter`, with the signal's state at the last check kept as + /// in [`Baseline::Raw`]. + RawCounter { seen: u32, raw: Vec }, } /// A process suspended at a wait. diff --git a/crates/celox/src/simulator/state_file.rs b/crates/celox/src/simulator/state_file.rs index 50b7218fed1..ecf643082d2 100644 --- a/crates/celox/src/simulator/state_file.rs +++ b/crates/celox/src/simulator/state_file.rs @@ -533,6 +533,13 @@ impl Simulator { .collect() }) .unwrap_or_default(); + let split = |term: usize, image: &[u8]| { + let bytes = widths + .get(term) + .map_or(image.len(), |width| width.div_ceil(8).max(1)) + .min(image.len()); + (image[..bytes].to_vec(), image[bytes..].to_vec()) + }; WaiterRecord { process: process(index), site: waiter.site as u64, @@ -554,13 +561,18 @@ impl Simulator { mask: mask.to_bytes_le(), }, Baseline::Raw(image) => { - let bytes = widths - .get(term) - .map_or(image.len(), |width| width.div_ceil(8).max(1)) - .min(image.len()); - BaselineRecord::Value { - value: image[..bytes].to_vec(), - mask: image[bytes..].to_vec(), + let (value, mask) = split(term, image); + BaselineRecord::Value { value, mask } + } + Baseline::RawCounter { seen, raw } => { + // The bytes a `Counter` baseline's value saves as. + let (value, mask) = split(term, raw); + let number = + |bytes: &[u8]| BigUint::from_bytes_le(bytes).to_bytes_le(); + BaselineRecord::Counter { + seen: *seen, + value: number(&value), + mask: number(&mask), } } }) From dcebf338a554a790511661d5d52331952987e74d Mon Sep 17 00:00:00 2001 From: tignear Date: Fri, 23 Oct 2026 13:17:33 +0900 Subject: [PATCH 2/2] test(sv): keep counted wait baselines across a state file Change-Id: I1a6d04d2baedc95cd44502fe1d7c769680f54ec2 --- .../tests/frontends/systemverilog/timing.rs | 85 +++++++++++++++++++ 1 file changed, 85 insertions(+) diff --git a/crates/celox/tests/frontends/systemverilog/timing.rs b/crates/celox/tests/frontends/systemverilog/timing.rs index 204f36c4992..9a1070623df 100644 --- a/crates/celox/tests/frontends/systemverilog/timing.rs +++ b/crates/celox/tests/frontends/systemverilog/timing.rs @@ -748,3 +748,88 @@ fn runtime_error_before_a_commit_leaves_no_held_writes() { } } } + +/// The baselines of waits on a counted variable survive a state file: a +/// simulation loaded between two pulses of `y` wakes the waiters as the +/// uninterrupted one does, and saves the same baselines again. +#[test] +fn counted_wait_baselines_survive_a_state_file() { + let source = r#" + module Top(output int n, r); + timeunit 1ns / 1ns; + bit a, b, c; + logic y; + always_comb begin y = 0; if (a) y = 1; c = b; end + initial #1 forever begin @(y); n++; end + initial #1 forever begin @(posedge y); r++; end + initial begin + #2 a = 1; + #1 b = 1; + #1 b = 0; + #1 b = 1; + #1 a = 0; + end + endmodule + "#; + let build = |four_state: bool| { + celox::Simulation::from_sv_sources( + vec![(source, std::path::Path::new("counted_state.sv"))], + "Top", + ) + .four_state(four_state) + .build() + .unwrap() + }; + let through_bytes = |file: &celox::StateFile| { + let mut bytes = Vec::new(); + file.write_to(&mut bytes).unwrap(); + celox::StateFile::read_from(bytes.as_slice()).unwrap() + }; + for four_state in [false, true] { + let counts = |sim: &mut celox::Simulation| { + ["n", "r"].map(|name| { + let signal = sim.signal(name); + sim.get(signal) + }) + }; + let mut whole = build(four_state); + whole.run_until(10).unwrap(); + let expected = counts(&mut whole); + assert_eq!( + expected, + [5u32.into(), 4u32.into()], + "four_state={four_state}" + ); + + let mut first = build(four_state); + first.run_until(3).unwrap(); + let file = through_bytes(&first.save_state().unwrap()); + assert!( + file.schedule + .as_ref() + .unwrap() + .processes + .waiters + .iter() + .flat_map(|waiter| &waiter.baselines) + .any(|baseline| matches!(baseline, celox::BaselineRecord::Counter { .. })), + "four_state={four_state}: `y` is counted" + ); + let mut loaded = build(four_state); + loaded.load_state(&file).unwrap(); + assert_eq!( + through_bytes(&loaded.save_state().unwrap()), + file, + "four_state={four_state}" + ); + loaded.run_until(10).unwrap(); + assert_eq!(counts(&mut loaded), expected, "four_state={four_state}"); + // A baseline taken at a check in this run saves as the loaded one. + first.run_until(4).unwrap(); + loaded = build(four_state); + let file = through_bytes(&first.save_state().unwrap()); + loaded.load_state(&file).unwrap(); + loaded.run_until(10).unwrap(); + assert_eq!(counts(&mut loaded), expected, "four_state={four_state}"); + } +}