Skip to content

fix(sir-opt): keep the And width in the bit-extract peephole - #1887

Merged
mergify[bot] merged 1 commit into
masterfrom
feature/fix/1854-native-inline-width
Oct 24, 2026
Merged

mergify[bot] merged 1 commit into
masterfrom
feature/fix/1854-native-inline-width

Conversation

@tignear

@tignear tignear commented Oct 24, 2026

Copy link
Copy Markdown
Contributor

What

BitExtractPeephole rewrote (load >> k) & mask as a mask-width Load and retyped the And's destination register to the mask width. The And's users still expected its original width. In #1854's mem[(lfsr >> 7) & 1023], the 10-bit register fed the 32-bit array-index range checks (r66 GeU r9, with r9 = 32'h3ff). The same thing happens with plain ((a >> 4) & 15) == 32'd9.

The pass verifier only runs per pass with verify_passes, so in release builds the ill-typed SIR got past the per-EU pipeline. Cranelift tolerated it. The native backend's merged-chain verifier rejected it, and the error was reported after single-predecessor inlining even though inlining didn't cause it.

Fix

Keep the And destination's type. If the mask is narrower than the result, load the field into a fresh register and zero-extend it with Concat([0, field]). If the mask is wider, load only the result width, which matches the And's truncation. When the widths are equal, the output is unchanged.

Tests

  • pass_bit_extract_peephole::tests::zero_extends_field_narrower_than_and_result: unit test that checks the result width and the zero-extension. It fails before the fix.
  • Shared SV case operators::masked_shift_fields_keep_expression_width, covering comparison, subtraction (field - 1 wraps to 0xffffffff) and array indexing with a masked shift field. It fails before the fix on all 5 backend variants (SIR verify panic) and passes after. Verilator and Icarus agree with it.
  • The existing keeps_shift_result_used_by_runtime_event unit test relied on the old retyping (an 8-bit And rewritten as a 2-bit load). Its register now has the mask width, so it still checks its actual subject, the live Shr.

Validation

  • cargo test -p celox -p celox-sir-opt -p celox-test-suite --features celox/systemverilog: 14420 passed, 0 failed
  • cargo clippy -p celox-sir-opt --all-targets -- -D warnings, cargo fmt --all -- --check: clean
  • verify-sv-verilator / verify-sv-icarus --filter operators::masked_shift_fields: passed
  • xezim benchmarks, native backend, --two-state:
    • b3_mem_sweep 10/12/14/16/22 (bit, plus the logic 10/16/22 versions): O1/O2 previously failed to compile; now they print BENCH_DONE cycles=100000 checksum=0, the same as O0, xezim and Icarus. b3_22 was checked at full length at O2, and via the 2-cycle variant at O0/O1/O2.
    • B2/B2b (bit, logic): O0/O1/O2 unchanged before and after (checksum=136 / 0, the same as xezim).

Performance

callgrind instruction counts, simulation only (100k-cycle run minus the 2-cycle run), base and fix binaries built from the same incremental state:

workload base fix
B2 native O1 4599.1M 4593.9M (noise; identical SIR, the new path never fires)
B2 Cranelift O1 5966.1M 5966.1M
b3_10 Cranelift O1 1128.2M 1128.1M
b3_10 native O1 compile error 1009.9M (O0: 1009.6M)

b3's per-cycle cost is dominated by a whole-array commit (Commit of mem's full 32768 bits per cycle at 2^10 entries, 16 MiB at 2^22). That is why b3_22 takes about 20 minutes. It is unrelated to this fix and left for a separate issue.

Fixes #1854

BitExtractPeephole rewrote `(load >> k) & mask` as a mask-width load and
retyped the And's destination to the mask width. Its users still expected
the And's own width, e.g. `((a >> 4) & 15) == 32'd9` compared a 4-bit
register with a 32-bit one. Cranelift tolerated the ill-typed SIR; the
native merged-chain verifier rejected it at O1/O2 (#1854).

Keep the destination type: load the field into a fresh register and
zero-extend it with a Concat when the mask is narrower than the result,
and load only the result width when the mask is wider.

Fixes #1854

Change-Id: Icc91ce47ea0358c2f3eb597eaf151257a7c536d7
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 24, 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-24T09:28:41.823612Z 18517ce 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 24, 2026
@mergify mergify Bot added the queued Mergify queue entry label Oct 24, 2026
@mergify

mergify Bot commented Oct 24, 2026 •

Copy link
Copy Markdown
Contributor

Merge Queue Status

This pull request spent 3 hours 30 minutes 50 seconds in the queue, including 2 hours 11 minutes 20 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 9990077 into master Oct 24, 2026
30 checks passed
@mergify
mergify Bot deleted the feature/fix/1854-native-inline-width branch October 24, 2026 15:24
@mergify mergify Bot removed the queued Mergify queue entry label Oct 24, 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.

native: O1 jump inlining fails SIR verify on a dynamic-index memory design

1 participant