Repository navigation
Restore ptxas SASS loop unrolling under LLVM 23 (disable llc's late branch folding); assemble every compile-only PTX in CI - #1205
Conversation
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.
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.
|
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 ( Fix in |
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: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:
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.unicontinue. Negated predicates themselves are innocent.The fix
Two ancient, generic, disable-only llc flags on every llc invocation:
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?
ptxas -vregisters compared with/without the flags. 45 kernels move, every one explained:Zero unexplained movers, zero below pre-bump baselines.
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