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
39 changes: 39 additions & 0 deletions crates/celox-frontend-sv/src/lowering.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10686,6 +10686,45 @@ fn inferred_clock(
mod tests {
use super::*;

#[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);
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];
endfunction
assign q = d ^ pattern();
endmodule"#;
for four_state in [false, true] {
let output = schedule_sources(
&[(source, Path::new("top.sv"))],
"Top",
&[],
&[],
&[],
four_state,
&celox_frontend_core::ParallelScheduleOptions::default(),
&FrontendTraceOptions::default(),
None,
&mut Vec::new(),
)
.unwrap();
let instructions: usize = output
.scheduled
.sir
.eval_comb
.iter()
.flat_map(|unit| unit.blocks.values())
.map(|block| block.instructions.len())
.sum();
assert!(
instructions < 32,
"constant scratch loops produced {instructions} runtime instructions"
);
}
}

#[test]
fn bit_select_width_does_not_evaluate_a_runtime_zero_sample() {
let index = sv::ir::ConstExpr::Ident("index".into());
Expand Down
11 changes: 11 additions & 0 deletions crates/celox-frontend-sv/src/lowering/comb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3288,6 +3288,17 @@ impl<'p, 'a> Comb<'p, 'a> {
args: &[Option<sv::ir::Expr>],
arg_order: &[usize],
) -> Result<Option<Value>, sv::AnalyzerError> {
if args.is_empty()
&& let Some(literal) = self.m.constant_functions.value(name)
Comment on lines +3291 to +3292

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Preserve four-state case matching when folding runtime calls

In four-state mode, a zero-argument function containing an ordinary case can now be replaced with the wrong constant. For example, function automatic int f(); logic x; case (x) 1'bx: return 1; default: return 0; endcase endfunction should return 1, but the reused interpreter compares case labels with BinaryOp::Eq in Frame::stmt, then treats the unknown comparison as false, producing 0. Ordinary runtime lowering correctly uses BinaryOp::EqCase for CaseKind::Exact in Comb::case. Use the same case-equality semantics in the interpreter, or decline this optimization for unknown-sensitive case comparisons.

Useful? React with 👍 / 👎.

Comment on lines +3291 to +3292

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Apply case-wide type normalization before folding calls

In either state mode, the newly reused interpreter compares each case label independently instead of using the common width and signedness of the selector and all labels. For example, an automatic function containing logic signed [7:0] x = 8'hff; case (x) 16'shffff: return 1; 16'h0000: return 2; default: return 0; endcase is folded to 1, although it should return 0: the unsigned second label makes every comparison unsigned, so the selector must zero-extend to 16'h00ff. Ordinary runtime lowering already implements this normalization in Comb::case_conditions. Apply that normalization in the interpreter too, or decline folding these cases.

Useful? React with 👍 / 👎.

{
let node = self.alloc(SLTNode::Constant(
literal.value,
literal.mask,
literal.width,
literal.signed,
))?;
return Ok(Some((node, Sources::default())));
}
let subroutine = self
.m
.subroutine(name)
Expand Down
2 changes: 2 additions & 0 deletions crates/celox-frontend-sv/src/lowering/procedural.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ pub(super) struct ProcModule<'a> {
pub constants: &'a HashMap<String, i128>,
pub parameter_types: &'a HashMap<String, (usize, bool)>,
pub subroutines: HashMap<String, sv::ir::Subroutine>,
pub constant_functions: sv::RuntimeConstantFunctions<'a>,
/// DPI-C imports of the module, by SystemVerilog name.
pub dpi_imports: HashMap<String, sv::ir::DpiImport>,
/// Extern functions the module calls, by local index.
Expand Down Expand Up @@ -246,6 +247,7 @@ impl<'a> ProcModule<'a> {
constants,
parameter_types,
subroutines,
constant_functions: sv::RuntimeConstantFunctions::new(module, constants, four_state),
dpi_imports,
extern_functions: Vec::new(),
extern_calls: 0,
Expand Down
Loading
Loading