Repository navigation
fix(sir-opt): keep the And width in the bit-extract peephole - #1887
Merged
Merged
Conversation
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
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 3 hours 30 minutes 50 seconds in the queue, including 2 hours 11 minutes 20 seconds running CI. Required conditions to merge
|
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.
What
BitExtractPeepholerewrote(load >> k) & maskas a mask-widthLoadand retyped the And's destination register to the mask width. The And's users still expected its original width. In #1854'smem[(lfsr >> 7) & 1023], the 10-bit register fed the 32-bit array-index range checks (r66 GeU r9, withr9 = 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.operators::masked_shift_fields_keep_expression_width, covering comparison, subtraction (field - 1wraps to0xffffffff) 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.keeps_shift_result_used_by_runtime_eventunit 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 liveShr.Validation
cargo test -p celox -p celox-sir-opt -p celox-test-suite --features celox/systemverilog: 14420 passed, 0 failedcargo clippy -p celox-sir-opt --all-targets -- -D warnings,cargo fmt --all -- --check: cleanverify-sv-verilator/verify-sv-icarus --filter operators::masked_shift_fields: passed--two-state:bit, plus thelogic10/16/22 versions): O1/O2 previously failed to compile; now they printBENCH_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.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:
b3's per-cycle cost is dominated by a whole-array commit (
Commitofmem'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