Module Sarek_codegen.Sarek_ir_ptx_coopmat

Cooperative matrix (SCoopmat) for the PTX generator: the wmma.* instructions of the PTX ISA (tensor cores from sm_70, PTX ISA 6.0).

The lowering accepts exactly what the nvcuda::wmma arm of Sarek_ir_cuda_coopmat accepts: 16x16x16 on subgroup (warp) scope, any layout of A (row/col), B (row/col) and of the accumulator in memory, and two families of configuration.

wmma and not mma.sync: mma.sync.m16n8k16 needs sm_80, and the Turing form m16n8k8 is not translated by ZLUDA (specs/zluda-mma-nvrtc13.md). wmma is the one form sm_70 and later share, and CUDA/C already lowers to it.

Fragments. PTX fixes the per-lane register group of a wmma fragment (m16n16k16): an f16 operand is 8 .b32 registers each holding an f16x2 pair, an 8-bit operand 2 .b32 registers of four bytes, an f32 accumulator 8 .f32 registers and an integer accumulator 8 .b32 registers. The emitter never indexes into a group; it passes the whole group to wmma.load / wmma.mma / wmma.store, which agree on the distribution across lanes.

Alignment. wmma.load/wmma.store are undefined unless the address is 32-byte aligned and stride * element bytes is a multiple of 16, while CM_load/CM_store promise any index and stride. An access that Sarek_ir_coopmat_align proves aligned is the bare instruction. Any other access tests alignment at run time (the CUDA/C sarek_cm_require test) and, when the launch block is known, branches (warp-uniform) to an out-of-line slow path that stages the 16x16 tile element by element through a 32-byte aligned per-warp .shared buffer (sarek_cm_tiles, 1 KiB per warp, declared only when an access is unproven): results are the IR's for any index and stride, in either layout; a store writes only its tile. With no block known no buffer can be sized and a misaligned access executes trap. A .shared array a cooperative-matrix statement accesses is declared 32-byte aligned.

What is validated where. Text and refusals: unit tests, any machine. The instructions are checked by ptxas where it is installed. Execution needs an NVIDIA GPU of compute capability 7.0 or later; ZLUDA rejects wmma.*.

val min_sm : int

First SM with wmma, and with 8-bit wmma.

val min_sm_int8 : int
val shape_16 : C.shape

What the lowering accepts

val refuse : string -> string -> 'a
val check_fragment : C.fragment -> unit

Refuse a fragment this lowering cannot represent. Total over the fragment type: every case is accepted or raises with the reason.

val is_8bit : C.component_type -> bool
val is_int32 : C.component_type -> bool
val check_config : C.config -> unit

The configurations a multiply-add is emitted for: f16*f16+f32->f32, and 8-bit operands of one signedness with the same 32-bit integer accumulator type as addend and result (as the CUDA/C arm). The four fragments are checked first.

Layouts

val operand_layouts : Sarek_ir_types.kernel -> (string * C.layout) list

The layout of every operand fragment of the kernel, by name; refuses a name loaded in both layouts. wmma.mma names the layouts of A and B and expects the fragments to have been loaded in them, so a fragment has exactly one (the CUDA/C arm fixes the same thing in the fragment's type).

val accessed_buffers : Sarek_ir_types.kernel -> string list

The arrays a load or store of the kernel accesses. A .shared one among them is declared 32-byte aligned (Sarek_ir_ptx_types.shared_align), so a tile at index 0 passes the address check whatever its neighbours are.

Kernel-level gate

val sm_number : Stdlib.String.t -> int option

sm_number "sm_86" is 86; a suffix (sm_90a) is ignored.

val check_kernel : ?block:(int * int * int) -> sm_target:Stdlib.String.t -> Sarek_ir_types.kernel -> unit

The whole-kernel refusal, run before any instruction is emitted (a per-node refusal would name whichever node the emitter reached first). A uint8 buffer without a cooperative-matrix statement is ordinary 8-bit storage (Sarek_ir_int8) and is not this function's business. Also the launch shape: the instructions run on whole warps, and a partial warp has inactive lanes that would feed them undefined operands.

Statements

val regs_text : string list -> string
val layout_suffix : C.layout -> string
val elt_of : C.fragment -> Sarek_ir_types.elttype * int

Buffer element type and byte size a fragment is loaded from or stored to (an integer accumulator of either signedness lives in an int32 buffer, an 8-bit operand of either signedness in a uint8 one).

val elt_name : Sarek_ir_types.elttype -> string
val ptx_type : C.component_type -> string

The PTX type letters of a fragment component in wmma.load/wmma.store (f16, s8, u8, f32, s32).

val frag_reg_count : C.fragment -> int

Registers per lane of a fragment at m16n16k16.

val operand_layout : Sarek_ir_ptx_types.reg_alloc -> string -> C.layout
val emit_decl : Stdlib.Buffer.t -> Sarek_ir_ptx_types.reg_alloc -> string -> C.fragment -> unit
type access = {
  1. frag : C.fragment;
  2. space : Sarek_ir_ptx_types.arr_space option;
  3. base : string;
    (*

    the array's base register

    *)
  4. index : string;
    (*

    int32 register

    *)
  5. stride : string;
    (*

    int32 register

    *)
  6. addr : string;
    (*

    the element address, 32-bit for .shared else 64-bit

    *)
  7. bytes : int;
    (*

    element bytes

    *)
}

One fragment access, after the checks that make it meaningful: the buffer holds the fragment's element type, and is not per-thread .local memory (a warp-collective access to it has no meaning).

val ss : access -> string
val emit_access : Stdlib.Buffer.t -> Sarek_ir_ptx_types.reg_alloc -> Sarek_ir_ptx_types.env -> what:string -> frag:C.fragment -> buf_name:string -> index:Sarek_ir_types.expr -> stride:Sarek_ir_types.expr -> access
val emit_misaligned : Stdlib.Buffer.t -> Sarek_ir_ptx_types.reg_alloc -> access -> string

The predicate "this access is misaligned" (sarek_cm_require of the CUDA/C arm): the address is not 32-byte aligned, or the stride is not a whole number of 16-byte units. mul.lo.u32 keeps the low bits, the only ones tested.

val emit_unsafe : Stdlib.Buffer.t -> Sarek_ir_ptx_types.reg_alloc -> access -> load:bool -> string

The access must leave the bare instruction: misaligned, or a STORE whose stride is below the tile edge (its rows overlap in memory, and the answer is the last write of the interpreter's loop, which a matrix-unit store does not promise: such a store is written serially, see emit_slow_path).

Staging through shared memory

A misaligned access cannot use wmma.load / wmma.store on its own address. The slow path copies the tile element by element, with the IR's own formula, through a 32-byte aligned per-warp buffer in .shared: element t (0..255) of the buffer is the memory element at index + (t / 16) * stride + (t mod 16), in either layout (row-major: r * stride + c; column-major: c * stride + r); wmma then loads from / stores to the buffer with a leading dimension of 16, which is aligned for every element size. Lane l copies t = l + 32 j, j < 8. Results are those of the IR whatever the address and stride.

val tile_name : string
val tile_bytes_per_warp : int
val ensure_tiles : Sarek_ir_ptx_types.reg_alloc -> warps:int -> unit

The buffer, declared at its first use, warps tiles of 1 KiB.

val move_width : int -> string
val copy_register : Sarek_ir_ptx_types.reg_alloc -> int -> string

One element register of the staging copy.

val emit_slow_path : Sarek_ir_ptx_types.reg_alloc -> label:string -> done_label:string -> access -> layout:C.layout -> load:bool -> wmma:(Stdlib.Buffer.t -> string -> string -> unit) -> unit

The slow path of one access, appended out of line. wmma receives the address register of the buffer (a .shared window offset) and the register holding 16. load: copy in, then wmma.load; else wmma.store, then copy out.

val emit_guarded : Stdlib.Buffer.t -> Sarek_ir_ptx_types.reg_alloc -> op:Sarek_ir_types.coopmat_op -> access -> layout:C.layout -> load:bool -> direct:(Stdlib.Buffer.t -> string -> string -> unit) -> staged:(Stdlib.Buffer.t -> string -> string -> unit) -> unit

The shape shared by loads and stores: direct buf addr stride is the wmma instruction on addr/stride; staged buf tile sixteen the same on the staging buffer. Proven aligned: the bare instruction. Unproven with a known block: a warp-uniform branch to the out-of-line slow path. Unproven without a block (no buffer can be sized): the trap.

val emit_load : Stdlib.Buffer.t -> Sarek_ir_ptx_types.reg_alloc -> Sarek_ir_ptx_types.env -> op:Sarek_ir_types.coopmat_op -> dst:string -> frag:C.fragment -> src:string -> index:Sarek_ir_types.expr -> stride:Sarek_ir_types.expr -> layout:C.layout -> unit
val emit_store : Stdlib.Buffer.t -> Sarek_ir_ptx_types.reg_alloc -> Sarek_ir_ptx_types.env -> op:Sarek_ir_types.coopmat_op -> src:string -> frag:C.fragment -> dst:string -> index:Sarek_ir_types.expr -> stride:Sarek_ir_types.expr -> layout:C.layout -> unit
val emit_muladd : Stdlib.Buffer.t -> Sarek_ir_ptx_types.reg_alloc -> dst:string -> a:string -> b:string -> c:string -> C.config -> unit

emit_op buf alloc env op emits one cooperative-matrix statement.