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/CUDApp/blob/main/src/omega_blackwell_codegen.h # CUDApp CUDA++: AIEN’s GPU machine-code toolchain, runtime and driver client for NVIDIA GPUs without the CUDA toolkit. Hardware coverage and driver replacement limits are explicit. Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `CUDApp / src / omega_blackwell_codegen.h` 421 lines · 27486 bytes Raw Blame History 1 /* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 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 /* Omega NG kernel traps (2026-11-07, Hive NG D10): one launch-wide trap word holds the lowest 150 * (linear thread id << 4 | trap code); a trapping thread folds its key in with an unsigned 64-bit atomic min. 151 * Same layout as ATOMG_ADD_STRONG_SYS with w2 op bits 23..26 = 1 (MIN) and size bits 9..11 = 2 (.64, unsigned; 152 * 3 decodes .S64). src2_vreg and dst_vreg are 64-bit pairs; dst_vreg < 0 encodes RZ (no old value returned). 153 * Words: om/encoder/atomg_min_test.om, make test-om-encoder-atomg (nvdisasm 13.0.85 --binary SM121). */ 154 BW_IR_ATOMG_MIN_U64_STRONG_SYS, /* ATOMG.E.MIN.64.STRONG.SYS PT, Rd|RZ, desc[UR][Ra.64], Rb: Rd = old, mem = min(mem, Rb) */ 155 /* C1 kernel conversions, append-only: preserve main op 64; conversions occupy 65..68. */ 156 BW_IR_I2FP_U32, /* u32 -> f32, round to nearest even, fixed latency */ 157 BW_IR_F2I_U32, /* f32 -> u32, toward zero, saturating, NaN -> 0; variable latency */ 158 /* Scalar half conversions: low f16 lane, f32 register; round-nearest-even. */ 159 BW_IR_F16_TO_F32, /* HADD2.F32 Rd, -RZ, Ra.H0_H0; fixed latency */ 160 BW_IR_F32_TO_F16, /* F2F.F16.F32 Rd, Ra; upper half cleared; variable latency */ 161 /* IEEE binary64 register forms, append-only (69..79). Use alloc_vreg64. 162 * MUFU.*64H alone reads/writes one high word: select subreg 1 explicitly; 163 * it neither reads nor clears the low word. DSETP uses FSETP's imm modes/P0. 164 * F64 arithmetic and F2F narrowing round nearest even; F2I truncates and 165 * saturates (f64 NaN -> INT32_MIN), matching PTX cvt.rzi.s32.f64. */ 166 BW_IR_DADD, 167 BW_IR_DMUL, 168 BW_IR_DFMA, 169 BW_IR_DSETP, 170 BW_IR_MUFU_RCP64H, 171 BW_IR_MUFU_RSQ64H, 172 BW_IR_F64_F32, 173 BW_IR_F32_F64, 174 BW_IR_F64_S32, 175 BW_IR_F64_U32, 176 BW_IR_S32_F64, 177 BW_IR_LAST = BW_IR_S32_F64 178 } BlackwellIROpcode; 179 180 /* Barrier (reconvergence) registers: the sm_121 BSSY/BSYNC words carry a 4-bit barrier id 181 * (NAK sm70_encode.rs set_bar_dst(16..20)), so B0..B15 are encodable. How many the chip 182 * provides is not documented to us; chip tests nest to BW_RECONV_CHIP_TESTED_DEPTH. */ 183 #define BW_RECONV_MAX_BAR 16 184 185 /* Special Register Identifiers */ 186 typedef enum { 187 BW_SR_TID_X = 0x21, 188 BW_SR_TID_Y = 0x22, 189 BW_SR_CTAID_X = 0x25, 190 BW_SR_CTAID_Y = 0x26 191 } BlackwellSR; 192 193 /* IR Instruction Node */ 194 typedef struct { 195 BlackwellIROpcode op; 196 int dst_vreg; /* Virtual destination register (-1 if none) */ 197 int src1_vreg; /* Virtual source 1 register (-1 if none) */ 198 int src2_vreg; /* Virtual source 2 register (-1 if none / immediate mode) */ 199 int src3_vreg; /* Virtual source 3 register (-1 if none / RZ) */ 200 int ureg; /* Uniform register (-1 if none) */ 201 uint32_t imm; /* Immediate value / constant bank offset / SR code. 202 * For BW_IR_BRA: signed instruction delta (target minus 203 * this instruction). 0 branches to itself. */ 204 uint32_t control; /* Bundle control word */ 205 bool is_uniform; /* True if targets/uses uniform registers */ 206 bool predicate_p0; /* True if predicated on P0 */ 207 bool predicate_not; /* With predicate_p0, encode @!P0 */ 208 uint8_t dst_subreg; /* Subregister offset (0..3) within register bundle */ 209 uint8_t src1_subreg; 210 uint8_t src2_subreg; 211 uint8_t src3_subreg; 212 uint8_t bar_reg; /* BW_IR_BSSY / BW_IR_BSYNC: barrier register B0..B15 */ 213 } BlackwellIRInsn; 214 215 /* Live Interval for Bounded Linear Register Allocation */ 216 typedef struct { 217 int vreg; 218 int first_def; 219 int last_use; 220 int phys_reg; 221 uint32_t bundle_size; /* 1: scalar (32-bit), 2: pair (64-bit), 4: quad (128-bit) */ 222 bool is_pair; /* True if 64-bit register pair (requires even-aligned R_2k) */ 223 bool is_quad; /* True if 128-bit register quad (requires 4-aligned R_4k) */ 224 bool active; 225 } OmegaLiveInterval; 226 227 /* Register Allocator State */ 228 typedef struct { 229 int num_vregs; 230 int num_uvregs; 231 OmegaLiveInterval intervals[BW_MAX_VREGS]; 232 OmegaLiveInterval uintervals[BW_MAX_UVREGS]; 233 int vreg_to_phys[BW_MAX_VREGS]; 234 int uvreg_to_phys[BW_MAX_UVREGS]; 235 uint32_t peak_gpr_usage; 236 uint32_t peak_ugpr_usage; 237 uint32_t gpr_limit; /* 0: BW_PHYS_GPR_MAX; else the exclusive physical limit, <= BW_PHYS_GPR_HW_LIMIT */ 238 } OmegaRegAlloc; 239 240 /* IR Program */ 241 typedef struct { 242 BlackwellIRInsn insns[BW_MAX_IR_INSNS]; 243 size_t count; 244 OmegaRegAlloc regalloc; 245 } BlackwellIRProgram; 246 247 /* Initializes IR program */ 248 void omega_bw_ir_init(BlackwellIRProgram *prog); 249 250 /* Appends an instruction node to IR program */ 251 int omega_bw_ir_append(BlackwellIRProgram *prog, const BlackwellIRInsn *insn); 252 253 /* Allocates a new 32-bit virtual general-purpose register */ 254 int omega_bw_ir_alloc_vreg(BlackwellIRProgram *prog); 255 256 /* Allocates a new 64-bit aligned virtual register pair (for 64-bit memory addresses or HMMA B matrix) */ 257 int omega_bw_ir_alloc_vreg64(BlackwellIRProgram *prog); 258 259 /* Allocates a new 128-bit aligned virtual register quad (for HMMA accumulator and A matrix) */ 260 int omega_bw_ir_alloc_vreg128(BlackwellIRProgram *prog); 261 262 /* Allocates a new 32-bit virtual uniform register */ 263 int omega_bw_ir_alloc_uvreg(BlackwellIRProgram *prog); 264 265 /* Allocates a new 64-bit aligned virtual uniform register pair */ 266 int omega_bw_ir_alloc_uvreg64(BlackwellIRProgram *prog); 267 268 /* Solves bounded deterministic live-interval register allocation */ 269 int omega_bw_regalloc_solve(BlackwellIRProgram *prog); 270 /* Opt this program into physical registers up to limit - 1 (BW_PHYS_GPR_MAX < limit <= BW_PHYS_GPR_HW_LIMIT); call after 271 * omega_bw_ir_init. Every other program keeps the default, so its allocation is unchanged. 0 ok, -1 out of range. */ 272 int omega_bw_ir_set_gpr_limit(BlackwellIRProgram *prog, uint32_t limit); 273 274 /* Encodes scheduled IR program into 128-bit sm_121 machine code */ 275 int omega_bw_encode_program(const BlackwellIRProgram *prog, uint8_t *code_buf, size_t max_len, size_t *out_len); 276 /* C6 kernel capture: writes code to $OMEGA_KERNEL_DUMP_DIR/.bin when that is set; no-op otherwise (see the .c) */ 277 void omega_kernel_dump(const uint8_t *code, size_t len); 278 279 /* ---- native v1 P07 cut A.2: allocation-free code generation (aienos#286) ---------------------------- 280 * The freestanding build (OMEGA_GPU_NO_POSIX, make gpu-engine-freestanding) has no allocator: the caller 281 * supplies the code buffer and its capacity. Build the program with one of the *_prog generators, then 282 * encode it with omega_blackwell_kernel_encode_into. The hosted allocating generators below encode through 283 * the same function, so both builds emit the same bytes (make test-codegen-freestanding). 284 * 285 * omega_bw_program_code_bytes: the exact bytes the encoder writes for prog (padded to 8 instructions, 286 * at least 32), 0 for NULL. 287 * omega_blackwell_kernel_encode_into: zeroes *kernel, then encodes prog into code_buf. Refuses with 288 * OMEGA_BW_CODE_BUFFER_TOO_SMALL, writing no byte of code_buf, when code_cap is below 289 * omega_bw_program_code_bytes(prog); -1 for NULL arguments or an instruction the encoder rejects. On 290 * success kernel->code points INTO the caller's buffer: the kernel owns nothing, the caller keeps 291 * code_buf alive as long as the kernel is used and never passes such a kernel to 292 * omega_blackwell_kernel_free (which exists only in the hosted build). */ 293 #define OMEGA_BW_CODE_BUFFER_TOO_SMALL (-2) 294 #define OMEGA_BW_CODE_CAP_SMALL 4096u /* cap of the tensor, tensor_loop and frag generators */ 295 #define OMEGA_BW_CODE_CAP_MAX ((size_t)BW_MAX_IR_INSNS * 16u) /* cap of frag_block; no program encodes past it */ 296 size_t omega_bw_program_code_bytes(const BlackwellIRProgram *prog); 297 int omega_blackwell_kernel_encode_into(const BlackwellIRProgram *prog, uint8_t *code_buf, size_t code_cap, 298 OmegaBlackwellKernel *kernel); 299 /* The integer generator's program (K <= 32); the allocating omega_blackwell_codegen_matmul_i32 encodes it. */ 300 int omega_blackwell_codegen_matmul_i32_prog(const OmegaMatMulSpec *spec, BlackwellIRProgram *prog); 301 302 /* The allocating generators (omega_blackwell_codegen_matmul_i32, _tensor, _tensor_loop, _frag, _frag_block), 303 * omega_blackwell_codegen_matmul, omega_blackwell_kernel_free and omega_blackwell_test_codegen_variation exist 304 * only in the hosted build. */ 305 306 /* Top-level dynamic code generator for integer matrix multiplication */ 307 int omega_blackwell_codegen_matmul_i32(const OmegaMatMulSpec *spec, OmegaBlackwellKernel *kernel); 308 309 /* Dynamic sm_121 Tensor Core code generator (FP16 & BF16 via HMMA.16816) */ 310 int omega_blackwell_codegen_matmul_tensor_prog(const OmegaMatMulSpec *spec, BlackwellIRProgram *prog); 311 int omega_blackwell_codegen_matmul_tensor(const OmegaMatMulSpec *spec, OmegaBlackwellKernel *kernel); 312 313 /* FB-1 cut 1b: looped Tensor Core matmul. One warp per CTA. CTA (x, y) owns row 314 * tile y (16 rows) and column tiles x, x + grid_x, x + 2 grid_x, ... (8 columns 315 * each); the whole K dimension is accumulated in the HMMA accumulator on the 316 * chip (no host K slicing). Requires M % 16 == 0, N % 8 == 0, K % 16 == 0 and 317 * 1 <= grid_x <= N / 8. The kernel does not depend on M (rows come from 318 * CTAID.Y), so one kernel serves every row count of a (K, N, grid_x) triple. 319 * Uses only instructions already present in the chip-proven single-tile kernel 320 * plus the predicated backward BRA and ISETP.GE this IR already encodes. 321 * mutant != 0 drops the last K step (test oracle: the wrong kernel must be 322 * caught by the parity test; never set in production). */ 323 int omega_blackwell_codegen_matmul_tensor_loop_prog(const OmegaMatMulSpec *spec, uint32_t grid_x, 324 int mutant, BlackwellIRProgram *prog); 325 int omega_blackwell_codegen_matmul_tensor_loop(const OmegaMatMulSpec *spec, uint32_t grid_x, 326 int mutant, OmegaBlackwellKernel *kernel); 327 328 /* omega #328: fragment-major tensor matmul. Same grid, C layout (row-major f32) and argument 329 * words as the looped kernel; A and B are in fragment order instead of row-major: 330 * A (mp x kp, from omega_matmul_frag_pack_a): block (mt, ks) of 256 bf16 at ((mt*KS + ks)*32 331 * + lane)*8, lane = group*4 + tig, holding A[r][c], A[r][c+1], A[r+8][c], A[r+8][c+1], 332 * 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; 333 * B (kp x np, from omega_matmul_frag_pack_b): block (nt, ks) of 128 bf16 at ((nt*KS + ks)*32 334 * + lane)*4 holding B[k][j], B[k+1][j], B[k+8][j], B[k+9][j] with k = 16ks + 2tig, 335 * j = 8nt + group; KS = kp / 16, entries outside the real matrix are zero. 336 * One LDG.E.128 (A) and one LDG.E.64 (B) per K step; `unroll` (1, 2, 4 or 8, dividing KS) 337 * steps of loads are issued before the HMMA chain. mutant != 0 drops the last pass (test 338 * oracle only). Each tile occupies 16*kp (A) and 8*kp (B) elements, so a row-tile offset into 339 * A is the same in bytes as for row-major A. */ 340 int omega_blackwell_codegen_matmul_frag_prog(const OmegaMatMulSpec *spec, uint32_t grid_x, uint32_t unroll, 341 int mutant, BlackwellIRProgram *prog); 342 int omega_blackwell_codegen_matmul_frag(const OmegaMatMulSpec *spec, uint32_t grid_x, uint32_t unroll, 343 int mutant, OmegaBlackwellKernel *kernel); 344 /* The largest of 4, 2, 1 that divides kp / 16 (unroll 8 is generated for experiments only: it 345 * did not complete on the GB10, see the definition). */ 346 uint32_t omega_matmul_frag_unroll(uint32_t kp); 347 /* 2026-10-12: blocked fragment kernel for prefill rows. One warp owns rt row tiles x ct column tiles 348 * (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; 349 * grid_x <= nt/ct). Same packing, argument words and C layout as the single-tile kernel; a CTA's 350 * row block is rt row tiles (grid_y = row tiles / rt). mutant != 0 drops the last K pass. */ 351 int omega_blackwell_codegen_matmul_frag_block_prog(const OmegaMatMulSpec *spec, uint32_t grid_x, uint32_t unroll, 352 uint32_t rt, uint32_t ct, int mutant, BlackwellIRProgram *prog); 353 int omega_blackwell_codegen_matmul_frag_block(const OmegaMatMulSpec *spec, uint32_t grid_x, uint32_t unroll, 354 uint32_t rt, uint32_t ct, int mutant, OmegaBlackwellKernel *kernel); 355 /* rt, ct for mt row tiles and nt column tiles (1, 1 = the single-tile kernel), and the unroll the API uses */ 356 void omega_matmul_frag_block_shape(uint32_t mt, uint32_t nt, uint32_t *rt, uint32_t *ct); 357 uint32_t omega_matmul_frag_block_unroll(uint32_t kp, uint32_t rt, uint32_t ct); 358 359 /* Registers to declare in the QMD for a kernel whose allocator peak (OmegaLiveInterval count, regalloc.peak_gpr_usage) 360 * is `peak`: peak + 2, rounded up to a multiple of 8 (the session raises anything below 64 to 64). 361 * Chip evidence (OWN-30B, 2026-10-13): the fp8 grouped GEMM (lane 8, 8 rows) with peak 103 declared 104 (= peak + 1 362 * rounded up, the earlier rule) hung the chip in 7 of 7 runs; the same program declared 112 ran every gate. Earlier 363 * evidence: peak 64 declared 64 faulted (Xid 13 Out Of Range Register). Declared >= peak + 2 has run everywhere else 364 * (router peak 46 declared 48). The exact minimum is UNKNOWN (declarations are multiples of 8); the rule is INFERRED. */ 365 uint32_t omega_bw_gpr_declared(uint32_t peak); 366 /* 1 iff `declared` is a legal declaration for `peak`: a multiple of 8 and >= peak + 2. Launchers refuse otherwise. */ 367 int omega_bw_gpr_declared_ok(uint32_t declared, uint32_t peak); 368 /* a: m x k row-major. dst: mp x kp elements, fully written (zero padding). */ 369 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); 370 /* the same from f32 rows, converted to bf16 as omega_fp32_to_bf16 does (one pass, no staging copy) */ 371 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); 372 /* Row 0 of a one-row call into a buffer whose rows 1..15 already hold zeros for this kp (every column, 373 * zeros past k); the caller tracks that the zeros are there (omega_gpu_matmul_api.c, m = 1 path). */ 374 void omega_matmul_frag_pack_a_row0(const uint16_t *a, uint32_t k, uint32_t kp, uint16_t *dst); 375 void omega_matmul_frag_pack_a_row0_f32(const float *a, uint32_t k, uint32_t kp, uint16_t *dst); 376 /* Result sentinels (host side of the poison check): one word per 16 x 8 fragment tile that holds a real 377 * output word, at the tile's last real row and column, in the row-major m x np result. set != 0 writes 378 * poison into every sentinel and returns their count; set == 0 returns how many still hold poison. The 379 * kernel's epilogue stores a fragment tile as one straight-line run of STG.E words, so a tile whose 380 * sentinel survived was never stored. */ 381 size_t omega_matmul_result_sentinels(float *cd, uint32_t m, uint32_t n, uint32_t np, float poison, int set); 382 /* b: k x n row-major. dst: the column tiles [nt0, nt1), (nt1 - nt0) * kp * 8 elements, fully 383 * written; it is the part of the packed tensor starting at element nt0 * kp * 8. */ 384 void omega_matmul_frag_pack_b(const uint16_t *b, uint32_t k, uint32_t n, uint32_t kp, 385 uint32_t nt0, uint32_t nt1, uint16_t *dst); 386 387 /* Unit test for instruction encoding bitfield fixtures (Gate 3) */ 388 int omega_blackwell_verify_codegen_fixtures(void); 389 390 /* FB-1 cut 4: fixtures for MUFU_EX2, BAR_SYNC, LDS32, STS32 (0 = pass) */ 391 int omega_blackwell_verify_codegen_fixtures_fb1cut4(void); 392 393 /* Prime race cut: golden words for LOP3_LUT, SHF_L_U32, IMAD_HI_U32 (0 = pass) */ 394 int omega_blackwell_verify_codegen_fixtures_intops(void); 395 396 /* omega #328: golden words for LDG.E.64 / LDG.E.128 with offsets and the timed NOP (0 = pass) */ 397 int omega_blackwell_verify_codegen_fixtures_fragmm(void); 398 399 /* omega #308: golden words for BSSY / BSYNC (nvcc 13.0.88 sm_121 oracle), plus the 400 * encoder's refusals (negative delta, barrier id > 15). 0 = pass. */ 401 int omega_blackwell_verify_codegen_fixtures_reconv(void); 402 403 /* Training R5: golden words for SHFL.BFLY and F2FP.BF16.F32.PACK_AB (nvcc 13.0.88 sm_121 oracle sealed in 404 * evidence/OWN30B-T3-TC-PEAK-BW-`5faa0f70`-20261013T2236Z), plus SHFL.BFLY = SHFL.DOWN with bit 58 for every lane. 0 = pass. */ 405 int omega_blackwell_verify_codegen_fixtures_bfly_f2fp(void); 406 407 /* Training R5 part 2: golden words for ATOMG.E.ADD.F32.FTZ.RN.STRONG.SYS and .STRONG.GPU, with and without a destination 408 * (decoded with nvdisasm -b SM121; no nvcc). 0 = pass. */ 409 int omega_blackwell_verify_codegen_fixtures_atomf32(void); 410 411 /* omega #308: encodes one instruction with a given physical register map of 412 * identity (vreg n -> Rn) so offline tools can disassemble single words. 0 = ok. */ 413 int omega_blackwell_encode_one(const BlackwellIRInsn *insn, uint32_t w[4]); 414 415 /* Unit test for bounded register allocation live intervals and bounds enforcement (Gate 4) */ 416 int omega_blackwell_test_regalloc_bounds(void); 417 418 /* Stage-1 dynamic codegen variation demonstration between 16x16x16 and 32x16x64 (Gate 7) */ 419 int omega_blackwell_test_codegen_variation(void); 420 421 #endif /* OMEGA_BLACKWELL_CODEGEN_H */