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
362 changes: 360 additions & 2 deletions crates/celox-frontend-sv/src/lowering.rs

Large diffs are not rendered by default.

127 changes: 69 additions & 58 deletions crates/celox-frontend-sv/src/lowering/ff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6433,62 +6433,73 @@ fn impure_call(pm: &ProcModule<'_>, expr: &sv::ir::Expr) -> Option<String> {
}

/// `expr` with the identifiers in `renames` replaced.
fn rename_idents(expr: &sv::ir::Expr, renames: &HashMap<String, String>) -> sv::ir::Expr {
fn constant(expr: &sv::ir::ConstExpr, renames: &HashMap<String, String>) -> sv::ir::ConstExpr {
use sv::ir::ConstExpr;
let go = |expr: &ConstExpr| Box::new(constant(expr, renames));
match expr {
ConstExpr::Ident(name) => ConstExpr::Ident(renames.get(name).unwrap_or(name).clone()),
ConstExpr::Literal(_) => expr.clone(),
ConstExpr::SelectRange {
expr,
msb,
lsb,
signed,
} => ConstExpr::SelectRange {
expr: go(expr),
msb: go(msb),
lsb: go(lsb),
signed: *signed,
},
ConstExpr::Select { expr, bit } => ConstExpr::Select {
expr: go(expr),
bit: go(bit),
},
ConstExpr::Function { callee, args, site } => ConstExpr::Function {
callee: callee.clone(),
site: *site,
args: args.iter().map(|arg| constant(arg, renames)).collect(),
},
ConstExpr::Resize {
expr,
width,
signed,
} => ConstExpr::Resize {
expr: go(expr),
width: *width,
signed: *signed,
},
ConstExpr::Unary { op, expr } => ConstExpr::Unary {
op: *op,
expr: go(expr),
},
ConstExpr::Binary { left, op, right } => ConstExpr::Binary {
left: go(left),
op: *op,
right: go(right),
},
ConstExpr::Mux {
condition,
then_expr,
else_expr,
} => ConstExpr::Mux {
condition: go(condition),
then_expr: go(then_expr),
else_expr: go(else_expr),
},
}
pub(super) fn rename_const_idents(
expr: &sv::ir::ConstExpr,
renames: &HashMap<String, String>,
) -> sv::ir::ConstExpr {
use sv::ir::ConstExpr;
let go = |expr: &ConstExpr| Box::new(rename_const_idents(expr, renames));
match expr {
ConstExpr::Ident(name) => ConstExpr::Ident(renames.get(name).unwrap_or(name).clone()),
ConstExpr::Literal(_) => expr.clone(),
ConstExpr::SelectRange {
expr,
msb,
lsb,
signed,
} => ConstExpr::SelectRange {
expr: go(expr),
msb: go(msb),
lsb: go(lsb),
signed: *signed,
},
ConstExpr::Select { expr, bit } => ConstExpr::Select {
expr: go(expr),
bit: go(bit),
},
ConstExpr::Function { callee, args, site } => ConstExpr::Function {
callee: callee.clone(),
site: *site,
args: args
.iter()
.map(|arg| rename_const_idents(arg, renames))
.collect(),
},
ConstExpr::Resize {
expr,
width,
signed,
} => ConstExpr::Resize {
expr: go(expr),
width: *width,
signed: *signed,
},
ConstExpr::Unary { op, expr } => ConstExpr::Unary {
op: *op,
expr: go(expr),
},
ConstExpr::Binary { left, op, right } => ConstExpr::Binary {
left: go(left),
op: *op,
right: go(right),
},
ConstExpr::Mux {
condition,
then_expr,
else_expr,
} => ConstExpr::Mux {
condition: go(condition),
then_expr: go(then_expr),
else_expr: go(else_expr),
},
}
}

/// `expr` with the identifiers in `renames` replaced.
pub(super) fn rename_idents(
expr: &sv::ir::Expr,
renames: &HashMap<String, String>,
) -> sv::ir::Expr {
use sv::ir::Expr;
let go = |expr: &Expr| Box::new(rename_idents(expr, renames));
match expr {
Expand All @@ -6501,8 +6512,8 @@ fn rename_idents(expr: &sv::ir::Expr, renames: &HashMap<String, String>) -> sv::
signed,
} => Expr::Select {
expr: go(expr),
msb: constant(msb, renames),
lsb: constant(lsb, renames),
msb: rename_const_idents(msb, renames),
lsb: rename_const_idents(lsb, renames),
signed: *signed,
},
Expr::Concat(parts) => Expr::Concat(
Expand All @@ -6512,7 +6523,7 @@ fn rename_idents(expr: &sv::ir::Expr, renames: &HashMap<String, String>) -> sv::
.collect(),
),
Expr::RepeatConcat { count, parts } => Expr::RepeatConcat {
count: constant(count, renames),
count: rename_const_idents(count, renames),
parts: parts
.iter()
.map(|part| rename_idents(part, renames))
Expand Down
16 changes: 16 additions & 0 deletions crates/celox-frontend-sv/src/warning.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,18 @@ pub enum FrontendWarningKind {
/// IEEE 1800-2023 24.7 ignores.
#[error("`$exit` outside a program block is ignored")]
ExitOutsideProgram,
/// A variable that only several `always @*` blocks write is a temporary
/// of each block, so its writes do not wake the other blocks as IEEE
/// 1800-2023 9.4.2.2 requires. Applied only when the diagnostic is not
/// an error.
#[error(
"`{signal}` in `{module}` is treated as a temporary of each `always @*` block that writes it ({blocks}): its writes do not wake the other blocks"
)]
SharedAlwaysTemporary {
module: String,
signal: String,
blocks: String,
},
}

/// Where a warning is in the source.
Expand Down Expand Up @@ -160,6 +172,7 @@ impl miette::Diagnostic for FrontendWarning {
FrontendWarningKind::InferredLatch => "inferred_latch",
FrontendWarningKind::MemoryFileWordCount { .. } => "memory_file_word_count",
FrontendWarningKind::ExitOutsideProgram => "exit_outside_program",
FrontendWarningKind::SharedAlwaysTemporary { .. } => "shared_always_temporary",
}))
}

Expand Down Expand Up @@ -190,6 +203,9 @@ impl miette::Diagnostic for FrontendWarning {
FrontendWarningKind::ExitOutsideProgram => {
"end the simulation with `$finish`, or remove the call"
}
FrontendWarningKind::SharedAlwaysTemporary { .. } => {
"a language deviation enabled by shared_always_temporary=warn or allow; declare the variable inside each block to keep IEEE 1800-2023 semantics"
}
}))
}

Expand Down
18 changes: 18 additions & 0 deletions crates/celox-runtime/src/simulation/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,11 @@ pub struct SimulationState<B: SimBackend> {
return_budget: u64,
/// Rounds and process-phase passes the current time slot has run.
slot_passes: u64,
/// The processes the current time slot ran, most recent last, up to
/// [`RESUMED_PROCESSES`]: the ones a slot that does not settle reports.
slot_resumed: std::collections::VecDeque<usize>,
/// Names of the processes for diagnostics, by index.
process_names: Vec<String>,
/// The loop that runs time slots, and the slots each loop ran.
stats: SchedulerStats,
/// The bits of the compiled scheduler's events whose changes the host
Expand All @@ -173,6 +178,10 @@ pub struct SimulationState<B: SimBackend> {
observed_events: u64,
}

/// The most recent processes of a time slot that a slot that does not
/// settle reports.
const RESUMED_PROCESSES: usize = 64;

impl<B: SimBackend> SimulationState<B> {
/// Bound the work of one time slot: `rounds` rounds and passes of the
/// process phase, and `returns` kernel returns per round, before a step
Expand All @@ -183,6 +192,12 @@ impl<B: SimBackend> SimulationState<B> {
self.return_budget = returns;
}

/// Name the processes, by index, in diagnostics such as a time slot
/// that does not settle.
pub fn set_process_names(&mut self, names: Vec<String>) {
self.process_names = names;
}

/// Every process of `model` starts at time zero, in list order.
pub fn new(backend: &B, model: ResolvedSchedule<B>) -> Self {
let process_state = ProcessState {
Expand Down Expand Up @@ -227,6 +242,8 @@ impl<B: SimBackend> SimulationState<B> {
round_budget: SLOT_ROUND_BUDGET,
return_budget: SLOT_RETURN_BUDGET,
slot_passes: 0,
slot_resumed: std::collections::VecDeque::new(),
process_names: Vec::new(),
stats: SchedulerStats::default(),
observed_events: 0,
};
Expand Down Expand Up @@ -484,6 +501,7 @@ impl<B: SimBackend> SimulationState<B> {
E: SimulationExecutor<Backend = B>,
{
self.slot_passes = 0;
self.slot_resumed.clear();
match self.step_round(executor, time, Vec::new(), Vec::new(), true, false) {
Ok(()) => {
self.finish_timed_boundary(executor, time)?;
Expand Down
6 changes: 5 additions & 1 deletion crates/celox-runtime/src/simulation/process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ use std::collections::BTreeSet;

use celox_design::{HostRequestKind, ProcessStatus};

use super::SimulationState;
use super::host::SimulationExecutor;
use super::round::Round;
use super::wait::wake_in_region;
use super::{RESUMED_PROCESSES, SimulationState};
use crate::{
SimulatorErrorCode,
backend::SimBackend,
Expand Down Expand Up @@ -127,6 +127,10 @@ impl<B: SimBackend> SimulationState<B> {
where
E: SimulationExecutor<Backend = B>,
{
if self.slot_resumed.len() == RESUMED_PROCESSES {
self.slot_resumed.pop_front();
}
self.slot_resumed.push_back(process);
let kernel = self.restore_activation(executor.backend_mut(), process);
let result = self.run_activation(executor, current_time, process, kernel, returns);
self.save_activation(executor.backend(), process);
Expand Down
28 changes: 23 additions & 5 deletions crates/celox-runtime/src/simulation/time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ impl<B: SimBackend> SimulationState<B> {
{
self.host_pending = false;
self.slot_passes = 0;
self.slot_resumed.clear();
let events_to_process = if self.scheduler.next_event_time() == Some(current_time) {
self.scheduler
.pop_all_at_next_time()
Expand Down Expand Up @@ -245,15 +246,32 @@ impl<B: SimBackend> SimulationState<B> {
if returns <= budget {
return Ok(());
}
let name = |process: usize| {
self.process_names
.get(process)
.cloned()
.unwrap_or_else(|| format!("process {process}"))
};
Err(Halt::Error(SimulatorErrorCode::Runtime {
message: match process {
Some(process) => {
format!("process {process} does not suspend within one time slot")
format!("{} does not suspend within one time slot", name(process))
}
None => {
let mut resumed: Vec<usize> = self.slot_resumed.iter().copied().collect();
resumed.sort_unstable();
resumed.dedup();
let resumed: Vec<String> = resumed.into_iter().map(name).collect();
format!(
"time {} does not settle: processes keep resuming{}",
self.scheduler.time,
if resumed.is_empty() {
String::new()
} else {
format!(": {}", resumed.join(", "))
}
)
}
None => format!(
"time {} does not settle: processes keep resuming",
self.scheduler.time
),
},
signals: Vec::new(),
}))
Expand Down
6 changes: 6 additions & 0 deletions crates/celox-sv-analyzer/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2416,6 +2416,12 @@ pub struct CombProcess {
}

impl CombProcess {
/// Where an `always` procedure is written, for diagnostics about it as
/// a process.
pub fn process_site(&self) -> Option<&crate::procedural::ProcessSite> {
self.process_site.as_ref()
}

pub fn site(&self) -> Option<&crate::procedural::SourceSpan> {
self.site.as_ref()
}
Expand Down
Loading
Loading