Repository navigation
Conversation
Teach induction to see through MirEq/MirNe of a relational comparison against a boolean constant—the common post-import shape of `for i in 0..N` after switchInt lowering—so #[unroll] can fully unroll those loops. Add fixtures and update attribute docs. Closes NVIDIA#557 Signed-off-by: Ayush7614 <[email protected]>
The eq-wrapped guard matcher this PR added is correct, but nothing in the pipeline produces that shape for range loops: rustc desugars 'for i in lo..hi' through Iterator::next into an Option discriminant test in a separate merge block, the importer emits bool switches as direct/not conditions, and the counter is fed back unchanged on the None arm. Verified by building the new range kernels with only the matcher in place: all four still warned 'no recognized induction variable' / 'early break or multiple exits' and stayed rolled. Fix at the layer issue NVIDIA#557's scope names first: the #[kernel]/#[device] macro already owns the loop AST (LoopUnrollAttrVisitor), so an annotated 'for pat in lo..hi' over an exclusive range is desugared into the counted while form the pass recognizes. Bounds evaluate once in order, the binding is a fresh copy per iteration, labels move to the while, and the increment precedes the body so continue still advances the counter. Inclusive ranges, step_by, and arbitrary iterators keep the marker-in-body path and its warning. The matcher stays as defense in depth for guards that genuinely arrive eq-wrapped (hand-written '(i < n) == true', foreign producers); its fixture is renamed eq_wrapped_guard_loop and its comments and the three doc claims now describe the real mechanism instead of a fictional post-import range-for shape. Validation on hardware (RTX 5090): unroll_smoke's four new range kernels (full_range, partial_range, full_range_continue, full_range_break) produce correct sums; full_range* emit zero backedges in the .ll, matching full_unroll's structure, and partial_range matches partial_unroll (main + remainder). The only remaining warnings are the two deliberately-rejected fixtures. 95 cuda-macros tests (4 new desugar pins) and all mir-transforms suites pass. Closes NVIDIA#557
- The loop's remaining attributes now ride the wrapper block instead of the generated while, so #[cfg(off)] strips the bound evaluations too (previously they still executed, and their lets drew unused warnings). - The user pattern binds a per-iteration item temp instead of the live counter, restoring 'for ref i in ...' (previously E0506 against an internal name) while keeping the increment ahead of the body. - The generated locals use Span::mixed_site() hygiene, so user variables spelled __cuda_oxide_unroll_* can neither shadow nor be captured by the desugar's bookkeeping. - A duplicate #[unroll] attribute on one loop is now a clear macro error (it previously either hit an opaque rustc error on while loops or silently planted two hints on desugared for loops). - New pins: parenthesized-range desugar, cfg-on-wrapper ordering, duplicate-attr rejection, and a mir-transforms fixture for the eq-wrapped FALSE guard with the body on the false edge (the flip path of the truth table had no coverage). Re-verified on hardware: unroll_smoke PASS, full_range* kernels at zero backedges, partial_range at the designed two.
|
A couple of things: before we talk about the implementation, I want to step back to motivation.
For background on why this PR doesn't get us there: rustc desugars
Right now we're going with option 3. Rust's for loops aren't simple counted loops, they're iterators, and treating them as anything else honestly requires option 2's machinery. The one narrow case where a rewrite is even conceivable is literal integer ranges like |
|
Thanks @nihalpasham — you’re right that the original eq-wrapped shape wasn’t what range-for produces in-pipeline; that was a mistaken assumption on my side. I don’t have a measured GPU perf win for iterator-for vs while; the goal was ergonomics / matching the advertised #[unroll] story. Happy to close this and leave docs-only for now, or keep your macro desugar commits if the project wants that path. Prefer your call. |
|
Thanks for confirming, closing this one. We'll stay docs-only for now; |
Summary
MirEq/MirNewrapping</<=/>/>=vs a bool constant (post-switchIntrange-forshape).#[unroll]removes the loop.#[unroll]docs in mir-transforms / cuda-device / cuda-macros.step_by, inclusive ranges, custom iterators, Option-discriminant-only headers without a recoverable relational cmp.Closes #557
Test plan
cargo test -p mir-transforms analyzes_range_for_eq_wrapped_guardcargo test -p mir-transforms full_unroll_range_for_eq_wrapped_removes_the_loopcargo test -p mir-transforms(full suite, 30 passed)