Skip to content

feat(cuda-oxide): warn on rolled-loop local arrays - #1737

Closed
praveenperera wants to merge 1 commit into
NVIDIA:mainfrom
praveenperera:feat/rolled-loop-local-warning
Closed

praveenperera wants to merge 1 commit into
NVIDIA:mainfrom
praveenperera:feat/rolled-loop-local-warning

Conversation

@praveenperera

Copy link
Copy Markdown
Contributor

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 --verbose build said so. #1705 proposes a warning under "Expected behavior"; this PR adds it.

This was split out of #1732, which now contains only the opt pipeline fix (-mcpu, the reflect values, and O3). The two PRs are independent: this one builds and passes on main by itself, and its help text and docs make no assumption about the opt level.

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:

    warning: local `v` (`[F32x4; 8]`, 128 bytes) in `softmax` could not be promoted to registers
      = note: indexed by the counter of a loop LLVM did not unroll, so the array stays in local memory
      = help: add `#[unroll]` to that loop so every index becomes a constant
    

    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:

    • every phi in its recurrence is a loop-header phi with a literal start;
    • a literal compare of the counter alone ends the loop, taking into account which branch target is the back edge;
    • it steps by a literal, or by the widened compare that Rust's range desugaring produces, which 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 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::diagnostics now reports the pipeline's warning: -prefixed messages at DiagnosticLevel::Warning instead of Note. Besides the new warning, this covers the existing "continuing with unoptimized IR" fallbacks.

  • Book. compiler-optimizations.md describes when the default warning fires.

Validation

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]>
@copy-pr-bot

copy-pr-bot Bot commented Nov 4, 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.

@nihalpasham nihalpasham added cuda-oxide SIMT programming model: rustc backend, cargo oxide, cuda-device, cuda-host, book enhancement New feature or request codegen Device code-generation pipeline (Rust MIR to IR to PTX) logs Logging, diagnostics, or verbose output declined PR intentionally not merged (closed without merging) labels Nov 9, 2026
@nihalpasham

nihalpasham commented Nov 9, 2026 •

Copy link
Copy Markdown
Collaborator

Thanks for putting this together. Closing this PR because we want to keep post-opt .ll local-memory diagnostics behind --verbose rather than running a textual LLVM IR CFG/recurrence parser on every build:

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 declined PR intentionally not merged (closed without merging) enhancement New feature or request logs Logging, diagnostics, or verbose output

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants