Repository navigation
feat(cuda-oxide): warn on rolled-loop local arrays - #1737
Closed
praveenperera wants to merge 1 commit into
Closed
praveenperera wants to merge 1 commit into
praveenperera wants to merge 1 commit into
Conversation
A small counted loop that fills an array by its counter, such as
`for e in 0..4 { r[e] = erff(a[e]) }`, keeps the array in registers
only if LLVM unrolls it. When the body is over the unroll threshold
the array silently moves to .local, and only a verbose build said so.
Report that case in every optimized build, with a help line pointing
at #[unroll]. A counter qualifies only when every phi of its
recurrence is a loop-header phi with a literal start, a literal
compare of it alone ends the loop (judged by which target is the back
edge), and it steps by a literal or by the widened compare of Rust's
range desugaring that the back edge itself requires. The local is
reported only when its address does not escape and every index into
it comes from such counters and literals through phis that pick by
iteration, not by a runtime branch. Anything unsure stays quiet.
Every other local-memory diagnostic remains verbose-only.
The analysis runs on every optimized build now, so it is linear in
the function: Tarjan SCCs for recurrences, one memoized operand walk,
one branch index, and one pointer and escape pass per function, all
with explicit stacks.
The standalone API now reports the pipeline's `warning: ` diagnostics,
this one included, as warnings instead of notes.
Refs NVIDIA#1705
Signed-off-by: Praveen Perera <[email protected]>
Collaborator
|
Thanks for putting this together. Closing this PR because we want to keep post-opt
|
Open
5 of 6 tasks
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.
Refs #1705
A small counted loop that fills an array by its counter, such as
for e in 0..4 { r[e] = erff(a[e]) }, keeps the array in registers only if LLVM unrolls it. When the body goes over LLVM's unroll threshold, the array moves to.local, and before this change only a--verbosebuild said so. #1705 proposes a warning under "Expected behavior"; this PR adds it.This was split out of #1732, which now contains only the
optpipeline fix (-mcpu, the reflect values, and O3). The two PRs are independent: this one builds and passes onmainby itself, and its help text and docs make no assumption about theoptlevel.What changes
A default-on warning for one case. In every optimized build, cuda-oxide reports a local that stays in local memory because a rolled loop with a constant trip count indexes it by its counter:
Every other local-memory diagnostic stays verbose-only.
Conservative matching. Because the warning appears in every build, the analysis stays quiet whenever it is unsure. A counter qualifies only when all of these hold:
The local is reported only when its address does not escape and every index into it comes from such counters and literals, through phis that choose by iteration and not by a runtime branch. Runtime trip counts, runtime offsets, runtime-chosen steps or indices, counters that can stall before the bound, exits that also wait on a runtime condition, and escaping locals do not warn.
Linear cost. The analysis now runs on every optimized build, so its cost is linear in function size. It finds recurrences with Tarjan SCCs, walks operands once with memoization, indexes branches once, and does one pointer and escape pass per function. Explicit stacks keep long, fully unrolled bodies from overflowing the stack.
Standalone API.
Compilation::diagnosticsnow reports the pipeline'swarning:-prefixed messages atDiagnosticLevel::Warninginstead ofNote. Besides the new warning, this covers the existing "continuing with unoptimized IR" fallbacks.Book.
compiler-optimizations.mddescribes when the default warning fires.Validation
cargo oxide fmt --check, plus clippy with-D warningsforcuda-oxide-codegenandrustc-codegen-cuda, all targets.cargo test -p cuda-oxide-codegen --all-targetsandrustc-codegen-cudacargo test --lib.tests/compile_to_ptx.rsfails onmain.optIR of 52 examples built with the fix(cuda-oxide): unroll libdevice loops like clang #1732 pipeline: no default warnings (11 verbose-only reports). Onmain'sopt -O2IR for the same examples, it warns only for the twoerffloops of thelibm_mathexample from fix(cuda-oxide): unroll libdevice loops like clang #1732 (r,lo,hi). Those are the arraysmainleaves in.local, and those loops are the motivating case in [BUG]: constant-trip loops calling libdevice math stay rolled and put their arrays in.local; nvcc and clang unroll them #1705.