From 7e69561cfa427e027da7af31363df3f642d29a6d Mon Sep 17 00:00:00 2001 From: tignear Date: Tue, 27 Oct 2026 03:53:52 +0900 Subject: [PATCH 1/3] 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/3] 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}" + ); +} From cdb6a906af76f4abb306e8f62d6e823ce577c17a Mon Sep 17 00:00:00 2001 From: tignear Date: Tue, 27 Oct 2026 04:18:54 +0900 Subject: [PATCH 3/3] feat(sv): support logical equivalence expressions Evaluate both operands exactly once and preserve four-state truth values. Six generated sv-tests cases now pass in both state modes. Validation: full sv-tests, operator and analyzer tests, Icarus and Verilator focused cases, formatting and analyzer Clippy. Verilator 5.052 evaluates one operand twice; retain a reviewed exclusion with a standalone reproduction. Depends-On: celox-sim/celox#2201 Change-Id: I918a75f01dff54bb45806788937909ae12075edb --- conformance/sv-tests/README.md | 6 +++ conformance/sv-tests/expected.tsv | 24 ++++----- .../src/ast/constant_binding.rs | 18 +++++-- .../src/ast/expression_binding/direct.rs | 17 ++++++- .../src/sv/cases/operators.vtest | 51 +++++++++++++++++++ .../repros/sv_logical_equivalence_once.sv | 11 ++++ .../verification/sv/limitations.json | 17 +++++++ .../verification/sv/verilator/operators.json | 23 +++++++++ .../frontends/systemverilog/operators.rs | 3 ++ 9 files changed, 153 insertions(+), 17 deletions(-) create mode 100644 crates/celox-test-suite/verification/repros/sv_logical_equivalence_once.sv diff --git a/conformance/sv-tests/README.md b/conformance/sv-tests/README.md index 973febecc25..4a8ca5f110b 100644 --- a/conformance/sv-tests/README.md +++ b/conformance/sv-tests/README.md @@ -164,6 +164,12 @@ the initial 28 upstream checks passed. Arity and void-result rejection remain ch Queue slices, queue operands in concatenations, element-reference persistence, warning diagnostics, and queries on nonintegral pop results remain outside this change's support contract. +Logical equivalence (`<->`) 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 before the container +method changes above changed only these twelve verdicts, with no passing case +regressing: 1,208 cases pass in 2state and 1,219 in 4state. Shared tests cover unknown operands and +evaluate both operands exactly once (IEEE 1800-2023 11.4.7). Logical implication (`->`) now passes six generated cases in both state modes. The binary-operator case passes elaboration; the five logical-operator cases diff --git a/conformance/sv-tests/expected.tsv b/conformance/sv-tests/expected.tsv index 71a981ebf40..89501cb5d10 100644 --- a/conformance/sv-tests/expected.tsv +++ b/conformance/sv-tests/expected.tsv @@ -1817,8 +1817,8 @@ generated/binary_op/11.4.6--binary_op_wild_neq.sv 2state pass generated/binary_op/11.4.6--binary_op_wild_neq.sv 4state pass 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_equiv.sv 2state pass +generated/binary_op/11.4.7--binary_op_log_equiv.sv 4state pass 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 @@ -2451,14 +2451,14 @@ generated/keywords/5.6.2--keyword_xnor.sv 2state pass generated/keywords/5.6.2--keyword_xnor.sv 4state pass generated/keywords/5.6.2--keyword_xor.sv 2state pass generated/keywords/5.6.2--keyword_xor.sv 4state pass -generated/logical_equivalence_operator_sim/11.4.7--logical_equiv_operator_0.sv 2state fail -generated/logical_equivalence_operator_sim/11.4.7--logical_equiv_operator_0.sv 4state fail -generated/logical_equivalence_operator_sim/11.4.7--logical_equiv_operator_1.sv 2state fail -generated/logical_equivalence_operator_sim/11.4.7--logical_equiv_operator_1.sv 4state fail -generated/logical_equivalence_operator_sim/11.4.7--logical_equiv_operator_2.sv 2state fail -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_equivalence_operator_sim/11.4.7--logical_equiv_operator_0.sv 2state pass +generated/logical_equivalence_operator_sim/11.4.7--logical_equiv_operator_0.sv 4state pass +generated/logical_equivalence_operator_sim/11.4.7--logical_equiv_operator_1.sv 2state pass +generated/logical_equivalence_operator_sim/11.4.7--logical_equiv_operator_1.sv 4state pass +generated/logical_equivalence_operator_sim/11.4.7--logical_equiv_operator_2.sv 2state pass +generated/logical_equivalence_operator_sim/11.4.7--logical_equiv_operator_2.sv 4state pass +generated/logical_equivalence_operator_sim/11.4.7--logical_equiv_operator_3.sv 2state pass +generated/logical_equivalence_operator_sim/11.4.7--logical_equiv_operator_3.sv 4state pass 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 @@ -2647,8 +2647,8 @@ generated/sampled_functions/20.13--steady_gclk.sv 2state fail generated/sampled_functions/20.13--steady_gclk.sv 4state fail generated/simple_logical_operators_sim/11.4.7--simple_and_logical_operator.sv 2state pass 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_equiv_logical_operator.sv 2state pass +generated/simple_logical_operators_sim/11.4.7--simple_equiv_logical_operator.sv 4state pass 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 diff --git a/crates/celox-sv-analyzer/src/ast/constant_binding.rs b/crates/celox-sv-analyzer/src/ast/constant_binding.rs index ada0f98cb56..881680ca018 100644 --- a/crates/celox-sv-analyzer/src/ast/constant_binding.rs +++ b/crates/celox-sv-analyzer/src/ast/constant_binding.rs @@ -125,7 +125,9 @@ fn bind_in_context( operator, right, } => { - if operator != "->" && constants::binary_op_from_text(operator).is_none() { + if !matches!(operator.as_str(), "->" | "<->") + && constants::binary_op_from_text(operator).is_none() + { return Ok(None); } jobs.push(Job::Binary(operator.clone())); @@ -366,7 +368,7 @@ 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 == "->" { + let left = if op == "->" || op == "<->" { ConstExpr::Unary { op: UnaryOp::LogicNot, expr: Box::new(left), @@ -374,9 +376,19 @@ fn bind_in_context( } else { left }; + let right = if op == "<->" { + ConstExpr::Unary { + op: UnaryOp::LogicNot, + expr: Box::new(right), + } + } else { + right + }; values.push(ConstExpr::Binary { left: Box::new(left), - op: if op == "->" { + op: if op == "<->" { + BinaryOp::Eq + } else if op == "->" { BinaryOp::LogicOr } else { constants::binary_op_from_text(&op).ok_or_else(disagreement)? 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 cec78ae07db..919d962913b 100644 --- a/crates/celox-sv-analyzer/src/ast/expression_binding/direct.rs +++ b/crates/celox-sv-analyzer/src/ast/expression_binding/direct.rs @@ -961,12 +961,14 @@ pub(in crate::ast) fn bind_value( | "&&" | "||" | "->" + | "<->" ) { return Err(unsupported("operator on a real operand")); } let implication = op == "->"; - let left = if implication { + let equivalence = op == "<->"; + let left = if implication || equivalence { Expr::Unary { op: UnaryOp::LogicNot, expr: Box::new(left), @@ -974,10 +976,21 @@ pub(in crate::ast) fn bind_value( } else { left }; + // Each truth-value conversion evaluates its operand once (11.4.7). + let right = if equivalence { + Expr::Unary { + op: UnaryOp::LogicNot, + expr: Box::new(right), + } + } else { + right + }; let xnor = matches!(op.as_str(), "~^" | "^~"); let value = Expr::Binary { left: Box::new(left), - op: if implication { + op: if equivalence { + BinaryOp::Eq + } else if implication { BinaryOp::LogicOr } else if xnor { BinaryOp::BitXor diff --git a/crates/celox-test-suite/src/sv/cases/operators.vtest b/crates/celox-test-suite/src/sv/cases/operators.vtest index 5bc597caa33..fceded314db 100644 --- a/crates/celox-test-suite/src/sv/cases/operators.vtest +++ b/crates/celox-test-suite/src/sv/cases/operators.vtest @@ -1849,3 +1849,54 @@ endmodule (top Top) (assert_eq hit 0) (assert_eq y 1)) + +(case logical_equivalence + (source "logical_equivalence.sv" #" + module Top(input logic [3:0] a, b, output logic y, output logic c); + localparam bit C = 4 <-> 2; + assign y = a <-> b; + assign c = C; + endmodule + "#) + (top Top) + (modify (set a 0) (set b 0)) + (assert_eq y 1) + (assert_eq c 1) + (modify (set a 2) (set b 0)) + (assert_eq y 0) + (modify (set a 2) (set b 4)) + (assert_eq y 1) + (modify (set a 0) (set b 4)) + (assert_eq y 0)) + +(case logical_equivalence_unknown + (four_state) + (source "logical_equivalence_unknown.sv" #" + module Top(input logic [1:0] a, b, output logic y); + assign y = a <-> b; + endmodule + "#) + (top Top) + (modify (set a (fourstate 1 1)) (set b 0)) + (assert_eq y (fourstate 1 1)) + (set b 1) + (assert_eq y (fourstate 1 1)) + (set a (fourstate 3 1)) + (assert_eq y 1)) + +(case logical_equivalence_evaluates_operands_once + (source "logical_equivalence_calls.sv" #" + module Top(output logic [7:0] a_count, b_count, output logic y); + function automatic logic left(); a_count = a_count + 1; return 0; endfunction + function automatic logic right(); b_count = b_count + 1; return 1; endfunction + initial begin + a_count = 0; + b_count = 0; + y = left() <-> right(); + end + endmodule + "#) + (top Top) + (assert_eq a_count 1) + (assert_eq b_count 1) + (assert_eq y 0)) diff --git a/crates/celox-test-suite/verification/repros/sv_logical_equivalence_once.sv b/crates/celox-test-suite/verification/repros/sv_logical_equivalence_once.sv new file mode 100644 index 00000000000..eaf5c845486 --- /dev/null +++ b/crates/celox-test-suite/verification/repros/sv_logical_equivalence_once.sv @@ -0,0 +1,11 @@ +module top; + int a_count = 0, b_count = 0; + function automatic bit left(); a_count++; return 0; endfunction + function automatic bit right(); b_count++; return 1; endfunction + initial begin + bit y; + y = left() <-> right(); + $display("y=%0d a=%0d b=%0d", y, a_count, b_count); + $finish; + end +endmodule diff --git a/crates/celox-test-suite/verification/sv/limitations.json b/crates/celox-test-suite/verification/sv/limitations.json index 6c53e1025bc..2ba61af7882 100644 --- a/crates/celox-test-suite/verification/sv/limitations.json +++ b/crates/celox-test-suite/verification/sv/limitations.json @@ -1,4 +1,21 @@ [ + { + "id": "sv_verilator_logical_equivalence_side_effects", + "category": "simulator_disagreement", + "phase": "execute", + "observed_version": "Verilator 5.052", + "reason": "IEEE 1800-2023 11.4.7 requires each logical equivalence operand to be evaluated exactly once. Verilator 5.052 evaluates the left function twice in the standalone reproduction and emits SIDEEFFECT warnings; Icarus 13.0 evaluates both functions once.", + "upstream": [], + "evidence": [ + "verification/repros/sv_logical_equivalence_once.sv", + "src/sv/cases/operators.vtest" + ], + "cases": { + "verilator": [ + "operators::logical_equivalence_evaluates_operands_once" + ] + } + }, { "id": "sv_static_function_result_lifetime", "category": "simulator_disagreement", diff --git a/crates/celox-test-suite/verification/sv/verilator/operators.json b/crates/celox-test-suite/verification/sv/verilator/operators.json index 0a9e2636556..a71785fcc6f 100644 --- a/crates/celox-test-suite/verification/sv/verilator/operators.json +++ b/crates/celox-test-suite/verification/sv/verilator/operators.json @@ -44,6 +44,29 @@ "tag_reasons": {}, "tags": [] }, + { + "category": "Operators", + "detail": "IEEE 1800-2023 11.4.7 requires each logical equivalence operand to be evaluated exactly once. Verilator 5.052 evaluates the left function twice in the standalone reproduction and emits SIDEEFFECT warnings; Icarus 13.0 evaluates both functions once.", + "expectation": "Simulation", + "known_issue": { + "category": "simulator_disagreement", + "evidence": [ + "verification/repros/sv_logical_equivalence_once.sv", + "src/sv/cases/operators.vtest" + ], + "id": "sv_verilator_logical_equivalence_side_effects", + "observed_version": "Verilator 5.052", + "phase": "execute", + "reason": "IEEE 1800-2023 11.4.7 requires each logical equivalence operand to be evaluated exactly once. Verilator 5.052 evaluates the left function twice in the standalone reproduction and emits SIDEEFFECT warnings; Icarus 13.0 evaluates both functions once.", + "upstream": [] + }, + "name": "operators::logical_equivalence_evaluates_operands_once", + "phase": "skip", + "status": "ignored", + "stronger_than_sv": false, + "tag_reasons": {}, + "tags": [] + }, { "category": "Operators", "detail": "", diff --git a/crates/celox/tests/frontends/systemverilog/operators.rs b/crates/celox/tests/frontends/systemverilog/operators.rs index ad4fcd8ce0f..7be6789e3d7 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_equivalence(sim) { @case "operators::logical_equivalence"; } + fn logical_equivalence_unknown(sim) { @case "operators::logical_equivalence_unknown"; } + fn logical_equivalence_evaluates_operands_once(sim) { @case "operators::logical_equivalence_evaluates_operands_once"; } 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"; }