Skip to content

fix(dialect-mir): fold shifts whose amount has a different width - #1314

Merged
nihalpasham merged 2 commits into
NVIDIA:mainfrom
midagedev:fix/const-fold-mixed-width-shift
Sep 23, 2026
Merged

nihalpasham merged 2 commits into
NVIDIA:mainfrom
midagedev:fix/const-fold-mixed-width-shift

Conversation

@midagedev

@midagedev midagedev commented Sep 21, 2026 •

Copy link
Copy Markdown
Contributor

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 << usize after loop unrolling.

let mut bits = 0u32;
let mut i = 0usize;
#[unroll]
while i < 4 {
    bits |= 1u32 << (2 * i);
    i += 1;
}
before: compiler panic — APInt::shl bitwidth mismatch (32 vs 64)
after:  compilation succeeds; bits == 85

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

  • Kept @midagedev's implementation and original regression cases.
  • Added exact result-type checks, correctly typed test operands, and coverage for 128-bit values, negative counts and counts with bits above 64.
  • Added a regression to unroll_smoke covering wider and narrower counts, left shifts, and arithmetic/logical right shifts.
  • Rebased onto current main and signed both commits, preserving contributor authorship and DCO credit.

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.
  • Independent review: 378,720 value/type checks, 40,590 invalid-count checks and 900 nonconstant controls pass across the supported Rust integer widths.
  • Both the original regression and added tests fail with the old folder. A deliberately broken 64-bit count read passes the original tests but fails the added 128-bit boundary test.
  • Eight arithmetic kernels, including 128-bit shifts, pass through LLVM and libNVVM on RTX 5090. Final unroll_smoke passes 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.
  • The existing unroll_bounds_check harness passes its valid-value and expected checked-panic cases.
  • All 51 non-CodeQL hosted checks passed; PR book deployment was intentionally skipped. CodeQL is allowed to finish separately by maintainer decision.
  • Current-main integration at 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.

midagedev and others added 2 commits September 23, 2026 11:15
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]>
@nihalpasham
nihalpasham force-pushed the fix/const-fold-mixed-width-shift branch from 2683c17 to b024b5f Compare September 23, 2026 05:59
@nihalpasham nihalpasham added bug Something isn't working codegen Device code-generation pipeline (Rust MIR to IR to PTX) tests Adds or improves test coverage examples Example kernels and host harnesses under crates/rustc-codegen-cuda/examples labels Sep 23, 2026

@nihalpasham nihalpasham left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 nihalpasham left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@nihalpasham
nihalpasham merged commit cc32f26 into NVIDIA:main Sep 23, 2026
55 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working codegen Device code-generation pipeline (Rust MIR to IR to PTX) examples Example kernels and host harnesses under crates/rustc-codegen-cuda/examples tests Adds or improves test coverage

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants