Sarek_codegen.Sarek_ir_ptx_coopmatCooperative 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.mma ... .s32.s8.s8.s32 or .s32.u8.u8.s32: a 32-bit wrapping sum has the same bits either way. Mixed-sign operands have no wmma form and are refused (a named #error on CUDA/C). The non-saturating form only. bf16 is refused by both: nvcuda::wmma's bf16 fragment reads a bf16 buffer, not a float32 buffer rounded on load.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.*.
module C = Sarek_coopmat_typesval shape_16 : C.shapeval check_fragment : C.fragment -> unitRefuse 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 -> boolval is_int32 : C.component_type -> boolval check_config : C.config -> unitThe 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.
type operand_layout = Sarek_ir_cuda_coopmat.operand_layoutval operand_layouts : Sarek_ir_types.kernel -> (string * C.layout) listThe 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 listThe 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.
val check_kernel :
?block:(int * int * int) ->
sm_target:Stdlib.String.t ->
Sarek_ir_types.kernel ->
unitThe 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.
val layout_suffix : C.layout -> stringval elt_of : C.fragment -> Sarek_ir_types.elttype * intBuffer 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 -> stringval ptx_type : C.component_type -> stringThe PTX type letters of a fragment component in wmma.load/wmma.store (f16, s8, u8, f32, s32).
val frag_reg_count : C.fragment -> intRegisters per lane of a fragment at m16n16k16.
val declared :
Sarek_ir_ptx_types.reg_alloc ->
string ->
Sarek_ir_ptx_types.coop_fragval operand_layout : Sarek_ir_ptx_types.reg_alloc -> string -> C.layoutval emit_decl :
Stdlib.Buffer.t ->
Sarek_ir_ptx_types.reg_alloc ->
string ->
C.fragment ->
unittype access = {frag : C.fragment;space : Sarek_ir_ptx_types.arr_space option;base : string;the array's base register
*)index : string;int32 register
*)stride : string;int32 register
*)addr : string;the element address, 32-bit for .shared else 64-bit
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 -> stringval 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 ->
accessval emit_misaligned :
Stdlib.Buffer.t ->
Sarek_ir_ptx_types.reg_alloc ->
access ->
stringThe 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 ->
stringThe 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).
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 ensure_tiles : Sarek_ir_ptx_types.reg_alloc -> warps:int -> unitThe buffer, declared at its first use, warps tiles of 1 KiB.
val copy_register : Sarek_ir_ptx_types.reg_alloc -> int -> stringOne 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) ->
unitThe 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) ->
unitThe 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 ->
unitval 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 ->
unitval emit_muladd :
Stdlib.Buffer.t ->
Sarek_ir_ptx_types.reg_alloc ->
dst:string ->
a:string ->
b:string ->
c:string ->
C.config ->
unitval emit_op :
Stdlib.Buffer.t ->
Sarek_ir_ptx_types.reg_alloc ->
Sarek_ir_ptx_types.env ->
Sarek_ir_types.coopmat_op ->
unitemit_op buf alloc env op emits one cooperative-matrix statement.