Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -175,16 +175,16 @@ jobs:
cargo doc --locked --no-deps "${args[@]}"

- uses: pnpm/action-setup@v6
if: needs.changes.result != 'success' || needs.changes.outputs.javascript == 'true'
if: needs.changes.result != 'success' || needs.changes.outputs.javascript == 'true' || needs.changes.outputs.scripts == 'true'

- uses: actions/setup-node@v7
if: needs.changes.result != 'success' || needs.changes.outputs.javascript == 'true'
if: needs.changes.result != 'success' || needs.changes.outputs.javascript == 'true' || needs.changes.outputs.scripts == 'true'
with:
node-version: 24
cache: pnpm

- name: Install dependencies
if: needs.changes.result != 'success' || needs.changes.outputs.javascript == 'true'
if: needs.changes.result != 'success' || needs.changes.outputs.javascript == 'true' || needs.changes.outputs.scripts == 'true'
run: pnpm install --frozen-lockfile --ignore-scripts

- name: Biome check
Expand Down
5 changes: 5 additions & 0 deletions .github/workflows/pr-title.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ permissions:
jobs:
conventional-title:
name: Conventional Commit title
# Edits can arrive after closure. Do not allocate a runner for those events.
# Keep all open-PR edits: a skipped required check could mask a prior failure.
if: >-
github.event_name == 'merge_group' ||
github.event.pull_request.state == 'open'
runs-on: ubuntu-latest
steps:
- name: Check out the trusted title validator
Expand Down
4 changes: 4 additions & 0 deletions crates/celox-design/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -457,6 +457,9 @@ pub struct ProcessSlots<A> {
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,
Expand All @@ -467,6 +470,7 @@ impl<A> ProcessSlots<A> {
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),
Expand Down
33 changes: 29 additions & 4 deletions crates/celox-frontend-core/src/process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<SourceVarId> {
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<RegionedSourceAddr> {
Expand Down Expand Up @@ -135,20 +144,36 @@ 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<u64>,
) -> Result<(), ProcessKernelError> {
self.note_delay(constant);
self.store(self.slots.delay, PROCESS_DELAY_WIDTH, amount);
self.suspend(ProcessStatus::Delay)
}

/// 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<u64>,
) -> 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<u64>) {
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.
Expand Down
24 changes: 22 additions & 2 deletions crates/celox-frontend-core/src/sdk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<u64> {
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,
Expand Down Expand Up @@ -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,
Expand All @@ -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 } => {
Expand Down Expand Up @@ -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),
Expand All @@ -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())
Expand Down
3 changes: 2 additions & 1 deletion crates/celox-frontend-sv/src/lowering.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4080,6 +4080,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),
Expand Down Expand Up @@ -4124,7 +4125,7 @@ fn lower_initial_processes(
}));
Ok(SymbolicProcess {
kernel: kernel.unit,
slots,
slots: kernel.slots,
origin: process_origin(process),
})
})
Expand Down
15 changes: 14 additions & 1 deletion crates/celox-frontend-sv/src/lowering/ff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4285,10 +4285,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,
Expand Down Expand Up @@ -4431,13 +4433,23 @@ 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)?,
ticks_per_precision,
),
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)?;
Expand Down Expand Up @@ -4486,7 +4498,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)
}

Expand Down Expand Up @@ -4846,6 +4858,7 @@ impl Ff<'_, '_> {
/// The kernel of a process and its wait and nonblocking assignment sites.
pub(super) struct KernelUnit {
pub unit: ExecutionUnit<Addr>,
pub slots: ProcessSlots<SourceVarId>,
pub wait_sites: Vec<WaitSite<SourceVarId>>,
pub nba_sites: Vec<NbaSite<SourceVarId>>,
pub host_request_sites: Vec<celox_design::HostRequestSite<SourceVarId>>,
Expand Down
4 changes: 2 additions & 2 deletions crates/celox-runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ 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, UnitExecutor,
};
pub use state_file::{
BaselineRecord, NbaRecord, ProcessRecord, ScheduleRecord, ScheduledEvent, StateDifference,
Expand Down
57 changes: 56 additions & 1 deletion crates/celox-runtime/src/simulation/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ mod nba;
mod persist;
mod process;
mod round;
mod stats;
mod time;
mod wait;

Expand All @@ -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;
Expand Down Expand Up @@ -118,6 +120,8 @@ pub struct SimulationState<B: SimBackend> {
return_budget: u64,
/// Rounds and process-phase passes the current time slot has run.
slot_passes: u64,
/// The loop that runs time slots, and the slots each loop ran.
stats: SchedulerStats,
}

impl<B: SimBackend> SimulationState<B> {
Expand Down Expand Up @@ -169,7 +173,12 @@ impl<B: SimBackend> SimulationState<B> {
round_budget: SLOT_ROUND_BUDGET,
return_budget: SLOT_RETURN_BUDGET,
slot_passes: 0,
stats: SchedulerStats::default(),
};
state.stats.kernels = vec![
KernelSchedule::Reference("the design has no compiled scheduler");
state.model.processes.len()
];
state.synchronize_event_values(backend);
state
}
Expand Down Expand Up @@ -215,6 +224,52 @@ impl<B: SimBackend> SimulationState<B> {
}
}

/// Run every time slot with work up to and including `end`, then move
/// to `end`. A target before the current time changes nothing. Returns
/// whether the simulation is at `end` afterwards; it is not when a
/// process stopped it (`$stop`) or finished it on the way.
pub fn run_until<E>(&mut self, executor: &mut E, end: u64) -> Result<bool, SimulatorErrorCode>
where
E: SimulationExecutor<Backend = B>,
{
if end < self.scheduler.time {
// Time only moves forward. Moving the timestamp back while the
// design keeps the state it reached would leave the two
// describing different moments, so an earlier target is a no-op.
return Ok(false);
}
while let Some(next_time) = self.next_event_time() {
if next_time > end {
break;
}
self.step_until(executor, end)?;
if self.stopped {
return Ok(false);
}
}
if self.finished {
return Ok(false);
}
self.advance_time(executor.backend_mut(), end);
Ok(true)
}

/// Select the loop that runs time slots.
pub fn set_scheduler_mode(&mut self, mode: SchedulerMode) {
self.stats.mode = mode;
}

/// The loop that runs time slots, what it ran and how each process
/// kernel is scheduled.
pub fn scheduler_stats(&self) -> &SchedulerStats {
&self.stats
}

/// Count a step checked against a shadow simulation.
pub fn note_checked_step(&mut self) {
self.stats.checked_steps += 1;
}

/// Complete a boundary reached from an entry point that returns runtime
/// errors rather than scheduler control halts.
fn finish_timed_boundary<E>(
Expand Down
Loading
Loading