Skip to content

Restore ptxas SASS loop unrolling under LLVM 23 (disable llc's late branch folding); assemble every compile-only PTX in CI - #1205

Merged
nihalpasham merged 5 commits into
mainfrom
fix/llc23-branch-layout
Aug 30, 2026
Merged

nihalpasham merged 5 commits into
mainfrom
fix/llc23-branch-layout

Conversation

@nihalpasham

Copy link
Copy Markdown
Collaborator

The bug

After the LLVM 23 bump, gemm_views' naive raw kernel lost 26% (7218 → 5708 GFLOPS) while its safe twin stayed put. The PTX math was identical — only the jump layout of the loop changed:

llc-22 (fast)                          llc-23 (slow)
$LOOP:                                 $LOOP:
   ...same loads and FMAs...              ...same loads and FMAs...
   setp.eq  %p, %r3, %r26;                setp.eq  %p, %r3, %r26;
   @%p  bra $EXIT;    ← if done, leave    @!%p bra $LOOP;  ← if NOT done,
   bra.uni  $LOOP;    ← jump back                            jump back;
$EXIT:                                                       exit falls
                                                             through

LLVM 23's NVPTX backend learned to reverse branch conditions (llvm/llvm-project #191889, follow-up #191890), letting branch folding + block placement collapse the two-jump idiom. Legal, one instruction smaller — but ptxas's SASS unroller recognizes hot loops by that layout and stops unrolling the folded form:

what ptxas produced          llc-22 layout    llc-23 layout
  registers                      38               26
  FMAs in the loop               18                4
  loads in flight                35                7    ← the 26%

Bisected by hand-editing PTX (live-benched, bit-identical numerics): ptxas requires the old orientation at two sites — the loop guard's taken-branch entering the loop, and the latch's taken-branch exiting with bra.uni continue. Negated predicates themselves are innocent.

The fix

Two ancient, generic, disable-only llc flags on every llc invocation:

-disable-branch-fold -disable-block-placement
    accepted identically by llc-21 / 22 / 23
    only TURN OFF passes → no new behavior to be wrong
    cost: PTX +2.6% lines suite-wide (redundant jumps that
    ptxas discards when laying out SASS itself)

Result, live on an RTX 5090: naive raw back to 7201 GFLOPS (README table refreshed; all four kernels match the pre-bump numbers again). Upstream has no scoped opt-out (the enabling PR shipped with a single LGTM and no discussion of ptxas loop-shape sensitivity); an LLVM issue and an internal NVBug for the ptxas side are queued separately.

Is it safe for everything else?

  • Correctness: full GPU smoketest 221/221 — every example executes and self-checks numerically with the flags on.
  • Codegen sweep: 890 kernels across 197 modules, per-kernel ptxas -v registers compared with/without the flags. 45 kernels move, every one explained:
class                                    examples                         meaning
the unroller waking back up              table-lookup scan loops now      the intended
(registers AND SASS body grow)           fully unroll (SASS 64→~400),     effect
                                         array_index, split_at_mut, ...
exact return to pre-bump registers       dpx_minmax_chains (10 kernels    restoration
                                         20→18 = old baseline), others
tiny scheduling deltas, SASS size flat   hashmap_v3 (48→46),              benign
                                         set_discriminant_niche (38→39)

Zero unexplained movers, zero below pre-bump baselines.

  • Benched where benches exist: gemm_views restored; the warp-reduction family measured at ±0.3% (noise) at example scale and marginally faster at compute-bound scale.
  • Honest asterisk: a few table-lookup micro-examples now unroll beyond their pre-bump shape (regs → 40). Numerically verified on hardware; no perf gate exists for them; expected faster (unrolled scans), not benchmarked.

Also in this PR: ptxas gate for every example in CI

The compile-only smoketest (CI's mode) now assembles every example's PTX with ptxas, not just the GPU-gated categories — closing the remaining "llc emits garbage silently" window for all 221 examples. Measured cost: 183 files assembled in +4.5s, ~3% of the compile-only run. Normal-mode verdicts keep their scope: on a GPU box, execution supersedes assembly.

Separate finding, measured and closed: the warp-reduction family

LLVM 23 unrolls the warp-shuffle reduction loops less at the IR level (PTX 97→60 lines, before ptxas). Benched properly: immaterial — ±0.3% at example geometry, 0.06-0.14% faster at 25M-thread scale, bitwise-identical outputs. Documented, deliberately not "fixed": chasing the old unrolled shape would spend registers for nothing.

Validation

cargo test --workspace          5307 passed, 0 failed
full GPU smoketest              221/221 (321s)
compile-only + wide gate        221/221 (141s, gate cost +4.5s)
register sweep                  890 kernels, all movers accounted for
gemm_views live bench           naive raw 5708 → 7201 GFLOPS
fmt / clippy / lock + contract  clean

LLVM 23's NVPTX backend gained reverseBranchCondition (llvm/llvm-project
PR #191889), letting BranchFolding and MachineBlockPlacement collapse the
"cond-bra + bra.uni" loop idiom into single negated conditionals with
fallthrough. ptxas's SASS unroller keys on the original orientation at
the loop guard and latch, so the folded form silently turns off SASS
unrolling: gemm_views' sgemm_naive_raw dropped 38 -> 26 registers and
FFMA 18 -> 4, costing 26% live throughput (7218 -> 5708 GFLOPS).

Append -disable-branch-fold and -disable-block-placement to every llc
invocation. Both are generic disable-only cl::opts accepted by llc 21+,
so there is no correctness surface. Measured: naive_raw back to 38
registers and 7226 GFLOPS live, zero register movement on 16 other
kernels across 6 examples, PTX +1-6% lines (redundant jumps ptxas
discards), bit-identical numerics throughout.

The llc argument head moves into base_llc_args so the unit tests assert
the argument list verbatim, next to the existing optimization_args tests.
… ones

The compile-only ptxas gate previously ran only for the tcgen05, wgmma,
and blackwell-mma categories. Every other example's verdict accepted "a
.ptx exists", which misses PTX that llc emits but ptxas rejects (e.g.
.shared symbols in .global initializers, which only fail at driver JIT
load). Nothing executes in compile-only mode, so assembly is the
strongest check available: run ptxas_verify on every emitted .ptx.

Normal-mode verdicts keep their current scope, since real execution
supersedes assembly there. The missing/too-old-ptxas noted-skip
semantics are unchanged. Assembling all ~200 PTX files costs about 4.5
seconds total.
Measured live via the example itself (1024^3, average of 5 runs) with
llc's branch folding disabled pipeline-wide:

  naive views  0.300 ms / 7159 -> 0.298 ms / 7201 GFLOPS
  naive raw    0.300 ms / 7161 -> 0.298 ms / 7201 GFLOPS
  tiled views  0.232 ms / 9272 -> 0.229 ms / 9370 GFLOPS
  tiled raw    0.231 ms / 9286 -> 0.230 ms / 9337 GFLOPS

Also note the dependence on the pipeline-wide flags, pointing at the
DISABLE_BRANCH_FOLD rustdoc in crates/cuda-oxide-codegen/src/ptx.rs.
@nihalpasham nihalpasham added bug Something isn't working build-related Build scripts, toolchain discovery, or compilation setup perf Performance of generated code or of the compiler itself codegen Device code-generation pipeline (Rust MIR to IR to PTX) labels Aug 30, 2026
The all-examples ptxas gate caught a pre-existing latent bug on its
first CI run: llc-22's DWARF emission writes label-difference
expressions into debug sections (.debug_pubnames end-start), a PTX ISA
7.5 feature, while declaring the target's default .version (7.0 at
sm_80). ptxas rejects the module; nothing ever assembled full-debug PTX
before this gate (GPU boxes target sm_120a where the default .version
is already high enough, and llc-23 shapes its debug sections
differently).

Raise the module's PTX ISA requirement to 7.8 (the nearest supported
spelling at or above 7.5) when full device debug is enabled, through
the existing required_ptx_feature mechanism, which already refuses to
downgrade targets whose floor is higher. Line-tables debug emits no
such expressions and is untouched.
@nihalpasham

Copy link
Copy Markdown
Collaborator Author

The examples-compile failure was the new all-examples ptxas gate making its fourth catch, and this one is a pre-existing bug on main, not from this branch: llc-22 (CI's floor pin) emits DWARF label-difference expressions (.debug_pubnames end minus start, a PTX ISA 7.5 feature) into full-debug modules while declaring the target's default .version (7.0 at sm_80), so ptxas rejects them. Nothing ever assembled full-debug PTX before this gate: GPU boxes target sm_120a where the default version is already high enough, and llc-23 shapes its debug sections differently.

Fix in 049db58d5: full-debug modules raise the existing PTX-ISA requirement floor to 7.8 (nearest supported spelling at or above 7.5) via required_ptx_feature, which already refuses to downgrade newer targets. debug and reborrow now declare .version 7.8 and assemble under llc-22; full compile-only smoketest under CUDA_OXIDE_LLC=llc-22: 221/221.

@nihalpasham
nihalpasham merged commit 5d17828 into main Aug 30, 2026
52 checks passed
@nihalpasham
nihalpasham deleted the fix/llc23-branch-layout branch September 5, 2026 15:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working build-related Build scripts, toolchain discovery, or compilation setup codegen Device code-generation pipeline (Rust MIR to IR to PTX) perf Performance of generated code or of the compiler itself

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant