From 5dde492197c89cb5af4abca9226d089b6d352510 Mon Sep 17 00:00:00 2001 From: tignear Date: Sat, 24 Oct 2026 10:21:33 +0900 Subject: [PATCH] fix(sv): accept real-valued module parameter overrides Taxi's watchdog passes COUNT_125US=125000/6.4 through module parameters, but the frontend rejected the binding because it only tried integral constant evaluation. Accept numeric real constants using the existing real evaluator and retain the original expression for the receiving parameter's type context and specialization. Continue rejecting unresolved values and preserve diagnostic tracking issue 461. Add three shared SystemVerilog regressions covering fractional forwarding, real/shortreal/integer conversion contexts, math functions, and generate scopes. Exercise each through all five Celox backends. Validation: - Focused shared cases: 15 passed; full SV suite: 4,366 passed, 20 existing ignored. - Analyzer: 795 passed, 3 existing ignored; frontend: 5 passed; shared suite: 30 passed; doctests: 2 passed. - Final diagnostic regression passed; affected libraries and SV harness passed Clippy with -D warnings; formatting passed. - Icarus: all three new cases passed. Verilator 5.052 passed two; the typed shortreal case mismatches because Verilator promotes shortreal to real (Warning-SHORTREAL). Slang 12 elaborates all three. - A custom cocotb regression of unchanged upstream Taxi watchdog RTL passed with native O1 and two-state simulation: load 19531 and 3, countdown, reload, and reset. This is not a full upstream test-module pass. - Paired release-frontend replay of 25 previously COUNT_125US-blocked Taxi configurations: baseline rejects all 25; this patch reaches STAT_PREFIX_STR in seven and GT_TX_DIFFCTRL in one. The other 17 time out at 120 seconds. No complete FPGA-core configuration passes are claimed. All 188 resolved upstream sources remain unchanged. The remaining unpacked-array parameter forwarding errors and frontend runtime limits are separate follow-up work. Change-Id: Id2cf14ddc0d997cc362eb07f2211c6bbedd6ed87 --- crates/celox-frontend-sv/src/lowering.rs | 34 +++++-- crates/celox-sv-analyzer/src/lib.rs | 1 + crates/celox-sv-analyzer/src/typecheck.rs | 9 ++ .../src/sv/cases/floating_point.vtest | 94 +++++++++++++++++++ .../frontends/systemverilog/floating_point.rs | 10 ++ 5 files changed, 141 insertions(+), 7 deletions(-) diff --git a/crates/celox-frontend-sv/src/lowering.rs b/crates/celox-frontend-sv/src/lowering.rs index 605b02f32bb..c44bd3b34c1 100644 --- a/crates/celox-frontend-sv/src/lowering.rs +++ b/crates/celox-frontend-sv/src/lowering.rs @@ -3279,17 +3279,19 @@ fn evaluated_parameter_overrides( continue; }; if !matches!(value, sv::ir::ConstExpr::Literal(value) if value == "$") { - sv::typecheck::eval_const_integral_literal_with_types( + let integral = sv::typecheck::eval_const_integral_literal_with_types( value, &constants, &HashMap::default(), - ) - .ok_or_else(|| { - sv::AnalyzerError::Unsupported(format!( - "non-integer module parameter override `{}`", + ); + if integral.is_none() + && sv::typecheck::eval_const_real_expr(value, &constants).is_none() + { + return Err(sv::AnalyzerError::Unsupported(format!( + "nonconstant module parameter override `{}`", parameter.name - )) - })?; + ))); + } } evaluated.insert(parameter.name.clone(), value.clone()); } @@ -9140,6 +9142,24 @@ pub(crate) fn unsupported_sv_inout(path: String) -> ParserError { mod tests { use super::*; + #[test] + fn parameter_override_validation_rejects_unresolved_values() { + let overrides = [LoweredSvParameterOverride { + name: "P".into(), + value: Some(sv::ir::ConstExpr::Ident("unresolved".into())), + bound_type: None, + bound_pattern: None, + }]; + let error = evaluated_parameter_overrides(&overrides).unwrap_err(); + assert_eq!(error.tracking_issue(), 461); + assert!( + error + .to_string() + .contains("nonconstant module parameter override `P`"), + "{error}" + ); + } + /// The process kernels of `source`, other than declaration initializers. fn procedure_kernels(source: &str) -> usize { let scheduled = schedule_sources( diff --git a/crates/celox-sv-analyzer/src/lib.rs b/crates/celox-sv-analyzer/src/lib.rs index 76c037db994..58712e85d28 100644 --- a/crates/celox-sv-analyzer/src/lib.rs +++ b/crates/celox-sv-analyzer/src/lib.rs @@ -193,6 +193,7 @@ const UNSUPPORTED_CONSTRUCT_ISSUES: &[(&str, u32)] = &[ ("gate primitive instantiation", 457), ("undriven net declaration", 460), ("non-integer module parameter override", 461), + ("nonconstant module parameter override", 461), ("always_ff event expression", 464), ("package net", 1146), ]; diff --git a/crates/celox-sv-analyzer/src/typecheck.rs b/crates/celox-sv-analyzer/src/typecheck.rs index 48d1d741fe0..580948196f1 100644 --- a/crates/celox-sv-analyzer/src/typecheck.rs +++ b/crates/celox-sv-analyzer/src/typecheck.rs @@ -328,6 +328,15 @@ pub fn eval_const_expr_with_types( eval_const_expr(&expr, constants) } +/// Evaluate a numeric constant expression in a real context. +/// +/// Integral subexpressions retain their integer arithmetic before conversion. +/// Callers that only validate a parameter binding should preserve the original +/// expression so the receiving declaration still supplies its type context. +pub fn eval_const_real_expr(expr: &ConstExpr, constants: &HashMap) -> Option { + real::evaluate(expr, constants) +} + /// Evaluate a literal constant expression without discarding four-state masks. pub fn eval_const_integral_literal_with_types( expr: &ConstExpr, diff --git a/crates/celox-test-suite/src/sv/cases/floating_point.vtest b/crates/celox-test-suite/src/sv/cases/floating_point.vtest index 885e6152027..1c954ffd085 100644 --- a/crates/celox-test-suite/src/sv/cases/floating_point.vtest +++ b/crates/celox-test-suite/src/sv/cases/floating_point.vtest @@ -263,3 +263,97 @@ endmodule "#) (top Top) (run_until 0) (assert_eq done 1) (expect_output "file=[ 1.50] expr=[3.0]\nwrite=[1.5e+00]\n")) + +; IEEE 1800-2023 6.20.2: untyped parameters take the type of their final value. +(case real_parameter_overrides_preserve_fractional_values + (source "real_parameter_overrides.sv" #" + module Leaf #(parameter P=1)(output wire [63:0] bits, + output int truncated, rounded); + localparam COUNT_W=$clog2($rtoi(P)+1); + localparam logic [COUNT_W-1:0] COUNT=COUNT_W'($rtoi(P)); + assign bits=$realtobits(P); + assign truncated=COUNT; + assign rounded=P; + endmodule + module Negative #(parameter P=-1)(output wire [63:0] bits, + output int truncated, rounded); + assign bits=$realtobits(P); + assign truncated=$rtoi(P); + assign rounded=P; + endmodule + module Relay #(parameter P=2)(output wire [63:0] bits, + output int truncated, rounded); + Leaf #(.P(P+0.25)) leaf(.bits(bits),.truncated(truncated),.rounded(rounded)); + endmodule + module Top(output wire [63:0] direct_bits, forwarded_bits, negative_bits, + output int direct_truncated, direct_rounded, + forwarded_truncated, forwarded_rounded, + negative_truncated, negative_rounded); + localparam PERIOD=6.4; + Leaf #(.P(125000/PERIOD)) direct(.bits(direct_bits),.truncated(direct_truncated), + .rounded(direct_rounded)); + Relay #(.P(125000/PERIOD)) forwarded(.bits(forwarded_bits),.truncated(forwarded_truncated), + .rounded(forwarded_rounded)); + Negative #(.P(-2.5)) negative(.bits(negative_bits),.truncated(negative_truncated), + .rounded(negative_rounded)); + endmodule + "#) + (top Top) + (eval) + (assert_eq direct_bits 0x40d312d000000000) + (assert_eq forwarded_bits 0x40d312e000000000) + (assert_eq negative_bits 0xc004000000000000) + (assert_eq direct_truncated 19531) (assert_eq direct_rounded 19531) + (assert_eq forwarded_truncated 19531) (assert_eq forwarded_rounded 19532) + (assert_eq negative_truncated 0xfffffffe) (assert_eq negative_rounded 0xfffffffd)) + +(case real_parameter_overrides_keep_declared_conversion_contexts + (source "typed_real_parameter_overrides.sv" #" + module Typed #(parameter real R=0.0, parameter shortreal S=0.0, parameter int I=0) + (output wire [63:0] real_bits, output wire [31:0] short_bits, output int integer_value); + assign real_bits=$realtobits(R); + assign short_bits=$shortrealtobits(S); + assign integer_value=I; + endmodule + module Top(output wire [63:0] fractional_bits, integer_bits, + output wire [31:0] narrowed_bits, negative_zero_bits, + output int negative_value, positive_value); + localparam real LARGE=16777217.0; + Typed #(.R(LARGE/2.0),.S(LARGE),.I(-2.5)) negative(.real_bits(fractional_bits), + .short_bits(narrowed_bits),.integer_value(negative_value)); + Typed #(.R(5),.S(-0.0),.I(2.5)) positive(.real_bits(integer_bits), + .short_bits(negative_zero_bits),.integer_value(positive_value)); + endmodule + "#) + (top Top) + (eval) + (assert_eq fractional_bits 0x4160000010000000) + (assert_eq integer_bits 0x4014000000000000) + (assert_eq narrowed_bits 0x4b800000) + (assert_eq negative_zero_bits 0x80000000) + (assert_eq negative_value 0xfffffffd) (assert_eq positive_value 3)) + +(case real_parameter_overrides_from_math_and_generate_scopes + (source "generated_real_parameter_overrides.sv" #" + package Values; + localparam real STEP=0.25; + endpackage + module Leaf #(parameter P=0)(output wire [63:0] bits); + assign bits=$realtobits(P); + endmodule + module Top(output wire [191:0] generated_bits, + output wire [63:0] math_bits, selected_bits); + import Values::*; + localparam BASE=$sqrt(2.25); + for(genvar i=0;i<3;i++) begin: g + Leaf #(.P(BASE+i*STEP)) leaf(.bits(generated_bits[i*64 +: 64])); + end + Leaf #(.P($sqrt(2.25))) math(.bits(math_bits)); + Leaf #(.P(1 ? -0.0 : 1.5)) selected(.bits(selected_bits)); + endmodule + "#) + (top Top) + (eval) + (assert_eq generated_bits 0x40000000000000003ffc0000000000003ff8000000000000) + (assert_eq math_bits 0x3ff8000000000000) + (assert_eq selected_bits 0x8000000000000000)) diff --git a/crates/celox/tests/frontends/systemverilog/floating_point.rs b/crates/celox/tests/frontends/systemverilog/floating_point.rs index 84eb8028ee6..fcc1265bd03 100644 --- a/crates/celox/tests/frontends/systemverilog/floating_point.rs +++ b/crates/celox/tests/frontends/systemverilog/floating_point.rs @@ -1,6 +1,16 @@ use super::*; sv_backends! { + fn real_parameter_overrides_preserve_fractional_values(sim) { + @case "floating_point::real_parameter_overrides_preserve_fractional_values"; + } + fn real_parameter_overrides_keep_declared_conversion_contexts(sim) { + @case "floating_point::real_parameter_overrides_keep_declared_conversion_contexts"; + } + fn real_parameter_overrides_from_math_and_generate_scopes(sim) { + @case "floating_point::real_parameter_overrides_from_math_and_generate_scopes"; + } + fn real_math_runtime_functions(sim) { @case "real_math::runtime_functions"; } fn real_math_constant_functions(sim) { @case "real_math::constant_functions"; } fn real_arithmetic_and_rounding(sim) { @case "floating_point::real_arithmetic_and_rounding"; }