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.c # 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.c` 3258 lines · 183546 bytes Raw Blame History 1 /* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception */ 2 #include "omega_blackwell_codegen.h" 3 #include "omega_blackwell_submit.h" 4 #include "sha256.h" 5 #include "omega_path_check.h" 6 #include "omega_fixture_verify.h" 7 #include 8 #include "cudapp_native.h" 9 #include "omega_bw_bind.h" 10 /* Freestanding uses seeded caller-stack native units; C switches are default-OFF oracles. */ 11 #ifdef OMEGA_GPU_NO_POSIX 12 #include "omega_native.h" 13 #define omega_ra_ir omega2_ra_ir 14 #define omega_ra_run omega2_ra_run 15 #define omega_cg_par omega2_cg_par 16 #define omega_cg_matmul omega2_cg_matmul 17 #define omega_cg_pklen omega2_cg_pklen 18 #define omega_cg_pkh omega2_cg_pkh 19 #define omega_sentin(r, q, nq, c, nc) omega2_sentin(r, q, nq, (void *)(c), nc) 20 #ifndef CUDAPP_MIGRATION_PENDING_C_CODEGEN_MATMUL 21 /* Replay takes void views; native entries take typed views. These only route pointers. */ 22 #define BW_NATIVE_REPLAY(name) \ 23 static uint32_t bw_native_##name(uint64_t *r, uint64_t k, uint64_t a, uint64_t b, uint64_t c, \ 24 void *i, uint64_t ni, void *m, uint64_t nm) { \ 25 return omega2_##name(r, k, a, b, c, i, ni, m, nm); \ 26 } 27 BW_NATIVE_REPLAY(cg_i32) 28 BW_NATIVE_REPLAY(cg_tens) 29 BW_NATIVE_REPLAY(cg_loop) 30 BW_NATIVE_REPLAY(cg_frag) 31 BW_NATIVE_REPLAY(cg_block) 32 #undef BW_NATIVE_REPLAY 33 #define omega_cg_i32 bw_native_cg_i32 34 #define omega_cg_tens bw_native_cg_tens 35 #define omega_cg_loop bw_native_cg_loop 36 #define omega_cg_frag bw_native_cg_frag 37 #define omega_cg_block bw_native_cg_block 38 #endif 39 #endif 40 #if defined(CUDAPP_MIGRATION_PENDING_C_REGALLOC) 41 #define BW_RA_C_PRIMARY 1 42 #endif 43 #if defined(CUDAPP_MIGRATION_PENDING_C_CODEGEN_MATMUL) 44 #define BW_MATMUL_C_PRIMARY 1 45 #endif 46 #include "omega_gpu_om_ir.h" 47 #include "omega_bw_fields.h" 48 /* native v1 P07 cut A.2 (aienos#286, docs/numeric/OMEGA_GPU_NATIVE_BOUNDARY.md): with OMEGA_GPU_NO_POSIX this file 49 * builds -ffreestanding. Code generation then runs only through the allocation-free entry points 50 * (omega_blackwell_kernel_encode_into: the caller supplies the code buffer and its capacity); the allocating 51 * generators, their diagnostics and the printing variation self-test exist only in the hosted build. */ 52 #ifndef OMEGA_GPU_NO_POSIX 53 #include 54 #include 55 #include 56 #include 57 #define BW_DIAG(...) fprintf(stderr, __VA_ARGS__) 58 #else 59 #define BW_DIAG(...) ((void)0) 60 #endif 61 62 void omega_bw_ir_init(BlackwellIRProgram *prog) { 63 if (!prog) return; 64 memset(prog, 0, sizeof(*prog)); 65 } 66 67 #ifdef BW_RA_C_PRIMARY 68 int omega_bw_ir_alloc_vreg(BlackwellIRProgram *prog) { 69 if (!prog || prog->regalloc.num_vregs >= BW_MAX_VREGS) return -1; 70 int v = prog->regalloc.num_vregs++; 71 prog->regalloc.intervals[v].vreg = v; 72 prog->regalloc.intervals[v].first_def = -1; 73 prog->regalloc.intervals[v].last_use = -1; 74 prog->regalloc.intervals[v].phys_reg = -1; 75 prog->regalloc.intervals[v].bundle_size = 1; 76 prog->regalloc.intervals[v].is_pair = false; 77 prog->regalloc.intervals[v].is_quad = false; 78 prog->regalloc.intervals[v].active = false; 79 prog->regalloc.vreg_to_phys[v] = -1; 80 return v; 81 } 82 83 int omega_bw_ir_alloc_vreg64(BlackwellIRProgram *prog) { 84 if (!prog || prog->regalloc.num_vregs >= BW_MAX_VREGS) return -1; 85 int v = prog->regalloc.num_vregs++; 86 prog->regalloc.intervals[v].vreg = v; 87 prog->regalloc.intervals[v].first_def = -1; 88 prog->regalloc.intervals[v].last_use = -1; 89 prog->regalloc.intervals[v].phys_reg = -1; 90 prog->regalloc.intervals[v].bundle_size = 2; 91 prog->regalloc.intervals[v].is_pair = true; 92 prog->regalloc.intervals[v].is_quad = false; 93 prog->regalloc.intervals[v].active = false; 94 prog->regalloc.vreg_to_phys[v] = -1; 95 return v; 96 } 97 98 int omega_bw_ir_alloc_vreg128(BlackwellIRProgram *prog) { 99 if (!prog || prog->regalloc.num_vregs >= BW_MAX_VREGS) return -1; 100 int v = prog->regalloc.num_vregs++; 101 prog->regalloc.intervals[v].vreg = v; 102 prog->regalloc.intervals[v].first_def = -1; 103 prog->regalloc.intervals[v].last_use = -1; 104 prog->regalloc.intervals[v].phys_reg = -1; 105 prog->regalloc.intervals[v].bundle_size = 4; 106 prog->regalloc.intervals[v].is_pair = false; 107 prog->regalloc.intervals[v].is_quad = true; 108 prog->regalloc.intervals[v].active = false; 109 prog->regalloc.vreg_to_phys[v] = -1; 110 return v; 111 } 112 113 int omega_bw_ir_alloc_uvreg(BlackwellIRProgram *prog) { 114 if (!prog || prog->regalloc.num_uvregs >= BW_MAX_UVREGS) return -1; 115 int uv = prog->regalloc.num_uvregs++; 116 prog->regalloc.uintervals[uv].vreg = uv; 117 prog->regalloc.uintervals[uv].first_def = -1; 118 prog->regalloc.uintervals[uv].last_use = -1; 119 prog->regalloc.uintervals[uv].phys_reg = -1; 120 prog->regalloc.uintervals[uv].is_pair = false; 121 prog->regalloc.uintervals[uv].active = false; 122 prog->regalloc.uvreg_to_phys[uv] = -1; 123 return uv; 124 } 125 126 int omega_bw_ir_alloc_uvreg64(BlackwellIRProgram *prog) { 127 if (!prog || prog->regalloc.num_uvregs >= BW_MAX_UVREGS) return -1; 128 int uv = prog->regalloc.num_uvregs++; 129 prog->regalloc.uintervals[uv].vreg = uv; 130 prog->regalloc.uintervals[uv].first_def = -1; 131 prog->regalloc.uintervals[uv].last_use = -1; 132 prog->regalloc.uintervals[uv].phys_reg = -1; 133 prog->regalloc.uintervals[uv].is_pair = true; 134 prog->regalloc.uintervals[uv].active = false; 135 prog->regalloc.uvreg_to_phys[uv] = -1; 136 return uv; 137 } 138 139 int omega_bw_ir_append(BlackwellIRProgram *prog, const BlackwellIRInsn *insn) { 140 if (!prog || !insn || prog->count >= BW_MAX_IR_INSNS) return -1; 141 int idx = (int)prog->count++; 142 prog->insns[idx] = *insn; 143 144 /* Live-interval def/use tracking. Only LDCU/LDCU64 write a uniform register (the encoder's rule, 145 * bw_c_encode_insn and om/encoder/bw_enc_sm121.om isuld); every other dst is a GPR, also when 146 * is_uniform marks a uniform SOURCE operand. */ 147 const bool uniform_dst = insn->op == BW_IR_LDCU || insn->op == BW_IR_LDCU64; 148 if (insn->dst_vreg >= 0 && insn->dst_vreg < prog->regalloc.num_vregs) { 149 if (!uniform_dst) { 150 OmegaLiveInterval *iv = &prog->regalloc.intervals[insn->dst_vreg]; 151 if (iv->first_def < 0) iv->first_def = idx; 152 if (idx > iv->last_use) iv->last_use = idx; 153 } 154 } 155 if (insn->src1_vreg >= 0 && insn->src1_vreg < prog->regalloc.num_vregs) { 156 OmegaLiveInterval *iv = &prog->regalloc.intervals[insn->src1_vreg]; 157 if (iv->first_def < 0) iv->first_def = idx; 158 if (idx > iv->last_use) iv->last_use = idx; 159 } 160 if (insn->src2_vreg >= 0 && insn->src2_vreg < prog->regalloc.num_vregs) { 161 OmegaLiveInterval *iv = &prog->regalloc.intervals[insn->src2_vreg]; 162 if (iv->first_def < 0) iv->first_def = idx; 163 if (idx > iv->last_use) iv->last_use = idx; 164 } 165 if (insn->src3_vreg >= 0 && insn->src3_vreg < prog->regalloc.num_vregs) { 166 OmegaLiveInterval *iv = &prog->regalloc.intervals[insn->src3_vreg]; 167 if (iv->first_def < 0) iv->first_def = idx; 168 if (idx > iv->last_use) iv->last_use = idx; 169 } 170 171 /* Uniform register def/use tracking */ 172 if (uniform_dst && insn->dst_vreg >= 0 && insn->dst_vreg < prog->regalloc.num_uvregs) { 173 OmegaLiveInterval *uiv = &prog->regalloc.uintervals[insn->dst_vreg]; 174 if (uiv->first_def < 0) uiv->first_def = idx; 175 if (idx > uiv->last_use) uiv->last_use = idx; 176 } 177 if (insn->ureg >= 0 && insn->ureg < prog->regalloc.num_uvregs) { 178 OmegaLiveInterval *uiv = &prog->regalloc.uintervals[insn->ureg]; 179 if (uiv->first_def < 0) uiv->first_def = idx; 180 if (idx > uiv->last_use) uiv->last_use = idx; 181 } 182 183 return idx; 184 } 185 #else 186 /* ABI shell only: Omega decides capacity, operand membership and interval updates. */ 187 static int bw_ra_decide(uint64_t kind, int64_t a, int64_t b) { 188 uint64_t result = UINT64_MAX; 189 if (omega_ra_ir(&result, kind, (uint64_t)a, (uint64_t)b) != 0) return -1; 190 return (int)result; 191 } 192 193 static int bw_ra_alloc(BlackwellIRProgram *prog, unsigned width, bool uniform) { 194 if (!prog) return -1; 195 OmegaRegAlloc *ra = &prog->regalloc; 196 int *count = uniform ? &ra->num_uvregs : &ra->num_vregs; 197 int v = bw_ra_decide(0, *count, uniform ? BW_MAX_UVREGS : BW_MAX_VREGS); 198 if (v < 0) return -1; 199 OmegaLiveInterval *iv = uniform ? &ra->uintervals[v] : &ra->intervals[v]; 200 iv->vreg = v; iv->first_def = -1; iv->last_use = -1; iv->phys_reg = -1; 201 iv->is_pair = width == 2; iv->active = false; 202 if (!uniform) { iv->bundle_size = width; iv->is_quad = width == 4; } 203 if (uniform) ra->uvreg_to_phys[v] = -1; 204 else ra->vreg_to_phys[v] = -1; 205 *count = v + 1; 206 return v; 207 } 208 int omega_bw_ir_alloc_vreg(BlackwellIRProgram *p) { return bw_ra_alloc(p, 1, false); } 209 int omega_bw_ir_alloc_vreg64(BlackwellIRProgram *p) { return bw_ra_alloc(p, 2, false); } 210 int omega_bw_ir_alloc_vreg128(BlackwellIRProgram *p) { return bw_ra_alloc(p, 4, false); } 211 int omega_bw_ir_alloc_uvreg(BlackwellIRProgram *p) { return bw_ra_alloc(p, 1, true); } 212 int omega_bw_ir_alloc_uvreg64(BlackwellIRProgram *p) { return bw_ra_alloc(p, 2, true); } 213 214 static void bw_ra_touch(OmegaLiveInterval *iv, int at) { 215 iv->first_def = bw_ra_decide(1, iv->first_def, at); 216 iv->last_use = bw_ra_decide(2, iv->last_use, at); 217 } 218 int omega_bw_ir_append(BlackwellIRProgram *prog, const BlackwellIRInsn *insn) { 219 if (!prog || !insn || bw_ra_decide(5, (int64_t)prog->count, 0) != 1) return -1; 220 int idx = (int)prog->count++; 221 prog->insns[idx] = *insn; 222 OmegaRegAlloc *ra = &prog->regalloc; 223 int udst = bw_ra_decide(4, insn->op, 0); 224 if (!udst && bw_ra_decide(3, insn->dst_vreg, ra->num_vregs) == 1) 225 bw_ra_touch(&ra->intervals[insn->dst_vreg], idx); 226 const int src[] = {insn->src1_vreg, insn->src2_vreg, insn->src3_vreg}; 227 for (unsigned i = 0; i < 3; ++i) 228 if (bw_ra_decide(3, src[i], ra->num_vregs) == 1) 229 bw_ra_touch(&ra->intervals[src[i]], idx); 230 if (udst && bw_ra_decide(3, insn->dst_vreg, ra->num_uvregs) == 1) 231 bw_ra_touch(&ra->uintervals[insn->dst_vreg], idx); 232 if (bw_ra_decide(3, insn->ureg, ra->num_uvregs) == 1) 233 bw_ra_touch(&ra->uintervals[insn->ureg], idx); 234 return idx; 235 } 236 #endif 237 238 #if defined(BW_RA_C_PRIMARY) || defined(CUDAPP_REGALLOC_DIFFERENTIAL) 239 int omega_bw_regalloc_c_oracle(BlackwellIRProgram *prog) { 240 if (!prog) return -1; 241 OmegaRegAlloc *ra = &prog->regalloc; 242 243 /* 244 * Bounded deterministic linear-scan register allocator. 245 * Computes genuine live intervals across virtual registers and assigns 246 * available physical GPRs (R2..R127, or up to the program's opt-in gpr_limit) and UGPRs (UR4..UR31). 247 * Enforces even-alignment and dual-register reservation for 64-bit pairs. 248 */ 249 const int gpr_lim = ra->gpr_limit ? (int)ra->gpr_limit : BW_PHYS_GPR_MAX; 250 bool gpr_busy[BW_PHYS_GPR_HW_LIMIT]; 251 memset(gpr_busy, 0, sizeof(gpr_busy)); 252 253 int max_phys_used = BW_PHYS_GPR_START; 254 255 /* Sort virtual register indices chronologically by first_def (Poletto & Sarkar Linear Scan) */ 256 int order[BW_MAX_VREGS]; 257 int count = 0; 258 for (int v = 0; v < ra->num_vregs; v++) { 259 if (ra->intervals[v].first_def >= 0) { 260 order[count++] = v; 261 } else { 262 ra->intervals[v].phys_reg = BW_PHYS_GPR_START; 263 ra->vreg_to_phys[v] = ra->intervals[v].phys_reg; 264 } 265 } 266 for (int i = 0; i < count - 1; i++) { 267 for (int j = i + 1; j < count; j++) { 268 if (ra->intervals[order[i]].first_def > ra->intervals[order[j]].first_def) { 269 int tmp = order[i]; 270 order[i] = order[j]; 271 order[j] = tmp; 272 } 273 } 274 } 275 276 for (int idx = 0; idx < count; idx++) { 277 int v = order[idx]; 278 OmegaLiveInterval *iv = &ra->intervals[v]; 279 280 /* Free physical registers whose virtual live intervals have ended */ 281 for (int p = 0; p < ra->num_vregs; p++) { 282 if (ra->intervals[p].active && ra->intervals[p].last_use < iv->first_def) { 283 int freed = ra->intervals[p].phys_reg; 284 if (freed >= 0 && freed < gpr_lim) { 285 uint32_t bsize = ra->intervals[p].bundle_size ? ra->intervals[p].bundle_size : (ra->intervals[p].is_pair ? 2 : (ra->intervals[p].is_quad ? 4 : 1)); 286 for (uint32_t k = 0; k < bsize && (freed + (int)k) < gpr_lim; k++) { 287 gpr_busy[freed + k] = false; 288 } 289 } 290 ra->intervals[p].active = false; 291 } 292 } 293 294 /* Find lowest available physical register */ 295 int assigned = -1; 296 uint32_t bsize = iv->bundle_size ? iv->bundle_size : (iv->is_pair ? 2 : (iv->is_quad ? 4 : 1)); 297 if (bsize == 4) { 298 /* 128-bit quad requires 4-alignment and 4 contiguous free registers */ 299 int start = (BW_PHYS_GPR_START + 3) & ~3; 300 for (int p = start; p <= gpr_lim - 4; p += 4) { 301 if (!gpr_busy[p] && !gpr_busy[p + 1] && !gpr_busy[p + 2] && !gpr_busy[p + 3]) { 302 assigned = p; 303 for (int k = 0; k < 4; k++) gpr_busy[p + k] = true; 304 break; 305 } 306 } 307 } else if (bsize == 2) { 308 /* 64-bit pair requires even alignment and contiguous free registers */ 309 int start = (BW_PHYS_GPR_START + 1) & ~1; 310 for (int p = start; p <= gpr_lim - 2; p += 2) { 311 if (!gpr_busy[p] && !gpr_busy[p + 1]) { 312 assigned = p; 313 gpr_busy[p] = true; 314 gpr_busy[p + 1] = true; 315 break; 316 } 317 } 318 } else { 319 for (int p = BW_PHYS_GPR_START; p < gpr_lim; p++) { 320 if (!gpr_busy[p]) { 321 assigned = p; 322 gpr_busy[p] = true; 323 break; 324 } 325 } 326 } 327 328 if (assigned < 0) { 329 /* Hardware register capacity exceeded: fail closed loudly */ 330 return -1; 331 } 332 333 iv->phys_reg = assigned; 334 iv->active = true; 335 ra->vreg_to_phys[v] = assigned; 336 337 int top = assigned + (int)bsize - 1; 338 if (top > max_phys_used) { 339 max_phys_used = top; 340 } 341 } 342 ra->peak_gpr_usage = (uint32_t)(max_phys_used + 1); 343 344 /* Allocate uniform registers */ 345 int cur_u = BW_PHYS_UGPR_START; 346 for (int uv = 0; uv < ra->num_uvregs; uv++) { 347 OmegaLiveInterval *uiv = &ra->uintervals[uv]; 348 if (uiv->is_pair) { 349 if (cur_u % 2 != 0) cur_u++; 350 if (cur_u + 1 >= BW_PHYS_UGPR_MAX) return -1; 351 uiv->phys_reg = cur_u; 352 ra->uvreg_to_phys[uv] = cur_u; 353 cur_u += 2; 354 } else { 355 if (cur_u >= BW_PHYS_UGPR_MAX) return -1; 356 uiv->phys_reg = cur_u; 357 ra->uvreg_to_phys[uv] = cur_u++; 358 } 359 } 360 ra->peak_ugpr_usage = (uint32_t)cur_u; 361 362 return 0; 363 } 364 #endif 365 366 int omega_bw_regalloc_solve(BlackwellIRProgram *prog) { 367 #ifdef BW_RA_C_PRIMARY 368 return omega_bw_regalloc_c_oracle(prog); 369 #else 370 if (!prog) return -1; 371 OmegaRegAlloc *ra = &prog->regalloc; 372 uint64_t wire[4096] = {0}; 373 wire[0] = ra->num_vregs; 374 wire[1] = ra->num_uvregs; 375 wire[2] = ra->gpr_limit; 376 wire[3] = ra->peak_gpr_usage; 377 wire[4] = ra->peak_ugpr_usage; 378 for (unsigned v = 0; v < BW_MAX_VREGS + BW_MAX_UVREGS; ++v) { 379 const OmegaLiveInterval *iv = v < BW_MAX_VREGS 380 ? &ra->intervals[v] : &ra->uintervals[v - BW_MAX_VREGS]; 381 uint64_t *p = &wire[16 + 10 * v]; 382 p[0] = iv->vreg; p[1] = iv->first_def; p[2] = iv->last_use; 383 p[3] = iv->phys_reg; p[4] = iv->bundle_size; p[5] = iv->is_pair; 384 p[6] = iv->is_quad; p[7] = iv->active; 385 p[8] = v < BW_MAX_VREGS ? ra->vreg_to_phys[v] : ra->uvreg_to_phys[v - BW_MAX_VREGS]; 386 } 387 uint64_t result = UINT64_MAX; 388 if (omega_ra_run(&result, wire, 4096) != 0) return -1; 389 /* The oracle exposes partial assignments on exhaustion; preserve that contract. */ 390 for (unsigned v = 0; v < BW_MAX_VREGS + BW_MAX_UVREGS; ++v) { 391 OmegaLiveInterval *iv = v < BW_MAX_VREGS 392 ? &ra->intervals[v] : &ra->uintervals[v - BW_MAX_VREGS]; 393 const uint64_t *p = &wire[16 + 10 * v]; 394 iv->phys_reg = (int)p[3]; iv->active = p[7] != 0; 395 if (v < BW_MAX_VREGS) ra->vreg_to_phys[v] = (int)p[8]; 396 else ra->uvreg_to_phys[v - BW_MAX_VREGS] = (int)p[8]; 397 } 398 ra->peak_gpr_usage = (uint32_t)wire[3]; 399 ra->peak_ugpr_usage = (uint32_t)wire[4]; 400 return (int)result; 401 #endif 402 } 403 404 /* sm_121 computation uses seeded Omega in both builds. The native caller-stack 405 * binding removes the former NO_POSIX exception; C is a default-OFF oracle. */ 406 #if defined(CUDAPP_MIGRATION_PENDING_C_NO_POSIX) || defined(CUDAPP_MIGRATION_PENDING_C_ENCODER) 407 #define BW_ENC_C_PRIMARY 1 408 #endif 409 #if defined(BW_ENC_C_PRIMARY) && defined(CUDAPP_MIGRATION_PENDING_ENCODER_DIFF) 410 #error "CUDAPP_MIGRATION_PENDING_ENCODER_DIFF needs the .om encoder, not the C oracle" 411 #endif 412 #if defined(BW_ENC_C_PRIMARY) || defined(CUDAPP_MIGRATION_PENDING_ENCODER_DIFF) 413 static int bw_c_encode_insn(const BlackwellIRInsn *insn, const OmegaRegAlloc *ra, uint32_t w[4]) { 414 memset(w, 0, 16); 415 416 int dst = (insn->dst_vreg >= 0) ? (ra->vreg_to_phys[insn->dst_vreg] + insn->dst_subreg) : 0; 417 int src1 = (insn->src1_vreg >= 0) ? (ra->vreg_to_phys[insn->src1_vreg] + insn->src1_subreg) : 0; 418 int src2 = (insn->src2_vreg >= 0) ? (ra->vreg_to_phys[insn->src2_vreg] + insn->src2_subreg) : 0; 419 int src3 = (insn->src3_vreg >= 0) ? (ra->vreg_to_phys[insn->src3_vreg] + insn->src3_subreg) : 0xff; /* Default to RZ (0xff) */ 420 int ureg = (insn->ureg >= 0) ? ra->uvreg_to_phys[insn->ureg] : 0; 421 422 if ((insn->op == BW_IR_LDCU || insn->op == BW_IR_LDCU64) && insn->dst_vreg >= 0) { 423 dst = ra->uvreg_to_phys[insn->dst_vreg]; 424 } 425 426 /* Binary64 cannot use an odd base or include R255 (the zero register). 427 * Keep this independent of the seeded encoder's dpair check. */ 428 if (insn->op >= BW_IR_DADD && insn->op <= BW_IR_DSETP && 429 ((src1 & 1) || src1 < 0 || src1 >= 254 || 430 (src2 & 1) || src2 < 0 || src2 >= 254)) return -1; 431 if ((insn->op >= BW_IR_DADD && insn->op <= BW_IR_DFMA) || 432 insn->op == BW_IR_F64_F32 || insn->op == BW_IR_F64_S32 || insn->op == BW_IR_F64_U32) 433 if ((dst & 1) || dst < 0 || dst >= 254) return -1; 434 if (insn->op == BW_IR_DFMA && src3 != 255 && 435 ((src3 & 1) || src3 < 0 || src3 >= 254)) return -1; 436 if ((insn->op == BW_IR_F32_F64 || insn->op == BW_IR_S32_F64) && 437 ((src1 & 1) || src1 < 0 || src1 >= 254)) return -1; 438 439 switch (insn->op) { 440 case BW_IR_NOP: 441 w[0] = 0x00007918; 442 w[1] = 0x00000000; 443 w[2] = 0x00000000; 444 w[3] = insn->control ? insn->control : 0x000fc000; /* control: a timed pause (fragment matmul) */ 445 break; 446 447 case BW_IR_MOV_RZ: 448 /* MOV Rd, RZ */ 449 w[0] = 0x7202 | ((uint32_t)(dst & 0xff) << 16); 450 w[1] = 0x000000ff; 451 w[2] = 0x00000f00; 452 w[3] = insn->control ? insn->control : 0x000fe200; 453 break; 454 455 case BW_IR_MOV_IMM: 456 /* MOV Rd, imm32 */ 457 w[0] = 0x7802 | ((uint32_t)(dst & 0xff) << 16); 458 w[1] = insn->imm; 459 w[2] = 0x00000f00; 460 w[3] = insn->control ? insn->control : 0x000fe200; 461 break; 462 463 case BW_IR_S2R: 464 /* S2R Rd, SR */ 465 w[0] = 0x7919 | ((uint32_t)(dst & 0xff) << 16); 466 w[1] = 0x0; 467 w[2] = (insn->imm & 0xff) << 8; 468 w[3] = insn->control ? insn->control : 0x000e2200; 469 break; 470 471 case BW_IR_LDC: 472 /* LDC Rd, c[0x0][imm] */ 473 w[0] = 0xff007b82 | ((uint32_t)(dst & 0xff) << 16); 474 w[1] = (insn->imm << 6) & 0x00ffff00; 475 w[2] = 0x00000800; 476 w[3] = insn->control ? insn->control : 0x000fe200; 477 break; 478 479 case BW_IR_LDC64: 480 /* LDC.64 Rd:Rd+1, c[0x0][imm] */ 481 w[0] = 0xff007b82 | ((uint32_t)(dst & 0xff) << 16); 482 w[1] = (insn->imm << 6) & 0x00ffff00; 483 w[2] = 0x00000a00; 484 w[3] = insn->control ? insn->control : 0x000e2200; 485 break; 486 487 case BW_IR_LDCU: 488 /* LDCU URd, c[0x0][imm] */ 489 w[0] = 0xff0077ac | ((uint32_t)(dst & 0xff) << 16); 490 w[1] = (insn->imm << 5) & 0x00ffff00; 491 w[2] = 0x08000800; 492 w[3] = insn->control ? insn->control : 0x000e2200; 493 break; 494 495 case BW_IR_LDCU64: 496 /* LDCU.64 URd:URd+1, c[0x0][imm] */ 497 w[0] = 0xff0077ac | ((uint32_t)(dst & 0xff) << 16); 498 w[1] = (insn->imm << 5) & 0x00ffff00; 499 w[2] = 0x08000a00; 500 w[3] = insn->control ? insn->control : 0x000e6e00; 501 break; 502 503 case BW_IR_IMAD: 504 if (insn->src2_vreg >= 0) { 505 /* Register-register: IMAD Rd, Ra, Rb, Rc (0x7224) */ 506 w[0] = 0x7224 | ((uint32_t)(dst & 0xff) << 16) | ((uint32_t)(src1 & 0xff) << 24); 507 w[1] = (uint32_t)(src2 & 0xff); 508 w[2] = 0x078e0200 | (uint32_t)(src3 & 0xff); 509 w[3] = insn->control ? insn->control : 0x001fca00; 510 } else if (insn->is_uniform && insn->ureg >= 0) { 511 /* Uniform register multiplier: IMAD Rd, Ra, URb, Rc (0x7c24) */ 512 w[0] = 0x7c24 | ((uint32_t)(dst & 0xff) << 16) | ((uint32_t)(src1 & 0xff) << 24); 513 w[1] = (uint32_t)(ureg & 0xff); 514 w[2] = 0x0f8e0200 | (uint32_t)(src3 & 0xff); 515 w[3] = insn->control ? insn->control : 0x001fca00; 516 } else { 517 /* Immediate multiplier: IMAD Rd, Ra, imm, Rc (0x7824) */ 518 w[0] = 0x7824 | ((uint32_t)(dst & 0xff) << 16) | ((uint32_t)(src1 & 0xff) << 24); 519 w[1] = insn->imm; 520 w[2] = 0x078e0200 | (uint32_t)(src3 & 0xff); 521 w[3] = insn->control ? insn->control : 0x001fca00; 522 } 523 break; 524 525 case BW_IR_IMAD_WIDE: 526 /* IMAD.WIDE.U32 Rd:Rd+1, Ra, imm, Rc:Rc+1 */ 527 w[0] = 0x7825 | ((uint32_t)(dst & 0xff) << 16) | ((uint32_t)(src1 & 0xff) << 24); 528 w[1] = insn->imm; 529 w[2] = 0x078e0000 | (uint32_t)(src3 & 0xff); 530 w[3] = insn->control ? insn->control : 0x001fcc00; 531 break; 532 533 case BW_IR_ISETP_GE: 534 if (insn->is_uniform && insn->ureg >= 0) { 535 w[0] = 0x7c0c | ((uint32_t)(dst & 0xff) << 16) | ((uint32_t)(src1 & 0xff) << 24); 536 w[1] = (uint32_t)(ureg & 0xff); 537 w[2] = 0x0bf06070; 538 w[3] = insn->control ? insn->control : 0x002fda00; 539 } else { 540 w[0] = 0x780c | ((uint32_t)(dst & 0xff) << 16) | ((uint32_t)(src1 & 0xff) << 24); 541 w[1] = insn->imm; 542 w[2] = 0x03f06270; 543 w[3] = insn->control ? insn->control : 0x001fda00; 544 } 545 break; 546 547 case BW_IR_LDG_E: { 548 /* LDG.E Rd, desc[URd][Ra.64+imm]: the 32-bit size (4) of the LDG.E.64 / LDG.E.128 words below; imm as there 549 * (signed 24-bit byte offset at bits 40..63, w[1] bits 8..31), unchanged words when imm is 0. */ 550 int32_t off = (int32_t)insn->imm; 551 if (off < -(1 << 23) || off >= (1 << 23)) return -1; 552 w[0] = 0x7981 | ((uint32_t)(dst & 0xff) << 16) | ((uint32_t)(src1 & 0xff) << 24); 553 w[1] = (uint32_t)(ureg & 0xff) | (((uint32_t)off & 0xffffffU) << 8); 554 w[2] = 0x0c1e1900; 555 w[3] = insn->control ? insn->control : 0x000f2200; 556 break; 557 } 558 559 case BW_IR_STG_E: { 560 /* STG.E desc[URd][Ra.64+imm], Rb. imm = signed 24-bit byte offset at bits 40..63 (w[1] bits 561 * 8..31), as for LDG; words from nvcc 13.0.88 -arch=sm_121 (omega_blackwell_verify_codegen_fixtures_fragmm, 562 * 2026-10-12: STG.E desc[UR4][R4.64+0x4], R9 = 0x0000040904007986 0x000fe8000c101904). */ 563 int32_t off = (int32_t)insn->imm; 564 if (off < -(1 << 23) || off >= (1 << 23)) return -1; 565 w[0] = 0x7986 | ((uint32_t)(src1 & 0xff) << 24); 566 w[1] = (uint32_t)(src2 & 0xff) | (((uint32_t)off & 0xffffffU) << 8); 567 w[2] = 0x0c101900 | (uint32_t)(ureg & 0xff); 568 w[3] = insn->control ? insn->control : 0x000fe200; 569 break; 570 } 571 572 case BW_IR_STG_EF: 573 /* STG.E.MMIO.GPU. Uncached store, meant to be visible without a fence. */ 574 w[0] = 0x7986 | ((uint32_t)(src1 & 0xff) << 24); 575 w[1] = (uint32_t)(src2 & 0xff); 576 w[2] = 0x0c111900 | (uint32_t)(ureg & 0xff); 577 w[3] = insn->control ? insn->control : 0x000fe200; 578 break; 579 580 case BW_IR_IADD3: 581 /* IADD3 Rd, PT, PT, Ra, Rb, Rc */ 582 w[0] = 0x7210 | ((uint32_t)(dst & 0xff) << 16) | ((uint32_t)(src1 & 0xff) << 24); 583 w[1] = (uint32_t)(src2 & 0xff); 584 w[2] = 0x07ffe0ff; 585 w[3] = insn->control ? insn->control : 0x010fca00; /* omega-mv#545: the emitter's waits, not a fixed SB4 wait */ 586 break; 587 588 case BW_IR_HMMA_F16: 589 case BW_IR_HMMA_BF16: { 590 /* HMMA.16816.F32[.BF16] Rd, Ra, Rb, Rc */ 591 w[0] = 0x723c | ((uint32_t)(dst & 0xff) << 16) | ((uint32_t)(src1 & 0xff) << 24); 592 w[1] = (uint32_t)(src2 & 0xff); 593 uint32_t prec_bit = (insn->op == BW_IR_HMMA_BF16) ? 0x00040000 : 0x00000000; 594 w[2] = (uint32_t)(src3 & 0xff) | (0x18 << 8) | prec_bit; 595 w[3] = insn->control ? insn->control : 0x000fe200; 596 break; 597 } 598 599 case BW_IR_SHF_R: 600 /* SHF.R.U32.HI Rd, RZ, imm, Ra */ 601 w[0] = 0x7819U | ((uint32_t)(dst & 0xff) << 16) | (0xffU << 24); 602 w[1] = (uint32_t)insn->imm; 603 w[2] = 0x00011600 | (uint32_t)(src1 & 0xff); 604 w[3] = insn->control ? insn->control : 0x001fca00; 605 break; 606 607 case BW_IR_LOP3_AND: 608 /* LOP3.LUT Rd, Ra, imm, RZ, 0xc0, !PT */ 609 w[0] = 0x7812U | ((uint32_t)(dst & 0xff) << 16) | ((uint32_t)(src1 & 0xff) << 24); 610 w[1] = (uint32_t)insn->imm; 611 w[2] = 0x078ec0ff; 612 w[3] = insn->control ? insn->control : 0x001fca00; 613 break; 614 615 case BW_IR_LDG_E_64: 616 case BW_IR_LDG_E_128: { 617 /* LDG.E.64 / LDG.E.128 Rd, desc[URd][Ra.64+imm]. imm = signed 24-bit byte offset at 618 * bits 40..63 (w[1] bits 8..31); size field in w[2] bits 9..11 (32 = 4, 64 = 5, 128 = 6). 619 * Words from nvcc 13.0.88 -arch=sm_121 (omega_blackwell_verify_codegen_fixtures_fragmm). */ 620 int32_t off = (int32_t)insn->imm; 621 if (off < -(1 << 23) || off >= (1 << 23)) return -1; 622 w[0] = 0x7981U | ((uint32_t)(dst & 0xff) << 16) | ((uint32_t)(src1 & 0xff) << 24); 623 w[1] = (uint32_t)(ureg & 0xff) | (((uint32_t)off & 0xffffffU) << 8); 624 w[2] = insn->op == BW_IR_LDG_E_64 ? 0x0c1e1b00U : 0x0c1e1d00U; 625 w[3] = insn->control ? insn->control : 0x000f2200; 626 break; 627 } 628 629 case BW_IR_LDG_E_U16: 630 /* LDG.E.U16 Rd, desc[URd][Ra.64] */ 631 w[0] = 0x7981U | ((uint32_t)(dst & 0xff) << 16) | ((uint32_t)(src1 & 0xff) << 24); 632 w[1] = (uint32_t)(ureg & 0xff); 633 w[2] = 0x0c1e1500; 634 w[3] = insn->control ? insn->control : 0x000f2200; 635 break; 636 637 case BW_IR_LDG_STRONG_SYS: 638 /* LDG.E.STRONG.SYS. Same shape as LDG.E; w[2] carries the 639 * system-scope bits (0x0c1e1900 -> 0x0c1f5900). */ 640 w[0] = 0x7981 | ((uint32_t)(dst & 0xff) << 16) | ((uint32_t)(src1 & 0xff) << 24); 641 w[1] = (uint32_t)(ureg & 0xff); 642 w[2] = 0x0c1f5900; 643 w[3] = insn->control ? insn->control : 0x000f2200; 644 break; 645 646 case BW_IR_LDG_MMIO: 647 /* LDG.E.MMIO.GPU, as required by BW_IR_LDG_MMIO. Mesa NAK's sm_80+ fields (sm70_encode.rs 648 * set_mem_access): eviction 84..86 = 1 (normal, not .EF = 0), memory order 77..80 = 0x8, a value 649 * NAK does not list (weak = 0 would be a cached load); nvdisasm 13.0.85 decodes it as MMIO.GPU. 650 * Pinned by tests/bw_ldg_mmio_test.c (issue #205); GB10 behaviour NOT_RUN. */ 651 w[0] = 0x7981 | ((uint32_t)(dst & 0xff) << 16) | ((uint32_t)(src1 & 0xff) << 24); 652 w[1] = (uint32_t)(ureg & 0xff); 653 w[2] = 0x0c1f1900; 654 w[3] = insn->control ? insn->control : 0x000f2200; 655 break; 656 657 case BW_IR_STG_STRONG_SYS: 658 w[0] = 0x7986 | ((uint32_t)(src1 & 0xff) << 24); 659 if (insn->predicate_p0) { 660 uint32_t pred = insn->predicate_not ? 0x8u : 0x0u; 661 w[0] = (w[0] & ~0xF000u) | (pred << 12); 662 } 663 w[1] = (uint32_t)(src2 & 0xff); 664 w[2] = 0x0c115900 | (uint32_t)(ureg & 0xff); 665 w[3] = insn->control ? insn->control : 0x000fe200; 666 break; 667 668 case BW_IR_MEMBAR_ALL_SYS: 669 w[0] = 0x00007992; 670 w[1] = 0x00000000; 671 w[2] = 0x0000b000; 672 w[3] = insn->control ? insn->control : 0x000fec00; 673 break; 674 675 case BW_IR_MEMBAR_SC_SYS: 676 w[0] = 0x00007992; 677 w[1] = 0x00000000; 678 w[2] = 0x00003000; 679 w[3] = insn->control ? insn->control : 0x000fec00; 680 break; 681 682 case BW_IR_CCTL_IVALL: 683 w[0] = 0xff00798f; 684 w[1] = 0x00000000; 685 w[2] = 0x02000000; 686 w[3] = insn->control ? insn->control : 0x000fe800; 687 break; 688 689 case BW_IR_ATOMG_ADD_STRONG_SYS: 690 w[0] = 0x79a8 | ((uint32_t)(dst & 0xff) << 16) | ((uint32_t)(src1 & 0xff) << 24); 691 w[1] = (uint32_t)(src2 & 0xff) | 0x80000000u; 692 w[2] = 0x081f5100 | (uint32_t)(ureg & 0xff); 693 w[3] = insn->control ? insn->control : 0x00321e00; 694 break; 695 696 case BW_IR_ATOMG_EXCH_STRONG_SYS: 697 w[0] = 0x79a8 | ((uint32_t)(dst & 0xff) << 16) | ((uint32_t)(src1 & 0xff) << 24); 698 w[1] = (uint32_t)(src2 & 0xff) | 0x80000000u; 699 w[2] = 0x0c1f5100 | (uint32_t)(ureg & 0xff); 700 w[3] = insn->control ? insn->control : 0x00321e00; 701 break; 702 703 case BW_IR_ATOMG_ADD_F32_STRONG_SYS: 704 case BW_IR_ATOMG_ADD_F32_STRONG_GPU: 705 /* ATOMG.E.ADD.F32.FTZ.RN.STRONG.{SYS,GPU} PT, Rd|RZ, desc[UR][Ra.64], Rb (omega_blackwell_verify_codegen_fixtures_atomf32). 706 * Float opcode 0x9a3; w2 bit 26 selects the desc[] form, type 9 (w2 bits 9..12) is F32.FTZ.RN, order (w2 bits 707 * 13..16) 0xa = SYS, 0x7 = GPU. No destination vreg encodes RZ (the generic default 0 would clobber R0). */ 708 w[0] = 0x79a3 | ((uint32_t)(insn->dst_vreg < 0 ? 0xff : (dst & 0xff)) << 16) | ((uint32_t)(src1 & 0xff) << 24); 709 w[1] = (uint32_t)(src2 & 0xff) | 0x80000000u; 710 w[2] = (insn->op == BW_IR_ATOMG_ADD_F32_STRONG_GPU ? 0x0c1ef300u : 0x0c1f5300u) | (uint32_t)(ureg & 0xff); 711 w[3] = insn->control ? insn->control : 0x00321e00; 712 break; 713 714 case BW_IR_ATOMG_MIN_U64_STRONG_SYS: 715 /* ATOMG.E.MIN.64.STRONG.SYS PT, Rd|RZ, desc[UR][Ra.64], Rb (golden words: om/encoder/atomg_min_test.om, 716 * oracle nvdisasm 13.0.85). ATOMG_ADD_STRONG_SYS's words with w2 op bits 23..26 = 1 (MIN) and size bits 717 * 9..11 = 2 (.64 unsigned). No destination vreg encodes RZ. */ 718 w[0] = 0x79a8 | ((uint32_t)(insn->dst_vreg < 0 ? 0xff : (dst & 0xff)) << 16) | ((uint32_t)(src1 & 0xff) << 24); 719 w[1] = (uint32_t)(src2 & 0xff) | 0x80000000u; 720 w[2] = 0x089f5500u | (uint32_t)(ureg & 0xff); 721 w[3] = insn->control ? insn->control : 0x00321e00; 722 break; 723 724 case BW_IR_ISETP_GE_U32: 725 /* ISETP.GE.U32.AND P0, PT, Ra, Rb, PT. Checked with nvdisasm -b SM121. */ 726 w[0] = 0x720c | ((uint32_t)(src1 & 0xff) << 24); 727 w[1] = (uint32_t)(src2 & 0xff); 728 w[2] = 0x03f06070; 729 w[3] = insn->control ? insn->control : 0x001fda00; 730 break; 731 732 case BW_IR_LOP3_XOR: 733 /* LOP3.LUT Rd, Ra, Rb, RZ, 0x3c, !PT. Checked with nvdisasm -b SM121. */ 734 w[0] = 0x7212 | ((uint32_t)(dst & 0xff) << 16) | ((uint32_t)(src1 & 0xff) << 24); 735 w[1] = (uint32_t)(src2 & 0xff); 736 w[2] = 0x078e3cff; 737 w[3] = insn->control ? insn->control : 0x001fca00; 738 break; 739 740 case BW_IR_FADD: 741 /* FADD Rd, Ra, Rb */ 742 w[0] = 0x7221U | ((uint32_t)(dst & 0xff) << 16) | ((uint32_t)(src1 & 0xff) << 24); 743 w[1] = (uint32_t)(src2 & 0xff); 744 w[2] = 0x00000000; 745 w[3] = insn->control ? insn->control : 0x004fc400; 746 break; 747 748 case BW_IR_FSUB: 749 /* FSUB Rd, Ra, Rb (FADD Rd, Ra, -Rb) */ 750 w[0] = 0x7221U | ((uint32_t)(dst & 0xff) << 16) | ((uint32_t)(src1 & 0xff) << 24); 751 w[1] = (uint32_t)(src2 & 0xff) | 0x80000000U; 752 w[2] = 0x00000000; 753 w[3] = insn->control ? insn->control : 0x004fc400; 754 break; 755 756 case BW_IR_FMUL: 757 /* FMUL Rd, Ra, Rb */ 758 w[0] = 0x7220U | ((uint32_t)(dst & 0xff) << 16) | ((uint32_t)(src1 & 0xff) << 24); 759 w[1] = (uint32_t)(src2 & 0xff); 760 w[2] = 0x00400000; 761 w[3] = insn->control ? insn->control : 0x004fc400; 762 break; 763 764 case BW_IR_FFMA: 765 /* FFMA Rd, Ra, Rb, Rc */ 766 w[0] = 0x7223U | ((uint32_t)(dst & 0xff) << 16) | ((uint32_t)(src1 & 0xff) << 24); 767 w[1] = (uint32_t)(src2 & 0xff); 768 w[2] = (uint32_t)(src3 & 0xff); 769 w[3] = insn->control ? insn->control : 0x004fc400; 770 break; 771 772 case BW_IR_FSETP: 773 /* FSETP.cond.AND P0, PT, Ra, Rb, PT */ 774 w[0] = 0x720bU | ((uint32_t)(dst & 0x7) << 16) | ((uint32_t)(src1 & 0xff) << 24); 775 w[1] = (uint32_t)(src2 & 0xff); 776 w[2] = insn->imm ? insn->imm : 0x03f06000; /* Default GE condition */ 777 w[3] = insn->control ? insn->control : 0x004fc400; 778 break; 779 780 case BW_IR_FSEL: 781 /* FSEL Rd, Ra, Rb, P0 */ 782 w[0] = 0x7208U | ((uint32_t)(dst & 0xff) << 16) | ((uint32_t)(src1 & 0xff) << 24); 783 w[1] = (uint32_t)(src2 & 0xff); 784 w[2] = (uint32_t)(src3 & 0x7); 785 w[3] = insn->control ? insn->control : 0x000fca00; 786 break; 787 788 case BW_IR_FMNMX_MIN: 789 /* FMNMX Rd, Ra, Rb, PT (minimum) */ 790 w[0] = 0x7209U | ((uint32_t)(dst & 0xff) << 16) | ((uint32_t)(src1 & 0xff) << 24); 791 w[1] = (uint32_t)(src2 & 0xff); 792 w[2] = 0x03800000; 793 w[3] = insn->control ? insn->control : 0x004fc400; 794 break; 795 796 case BW_IR_FMNMX_MAX: 797 /* FMNMX Rd, Ra, Rb, !PT (maximum) */ 798 w[0] = 0x7209U | ((uint32_t)(dst & 0xff) << 16) | ((uint32_t)(src1 & 0xff) << 24); 799 w[1] = (uint32_t)(src2 & 0xff); 800 w[2] = 0x07800000; 801 w[3] = insn->control ? insn->control : 0x004fc400; 802 break; 803 804 case BW_IR_I2FP: 805 /* I2FP.F32.S32 Rd, Ra */ 806 w[0] = 0x7245U | ((uint32_t)(dst & 0xff) << 16); 807 w[1] = (uint32_t)(src1 & 0xff); 808 w[2] = 0x00201400; 809 w[3] = insn->control ? insn->control : 0x004fe200; 810 break; 811 812 case BW_IR_F2I: 813 /* F2I.TRUNC.NTZ Rd, Ra */ 814 w[0] = 0x7305U | ((uint32_t)(dst & 0xff) << 16); 815 w[1] = (uint32_t)(src1 & 0xff); 816 w[2] = 0x0020f100; 817 w[3] = insn->control ? insn->control : 0x004e2200; 818 break; 819 820 case BW_IR_MUFU_RCP: 821 /* MUFU.RCP Rd, Ra */ 822 w[0] = 0x7308U | ((uint32_t)(dst & 0xff) << 16); 823 w[1] = (uint32_t)(src1 & 0xff); 824 w[2] = 0x00001000; 825 w[3] = insn->control ? insn->control : 0x000e2400; 826 break; 827 828 case BW_IR_MUFU_LG2: 829 /* MUFU.LG2 Rd, Ra (nvdisasm 13.0 -b SM121 decodes sub-op 0x0c00 as MUFU.LG2) */ 830 w[0] = 0x7308U | ((uint32_t)(dst & 0xff) << 16); 831 w[1] = (uint32_t)(src1 & 0xff); 832 w[2] = 0x00000c00; 833 w[3] = insn->control ? insn->control : 0x000e2400; 834 break; 835 836 case BW_IR_MUFU_RSQ: 837 /* MUFU.RSQ Rd, Ra */ 838 w[0] = 0x7308U | ((uint32_t)(dst & 0xff) << 16); 839 w[1] = (uint32_t)(src1 & 0xff); 840 w[2] = 0x00001400; 841 w[3] = insn->control ? insn->control : 0x000e2400; 842 break; 843 844 case BW_IR_SHFL_DOWN: 845 /* SHFL.DOWN PT, Rd, Ra, offset, 0x1f */ 846 w[0] = 0x7f89U | ((uint32_t)(dst & 0xff) << 16) | ((uint32_t)(src1 & 0xff) << 24); 847 w[1] = 0x08001f00U | (((uint32_t)insn->imm & 0x1f) << 21); 848 w[2] = 0x000e0000; 849 w[3] = insn->control ? insn->control : 0x000e2400; 850 break; 851 852 case BW_IR_SHFL_BFLY: 853 /* SHFL.BFLY PT, Rd, Ra, lane, 0x1f: SHFL.DOWN with bit 58 (w[1] bit 26) set 854 * (omega_blackwell_verify_codegen_fixtures_bfly_f2fp) */ 855 w[0] = 0x7f89U | ((uint32_t)(dst & 0xff) << 16) | ((uint32_t)(src1 & 0xff) << 24); 856 w[1] = 0x0c001f00U | (((uint32_t)insn->imm & 0x1f) << 21); 857 w[2] = 0x000e0000; 858 w[3] = insn->control ? insn->control : 0x000e2400; 859 break; 860 861 case BW_IR_F2FP_BF16_PACK: 862 /* F2FP.BF16.F32.PACK_AB Rd, Ra, Rb (omega_blackwell_verify_codegen_fixtures_bfly_f2fp). The default control 863 * is FMNMX's: the kernels emitted this as a patched FMNMX_MAX before R5 and their bytes must not move. */ 864 w[0] = 0x723eU | ((uint32_t)(dst & 0xff) << 16) | ((uint32_t)(src1 & 0xff) << 24); 865 w[1] = (uint32_t)(src2 & 0xff); 866 w[2] = 0x000010ff; 867 w[3] = insn->control ? insn->control : 0x004fc400; 868 break; 869 870 case BW_IR_LDS: 871 /* LDS Rd, [Ra] */ 872 w[0] = 0x7984U | ((uint32_t)(dst & 0xff) << 16) | ((uint32_t)(src1 & 0xff) << 24); 873 w[1] = 0x00000000; 874 w[2] = (uint32_t)(insn->imm & 0xffff); 875 w[3] = insn->control ? insn->control : 0x000fe200; 876 break; 877 878 case BW_IR_STS: 879 /* STS [Ra], Rb */ 880 w[0] = 0x7388U | ((uint32_t)(src1 & 0xff) << 24); 881 w[1] = (uint32_t)(src2 & 0xff); 882 w[2] = (uint32_t)(insn->imm & 0xffff); 883 w[3] = insn->control ? insn->control : 0x000fe200; 884 break; 885 886 case BW_IR_EXIT: 887 w[0] = 0x0000794d; /* generic guard handling below */ 888 w[1] = 0x00000000; 889 w[2] = 0x03800000; 890 w[3] = insn->control ? insn->control : 0x000fea00; 891 break; 892 893 case BW_IR_BRA: { 894 /* sm_121 BRA. Distance is (target - this) - 1, split across the 895 * low six bits at instruction bits 18..23 and the rest at bit 34. 896 * Predicate bits 12..15: 7 = always, 0 = @P0, 8 = @!P0. 897 * A zero delta with no predicate keeps the self-branch word the 898 * existing kernels already emit after EXIT. */ 899 int32_t delta = (int32_t)insn->imm; 900 int32_t rel = delta - 1; 901 uint32_t pred_field = 0x7u; 902 if (insn->predicate_p0) 903 pred_field = insn->predicate_not ? 0x8u : 0x0u; 904 w[0] = 0x947u | (pred_field << 12) | (((uint32_t)rel & 0x3fu) << 18); 905 w[1] = (uint32_t)(rel >> 6) << 2; 906 w[2] = (rel < 0) ? 0x0383ffffu : 0x03800000u; 907 if (insn->control) 908 w[3] = insn->control; 909 else if (delta == 0 && !insn->predicate_p0) 910 w[3] = 0x000fc000u; 911 else 912 w[3] = 0x000fea00u; 913 break; 914 } 915 916 case BW_IR_BSSY: { 917 /* BSSY.RECONVERGENT Bn, target (omega #308). nvcc 13.0.88 -arch=sm_121: 918 * [0070] BSSY.RECONVERGENT B0, 0x140 ; 0x000000c000007945 0x000fe20003800200 919 * opcode 0x945 (NAK sm70_encode.rs:4165), barrier id at bits 16..19 (set_bar_dst 920 * 16..20), guard predicate at bits 12..15 like BRA, relative byte offset 921 * target - (this + 16) at bit 32 (NAK: field 34.. in 4-byte units), the cond 922 * predicate PT at bits 87..89 (0x0380 << 16 in w[2]) and bit 73 (0x200 in w[2]), 923 * which the disassembler prints as .RECONVERGENT; nvcc sets it on every BSSY/BSYNC 924 * for sm_121 and Mesa NAK does not. We follow nvcc. The target is the instruction 925 * after the matching BSYNC, so the delta is at least 2; smaller is refused. */ 926 int32_t delta = (int32_t)insn->imm; 927 if (delta < 2 || insn->bar_reg >= BW_RECONV_MAX_BAR) return -1; 928 uint32_t pred_field = 0x7u; 929 if (insn->predicate_p0) 930 pred_field = insn->predicate_not ? 0x8u : 0x0u; 931 w[0] = 0x945u | (pred_field << 12) | ((uint32_t)insn->bar_reg << 16); 932 w[1] = (uint32_t)(delta - 1) * 16u; 933 w[2] = 0x03800200u; 934 w[3] = insn->control ? insn->control : 0x000fe200u; 935 break; 936 } 937 938 case BW_IR_BSYNC: { 939 /* BSYNC.RECONVERGENT Bn: [0130] BSYNC.RECONVERGENT B0 ; 0x0000000000007941 0x000fea0003800200 940 * opcode 0x941 (NAK sm70_encode.rs:4179), barrier id bits 16..19, cond PT, bit 73. */ 941 if (insn->bar_reg >= BW_RECONV_MAX_BAR) return -1; 942 uint32_t pred_field = 0x7u; 943 if (insn->predicate_p0) 944 pred_field = insn->predicate_not ? 0x8u : 0x0u; 945 w[0] = 0x941u | (pred_field << 12) | ((uint32_t)insn->bar_reg << 16); 946 w[1] = 0; 947 w[2] = 0x03800200u; 948 w[3] = insn->control ? insn->control : 0x000fea00u; 949 break; 950 } 951 952 case BW_IR_MUFU_EX2: 953 /* MUFU.EX2 Rd, Ra. nvdisasm 13.0 -b SM121: 0x7308 with sub-op 0x0800 decodes 954 * "MUFU.EX2 R2, R4" (RCP is 0x1000, RSQ 0x1400 above). */ 955 w[0] = 0x7308U | ((uint32_t)(dst & 0xff) << 16); 956 w[1] = (uint32_t)(src1 & 0xff); 957 w[2] = 0x00000800; 958 w[3] = insn->control ? insn->control : 0x000e2400; 959 break; 960 961 case BW_IR_BAR_SYNC: 962 /* BAR.SYNC.DEFER_BLOCKING 0x0: the exact words src/omega_numeric.c:885 runs on 963 * the chip (nvdisasm: "BAR.SYNC.DEFER_BLOCKING 0x0"). No registers. */ 964 w[0] = 0x00007b1dU; 965 w[1] = 0x00000000; 966 w[2] = 0x00010000; 967 w[3] = insn->control ? insn->control : 0x000fec00; 968 break; 969 970 case BW_IR_LDS128: { 971 /* LDS.128 Rd..Rd+3, [Ra+imm]. nvcc 13.0.88 -arch=sm_121 words (fixtures below): 972 * LDS.128 R4, [R20] ; 0x0000000014047984 0x000fe80000000c00 973 * LDS.128 R8, [R20+0x40] ; 0x0000400014087984 0x000e280000000c00 974 * imm = byte offset at w[1] bits 8..31; size field 6 (128) at w[2] bits 9..11. */ 975 if (insn->imm >= (1u << 23)) return -1; 976 w[0] = 0x7984U | ((uint32_t)(dst & 0xff) << 16) | ((uint32_t)(src1 & 0xff) << 24); 977 w[1] = (insn->imm & 0xffffffU) << 8; 978 w[2] = 0x00000c00U; 979 w[3] = insn->control ? insn->control : 0x000fe800; 980 break; 981 } 982 983 case BW_IR_LDS32: 984 /* LDS Rd, [Ra+URZ] (32-bit), the src/omega_numeric.c:886 form. */ 985 w[0] = 0x7984U | ((uint32_t)(dst & 0xff) << 16) | ((uint32_t)(src1 & 0xff) << 24); 986 w[1] = 0x000000ff; 987 w[2] = 0x08000800; 988 w[3] = insn->control ? insn->control : 0x000e2800; 989 break; 990 991 case BW_IR_STS32: 992 /* STS [Ra+URZ], Rb (32-bit), the src/omega_numeric.c:884 form. */ 993 w[0] = 0x7988U | ((uint32_t)(src1 & 0xff) << 24); 994 w[1] = (uint32_t)(src2 & 0xff); 995 w[2] = 0x080008ffU; 996 w[3] = insn->control ? insn->control : 0x000fe200; 997 break; 998 999 /* Prime race cut (2026-10-05). An absent operand is RZ for these three ops. */ 1000 case BW_IR_LOP3_LUT: { 1001 /* LOP3.LUT Rd, Ra, Rb, Rc, imm8, !PT. LOP3_XOR above is this form with Rc = RZ, LUT 0x3c. */ 1002 uint32_t a = (insn->src1_vreg >= 0) ? (uint32_t)(src1 & 0xff) : 0xffU; 1003 uint32_t b = (insn->src2_vreg >= 0) ? (uint32_t)(src2 & 0xff) : 0xffU; 1004 w[0] = 0x7212U | ((uint32_t)(dst & 0xff) << 16) | (a << 24); 1005 w[1] = b; 1006 w[2] = 0x078e0000U | ((insn->imm & 0xffU) << 8) | (uint32_t)(src3 & 0xff); 1007 w[3] = insn->control ? insn->control : 0x001fca00; 1008 break; 1009 } 1010 1011 case BW_IR_SHF_L_U32: { 1012 /* SHF.L.U32 Rd, Ra, Rb, Rc: register-amount form (0x7219) of SHF_R's 0x7819, with the 1013 * .R (w[2] bit 12) and .HI (bit 16) flags clear. Rc is the funnel high word (RZ). */ 1014 uint32_t a = (insn->src1_vreg >= 0) ? (uint32_t)(src1 & 0xff) : 0xffU; 1015 uint32_t b = (insn->src2_vreg >= 0) ? (uint32_t)(src2 & 0xff) : 0xffU; 1016 w[0] = 0x7219U | ((uint32_t)(dst & 0xff) << 16) | (a << 24); 1017 w[1] = b; 1018 w[2] = 0x00000600U | (uint32_t)(src3 & 0xff); 1019 w[3] = insn->control ? insn->control : 0x001fca00; 1020 break; 1021 } 1022 1023 case BW_IR_IMAD_HI_U32: { 1024 /* IMAD.HI.U32 Rd, Ra, Rb, Rc: opcode 0x227 with the IMAD.WIDE.U32 modifier word. */ 1025 uint32_t a = (insn->src1_vreg >= 0) ? (uint32_t)(src1 & 0xff) : 0xffU; 1026 uint32_t b = (insn->src2_vreg >= 0) ? (uint32_t)(src2 & 0xff) : 0xffU; 1027 w[0] = 0x7227U | ((uint32_t)(dst & 0xff) << 16) | (a << 24); 1028 w[1] = b; 1029 w[2] = 0x078e0000U | (uint32_t)(src3 & 0xff); 1030 w[3] = insn->control ? insn->control : 0x001fca00; 1031 break; 1032 } 1033 1034 case BW_IR_I2FP_U32: 1035 w[0] = 0x7245U | ((uint32_t)(dst & 0xff) << 16); 1036 w[1] = (uint32_t)(src1 & 0xff); 1037 w[2] = 0x00201000; 1038 w[3] = insn->control ? insn->control : 0x004fe200; 1039 break; 1040 case BW_IR_F2I_U32: 1041 w[0] = 0x7305U | ((uint32_t)(dst & 0xff) << 16); 1042 w[1] = (uint32_t)(src1 & 0xff); 1043 w[2] = 0x0020f000; 1044 w[3] = insn->control ? insn->control : 0x004e2200; 1045 break; 1046 /* Authoritative E1_CVT words: omega_numeric.c, sm_121 ptxas/nvdisasm fixtures. */ 1047 case BW_IR_F16_TO_F32: 1048 w[0] = 0xff007230U | ((uint32_t)(dst & 0xff) << 16); 1049 w[1] = 0x20000000U | (uint32_t)(src1 & 0xff); 1050 w[2] = 0x00004100U; 1051 w[3] = insn->control ? insn->control : 0x010fca00U; 1052 break; 1053 case BW_IR_F32_TO_F16: 1054 w[0] = 0x7304U | ((uint32_t)(dst & 0xff) << 16); 1055 w[1] = (uint32_t)(src1 & 0xff); 1056 w[2] = 0x00200800U; 1057 w[3] = insn->control ? insn->control : 0x010e2800U; 1058 break; 1059 /* Register forms derived offline from our CUDA probes with nvcc 13.0.88 1060 * and nvdisasm 13.0.85, sm_121. No toolkit is used at runtime. */ 1061 case BW_IR_DADD: 1062 case BW_IR_DMUL: 1063 case BW_IR_DFMA: 1064 w[0] = (insn->op == BW_IR_DADD ? 0x7229u : 1065 insn->op == BW_IR_DMUL ? 0x7228u : 0x722bu) | 1066 ((uint32_t)(dst & 0xff) << 16) | ((uint32_t)(src1 & 0xff) << 24); 1067 w[1] = insn->op == BW_IR_DADD ? 0u : (uint32_t)(src2 & 0xff); 1068 w[2] = insn->op == BW_IR_DADD ? (uint32_t)(src2 & 0xff) : 1069 insn->op == BW_IR_DFMA ? (uint32_t)(src3 & 0xff) : 0u; 1070 w[3] = insn->control ? insn->control : 0x000e2400u; 1071 break; 1072 case BW_IR_DSETP: 1073 /* Same comparison mask as FSETP, but the destination predicate is 1074 * in word 2, not the GPR destination field. This IR exposes P0. */ 1075 if (insn->dst_vreg >= 0) return -1; 1076 w[0] = 0x722au | ((uint32_t)(src1 & 0xff) << 24); 1077 w[1] = (uint32_t)(src2 & 0xff); 1078 w[2] = insn->imm ? insn->imm : 0x03f06000u; 1079 w[3] = insn->control ? insn->control : 0x000e2400u; 1080 break; 1081 case BW_IR_MUFU_RCP64H: 1082 case BW_IR_MUFU_RSQ64H: 1083 case BW_IR_F64_F32: 1084 case BW_IR_F32_F64: 1085 case BW_IR_F64_S32: 1086 case BW_IR_F64_U32: 1087 case BW_IR_S32_F64: 1088 w[0] = (insn->op <= BW_IR_MUFU_RSQ64H ? 0x7308u : 1089 insn->op <= BW_IR_F32_F64 ? 0x7310u : 1090 insn->op <= BW_IR_F64_U32 ? 0x7312u : 0x7311u) | 1091 ((uint32_t)(dst & 0xff) << 16); 1092 w[1] = (uint32_t)(src1 & 0xff); 1093 w[2] = insn->op == BW_IR_MUFU_RCP64H ? 0x1800u : 1094 insn->op == BW_IR_MUFU_RSQ64H ? 0x1c00u : 1095 insn->op == BW_IR_F64_F32 ? 0x201800u : 1096 insn->op == BW_IR_F32_F64 ? 0x301000u : 1097 insn->op == BW_IR_F64_S32 ? 0x201c00u : 1098 insn->op == BW_IR_F64_U32 ? 0x201800u : 0x30d100u; 1099 w[3] = insn->control ? insn->control : 0x000e2400u; 1100 break; 1101 default: 1102 return -1; 1103 } 1104 /* DRA-483 C1b oracle correction: bits 12..15 hold the guard on every form. 1105 * LDCU and LDCU.64 use the uniform predicate namespace, so P0 is refused. 1106 * This matches om/encoder/bw_enc_sm121.om and the reviewed F1 proposal. */ 1107 if ((insn->op == BW_IR_LDCU || insn->op == BW_IR_LDCU64) && insn->predicate_p0) 1108 return -1; 1109 if (insn->predicate_p0) { 1110 w[0] = (w[0] & ~0xf000u) | ((insn->predicate_not ? 0x8u : 0x0u) << 12); 1111 } 1112 return 0; 1113 } 1114 #endif /* C encoder, migration-pending */ 1115 1116 #ifdef BW_ENC_C_PRIMARY 1117 static int encode_single_insn(const BlackwellIRInsn *insn, const OmegaRegAlloc *ra, uint32_t w[4]) { 1118 return bw_c_encode_insn(insn, ra, w); 1119 } 1120 #else 1121 #ifdef OMEGA_GPU_NO_POSIX 1122 #include "omega_native.h" 1123 #define omega_bw_enc_sm121 omega2_bw_enc_sm121 1124 #else 1125 #include "cudapp_native.h" 1126 #endif 1127 /* The .om unit reads the IR as 8 ulongs per instruction and the allocation as one 288-entry table; these are the 1128 * layouts its source documents (om/encoder/bw_enc_sm121.om), so the enum values and sizes are pinned here. */ 1129 #define BW_OM_INS_LEN 8192u /* BW_MAX_IR_INSNS * 8 */ 1130 #define BW_OM_PHYS_LEN 288u /* BW_MAX_VREGS + BW_MAX_UVREGS */ 1131 #define BW_OM_CODE_LEN 16384u /* BW_MAX_IR_INSNS * 16 */ 1132 _Static_assert(BW_MAX_IR_INSNS == 1024 && BW_MAX_VREGS == 256 && BW_MAX_UVREGS == 32, "bw_enc_sm121.om array sizes"); 1133 _Static_assert(BW_IR_NOP == 0 && BW_IR_MOV_RZ == 1 && BW_IR_MOV_IMM == 2 && BW_IR_S2R == 3 && BW_IR_LDC == 4 && 1134 BW_IR_LDC64 == 5 && BW_IR_LDCU == 6 && BW_IR_LDCU64 == 7 && BW_IR_IMAD == 8 && BW_IR_IMAD_WIDE == 9 && 1135 BW_IR_ISETP_GE == 10 && BW_IR_LDG_E == 11 && BW_IR_STG_E == 12 && BW_IR_STG_EF == 13 && 1136 BW_IR_IADD3 == 14 && BW_IR_HMMA_F16 == 15 && BW_IR_HMMA_BF16 == 16 && BW_IR_SHF_R == 17 && 1137 BW_IR_LOP3_AND == 18 && BW_IR_LDG_E_U16 == 19 && BW_IR_LDG_STRONG_SYS == 20 && BW_IR_LDG_MMIO == 21 && 1138 BW_IR_STG_STRONG_SYS == 22 && BW_IR_MEMBAR_ALL_SYS == 23 && BW_IR_MEMBAR_SC_SYS == 24 && 1139 BW_IR_CCTL_IVALL == 25 && BW_IR_ATOMG_ADD_STRONG_SYS == 26 && BW_IR_ATOMG_EXCH_STRONG_SYS == 27 && 1140 BW_IR_ISETP_GE_U32 == 28 && BW_IR_LOP3_XOR == 29, "bw_enc_sm121.om opcode numbers 0-29"); 1141 _Static_assert(BW_IR_FADD == 30 && BW_IR_FSUB == 31 && BW_IR_FMUL == 32 && BW_IR_FFMA == 33 && BW_IR_FSETP == 34 && 1142 BW_IR_FSEL == 35 && BW_IR_FMNMX_MIN == 36 && BW_IR_FMNMX_MAX == 37 && BW_IR_I2FP == 38 && 1143 BW_IR_F2I == 39 && BW_IR_MUFU_RCP == 40 && BW_IR_MUFU_RSQ == 41 && BW_IR_SHFL_DOWN == 42 && 1144 BW_IR_LDS == 43 && BW_IR_STS == 44 && BW_IR_EXIT == 45 && BW_IR_BRA == 46 && BW_IR_MUFU_EX2 == 47 && 1145 BW_IR_BAR_SYNC == 48 && BW_IR_LDS32 == 49 && BW_IR_STS32 == 50 && BW_IR_LOP3_LUT == 51 && 1146 BW_IR_SHF_L_U32 == 52 && BW_IR_IMAD_HI_U32 == 53 && BW_IR_BSSY == 54 && BW_IR_BSYNC == 55 && 1147 BW_IR_LDG_E_64 == 56 && BW_IR_LDG_E_128 == 57 && BW_IR_LDS128 == 58 && BW_IR_MUFU_LG2 == 59, 1148 "bw_enc_sm121.om opcode numbers 30-59"); 1149 _Static_assert(BW_IR_SHFL_BFLY == 60 && BW_IR_F2FP_BF16_PACK == 61 && BW_IR_ATOMG_ADD_F32_STRONG_SYS == 62 && 1150 BW_IR_ATOMG_ADD_F32_STRONG_GPU == 63 && BW_IR_ATOMG_MIN_U64_STRONG_SYS == 64, 1151 "bw_enc_sm121.om opcode numbers 60-64"); 1152 _Static_assert(BW_IR_I2FP_U32 == 65 && BW_IR_F2I_U32 == 66 && 1153 BW_IR_F16_TO_F32 == 67 && BW_IR_F32_TO_F16 == 68, 1154 "bw_enc_sm121.om append-only opcode numbers 65-68"); 1155 _Static_assert(BW_IR_DADD == 69 && BW_IR_DMUL == 70 && BW_IR_DFMA == 71 && 1156 BW_IR_DSETP == 72 && BW_IR_MUFU_RCP64H == 73 && BW_IR_MUFU_RSQ64H == 74 && 1157 BW_IR_F64_F32 == 75 && BW_IR_F32_F64 == 76 && BW_IR_F64_S32 == 77 && 1158 BW_IR_F64_U32 == 78 && BW_IR_S32_F64 == 79 && BW_IR_LAST == 79, 1159 "bw_enc_sm121.om append-only opcode numbers 69-79"); 1160 1161 /* Encodes n instructions through the .om unit into code (BW_OM_CODE_LEN bytes). pad != 0 is program mode (NOP 1162 * padding to a multiple of 8, at least 32). Returns the bytes written; 0 = refused. */ 1163 static size_t bw_om_encode(const BlackwellIRInsn *insns, size_t n, const OmegaRegAlloc *ra, int pad, uint8_t *code) { 1164 if (n > BW_MAX_IR_INSNS) return 0; 1165 uint64_t ins[BW_OM_INS_LEN], phys[BW_OM_PHYS_LEN]; 1166 for (size_t i = 0; i < n; i++) { 1167 const BlackwellIRInsn *in = &insns[i]; 1168 uint64_t *r = &ins[i * 8]; 1169 #ifdef CUDAPP_MIGRATION_PENDING_C_CODEGEN_BASE 1170 r[0] = (uint64_t)(int64_t)(int)in->op; 1171 r[1] = (uint64_t)(int64_t)in->dst_vreg; 1172 r[2] = (uint64_t)(int64_t)in->src1_vreg; 1173 r[3] = (uint64_t)(int64_t)in->src2_vreg; 1174 r[4] = (uint64_t)(int64_t)in->src3_vreg; 1175 r[5] = (uint64_t)(int64_t)in->ureg; 1176 r[6] = (uint64_t)in->imm | (uint64_t)in->control << 32; 1177 r[7] = (uint64_t)in->is_uniform | (uint64_t)in->predicate_p0 << 1 | (uint64_t)in->predicate_not << 2 | 1178 (uint64_t)in->bar_reg << 8 | (uint64_t)in->dst_subreg << 16 | (uint64_t)in->src1_subreg << 24 | 1179 (uint64_t)in->src2_subreg << 32 | (uint64_t)in->src3_subreg << 40; 1180 #else 1181 if (omega_bw_fields_pack(in, r) != 0) return 0; 1182 #endif 1183 } 1184 for (size_t i = n * 8; i < BW_OM_INS_LEN; i++) ins[i] = 0; 1185 for (int i = 0; i < BW_MAX_VREGS; i++) phys[i] = (uint64_t)(int64_t)ra->vreg_to_phys[i]; 1186 for (int i = 0; i < BW_MAX_UVREGS; i++) phys[BW_MAX_VREGS + i] = (uint64_t)(int64_t)ra->uvreg_to_phys[i]; 1187 uint64_t bytes = 0; 1188 if (omega_bw_enc_sm121(&bytes, ins, BW_OM_INS_LEN, phys, BW_OM_PHYS_LEN, (uint64_t)n, pad ? 1u : 0u, code, 1189 BW_OM_CODE_LEN) != OMEGA_OK) 1190 return 0; 1191 return (size_t)bytes; 1192 } 1193 1194 #ifdef CUDAPP_MIGRATION_PENDING_ENCODER_DIFF 1195 /* One line per agreement, appended; any disagreement aborts the process after naming it. */ 1196 static void bw_enc_diff_note(const char *what, size_t insns, const uint8_t *code, size_t len, int agree) { 1197 char hex[17] = "-"; 1198 if (len) { 1199 uint8_t d[SHA256_DIGEST_SIZE]; sha256_hash(code, len, d); 1200 for (int i = 0; i < 8; i++) snprintf(hex + 2 * i, 3, "%02x", (unsigned)d[i]); 1201 } 1202 const char *path = getenv("CUDAPP_ENCODER_DIFF_LOG"); 1203 if (path && path[0]) { 1204 int fd = open(path, O_WRONLY | O_CREAT | O_APPEND | O_CLOEXEC, 0600); 1205 if (fd >= 0) { 1206 char line[160]; 1207 int k = snprintf(line, sizeof line, "%s insns=%zu bytes=%zu sha16=%s %s\n", what, insns, len, hex, 1208 agree ? "SAME" : "DIFFER"); 1209 if (k > 0 && write(fd, line, (size_t)k) != k) BW_DIAG("encoder diff: short log write\n"); 1210 close(fd); 1211 } 1212 } 1213 if (!agree) { 1214 BW_DIAG("ENCODER DIFF: .om and C encoders disagree (%s, %zu insns)\n", what, insns); 1215 abort(); 1216 } 1217 } 1218 #endif 1219 1220 static int encode_single_insn(const BlackwellIRInsn *insn, const OmegaRegAlloc *ra, uint32_t w[4]) { 1221 uint8_t code[BW_OM_CODE_LEN]; 1222 int rc = bw_om_encode(insn, 1, ra, 0, code) == 16 ? 0 : -1; 1223 memset(w, 0, 16); 1224 #ifdef CUDAPP_MIGRATION_PENDING_C_CODEGEN_BASE 1225 if (rc == 0) 1226 for (int k = 0; k < 4; k++) 1227 w[k] = (uint32_t)code[4 * k] | (uint32_t)code[4 * k + 1] << 8 | (uint32_t)code[4 * k + 2] << 16 | 1228 (uint32_t)code[4 * k + 3] << 24; 1229 #else 1230 if (rc == 0) { 1231 uint64_t raw[16], record[8] = {0}, result = 0; 1232 for (int k = 0; k < 16; ++k) raw[k] = code[k]; 1233 if (omega_cg_fields(&result, 2, raw, 16, record, 8) != OMEGA_OK || result != 0) rc = -1; 1234 else for (int k = 0; k < 4; ++k) w[k] = (uint32_t)raw[k]; 1235 } 1236 #endif 1237 #ifdef CUDAPP_MIGRATION_PENDING_ENCODER_DIFF 1238 uint32_t c[4]; 1239 int crc = bw_c_encode_insn(insn, ra, c); 1240 bw_enc_diff_note("insn", 1, rc == 0 ? code : NULL, rc == 0 ? 16 : 0, 1241 crc == rc && (rc != 0 || memcmp(c, w, 16) == 0)); 1242 #endif 1243 return rc; 1244 } 1245 #endif 1246 1247 /* Prime race cut (2026-10-05): golden words for LOP3_LUT, SHF_L_U32 and IMAD_HI_U32. 1248 * Each expected (w0, w1, w2) is the low 96 bits of an instruction nvcc 13.0.88 emitted 1249 * for sm_121 (nvcc -arch=sm_121 -cubin, cuobjdump -sass; source and dump in 1250 * bench/prime_race/tests/encoding_oracle.md). Control words (w3) are scheduler output and 1251 * not compared. nvcc is an offline oracle only; nothing here links CUDA. 0 = all good. */ 1252 int omega_blackwell_verify_codegen_fixtures_intops(void) { 1253 OmegaRegAlloc ra; 1254 memset(&ra, 0, sizeof(ra)); 1255 static const int phys[] = { 0, 2, 6, 7, 9, 11, 13 }; 1256 for (int i = 0; i < (int)(sizeof phys / sizeof phys[0]); i++) ra.vreg_to_phys[i] = phys[i]; 1257 enum { R0 = 0, R2, R6, R7, R9, R11, R13, RZ = -1 }; 1258 static const struct { 1259 BlackwellIROpcode op; int d, a, b, c; uint32_t imm; uint32_t w0, w1, w2; const char *text; 1260 } fx[] = { 1261 { BW_IR_LOP3_LUT, R9, R7, R0, RZ, 0xfc, 0x07097212U, 0x00000000U, 0x078efcffU, "LOP3.LUT R9, R7, R0, RZ, 0xfc, !PT" }, 1262 { BW_IR_LOP3_LUT, R11, R6, R7, R0, 0x78, 0x060b7212U, 0x00000007U, 0x078e7800U, "LOP3.LUT R11, R6, R7, R0, 0x78, !PT" }, 1263 { BW_IR_LOP3_LUT, R13, R6, R7, R0, 0x10, 0x060d7212U, 0x00000007U, 0x078e1000U, "LOP3.LUT R13, R6, R7, R0, 0x10, !PT" }, 1264 { BW_IR_LOP3_LUT, R7, RZ, R9, RZ, 0x33, 0xff077212U, 0x00000009U, 0x078e33ffU, "LOP3.LUT R7, RZ, R9, RZ, 0x33, !PT" }, 1265 { BW_IR_SHF_L_U32, R7, R9, R2, RZ, 0, 0x09077219U, 0x00000002U, 0x000006ffU, "SHF.L.U32 R7, R9, R2, RZ" }, 1266 { BW_IR_SHF_L_U32, R7, R0, R7, RZ, 0, 0x00077219U, 0x00000007U, 0x000006ffU, "SHF.L.U32 R7, R0, R7, RZ" }, 1267 { BW_IR_IMAD_HI_U32, R9, R9, R0, R6, 0, 0x09097227U, 0x00000000U, 0x078e0006U, "IMAD.HI.U32 R9, R9, R0, R6" }, 1268 { BW_IR_IMAD_HI_U32, R7, R0, R7, RZ, 0, 0x00077227U, 0x00000007U, 0x078e00ffU, "IMAD.HI.U32 R7, R0, R7, RZ" }, 1269 }; 1270 for (int i = 0; i < (int)(sizeof fx / sizeof fx[0]); i++) { 1271 BlackwellIRInsn in; 1272 memset(&in, 0, sizeof in); 1273 in.op = fx[i].op; in.dst_vreg = fx[i].d; in.src1_vreg = fx[i].a; in.src2_vreg = fx[i].b; 1274 in.src3_vreg = fx[i].c; in.ureg = -1; in.imm = fx[i].imm; 1275 uint32_t w[4]; 1276 if (!omega_fv_status(encode_single_insn(&in, &ra, w), 1)) return omega_fv_error(i, 1); 1277 if (!omega_fv_words(w, fx[i].w0, fx[i].w1, fx[i].w2, 0, 7)) return omega_fv_error(i, 2); 1278 } 1279 return 0; 1280 } 1281 1282 int omega_blackwell_encode_one(const BlackwellIRInsn *insn, uint32_t w[4]) { 1283 if (!insn || !w) return -1; 1284 OmegaRegAlloc ra; 1285 memset(&ra, 0, sizeof ra); 1286 for (int i = 0; i < BW_MAX_VREGS; i++) ra.vreg_to_phys[i] = i; 1287 for (int i = 0; i < BW_MAX_UVREGS; i++) ra.uvreg_to_phys[i] = i; 1288 return encode_single_insn(insn, &ra, w); 1289 } 1290 1291 /* omega #328: golden words for the fragment-major matmul. Oracle: nvcc 13.0.88 -arch=sm_121 1292 * -cubin -O3, cuobjdump -sass, on a kernel with uint2 / uint4 loads at immediate offsets and a 1293 * dependent mma.m16n8k16 bf16 chain (source and dump sealed in 1294 * ~/workspace/investigations/2026-10-07-qwen3-decode-matmul/oracle/frag_plain.{cu,sass}): 1295 * LDG.E.64 R2, desc[UR8][R30.64] ; 0x000000081e027981 0x000ea8000c1e1b00 1296 * LDG.E.64 R28, desc[UR8][R30.64+0x100] ; 0x000100081e1c7981 0x000ee8000c1e1b00 1297 * LDG.E.128 R8, desc[UR8][R32.64] ; 0x0000000820087981 0x000ea8000c1e1d00 1298 * LDG.E.128 R20, desc[UR8][R32.64+0x1000] ; 0x0010000820147981 0x000e9e000c1e1d00 1299 * HMMA.16816.F32.BF16 R8, R12, R6, R8 ; 0x000000060c08723c 0x010fe20000041808 1300 * NOP ; 0x0000000000007918 0x000fdc0000000000 1301 * The dependent HMMA chain nvcc emits is HMMA (stall 15) then NOP (stall 14): 29 cycles from 1302 * one HMMA to the next that reads its accumulator. w3 is compared for the NOP only (it is the 1303 * whole point of that instruction). 0 = all good. */ 1304 int omega_blackwell_verify_codegen_fixtures_fragmm(void) { 1305 static const struct { 1306 BlackwellIROpcode op; int d, a, b, c, u; uint32_t imm, control; uint32_t w0, w1, w2, w3; const char *text; 1307 } fx[] = { 1308 { BW_IR_LDG_E_64, 2, 30, -1, -1, 8, 0, 0, 0x1e027981U, 0x00000008U, 0x0c1e1b00U, 0, "LDG.E.64 R2, desc[UR8][R30.64]" }, 1309 { BW_IR_LDG_E_64, 28, 30, -1, -1, 8, 0x100, 0, 0x1e1c7981U, 0x00010008U, 0x0c1e1b00U, 0, "LDG.E.64 R28, desc[UR8][R30.64+0x100]" }, 1310 { BW_IR_LDG_E_128, 8, 32, -1, -1, 8, 0, 0, 0x20087981U, 0x00000008U, 0x0c1e1d00U, 0, "LDG.E.128 R8, desc[UR8][R32.64]" }, 1311 { BW_IR_LDG_E_128, 20, 32, -1, -1, 8, 0x1000, 0, 0x20147981U, 0x00100008U, 0x0c1e1d00U, 0, "LDG.E.128 R20, desc[UR8][R32.64+0x1000]" }, 1312 { BW_IR_HMMA_BF16, 8, 12, 6, 8, -1, 0, 0, 0x0c08723cU, 0x00000006U, 0x00041808U, 0, "HMMA.16816.F32.BF16 R8, R12, R6, R8" }, 1313 { BW_IR_NOP, -1, -1, -1, -1, -1, 0, 0x000fdc00U, 0x00007918U, 0, 0, 0x000fdc00U, "NOP (stall 14)" }, 1314 /* LDS.128 with an immediate offset (attention batched loads, 2026-10-12): nvcc 13.0.88 -arch=sm_121 on 1315 * float4 shared reads at register + constant offsets (fixture source in the omega speed/attention-batched-loads PR) */ 1316 { BW_IR_LDS128, 4, 20, -1, -1, -1, 0, 0, 0x14047984U, 0x00000000U, 0x00000c00U, 0, "LDS.128 R4, [R20]" }, 1317 { BW_IR_LDS128, 8, 20, -1, -1, -1, 0x40, 0, 0x14087984U, 0x00004000U, 0x00000c00U, 0, "LDS.128 R8, [R20+0x40]" }, 1318 { BW_IR_LDS128, 12, 20, -1, -1, -1, 0x100, 0, 0x140c7984U, 0x00010000U, 0x00000c00U, 0, "LDS.128 R12, [R20+0x100]" }, 1319 /* STG.E with an immediate offset (blocked prefill matmul, 2026-10-12): nvcc 13.0.88 -arch=sm_121 on float stores 1320 * at register + constant offsets (~/.claude/jobs/`ade3c837`/tmp/stg-oracle/stg.cu); the second word is the 1321 * offset-free STG word with UR4 */ 1322 { BW_IR_STG_E, -1, 4, 8, -1, 4, 0, 0, 0x04007986U, 0x00000008U, 0x0c101904U, 0, "STG.E desc[UR4][R4.64], R8" }, 1323 { BW_IR_STG_E, -1, 4, 9, -1, 4, 0x4, 0, 0x04007986U, 0x00000409U, 0x0c101904U, 0, "STG.E desc[UR4][R4.64+0x4], R9" }, 1324 { BW_IR_STG_E, -1, 4, 10, -1, 4, 0x4000, 0, 0x04007986U, 0x0040000aU, 0x0c101904U, 0, "STG.E desc[UR4][R4.64+0x4000], R10" }, 1325 { BW_IR_STG_E, -1, 6, 11, -1, 4, 0xfff00000u, 0, 0x06007986U, 0xf000000bU, 0x0c101904U, 0, "STG.E desc[UR4][R6.64+-0x100000], R11" }, 1326 }; 1327 for (int i = 0; i < (int)(sizeof fx / sizeof fx[0]); i++) { 1328 BlackwellIRInsn in; 1329 memset(&in, 0, sizeof in); 1330 in.op = fx[i].op; in.dst_vreg = fx[i].d; in.src1_vreg = fx[i].a; in.src2_vreg = fx[i].b; 1331 in.src3_vreg = fx[i].c; in.ureg = fx[i].u; in.imm = fx[i].imm; in.control = fx[i].control; 1332 uint32_t w[4]; 1333 if (!omega_fv_status(omega_blackwell_encode_one(&in, w), 1)) return omega_fv_error(i, 1); 1334 if (!omega_fv_optional(w, fx[i].w0, fx[i].w1, fx[i].w2, fx[i].w3)) return omega_fv_error(i, 2); 1335 } 1336 /* offsets outside the signed 24-bit field are refused, not wrapped */ 1337 BlackwellIRInsn big; 1338 memset(&big, 0, sizeof big); 1339 big.op = BW_IR_LDG_E_64; big.dst_vreg = 2; big.src1_vreg = 30; big.src2_vreg = -1; big.src3_vreg = -1; big.ureg = 8; 1340 big.imm = 8388608U; 1341 uint32_t w[4]; 1342 if (!omega_fv_status(omega_blackwell_encode_one(&big, w), 0)) return -100; 1343 big.imm = 4286578688U; 1344 if (!omega_fv_status(omega_blackwell_encode_one(&big, w), 1)) return -101; 1345 return 0; 1346 } 1347 1348 /* omega #308: BSSY / BSYNC golden words. Oracle: nvcc 13.0.88 -arch=sm_121 -cubin -O3 on the 1349 * kernels in docs/gpu-reconvergence-308.md, cuobjdump -sass (low 64-bit word, then high; the 1350 * top 23 bits of the high word are the control word and are not compared here): 1351 * k_loop [0070] BSSY.RECONVERGENT B0, 0x140 ; 0x000000c000007945 0x000fe20003800200 (delta 13) 1352 * k_nested [0070] BSSY.RECONVERGENT B0, 0x300 ; 0x0000028000007945 0x000fe20003800200 (delta 41) 1353 * k_break [0070] BSSY.RECONVERGENT B0, 0x110 ; 0x0000009000007945 0x000fe20003800200 (delta 10) 1354 * k_loop [0130] BSYNC.RECONVERGENT B0 ; 0x0000000000007941 0x000fea0003800200 1355 * Barrier ids other than B0 and the predicated forms are decoded by tools/reconv_nvdisasm_check.sh 1356 * (nvdisasm -b SM121 text), not by this table. */ 1357 int omega_blackwell_verify_codegen_fixtures_reconv(void) { 1358 static const struct { BlackwellIROpcode op; int delta; int bar; uint32_t w0, w1, w2; } fx[] = { 1359 { BW_IR_BSSY, 13, 0, 0x00007945U, 0x000000c0U, 0x03800200U }, 1360 { BW_IR_BSSY, 41, 0, 0x00007945U, 0x00000280U, 0x03800200U }, 1361 { BW_IR_BSSY, 10, 0, 0x00007945U, 0x00000090U, 0x03800200U }, 1362 { BW_IR_BSYNC, 0, 0, 0x00007941U, 0x00000000U, 0x03800200U }, 1363 }; 1364 for (int i = 0; i < (int)(sizeof fx / sizeof fx[0]); i++) { 1365 BlackwellIRInsn in; 1366 memset(&in, 0, sizeof in); 1367 in.op = fx[i].op; in.dst_vreg = -1; in.src1_vreg = -1; in.src2_vreg = -1; in.src3_vreg = -1; in.ureg = -1; 1368 in.imm = (uint32_t)fx[i].delta; in.bar_reg = (uint8_t)fx[i].bar; 1369 uint32_t w[4]; 1370 if (!omega_fv_status(omega_blackwell_encode_one(&in, w), 1)) return omega_fv_error(i, 1); 1371 if (!omega_fv_words(w, fx[i].w0, fx[i].w1, fx[i].w2, 0, 7)) return omega_fv_error(i, 2); 1372 /* control word defaults mirror nvcc's: BSSY 0x000fe200, BSYNC 0x000fea00 */ 1373 if (!omega_fv_words(w, 0, 0, 0, omega_fv_choose(fx[i].op, BW_IR_BSSY, 0x000fe200U, 0x000fea00U), 8)) return omega_fv_error(i, 2); 1374 } 1375 /* refusals: a BSSY must name a later instruction (delta >= 2), barrier ids stop at B15 */ 1376 BlackwellIRInsn bad; uint32_t w[4]; 1377 memset(&bad, 0, sizeof bad); bad.op = BW_IR_BSSY; bad.dst_vreg = bad.src1_vreg = bad.src2_vreg = bad.src3_vreg = bad.ureg = -1; 1378 bad.imm = 1; bad.bar_reg = 0; 1379 if (!omega_fv_status(omega_blackwell_encode_one(&bad, w), 0)) return -101; 1380 bad.imm = 4294967293U; 1381 if (!omega_fv_status(omega_blackwell_encode_one(&bad, w), 0)) return -102; 1382 bad.imm = 5; bad.bar_reg = 16; 1383 if (!omega_fv_status(omega_blackwell_encode_one(&bad, w), 0)) return -103; 1384 bad.op = BW_IR_BSYNC; bad.imm = 0; bad.bar_reg = 16; 1385 if (!omega_fv_status(omega_blackwell_encode_one(&bad, w), 0)) return -104; 1386 bad.bar_reg = 15; 1387 if (!omega_fv_status(omega_blackwell_encode_one(&bad, w), 1)) return -105; 1388 if (!omega_fv_words(w, omega_fv_bits(0x00007941U, 15, 16, 0, 0), 0, 0, 0, 1)) return -105; 1389 return 0; 1390 } 1391 1392 1393 /* Training R5 (2026-10-15): golden words for SHFL.BFLY and F2FP.BF16.F32.PACK_AB, the two instructions the prefill 1394 * kernels emitted as placeholders patched after encoding until R5. Oracle: nvcc 13.0.88 -arch=sm_121, cuobjdump -sass, 1395 * sealed in evidence/OWN30B-T3-TC-PEAK-BW-`5faa0f70`-20261013T2236Z/fa_ops.{cu,sass} (low 64-bit word, then high; the top 1396 * 23 bits of the high word are the control word and are not compared): 1397 * SHFL.BFLY PT, R5, R0, 0x1, 0x1f ; 0x0c201f0000057f89 0x004e2800000e0000 1398 * SHFL.BFLY PT, R7, R8, 0x1, 0x1f ; 0x0c201f0008077f89 0x008e6200000e0000 1399 * SHFL.BFLY PT, R4, R9, 0x2, 0x1f ; 0x0c401f0009047f89 0x000e2800000e0000 1400 * SHFL.BFLY PT, R6, R11, 0x2, 0x1f ; 0x0c401f000b067f89 0x000e6200000e0000 1401 * F2FP.BF16.F32.PACK_AB R9, R3, R0 ; 0x000000000309723e 0x000fe200000010ff 1402 * F2FP.BF16.F32.PACK_AB R13, R13, R10 ; 0x0000000a0d0d723e 0x000fe400000010ff 1403 * SHFL.BFLY is SHFL.DOWN (oracle words in omega_numeric.c, lanes 16 .. 1) with bit 58 set (Mesa NAK mode field 58..59: Idx 0, 1404 * Up 1, Down 2, Bfly 3; same lane field 53..57); the lane check below covers every lane 0 .. 31 against that rule. 0 = all good. */ 1405 int omega_blackwell_verify_codegen_fixtures_bfly_f2fp(void) { 1406 static const struct { 1407 BlackwellIROpcode op; int d, a, b; uint32_t imm; uint32_t w0, w1, w2; const char *text; 1408 } fx[] = { 1409 { BW_IR_SHFL_BFLY, 5, 0, -1, 1, 0x00057f89U, 0x0c201f00U, 0x000e0000U, "SHFL.BFLY PT, R5, R0, 0x1, 0x1f" }, 1410 { BW_IR_SHFL_BFLY, 7, 8, -1, 1, 0x08077f89U, 0x0c201f00U, 0x000e0000U, "SHFL.BFLY PT, R7, R8, 0x1, 0x1f" }, 1411 { BW_IR_SHFL_BFLY, 4, 9, -1, 2, 0x09047f89U, 0x0c401f00U, 0x000e0000U, "SHFL.BFLY PT, R4, R9, 0x2, 0x1f" }, 1412 { BW_IR_SHFL_BFLY, 6, 11, -1, 2, 0x0b067f89U, 0x0c401f00U, 0x000e0000U, "SHFL.BFLY PT, R6, R11, 0x2, 0x1f" }, 1413 { BW_IR_F2FP_BF16_PACK, 9, 3, 0, 0, 0x0309723eU, 0x00000000U, 0x000010ffU, "F2FP.BF16.F32.PACK_AB R9, R3, R0" }, 1414 { BW_IR_F2FP_BF16_PACK, 13, 13, 10, 0, 0x0d0d723eU, 0x0000000aU, 0x000010ffU, "F2FP.BF16.F32.PACK_AB R13, R13, R10" }, 1415 }; 1416 uint32_t w[4]; 1417 for (int i = 0; i < (int)(sizeof fx / sizeof fx[0]); i++) { 1418 BlackwellIRInsn in; 1419 memset(&in, 0, sizeof in); 1420 in.op = fx[i].op; in.dst_vreg = fx[i].d; in.src1_vreg = fx[i].a; in.src2_vreg = fx[i].b; 1421 in.src3_vreg = -1; in.ureg = -1; in.imm = fx[i].imm; 1422 if (!omega_fv_status(omega_blackwell_encode_one(&in, w), 1)) return omega_fv_error(i, 1); 1423 if (!omega_fv_words(w, fx[i].w0, fx[i].w1, fx[i].w2, 0, 7)) return omega_fv_error(i, 2); 1424 } 1425 for (uint32_t lane = 0; lane < 32; lane++) { 1426 BlackwellIRInsn in; uint32_t d[4]; 1427 memset(&in, 0, sizeof in); 1428 in.op = BW_IR_SHFL_DOWN; in.dst_vreg = 9; in.src1_vreg = 2; in.src2_vreg = in.src3_vreg = in.ureg = -1; in.imm = lane; 1429 if (!omega_fv_status(omega_blackwell_encode_one(&in, d), 1)) return -100; 1430 in.op = BW_IR_SHFL_BFLY; 1431 if (!omega_fv_status(omega_blackwell_encode_one(&in, w), 1)) return -101; 1432 if (!omega_fv_bfly(w, d)) return -102; 1433 } 1434 return 0; 1435 } 1436 1437 /* Training R5 part 2: f32 global atomic add. Starting map from Mesa NAK sm70_encode.rs (`a08a0b9`, sm >= 90 float ATOMG; 1438 * Mesa ranges are half-open): opcode 0x9a3, op 87..90 (Add 0, Min 2, Max 4), type 73..76 (F32 = 9, which is .FTZ.RN; 1439 * 12 decodes as .RN without FTZ), order 77..80 (Strong System 0xa, Strong GPU 0x7), pred-dst 81..83 = PT, eviction 1440 * 84..86 = 1, bit 63 = Ra.64, bit 91 = UR form. Then every word below was decoded offline with nvdisasm -b SM121 1441 * (CUDA 13.0) to the text shown. Where Mesa and the decoder disagree the decoder (and the chip probe) win: this word 1442 * sets bit 90 (w2 bit 26), which falls inside Mesa's op field, yet it decodes as .ADD with desc[UR][R.64]; without it 1443 * the same word decodes as [R.64+UR]. (Bit 88 decodes as .MIN, so the op field is there.) 1444 * 0x80000005020479a3 0x00321e000c1f5306 ATOMG.E.ADD.F32.FTZ.RN.STRONG.SYS PT, R4, desc[UR6][R2.64], R5 ; 1445 * 0x800000070a0979a3 0x00321e000c1f5304 ATOMG.E.ADD.F32.FTZ.RN.STRONG.SYS PT, R9, desc[UR4][R10.64], R7 ; 1446 * 0x8000000502ff79a3 0x00321e000c1f5306 ATOMG.E.ADD.F32.FTZ.RN.STRONG.SYS PT, RZ, desc[UR6][R2.64], R5 ; 1447 * 0x80000005020479a3 0x00321e000c1ef306 ATOMG.E.ADD.F32.FTZ.RN.STRONG.GPU PT, R4, desc[UR6][R2.64], R5 ; 1448 * 0x800000070a0979a3 0x00321e000c1ef304 ATOMG.E.ADD.F32.FTZ.RN.STRONG.GPU PT, R9, desc[UR4][R10.64], R7 ; 1449 * 0x8000000502ff79a3 0x00321e000c1ef306 ATOMG.E.ADD.F32.FTZ.RN.STRONG.GPU PT, RZ, desc[UR6][R2.64], R5 ; 1450 * Rows with d = -1 (no destination vreg) must encode RZ, not R0. These are host golden words. 1451 * The GB10 probe tests/gpu_atomf32_probe_test.c is NOT QUALIFIED (pending a receipt). See docs/status.md. 1452 * 0 = all good. */ 1453 int omega_blackwell_verify_codegen_fixtures_atomf32(void) { 1454 static const struct { BlackwellIROpcode op; int d, a, b, u; uint32_t w0, w1, w2; } fx[] = { 1455 { BW_IR_ATOMG_ADD_F32_STRONG_SYS, 4, 2, 5, 6, 0x020479a3U, 0x80000005U, 0x0c1f5306U }, 1456 { BW_IR_ATOMG_ADD_F32_STRONG_SYS, 9, 10, 7, 4, 0x0a0979a3U, 0x80000007U, 0x0c1f5304U }, 1457 { BW_IR_ATOMG_ADD_F32_STRONG_SYS, 255, 2, 5, 6, 0x02ff79a3U, 0x80000005U, 0x0c1f5306U }, 1458 { BW_IR_ATOMG_ADD_F32_STRONG_SYS, -1, 2, 5, 6, 0x02ff79a3U, 0x80000005U, 0x0c1f5306U }, 1459 { BW_IR_ATOMG_ADD_F32_STRONG_GPU, 4, 2, 5, 6, 0x020479a3U, 0x80000005U, 0x0c1ef306U }, 1460 { BW_IR_ATOMG_ADD_F32_STRONG_GPU, 9, 10, 7, 4, 0x0a0979a3U, 0x80000007U, 0x0c1ef304U }, 1461 { BW_IR_ATOMG_ADD_F32_STRONG_GPU, -1, 2, 5, 6, 0x02ff79a3U, 0x80000005U, 0x0c1ef306U }, 1462 }; 1463 uint32_t w[4]; 1464 for (int i = 0; i < (int)(sizeof fx / sizeof fx[0]); i++) { 1465 BlackwellIRInsn in; 1466 memset(&in, 0, sizeof in); 1467 in.op = fx[i].op; in.dst_vreg = fx[i].d; in.src1_vreg = fx[i].a; in.src2_vreg = fx[i].b; 1468 in.src3_vreg = -1; in.ureg = fx[i].u; 1469 if (!omega_fv_status(omega_blackwell_encode_one(&in, w), 1)) return omega_fv_error(i, 1); 1470 if (!omega_fv_words(w, fx[i].w0, fx[i].w1, fx[i].w2, 0x00321e00U, 15)) return omega_fv_error(i, 2); 1471 } 1472 return 0; 1473 } 1474 /* FB-1 cut 4: word-level fixtures for the four additive ops (checked against the 1475 * nvdisasm decodes recorded in docs/numeric/FB1_CUT4_ELEMENTWISE.md). 0 = all good. */ 1476 int omega_blackwell_verify_codegen_fixtures_fb1cut4(void) { 1477 OmegaRegAlloc ra; 1478 memset(&ra, 0, sizeof(ra)); 1479 ra.vreg_to_phys[0] = 2; /* R2 */ 1480 ra.vreg_to_phys[1] = 4; /* R4 */ 1481 ra.vreg_to_phys[2] = 9; /* R9 */ 1482 ra.vreg_to_phys[3] = 10; /* R10 */ 1483 ra.vreg_to_phys[4] = 8; /* R8 */ 1484 uint32_t w[4]; 1485 1486 /* MUFU.EX2 R2, R4 */ 1487 BlackwellIRInsn ex2 = { .op = BW_IR_MUFU_EX2, .dst_vreg = 0, .src1_vreg = 1 }; 1488 if (!omega_fv_status(encode_single_insn(&ex2, &ra, w), 1)) return -1; 1489 if (!omega_fv_words(w, 0x00027308U, 4, 0x00000800U, 0, 7)) return -2; 1490 /* BAR.SYNC.DEFER_BLOCKING 0x0 */ 1491 BlackwellIRInsn bar = { .op = BW_IR_BAR_SYNC }; 1492 if (!omega_fv_status(encode_single_insn(&bar, &ra, w), 1)) return -3; 1493 if (!omega_fv_words(w, 0x00007b1dU, 0, 0x00010000U, 0x000fec00U, 15)) return -4; 1494 /* LDS R9, [R10+URZ] */ 1495 BlackwellIRInsn lds = { .op = BW_IR_LDS32, .dst_vreg = 2, .src1_vreg = 3 }; 1496 if (!omega_fv_status(encode_single_insn(&lds, &ra, w), 1)) return -5; 1497 if (!omega_fv_words(w, 0x0a097984U, 0xff, 0x08000800U, 0, 7)) return -6; 1498 /* STS [R8+URZ], R2 */ 1499 BlackwellIRInsn sts = { .op = BW_IR_STS32, .src1_vreg = 4, .src2_vreg = 0 }; 1500 if (!omega_fv_status(encode_single_insn(&sts, &ra, w), 1)) return -7; 1501 if (!omega_fv_words(w, 0x08007988U, 2, 0x080008ffU, 0, 7)) return -8; 1502 return 0; 1503 } 1504 1505 /* Kernel capture (C6 of docs/OMEGA.md, tools/sm121dis): with OMEGA_KERNEL_DUMP_DIR set, every encoded kernel is written 1506 * there as .bin, once per distinct body, so the disassembler can be checked against nvdisasm on 1507 * every kernel the library builds. Unset (the normal case) it does nothing; the freestanding build has no files. */ 1508 void omega_kernel_dump(const uint8_t *code, size_t len) { 1509 #ifndef OMEGA_GPU_NO_POSIX 1510 const char *env = getenv("OMEGA_KERNEL_DUMP_DIR"); 1511 if (!env || !env[0] || !code || !len) return; 1512 /* Only the checked copy (src/omega_path_check.h) is opened; a refused directory is a diagnostic, which 1513 * tools/sm121dis/corpus.sh fails on, never a silent skip. */ 1514 char dir[4000]; 1515 if (omega_path_copy(dir, sizeof dir, env) != 0) { 1516 BW_DIAG("omega_kernel_dump: OMEGA_KERNEL_DUMP_DIR refused (src/omega_path_check.h)\n"); 1517 return; 1518 } 1519 uint8_t d[SHA256_DIGEST_SIZE]; sha256_hash(code, len, d); 1520 char path[4096]; 1521 int n = snprintf(path, sizeof path, "%s/%02x%02x%02x%02x%02x%02x%02x%02x.bin", 1522 dir, (unsigned)d[0], (unsigned)d[1], (unsigned)d[2], (unsigned)d[3], 1523 (unsigned)d[4], (unsigned)d[5], (unsigned)d[6], (unsigned)d[7]); 1524 if (n < 0 || (size_t)n >= sizeof path) return; 1525 /* Private, exclusive creation: an existing body or symlink is left alone. */ 1526 int fd = open(path, O_WRONLY | O_CREAT | O_EXCL | O_NOFOLLOW | O_CLOEXEC, 0600); 1527 if (fd < 0) return; 1528 FILE *f = fdopen(fd, "wb"); 1529 if (!f) { close(fd); unlink(path); BW_DIAG("omega_kernel_dump: write failed, removed %s\n", path); return; } 1530 /* A body cut short (a full disk) must not stay under its full-content name: it would be compared as a smaller kernel 1531 * and, created O_EXCL, never rewritten. tools/sm121dis/corpus.sh fails on these diagnostics. */ 1532 int bad = fwrite(code, 1, len, f) != len; 1533 if (fclose(f) != 0) bad = 1; 1534 if (bad) { unlink(path); BW_DIAG("omega_kernel_dump: write failed, removed %s\n", path); } 1535 #else 1536 (void)code; (void)len; 1537 #endif 1538 } 1539 1540 #ifdef BW_ENC_C_PRIMARY 1541 static int bw_encode_program_bytes(const BlackwellIRProgram *prog, uint8_t *code_buf, size_t req_bytes) { 1542 size_t padded_insns = req_bytes / 16; 1543 uint8_t *p = code_buf; 1544 for (size_t i = 0; i < padded_insns; i++) { 1545 uint32_t w[4]; 1546 if (i < prog->count) { 1547 if (encode_single_insn(&prog->insns[i], &prog->regalloc, w) != 0) { 1548 return -1; 1549 } 1550 } else { 1551 /* Pad with NOP */ 1552 w[0] = 0x00007918; 1553 w[1] = 0x00000000; 1554 w[2] = 0x00000000; 1555 w[3] = 0x000fc000; 1556 } 1557 for (int k = 0; k < 4; k++) { 1558 p[4 * k] = (uint8_t)(w[k] & 0xff); 1559 p[4 * k + 1] = (uint8_t)((w[k] >> 8) & 0xff); 1560 p[4 * k + 2] = (uint8_t)((w[k] >> 16) & 0xff); 1561 p[4 * k + 3] = (uint8_t)((w[k] >> 24) & 0xff); 1562 } 1563 p += 16; 1564 } 1565 return 0; 1566 } 1567 #else 1568 static int bw_encode_program_bytes(const BlackwellIRProgram *prog, uint8_t *code_buf, size_t req_bytes) { 1569 uint8_t code[BW_OM_CODE_LEN]; 1570 size_t got = bw_om_encode(prog->insns, prog->count, &prog->regalloc, 1, code); 1571 int rc = (got == req_bytes && got != 0) ? 0 : -1; 1572 #ifdef CUDAPP_MIGRATION_PENDING_ENCODER_DIFF 1573 /* The C oracle encodes the same program, instruction by instruction, NOP-padded the same way. */ 1574 #ifdef CUDAPP_ENCODER_DIFF_SELFTEST 1575 if (rc == 0) code[0] ^= 1; /* red check of the comparison itself: one flipped bit must abort */ 1576 #endif 1577 uint8_t ref[BW_OM_CODE_LEN]; 1578 int crc = 0; 1579 for (size_t i = 0; i < req_bytes / 16 && crc == 0; i++) { 1580 uint32_t w[4] = { 0x00007918, 0, 0, 0x000fc000 }; 1581 if (i < prog->count) crc = bw_c_encode_insn(&prog->insns[i], &prog->regalloc, w); 1582 for (int k = 0; k < 16; k++) ref[16 * i + (size_t)k] = (uint8_t)(w[k / 4] >> (8 * (k % 4))); 1583 } 1584 bw_enc_diff_note("program", prog->count, rc == 0 ? code : NULL, rc == 0 ? req_bytes : 0, 1585 crc == rc && (rc != 0 || memcmp(code, ref, req_bytes) == 0)); 1586 #endif 1587 if (rc == 0) memcpy(code_buf, code, req_bytes); 1588 return rc; 1589 } 1590 #endif 1591 1592 int omega_bw_encode_program(const BlackwellIRProgram *prog, uint8_t *code_buf, size_t max_len, size_t *out_len) { 1593 if (!prog || !code_buf || !out_len) return -1; 1594 1595 #ifndef BW_BIND_C_PRIMARY 1596 uint8_t bw[256] = {0}; 1597 int64_t fit = omega_bw_bind(6, (uint64_t)prog->count, max_len, bw); 1598 if (fit < 0) return -1; 1599 size_t req_bytes = (size_t)fit; 1600 #else 1601 size_t padded_insns = (prog->count + 7) & ~7ULL; 1602 if (padded_insns < 32) padded_insns = 32; 1603 1604 size_t req_bytes = padded_insns * 16; 1605 if (max_len < req_bytes) return -1; 1606 #endif 1607 1608 if (bw_encode_program_bytes(prog, code_buf, req_bytes) != 0) return -1; 1609 #ifdef CUDAPP_AMPERE_CORPUS 1610 /* Offline-only capture of the original solved IR, before family raw patches. */ 1611 extern void omega_ampere_corpus_capture(const BlackwellIRProgram *, const uint8_t *, size_t); 1612 omega_ampere_corpus_capture(prog, code_buf, req_bytes); 1613 #endif 1614 1615 omega_kernel_dump(code_buf, req_bytes); 1616 *out_len = req_bytes; 1617 return 0; 1618 } 1619 1620 #ifndef BW_MATMUL_C_PRIMARY 1621 /* ABI and memory routing only; validation, schedules and lifetime hints are seeded Omega. */ 1622 static int bw_om_matprog(OmegaOmIrUnit unit, const OmegaMatMulSpec *s, uint32_t grid, 1623 uint32_t u, uint32_t rt, uint32_t ct, int mutant, BlackwellIRProgram *p) { 1624 if (!s || !p) return -1; 1625 uint64_t controls = UINT64_MAX; 1626 if (omega_cg_par(&controls, grid, u, rt, ct, (uint64_t)(int64_t)mutant) != 0 || 1627 controls == UINT64_MAX) return -1; 1628 uint64_t a = (uint64_t)s->m | ((uint64_t)s->k << 32); 1629 uint64_t b = (uint64_t)s->n | ((uint64_t)(uint32_t)s->precision << 32); 1630 return omega_gpu_om_ir_replay(unit, 0, a, b, controls, p, NULL) == OMEGA_OM_IR_OK ? 0 : -1; 1631 } 1632 #endif 1633 #ifdef BW_MATMUL_C_PRIMARY 1634 int omega_blackwell_codegen_matmul_i32_prog(const OmegaMatMulSpec *spec, BlackwellIRProgram *prog) { 1635 if (!spec || !prog) return -1; 1636 if (spec->m == 0 || spec->k == 0 || spec->n == 0) return -1; 1637 1638 /* Fail closed loudly if K exceeds bounded unroll limit */ 1639 if (spec->k > 32) { 1640 BW_DIAG("ERROR: MatMul integer codegen exceeds bounded unroll limit (K=%u > 32)\n", spec->k); 1641 return -1; 1642 } 1643 1644 omega_bw_ir_init(prog); 1645 1646 /* 1. Allocate virtual registers */ 1647 /* 64-bit base pointers */ 1648 int v_ptr_a = omega_bw_ir_alloc_vreg64(prog); 1649 int v_ptr_b = omega_bw_ir_alloc_vreg64(prog); 1650 int v_ptr_c = omega_bw_ir_alloc_vreg64(prog); 1651 int uv_desc = omega_bw_ir_alloc_uvreg64(prog); 1652 1653 /* 32-bit CTA / Thread indexing */ 1654 int v_ctaid_x = omega_bw_ir_alloc_vreg(prog); 1655 int v_ctaid_y = omega_bw_ir_alloc_vreg(prog); 1656 int v_tid_x = omega_bw_ir_alloc_vreg(prog); 1657 int v_tid_y = omega_bw_ir_alloc_vreg(prog); 1658 1659 /* 2D Coordinates & loop invariants */ 1660 int v_col = omega_bw_ir_alloc_vreg(prog); 1661 int v_row = omega_bw_ir_alloc_vreg(prog); 1662 int v_row_k = omega_bw_ir_alloc_vreg(prog); 1663 int v_const_1 = omega_bw_ir_alloc_vreg(prog); 1664 int v_acc = omega_bw_ir_alloc_vreg(prog); 1665 1666 /* Working registers for memory address and operands */ 1667 int v_idx_a = omega_bw_ir_alloc_vreg(prog); 1668 int v_off_a = omega_bw_ir_alloc_vreg64(prog); 1669 int v_val_a = omega_bw_ir_alloc_vreg(prog); 1670 1671 int v_idx_b = omega_bw_ir_alloc_vreg(prog); 1672 int v_off_b = omega_bw_ir_alloc_vreg64(prog); 1673 int v_val_b = omega_bw_ir_alloc_vreg(prog); 1674 1675 int v_idx_c = omega_bw_ir_alloc_vreg(prog); 1676 int v_off_c = omega_bw_ir_alloc_vreg64(prog); 1677 1678 if (v_off_c < 0) return -1; 1679 1680 /* 2. Load 64-bit global memory descriptor into uniform register pair */ 1681 /* LDCU.64 uv_desc, c[0x0][0x358] */ 1682 BlackwellIRInsn insn_desc = { .op = BW_IR_LDCU64, .dst_vreg = uv_desc, .imm = 0x358, .is_uniform = true }; 1683 omega_bw_ir_append(prog, &insn_desc); 1684 1685 /* 3. Load 64-bit base pointers from Constant Bank 0 */ 1686 /* LDC.64 v_ptr_a, c[0x0][0x380] */ 1687 BlackwellIRInsn insn_ptra = { .op = BW_IR_LDC64, .dst_vreg = v_ptr_a, .imm = 0x380 }; 1688 omega_bw_ir_append(prog, &insn_ptra); 1689 1690 /* LDC.64 v_ptr_b, c[0x0][0x388] */ 1691 BlackwellIRInsn insn_ptrb = { .op = BW_IR_LDC64, .dst_vreg = v_ptr_b, .imm = 0x388 }; 1692 omega_bw_ir_append(prog, &insn_ptrb); 1693 1694 /* LDC.64 v_ptr_c, c[0x0][0x390] */ 1695 BlackwellIRInsn insn_ptrc = { .op = BW_IR_LDC64, .dst_vreg = v_ptr_c, .imm = 0x390 }; 1696 omega_bw_ir_append(prog, &insn_ptrc); 1697 1698 /* 4. Query 2D CTA and Thread coordinates */ 1699 /* S2R v_ctaid_x, SR_CTAID.X */ 1700 BlackwellIRInsn insn_ctax = { .op = BW_IR_S2R, .dst_vreg = v_ctaid_x, .imm = BW_SR_CTAID_X }; 1701 omega_bw_ir_append(prog, &insn_ctax); 1702 1703 /* S2R v_ctaid_y, SR_CTAID.Y */ 1704 BlackwellIRInsn insn_ctay = { .op = BW_IR_S2R, .dst_vreg = v_ctaid_y, .imm = BW_SR_CTAID_Y }; 1705 omega_bw_ir_append(prog, &insn_ctay); 1706 1707 /* S2R v_tid_x, SR_TID.X */ 1708 BlackwellIRInsn insn_tidx = { .op = BW_IR_S2R, .dst_vreg = v_tid_x, .imm = BW_SR_TID_X }; 1709 omega_bw_ir_append(prog, &insn_tidx); 1710 1711 /* S2R v_tid_y, SR_TID.Y */ 1712 BlackwellIRInsn insn_tidy = { .op = BW_IR_S2R, .dst_vreg = v_tid_y, .imm = BW_SR_TID_Y }; 1713 omega_bw_ir_append(prog, &insn_tidy); 1714 1715 /* 5. Compute 2D (row, col) coordinates */ 1716 /* Standard 16x16 tile threads: col = ctaid_x * 16 + tid_x */ 1717 BlackwellIRInsn insn_col = { .op = BW_IR_IMAD, .dst_vreg = v_col, .src1_vreg = v_ctaid_x, .src2_vreg = -1, .imm = 16, .src3_vreg = v_tid_x }; 1718 omega_bw_ir_append(prog, &insn_col); 1719 1720 /* row = ctaid_y * 16 + tid_y */ 1721 BlackwellIRInsn insn_row = { .op = BW_IR_IMAD, .dst_vreg = v_row, .src1_vreg = v_ctaid_y, .src2_vreg = -1, .imm = 16, .src3_vreg = v_tid_y }; 1722 omega_bw_ir_append(prog, &insn_row); 1723 1724 /* 6. Precompute row base index: v_row_k = v_row * K + RZ */ 1725 BlackwellIRInsn insn_row_k = { .op = BW_IR_IMAD, .dst_vreg = v_row_k, .src1_vreg = v_row, .src2_vreg = -1, .imm = spec->k, .src3_vreg = -1 }; 1726 omega_bw_ir_append(prog, &insn_row_k); 1727 1728 /* 7. Initialize v_const_1 = 1 for immediate linear indexing */ 1729 BlackwellIRInsn insn_c1 = { .op = BW_IR_MOV_IMM, .dst_vreg = v_const_1, .imm = 1 }; 1730 omega_bw_ir_append(prog, &insn_c1); 1731 1732 /* 8. Initialize accumulator to zero: MOV v_acc, RZ */ 1733 BlackwellIRInsn insn_init_acc = { .op = BW_IR_MOV_RZ, .dst_vreg = v_acc }; 1734 omega_bw_ir_append(prog, &insn_init_acc); 1735 1736 /* 9. Synthesize inner accumulation loop across K iterations */ 1737 for (uint32_t step = 0; step < spec->k; step++) { 1738 /* A[row, step]: idx_a = v_row_k + step = v_const_1 * step + v_row_k */ 1739 BlackwellIRInsn insn_idxa = { .op = BW_IR_IMAD, .dst_vreg = v_idx_a, .src1_vreg = v_const_1, .src2_vreg = -1, .imm = step, .src3_vreg = v_row_k }; 1740 omega_bw_ir_append(prog, &insn_idxa); 1741 1742 /* off_a = v_ptr_a + idx_a * 4 */ 1743 BlackwellIRInsn insn_offa = { .op = BW_IR_IMAD_WIDE, .dst_vreg = v_off_a, .src1_vreg = v_idx_a, .src2_vreg = -1, .imm = 4, .src3_vreg = v_ptr_a }; 1744 omega_bw_ir_append(prog, &insn_offa); 1745 1746 /* Load A[row, step]: LDG.E v_val_a, desc[uv_desc][v_off_a.64] */ 1747 BlackwellIRInsn insn_lda = { .op = BW_IR_LDG_E, .dst_vreg = v_val_a, .src1_vreg = v_off_a, .src2_vreg = -1, .src3_vreg = -1, .ureg = uv_desc, .control = 0x001ea800 }; 1748 omega_bw_ir_append(prog, &insn_lda); 1749 1750 /* B[step, col]: idx_b = step * N + col = v_const_1 * (step * N) + v_col */ 1751 uint32_t step_n = step * spec->n; 1752 BlackwellIRInsn insn_idxb = { .op = BW_IR_IMAD, .dst_vreg = v_idx_b, .src1_vreg = v_const_1, .src2_vreg = -1, .imm = step_n, .src3_vreg = v_col }; 1753 omega_bw_ir_append(prog, &insn_idxb); 1754 1755 /* off_b = v_ptr_b + idx_b * 4 */ 1756 BlackwellIRInsn insn_offb = { .op = BW_IR_IMAD_WIDE, .dst_vreg = v_off_b, .src1_vreg = v_idx_b, .src2_vreg = -1, .imm = 4, .src3_vreg = v_ptr_b }; 1757 omega_bw_ir_append(prog, &insn_offb); 1758 1759 /* Load B[step, col]: LDG.E v_val_b, desc[uv_desc][v_off_b.64] */ 1760 BlackwellIRInsn insn_ldb = { .op = BW_IR_LDG_E, .dst_vreg = v_val_b, .src1_vreg = v_off_b, .src2_vreg = -1, .src3_vreg = -1, .ureg = uv_desc, .control = 0x002ea800 }; 1761 omega_bw_ir_append(prog, &insn_ldb); 1762 1763 /* Multiply and accumulate: IMAD v_acc, v_val_a, v_val_b, v_acc */ 1764 BlackwellIRInsn insn_acc = { .op = BW_IR_IMAD, .dst_vreg = v_acc, .src1_vreg = v_val_a, .src2_vreg = v_val_b, .src3_vreg = v_acc, .control = 0x004fca00 }; 1765 omega_bw_ir_append(prog, &insn_acc); 1766 } 1767 1768 /* 10. Compute output index: idx_c = row * N + col */ 1769 BlackwellIRInsn insn_idxc = { .op = BW_IR_IMAD, .dst_vreg = v_idx_c, .src1_vreg = v_row, .src2_vreg = -1, .imm = spec->n, .src3_vreg = v_col }; 1770 omega_bw_ir_append(prog, &insn_idxc); 1771 1772 /* off_c = v_ptr_c + idx_c * 4 */ 1773 BlackwellIRInsn insn_offc = { .op = BW_IR_IMAD_WIDE, .dst_vreg = v_off_c, .src1_vreg = v_idx_c, .src2_vreg = -1, .imm = 4, .src3_vreg = v_ptr_c }; 1774 omega_bw_ir_append(prog, &insn_offc); 1775 1776 /* Store C[row, col]: STG.E desc[uv_desc][v_off_c.64], v_acc */ 1777 BlackwellIRInsn insn_stc = { .op = BW_IR_STG_E, .src1_vreg = v_off_c, .src2_vreg = v_acc, .src3_vreg = -1, .ureg = uv_desc }; 1778 omega_bw_ir_append(prog, &insn_stc); 1779 1780 /* 11. Kernel termination */ 1781 BlackwellIRInsn insn_exit = { .op = BW_IR_EXIT, .predicate_p0 = false }; 1782 omega_bw_ir_append(prog, &insn_exit); 1783 1784 BlackwellIRInsn insn_bra = { .op = BW_IR_BRA }; 1785 omega_bw_ir_append(prog, &insn_bra); 1786 1787 /* 12. Solve bounded deterministic register allocation */ 1788 if (omega_bw_regalloc_solve(prog) != 0) { 1789 return -1; 1790 } 1791 return 0; 1792 } 1793 #else 1794 int omega_blackwell_codegen_matmul_i32_prog(const OmegaMatMulSpec *s, BlackwellIRProgram *p) { 1795 return bw_om_matprog(omega_cg_i32, s, 0, 0, 0, 0, 0, p); 1796 } 1797 #endif 1798 1799 /* ---- native v1 P07 cut A.2: allocation-free kernel encoding --------------------------------- 1800 * The one place a solved IR program becomes a kernel. Hosted and freestanding builds both encode 1801 * through it, so the two produce the same bytes for the same program (make test-codegen-freestanding 1802 * compares them). */ 1803 size_t omega_bw_program_code_bytes(const BlackwellIRProgram *prog) { 1804 if (!prog) return 0; 1805 #ifndef BW_BIND_C_PRIMARY 1806 uint8_t w[256] = {0}; 1807 return (size_t)omega_bw_bind(5, (uint64_t)prog->count, 0, w); 1808 #else 1809 size_t padded_insns = (prog->count + 7) & ~(size_t)7; 1810 if (padded_insns < 32) padded_insns = 32; 1811 return padded_insns * 16; 1812 #endif 1813 } 1814 1815 int omega_blackwell_kernel_encode_into(const BlackwellIRProgram *prog, uint8_t *code_buf, size_t code_cap, 1816 OmegaBlackwellKernel *kernel) { 1817 if (!kernel) return -1; 1818 memset(kernel, 0, sizeof *kernel); 1819 if (!prog || !code_buf) return -1; 1820 #ifndef BW_BIND_C_PRIMARY 1821 uint8_t w[256] = {0}; 1822 /* refused before any byte is written */ 1823 if (omega_bw_bind(6, (uint64_t)prog->count, code_cap, w) == -2) return OMEGA_BW_CODE_BUFFER_TOO_SMALL; 1824 size_t emitted_size = 0; 1825 if (omega_bw_encode_program(prog, code_buf, code_cap, &emitted_size) != 0) return -1; 1826 kernel->code = code_buf; 1827 kernel->code_size = emitted_size; 1828 kernel->insn_count = (size_t)omega_bw_bind(8, emitted_size, 0, w); 1829 #else 1830 size_t need = omega_bw_program_code_bytes(prog); 1831 if (code_cap < need) return OMEGA_BW_CODE_BUFFER_TOO_SMALL; /* refused before any byte is written */ 1832 size_t emitted_size = 0; 1833 if (omega_bw_encode_program(prog, code_buf, code_cap, &emitted_size) != 0) return -1; 1834 kernel->code = code_buf; 1835 kernel->code_size = emitted_size; 1836 kernel->insn_count = emitted_size / 16; 1837 #endif 1838 kernel->gpr_count = prog->regalloc.peak_gpr_usage; 1839 kernel->uniform_gpr_count = prog->regalloc.peak_ugpr_usage; 1840 sha256_hash(kernel->code, kernel->code_size, kernel->code_digest); 1841 return 0; 1842 } 1843 1844 #ifndef OMEGA_GPU_NO_POSIX 1845 /* Hosted only: the allocating generators keep their names, caps and behavior. Each builds the program, 1846 * mallocs max_bytes (the cap each generator always had) and encodes through omega_blackwell_kernel_encode_into; 1847 * omega_blackwell_kernel_free releases the buffer. */ 1848 static int bw_kernel_alloc_encode(const BlackwellIRProgram *prog, size_t max_bytes, OmegaBlackwellKernel *kernel) { 1849 uint8_t *buf = (uint8_t *)malloc(max_bytes); 1850 if (!buf) { kernel->code = NULL; return -1; } 1851 if (omega_blackwell_kernel_encode_into(prog, buf, max_bytes, kernel) != 0) { 1852 free(buf); 1853 kernel->code = NULL; 1854 return -1; 1855 } 1856 return 0; 1857 } 1858 1859 int omega_blackwell_codegen_matmul_i32(const OmegaMatMulSpec *spec, OmegaBlackwellKernel *kernel) { 1860 if (!spec || !kernel) return -1; 1861 BlackwellIRProgram prog; 1862 if (omega_blackwell_codegen_matmul_i32_prog(spec, &prog) != 0) return -1; 1863 /* 13. the code buffer is exactly the padded program (the i32 generator never had a fixed cap) */ 1864 return bw_kernel_alloc_encode(&prog, omega_bw_program_code_bytes(&prog), kernel); 1865 } 1866 #endif 1867 1868 #ifdef BW_MATMUL_C_PRIMARY 1869 int omega_blackwell_codegen_matmul_tensor_prog(const OmegaMatMulSpec *spec, BlackwellIRProgram *prog) { 1870 if (!spec || !prog) return -1; 1871 if (spec->m == 0 || spec->k == 0 || spec->n == 0) return -1; 1872 if (spec->precision != OMEGA_MATMUL_PRECISION_FP16 && spec->precision != OMEGA_MATMUL_PRECISION_BF16) return -1; 1873 if (spec->m % 16 != 0 || spec->n % 8 != 0 || spec->k != 16) { 1874 BW_DIAG("ERROR: MatMul tensor codegen requires M multiple of 16, N multiple of 8, K=16 (got %ux%ux%u)\n", 1875 spec->m, spec->k, spec->n); 1876 return -1; 1877 } 1878 1879 omega_bw_ir_init(prog); 1880 1881 /* 1. Allocate virtual registers */ 1882 /* 64-bit base pointers */ 1883 int v_ptr_a = omega_bw_ir_alloc_vreg64(prog); 1884 int v_ptr_b = omega_bw_ir_alloc_vreg64(prog); 1885 int v_ptr_c = omega_bw_ir_alloc_vreg64(prog); 1886 int uv_desc = omega_bw_ir_alloc_uvreg64(prog); 1887 1888 /* 32-bit CTA / Thread indexing */ 1889 int v_tid = omega_bw_ir_alloc_vreg(prog); 1890 int v_ctaid_x = omega_bw_ir_alloc_vreg(prog); 1891 int v_ctaid_y = omega_bw_ir_alloc_vreg(prog); 1892 1893 /* Tile and thread coordinates */ 1894 int v_group_id = omega_bw_ir_alloc_vreg(prog); 1895 int v_tid_in_grp = omega_bw_ir_alloc_vreg(prog); 1896 int v_row_base = omega_bw_ir_alloc_vreg(prog); 1897 int v_col_base = omega_bw_ir_alloc_vreg(prog); 1898 int v_r0 = omega_bw_ir_alloc_vreg(prog); 1899 int v_r1 = omega_bw_ir_alloc_vreg(prog); 1900 int v_col_b = omega_bw_ir_alloc_vreg(prog); 1901 int v_tid_col2 = omega_bw_ir_alloc_vreg(prog); 1902 int v_const_1 = omega_bw_ir_alloc_vreg(prog); 1903 1904 /* Bundles for HMMA */ 1905 int v_ra = omega_bw_ir_alloc_vreg128(prog); /* Quad: Ra[0]..Ra[3] */ 1906 int v_rb = omega_bw_ir_alloc_vreg64(prog); /* Pair: Rb[0]..Rb[1] */ 1907 int v_rd = omega_bw_ir_alloc_vreg128(prog); /* Quad: Rd[0]..Rd[3] */ 1908 1909 /* Scratch registers for indexing and loads */ 1910 int v_idx = omega_bw_ir_alloc_vreg(prog); 1911 int v_off = omega_bw_ir_alloc_vreg64(prog); 1912 int v_b_tmp0 = omega_bw_ir_alloc_vreg(prog); 1913 int v_b_tmp1 = omega_bw_ir_alloc_vreg(prog); 1914 1915 if (v_b_tmp1 < 0) return -1; 1916 1917 /* 2. Load 64-bit global memory descriptor into uniform register pair */ 1918 /* LDCU.64 uv_desc, c[0x0][0x358] */ 1919 BlackwellIRInsn insn_desc = { .op = BW_IR_LDCU64, .dst_vreg = uv_desc, .imm = 0x358, .is_uniform = true }; 1920 omega_bw_ir_append(prog, &insn_desc); 1921 1922 /* 3. Load 64-bit base pointers from Constant Bank 0 */ 1923 /* LDC.64 v_ptr_a, c[0x0][0x380] */ 1924 BlackwellIRInsn insn_ptra = { .op = BW_IR_LDC64, .dst_vreg = v_ptr_a, .imm = 0x380 }; 1925 omega_bw_ir_append(prog, &insn_ptra); 1926 1927 /* LDC.64 v_ptr_b, c[0x0][0x388] */ 1928 BlackwellIRInsn insn_ptrb = { .op = BW_IR_LDC64, .dst_vreg = v_ptr_b, .imm = 0x388 }; 1929 omega_bw_ir_append(prog, &insn_ptrb); 1930 1931 /* LDC.64 v_ptr_c, c[0x0][0x390] */ 1932 BlackwellIRInsn insn_ptrc = { .op = BW_IR_LDC64, .dst_vreg = v_ptr_c, .imm = 0x390 }; 1933 omega_bw_ir_append(prog, &insn_ptrc); 1934 1935 /* 4. Query Special Registers */ 1936 /* S2R v_tid, SR_TID.X */ 1937 BlackwellIRInsn insn_tidx = { .op = BW_IR_S2R, .dst_vreg = v_tid, .imm = BW_SR_TID_X }; 1938 omega_bw_ir_append(prog, &insn_tidx); 1939 1940 /* S2R v_ctaid_x, SR_CTAID.X */ 1941 BlackwellIRInsn insn_cta_x = { .op = BW_IR_S2R, .dst_vreg = v_ctaid_x, .imm = BW_SR_CTAID_X }; 1942 omega_bw_ir_append(prog, &insn_cta_x); 1943 1944 /* S2R v_ctaid_y, SR_CTAID.Y */ 1945 BlackwellIRInsn insn_cta_y = { .op = BW_IR_S2R, .dst_vreg = v_ctaid_y, .imm = BW_SR_CTAID_Y }; 1946 omega_bw_ir_append(prog, &insn_cta_y); 1947 1948 /* 5. Set up constant 1 */ 1949 BlackwellIRInsn insn_c1 = { .op = BW_IR_MOV_IMM, .dst_vreg = v_const_1, .imm = 1 }; 1950 omega_bw_ir_append(prog, &insn_c1); 1951 1952 /* 6. Extract warp-level tile and thread coordinates */ 1953 /* group_id = tid >> 2 */ 1954 BlackwellIRInsn insn_grp = { .op = BW_IR_SHF_R, .dst_vreg = v_group_id, .src1_vreg = v_tid, .imm = 2 }; 1955 omega_bw_ir_append(prog, &insn_grp); 1956 1957 /* tid_in_grp = tid & 3 */ 1958 BlackwellIRInsn insn_tig = { .op = BW_IR_LOP3_AND, .dst_vreg = v_tid_in_grp, .src1_vreg = v_tid, .imm = 3 }; 1959 omega_bw_ir_append(prog, &insn_tig); 1960 1961 /* row_base = ctaid_y * 16 */ 1962 BlackwellIRInsn insn_rbase = { .op = BW_IR_IMAD, .dst_vreg = v_row_base, .src1_vreg = v_ctaid_y, .src2_vreg = -1, .imm = 16, .src3_vreg = -1 }; 1963 omega_bw_ir_append(prog, &insn_rbase); 1964 1965 /* col_base = ctaid_x * 8 */ 1966 BlackwellIRInsn insn_cbase = { .op = BW_IR_IMAD, .dst_vreg = v_col_base, .src1_vreg = v_ctaid_x, .src2_vreg = -1, .imm = 8, .src3_vreg = -1 }; 1967 omega_bw_ir_append(prog, &insn_cbase); 1968 1969 /* r0 = row_base + group_id */ 1970 BlackwellIRInsn insn_r0 = { .op = BW_IR_IADD3, .dst_vreg = v_r0, .src1_vreg = v_row_base, .src2_vreg = v_group_id, .src3_vreg = -1 }; 1971 omega_bw_ir_append(prog, &insn_r0); 1972 1973 /* r1 = r0 + 8 */ 1974 BlackwellIRInsn insn_r1 = { .op = BW_IR_IMAD, .dst_vreg = v_r1, .src1_vreg = v_const_1, .src2_vreg = -1, .imm = 8, .src3_vreg = v_r0 }; 1975 omega_bw_ir_append(prog, &insn_r1); 1976 1977 /* col_b = col_base + group_id */ 1978 BlackwellIRInsn insn_colb = { .op = BW_IR_IADD3, .dst_vreg = v_col_b, .src1_vreg = v_col_base, .src2_vreg = v_group_id, .src3_vreg = -1 }; 1979 omega_bw_ir_append(prog, &insn_colb); 1980 1981 /* tid_col2 = tid_in_grp * 2 */ 1982 BlackwellIRInsn insn_tcol2 = { .op = BW_IR_IMAD, .dst_vreg = v_tid_col2, .src1_vreg = v_tid_in_grp, .src2_vreg = -1, .imm = 2, .src3_vreg = -1 }; 1983 omega_bw_ir_append(prog, &insn_tcol2); 1984 1985 /* 7. Load A fragments (16x16 row-major, 2 bytes/element, row stride 32 bytes) */ 1986 /* Pair 0: a0, a1 at r0 * 32 + tid_col2 * 2 */ 1987 BlackwellIRInsn insn_a0_idx1 = { .op = BW_IR_IMAD, .dst_vreg = v_idx, .src1_vreg = v_r0, .src2_vreg = -1, .imm = 32, .src3_vreg = -1 }; 1988 omega_bw_ir_append(prog, &insn_a0_idx1); 1989 BlackwellIRInsn insn_a0_idx2 = { .op = BW_IR_IMAD, .dst_vreg = v_idx, .src1_vreg = v_tid_col2, .src2_vreg = -1, .imm = 2, .src3_vreg = v_idx }; 1990 omega_bw_ir_append(prog, &insn_a0_idx2); 1991 BlackwellIRInsn insn_a0_off = { .op = BW_IR_IMAD_WIDE, .dst_vreg = v_off, .src1_vreg = v_idx, .src2_vreg = -1, .imm = 1, .src3_vreg = v_ptr_a }; 1992 omega_bw_ir_append(prog, &insn_a0_off); 1993 BlackwellIRInsn insn_lda0 = { .op = BW_IR_LDG_E, .dst_vreg = v_ra, .dst_subreg = 0, .src1_vreg = v_off, .src2_vreg = -1, .src3_vreg = -1, .ureg = uv_desc, .control = 0x001ea800 }; 1994 omega_bw_ir_append(prog, &insn_lda0); 1995 1996 /* Pair 1: a2, a3 at r1 * 32 + tid_col2 * 2 */ 1997 BlackwellIRInsn insn_a1_idx1 = { .op = BW_IR_IMAD, .dst_vreg = v_idx, .src1_vreg = v_r1, .src2_vreg = -1, .imm = 32, .src3_vreg = -1 }; 1998 omega_bw_ir_append(prog, &insn_a1_idx1); 1999 BlackwellIRInsn insn_a1_idx2 = { .op = BW_IR_IMAD, .dst_vreg = v_idx, .src1_vreg = v_tid_col2, .src2_vreg = -1, .imm = 2, .src3_vreg = v_idx }; 2000 omega_bw_ir_append(prog, &insn_a1_idx2); 2001 BlackwellIRInsn insn_a1_off = { .op = BW_IR_IMAD_WIDE, .dst_vreg = v_off, .src1_vreg = v_idx, .src2_vreg = -1, .imm = 1, .src3_vreg = v_ptr_a }; 2002 omega_bw_ir_append(prog, &insn_a1_off); 2003 BlackwellIRInsn insn_lda1 = { .op = BW_IR_LDG_E, .dst_vreg = v_ra, .dst_subreg = 1, .src1_vreg = v_off, .src2_vreg = -1, .src3_vreg = -1, .ureg = uv_desc, .control = 0x001ea800 }; 2004 omega_bw_ir_append(prog, &insn_lda1); 2005 2006 /* Pair 2: a4, a5 at r0 * 32 + tid_col2 * 2 + 16 */ 2007 BlackwellIRInsn insn_a2_idx1 = { .op = BW_IR_IMAD, .dst_vreg = v_idx, .src1_vreg = v_r0, .src2_vreg = -1, .imm = 32, .src3_vreg = -1 }; 2008 omega_bw_ir_append(prog, &insn_a2_idx1); 2009 BlackwellIRInsn insn_a2_idx2 = { .op = BW_IR_IMAD, .dst_vreg = v_idx, .src1_vreg = v_tid_col2, .src2_vreg = -1, .imm = 2, .src3_vreg = v_idx }; 2010 omega_bw_ir_append(prog, &insn_a2_idx2); 2011 BlackwellIRInsn insn_a2_idx3 = { .op = BW_IR_IMAD, .dst_vreg = v_idx, .src1_vreg = v_const_1, .src2_vreg = -1, .imm = 16, .src3_vreg = v_idx }; 2012 omega_bw_ir_append(prog, &insn_a2_idx3); 2013 BlackwellIRInsn insn_a2_off = { .op = BW_IR_IMAD_WIDE, .dst_vreg = v_off, .src1_vreg = v_idx, .src2_vreg = -1, .imm = 1, .src3_vreg = v_ptr_a }; 2014 omega_bw_ir_append(prog, &insn_a2_off); 2015 BlackwellIRInsn insn_lda2 = { .op = BW_IR_LDG_E, .dst_vreg = v_ra, .dst_subreg = 2, .src1_vreg = v_off, .src2_vreg = -1, .src3_vreg = -1, .ureg = uv_desc, .control = 0x001ea800 }; 2016 omega_bw_ir_append(prog, &insn_lda2); 2017 2018 /* Pair 3: a6, a7 at r1 * 32 + tid_col2 * 2 + 16 */ 2019 BlackwellIRInsn insn_a3_idx1 = { .op = BW_IR_IMAD, .dst_vreg = v_idx, .src1_vreg = v_r1, .src2_vreg = -1, .imm = 32, .src3_vreg = -1 }; 2020 omega_bw_ir_append(prog, &insn_a3_idx1); 2021 BlackwellIRInsn insn_a3_idx2 = { .op = BW_IR_IMAD, .dst_vreg = v_idx, .src1_vreg = v_tid_col2, .src2_vreg = -1, .imm = 2, .src3_vreg = v_idx }; 2022 omega_bw_ir_append(prog, &insn_a3_idx2); 2023 BlackwellIRInsn insn_a3_idx3 = { .op = BW_IR_IMAD, .dst_vreg = v_idx, .src1_vreg = v_const_1, .src2_vreg = -1, .imm = 16, .src3_vreg = v_idx }; 2024 omega_bw_ir_append(prog, &insn_a3_idx3); 2025 BlackwellIRInsn insn_a3_off = { .op = BW_IR_IMAD_WIDE, .dst_vreg = v_off, .src1_vreg = v_idx, .src2_vreg = -1, .imm = 1, .src3_vreg = v_ptr_a }; 2026 omega_bw_ir_append(prog, &insn_a3_off); 2027 BlackwellIRInsn insn_lda3 = { .op = BW_IR_LDG_E, .dst_vreg = v_ra, .dst_subreg = 3, .src1_vreg = v_off, .src2_vreg = -1, .src3_vreg = -1, .ureg = uv_desc, .control = 0x001ea800 }; 2028 omega_bw_ir_append(prog, &insn_lda3); 2029 2030 /* 8. Load and pack B fragments (16xN row-major, 2 bytes/element) */ 2031 /* b0 at tid_col2 * N + col_b */ 2032 BlackwellIRInsn insn_b0_idx = { .op = BW_IR_IMAD, .dst_vreg = v_idx, .src1_vreg = v_tid_col2, .src2_vreg = -1, .imm = spec->n, .src3_vreg = v_col_b }; 2033 omega_bw_ir_append(prog, &insn_b0_idx); 2034 BlackwellIRInsn insn_b0_off = { .op = BW_IR_IMAD_WIDE, .dst_vreg = v_off, .src1_vreg = v_idx, .src2_vreg = -1, .imm = 2, .src3_vreg = v_ptr_b }; 2035 omega_bw_ir_append(prog, &insn_b0_off); 2036 BlackwellIRInsn insn_ldb0 = { .op = BW_IR_LDG_E_U16, .dst_vreg = v_b_tmp0, .src1_vreg = v_off, .src2_vreg = -1, .src3_vreg = -1, .ureg = uv_desc, .control = 0x002ea800 }; 2037 omega_bw_ir_append(prog, &insn_ldb0); 2038 2039 /* b1 at (tid_col2 + 1) * N + col_b = b0_idx + N */ 2040 BlackwellIRInsn insn_b1_idx = { .op = BW_IR_IMAD, .dst_vreg = v_idx, .src1_vreg = v_const_1, .src2_vreg = -1, .imm = spec->n, .src3_vreg = v_idx }; 2041 omega_bw_ir_append(prog, &insn_b1_idx); 2042 BlackwellIRInsn insn_b1_off = { .op = BW_IR_IMAD_WIDE, .dst_vreg = v_off, .src1_vreg = v_idx, .src2_vreg = -1, .imm = 2, .src3_vreg = v_ptr_b }; 2043 omega_bw_ir_append(prog, &insn_b1_off); 2044 BlackwellIRInsn insn_ldb1 = { .op = BW_IR_LDG_E_U16, .dst_vreg = v_b_tmp1, .src1_vreg = v_off, .src2_vreg = -1, .src3_vreg = -1, .ureg = uv_desc, .control = 0x002ea800 }; 2045 omega_bw_ir_append(prog, &insn_ldb1); 2046 2047 /* Pack Rb[0] = (b1 << 16) | b0 (wait on barrier 2) */ 2048 BlackwellIRInsn insn_pack_b0 = { .op = BW_IR_IMAD, .dst_vreg = v_rb, .dst_subreg = 0, .src1_vreg = v_b_tmp1, .src2_vreg = -1, .imm = 0x10000, .src3_vreg = v_b_tmp0, .control = 0x004fca00 }; 2049 omega_bw_ir_append(prog, &insn_pack_b0); 2050 2051 /* b2 at (tid_col2 + 8) * N + col_b */ 2052 BlackwellIRInsn insn_b2_idx1 = { .op = BW_IR_IMAD, .dst_vreg = v_idx, .src1_vreg = v_tid_col2, .src2_vreg = -1, .imm = spec->n, .src3_vreg = v_col_b }; 2053 omega_bw_ir_append(prog, &insn_b2_idx1); 2054 BlackwellIRInsn insn_b2_idx2 = { .op = BW_IR_IMAD, .dst_vreg = v_idx, .src1_vreg = v_const_1, .src2_vreg = -1, .imm = 8 * spec->n, .src3_vreg = v_idx }; 2055 omega_bw_ir_append(prog, &insn_b2_idx2); 2056 BlackwellIRInsn insn_b2_off = { .op = BW_IR_IMAD_WIDE, .dst_vreg = v_off, .src1_vreg = v_idx, .src2_vreg = -1, .imm = 2, .src3_vreg = v_ptr_b }; 2057 omega_bw_ir_append(prog, &insn_b2_off); 2058 BlackwellIRInsn insn_ldb2 = { .op = BW_IR_LDG_E_U16, .dst_vreg = v_b_tmp0, .src1_vreg = v_off, .src2_vreg = -1, .src3_vreg = -1, .ureg = uv_desc, .control = 0x002ea800 }; 2059 omega_bw_ir_append(prog, &insn_ldb2); 2060 2061 /* b3 at (tid_col2 + 9) * N + col_b = b2_idx + N */ 2062 BlackwellIRInsn insn_b3_idx = { .op = BW_IR_IMAD, .dst_vreg = v_idx, .src1_vreg = v_const_1, .src2_vreg = -1, .imm = spec->n, .src3_vreg = v_idx }; 2063 omega_bw_ir_append(prog, &insn_b3_idx); 2064 BlackwellIRInsn insn_b3_off = { .op = BW_IR_IMAD_WIDE, .dst_vreg = v_off, .src1_vreg = v_idx, .src2_vreg = -1, .imm = 2, .src3_vreg = v_ptr_b }; 2065 omega_bw_ir_append(prog, &insn_b3_off); 2066 BlackwellIRInsn insn_ldb3 = { .op = BW_IR_LDG_E_U16, .dst_vreg = v_b_tmp1, .src1_vreg = v_off, .src2_vreg = -1, .src3_vreg = -1, .ureg = uv_desc, .control = 0x002ea800 }; 2067 omega_bw_ir_append(prog, &insn_ldb3); 2068 2069 /* Pack Rb[1] = (b3 << 16) | b2 (wait on barrier 2) */ 2070 BlackwellIRInsn insn_pack_b1 = { .op = BW_IR_IMAD, .dst_vreg = v_rb, .dst_subreg = 1, .src1_vreg = v_b_tmp1, .src2_vreg = -1, .imm = 0x10000, .src3_vreg = v_b_tmp0, .control = 0x004fca00 }; 2071 omega_bw_ir_append(prog, &insn_pack_b1); 2072 2073 /* 9. Execute HMMA Tensor Core instruction (wait on barrier 1 for A loads) */ 2074 BlackwellIROpcode mma_op = (spec->precision == OMEGA_MATMUL_PRECISION_BF16) ? BW_IR_HMMA_BF16 : BW_IR_HMMA_F16; 2075 BlackwellIRInsn insn_hmma = { 2076 .op = mma_op, 2077 .dst_vreg = v_rd, 2078 .dst_subreg = 0, 2079 .src1_vreg = v_ra, 2080 .src1_subreg = 0, 2081 .src2_vreg = v_rb, 2082 .src2_subreg = 0, 2083 .src3_vreg = -1, /* RZ accumulator input */ 2084 .control = 0x002fca00 /* Wait on barrier 1 */ 2085 }; 2086 omega_bw_ir_append(prog, &insn_hmma); 2087 2088 /* 10. Store C output fragments (16xN row-major, 4 bytes/float element) */ 2089 /* d0: row r0, col col_base + tid_col2 + 0 */ 2090 BlackwellIRInsn insn_d0_idx1 = { .op = BW_IR_IMAD, .dst_vreg = v_idx, .src1_vreg = v_r0, .src2_vreg = -1, .imm = spec->n, .src3_vreg = v_col_base }; 2091 omega_bw_ir_append(prog, &insn_d0_idx1); 2092 BlackwellIRInsn insn_d0_idx2 = { .op = BW_IR_IADD3, .dst_vreg = v_idx, .src1_vreg = v_idx, .src2_vreg = v_tid_col2, .src3_vreg = -1 }; 2093 omega_bw_ir_append(prog, &insn_d0_idx2); 2094 BlackwellIRInsn insn_d0_off = { .op = BW_IR_IMAD_WIDE, .dst_vreg = v_off, .src1_vreg = v_idx, .src2_vreg = -1, .imm = 4, .src3_vreg = v_ptr_c }; 2095 omega_bw_ir_append(prog, &insn_d0_off); 2096 BlackwellIRInsn insn_std0 = { .op = BW_IR_STG_E, .src1_vreg = v_off, .src2_vreg = v_rd, .src2_subreg = 0, .ureg = uv_desc, .control = 0x000fe200 }; 2097 omega_bw_ir_append(prog, &insn_std0); 2098 2099 /* d1: row r0, col col_base + tid_col2 + 1 */ 2100 BlackwellIRInsn insn_d1_idx = { .op = BW_IR_IMAD, .dst_vreg = v_idx, .src1_vreg = v_const_1, .src2_vreg = -1, .imm = 1, .src3_vreg = v_idx }; 2101 omega_bw_ir_append(prog, &insn_d1_idx); 2102 BlackwellIRInsn insn_d1_off = { .op = BW_IR_IMAD_WIDE, .dst_vreg = v_off, .src1_vreg = v_idx, .src2_vreg = -1, .imm = 4, .src3_vreg = v_ptr_c }; 2103 omega_bw_ir_append(prog, &insn_d1_off); 2104 BlackwellIRInsn insn_std1 = { .op = BW_IR_STG_E, .src1_vreg = v_off, .src2_vreg = v_rd, .src2_subreg = 1, .ureg = uv_desc, .control = 0x000fe200 }; 2105 omega_bw_ir_append(prog, &insn_std1); 2106 2107 /* d2: row r1, col col_base + tid_col2 + 0 */ 2108 BlackwellIRInsn insn_d2_idx1 = { .op = BW_IR_IMAD, .dst_vreg = v_idx, .src1_vreg = v_r1, .src2_vreg = -1, .imm = spec->n, .src3_vreg = v_col_base }; 2109 omega_bw_ir_append(prog, &insn_d2_idx1); 2110 BlackwellIRInsn insn_d2_idx2 = { .op = BW_IR_IADD3, .dst_vreg = v_idx, .src1_vreg = v_idx, .src2_vreg = v_tid_col2, .src3_vreg = -1 }; 2111 omega_bw_ir_append(prog, &insn_d2_idx2); 2112 BlackwellIRInsn insn_d2_off = { .op = BW_IR_IMAD_WIDE, .dst_vreg = v_off, .src1_vreg = v_idx, .src2_vreg = -1, .imm = 4, .src3_vreg = v_ptr_c }; 2113 omega_bw_ir_append(prog, &insn_d2_off); 2114 BlackwellIRInsn insn_std2 = { .op = BW_IR_STG_E, .src1_vreg = v_off, .src2_vreg = v_rd, .src2_subreg = 2, .ureg = uv_desc, .control = 0x000fe200 }; 2115 omega_bw_ir_append(prog, &insn_std2); 2116 2117 /* d3: row r1, col col_base + tid_col2 + 1 */ 2118 BlackwellIRInsn insn_d3_idx = { .op = BW_IR_IMAD, .dst_vreg = v_idx, .src1_vreg = v_const_1, .src2_vreg = -1, .imm = 1, .src3_vreg = v_idx }; 2119 omega_bw_ir_append(prog, &insn_d3_idx); 2120 BlackwellIRInsn insn_d3_off = { .op = BW_IR_IMAD_WIDE, .dst_vreg = v_off, .src1_vreg = v_idx, .src2_vreg = -1, .imm = 4, .src3_vreg = v_ptr_c }; 2121 omega_bw_ir_append(prog, &insn_d3_off); 2122 BlackwellIRInsn insn_std3 = { .op = BW_IR_STG_E, .src1_vreg = v_off, .src2_vreg = v_rd, .src2_subreg = 3, .ureg = uv_desc, .control = 0x000fe200 }; 2123 omega_bw_ir_append(prog, &insn_std3); 2124 2125 /* 11. Program Exit */ 2126 BlackwellIRInsn insn_exit = { .op = BW_IR_EXIT }; 2127 omega_bw_ir_append(prog, &insn_exit); 2128 BlackwellIRInsn insn_bra = { .op = BW_IR_BRA }; 2129 omega_bw_ir_append(prog, &insn_bra); 2130 2131 /* 12. Run bounded register allocation */ 2132 if (omega_bw_regalloc_solve(prog) != 0) { 2133 return -1; 2134 } 2135 return 0; 2136 } 2137 #else 2138 int omega_blackwell_codegen_matmul_tensor_prog(const OmegaMatMulSpec *s, BlackwellIRProgram *p) { 2139 return bw_om_matprog(omega_cg_tens, s, 0, 0, 0, 0, 0, p); 2140 } 2141 #endif 2142 2143 #ifndef OMEGA_GPU_NO_POSIX 2144 int omega_blackwell_codegen_matmul_tensor(const OmegaMatMulSpec *spec, OmegaBlackwellKernel *kernel) { 2145 if (!spec || !kernel) return -1; 2146 BlackwellIRProgram prog; 2147 if (omega_blackwell_codegen_matmul_tensor_prog(spec, &prog) != 0) { 2148 return -1; 2149 } 2150 2151 return bw_kernel_alloc_encode(&prog, OMEGA_BW_CODE_CAP_SMALL, kernel); 2152 } 2153 #endif 2154 2155 /* ---- FB-1 cut 1b: looped tensor matmul ------------------------------------------ 2156 * 2157 * Control words (bundle bits 105..125, decoded from the chip-proven kernel and from 2158 * the vendor compiler's SASS for an equivalent HMMA loop on this chip, 2026-10-04): 2159 * stall = bits 9..12 of w[3], yield = 13, write barrier = 14..16, read barrier = 2160 * 17..19, wait mask = 20..25. 7 in a barrier field means "none". 2161 * LDG (A) 0x001ea800 stall 4, yield, sets SB2 2162 * LDG.U16 (B) 0x002ea800 stall 4, yield, sets SB2 2163 * pack IMAD 0x004fca00 stall 5, waits SB2 (every outstanding load has landed) 2164 * HMMA 0x002fca00 stall 5 (fixed latency: the vendor compiler sets no 2165 * write barrier on HMMA either; consumers wait by stall count) 2166 * STG 0x0007e200 stall 1, yield, read barrier SB3 (the store may read its 2167 * data/address registers late; the next writer waits SB3) 2168 * wait SB3 mask bit 23 (0x00800000) on the first instruction that overwrites a 2169 * register a store reads: the C pointer advance. 2170 * Loop-carried registers are kept live to the end of their loop (keep_alive) because 2171 * the allocator is a straight-line linear scan and knows nothing about back edges. 2172 */ 2173 #ifdef BW_MATMUL_C_PRIMARY 2174 static void bw_keep_alive(BlackwellIRProgram *prog, int from_idx, int to_idx, int last_idx) { 2175 /* every vreg first defined in [from_idx, to_idx) stays live until last_idx */ 2176 for (int v = 0; v < prog->regalloc.num_vregs; v++) { 2177 OmegaLiveInterval *iv = &prog->regalloc.intervals[v]; 2178 if (iv->first_def >= from_idx && iv->first_def < to_idx && iv->last_use < last_idx) 2179 iv->last_use = last_idx; 2180 } 2181 } 2182 2183 #define BW_APPEND(prog, ...) do { BlackwellIRInsn _i = { __VA_ARGS__ }; if (omega_bw_ir_append((prog), &_i) < 0) return -1; } while (0) 2184 2185 int omega_blackwell_codegen_matmul_tensor_loop_prog(const OmegaMatMulSpec *spec, uint32_t grid_x, 2186 int mutant, BlackwellIRProgram *prog) { 2187 if (!spec || !prog) return -1; 2188 if (spec->m == 0 || spec->k == 0 || spec->n == 0) return -1; 2189 if (spec->precision != OMEGA_MATMUL_PRECISION_FP16 && spec->precision != OMEGA_MATMUL_PRECISION_BF16) return -1; 2190 if (spec->m % 16 != 0 || spec->n % 8 != 0 || spec->k % 16 != 0) { 2191 BW_DIAG("ERROR: looped MatMul tensor codegen requires M%%16, N%%8, K%%16 (got %ux%ux%u)\n", 2192 spec->m, spec->k, spec->n); 2193 return -1; 2194 } 2195 const uint32_t K = spec->k, N = spec->n, NT = N / 8, KSTEPS = K / 16; 2196 if (grid_x == 0 || grid_x > NT) return -1; 2197 /* every immediate below must fit 32 bits: 32 * N (B row-block stride in bytes) is the largest */ 2198 if ((uint64_t)N * 32u > 0xffffffffull || (uint64_t)grid_x * 32u > 0xffffffffull) return -1; 2199 if (mutant && KSTEPS < 2) return -1; 2200 2201 omega_bw_ir_init(prog); 2202 2203 int v_ptr_a = omega_bw_ir_alloc_vreg64(prog), v_ptr_b = omega_bw_ir_alloc_vreg64(prog), v_ptr_c = omega_bw_ir_alloc_vreg64(prog); 2204 int uv_desc = omega_bw_ir_alloc_uvreg64(prog); 2205 int v_tid = omega_bw_ir_alloc_vreg(prog), v_ctaid_x = omega_bw_ir_alloc_vreg(prog), v_ctaid_y = omega_bw_ir_alloc_vreg(prog); 2206 int v_group = omega_bw_ir_alloc_vreg(prog), v_tig = omega_bw_ir_alloc_vreg(prog), v_tcol2 = omega_bw_ir_alloc_vreg(prog); 2207 int v_r0 = omega_bw_ir_alloc_vreg(prog), v_r1 = omega_bw_ir_alloc_vreg(prog), v_c1 = omega_bw_ir_alloc_vreg(prog); 2208 int v_idx = omega_bw_ir_alloc_vreg(prog), v_col = omega_bw_ir_alloc_vreg(prog), v_cnt = omega_bw_ir_alloc_vreg(prog); 2209 int v_pa0 = omega_bw_ir_alloc_vreg64(prog), v_pa1 = omega_bw_ir_alloc_vreg64(prog), v_pbcol = omega_bw_ir_alloc_vreg64(prog); 2210 int v_pc0 = omega_bw_ir_alloc_vreg64(prog), v_pc1 = omega_bw_ir_alloc_vreg64(prog); 2211 int v_pa0c = omega_bw_ir_alloc_vreg64(prog), v_pa1c = omega_bw_ir_alloc_vreg64(prog), v_pbc = omega_bw_ir_alloc_vreg64(prog); 2212 int v_ta2 = omega_bw_ir_alloc_vreg64(prog), v_ta3 = omega_bw_ir_alloc_vreg64(prog); 2213 int v_tb1 = omega_bw_ir_alloc_vreg64(prog), v_tb2 = omega_bw_ir_alloc_vreg64(prog), v_tb3 = omega_bw_ir_alloc_vreg64(prog); 2214 int v_tc1 = omega_bw_ir_alloc_vreg64(prog), v_tc3 = omega_bw_ir_alloc_vreg64(prog); 2215 int v_ra = omega_bw_ir_alloc_vreg128(prog), v_rb = omega_bw_ir_alloc_vreg64(prog), v_rd = omega_bw_ir_alloc_vreg128(prog); 2216 int v_b0 = omega_bw_ir_alloc_vreg(prog), v_b1 = omega_bw_ir_alloc_vreg(prog); 2217 if (v_b1 < 0 || uv_desc < 0) return -1; 2218 2219 const uint32_t CTRL_LDA = 0x001ea800u, CTRL_LDB = 0x002ea800u, CTRL_PACK = 0x004fca00u; 2220 const uint32_t CTRL_HMMA = 0x002fca00u, CTRL_STG_RDBAR3 = 0x0007e200u, CTRL_IMADW_WAIT3 = 0x009fcc00u; 2221 const uint32_t CTRL_MOVRZ_WAIT3 = 0x008fe200u; 2222 2223 /* prologue: descriptor, pointers, ids */ 2224 BW_APPEND(prog, .op = BW_IR_LDCU64, .dst_vreg = uv_desc, .imm = 0x358, .is_uniform = true); 2225 BW_APPEND(prog, .op = BW_IR_LDC64, .dst_vreg = v_ptr_a, .imm = 0x380); 2226 BW_APPEND(prog, .op = BW_IR_LDC64, .dst_vreg = v_ptr_b, .imm = 0x388); 2227 BW_APPEND(prog, .op = BW_IR_LDC64, .dst_vreg = v_ptr_c, .imm = 0x390); 2228 BW_APPEND(prog, .op = BW_IR_S2R, .dst_vreg = v_tid, .imm = BW_SR_TID_X); 2229 BW_APPEND(prog, .op = BW_IR_S2R, .dst_vreg = v_ctaid_x, .imm = BW_SR_CTAID_X); 2230 BW_APPEND(prog, .op = BW_IR_S2R, .dst_vreg = v_ctaid_y, .imm = BW_SR_CTAID_Y); 2231 BW_APPEND(prog, .op = BW_IR_MOV_IMM, .dst_vreg = v_c1, .imm = 1); 2232 BW_APPEND(prog, .op = BW_IR_SHF_R, .dst_vreg = v_group, .src1_vreg = v_tid, .imm = 2); 2233 BW_APPEND(prog, .op = BW_IR_LOP3_AND, .dst_vreg = v_tig, .src1_vreg = v_tid, .imm = 3); 2234 BW_APPEND(prog, .op = BW_IR_IMAD, .dst_vreg = v_tcol2, .src1_vreg = v_tig, .src2_vreg = -1, .imm = 2, .src3_vreg = -1); 2235 BW_APPEND(prog, .op = BW_IR_IMAD, .dst_vreg = v_r0, .src1_vreg = v_ctaid_y, .src2_vreg = -1, .imm = 16, .src3_vreg = v_group); 2236 BW_APPEND(prog, .op = BW_IR_IMAD, .dst_vreg = v_r1, .src1_vreg = v_c1, .src2_vreg = -1, .imm = 8, .src3_vreg = v_r0); 2237 /* pa0 = A + (r0*K + tcol2)*2 ; pa1 likewise with r1 (A row-major, K bf16 per row) */ 2238 BW_APPEND(prog, .op = BW_IR_IMAD, .dst_vreg = v_idx, .src1_vreg = v_r0, .src2_vreg = -1, .imm = K, .src3_vreg = v_tcol2); 2239 BW_APPEND(prog, .op = BW_IR_IMAD_WIDE, .dst_vreg = v_pa0, .src1_vreg = v_idx, .src2_vreg = -1, .imm = 2, .src3_vreg = v_ptr_a); 2240 BW_APPEND(prog, .op = BW_IR_IMAD, .dst_vreg = v_idx, .src1_vreg = v_r1, .src2_vreg = -1, .imm = K, .src3_vreg = v_tcol2); 2241 BW_APPEND(prog, .op = BW_IR_IMAD_WIDE, .dst_vreg = v_pa1, .src1_vreg = v_idx, .src2_vreg = -1, .imm = 2, .src3_vreg = v_ptr_a); 2242 /* col = ctaid_x (first column tile of this CTA) */ 2243 BW_APPEND(prog, .op = BW_IR_IMAD, .dst_vreg = v_col, .src1_vreg = v_ctaid_x, .src2_vreg = -1, .imm = 1, .src3_vreg = -1); 2244 /* pbcol = B + (tcol2*N + col*8 + group)*2 */ 2245 BW_APPEND(prog, .op = BW_IR_IMAD, .dst_vreg = v_idx, .src1_vreg = v_tcol2, .src2_vreg = -1, .imm = N, .src3_vreg = v_group); 2246 BW_APPEND(prog, .op = BW_IR_IMAD, .dst_vreg = v_idx, .src1_vreg = v_col, .src2_vreg = -1, .imm = 8, .src3_vreg = v_idx); 2247 BW_APPEND(prog, .op = BW_IR_IMAD_WIDE, .dst_vreg = v_pbcol, .src1_vreg = v_idx, .src2_vreg = -1, .imm = 2, .src3_vreg = v_ptr_b); 2248 /* pc0 = C + (r0*N + col*8 + tcol2)*4 ; pc1 with r1 */ 2249 BW_APPEND(prog, .op = BW_IR_IMAD, .dst_vreg = v_idx, .src1_vreg = v_r0, .src2_vreg = -1, .imm = N, .src3_vreg = v_tcol2); 2250 BW_APPEND(prog, .op = BW_IR_IMAD, .dst_vreg = v_idx, .src1_vreg = v_col, .src2_vreg = -1, .imm = 8, .src3_vreg = v_idx); 2251 BW_APPEND(prog, .op = BW_IR_IMAD_WIDE, .dst_vreg = v_pc0, .src1_vreg = v_idx, .src2_vreg = -1, .imm = 4, .src3_vreg = v_ptr_c); 2252 BW_APPEND(prog, .op = BW_IR_IMAD, .dst_vreg = v_idx, .src1_vreg = v_r1, .src2_vreg = -1, .imm = N, .src3_vreg = v_tcol2); 2253 BW_APPEND(prog, .op = BW_IR_IMAD, .dst_vreg = v_idx, .src1_vreg = v_col, .src2_vreg = -1, .imm = 8, .src3_vreg = v_idx); 2254 BW_APPEND(prog, .op = BW_IR_IMAD_WIDE, .dst_vreg = v_pc1, .src1_vreg = v_idx, .src2_vreg = -1, .imm = 4, .src3_vreg = v_ptr_c); 2255 2256 /* ---- column-tile loop ---- */ 2257 const int L_col = (int)prog->count; 2258 /* accumulator = 0 (the first write waits SB3: the previous tile's stores may still read it) */ 2259 BW_APPEND(prog, .op = BW_IR_MOV_RZ, .dst_vreg = v_rd, .dst_subreg = 0, .control = CTRL_MOVRZ_WAIT3); 2260 BW_APPEND(prog, .op = BW_IR_MOV_RZ, .dst_vreg = v_rd, .dst_subreg = 1); 2261 BW_APPEND(prog, .op = BW_IR_MOV_RZ, .dst_vreg = v_rd, .dst_subreg = 2); 2262 BW_APPEND(prog, .op = BW_IR_MOV_RZ, .dst_vreg = v_rd, .dst_subreg = 3); 2263 /* K cursors start at the row/column bases (IMAD.WIDE with imm 0 is a 64-bit copy) */ 2264 BW_APPEND(prog, .op = BW_IR_IMAD_WIDE, .dst_vreg = v_pa0c, .src1_vreg = v_c1, .src2_vreg = -1, .imm = 0, .src3_vreg = v_pa0); 2265 BW_APPEND(prog, .op = BW_IR_IMAD_WIDE, .dst_vreg = v_pa1c, .src1_vreg = v_c1, .src2_vreg = -1, .imm = 0, .src3_vreg = v_pa1); 2266 BW_APPEND(prog, .op = BW_IR_IMAD_WIDE, .dst_vreg = v_pbc, .src1_vreg = v_c1, .src2_vreg = -1, .imm = 0, .src3_vreg = v_pbcol); 2267 BW_APPEND(prog, .op = BW_IR_MOV_IMM, .dst_vreg = v_cnt, .imm = mutant ? KSTEPS - 1 : KSTEPS); 2268 2269 /* ---- K loop: one 16x16 A fragment and one 16x8 B fragment per step ---- */ 2270 const int L_k = (int)prog->count; 2271 BW_APPEND(prog, .op = BW_IR_IMAD_WIDE, .dst_vreg = v_ta2, .src1_vreg = v_c1, .src2_vreg = -1, .imm = 16, .src3_vreg = v_pa0c); 2272 BW_APPEND(prog, .op = BW_IR_IMAD_WIDE, .dst_vreg = v_ta3, .src1_vreg = v_c1, .src2_vreg = -1, .imm = 16, .src3_vreg = v_pa1c); 2273 BW_APPEND(prog, .op = BW_IR_IMAD_WIDE, .dst_vreg = v_tb1, .src1_vreg = v_c1, .src2_vreg = -1, .imm = 2u * N, .src3_vreg = v_pbc); 2274 BW_APPEND(prog, .op = BW_IR_IMAD_WIDE, .dst_vreg = v_tb2, .src1_vreg = v_c1, .src2_vreg = -1, .imm = 16u * N, .src3_vreg = v_pbc); 2275 BW_APPEND(prog, .op = BW_IR_IMAD_WIDE, .dst_vreg = v_tb3, .src1_vreg = v_c1, .src2_vreg = -1, .imm = 18u * N, .src3_vreg = v_pbc); 2276 /* A: a0,a1 at (r0, kk+tcol2) ; a2,a3 at (r1, ...) ; a4,a5 at (r0, +8) ; a6,a7 at (r1, +8) */ 2277 BW_APPEND(prog, .op = BW_IR_LDG_E, .dst_vreg = v_ra, .dst_subreg = 0, .src1_vreg = v_pa0c, .src2_vreg = -1, .src3_vreg = -1, .ureg = uv_desc, .control = CTRL_LDA); 2278 BW_APPEND(prog, .op = BW_IR_LDG_E, .dst_vreg = v_ra, .dst_subreg = 1, .src1_vreg = v_pa1c, .src2_vreg = -1, .src3_vreg = -1, .ureg = uv_desc, .control = CTRL_LDA); 2279 BW_APPEND(prog, .op = BW_IR_LDG_E, .dst_vreg = v_ra, .dst_subreg = 2, .src1_vreg = v_ta2, .src2_vreg = -1, .src3_vreg = -1, .ureg = uv_desc, .control = CTRL_LDA); 2280 BW_APPEND(prog, .op = BW_IR_LDG_E, .dst_vreg = v_ra, .dst_subreg = 3, .src1_vreg = v_ta3, .src2_vreg = -1, .src3_vreg = -1, .ureg = uv_desc, .control = CTRL_LDA); 2281 /* B: rows kk+tcol2, +1, +8, +9 at column col*8+group */ 2282 BW_APPEND(prog, .op = BW_IR_LDG_E_U16, .dst_vreg = v_b0, .src1_vreg = v_pbc, .src2_vreg = -1, .src3_vreg = -1, .ureg = uv_desc, .control = CTRL_LDB); 2283 BW_APPEND(prog, .op = BW_IR_LDG_E_U16, .dst_vreg = v_b1, .src1_vreg = v_tb1, .src2_vreg = -1, .src3_vreg = -1, .ureg = uv_desc, .control = CTRL_LDB); 2284 BW_APPEND(prog, .op = BW_IR_IMAD, .dst_vreg = v_rb, .dst_subreg = 0, .src1_vreg = v_b1, .src2_vreg = -1, .imm = 0x10000, .src3_vreg = v_b0, .control = CTRL_PACK); 2285 BW_APPEND(prog, .op = BW_IR_LDG_E_U16, .dst_vreg = v_b0, .src1_vreg = v_tb2, .src2_vreg = -1, .src3_vreg = -1, .ureg = uv_desc, .control = CTRL_LDB); 2286 BW_APPEND(prog, .op = BW_IR_LDG_E_U16, .dst_vreg = v_b1, .src1_vreg = v_tb3, .src2_vreg = -1, .src3_vreg = -1, .ureg = uv_desc, .control = CTRL_LDB); 2287 BW_APPEND(prog, .op = BW_IR_IMAD, .dst_vreg = v_rb, .dst_subreg = 1, .src1_vreg = v_b1, .src2_vreg = -1, .imm = 0x10000, .src3_vreg = v_b0, .control = CTRL_PACK); 2288 /* rd += ra * rb (accumulate in place) */ 2289 BW_APPEND(prog, .op = (spec->precision == OMEGA_MATMUL_PRECISION_BF16) ? BW_IR_HMMA_BF16 : BW_IR_HMMA_F16, 2290 .dst_vreg = v_rd, .dst_subreg = 0, .src1_vreg = v_ra, .src1_subreg = 0, .src2_vreg = v_rb, .src2_subreg = 0, 2291 .src3_vreg = v_rd, .src3_subreg = 0, .control = CTRL_HMMA); 2292 /* advance K: A by 16 bf16 (32 bytes), B by 16 rows (32 N bytes); every load above has landed (packs waited SB2) */ 2293 BW_APPEND(prog, .op = BW_IR_IMAD_WIDE, .dst_vreg = v_pa0c, .src1_vreg = v_c1, .src2_vreg = -1, .imm = 32, .src3_vreg = v_pa0c); 2294 BW_APPEND(prog, .op = BW_IR_IMAD_WIDE, .dst_vreg = v_pa1c, .src1_vreg = v_c1, .src2_vreg = -1, .imm = 32, .src3_vreg = v_pa1c); 2295 BW_APPEND(prog, .op = BW_IR_IMAD_WIDE, .dst_vreg = v_pbc, .src1_vreg = v_c1, .src2_vreg = -1, .imm = 32u * N, .src3_vreg = v_pbc); 2296 BW_APPEND(prog, .op = BW_IR_IMAD, .dst_vreg = v_cnt, .src1_vreg = v_c1, .src2_vreg = -1, .imm = 0xffffffffu, .src3_vreg = v_cnt); 2297 BW_APPEND(prog, .op = BW_IR_ISETP_GE, .dst_vreg = -1, .src1_vreg = v_cnt, .src2_vreg = -1, .imm = 1, .src3_vreg = -1); 2298 { 2299 int here = (int)prog->count; 2300 BW_APPEND(prog, .op = BW_IR_BRA, .imm = (uint32_t)(int32_t)(L_k - here), .predicate_p0 = true); 2301 } 2302 const int L_k_end = (int)prog->count - 1; 2303 2304 /* ---- store the 16x8 tile: d0 (r0, c), d1 (r0, c+1), d2 (r1, c), d3 (r1, c+1) ---- */ 2305 BW_APPEND(prog, .op = BW_IR_IMAD_WIDE, .dst_vreg = v_tc1, .src1_vreg = v_c1, .src2_vreg = -1, .imm = 4, .src3_vreg = v_pc0); 2306 BW_APPEND(prog, .op = BW_IR_IMAD_WIDE, .dst_vreg = v_tc3, .src1_vreg = v_c1, .src2_vreg = -1, .imm = 4, .src3_vreg = v_pc1); 2307 BW_APPEND(prog, .op = BW_IR_STG_E, .src1_vreg = v_pc0, .src2_vreg = v_rd, .src2_subreg = 0, .src3_vreg = -1, .ureg = uv_desc, .control = CTRL_STG_RDBAR3); 2308 BW_APPEND(prog, .op = BW_IR_STG_E, .src1_vreg = v_tc1, .src2_vreg = v_rd, .src2_subreg = 1, .src3_vreg = -1, .ureg = uv_desc, .control = CTRL_STG_RDBAR3); 2309 BW_APPEND(prog, .op = BW_IR_STG_E, .src1_vreg = v_pc1, .src2_vreg = v_rd, .src2_subreg = 2, .src3_vreg = -1, .ureg = uv_desc, .control = CTRL_STG_RDBAR3); 2310 BW_APPEND(prog, .op = BW_IR_STG_E, .src1_vreg = v_tc3, .src2_vreg = v_rd, .src2_subreg = 3, .src3_vreg = -1, .ureg = uv_desc, .control = CTRL_STG_RDBAR3); 2311 /* next column tile of this CTA: col += grid_x ; B by grid_x*8 bf16 ; C by grid_x*8 f32 (waits SB3 first) */ 2312 BW_APPEND(prog, .op = BW_IR_IMAD, .dst_vreg = v_col, .src1_vreg = v_c1, .src2_vreg = -1, .imm = grid_x, .src3_vreg = v_col); 2313 BW_APPEND(prog, .op = BW_IR_IMAD_WIDE, .dst_vreg = v_pc0, .src1_vreg = v_c1, .src2_vreg = -1, .imm = grid_x * 32u, .src3_vreg = v_pc0, .control = CTRL_IMADW_WAIT3); 2314 BW_APPEND(prog, .op = BW_IR_IMAD_WIDE, .dst_vreg = v_pc1, .src1_vreg = v_c1, .src2_vreg = -1, .imm = grid_x * 32u, .src3_vreg = v_pc1); 2315 BW_APPEND(prog, .op = BW_IR_IMAD_WIDE, .dst_vreg = v_pbcol, .src1_vreg = v_c1, .src2_vreg = -1, .imm = grid_x * 16u, .src3_vreg = v_pbcol); 2316 BW_APPEND(prog, .op = BW_IR_ISETP_GE, .dst_vreg = -1, .src1_vreg = v_col, .src2_vreg = -1, .imm = NT, .src3_vreg = -1); 2317 { 2318 int here = (int)prog->count; 2319 BW_APPEND(prog, .op = BW_IR_BRA, .imm = (uint32_t)(int32_t)(L_col - here), .predicate_p0 = true, .predicate_not = true); 2320 } 2321 const int L_col_end = (int)prog->count - 1; 2322 BW_APPEND(prog, .op = BW_IR_EXIT); 2323 BW_APPEND(prog, .op = BW_IR_BRA); 2324 2325 /* loop-carried values live to the end of their loop */ 2326 bw_keep_alive(prog, 0, L_col, L_col_end); 2327 bw_keep_alive(prog, L_col, L_k, L_k_end); 2328 /* K-loop temporaries too: a load's address pair must never be recycled by a 2329 * later instruction of the same iteration (the load may read it late). */ 2330 bw_keep_alive(prog, L_k, L_k_end, L_k_end); 2331 /* the accumulator is read by the stores after the K loop */ 2332 if (prog->regalloc.intervals[v_rd].last_use < L_col_end) prog->regalloc.intervals[v_rd].last_use = L_col_end; 2333 2334 return omega_bw_regalloc_solve(prog); 2335 } 2336 #else 2337 int omega_blackwell_codegen_matmul_tensor_loop_prog(const OmegaMatMulSpec *s, uint32_t grid, 2338 int mutant, BlackwellIRProgram *p) { 2339 return bw_om_matprog(omega_cg_loop, s, grid, 0, 0, 0, mutant, p); 2340 } 2341 #endif 2342 2343 #ifndef OMEGA_GPU_NO_POSIX 2344 int omega_blackwell_codegen_matmul_tensor_loop(const OmegaMatMulSpec *spec, uint32_t grid_x, 2345 int mutant, OmegaBlackwellKernel *kernel) { 2346 if (!spec || !kernel) return -1; 2347 BlackwellIRProgram prog; 2348 if (omega_blackwell_codegen_matmul_tensor_loop_prog(spec, grid_x, mutant, &prog) != 0) return -1; 2349 return bw_kernel_alloc_encode(&prog, OMEGA_BW_CODE_CAP_SMALL, kernel); 2350 } 2351 #endif 2352 2353 /* ---- omega #328: fragment-major tensor matmul ---------------------------------------- 2354 * 2355 * The looped kernel above reads B as four scattered 2-byte loads per K step and waits for 2356 * them before the next step (pack waits SB2), so each warp has one 256-byte step in flight; 2357 * at m = 1 on GB10 that streams the weights at about 28 GB/s (omega #328 measurement). 2358 * Here A and B arrive in the order HMMA.16816 consumes them (omega_matmul_frag_pack_a / _b): 2359 * A block (mt, ks): 512 bytes, lane L at L*16: a0..a7, one LDG.E.128 per step; 2360 * B block (nt, ks): 256 bytes, lane L at L*8: b0..b3, one LDG.E.64 per step; 2361 * consecutive K steps of one tile are adjacent, so a warp streams its column tile. Each loop 2362 * pass issues `unroll` steps of loads at immediate offsets (A on SB2, B on SB4), then runs the HMMA 2363 * chain: every HMMA waits SB2 and SB4 (stall 15) and a NOP (stall 14) follows it, the 29 cycles nvcc 2364 * puts between dependent HMMA.16816 (omega_blackwell_verify_codegen_fixtures_fragmm). 2365 * The cursors advance only after the chain, when every load has landed (the HMMAs waited 2366 * SB2 and SB4), as in the looped kernel. Stores, the column-tile loop and the C layout (row-major 2367 * f32) are the looped kernel's. */ 2368 /* A loads set SB2 and B loads SB4 (nvcc spreads loads over several scoreboards too); at the 2369 * product unroll of 4 that is 4 loads per scoreboard per pass. The scoreboard depth is not 2370 * documented to us; splitting did not make unroll 8 complete (omega_matmul_frag_unroll). */ 2371 #define BW_FRAG_CTRL_LDG_A 0x000ea800u /* stall 4, yield, sets SB2 (nvcc word) */ 2372 #define BW_FRAG_CTRL_LDG_B 0x000f2800u /* stall 4, yield, sets SB4 (nvcc word) */ 2373 #define BW_FRAG_CTRL_HMMA 0x014fde00u /* stall 15, waits SB2 and SB4 (nvcc: dependent HMMA chain) */ 2374 #define BW_FRAG_CTRL_NOP 0x000fdc00u /* stall 14 (nvcc, between dependent HMMAs) */ 2375 2376 #ifdef BW_MATMUL_C_PRIMARY 2377 static void bw_live_until(BlackwellIRProgram *prog, int vreg, int last_idx) { 2378 if (prog->regalloc.intervals[vreg].last_use < last_idx) prog->regalloc.intervals[vreg].last_use = last_idx; 2379 } 2380 2381 int omega_blackwell_codegen_matmul_frag_prog(const OmegaMatMulSpec *spec, uint32_t grid_x, uint32_t unroll, 2382 int mutant, BlackwellIRProgram *prog) { 2383 if (!spec || !prog) return -1; 2384 if (spec->m == 0 || spec->k == 0 || spec->n == 0) return -1; 2385 if (spec->precision != OMEGA_MATMUL_PRECISION_FP16 && spec->precision != OMEGA_MATMUL_PRECISION_BF16) return -1; 2386 if (spec->m % 16 != 0 || spec->n % 8 != 0 || spec->k % 16 != 0) return -1; 2387 if (unroll != 1 && unroll != 2 && unroll != 4 && unroll != 8) return -1; 2388 const uint32_t K = spec->k, N = spec->n, NT = N / 8, KS = K / 16; 2389 if (KS % unroll != 0) return -1; 2390 const uint32_t ITERS = KS / unroll; 2391 if (grid_x == 0 || grid_x > NT) return -1; 2392 if (mutant && ITERS < 2) return -1; 2393 /* every index and immediate below must fit 32 bits (rows up to OMEGA_BW_MATMUL_MAX_M) */ 2394 if ((uint64_t)NT * KS * 32u > 0xffffffffull || (uint64_t)(OMEGA_BW_MATMUL_MAX_M / 16u) * KS * 32u > 0xffffffffull || 2395 (uint64_t)grid_x * KS * 256u > 0xffffffffull || (uint64_t)N * 32u > 0xffffffffull || 2396 (uint64_t)OMEGA_BW_MATMUL_MAX_M * N > 0xffffffffull) 2397 return -1; 2398 2399 omega_bw_ir_init(prog); 2400 int v_ptr_a = omega_bw_ir_alloc_vreg64(prog), v_ptr_b = omega_bw_ir_alloc_vreg64(prog), v_ptr_c = omega_bw_ir_alloc_vreg64(prog); 2401 int uv_desc = omega_bw_ir_alloc_uvreg64(prog); 2402 int v_tid = omega_bw_ir_alloc_vreg(prog), v_ctaid_x = omega_bw_ir_alloc_vreg(prog), v_ctaid_y = omega_bw_ir_alloc_vreg(prog); 2403 int v_group = omega_bw_ir_alloc_vreg(prog), v_tig = omega_bw_ir_alloc_vreg(prog), v_tcol2 = omega_bw_ir_alloc_vreg(prog); 2404 int v_r0 = omega_bw_ir_alloc_vreg(prog), v_r1 = omega_bw_ir_alloc_vreg(prog), v_c1 = omega_bw_ir_alloc_vreg(prog); 2405 int v_idx = omega_bw_ir_alloc_vreg(prog), v_col = omega_bw_ir_alloc_vreg(prog), v_cnt = omega_bw_ir_alloc_vreg(prog); 2406 int v_pa = omega_bw_ir_alloc_vreg64(prog), v_pbcol = omega_bw_ir_alloc_vreg64(prog); 2407 int v_pc0 = omega_bw_ir_alloc_vreg64(prog), v_pc1 = omega_bw_ir_alloc_vreg64(prog); 2408 int v_pac = omega_bw_ir_alloc_vreg64(prog), v_pbc = omega_bw_ir_alloc_vreg64(prog); 2409 int v_tc1 = omega_bw_ir_alloc_vreg64(prog), v_tc3 = omega_bw_ir_alloc_vreg64(prog); 2410 int v_rd = omega_bw_ir_alloc_vreg128(prog); 2411 int v_ra[8], v_rb[8]; 2412 for (uint32_t u = 0; u < unroll; u++) { v_ra[u] = omega_bw_ir_alloc_vreg128(prog); v_rb[u] = omega_bw_ir_alloc_vreg64(prog); } 2413 if (v_rb[unroll - 1] < 0 || v_ra[unroll - 1] < 0 || uv_desc < 0) return -1; 2414 2415 const uint32_t CTRL_STG_RDBAR3 = 0x0007e200u, CTRL_IMADW_WAIT3 = 0x009fcc00u, CTRL_MOVRZ_WAIT3 = 0x008fe200u; 2416 2417 /* prologue: descriptor, pointers, ids (as the looped kernel) */ 2418 BW_APPEND(prog, .op = BW_IR_LDCU64, .dst_vreg = uv_desc, .imm = 0x358, .is_uniform = true); 2419 BW_APPEND(prog, .op = BW_IR_LDC64, .dst_vreg = v_ptr_a, .imm = 0x380); 2420 BW_APPEND(prog, .op = BW_IR_LDC64, .dst_vreg = v_ptr_b, .imm = 0x388); 2421 BW_APPEND(prog, .op = BW_IR_LDC64, .dst_vreg = v_ptr_c, .imm = 0x390); 2422 BW_APPEND(prog, .op = BW_IR_S2R, .dst_vreg = v_tid, .imm = BW_SR_TID_X); 2423 BW_APPEND(prog, .op = BW_IR_S2R, .dst_vreg = v_ctaid_x, .imm = BW_SR_CTAID_X); 2424 BW_APPEND(prog, .op = BW_IR_S2R, .dst_vreg = v_ctaid_y, .imm = BW_SR_CTAID_Y); 2425 BW_APPEND(prog, .op = BW_IR_MOV_IMM, .dst_vreg = v_c1, .imm = 1); 2426 BW_APPEND(prog, .op = BW_IR_SHF_R, .dst_vreg = v_group, .src1_vreg = v_tid, .imm = 2); 2427 BW_APPEND(prog, .op = BW_IR_LOP3_AND, .dst_vreg = v_tig, .src1_vreg = v_tid, .imm = 3); 2428 BW_APPEND(prog, .op = BW_IR_IMAD, .dst_vreg = v_tcol2, .src1_vreg = v_tig, .src2_vreg = -1, .imm = 2, .src3_vreg = -1); 2429 BW_APPEND(prog, .op = BW_IR_IMAD, .dst_vreg = v_r0, .src1_vreg = v_ctaid_y, .src2_vreg = -1, .imm = 16, .src3_vreg = v_group); 2430 BW_APPEND(prog, .op = BW_IR_IMAD, .dst_vreg = v_r1, .src1_vreg = v_c1, .src2_vreg = -1, .imm = 8, .src3_vreg = v_r0); 2431 /* pa = A + (ctaid_y * KS * 32 + tid) * 16: this CTA's row tile, this lane's 16 bytes of step 0 */ 2432 BW_APPEND(prog, .op = BW_IR_IMAD, .dst_vreg = v_idx, .src1_vreg = v_ctaid_y, .src2_vreg = -1, .imm = KS * 32u, .src3_vreg = v_tid); 2433 BW_APPEND(prog, .op = BW_IR_IMAD_WIDE, .dst_vreg = v_pa, .src1_vreg = v_idx, .src2_vreg = -1, .imm = 16, .src3_vreg = v_ptr_a); 2434 BW_APPEND(prog, .op = BW_IR_IMAD, .dst_vreg = v_col, .src1_vreg = v_ctaid_x, .src2_vreg = -1, .imm = 1, .src3_vreg = -1); 2435 /* pbcol = B + (col * KS * 32 + tid) * 8: column tile col, this lane's 8 bytes of step 0 */ 2436 BW_APPEND(prog, .op = BW_IR_IMAD, .dst_vreg = v_idx, .src1_vreg = v_col, .src2_vreg = -1, .imm = KS * 32u, .src3_vreg = v_tid); 2437 BW_APPEND(prog, .op = BW_IR_IMAD_WIDE, .dst_vreg = v_pbcol, .src1_vreg = v_idx, .src2_vreg = -1, .imm = 8, .src3_vreg = v_ptr_b); 2438 /* pc0 = C + (r0*N + col*8 + tcol2)*4 ; pc1 with r1 */ 2439 BW_APPEND(prog, .op = BW_IR_IMAD, .dst_vreg = v_idx, .src1_vreg = v_r0, .src2_vreg = -1, .imm = N, .src3_vreg = v_tcol2); 2440 BW_APPEND(prog, .op = BW_IR_IMAD, .dst_vreg = v_idx, .src1_vreg = v_col, .src2_vreg = -1, .imm = 8, .src3_vreg = v_idx); 2441 BW_APPEND(prog, .op = BW_IR_IMAD_WIDE, .dst_vreg = v_pc0, .src1_vreg = v_idx, .src2_vreg = -1, .imm = 4, .src3_vreg = v_ptr_c); 2442 BW_APPEND(prog, .op = BW_IR_IMAD, .dst_vreg = v_idx, .src1_vreg = v_r1, .src2_vreg = -1, .imm = N, .src3_vreg = v_tcol2); 2443 BW_APPEND(prog, .op = BW_IR_IMAD, .dst_vreg = v_idx, .src1_vreg = v_col, .src2_vreg = -1, .imm = 8, .src3_vreg = v_idx); 2444 BW_APPEND(prog, .op = BW_IR_IMAD_WIDE, .dst_vreg = v_pc1, .src1_vreg = v_idx, .src2_vreg = -1, .imm = 4, .src3_vreg = v_ptr_c); 2445 2446 /* ---- column-tile loop ---- */ 2447 const int L_col = (int)prog->count; 2448 BW_APPEND(prog, .op = BW_IR_MOV_RZ, .dst_vreg = v_rd, .dst_subreg = 0, .control = CTRL_MOVRZ_WAIT3); 2449 BW_APPEND(prog, .op = BW_IR_MOV_RZ, .dst_vreg = v_rd, .dst_subreg = 1); 2450 BW_APPEND(prog, .op = BW_IR_MOV_RZ, .dst_vreg = v_rd, .dst_subreg = 2); 2451 BW_APPEND(prog, .op = BW_IR_MOV_RZ, .dst_vreg = v_rd, .dst_subreg = 3); 2452 BW_APPEND(prog, .op = BW_IR_IMAD_WIDE, .dst_vreg = v_pac, .src1_vreg = v_c1, .src2_vreg = -1, .imm = 0, .src3_vreg = v_pa); 2453 BW_APPEND(prog, .op = BW_IR_IMAD_WIDE, .dst_vreg = v_pbc, .src1_vreg = v_c1, .src2_vreg = -1, .imm = 0, .src3_vreg = v_pbcol); 2454 BW_APPEND(prog, .op = BW_IR_MOV_IMM, .dst_vreg = v_cnt, .imm = mutant ? ITERS - 1 : ITERS); 2455 2456 /* ---- K loop: `unroll` steps of loads, then the HMMA chain ---- */ 2457 const int L_k = (int)prog->count; 2458 for (uint32_t u = 0; u < unroll; u++) { 2459 BW_APPEND(prog, .op = BW_IR_LDG_E_128, .dst_vreg = v_ra[u], .src1_vreg = v_pac, .src2_vreg = -1, .src3_vreg = -1, 2460 .ureg = uv_desc, .imm = u * 512u, .control = BW_FRAG_CTRL_LDG_A); 2461 BW_APPEND(prog, .op = BW_IR_LDG_E_64, .dst_vreg = v_rb[u], .src1_vreg = v_pbc, .src2_vreg = -1, .src3_vreg = -1, 2462 .ureg = uv_desc, .imm = u * 256u, .control = BW_FRAG_CTRL_LDG_B); 2463 } 2464 const BlackwellIROpcode hmma = (spec->precision == OMEGA_MATMUL_PRECISION_BF16) ? BW_IR_HMMA_BF16 : BW_IR_HMMA_F16; 2465 for (uint32_t u = 0; u < unroll; u++) { 2466 BW_APPEND(prog, .op = hmma, .dst_vreg = v_rd, .src1_vreg = v_ra[u], .src2_vreg = v_rb[u], .src3_vreg = v_rd, 2467 .control = BW_FRAG_CTRL_HMMA); 2468 BW_APPEND(prog, .op = BW_IR_NOP, .dst_vreg = -1, .src1_vreg = -1, .src2_vreg = -1, .src3_vreg = -1, .ureg = -1, 2469 .control = BW_FRAG_CTRL_NOP); 2470 } 2471 BW_APPEND(prog, .op = BW_IR_IMAD_WIDE, .dst_vreg = v_pac, .src1_vreg = v_c1, .src2_vreg = -1, .imm = unroll * 512u, .src3_vreg = v_pac); 2472 BW_APPEND(prog, .op = BW_IR_IMAD_WIDE, .dst_vreg = v_pbc, .src1_vreg = v_c1, .src2_vreg = -1, .imm = unroll * 256u, .src3_vreg = v_pbc); 2473 BW_APPEND(prog, .op = BW_IR_IMAD, .dst_vreg = v_cnt, .src1_vreg = v_c1, .src2_vreg = -1, .imm = 0xffffffffu, .src3_vreg = v_cnt); 2474 BW_APPEND(prog, .op = BW_IR_ISETP_GE, .dst_vreg = -1, .src1_vreg = v_cnt, .src2_vreg = -1, .imm = 1, .src3_vreg = -1); 2475 { 2476 int here = (int)prog->count; 2477 BW_APPEND(prog, .op = BW_IR_BRA, .imm = (uint32_t)(int32_t)(L_k - here), .predicate_p0 = true); 2478 } 2479 const int L_k_end = (int)prog->count - 1; 2480 2481 /* ---- store the 16x8 tile (as the looped kernel) ---- */ 2482 BW_APPEND(prog, .op = BW_IR_IMAD_WIDE, .dst_vreg = v_tc1, .src1_vreg = v_c1, .src2_vreg = -1, .imm = 4, .src3_vreg = v_pc0); 2483 BW_APPEND(prog, .op = BW_IR_IMAD_WIDE, .dst_vreg = v_tc3, .src1_vreg = v_c1, .src2_vreg = -1, .imm = 4, .src3_vreg = v_pc1); 2484 BW_APPEND(prog, .op = BW_IR_STG_E, .src1_vreg = v_pc0, .src2_vreg = v_rd, .src2_subreg = 0, .src3_vreg = -1, .ureg = uv_desc, .control = CTRL_STG_RDBAR3); 2485 BW_APPEND(prog, .op = BW_IR_STG_E, .src1_vreg = v_tc1, .src2_vreg = v_rd, .src2_subreg = 1, .src3_vreg = -1, .ureg = uv_desc, .control = CTRL_STG_RDBAR3); 2486 BW_APPEND(prog, .op = BW_IR_STG_E, .src1_vreg = v_pc1, .src2_vreg = v_rd, .src2_subreg = 2, .src3_vreg = -1, .ureg = uv_desc, .control = CTRL_STG_RDBAR3); 2487 BW_APPEND(prog, .op = BW_IR_STG_E, .src1_vreg = v_tc3, .src2_vreg = v_rd, .src2_subreg = 3, .src3_vreg = -1, .ureg = uv_desc, .control = CTRL_STG_RDBAR3); 2488 /* next column tile: col += grid_x ; C by grid_x*8 f32 (waits SB3 first) ; B by grid_x tiles of KS*256 bytes */ 2489 BW_APPEND(prog, .op = BW_IR_IMAD, .dst_vreg = v_col, .src1_vreg = v_c1, .src2_vreg = -1, .imm = grid_x, .src3_vreg = v_col); 2490 BW_APPEND(prog, .op = BW_IR_IMAD_WIDE, .dst_vreg = v_pc0, .src1_vreg = v_c1, .src2_vreg = -1, .imm = grid_x * 32u, .src3_vreg = v_pc0, .control = CTRL_IMADW_WAIT3); 2491 BW_APPEND(prog, .op = BW_IR_IMAD_WIDE, .dst_vreg = v_pc1, .src1_vreg = v_c1, .src2_vreg = -1, .imm = grid_x * 32u, .src3_vreg = v_pc1); 2492 BW_APPEND(prog, .op = BW_IR_IMAD_WIDE, .dst_vreg = v_pbcol, .src1_vreg = v_c1, .src2_vreg = -1, .imm = grid_x * KS * 256u, .src3_vreg = v_pbcol); 2493 BW_APPEND(prog, .op = BW_IR_ISETP_GE, .dst_vreg = -1, .src1_vreg = v_col, .src2_vreg = -1, .imm = NT, .src3_vreg = -1); 2494 { 2495 int here = (int)prog->count; 2496 BW_APPEND(prog, .op = BW_IR_BRA, .imm = (uint32_t)(int32_t)(L_col - here), .predicate_p0 = true, .predicate_not = true); 2497 } 2498 const int L_col_end = (int)prog->count - 1; 2499 BW_APPEND(prog, .op = BW_IR_EXIT); 2500 BW_APPEND(prog, .op = BW_IR_BRA); 2501 2502 /* Loop-carried values live to the end of their loop (the allocator knows no back edges). 2503 * Only these are read in a later pass than the one that wrote them; prologue temporaries 2504 * are dead before the first loop and may be reused. K-loop temporaries (fragments) are 2505 * kept to the end of the pass, as in the looped kernel. */ 2506 const int col_carried[] = { v_c1, v_col, v_pa, v_pbcol, v_pc0, v_pc1, v_rd }; 2507 for (size_t i = 0; i < sizeof col_carried / sizeof col_carried[0]; i++) bw_live_until(prog, col_carried[i], L_col_end); 2508 const int k_carried[] = { v_pac, v_pbc, v_cnt }; 2509 for (size_t i = 0; i < sizeof k_carried / sizeof k_carried[0]; i++) bw_live_until(prog, k_carried[i], L_k_end); 2510 bw_keep_alive(prog, L_k, L_k_end, L_k_end); 2511 return omega_bw_regalloc_solve(prog); 2512 } 2513 #else 2514 int omega_blackwell_codegen_matmul_frag_prog(const OmegaMatMulSpec *s, uint32_t grid, uint32_t u, 2515 int mutant, BlackwellIRProgram *p) { 2516 return bw_om_matprog(omega_cg_frag, s, grid, u, 0, 0, mutant, p); 2517 } 2518 #endif 2519 2520 #ifndef OMEGA_GPU_NO_POSIX 2521 int omega_blackwell_codegen_matmul_frag(const OmegaMatMulSpec *spec, uint32_t grid_x, uint32_t unroll, 2522 int mutant, OmegaBlackwellKernel *kernel) { 2523 if (!spec || !kernel) return -1; 2524 BlackwellIRProgram prog; 2525 if (omega_blackwell_codegen_matmul_frag_prog(spec, grid_x, unroll, mutant, &prog) != 0) return -1; 2526 return bw_kernel_alloc_encode(&prog, OMEGA_BW_CODE_CAP_SMALL, kernel); 2527 } 2528 #endif 2529 2530 /* Capped at 4: unroll 8 (72 registers, 16 loads per pass) never completed on the GB10, with all 2531 * loads on SB2 and again with A on SB2 and B on SB4 (2026-10-07 07:50Z and 07:54Z, omega #328), 2532 * while unroll 4 (48 registers) ran every Qwen3 decode shape correctly. Cause UNKNOWN; the next 2533 * experiment is the unroll-4 kernel launched with 72 declared registers (one variable). */ 2534 /* ---- 2026-10-12: row/column-blocked fragment kernel (prefill rows) ------------------------ 2535 * 2536 * The kernel above gives one warp one 16x8 output tile: every B fragment is streamed once per row 2537 * tile and every A fragment once per column tile, so at m = 128 on GB10 the three Qwen3-8B prefill 2538 * shapes move about 4.7 GB per call at the DRAM rate (128x4096x24576: 20.9 ms, 2026-10-12 baseline). 2539 * Here one warp owns RT row tiles x CT column tiles (RT*CT accumulators) over all of K: per K step 2540 * it loads RT A fragments and CT B fragments and runs RT*CT HMMAs, so each loaded fragment feeds 2541 * RT (B) or CT (A) tensor ops. Same packing, grid rules and C layout as the kernel above: the A 2542 * fragments of row tile i sit at +i*KS*512 bytes, the B fragments of column tile j at +j*KS*256, 2543 * both immediate offsets of the same cursors; the C tile (i, j) is at +i*16*N*4 + j*32 bytes from 2544 * the two row pointers (STG.E immediate offsets, fixtures in omega_blackwell_verify_codegen_fixtures_fragmm). 2545 * Loads per pass stay at or under 8 (unroll*(RT+CT)), the count the proven unroll-4 kernel issues; 2546 * unroll 8 of that kernel (16 loads per pass) never completed on the GB10 and the cause is UNKNOWN, 2547 * so this kernel does not go past that envelope. The HMMA chain keeps the dependent-chain control 2548 * words (stall 15 then NOP stall 14) although the RT*CT accumulators are independent: a longer stall 2549 * is never wrong, and the loads dominate the pass. CTA = one warp, as above; a grid of 2550 * (grid_x, row blocks); column blocks advance by grid_x per pass of the column loop. */ 2551 /* The C store offset of the last tile (rt-1, ct-1) is a signed 24-bit STG immediate; for wide N (lm_head, 2552 * 151936 columns) rt > 1 does not fit. Shared by the generator below and omega_matmul_frag_block_shape. */ 2553 #ifdef BW_MATMUL_C_PRIMARY 2554 static int frag_block_store_fits(uint32_t rt, uint32_t ct, uint32_t n) { 2555 return (uint64_t)(rt - 1) * 16u * n * 4u + (uint64_t)(ct - 1) * 32u + 4u < (1ull << 23); 2556 } 2557 2558 int omega_blackwell_codegen_matmul_frag_block_prog(const OmegaMatMulSpec *spec, uint32_t grid_x, uint32_t unroll, 2559 uint32_t rt, uint32_t ct, int mutant, BlackwellIRProgram *prog) { 2560 if (!spec || !prog) return -1; 2561 if (spec->m == 0 || spec->k == 0 || spec->n == 0) return -1; 2562 if (spec->precision != OMEGA_MATMUL_PRECISION_FP16 && spec->precision != OMEGA_MATMUL_PRECISION_BF16) return -1; 2563 if (spec->m % 16 != 0 || spec->n % 8 != 0 || spec->k % 16 != 0) return -1; 2564 if (rt != 1 && rt != 2 && rt != 4 && rt != 8) return -1; 2565 if (ct != 1 && ct != 2 && ct != 4) return -1; 2566 if (rt * ct > 16 || rt * ct < 2) return -1; /* 1x1 is the kernel above */ 2567 if (unroll != 1 && unroll != 2) return -1; 2568 if (unroll * (rt + ct) > 8) return -1; 2569 const uint32_t K = spec->k, N = spec->n, NT = N / 8, KS = K / 16; 2570 if (KS % unroll != 0 || NT % ct != 0) return -1; 2571 const uint32_t ITERS = KS / unroll, NB = NT / ct; 2572 if (grid_x == 0 || grid_x > NB) return -1; 2573 if (mutant && ITERS < 2) return -1; 2574 /* every index and immediate below must fit: 32 bits for the index arithmetic, signed 24 bits for 2575 * the load/store offsets (rows up to OMEGA_BW_MATMUL_MAX_M) */ 2576 if ((uint64_t)NT * KS * 32u > 0xffffffffull || (uint64_t)(OMEGA_BW_MATMUL_MAX_M / 16u) * KS * 32u > 0xffffffffull || 2577 (uint64_t)grid_x * ct * KS * 256u > 0xffffffffull || (uint64_t)N * 32u > 0xffffffffull || 2578 (uint64_t)OMEGA_BW_MATMUL_MAX_M * N > 0xffffffffull) 2579 return -1; 2580 if ((uint64_t)(rt - 1) * KS * 512u + (unroll - 1) * 512u >= (1ull << 23) || 2581 (uint64_t)(ct - 1) * KS * 256u + (unroll - 1) * 256u >= (1ull << 23) || 2582 !frag_block_store_fits(rt, ct, N)) 2583 return -1; 2584 2585 omega_bw_ir_init(prog); 2586 int v_ptr_a = omega_bw_ir_alloc_vreg64(prog), v_ptr_b = omega_bw_ir_alloc_vreg64(prog), v_ptr_c = omega_bw_ir_alloc_vreg64(prog); 2587 int uv_desc = omega_bw_ir_alloc_uvreg64(prog); 2588 int v_tid = omega_bw_ir_alloc_vreg(prog), v_ctaid_x = omega_bw_ir_alloc_vreg(prog), v_ctaid_y = omega_bw_ir_alloc_vreg(prog); 2589 int v_group = omega_bw_ir_alloc_vreg(prog), v_tig = omega_bw_ir_alloc_vreg(prog), v_tcol2 = omega_bw_ir_alloc_vreg(prog); 2590 int v_r0 = omega_bw_ir_alloc_vreg(prog), v_r1 = omega_bw_ir_alloc_vreg(prog), v_c1 = omega_bw_ir_alloc_vreg(prog); 2591 int v_idx = omega_bw_ir_alloc_vreg(prog), v_col = omega_bw_ir_alloc_vreg(prog), v_cnt = omega_bw_ir_alloc_vreg(prog); 2592 int v_pa = omega_bw_ir_alloc_vreg64(prog), v_pbcol = omega_bw_ir_alloc_vreg64(prog); 2593 int v_pc0 = omega_bw_ir_alloc_vreg64(prog), v_pc1 = omega_bw_ir_alloc_vreg64(prog); 2594 int v_pac = omega_bw_ir_alloc_vreg64(prog), v_pbc = omega_bw_ir_alloc_vreg64(prog); 2595 int v_rd[16], v_ra[2][8], v_rb[2][4]; 2596 for (uint32_t t = 0; t < rt * ct; t++) v_rd[t] = omega_bw_ir_alloc_vreg128(prog); 2597 for (uint32_t u = 0; u < unroll; u++) { 2598 for (uint32_t i = 0; i < rt; i++) v_ra[u][i] = omega_bw_ir_alloc_vreg128(prog); 2599 for (uint32_t j = 0; j < ct; j++) v_rb[u][j] = omega_bw_ir_alloc_vreg64(prog); 2600 } 2601 if (v_rb[unroll - 1][ct - 1] < 0 || v_ra[unroll - 1][rt - 1] < 0 || v_rd[rt * ct - 1] < 0 || uv_desc < 0) return -1; 2602 2603 const uint32_t CTRL_STG_RDBAR3 = 0x0007e200u, CTRL_IMADW_WAIT3 = 0x009fcc00u, CTRL_MOVRZ_WAIT3 = 0x008fe200u; 2604 2605 /* prologue: descriptor, pointers, ids (as the kernel above; a CTA's row block is rt row tiles) */ 2606 BW_APPEND(prog, .op = BW_IR_LDCU64, .dst_vreg = uv_desc, .imm = 0x358, .is_uniform = true); 2607 BW_APPEND(prog, .op = BW_IR_LDC64, .dst_vreg = v_ptr_a, .imm = 0x380); 2608 BW_APPEND(prog, .op = BW_IR_LDC64, .dst_vreg = v_ptr_b, .imm = 0x388); 2609 BW_APPEND(prog, .op = BW_IR_LDC64, .dst_vreg = v_ptr_c, .imm = 0x390); 2610 BW_APPEND(prog, .op = BW_IR_S2R, .dst_vreg = v_tid, .imm = BW_SR_TID_X); 2611 BW_APPEND(prog, .op = BW_IR_S2R, .dst_vreg = v_ctaid_x, .imm = BW_SR_CTAID_X); 2612 BW_APPEND(prog, .op = BW_IR_S2R, .dst_vreg = v_ctaid_y, .imm = BW_SR_CTAID_Y); 2613 BW_APPEND(prog, .op = BW_IR_MOV_IMM, .dst_vreg = v_c1, .imm = 1); 2614 BW_APPEND(prog, .op = BW_IR_SHF_R, .dst_vreg = v_group, .src1_vreg = v_tid, .imm = 2); 2615 BW_APPEND(prog, .op = BW_IR_LOP3_AND, .dst_vreg = v_tig, .src1_vreg = v_tid, .imm = 3); 2616 BW_APPEND(prog, .op = BW_IR_IMAD, .dst_vreg = v_tcol2, .src1_vreg = v_tig, .src2_vreg = -1, .imm = 2, .src3_vreg = -1); 2617 /* r0 = ctaid_y * 16 * rt + group: row of this lane in row tile 0 of the block; r1 = r0 + 8 */ 2618 BW_APPEND(prog, .op = BW_IR_IMAD, .dst_vreg = v_r0, .src1_vreg = v_ctaid_y, .src2_vreg = -1, .imm = 16u * rt, .src3_vreg = v_group); 2619 BW_APPEND(prog, .op = BW_IR_IMAD, .dst_vreg = v_r1, .src1_vreg = v_c1, .src2_vreg = -1, .imm = 8, .src3_vreg = v_r0); 2620 /* pa = A + (ctaid_y * rt * KS * 32 + tid) * 16: row tile 0 of this block, this lane's 16 bytes of step 0 */ 2621 BW_APPEND(prog, .op = BW_IR_IMAD, .dst_vreg = v_idx, .src1_vreg = v_ctaid_y, .src2_vreg = -1, .imm = rt * KS * 32u, .src3_vreg = v_tid); 2622 BW_APPEND(prog, .op = BW_IR_IMAD_WIDE, .dst_vreg = v_pa, .src1_vreg = v_idx, .src2_vreg = -1, .imm = 16, .src3_vreg = v_ptr_a); 2623 /* col = ctaid_x * ct: first column tile of this CTA's first column block */ 2624 BW_APPEND(prog, .op = BW_IR_IMAD, .dst_vreg = v_col, .src1_vreg = v_ctaid_x, .src2_vreg = -1, .imm = ct, .src3_vreg = -1); 2625 /* pbcol = B + (col * KS * 32 + tid) * 8: column tile col, this lane's 8 bytes of step 0 */ 2626 BW_APPEND(prog, .op = BW_IR_IMAD, .dst_vreg = v_idx, .src1_vreg = v_col, .src2_vreg = -1, .imm = KS * 32u, .src3_vreg = v_tid); 2627 BW_APPEND(prog, .op = BW_IR_IMAD_WIDE, .dst_vreg = v_pbcol, .src1_vreg = v_idx, .src2_vreg = -1, .imm = 8, .src3_vreg = v_ptr_b); 2628 /* pc0 = C + (r0*N + col*8 + tcol2)*4 ; pc1 with r1 */ 2629 BW_APPEND(prog, .op = BW_IR_IMAD, .dst_vreg = v_idx, .src1_vreg = v_r0, .src2_vreg = -1, .imm = N, .src3_vreg = v_tcol2); 2630 BW_APPEND(prog, .op = BW_IR_IMAD, .dst_vreg = v_idx, .src1_vreg = v_col, .src2_vreg = -1, .imm = 8, .src3_vreg = v_idx); 2631 BW_APPEND(prog, .op = BW_IR_IMAD_WIDE, .dst_vreg = v_pc0, .src1_vreg = v_idx, .src2_vreg = -1, .imm = 4, .src3_vreg = v_ptr_c); 2632 BW_APPEND(prog, .op = BW_IR_IMAD, .dst_vreg = v_idx, .src1_vreg = v_r1, .src2_vreg = -1, .imm = N, .src3_vreg = v_tcol2); 2633 BW_APPEND(prog, .op = BW_IR_IMAD, .dst_vreg = v_idx, .src1_vreg = v_col, .src2_vreg = -1, .imm = 8, .src3_vreg = v_idx); 2634 BW_APPEND(prog, .op = BW_IR_IMAD_WIDE, .dst_vreg = v_pc1, .src1_vreg = v_idx, .src2_vreg = -1, .imm = 4, .src3_vreg = v_ptr_c); 2635 2636 /* ---- column-block loop: zero the rt*ct accumulators (the first MOV waits SB3: the previous 2637 * block's stores have read them), rewind the K cursors ---- */ 2638 const int L_col = (int)prog->count; 2639 for (uint32_t t = 0; t < rt * ct; t++) 2640 for (uint32_t s = 0; s < 4; s++) 2641 BW_APPEND(prog, .op = BW_IR_MOV_RZ, .dst_vreg = v_rd[t], .dst_subreg = (int)s, 2642 .control = (t == 0 && s == 0) ? CTRL_MOVRZ_WAIT3 : 0); 2643 BW_APPEND(prog, .op = BW_IR_IMAD_WIDE, .dst_vreg = v_pac, .src1_vreg = v_c1, .src2_vreg = -1, .imm = 0, .src3_vreg = v_pa); 2644 BW_APPEND(prog, .op = BW_IR_IMAD_WIDE, .dst_vreg = v_pbc, .src1_vreg = v_c1, .src2_vreg = -1, .imm = 0, .src3_vreg = v_pbcol); 2645 BW_APPEND(prog, .op = BW_IR_MOV_IMM, .dst_vreg = v_cnt, .imm = mutant ? ITERS - 1 : ITERS); 2646 2647 /* ---- K loop: `unroll` steps of (rt A + ct B) loads, then the rt*ct*unroll HMMAs ---- */ 2648 const int L_k = (int)prog->count; 2649 for (uint32_t u = 0; u < unroll; u++) { 2650 for (uint32_t i = 0; i < rt; i++) 2651 BW_APPEND(prog, .op = BW_IR_LDG_E_128, .dst_vreg = v_ra[u][i], .src1_vreg = v_pac, .src2_vreg = -1, .src3_vreg = -1, 2652 .ureg = uv_desc, .imm = u * 512u + i * KS * 512u, .control = BW_FRAG_CTRL_LDG_A); 2653 for (uint32_t j = 0; j < ct; j++) 2654 BW_APPEND(prog, .op = BW_IR_LDG_E_64, .dst_vreg = v_rb[u][j], .src1_vreg = v_pbc, .src2_vreg = -1, .src3_vreg = -1, 2655 .ureg = uv_desc, .imm = u * 256u + j * KS * 256u, .control = BW_FRAG_CTRL_LDG_B); 2656 } 2657 const BlackwellIROpcode hmma = (spec->precision == OMEGA_MATMUL_PRECISION_BF16) ? BW_IR_HMMA_BF16 : BW_IR_HMMA_F16; 2658 for (uint32_t u = 0; u < unroll; u++) 2659 for (uint32_t i = 0; i < rt; i++) 2660 for (uint32_t j = 0; j < ct; j++) { 2661 const int rd = v_rd[i * ct + j]; 2662 BW_APPEND(prog, .op = hmma, .dst_vreg = rd, .src1_vreg = v_ra[u][i], .src2_vreg = v_rb[u][j], .src3_vreg = rd, 2663 .control = BW_FRAG_CTRL_HMMA); 2664 BW_APPEND(prog, .op = BW_IR_NOP, .dst_vreg = -1, .src1_vreg = -1, .src2_vreg = -1, .src3_vreg = -1, .ureg = -1, 2665 .control = BW_FRAG_CTRL_NOP); 2666 } 2667 BW_APPEND(prog, .op = BW_IR_IMAD_WIDE, .dst_vreg = v_pac, .src1_vreg = v_c1, .src2_vreg = -1, .imm = unroll * 512u, .src3_vreg = v_pac); 2668 BW_APPEND(prog, .op = BW_IR_IMAD_WIDE, .dst_vreg = v_pbc, .src1_vreg = v_c1, .src2_vreg = -1, .imm = unroll * 256u, .src3_vreg = v_pbc); 2669 BW_APPEND(prog, .op = BW_IR_IMAD, .dst_vreg = v_cnt, .src1_vreg = v_c1, .src2_vreg = -1, .imm = 0xffffffffu, .src3_vreg = v_cnt); 2670 BW_APPEND(prog, .op = BW_IR_ISETP_GE, .dst_vreg = -1, .src1_vreg = v_cnt, .src2_vreg = -1, .imm = 1, .src3_vreg = -1); 2671 { 2672 int here = (int)prog->count; 2673 BW_APPEND(prog, .op = BW_IR_BRA, .imm = (uint32_t)(int32_t)(L_k - here), .predicate_p0 = true); 2674 } 2675 const int L_k_end = (int)prog->count - 1; 2676 2677 /* ---- store the rt*ct tiles: tile (i, j) at +i*16*N*4 + j*32 bytes from pc0 (rows r0) and pc1 (rows r1) ---- */ 2678 for (uint32_t i = 0; i < rt; i++) 2679 for (uint32_t j = 0; j < ct; j++) { 2680 const int rd = v_rd[i * ct + j]; 2681 const uint32_t off = i * 16u * N * 4u + j * 32u; 2682 BW_APPEND(prog, .op = BW_IR_STG_E, .src1_vreg = v_pc0, .src2_vreg = rd, .src2_subreg = 0, .src3_vreg = -1, .ureg = uv_desc, .imm = off, .control = CTRL_STG_RDBAR3); 2683 BW_APPEND(prog, .op = BW_IR_STG_E, .src1_vreg = v_pc0, .src2_vreg = rd, .src2_subreg = 1, .src3_vreg = -1, .ureg = uv_desc, .imm = off + 4u, .control = CTRL_STG_RDBAR3); 2684 BW_APPEND(prog, .op = BW_IR_STG_E, .src1_vreg = v_pc1, .src2_vreg = rd, .src2_subreg = 2, .src3_vreg = -1, .ureg = uv_desc, .imm = off, .control = CTRL_STG_RDBAR3); 2685 BW_APPEND(prog, .op = BW_IR_STG_E, .src1_vreg = v_pc1, .src2_vreg = rd, .src2_subreg = 3, .src3_vreg = -1, .ureg = uv_desc, .imm = off + 4u, .control = CTRL_STG_RDBAR3); 2686 } 2687 /* next column block: col += grid_x*ct ; C by grid_x*ct*8 f32 (waits SB3 first) ; B by grid_x*ct tiles of KS*256 bytes */ 2688 BW_APPEND(prog, .op = BW_IR_IMAD, .dst_vreg = v_col, .src1_vreg = v_c1, .src2_vreg = -1, .imm = grid_x * ct, .src3_vreg = v_col); 2689 BW_APPEND(prog, .op = BW_IR_IMAD_WIDE, .dst_vreg = v_pc0, .src1_vreg = v_c1, .src2_vreg = -1, .imm = grid_x * ct * 32u, .src3_vreg = v_pc0, .control = CTRL_IMADW_WAIT3); 2690 BW_APPEND(prog, .op = BW_IR_IMAD_WIDE, .dst_vreg = v_pc1, .src1_vreg = v_c1, .src2_vreg = -1, .imm = grid_x * ct * 32u, .src3_vreg = v_pc1); 2691 BW_APPEND(prog, .op = BW_IR_IMAD_WIDE, .dst_vreg = v_pbcol, .src1_vreg = v_c1, .src2_vreg = -1, .imm = grid_x * ct * KS * 256u, .src3_vreg = v_pbcol); 2692 BW_APPEND(prog, .op = BW_IR_ISETP_GE, .dst_vreg = -1, .src1_vreg = v_col, .src2_vreg = -1, .imm = NT, .src3_vreg = -1); 2693 { 2694 int here = (int)prog->count; 2695 BW_APPEND(prog, .op = BW_IR_BRA, .imm = (uint32_t)(int32_t)(L_col - here), .predicate_p0 = true, .predicate_not = true); 2696 } 2697 const int L_col_end = (int)prog->count - 1; 2698 BW_APPEND(prog, .op = BW_IR_EXIT); 2699 BW_APPEND(prog, .op = BW_IR_BRA); 2700 2701 /* loop-carried values live to the end of their loop (as the kernel above) */ 2702 const int col_carried[] = { v_c1, v_col, v_pa, v_pbcol, v_pc0, v_pc1 }; 2703 for (size_t i = 0; i < sizeof col_carried / sizeof col_carried[0]; i++) bw_live_until(prog, col_carried[i], L_col_end); 2704 for (uint32_t t = 0; t < rt * ct; t++) bw_live_until(prog, v_rd[t], L_col_end); 2705 const int k_carried[] = { v_pac, v_pbc, v_cnt }; 2706 for (size_t i = 0; i < sizeof k_carried / sizeof k_carried[0]; i++) bw_live_until(prog, k_carried[i], L_k_end); 2707 bw_keep_alive(prog, L_k, L_k_end, L_k_end); 2708 return omega_bw_regalloc_solve(prog); 2709 } 2710 #else 2711 int omega_blackwell_codegen_matmul_frag_block_prog(const OmegaMatMulSpec *s, uint32_t grid, uint32_t u, 2712 uint32_t rt, uint32_t ct, int mutant, BlackwellIRProgram *p) { 2713 return bw_om_matprog(omega_cg_block, s, grid, u, rt, ct, mutant, p); 2714 } 2715 #endif 2716 2717 #ifndef OMEGA_GPU_NO_POSIX 2718 int omega_blackwell_codegen_matmul_frag_block(const OmegaMatMulSpec *spec, uint32_t grid_x, uint32_t unroll, 2719 uint32_t rt, uint32_t ct, int mutant, OmegaBlackwellKernel *kernel) { 2720 if (!spec || !kernel) return -1; 2721 BlackwellIRProgram prog; 2722 if (omega_blackwell_codegen_matmul_frag_block_prog(spec, grid_x, unroll, rt, ct, mutant, &prog) != 0) return -1; 2723 return bw_kernel_alloc_encode(&prog, OMEGA_BW_CODE_CAP_MAX, kernel); 2724 } 2725 #endif 2726 2727 /* The row and column tiles one warp owns in the blocked kernel: the largest of 4, 2, 1 dividing the 2728 * row tiles (rt), and for rt > 1 the largest of 4, 2, 1 dividing the column tiles (ct); rt = ct = 1 2729 * is the single-tile kernel (decode rows). An rt whose last C tile's store offset does not fit the 2730 * 24-bit immediate (frag_block_store_fits: wide N such as lm_head) steps down, to 1 at the end. */ 2731 #ifdef BW_MATMUL_C_PRIMARY 2732 void omega_matmul_frag_block_shape(uint32_t mt, uint32_t nt, uint32_t *rt, uint32_t *ct) { 2733 uint32_t r = mt % 4u == 0 ? 4u : mt % 2u == 0 ? 2u : 1u, c = 1u; 2734 for (; r > 1; r /= 2u) { 2735 c = nt % 4u == 0 ? 4u : nt % 2u == 0 ? 2u : 1u; 2736 if (frag_block_store_fits(r, c, nt * 8u)) break; 2737 } 2738 *rt = r; 2739 *ct = r > 1 ? c : 1u; 2740 } 2741 2742 /* loads per pass at or under 8 (see the kernel comment): unroll 2 only when 2*(rt+ct) fits and KS is even */ 2743 uint32_t omega_matmul_frag_block_unroll(uint32_t kp, uint32_t rt, uint32_t ct) { 2744 const uint32_t ks = kp / 16u; 2745 return (ks % 2u == 0 && 2u * (rt + ct) <= 8u) ? 2u : 1u; 2746 } 2747 2748 uint32_t omega_matmul_frag_unroll(uint32_t kp) { 2749 const uint32_t ks = kp / 16u; 2750 return ks % 4u == 0 ? 4u : ks % 2u == 0 ? 2u : 1u; 2751 } 2752 #else 2753 static uint32_t bw_mat_decide(uint64_t kind, uint64_t a, uint64_t b, uint64_t c) { 2754 uint64_t result = 0; 2755 if (omega_cg_matmul(&result, kind, a, b, c) != 0) __builtin_trap(); 2756 return (uint32_t)result; 2757 } 2758 void omega_matmul_frag_block_shape(uint32_t mt, uint32_t nt, uint32_t *rt, uint32_t *ct) { 2759 uint32_t shape = bw_mat_decide(0, mt, nt, 0); 2760 *rt = shape & 255u; 2761 *ct = shape >> 8; 2762 } 2763 uint32_t omega_matmul_frag_block_unroll(uint32_t kp, uint32_t rt, uint32_t ct) { 2764 return bw_mat_decide(1, kp, rt, ct); 2765 } 2766 uint32_t omega_matmul_frag_unroll(uint32_t kp) { 2767 return bw_mat_decide(2, kp, 0, 0); 2768 } 2769 #endif 2770 2771 #if defined(CUDAPP_MIGRATION_PENDING_C_PACK) 2772 #define BW_PACK_C_PRIMARY 1 2773 #endif 2774 #ifndef BW_PACK_C_PRIMARY 2775 /* Five native slots: fixed metadata plus two runtime views; no staging copy. 2776 * All shape/extent arithmetic, conversion and lane placement is seeded Omega. */ 2777 static void frag_pack(uint64_t p[12], const void *src, uint16_t *dst) { 2778 uint64_t result = UINT64_MAX; 2779 if (omega_cg_pklen(&result, p, 12) || result != 0) return; 2780 (void)omega_cg_pkh(&result, p, 12, src, p[8], dst, p[9]); 2781 } 2782 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) { 2783 uint64_t p[12] = {0, m, k, mp, kp}; 2784 frag_pack(p, a, dst); 2785 } 2786 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) { 2787 uint64_t p[12] = {3, m, k, mp, kp}; 2788 frag_pack(p, a, dst); 2789 } 2790 void omega_matmul_frag_pack_a_row0(const uint16_t *a, uint32_t k, uint32_t kp, uint16_t *dst) { 2791 uint64_t p[12] = {1, 1, k, 16, kp}; 2792 frag_pack(p, a, dst); 2793 } 2794 void omega_matmul_frag_pack_a_row0_f32(const float *a, uint32_t k, uint32_t kp, uint16_t *dst) { 2795 uint64_t p[12] = {4, 1, k, 16, kp}; 2796 frag_pack(p, a, dst); 2797 } 2798 void omega_matmul_frag_pack_b(const uint16_t *b, uint32_t k, uint32_t n, uint32_t kp, 2799 uint32_t nt0, uint32_t nt1, uint16_t *dst) { 2800 uint64_t p[12] = {2, 0, k, 0, kp, n, nt0, nt1}; 2801 frag_pack(p, b, dst); 2802 } 2803 #else 2804 /* A block (mt, ks) holds rows 16mt..16mt+15 and columns 16ks..16ks+15 as 256 bf16 at element 2805 * (mt*KS + ks)*256; inside the block, element (rr, cc) sits at lane*8 + e with lane = (rr%8)*4 + (cc%8)/2 2806 * and e = (cc/8)*4 + (rr/8)*2 + cc%2 (the HMMA.16816 A fragment order, header comment). The 256-entry 2807 * table makes the pack a tile loop with no per-element division (2026-10-12: the per-element form with 2808 * six divisions and a conversion call per element was ~2.5 ms per MB of activations at m = 128). */ 2809 static uint16_t g_frag_a_tab[256]; 2810 static void frag_a_tab_init(void) { 2811 if (g_frag_a_tab[255]) return; 2812 for (uint32_t rr = 0; rr < 16; rr++) 2813 for (uint32_t cc = 0; cc < 16; cc++) 2814 g_frag_a_tab[rr * 16 + cc] = (uint16_t)((((rr & 7u) * 4u + ((cc & 7u) >> 1)) * 8u) + (cc >> 3) * 4u + (rr >> 3) * 2u + (cc & 1u)); 2815 } 2816 /* bit for bit what the compiler's (__bf16)f cast gives (gpu_matmul_api_test checks it against 2817 * omega_fp32_to_bf16 on ties, denormals, inf and NaN payloads): round to nearest even, a NaN keeps 2818 * its top payload bits and is made quiet. gcc 13 on aarch64 emits the cast as a call to the 2819 * soft-float routine __truncsfbf2 per element, which was most of the packing cost. */ 2820 static inline uint16_t frag_f32_to_bf16(float f) { 2821 uint32_t u; 2822 memcpy(&u, &f, 4); 2823 if ((u & 0x7f800000u) == 0x7f800000u) return (uint16_t)((u >> 16) | ((u & 0x007fffffu) ? 0x0040u : 0u)); 2824 return (uint16_t)((u + 0x7fffu + ((u >> 16) & 1u)) >> 16); 2825 } 2826 #define FRAG_PACK_A_BODY(LOAD) \ 2827 do { \ 2828 const uint32_t KS = kp / 16u; \ 2829 frag_a_tab_init(); \ 2830 memset(dst, 0, (size_t)mp * kp * sizeof *dst); \ 2831 for (uint32_t mt = 0; mt < (m + 15u) / 16u; mt++) { \ 2832 const uint32_t r1 = m - mt * 16u < 16u ? m - mt * 16u : 16u; \ 2833 for (uint32_t ks = 0; ks < (k + 15u) / 16u; ks++) { \ 2834 const uint32_t c1 = k - ks * 16u < 16u ? k - ks * 16u : 16u; \ 2835 uint16_t *blk = dst + ((size_t)mt * KS + ks) * 256u; \ 2836 for (uint32_t rr = 0; rr < r1; rr++) { \ 2837 const uint16_t *tab = g_frag_a_tab + rr * 16u; \ 2838 const size_t src = (size_t)(mt * 16u + rr) * k + ks * 16u; \ 2839 for (uint32_t cc = 0; cc < c1; cc++) blk[tab[cc]] = LOAD(a[src + cc]); \ 2840 } \ 2841 } \ 2842 } \ 2843 } while (0) 2844 #define FRAG_ID(x) (x) 2845 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) { 2846 FRAG_PACK_A_BODY(FRAG_ID); 2847 } 2848 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) { 2849 FRAG_PACK_A_BODY(frag_f32_to_bf16); 2850 } 2851 2852 /* Row 0 only, every column of every K block (zeros past k): for a one-row call whose buffer already 2853 * holds zeros in rows 1..15 of every block from an earlier full pack of the same width. */ 2854 #define FRAG_PACK_A_ROW0_BODY(LOAD) \ 2855 do { \ 2856 const uint32_t KS = kp / 16u; \ 2857 frag_a_tab_init(); \ 2858 const uint16_t *tab = g_frag_a_tab; \ 2859 for (uint32_t ks = 0; ks < KS; ks++) { \ 2860 uint16_t *blk = dst + (size_t)ks * 256u; \ 2861 const uint32_t c0 = ks * 16u, c1 = c0 >= k ? 0u : (k - c0 < 16u ? k - c0 : 16u); \ 2862 for (uint32_t cc = 0; cc < c1; cc++) blk[tab[cc]] = LOAD(a[c0 + cc]); \ 2863 for (uint32_t cc = c1; cc < 16u; cc++) blk[tab[cc]] = 0; \ 2864 } \ 2865 } while (0) 2866 void omega_matmul_frag_pack_a_row0(const uint16_t *a, uint32_t k, uint32_t kp, uint16_t *dst) { 2867 FRAG_PACK_A_ROW0_BODY(FRAG_ID); 2868 } 2869 #endif /* BW_PACK_C_PRIMARY */ 2870 #ifdef BW_PACK_C_PRIMARY 2871 size_t omega_matmul_result_sentinels(float *cd, uint32_t m, uint32_t n, uint32_t np, float poison, int set) { 2872 /* one word per 16 x 8 fragment tile that holds a real output word: the tile's last real row and column */ 2873 size_t count = 0; 2874 if (m == 0 || n == 0) return 0; 2875 const uint32_t mt = (m + 15u) / 16u, nt = (n + 7u) / 8u; 2876 for (uint32_t ti = 0; ti < mt; ti++) { 2877 uint32_t r = ti * 16u + 15u; if (r >= m) r = m - 1u; 2878 float *row = cd + (size_t)r * np; 2879 for (uint32_t tj = 0; tj < nt; tj++) { 2880 uint32_t c = tj * 8u + 7u; if (c >= n) c = n - 1u; 2881 if (set) { row[c] = poison; count++; } 2882 else if (row[c] == poison) count++; 2883 } 2884 } 2885 return count; 2886 } 2887 #else 2888 size_t omega_matmul_result_sentinels(float *cd, uint32_t m, uint32_t n, uint32_t np, float poison, int set) { 2889 uint32_t bits; 2890 memcpy(&bits, &poison, sizeof bits); 2891 uint64_t q[5] = {m, n, np, (uint32_t)set, bits}, count = 0; 2892 if (omega_sentin(&count, q, 5, cd, (uint64_t)m * np)) return 0; 2893 return (size_t)count; 2894 } 2895 #endif 2896 2897 #ifdef BW_PACK_C_PRIMARY 2898 void omega_matmul_frag_pack_a_row0_f32(const float *a, uint32_t k, uint32_t kp, uint16_t *dst) { 2899 FRAG_PACK_A_ROW0_BODY(frag_f32_to_bf16); 2900 } 2901 2902 void omega_matmul_frag_pack_b(const uint16_t *b, uint32_t k, uint32_t n, uint32_t kp, 2903 uint32_t nt0, uint32_t nt1, uint16_t *dst) { 2904 const uint32_t KS = kp / 16u; 2905 memset(dst, 0, (size_t)(nt1 - nt0) * KS * 128u * sizeof *dst); 2906 for (uint32_t row = 0; row < k; row++) { 2907 const uint32_t ks = row / 16u, rr = row % 16u, khi = rr / 8u, tig = (rr % 8u) / 2u, lo = rr % 2u; 2908 const uint16_t *src = &b[(size_t)row * n]; 2909 for (uint32_t nt = nt0; nt < nt1; nt++) { 2910 const uint32_t c0 = nt * 8u; 2911 for (uint32_t group = 0; group < 8u && c0 + group < n; group++) { 2912 const size_t lane = group * 4u + tig; 2913 dst[(((size_t)(nt - nt0) * KS + ks) * 32u + lane) * 4u + khi * 2u + lo] = src[c0 + group]; 2914 } 2915 } 2916 } 2917 } 2918 #endif /* BW_PACK_C_PRIMARY */ 2919 2920 int omega_blackwell_verify_codegen_fixtures(void) { 2921 OmegaRegAlloc ra; 2922 memset(&ra, 0, sizeof(ra)); 2923 ra.vreg_to_phys[0] = 2; 2924 ra.vreg_to_phys[1] = 4; 2925 ra.vreg_to_phys[2] = 6; 2926 ra.vreg_to_phys[3] = 7; 2927 ra.uvreg_to_phys[0] = 4; 2928 2929 uint32_t w[4]; 2930 2931 /* 1. MOV R7, RZ */ 2932 BlackwellIRInsn insn_mov_rz = { .op = BW_IR_MOV_RZ, .dst_vreg = 3 }; 2933 if (!omega_fv_status(encode_single_insn(&insn_mov_rz, &ra, w), 1)) return -1; 2934 if (!omega_fv_words(w, omega_fv_bits(0x7202, 7, 16, 0, 0), 0xff, 0x00000f00, 0, 7)) return -2; 2935 2936 /* 2. MOV R2, 1 */ 2937 BlackwellIRInsn insn_mov_imm = { .op = BW_IR_MOV_IMM, .dst_vreg = 0, .imm = 1 }; 2938 if (!omega_fv_status(encode_single_insn(&insn_mov_imm, &ra, w), 1)) return -3; 2939 if (!omega_fv_words(w, omega_fv_bits(0x7802, 2, 16, 0, 0), 1, 0x00000f00, 0, 7)) return -4; 2940 2941 /* 3. Register-Register IMAD: IMAD R7, R2, R6, R7 */ 2942 BlackwellIRInsn insn_imad_rr = { .op = BW_IR_IMAD, .dst_vreg = 3, .src1_vreg = 0, .src2_vreg = 2, .src3_vreg = 3 }; 2943 if (!omega_fv_status(encode_single_insn(&insn_imad_rr, &ra, w), 1)) return -5; 2944 if (!omega_fv_words(w, omega_fv_bits(0x7224, 7, 16, 2, 24), 6, omega_fv_bits(0x078e0200, 7, 0, 0, 0), 0, 7)) return -6; 2945 2946 /* 4. Immediate IMAD: IMAD R6, R2, 16, RZ */ 2947 BlackwellIRInsn insn_imad_imm = { .op = BW_IR_IMAD, .dst_vreg = 2, .src1_vreg = 0, .src2_vreg = -1, .imm = 16, .src3_vreg = -1 }; 2948 if (!omega_fv_status(encode_single_insn(&insn_imad_imm, &ra, w), 1)) return -7; 2949 if (!omega_fv_words(w, omega_fv_bits(0x7824, 6, 16, 2, 24), 16, 0x078e02ff, 0, 7)) return -8; 2950 2951 /* 5. IMAD.WIDE: IMAD.WIDE R4, R2, 4, R4 */ 2952 BlackwellIRInsn insn_imad_wide = { .op = BW_IR_IMAD_WIDE, .dst_vreg = 1, .src1_vreg = 0, .imm = 4, .src3_vreg = 1 }; 2953 if (!omega_fv_status(encode_single_insn(&insn_imad_wide, &ra, w), 1)) return -9; 2954 if (!omega_fv_words(w, omega_fv_bits(0x7825, 4, 16, 2, 24), 4, omega_fv_bits(0x078e0000, 4, 0, 0, 0), 0, 7)) return -10; 2955 2956 /* 6. LDC.64 R4, c[0x0][0x380] */ 2957 BlackwellIRInsn insn_ldc64 = { .op = BW_IR_LDC64, .dst_vreg = 1, .imm = 0x380 }; 2958 if (!omega_fv_status(encode_single_insn(&insn_ldc64, &ra, w), 1)) return -11; 2959 if (!omega_fv_words(w, omega_fv_bits(0xff007b82, 4, 16, 0, 0), omega_fv_bits(0, 0x380, 6, 0, 0), 0x00000a00, 0, 7)) return -12; 2960 2961 /* 7. LDCU.64 UR4, c[0x0][0x358] */ 2962 BlackwellIRInsn insn_ldcu64 = { .op = BW_IR_LDCU64, .dst_vreg = 0, .imm = 0x358, .is_uniform = true }; 2963 if (!omega_fv_status(encode_single_insn(&insn_ldcu64, &ra, w), 1)) return -13; 2964 if (!omega_fv_words(w, omega_fv_bits(0xff0077ac, 4, 16, 0, 0), omega_fv_bits(0, 0x358, 5, 0, 0), 0x08000a00, 0, 7)) return -14; 2965 2966 /* 8. LDG.E R2, desc[UR4][R4.64] */ 2967 BlackwellIRInsn insn_ldg = { .op = BW_IR_LDG_E, .dst_vreg = 0, .src1_vreg = 1, .ureg = 0 }; 2968 if (!omega_fv_status(encode_single_insn(&insn_ldg, &ra, w), 1)) return -15; 2969 if (!omega_fv_words(w, omega_fv_bits(0x7981, 2, 16, 4, 24), 4, 0x0c1e1900, 0, 7)) return -16; 2970 2971 /* 9. STG.E desc[UR4][R4.64], R7 */ 2972 BlackwellIRInsn insn_stg = { .op = BW_IR_STG_E, .src1_vreg = 1, .src2_vreg = 3, .ureg = 0 }; 2973 if (!omega_fv_status(encode_single_insn(&insn_stg, &ra, w), 1)) return -17; 2974 if (!omega_fv_words(w, omega_fv_bits(0x7986, 4, 24, 0, 0), 7, omega_fv_bits(0x0c101900, 4, 0, 0, 0), 0, 7)) return -18; 2975 2976 /* 10. S2R R2, SR_CTAID.X */ 2977 BlackwellIRInsn insn_s2r = { .op = BW_IR_S2R, .dst_vreg = 0, .imm = BW_SR_CTAID_X }; 2978 if (!omega_fv_status(encode_single_insn(&insn_s2r, &ra, w), 1)) return -19; 2979 if (!omega_fv_words(w, omega_fv_bits(0x7919, 2, 16, 0, 0), 0, omega_fv_bits(0, BW_SR_CTAID_X, 8, 0, 0), 0, 5)) return -20; 2980 2981 /* 11. HMMA.16816.F32 R4, R4, R2, RZ */ 2982 BlackwellIRInsn insn_hmma_f16 = { .op = BW_IR_HMMA_F16, .dst_vreg = 1, .src1_vreg = 1, .src2_vreg = 0, .src3_vreg = -1 }; 2983 if (!omega_fv_status(encode_single_insn(&insn_hmma_f16, &ra, w), 1)) return -21; 2984 if (!omega_fv_words(w, 0x0404723c, 0x00000002, 0x000018ff, 0x000fe200, 15)) return -22; 2985 2986 /* 12. HMMA.16816.F32.BF16 R4, R4, R2, RZ */ 2987 BlackwellIRInsn insn_hmma_bf16 = { .op = BW_IR_HMMA_BF16, .dst_vreg = 1, .src1_vreg = 1, .src2_vreg = 0, .src3_vreg = -1 }; 2988 uint32_t w_bf16[4]; 2989 if (!omega_fv_status(encode_single_insn(&insn_hmma_bf16, &ra, w_bf16), 1)) return -23; 2990 if (!omega_fv_words(w_bf16, 0x0404723c, 0x00000002, 0x000418ff, 0x000fe200, 15)) return -24; 2991 2992 /* 13. Differential Invariant: w[2] bit 18 toggles strictly between FP16 and BF16 */ 2993 if (!omega_fv_pair(w, w_bf16, 0)) return -25; 2994 if (!omega_fv_pair(w, w_bf16, 1)) return -26; 2995 2996 /* 14. SHF.R.U32.HI R4, RZ, 2, R2 */ 2997 BlackwellIRInsn insn_shf = { .op = BW_IR_SHF_R, .dst_vreg = 1, .src1_vreg = 0, .imm = 2 }; 2998 if (!omega_fv_status(encode_single_insn(&insn_shf, &ra, w), 1)) return -27; 2999 if (!omega_fv_words(w, 0xff047819, 2, 0x00011602, 0x001fca00, 15)) return -28; 3000 3001 /* 15. LOP3.LUT R4, R2, 3, RZ, 0xc0, !PT */ 3002 BlackwellIRInsn insn_lop3 = { .op = BW_IR_LOP3_AND, .dst_vreg = 1, .src1_vreg = 0, .imm = 3 }; 3003 if (!omega_fv_status(encode_single_insn(&insn_lop3, &ra, w), 1)) return -29; 3004 if (!omega_fv_words(w, 0x02047812, 3, 0x078ec0ff, 0x001fca00, 15)) return -30; 3005 3006 /* 16. LDG.E.U16 R2, desc[UR4][R4.64] */ 3007 BlackwellIRInsn insn_ldg16 = { .op = BW_IR_LDG_E_U16, .dst_vreg = 0, .src1_vreg = 1, .ureg = 0 }; 3008 if (!omega_fv_status(encode_single_insn(&insn_ldg16, &ra, w), 1)) return -31; 3009 if (!omega_fv_words(w, 0x04027981, 4, 0x0c1e1500, 0x000f2200, 15)) return -32; 3010 3011 /* A zero-delta branch stays the self-branch the matmul kernels already emit. */ 3012 BlackwellIRInsn insn_bra0 = { .op = BW_IR_BRA }; 3013 if (!omega_fv_status(encode_single_insn(&insn_bra0, &ra, w), 1)) return -33; 3014 if (!omega_fv_words(w, 0x00fc7947, 0xfffffffc, 0x0383ffff, 0x000fc000, 15)) return -34; 3015 3016 BlackwellIRInsn insn_ge = { .op = BW_IR_ISETP_GE_U32, .src1_vreg = 1, .src2_vreg = 2 }; 3017 if (!omega_fv_status(encode_single_insn(&insn_ge, &ra, w), 1)) return -35; 3018 if (!omega_fv_words(w, omega_fv_bits(0x720c, 4, 24, 0, 0), 6, 0x03f06070, 0, 7)) return -36; 3019 3020 BlackwellIRInsn insn_xor = { .op = BW_IR_LOP3_XOR, .dst_vreg = 1, .src1_vreg = 0, .src2_vreg = 2 }; 3021 if (!omega_fv_status(encode_single_insn(&insn_xor, &ra, w), 1)) return -37; 3022 if (!omega_fv_words(w, omega_fv_bits(0x7212, 4, 16, 2, 24), 6, 0x078e3cff, 0, 7)) return -38; 3023 3024 BlackwellIRInsn insn_ldg_ss = { .op = BW_IR_LDG_STRONG_SYS, .dst_vreg = 0, .src1_vreg = 1, .ureg = 0 }; 3025 if (!omega_fv_status(encode_single_insn(&insn_ldg_ss, &ra, w), 1)) return -39; 3026 if (!omega_fv_words(w, omega_fv_bits(0x7981, 2, 16, 4, 24), 4, 0x0c1f5900, 0, 7)) return -40; 3027 /* 17. FADD R7, R2, R4 */ 3028 BlackwellIRInsn insn_fadd = { .op = BW_IR_FADD, .dst_vreg = 3, .src1_vreg = 0, .src2_vreg = 1 }; 3029 if (!omega_fv_status(encode_single_insn(&insn_fadd, &ra, w), 1)) return -133; 3030 if (!omega_fv_words(w, 0x02077221, 4, 0, 0x004fc400, 15)) return -134; 3031 3032 /* 18. FSUB R7, R2, R4 (negated src2) */ 3033 BlackwellIRInsn insn_fsub = { .op = BW_IR_FSUB, .dst_vreg = 3, .src1_vreg = 0, .src2_vreg = 1 }; 3034 if (!omega_fv_status(encode_single_insn(&insn_fsub, &ra, w), 1)) return -135; 3035 if (!omega_fv_words(w, 0x02077221, 0x80000004, 0, 0x004fc400, 15)) return -136; 3036 3037 /* 19. FMUL R7, R2, R4 */ 3038 BlackwellIRInsn insn_fmul = { .op = BW_IR_FMUL, .dst_vreg = 3, .src1_vreg = 0, .src2_vreg = 1 }; 3039 if (!omega_fv_status(encode_single_insn(&insn_fmul, &ra, w), 1)) return -137; 3040 if (!omega_fv_words(w, 0x02077220, 4, 0x00400000, 0x004fc400, 15)) return -138; 3041 3042 /* 20. FFMA R7, R2, R4, R6 */ 3043 BlackwellIRInsn insn_ffma = { .op = BW_IR_FFMA, .dst_vreg = 3, .src1_vreg = 0, .src2_vreg = 1, .src3_vreg = 2 }; 3044 if (!omega_fv_status(encode_single_insn(&insn_ffma, &ra, w), 1)) return -139; 3045 if (!omega_fv_words(w, 0x02077223, 4, 6, 0x004fc400, 15)) return -140; 3046 3047 /* 21. FMNMX_MIN R7, R2, R4 */ 3048 BlackwellIRInsn insn_min = { .op = BW_IR_FMNMX_MIN, .dst_vreg = 3, .src1_vreg = 0, .src2_vreg = 1 }; 3049 if (!omega_fv_status(encode_single_insn(&insn_min, &ra, w), 1)) return -141; 3050 if (!omega_fv_words(w, 0x02077209, 4, 0x03800000, 0x004fc400, 15)) return -142; 3051 3052 /* 22. FMNMX_MAX R7, R2, R4 */ 3053 BlackwellIRInsn insn_max = { .op = BW_IR_FMNMX_MAX, .dst_vreg = 3, .src1_vreg = 0, .src2_vreg = 1 }; 3054 if (!omega_fv_status(encode_single_insn(&insn_max, &ra, w), 1)) return -143; 3055 if (!omega_fv_words(w, 0x02077209, 4, 0x07800000, 0x004fc400, 15)) return -144; 3056 3057 /* 23. I2FP R7, R2 */ 3058 BlackwellIRInsn insn_i2f = { .op = BW_IR_I2FP, .dst_vreg = 3, .src1_vreg = 0 }; 3059 if (!omega_fv_status(encode_single_insn(&insn_i2f, &ra, w), 1)) return -145; 3060 if (!omega_fv_words(w, 0x00077245, 2, 0x00201400, 0x004fe200, 15)) return -146; 3061 3062 /* 24. F2I R7, R2 */ 3063 BlackwellIRInsn insn_f2i = { .op = BW_IR_F2I, .dst_vreg = 3, .src1_vreg = 0 }; 3064 if (!omega_fv_status(encode_single_insn(&insn_f2i, &ra, w), 1)) return -147; 3065 if (!omega_fv_words(w, 0x00077305, 2, 0x0020f100, 0x004e2200, 15)) return -148; 3066 3067 /* 25. MUFU_RCP R7, R2 */ 3068 BlackwellIRInsn insn_mufu_rcp = { .op = BW_IR_MUFU_RCP, .dst_vreg = 3, .src1_vreg = 0 }; 3069 if (!omega_fv_status(encode_single_insn(&insn_mufu_rcp, &ra, w), 1)) return -149; 3070 if (!omega_fv_words(w, 0x00077308, 2, 0x00001000, 0x000e2400, 15)) return -150; 3071 3072 /* 26. MUFU_RSQ R7, R2 */ 3073 BlackwellIRInsn insn_mufu_rsq = { .op = BW_IR_MUFU_RSQ, .dst_vreg = 3, .src1_vreg = 0 }; 3074 if (!omega_fv_status(encode_single_insn(&insn_mufu_rsq, &ra, w), 1)) return -151; 3075 if (!omega_fv_words(w, 0x00077308, 2, 0x00001400, 0x000e2400, 15)) return -152; 3076 3077 /* 27. SHFL_DOWN R7, R2, 16 */ 3078 BlackwellIRInsn insn_shfl = { .op = BW_IR_SHFL_DOWN, .dst_vreg = 3, .src1_vreg = 0, .imm = 16 }; 3079 if (!omega_fv_status(encode_single_insn(&insn_shfl, &ra, w), 1)) return -153; 3080 if (!omega_fv_words(w, 0x02077f89, 0x0a001f00, 0x000e0000, 0x000e2400, 15)) return -154; 3081 3082 /* 28. LDS R7, [R2] */ 3083 BlackwellIRInsn insn_lds = { .op = BW_IR_LDS, .dst_vreg = 3, .src1_vreg = 0, .imm = 0 }; 3084 if (!omega_fv_status(encode_single_insn(&insn_lds, &ra, w), 1)) return -155; 3085 if (!omega_fv_words(w, 0x02077984, 0, 0, 0x000fe200, 15)) return -156; 3086 3087 /* 29. STS [R2], R4 */ 3088 BlackwellIRInsn insn_sts = { .op = BW_IR_STS, .src1_vreg = 0, .src2_vreg = 1, .imm = 0 }; 3089 if (!omega_fv_status(encode_single_insn(&insn_sts, &ra, w), 1)) return -157; 3090 if (!omega_fv_words(w, 0x02007388, 4, 0, 0x000fe200, 15)) return -158; 3091 3092 return 0; 3093 } 3094 3095 int omega_blackwell_test_regalloc_bounds(void) { 3096 BlackwellIRProgram prog; 3097 omega_bw_ir_init(&prog); 3098 3099 int v0 = omega_bw_ir_alloc_vreg64(&prog); 3100 int v1 = omega_bw_ir_alloc_vreg(&prog); 3101 int v2 = omega_bw_ir_alloc_vreg64(&prog); 3102 int v3 = omega_bw_ir_alloc_vreg128(&prog); /* Test 128-bit quad allocation */ 3103 3104 BlackwellIRInsn i0 = { .op = BW_IR_LDC64, .dst_vreg = v0, .imm = 0x380 }; 3105 omega_bw_ir_append(&prog, &i0); 3106 BlackwellIRInsn i1 = { .op = BW_IR_MOV_IMM, .dst_vreg = v1, .imm = 10 }; 3107 omega_bw_ir_append(&prog, &i1); 3108 BlackwellIRInsn i2 = { .op = BW_IR_LDC64, .dst_vreg = v2, .imm = 0x388 }; 3109 omega_bw_ir_append(&prog, &i2); 3110 BlackwellIRInsn i3 = { .op = BW_IR_HMMA_F16, .dst_vreg = v3, .src1_vreg = v3, .src2_vreg = v0, .src3_vreg = -1 }; 3111 omega_bw_ir_append(&prog, &i3); 3112 /* i4 uses v1, v2, v3 simultaneously so all intervals are concurrently active */ 3113 int v_sink = omega_bw_ir_alloc_vreg(&prog); 3114 BlackwellIRInsn i4 = { .op = BW_IR_IADD3, .dst_vreg = v_sink, .src1_vreg = v1, .src2_vreg = v2, .src3_vreg = v3 }; 3115 omega_bw_ir_append(&prog, &i4); 3116 3117 if (omega_bw_regalloc_solve(&prog) != 0) return -1; 3118 3119 int phys_v0 = prog.regalloc.vreg_to_phys[v0]; 3120 int phys_v1 = prog.regalloc.vreg_to_phys[v1]; 3121 int phys_v2 = prog.regalloc.vreg_to_phys[v2]; 3122 int phys_v3 = prog.regalloc.vreg_to_phys[v3]; 3123 3124 if (phys_v0 % 2 != 0) return -2; 3125 if (phys_v2 % 2 != 0) return -3; 3126 if (phys_v3 % 4 != 0) return -4; /* 4-alignment for 128-bit quad */ 3127 if (phys_v1 == phys_v0 || phys_v1 == phys_v0 + 1) return -5; 3128 if (phys_v2 == phys_v0 || phys_v2 == phys_v0 + 1) return -6; 3129 for (int k = 0; k < 4; k++) { 3130 if (phys_v3 + k == phys_v0 || phys_v3 + k == phys_v0 + 1) return -7; 3131 if (phys_v3 + k == phys_v2 || phys_v3 + k == phys_v2 + 1) return -8; 3132 if (phys_v3 + k == phys_v1) return -9; 3133 } 3134 3135 /* Test bounds refusal: intentionally attempt to allocate more registers than hardware capacity */ 3136 BlackwellIRProgram overflow_prog; 3137 omega_bw_ir_init(&overflow_prog); 3138 for (int i = 0; i < BW_PHYS_GPR_MAX + 5; i++) { 3139 int v = omega_bw_ir_alloc_vreg(&overflow_prog); 3140 if (v < 0) break; 3141 BlackwellIRInsn insn = { .op = BW_IR_MOV_IMM, .dst_vreg = v, .imm = (uint32_t)i }; 3142 omega_bw_ir_append(&overflow_prog, &insn); 3143 } 3144 for (size_t i = 0; i < overflow_prog.count; i++) { 3145 for (int v = 0; v < overflow_prog.regalloc.num_vregs; v++) { 3146 overflow_prog.regalloc.intervals[v].last_use = (int)overflow_prog.count - 1; 3147 } 3148 } 3149 if (omega_bw_regalloc_solve(&overflow_prog) == 0) { 3150 return -6; 3151 } 3152 3153 return 0; 3154 } 3155 3156 #ifndef OMEGA_GPU_NO_POSIX /* prints: hosted only (the freestanding build has no stdio) */ 3157 int omega_blackwell_test_codegen_variation(void) { 3158 OmegaMatMulSpec spec1, spec2; 3159 if (omega_matmul_spec_init(&spec1, 16, 16, 16, OMEGA_MATMUL_PRECISION_INT32) != 0) { 3160 printf("FAIL: spec1 init failed\n"); 3161 return -1; 3162 } 3163 if (omega_matmul_spec_init(&spec2, 32, 16, 64, OMEGA_MATMUL_PRECISION_INT32) != 0) { 3164 printf("FAIL: spec2 init failed\n"); 3165 return -1; 3166 } 3167 3168 OmegaBlackwellKernel k1, k2; 3169 memset(&k1, 0, sizeof(k1)); 3170 memset(&k2, 0, sizeof(k2)); 3171 3172 if (omega_blackwell_codegen_matmul(&spec1, &k1) != 0) { 3173 printf("FAIL: k1 codegen failed\n"); 3174 return -1; 3175 } 3176 if (omega_blackwell_codegen_matmul(&spec2, &k2) != 0) { 3177 printf("FAIL: k2 codegen failed\n"); 3178 omega_blackwell_kernel_free(&k1); 3179 return -1; 3180 } 3181 3182 OmegaBlackwellRealizationIdentity id1, id2; 3183 omega_blackwell_bind_matmul_realization(&spec1, &k1, &id1); 3184 omega_blackwell_bind_matmul_realization(&spec2, &k2, &id2); 3185 3186 printf("================================================================================\n"); 3187 printf(" OMEGA BLACKWELL CODEGEN VARIATION DEMONSTRATION (GATE 7 STAGE-1)\n"); 3188 printf("================================================================================\n"); 3189 printf("Config 1: 16x16x16 INT32\n"); 3190 printf(" Instructions: %zu (%zu bytes, %u GPRs, %u UGPRs)\n", k1.insn_count, k1.code_size, k1.gpr_count, k1.uniform_gpr_count); 3191 printf(" Spec ID: "); 3192 for (int i = 0; i < 16; i++) printf("%02x", spec1.spec_id[i]); 3193 printf("...\n"); 3194 printf(" Code Digest: "); 3195 for (int i = 0; i < 16; i++) printf("%02x", k1.code_digest[i]); 3196 printf("...\n"); 3197 printf(" Realization: "); 3198 for (int i = 0; i < 16; i++) printf("%02x", id1.realization_id[i]); 3199 printf("...\n\n"); 3200 3201 printf("Config 2: 32x16x64 INT32\n"); 3202 printf(" Instructions: %zu (%zu bytes, %u GPRs, %u UGPRs)\n", k2.insn_count, k2.code_size, k2.gpr_count, k2.uniform_gpr_count); 3203 printf(" Spec ID: "); 3204 for (int i = 0; i < 16; i++) printf("%02x", spec2.spec_id[i]); 3205 printf("...\n"); 3206 printf(" Code Digest: "); 3207 for (int i = 0; i < 16; i++) printf("%02x", k2.code_digest[i]); 3208 printf("...\n"); 3209 printf(" Realization: "); 3210 for (int i = 0; i < 16; i++) printf("%02x", id2.realization_id[i]); 3211 printf("...\n"); 3212 printf("================================================================================\n"); 3213 3214 /* Validation checks */ 3215 if (memcmp(spec1.spec_id, spec2.spec_id, 32) == 0) { 3216 printf("FAIL: spec IDs match (expected distinct)\n"); 3217 return -2; 3218 } 3219 if (memcmp(k1.code_digest, k2.code_digest, 32) == 0) { 3220 printf("FAIL: code digests match (expected distinct)\n"); 3221 return -3; 3222 } 3223 if (memcmp(id1.realization_id, id2.realization_id, 32) == 0) { 3224 printf("FAIL: realization IDs match (expected distinct)\n"); 3225 return -4; 3226 } 3227 3228 printf("PASS: Dynamic codegen variation verified (distinct specifications, code digests, and realization IDs).\n"); 3229 3230 omega_blackwell_kernel_free(&k1); 3231 omega_blackwell_kernel_free(&k2); 3232 return 0; 3233 } 3234 #endif 3235 3236 /* Declared registers: peak + 2 rounded up to 8 (see omega_blackwell_codegen.h for the chip evidence). */ 3237 #ifdef BW_RA_C_PRIMARY 3238 uint32_t omega_bw_gpr_declared(uint32_t peak) { return ((peak + 2u + 7u) / 8u) * 8u; } 3239 int omega_bw_gpr_declared_ok(uint32_t declared, uint32_t peak) { return declared % 8u == 0u && declared >= peak + 2u; } 3240 3241 int omega_bw_ir_set_gpr_limit(BlackwellIRProgram *prog, uint32_t limit) { 3242 if (!prog || limit <= BW_PHYS_GPR_MAX || limit > BW_PHYS_GPR_HW_LIMIT) return -1; 3243 prog->regalloc.gpr_limit = limit; 3244 return 0; 3245 } 3246 #else 3247 uint32_t omega_bw_gpr_declared(uint32_t peak) { 3248 return (uint32_t)bw_ra_decide(6, peak, 0); 3249 } 3250 int omega_bw_gpr_declared_ok(uint32_t declared, uint32_t peak) { 3251 return bw_ra_decide(7, declared, peak); 3252 } 3253 int omega_bw_ir_set_gpr_limit(BlackwellIRProgram *prog, uint32_t limit) { 3254 if (!prog || bw_ra_decide(8, limit, 0) != 1) return -1; 3255 prog->regalloc.gpr_limit = limit; 3256 return 0; 3257 } 3258 #endif