diff --git a/crates/celox-design/src/lib.rs b/crates/celox-design/src/lib.rs
index f1b9fbe01c7..50e687c8a08 100644
--- a/crates/celox-design/src/lib.rs
+++ b/crates/celox-design/src/lib.rs
@@ -457,6 +457,9 @@ pub struct ProcessSlots {
pub is_initializer: bool,
/// This kernel originated from an edge procedure and must execute in Simulation.
pub requires_timed_simulation: bool,
+ /// Every delay the kernel suspends for is a constant of at least one
+ /// time unit.
+ pub const_delays: bool,
pub resume: A,
pub status: A,
pub delay: A,
@@ -467,6 +470,7 @@ impl ProcessSlots {
ProcessSlots {
is_initializer: self.is_initializer,
requires_timed_simulation: self.requires_timed_simulation,
+ const_delays: self.const_delays,
resume: map(self.resume),
status: map(self.status),
delay: map(self.delay),
diff --git a/crates/celox-frontend-core/src/process.rs b/crates/celox-frontend-core/src/process.rs
index a34363cb372..8f8c1373831 100644
--- a/crates/celox-frontend-core/src/process.rs
+++ b/crates/celox-frontend-core/src/process.rs
@@ -72,12 +72,21 @@ impl ProcessKernelBuilder {
let start = builder.current_block();
Self {
builder,
- slots,
+ slots: ProcessSlots {
+ const_delays: true,
+ ..slots
+ },
resume_blocks: vec![start],
wait_site: None,
}
}
+ /// The kernel's control slots, with [`ProcessSlots::const_delays`] set
+ /// for the delays lowered so far.
+ pub fn slots(&self) -> ProcessSlots {
+ self.slots
+ }
+
/// The SIR builder positioned where the process continues. After
/// [`Self::finish`] no block is open until the caller switches to one.
pub fn builder(&mut self) -> &mut SIRBuilder {
@@ -135,8 +144,15 @@ impl ProcessKernelBuilder {
/// Suspend for the number of time units in `amount`, an unsigned
/// two-state register of [`PROCESS_DELAY_WIDTH`] bits, and continue at
/// a new resume point. A zero delay resumes after the nonblocking
- /// assignments of the time are published.
- pub fn delay(&mut self, amount: RegisterId) -> Result<(), ProcessKernelError> {
+ /// assignments of the time are published. `constant` is the amount's
+ /// value when the frontend knows it, which it records in
+ /// [`ProcessSlots::const_delays`].
+ pub fn delay(
+ &mut self,
+ amount: RegisterId,
+ constant: Option,
+ ) -> Result<(), ProcessKernelError> {
+ self.note_delay(constant);
self.store(self.slots.delay, PROCESS_DELAY_WIDTH, amount);
self.suspend(ProcessStatus::Delay)
}
@@ -144,11 +160,20 @@ impl ProcessKernelBuilder {
/// A delay with the scheduling of IEEE 1800-2023: a zero delay resumes
/// in the inactive region of the same time (4.4.2.3), before
/// nonblocking assignments are published.
- pub fn inactive_delay(&mut self, amount: RegisterId) -> Result<(), ProcessKernelError> {
+ pub fn inactive_delay(
+ &mut self,
+ amount: RegisterId,
+ constant: Option,
+ ) -> Result<(), ProcessKernelError> {
+ self.note_delay(constant);
self.store(self.slots.delay, PROCESS_DELAY_WIDTH, amount);
self.suspend(ProcessStatus::InactiveDelay)
}
+ fn note_delay(&mut self, constant: Option) {
+ self.slots.const_delays &= constant.is_some_and(|amount| amount > 0);
+ }
+
/// Return to the runtime, which charges its time-slot budget and
/// resumes the process at once. Frontends place this in loops without
/// timing controls so that a runaway process is reported, not hung in.
diff --git a/crates/celox-frontend-core/src/sdk.rs b/crates/celox-frontend-core/src/sdk.rs
index 2a5196e29ef..f3da9a7af9f 100644
--- a/crates/celox-frontend-core/src/sdk.rs
+++ b/crates/celox-frontend-core/src/sdk.rs
@@ -481,6 +481,23 @@ fn coerce_sir_register(
Ok(current)
}
+/// The value of expression `id` as a delay amount, if it is a two-state
+/// constant whose coercion to [`PROCESS_DELAY_WIDTH`] bits keeps its value.
+fn constant_delay(artifact: &FrontendArtifact, id: ExprId) -> Option {
+ let expression = artifact.expression(id)?;
+ let ExprNode::Constant(value) = expression.node() else {
+ return None;
+ };
+ let value_type = expression.value_type();
+ let negative = value_type.is_signed()
+ && value_type.width() > 0
+ && value.payload().bit(value_type.width() as u64 - 1);
+ if value.mask().bits() != 0 || negative {
+ return None;
+ }
+ u64::try_from(value.payload()).ok()
+}
+
fn coerce_sir_expression(
artifact: &FrontendArtifact,
id: ExprId,
@@ -1319,6 +1336,7 @@ fn lower_statements(
return Ok(false);
}
Statement::Delay { amount } | Statement::InactiveDelay { amount } => {
+ let constant = constant_delay(artifact, *amount);
let amount = coerce_sir_expression(
artifact,
*amount,
@@ -1327,9 +1345,9 @@ fn lower_statements(
&mut cache,
)?;
if matches!(statement, Statement::Delay { .. }) {
- kernel.delay(amount)?;
+ kernel.delay(amount, constant)?;
} else {
- kernel.inactive_delay(amount)?;
+ kernel.inactive_delay(amount, constant)?;
}
}
Statement::WaitEvent { terms } => {
@@ -1713,6 +1731,7 @@ fn lower_processes(
let slots = ProcessSlots {
is_initializer: false,
requires_timed_simulation: false,
+ const_delays: false,
resume: lowering
.hidden
.declare(name("resume"), PROCESS_RESUME_WIDTH, false),
@@ -1728,6 +1747,7 @@ fn lower_processes(
lowering.process_nba_sites = 0;
let mut kernel = ProcessKernelBuilder::new(slots);
lower_statements(&mut lowering, &mut kernel, process.body())?;
+ let slots = kernel.slots();
let kernel = kernel.build();
candidates.extend(crate::watch::multi_store_candidates(&kernel, |address| {
stable_signal(address, artifact.signals().len())
diff --git a/crates/celox-frontend-sv/src/lowering.rs b/crates/celox-frontend-sv/src/lowering.rs
index 751a066eef8..440413e53e0 100644
--- a/crates/celox-frontend-sv/src/lowering.rs
+++ b/crates/celox-frontend-sv/src/lowering.rs
@@ -4089,6 +4089,7 @@ fn lower_initial_processes(
ProcessSlots {
is_initializer: process.is_initializer(),
requires_timed_simulation: process.is_scheduled_edge(),
+ const_delays: false,
resume: declare("resume", PROCESS_RESUME_WIDTH),
status: declare("status", PROCESS_STATUS_WIDTH),
delay: declare("delay", PROCESS_DELAY_WIDTH),
@@ -4133,7 +4134,7 @@ fn lower_initial_processes(
}));
Ok(SymbolicProcess {
kernel: kernel.unit,
- slots,
+ slots: kernel.slots,
origin: process_origin(process),
})
})
diff --git a/crates/celox-frontend-sv/src/lowering/ff.rs b/crates/celox-frontend-sv/src/lowering/ff.rs
index 3a992de49cd..5ed7466401a 100644
--- a/crates/celox-frontend-sv/src/lowering/ff.rs
+++ b/crates/celox-frontend-sv/src/lowering/ff.rs
@@ -4288,10 +4288,12 @@ impl Ff<'_, '_> {
self.exec_block(body)?;
let mut kernel = self.kernel.take().expect("lowering a process kernel");
std::mem::swap(&mut self.b, kernel.builder());
+ let slots = kernel.slots();
let mut unit = kernel.build();
prune_unreachable_blocks(&mut unit);
Ok(KernelUnit {
unit,
+ slots,
wait_sites: self.wait_sites,
nba_sites: self.nba_sites,
host_request_sites: self.host_request_sites,
@@ -4434,6 +4436,9 @@ impl Ff<'_, '_> {
ticks_per_unit: u64,
ticks_per_precision: u64,
) -> Result<(), sv::AnalyzerError> {
+ // Only integral constants are recorded; a real delay counts as
+ // variable.
+ let mut constant = None;
let (mut delay, ticks_per_step) = match self.m.expr_float_precision(amount) {
Some(precision) => (
self.real_delay(amount, precision, ticks_per_unit / ticks_per_precision)?,
@@ -4441,6 +4446,13 @@ impl Ff<'_, '_> {
),
None => {
let signed = self.expr_signed(amount);
+ constant = self
+ .eval_const(amount)?
+ .filter(|(value, width)| {
+ !(signed && *width > 0 && value.bit(*width as u64 - 1))
+ })
+ .and_then(|(value, _)| u64::try_from(&value).ok())
+ .and_then(|value| value.checked_mul(ticks_per_unit));
let mut arena = SLTNodeArena::new();
let node = self.expr_slt(amount, None, &mut arena)?;
let original = self.lower_slt(&arena, node)?;
@@ -4489,7 +4501,7 @@ impl Ff<'_, '_> {
));
delay = product;
}
- self.with_kernel(|kernel| kernel.inactive_delay(delay))
+ self.with_kernel(|kernel| kernel.inactive_delay(delay, constant))
.map_err(kernel_error)
}
@@ -4849,6 +4861,7 @@ impl Ff<'_, '_> {
/// The kernel of a process and its wait and nonblocking assignment sites.
pub(super) struct KernelUnit {
pub unit: ExecutionUnit,
+ pub slots: ProcessSlots,
pub wait_sites: Vec>,
pub nba_sites: Vec>,
pub host_request_sites: Vec>,
diff --git a/crates/celox-heap/src/lib.rs b/crates/celox-heap/src/lib.rs
index 09ebcd75fa8..644e75b2a20 100644
--- a/crates/celox-heap/src/lib.rs
+++ b/crates/celox-heap/src/lib.rs
@@ -150,6 +150,16 @@ impl Heap {
self.strings.contains_key(&handle)
}
+ /// Whether `other` holds the same strings under the same handles, the
+ /// same strings under construction, and names new strings alike.
+ pub fn same_contents(&self, other: &Heap) -> bool {
+ self.strings == other.strings
+ && self.next == other.next
+ && self.buffers == other.buffers
+ && self.free_buffers == other.free_buffers
+ && self.pinned == other.pinned
+ }
+
/// The number of live strings, including `""`.
pub fn len(&self) -> usize {
self.strings.len()
diff --git a/crates/celox-runtime/src/lib.rs b/crates/celox-runtime/src/lib.rs
index 2d2b872cc15..f0c637d01e0 100644
--- a/crates/celox-runtime/src/lib.rs
+++ b/crates/celox-runtime/src/lib.rs
@@ -35,8 +35,9 @@ pub use reflection::{
};
pub use schedule::{ResolvedSchedule, host_watch};
pub use simulation::{
- EventInfo, HostBoundary, ScheduleParts, SimulationExecutor, SimulationSnapshot,
- SimulationState, UnitExecutor,
+ EventInfo, HostBoundary, KernelSchedule, ScheduleKey, ScheduleParts, SchedulerMode,
+ SchedulerStats, SimulationExecutor, SimulationSnapshot, SimulationState, StepObserver,
+ UnitExecutor,
};
pub use state_file::{
BaselineRecord, NbaRecord, ProcessRecord, ScheduleRecord, ScheduledEvent, StateDifference,
diff --git a/crates/celox-runtime/src/simulation/mod.rs b/crates/celox-runtime/src/simulation/mod.rs
index 5a6766970e6..81e6885be18 100644
--- a/crates/celox-runtime/src/simulation/mod.rs
+++ b/crates/celox-runtime/src/simulation/mod.rs
@@ -8,6 +8,7 @@ mod nba;
mod persist;
mod process;
mod round;
+mod stats;
mod time;
mod wait;
@@ -19,7 +20,8 @@ use celox_design::CounterKind;
pub(crate) use batch::{edge_level, event_level};
pub use host::{HostBoundary, HostWatch, SimulationExecutor, UnitExecutor};
pub use nba::NbaUpdate;
-pub use persist::{ScheduleParts, SimulationSnapshot};
+pub use persist::{ScheduleKey, ScheduleParts, SimulationSnapshot};
+pub use stats::{KernelSchedule, SchedulerMode, SchedulerStats};
pub use wait::{Baseline, Waiter, Waiters};
use self::round::Round;
@@ -82,6 +84,38 @@ struct Suspended {
levels: Vec,
}
+/// Sees the steps of [`SimulationState::run_until_observed`] and
+/// [`SimulationState::step_until_observed`].
+pub trait StepObserver {
+ /// Called before a step, with the state it starts from.
+ fn before_step(&mut self, state: &SimulationState, executor: &mut E);
+
+ /// Called after a step to at most `limit` that returned `result`. An
+ /// error replaces the step's result.
+ fn after_step(
+ &mut self,
+ state: &SimulationState,
+ executor: &mut E,
+ limit: u64,
+ result: &Result