Skip to content

Every mbarrier spin round-trips a predicate through an i32 — five PTX instructions, unconfirmed at the machine level #151

Closed as not planned

Description

@feitreim

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

  1. feitreim commented on Jul 30, 2026

    @feitreim
    OwnerAuthor

    Closing: ptxas folds all of it. The instrument that would settle this is built (modal_app.py::_print_sass_loops, CPU-only, in build) and it settles it against the lever.

    The spin loop as the machine runs it, nvdisasm -c on the cubin build already assembles, found by backward branch rather than by mnemonic:

    YIELD ;
    SYNCS.PHASECHK.TRANS64.TRYWAIT P0, [R5+URZ], R4 ;
    @!P0 BRA `(.L_x_1845) ;
    

    Three instructions an iteration, and the same three in all three kernels:

    PTX poll body SASS poll body
    upstream gemm_sol_clc_multicast_4_stage_pipeline 7 3
    gemm_sol_m256_runtime 7 3
    gemm_sol_m256 as shipped 8 3

    So both PTX findings are folded, and neither is a cost anything executes:

    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_m256 carries a 10-instruction loop where gemm_sol_m256_runtime carries 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.md rather 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions