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
12 changes: 6 additions & 6 deletions conformance/sv-tests/expected.tsv
Original file line number Diff line number Diff line change
Expand Up @@ -1187,8 +1187,8 @@ chapter-6/6.19.5.4--enum_prev.sv 2state pass
chapter-6/6.19.5.4--enum_prev.sv 4state pass
chapter-6/6.19.5.5--enum_num.sv 2state pass
chapter-6/6.19.5.5--enum_num.sv 4state pass
chapter-6/6.19.5.6--enum_name.sv 2state fail
chapter-6/6.19.5.6--enum_name.sv 4state fail
chapter-6/6.19.5.6--enum_name.sv 2state pass
chapter-6/6.19.5.6--enum_name.sv 4state pass
chapter-6/6.20.2--parameter.sv 2state pass
chapter-6/6.20.2--parameter.sv 4state pass
chapter-6/6.20.2--parameter_aggregate.sv 2state pass
Expand Down Expand Up @@ -1557,10 +1557,10 @@ chapter-8/8.5--properties_enum.sv 2state fail
chapter-8/8.5--properties_enum.sv 4state fail
chapter-8/8.6--methods.sv 2state pass
chapter-8/8.6--methods.sv 4state pass
chapter-8/8.7--constructor.sv 2state fail
chapter-8/8.7--constructor.sv 4state fail
chapter-8/8.7--constructor_param.sv 2state fail
chapter-8/8.7--constructor_param.sv 4state fail
chapter-8/8.7--constructor.sv 2state pass
chapter-8/8.7--constructor.sv 4state pass
chapter-8/8.7--constructor_param.sv 2state pass
chapter-8/8.7--constructor_param.sv 4state pass
chapter-8/8.7--constructor_super.sv 2state pass
chapter-8/8.7--constructor_super.sv 4state pass
chapter-8/8.8--typed_constructor.sv 2state pass
Expand Down
26 changes: 21 additions & 5 deletions crates/celox-sv-analyzer/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1263,6 +1263,7 @@ impl Module {
subroutine_directions: &subroutine_directions,
automatic: lifetimes::automatic_by_default(syntax_tree),
statics: Vec::new(),
explicit_statics: HashSet::default(),
static_initializers: Vec::new(),
constant_evaluation: false,
};
Expand Down Expand Up @@ -1330,18 +1331,25 @@ impl Module {
&subroutine_directions,
comb_processes
.iter_mut()
.map(|process| &mut process.body)
.chain(ff_processes.iter_mut().map(|process| &mut process.body))
.map(|process| (&mut process.body, false))
.chain(
ff_processes
.iter_mut()
.map(|process| (&mut process.body, false)),
)
.chain(
initial_processes
.iter_mut()
.map(|process| &mut process.body),
.map(|process| (&mut process.body, !process.repeats)),
),
Some(&body_state.explicit_statics),
)?;
for subroutine in &subroutines {
kept.extend(subroutine.persistent_locals.iter().cloned());
}
static_initializers.append(&mut body_state.static_initializers);
// Statics of subroutines are initialized first: a run-time
// initializer of a process local may call the subroutine.
static_initializers.splice(0..0, body_state.static_initializers.drain(..));
locals.retain(|local| !kept.contains(&local.name));
for name in kept {
signals.push(Signal::new(name.clone(), statics[&name].clone()));
Expand All @@ -1352,10 +1360,18 @@ impl Module {
.iter()
.position(|process| !process.is_initializer())
.unwrap_or(initial_processes.len());
// They may read module variables, so they also stay after those
// initializers when processes are reordered by creation.
let creation_order = initial_processes
.iter()
.filter(|process| process.is_initializer())
.map(|process| process.creation_order)
.max()
.unwrap_or(0);
initial_processes.insert(
first_block,
InitialProcess {
creation_order: 0,
creation_order,
site: None,
condition: None,
body: static_initializers,
Expand Down
1 change: 1 addition & 0 deletions crates/celox-sv-analyzer/src/ast/const_functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -503,6 +503,7 @@ fn collect_module_constant_functions(
subroutine_directions: &subroutine_directions,
automatic: lifetimes::automatic_by_default(tree),
statics: Vec::new(),
explicit_statics: HashSet::default(),
static_initializers: Vec::new(),
constant_evaluation: true,
};
Expand Down
1 change: 1 addition & 0 deletions crates/celox-sv-analyzer/src/ast/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ pub(super) fn native_functions(
subroutine_directions: &directions,
automatic,
statics: Vec::new(),
explicit_statics: HashSet::default(),
static_initializers: Vec::new(),
constant_evaluation: false,
};
Expand Down
99 changes: 95 additions & 4 deletions crates/celox-sv-analyzer/src/ast/lifetimes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,18 +31,27 @@ enum Entry {
/// time-zero initializations.
/// `directions` gives the argument directions of the subroutines a body
/// may call.
/// For the bodies of processes, `explicit` gives the statics declared with
/// the `static` keyword, and each body tells whether its process runs once;
/// see [`initialized_at_time_zero`].
pub(super) fn keep_static_locals<'b, T>(
state: &AnalysisState,
statics: &HashMap<String, T>,
directions: &Directions,
bodies: impl IntoIterator<Item = &'b mut Vec<Stmt>>,
bodies: impl IntoIterator<Item = (&'b mut Vec<Stmt>, bool)>,
explicit: Option<&HashSet<String>>,
) -> Result<(Vec<String>, Vec<Stmt>), AnalyzerError> {
let mut kept = Vec::new();
let mut initializers = Vec::new();
if statics.is_empty() {
return Ok((kept, initializers));
}
for body in bodies {
let mut bodies = bodies.into_iter().collect::<Vec<_>>();
let initialized = match explicit {
Some(explicit) => initialized_at_time_zero(state, statics, explicit, &bodies),
None => HashSet::default(),
};
for (body, _) in &mut bodies {
let mut concurrent = false;
for stmt in body.iter() {
stmt.walk(&mut |stmt| concurrent |= matches!(stmt, Stmt::Fork { .. }));
Expand All @@ -51,6 +60,7 @@ pub(super) fn keep_static_locals<'b, T>(
state,
body,
statics,
&initialized,
directions,
&mut kept,
&mut initializers,
Expand All @@ -60,10 +70,74 @@ pub(super) fn keep_static_locals<'b, T>(
Ok((kept, initializers))
}

/// The static locals of process `bodies` whose initializer is not constant
/// but reads no automatic variable, with the static locals they read. The
/// initializer of a static variable may be any run-time expression, and
/// runs once before any procedure starts (IEEE 1800-2023 6.8, 6.21); these
/// locals keep their value so that the time-zero initialization does that.
///
/// 6.21 requires the `static` keyword on an initialized static variable of
/// a procedural block, to show that the initializer runs only once. Without
/// it, a non-constant initializer is accepted only in a process that runs
/// once, such as an `initial` procedure, where running it at time zero
/// rather than on entry cannot repeat; in an `always` procedure it remains
/// rejected, like one that reads an automatic variable.
fn initialized_at_time_zero<T>(
state: &AnalysisState,
statics: &HashMap<String, T>,
explicit: &HashSet<String>,
bodies: &[(&mut Vec<Stmt>, bool)],
) -> HashSet<String> {
let mut initializers = HashMap::default();
let mut automatic = Vec::new();
for (body, once) in bodies {
for stmt in body.iter() {
stmt.walk(&mut |stmt| {
if let Stmt::Local { name, init } = stmt {
if statics.contains_key(name) {
let allowed = *once || explicit.contains(name);
initializers.insert(name.as_str(), (init.as_ref(), allowed));
} else {
automatic.push(name.as_str());
}
}
});
}
}
let runtime = |init: &Expr| {
!expr_is_constant(state, init) && !automatic.iter().any(|name| expr_reads(init, name))
};
let mut pending = initializers
.iter()
.filter(|(_, (init, allowed))| *allowed && init.is_some_and(runtime))
.map(|(name, _)| *name)
Comment thread
tignear marked this conversation as resolved.
Resolved
.collect::<Vec<_>>();
let mut initialized = HashSet::default();
while let Some(name) = pending.pop() {
let (init, allowed) = initializers.get(name).copied().unwrap_or_default();
if init.is_some_and(|init| !expr_is_constant(state, init) && !(allowed && runtime(init)))
|| !initialized.insert(name.to_string())
{
continue;
}
if let Some(init) = init {
pending.extend(
initializers
.keys()
.filter(|other| expr_reads(init, other))
.copied(),
);
}
}
initialized
}

#[allow(clippy::too_many_arguments)]
fn keep_in<T>(
state: &AnalysisState,
stmts: &mut Vec<Stmt>,
statics: &HashMap<String, T>,
initialized: &HashSet<String>,
directions: &Directions,
kept: &mut Vec<String>,
initializers: &mut Vec<Stmt>,
Expand All @@ -75,7 +149,8 @@ fn keep_in<T>(
// time slot (IEEE 1800-2023 16.5.1), which the local keeps.
if let Stmt::Local { name, init } = &stmts[index]
&& statics.contains_key(name)
&& (concurrent
&& (initialized.contains(name)
Comment thread
tignear marked this conversation as resolved.
Resolved
|| concurrent
|| sampled_reads(&stmts[index + 1..], name)
|| entry(
state,
Expand Down Expand Up @@ -109,6 +184,7 @@ fn keep_in<T>(
state,
then_body,
statics,
initialized,
directions,
kept,
initializers,
Expand All @@ -118,6 +194,7 @@ fn keep_in<T>(
state,
else_body,
statics,
initialized,
directions,
kept,
initializers,
Expand All @@ -130,6 +207,7 @@ fn keep_in<T>(
state,
&mut item.body,
statics,
initialized,
directions,
kept,
initializers,
Expand All @@ -141,6 +219,7 @@ fn keep_in<T>(
state,
default,
statics,
initialized,
directions,
kept,
initializers,
Expand All @@ -155,6 +234,7 @@ fn keep_in<T>(
state,
init,
statics,
initialized,
directions,
kept,
initializers,
Expand All @@ -164,6 +244,7 @@ fn keep_in<T>(
state,
step,
statics,
initialized,
directions,
kept,
initializers,
Expand All @@ -173,6 +254,7 @@ fn keep_in<T>(
state,
body,
statics,
initialized,
directions,
kept,
initializers,
Expand All @@ -181,7 +263,16 @@ fn keep_in<T>(
}
Stmt::Fork { branches, .. } => {
for branch in branches {
keep_in(state, branch, statics, directions, kept, initializers, true)?;
keep_in(
state,
branch,
statics,
initialized,
directions,
kept,
initializers,
true,
)?;
}
}
_ => {}
Expand Down
8 changes: 7 additions & 1 deletion crates/celox-sv-analyzer/src/ast/procedural.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ pub(super) struct BodyState<'a> {
/// The locals declared with a static lifetime: their unique names and
/// types.
pub statics: Vec<(String, Type)>,
/// The statics declared with the `static` keyword.
pub explicit_statics: HashSet<String>,
/// Function static declarations initialized once at time zero.
pub static_initializers: Vec<Stmt>,
/// Constant calls get a fresh frame, even for static declarations (13.4.3).
Expand Down Expand Up @@ -1134,6 +1136,9 @@ impl<'s, 't, 'a> BodyBuilder<'s, 't, 'a> {
_ => self.automatic,
};
if !automatic {
if native.lifetime.as_deref() == Some("static") {
self.state.explicit_statics.insert(name.clone());
}
let r#type = scoped_type(signal.r#type().clone(), &self.dims.const_env);
self.state.statics.push((name.clone(), r#type));
}
Expand Down Expand Up @@ -2555,7 +2560,8 @@ pub(super) fn lower_subroutine(
&tree.state,
&retained_types,
builder.state.subroutine_directions,
std::iter::once(&mut subroutine.body),
std::iter::once((&mut subroutine.body, false)),
None,
)?;
if retain_result {
kept.extend(subroutine.return_var.clone());
Expand Down
1 change: 1 addition & 0 deletions crates/celox-sv-analyzer/src/ast/procedural/control.rs
Original file line number Diff line number Diff line change
Expand Up @@ -708,6 +708,7 @@ mod tests {
subroutine_directions: &directions,
automatic: false,
statics: Vec::new(),
explicit_statics: HashSet::default(),
static_initializers: Vec::new(),
constant_evaluation: false,
};
Expand Down
21 changes: 21 additions & 0 deletions crates/celox-test-suite/src/sv/cases/procedural.vtest
Original file line number Diff line number Diff line change
Expand Up @@ -1510,3 +1510,24 @@
(tick clk) (assert_eq result wide)
(modify (set update 1))
(tick clk) (assert_eq result 0))

(case static_locals_with_run_time_initializers_run_before_procedures
(source "static_initializers.sv" #"
module Top(output bit [7:0] first, output bit [7:0] looped);
int m = 1;
initial m = 5;
initial begin
static int k = m + 1;
static int j = k * 10;
first = 8'(j);
for (int i = 0; i < 3; i++) begin
static int once = k;
once++;
looped = 8'(once);
end
end
endmodule
"#)
(top Top) (run_until 0)
(assert_eq first 20)
(assert_eq looped 5))
4 changes: 4 additions & 0 deletions crates/celox/tests/frontends/systemverilog/procedural.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,10 @@ sv_backends! {
@case "procedural::static_function_scratch_arrays_are_filled_before_reads";
}

fn static_locals_with_run_time_initializers_run_before_procedures(sim) {
@case "procedural::static_locals_with_run_time_initializers_run_before_procedures";
}

fn static_function_result_filled_by_constant_loops(sim) {
@case "procedural::static_function_result_filled_by_constant_loops";
}
Expand Down
Loading
Loading