Skip to content

Settle the lane-passing convention before the op set grows #27

Closed

Description

@feitreim

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

  1. added
    area:types
    on Jul 27, 2026
  2. feitreim commented on Jul 27, 2026

    @feitreim
    OwnerAuthor

    Acceptance: update examples/ as part of closing this.

    The invented make_causal_at, load_tile and store_tile in the examples all take lane explicitly 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:

    • Delete any workaround or GAP fence in examples/src/ that this issue removes the need for, and replace it with the real API.
    • If a kernel becomes fully unblocked, move it from aspirational to compiles: flip its header, take it out of its feature gate, and confirm modal run modal_app.py::build builds it for sm_100a.
    • Update the status table and the blocked-on lists in 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.

  3. feitreim commented on Jul 27, 2026

    @feitreim
    OwnerAuthor

    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.rs and src/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.

  4. feitreim commented on Jul 27, 2026

    @feitreim
    OwnerAuthor

    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, a RegVec::row vector into sub_row, a ColVec::column vector into add_col, a second coordinate pass for a causal mask, and a store_fragment per [16, 16] block. That is the pattern where hoisting the lane would compound if it compounds anywhere.

    • HOISTED — today's rule: one warp::lane_id(), threaded through
    • PER_OP — each op reads %laneid itself
    • PER_QUERY — each row/column query reads it (i.e. coordinate reads 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.

    kernel regs spill st spill ld stack
    lane_probe_128_hoisted 198 0 0 2816
    lane_probe_128_per_op 168 0 0 2816
    lane_probe_128_per_query 168 0 0 2816
    lane_probe_32_hoisted 72 0 0 256
    lane_probe_32_per_op 40 0 0 512
    lane_probe_32_per_query 40 0 0 512

    Reproduced 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 %laneid in the emitted PTX:

    kernel insns %laneid ld.local st.local
    lane_probe_128_hoisted 2495 1 750 1294
    lane_probe_128_per_op 2490 1 749 1289
    lane_probe_128_per_query 2490 1 749 1289
    lane_probe_32_hoisted 517 1 40 72
    lane_probe_32_per_op 643 1 120 152
    lane_probe_32_per_query 643 1 120 152

    One mov %laneid in every kernel, including the ones that spell dozens of separate warp::lane_id() calls. PER_OP and PER_QUERY are indistinguishable on every column at both widths — doubling the reads changes literally nothing. The number of warp::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_id is #[inline(never)] with a unreachable!() body, and it is the backend that rewrites it into llvm.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 steps loop, 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:

    • At 128: implicit is 30 registers cheaper on a PTX body 5 instructions out of 2495 from identical, with identical local traffic. A 30-register swing from a 0.2% instruction delta is the allocator landing differently on near-identical input.
    • At 32: implicit is 32 registers cheaper but buys them with 126 more instructions (517 → 643) and triple the local loads (40 → 120). Here the trade is real and visible, and it is not obviously a win.

    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 a RegVec accumulator moved all six rows and the spill columns with them:

    kernel before after
    lane_probe_128_hoisted 255 regs, 84/84 spill, 3288 stack 198, 0/0, 2816
    lane_probe_128_per_op 255, 84/84, 3288 168, 0/0, 2816
    lane_probe_128_per_query 255, 84/84, 3288 168, 0/0, 2816
    lane_probe_32_hoisted 96, 0/0, 256 72, 0/0, 256
    lane_probe_32_per_op 63, 0/0, 512 40, 0/0, 512
    lane_probe_32_per_query 63, 0/0, 512 40, 0/0, 512

    Note 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 exp2 for exp2_hw moved lane_probe_32_hoisted 72 → 80 and lane_probe_128_hoisted 198 → 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_map reads 255 against hand_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_fragment today, and whatever #21, #22 and #7 add. These are unsafe, 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_fragment takes 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 lane on 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 calling RegTile::coordinate(lane, ..) across all 32 lanes. That is what turns a device dump into a coordinate-checked assertion.
    • reg.rs's own covers_each_coordinate_once sweeps all 32 lanes to prove the layout is a bijection, plus coordinate_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) alongside coordinate(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

    • The probe holds its tiles in local memory (2816 bytes stack, zero spills), as the existing probes do. A real kernel keeping its accumulator in TMEM and draining may allocate differently — the same caveat already recorded on the 206 → 212 note.
    • The 30-register delta is measured on this probe's shape. It is an allocator outcome, so I would not expect its sign to be stable across kernels; the claim I'd defend is "the read count is free", not "implicit saves registers".
    • 128 was necessary. At 32 the hoisted/implicit gap exists but comes bundled with a 25% instruction increase that points the other way — a 32-only measurement would have been misleading in both directions.
    • This does not measure Generic map mechanism and the standard elementwise op set #5's row_map/col_map directly, because they take a RegVec/ColVec and 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 into reg.rs then.

  5. feitreim commented on Jul 27, 2026

    @feitreim
    OwnerAuthor

    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 %laneid per 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/column are not only device code. device-tests/src/main.rs builds 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's covers_each_coordinate_once proves 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:

    That line is invisible to ptxas and 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)] — into llvm.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.rs and src/ldst.rs are quiet.

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

    area:types

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions