Repository navigation
fix(backend): zero logical shifts by counts at or above the width - #1340
Merged
Merged
Conversation
The Cranelift and WASM backends lowered dynamic `<<` and `>>` to machine shifts, which take the count modulo the register width. A count at or above the operand width therefore wrapped (8-bit `1 << 8` gave 1) instead of producing 0. Select zero for out-of-range counts in both the value and the four-state mask planes, matching `>>>`, the native backends, and the interpreter.
Codex Review SummaryThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
Contributor
Merge Queue Status
This pull request spent 34 minutes 49 seconds in the queue, including 22 minutes 56 seconds running CI. Required conditions to merge
|
52 of 94 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
The Cranelift and WASM backends lowered dynamic
<</>>straight to machine shifts, which take the count modulo the register width. A shift count at or above the operand width therefore wrapped instead of producing 0:1 << 8gave1,8'hFF >> 9gave0x7F, and 64-bit1 << 64gave1.1 << 64gave1. Narrower widths were only correct by accident of the post-shift mask.The native x86/arm64 backends and the SIR interpreter already return 0 (
legalize_variable_shift_counts), and>>>was already clamped in both affected backends.Changes:
Cranelift (
translator/arith.rs): selects 0 when the count is not below the physical width. It applies to the value plane and the four-state mask plane, and an X/Z count still makes the result unknown.WASM (
lib.rs): addsemit_logical_shift_saturation(zero when count ≥ 64), used for the value and mask shifts.Shared case group
logical_shift_count.vtest, run on every backend throughall_backends!incrates/celox/tests/logical_shift_count.rs, with four cases:The shared-case count in
external_adapter.rsgoes from 745 to 749.With the backend change reverted, all 12 Cranelift/WASM variants of the new cases fail and the other backends pass.
Validation
cargo test --locked -p celox --features systemverilog --test logical_shift_count: 32 passed.cargo test --locked -p celox --features systemverilog --no-fail-fast -- cranelift wasm: 4318 passed, 0 failed, 60 ignored. This is the whole shared suite through Cranelift and WASM, including the parallel variants.cargo test --locked -p celox-backend-cranelift -p celox-backend-wasm -p celox-test-suite: passed.cargo fmt --all -- --checkandcargo clippy --locked -p celox-backend-cranelift -p celox-backend-wasm -p celox -p celox-test-suite --all-targets --features celox/systemverilog --no-deps -- -D warnings: clean.verify-verilator --filter logical_shift_count::: 3 passed. The four-state case is unsupported on two-state Verilator.verify-icarus --filter logical_shift_count::: 4 passed.Not run:
celoxsuite. Those backends are unchanged and pass the new cases.The branch was rebased onto
masterafter validation; the only intervening commit is a pnpm update.