Skip to content

fix(backend): zero logical shifts by counts at or above the width - #1340

Merged
mergify[bot] merged 1 commit into
masterfrom
fix/logical-shift-count-saturation
Oct 16, 2026
Merged

mergify[bot] merged 1 commit into
masterfrom
fix/logical-shift-count-saturation

Conversation

@tignear

@tignear tignear commented Oct 16, 2026

Copy link
Copy Markdown
Contributor

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:

  • Cranelift, every width ≤ 64: 8-bit 1 << 8 gave 1, 8'hFF >> 9 gave 0x7F, and 64-bit 1 << 64 gave 1.
  • WASM, 64-bit: 1 << 64 gave 1. 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): adds emit_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 through all_backends! in crates/celox/tests/logical_shift_count.rs, with four cases:

    • two-state dynamic counts at widths 5/8/32/64 in both directions
    • single-bit wrap
    • a 72-bit count register
    • four-state

    The shared-case count in external_adapter.rs goes 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 -- --check and cargo 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:

  • The NAPI/JS tests.
  • The arm64 backend, which is unchanged and already correct.
  • The full native/interp celox suite. Those backends are unchanged and pass the new cases.

The branch was rebased onto master after validation; the only intervening commit is a pnpm update.

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.
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 16, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-16T09:03:50.917333Z 43572c1 PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@tignear tignear added the merge-ready Request merging through Mergify after required checks label Oct 16, 2026
@mergify mergify Bot added the queued Mergify queue entry label Oct 16, 2026
@mergify

mergify Bot commented Oct 16, 2026 •

Copy link
Copy Markdown
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
  • check-success = Conventional Commit title
  • check-success = Queue validation
  • any of [🛡 GitHub repository ruleset rule master]:
    • check-success = @github-actions/Lint & Format
    • check-neutral = @github-actions/Lint & Format
    • check-skipped = @github-actions/Lint & Format
  • any of [🛡 GitHub repository ruleset rule master]:
    • check-success = @github-actions/NAPI Build (Linux ARM64 GNU)
    • check-neutral = @github-actions/NAPI Build (Linux ARM64 GNU)
    • check-skipped = @github-actions/NAPI Build (Linux ARM64 GNU)
  • any of [🛡 GitHub repository ruleset rule master]:
    • check-success = @github-actions/NAPI Build (Windows)
    • check-neutral = @github-actions/NAPI Build (Windows)
    • check-skipped = @github-actions/NAPI Build (Windows)
  • any of [🛡 GitHub repository ruleset rule master]:
    • check-success = @github-actions/Rust Test & NAPI Build
    • check-neutral = @github-actions/Rust Test & NAPI Build
    • check-skipped = @github-actions/Rust Test & NAPI Build
  • any of [🛡 GitHub repository ruleset rule master]:
    • check-success = @github-actions/JS Test (Ubuntu)
    • check-neutral = @github-actions/JS Test (Ubuntu)
    • check-skipped = @github-actions/JS Test (Ubuntu)
  • any of [🛡 GitHub repository ruleset rule master]:
    • check-success = @github-actions/JS Test (Windows)
    • check-neutral = @github-actions/JS Test (Windows)
    • check-skipped = @github-actions/JS Test (Windows)
  • any of [🛡 GitHub repository ruleset rule master]:
    • check-success = @github-actions/Conventional Commit title
    • check-neutral = @github-actions/Conventional Commit title
    • check-skipped = @github-actions/Conventional Commit title

@mergify
mergify Bot merged commit 4c587c8 into master Oct 16, 2026
26 checks passed
@mergify
mergify Bot deleted the fix/logical-shift-count-saturation branch October 16, 2026 10:07
@mergify mergify Bot removed the queued Mergify queue entry label Oct 16, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

merge-ready Request merging through Mergify after required checks

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant