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
10 changes: 9 additions & 1 deletion crates/celox-design/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -542,6 +542,9 @@ pub enum HostRequestKind {
Scan,
Distribution,
Process,
// Append new variants: native images persist Serde variant indices.
/// Services owned by an explicitly invoked testbench kernel.
Testbench,
}

impl HostRequestKind {
Expand All @@ -554,6 +557,7 @@ impl HostRequestKind {
Self::Scan => 4,
Self::Distribution => 5,
Self::Process => 6,
Self::Testbench => 7,
};
(family << 32) | site as u64
}
Expand All @@ -567,6 +571,7 @@ impl HostRequestKind {
4 => Self::Scan,
5 => Self::Distribution,
6 => Self::Process,
7 => Self::Testbench,
_ => return None,
};
Some((family, request as u32))
Expand Down Expand Up @@ -707,6 +712,8 @@ pub enum ProcessKind {
Regular,
/// A final procedure runs only when simulation ends (IEEE 1800-2023 9.2.3).
Final,
/// Invoked explicitly by its caller; never enqueued as an initial procedure.
Callable,
}

/// A pure expression evaluated with the storage of one waiting activation.
Expand Down Expand Up @@ -2263,6 +2270,7 @@ mod tests {
HostRequestKind::Scan,
HostRequestKind::Distribution,
HostRequestKind::Process,
HostRequestKind::Testbench,
] {
for site in [0, 1, u32::MAX] {
assert_eq!(
Expand All @@ -2271,7 +2279,7 @@ mod tests {
);
}
}
for invalid in [0, u32::MAX as u64, 7u64 << 32, 1u64 << 63] {
for invalid in [0, u32::MAX as u64, 8u64 << 32, 1u64 << 63] {
assert_eq!(HostRequestKind::decode(invalid), None);
}
assert_eq!(
Expand Down
4 changes: 3 additions & 1 deletion crates/celox-design/src/schedule.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,9 @@ impl<A: Copy + Eq + std::hash::Hash> ScheduleModel<A> {
Self {
events: design.events.clone(),
kinds: design.event_kinds(),
timed: !processes.is_empty(),
timed: processes
.iter()
.any(|process| process.slots.kind != crate::ProcessKind::Callable),
counted,
probes: schema.occurrence_probes.clone(),
processes,
Expand Down
40 changes: 20 additions & 20 deletions crates/celox-frontend-core/src/process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ use thiserror::Error;

use crate::SourceVarId;

type RegionedSourceAddr = RegionedVarAddrBase<SourceVarId>;
type RegionedSourceAddr<A> = RegionedVarAddrBase<A>;

/// Widest selector of a single `Switch`.
const SWITCH_BITS: usize = 8;
Expand All @@ -55,9 +55,9 @@ pub enum ProcessKernelError {
pub const NBA_POSITION_WIDTH: usize = 64;

/// Builder of one process kernel.
pub struct ProcessKernelBuilder {
builder: SIRBuilder<RegionedSourceAddr>,
slots: ProcessSlots<SourceVarId>,
pub struct ProcessKernelBuilder<A = SourceVarId> {
builder: SIRBuilder<RegionedSourceAddr<A>>,
slots: ProcessSlots<A>,
/// The block each resume point continues in; point 0 is the start.
resume_blocks: Vec<BlockId>,
/// The wait site and predicate entry resume point being lowered.
Expand All @@ -67,10 +67,10 @@ pub struct ProcessKernelBuilder {
flush_site: Option<u32>,
}

impl ProcessKernelBuilder {
impl<A: Copy> ProcessKernelBuilder<A> {
/// Start a kernel. The builder is positioned at the start of the
/// process body.
pub fn new(slots: ProcessSlots<SourceVarId>) -> Self {
pub fn new(slots: ProcessSlots<A>) -> Self {
let builder = SIRBuilder::new();
let start = builder.current_block();
Self {
Expand Down Expand Up @@ -99,24 +99,24 @@ impl ProcessKernelBuilder {

/// The kernel's control slots, with [`ProcessSlots::const_delays`] set
/// for the delays lowered so far.
pub fn slots(&self) -> ProcessSlots<SourceVarId> {
pub fn slots(&self) -> ProcessSlots<A> {
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> {
pub fn builder(&mut self) -> &mut SIRBuilder<RegionedSourceAddr<A>> {
&mut self.builder
}

fn store_constant(&mut self, slot: SourceVarId, width: usize, value: u64) {
fn store_constant(&mut self, slot: A, width: usize, value: u64) {
let register = self.builder.alloc_bit(width, false);
self.builder
.emit(SIRInstruction::Imm(register, SIRValue::new(value)));
self.store(slot, width, register);
}

fn store(&mut self, slot: SourceVarId, width: usize, value: RegisterId) {
fn store(&mut self, slot: A, width: usize, value: RegisterId) {
self.builder.emit(SIRInstruction::Store(
stable(slot),
SIROffset::Static(0),
Expand Down Expand Up @@ -343,7 +343,7 @@ impl ProcessKernelBuilder {
pub fn wait_level(
&mut self,
site: u32,
mut predicate: impl FnMut(&mut SIRBuilder<RegionedSourceAddr>) -> RegisterId,
mut predicate: impl FnMut(&mut SIRBuilder<RegionedSourceAddr<A>>) -> RegisterId,
) -> Result<(), ProcessKernelError> {
let holds = predicate(&mut self.builder);
let join = self.builder.new_block();
Expand Down Expand Up @@ -379,9 +379,9 @@ impl ProcessKernelBuilder {
pub fn nonblocking<E: From<ProcessKernelError>>(
&mut self,
index: u32,
site: &NbaSite<SourceVarId>,
site: &NbaSite<A>,
lower: impl FnOnce(
&mut SIRBuilder<RegionedSourceAddr>,
&mut SIRBuilder<RegionedSourceAddr<A>>,
) -> Result<(RegisterId, Option<NbaPosition<RegisterId>>), E>,
) -> Result<(), E> {
if let Some(pending) = site.pending {
Expand Down Expand Up @@ -432,10 +432,10 @@ impl ProcessKernelBuilder {
pub fn nonblocking_switched<E: From<ProcessKernelError>>(
&mut self,
index: u32,
site: &NbaSite<SourceVarId>,
switch: SourceVarId,
site: &NbaSite<A>,
switch: A,
lower: impl FnOnce(
&mut SIRBuilder<RegionedSourceAddr>,
&mut SIRBuilder<RegionedSourceAddr<A>>,
) -> Result<(RegisterId, Option<NbaPosition<RegisterId>>), E>,
) -> Result<(), E> {
assert!(
Expand Down Expand Up @@ -489,7 +489,7 @@ impl ProcessKernelBuilder {
}

/// Complete the kernel. If a block is still open, the process ends there.
pub fn build(mut self) -> ExecutionUnit<RegionedSourceAddr> {
pub fn build(mut self) -> ExecutionUnit<RegionedSourceAddr<A>> {
if self.builder.has_open_block() {
self.end(ProcessStatus::Done);
}
Expand All @@ -513,7 +513,7 @@ impl ProcessKernelBuilder {
.chunks(1 << SWITCH_BITS)
.map(<[BlockId]>::to_vec)
.collect::<Vec<_>>();
let byte = |builder: &mut SIRBuilder<RegionedSourceAddr>, lsb: usize| {
let byte = |builder: &mut SIRBuilder<RegionedSourceAddr<A>>, lsb: usize| {
let byte = builder.alloc_bit(SWITCH_BITS, false);
builder.emit(SIRInstruction::Slice(byte, resume, lsb, SWITCH_BITS));
byte
Expand Down Expand Up @@ -565,8 +565,8 @@ impl ProcessKernelBuilder {
}
}

fn stable(var_id: SourceVarId) -> RegionedSourceAddr {
RegionedSourceAddr {
fn stable<A>(var_id: A) -> RegionedSourceAddr<A> {
RegionedVarAddrBase {
region: STABLE_REGION,
var_id,
}
Expand Down
7 changes: 6 additions & 1 deletion crates/celox-runtime/src/simulation/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,12 @@ impl<B: SimBackend> SimulationState<B> {
last_clock_unknown: BitSet::new(),
four_state: backend.layout().four_state,
process_wakeups: (0..model.processes.len())
.filter(|&index| model.processes[index].kind != celox_design::ProcessKind::Final)
.filter(|&index| {
matches!(
model.processes[index].kind,
celox_design::ProcessKind::Regular | celox_design::ProcessKind::Initializer
)
})
.map(|process| Reverse((0, process)))
.collect(),
kernel_count: model.processes.len(),
Expand Down
5 changes: 4 additions & 1 deletion crates/celox/src/backend/native/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -609,7 +609,10 @@ impl NativeProgramImage {
/// Whether the design has process kernels, which only a timed
/// [`crate::Simulation`] runs.
pub fn has_processes(&self) -> bool {
!self.runtime_schema.processes.is_empty()
self.runtime_schema
.processes
.iter()
.any(|process| process.kind != celox_design::ProcessKind::Callable)
}

/// The compiler-inserted override slots of a reflected signal, as
Expand Down
27 changes: 27 additions & 0 deletions crates/celox/src/backend/native/image_file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,33 @@ mod tests {
.program_image()
.clone()
}
#[test]
fn host_request_variants_preserve_existing_image_encoding() {
use celox_design::HostRequestKind;

// These bytes are the enum indices stored in pre-Callable images.
// Round trips alone would miss a simultaneous encoder/decoder change.
for (index, kind) in [
HostRequestKind::Plusargs,
HostRequestKind::FileIo,
HostRequestKind::Vcd,
HostRequestKind::Scan,
HostRequestKind::Distribution,
HostRequestKind::Process,
HostRequestKind::Testbench,
]
.into_iter()
.enumerate()
{
let bytes = [index as u8];
assert_eq!(
postcard::from_bytes::<HostRequestKind>(&bytes).unwrap(),
kind
);
assert_eq!(postcard::to_allocvec(&kind).unwrap(), bytes);
}
}

#[test]
fn reader_ignores_runtime_prefix_and_preserves_image_and_offset() {
let image = image();
Expand Down
4 changes: 3 additions & 1 deletion crates/celox/src/scheduler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -425,7 +425,9 @@ impl Plan {
for (index, slots) in schema.processes.iter().enumerate() {
let process = &model.processes[index];
let unit = &sir.processes[index];
let reason = if slots.kind == celox_design::ProcessKind::Initializer {
let reason = if slots.kind == celox_design::ProcessKind::Callable {
Some("an explicitly invoked kernel")
} else if slots.kind == celox_design::ProcessKind::Initializer {
Some("a declaration initializer")
} else if slots.kind == celox_design::ProcessKind::Final {
Some("a final procedure")
Expand Down
2 changes: 1 addition & 1 deletion crates/celox/src/simulation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ impl<B: SimBackend> UnitExecutor for Simulator<B> {
timestamp: u64,
) -> Result<(), Halt> {
match request.kind {
celox_design::HostRequestKind::Process => {
celox_design::HostRequestKind::Process | celox_design::HostRequestKind::Testbench => {
Err(celox_runtime::SimulatorErrorCode::InternalError.into())
}
celox_design::HostRequestKind::Plusargs => {
Expand Down
6 changes: 5 additions & 1 deletion crates/celox/src/simulator/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1767,7 +1767,11 @@ mod host {
program: &crate::ir::RuntimeProgram,
) -> Result<(), SimulatorError> {
let schema = &program.runtime_schema;
if schema.processes.is_empty() {
if schema
.processes
.iter()
.all(|process| process.kind == celox_design::ProcessKind::Callable)
{
return Ok(());
}
let origin = schema.process_origins.first().cloned().unwrap_or_default();
Expand Down
Loading