Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/omega-mv/blob/main/src/omega_blackwell_codegen.h # omega-mv Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance. Brought by drakestapleton Sign in to star 1 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `omega-mv / src / omega_blackwell_codegen.h` 392 lines ยท 25645 bytes Raw Blame History 1 /* SPDX-License-Identifier: Apache-2.0 2 * Copyright 2026 Drake Stapleton (AIEN / aien-dev) 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 #ifndef OMEGA_BLACKWELL_CODEGEN_H 18 #define OMEGA_BLACKWELL_CODEGEN_H 19 20 #include 21 #include 22 #include 23 #include "omega_blackwell_matmul.h" 24 25 #define BW_MAX_IR_INSNS 1024 /* cut K: the four-head hd 128 attention kernel is ~700 instructions */ 26 #define BW_MAX_VREGS 256 27 #define BW_MAX_UVREGS 32 28 #define BW_PHYS_GPR_START 2 /* Reserve R0, R1 */ 29 #define BW_PHYS_GPR_MAX 128 /* default allocation limit (R2..R127); a program may opt into more (omega_bw_ir_set_gpr_limit) */ 30 /* Highest limit a program may ask for: the chip allows 255 registers per thread and 64K per block (CUDA Programming 31 * Guide, Compute Capabilities appendix: "per thread is 255"); a peak of 246 declares 248 (peak + 2 rounded up to 8). 32 * The launcher must keep threads * declared <= 65536 (omega#545). */ 33 #define BW_PHYS_GPR_HW_LIMIT 246 34 #define BW_PHYS_UGPR_START 4 /* Reserve UR0-UR3 */ 35 #define BW_PHYS_UGPR_MAX 32 36 37 /* Minimal Blackwell IR Opcodes for Stage 1 & Stage 2 */ 38 typedef enum { 39 BW_IR_NOP = 0, 40 BW_IR_MOV_RZ, /* MOV Rd, RZ (zero initialization) */ 41 BW_IR_MOV_IMM, /* MOV Rd, imm32 */ 42 BW_IR_S2R, /* S2R Rd, SR */ 43 BW_IR_LDC, /* LDC Rd, c[bank][offset] */ 44 BW_IR_LDC64, /* LDC.64 Rd:Rd+1, c[bank][offset] */ 45 BW_IR_LDCU, /* LDCU URd, c[bank][offset] */ 46 BW_IR_LDCU64, /* LDCU.64 URd:URd+1, c[bank][offset] */ 47 BW_IR_IMAD, /* IMAD Rd, Ra, Rb/imm, Rc (multiply-add) */ 48 BW_IR_IMAD_WIDE, /* IMAD.WIDE.U32 Rd:Rd+1, Ra, imm, Rc:Rc+1 */ 49 BW_IR_ISETP_GE, /* ISETP.GE P0, PT, Ra, Rb/imm, PT */ 50 BW_IR_LDG_E, /* LDG.E Rd, desc[URd][Ra.64+imm] */ 51 BW_IR_STG_E, /* STG.E desc[URd][Ra.64], Rb */ 52 BW_IR_STG_EF, /* STG.E.EF: evict the line so the other side can see it */ 53 BW_IR_IADD3, /* IADD3 Rd, PT, PT, Ra, Rb, Rc */ 54 BW_IR_HMMA_F16, /* HMMA.16816.F32 Rd, Ra, Rb, Rc (FP16 input, FP32 accumulator) */ 55 BW_IR_HMMA_BF16, /* HMMA.16816.F32.BF16 Rd, Ra, Rb, Rc (BF16 input, FP32 accumulator) */ 56 BW_IR_SHF_R, /* SHF.R.U32.HI Rd, RZ, imm, Ra */ 57 BW_IR_LOP3_AND, /* LOP3.LUT Rd, Ra, imm, RZ, 0xc0, !PT (Bitwise AND) */ 58 BW_IR_LDG_E_U16, /* LDG.E.U16 Rd, desc[URd][Ra.64] */ 59 /* System-scope ordering. Encodings checked against the sm_121 disassembler. 60 * A host build of these opcodes is not a graphics-chip result. */ 61 BW_IR_LDG_STRONG_SYS, 62 BW_IR_LDG_MMIO, /* LDG.E.MMIO.GPU: do not keep a stale cached copy */ 63 BW_IR_STG_STRONG_SYS, 64 BW_IR_MEMBAR_ALL_SYS, 65 BW_IR_MEMBAR_SC_SYS, 66 BW_IR_CCTL_IVALL, 67 BW_IR_ATOMG_ADD_STRONG_SYS, 68 BW_IR_ATOMG_EXCH_STRONG_SYS, 69 BW_IR_ISETP_GE_U32, /* ISETP.GE.U32.AND P0, PT, Ra, Rb, PT */ 70 BW_IR_LOP3_XOR, /* LOP3.LUT Rd, Ra, Rb, RZ, 0x3c, !PT */ 71 BW_IR_FADD, /* FADD Rd, Ra, Rb */ 72 BW_IR_FSUB, /* FSUB Rd, Ra, Rb (FADD Rd, Ra, -Rb) */ 73 BW_IR_FMUL, /* FMUL Rd, Ra, Rb */ 74 BW_IR_FFMA, /* FFMA Rd, Ra, Rb, Rc (Rd = Ra * Rb + Rc) */ 75 BW_IR_FSETP, /* FSETP.cond.AND P0, PT, Ra, Rb, PT */ 76 BW_IR_FSEL, /* FSEL Rd, Ra, Rb, P0 */ 77 BW_IR_FMNMX_MIN, /* FMNMX Rd, Ra, Rb, PT (minimum) */ 78 BW_IR_FMNMX_MAX, /* FMNMX Rd, Ra, Rb, !PT (maximum) */ 79 BW_IR_I2FP, /* I2FP.F32.S32 Rd, Ra */ 80 BW_IR_F2I, /* F2I.TRUNC.NTZ Rd, Ra */ 81 BW_IR_MUFU_RCP, /* MUFU.RCP Rd, Ra */ 82 BW_IR_MUFU_RSQ, /* MUFU.RSQ Rd, Ra */ 83 BW_IR_SHFL_DOWN, /* SHFL.DOWN PT, Rd, Ra, offset, 0x1f */ 84 BW_IR_LDS, /* LDS Rd, [Ra] */ 85 BW_IR_STS, /* STS [Ra], Rb */ 86 BW_IR_EXIT, /* EXIT */ 87 BW_IR_BRA, /* BRA. imm = signed instruction delta; predicate_p0 / predicate_not select @P0 or @!P0 */ 88 /* FB-1 cut 4 (2026-10-04), additive. Encodings decoded with nvdisasm 13.0 -b SM121 89 * (tools/divsqrt_nvdisasm_check.sh method); the BAR/LDS/STS words are the ones 90 * src/omega_numeric.c:884-886 runs on the chip (LDS_STS op, 148/148 specs PASS). */ 91 BW_IR_MUFU_EX2, /* MUFU.EX2 Rd, Ra (2^x, approximate: PTX ex2.approx.f32 bound 2^-22.5 rel) */ 92 BW_IR_BAR_SYNC, /* BAR.SYNC.DEFER_BLOCKING 0x0 (CTA barrier 0; the QMD declares 1 barrier) */ 93 BW_IR_LDS32, /* LDS Rd, [Ra+URZ] 32-bit shared load (BW_IR_LDS encodes LDS.U8: byte) */ 94 BW_IR_STS32, /* STS [Ra+URZ], Rb 32-bit shared store (BW_IR_STS encodes STS.U8: byte) */ 95 /* Prime race cut (2026-10-05), additive, general integer ops. Words derived from the 96 * register forms of LOP3_XOR / SHF_R / IMAD and matched word for word against 97 * nvcc 13.0.88 -arch=sm_121 -cubin + cuobjdump -sass (offline oracle only; see 98 * omega_blackwell_verify_codegen_fixtures_intops). For these three ops an operand 99 * vreg of -1 encodes RZ (not R0). */ 100 BW_IR_LOP3_LUT, /* LOP3.LUT Rd, Ra, Rb, Rc, imm8, !PT: Rd = LUT(Ra, Rb, Rc). imm & 0xff is the 101 * truth table over Ra = 0xf0, Rb = 0xcc, Rc = 0xaa (a|b = 0xfc, ~(a|b) = 0x03) */ 102 BW_IR_SHF_L_U32, /* SHF.L.U32 Rd, Ra, Rb, RZ: Rd = Ra << Rb, register amount (the form nvcc emits 103 * for PTX shl.b32). Amounts >= 32 are NOT chip-verified: callers keep Rb < 32 */ 104 BW_IR_IMAD_HI_U32, /* IMAD.HI.U32 Rd, Ra, Rb, Rc: Rd = hi32(Ra * Rb + Rc+1:Rc), low word Rc. Rc is a 64-bit register 105 * pair, NOT one 32-bit word: nvcc 13.0 zeroes the upper half before using a 32-bit c, puts 106 * __umulhi(a, b) + c through the pair {lo 0, hi c}, and emits only even pair bases (R2, R4, UR4 107 * in evidence/IMADHI-RC-ORACLE-nvcc13.0-`20261016`). Compiler reading, not chip-probed (omega #492). 108 * The warp simulators model an even Rc as that pair and refuse an odd Rc (no evidence). 109 * hi32(Ra * Rb) + c32 is NOT this instruction with Rc = c32: use RZ then IADD3, or SHF.R + IADD3 110 * (the PR #487 odd-column bug, tests/gpu_imadhi_rc_test.c). Rc = RZ: src3_vreg = -1 */ 111 /* Structured warp reconvergence (omega #308, 2026-10-05), additive. Words matched against 112 * nvcc 13.0.88 -arch=sm_121 output (BSSY.RECONVERGENT / BSYNC.RECONVERGENT; the oracle 113 * kernels are listed in docs/gpu-reconvergence-308.md) and the Mesa NAK encoder 114 * (sm70_encode.rs OpBSSy 0x945 / OpBSync 0x941, barrier register at bits 16..19). 115 * bar_reg selects B0..B15. BSSY arms the barrier for the lanes executing it and names 116 * the join point; BSYNC waits for every armed lane that has not exited, then the warp 117 * continues as one. imm of BSSY = signed instruction delta to the join point (the 118 * instruction after the BSYNC), like BW_IR_BRA; the encoder refuses deltas < 1. */ 119 BW_IR_BSSY, 120 BW_IR_BSYNC, 121 /* Fragment-major matmul (omega #328, 2026-10-07), additive. Words matched against nvcc 13.0.88 122 * -arch=sm_121 (omega_blackwell_verify_codegen_fixtures_fragmm). imm = signed byte offset, 123 * -2^23 .. 2^23-1; dst is a register pair (64) or an aligned quad (128). */ 124 BW_IR_LDG_E_64, /* LDG.E.64 Rd:Rd+1, desc[URd][Ra.64+imm] */ 125 BW_IR_LDG_E_128, /* LDG.E.128 Rd..Rd+3, desc[URd][Ra.64+imm] */ 126 /* Batched attention loads (2026-10-12). Words matched against nvcc 13.0.88 -arch=sm_121 127 * (omega_blackwell_verify_codegen_fixtures_fragmm). imm = unsigned byte offset < 2^23; dst an aligned quad. */ 128 BW_IR_LDS128, /* LDS.128 Rd..Rd+3, [Ra+imm] */ 129 /* Gated DeltaNet softplus (2026-10-18, omega#542): sub-op 0x0c00 checked with nvdisasm 13.0 -b SM121, which decodes 130 * "MUFU.LG2 R2, R4" (EX2 0x0800, RCP 0x1000, RSQ 0x1400 decode the same way). log2(x), approximate. */ 131 BW_IR_MUFU_LG2, /* MUFU.LG2 Rd, Ra */ 132 /* Training R5 (2026-10-15), additive (numbers 60..63 after MUFU_LG2, pinned for om/encoder/bw_enc_sm121.om): native 133 * entries for the two instructions the prefill kernels used to emit as placeholders patched after encoding. 134 * Words: omega_blackwell_verify_codegen_fixtures_bfly_f2fp. */ 135 BW_IR_SHFL_BFLY, /* SHFL.BFLY PT, Rd, Ra, imm & 31, 0x1f: Rd = Ra of lane (lane ^ imm) */ 136 BW_IR_F2FP_BF16_PACK, /* F2FP.BF16.F32.PACK_AB Rd, Ra, Rb: Rd = bf16_rn(Ra) << 16 | bf16_rn(Rb) (fixed latency) */ 137 /* Training R5 part 2 (2026-10-15), additive: f32 global atomic add, the float twins of BW_IR_ATOMG_ADD_STRONG_SYS. 138 * Flush-to-zero like every f32 global atomic add (CUDA Programming Guide: "equivalent to PTX add.rn.ftz.f32"). 139 * dst_vreg < 0 encodes RZ: the add happens and the old value is dropped (no register is written). 140 * Two scopes, same arithmetic: 141 * .STRONG.SYS for a word the CPU (or another agent) may watch while the kernel runs: host-visible memory. 142 * .STRONG.GPU for a word only this GPU touches during the kernel and reads in a later launch, e.g. a gradient 143 * accumulated in device memory. This is the scope nvcc gives atomicAdd. 144 * nvcc's no-return form is REDG; Mesa's sm90 float REDG layout (opcode 0x9a6) does not decode on SM121 in any variant 145 * tried, so ATOMG with RZ is the no-return form here. Words: omega_blackwell_verify_codegen_fixtures_atomf32; 146 * chip probe: tests/gpu_atomf32_probe_test.c. */ 147 BW_IR_ATOMG_ADD_F32_STRONG_SYS, /* ATOMG.E.ADD.F32.FTZ.RN.STRONG.SYS PT, Rd|RZ, desc[UR][Ra.64], Rb: Rd = old, mem += Rb */ 148 BW_IR_ATOMG_ADD_F32_STRONG_GPU, /* ATOMG.E.ADD.F32.FTZ.RN.STRONG.GPU PT, Rd|RZ, desc[UR][Ra.64], Rb: Rd = old, mem += Rb */ 149 } BlackwellIROpcode; 150 151 /* Barrier (reconvergence) registers: the sm_121 BSSY/BSYNC words carry a 4-bit barrier id 152 * (NAK sm70_encode.rs set_bar_dst(16..20)), so B0..B15 are encodable. How many the chip 153 * provides is not documented to us; chip tests nest to BW_RECONV_CHIP_TESTED_DEPTH. */ 154 #define BW_RECONV_MAX_BAR 16 155 156 /* Special Register Identifiers */ 157 typedef enum { 158 BW_SR_TID_X = 0x21, 159 BW_SR_TID_Y = 0x22, 160 BW_SR_CTAID_X = 0x25, 161 BW_SR_CTAID_Y = 0x26 162 } BlackwellSR; 163 164 /* IR Instruction Node */ 165 typedef struct { 166 BlackwellIROpcode op; 167 int dst_vreg; /* Virtual destination register (-1 if none) */ 168 int src1_vreg; /* Virtual source 1 register (-1 if none) */ 169 int src2_vreg; /* Virtual source 2 register (-1 if none / immediate mode) */ 170 int src3_vreg; /* Virtual source 3 register (-1 if none / RZ) */ 171 int ureg; /* Uniform register (-1 if none) */ 172 uint32_t imm; /* Immediate value / constant bank offset / SR code. 173 * For BW_IR_BRA: signed instruction delta (target minus 174 * this instruction). 0 branches to itself. */ 175 uint32_t control; /* Bundle control word */ 176 bool is_uniform; /* True if targets/uses uniform registers */ 177 bool predicate_p0; /* True if predicated on P0 */ 178 bool predicate_not; /* With predicate_p0, encode @!P0 */ 179 uint8_t dst_subreg; /* Subregister offset (0..3) within register bundle */ 180 uint8_t src1_subreg; 181 uint8_t src2_subreg; 182 uint8_t src3_subreg; 183 uint8_t bar_reg; /* BW_IR_BSSY / BW_IR_BSYNC: barrier register B0..B15 */ 184 } BlackwellIRInsn; 185 186 /* Live Interval for Bounded Linear Register Allocation */ 187 typedef struct { 188 int vreg; 189 int first_def; 190 int last_use; 191 int phys_reg; 192 uint32_t bundle_size; /* 1: scalar (32-bit), 2: pair (64-bit), 4: quad (128-bit) */ 193 bool is_pair; /* True if 64-bit register pair (requires even-aligned R_2k) */ 194 bool is_quad; /* True if 128-bit register quad (requires 4-aligned R_4k) */ 195 bool active; 196 } OmegaLiveInterval; 197 198 /* Register Allocator State */ 199 typedef struct { 200 int num_vregs; 201 int num_uvregs; 202 OmegaLiveInterval intervals[BW_MAX_VREGS]; 203 OmegaLiveInterval uintervals[BW_MAX_UVREGS]; 204 int vreg_to_phys[BW_MAX_VREGS]; 205 int uvreg_to_phys[BW_MAX_UVREGS]; 206 uint32_t peak_gpr_usage; 207 uint32_t peak_ugpr_usage; 208 uint32_t gpr_limit; /* 0: BW_PHYS_GPR_MAX; else the exclusive physical limit, <= BW_PHYS_GPR_HW_LIMIT */ 209 } OmegaRegAlloc; 210 211 /* IR Program */ 212 typedef struct { 213 BlackwellIRInsn insns[BW_MAX_IR_INSNS]; 214 size_t count; 215 OmegaRegAlloc regalloc; 216 } BlackwellIRProgram; 217 218 /* Initializes IR program */ 219 void omega_bw_ir_init(BlackwellIRProgram *prog); 220 221 /* Appends an instruction node to IR program */ 222 int omega_bw_ir_append(BlackwellIRProgram *prog, const BlackwellIRInsn *insn); 223 224 /* Allocates a new 32-bit virtual general-purpose register */ 225 int omega_bw_ir_alloc_vreg(BlackwellIRProgram *prog); 226 227 /* Allocates a new 64-bit aligned virtual register pair (for 64-bit memory addresses or HMMA B matrix) */ 228 int omega_bw_ir_alloc_vreg64(BlackwellIRProgram *prog); 229 230 /* Allocates a new 128-bit aligned virtual register quad (for HMMA accumulator and A matrix) */ 231 int omega_bw_ir_alloc_vreg128(BlackwellIRProgram *prog); 232 233 /* Allocates a new 32-bit virtual uniform register */ 234 int omega_bw_ir_alloc_uvreg(BlackwellIRProgram *prog); 235 236 /* Allocates a new 64-bit aligned virtual uniform register pair */ 237 int omega_bw_ir_alloc_uvreg64(BlackwellIRProgram *prog); 238 239 /* Solves bounded deterministic live-interval register allocation */ 240 int omega_bw_regalloc_solve(BlackwellIRProgram *prog); 241 /* Opt this program into physical registers up to limit - 1 (BW_PHYS_GPR_MAX < limit <= BW_PHYS_GPR_HW_LIMIT); call after 242 * omega_bw_ir_init. Every other program keeps the default, so its allocation is unchanged. 0 ok, -1 out of range. */ 243 int omega_bw_ir_set_gpr_limit(BlackwellIRProgram *prog, uint32_t limit); 244 245 /* Encodes scheduled IR program into 128-bit sm_121 machine code */ 246 int omega_bw_encode_program(const BlackwellIRProgram *prog, uint8_t *code_buf, size_t max_len, size_t *out_len); 247 /* C6 kernel capture: writes code to $OMEGA_KERNEL_DUMP_DIR/.bin when that is set; no-op otherwise (see the .c) */ 248 void omega_kernel_dump(const uint8_t *code, size_t len); 249 250 /* ---- native v1 P07 cut A.2: allocation-free code generation (aienos#286) ---------------------------- 251 * The freestanding build (OMEGA_GPU_NO_POSIX, make gpu-engine-freestanding) has no allocator: the caller 252 * supplies the code buffer and its capacity. Build the program with one of the *_prog generators, then 253 * encode it with omega_blackwell_kernel_encode_into. The hosted allocating generators below encode through 254 * the same function, so both builds emit the same bytes (make test-codegen-freestanding). 255 * 256 * omega_bw_program_code_bytes: the exact bytes the encoder writes for prog (padded to 8 instructions, 257 * at least 32), 0 for NULL. 258 * omega_blackwell_kernel_encode_into: zeroes *kernel, then encodes prog into code_buf. Refuses with 259 * OMEGA_BW_CODE_BUFFER_TOO_SMALL, writing no byte of code_buf, when code_cap is below 260 * omega_bw_program_code_bytes(prog); -1 for NULL arguments or an instruction the encoder rejects. On 261 * success kernel->code points INTO the caller's buffer: the kernel owns nothing, the caller keeps 262 * code_buf alive as long as the kernel is used and never passes such a kernel to 263 * omega_blackwell_kernel_free (which exists only in the hosted build). */ 264 #define OMEGA_BW_CODE_BUFFER_TOO_SMALL (-2) 265 #define OMEGA_BW_CODE_CAP_SMALL 4096u /* cap of the tensor, tensor_loop and frag generators */ 266 #define OMEGA_BW_CODE_CAP_MAX ((size_t)BW_MAX_IR_INSNS * 16u) /* cap of frag_block; no program encodes past it */ 267 size_t omega_bw_program_code_bytes(const BlackwellIRProgram *prog); 268 int omega_blackwell_kernel_encode_into(const BlackwellIRProgram *prog, uint8_t *code_buf, size_t code_cap, 269 OmegaBlackwellKernel *kernel); 270 /* The integer generator's program (K <= 32); the allocating omega_blackwell_codegen_matmul_i32 encodes it. */ 271 int omega_blackwell_codegen_matmul_i32_prog(const OmegaMatMulSpec *spec, BlackwellIRProgram *prog); 272 273 /* The allocating generators (omega_blackwell_codegen_matmul_i32, _tensor, _tensor_loop, _frag, _frag_block), 274 * omega_blackwell_codegen_matmul, omega_blackwell_kernel_free and omega_blackwell_test_codegen_variation exist 275 * only in the hosted build. */ 276 277 /* Top-level dynamic code generator for integer matrix multiplication */ 278 int omega_blackwell_codegen_matmul_i32(const OmegaMatMulSpec *spec, OmegaBlackwellKernel *kernel); 279 280 /* Dynamic sm_121 Tensor Core code generator (FP16 & BF16 via HMMA.16816) */ 281 int omega_blackwell_codegen_matmul_tensor_prog(const OmegaMatMulSpec *spec, BlackwellIRProgram *prog); 282 int omega_blackwell_codegen_matmul_tensor(const OmegaMatMulSpec *spec, OmegaBlackwellKernel *kernel); 283 284 /* FB-1 cut 1b: looped Tensor Core matmul. One warp per CTA. CTA (x, y) owns row 285 * tile y (16 rows) and column tiles x, x + grid_x, x + 2 grid_x, ... (8 columns 286 * each); the whole K dimension is accumulated in the HMMA accumulator on the 287 * chip (no host K slicing). Requires M % 16 == 0, N % 8 == 0, K % 16 == 0 and 288 * 1 <= grid_x <= N / 8. The kernel does not depend on M (rows come from 289 * CTAID.Y), so one kernel serves every row count of a (K, N, grid_x) triple. 290 * Uses only instructions already present in the chip-proven single-tile kernel 291 * plus the predicated backward BRA and ISETP.GE this IR already encodes. 292 * mutant != 0 drops the last K step (test oracle: the wrong kernel must be 293 * caught by the parity test; never set in production). */ 294 int omega_blackwell_codegen_matmul_tensor_loop_prog(const OmegaMatMulSpec *spec, uint32_t grid_x, 295 int mutant, BlackwellIRProgram *prog); 296 int omega_blackwell_codegen_matmul_tensor_loop(const OmegaMatMulSpec *spec, uint32_t grid_x, 297 int mutant, OmegaBlackwellKernel *kernel); 298 299 /* omega #328: fragment-major tensor matmul. Same grid, C layout (row-major f32) and argument 300 * words as the looped kernel; A and B are in fragment order instead of row-major: 301 * A (mp x kp, from omega_matmul_frag_pack_a): block (mt, ks) of 256 bf16 at ((mt*KS + ks)*32 302 * + lane)*8, lane = group*4 + tig, holding A[r][c], A[r][c+1], A[r+8][c], A[r+8][c+1], 303 * A[r][c+8], A[r][c+9], A[r+8][c+8], A[r+8][c+9] with r = 16mt + group, c = 16ks + 2tig; 304 * B (kp x np, from omega_matmul_frag_pack_b): block (nt, ks) of 128 bf16 at ((nt*KS + ks)*32 305 * + lane)*4 holding B[k][j], B[k+1][j], B[k+8][j], B[k+9][j] with k = 16ks + 2tig, 306 * j = 8nt + group; KS = kp / 16, entries outside the real matrix are zero. 307 * One LDG.E.128 (A) and one LDG.E.64 (B) per K step; `unroll` (1, 2, 4 or 8, dividing KS) 308 * steps of loads are issued before the HMMA chain. mutant != 0 drops the last pass (test 309 * oracle only). Each tile occupies 16*kp (A) and 8*kp (B) elements, so a row-tile offset into 310 * A is the same in bytes as for row-major A. */ 311 int omega_blackwell_codegen_matmul_frag_prog(const OmegaMatMulSpec *spec, uint32_t grid_x, uint32_t unroll, 312 int mutant, BlackwellIRProgram *prog); 313 int omega_blackwell_codegen_matmul_frag(const OmegaMatMulSpec *spec, uint32_t grid_x, uint32_t unroll, 314 int mutant, OmegaBlackwellKernel *kernel); 315 /* The largest of 4, 2, 1 that divides kp / 16 (unroll 8 is generated for experiments only: it 316 * did not complete on the GB10, see the definition). */ 317 uint32_t omega_matmul_frag_unroll(uint32_t kp); 318 /* 2026-10-12: blocked fragment kernel for prefill rows. One warp owns rt row tiles x ct column tiles 319 * (rt in 1, 2, 4, 8; ct in 1, 2, 4; 2 <= rt*ct <= 16; unroll 1 or 2 with unroll*(rt+ct) <= 8; nt % ct == 0; 320 * grid_x <= nt/ct). Same packing, argument words and C layout as the single-tile kernel; a CTA's 321 * row block is rt row tiles (grid_y = row tiles / rt). mutant != 0 drops the last K pass. */ 322 int omega_blackwell_codegen_matmul_frag_block_prog(const OmegaMatMulSpec *spec, uint32_t grid_x, uint32_t unroll, 323 uint32_t rt, uint32_t ct, int mutant, BlackwellIRProgram *prog); 324 int omega_blackwell_codegen_matmul_frag_block(const OmegaMatMulSpec *spec, uint32_t grid_x, uint32_t unroll, 325 uint32_t rt, uint32_t ct, int mutant, OmegaBlackwellKernel *kernel); 326 /* rt, ct for mt row tiles and nt column tiles (1, 1 = the single-tile kernel), and the unroll the API uses */ 327 void omega_matmul_frag_block_shape(uint32_t mt, uint32_t nt, uint32_t *rt, uint32_t *ct); 328 uint32_t omega_matmul_frag_block_unroll(uint32_t kp, uint32_t rt, uint32_t ct); 329 330 /* Registers to declare in the QMD for a kernel whose allocator peak (OmegaLiveInterval count, regalloc.peak_gpr_usage) 331 * is `peak`: peak + 2, rounded up to a multiple of 8 (the session raises anything below 64 to 64). 332 * Chip evidence (OWN-30B, 2026-10-13): the fp8 grouped GEMM (lane 8, 8 rows) with peak 103 declared 104 (= peak + 1 333 * rounded up, the earlier rule) hung the chip in 7 of 7 runs; the same program declared 112 ran every gate. Earlier 334 * evidence: peak 64 declared 64 faulted (Xid 13 Out Of Range Register). Declared >= peak + 2 has run everywhere else 335 * (router peak 46 declared 48). The exact minimum is UNKNOWN (declarations are multiples of 8); the rule is INFERRED. */ 336 uint32_t omega_bw_gpr_declared(uint32_t peak); 337 /* 1 iff `declared` is a legal declaration for `peak`: a multiple of 8 and >= peak + 2. Launchers refuse otherwise. */ 338 int omega_bw_gpr_declared_ok(uint32_t declared, uint32_t peak); 339 /* a: m x k row-major. dst: mp x kp elements, fully written (zero padding). */ 340 void omega_matmul_frag_pack_a(const uint16_t *a, uint32_t m, uint32_t k, uint32_t mp, uint32_t kp, uint16_t *dst); 341 /* the same from f32 rows, converted to bf16 as omega_fp32_to_bf16 does (one pass, no staging copy) */ 342 void omega_matmul_frag_pack_a_f32(const float *a, uint32_t m, uint32_t k, uint32_t mp, uint32_t kp, uint16_t *dst); 343 /* Row 0 of a one-row call into a buffer whose rows 1..15 already hold zeros for this kp (every column, 344 * zeros past k); the caller tracks that the zeros are there (omega_gpu_matmul_api.c, m = 1 path). */ 345 void omega_matmul_frag_pack_a_row0(const uint16_t *a, uint32_t k, uint32_t kp, uint16_t *dst); 346 void omega_matmul_frag_pack_a_row0_f32(const float *a, uint32_t k, uint32_t kp, uint16_t *dst); 347 /* Result sentinels (host side of the poison check): one word per 16 x 8 fragment tile that holds a real 348 * output word, at the tile's last real row and column, in the row-major m x np result. set != 0 writes 349 * poison into every sentinel and returns their count; set == 0 returns how many still hold poison. The 350 * kernel's epilogue stores a fragment tile as one straight-line run of STG.E words, so a tile whose 351 * sentinel survived was never stored. */ 352 size_t omega_matmul_result_sentinels(float *cd, uint32_t m, uint32_t n, uint32_t np, float poison, int set); 353 /* b: k x n row-major. dst: the column tiles [nt0, nt1), (nt1 - nt0) * kp * 8 elements, fully 354 * written; it is the part of the packed tensor starting at element nt0 * kp * 8. */ 355 void omega_matmul_frag_pack_b(const uint16_t *b, uint32_t k, uint32_t n, uint32_t kp, 356 uint32_t nt0, uint32_t nt1, uint16_t *dst); 357 358 /* Unit test for instruction encoding bitfield fixtures (Gate 3) */ 359 int omega_blackwell_verify_codegen_fixtures(void); 360 361 /* FB-1 cut 4: fixtures for MUFU_EX2, BAR_SYNC, LDS32, STS32 (0 = pass) */ 362 int omega_blackwell_verify_codegen_fixtures_fb1cut4(void); 363 364 /* Prime race cut: golden words for LOP3_LUT, SHF_L_U32, IMAD_HI_U32 (0 = pass) */ 365 int omega_blackwell_verify_codegen_fixtures_intops(void); 366 367 /* omega #328: golden words for LDG.E.64 / LDG.E.128 with offsets and the timed NOP (0 = pass) */ 368 int omega_blackwell_verify_codegen_fixtures_fragmm(void); 369 370 /* omega #308: golden words for BSSY / BSYNC (nvcc 13.0.88 sm_121 oracle), plus the 371 * encoder's refusals (negative delta, barrier id > 15). 0 = pass. */ 372 int omega_blackwell_verify_codegen_fixtures_reconv(void); 373 374 /* Training R5: golden words for SHFL.BFLY and F2FP.BF16.F32.PACK_AB (nvcc 13.0.88 sm_121 oracle sealed in 375 * evidence/OWN30B-T3-TC-PEAK-BW-`5faa0f70`-20261013T2236Z), plus SHFL.BFLY = SHFL.DOWN with bit 58 for every lane. 0 = pass. */ 376 int omega_blackwell_verify_codegen_fixtures_bfly_f2fp(void); 377 378 /* Training R5 part 2: golden words for ATOMG.E.ADD.F32.FTZ.RN.STRONG.SYS and .STRONG.GPU, with and without a destination 379 * (decoded with nvdisasm -b SM121; no nvcc). 0 = pass. */ 380 int omega_blackwell_verify_codegen_fixtures_atomf32(void); 381 382 /* omega #308: encodes one instruction with a given physical register map of 383 * identity (vreg n -> Rn) so offline tools can disassemble single words. 0 = ok. */ 384 int omega_blackwell_encode_one(const BlackwellIRInsn *insn, uint32_t w[4]); 385 386 /* Unit test for bounded register allocation live intervals and bounds enforcement (Gate 4) */ 387 int omega_blackwell_test_regalloc_bounds(void); 388 389 /* Stage-1 dynamic codegen variation demonstration between 16x16x16 and 32x16x64 (Gate 7) */ 390 int omega_blackwell_test_codegen_variation(void); 391 392 #endif /* OMEGA_BLACKWELL_CODEGEN_H */