Repository navigation
fix(cuda-oxide): unroll libdevice loops like clang - #1732
Merged
Merged
Conversation
praveenperera
force-pushed
the
fix/libdevice-unroll-1705
branch
from
November 4, 2026 05:39
4c72f79 to
156c3a7
Compare
Small counted loops whose bodies call libdevice math (erff, expf) stayed rolled, so the arrays they fill by the loop counter went to .local: 32 B for a 4-step erff fill, 96-128 B in bias+GELU and layernorm kernels. nvcc and clang -O3 unroll the same loops. Two pipeline differences caused it. opt ran without -mcpu, and LLVM's NVPTX target only adds NVVMReflectPass to opt's pipeline when it has a CPU, so every inlined libdevice body kept both __CUDA_FTZ arms and looked twice its size to the unroller. And opt ran default<O2>, whose full-unroll threshold is half of O3's, although rustc builds device code at opt-level=3. Pass -mcpu to opt on both optimization routes and run O3, which matches clang -O3 on these kernels. With -mcpu, opt folds reflect itself, so it also gets the __CUDA_PREC_SQRT/__CUDA_PREC_DIV values llc uses; without them sqrt would fold to sqrt.approx.f32. Optimization::O2 becomes O3. Fixes NVIDIA#1705 Signed-off-by: Praveen Perera <[email protected]>
Add erf_fill_loop and erf_pair_loop: counted loops calling libm::erff that fill per-thread arrays by the loop counter, with no #[unroll]. verify-code-shape.sh asserts both use no local memory, and that f32::sqrt and f64::sqrt, which reach libdevice's __nv_sqrtf/__nv_sqrt, lower to sqrt.rn now that opt folds __CUDA_PREC_SQRT itself. Those are PTX checks, and the NVVM route taken when no llvm-link matches llc writes only the .ll and .target. The script always checks that the IR calls libdevice's erff and sqrt, and checks the PTX only when a current one exists, as function_item_call does, so that build still passes. smoketest.sh runs the script in compile-only CI, which takes the PTX route. Signed-off-by: Praveen Perera <[email protected]>
praveenperera
force-pushed
the
fix/libdevice-unroll-1705
branch
from
November 4, 2026 06:43
156c3a7 to
0e6244b
Compare
praveenperera
added a commit
to praveenperera/cuda-rust
that referenced
this pull request
Nov 4, 2026
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
Stacked on NVIDIA#1732, which changed the same three sentences from -O2 to
-O3: the local_memory_diagnostic module doc, the with_verbose doc and
the compiler-optimizations scalar-replacement sentence keep this
change's wording, which names no optimization level.
Signed-off-by: Praveen Perera <[email protected]>
praveenperera
added a commit
to praveenperera/cuda-rust
that referenced
this pull request
Nov 5, 2026
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
Stacked on NVIDIA#1732, which changed the same three sentences from -O2 to
-O3: the local_memory_diagnostic module doc, the with_verbose doc and
the compiler-optimizations scalar-replacement sentence keep this
change's wording, which names no optimization level.
Signed-off-by: Praveen Perera <[email protected]>
…ct flags Signed-off-by: nihalpasham <[email protected]>
Signed-off-by: nihalpasham <[email protected]>
Collaborator
|
Thanks, passing
At |
nihalpasham
approved these changes
Nov 9, 2026
Collaborator
|
/ok to test 708f9c6 |
@nihalpasham, there was an error processing your request: See the following link for more information: https://docs.gha-runners.nvidia.com/cpr/e/2/ |
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.
What This PR Does
Passes
-mcpu=<target_sm>and the libdevice precision reflect flags (__CUDA_PREC_SQRT=1,__CUDA_PREC_DIV=1) tooptincuda-oxide-codegensoNVVMReflectPassfolds__nvvm_reflectbranches before LLVM's loop unroller and inliner run:opt(crates/cuda-oxide-codegen/src/ptx.rs)erffLoop &sqrt.rnRegressions (examples/libm_math)1. Folding
__nvvm_reflectDuringopt -O2LLVM's
NVPTXTargetMachine::registerPassBuilderCallbacksregistersNVVMReflectPassinoptonly when-mcpu=<sm>is provided. Previously,optran without-mcpu, so every inlinedlibdevicebody (__nv_erff,__nv_expf,__nv_sqrtf) kept both its FTZ and non-FTZ branches untilllcran, making the loop body look roughly twice its real size toopt's loop unroller:Verification
RTX 5090,sm_120a):cargo oxide run libm_mathandverify-code-shape.shpass (0 B.localstack frame,sqrt.rn.f32/sqrt.rn.f64verified).cargo test -p cuda-oxide-codegen --all-targets: PASScargo clippy -p cuda-oxide-codegen --all-targets -- -D warnings: PASSCloses #1705