Repository navigation
Description
Every coordinate-dependent op takes lane: u32 explicitly: ldst::store_fragment,
RegTile::coordinate, and the ops the examples had to invent (make_causal_at,
load_tile, store_tile) inherit it. Each call site writes
let lane = warp::lane_id(); and threads it through — a value the op could read
itself.
It is consistent, and it may well be right. But it should be a decision rather
than an accident, and the window to decide it cheaply is closing.
Why now
#5 has landed: a generic map mechanism plus 18 op structs, with row_map /
col_map already coordinate-dependent. #21 (shared -> register), #22 (composed
movers), and #7 (masking) will each add more. Whatever convention those adopt is
the convention for roughly thirty functions, and changing it later means
touching all of them plus every downstream kernel.
The actual trade
Reading warp::lane_id() inside the op is one %laneid move, which the
compiler will very likely CSE across ops in the same basic block — but "very
likely" is doing work in that sentence, and this library's whole premise is that
register pressure and instruction count are worth being deliberate about. The
explicit form makes the cost visible and hoistable by construction; the implicit
form makes every call site shorter and removes a parameter that can be passed
wrongly.
Note the failure mode is not symmetric. Passing the wrong lane is silent —
it produces a plausible tile with values at the wrong coordinates, exactly the
class of bug the device harness exists to catch.
What would settle it
ptxas -v per-kernel register and spill counts (the regcount path added
alongside #5) over a kernel that calls several coordinate-dependent ops in
sequence, built both ways. If the implicit form costs nothing, take it — it is
strictly less for a kernel author to get wrong. If it costs, keep the explicit
form and record the number in reg.rs next to the existing 206->212 note, so
the next person does not re-litigate it.
Surfaced by the examples (#18), §9.
Activity
feitreim commentedon Jul 27, 2026
Acceptance: update
examples/as part of closing this.The invented
make_causal_at,load_tileandstore_tilein the examples all takelaneexplicitly because that is today's convention. Whichever way this is settled, the examples should be rewritten to match — they are where the convention is most visible.The examples crate (#18) exists so that "done" is checkable in the terms a kernel author cares about, rather than as a claim about a primitive nobody has tried to hold. Concretely, on closing this:
GAPfence inexamples/src/that this issue removes the need for, and replace it with the real API.aspirationaltocompiles: flip its header, take it out of its feature gate, and confirmmodal run modal_app.py::buildbuilds it forsm_100a.examples/README.md.If the example still does not compile afterwards, that is a finding worth writing down — say what is still missing rather than leaving the README stale.
feitreim commentedon Jul 27, 2026
Sequencing: measuring now, sweeping once the tree is quiet.
Splitting this issue's decision from its execution, because they have opposite timing.
The decision is urgent for the reason the issue states — #5 has landed and #6, #7, #21 and #22 each add more coordinate-dependent surface, so every day this stays open is more call sites to touch later.
The sweep is the worst thing to run right now: it rewrites signatures across
src/reg.rs,src/ldst.rsandsrc/mma.rs, all three of which have open PRs in flight. Hand-resolving conflicts in hardware-validated code is exactly where a silent coordinate bug would get introduced.So: the measurement is running now (it touches no library code and conflicts with nobody), and the sweep is queued as the next thing after #6, #21 and #30 merge — deliberately before #22, #25 and #7 widen the surface further.
Recording the tiebreak in advance, so the recommendation is not made after seeing the number it should not depend on: if codegen is a genuine wash, the explicit form loses on failure mode rather than on ergonomics. Passing the wrong lane is silent — it yields a fully-populated tile with values at the wrong coordinates, which no host test can catch and only the device harness would. Removing the parameter removes the possibility. If codegen is not a wash, the number wins and the explicit form stays.
feitreim commentedon Jul 27, 2026
Measured. Implicit is free; recommendation is a hybrid, split on a line the codegen didn't draw.
Probe in #36 (measurement only — it does not close this). Six kernels, three conventions at two widths, leaning on every lane-taking op in one loop body: a
coordinate-addressed load of a[32, N]block, aRegVec::rowvector intosub_row, aColVec::columnvector intoadd_col, a secondcoordinatepass for a causal mask, and astore_fragmentper[16, 16]block. That is the pattern where hoisting the lane would compound if it compounds anywhere.HOISTED— today's rule: onewarp::lane_id(), threaded throughPER_OP— each op reads%laneiditselfPER_QUERY— each row/column query reads it (i.e.coordinatereads once for the row and again for the column). The pessimistic bound.The numbers
modal run modal_app.py::regcount, sm_100a. No spills anywhere.lane_probe_128_hoistedlane_probe_128_per_oplane_probe_128_per_querylane_probe_32_hoistedlane_probe_32_per_oplane_probe_32_per_queryReproduced exactly across two independent runs. Every pre-existing kernel in the table is unchanged from
origin/main.The direct answer: the reads CSE, and I checked rather than assumed
Counting
%laneidin the emitted PTX:%laneidlane_probe_128_hoistedlane_probe_128_per_oplane_probe_128_per_querylane_probe_32_hoistedlane_probe_32_per_oplane_probe_32_per_queryOne
mov %laneidin every kernel, including the ones that spell dozens of separatewarp::lane_id()calls.PER_OPandPER_QUERYare indistinguishable on every column at both widths — doubling the reads changes literally nothing. The number ofwarp::lane_id()calls is free. The issue's prior was right on that point, and it now has a number behind it instead of "very likely".Worth noting the prior was right for a slightly lucky reason:
cuda_device::warp::lane_idis#[inline(never)]with aunreachable!()body, and it is the backend that rewrites it intollvm.nvvm.read.ptx.sreg.laneid. That the result is CSE-able is a property of that lowering, not of the source. If it ever stops being modelled as a pure read, this table moves — which is the argument for the probe being permanent rather than a one-off.What the register deltas actually are — read this before quoting "implicit saves 30 registers"
The implicit form is cheaper, which is as surprising as a loss would have been, so it needs an explanation rather than a victory lap.
It is not about read count (that is provably zero). It is about where the value is defined. Hoisting into the entry block makes the entire coordinate map loop-invariant, LICM lifts it out of the
stepsloop, and ptxas pays for the resulting long-lived values in registers. Reading inside the loop leaves the map to be recomputed.That trade is not the same at both widths:
So: codegen is a wash on the question actually asked (does an op reading its own lane cost anything — no), with a second-order allocator effect that is not a property of the convention and should not be cited as one. Per the framing in the issue, that hands the decision to the failure mode.
Positive control
A null result from a broken measurement looks the same, so:
Primary control. The probe originally carried a
RegTile<32, N>accumulator, which put the 128 kernels on the 255-register ceiling where everything looks alike by force. Swapping it for aRegVecaccumulator moved all six rows and the spill columns with them:lane_probe_128_hoistedlane_probe_128_per_oplane_probe_128_per_querylane_probe_32_hoistedlane_probe_32_per_oplane_probe_32_per_queryNote the ceiling case is itself the warning this issue needed: at 255 registers all three conventions read 255/84/84/3288 and I would have reported a clean null from a saturated probe.
Secondary control. Swapping the probe's
exp2forexp2_hwmovedlane_probe_32_hoisted72 → 80 andlane_probe_128_hoisted198 → 199, and moved the implicit rows not at all. Reverted. Interesting on its own: the hoisted form is the one that jitters under perturbation, the implicit form sat still — consistent with the deltas above being allocator behaviour rather than structure.Free control.
softmax_probe_128_row_mapreads 255 againsthand_written's 168 in the same table, the known 87-register result. The pipeline is live and sensitive at 128.Recommendation: implicit for ops, explicit for map queries
Not a fudge — the two groups differ in a way the measurement is silent about and the type system is not.
Go implicit for ops that execute.
ldst::store_fragmenttoday, and whatever #21, #22 and #7 add. These areunsafe, warp-collective, device-only, and can never be called with a lane other than the caller's. Codegen says the parameter buys nothing, and it is exactly the parameter whose misuse is silent:store_fragmenttakes its addresses from lanes 0..15 while the data lives elsewhere, so a wrong lane produces a fully-populated tile with values in the wrong places. Deleting the parameter deletes the bug class. This is where the ~30 functions this issue is worried about are going to land.Keep the explicit
laneon the pure map queries —RegTile::coordinate,RegVec::row,ColVec::column,BaseLdtm::row/::column. Not for codegen. Because they are already called from the host, for every lane, and that is load-bearing:device-tests/src/main.rs:1022,Shape::coordinate, builds the expected fragment map by callingRegTile::coordinate(lane, ..)across all 32 lanes. That is what turns a device dump into a coordinate-checked assertion.reg.rs's owncovers_each_coordinate_oncesweeps all 32 lanes to prove the layout is a bijection, pluscoordinate_tile,row_indices,column_indices,column_values_are_a_lane_groups_share_of_the_columns.warp::lane_id()does not exist on the host. Making these implicit deletes the tests that establish the ownership map is a map. They are queries about the layout, not operations at a lane, and the signature saying so is correct.If the ergonomics still grate, the free move is an implicit convenience on top —
RegTile::here(slot, value)alongsidecoordinate(lane, slot, value)— since the extra read provably costs nothing. I'd add it only when a call site wants it, not up front.Caveats
row_map/col_mapdirectly, because they take aRegVec/ColVecand no lane; the lane enters when building those vectors, which the probe does exercise.I did not touch
reg.rs— three branches are editing it. Since the answer is "no cost", there is no number to record next to 206 → 212; the measured table lives in the probe's doc comment, and whoever runs the sweep should carry a one-line pointer intoreg.rsthen.device-tests: a lane-passing convention probe (#27) (#36)
feitreim commentedon Jul 27, 2026
Decision, on the measurement above: hybrid — implicit for ops that execute, explicit for pure map queries.
The stated tiebreak was "if codegen is a wash, the explicit form loses on failure mode." Codegen is a wash — one
mov %laneidper kernel regardless of how many times the source spells the call, at both 32 and 128 columns, with a positive control confirming the probe moves. So the tiebreak applies, and the answer is implicit.But the measurement turned up a constraint the tiebreak did not anticipate, and it splits the surface in two.
The map queries are called from the host, and that is load-bearing.
RegTile::coordinate,RegVec::row,ColVec::column,BaseLdtm::row/columnare not only device code.device-tests/src/main.rsbuilds every expectation by calling them across all 32 lanes on the host — that is precisely the discipline that makes the device suite non-vacuous, since the kernel dumps by thread coordinate and the host applies the map.reg.rs'scovers_each_coordinate_onceproves the layout is a bijection the same way.warp::lane_id()does not exist in either context. Making those implicit would delete the tests that verify the fragment map against silicon.So:
warp::lane_id()read inside) for ops that execute:store_fragment,load_fragment, and whatever Movers work in [16,16] blocks; every kernel rewrites the same composition loop #22 and Masking and shape utilities: make_causal, tril/triu, fills, transpose #7 add. These are device-only,unsafe, warp-collective, and can never legitimately take a foreign lane. Free by measurement, and it removes the silent failure mode.lanestays on the pure coordinate queries — not for codegen, but because they must remain callable off-device.That line is invisible to
ptxasand would not have been found by measuring alone.Do not cite the 30-register gap as evidence for implicit. It is not read count, which is provably zero: it is where the value is defined. Hoisting makes the coordinate map loop-invariant, LICM lifts it, and ptxas pays in live registers. At 128 columns that is 30 registers on bodies 5 instructions out of 2495 apart — allocator jitter. At 32 columns the same 32-register saving costs 126 extra instructions and triple the local loads, pointing the other way. The honest summary is "no cost either way", not "implicit is faster".
The probe is now permanent (#36) rather than throwaway, because the CSE depends on the backend rewriting
lane_id— which is#[inline(never)]— intollvm.nvvm.read.ptx.sreg.laneid. A cuda-oxide lowering change would move the table silently.Remaining work to close this: the sweep over the executing ops. Queued for when
src/reg.rsandsrc/ldst.rsare quiet.Masking, against the coordinate origin a tiled kernel has (#7)
Masking, against the coordinate origin a tiled kernel has (#7)
Open the RegTile shape set, and mask against a coordinate origin (#23 +
Sweep the register ladder instead of measuring two widths (#60)
Sweep the register ladder instead of measuring two widths (#60) (#62)