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
7 changes: 4 additions & 3 deletions crates/celox-frontend-sv/src/lowering/comb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2049,9 +2049,10 @@ impl<'p, 'a> Comb<'p, 'a> {
Ok(store)
}
// A timing control suspends a process; this lowering has none.
sv::ir::Stmt::Delay { .. } | sv::ir::Stmt::WaitEvent(_) | sv::ir::Stmt::Wait(_) => {
Err(unsupported("procedural timing control"))
}
sv::ir::Stmt::Delay { .. }
| sv::ir::Stmt::WaitEvent(_)
| sv::ir::Stmt::WaitImplicit(_)
| sv::ir::Stmt::Wait(_) => Err(unsupported("procedural timing control")),
sv::ir::Stmt::Local { name, init } => {
let id = self
.m
Expand Down
140 changes: 136 additions & 4 deletions crates/celox-frontend-sv/src/lowering/ff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2629,7 +2629,10 @@ impl<'p, 'a> Ff<'p, 'a> {
fn exec(&mut self, stmt: &sv::ir::Stmt) -> Result<bool, sv::AnalyzerError> {
if matches!(
stmt,
sv::ir::Stmt::Delay { .. } | sv::ir::Stmt::WaitEvent(_) | sv::ir::Stmt::Wait(_)
sv::ir::Stmt::Delay { .. }
| sv::ir::Stmt::WaitEvent(_)
| sv::ir::Stmt::WaitImplicit(_)
| sv::ir::Stmt::Wait(_)
) && (self.kernel.is_none()
|| self.active_calls.iter().any(|name| {
self.m
Expand Down Expand Up @@ -2676,6 +2679,10 @@ impl<'p, 'a> Ff<'p, 'a> {
self.wait_event(items)?;
Ok(true)
}
sv::ir::Stmt::WaitImplicit(reads) => {
self.wait_implicit(reads)?;
Ok(true)
}
sv::ir::Stmt::Wait(condition) => {
self.wait_level(condition)?;
Ok(true)
Expand Down Expand Up @@ -4937,6 +4944,65 @@ impl Ff<'_, '_> {
fn wait_event(&mut self, items: &[sv::ir::EventItem]) -> Result<(), sv::AnalyzerError> {
let mut terms = Vec::new();
let mut samples = Vec::new();
self.event_terms(items, &mut terms, &mut samples)?;
self.suspend_on(terms, samples)
}

/// `@*` (IEEE 1800-2023 9.4.2.2): wait for a change of a variable that
/// `reads` read. The module variables are those a combinational
/// `always @*` is sensitive to, from the same analysis, so folded
/// branches and the defaults of omitted subroutine arguments count. A
/// process-local variable it reads is sampled like an operand of an
/// event expression.
fn wait_implicit(&mut self, reads: &[sv::ir::Stmt]) -> Result<(), sv::AnalyzerError> {
// The expressions of timing controls are not part of the event list.
let reads = without_timing_controls(reads);
let reads = reads.as_slice();
let mut ids: Vec<SourceVarId> = self
.m
.implicit_reads(reads, false)
.into_iter()
.map(|atom| atom.id)
.collect();
ids.sort_unstable();
ids.dedup();
let mut terms: Vec<_> = ids
.into_iter()
.map(|id| (id, CounterKind::Changes))
.collect();
let mut names = HashSet::default();
for stmt in reads {
stmt.clone().visit_mut(
&mut |expr: &mut sv::ir::Expr| expr_idents(expr, &mut names),
&mut |_: &mut sv::ir::LValue| {},
&mut |_: &mut String| {},
);
}
let mut locals: Vec<String> = names
.into_iter()
.filter(|name| self.m.id(name).is_some_and(|id| self.m.is_hidden(id)))
.collect();
locals.sort_unstable();
let locals: Vec<_> = locals
.into_iter()
.map(|name| sv::ir::EventItem {
edge: sv::ir::EventEdge::Any,
expr: sv::ir::Expr::Ident(name),
})
.collect();
let mut samples = Vec::new();
self.event_terms(&locals, &mut terms, &mut samples)?;
self.suspend_on(terms, samples)
}

/// The counter terms of `items` and the samples the process takes of
/// their expressions before it suspends.
fn event_terms(
&mut self,
items: &[sv::ir::EventItem],
terms: &mut Vec<(SourceVarId, CounterKind)>,
samples: &mut Vec<(String, sv::ir::Expr)>,
) -> Result<(), sv::AnalyzerError> {
for item in items {
let expr = self.propagate(&item.expr);
let edge = match item.edge {
Expand All @@ -4953,12 +5019,12 @@ impl Ff<'_, '_> {
let id = match &expr {
sv::ir::Expr::Ident(name) => match self.m.id(name) {
Some(id) if !self.m.is_hidden(id) => id,
Some(_) => self.event_expression(&expr, &mut samples)?,
Some(_) => self.event_expression(&expr, samples)?,
// A constant never changes.
None if self.m.constants.contains_key(name) => continue,
None => return Err(unsupported(format!("event expression `{name}`"))),
},
_ => self.event_expression(&expr, &mut samples)?,
_ => self.event_expression(&expr, samples)?,
};
match item.edge {
sv::ir::EventEdge::Any => terms.push((id, CounterKind::Changes)),
Expand All @@ -4970,6 +5036,15 @@ impl Ff<'_, '_> {
}
}
}
Ok(())
}

/// Suspend until one of `terms` counts, after taking `samples`.
fn suspend_on(
&mut self,
terms: Vec<(SourceVarId, CounterKind)>,
samples: Vec<(String, sv::ir::Expr)>,
) -> Result<(), sv::AnalyzerError> {
for (target, value) in samples {
self.assign(&sv::ir::LValue::Ident(target), &value)?;
}
Expand Down Expand Up @@ -5217,12 +5292,68 @@ pub(super) struct KernelUnit {
/// may not suspend.
const LOOP_YIELD_BITS: usize = 16;

/// `stmts` without their delays, event controls and waits, at any depth.
fn without_timing_controls(stmts: &[sv::ir::Stmt]) -> Vec<sv::ir::Stmt> {
use sv::ir::Stmt;
stmts
.iter()
.filter(|stmt| {
!matches!(
stmt,
Stmt::Delay { .. } | Stmt::WaitEvent(_) | Stmt::WaitImplicit(_) | Stmt::Wait(_)
)
})
.map(|stmt| match stmt {
Stmt::If {
condition,
then_body,
else_body,
} => Stmt::If {
condition: condition.clone(),
then_body: without_timing_controls(then_body),
else_body: without_timing_controls(else_body),
},
Stmt::Case {
kind,
selector,
items,
default,
} => Stmt::Case {
kind: *kind,
selector: selector.clone(),
items: items
.iter()
.map(|item| sv::ir::CaseItem {
labels: item.labels.clone(),
body: without_timing_controls(&item.body),
})
.collect(),
default: default.as_deref().map(without_timing_controls),
},
Stmt::Loop {
kind,
init,
condition,
step,
body,
} => Stmt::Loop {
kind: kind.clone(),
init: without_timing_controls(init),
condition: condition.clone(),
step: without_timing_controls(step),
body: without_timing_controls(body),
},
stmt => stmt.clone(),
})
.collect()
}

/// Whether every pass through `stmts` suspends the process.
fn always_suspends(stmts: &[sv::ir::Stmt]) -> bool {
stmts.iter().any(|stmt| {
matches!(
stmt,
sv::ir::Stmt::Delay { .. } | sv::ir::Stmt::WaitEvent(_)
sv::ir::Stmt::Delay { .. } | sv::ir::Stmt::WaitEvent(_) | sv::ir::Stmt::WaitImplicit(_)
)
})
}
Expand Down Expand Up @@ -5336,6 +5467,7 @@ fn impure_call(pm: &ProcModule<'_>, expr: &sv::ir::Expr) -> Option<String> {
sv::ir::Stmt::SystemTask { .. }
| sv::ir::Stmt::Delay { .. }
| sv::ir::Stmt::WaitEvent(_)
| sv::ir::Stmt::WaitImplicit(_)
| sv::ir::Stmt::Wait(_) => effect = true,
_ => {}
});
Expand Down
1 change: 1 addition & 0 deletions crates/celox-frontend-sv/src/lowering/procedural.rs
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,7 @@ impl<'a> ProcModule<'a> {
stmt,
sv::ir::Stmt::Delay { .. }
| sv::ir::Stmt::WaitEvent(_)
| sv::ir::Stmt::WaitImplicit(_)
| sv::ir::Stmt::Wait(_)
| sv::ir::Stmt::Assign {
nonblocking: true,
Expand Down
7 changes: 6 additions & 1 deletion crates/celox-sv-analyzer/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1300,7 +1300,7 @@ impl Module {
}
procedural::timed_tasks::classify(
subroutines.iter().chain(imported.subroutines.iter()),
&comb_processes,
&mut comb_processes,
&mut ff_processes,
&mut initial_processes,
)?;
Expand Down Expand Up @@ -2254,6 +2254,9 @@ impl LValue {
pub struct CombProcess {
creation_order: usize,
site: Option<crate::procedural::SourceSpan>,
/// Where the procedure is written, for an `always @*` that becomes a
/// process when it calls a timed task.
process_site: Option<crate::procedural::ProcessSite>,
kind: CombProcessKind,
condition: Option<ConstExpr>,
assignments: Vec<Assignment>,
Expand Down Expand Up @@ -2292,6 +2295,7 @@ impl CombProcess {
Self {
creation_order: 0,
site: None,
process_site: None,
sensitivity_body: Vec::new(),
kind,
condition,
Expand All @@ -2304,6 +2308,7 @@ impl CombProcess {
Self {
creation_order: 0,
site: None,
process_site: None,
sensitivity_body: Vec::new(),
kind,
condition,
Expand Down
7 changes: 3 additions & 4 deletions crates/celox-sv-analyzer/src/ast/comb_process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,9 @@ pub(super) fn comb_processes_from_scope(
)?;
for process in &mut processes[start..] {
process.creation_order = creation_order;
process.site = procedural::source_span(
syntax_tree.declarations().body_item(item.id).tokens,
syntax_tree,
);
let tokens = syntax_tree.declarations().body_item(item.id).tokens;
process.site = procedural::source_span(tokens, syntax_tree);
process.process_site = procedural::process_site("always", tokens, syntax_tree);
for assignment in &mut process.assignments {
item.assignment(assignment);
}
Expand Down
4 changes: 3 additions & 1 deletion crates/celox-sv-analyzer/src/ast/const_functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1345,7 +1345,9 @@ impl Frame<'_> {
// diagnostic belongs to the declaration, not to evaluation.
Stmt::SystemTask { .. } => {}
// A function has no timing controls (IEEE 1800-2023 13.4).
Stmt::Delay { .. } | Stmt::WaitEvent(_) | Stmt::Wait(_) => return None,
Stmt::Delay { .. } | Stmt::WaitEvent(_) | Stmt::WaitImplicit(_) | Stmt::Wait(_) => {
return None;
}
Stmt::AssignConcat { .. } | Stmt::Call { .. } => return None,
}
Some(Flow::Next)
Expand Down
2 changes: 2 additions & 0 deletions crates/celox-sv-analyzer/src/ast/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ pub(super) fn substitute_process_constants_with_parameter_literals(
return CombProcess {
creation_order: process.creation_order,
site: process.site,
process_site: process.process_site,
sensitivity_body,
..CombProcess::procedural(process.kind, condition, body)
};
Expand All @@ -140,6 +141,7 @@ pub(super) fn substitute_process_constants_with_parameter_literals(
);
result.creation_order = process.creation_order;
result.site = process.site;
result.process_site = process.process_site;
result.sensitivity_body = sensitivity_body;
result
}
Expand Down
11 changes: 10 additions & 1 deletion crates/celox-sv-analyzer/src/ast/dimensions/query_binding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,16 @@ pub(in crate::ast) fn query_context_for_operand(
tree: &AnalysisContext<'_>,
context: &PackedDimensions,
) -> Converted<PackedDimensions> {
if context.scope_types_complete {
// Literals carry their type in their spelling and cannot reference lexical
// declarations. Pattern elements otherwise rediscover the whole scope for
// each literal while the surrounding declaration is still being bound.
let operand = expression_binding::ungroup(root, tree.declarations());
if context.scope_types_complete
|| matches!(
tree.declarations().expression(operand).kind,
ExprKind::Literal(_)
)
{
return Ok(query_context(context));
}
Ok(query_context(&query_operand_dimensions_id(
Expand Down
56 changes: 56 additions & 0 deletions crates/celox-sv-analyzer/src/ast/dimensions/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -517,3 +517,59 @@ fn recursive_generate_dimension_discovery_uses_explicit_specialization() {
);
}
}

#[test]
fn preliminary_literal_queries_do_not_discover_scope_declarations() {
let tree = crate::source_tree::parse_scope_fixture(
r#"module Top;
logic [7:0] unrelated [0:31];
localparam A = $bits(123);
localparam B = $bits((8'shff));
localparam C = $bits(4'bx);
localparam D = $bits("ab");
localparam E = $bits('1);
endmodule"#,
Path::new("literal_query.sv"),
)
.unwrap();
let tree = AnalysisContext::unspecialized(&tree);
let input = PackedDimensions::default();
let calls = native_calls(&tree);
assert_eq!(calls.len(), 5);
// Keep the discovery path as an independent reference for the result.
let discovered = calls
.iter()
.map(|call| {
let celox_sv_syntax::ExprKind::Call { arguments, .. } =
&tree.declarations().expression(*call).kind
else {
unreachable!()
};
let context =
query_operand_dimensions_id(arguments[0].value.unwrap(), &tree, &input).unwrap();
query_number(
*call,
&tree,
&HashMap::default(),
&HashMap::default(),
Some(&context),
)
})
.collect::<Vec<_>>();
SYNTAX_TYPE_DISCOVERIES.with(|count| count.set(0));
let direct = calls
.iter()
.map(|call| {
query_number(
*call,
&tree,
&HashMap::default(),
&HashMap::default(),
Some(&input),
)
})
.collect::<Vec<_>>();
assert_eq!(&direct[..4], &[Some(32), Some(8), Some(4), Some(16)]);
assert_eq!(direct, discovered);
assert_eq!(SYNTAX_TYPE_DISCOVERIES.with(|count| count.get()), 0);
}
Loading
Loading