Sarek_ir_coopmat_alignStatic 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):
c: its lowest set bit (0: unbounded);a + b, a - b: the minimum of the factors;a * b: the product;a << c with a constant c: the factor of a times 2^c;let: the factor of its definition;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.
module C = Sarek_coopmat_typesCap of a factor: 2^30, standing for "divisible by every power of two that matters".
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 factor : env -> Sarek_ir_types.expr -> intval element_bytes : C.fragment -> intBytes 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 ->
boolval tile_bytes : C.fragment -> intBytes of the 16x16 staging tile of a fragment.
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.
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 -> boolval is_marked : Sarek_ir_types.stmt -> boolval proven_ops : Sarek_ir_types.stmt -> Sarek_ir_types.coopmat_op listThe proven loads and stores of a kernel body; all of them in the aligned variant (mark_aligned).
Whether op is one of the proven operations (physical identity).
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 predicate = atom list listDisjunction of conjunctions; [[]] is true, [] false.
val p_true : predicateval p_false : predicatetype binding = | Bound of Sarek_ir_types.expr * (string * binding) listan immutable let: its definition and the scope it was made in
| Opaquea loop, mutable or match variable: unknown
*)type symbolic_env = (string * binding) listval need :
params:string list ->
symbolic_env ->
Sarek_ir_types.expr ->
int ->
predicateval at_least :
params:string list ->
symbolic_env ->
Sarek_ir_types.expr ->
int ->
predicateThe 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 listThe scalar int32 parameters of a kernel.
val derive : Sarek_ir_types.kernel -> predicate optionThe 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 -> boolWhether p holds for the scalar arguments args (parameter name, value). A parameter without a value makes its atom false.
val mark_aligned : Sarek_ir_types.kernel -> Sarek_ir_types.kernelThe aligned variant of k: the same kernel with its body marked.
val mark_checked : Sarek_ir_types.kernel -> Sarek_ir_types.kernelThe kernel with every static proof switched off: the checked variant.
val arrays_accessed : Sarek_ir_types.kernel -> string listThe arrays the kernel's cooperative-matrix loads and stores read or write, in order of first appearance.