Repository navigation
Conversation
…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]>
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. |
Collaborator
|
@nihalpasham do you mind reviewing when you get a chance? |
This branch has not been deployed
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.
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. Atopt -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 takesmir.array_element_addrof a localmir.allocaof 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:Any other outcome is silent, because the user didn't ask for that unroll. Annotated loops behave as before, and
unroll_annotated_loopsis unchanged.prep.rs. It calls the new entry point.Opt-outs.
#[nounroll]opts out a single loop, andCUDA_OXIDE_NO_AUTO_UNROLL=1turns the rule off for a build.This PR does not change any diagnostics. I had considered making the
DynamicIndexlocal-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:.localdepot drops by 224 B.projected_destinationsgoes from 120 B to 24 B, andunannotated_range_accumulatorfrom 128 B to 0;Smoketest on sm_121: 256/257 pass, the same as
main. Themma_mxf8f6f4failure 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_gemmandhashmap_v3are 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_smokepasses, 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:i ^ param);#[nounroll].The annotation-only entry point doesn't unroll anything automatically.
unroll_smoke: gainsunannotated_range_accumulator, whichverify-code-shape.shrequires to have no local memory. It fails onmainand passes with this change.cargo oxide run unroll_smokepasses.just -f cuda-oxide/Justfile checkpasses (the local mirror of CI: fmt, clippy, tests, guards, docs). I ran each step of the recipe by hand. All pass except thecargo denysteps andcheck-example-license-policy.sh, which needcargo-deny, not installed here. The PR adds no dependencies.cargo oxide run <example>passes, orcuda-oxide/scripts/smoketest.sh -o '^<example>$'New example added (if applicable): new kernel in
unroll_smokeChecklist
git commit -s)git commit -S)