From 6a83f59c9ec9b14771473016afd95e9cf1741eef Mon Sep 17 00:00:00 2001 From: tignear Date: Wed, 21 Oct 2026 01:29:36 +0900 Subject: [PATCH] fix(native): ignore padding in wide reduction AND A two-state reduction AND of an all-ones vector such as 981 or 2051 bits currently returns zero because unused high bits of the last storage word are included. In both the x86-64 and AArch64 backends, fill that padding with ones before reducing and iterate over the declared source width. Add a shared regression that checks the all-ones value and clears each of the 981 declared bits individually. It passes native, Cranelift, Wasm and parallel variants on x86-64, plus Verilator and Icarus. Run its native and native-parallel variants in the ARM64 CI job as well. Add a direct AArch64 regression for widths 1, 63, 64, 65, 127, 128, 129, 981, 1024 and 2051, including widening the one-bit result. It generates AArch64 code on every host; on AArch64 Linux it executes each all-ones value and every single-cleared-bit value, then verifies restoration. Validation: 611 x86 native unit tests and 47 AArch64 backend tests on x86-64; focused shared SV regression; affected backend Clippy and formatting; AArch64-target test compilation using the installed Rust 1.99.1 toolchain; 16 CI workflow script tests and actionlint (ignoring its outdated runner-label catalogue for the existing ubuntu-26.04-arm label). Related wide-operator, four-state and parameterized-operation consumers also passed. AArch64 machine-code execution requires the ARM CI runner and was not run locally. Change-Id: I57339044d93504b32e95b254505a76153682d1f6 --- .github/workflows/ci.yml | 5 + crates/celox-backend-arm64/src/isel/tests.rs | 1 + .../src/isel/tests/reductions.rs | 120 ++++++++++++++++++ .../src/isel/wide_unary.rs | 22 +++- .../src/backend/native/isel/wide_unary.rs | 22 +++- .../src/sv/cases/operators.vtest | 16 +++ .../frontends/systemverilog/operators.rs | 6 + 7 files changed, 188 insertions(+), 4 deletions(-) create mode 100644 crates/celox-backend-arm64/src/isel/tests/reductions.rs diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index a7eba619c0d..e2eace94df0 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -673,6 +673,11 @@ jobs: - name: Test AArch64 facade integration run: cargo test --locked -p celox --lib --test native_exec + - name: Test partial-word reduction AND on AArch64 + run: >- + cargo test --locked -p celox --features systemverilog + --test systemverilog wide_reduction_and_respects_partial_storage_words::native + - name: Check x86-64 cross-codegen from AArch64 run: >- cargo check --locked -p celox -p celox-bench diff --git a/crates/celox-backend-arm64/src/isel/tests.rs b/crates/celox-backend-arm64/src/isel/tests.rs index a7aa3bd8706..403ea737368 100644 --- a/crates/celox-backend-arm64/src/isel/tests.rs +++ b/crates/celox-backend-arm64/src/isel/tests.rs @@ -1,4 +1,5 @@ mod partial_load; +mod reductions; mod shifts; mod slice; mod strided; diff --git a/crates/celox-backend-arm64/src/isel/tests/reductions.rs b/crates/celox-backend-arm64/src/isel/tests/reductions.rs new file mode 100644 index 00000000000..c5033ee86ad --- /dev/null +++ b/crates/celox-backend-arm64/src/isel/tests/reductions.rs @@ -0,0 +1,120 @@ +//! Compile on every host and execute the emitted code on AArch64. +use super::*; + +#[test] +fn wide_reduction_and_respects_source_width_and_partial_words() { + for width in [1usize, 63, 64, 65, 127, 128, 129, 981, 1024, 2051] { + // Also check that widening the one-bit result keeps the high bits clear. + for result_width in [1, 193] { + let input = AbsoluteAddr { + instance_id: InstanceId(0), + var_id: StateObjectId(0), + }; + let output = AbsoluteAddr { + var_id: StateObjectId(1), + ..input + }; + let address = + |absolute| RegionedAbsoluteAddr::from_absolute_addr(STABLE_REGION, absolute); + let loaded = RegisterId(0); + let reduced = RegisterId(1); + let result = RegisterId(2); + let unit = ExecutionUnit { + entry_block_id: SirBlockId(0), + blocks: [( + SirBlockId(0), + BasicBlock { + id: SirBlockId(0), + params: vec![], + instructions: vec![ + SIRInstruction::Load( + loaded, + address(input), + SIROffset::Static(0), + width, + ), + SIRInstruction::Unary(reduced, UnaryOp::And, loaded), + SIRInstruction::Unary(result, UnaryOp::Ident, reduced), + SIRInstruction::Store( + address(output), + SIROffset::Static(0), + result_width, + result, + vec![], + vec![], + ), + ], + terminator: SIRTerminator::Return, + }, + )] + .into_iter() + .collect(), + register_map: [(loaded, width), (reduced, 1), (result, result_width)] + .into_iter() + .map(|(register, width)| { + ( + register, + RegisterType::Bit { + width, + signed: false, + }, + ) + }) + .collect(), + }; + let output_offset = width.div_ceil(64) * 8; + let size = output_offset + result_width.div_ceil(64) * 8; + let mut layout = layout_for(input, width, false); + layout.offsets.insert(output, output_offset); + layout.widths.insert(output, result_width); + layout.is_4states.insert(output, false); + layout.total_size = size; + layout.working_base_offset = size; + layout.sparse_base_offset = size; + layout.sparse_active_bits_offset = size; + layout.merged_total_size = size; + layout.triggered_bits_offset = size; + layout.scratch_base_offset = size; + let image = crate::scalar::emit_prepared_eu( + &unit, + &layout, + false, + "reduction_and", + false, + None, + || false, + ) + .unwrap(); + assert!(!image.code.is_empty()); + + #[cfg(all(target_arch = "aarch64", target_os = "linux"))] + { + use num_bigint::BigUint; + let jit = crate::jit_mem::JitCode::new(&image.code).unwrap(); + let ones = (BigUint::from(1u8) << width) - 1u8; + let mut state = vec![0u8; size]; + let mut check = |value: &BigUint, expected: u8| { + state.fill(0); + let bytes = value.to_bytes_le(); + state[..bytes.len()].copy_from_slice(&bytes); + // Ensure the entire result, including widened high bits, is written. + state[output_offset..].fill(0xff); + assert_eq!(unsafe { jit.call(&mut state) }, 0); + let end = output_offset + result_width.div_ceil(8); + let actual = BigUint::from_bytes_le(&state[output_offset..end]) + & ((BigUint::from(1u8) << result_width) - 1u8); + assert_eq!( + actual, + BigUint::from(expected), + "width={width} result_width={result_width} input={value:x}" + ); + }; + check(&ones, 1); + for bit in 0..width { + check(&(&ones ^ (BigUint::from(1u8) << bit)), 0); + } + check(&ones, 1); + } + } + } +} diff --git a/crates/celox-backend-arm64/src/isel/wide_unary.rs b/crates/celox-backend-arm64/src/isel/wide_unary.rs index 4909d87454d..91cb349f357 100644 --- a/crates/celox-backend-arm64/src/isel/wide_unary.rs +++ b/crates/celox-backend-arm64/src/isel/wide_unary.rs @@ -170,8 +170,26 @@ pub(super) fn lower_wide_unary( value: u64::MAX, }); let mut acc = all_ones; - for i in 0..n_chunks { - let c = ctx.wide_chunk_or_zero(&src_chunks, i, block); + for i in 0..ISelContext::num_chunks(src_width) { + let mut c = ctx.wide_chunk_or_zero(&src_chunks, i, block); + let valid_bits = (src_width - i * 64).min(64); + if valid_bits < 64 { + // Padding outside the declared source width must not + // make an all-ones packed value reduce to zero. + let padding = u64::MAX << valid_bits; + let mask = ctx.alloc_vreg(SpillDesc::remat(padding)); + block.push(MInst::LoadImm { + dst: mask, + value: padding, + }); + let filled = ctx.alloc_vreg(SpillDesc::transient()); + block.push(MInst::Or { + dst: filled, + lhs: c, + rhs: mask, + }); + c = filled; + } let next = ctx.alloc_vreg(SpillDesc::transient()); block.push(MInst::And { dst: next, diff --git a/crates/celox-backend-x86/src/backend/native/isel/wide_unary.rs b/crates/celox-backend-x86/src/backend/native/isel/wide_unary.rs index 4909d87454d..91cb349f357 100644 --- a/crates/celox-backend-x86/src/backend/native/isel/wide_unary.rs +++ b/crates/celox-backend-x86/src/backend/native/isel/wide_unary.rs @@ -170,8 +170,26 @@ pub(super) fn lower_wide_unary( value: u64::MAX, }); let mut acc = all_ones; - for i in 0..n_chunks { - let c = ctx.wide_chunk_or_zero(&src_chunks, i, block); + for i in 0..ISelContext::num_chunks(src_width) { + let mut c = ctx.wide_chunk_or_zero(&src_chunks, i, block); + let valid_bits = (src_width - i * 64).min(64); + if valid_bits < 64 { + // Padding outside the declared source width must not + // make an all-ones packed value reduce to zero. + let padding = u64::MAX << valid_bits; + let mask = ctx.alloc_vreg(SpillDesc::remat(padding)); + block.push(MInst::LoadImm { + dst: mask, + value: padding, + }); + let filled = ctx.alloc_vreg(SpillDesc::transient()); + block.push(MInst::Or { + dst: filled, + lhs: c, + rhs: mask, + }); + c = filled; + } let next = ctx.alloc_vreg(SpillDesc::transient()); block.push(MInst::And { dst: next, diff --git a/crates/celox-test-suite/src/sv/cases/operators.vtest b/crates/celox-test-suite/src/sv/cases/operators.vtest index be1e0c90737..0cd6abf3297 100644 --- a/crates/celox-test-suite/src/sv/cases/operators.vtest +++ b/crates/celox-test-suite/src/sv/cases/operators.vtest @@ -1500,3 +1500,19 @@ endmodule (modify (set row 5) (set col 3)) (assert_eq selected 0x38) (assert_eq bit_value 1)) + +(case wide_reduction_and_respects_partial_storage_words + (source "partial_reduction.sv" #" + module Top(input logic [980:0] a, output logic all_set); + assign all_set = &a; + endmodule + "#) + (top Top) + (let ones (- (<< 1 981) 1)) + (modify (set a ones)) + (assert_eq all_set 1) + (for bit (range 0 981) + (modify (set a (^ ones (<< 1 bit)))) + (assert_eq all_set 0 "each declared bit contributes, including the partial last word")) + (modify (set a ones)) + (assert_eq all_set 1)) diff --git a/crates/celox/tests/frontends/systemverilog/operators.rs b/crates/celox/tests/frontends/systemverilog/operators.rs index 58533bc3e51..1927a0ffded 100644 --- a/crates/celox/tests/frontends/systemverilog/operators.rs +++ b/crates/celox/tests/frontends/systemverilog/operators.rs @@ -158,3 +158,9 @@ sv_backends! { @case "operators::wide_wildcard_comparisons_normalize_unknown_to_x"; } } + +sv_backends! { + fn wide_reduction_and_respects_partial_storage_words(sim) { + @case "operators::wide_reduction_and_respects_partial_storage_words"; + } +}