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
58 changes: 49 additions & 9 deletions crates/celox-frontend-sv/src/lowering.rs
Original file line number Diff line number Diff line change
Expand Up @@ -945,12 +945,13 @@ fn package_warnings(
/// instance each, numbered from `first`.
fn lower_package_states(
packages: &sv::Packages,
design: &sv::Design,
first: usize,
four_state: bool,
ff_parts: bool,
warnings: &mut Vec<crate::warning::PendingWarning>,
) -> Result<Vec<(String, ModuleId, LoweredSvModule)>, sv::AnalyzerError> {
let states = packages.specialized_state_modules();
let states = packages.specialized_state_modules(design);
let mut states = states.iter().collect::<Vec<_>>();
states.sort_by(|left, right| left.name().cmp(right.name()));
states
Expand Down Expand Up @@ -2200,6 +2201,12 @@ fn prepare_external_hierarchy_collecting(
analysis_options,
)?;
package_warnings(&analyzed, warnings);
// The specializations that the modules request of package class
// templates, shared by all of their analyses.
let mut design = analyzed
.values()
.next()
.map_or_else(sv::Design::default, |module| module.packages.design());
let dummy = sv::ParsedSource::parse(
"module __celox_foreign(); endmodule",
Path::new("<foreign-signature>"),
Expand All @@ -2211,6 +2218,7 @@ fn prepare_external_hierarchy_collecting(
&[],
&sv::ModuleInterfaces::default(),
&sv::Packages::default(),
&mut sv::Design::default(),
)?;
let lower_foreign = |signature: &ExternalModule| {
let variables: HashMap<_, _> = signature
Expand Down Expand Up @@ -2405,6 +2413,7 @@ fn prepare_external_hierarchy_collecting(
.ok_or_else(|| unsupported_sv_instance(key.name.clone()))?;
let lowered = specialize_module(
base,
&mut design,
&key,
four_state,
false,
Expand Down Expand Up @@ -2444,6 +2453,7 @@ fn prepare_external_hierarchy_collecting(
module_id,
specialize_module(
base,
&mut design,
key,
four_state,
false,
Expand Down Expand Up @@ -2517,6 +2527,7 @@ fn prepare_external_hierarchy_collecting(
};
let package_states = lower_package_states(
&any_module.packages,
&design,
module_ids.len(),
four_state,
false,
Expand Down Expand Up @@ -2875,6 +2886,12 @@ fn schedule_sources_collecting(
let found_warnings = warnings.len();
let analyzed = analyze_sources(sources, &[top], analysis_options.clone())?;
package_warnings(&analyzed, warnings);
// The specializations that the modules request of package class
// templates, shared by all of their analyses.
let mut design = analyzed
.values()
.next()
.map_or_else(sv::Design::default, |module| module.packages.design());
let top = top.to_string();
let root_key = LoweredSvModuleKey {
name: top.clone(),
Expand Down Expand Up @@ -2921,6 +2938,7 @@ fn schedule_sources_collecting(
let module_id = module_ids[&key];
let module = match specialize_module(
base,
&mut design,
&key,
four_state,
false,
Expand All @@ -2938,7 +2956,7 @@ fn schedule_sources_collecting(
// hierarchical names.
Err(error) if hierarchical_reference_path(&error).is_some() => {
hierarchical_modules.push(module_id);
specialize_module_declarations(base, &key)?
specialize_module_declarations(base, &mut design, &key)?
}
Err(error) => return Err(error.into()),
};
Expand Down Expand Up @@ -3018,7 +3036,13 @@ fn schedule_sources_collecting(
.iter()
.map(|reference| reference.signal.clone())
.collect();
match analyze_specialization(&analyzed[&key.name], key, &signals, &mut Vec::new()) {
match analyze_specialization(
&analyzed[&key.name],
&mut design,
key,
&signals,
&mut Vec::new(),
) {
Ok(ir) => break ir,
// A name with an instance index that is a constant
// expression reaches the analysis with the index evaluated.
Expand Down Expand Up @@ -3096,6 +3120,7 @@ fn schedule_sources_collecting(
.collect();
Ok(specialize_module(
base,
&mut design,
key,
four_state,
parallel.enabled(),
Expand Down Expand Up @@ -3185,6 +3210,7 @@ fn schedule_sources_collecting(

let package_states = lower_package_states(
&analyzed[&top].packages,
&design,
module_ids.len(),
four_state,
parallel.enabled(),
Expand Down Expand Up @@ -3434,6 +3460,7 @@ fn specialization_overrides(
/// `hierarchical`.
fn analyze_specialization(
module: &AnalyzedSvModule,
design: &mut sv::Design,
key: &LoweredSvModuleKey,
hierarchical: &[sv::ast::HierarchicalSignal],
warnings: &mut Vec<crate::warning::PendingWarning>,
Expand All @@ -3446,6 +3473,7 @@ fn analyze_specialization(
&pattern_overrides,
&module.interfaces,
&module.packages,
design,
hierarchical,
);
if let Err(error) = &analyzed {
Expand All @@ -3459,6 +3487,7 @@ fn analyze_specialization(
#[allow(clippy::too_many_arguments)]
fn specialize_module(
module: &AnalyzedSvModule,
design: &mut sv::Design,
key: &LoweredSvModuleKey,
four_state: bool,
ff_parts: bool,
Expand All @@ -3473,7 +3502,7 @@ fn specialize_module(
ir
}
None => {
analyzed = analyze_specialization(module, key, hierarchical.signals, warnings)?;
analyzed = analyze_specialization(module, design, key, hierarchical.signals, warnings)?;
&analyzed
}
};
Expand Down Expand Up @@ -3570,6 +3599,7 @@ fn propagate_force_signals(instances: &mut [LoweredSvInstance], force_signals: &
/// The declarations and instances of specialization `key`.
fn specialize_module_declarations(
module: &AnalyzedSvModule,
design: &mut sv::Design,
key: &LoweredSvModuleKey,
) -> Result<hierarchy::ElaboratedModule, sv::AnalyzerError> {
let (overrides, type_overrides, pattern_overrides) = specialization_overrides(key)?;
Expand All @@ -3580,6 +3610,7 @@ fn specialize_module_declarations(
&pattern_overrides,
&module.interfaces,
&module.packages,
design,
)?;
let specialized = ir
.modules()
Expand Down Expand Up @@ -11246,17 +11277,26 @@ mod tests {

#[test]
fn stateless_function_scratch_loops_do_not_become_runtime_loops() {
let source = r#"module Top(input wire [255:0] d, output wire [255:0] q);
// The second result keeps bit 15 of every row between calls; nothing
// reads it, so each call still returns the same constant.
let sources = [16, 15].map(|columns| {
format!(
r#"module Top(input wire [255:0] d, output wire [255:0] q);
function [15:0][15:0] pattern();
logic [15:0] words [15:0];
for (int i=0; i<16; i++) words[i] = 16'b1 << i;
for (int i=0; i<16; i++) for (int j=0; j<16; j++) pattern[i][j] = words[i][j];
for (int i=0; i<16; i++) for (int j=0; j<{columns}; j++) pattern[i][j] = words[i][j];
endfunction
assign q = d ^ pattern();
endmodule"#;
for four_state in [false, true] {
endmodule"#
)
});
for (source, four_state) in sources
.iter()
.flat_map(|source| [(source, false), (source, true)])
{
let output = schedule_sources(
&[(source, Path::new("top.sv"))],
&[(source.as_str(), Path::new("top.sv"))],
"Top",
&[],
&[],
Expand Down
6 changes: 5 additions & 1 deletion crates/celox-frontend-sv/src/lowering/procedural.rs
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,7 @@ impl<'a> ProcModule<'a> {
/// Whether a process calls a system function that reads the state of
/// the simulation, such as `$urandom` or `$time`, including through user
/// subroutines and omitted argument defaults.
pub fn reads_simulation_state(&self, body: &[sv::ir::Stmt]) -> bool {
pub fn reads_simulation_state(&mut self, body: &[sv::ir::Stmt]) -> bool {
let mut calls = Vec::new();
for stmt in body {
stmt.walk(&mut |stmt| stmt_calls(stmt, &mut calls));
Expand All @@ -383,6 +383,10 @@ impl<'a> ProcModule<'a> {
| sv::ir::Callee::Checked { .. } => return true,
sv::ir::Callee::Subroutine(name) => name,
};
// A call lowered as its constant value runs nothing.
if args.is_empty() && self.constant_functions.value(&name).is_some() {
continue;
}
if let Some(subroutine) = self.subroutine(&name) {
if !subroutine.persistent_locals.is_empty() {
return true;
Expand Down
18 changes: 15 additions & 3 deletions crates/celox-sv-analyzer/src/analyze.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ fn analyze_source_impl(source: ast::Source) -> Result<ir::Ir, AnalyzerError> {
for module in source.modules() {
let syntax = crate::source_tree::SourceTree::empty();
let tree = &module.constant_context(&syntax);
let session = &mut ast::Session::default();
let id = module_table.insert(module)?;
let mut constants = HashMap::default();
let mut parameter_types = HashMap::default();
Expand All @@ -44,6 +45,7 @@ fn analyze_source_impl(source: ast::Source) -> Result<ir::Ir, AnalyzerError> {
let value: Option<ir::ConstExpr> = parameter.value().cloned().map(Into::into);
let (resolved_value, literal) = parameter.resolved_value_and_literal(
tree,
session,
&constants,
&parameter_types,
&parameter_literals,
Expand Down Expand Up @@ -80,7 +82,7 @@ fn analyze_source_impl(source: ast::Source) -> Result<ir::Ir, AnalyzerError> {
.with_declaration(
parameter.value().map(|value| {
module
.declaration_expr(tree, value, &parameter_signedness)
.declaration_expr(tree, session, value, &parameter_signedness)
.into()
}),
parameter
Expand All @@ -89,10 +91,20 @@ fn analyze_source_impl(source: ast::Source) -> Result<ir::Ir, AnalyzerError> {
.map(|range| {
ir::PackedRange::new(
module
.declaration_expr(tree, range.left(), &parameter_signedness)
.declaration_expr(
tree,
session,
range.left(),
&parameter_signedness,
)
.into(),
module
.declaration_expr(tree, range.right(), &parameter_signedness)
.declaration_expr(
tree,
session,
range.right(),
&parameter_signedness,
)
.into(),
)
})
Expand Down
Loading
Loading