Skip to content

TMEM columns are an unchecked u32 and the MMA takes a raw address, not the TmemTile that exists #128

Closed

Description

@feitreim

Found by the 2026-07-29 library audit (GAPS.md §7). Two halves of one thing:
the TMEM surface is typed everywhere except at the two points where being untyped
has already cost something.

1. alloc_block/alloc_cluster take a runtime columns: u32 and check nothing

pub unsafe fn alloc_block(slot: *mut u32, columns: u32) -> u32
pub unsafe fn alloc_cluster(slot: *mut u32, columns: u32) -> u32

tcgen05.alloc requires a power of two in [32, 512]. Nothing in these
signatures can see the argument, so flash_forward asked for 192 from the day
it was written and nothing caught it
— no launcher, no codegen assert, no
runtime check. It now carries the rule itself
(examples/src/flash_forward.rs:203-209) and says why in its own doc at
191-196: "the column count is an argument to an instruction, so no type and no
const assertion was ever in a position to see it."

A const COLUMNS: u32 parameter puts it in a position to see it. A
post-monomorphization const { assert!(..) } fires at codegen, which is exactly
the class of failure tier 2 exists to catch (CI.md), and every caller in the
repo passes a constant already: TMEM_COLUMNS (flash_forward.rs:202),
BLOCK_N as u32 (gemm.rs:2707), ACCUM_COLUMNS (gemm_ws.rs:1121).

While there: the columns are also passed a second time to dealloc_*, where
they must match, and nothing checks that either. A const parameter makes the
pair type-checkable.

2. Every MMA entry point takes a bare u32 where a TmemTile exists

mma::mma_abt, mma_ab, mma_atb, mma_atbt, mma_walk_cg2 and their four
mm_* twins all take tmem: u32. TmemTile<R, C> is the crate's type for
exactly that address, it knows R and C, and the MMA is the only thing that
writes it. src/mma.rs:84-88 says so itself:

The accumulator stays fp32 because a walk names TMEM by a bare u32 and has
no accumulator type to read one off; fp16 accumulation is a .kind::f16-only
mode no kernel here uses, and threading a typed accumulator through belongs
with whatever gives the walks a crate::tmem tile instead of an address.

Three things that costs today:

  • MmaShape is a fourth argument the accumulator already implies.
    examples/src/gemm.rs:545-551 is a const fn pair_shape(block_n) -> MmaShape
    that panic!s on an unmatched width — a lookup from a tile's columns to a
    shape, written in a kernel, because the type that knows the columns is not in
    the call.
  • mma_ab accumulates into tmem + band * 64 (src/mma.rs:187), i.e. it
    does TmemTile::columns_right's arithmetic on a raw integer.
  • fp32 accumulate is hardcoded in descriptor, and cannot be otherwise while
    there is no accumulator type to read it off.

Scope

  • alloc_block::<COLUMNS> / alloc_cluster::<COLUMNS> with the legality assert
    in a const block, and dealloc_* taking the same parameter. Keep a runtime
    escape hatch only if something needs it — nothing in tree does.
  • Let the MMA walks take TmemTile<M, N> instead of u32, deriving MmaShape
    from (M, N) where a legal shape exists and rejecting at codegen where it does
    not. This subsumes pair_shape.
  • Do not thread an accumulator element type in this issue. fp16 accumulate
    is a separate ask with no caller; the point here is that the type becomes
    possible, not that it lands.

Estimate

Part 1 is ~30 lines and a mechanical sweep of five call sites. Part 2 is ~80
lines in src/mma.rs, and the risk is the MmaShape derivation: the shape set
is the ISA's and not every (M, N) has one, so the mapping has to reject rather
than round. device-tests' five mma A·Bᵀ … mma transpose control cases are
the existing check.

Related: #124 (the allocators' scope is also implicit), #16 (fp8, which touches
MmaElement but not this), #12 (which closed the operand-order square and left
the accumulator untyped).

Activity

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

    @feitreim
    OwnerAuthor

    Upgrade note (20a5616): cuda-oxide adds helpers for the conventional mma.m16n8k16 accumulator layout in 5c428ed, extended with logical-extent masks in 13fab63.

    This does not directly type tcgen05 TMEM accumulators: it maps per-lane register fragments for the older warp MMA shape, while this issue is about TmemTile<M, N> and tcgen05 addresses. It is still worth using as an API/design reference for coordinate decoding, validity masks and rejecting impossible extents rather than silently rounding them.

  3. added a commit that references this issue on Aug 6, 2026
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:mmaarea:types

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions