From fa06cd7fc8890f7c57364ee2533bd6d45d1b6911 Mon Sep 17 00:00:00 2001 From: tignear Date: Fri, 23 Oct 2026 02:24:28 +0900 Subject: [PATCH] fix(sv): preserve guarded division types through casts Casts of divisions and remainders could fail with unknown operand signedness because the internal zero-divisor guard has an untyped unknown arm. Infer the guard's signedness and width from its operation, and avoid inserting guards again when binding an enclosing expression. This also preserves signed arithmetic in four-state lowering instead of creating nested unsigned muxes. Evaluate signing-cast operands in their original type before changing the result sign, so unsigned'(a / b) does not turn a signed division unsigned. Add two shared regressions for mixed signs, parameter divisors, narrowing, nested divisions, signing/type casts, and four-state zero-divisor results. Validation: - Full SV suite: 3952 passed, 20 existing ignored across five backends. - Analyzer unit tests: 791 passed, 3 existing ignored. - Shared suite unit tests: 46 passed, 2 existing ignored. - Icarus: both new cases passed. Verilator: ordinary case passed; the adapter reports the four-state case unsupported due to four-state expectations. - Affected library/harness Clippy (-D warnings), cargo fmt and diff checks pass. - Unchanged Taxi sources with O1/two-state pass 29 cocotb tests across mac_ctrl_rx[8], mac_ctrl_tx[8], and zircon_ip_len_cksum[32], with zero skips. Three other previously cast-blocked configurations now reach existing $size or SERDES array-range blockers. All 52 resolved SV source fingerprints match the original audit; no optimization settings or exclusions changed. Change-Id: I560b79b58966e9083629921a4dbf6563ff5d09f3 --- crates/celox-sv-analyzer/src/ast/casts.rs | 11 ++- .../celox-sv-analyzer/src/ast/comb_rewrite.rs | 8 +++ .../celox-sv-analyzer/src/ast/expressions.rs | 8 +++ crates/celox-sv-analyzer/src/ast/inlining.rs | 14 ++++ .../src/sv/cases/operators.vtest | 69 +++++++++++++++++++ .../frontends/systemverilog/operators.rs | 8 +++ 6 files changed, 117 insertions(+), 1 deletion(-) diff --git a/crates/celox-sv-analyzer/src/ast/casts.rs b/crates/celox-sv-analyzer/src/ast/casts.rs index 0f17d06b6b6..f1819cdce52 100644 --- a/crates/celox-sv-analyzer/src/ast/casts.rs +++ b/crates/celox-sv-analyzer/src/ast/casts.rs @@ -87,8 +87,17 @@ pub(super) fn runtime_cast_target_expr( let width = expr_static_width(state, &expr, packed_dimensions).ok_or_else(|| { unsupported("signedness cast of an operand without a static width") })?; + let operand_signed = + dimensions::expression_signedness(&expr, packed_dimensions, const_env) + .ok_or_else(|| unsupported("cast of an operand whose signedness is unknown"))?; return Ok(Expr::Resize { - expr: Box::new(expr), + // Signing changes the result type after evaluating the operand + // in its own type; it must not change a division's operator. + expr: Box::new(Expr::Resize { + expr: Box::new(expr), + width, + signed: operand_signed, + }), width, signed, }); diff --git a/crates/celox-sv-analyzer/src/ast/comb_rewrite.rs b/crates/celox-sv-analyzer/src/ast/comb_rewrite.rs index 41cb101e880..6241980bc29 100644 --- a/crates/celox-sv-analyzer/src/ast/comb_rewrite.rs +++ b/crates/celox-sv-analyzer/src/ast/comb_rewrite.rs @@ -84,6 +84,14 @@ pub(super) fn expr_static_width( } } Expr::Inside { .. } => Some(1), + // The synthetic unknown arm has the guarded operation's width. + Expr::Mux { + then_expr, + else_expr, + .. + } if matches!(&**then_expr, Expr::Literal(text) if text == crate::DIV_ZERO_UNKNOWN_LITERAL) => { + expr_static_width(state, else_expr, packed_dimensions) + } Expr::Mux { then_expr, else_expr, diff --git a/crates/celox-sv-analyzer/src/ast/expressions.rs b/crates/celox-sv-analyzer/src/ast/expressions.rs index 19ded55a670..5dd450c54b0 100644 --- a/crates/celox-sv-analyzer/src/ast/expressions.rs +++ b/crates/celox-sv-analyzer/src/ast/expressions.rs @@ -3,6 +3,14 @@ use super::*; pub(super) fn guard_zero_divisions(state: &AnalysisState, expr: Expr) -> Expr { + // Binding an enclosing expression can visit an already guarded operand. + // Its children were guarded when it was constructed. Wrapping it again + // loses the operation's type when the internal unknown arm is lowered. + if matches!(&expr, Expr::Mux { then_expr, .. } + if matches!(&**then_expr, Expr::Literal(text) if text == crate::DIV_ZERO_UNKNOWN_LITERAL)) + { + return expr; + } match expr { Expr::Ident(_) | Expr::Literal(_) => expr, Expr::Select { diff --git a/crates/celox-sv-analyzer/src/ast/inlining.rs b/crates/celox-sv-analyzer/src/ast/inlining.rs index 87149c159ab..424f95348e4 100644 --- a/crates/celox-sv-analyzer/src/ast/inlining.rs +++ b/crates/celox-sv-analyzer/src/ast/inlining.rs @@ -83,6 +83,20 @@ pub(super) fn expr_signedness_with_return_types( ) } } + // The internal zero-divisor arm represents an unknown value of the + // guarded operation's type, not a separate source-language operand. + Expr::Mux { + then_expr, + else_expr, + .. + } if matches!(&**then_expr, Expr::Literal(text) if text == crate::DIV_ZERO_UNKNOWN_LITERAL) => { + expr_signedness_with_return_types( + else_expr, + identifiers, + functions, + function_return_types, + ) + } Expr::Mux { then_expr, else_expr, diff --git a/crates/celox-test-suite/src/sv/cases/operators.vtest b/crates/celox-test-suite/src/sv/cases/operators.vtest index 611fb4327de..28f734e4535 100644 --- a/crates/celox-test-suite/src/sv/cases/operators.vtest +++ b/crates/celox-test-suite/src/sv/cases/operators.vtest @@ -1690,3 +1690,72 @@ endmodule (assert_eq mul 0xfffffffc) (assert_eq div 0) (assert_eq dynamic_mul 0xfffffff0)) + +; IEEE 1800-2023 6.24.1: a size cast preserves operand signedness. +(case casts_preserve_guarded_division_signedness + (source "guarded_division_casts.sv" #" + module Top #(parameter DIVISOR = 2)( + input logic signed [12:0] a, + input logic signed [3:0] b, + input logic [3:0] u, + output logic [31:0] quotient, remainder, mixed, narrowed, nested, + output logic [31:0] parameter_quotient, typed, unsigned_quotient, signed_mixed); + assign quotient = 32'(a / b); + assign remainder = 32'(a % b); + assign mixed = 32'(a / u); + assign narrowed = 4'(a / b); + assign nested = 32'((a / b) / b); + assign parameter_quotient = 32'(a / DIVISOR); + assign typed = int'(a / b); + assign unsigned_quotient = unsigned'(a / b); + assign signed_mixed = signed'(a / u); + endmodule + "#) + (top Top) + (modify (set a 0x1ff3) (set b 2) (set u 2)) + (assert_eq quotient 0xfffffffa) + (assert_eq remainder 0xffffffff) + (assert_eq mixed 4089) + (assert_eq narrowed 0xfffffffa) + (assert_eq nested 0xfffffffd) + (assert_eq parameter_quotient 0xfffffffa) + (assert_eq typed 0xfffffffa) + (assert_eq unsigned_quotient 8186) + (assert_eq signed_mixed 4089) + (modify (set u 1)) + (assert_eq signed_mixed 0xfffffff3) + (modify (set u 2)) + (modify (set a 13) (set b 0xe)) + (assert_eq quotient 0xfffffffa) + (assert_eq remainder 1) + (assert_eq mixed 6) + (assert_eq narrowed 0xfffffffa) + (assert_eq nested 3) + (assert_eq parameter_quotient 6) + (assert_eq typed 0xfffffffa) + (assert_eq unsigned_quotient 8186) + (assert_eq signed_mixed 6)) + +(case casts_preserve_guarded_division_unknowns + (four_state) + (source "guarded_division_unknowns.sv" #" + module Top(input logic signed [12:0] a, input logic signed [3:0] b, + output logic [31:0] quotient, remainder, nested, + output int two_state); + assign quotient = 32'(a / b); + assign remainder = 32'(a % b); + assign nested = 32'((a / b) / b); + assign two_state = int'(a / b); + endmodule + "#) + (top Top) + (modify (set a 0x1ff3) (set b 2)) + (assert_eq quotient 0xfffffffa) + (assert_eq remainder 0xffffffff) + (assert_eq nested 0xfffffffd) + (assert_eq two_state 0xfffffffa) + (modify (set b 0)) + (assert_eq quotient (fourstate 0xffffffff 0xffffffff)) + (assert_eq remainder (fourstate 0xffffffff 0xffffffff)) + (assert_eq nested (fourstate 0xffffffff 0xffffffff)) + (assert_eq two_state 0)) diff --git a/crates/celox/tests/frontends/systemverilog/operators.rs b/crates/celox/tests/frontends/systemverilog/operators.rs index 425858e0afb..04f2499e68f 100644 --- a/crates/celox/tests/frontends/systemverilog/operators.rs +++ b/crates/celox/tests/frontends/systemverilog/operators.rs @@ -1,4 +1,12 @@ sv_backends! { + fn casts_preserve_guarded_division_signedness(sim) { + @case "operators::casts_preserve_guarded_division_signedness"; + } + + fn casts_preserve_guarded_division_unknowns(sim) { + @case "operators::casts_preserve_guarded_division_unknowns"; + } + fn preserves_power_signedness_in_arithmetic(sim) { @case "operators::preserves_power_signedness_in_arithmetic"; }