Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions crates/celox-backend-arm64/src/isel/tests.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
mod partial_load;
mod reductions;
mod shifts;
mod slice;
mod strided;
Expand Down
120 changes: 120 additions & 0 deletions crates/celox-backend-arm64/src/isel/tests/reductions.rs
Original file line number Diff line number Diff line change
@@ -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);
}
}
}
}
22 changes: 20 additions & 2 deletions crates/celox-backend-arm64/src/isel/wide_unary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
22 changes: 20 additions & 2 deletions crates/celox-backend-x86/src/backend/native/isel/wide_unary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
16 changes: 16 additions & 0 deletions crates/celox-test-suite/src/sv/cases/operators.vtest
Original file line number Diff line number Diff line change
Expand Up @@ -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))
6 changes: 6 additions & 0 deletions crates/celox/tests/frontends/systemverilog/operators.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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";
}
}
Loading