Repository navigation
fix(dialect-mir): fold shifts whose amount has a different width - #1314
Conversation
Rust allows the shift amount to have any integer type (u32 << usize) and convert_shift already widens or narrows it during lowering. The constant folder passed both operands straight to APInt::shl/lshr/ashr, which assert equal bit widths, so folding such a shift panicked the compiler with 'APInt::shl: bitwidth mismatch (32 vs 64)'. #[unroll] makes this easy to hit: it materialises a usize loop counter as a 64-bit constant and runs SCCP, which folds 'x_u32 << i'. Re-express the amount at the shifted value's width before shifting. The range check still reads the amount at its own width, so a wide amount such as 1 << 32 is refused rather than truncated to 0. Signed-off-by: midagedev <[email protected]>
Signed-off-by: nihalp <[email protected]>
2683c17 to
b024b5f
Compare
nihalpasham
left a comment
There was a problem hiding this comment.
Thanks, @midagedev — this fixes a real compiler crash in unrolled u32 << usize code.
I kept your implementation and added 128-bit and invalid-count checks, exact result-type assertions, and a mixed-width unroll_smoke regression. The added tests catch a truncated-count bug that the original cases miss; both regressions fail with the old folder.
At b024b5fd, just check passes all 5,426 tests/doctests. Independent boundary checks pass, and the GPU regression passes through LLVM and libNVVM with zero memcheck errors. I found no introduced correctness or soundness issue for supported Rust integers. Hosted workflows await approval to run; final CodeQL monitoring remains with the maintainer.
No linked issue is closed.
nihalpasham
left a comment
There was a problem hiding this comment.
All 51 non-CodeQL checks passed at b024b5fd. I also checked the clean integration with current main (ecbf6335, Pliron 0.18): 1,885 affected compiler tests and the GPU regression pass, with zero memcheck errors.
Merging with the test follow-ups above. CodeQL can finish separately as agreed. No linked issue is closed.
What this fixes
Constant folding now accepts shifts whose count has a different integer width from the shifted value. This fixes a compiler crash in ordinary Rust code such as
u32 << usizeafter loop unrolling.The folder checks the complete count before converting it to the value's width. Large counts therefore cannot lose their high bits and accidentally become valid shifts. Right-shift signedness and result types still come from the shifted value.
What we added
unroll_smokecovering wider and narrower counts, left shifts, and arithmetic/logical right shifts.Verification
Checked at
b024b5fd:just check: 5,426 tests/doctests pass, along with formatting, strict Clippy, guards, generated-intrinsic checks and warning-denied docs. Strict Clippy also passes for the changed example.unroll_smokepasses memcheck on both GPUs through LLVM and on GPU 0 through libNVVM, with zero errors. LLVM/PTX output was inspected and assembled with CUDA 13.3.unroll_bounds_checkharness passes its valid-value and expected checked-panic cases.ecbf6335(Pliron 0.18) passes 1,885 affected compiler tests and the GPU regression with zero memcheck errors.Nonconstant and out-of-range shifts retain their existing handling. No linked issue is closed.
The contributor disclosed AI assistance in reducing the reproducer and drafting the original patch.