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";
}
}
2 changes: 1 addition & 1 deletion packages/playground/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"preview": "vite preview",
"test": "vitest run",
"typecheck": "node test/generate-sidecars.mjs && tsc -p .",
"test:vite-components": "vitest run test/vite-plugin-cache.test.ts test/generated-dts.test.ts test/vite-assertion-bytes.test.ts test/vite-plugin-cache-real.test.ts test/vite-plugin-watch.test.ts test/sidecar-orphans.test.ts test/sidecar-lock.test.ts && vitest run --config test/fixtures/injected-components/vitest.config.ts && node test/fixtures/injected-components/dependency-e2e.mjs && node test/fixtures/source-watch/watch-e2e.mjs && node test/fixtures/source-watch/build-watch-e2e.mjs && node test/fixtures/source-watch/component-watch-e2e.mjs",
"test:vite-components": "vitest run test/vite-plugin-cache.test.ts test/generated-dts.test.ts test/vite-assertion-bytes.test.ts test/vite-plugin-cache-real.test.ts test/vite-plugin-watch.test.ts test/sidecar-orphans.test.ts test/sidecar-lock.test.ts && node test/fixtures/injected-components/cache-e2e.mjs && node test/fixtures/injected-components/dependency-e2e.mjs && node test/fixtures/source-watch/watch-e2e.mjs && node test/fixtures/source-watch/build-watch-e2e.mjs && node test/fixtures/source-watch/component-watch-e2e.mjs",
"test:lsp-components": "pnpm run test:vite-components && node test/fixtures/injected-components/lsp-e2e.mjs",
"test:e2e": "pnpm run build && playwright test",
"test:watch": "vitest",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import assert from "node:assert/strict";
import { spawnSync } from "node:child_process";
import { mkdtempSync, rmSync } from "node:fs";
import { tmpdir } from "node:os";
import { dirname, join } from "node:path";
import { fileURLToPath } from "node:url";

const vitest = join(
dirname(fileURLToPath(import.meta.resolve("vitest/package.json"))),
"vitest.mjs",
);
for (const layout of ["internal", "external"]) {
const cache = mkdtempSync(join(tmpdir(), "celox-vitest-cache-"));
const client =
layout === "external"
? mkdtempSync(join(import.meta.dirname, ".cache-client-"))
: undefined;
try {
for (const phase of ["cold", "warm"]) {
const result = spawnSync(
process.execPath,
[
vitest,
"run",
"--config",
join(import.meta.dirname, "vitest.config.ts"),
"--fsModuleCachePath",
cache,
],
{
cwd: import.meta.dirname,
env: { ...process.env, CELOX_TEST_EXTERNAL_VITE_ROOT: client ?? "" },
encoding: "utf8",
timeout: 120_000,
},
);
process.stdout.write(result.stdout ?? "");
process.stderr.write(result.stderr ?? "");
assert.ifError(result.error);
assert.equal(
result.status,
0,
`${layout} ${phase} module-cache run failed`,
);
}
} finally {
rmSync(cache, { recursive: true, force: true });
if (client) rmSync(client, { recursive: true, force: true });
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import celox from "@celox-sim/vite-plugin";
import { resolve } from "node:path";
import type { Plugin } from "vite";
import celox from "@celox-sim/vite-plugin";
import { normalizePath, type Plugin } from "vite";
import { defineConfig } from "vitest/config";

const virtualTbStep: Plugin = {
Expand All @@ -14,7 +14,7 @@ const virtualTbStep: Plugin = {
};

export default defineConfig({
root: import.meta.dirname,
root: process.env.CELOX_TEST_EXTERNAL_VITE_ROOT || import.meta.dirname,
resolve: {
alias: {
"@fixture/tb-helper": resolve(
Expand All @@ -26,8 +26,11 @@ export default defineConfig({
plugins: [
virtualTbStep,
celox({
projectRoot: import.meta.dirname,
testbenchComponents: "./tb-components.ts",
}),
],
test: { include: ["entry.test.ts"] },
test: {
include: [normalizePath(resolve(import.meta.dirname, "entry.test.ts"))],
},
});
Loading
Loading