From 7e69561cfa427e027da7af31363df3f642d29a6d Mon Sep 17 00:00:00 2001 From: tignear Date: Tue, 27 Oct 2026 03:53:52 +0900 Subject: [PATCH 1/2] feat(sv): support logical implication expressions Change-Id: I8fe3f15eadf55c160b3995f4b0928282055dc244 --- conformance/sv-tests/README.md | 7 ++ conformance/sv-tests/expected.tsv | 24 +++---- .../src/ast/constant_binding.rs | 16 ++++- .../src/ast/expression_binding/direct.rs | 14 +++- .../src/sv/cases/operators.vtest | 67 +++++++++++++++++++ .../frontends/systemverilog/operators.rs | 3 + 6 files changed, 116 insertions(+), 15 deletions(-) diff --git a/conformance/sv-tests/README.md b/conformance/sv-tests/README.md index aeb7df2da7b..ee1f17ca2ae 100644 --- a/conformance/sv-tests/README.md +++ b/conformance/sv-tests/README.md @@ -145,6 +145,13 @@ contains 3,300 case/mode verdicts at the pinned revision. These are its only changed rows, with no passing case regressing: 1,201 cases pass in 2state and 1,212 in 4state. Only these reviewed expectations are updated. +Logical implication (`->`) now passes six generated cases in both state modes. +The binary-operator case passes elaboration; the five logical-operator cases +execute simulations and pass their assertions. A full run at the pinned revision +changes only these twelve verdicts, with no passing case regressing: 1,208 cases +pass in 2state and 1,219 in 4state. Shared tests also cover short-circuiting and +unknown operands (IEEE 1800-2023 11.4.7). + ## Running locally Use the locked development shell and install the pinned [`requirements.txt`](requirements.txt) in a Python environment. diff --git a/conformance/sv-tests/expected.tsv b/conformance/sv-tests/expected.tsv index b9bae1143b3..743c206fd2e 100644 --- a/conformance/sv-tests/expected.tsv +++ b/conformance/sv-tests/expected.tsv @@ -1819,8 +1819,8 @@ generated/binary_op/11.4.7--binary_op_log_and.sv 2state pass generated/binary_op/11.4.7--binary_op_log_and.sv 4state pass generated/binary_op/11.4.7--binary_op_log_equiv.sv 2state fail generated/binary_op/11.4.7--binary_op_log_equiv.sv 4state fail -generated/binary_op/11.4.7--binary_op_log_imp.sv 2state fail -generated/binary_op/11.4.7--binary_op_log_imp.sv 4state fail +generated/binary_op/11.4.7--binary_op_log_imp.sv 2state pass +generated/binary_op/11.4.7--binary_op_log_imp.sv 4state pass generated/binary_op/11.4.7--binary_op_log_or.sv 2state pass generated/binary_op/11.4.7--binary_op_log_or.sv 4state pass generated/binary_op/11.4.8--binary_op_bit_and.sv 2state pass @@ -2459,14 +2459,14 @@ generated/logical_equivalence_operator_sim/11.4.7--logical_equiv_operator_2.sv 2 generated/logical_equivalence_operator_sim/11.4.7--logical_equiv_operator_2.sv 4state fail generated/logical_equivalence_operator_sim/11.4.7--logical_equiv_operator_3.sv 2state fail generated/logical_equivalence_operator_sim/11.4.7--logical_equiv_operator_3.sv 4state fail -generated/logical_implication_operator_sim/11.4.7--logical_impl_operator_0.sv 2state fail -generated/logical_implication_operator_sim/11.4.7--logical_impl_operator_0.sv 4state fail -generated/logical_implication_operator_sim/11.4.7--logical_impl_operator_1.sv 2state fail -generated/logical_implication_operator_sim/11.4.7--logical_impl_operator_1.sv 4state fail -generated/logical_implication_operator_sim/11.4.7--logical_impl_operator_2.sv 2state fail -generated/logical_implication_operator_sim/11.4.7--logical_impl_operator_2.sv 4state fail -generated/logical_implication_operator_sim/11.4.7--logical_impl_operator_3.sv 2state fail -generated/logical_implication_operator_sim/11.4.7--logical_impl_operator_3.sv 4state fail +generated/logical_implication_operator_sim/11.4.7--logical_impl_operator_0.sv 2state pass +generated/logical_implication_operator_sim/11.4.7--logical_impl_operator_0.sv 4state pass +generated/logical_implication_operator_sim/11.4.7--logical_impl_operator_1.sv 2state pass +generated/logical_implication_operator_sim/11.4.7--logical_impl_operator_1.sv 4state pass +generated/logical_implication_operator_sim/11.4.7--logical_impl_operator_2.sv 2state pass +generated/logical_implication_operator_sim/11.4.7--logical_impl_operator_2.sv 4state pass +generated/logical_implication_operator_sim/11.4.7--logical_impl_operator_3.sv 2state pass +generated/logical_implication_operator_sim/11.4.7--logical_impl_operator_3.sv 4state pass generated/logical_operators_sim/11.4.7--logical_operator_0.sv 2state pass generated/logical_operators_sim/11.4.7--logical_operator_0.sv 4state pass generated/logical_operators_sim/11.4.7--logical_operator_1.sv 2state pass @@ -2649,8 +2649,8 @@ generated/simple_logical_operators_sim/11.4.7--simple_and_logical_operator.sv 2s generated/simple_logical_operators_sim/11.4.7--simple_and_logical_operator.sv 4state pass generated/simple_logical_operators_sim/11.4.7--simple_equiv_logical_operator.sv 2state fail generated/simple_logical_operators_sim/11.4.7--simple_equiv_logical_operator.sv 4state fail -generated/simple_logical_operators_sim/11.4.7--simple_impl_logical_operator.sv 2state fail -generated/simple_logical_operators_sim/11.4.7--simple_impl_logical_operator.sv 4state fail +generated/simple_logical_operators_sim/11.4.7--simple_impl_logical_operator.sv 2state pass +generated/simple_logical_operators_sim/11.4.7--simple_impl_logical_operator.sv 4state pass generated/simple_logical_operators_sim/11.4.7--simple_or_logical_operator.sv 2state pass generated/simple_logical_operators_sim/11.4.7--simple_or_logical_operator.sv 4state pass generated/trig_functions/20.8--acos.sv 2state pass diff --git a/crates/celox-sv-analyzer/src/ast/constant_binding.rs b/crates/celox-sv-analyzer/src/ast/constant_binding.rs index bf5a52b71b2..ada0f98cb56 100644 --- a/crates/celox-sv-analyzer/src/ast/constant_binding.rs +++ b/crates/celox-sv-analyzer/src/ast/constant_binding.rs @@ -125,7 +125,7 @@ fn bind_in_context( operator, right, } => { - if constants::binary_op_from_text(operator).is_none() { + if operator != "->" && constants::binary_op_from_text(operator).is_none() { return Ok(None); } jobs.push(Job::Binary(operator.clone())); @@ -366,9 +366,21 @@ fn bind_in_context( Job::Binary(op) => { let right = values.pop().ok_or_else(disagreement)?; let left = values.pop().ok_or_else(disagreement)?; + let left = if op == "->" { + ConstExpr::Unary { + op: UnaryOp::LogicNot, + expr: Box::new(left), + } + } else { + left + }; values.push(ConstExpr::Binary { left: Box::new(left), - op: constants::binary_op_from_text(&op).ok_or_else(disagreement)?, + op: if op == "->" { + BinaryOp::LogicOr + } else { + constants::binary_op_from_text(&op).ok_or_else(disagreement)? + }, right: Box::new(right), }); } diff --git a/crates/celox-sv-analyzer/src/ast/expression_binding/direct.rs b/crates/celox-sv-analyzer/src/ast/expression_binding/direct.rs index c7416bb7102..cec78ae07db 100644 --- a/crates/celox-sv-analyzer/src/ast/expression_binding/direct.rs +++ b/crates/celox-sv-analyzer/src/ast/expression_binding/direct.rs @@ -960,14 +960,26 @@ pub(in crate::ast) fn bind_value( | ">=" | "&&" | "||" + | "->" ) { return Err(unsupported("operator on a real operand")); } + let implication = op == "->"; + let left = if implication { + Expr::Unary { + op: UnaryOp::LogicNot, + expr: Box::new(left), + } + } else { + left + }; let xnor = matches!(op.as_str(), "~^" | "^~"); let value = Expr::Binary { left: Box::new(left), - op: if xnor { + op: if implication { + BinaryOp::LogicOr + } else if xnor { BinaryOp::BitXor } else { constants::binary_op_from_text(&op) diff --git a/crates/celox-test-suite/src/sv/cases/operators.vtest b/crates/celox-test-suite/src/sv/cases/operators.vtest index 78a54d85cf2..5bc597caa33 100644 --- a/crates/celox-test-suite/src/sv/cases/operators.vtest +++ b/crates/celox-test-suite/src/sv/cases/operators.vtest @@ -1782,3 +1782,70 @@ endmodule (assert_eq hit 0) (assert_eq field_minus_one 0xffffffff) (assert_eq q 0xab)) + +(case logical_implication + ; IEEE 1800-2023 11.4.7: implication short-circuits for a false antecedent. + (source "logical_implication.sv" #" + module Top(input logic [3:0] a, input logic b, output logic y, + output logic hit, output logic constant_result); + localparam bit C = 2 -> 0; + function automatic logic mark(); + hit = 1; + return b; + endfunction + always_comb begin + hit = 0; + y = a -> mark(); + end + assign constant_result = C; + endmodule + "#) + (top Top) + (modify (set a 2) (set b 0)) + (assert_eq y 0) + (assert_eq hit 1) + (assert_eq constant_result 0) + (set a 0) + (assert_eq y 1) + (modify (set a 2) (set b 1)) + (assert_eq y 1)) + +(case logical_implication_unknown + (four_state) + (source "logical_implication_unknown.sv" #" + module Top(input logic a, input logic b, output logic y, output logic hit); + function automatic logic mark(); + hit = 1; + return b; + endfunction + always_comb begin + hit = 0; + y = a -> mark(); + end + endmodule + "#) + (top Top) + (set a (fourstate 1 1)) + (set b 0) + (assert_eq y (fourstate 1 1)) + (assert_eq hit 1) + (set b 1) + (assert_eq y 1) + (assert_eq hit 1)) + +(case logical_implication_short_circuit + (source "logical_implication_short_circuit.sv" #" + module Top(output logic hit, output logic y); + function automatic logic mark(); + hit = 1; + return 0; + endfunction + initial begin + hit = 0; + y = 1'b0 -> mark(); + end + endmodule + "#) + (top Top) + (assert_eq hit 0) + (assert_eq y 1)) diff --git a/crates/celox/tests/frontends/systemverilog/operators.rs b/crates/celox/tests/frontends/systemverilog/operators.rs index d19f42f5af4..ad4fcd8ce0f 100644 --- a/crates/celox/tests/frontends/systemverilog/operators.rs +++ b/crates/celox/tests/frontends/systemverilog/operators.rs @@ -1,4 +1,7 @@ sv_backends! { + fn logical_implication_short_circuit(sim) { @case "operators::logical_implication_short_circuit"; } + fn logical_implication_unknown(sim) { @case "operators::logical_implication_unknown"; } + fn logical_implication(sim) { @case "operators::logical_implication"; } fn casts_preserve_guarded_division_signedness(sim) { @case "operators::casts_preserve_guarded_division_signedness"; } From 2042895ab873f89be4d7059232f791c3d76e7300 Mon Sep 17 00:00:00 2001 From: tignear Date: Tue, 27 Oct 2026 04:11:46 +0900 Subject: [PATCH 2/2] perf(x86): validate wide values at mutation boundaries Debug ISel scanned the entire growing wide-value map after every instruction. Large process functions consequently repeated the same invariant checks quadratically. Validate the changed entry in set_wide_chunks, the only insertion path, and scan the complete map once before extracting it for phi construction. Retain all width and chunk-count invariants. Release behavior, HDL optimization levels and generated instruction selection are unchanged. Add a debug-only 128-value regression that counts invariant checks rather than asserting wall-clock time. Validation covers 620 x86 crate tests and 5,700 SystemVerilog/diagnostic tests (20 existing skips), affected clippy with warnings denied, formatting and diff checks. Full VCU108 and HTG9200 compiles still exceeded 300 seconds in the implementation snapshot: this removes a quadratic debug check but does not claim to resolve the board-level timeout or demonstrate an end-to-end speedup. Change-Id: Ief6ae9750c629821b5b52b0e8e7a30ae89b7e545 --- .../src/backend/native/isel.rs | 64 ++++++++++++------- .../src/backend/native/isel/tests/wide.rs | 41 ++++++++++++ 2 files changed, 81 insertions(+), 24 deletions(-) diff --git a/crates/celox-backend-x86/src/backend/native/isel.rs b/crates/celox-backend-x86/src/backend/native/isel.rs index e4da6840d2a..798f1615f56 100644 --- a/crates/celox-backend-x86/src/backend/native/isel.rs +++ b/crates/celox-backend-x86/src/backend/native/isel.rs @@ -1480,10 +1480,6 @@ pub fn lower_execution_unit_with_diagnostics( sparse_write_states.metadata_action(sir_block_id, inst_idx), ); } - if cfg!(debug_assertions) { - ctx.verify_wide_values(); - } - // Track known bit width for redundant mask elimination. let dst_reg = inst.defined_register(); if let Some(dr) = dst_reg { @@ -1563,6 +1559,12 @@ pub fn lower_execution_unit_with_diagnostics( // Extract mask_map for phi node construction (ctx borrows func fields) let saved_mask_map = std::mem::replace(&mut ctx.mask_map, RegMap::new(0)); + // Each mutation validates its own entry. Scan the whole map once at + // the boundary rather than once per instruction, which is quadratic in + // large functions with many wide values. + if cfg!(debug_assertions) { + ctx.verify_wide_values(); + } let saved_wide_regs = std::mem::take(&mut ctx.wide_regs); let saved_wide_masks = std::mem::take(&mut ctx.wide_masks); drop(ctx); // Release borrows on func @@ -1758,6 +1760,11 @@ type WideMaskMap = crate::HashMap>; /// Tracks known constant values for SIR registers (for constant folding in ISel). type ConstMap = crate::HashMap; +#[cfg(test)] +thread_local! { + static WIDE_VALUE_VERIFICATIONS: std::cell::Cell = const { std::cell::Cell::new(0) }; +} + struct ISelContext<'a> { vregs: &'a mut VRegAllocator, spill_descs: &'a mut Vec, @@ -2193,6 +2200,9 @@ impl<'a> ISelContext<'a> { if width <= 64 { self.wide_regs.remove(®); } else { + if cfg!(debug_assertions) { + self.verify_wide_value(®, &chunks); + } self.wide_regs.replace(reg, chunks); } } @@ -2244,28 +2254,34 @@ impl<'a> ISelContext<'a> { /// this map through a direct insertion. fn verify_wide_values(&self) { for (reg, chunks) in self.wide_regs.iter() { - let width = self.sir_width(reg); - assert!( - width > 64, - "narrow SIR register r{} has a wide native representation (width={width})", - reg.0 - ); - let expected_chunks = Self::num_chunks(width); + self.verify_wide_value(reg, chunks); + } + } + + fn verify_wide_value(&self, reg: &RegisterId, chunks: &[(VReg, usize)]) { + #[cfg(test)] + WIDE_VALUE_VERIFICATIONS.with(|count| count.set(count.get() + 1)); + let width = self.sir_width(reg); + assert!( + width > 64, + "narrow SIR register r{} has a wide native representation (width={width})", + reg.0 + ); + let expected_chunks = Self::num_chunks(width); + assert_eq!( + chunks.len(), + expected_chunks, + "wide SIR register r{} has {} chunks, expected {expected_chunks}", + reg.0, + chunks.len() + ); + for (index, (_, chunk_width)) in chunks.iter().enumerate() { + let expected_width = width.saturating_sub(index * 64).min(64); assert_eq!( - chunks.len(), - expected_chunks, - "wide SIR register r{} has {} chunks, expected {expected_chunks}", - reg.0, - chunks.len() + *chunk_width, expected_width, + "wide SIR register r{} chunk {index} has width {chunk_width}, expected {expected_width}", + reg.0 ); - for (index, (_, chunk_width)) in chunks.iter().enumerate() { - let expected_width = width.saturating_sub(index * 64).min(64); - assert_eq!( - *chunk_width, expected_width, - "wide SIR register r{} chunk {index} has width {chunk_width}, expected {expected_width}", - reg.0 - ); - } } } diff --git a/crates/celox-backend-x86/src/backend/native/isel/tests/wide.rs b/crates/celox-backend-x86/src/backend/native/isel/tests/wide.rs index 3f94d1bda99..e1b98ebefce 100644 --- a/crates/celox-backend-x86/src/backend/native/isel/tests/wide.rs +++ b/crates/celox-backend-x86/src/backend/native/isel/tests/wide.rs @@ -1463,3 +1463,44 @@ fn widened_zero_compares_equal_to_a_zero_wide_load() { assert_eq!(state[output_offset] & 1, 1, "four_state={four_state}"); } } + +#[cfg(debug_assertions)] +#[test] +fn wide_value_invariant_validation_scales_with_values() { + const COUNT: usize = 128; + let unit = ExecutionUnit { + entry_block_id: SirBlockId(0), + blocks: [( + SirBlockId(0), + BasicBlock { + id: SirBlockId(0), + params: vec![], + instructions: (0..COUNT) + .map(|i| { + SIRInstruction::Imm( + RegisterId(i), + SIRValue::new_four_state(BigUint::from(i), BigUint::default()), + ) + }) + .collect(), + terminator: SIRTerminator::Return, + }, + )] + .into_iter() + .collect(), + register_map: (0..COUNT) + .map(|i| (RegisterId(i), RegisterType::Logic { width: 65 })) + .collect(), + }; + WIDE_VALUE_VERIFICATIONS.with(|count| count.set(0)); + let _ = lower_execution_unit(&unit, &empty_layout(), false); + let checks = WIDE_VALUE_VERIFICATIONS.with(|count| count.get()); + assert!( + checks >= COUNT, + "the invariant must still be checked: {checks}" + ); + assert!( + checks <= 2 * COUNT, + "repeated whole-map validation: {checks}" + ); +}