Skip to content
Merged
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: 7 additions & 0 deletions conformance/sv-tests/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,13 @@ cases retain their verdicts. The pinned `real_bit_select_idx` input is rejected
earlier for its undriven net, so a separate analyzer regression uses a driven
input to check that real-valued indices themselves are rejected.

Static function storage support updates the two rows for
`13.4.2--function-static` from fail to pass in both state modes. The official
runner selects elaboration for this probe: its local accumulator now has
persistent storage rather than being rejected. Runtime retention is exercised
by the shared static-function simulation cases. The full pinned run matches
the reviewed expectations in both state modes.

Blocking latch support changes ten expectation rows: `12.4--if`,
`12.4.1--if_else_if`, and the three `12.4.2` priority/unique conditional probes
now pass elaboration in both state modes. These sources use ordinary
Expand Down
4 changes: 2 additions & 2 deletions conformance/sv-tests/expected.tsv
Original file line number Diff line number Diff line change
Expand Up @@ -271,8 +271,8 @@ chapter-13/13.4.2--function-automatic.sv 2state pass
chapter-13/13.4.2--function-automatic.sv 4state pass
chapter-13/13.4.2--function-recursive.sv 2state fail
chapter-13/13.4.2--function-recursive.sv 4state fail
chapter-13/13.4.2--function-static.sv 2state fail
chapter-13/13.4.2--function-static.sv 4state fail
chapter-13/13.4.2--function-static.sv 2state pass
chapter-13/13.4.2--function-static.sv 4state pass
chapter-13/13.4.3--const-function.sv 2state pass
chapter-13/13.4.3--const-function.sv 4state pass
chapter-13/13.4.4--fork-invalid.sv 2state pass
Expand Down
21 changes: 15 additions & 6 deletions crates/celox-frontend-sv/src/lowering.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1087,12 +1087,21 @@ fn validate_variable_driver_ranges(
.iter()
.any(|right| left.0 != right.0 && net_driver_ranges_overlap(left.1, right.1))
});
let overlapping_continuous = driver_ranges_overlap(&continuous_driver_ranges(
module,
signal_name,
constants,
parameter_types,
));
let continuous = continuous_driver_ranges(module, signal_name, constants, parameter_types);
let function_storage = module.subroutines().iter().any(|subroutine| {
subroutine
.persistent_locals
.iter()
.any(|name| name == signal_name)
});
// Multiple statements in a function are one procedural driver for
// its static storage, even when the caller is a continuous assignment.
let overlapping_continuous = continuous.iter().enumerate().any(|(index, left)| {
continuous[index + 1..].iter().any(|right| {
(!function_storage || left.0 != right.0)
&& net_driver_ranges_overlap(left.1, right.1)
})
});
if overlapping_continuous || (has_overlap && !shared_memories.contains(signal_name)) {
return Err(sv::AnalyzerError::Unsupported(format!(
"multiple variable drivers for `{signal_name}`"
Expand Down
5 changes: 5 additions & 0 deletions crates/celox-frontend-sv/src/lowering/comb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3167,6 +3167,11 @@ impl<'p, 'a> Comb<'p, 'a> {
.subroutine(name)
.cloned()
.ok_or_else(|| unsupported(format!("call of `{name}`")))?;
// Symbolic execution cannot represent state shared by invocations.
// Fall back to Ff, including for runtime declaration initializers.
if !subroutine.persistent_locals.is_empty() {
return Err(unsupported("stateful function call"));
}
if self.active_calls.iter().any(|active| active == name) {
return Err(unsupported(format!("recursive call of `{name}`")));
}
Expand Down
11 changes: 7 additions & 4 deletions crates/celox-frontend-sv/src/lowering/ff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3529,9 +3529,10 @@ impl<'p, 'a> Ff<'p, 'a> {
.map_err(slt_error)?;
}
inputs.push((id, self.lower_slt(&arena, node)?));
} else if self.kernel.is_none()
|| !subroutine.is_task
|| subroutine.automatic_locals.contains(&param.name)
} else if !subroutine.persistent_locals.contains(&param.name)
&& (self.kernel.is_none()
|| !subroutine.is_task
|| subroutine.automatic_locals.contains(&param.name))
{
let value = self.default_reg(id);
inputs.push((id, value));
Expand All @@ -3547,7 +3548,9 @@ impl<'p, 'a> Ff<'p, 'a> {
.m
.id(return_var)
.ok_or_else(|| unsupported(format!("result of `{name}`")))?;
self.init_default(id);
if !subroutine.persistent_locals.contains(return_var) {
self.init_default(id);
}
}
let return_block = self.b.new_block();
self.functions.push(FunctionBlocks {
Expand Down
3 changes: 3 additions & 0 deletions crates/celox-frontend-sv/src/lowering/procedural.rs
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,9 @@ impl<'a> ProcModule<'a> {
sv::ir::Callee::Subroutine(name) => name,
};
if let Some(subroutine) = self.subroutine(&name) {
if !subroutine.persistent_locals.is_empty() {
return true;
}
default_calls(subroutine, &args, &mut calls);
if visited.insert(name) {
for stmt in &subroutine.body {
Expand Down
8 changes: 7 additions & 1 deletion crates/celox-sv-analyzer/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1105,6 +1105,8 @@ impl Module {
subroutine_directions: &subroutine_directions,
automatic: lifetimes::automatic_by_default(syntax_tree),
statics: Vec::new(),
static_initializers: Vec::new(),
constant_evaluation: false,
};
let (mut subroutines, mut comb_processes, mut ff_processes, mut initial_processes) =
if imported.declarations_only {
Expand Down Expand Up @@ -1157,7 +1159,7 @@ impl Module {
// Static locals of the processes that keep their value are variables
// of the module, initialized once with the declaration initializers.
let statics: HashMap<String, Type> = body_state.statics.drain(..).collect();
let (kept, static_initializers) = lifetimes::keep_static_locals(
let (mut kept, mut static_initializers) = lifetimes::keep_static_locals(
&syntax_tree.state,
&statics,
&subroutine_directions,
Expand All @@ -1171,6 +1173,10 @@ impl Module {
.map(|process| &mut process.body),
),
)?;
for subroutine in &subroutines {
kept.extend(subroutine.persistent_locals.iter().cloned());
}
static_initializers.append(&mut body_state.static_initializers);
locals.retain(|local| !kept.contains(&local.name));
for name in kept {
signals.push(Signal::new(name.clone(), statics[&name].clone()));
Expand Down
3 changes: 3 additions & 0 deletions crates/celox-sv-analyzer/src/ast/const_functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,7 @@ impl ConstantFunctions {
sensitivity_body: Vec::new(),
is_task: false,
automatic_locals: Vec::new(),
persistent_locals: Vec::new(),
return_type: function.return_var.as_ref().map(|(_, ty)| ty.clone()),
return_var: function.return_var.as_ref().map(|(name, _)| name.clone()),
params: function
Expand Down Expand Up @@ -481,6 +482,8 @@ fn collect_module_constant_functions(
subroutine_directions: &subroutine_directions,
automatic: lifetimes::automatic_by_default(tree),
statics: Vec::new(),
static_initializers: Vec::new(),
constant_evaluation: true,
};
let mut rejected = HashMap::default();
let subroutines = match procedural::subroutines_with_symbolic_parameters(
Expand Down
2 changes: 2 additions & 0 deletions crates/celox-sv-analyzer/src/ast/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@ pub(super) fn native_functions(
subroutine_directions: &directions,
automatic,
statics: Vec::new(),
static_initializers: Vec::new(),
constant_evaluation: false,
};
let mut dims = item.dimensions(dimensions);
dims.subroutine_param_names = names.clone();
Expand Down
3 changes: 3 additions & 0 deletions crates/celox-sv-analyzer/src/ast/functions/symbolic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,9 @@ pub(super) fn body(
subroutine: &Subroutine,
locals: &[LocalVariable],
) -> Option<(Expr, Vec<(String, Expr)>)> {
if !subroutine.persistent_locals.is_empty() {
return None;
}
let mut types = HashMap::default();
for local in locals {
if !local.r#type.unpacked_ranges().is_empty() {
Expand Down
39 changes: 37 additions & 2 deletions crates/celox-sv-analyzer/src/ast/lifetimes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
//! entry is never read gives the same results that way. Any other static
//! local of a procedural block keeps its value instead: its declaration no
//! longer initializes it, and its initializer runs once at time zero. Static
//! locals of subroutines that keep their value are not supported.
//! locals of functions use the same storage; task locals that retain state
//! remain unsupported.

use super::*;
mod bit_result;
Expand Down Expand Up @@ -210,7 +211,7 @@ pub(super) fn check_subroutine_statics(
result
}

/// Reject a static function whose result keeps its value between calls: in
/// Detect a static function whose result keeps its value between calls: in
/// a static function the variable named after it is static too, so a call
/// that does not assign it returns the result of an earlier call.
pub(super) fn check_static_result(
Expand All @@ -228,6 +229,7 @@ pub(super) fn check_static_result(
name: result,
directions,
two_state_widths: &widths,
read_on_return: false,
};
// `return value;` assigns the result as it leaves.
match flow.block(&subroutine.body, false) {
Expand All @@ -241,6 +243,22 @@ pub(super) fn check_static_result(
}
}

/// An output-only static formal also preserves its value before copy-out.
pub(super) fn output_retains_value(
subroutine: &Subroutine,
name: &str,
directions: &Directions,
) -> bool {
let widths = argument_widths(subroutine);
let flow = Flow {
name,
directions,
two_state_widths: &widths,
read_on_return: true,
};
!matches!(flow.block(&subroutine.body, false), Ok(exits) if exits.next != Some(false))
}

/// The widths of the two-state arguments of `subroutine`.
fn argument_widths(subroutine: &Subroutine) -> HashMap<String, usize> {
subroutine
Expand Down Expand Up @@ -279,7 +297,20 @@ fn entry(
name,
directions,
two_state_widths: widths,
read_on_return: false,
};
// Even a function that is legal in a constant expression can retain
// different state when called at runtime (13.4.3). Its initializer must
// execute once, including when the initialized local is read-only.
let mut calls_function = false;
if let Some(init) = init {
visit_calls(init, &mut |callee, _| {
calls_function |= callee.subroutine().is_some();
});
}
if calls_function {
return Ok(Entry::Observed);
}
let observed = flow.block(after, false).is_err();
Ok(match init {
// Initialized once or on every entry, a local that is never written
Expand All @@ -296,6 +327,7 @@ struct Observed;
/// Follows whether one local is written on every path, from its declaration
/// on.
struct Flow<'n> {
read_on_return: bool,
name: &'n str,
directions: &'n Directions,
/// The widths of the two-state variables a `case` may select on, to
Expand Down Expand Up @@ -527,6 +559,9 @@ impl Flow<'_> {
..Exits::default()
}),
Stmt::Return(value) => {
if self.read_on_return && !written {
return Err(Observed);
}
if let Some(value) = value {
self.read(value, written)?;
}
Expand Down
43 changes: 25 additions & 18 deletions crates/celox-sv-analyzer/src/ast/lifetimes/bit_result.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ impl Proof<'_> {
}

/// `true` means no entry bit can subsequently be observed. Unknown control
/// flow or calls give no proof; the ordinary lifetime check still rejects.
/// flow or calls give no proof; the ordinary lifetime check requires persistent storage.
fn block(&mut self, statements: &[Stmt]) -> Option<bool> {
for statement in statements {
self.remaining = self.remaining.checked_sub(1)?;
Expand Down Expand Up @@ -276,7 +276,7 @@ fn pure(expression: &Expr) -> bool {
#[cfg(test)]
mod tests {
#[test]
fn incomplete_or_entry_dependent_scratch_arrays_stay_rejected() {
fn incomplete_or_entry_dependent_scratch_arrays_retain_storage() {
for body in [
"for(integer i=0;i<1;i=i+1) words[i]=a;",
"for(integer i=0;i<2;i=i+1) words[0]=a;",
Expand All @@ -290,15 +290,12 @@ mod tests {
logic[7:0] words[1:0]; logic[7:0] other; {body} \
f=words[0]+words[1]; endfunction assign y=f(a); endmodule"
);
let error = crate::analyze_source(&source, std::path::Path::new("partial-scratch.sv"))
.unwrap_err()
.to_string();
assert!(error.contains("static variable `words`"), "{body}: {error}");
assert_retained(&source, "words");
}
}

#[test]
fn incomplete_or_entry_dependent_results_stay_rejected() {
fn incomplete_or_entry_dependent_results_retain_storage() {
for body in [
"for(integer i=0;i<3;i=i+1) f[i]=a[i];",
"for(integer i=0;i<4;i=i+1) f[0]=a[i];",
Expand All @@ -313,13 +310,7 @@ mod tests {
function [3:0] f(input logic[3:0] a); {body} endfunction \
assign y=f(a); endmodule"
);
let error = crate::analyze_source(&source, std::path::Path::new("partial-result.sv"))
.unwrap_err()
.to_string();
assert!(
error.contains("whose result keeps its value"),
"{body}: {error}"
);
assert_retained(&source, "f");
}
}

Expand All @@ -336,9 +327,25 @@ mod tests {
assign y=f(a);
endmodule
"#;
let error = crate::analyze_source(source, std::path::Path::new("index-call.sv"))
.unwrap_err()
.to_string();
assert!(error.contains("whose result keeps its value"), "{error}");
assert_retained(source, "f");
}

fn assert_retained(source: &str, variable: &str) {
let ir =
crate::analyze_source(source, std::path::Path::new("retained-storage.sv")).unwrap();
let module = &ir.modules()[0];
let function = module
.subroutines()
.iter()
.find(|function| function.name == "f")
.unwrap();
let prefix = format!("{variable}@");
let name = function
.persistent_locals
.iter()
.find(|name| name.starts_with(&prefix))
.unwrap_or_else(|| panic!("missing persistent {variable}: {source}"));
assert!(module.signals().iter().any(|signal| signal.name() == name));
assert!(!module.locals().iter().any(|local| &local.name == name));
}
}
5 changes: 4 additions & 1 deletion crates/celox-sv-analyzer/src/ast/packages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -482,7 +482,7 @@ fn analyze_package_scope(
// The names the package declares, from its syntax: an escaped name may
// contain `::` itself.
let own = imports::scope_names(tree)?;
let variables = package_variables(name, tree)?;
let mut variables = package_variables(name, tree)?;
let mut module = Module::from_scope_with_parameter_overrides(
tree,
"",
Expand All @@ -493,6 +493,9 @@ fn analyze_package_scope(
},
Arc::new(imported),
)?;
for subroutine in &module.subroutines {
variables.extend(subroutine.persistent_locals.iter().cloned());
}
let mut symbols = module
.symbols
.take()
Expand Down
Loading
Loading