Module Sarek_ir_coopmat_align

Static alignment proof for cooperative-matrix loads and stores on NVIDIA (wmma), shared by the CUDA/C and PTX generators.

wmma.load / wmma.store need a 32-byte aligned address and a leading dimension that is a multiple of 16 bytes. The IR promises neither (any start index, any stride: Sarek_ir_types.coopmat_op.CM_load), so a generator either checks at run time and stages a misaligned tile through shared memory, or proves the access aligned and emits the bare instruction. This module is the proof.

What is proved. A power-of-two factor of an int32 expression: f such that f divides the expression's value, whatever the run. It is computed from the expression alone, by rules that hold under wrapping 32-bit arithmetic (divisibility by a power of two is preserved modulo 2^32):

An access is proven when factor index * element bytes >= 32 and factor stride * element bytes >= 16 (both sides are powers of two, so >= is divisibility). The base pointer is assumed aligned: a global buffer is a device allocation (256-byte aligned) and a .shared array a cooperative-matrix statement accesses is declared 32-byte aligned by the generators. A cooperative-matrix kernel that does not rely on either (a pointer into the middle of an allocation) must not pass a view to the launch.

The walk is by name with lexical scoping, so a shadowing binder (a let, a for variable, a match payload) hides an outer fact. The result is the list of proven operations, identified physically: a generator asks about the very node it is about to emit, and an operation it cannot find (a rewritten tree, a helper body) is unproven, which is the safe answer.

val unbounded : int

Cap of a factor: 2^30, standing for "divisible by every power of two that matters".

val tile_edge : int

The edge of a cooperative-matrix tile: a store with a stride below it overlaps its own rows (columns), and is written serially (the answer is the last write of the interpreter's loop; a matrix-unit store does not promise an order).

val lowest_bit : int -> int
type env = (string * int) list
val factor : env -> Sarek_ir_types.expr -> int
val element_bytes : C.fragment -> int

Bytes per element of the buffer a fragment is loaded from or stored to.

val access_is_proven : env -> frag:C.fragment -> index:Sarek_ir_types.expr -> stride:Sarek_ir_types.expr -> bool
val tile_bytes : C.fragment -> int

Bytes of the 16x16 staging tile of a fragment.

val aligned_hint : string

Pragma hint carried by the body of the aligned variant of a kernel. The generators read it as "every access is aligned at run time" and emit the bare instruction; every other consumer ignores a pragma.

val checked_hint : string

Pragma hint of the checked variant: no static proof is used, every access is tested at run time (and staged when misaligned). Selected when a launch argument's base pointer is not 32-byte aligned, which every static proof otherwise assumes.

val is_checked : Sarek_ir_types.stmt -> bool
val is_marked : Sarek_ir_types.stmt -> bool

The proven loads and stores of a kernel body; all of them in the aligned variant (mark_aligned).

val is_proven : 'a list -> 'a -> bool

Whether op is one of the proven operations (physical identity).

Launch-time variant: a predicate over the scalar arguments

An access whose index or stride depends on a kernel parameter (a GEMM's k and n) cannot be proven aligned from the IR alone, but the host knows the parameter's value at launch. derive reduces "every cooperative-matrix access of the kernel is aligned" to a condition on the kernel's scalar int32 parameters, a disjunction of conjunctions of atoms "parameter p is a multiple of n" (a predicate). The launch evaluates it (holds) on the actual arguments: true selects the variant of the kernel generated with mark_aligned (bare wmma, no check, no buffer), false the staged variant.

need env e n is the condition under which e is a multiple of n (a power of two), by the rules of factor extended with parameters as atoms, and with a product a * b allowed any split a multiple of x, b multiple of n / x. A kernel parameter that is not shadowed is the atom (name, n); anything the rules cannot reduce makes the condition false and derive answer None (the kernel always takes the staged variant).

type atom =
  1. | Multiple of string * int
    (*

    the parameter is a multiple of n

    *)
  2. | At_least of string * int
    (*

    |parameter| >= n: a store whose stride is below the tile edge overlaps its own rows, and is not left to the matrix unit

    *)
type predicate = atom list list

Disjunction of conjunctions; [[]] is true, [] false.

val p_true : predicate
val p_false : predicate
val max_terms : int
val implies : atom -> atom -> bool
val reduce_conj : atom list -> atom list
val normalise : predicate -> predicate
val p_and : predicate -> predicate -> predicate
val p_or : predicate -> predicate -> predicate
type binding =
  1. | Bound of Sarek_ir_types.expr * (string * binding) list
    (*

    an immutable let: its definition and the scope it was made in

    *)
  2. | Opaque
    (*

    a loop, mutable or match variable: unknown

    *)
type symbolic_env = (string * binding) list
val need : params:string list -> symbolic_env -> Sarek_ir_types.expr -> int -> predicate
val at_least : params:string list -> symbolic_env -> Sarek_ir_types.expr -> int -> predicate

The condition under which |e| >= n: a constant, a parameter (or a let bound to one), nothing else (anything the rules cannot reduce is false).

val scalar_params : Sarek_ir_types.kernel -> string list

The scalar int32 parameters of a kernel.

val derive : Sarek_ir_types.kernel -> predicate option

The condition under which every cooperative-matrix access of k's body is aligned, None when it cannot be derived (a coopmat statement in a helper function, or an access the rules cannot reduce). A kernel with no access that needs a condition gives Some p_true.

val holds : predicate -> (string * int) list -> bool

Whether p holds for the scalar arguments args (parameter name, value). A parameter without a value makes its atom false.

The aligned variant of k: the same kernel with its body marked.

The kernel with every static proof switched off: the checked variant.

val arrays_accessed : Sarek_ir_types.kernel -> string list

The arrays the kernel's cooperative-matrix loads and stores read or write, in order of first appearance.