Skip to content

feat(mir-transforms): fully unroll loops that index a small local array with their counter - #1841

Open
Code4me2 wants to merge 1 commit into
NVIDIA:mainfrom
Code4me2:feat/oxide-auto-unroll-local-arrays
Open

Code4me2 wants to merge 1 commit into
NVIDIA:mainfrom
Code4me2:feat/oxide-auto-unroll-local-arrays

Conversation

@Code4me2

Copy link
Copy Markdown
Contributor

Summary

Fixes #1815. A constant-trip loop that indexes a small local array with its counter, such as for n in 0..8 { acc[n] = mma(acc[n], ..) }, keeps the array in registers only if LLVM fully unrolls it. At opt -O2, it often doesn't, and the array moves to .local. On sm_121 this made the kernel in #1815 22× slower (240.7 ms vs 11.05 ms). This PR makes the MIR unroll pass unroll such loops itself, using the rule LLVM's AMDGPU backend applies to private arrays (NVPTX has no equivalent).

Changes

  • New mir-transforms/src/auto_unroll.rs. It plants a full-unroll hint in every innermost loop that has no #[unroll] or #[nounroll] and that takes mir.array_element_addr of a local mir.alloca of at most 512 bytes with an index computed in the loop.

  • unroll.rs. A new entry point, unroll_annotated_and_automatic_loops, plants these hints and then runs the existing unroller. A planted hint is applied only when:

    • the trip count is a constant of at most 64;
    • the index is computed from the counter and constants alone, so every copy gets a constant index;
    • the copies stay within 4,096 operations.

    Any other outcome is silent, because the user didn't ask for that unroll. Annotated loops behave as before, and unroll_annotated_loops is unchanged.

  • prep.rs. It calls the new entry point.

  • Opt-outs. #[nounroll] opts out a single loop, and CUDA_OXIDE_NO_AUTO_UNROLL=1 turns the rule off for a build.

This PR does not change any diagnostics. I had considered making the DynamicIndex local-memory diagnostic default-on, but left it out after the decision on #1737 to keep post-opt diagnostics behind --verbose.

Testing

  • PTX for every example on sm_80, sm_90 and sm_120: I built each example with the rule on and with it off (CUDA_OXIDE_NO_AUTO_UNROLL=1) and compared every kernel. The result was the same on all three targets:

    • 15 kernels change, out of 928, 986 and 989 respectively;
    • none gains local memory, and the total .local depot drops by 224 B. projected_destinations goes from 120 B to 24 B, and unannotated_range_accumulator from 128 B to 0;
    • total PTX grows by 0.1%;
    • examples that don't support a target fail to build the same way with the rule on or off.
  • Smoketest on sm_121: 256/257 pass, the same as main. The mma_mxf8f6f4 failure is pre-existing.

  • [BUG]: constant-trip loops stay rolled at opt -O2, silently demoting loop-indexed register arrays (e.g. mma accumulators) to .local #1815 kernel on sm_121 (GB10): 243.8 ms → 11.0 ms, the same as unrolling by hand. tiled_gemm and hashmap_v3 are unchanged.

  • [BUG]: constant-trip loops stay rolled at opt -O2, silently demoting loop-indexed register arrays (e.g. mma accumulators) to .local #1815 kernel on a discrete sm_120 GPU: 34.6 ms → 4.43 ms (7.8×). The hand-unrolled version takes 4.49 ms with the rule off and 4.44 ms with it on. The plain loop's local depot goes from 128 B to 0, and outputs are identical in both modes. Timings are the best of five 10-launch averages. unroll_smoke passes, and no smoketest example failed there.

  • New tests in mir-transforms/tests/auto_unroll.rs: a counter-indexed loop is unrolled without warnings. Each of these stays rolled without a warning:

    • a runtime trip count;
    • an index that never becomes constant (i ^ param);
    • an array over the size limit;
    • a trip count over the copy limit;
    • #[nounroll].

    The annotation-only entry point doesn't unroll anything automatically.

  • unroll_smoke: gains unannotated_range_accumulator, which verify-code-shape.sh requires to have no local memory. It fails on main and passes with this change. cargo oxide run unroll_smoke passes.

  • just -f cuda-oxide/Justfile check passes (the local mirror of CI: fmt, clippy, tests, guards, docs). I ran each step of the recipe by hand. All pass except the cargo deny steps and check-example-license-policy.sh, which need cargo-deny, not installed here. The PR adds no dependencies.

  • cargo oxide run <example> passes, or cuda-oxide/scripts/smoketest.sh -o '^<example>$'

  • New example added (if applicable): new kernel in unroll_smoke

Checklist

  • All commits signed off (git commit -s)
  • All commits cryptographically signed and shown as Verified on GitHub (git commit -S)
  • SPDX headers on new source files

…ay with their counter

A constant-trip loop such as `for n in 0..8 { acc[n] = mma(acc[n], ..) }`
keeps `acc` in memory unless LLVM's cost model unrolls the loop, and at opt
-O2 it often does not: the array becomes a .local stack slot (NVIDIA#1815; 22x
slower than the unrolled loop on sm_121).

auto_unroll plants a full-unroll hint in every innermost loop without
#[unroll] or #[nounroll] that takes mir.array_element_addr of a local
mir.alloca of at most 512 bytes with an index computed in the loop. After
normalizing the loop, the unroll pass unrolls it only when the trip count is
a constant of at most 64, the index is computed from the counter and
constants alone, and the copies stay within 4096 operations. Any other
outcome is silent, because nobody asked for the unroll. #[nounroll] opts a
loop out, and CUDA_OXIDE_NO_AUTO_UNROLL turns the rule off. This is the rule
LLVM's AMDGPU backend applies to private arrays; NVPTX has none.

unroll_smoke gains unannotated_range_accumulator, asserted by
verify-code-shape.sh to have no local memory: it fails on main and passes
with this change. On the smoketest's 991 kernels (sm_121), 6 change, none
gains local memory, and total PTX grows by 0.02%.

Refs NVIDIA#1815

Signed-off-by: Velvet Moon <[email protected]>
@copy-pr-bot

copy-pr-bot Bot commented Nov 20, 2026

Copy link
Copy Markdown

This pull request requires additional validation before any workflows can run on NVIDIA's runners.

Pull request vetters can view their responsibilities here.

Contributors can view more details about this message here.

@Code4me2

Copy link
Copy Markdown
Contributor Author

@elibol sry to ping you late on this but figured this may also be a helpful addition. It's a missed optimization, where idiomatic constant-trip loops over small arrays silently spilling to local memory. This makes them as fast as hand-unrolled code (7.8× on sm_120, ~21× on GB10) without changing other kernels.

@elibol

elibol commented Nov 20, 2026

Copy link
Copy Markdown
Collaborator

@nihalpasham do you mind reviewing when you get a chance?

@nihalpasham nihalpasham added cuda-oxide SIMT programming model: rustc backend, cargo oxide, cuda-device, cuda-host, book enhancement New feature or request perf Performance of generated code or of the compiler itself codegen Device code-generation pipeline (Rust MIR to IR to PTX) labels Nov 25, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

codegen Device code-generation pipeline (Rust MIR to IR to PTX) cuda-oxide SIMT programming model: rustc backend, cargo oxide, cuda-device, cuda-host, book enhancement New feature or request perf Performance of generated code or of the compiler itself

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG]: constant-trip loops stay rolled at opt -O2, silently demoting loop-indexed register arrays (e.g. mma accumulators) to .local

3 participants