Repository navigation
Description
Every mbarrier.try_wait.parity spin in this repo emits five instructions around the wait that do nothing but turn a predicate into an i32, mask it, turn it back into a predicate and invert it — because cuda_device's mbarrier_try_wait_parity returns a Rust bool and the idiom is while !mbarrier_try_wait_parity(..) {}. A tight spin is the wait followed by @!p bra.
Found alongside #150 while counting the MMA warp's poll-loop body in #148. It does not explain any gap we are chasing — upstream's vendored copy pays exactly the same five, because it calls the same intrinsic — which is precisely why it wants its own issue rather than a line in a GEMM PR.
The body
From modal_app.py::build's _print_mma_stream, gemm_sol_m256's load wait:
$L__BB2_42:
mov.b64 %rd200, __dynamic_smem__RINvNt…small_body…; // addressing, see #150
add.s64 %rd199, %rd200, 131080; // addressing, see #150
{ mbarrier.try_wait.parity.shared::cta.b64 %p0, [%rd199], %r158;
selp.b32 %r125, 1, 0, %p0; } // pred -> i32
and.b32 %r127, %r125, 1; // mask the bool
setp.ne.b32 %p20, %r127, 0; // i32 -> pred
not.pred %p21, %p20; // invert for `while !`
@%p21 bra $L__BB2_42;
Eight instructions, of which two are addressing (#150) and five are the round trip: selp.b32, and.b32, setp.ne.b32, not.pred, bra. What the hardware needs is mbarrier.try_wait.parity and a predicated branch on its result — two. Upstream's body is the same minus the add.s64, so it carries the identical five.
The selp.b32 sits inside the ptx_asm! block, i.e. it is the intrinsic's own doing rather than the caller's: the wrapper's job is to hand Rust a bool, and a bool cannot be a predicate. The and/setp/not are LLVM materializing and re-testing that bool on the other side of the ABI boundary.
Where it appears: everything reached through src/sync.rs's Semaphore::wait, SemaphoreRing::wait and SemaphoreRing::wait_recycled — which is every warp-specialized kernel in the repo, gemm, gemm_ws, gemm_sol's three entries and flash_forward. It is not a GEMM-shaped finding.
The caveat, which is the whole of why this is a report and not a lever
This is a PTX count, and ptxas is a real optimizer. A predicate spilled to an i32 and re-tested is one of the most recognizable peephole patterns there is, and the NVPTX back end emits it for every bool-returning intrinsic, so ptxas has every reason to carry a pattern for it. My expectation is that ptxas already folds most or all of these five and that the executed spin is close to the two instructions it should be. It is filed as a PTX observation for that reason: "six wasted instructions" is not earned, and this issue should not be quoted as though it were.
There is a precedent in this repo for exactly this distinction. regcount's census carries a selp column, added by #125 to check whether a flag folded — so selp counts have already been read here as a "did the compiler do the obvious thing" signal at the PTX level, and #125's answer was that it had. The same standard applies to this issue and it has not yet been met.
What would confirm or kill it
SASS, and it is CPU-only. modal_app.py::build already runs ptxas over the bundle; cuobjdump -sass or nvdisasm on the cubin it produces, printing the spin loop for gemm_sol_m256, would settle this outright. On the order of ten lines beside the existing step, no GPU, and it settles #150 in the same output. Do this before treating either as a lever.
Failing that, a timed spin microbenchmark: one warp polling an mbarrier a fixed number of times, against a hand-written ptx_asm! spin that never leaves the predicate domain. That prices the difference without reading machine code.
If SASS says the round trip survives, the fix is a cuda_device- or src/sync.rs-level spin that keeps the predicate: one ptx_asm! whose body is the try_wait and the backward branch, so the loop never crosses the bool boundary. Small change, wide blast radius — it is the inner loop of every pipeline here — and it would want its own correctness argument, because a hand-written backward branch inside inline asm is not something the Semaphore contract has had to reason about before.
If SASS says it is already folded, this closes with a useful fact in it: the selp/setp/not.pred chains around every mbarrier wait in this repo are census noise rather than cost, and nobody need look again.
Activity
File the two library-level poll-body findings as #150 and #151
feitreim commentedon Jul 30, 2026
Closing:
ptxasfolds all of it. The instrument that would settle this is built (modal_app.py::_print_sass_loops, CPU-only, inbuild) and it settles it against the lever.The spin loop as the machine runs it,
nvdisasm -con the cubinbuildalready assembles, found by backward branch rather than by mnemonic:Three instructions an iteration, and the same three in all three kernels:
gemm_sol_clc_multicast_4_stage_pipelinegemm_sol_m256_runtimegemm_sol_m256as shippedSo both PTX findings are folded, and neither is a cost anything executes:
TRYWAIT P0to@!P0 BRA; there is noselp, noand, nosetp, nonot.pred. The two instructions a tight spin needs are the two it has, plus aYIELDthatptxasadds as a spin hint rather than as overhead.add.s64and the symbol rematerialization (Every mbarrier spin pays an add.s64 per poll, because barriers live at offsets into one dynamic shared allocation #150) are gone entirely. The operand is[R5+URZ]— the address held in a register across the loop, no add and nomovof a shared symbol in the body. Our poll body is upstream's poll body, exactly.The prediction #150 filed — 0.0093 µs a K block, 2.7% of the K-block rate, 78.8% → ~81.0% — is dead, and not through its named falsifier (a latency-bound spin) but through something simpler: the instruction is not there. No container was spent on it, which is the one thing that went right about the sequencing.
A third thing this confirms, and it was the more valuable claim. #148 attributed the stage fold's +2.2–4.3% to the inter-stage critical path and explicitly not to the poll body, after an earlier revision had it the other way round. SASS is decisive: the shipped and pre-fold spin loops are identical at 3 instructions, so the fold's win cannot have come from the poll body at any level. And the two kernels' loop tables differ in exactly one row —
gemm_sol_m256carries a 10-instruction loop wheregemm_sol_m256_runtimecarries a 14 — 4 instructions an iteration removed from a loop that is not the poll loop. That is the fold, at the machine level, in the place #148 said it was.The general lesson is written up in
experiments/README.mdrather than left in these two issues, because it is about the instrument set: nearly every conclusion in this GEMM effort is a PTX count, and this is the first time one was checked against SASS.