Repository navigation
Description
Every mbarrier in this repo lives at a const offset into one dynamic shared allocation, so every mbarrier.try_wait.parity spin pays a mov.b64 of the dynamic-shared symbol plus an add.s64 inside the loop body. A static .shared symbol per barrier pays the mov.b64 and not the add.
Found while chasing the last 3.5 points of tensor-core peak between gemm_sol's [256, 256] K loop and upstream's (#148). It is filed separately because it is not a gemm_sol fact: src/sync.rs is what every warp-specialized kernel here spins on.
The count
modal_app.py::build's _print_mma_stream prints the MMA warp's K-loop PTX. Instructions per try_wait iteration, from one bundle carrying our kernels and upstream's vendored copy side by side:
| per poll | the address | |
|---|---|---|
upstream gemm_sol_clc_multicast_4_stage_pipeline |
7 | mov.b64 %rd180, __shared_mem_27 |
gemm_sol_m256_runtime |
7 | held in a register across the loop |
gemm_sol_m256 as shipped |
8 | mov.b64 of __dynamic_smem__… then add.s64 %rd199, %rd200, 131080 |
Ours, in full:
$L__BB2_42:
mov.b64 %rd200, __dynamic_smem__RINvNt…small_body…;
add.s64 %rd199, %rd200, 131080;
{ mbarrier.try_wait.parity.shared::cta.b64 %p0, [%rd199], %r158; selp.b32 %r125, 1, 0, %p0; }
and.b32 %r127, %r125, 1;
setp.ne.b32 %p20, %r127, 0;
not.pred %p21, %p20;
@%p21 bra $L__BB2_42;
The add.s64 is adding a literal to a loop-invariant symbol, on every iteration.
Why a static census cannot price it, and what can
This is the reason the issue exists at all. #148's other census counts instructions between the first and last tcgen05.mma and found our stream 2.6× leaner than upstream's while still slower — from which an earlier revision of that PR wrongly concluded that instruction count was not a term anywhere. A spin loop's cost is its body times its iteration count, and no static count between MMA issues can see the iterations.
The iterations are not small. gemm_sol's MMA warp spends whole − issue only = 0.3503 − 0.2758 = 0.0745 µs of every K block, 21% of it, inside this loop.
Its sign, and the experiment that would settle it
The available natural experiment does not settle it, and it is worth saying why, because it looks like it does. #148's stage fold took the poll body from 7 to 8 — LLVM stopped keeping the address in a register once the stage became a literal — and the kernel got 4% faster. That is not evidence a heavier poll body is free: the fold moved two variables with opposite signs, adding one instruction to the poll body while removing many from the inter-stage path (per-stage parity, ring offsets and both operand descriptors). It is a two-variable change and therefore not a controlled experiment on poll-body length.
Removing the add.s64 is the single-variable version, and on general grounds its sign is positive: one fewer instruction in a hot loop body, nothing else moved. The magnitude worth predicting in advance, and its falsifier:
- If the loop is issue-bound, one instruction of eight is an eighth of 0.0745 µs — 0.0093 µs, 2.7% of the K-block rate — which would take that K loop from 78.8% of peak to about 81.0% against upstream's 82.3%, i.e. most of the residual The small entry's K loop is its feed's duty cycle, and the ring index was a port defect (#144) #148 could not locate.
- If the loop is latency-bound on
try_wait's own shared read, the body's instruction count is a small fraction of each iteration and the win is much smaller. This is the more likely of the two and it is the thing to check first.
It is reachable, and it is not upstream-blocked
cuda_device at the pin plainly admits static .shared mbarriers. Upstream's vendored copy declares eight of them in this tree —
static mut TMA_BAR0: Barrier = Barrier::UNINIT;— inside its #[kernel] body, cuda-oxide places them as __shared_mem_27..29, and modal_app.py::build assembles that bundle with ptxas and upstream_bench runs it. So the mechanism exists, is proven here, and nothing about this waits on a pin bump.
What it costs, which is structure and not residency
- Not shared memory.
gemm_sol's barriers are ~104 B (STAGES * 8twice forload/free,2 * 8twice forfull/empty, 8 forready) out of a 196864 B plan, moving from the dynamic allocation to static shared inside the same 233472 B an SM divides. Each launch contract'sdynamic_shareddrops by that much and nothing crosses an occupancy step. - The layout is deliberate and this would undo part of it.
src/sync.rs'sSemaphoreandSemaphoreRingtake a*mut Barrierderived from a dynamic base, andgemm_sol.rs'sload_offset/free_offset/full_offset/empty_offset/ready_offsetare that base's layout — which is The shared plan is one walk now, and the two ends of it cannot share a pointer add (#125) #137's "the shared plan is one walk". A static-barrier spelling is a second placement mechanism beside the walk, not a replacement for it, and the two would have to coexist. - Unproven from where this kernel needs it. Upstream declares its statics inside a
#[kernel]body.gemm_sol'ssmall_bodyandlarge_bodyare deliberately outside#[cuda_module]so the shipped kernels and The small entry's K loop is its feed's duty cycle, and the ring index was a port defect (#144) #148's ablation arms are one text, and whether astatic mut Barrierin a plainfnstill lands in.sharedis the macro's business rather than the type's. If it does not, the statics have to be declared per#[kernel]and threaded in, and that is the real design cost.
The PTX caveat, and which of these survives ptxas
This is a PTX observation and ptxas is a real optimizer. Nothing here is confirmed at the machine level, and it should not be quoted as an instruction anybody actually executes until it is.
Between this and #151, the predicate round-trip, this one is the more likely of the two to survive, but not by much and not confidently. Against it: an add.s64 of a literal to a base is exactly what a [reg+imm] addressing mode absorbs, and PTX permits that form, so ptxas folding it into the try_wait operand is entirely plausible. For it: the mov.b64 of the dynamic-shared symbol is also inside the loop, and hoisting that is a loop-invariant motion decision at the machine level rather than a peephole — upstream pays the same mov.b64, so the difference between us really is only the add, which is the single most foldable instruction in the body. Honest expectation: there is a fair chance both this and the predicate round-trip are already free, and the difference between the two kernels' poll bodies is zero in SASS.
What would confirm or kill it, CPU-only: cuobjdump -sass or nvdisasm on the cubin ptxas already produces in modal_app.py::build, printing the spin loop for gemm_sol_m256 and gemm_sol_clc_multicast_4_stage_pipeline. That is on the order of ten lines beside the existing ptxas step, needs no GPU, and settles this issue and its sibling together. It should be done before either is treated as a lever. Only if SASS shows the difference is real is a timed arm worth a container, and then the prediction above is the one to hold it to.
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.