omega-mv
Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance.
Brought by drakestapleton
omega-mv / evidence / OWN30B-REQUAL-MAIN-710d438c-20261015 / gates / reconv / GPU-RECONV-710d438c / omega_blackwell_codegen.c
| 1 | #include "omega_blackwell_codegen.h" |
| 2 | #include "omega_blackwell_submit.h" |
| 3 | #include "sha256.h" |
| 4 | #include <string.h> |
| 5 | /* native v1 P07 cut A.2 (aienos#286, docs/numeric/OMEGA_GPU_NATIVE_BOUNDARY.md): with OMEGA_GPU_NO_POSIX this file |
| 6 | * builds -ffreestanding. Code generation then runs only through the allocation-free entry points |
| 7 | * (omega_blackwell_kernel_encode_into: the caller supplies the code buffer and its capacity); the allocating |
| 8 | * generators, their diagnostics and the printing variation self-test exist only in the hosted build. */ |
| 9 | #ifndef OMEGA_GPU_NO_POSIX |
| 10 | #include <stdio.h> |
| 11 | #include <stdlib.h> |
| 12 | #define BW_DIAG(...) fprintf(stderr, __VA_ARGS__) |
| 13 | #else |
| 14 | #define BW_DIAG(...) ((void)0) |
| 15 | #endif |
| 16 | |
| 17 | void omega_bw_ir_init(BlackwellIRProgram *prog) { |
| 18 | if (!prog) return; |
| 19 | memset(prog, 0, sizeof(*prog)); |
| 20 | } |
| 21 | |
| 22 | int omega_bw_ir_alloc_vreg(BlackwellIRProgram *prog) { |
| 23 | if (!prog || prog->regalloc.num_vregs >= BW_MAX_VREGS) return -1; |
| 24 | int v = prog->regalloc.num_vregs++; |
| 25 | prog->regalloc.intervals[v].vreg = v; |
| 26 | prog->regalloc.intervals[v].first_def = -1; |
| 27 | prog->regalloc.intervals[v].last_use = -1; |
| 28 | prog->regalloc.intervals[v].phys_reg = -1; |
| 29 | prog->regalloc.intervals[v].bundle_size = 1; |
| 30 | prog->regalloc.intervals[v].is_pair = false; |
| 31 | prog->regalloc.intervals[v].is_quad = false; |
| 32 | prog->regalloc.intervals[v].active = false; |
| 33 | prog->regalloc.vreg_to_phys[v] = -1; |
| 34 | return v; |
| 35 | } |
| 36 | |
| 37 | int omega_bw_ir_alloc_vreg64(BlackwellIRProgram *prog) { |
| 38 | if (!prog || prog->regalloc.num_vregs >= BW_MAX_VREGS) return -1; |
| 39 | int v = prog->regalloc.num_vregs++; |
| 40 | prog->regalloc.intervals[v].vreg = v; |
| 41 | prog->regalloc.intervals[v].first_def = -1; |
| 42 | prog->regalloc.intervals[v].last_use = -1; |
| 43 | prog->regalloc.intervals[v].phys_reg = -1; |
| 44 | prog->regalloc.intervals[v].bundle_size = 2; |
| 45 | prog->regalloc.intervals[v].is_pair = true; |
| 46 | prog->regalloc.intervals[v].is_quad = false; |
| 47 | prog->regalloc.intervals[v].active = false; |
| 48 | prog->regalloc.vreg_to_phys[v] = -1; |
| 49 | return v; |
| 50 | } |
| 51 | |
| 52 | int omega_bw_ir_alloc_vreg128(BlackwellIRProgram *prog) { |
| 53 | if (!prog || prog->regalloc.num_vregs >= BW_MAX_VREGS) return -1; |
| 54 | int v = prog->regalloc.num_vregs++; |
| 55 | prog->regalloc.intervals[v].vreg = v; |
| 56 | prog->regalloc.intervals[v].first_def = -1; |
| 57 | prog->regalloc.intervals[v].last_use = -1; |
| 58 | prog->regalloc.intervals[v].phys_reg = -1; |
| 59 | prog->regalloc.intervals[v].bundle_size = 4; |
| 60 | prog->regalloc.intervals[v].is_pair = false; |
| 61 | prog->regalloc.intervals[v].is_quad = true; |
| 62 | prog->regalloc.intervals[v].active = false; |
| 63 | prog->regalloc.vreg_to_phys[v] = -1; |
| 64 | return v; |
| 65 | } |
| 66 | |
| 67 | int omega_bw_ir_alloc_uvreg(BlackwellIRProgram *prog) { |
| 68 | if (!prog || prog->regalloc.num_uvregs >= BW_MAX_UVREGS) return -1; |
| 69 | int uv = prog->regalloc.num_uvregs++; |
| 70 | prog->regalloc.uintervals[uv].vreg = uv; |
| 71 | prog->regalloc.uintervals[uv].first_def = -1; |
| 72 | prog->regalloc.uintervals[uv].last_use = -1; |
| 73 | prog->regalloc.uintervals[uv].phys_reg = -1; |
| 74 | prog->regalloc.uintervals[uv].is_pair = false; |
| 75 | prog->regalloc.uintervals[uv].active = false; |
| 76 | prog->regalloc.uvreg_to_phys[uv] = -1; |
| 77 | return uv; |
| 78 | } |
| 79 | |
| 80 | int omega_bw_ir_alloc_uvreg64(BlackwellIRProgram *prog) { |
| 81 | if (!prog || prog->regalloc.num_uvregs >= BW_MAX_UVREGS) return -1; |
| 82 | int uv = prog->regalloc.num_uvregs++; |
| 83 | prog->regalloc.uintervals[uv].vreg = uv; |
| 84 | prog->regalloc.uintervals[uv].first_def = -1; |
| 85 | prog->regalloc.uintervals[uv].last_use = -1; |
| 86 | prog->regalloc.uintervals[uv].phys_reg = -1; |
| 87 | prog->regalloc.uintervals[uv].is_pair = true; |
| 88 | prog->regalloc.uintervals[uv].active = false; |
| 89 | prog->regalloc.uvreg_to_phys[uv] = -1; |
| 90 | return uv; |
| 91 | } |
| 92 | |
| 93 | int omega_bw_ir_append(BlackwellIRProgram *prog, const BlackwellIRInsn *insn) { |
| 94 | if (!prog || !insn || prog->count >= BW_MAX_IR_INSNS) return -1; |
| 95 | int idx = (int)prog->count++; |
| 96 | prog->insns[idx] = *insn; |
| 97 | |
| 98 | /* Update live-interval def/use tracking */ |
| 99 | if (insn->dst_vreg >= 0 && insn->dst_vreg < prog->regalloc.num_vregs) { |
| 100 | if (!insn->is_uniform) { |
| 101 | OmegaLiveInterval *iv = &prog->regalloc.intervals[insn->dst_vreg]; |
| 102 | if (iv->first_def < 0) iv->first_def = idx; |
| 103 | if (idx > iv->last_use) iv->last_use = idx; |
| 104 | } |
| 105 | } |
| 106 | if (insn->src1_vreg >= 0 && insn->src1_vreg < prog->regalloc.num_vregs) { |
| 107 | OmegaLiveInterval *iv = &prog->regalloc.intervals[insn->src1_vreg]; |
| 108 | if (iv->first_def < 0) iv->first_def = idx; |
| 109 | if (idx > iv->last_use) iv->last_use = idx; |
| 110 | } |
| 111 | if (insn->src2_vreg >= 0 && insn->src2_vreg < prog->regalloc.num_vregs) { |
| 112 | OmegaLiveInterval *iv = &prog->regalloc.intervals[insn->src2_vreg]; |
| 113 | if (iv->first_def < 0) iv->first_def = idx; |
| 114 | if (idx > iv->last_use) iv->last_use = idx; |
| 115 | } |
| 116 | if (insn->src3_vreg >= 0 && insn->src3_vreg < prog->regalloc.num_vregs) { |
| 117 | OmegaLiveInterval *iv = &prog->regalloc.intervals[insn->src3_vreg]; |
| 118 | if (iv->first_def < 0) iv->first_def = idx; |
| 119 | if (idx > iv->last_use) iv->last_use = idx; |
| 120 | } |
| 121 | |
| 122 | /* Uniform register def/use tracking */ |
| 123 | if (insn->is_uniform && insn->dst_vreg >= 0 && insn->dst_vreg < prog->regalloc.num_uvregs) { |
| 124 | OmegaLiveInterval *uiv = &prog->regalloc.uintervals[insn->dst_vreg]; |
| 125 | if (uiv->first_def < 0) uiv->first_def = idx; |
| 126 | if (idx > uiv->last_use) uiv->last_use = idx; |
| 127 | } |
| 128 | if (insn->ureg >= 0 && insn->ureg < prog->regalloc.num_uvregs) { |
| 129 | OmegaLiveInterval *uiv = &prog->regalloc.uintervals[insn->ureg]; |
| 130 | if (uiv->first_def < 0) uiv->first_def = idx; |
| 131 | if (idx > uiv->last_use) uiv->last_use = idx; |
| 132 | } |
| 133 | |
| 134 | return idx; |
| 135 | } |
| 136 | |
| 137 | int omega_bw_regalloc_solve(BlackwellIRProgram *prog) { |
| 138 | if (!prog) return -1; |
| 139 | OmegaRegAlloc *ra = &prog->regalloc; |
| 140 | |
| 141 | /* |
| 142 | * Bounded deterministic linear-scan register allocator. |
| 143 | * Computes genuine live intervals across virtual registers and assigns |
| 144 | * available physical GPRs (R2..R127) and UGPRs (UR4..UR31). |
| 145 | * Enforces even-alignment and dual-register reservation for 64-bit pairs. |
| 146 | */ |
| 147 | bool gpr_busy[BW_PHYS_GPR_MAX]; |
| 148 | memset(gpr_busy, 0, sizeof(gpr_busy)); |
| 149 | |
| 150 | int max_phys_used = BW_PHYS_GPR_START; |
| 151 | |
| 152 | /* Sort virtual register indices chronologically by first_def (Poletto & Sarkar Linear Scan) */ |
| 153 | int order[BW_MAX_VREGS]; |
| 154 | int count = 0; |
| 155 | for (int v = 0; v < ra->num_vregs; v++) { |
| 156 | if (ra->intervals[v].first_def >= 0) { |
| 157 | order[count++] = v; |
| 158 | } else { |
| 159 | ra->intervals[v].phys_reg = BW_PHYS_GPR_START; |
| 160 | ra->vreg_to_phys[v] = ra->intervals[v].phys_reg; |
| 161 | } |
| 162 | } |
| 163 | for (int i = 0; i < count - 1; i++) { |
| 164 | for (int j = i + 1; j < count; j++) { |
| 165 | if (ra->intervals[order[i]].first_def > ra->intervals[order[j]].first_def) { |
| 166 | int tmp = order[i]; |
| 167 | order[i] = order[j]; |
| 168 | order[j] = tmp; |
| 169 | } |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | for (int idx = 0; idx < count; idx++) { |
| 174 | int v = order[idx]; |
| 175 | OmegaLiveInterval *iv = &ra->intervals[v]; |
| 176 | |
| 177 | /* Free physical registers whose virtual live intervals have ended */ |
| 178 | for (int p = 0; p < ra->num_vregs; p++) { |
| 179 | if (ra->intervals[p].active && ra->intervals[p].last_use < iv->first_def) { |
| 180 | int freed = ra->intervals[p].phys_reg; |
| 181 | if (freed >= 0 && freed < BW_PHYS_GPR_MAX) { |
| 182 | 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)); |
| 183 | for (uint32_t k = 0; k < bsize && (freed + (int)k) < BW_PHYS_GPR_MAX; k++) { |
| 184 | gpr_busy[freed + k] = false; |
| 185 | } |
| 186 | } |
| 187 | ra->intervals[p].active = false; |
| 188 | } |
| 189 | } |
| 190 | |
| 191 | /* Find lowest available physical register */ |
| 192 | int assigned = -1; |
| 193 | uint32_t bsize = iv->bundle_size ? iv->bundle_size : (iv->is_pair ? 2 : (iv->is_quad ? 4 : 1)); |
| 194 | if (bsize == 4) { |
| 195 | /* 128-bit quad requires 4-alignment and 4 contiguous free registers */ |
| 196 | int start = (BW_PHYS_GPR_START + 3) & ~3; |
| 197 | for (int p = start; p <= BW_PHYS_GPR_MAX - 4; p += 4) { |
| 198 | if (!gpr_busy[p] && !gpr_busy[p + 1] && !gpr_busy[p + 2] && !gpr_busy[p + 3]) { |
| 199 | assigned = p; |
| 200 | for (int k = 0; k < 4; k++) gpr_busy[p + k] = true; |
| 201 | break; |
| 202 | } |
| 203 | } |
| 204 | } else if (bsize == 2) { |
| 205 | /* 64-bit pair requires even alignment and contiguous free registers */ |
| 206 | int start = (BW_PHYS_GPR_START + 1) & ~1; |
| 207 | for (int p = start; p <= BW_PHYS_GPR_MAX - 2; p += 2) { |
| 208 | if (!gpr_busy[p] && !gpr_busy[p + 1]) { |
| 209 | assigned = p; |
| 210 | gpr_busy[p] = true; |
| 211 | gpr_busy[p + 1] = true; |
| 212 | break; |
| 213 | } |
| 214 | } |
| 215 | } else { |
| 216 | for (int p = BW_PHYS_GPR_START; p < BW_PHYS_GPR_MAX; p++) { |
| 217 | if (!gpr_busy[p]) { |
| 218 | assigned = p; |
| 219 | gpr_busy[p] = true; |
| 220 | break; |
| 221 | } |
| 222 | } |
| 223 | } |
| 224 | |
| 225 | if (assigned < 0) { |
| 226 | /* Hardware register capacity exceeded: fail closed loudly */ |
| 227 | return -1; |
| 228 | } |
| 229 | |
| 230 | iv->phys_reg = assigned; |
| 231 | iv->active = true; |
| 232 | ra->vreg_to_phys[v] = assigned; |
| 233 | |
| 234 | int top = assigned + (int)bsize - 1; |
| 235 | if (top > max_phys_used) { |
| 236 | max_phys_used = top; |
| 237 | } |
| 238 | } |
| 239 | ra->peak_gpr_usage = (uint32_t)(max_phys_used + 1); |
| 240 | |
| 241 | /* Allocate uniform registers */ |
| 242 | int cur_u = BW_PHYS_UGPR_START; |
| 243 | for (int uv = 0; uv < ra->num_uvregs; uv++) { |
| 244 | OmegaLiveInterval *uiv = &ra->uintervals[uv]; |
| 245 | if (uiv->is_pair) { |
| 246 | if (cur_u % 2 != 0) cur_u++; |
| 247 | if (cur_u + 1 >= BW_PHYS_UGPR_MAX) return -1; |
| 248 | uiv->phys_reg = cur_u; |
| 249 | ra->uvreg_to_phys[uv] = cur_u; |
| 250 | cur_u += 2; |
| 251 | } else { |
| 252 | if (cur_u >= BW_PHYS_UGPR_MAX) return -1; |
| 253 | uiv->phys_reg = cur_u; |
| 254 | ra->uvreg_to_phys[uv] = cur_u++; |
| 255 | } |
| 256 | } |
| 257 | ra->peak_ugpr_usage = (uint32_t)cur_u; |
| 258 | |
| 259 | return 0; |
| 260 | } |
| 261 | |
| 262 | static int encode_single_insn(const BlackwellIRInsn *insn, const OmegaRegAlloc *ra, uint32_t w[4]) { |
| 263 | memset(w, 0, 16); |
| 264 | |
| 265 | int dst = (insn->dst_vreg >= 0) ? (ra->vreg_to_phys[insn->dst_vreg] + insn->dst_subreg) : 0; |
| 266 | int src1 = (insn->src1_vreg >= 0) ? (ra->vreg_to_phys[insn->src1_vreg] + insn->src1_subreg) : 0; |
| 267 | int src2 = (insn->src2_vreg >= 0) ? (ra->vreg_to_phys[insn->src2_vreg] + insn->src2_subreg) : 0; |
| 268 | int src3 = (insn->src3_vreg >= 0) ? (ra->vreg_to_phys[insn->src3_vreg] + insn->src3_subreg) : 0xff; /* Default to RZ (0xff) */ |
| 269 | int ureg = (insn->ureg >= 0) ? ra->uvreg_to_phys[insn->ureg] : 0; |
| 270 | |
| 271 | if (insn->is_uniform && insn->dst_vreg >= 0) { |
| 272 | dst = ra->uvreg_to_phys[insn->dst_vreg]; |
| 273 | } |
| 274 | |
| 275 | switch (insn->op) { |
| 276 | case BW_IR_NOP: |
| 277 | w[0] = 0x00007918; |
| 278 | w[1] = 0x00000000; |
| 279 | w[2] = 0x00000000; |
| 280 | w[3] = insn->control ? insn->control : 0x000fc000; /* control: a timed pause (fragment matmul) */ |
| 281 | break; |
| 282 | |
| 283 | case BW_IR_MOV_RZ: |
| 284 | /* MOV Rd, RZ */ |
| 285 | w[0] = 0x7202 | ((uint32_t)(dst & 0xff) << 16); |
| 286 | w[1] = 0x000000ff; |
| 287 | w[2] = 0x00000f00; |
| 288 | w[3] = insn->control ? insn->control : 0x000fe200; |
| 289 | break; |
| 290 | |
| 291 | case BW_IR_MOV_IMM: |
| 292 | /* MOV Rd, imm32 */ |
| 293 | w[0] = 0x7802 | ((uint32_t)(dst & 0xff) << 16); |
| 294 | w[1] = insn->imm; |
| 295 | w[2] = 0x00000f00; |
| 296 | w[3] = insn->control ? insn->control : 0x000fe200; |
| 297 | break; |
| 298 | |
| 299 | case BW_IR_S2R: |
| 300 | /* S2R Rd, SR */ |
| 301 | w[0] = 0x7919 | ((uint32_t)(dst & 0xff) << 16); |
| 302 | w[1] = 0x0; |
| 303 | w[2] = (insn->imm & 0xff) << 8; |
| 304 | w[3] = insn->control ? insn->control : 0x000e2200; |
| 305 | break; |
| 306 | |
| 307 | case BW_IR_LDC: |
| 308 | /* LDC Rd, c[0x0][imm] */ |
| 309 | w[0] = 0xff007b82 | ((uint32_t)(dst & 0xff) << 16); |
| 310 | w[1] = (insn->imm << 6) & 0x00ffff00; |
| 311 | w[2] = 0x00000800; |
| 312 | w[3] = insn->control ? insn->control : 0x000fe200; |
| 313 | break; |
| 314 | |
| 315 | case BW_IR_LDC64: |
| 316 | /* LDC.64 Rd:Rd+1, c[0x0][imm] */ |
| 317 | w[0] = 0xff007b82 | ((uint32_t)(dst & 0xff) << 16); |
| 318 | w[1] = (insn->imm << 6) & 0x00ffff00; |
| 319 | w[2] = 0x00000a00; |
| 320 | w[3] = insn->control ? insn->control : 0x000e2200; |
| 321 | break; |
| 322 | |
| 323 | case BW_IR_LDCU: |
| 324 | /* LDCU URd, c[0x0][imm] */ |
| 325 | w[0] = 0xff0077ac | ((uint32_t)(dst & 0xff) << 16); |
| 326 | w[1] = (insn->imm << 5) & 0x00ffff00; |
| 327 | w[2] = 0x08000800; |
| 328 | w[3] = insn->control ? insn->control : 0x000e2200; |
| 329 | break; |
| 330 | |
| 331 | case BW_IR_LDCU64: |
| 332 | /* LDCU.64 URd:URd+1, c[0x0][imm] */ |
| 333 | w[0] = 0xff0077ac | ((uint32_t)(dst & 0xff) << 16); |
| 334 | w[1] = (insn->imm << 5) & 0x00ffff00; |
| 335 | w[2] = 0x08000a00; |
| 336 | w[3] = insn->control ? insn->control : 0x000e6e00; |
| 337 | break; |
| 338 | |
| 339 | case BW_IR_IMAD: |
| 340 | if (insn->src2_vreg >= 0) { |
| 341 | /* Register-register: IMAD Rd, Ra, Rb, Rc (0x7224) */ |
| 342 | w[0] = 0x7224 | ((uint32_t)(dst & 0xff) << 16) | ((uint32_t)(src1 & 0xff) << 24); |
| 343 | w[1] = (uint32_t)(src2 & 0xff); |
| 344 | w[2] = 0x078e0200 | (uint32_t)(src3 & 0xff); |
| 345 | w[3] = insn->control ? insn->control : 0x001fca00; |
| 346 | } else if (insn->is_uniform && insn->ureg >= 0) { |
| 347 | /* Uniform register multiplier: IMAD Rd, Ra, URb, Rc (0x7c24) */ |
| 348 | w[0] = 0x7c24 | ((uint32_t)(dst & 0xff) << 16) | ((uint32_t)(src1 & 0xff) << 24); |
| 349 | w[1] = (uint32_t)(ureg & 0xff); |
| 350 | w[2] = 0x0f8e0200 | (uint32_t)(src3 & 0xff); |
| 351 | w[3] = insn->control ? insn->control : 0x001fca00; |
| 352 | } else { |
| 353 | /* Immediate multiplier: IMAD Rd, Ra, imm, Rc (0x7824) */ |
| 354 | w[0] = 0x7824 | ((uint32_t)(dst & 0xff) << 16) | ((uint32_t)(src1 & 0xff) << 24); |
| 355 | w[1] = insn->imm; |
| 356 | w[2] = 0x078e0200 | (uint32_t)(src3 & 0xff); |
| 357 | w[3] = insn->control ? insn->control : 0x001fca00; |
| 358 | } |
| 359 | break; |
| 360 | |
| 361 | case BW_IR_IMAD_WIDE: |
| 362 | /* IMAD.WIDE.U32 Rd:Rd+1, Ra, imm, Rc:Rc+1 */ |
| 363 | w[0] = 0x7825 | ((uint32_t)(dst & 0xff) << 16) | ((uint32_t)(src1 & 0xff) << 24); |
| 364 | w[1] = insn->imm; |
| 365 | w[2] = 0x078e0000 | (uint32_t)(src3 & 0xff); |
| 366 | w[3] = insn->control ? insn->control : 0x001fcc00; |
| 367 | break; |
| 368 | |
| 369 | case BW_IR_ISETP_GE: |
| 370 | if (insn->is_uniform && insn->ureg >= 0) { |
| 371 | w[0] = 0x7c0c | ((uint32_t)(dst & 0xff) << 16) | ((uint32_t)(src1 & 0xff) << 24); |
| 372 | w[1] = (uint32_t)(ureg & 0xff); |
| 373 | w[2] = 0x0bf06070; |
| 374 | w[3] = 0x002fda00; |
| 375 | } else { |
| 376 | w[0] = 0x780c | ((uint32_t)(dst & 0xff) << 16) | ((uint32_t)(src1 & 0xff) << 24); |
| 377 | w[1] = insn->imm; |
| 378 | w[2] = 0x03f06270; |
| 379 | w[3] = 0x001fda00; |
| 380 | } |
| 381 | break; |
| 382 | |
| 383 | case BW_IR_LDG_E: |
| 384 | /* LDG.E Rd, desc[URd][Ra.64] */ |
| 385 | w[0] = 0x7981 | ((uint32_t)(dst & 0xff) << 16) | ((uint32_t)(src1 & 0xff) << 24); |
| 386 | w[1] = (uint32_t)(ureg & 0xff); |
| 387 | w[2] = 0x0c1e1900; |
| 388 | w[3] = insn->control ? insn->control : 0x000f2200; |
| 389 | break; |
| 390 | |
| 391 | case BW_IR_STG_E: { |
| 392 | /* STG.E desc[URd][Ra.64+imm], Rb. imm = signed 24-bit byte offset at bits 40..63 (w[1] bits |
| 393 | * 8..31), as for LDG; words from nvcc 13.0.88 -arch=sm_121 (omega_blackwell_verify_codegen_fixtures_fragmm, |
| 394 | * 2026-10-12: STG.E desc[UR4][R4.64+0x4], R9 = 0x0000040904007986 0x000fe8000c101904). */ |
| 395 | int32_t off = (int32_t)insn->imm; |
| 396 | if (off < -(1 << 23) || off >= (1 << 23)) return -1; |
| 397 | w[0] = 0x7986 | ((uint32_t)(src1 & 0xff) << 24); |
| 398 | w[1] = (uint32_t)(src2 & 0xff) | (((uint32_t)off & 0xffffffU) << 8); |
| 399 | w[2] = 0x0c101900 | (uint32_t)(ureg & 0xff); |
| 400 | w[3] = insn->control ? insn->control : 0x000fe200; |
| 401 | break; |
| 402 | } |
| 403 | |
| 404 | case BW_IR_STG_EF: |
| 405 | /* STG.E.MMIO.GPU. Uncached store, meant to be visible without a fence. */ |
| 406 | w[0] = 0x7986 | ((uint32_t)(src1 & 0xff) << 24); |
| 407 | w[1] = (uint32_t)(src2 & 0xff); |
| 408 | w[2] = 0x0c111900 | (uint32_t)(ureg & 0xff); |
| 409 | w[3] = insn->control ? insn->control : 0x000fe200; |
| 410 | break; |
| 411 | |
| 412 | case BW_IR_IADD3: |
| 413 | /* IADD3 Rd, PT, PT, Ra, Rb, Rc */ |
| 414 | w[0] = 0x7210 | ((uint32_t)(dst & 0xff) << 16) | ((uint32_t)(src1 & 0xff) << 24); |
| 415 | w[1] = (uint32_t)(src2 & 0xff); |
| 416 | w[2] = 0x07ffe0ff; |
| 417 | w[3] = 0x010fca00; |
| 418 | break; |
| 419 | |
| 420 | case BW_IR_HMMA_F16: |
| 421 | case BW_IR_HMMA_BF16: { |
| 422 | /* HMMA.16816.F32[.BF16] Rd, Ra, Rb, Rc */ |
| 423 | w[0] = 0x723c | ((uint32_t)(dst & 0xff) << 16) | ((uint32_t)(src1 & 0xff) << 24); |
| 424 | w[1] = (uint32_t)(src2 & 0xff); |
| 425 | uint32_t prec_bit = (insn->op == BW_IR_HMMA_BF16) ? 0x00040000 : 0x00000000; |
| 426 | w[2] = (uint32_t)(src3 & 0xff) | (0x18 << 8) | prec_bit; |
| 427 | w[3] = insn->control ? insn->control : 0x000fe200; |
| 428 | break; |
| 429 | } |
| 430 | |
| 431 | case BW_IR_SHF_R: |
| 432 | /* SHF.R.U32.HI Rd, RZ, imm, Ra */ |
| 433 | w[0] = 0x7819U | ((uint32_t)(dst & 0xff) << 16) | (0xffU << 24); |
| 434 | w[1] = (uint32_t)insn->imm; |
| 435 | w[2] = 0x00011600 | (uint32_t)(src1 & 0xff); |
| 436 | w[3] = insn->control ? insn->control : 0x001fca00; |
| 437 | break; |
| 438 | |
| 439 | case BW_IR_LOP3_AND: |
| 440 | /* LOP3.LUT Rd, Ra, imm, RZ, 0xc0, !PT */ |
| 441 | w[0] = 0x7812U | ((uint32_t)(dst & 0xff) << 16) | ((uint32_t)(src1 & 0xff) << 24); |
| 442 | w[1] = (uint32_t)insn->imm; |
| 443 | w[2] = 0x078ec0ff; |
| 444 | w[3] = insn->control ? insn->control : 0x001fca00; |
| 445 | break; |
| 446 | |
| 447 | case BW_IR_LDG_E_64: |
| 448 | case BW_IR_LDG_E_128: { |
| 449 | /* LDG.E.64 / LDG.E.128 Rd, desc[URd][Ra.64+imm]. imm = signed 24-bit byte offset at |
| 450 | * bits 40..63 (w[1] bits 8..31); size field in w[2] bits 9..11 (32 = 4, 64 = 5, 128 = 6). |
| 451 | * Words from nvcc 13.0.88 -arch=sm_121 (omega_blackwell_verify_codegen_fixtures_fragmm). */ |
| 452 | int32_t off = (int32_t)insn->imm; |
| 453 | if (off < -(1 << 23) || off >= (1 << 23)) return -1; |
| 454 | w[0] = 0x7981U | ((uint32_t)(dst & 0xff) << 16) | ((uint32_t)(src1 & 0xff) << 24); |
| 455 | w[1] = (uint32_t)(ureg & 0xff) | (((uint32_t)off & 0xffffffU) << 8); |
| 456 | w[2] = insn->op == BW_IR_LDG_E_64 ? 0x0c1e1b00U : 0x0c1e1d00U; |
| 457 | w[3] = insn->control ? insn->control : 0x000f2200; |
| 458 | break; |
| 459 | } |
| 460 | |
| 461 | case BW_IR_LDG_E_U16: |
| 462 | /* LDG.E.U16 Rd, desc[URd][Ra.64] */ |
| 463 | w[0] = 0x7981U | ((uint32_t)(dst & 0xff) << 16) | ((uint32_t)(src1 & 0xff) << 24); |
| 464 | w[1] = (uint32_t)(ureg & 0xff); |
| 465 | w[2] = 0x0c1e1500; |
| 466 | w[3] = insn->control ? insn->control : 0x000f2200; |
| 467 | break; |
| 468 | |
| 469 | case BW_IR_LDG_STRONG_SYS: |
| 470 | /* LDG.E.STRONG.SYS. Same shape as LDG.E; w[2] carries the |
| 471 | * system-scope bits (0x0c1e1900 -> 0x0c1f5900). */ |
| 472 | w[0] = 0x7981 | ((uint32_t)(dst & 0xff) << 16) | ((uint32_t)(src1 & 0xff) << 24); |
| 473 | w[1] = (uint32_t)(ureg & 0xff); |
| 474 | w[2] = 0x0c1f5900; |
| 475 | w[3] = insn->control ? insn->control : 0x000f2200; |
| 476 | break; |
| 477 | |
| 478 | case BW_IR_LDG_MMIO: |
| 479 | /* LDG.E.EF. Drop the cached line, then load. */ |
| 480 | w[0] = 0x7981 | ((uint32_t)(dst & 0xff) << 16) | ((uint32_t)(src1 & 0xff) << 24); |
| 481 | w[1] = (uint32_t)(ureg & 0xff); |
| 482 | w[2] = 0x0c0e1900; |
| 483 | w[3] = insn->control ? insn->control : 0x000f2200; |
| 484 | break; |
| 485 | |
| 486 | case BW_IR_STG_STRONG_SYS: |
| 487 | w[0] = 0x7986 | ((uint32_t)(src1 & 0xff) << 24); |
| 488 | if (insn->predicate_p0) { |
| 489 | uint32_t pred = insn->predicate_not ? 0x8u : 0x0u; |
| 490 | w[0] = (w[0] & ~0xF000u) | (pred << 12); |
| 491 | } |
| 492 | w[1] = (uint32_t)(src2 & 0xff); |
| 493 | w[2] = 0x0c115900 | (uint32_t)(ureg & 0xff); |
| 494 | w[3] = insn->control ? insn->control : 0x000fe200; |
| 495 | break; |
| 496 | |
| 497 | case BW_IR_MEMBAR_ALL_SYS: |
| 498 | w[0] = 0x00007992; |
| 499 | w[1] = 0x00000000; |
| 500 | w[2] = 0x0000b000; |
| 501 | w[3] = insn->control ? insn->control : 0x000fec00; |
| 502 | break; |
| 503 | |
| 504 | case BW_IR_MEMBAR_SC_SYS: |
| 505 | w[0] = 0x00007992; |
| 506 | w[1] = 0x00000000; |
| 507 | w[2] = 0x00003000; |
| 508 | w[3] = insn->control ? insn->control : 0x000fec00; |
| 509 | break; |
| 510 | |
| 511 | case BW_IR_CCTL_IVALL: |
| 512 | w[0] = 0xff00798f; |
| 513 | w[1] = 0x00000000; |
| 514 | w[2] = 0x02000000; |
| 515 | w[3] = insn->control ? insn->control : 0x000fe800; |
| 516 | break; |
| 517 | |
| 518 | case BW_IR_ATOMG_ADD_STRONG_SYS: |
| 519 | w[0] = 0x79a8 | ((uint32_t)(dst & 0xff) << 16) | ((uint32_t)(src1 & 0xff) << 24); |
| 520 | w[1] = (uint32_t)(src2 & 0xff) | 0x80000000u; |
| 521 | w[2] = 0x081f5100 | (uint32_t)(ureg & 0xff); |
| 522 | w[3] = insn->control ? insn->control : 0x00321e00; |
| 523 | break; |
| 524 | |
| 525 | case BW_IR_ATOMG_EXCH_STRONG_SYS: |
| 526 | w[0] = 0x79a8 | ((uint32_t)(dst & 0xff) << 16) | ((uint32_t)(src1 & 0xff) << 24); |
| 527 | w[1] = (uint32_t)(src2 & 0xff) | 0x80000000u; |
| 528 | w[2] = 0x0c1f5100 | (uint32_t)(ureg & 0xff); |
| 529 | w[3] = insn->control ? insn->control : 0x00321e00; |
| 530 | break; |
| 531 | |
| 532 | case BW_IR_ISETP_GE_U32: |
| 533 | /* ISETP.GE.U32.AND P0, PT, Ra, Rb, PT. Checked with nvdisasm -b SM121. */ |
| 534 | w[0] = 0x720c | ((uint32_t)(src1 & 0xff) << 24); |
| 535 | w[1] = (uint32_t)(src2 & 0xff); |
| 536 | w[2] = 0x03f06070; |
| 537 | w[3] = insn->control ? insn->control : 0x001fda00; |
| 538 | break; |
| 539 | |
| 540 | case BW_IR_LOP3_XOR: |
| 541 | /* LOP3.LUT Rd, Ra, Rb, RZ, 0x3c, !PT. Checked with nvdisasm -b SM121. */ |
| 542 | w[0] = 0x7212 | ((uint32_t)(dst & 0xff) << 16) | ((uint32_t)(src1 & 0xff) << 24); |
| 543 | w[1] = (uint32_t)(src2 & 0xff); |
| 544 | w[2] = 0x078e3cff; |
| 545 | w[3] = insn->control ? insn->control : 0x001fca00; |
| 546 | break; |
| 547 | |
| 548 | case BW_IR_FADD: |
| 549 | /* FADD Rd, Ra, Rb */ |
| 550 | w[0] = 0x7221U | ((uint32_t)(dst & 0xff) << 16) | ((uint32_t)(src1 & 0xff) << 24); |
| 551 | w[1] = (uint32_t)(src2 & 0xff); |
| 552 | w[2] = 0x00000000; |
| 553 | w[3] = insn->control ? insn->control : 0x004fc400; |
| 554 | break; |
| 555 | |
| 556 | case BW_IR_FSUB: |
| 557 | /* FSUB Rd, Ra, Rb (FADD Rd, Ra, -Rb) */ |
| 558 | w[0] = 0x7221U | ((uint32_t)(dst & 0xff) << 16) | ((uint32_t)(src1 & 0xff) << 24); |
| 559 | w[1] = (uint32_t)(src2 & 0xff) | 0x80000000U; |
| 560 | w[2] = 0x00000000; |
| 561 | w[3] = insn->control ? insn->control : 0x004fc400; |
| 562 | break; |
| 563 | |
| 564 | case BW_IR_FMUL: |
| 565 | /* FMUL Rd, Ra, Rb */ |
| 566 | w[0] = 0x7220U | ((uint32_t)(dst & 0xff) << 16) | ((uint32_t)(src1 & 0xff) << 24); |
| 567 | w[1] = (uint32_t)(src2 & 0xff); |
| 568 | w[2] = 0x00400000; |
| 569 | w[3] = insn->control ? insn->control : 0x004fc400; |
| 570 | break; |
| 571 | |
| 572 | case BW_IR_FFMA: |
| 573 | /* FFMA Rd, Ra, Rb, Rc */ |
| 574 | w[0] = 0x7223U | ((uint32_t)(dst & 0xff) << 16) | ((uint32_t)(src1 & 0xff) << 24); |
| 575 | w[1] = (uint32_t)(src2 & 0xff); |
| 576 | w[2] = (uint32_t)(src3 & 0xff); |
| 577 | w[3] = insn->control ? insn->control : 0x004fc400; |
| 578 | break; |
| 579 | |
| 580 | case BW_IR_FSETP: |
| 581 | /* FSETP.cond.AND P0, PT, Ra, Rb, PT */ |
| 582 | w[0] = 0x720bU | ((uint32_t)(dst & 0x7) << 16) | ((uint32_t)(src1 & 0xff) << 24); |
| 583 | w[1] = (uint32_t)(src2 & 0xff); |
| 584 | w[2] = insn->imm ? insn->imm : 0x03f06000; /* Default GE condition */ |
| 585 | w[3] = insn->control ? insn->control : 0x004fc400; |
| 586 | break; |
| 587 | |
| 588 | case BW_IR_FSEL: |
| 589 | /* FSEL Rd, Ra, Rb, P0 */ |
| 590 | w[0] = 0x7208U | ((uint32_t)(dst & 0xff) << 16) | ((uint32_t)(src1 & 0xff) << 24); |
| 591 | w[1] = (uint32_t)(src2 & 0xff); |
| 592 | w[2] = (uint32_t)(src3 & 0x7); |
| 593 | w[3] = insn->control ? insn->control : 0x000fca00; |
| 594 | break; |
| 595 | |
| 596 | case BW_IR_FMNMX_MIN: |
| 597 | /* FMNMX Rd, Ra, Rb, PT (minimum) */ |
| 598 | w[0] = 0x7209U | ((uint32_t)(dst & 0xff) << 16) | ((uint32_t)(src1 & 0xff) << 24); |
| 599 | w[1] = (uint32_t)(src2 & 0xff); |
| 600 | w[2] = 0x03800000; |
| 601 | w[3] = insn->control ? insn->control : 0x004fc400; |
| 602 | break; |
| 603 | |
| 604 | case BW_IR_FMNMX_MAX: |
| 605 | /* FMNMX Rd, Ra, Rb, !PT (maximum) */ |
| 606 | w[0] = 0x7209U | ((uint32_t)(dst & 0xff) << 16) | ((uint32_t)(src1 & 0xff) << 24); |
| 607 | w[1] = (uint32_t)(src2 & 0xff); |
| 608 | w[2] = 0x07800000; |
| 609 | w[3] = insn->control ? insn->control : 0x004fc400; |
| 610 | break; |
| 611 | |
| 612 | case BW_IR_I2FP: |
| 613 | /* I2FP.F32.S32 Rd, Ra */ |
| 614 | w[0] = 0x7245U | ((uint32_t)(dst & 0xff) << 16); |
| 615 | w[1] = (uint32_t)(src1 & 0xff); |
| 616 | w[2] = 0x00201400; |
| 617 | w[3] = insn->control ? insn->control : 0x004fe200; |
| 618 | break; |
| 619 | |
| 620 | case BW_IR_F2I: |
| 621 | /* F2I.TRUNC.NTZ Rd, Ra */ |
| 622 | w[0] = 0x7305U | ((uint32_t)(dst & 0xff) << 16); |
| 623 | w[1] = (uint32_t)(src1 & 0xff); |
| 624 | w[2] = 0x0020f100; |
| 625 | w[3] = insn->control ? insn->control : 0x004e2200; |
| 626 | break; |
| 627 | |
| 628 | case BW_IR_MUFU_RCP: |
| 629 | /* MUFU.RCP Rd, Ra */ |
| 630 | w[0] = 0x7308U | ((uint32_t)(dst & 0xff) << 16); |
| 631 | w[1] = (uint32_t)(src1 & 0xff); |
| 632 | w[2] = 0x00001000; |
| 633 | w[3] = insn->control ? insn->control : 0x000e2400; |
| 634 | break; |
| 635 | |
| 636 | case BW_IR_MUFU_RSQ: |
| 637 | /* MUFU.RSQ Rd, Ra */ |
| 638 | w[0] = 0x7308U | ((uint32_t)(dst & 0xff) << 16); |
| 639 | w[1] = (uint32_t)(src1 & 0xff); |
| 640 | w[2] = 0x00001400; |
| 641 | w[3] = insn->control ? insn->control : 0x000e2400; |
| 642 | break; |
| 643 | |
| 644 | case BW_IR_SHFL_DOWN: |
| 645 | /* SHFL.DOWN PT, Rd, Ra, offset, 0x1f */ |
| 646 | w[0] = 0x7f89U | ((uint32_t)(dst & 0xff) << 16) | ((uint32_t)(src1 & 0xff) << 24); |
| 647 | w[1] = 0x08001f00U | (((uint32_t)insn->imm & 0x1f) << 21); |
| 648 | w[2] = 0x000e0000; |
| 649 | w[3] = insn->control ? insn->control : 0x000e2400; |
| 650 | break; |
| 651 | |
| 652 | case BW_IR_LDS: |
| 653 | /* LDS Rd, [Ra] */ |
| 654 | w[0] = 0x7984U | ((uint32_t)(dst & 0xff) << 16) | ((uint32_t)(src1 & 0xff) << 24); |
| 655 | w[1] = 0x00000000; |
| 656 | w[2] = (uint32_t)(insn->imm & 0xffff); |
| 657 | w[3] = insn->control ? insn->control : 0x000fe200; |
| 658 | break; |
| 659 | |
| 660 | case BW_IR_STS: |
| 661 | /* STS [Ra], Rb */ |
| 662 | w[0] = 0x7388U | ((uint32_t)(src1 & 0xff) << 24); |
| 663 | w[1] = (uint32_t)(src2 & 0xff); |
| 664 | w[2] = (uint32_t)(insn->imm & 0xffff); |
| 665 | w[3] = insn->control ? insn->control : 0x000fe200; |
| 666 | break; |
| 667 | |
| 668 | case BW_IR_EXIT: |
| 669 | w[0] = insn->predicate_p0 ? 0x0000094d : 0x0000794d; |
| 670 | w[1] = 0x00000000; |
| 671 | w[2] = 0x03800000; |
| 672 | w[3] = 0x000fea00; |
| 673 | break; |
| 674 | |
| 675 | case BW_IR_BRA: { |
| 676 | /* sm_121 BRA. Distance is (target - this) - 1, split across the |
| 677 | * low six bits at instruction bits 18..23 and the rest at bit 34. |
| 678 | * Predicate bits 12..15: 7 = always, 0 = @P0, 8 = @!P0. |
| 679 | * A zero delta with no predicate keeps the self-branch word the |
| 680 | * existing kernels already emit after EXIT. */ |
| 681 | int32_t delta = (int32_t)insn->imm; |
| 682 | int32_t rel = delta - 1; |
| 683 | uint32_t pred_field = 0x7u; |
| 684 | if (insn->predicate_p0) |
| 685 | pred_field = insn->predicate_not ? 0x8u : 0x0u; |
| 686 | w[0] = 0x947u | (pred_field << 12) | (((uint32_t)rel & 0x3fu) << 18); |
| 687 | w[1] = (uint32_t)(rel >> 6) << 2; |
| 688 | w[2] = (rel < 0) ? 0x0383ffffu : 0x03800000u; |
| 689 | if (insn->control) |
| 690 | w[3] = insn->control; |
| 691 | else if (delta == 0 && !insn->predicate_p0) |
| 692 | w[3] = 0x000fc000u; |
| 693 | else |
| 694 | w[3] = 0x000fea00u; |
| 695 | break; |
| 696 | } |
| 697 | |
| 698 | case BW_IR_BSSY: { |
| 699 | /* BSSY.RECONVERGENT Bn, target (omega #308). nvcc 13.0.88 -arch=sm_121: |
| 700 | * [0070] BSSY.RECONVERGENT B0, 0x140 ; 0x000000c000007945 0x000fe20003800200 |
| 701 | * opcode 0x945 (NAK sm70_encode.rs:4165), barrier id at bits 16..19 (set_bar_dst |
| 702 | * 16..20), guard predicate at bits 12..15 like BRA, relative byte offset |
| 703 | * target - (this + 16) at bit 32 (NAK: field 34.. in 4-byte units), the cond |
| 704 | * predicate PT at bits 87..89 (0x0380 << 16 in w[2]) and bit 73 (0x200 in w[2]), |
| 705 | * which the disassembler prints as .RECONVERGENT; nvcc sets it on every BSSY/BSYNC |
| 706 | * for sm_121 and Mesa NAK does not. We follow nvcc. The target is the instruction |
| 707 | * after the matching BSYNC, so the delta is at least 2; smaller is refused. */ |
| 708 | int32_t delta = (int32_t)insn->imm; |
| 709 | if (delta < 2 || insn->bar_reg >= BW_RECONV_MAX_BAR) return -1; |
| 710 | uint32_t pred_field = 0x7u; |
| 711 | if (insn->predicate_p0) |
| 712 | pred_field = insn->predicate_not ? 0x8u : 0x0u; |
| 713 | w[0] = 0x945u | (pred_field << 12) | ((uint32_t)insn->bar_reg << 16); |
| 714 | w[1] = (uint32_t)(delta - 1) * 16u; |
| 715 | w[2] = 0x03800200u; |
| 716 | w[3] = insn->control ? insn->control : 0x000fe200u; |
| 717 | break; |
| 718 | } |
| 719 | |
| 720 | case BW_IR_BSYNC: { |
| 721 | /* BSYNC.RECONVERGENT Bn: [0130] BSYNC.RECONVERGENT B0 ; 0x0000000000007941 0x000fea0003800200 |
| 722 | * opcode 0x941 (NAK sm70_encode.rs:4179), barrier id bits 16..19, cond PT, bit 73. */ |
| 723 | if (insn->bar_reg >= BW_RECONV_MAX_BAR) return -1; |
| 724 | uint32_t pred_field = 0x7u; |
| 725 | if (insn->predicate_p0) |
| 726 | pred_field = insn->predicate_not ? 0x8u : 0x0u; |
| 727 | w[0] = 0x941u | (pred_field << 12) | ((uint32_t)insn->bar_reg << 16); |
| 728 | w[1] = 0; |
| 729 | w[2] = 0x03800200u; |
| 730 | w[3] = insn->control ? insn->control : 0x000fea00u; |
| 731 | break; |
| 732 | } |
| 733 | |
| 734 | case BW_IR_MUFU_EX2: |
| 735 | /* MUFU.EX2 Rd, Ra. nvdisasm 13.0 -b SM121: 0x7308 with sub-op 0x0800 decodes |
| 736 | * "MUFU.EX2 R2, R4" (RCP is 0x1000, RSQ 0x1400 above). */ |
| 737 | w[0] = 0x7308U | ((uint32_t)(dst & 0xff) << 16); |
| 738 | w[1] = (uint32_t)(src1 & 0xff); |
| 739 | w[2] = 0x00000800; |
| 740 | w[3] = insn->control ? insn->control : 0x000e2400; |
| 741 | break; |
| 742 | |
| 743 | case BW_IR_BAR_SYNC: |
| 744 | /* BAR.SYNC.DEFER_BLOCKING 0x0: the exact words src/omega_numeric.c:885 runs on |
| 745 | * the chip (nvdisasm: "BAR.SYNC.DEFER_BLOCKING 0x0"). No registers. */ |
| 746 | w[0] = 0x00007b1dU; |
| 747 | w[1] = 0x00000000; |
| 748 | w[2] = 0x00010000; |
| 749 | w[3] = insn->control ? insn->control : 0x000fec00; |
| 750 | break; |
| 751 | |
| 752 | case BW_IR_LDS128: { |
| 753 | /* LDS.128 Rd..Rd+3, [Ra+imm]. nvcc 13.0.88 -arch=sm_121 words (fixtures below): |
| 754 | * LDS.128 R4, [R20] ; 0x0000000014047984 0x000fe80000000c00 |
| 755 | * LDS.128 R8, [R20+0x40] ; 0x0000400014087984 0x000e280000000c00 |
| 756 | * imm = byte offset at w[1] bits 8..31; size field 6 (128) at w[2] bits 9..11. */ |
| 757 | if (insn->imm >= (1u << 23)) return -1; |
| 758 | w[0] = 0x7984U | ((uint32_t)(dst & 0xff) << 16) | ((uint32_t)(src1 & 0xff) << 24); |
| 759 | w[1] = (insn->imm & 0xffffffU) << 8; |
| 760 | w[2] = 0x00000c00U; |
| 761 | w[3] = insn->control ? insn->control : 0x000fe800; |
| 762 | break; |
| 763 | } |
| 764 | |
| 765 | case BW_IR_LDS32: |
| 766 | /* LDS Rd, [Ra+URZ] (32-bit), the src/omega_numeric.c:886 form. */ |
| 767 | w[0] = 0x7984U | ((uint32_t)(dst & 0xff) << 16) | ((uint32_t)(src1 & 0xff) << 24); |
| 768 | w[1] = 0x000000ff; |
| 769 | w[2] = 0x08000800; |
| 770 | w[3] = insn->control ? insn->control : 0x000e2800; |
| 771 | break; |
| 772 | |
| 773 | case BW_IR_STS32: |
| 774 | /* STS [Ra+URZ], Rb (32-bit), the src/omega_numeric.c:884 form. */ |
| 775 | w[0] = 0x7988U | ((uint32_t)(src1 & 0xff) << 24); |
| 776 | w[1] = (uint32_t)(src2 & 0xff); |
| 777 | w[2] = 0x080008ffU; |
| 778 | w[3] = insn->control ? insn->control : 0x000fe200; |
| 779 | break; |
| 780 | |
| 781 | /* Prime race cut (2026-10-05). An absent operand is RZ for these three ops. */ |
| 782 | case BW_IR_LOP3_LUT: { |
| 783 | /* LOP3.LUT Rd, Ra, Rb, Rc, imm8, !PT. LOP3_XOR above is this form with Rc = RZ, LUT 0x3c. */ |
| 784 | uint32_t a = (insn->src1_vreg >= 0) ? (uint32_t)(src1 & 0xff) : 0xffU; |
| 785 | uint32_t b = (insn->src2_vreg >= 0) ? (uint32_t)(src2 & 0xff) : 0xffU; |
| 786 | w[0] = 0x7212U | ((uint32_t)(dst & 0xff) << 16) | (a << 24); |
| 787 | w[1] = b; |
| 788 | w[2] = 0x078e0000U | ((insn->imm & 0xffU) << 8) | (uint32_t)(src3 & 0xff); |
| 789 | w[3] = insn->control ? insn->control : 0x001fca00; |
| 790 | break; |
| 791 | } |
| 792 | |
| 793 | case BW_IR_SHF_L_U32: { |
| 794 | /* SHF.L.U32 Rd, Ra, Rb, Rc: register-amount form (0x7219) of SHF_R's 0x7819, with the |
| 795 | * .R (w[2] bit 12) and .HI (bit 16) flags clear. Rc is the funnel high word (RZ). */ |
| 796 | uint32_t a = (insn->src1_vreg >= 0) ? (uint32_t)(src1 & 0xff) : 0xffU; |
| 797 | uint32_t b = (insn->src2_vreg >= 0) ? (uint32_t)(src2 & 0xff) : 0xffU; |
| 798 | w[0] = 0x7219U | ((uint32_t)(dst & 0xff) << 16) | (a << 24); |
| 799 | w[1] = b; |
| 800 | w[2] = 0x00000600U | (uint32_t)(src3 & 0xff); |
| 801 | w[3] = insn->control ? insn->control : 0x001fca00; |
| 802 | break; |
| 803 | } |
| 804 | |
| 805 | case BW_IR_IMAD_HI_U32: { |
| 806 | /* IMAD.HI.U32 Rd, Ra, Rb, Rc: opcode 0x227 with the IMAD.WIDE.U32 modifier word. */ |
| 807 | uint32_t a = (insn->src1_vreg >= 0) ? (uint32_t)(src1 & 0xff) : 0xffU; |
| 808 | uint32_t b = (insn->src2_vreg >= 0) ? (uint32_t)(src2 & 0xff) : 0xffU; |
| 809 | w[0] = 0x7227U | ((uint32_t)(dst & 0xff) << 16) | (a << 24); |
| 810 | w[1] = b; |
| 811 | w[2] = 0x078e0000U | (uint32_t)(src3 & 0xff); |
| 812 | w[3] = insn->control ? insn->control : 0x001fca00; |
| 813 | break; |
| 814 | } |
| 815 | |
| 816 | default: |
| 817 | return -1; |
| 818 | } |
| 819 | return 0; |
| 820 | } |
| 821 | |
| 822 | /* Prime race cut (2026-10-05): golden words for LOP3_LUT, SHF_L_U32 and IMAD_HI_U32. |
| 823 | * Each expected (w0, w1, w2) is the low 96 bits of an instruction nvcc 13.0.88 emitted |
| 824 | * for sm_121 (nvcc -arch=sm_121 -cubin, cuobjdump -sass; source and dump in |
| 825 | * bench/prime_race/tests/encoding_oracle.md). Control words (w3) are scheduler output and |
| 826 | * not compared. nvcc is an offline oracle only; nothing here links CUDA. 0 = all good. */ |
| 827 | int omega_blackwell_verify_codegen_fixtures_intops(void) { |
| 828 | OmegaRegAlloc ra; |
| 829 | memset(&ra, 0, sizeof(ra)); |
| 830 | static const int phys[] = { 0, 2, 6, 7, 9, 11, 13 }; |
| 831 | for (int i = 0; i < (int)(sizeof phys / sizeof phys[0]); i++) ra.vreg_to_phys[i] = phys[i]; |
| 832 | enum { R0 = 0, R2, R6, R7, R9, R11, R13, RZ = -1 }; |
| 833 | static const struct { |
| 834 | BlackwellIROpcode op; int d, a, b, c; uint32_t imm; uint32_t w0, w1, w2; const char *text; |
| 835 | } fx[] = { |
| 836 | { BW_IR_LOP3_LUT, R9, R7, R0, RZ, 0xfc, 0x07097212U, 0x00000000U, 0x078efcffU, "LOP3.LUT R9, R7, R0, RZ, 0xfc, !PT" }, |
| 837 | { BW_IR_LOP3_LUT, R11, R6, R7, R0, 0x78, 0x060b7212U, 0x00000007U, 0x078e7800U, "LOP3.LUT R11, R6, R7, R0, 0x78, !PT" }, |
| 838 | { BW_IR_LOP3_LUT, R13, R6, R7, R0, 0x10, 0x060d7212U, 0x00000007U, 0x078e1000U, "LOP3.LUT R13, R6, R7, R0, 0x10, !PT" }, |
| 839 | { BW_IR_LOP3_LUT, R7, RZ, R9, RZ, 0x33, 0xff077212U, 0x00000009U, 0x078e33ffU, "LOP3.LUT R7, RZ, R9, RZ, 0x33, !PT" }, |
| 840 | { BW_IR_SHF_L_U32, R7, R9, R2, RZ, 0, 0x09077219U, 0x00000002U, 0x000006ffU, "SHF.L.U32 R7, R9, R2, RZ" }, |
| 841 | { BW_IR_SHF_L_U32, R7, R0, R7, RZ, 0, 0x00077219U, 0x00000007U, 0x000006ffU, "SHF.L.U32 R7, R0, R7, RZ" }, |
| 842 | { BW_IR_IMAD_HI_U32, R9, R9, R0, R6, 0, 0x09097227U, 0x00000000U, 0x078e0006U, "IMAD.HI.U32 R9, R9, R0, R6" }, |
| 843 | { BW_IR_IMAD_HI_U32, R7, R0, R7, RZ, 0, 0x00077227U, 0x00000007U, 0x078e00ffU, "IMAD.HI.U32 R7, R0, R7, RZ" }, |
| 844 | }; |
| 845 | for (int i = 0; i < (int)(sizeof fx / sizeof fx[0]); i++) { |
| 846 | BlackwellIRInsn in; |
| 847 | memset(&in, 0, sizeof in); |
| 848 | in.op = fx[i].op; in.dst_vreg = fx[i].d; in.src1_vreg = fx[i].a; in.src2_vreg = fx[i].b; |
| 849 | in.src3_vreg = fx[i].c; in.ureg = -1; in.imm = fx[i].imm; |
| 850 | uint32_t w[4]; |
| 851 | if (encode_single_insn(&in, &ra, w) != 0) return -(2 * i + 1); |
| 852 | if (w[0] != fx[i].w0 || w[1] != fx[i].w1 || w[2] != fx[i].w2) return -(2 * i + 2); |
| 853 | } |
| 854 | return 0; |
| 855 | } |
| 856 | |
| 857 | int omega_blackwell_encode_one(const BlackwellIRInsn *insn, uint32_t w[4]) { |
| 858 | if (!insn || !w) return -1; |
| 859 | OmegaRegAlloc ra; |
| 860 | memset(&ra, 0, sizeof ra); |
| 861 | for (int i = 0; i < BW_MAX_VREGS; i++) ra.vreg_to_phys[i] = i; |
| 862 | for (int i = 0; i < BW_MAX_UVREGS; i++) ra.uvreg_to_phys[i] = i; |
| 863 | return encode_single_insn(insn, &ra, w); |
| 864 | } |
| 865 | |
| 866 | /* omega #328: golden words for the fragment-major matmul. Oracle: nvcc 13.0.88 -arch=sm_121 |
| 867 | * -cubin -O3, cuobjdump -sass, on a kernel with uint2 / uint4 loads at immediate offsets and a |
| 868 | * dependent mma.m16n8k16 bf16 chain (source and dump sealed in |
| 869 | * ~/workspace/investigations/2026-10-07-qwen3-decode-matmul/oracle/frag_plain.{cu,sass}): |
| 870 | * LDG.E.64 R2, desc[UR8][R30.64] ; 0x000000081e027981 0x000ea8000c1e1b00 |
| 871 | * LDG.E.64 R28, desc[UR8][R30.64+0x100] ; 0x000100081e1c7981 0x000ee8000c1e1b00 |
| 872 | * LDG.E.128 R8, desc[UR8][R32.64] ; 0x0000000820087981 0x000ea8000c1e1d00 |
| 873 | * LDG.E.128 R20, desc[UR8][R32.64+0x1000] ; 0x0010000820147981 0x000e9e000c1e1d00 |
| 874 | * HMMA.16816.F32.BF16 R8, R12, R6, R8 ; 0x000000060c08723c 0x010fe20000041808 |
| 875 | * NOP ; 0x0000000000007918 0x000fdc0000000000 |
| 876 | * The dependent HMMA chain nvcc emits is HMMA (stall 15) then NOP (stall 14): 29 cycles from |
| 877 | * one HMMA to the next that reads its accumulator. w3 is compared for the NOP only (it is the |
| 878 | * whole point of that instruction). 0 = all good. */ |
| 879 | int omega_blackwell_verify_codegen_fixtures_fragmm(void) { |
| 880 | static const struct { |
| 881 | BlackwellIROpcode op; int d, a, b, c, u; uint32_t imm, control; uint32_t w0, w1, w2, w3; const char *text; |
| 882 | } fx[] = { |
| 883 | { BW_IR_LDG_E_64, 2, 30, -1, -1, 8, 0, 0, 0x1e027981U, 0x00000008U, 0x0c1e1b00U, 0, "LDG.E.64 R2, desc[UR8][R30.64]" }, |
| 884 | { 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]" }, |
| 885 | { BW_IR_LDG_E_128, 8, 32, -1, -1, 8, 0, 0, 0x20087981U, 0x00000008U, 0x0c1e1d00U, 0, "LDG.E.128 R8, desc[UR8][R32.64]" }, |
| 886 | { 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]" }, |
| 887 | { BW_IR_HMMA_BF16, 8, 12, 6, 8, -1, 0, 0, 0x0c08723cU, 0x00000006U, 0x00041808U, 0, "HMMA.16816.F32.BF16 R8, R12, R6, R8" }, |
| 888 | { BW_IR_NOP, -1, -1, -1, -1, -1, 0, 0x000fdc00U, 0x00007918U, 0, 0, 0x000fdc00U, "NOP (stall 14)" }, |
| 889 | /* LDS.128 with an immediate offset (attention batched loads, 2026-10-12): nvcc 13.0.88 -arch=sm_121 on |
| 890 | * float4 shared reads at register + constant offsets (fixture source in the omega speed/attention-batched-loads PR) */ |
| 891 | { BW_IR_LDS128, 4, 20, -1, -1, -1, 0, 0, 0x14047984U, 0x00000000U, 0x00000c00U, 0, "LDS.128 R4, [R20]" }, |
| 892 | { BW_IR_LDS128, 8, 20, -1, -1, -1, 0x40, 0, 0x14087984U, 0x00004000U, 0x00000c00U, 0, "LDS.128 R8, [R20+0x40]" }, |
| 893 | { BW_IR_LDS128, 12, 20, -1, -1, -1, 0x100, 0, 0x140c7984U, 0x00010000U, 0x00000c00U, 0, "LDS.128 R12, [R20+0x100]" }, |
| 894 | /* STG.E with an immediate offset (blocked prefill matmul, 2026-10-12): nvcc 13.0.88 -arch=sm_121 on float stores |
| 895 | * at register + constant offsets (~/.claude/jobs/ade3c837/tmp/stg-oracle/stg.cu); the second word is the |
| 896 | * offset-free STG word with UR4 */ |
| 897 | { BW_IR_STG_E, -1, 4, 8, -1, 4, 0, 0, 0x04007986U, 0x00000008U, 0x0c101904U, 0, "STG.E desc[UR4][R4.64], R8" }, |
| 898 | { BW_IR_STG_E, -1, 4, 9, -1, 4, 0x4, 0, 0x04007986U, 0x00000409U, 0x0c101904U, 0, "STG.E desc[UR4][R4.64+0x4], R9" }, |
| 899 | { BW_IR_STG_E, -1, 4, 10, -1, 4, 0x4000, 0, 0x04007986U, 0x0040000aU, 0x0c101904U, 0, "STG.E desc[UR4][R4.64+0x4000], R10" }, |
| 900 | { BW_IR_STG_E, -1, 6, 11, -1, 4, 0xfff00000u, 0, 0x06007986U, 0xf000000bU, 0x0c101904U, 0, "STG.E desc[UR4][R6.64+-0x100000], R11" }, |
| 901 | }; |
| 902 | for (int i = 0; i < (int)(sizeof fx / sizeof fx[0]); i++) { |
| 903 | BlackwellIRInsn in; |
| 904 | memset(&in, 0, sizeof in); |
| 905 | in.op = fx[i].op; in.dst_vreg = fx[i].d; in.src1_vreg = fx[i].a; in.src2_vreg = fx[i].b; |
| 906 | in.src3_vreg = fx[i].c; in.ureg = fx[i].u; in.imm = fx[i].imm; in.control = fx[i].control; |
| 907 | uint32_t w[4]; |
| 908 | if (omega_blackwell_encode_one(&in, w) != 0) return -(2 * i + 1); |
| 909 | if (w[0] != fx[i].w0 || w[1] != fx[i].w1 || w[2] != fx[i].w2 || (fx[i].w3 && w[3] != fx[i].w3)) return -(2 * i + 2); |
| 910 | } |
| 911 | /* offsets outside the signed 24-bit field are refused, not wrapped */ |
| 912 | BlackwellIRInsn big; |
| 913 | memset(&big, 0, sizeof big); |
| 914 | 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; |
| 915 | big.imm = 1u << 23; |
| 916 | uint32_t w[4]; |
| 917 | if (omega_blackwell_encode_one(&big, w) == 0) return -100; |
| 918 | big.imm = (uint32_t)-(1 << 23); |
| 919 | if (omega_blackwell_encode_one(&big, w) != 0) return -101; |
| 920 | return 0; |
| 921 | } |
| 922 | |
| 923 | /* omega #308: BSSY / BSYNC golden words. Oracle: nvcc 13.0.88 -arch=sm_121 -cubin -O3 on the |
| 924 | * kernels in docs/gpu-reconvergence-308.md, cuobjdump -sass (low 64-bit word, then high; the |
| 925 | * top 23 bits of the high word are the control word and are not compared here): |
| 926 | * k_loop [0070] BSSY.RECONVERGENT B0, 0x140 ; 0x000000c000007945 0x000fe20003800200 (delta 13) |
| 927 | * k_nested [0070] BSSY.RECONVERGENT B0, 0x300 ; 0x0000028000007945 0x000fe20003800200 (delta 41) |
| 928 | * k_break [0070] BSSY.RECONVERGENT B0, 0x110 ; 0x0000009000007945 0x000fe20003800200 (delta 10) |
| 929 | * k_loop [0130] BSYNC.RECONVERGENT B0 ; 0x0000000000007941 0x000fea0003800200 |
| 930 | * Barrier ids other than B0 and the predicated forms are decoded by tools/reconv_nvdisasm_check.sh |
| 931 | * (nvdisasm -b SM121 text), not by this table. */ |
| 932 | int omega_blackwell_verify_codegen_fixtures_reconv(void) { |
| 933 | static const struct { BlackwellIROpcode op; int delta; int bar; uint32_t w0, w1, w2; } fx[] = { |
| 934 | { BW_IR_BSSY, 13, 0, 0x00007945U, 0x000000c0U, 0x03800200U }, |
| 935 | { BW_IR_BSSY, 41, 0, 0x00007945U, 0x00000280U, 0x03800200U }, |
| 936 | { BW_IR_BSSY, 10, 0, 0x00007945U, 0x00000090U, 0x03800200U }, |
| 937 | { BW_IR_BSYNC, 0, 0, 0x00007941U, 0x00000000U, 0x03800200U }, |
| 938 | }; |
| 939 | for (int i = 0; i < (int)(sizeof fx / sizeof fx[0]); i++) { |
| 940 | BlackwellIRInsn in; |
| 941 | memset(&in, 0, sizeof in); |
| 942 | in.op = fx[i].op; in.dst_vreg = -1; in.src1_vreg = -1; in.src2_vreg = -1; in.src3_vreg = -1; in.ureg = -1; |
| 943 | in.imm = (uint32_t)fx[i].delta; in.bar_reg = (uint8_t)fx[i].bar; |
| 944 | uint32_t w[4]; |
| 945 | if (omega_blackwell_encode_one(&in, w) != 0) return -(2 * i + 1); |
| 946 | if (w[0] != fx[i].w0 || w[1] != fx[i].w1 || w[2] != fx[i].w2) return -(2 * i + 2); |
| 947 | /* control word defaults mirror nvcc's: BSSY 0x000fe200, BSYNC 0x000fea00 */ |
| 948 | if (w[3] != (fx[i].op == BW_IR_BSSY ? 0x000fe200U : 0x000fea00U)) return -(2 * i + 2); |
| 949 | } |
| 950 | /* refusals: a BSSY must name a later instruction (delta >= 2), barrier ids stop at B15 */ |
| 951 | BlackwellIRInsn bad; uint32_t w[4]; |
| 952 | 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; |
| 953 | bad.imm = 1; bad.bar_reg = 0; |
| 954 | if (omega_blackwell_encode_one(&bad, w) == 0) return -101; |
| 955 | bad.imm = (uint32_t)-3; |
| 956 | if (omega_blackwell_encode_one(&bad, w) == 0) return -102; |
| 957 | bad.imm = 5; bad.bar_reg = 16; |
| 958 | if (omega_blackwell_encode_one(&bad, w) == 0) return -103; |
| 959 | bad.op = BW_IR_BSYNC; bad.imm = 0; bad.bar_reg = 16; |
| 960 | if (omega_blackwell_encode_one(&bad, w) == 0) return -104; |
| 961 | bad.bar_reg = 15; |
| 962 | if (omega_blackwell_encode_one(&bad, w) != 0 || w[0] != (0x00007941U | (15U << 16))) return -105; |
| 963 | return 0; |
| 964 | } |
| 965 | |
| 966 | /* FB-1 cut 4: word-level fixtures for the four additive ops (checked against the |
| 967 | * nvdisasm decodes recorded in docs/numeric/FB1_CUT4_ELEMENTWISE.md). 0 = all good. */ |
| 968 | int omega_blackwell_verify_codegen_fixtures_fb1cut4(void) { |
| 969 | OmegaRegAlloc ra; |
| 970 | memset(&ra, 0, sizeof(ra)); |
| 971 | ra.vreg_to_phys[0] = 2; /* R2 */ |
| 972 | ra.vreg_to_phys[1] = 4; /* R4 */ |
| 973 | ra.vreg_to_phys[2] = 9; /* R9 */ |
| 974 | ra.vreg_to_phys[3] = 10; /* R10 */ |
| 975 | ra.vreg_to_phys[4] = 8; /* R8 */ |
| 976 | uint32_t w[4]; |
| 977 | |
| 978 | /* MUFU.EX2 R2, R4 */ |
| 979 | BlackwellIRInsn ex2 = { .op = BW_IR_MUFU_EX2, .dst_vreg = 0, .src1_vreg = 1 }; |
| 980 | if (encode_single_insn(&ex2, &ra, w) != 0) return -1; |
| 981 | if (w[0] != 0x00027308U || w[1] != 4 || w[2] != 0x00000800U) return -2; |
| 982 | /* BAR.SYNC.DEFER_BLOCKING 0x0 */ |
| 983 | BlackwellIRInsn bar = { .op = BW_IR_BAR_SYNC }; |
| 984 | if (encode_single_insn(&bar, &ra, w) != 0) return -3; |
| 985 | if (w[0] != 0x00007b1dU || w[1] != 0 || w[2] != 0x00010000U || w[3] != 0x000fec00U) return -4; |
| 986 | /* LDS R9, [R10+URZ] */ |
| 987 | BlackwellIRInsn lds = { .op = BW_IR_LDS32, .dst_vreg = 2, .src1_vreg = 3 }; |
| 988 | if (encode_single_insn(&lds, &ra, w) != 0) return -5; |
| 989 | if (w[0] != 0x0a097984U || w[1] != 0xff || w[2] != 0x08000800U) return -6; |
| 990 | /* STS [R8+URZ], R2 */ |
| 991 | BlackwellIRInsn sts = { .op = BW_IR_STS32, .src1_vreg = 4, .src2_vreg = 0 }; |
| 992 | if (encode_single_insn(&sts, &ra, w) != 0) return -7; |
| 993 | if (w[0] != 0x08007988U || w[1] != 2 || w[2] != 0x080008ffU) return -8; |
| 994 | return 0; |
| 995 | } |
| 996 | |
| 997 | int omega_bw_encode_program(const BlackwellIRProgram *prog, uint8_t *code_buf, size_t max_len, size_t *out_len) { |
| 998 | if (!prog || !code_buf || !out_len) return -1; |
| 999 | |
| 1000 | size_t padded_insns = (prog->count + 7) & ~7ULL; |
| 1001 | if (padded_insns < 32) padded_insns = 32; |
| 1002 | |
| 1003 | size_t req_bytes = padded_insns * 16; |
| 1004 | if (max_len < req_bytes) return -1; |
| 1005 | |
| 1006 | uint8_t *p = code_buf; |
| 1007 | for (size_t i = 0; i < padded_insns; i++) { |
| 1008 | uint32_t w[4]; |
| 1009 | if (i < prog->count) { |
| 1010 | if (encode_single_insn(&prog->insns[i], &prog->regalloc, w) != 0) { |
| 1011 | return -1; |
| 1012 | } |
| 1013 | } else { |
| 1014 | /* Pad with NOP */ |
| 1015 | w[0] = 0x00007918; |
| 1016 | w[1] = 0x00000000; |
| 1017 | w[2] = 0x00000000; |
| 1018 | w[3] = 0x000fc000; |
| 1019 | } |
| 1020 | |
| 1021 | p[0] = (uint8_t)(w[0] & 0xff); |
| 1022 | p[1] = (uint8_t)((w[0] >> 8) & 0xff); |
| 1023 | p[2] = (uint8_t)((w[0] >> 16) & 0xff); |
| 1024 | p[3] = (uint8_t)((w[0] >> 24) & 0xff); |
| 1025 | |
| 1026 | p[4] = (uint8_t)(w[1] & 0xff); |
| 1027 | p[5] = (uint8_t)((w[1] >> 8) & 0xff); |
| 1028 | p[6] = (uint8_t)((w[1] >> 16) & 0xff); |
| 1029 | p[7] = (uint8_t)((w[1] >> 24) & 0xff); |
| 1030 | |
| 1031 | p[8] = (uint8_t)(w[2] & 0xff); |
| 1032 | p[9] = (uint8_t)((w[2] >> 8) & 0xff); |
| 1033 | p[10] = (uint8_t)((w[2] >> 16) & 0xff); |
| 1034 | p[11] = (uint8_t)((w[2] >> 24) & 0xff); |
| 1035 | |
| 1036 | p[12] = (uint8_t)(w[3] & 0xff); |
| 1037 | p[13] = (uint8_t)((w[3] >> 8) & 0xff); |
| 1038 | p[14] = (uint8_t)((w[3] >> 16) & 0xff); |
| 1039 | p[15] = (uint8_t)((w[3] >> 24) & 0xff); |
| 1040 | |
| 1041 | p += 16; |
| 1042 | } |
| 1043 | |
| 1044 | *out_len = req_bytes; |
| 1045 | return 0; |
| 1046 | } |
| 1047 | |
| 1048 | int omega_blackwell_codegen_matmul_i32_prog(const OmegaMatMulSpec *spec, BlackwellIRProgram *prog) { |
| 1049 | if (!spec || !prog) return -1; |
| 1050 | if (spec->m == 0 || spec->k == 0 || spec->n == 0) return -1; |
| 1051 | |
| 1052 | /* Fail closed loudly if K exceeds bounded unroll limit */ |
| 1053 | if (spec->k > 32) { |
| 1054 | BW_DIAG("ERROR: MatMul integer codegen exceeds bounded unroll limit (K=%u > 32)\n", spec->k); |
| 1055 | return -1; |
| 1056 | } |
| 1057 | |
| 1058 | omega_bw_ir_init(prog); |
| 1059 | |
| 1060 | /* 1. Allocate virtual registers */ |
| 1061 | /* 64-bit base pointers */ |
| 1062 | int v_ptr_a = omega_bw_ir_alloc_vreg64(prog); |
| 1063 | int v_ptr_b = omega_bw_ir_alloc_vreg64(prog); |
| 1064 | int v_ptr_c = omega_bw_ir_alloc_vreg64(prog); |
| 1065 | int uv_desc = omega_bw_ir_alloc_uvreg64(prog); |
| 1066 | |
| 1067 | /* 32-bit CTA / Thread indexing */ |
| 1068 | int v_ctaid_x = omega_bw_ir_alloc_vreg(prog); |
| 1069 | int v_ctaid_y = omega_bw_ir_alloc_vreg(prog); |
| 1070 | int v_tid_x = omega_bw_ir_alloc_vreg(prog); |
| 1071 | int v_tid_y = omega_bw_ir_alloc_vreg(prog); |
| 1072 | |
| 1073 | /* 2D Coordinates & loop invariants */ |
| 1074 | int v_col = omega_bw_ir_alloc_vreg(prog); |
| 1075 | int v_row = omega_bw_ir_alloc_vreg(prog); |
| 1076 | int v_row_k = omega_bw_ir_alloc_vreg(prog); |
| 1077 | int v_const_1 = omega_bw_ir_alloc_vreg(prog); |
| 1078 | int v_acc = omega_bw_ir_alloc_vreg(prog); |
| 1079 | |
| 1080 | /* Working registers for memory address and operands */ |
| 1081 | int v_idx_a = omega_bw_ir_alloc_vreg(prog); |
| 1082 | int v_off_a = omega_bw_ir_alloc_vreg64(prog); |
| 1083 | int v_val_a = omega_bw_ir_alloc_vreg(prog); |
| 1084 | |
| 1085 | int v_idx_b = omega_bw_ir_alloc_vreg(prog); |
| 1086 | int v_off_b = omega_bw_ir_alloc_vreg64(prog); |
| 1087 | int v_val_b = omega_bw_ir_alloc_vreg(prog); |
| 1088 | |
| 1089 | int v_idx_c = omega_bw_ir_alloc_vreg(prog); |
| 1090 | int v_off_c = omega_bw_ir_alloc_vreg64(prog); |
| 1091 | |
| 1092 | if (v_off_c < 0) return -1; |
| 1093 | |
| 1094 | /* 2. Load 64-bit global memory descriptor into uniform register pair */ |
| 1095 | /* LDCU.64 uv_desc, c[0x0][0x358] */ |
| 1096 | BlackwellIRInsn insn_desc = { .op = BW_IR_LDCU64, .dst_vreg = uv_desc, .imm = 0x358, .is_uniform = true }; |
| 1097 | omega_bw_ir_append(prog, &insn_desc); |
| 1098 | |
| 1099 | /* 3. Load 64-bit base pointers from Constant Bank 0 */ |
| 1100 | /* LDC.64 v_ptr_a, c[0x0][0x380] */ |
| 1101 | BlackwellIRInsn insn_ptra = { .op = BW_IR_LDC64, .dst_vreg = v_ptr_a, .imm = 0x380 }; |
| 1102 | omega_bw_ir_append(prog, &insn_ptra); |
| 1103 | |
| 1104 | /* LDC.64 v_ptr_b, c[0x0][0x388] */ |
| 1105 | BlackwellIRInsn insn_ptrb = { .op = BW_IR_LDC64, .dst_vreg = v_ptr_b, .imm = 0x388 }; |
| 1106 | omega_bw_ir_append(prog, &insn_ptrb); |
| 1107 | |
| 1108 | /* LDC.64 v_ptr_c, c[0x0][0x390] */ |
| 1109 | BlackwellIRInsn insn_ptrc = { .op = BW_IR_LDC64, .dst_vreg = v_ptr_c, .imm = 0x390 }; |
| 1110 | omega_bw_ir_append(prog, &insn_ptrc); |
| 1111 | |
| 1112 | /* 4. Query 2D CTA and Thread coordinates */ |
| 1113 | /* S2R v_ctaid_x, SR_CTAID.X */ |
| 1114 | BlackwellIRInsn insn_ctax = { .op = BW_IR_S2R, .dst_vreg = v_ctaid_x, .imm = BW_SR_CTAID_X }; |
| 1115 | omega_bw_ir_append(prog, &insn_ctax); |
| 1116 | |
| 1117 | /* S2R v_ctaid_y, SR_CTAID.Y */ |
| 1118 | BlackwellIRInsn insn_ctay = { .op = BW_IR_S2R, .dst_vreg = v_ctaid_y, .imm = BW_SR_CTAID_Y }; |
| 1119 | omega_bw_ir_append(prog, &insn_ctay); |
| 1120 | |
| 1121 | /* S2R v_tid_x, SR_TID.X */ |
| 1122 | BlackwellIRInsn insn_tidx = { .op = BW_IR_S2R, .dst_vreg = v_tid_x, .imm = BW_SR_TID_X }; |
| 1123 | omega_bw_ir_append(prog, &insn_tidx); |
| 1124 | |
| 1125 | /* S2R v_tid_y, SR_TID.Y */ |
| 1126 | BlackwellIRInsn insn_tidy = { .op = BW_IR_S2R, .dst_vreg = v_tid_y, .imm = BW_SR_TID_Y }; |
| 1127 | omega_bw_ir_append(prog, &insn_tidy); |
| 1128 | |
| 1129 | /* 5. Compute 2D (row, col) coordinates */ |
| 1130 | /* Standard 16x16 tile threads: col = ctaid_x * 16 + tid_x */ |
| 1131 | 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 }; |
| 1132 | omega_bw_ir_append(prog, &insn_col); |
| 1133 | |
| 1134 | /* row = ctaid_y * 16 + tid_y */ |
| 1135 | 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 }; |
| 1136 | omega_bw_ir_append(prog, &insn_row); |
| 1137 | |
| 1138 | /* 6. Precompute row base index: v_row_k = v_row * K + RZ */ |
| 1139 | 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 }; |
| 1140 | omega_bw_ir_append(prog, &insn_row_k); |
| 1141 | |
| 1142 | /* 7. Initialize v_const_1 = 1 for immediate linear indexing */ |
| 1143 | BlackwellIRInsn insn_c1 = { .op = BW_IR_MOV_IMM, .dst_vreg = v_const_1, .imm = 1 }; |
| 1144 | omega_bw_ir_append(prog, &insn_c1); |
| 1145 | |
| 1146 | /* 8. Initialize accumulator to zero: MOV v_acc, RZ */ |
| 1147 | BlackwellIRInsn insn_init_acc = { .op = BW_IR_MOV_RZ, .dst_vreg = v_acc }; |
| 1148 | omega_bw_ir_append(prog, &insn_init_acc); |
| 1149 | |
| 1150 | /* 9. Synthesize inner accumulation loop across K iterations */ |
| 1151 | for (uint32_t step = 0; step < spec->k; step++) { |
| 1152 | /* A[row, step]: idx_a = v_row_k + step = v_const_1 * step + v_row_k */ |
| 1153 | 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 }; |
| 1154 | omega_bw_ir_append(prog, &insn_idxa); |
| 1155 | |
| 1156 | /* off_a = v_ptr_a + idx_a * 4 */ |
| 1157 | 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 }; |
| 1158 | omega_bw_ir_append(prog, &insn_offa); |
| 1159 | |
| 1160 | /* Load A[row, step]: LDG.E v_val_a, desc[uv_desc][v_off_a.64] */ |
| 1161 | 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 }; |
| 1162 | omega_bw_ir_append(prog, &insn_lda); |
| 1163 | |
| 1164 | /* B[step, col]: idx_b = step * N + col = v_const_1 * (step * N) + v_col */ |
| 1165 | uint32_t step_n = step * spec->n; |
| 1166 | 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 }; |
| 1167 | omega_bw_ir_append(prog, &insn_idxb); |
| 1168 | |
| 1169 | /* off_b = v_ptr_b + idx_b * 4 */ |
| 1170 | 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 }; |
| 1171 | omega_bw_ir_append(prog, &insn_offb); |
| 1172 | |
| 1173 | /* Load B[step, col]: LDG.E v_val_b, desc[uv_desc][v_off_b.64] */ |
| 1174 | 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 }; |
| 1175 | omega_bw_ir_append(prog, &insn_ldb); |
| 1176 | |
| 1177 | /* Multiply and accumulate: IMAD v_acc, v_val_a, v_val_b, v_acc */ |
| 1178 | 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 }; |
| 1179 | omega_bw_ir_append(prog, &insn_acc); |
| 1180 | } |
| 1181 | |
| 1182 | /* 10. Compute output index: idx_c = row * N + col */ |
| 1183 | 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 }; |
| 1184 | omega_bw_ir_append(prog, &insn_idxc); |
| 1185 | |
| 1186 | /* off_c = v_ptr_c + idx_c * 4 */ |
| 1187 | 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 }; |
| 1188 | omega_bw_ir_append(prog, &insn_offc); |
| 1189 | |
| 1190 | /* Store C[row, col]: STG.E desc[uv_desc][v_off_c.64], v_acc */ |
| 1191 | BlackwellIRInsn insn_stc = { .op = BW_IR_STG_E, .src1_vreg = v_off_c, .src2_vreg = v_acc, .src3_vreg = -1, .ureg = uv_desc }; |
| 1192 | omega_bw_ir_append(prog, &insn_stc); |
| 1193 | |
| 1194 | /* 11. Kernel termination */ |
| 1195 | BlackwellIRInsn insn_exit = { .op = BW_IR_EXIT, .predicate_p0 = false }; |
| 1196 | omega_bw_ir_append(prog, &insn_exit); |
| 1197 | |
| 1198 | BlackwellIRInsn insn_bra = { .op = BW_IR_BRA }; |
| 1199 | omega_bw_ir_append(prog, &insn_bra); |
| 1200 | |
| 1201 | /* 12. Solve bounded deterministic register allocation */ |
| 1202 | if (omega_bw_regalloc_solve(prog) != 0) { |
| 1203 | return -1; |
| 1204 | } |
| 1205 | return 0; |
| 1206 | } |
| 1207 | |
| 1208 | /* ---- native v1 P07 cut A.2: allocation-free kernel encoding --------------------------------- |
| 1209 | * The one place a solved IR program becomes a kernel. Hosted and freestanding builds both encode |
| 1210 | * through it, so the two produce the same bytes for the same program (make test-codegen-freestanding |
| 1211 | * compares them). */ |
| 1212 | size_t omega_bw_program_code_bytes(const BlackwellIRProgram *prog) { |
| 1213 | if (!prog) return 0; |
| 1214 | size_t padded_insns = (prog->count + 7) & ~(size_t)7; |
| 1215 | if (padded_insns < 32) padded_insns = 32; |
| 1216 | return padded_insns * 16; |
| 1217 | } |
| 1218 | |
| 1219 | int omega_blackwell_kernel_encode_into(const BlackwellIRProgram *prog, uint8_t *code_buf, size_t code_cap, |
| 1220 | OmegaBlackwellKernel *kernel) { |
| 1221 | if (!kernel) return -1; |
| 1222 | memset(kernel, 0, sizeof *kernel); |
| 1223 | if (!prog || !code_buf) return -1; |
| 1224 | size_t need = omega_bw_program_code_bytes(prog); |
| 1225 | if (code_cap < need) return OMEGA_BW_CODE_BUFFER_TOO_SMALL; /* refused before any byte is written */ |
| 1226 | size_t emitted_size = 0; |
| 1227 | if (omega_bw_encode_program(prog, code_buf, code_cap, &emitted_size) != 0) return -1; |
| 1228 | kernel->code = code_buf; |
| 1229 | kernel->code_size = emitted_size; |
| 1230 | kernel->insn_count = emitted_size / 16; |
| 1231 | kernel->gpr_count = prog->regalloc.peak_gpr_usage; |
| 1232 | kernel->uniform_gpr_count = prog->regalloc.peak_ugpr_usage; |
| 1233 | sha256_hash(kernel->code, kernel->code_size, kernel->code_digest); |
| 1234 | return 0; |
| 1235 | } |
| 1236 | |
| 1237 | #ifndef OMEGA_GPU_NO_POSIX |
| 1238 | /* Hosted only: the allocating generators keep their names, caps and behavior. Each builds the program, |
| 1239 | * mallocs max_bytes (the cap each generator always had) and encodes through omega_blackwell_kernel_encode_into; |
| 1240 | * omega_blackwell_kernel_free releases the buffer. */ |
| 1241 | static int bw_kernel_alloc_encode(const BlackwellIRProgram *prog, size_t max_bytes, OmegaBlackwellKernel *kernel) { |
| 1242 | uint8_t *buf = (uint8_t *)malloc(max_bytes); |
| 1243 | if (!buf) { kernel->code = NULL; return -1; } |
| 1244 | if (omega_blackwell_kernel_encode_into(prog, buf, max_bytes, kernel) != 0) { |
| 1245 | free(buf); |
| 1246 | kernel->code = NULL; |
| 1247 | return -1; |
| 1248 | } |
| 1249 | return 0; |
| 1250 | } |
| 1251 | |
| 1252 | int omega_blackwell_codegen_matmul_i32(const OmegaMatMulSpec *spec, OmegaBlackwellKernel *kernel) { |
| 1253 | if (!spec || !kernel) return -1; |
| 1254 | BlackwellIRProgram prog; |
| 1255 | if (omega_blackwell_codegen_matmul_i32_prog(spec, &prog) != 0) return -1; |
| 1256 | /* 13. the code buffer is exactly the padded program (the i32 generator never had a fixed cap) */ |
| 1257 | return bw_kernel_alloc_encode(&prog, omega_bw_program_code_bytes(&prog), kernel); |
| 1258 | } |
| 1259 | #endif |
| 1260 | |
| 1261 | int omega_blackwell_codegen_matmul_tensor_prog(const OmegaMatMulSpec *spec, BlackwellIRProgram *prog) { |
| 1262 | if (!spec || !prog) return -1; |
| 1263 | if (spec->m == 0 || spec->k == 0 || spec->n == 0) return -1; |
| 1264 | if (spec->precision != OMEGA_MATMUL_PRECISION_FP16 && spec->precision != OMEGA_MATMUL_PRECISION_BF16) return -1; |
| 1265 | if (spec->m % 16 != 0 || spec->n % 8 != 0 || spec->k != 16) { |
| 1266 | BW_DIAG("ERROR: MatMul tensor codegen requires M multiple of 16, N multiple of 8, K=16 (got %ux%ux%u)\n", |
| 1267 | spec->m, spec->k, spec->n); |
| 1268 | return -1; |
| 1269 | } |
| 1270 | |
| 1271 | omega_bw_ir_init(prog); |
| 1272 | |
| 1273 | /* 1. Allocate virtual registers */ |
| 1274 | /* 64-bit base pointers */ |
| 1275 | int v_ptr_a = omega_bw_ir_alloc_vreg64(prog); |
| 1276 | int v_ptr_b = omega_bw_ir_alloc_vreg64(prog); |
| 1277 | int v_ptr_c = omega_bw_ir_alloc_vreg64(prog); |
| 1278 | int uv_desc = omega_bw_ir_alloc_uvreg64(prog); |
| 1279 | |
| 1280 | /* 32-bit CTA / Thread indexing */ |
| 1281 | int v_tid = omega_bw_ir_alloc_vreg(prog); |
| 1282 | int v_ctaid_x = omega_bw_ir_alloc_vreg(prog); |
| 1283 | int v_ctaid_y = omega_bw_ir_alloc_vreg(prog); |
| 1284 | |
| 1285 | /* Tile and thread coordinates */ |
| 1286 | int v_group_id = omega_bw_ir_alloc_vreg(prog); |
| 1287 | int v_tid_in_grp = omega_bw_ir_alloc_vreg(prog); |
| 1288 | int v_row_base = omega_bw_ir_alloc_vreg(prog); |
| 1289 | int v_col_base = omega_bw_ir_alloc_vreg(prog); |
| 1290 | int v_r0 = omega_bw_ir_alloc_vreg(prog); |
| 1291 | int v_r1 = omega_bw_ir_alloc_vreg(prog); |
| 1292 | int v_col_b = omega_bw_ir_alloc_vreg(prog); |
| 1293 | int v_tid_col2 = omega_bw_ir_alloc_vreg(prog); |
| 1294 | int v_const_1 = omega_bw_ir_alloc_vreg(prog); |
| 1295 | |
| 1296 | /* Bundles for HMMA */ |
| 1297 | int v_ra = omega_bw_ir_alloc_vreg128(prog); /* Quad: Ra[0]..Ra[3] */ |
| 1298 | int v_rb = omega_bw_ir_alloc_vreg64(prog); /* Pair: Rb[0]..Rb[1] */ |
| 1299 | int v_rd = omega_bw_ir_alloc_vreg128(prog); /* Quad: Rd[0]..Rd[3] */ |
| 1300 | |
| 1301 | /* Scratch registers for indexing and loads */ |
| 1302 | int v_idx = omega_bw_ir_alloc_vreg(prog); |
| 1303 | int v_off = omega_bw_ir_alloc_vreg64(prog); |
| 1304 | int v_b_tmp0 = omega_bw_ir_alloc_vreg(prog); |
| 1305 | int v_b_tmp1 = omega_bw_ir_alloc_vreg(prog); |
| 1306 | |
| 1307 | if (v_b_tmp1 < 0) return -1; |
| 1308 | |
| 1309 | /* 2. Load 64-bit global memory descriptor into uniform register pair */ |
| 1310 | /* LDCU.64 uv_desc, c[0x0][0x358] */ |
| 1311 | BlackwellIRInsn insn_desc = { .op = BW_IR_LDCU64, .dst_vreg = uv_desc, .imm = 0x358, .is_uniform = true }; |
| 1312 | omega_bw_ir_append(prog, &insn_desc); |
| 1313 | |
| 1314 | /* 3. Load 64-bit base pointers from Constant Bank 0 */ |
| 1315 | /* LDC.64 v_ptr_a, c[0x0][0x380] */ |
| 1316 | BlackwellIRInsn insn_ptra = { .op = BW_IR_LDC64, .dst_vreg = v_ptr_a, .imm = 0x380 }; |
| 1317 | omega_bw_ir_append(prog, &insn_ptra); |
| 1318 | |
| 1319 | /* LDC.64 v_ptr_b, c[0x0][0x388] */ |
| 1320 | BlackwellIRInsn insn_ptrb = { .op = BW_IR_LDC64, .dst_vreg = v_ptr_b, .imm = 0x388 }; |
| 1321 | omega_bw_ir_append(prog, &insn_ptrb); |
| 1322 | |
| 1323 | /* LDC.64 v_ptr_c, c[0x0][0x390] */ |
| 1324 | BlackwellIRInsn insn_ptrc = { .op = BW_IR_LDC64, .dst_vreg = v_ptr_c, .imm = 0x390 }; |
| 1325 | omega_bw_ir_append(prog, &insn_ptrc); |
| 1326 | |
| 1327 | /* 4. Query Special Registers */ |
| 1328 | /* S2R v_tid, SR_TID.X */ |
| 1329 | BlackwellIRInsn insn_tidx = { .op = BW_IR_S2R, .dst_vreg = v_tid, .imm = BW_SR_TID_X }; |
| 1330 | omega_bw_ir_append(prog, &insn_tidx); |
| 1331 | |
| 1332 | /* S2R v_ctaid_x, SR_CTAID.X */ |
| 1333 | BlackwellIRInsn insn_cta_x = { .op = BW_IR_S2R, .dst_vreg = v_ctaid_x, .imm = BW_SR_CTAID_X }; |
| 1334 | omega_bw_ir_append(prog, &insn_cta_x); |
| 1335 | |
| 1336 | /* S2R v_ctaid_y, SR_CTAID.Y */ |
| 1337 | BlackwellIRInsn insn_cta_y = { .op = BW_IR_S2R, .dst_vreg = v_ctaid_y, .imm = BW_SR_CTAID_Y }; |
| 1338 | omega_bw_ir_append(prog, &insn_cta_y); |
| 1339 | |
| 1340 | /* 5. Set up constant 1 */ |
| 1341 | BlackwellIRInsn insn_c1 = { .op = BW_IR_MOV_IMM, .dst_vreg = v_const_1, .imm = 1 }; |
| 1342 | omega_bw_ir_append(prog, &insn_c1); |
| 1343 | |
| 1344 | /* 6. Extract warp-level tile and thread coordinates */ |
| 1345 | /* group_id = tid >> 2 */ |
| 1346 | BlackwellIRInsn insn_grp = { .op = BW_IR_SHF_R, .dst_vreg = v_group_id, .src1_vreg = v_tid, .imm = 2 }; |
| 1347 | omega_bw_ir_append(prog, &insn_grp); |
| 1348 | |
| 1349 | /* tid_in_grp = tid & 3 */ |
| 1350 | BlackwellIRInsn insn_tig = { .op = BW_IR_LOP3_AND, .dst_vreg = v_tid_in_grp, .src1_vreg = v_tid, .imm = 3 }; |
| 1351 | omega_bw_ir_append(prog, &insn_tig); |
| 1352 | |
| 1353 | /* row_base = ctaid_y * 16 */ |
| 1354 | 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 }; |
| 1355 | omega_bw_ir_append(prog, &insn_rbase); |
| 1356 | |
| 1357 | /* col_base = ctaid_x * 8 */ |
| 1358 | 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 }; |
| 1359 | omega_bw_ir_append(prog, &insn_cbase); |
| 1360 | |
| 1361 | /* r0 = row_base + group_id */ |
| 1362 | BlackwellIRInsn insn_r0 = { .op = BW_IR_IADD3, .dst_vreg = v_r0, .src1_vreg = v_row_base, .src2_vreg = v_group_id, .src3_vreg = -1 }; |
| 1363 | omega_bw_ir_append(prog, &insn_r0); |
| 1364 | |
| 1365 | /* r1 = r0 + 8 */ |
| 1366 | 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 }; |
| 1367 | omega_bw_ir_append(prog, &insn_r1); |
| 1368 | |
| 1369 | /* col_b = col_base + group_id */ |
| 1370 | 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 }; |
| 1371 | omega_bw_ir_append(prog, &insn_colb); |
| 1372 | |
| 1373 | /* tid_col2 = tid_in_grp * 2 */ |
| 1374 | 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 }; |
| 1375 | omega_bw_ir_append(prog, &insn_tcol2); |
| 1376 | |
| 1377 | /* 7. Load A fragments (16x16 row-major, 2 bytes/element, row stride 32 bytes) */ |
| 1378 | /* Pair 0: a0, a1 at r0 * 32 + tid_col2 * 2 */ |
| 1379 | BlackwellIRInsn insn_a0_idx1 = { .op = BW_IR_IMAD, .dst_vreg = v_idx, .src1_vreg = v_r0, .src2_vreg = -1, .imm = 32, .src3_vreg = -1 }; |
| 1380 | omega_bw_ir_append(prog, &insn_a0_idx1); |
| 1381 | 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 }; |
| 1382 | omega_bw_ir_append(prog, &insn_a0_idx2); |
| 1383 | 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 }; |
| 1384 | omega_bw_ir_append(prog, &insn_a0_off); |
| 1385 | 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 }; |
| 1386 | omega_bw_ir_append(prog, &insn_lda0); |
| 1387 | |
| 1388 | /* Pair 1: a2, a3 at r1 * 32 + tid_col2 * 2 */ |
| 1389 | BlackwellIRInsn insn_a1_idx1 = { .op = BW_IR_IMAD, .dst_vreg = v_idx, .src1_vreg = v_r1, .src2_vreg = -1, .imm = 32, .src3_vreg = -1 }; |
| 1390 | omega_bw_ir_append(prog, &insn_a1_idx1); |
| 1391 | 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 }; |
| 1392 | omega_bw_ir_append(prog, &insn_a1_idx2); |
| 1393 | 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 }; |
| 1394 | omega_bw_ir_append(prog, &insn_a1_off); |
| 1395 | 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 }; |
| 1396 | omega_bw_ir_append(prog, &insn_lda1); |
| 1397 | |
| 1398 | /* Pair 2: a4, a5 at r0 * 32 + tid_col2 * 2 + 16 */ |
| 1399 | BlackwellIRInsn insn_a2_idx1 = { .op = BW_IR_IMAD, .dst_vreg = v_idx, .src1_vreg = v_r0, .src2_vreg = -1, .imm = 32, .src3_vreg = -1 }; |
| 1400 | omega_bw_ir_append(prog, &insn_a2_idx1); |
| 1401 | 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 }; |
| 1402 | omega_bw_ir_append(prog, &insn_a2_idx2); |
| 1403 | 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 }; |
| 1404 | omega_bw_ir_append(prog, &insn_a2_idx3); |
| 1405 | 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 }; |
| 1406 | omega_bw_ir_append(prog, &insn_a2_off); |
| 1407 | 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 }; |
| 1408 | omega_bw_ir_append(prog, &insn_lda2); |
| 1409 | |
| 1410 | /* Pair 3: a6, a7 at r1 * 32 + tid_col2 * 2 + 16 */ |
| 1411 | BlackwellIRInsn insn_a3_idx1 = { .op = BW_IR_IMAD, .dst_vreg = v_idx, .src1_vreg = v_r1, .src2_vreg = -1, .imm = 32, .src3_vreg = -1 }; |
| 1412 | omega_bw_ir_append(prog, &insn_a3_idx1); |
| 1413 | 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 }; |
| 1414 | omega_bw_ir_append(prog, &insn_a3_idx2); |
| 1415 | 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 }; |
| 1416 | omega_bw_ir_append(prog, &insn_a3_idx3); |
| 1417 | 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 }; |
| 1418 | omega_bw_ir_append(prog, &insn_a3_off); |
| 1419 | 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 }; |
| 1420 | omega_bw_ir_append(prog, &insn_lda3); |
| 1421 | |
| 1422 | /* 8. Load and pack B fragments (16xN row-major, 2 bytes/element) */ |
| 1423 | /* b0 at tid_col2 * N + col_b */ |
| 1424 | 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 }; |
| 1425 | omega_bw_ir_append(prog, &insn_b0_idx); |
| 1426 | 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 }; |
| 1427 | omega_bw_ir_append(prog, &insn_b0_off); |
| 1428 | 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 }; |
| 1429 | omega_bw_ir_append(prog, &insn_ldb0); |
| 1430 | |
| 1431 | /* b1 at (tid_col2 + 1) * N + col_b = b0_idx + N */ |
| 1432 | 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 }; |
| 1433 | omega_bw_ir_append(prog, &insn_b1_idx); |
| 1434 | 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 }; |
| 1435 | omega_bw_ir_append(prog, &insn_b1_off); |
| 1436 | 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 }; |
| 1437 | omega_bw_ir_append(prog, &insn_ldb1); |
| 1438 | |
| 1439 | /* Pack Rb[0] = (b1 << 16) | b0 (wait on barrier 2) */ |
| 1440 | 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 }; |
| 1441 | omega_bw_ir_append(prog, &insn_pack_b0); |
| 1442 | |
| 1443 | /* b2 at (tid_col2 + 8) * N + col_b */ |
| 1444 | 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 }; |
| 1445 | omega_bw_ir_append(prog, &insn_b2_idx1); |
| 1446 | 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 }; |
| 1447 | omega_bw_ir_append(prog, &insn_b2_idx2); |
| 1448 | 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 }; |
| 1449 | omega_bw_ir_append(prog, &insn_b2_off); |
| 1450 | 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 }; |
| 1451 | omega_bw_ir_append(prog, &insn_ldb2); |
| 1452 | |
| 1453 | /* b3 at (tid_col2 + 9) * N + col_b = b2_idx + N */ |
| 1454 | 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 }; |
| 1455 | omega_bw_ir_append(prog, &insn_b3_idx); |
| 1456 | 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 }; |
| 1457 | omega_bw_ir_append(prog, &insn_b3_off); |
| 1458 | 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 }; |
| 1459 | omega_bw_ir_append(prog, &insn_ldb3); |
| 1460 | |
| 1461 | /* Pack Rb[1] = (b3 << 16) | b2 (wait on barrier 2) */ |
| 1462 | 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 }; |
| 1463 | omega_bw_ir_append(prog, &insn_pack_b1); |
| 1464 | |
| 1465 | /* 9. Execute HMMA Tensor Core instruction (wait on barrier 1 for A loads) */ |
| 1466 | BlackwellIROpcode mma_op = (spec->precision == OMEGA_MATMUL_PRECISION_BF16) ? BW_IR_HMMA_BF16 : BW_IR_HMMA_F16; |
| 1467 | BlackwellIRInsn insn_hmma = { |
| 1468 | .op = mma_op, |
| 1469 | .dst_vreg = v_rd, |
| 1470 | .dst_subreg = 0, |
| 1471 | .src1_vreg = v_ra, |
| 1472 | .src1_subreg = 0, |
| 1473 | .src2_vreg = v_rb, |
| 1474 | .src2_subreg = 0, |
| 1475 | .src3_vreg = -1, /* RZ accumulator input */ |
| 1476 | .control = 0x002fca00 /* Wait on barrier 1 */ |
| 1477 | }; |
| 1478 | omega_bw_ir_append(prog, &insn_hmma); |
| 1479 | |
| 1480 | /* 10. Store C output fragments (16xN row-major, 4 bytes/float element) */ |
| 1481 | /* d0: row r0, col col_base + tid_col2 + 0 */ |
| 1482 | 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 }; |
| 1483 | omega_bw_ir_append(prog, &insn_d0_idx1); |
| 1484 | BlackwellIRInsn insn_d0_idx2 = { .op = BW_IR_IADD3, .dst_vreg = v_idx, .src1_vreg = v_idx, .src2_vreg = v_tid_col2, .src3_vreg = -1 }; |
| 1485 | omega_bw_ir_append(prog, &insn_d0_idx2); |
| 1486 | 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 }; |
| 1487 | omega_bw_ir_append(prog, &insn_d0_off); |
| 1488 | BlackwellIRInsn insn_std0 = { .op = BW_IR_STG_E, .src1_vreg = v_off, .src2_vreg = v_rd, .src2_subreg = 0, .ureg = uv_desc, .control = 0x000fe200 }; |
| 1489 | omega_bw_ir_append(prog, &insn_std0); |
| 1490 | |
| 1491 | /* d1: row r0, col col_base + tid_col2 + 1 */ |
| 1492 | 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 }; |
| 1493 | omega_bw_ir_append(prog, &insn_d1_idx); |
| 1494 | 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 }; |
| 1495 | omega_bw_ir_append(prog, &insn_d1_off); |
| 1496 | BlackwellIRInsn insn_std1 = { .op = BW_IR_STG_E, .src1_vreg = v_off, .src2_vreg = v_rd, .src2_subreg = 1, .ureg = uv_desc, .control = 0x000fe200 }; |
| 1497 | omega_bw_ir_append(prog, &insn_std1); |
| 1498 | |
| 1499 | /* d2: row r1, col col_base + tid_col2 + 0 */ |
| 1500 | 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 }; |
| 1501 | omega_bw_ir_append(prog, &insn_d2_idx1); |
| 1502 | BlackwellIRInsn insn_d2_idx2 = { .op = BW_IR_IADD3, .dst_vreg = v_idx, .src1_vreg = v_idx, .src2_vreg = v_tid_col2, .src3_vreg = -1 }; |
| 1503 | omega_bw_ir_append(prog, &insn_d2_idx2); |
| 1504 | 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 }; |
| 1505 | omega_bw_ir_append(prog, &insn_d2_off); |
| 1506 | BlackwellIRInsn insn_std2 = { .op = BW_IR_STG_E, .src1_vreg = v_off, .src2_vreg = v_rd, .src2_subreg = 2, .ureg = uv_desc, .control = 0x000fe200 }; |
| 1507 | omega_bw_ir_append(prog, &insn_std2); |
| 1508 | |
| 1509 | /* d3: row r1, col col_base + tid_col2 + 1 */ |
| 1510 | 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 }; |
| 1511 | omega_bw_ir_append(prog, &insn_d3_idx); |
| 1512 | 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 }; |
| 1513 | omega_bw_ir_append(prog, &insn_d3_off); |
| 1514 | BlackwellIRInsn insn_std3 = { .op = BW_IR_STG_E, .src1_vreg = v_off, .src2_vreg = v_rd, .src2_subreg = 3, .ureg = uv_desc, .control = 0x000fe200 }; |
| 1515 | omega_bw_ir_append(prog, &insn_std3); |
| 1516 | |
| 1517 | /* 11. Program Exit */ |
| 1518 | BlackwellIRInsn insn_exit = { .op = BW_IR_EXIT }; |
| 1519 | omega_bw_ir_append(prog, &insn_exit); |
| 1520 | BlackwellIRInsn insn_bra = { .op = BW_IR_BRA }; |
| 1521 | omega_bw_ir_append(prog, &insn_bra); |
| 1522 | |
| 1523 | /* 12. Run bounded register allocation */ |
| 1524 | if (omega_bw_regalloc_solve(prog) != 0) { |
| 1525 | return -1; |
| 1526 | } |
| 1527 | return 0; |
| 1528 | } |
| 1529 | |
| 1530 | #ifndef OMEGA_GPU_NO_POSIX |
| 1531 | int omega_blackwell_codegen_matmul_tensor(const OmegaMatMulSpec *spec, OmegaBlackwellKernel *kernel) { |
| 1532 | if (!spec || !kernel) return -1; |
| 1533 | BlackwellIRProgram prog; |
| 1534 | if (omega_blackwell_codegen_matmul_tensor_prog(spec, &prog) != 0) { |
| 1535 | return -1; |
| 1536 | } |
| 1537 | |
| 1538 | return bw_kernel_alloc_encode(&prog, OMEGA_BW_CODE_CAP_SMALL, kernel); |
| 1539 | } |
| 1540 | #endif |
| 1541 | |
| 1542 | /* ---- FB-1 cut 1b: looped tensor matmul ------------------------------------------ |
| 1543 | * |
| 1544 | * Control words (bundle bits 105..125, decoded from the chip-proven kernel and from |
| 1545 | * the vendor compiler's SASS for an equivalent HMMA loop on this chip, 2026-10-04): |
| 1546 | * stall = bits 9..12 of w[3], yield = 13, write barrier = 14..16, read barrier = |
| 1547 | * 17..19, wait mask = 20..25. 7 in a barrier field means "none". |
| 1548 | * LDG (A) 0x001ea800 stall 4, yield, sets SB2 |
| 1549 | * LDG.U16 (B) 0x002ea800 stall 4, yield, sets SB2 |
| 1550 | * pack IMAD 0x004fca00 stall 5, waits SB2 (every outstanding load has landed) |
| 1551 | * HMMA 0x002fca00 stall 5 (fixed latency: the vendor compiler sets no |
| 1552 | * write barrier on HMMA either; consumers wait by stall count) |
| 1553 | * STG 0x0007e200 stall 1, yield, read barrier SB3 (the store may read its |
| 1554 | * data/address registers late; the next writer waits SB3) |
| 1555 | * wait SB3 mask bit 23 (0x00800000) on the first instruction that overwrites a |
| 1556 | * register a store reads: the C pointer advance. |
| 1557 | * Loop-carried registers are kept live to the end of their loop (keep_alive) because |
| 1558 | * the allocator is a straight-line linear scan and knows nothing about back edges. |
| 1559 | */ |
| 1560 | static void bw_keep_alive(BlackwellIRProgram *prog, int from_idx, int to_idx, int last_idx) { |
| 1561 | /* every vreg first defined in [from_idx, to_idx) stays live until last_idx */ |
| 1562 | for (int v = 0; v < prog->regalloc.num_vregs; v++) { |
| 1563 | OmegaLiveInterval *iv = &prog->regalloc.intervals[v]; |
| 1564 | if (iv->first_def >= from_idx && iv->first_def < to_idx && iv->last_use < last_idx) |
| 1565 | iv->last_use = last_idx; |
| 1566 | } |
| 1567 | } |
| 1568 | |
| 1569 | #define BW_APPEND(prog, ...) do { BlackwellIRInsn _i = { __VA_ARGS__ }; if (omega_bw_ir_append((prog), &_i) < 0) return -1; } while (0) |
| 1570 | |
| 1571 | int omega_blackwell_codegen_matmul_tensor_loop_prog(const OmegaMatMulSpec *spec, uint32_t grid_x, |
| 1572 | int mutant, BlackwellIRProgram *prog) { |
| 1573 | if (!spec || !prog) return -1; |
| 1574 | if (spec->m == 0 || spec->k == 0 || spec->n == 0) return -1; |
| 1575 | if (spec->precision != OMEGA_MATMUL_PRECISION_FP16 && spec->precision != OMEGA_MATMUL_PRECISION_BF16) return -1; |
| 1576 | if (spec->m % 16 != 0 || spec->n % 8 != 0 || spec->k % 16 != 0) { |
| 1577 | BW_DIAG("ERROR: looped MatMul tensor codegen requires M%%16, N%%8, K%%16 (got %ux%ux%u)\n", |
| 1578 | spec->m, spec->k, spec->n); |
| 1579 | return -1; |
| 1580 | } |
| 1581 | const uint32_t K = spec->k, N = spec->n, NT = N / 8, KSTEPS = K / 16; |
| 1582 | if (grid_x == 0 || grid_x > NT) return -1; |
| 1583 | /* every immediate below must fit 32 bits: 32 * N (B row-block stride in bytes) is the largest */ |
| 1584 | if ((uint64_t)N * 32u > 0xffffffffull || (uint64_t)grid_x * 32u > 0xffffffffull) return -1; |
| 1585 | if (mutant && KSTEPS < 2) return -1; |
| 1586 | |
| 1587 | omega_bw_ir_init(prog); |
| 1588 | |
| 1589 | 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); |
| 1590 | int uv_desc = omega_bw_ir_alloc_uvreg64(prog); |
| 1591 | 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); |
| 1592 | 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); |
| 1593 | 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); |
| 1594 | 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); |
| 1595 | 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); |
| 1596 | int v_pc0 = omega_bw_ir_alloc_vreg64(prog), v_pc1 = omega_bw_ir_alloc_vreg64(prog); |
| 1597 | 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); |
| 1598 | int v_ta2 = omega_bw_ir_alloc_vreg64(prog), v_ta3 = omega_bw_ir_alloc_vreg64(prog); |
| 1599 | 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); |
| 1600 | int v_tc1 = omega_bw_ir_alloc_vreg64(prog), v_tc3 = omega_bw_ir_alloc_vreg64(prog); |
| 1601 | 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); |
| 1602 | int v_b0 = omega_bw_ir_alloc_vreg(prog), v_b1 = omega_bw_ir_alloc_vreg(prog); |
| 1603 | if (v_b1 < 0 || uv_desc < 0) return -1; |
| 1604 | |
| 1605 | const uint32_t CTRL_LDA = 0x001ea800u, CTRL_LDB = 0x002ea800u, CTRL_PACK = 0x004fca00u; |
| 1606 | const uint32_t CTRL_HMMA = 0x002fca00u, CTRL_STG_RDBAR3 = 0x0007e200u, CTRL_IMADW_WAIT3 = 0x009fcc00u; |
| 1607 | const uint32_t CTRL_MOVRZ_WAIT3 = 0x008fe200u; |
| 1608 | |
| 1609 | /* prologue: descriptor, pointers, ids */ |
| 1610 | BW_APPEND(prog, .op = BW_IR_LDCU64, .dst_vreg = uv_desc, .imm = 0x358, .is_uniform = true); |
| 1611 | BW_APPEND(prog, .op = BW_IR_LDC64, .dst_vreg = v_ptr_a, .imm = 0x380); |
| 1612 | BW_APPEND(prog, .op = BW_IR_LDC64, .dst_vreg = v_ptr_b, .imm = 0x388); |
| 1613 | BW_APPEND(prog, .op = BW_IR_LDC64, .dst_vreg = v_ptr_c, .imm = 0x390); |
| 1614 | BW_APPEND(prog, .op = BW_IR_S2R, .dst_vreg = v_tid, .imm = BW_SR_TID_X); |
| 1615 | BW_APPEND(prog, .op = BW_IR_S2R, .dst_vreg = v_ctaid_x, .imm = BW_SR_CTAID_X); |
| 1616 | BW_APPEND(prog, .op = BW_IR_S2R, .dst_vreg = v_ctaid_y, .imm = BW_SR_CTAID_Y); |
| 1617 | BW_APPEND(prog, .op = BW_IR_MOV_IMM, .dst_vreg = v_c1, .imm = 1); |
| 1618 | BW_APPEND(prog, .op = BW_IR_SHF_R, .dst_vreg = v_group, .src1_vreg = v_tid, .imm = 2); |
| 1619 | BW_APPEND(prog, .op = BW_IR_LOP3_AND, .dst_vreg = v_tig, .src1_vreg = v_tid, .imm = 3); |
| 1620 | BW_APPEND(prog, .op = BW_IR_IMAD, .dst_vreg = v_tcol2, .src1_vreg = v_tig, .src2_vreg = -1, .imm = 2, .src3_vreg = -1); |
| 1621 | 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); |
| 1622 | BW_APPEND(prog, .op = BW_IR_IMAD, .dst_vreg = v_r1, .src1_vreg = v_c1, .src2_vreg = -1, .imm = 8, .src3_vreg = v_r0); |
| 1623 | /* pa0 = A + (r0*K + tcol2)*2 ; pa1 likewise with r1 (A row-major, K bf16 per row) */ |
| 1624 | BW_APPEND(prog, .op = BW_IR_IMAD, .dst_vreg = v_idx, .src1_vreg = v_r0, .src2_vreg = -1, .imm = K, .src3_vreg = v_tcol2); |
| 1625 | 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); |
| 1626 | BW_APPEND(prog, .op = BW_IR_IMAD, .dst_vreg = v_idx, .src1_vreg = v_r1, .src2_vreg = -1, .imm = K, .src3_vreg = v_tcol2); |
| 1627 | 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); |
| 1628 | /* col = ctaid_x (first column tile of this CTA) */ |
| 1629 | BW_APPEND(prog, .op = BW_IR_IMAD, .dst_vreg = v_col, .src1_vreg = v_ctaid_x, .src2_vreg = -1, .imm = 1, .src3_vreg = -1); |
| 1630 | /* pbcol = B + (tcol2*N + col*8 + group)*2 */ |
| 1631 | BW_APPEND(prog, .op = BW_IR_IMAD, .dst_vreg = v_idx, .src1_vreg = v_tcol2, .src2_vreg = -1, .imm = N, .src3_vreg = v_group); |
| 1632 | BW_APPEND(prog, .op = BW_IR_IMAD, .dst_vreg = v_idx, .src1_vreg = v_col, .src2_vreg = -1, .imm = 8, .src3_vreg = v_idx); |
| 1633 | 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); |
| 1634 | /* pc0 = C + (r0*N + col*8 + tcol2)*4 ; pc1 with r1 */ |
| 1635 | BW_APPEND(prog, .op = BW_IR_IMAD, .dst_vreg = v_idx, .src1_vreg = v_r0, .src2_vreg = -1, .imm = N, .src3_vreg = v_tcol2); |
| 1636 | BW_APPEND(prog, .op = BW_IR_IMAD, .dst_vreg = v_idx, .src1_vreg = v_col, .src2_vreg = -1, .imm = 8, .src3_vreg = v_idx); |
| 1637 | 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); |
| 1638 | BW_APPEND(prog, .op = BW_IR_IMAD, .dst_vreg = v_idx, .src1_vreg = v_r1, .src2_vreg = -1, .imm = N, .src3_vreg = v_tcol2); |
| 1639 | BW_APPEND(prog, .op = BW_IR_IMAD, .dst_vreg = v_idx, .src1_vreg = v_col, .src2_vreg = -1, .imm = 8, .src3_vreg = v_idx); |
| 1640 | 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); |
| 1641 | |
| 1642 | /* ---- column-tile loop ---- */ |
| 1643 | const int L_col = (int)prog->count; |
| 1644 | /* accumulator = 0 (the first write waits SB3: the previous tile's stores may still read it) */ |
| 1645 | BW_APPEND(prog, .op = BW_IR_MOV_RZ, .dst_vreg = v_rd, .dst_subreg = 0, .control = CTRL_MOVRZ_WAIT3); |
| 1646 | BW_APPEND(prog, .op = BW_IR_MOV_RZ, .dst_vreg = v_rd, .dst_subreg = 1); |
| 1647 | BW_APPEND(prog, .op = BW_IR_MOV_RZ, .dst_vreg = v_rd, .dst_subreg = 2); |
| 1648 | BW_APPEND(prog, .op = BW_IR_MOV_RZ, .dst_vreg = v_rd, .dst_subreg = 3); |
| 1649 | /* K cursors start at the row/column bases (IMAD.WIDE with imm 0 is a 64-bit copy) */ |
| 1650 | 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); |
| 1651 | 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); |
| 1652 | 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); |
| 1653 | BW_APPEND(prog, .op = BW_IR_MOV_IMM, .dst_vreg = v_cnt, .imm = mutant ? KSTEPS - 1 : KSTEPS); |
| 1654 | |
| 1655 | /* ---- K loop: one 16x16 A fragment and one 16x8 B fragment per step ---- */ |
| 1656 | const int L_k = (int)prog->count; |
| 1657 | 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); |
| 1658 | 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); |
| 1659 | 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); |
| 1660 | 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); |
| 1661 | 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); |
| 1662 | /* A: a0,a1 at (r0, kk+tcol2) ; a2,a3 at (r1, ...) ; a4,a5 at (r0, +8) ; a6,a7 at (r1, +8) */ |
| 1663 | 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); |
| 1664 | 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); |
| 1665 | 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); |
| 1666 | 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); |
| 1667 | /* B: rows kk+tcol2, +1, +8, +9 at column col*8+group */ |
| 1668 | 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); |
| 1669 | 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); |
| 1670 | 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); |
| 1671 | 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); |
| 1672 | 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); |
| 1673 | 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); |
| 1674 | /* rd += ra * rb (accumulate in place) */ |
| 1675 | BW_APPEND(prog, .op = (spec->precision == OMEGA_MATMUL_PRECISION_BF16) ? BW_IR_HMMA_BF16 : BW_IR_HMMA_F16, |
| 1676 | .dst_vreg = v_rd, .dst_subreg = 0, .src1_vreg = v_ra, .src1_subreg = 0, .src2_vreg = v_rb, .src2_subreg = 0, |
| 1677 | .src3_vreg = v_rd, .src3_subreg = 0, .control = CTRL_HMMA); |
| 1678 | /* advance K: A by 16 bf16 (32 bytes), B by 16 rows (32 N bytes); every load above has landed (packs waited SB2) */ |
| 1679 | 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); |
| 1680 | 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); |
| 1681 | 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); |
| 1682 | BW_APPEND(prog, .op = BW_IR_IMAD, .dst_vreg = v_cnt, .src1_vreg = v_c1, .src2_vreg = -1, .imm = 0xffffffffu, .src3_vreg = v_cnt); |
| 1683 | BW_APPEND(prog, .op = BW_IR_ISETP_GE, .dst_vreg = -1, .src1_vreg = v_cnt, .src2_vreg = -1, .imm = 1, .src3_vreg = -1); |
| 1684 | { |
| 1685 | int here = (int)prog->count; |
| 1686 | BW_APPEND(prog, .op = BW_IR_BRA, .imm = (uint32_t)(int32_t)(L_k - here), .predicate_p0 = true); |
| 1687 | } |
| 1688 | const int L_k_end = (int)prog->count - 1; |
| 1689 | |
| 1690 | /* ---- store the 16x8 tile: d0 (r0, c), d1 (r0, c+1), d2 (r1, c), d3 (r1, c+1) ---- */ |
| 1691 | 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); |
| 1692 | 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); |
| 1693 | 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); |
| 1694 | 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); |
| 1695 | 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); |
| 1696 | 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); |
| 1697 | /* next column tile of this CTA: col += grid_x ; B by grid_x*8 bf16 ; C by grid_x*8 f32 (waits SB3 first) */ |
| 1698 | 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); |
| 1699 | 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); |
| 1700 | 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); |
| 1701 | 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); |
| 1702 | BW_APPEND(prog, .op = BW_IR_ISETP_GE, .dst_vreg = -1, .src1_vreg = v_col, .src2_vreg = -1, .imm = NT, .src3_vreg = -1); |
| 1703 | { |
| 1704 | int here = (int)prog->count; |
| 1705 | BW_APPEND(prog, .op = BW_IR_BRA, .imm = (uint32_t)(int32_t)(L_col - here), .predicate_p0 = true, .predicate_not = true); |
| 1706 | } |
| 1707 | const int L_col_end = (int)prog->count - 1; |
| 1708 | BW_APPEND(prog, .op = BW_IR_EXIT); |
| 1709 | BW_APPEND(prog, .op = BW_IR_BRA); |
| 1710 | |
| 1711 | /* loop-carried values live to the end of their loop */ |
| 1712 | bw_keep_alive(prog, 0, L_col, L_col_end); |
| 1713 | bw_keep_alive(prog, L_col, L_k, L_k_end); |
| 1714 | /* K-loop temporaries too: a load's address pair must never be recycled by a |
| 1715 | * later instruction of the same iteration (the load may read it late). */ |
| 1716 | bw_keep_alive(prog, L_k, L_k_end, L_k_end); |
| 1717 | /* the accumulator is read by the stores after the K loop */ |
| 1718 | if (prog->regalloc.intervals[v_rd].last_use < L_col_end) prog->regalloc.intervals[v_rd].last_use = L_col_end; |
| 1719 | |
| 1720 | return omega_bw_regalloc_solve(prog); |
| 1721 | } |
| 1722 | |
| 1723 | #ifndef OMEGA_GPU_NO_POSIX |
| 1724 | int omega_blackwell_codegen_matmul_tensor_loop(const OmegaMatMulSpec *spec, uint32_t grid_x, |
| 1725 | int mutant, OmegaBlackwellKernel *kernel) { |
| 1726 | if (!spec || !kernel) return -1; |
| 1727 | BlackwellIRProgram prog; |
| 1728 | if (omega_blackwell_codegen_matmul_tensor_loop_prog(spec, grid_x, mutant, &prog) != 0) return -1; |
| 1729 | return bw_kernel_alloc_encode(&prog, OMEGA_BW_CODE_CAP_SMALL, kernel); |
| 1730 | } |
| 1731 | #endif |
| 1732 | |
| 1733 | /* ---- omega #328: fragment-major tensor matmul ---------------------------------------- |
| 1734 | * |
| 1735 | * The looped kernel above reads B as four scattered 2-byte loads per K step and waits for |
| 1736 | * them before the next step (pack waits SB2), so each warp has one 256-byte step in flight; |
| 1737 | * at m = 1 on GB10 that streams the weights at about 28 GB/s (omega #328 measurement). |
| 1738 | * Here A and B arrive in the order HMMA.16816 consumes them (omega_matmul_frag_pack_a / _b): |
| 1739 | * A block (mt, ks): 512 bytes, lane L at L*16: a0..a7, one LDG.E.128 per step; |
| 1740 | * B block (nt, ks): 256 bytes, lane L at L*8: b0..b3, one LDG.E.64 per step; |
| 1741 | * consecutive K steps of one tile are adjacent, so a warp streams its column tile. Each loop |
| 1742 | * pass issues `unroll` steps of loads at immediate offsets (A on SB2, B on SB4), then runs the HMMA |
| 1743 | * chain: every HMMA waits SB2 and SB4 (stall 15) and a NOP (stall 14) follows it, the 29 cycles nvcc |
| 1744 | * puts between dependent HMMA.16816 (omega_blackwell_verify_codegen_fixtures_fragmm). |
| 1745 | * The cursors advance only after the chain, when every load has landed (the HMMAs waited |
| 1746 | * SB2 and SB4), as in the looped kernel. Stores, the column-tile loop and the C layout (row-major |
| 1747 | * f32) are the looped kernel's. */ |
| 1748 | /* A loads set SB2 and B loads SB4 (nvcc spreads loads over several scoreboards too); at the |
| 1749 | * product unroll of 4 that is 4 loads per scoreboard per pass. The scoreboard depth is not |
| 1750 | * documented to us; splitting did not make unroll 8 complete (omega_matmul_frag_unroll). */ |
| 1751 | #define BW_FRAG_CTRL_LDG_A 0x000ea800u /* stall 4, yield, sets SB2 (nvcc word) */ |
| 1752 | #define BW_FRAG_CTRL_LDG_B 0x000f2800u /* stall 4, yield, sets SB4 (nvcc word) */ |
| 1753 | #define BW_FRAG_CTRL_HMMA 0x014fde00u /* stall 15, waits SB2 and SB4 (nvcc: dependent HMMA chain) */ |
| 1754 | #define BW_FRAG_CTRL_NOP 0x000fdc00u /* stall 14 (nvcc, between dependent HMMAs) */ |
| 1755 | |
| 1756 | static void bw_live_until(BlackwellIRProgram *prog, int vreg, int last_idx) { |
| 1757 | if (prog->regalloc.intervals[vreg].last_use < last_idx) prog->regalloc.intervals[vreg].last_use = last_idx; |
| 1758 | } |
| 1759 | |
| 1760 | int omega_blackwell_codegen_matmul_frag_prog(const OmegaMatMulSpec *spec, uint32_t grid_x, uint32_t unroll, |
| 1761 | int mutant, BlackwellIRProgram *prog) { |
| 1762 | if (!spec || !prog) return -1; |
| 1763 | if (spec->m == 0 || spec->k == 0 || spec->n == 0) return -1; |
| 1764 | if (spec->precision != OMEGA_MATMUL_PRECISION_FP16 && spec->precision != OMEGA_MATMUL_PRECISION_BF16) return -1; |
| 1765 | if (spec->m % 16 != 0 || spec->n % 8 != 0 || spec->k % 16 != 0) return -1; |
| 1766 | if (unroll != 1 && unroll != 2 && unroll != 4 && unroll != 8) return -1; |
| 1767 | const uint32_t K = spec->k, N = spec->n, NT = N / 8, KS = K / 16; |
| 1768 | if (KS % unroll != 0) return -1; |
| 1769 | const uint32_t ITERS = KS / unroll; |
| 1770 | if (grid_x == 0 || grid_x > NT) return -1; |
| 1771 | if (mutant && ITERS < 2) return -1; |
| 1772 | /* every index and immediate below must fit 32 bits (rows up to OMEGA_BW_MATMUL_MAX_M) */ |
| 1773 | if ((uint64_t)NT * KS * 32u > 0xffffffffull || (uint64_t)(OMEGA_BW_MATMUL_MAX_M / 16u) * KS * 32u > 0xffffffffull || |
| 1774 | (uint64_t)grid_x * KS * 256u > 0xffffffffull || (uint64_t)N * 32u > 0xffffffffull || |
| 1775 | (uint64_t)OMEGA_BW_MATMUL_MAX_M * N > 0xffffffffull) |
| 1776 | return -1; |
| 1777 | |
| 1778 | omega_bw_ir_init(prog); |
| 1779 | 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); |
| 1780 | int uv_desc = omega_bw_ir_alloc_uvreg64(prog); |
| 1781 | 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); |
| 1782 | 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); |
| 1783 | 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); |
| 1784 | 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); |
| 1785 | int v_pa = omega_bw_ir_alloc_vreg64(prog), v_pbcol = omega_bw_ir_alloc_vreg64(prog); |
| 1786 | int v_pc0 = omega_bw_ir_alloc_vreg64(prog), v_pc1 = omega_bw_ir_alloc_vreg64(prog); |
| 1787 | int v_pac = omega_bw_ir_alloc_vreg64(prog), v_pbc = omega_bw_ir_alloc_vreg64(prog); |
| 1788 | int v_tc1 = omega_bw_ir_alloc_vreg64(prog), v_tc3 = omega_bw_ir_alloc_vreg64(prog); |
| 1789 | int v_rd = omega_bw_ir_alloc_vreg128(prog); |
| 1790 | int v_ra[8], v_rb[8]; |
| 1791 | 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); } |
| 1792 | if (v_rb[unroll - 1] < 0 || v_ra[unroll - 1] < 0 || uv_desc < 0) return -1; |
| 1793 | |
| 1794 | const uint32_t CTRL_STG_RDBAR3 = 0x0007e200u, CTRL_IMADW_WAIT3 = 0x009fcc00u, CTRL_MOVRZ_WAIT3 = 0x008fe200u; |
| 1795 | |
| 1796 | /* prologue: descriptor, pointers, ids (as the looped kernel) */ |
| 1797 | BW_APPEND(prog, .op = BW_IR_LDCU64, .dst_vreg = uv_desc, .imm = 0x358, .is_uniform = true); |
| 1798 | BW_APPEND(prog, .op = BW_IR_LDC64, .dst_vreg = v_ptr_a, .imm = 0x380); |
| 1799 | BW_APPEND(prog, .op = BW_IR_LDC64, .dst_vreg = v_ptr_b, .imm = 0x388); |
| 1800 | BW_APPEND(prog, .op = BW_IR_LDC64, .dst_vreg = v_ptr_c, .imm = 0x390); |
| 1801 | BW_APPEND(prog, .op = BW_IR_S2R, .dst_vreg = v_tid, .imm = BW_SR_TID_X); |
| 1802 | BW_APPEND(prog, .op = BW_IR_S2R, .dst_vreg = v_ctaid_x, .imm = BW_SR_CTAID_X); |
| 1803 | BW_APPEND(prog, .op = BW_IR_S2R, .dst_vreg = v_ctaid_y, .imm = BW_SR_CTAID_Y); |
| 1804 | BW_APPEND(prog, .op = BW_IR_MOV_IMM, .dst_vreg = v_c1, .imm = 1); |
| 1805 | BW_APPEND(prog, .op = BW_IR_SHF_R, .dst_vreg = v_group, .src1_vreg = v_tid, .imm = 2); |
| 1806 | BW_APPEND(prog, .op = BW_IR_LOP3_AND, .dst_vreg = v_tig, .src1_vreg = v_tid, .imm = 3); |
| 1807 | BW_APPEND(prog, .op = BW_IR_IMAD, .dst_vreg = v_tcol2, .src1_vreg = v_tig, .src2_vreg = -1, .imm = 2, .src3_vreg = -1); |
| 1808 | 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); |
| 1809 | BW_APPEND(prog, .op = BW_IR_IMAD, .dst_vreg = v_r1, .src1_vreg = v_c1, .src2_vreg = -1, .imm = 8, .src3_vreg = v_r0); |
| 1810 | /* pa = A + (ctaid_y * KS * 32 + tid) * 16: this CTA's row tile, this lane's 16 bytes of step 0 */ |
| 1811 | 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); |
| 1812 | 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); |
| 1813 | BW_APPEND(prog, .op = BW_IR_IMAD, .dst_vreg = v_col, .src1_vreg = v_ctaid_x, .src2_vreg = -1, .imm = 1, .src3_vreg = -1); |
| 1814 | /* pbcol = B + (col * KS * 32 + tid) * 8: column tile col, this lane's 8 bytes of step 0 */ |
| 1815 | 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); |
| 1816 | 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); |
| 1817 | /* pc0 = C + (r0*N + col*8 + tcol2)*4 ; pc1 with r1 */ |
| 1818 | BW_APPEND(prog, .op = BW_IR_IMAD, .dst_vreg = v_idx, .src1_vreg = v_r0, .src2_vreg = -1, .imm = N, .src3_vreg = v_tcol2); |
| 1819 | BW_APPEND(prog, .op = BW_IR_IMAD, .dst_vreg = v_idx, .src1_vreg = v_col, .src2_vreg = -1, .imm = 8, .src3_vreg = v_idx); |
| 1820 | 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); |
| 1821 | BW_APPEND(prog, .op = BW_IR_IMAD, .dst_vreg = v_idx, .src1_vreg = v_r1, .src2_vreg = -1, .imm = N, .src3_vreg = v_tcol2); |
| 1822 | BW_APPEND(prog, .op = BW_IR_IMAD, .dst_vreg = v_idx, .src1_vreg = v_col, .src2_vreg = -1, .imm = 8, .src3_vreg = v_idx); |
| 1823 | 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); |
| 1824 | |
| 1825 | /* ---- column-tile loop ---- */ |
| 1826 | const int L_col = (int)prog->count; |
| 1827 | BW_APPEND(prog, .op = BW_IR_MOV_RZ, .dst_vreg = v_rd, .dst_subreg = 0, .control = CTRL_MOVRZ_WAIT3); |
| 1828 | BW_APPEND(prog, .op = BW_IR_MOV_RZ, .dst_vreg = v_rd, .dst_subreg = 1); |
| 1829 | BW_APPEND(prog, .op = BW_IR_MOV_RZ, .dst_vreg = v_rd, .dst_subreg = 2); |
| 1830 | BW_APPEND(prog, .op = BW_IR_MOV_RZ, .dst_vreg = v_rd, .dst_subreg = 3); |
| 1831 | 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); |
| 1832 | 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); |
| 1833 | BW_APPEND(prog, .op = BW_IR_MOV_IMM, .dst_vreg = v_cnt, .imm = mutant ? ITERS - 1 : ITERS); |
| 1834 | |
| 1835 | /* ---- K loop: `unroll` steps of loads, then the HMMA chain ---- */ |
| 1836 | const int L_k = (int)prog->count; |
| 1837 | for (uint32_t u = 0; u < unroll; u++) { |
| 1838 | BW_APPEND(prog, .op = BW_IR_LDG_E_128, .dst_vreg = v_ra[u], .src1_vreg = v_pac, .src2_vreg = -1, .src3_vreg = -1, |
| 1839 | .ureg = uv_desc, .imm = u * 512u, .control = BW_FRAG_CTRL_LDG_A); |
| 1840 | BW_APPEND(prog, .op = BW_IR_LDG_E_64, .dst_vreg = v_rb[u], .src1_vreg = v_pbc, .src2_vreg = -1, .src3_vreg = -1, |
| 1841 | .ureg = uv_desc, .imm = u * 256u, .control = BW_FRAG_CTRL_LDG_B); |
| 1842 | } |
| 1843 | const BlackwellIROpcode hmma = (spec->precision == OMEGA_MATMUL_PRECISION_BF16) ? BW_IR_HMMA_BF16 : BW_IR_HMMA_F16; |
| 1844 | for (uint32_t u = 0; u < unroll; u++) { |
| 1845 | BW_APPEND(prog, .op = hmma, .dst_vreg = v_rd, .src1_vreg = v_ra[u], .src2_vreg = v_rb[u], .src3_vreg = v_rd, |
| 1846 | .control = BW_FRAG_CTRL_HMMA); |
| 1847 | BW_APPEND(prog, .op = BW_IR_NOP, .dst_vreg = -1, .src1_vreg = -1, .src2_vreg = -1, .src3_vreg = -1, .ureg = -1, |
| 1848 | .control = BW_FRAG_CTRL_NOP); |
| 1849 | } |
| 1850 | 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); |
| 1851 | 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); |
| 1852 | BW_APPEND(prog, .op = BW_IR_IMAD, .dst_vreg = v_cnt, .src1_vreg = v_c1, .src2_vreg = -1, .imm = 0xffffffffu, .src3_vreg = v_cnt); |
| 1853 | BW_APPEND(prog, .op = BW_IR_ISETP_GE, .dst_vreg = -1, .src1_vreg = v_cnt, .src2_vreg = -1, .imm = 1, .src3_vreg = -1); |
| 1854 | { |
| 1855 | int here = (int)prog->count; |
| 1856 | BW_APPEND(prog, .op = BW_IR_BRA, .imm = (uint32_t)(int32_t)(L_k - here), .predicate_p0 = true); |
| 1857 | } |
| 1858 | const int L_k_end = (int)prog->count - 1; |
| 1859 | |
| 1860 | /* ---- store the 16x8 tile (as the looped kernel) ---- */ |
| 1861 | 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); |
| 1862 | 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); |
| 1863 | 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); |
| 1864 | 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); |
| 1865 | 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); |
| 1866 | 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); |
| 1867 | /* next column tile: col += grid_x ; C by grid_x*8 f32 (waits SB3 first) ; B by grid_x tiles of KS*256 bytes */ |
| 1868 | 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); |
| 1869 | 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); |
| 1870 | 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); |
| 1871 | 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); |
| 1872 | BW_APPEND(prog, .op = BW_IR_ISETP_GE, .dst_vreg = -1, .src1_vreg = v_col, .src2_vreg = -1, .imm = NT, .src3_vreg = -1); |
| 1873 | { |
| 1874 | int here = (int)prog->count; |
| 1875 | BW_APPEND(prog, .op = BW_IR_BRA, .imm = (uint32_t)(int32_t)(L_col - here), .predicate_p0 = true, .predicate_not = true); |
| 1876 | } |
| 1877 | const int L_col_end = (int)prog->count - 1; |
| 1878 | BW_APPEND(prog, .op = BW_IR_EXIT); |
| 1879 | BW_APPEND(prog, .op = BW_IR_BRA); |
| 1880 | |
| 1881 | /* Loop-carried values live to the end of their loop (the allocator knows no back edges). |
| 1882 | * Only these are read in a later pass than the one that wrote them; prologue temporaries |
| 1883 | * are dead before the first loop and may be reused. K-loop temporaries (fragments) are |
| 1884 | * kept to the end of the pass, as in the looped kernel. */ |
| 1885 | const int col_carried[] = { v_c1, v_col, v_pa, v_pbcol, v_pc0, v_pc1, v_rd }; |
| 1886 | for (size_t i = 0; i < sizeof col_carried / sizeof col_carried[0]; i++) bw_live_until(prog, col_carried[i], L_col_end); |
| 1887 | const int k_carried[] = { v_pac, v_pbc, v_cnt }; |
| 1888 | for (size_t i = 0; i < sizeof k_carried / sizeof k_carried[0]; i++) bw_live_until(prog, k_carried[i], L_k_end); |
| 1889 | bw_keep_alive(prog, L_k, L_k_end, L_k_end); |
| 1890 | return omega_bw_regalloc_solve(prog); |
| 1891 | } |
| 1892 | |
| 1893 | #ifndef OMEGA_GPU_NO_POSIX |
| 1894 | int omega_blackwell_codegen_matmul_frag(const OmegaMatMulSpec *spec, uint32_t grid_x, uint32_t unroll, |
| 1895 | int mutant, OmegaBlackwellKernel *kernel) { |
| 1896 | if (!spec || !kernel) return -1; |
| 1897 | BlackwellIRProgram prog; |
| 1898 | if (omega_blackwell_codegen_matmul_frag_prog(spec, grid_x, unroll, mutant, &prog) != 0) return -1; |
| 1899 | return bw_kernel_alloc_encode(&prog, OMEGA_BW_CODE_CAP_SMALL, kernel); |
| 1900 | } |
| 1901 | #endif |
| 1902 | |
| 1903 | /* Capped at 4: unroll 8 (72 registers, 16 loads per pass) never completed on the GB10, with all |
| 1904 | * loads on SB2 and again with A on SB2 and B on SB4 (2026-10-07 07:50Z and 07:54Z, omega #328), |
| 1905 | * while unroll 4 (48 registers) ran every Qwen3 decode shape correctly. Cause UNKNOWN; the next |
| 1906 | * experiment is the unroll-4 kernel launched with 72 declared registers (one variable). */ |
| 1907 | /* ---- 2026-10-12: row/column-blocked fragment kernel (prefill rows) ------------------------ |
| 1908 | * |
| 1909 | * The kernel above gives one warp one 16x8 output tile: every B fragment is streamed once per row |
| 1910 | * tile and every A fragment once per column tile, so at m = 128 on GB10 the three Qwen3-8B prefill |
| 1911 | * shapes move about 4.7 GB per call at the DRAM rate (128x4096x24576: 20.9 ms, 2026-10-12 baseline). |
| 1912 | * Here one warp owns RT row tiles x CT column tiles (RT*CT accumulators) over all of K: per K step |
| 1913 | * it loads RT A fragments and CT B fragments and runs RT*CT HMMAs, so each loaded fragment feeds |
| 1914 | * RT (B) or CT (A) tensor ops. Same packing, grid rules and C layout as the kernel above: the A |
| 1915 | * fragments of row tile i sit at +i*KS*512 bytes, the B fragments of column tile j at +j*KS*256, |
| 1916 | * both immediate offsets of the same cursors; the C tile (i, j) is at +i*16*N*4 + j*32 bytes from |
| 1917 | * the two row pointers (STG.E immediate offsets, fixtures in omega_blackwell_verify_codegen_fixtures_fragmm). |
| 1918 | * Loads per pass stay at or under 8 (unroll*(RT+CT)), the count the proven unroll-4 kernel issues; |
| 1919 | * unroll 8 of that kernel (16 loads per pass) never completed on the GB10 and the cause is UNKNOWN, |
| 1920 | * so this kernel does not go past that envelope. The HMMA chain keeps the dependent-chain control |
| 1921 | * words (stall 15 then NOP stall 14) although the RT*CT accumulators are independent: a longer stall |
| 1922 | * is never wrong, and the loads dominate the pass. CTA = one warp, as above; a grid of |
| 1923 | * (grid_x, row blocks); column blocks advance by grid_x per pass of the column loop. */ |
| 1924 | int omega_blackwell_codegen_matmul_frag_block_prog(const OmegaMatMulSpec *spec, uint32_t grid_x, uint32_t unroll, |
| 1925 | uint32_t rt, uint32_t ct, int mutant, BlackwellIRProgram *prog) { |
| 1926 | if (!spec || !prog) return -1; |
| 1927 | if (spec->m == 0 || spec->k == 0 || spec->n == 0) return -1; |
| 1928 | if (spec->precision != OMEGA_MATMUL_PRECISION_FP16 && spec->precision != OMEGA_MATMUL_PRECISION_BF16) return -1; |
| 1929 | if (spec->m % 16 != 0 || spec->n % 8 != 0 || spec->k % 16 != 0) return -1; |
| 1930 | if (rt != 1 && rt != 2 && rt != 4 && rt != 8) return -1; |
| 1931 | if (ct != 1 && ct != 2 && ct != 4) return -1; |
| 1932 | if (rt * ct > 16 || rt * ct < 2) return -1; /* 1x1 is the kernel above */ |
| 1933 | if (unroll != 1 && unroll != 2) return -1; |
| 1934 | if (unroll * (rt + ct) > 8) return -1; |
| 1935 | const uint32_t K = spec->k, N = spec->n, NT = N / 8, KS = K / 16; |
| 1936 | if (KS % unroll != 0 || NT % ct != 0) return -1; |
| 1937 | const uint32_t ITERS = KS / unroll, NB = NT / ct; |
| 1938 | if (grid_x == 0 || grid_x > NB) return -1; |
| 1939 | if (mutant && ITERS < 2) return -1; |
| 1940 | /* every index and immediate below must fit: 32 bits for the index arithmetic, signed 24 bits for |
| 1941 | * the load/store offsets (rows up to OMEGA_BW_MATMUL_MAX_M) */ |
| 1942 | if ((uint64_t)NT * KS * 32u > 0xffffffffull || (uint64_t)(OMEGA_BW_MATMUL_MAX_M / 16u) * KS * 32u > 0xffffffffull || |
| 1943 | (uint64_t)grid_x * ct * KS * 256u > 0xffffffffull || (uint64_t)N * 32u > 0xffffffffull || |
| 1944 | (uint64_t)OMEGA_BW_MATMUL_MAX_M * N > 0xffffffffull) |
| 1945 | return -1; |
| 1946 | if ((uint64_t)(rt - 1) * KS * 512u + (unroll - 1) * 512u >= (1ull << 23) || |
| 1947 | (uint64_t)(ct - 1) * KS * 256u + (unroll - 1) * 256u >= (1ull << 23) || |
| 1948 | (uint64_t)(rt - 1) * 16u * N * 4u + (ct - 1) * 32u + 4u >= (1ull << 23)) |
| 1949 | return -1; |
| 1950 | |
| 1951 | omega_bw_ir_init(prog); |
| 1952 | 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); |
| 1953 | int uv_desc = omega_bw_ir_alloc_uvreg64(prog); |
| 1954 | 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); |
| 1955 | 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); |
| 1956 | 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); |
| 1957 | 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); |
| 1958 | int v_pa = omega_bw_ir_alloc_vreg64(prog), v_pbcol = omega_bw_ir_alloc_vreg64(prog); |
| 1959 | int v_pc0 = omega_bw_ir_alloc_vreg64(prog), v_pc1 = omega_bw_ir_alloc_vreg64(prog); |
| 1960 | int v_pac = omega_bw_ir_alloc_vreg64(prog), v_pbc = omega_bw_ir_alloc_vreg64(prog); |
| 1961 | int v_rd[16], v_ra[2][8], v_rb[2][4]; |
| 1962 | for (uint32_t t = 0; t < rt * ct; t++) v_rd[t] = omega_bw_ir_alloc_vreg128(prog); |
| 1963 | for (uint32_t u = 0; u < unroll; u++) { |
| 1964 | for (uint32_t i = 0; i < rt; i++) v_ra[u][i] = omega_bw_ir_alloc_vreg128(prog); |
| 1965 | for (uint32_t j = 0; j < ct; j++) v_rb[u][j] = omega_bw_ir_alloc_vreg64(prog); |
| 1966 | } |
| 1967 | 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; |
| 1968 | |
| 1969 | const uint32_t CTRL_STG_RDBAR3 = 0x0007e200u, CTRL_IMADW_WAIT3 = 0x009fcc00u, CTRL_MOVRZ_WAIT3 = 0x008fe200u; |
| 1970 | |
| 1971 | /* prologue: descriptor, pointers, ids (as the kernel above; a CTA's row block is rt row tiles) */ |
| 1972 | BW_APPEND(prog, .op = BW_IR_LDCU64, .dst_vreg = uv_desc, .imm = 0x358, .is_uniform = true); |
| 1973 | BW_APPEND(prog, .op = BW_IR_LDC64, .dst_vreg = v_ptr_a, .imm = 0x380); |
| 1974 | BW_APPEND(prog, .op = BW_IR_LDC64, .dst_vreg = v_ptr_b, .imm = 0x388); |
| 1975 | BW_APPEND(prog, .op = BW_IR_LDC64, .dst_vreg = v_ptr_c, .imm = 0x390); |
| 1976 | BW_APPEND(prog, .op = BW_IR_S2R, .dst_vreg = v_tid, .imm = BW_SR_TID_X); |
| 1977 | BW_APPEND(prog, .op = BW_IR_S2R, .dst_vreg = v_ctaid_x, .imm = BW_SR_CTAID_X); |
| 1978 | BW_APPEND(prog, .op = BW_IR_S2R, .dst_vreg = v_ctaid_y, .imm = BW_SR_CTAID_Y); |
| 1979 | BW_APPEND(prog, .op = BW_IR_MOV_IMM, .dst_vreg = v_c1, .imm = 1); |
| 1980 | BW_APPEND(prog, .op = BW_IR_SHF_R, .dst_vreg = v_group, .src1_vreg = v_tid, .imm = 2); |
| 1981 | BW_APPEND(prog, .op = BW_IR_LOP3_AND, .dst_vreg = v_tig, .src1_vreg = v_tid, .imm = 3); |
| 1982 | BW_APPEND(prog, .op = BW_IR_IMAD, .dst_vreg = v_tcol2, .src1_vreg = v_tig, .src2_vreg = -1, .imm = 2, .src3_vreg = -1); |
| 1983 | /* r0 = ctaid_y * 16 * rt + group: row of this lane in row tile 0 of the block; r1 = r0 + 8 */ |
| 1984 | 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); |
| 1985 | BW_APPEND(prog, .op = BW_IR_IMAD, .dst_vreg = v_r1, .src1_vreg = v_c1, .src2_vreg = -1, .imm = 8, .src3_vreg = v_r0); |
| 1986 | /* pa = A + (ctaid_y * rt * KS * 32 + tid) * 16: row tile 0 of this block, this lane's 16 bytes of step 0 */ |
| 1987 | 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); |
| 1988 | 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); |
| 1989 | /* col = ctaid_x * ct: first column tile of this CTA's first column block */ |
| 1990 | BW_APPEND(prog, .op = BW_IR_IMAD, .dst_vreg = v_col, .src1_vreg = v_ctaid_x, .src2_vreg = -1, .imm = ct, .src3_vreg = -1); |
| 1991 | /* pbcol = B + (col * KS * 32 + tid) * 8: column tile col, this lane's 8 bytes of step 0 */ |
| 1992 | 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); |
| 1993 | 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); |
| 1994 | /* pc0 = C + (r0*N + col*8 + tcol2)*4 ; pc1 with r1 */ |
| 1995 | BW_APPEND(prog, .op = BW_IR_IMAD, .dst_vreg = v_idx, .src1_vreg = v_r0, .src2_vreg = -1, .imm = N, .src3_vreg = v_tcol2); |
| 1996 | BW_APPEND(prog, .op = BW_IR_IMAD, .dst_vreg = v_idx, .src1_vreg = v_col, .src2_vreg = -1, .imm = 8, .src3_vreg = v_idx); |
| 1997 | 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); |
| 1998 | BW_APPEND(prog, .op = BW_IR_IMAD, .dst_vreg = v_idx, .src1_vreg = v_r1, .src2_vreg = -1, .imm = N, .src3_vreg = v_tcol2); |
| 1999 | BW_APPEND(prog, .op = BW_IR_IMAD, .dst_vreg = v_idx, .src1_vreg = v_col, .src2_vreg = -1, .imm = 8, .src3_vreg = v_idx); |
| 2000 | 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); |
| 2001 | |
| 2002 | /* ---- column-block loop: zero the rt*ct accumulators (the first MOV waits SB3: the previous |
| 2003 | * block's stores have read them), rewind the K cursors ---- */ |
| 2004 | const int L_col = (int)prog->count; |
| 2005 | for (uint32_t t = 0; t < rt * ct; t++) |
| 2006 | for (uint32_t s = 0; s < 4; s++) |
| 2007 | BW_APPEND(prog, .op = BW_IR_MOV_RZ, .dst_vreg = v_rd[t], .dst_subreg = (int)s, |
| 2008 | .control = (t == 0 && s == 0) ? CTRL_MOVRZ_WAIT3 : 0); |
| 2009 | 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); |
| 2010 | 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); |
| 2011 | BW_APPEND(prog, .op = BW_IR_MOV_IMM, .dst_vreg = v_cnt, .imm = mutant ? ITERS - 1 : ITERS); |
| 2012 | |
| 2013 | /* ---- K loop: `unroll` steps of (rt A + ct B) loads, then the rt*ct*unroll HMMAs ---- */ |
| 2014 | const int L_k = (int)prog->count; |
| 2015 | for (uint32_t u = 0; u < unroll; u++) { |
| 2016 | for (uint32_t i = 0; i < rt; i++) |
| 2017 | 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, |
| 2018 | .ureg = uv_desc, .imm = u * 512u + i * KS * 512u, .control = BW_FRAG_CTRL_LDG_A); |
| 2019 | for (uint32_t j = 0; j < ct; j++) |
| 2020 | 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, |
| 2021 | .ureg = uv_desc, .imm = u * 256u + j * KS * 256u, .control = BW_FRAG_CTRL_LDG_B); |
| 2022 | } |
| 2023 | const BlackwellIROpcode hmma = (spec->precision == OMEGA_MATMUL_PRECISION_BF16) ? BW_IR_HMMA_BF16 : BW_IR_HMMA_F16; |
| 2024 | for (uint32_t u = 0; u < unroll; u++) |
| 2025 | for (uint32_t i = 0; i < rt; i++) |
| 2026 | for (uint32_t j = 0; j < ct; j++) { |
| 2027 | const int rd = v_rd[i * ct + j]; |
| 2028 | BW_APPEND(prog, .op = hmma, .dst_vreg = rd, .src1_vreg = v_ra[u][i], .src2_vreg = v_rb[u][j], .src3_vreg = rd, |
| 2029 | .control = BW_FRAG_CTRL_HMMA); |
| 2030 | BW_APPEND(prog, .op = BW_IR_NOP, .dst_vreg = -1, .src1_vreg = -1, .src2_vreg = -1, .src3_vreg = -1, .ureg = -1, |
| 2031 | .control = BW_FRAG_CTRL_NOP); |
| 2032 | } |
| 2033 | 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); |
| 2034 | 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); |
| 2035 | BW_APPEND(prog, .op = BW_IR_IMAD, .dst_vreg = v_cnt, .src1_vreg = v_c1, .src2_vreg = -1, .imm = 0xffffffffu, .src3_vreg = v_cnt); |
| 2036 | BW_APPEND(prog, .op = BW_IR_ISETP_GE, .dst_vreg = -1, .src1_vreg = v_cnt, .src2_vreg = -1, .imm = 1, .src3_vreg = -1); |
| 2037 | { |
| 2038 | int here = (int)prog->count; |
| 2039 | BW_APPEND(prog, .op = BW_IR_BRA, .imm = (uint32_t)(int32_t)(L_k - here), .predicate_p0 = true); |
| 2040 | } |
| 2041 | const int L_k_end = (int)prog->count - 1; |
| 2042 | |
| 2043 | /* ---- store the rt*ct tiles: tile (i, j) at +i*16*N*4 + j*32 bytes from pc0 (rows r0) and pc1 (rows r1) ---- */ |
| 2044 | for (uint32_t i = 0; i < rt; i++) |
| 2045 | for (uint32_t j = 0; j < ct; j++) { |
| 2046 | const int rd = v_rd[i * ct + j]; |
| 2047 | const uint32_t off = i * 16u * N * 4u + j * 32u; |
| 2048 | 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); |
| 2049 | 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); |
| 2050 | 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); |
| 2051 | 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); |
| 2052 | } |
| 2053 | /* 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 */ |
| 2054 | 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); |
| 2055 | 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); |
| 2056 | 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); |
| 2057 | 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); |
| 2058 | BW_APPEND(prog, .op = BW_IR_ISETP_GE, .dst_vreg = -1, .src1_vreg = v_col, .src2_vreg = -1, .imm = NT, .src3_vreg = -1); |
| 2059 | { |
| 2060 | int here = (int)prog->count; |
| 2061 | BW_APPEND(prog, .op = BW_IR_BRA, .imm = (uint32_t)(int32_t)(L_col - here), .predicate_p0 = true, .predicate_not = true); |
| 2062 | } |
| 2063 | const int L_col_end = (int)prog->count - 1; |
| 2064 | BW_APPEND(prog, .op = BW_IR_EXIT); |
| 2065 | BW_APPEND(prog, .op = BW_IR_BRA); |
| 2066 | |
| 2067 | /* loop-carried values live to the end of their loop (as the kernel above) */ |
| 2068 | const int col_carried[] = { v_c1, v_col, v_pa, v_pbcol, v_pc0, v_pc1 }; |
| 2069 | for (size_t i = 0; i < sizeof col_carried / sizeof col_carried[0]; i++) bw_live_until(prog, col_carried[i], L_col_end); |
| 2070 | for (uint32_t t = 0; t < rt * ct; t++) bw_live_until(prog, v_rd[t], L_col_end); |
| 2071 | const int k_carried[] = { v_pac, v_pbc, v_cnt }; |
| 2072 | for (size_t i = 0; i < sizeof k_carried / sizeof k_carried[0]; i++) bw_live_until(prog, k_carried[i], L_k_end); |
| 2073 | bw_keep_alive(prog, L_k, L_k_end, L_k_end); |
| 2074 | return omega_bw_regalloc_solve(prog); |
| 2075 | } |
| 2076 | |
| 2077 | #ifndef OMEGA_GPU_NO_POSIX |
| 2078 | int omega_blackwell_codegen_matmul_frag_block(const OmegaMatMulSpec *spec, uint32_t grid_x, uint32_t unroll, |
| 2079 | uint32_t rt, uint32_t ct, int mutant, OmegaBlackwellKernel *kernel) { |
| 2080 | if (!spec || !kernel) return -1; |
| 2081 | BlackwellIRProgram prog; |
| 2082 | if (omega_blackwell_codegen_matmul_frag_block_prog(spec, grid_x, unroll, rt, ct, mutant, &prog) != 0) return -1; |
| 2083 | return bw_kernel_alloc_encode(&prog, OMEGA_BW_CODE_CAP_MAX, kernel); |
| 2084 | } |
| 2085 | #endif |
| 2086 | |
| 2087 | /* The row and column tiles one warp owns in the blocked kernel: the largest of 4, 2, 1 dividing the |
| 2088 | * row tiles (rt), and for rt > 1 the largest of 4, 2, 1 dividing the column tiles (ct); rt = ct = 1 |
| 2089 | * is the single-tile kernel (decode rows). */ |
| 2090 | void omega_matmul_frag_block_shape(uint32_t mt, uint32_t nt, uint32_t *rt, uint32_t *ct) { |
| 2091 | uint32_t r = mt % 4u == 0 ? 4u : mt % 2u == 0 ? 2u : 1u; |
| 2092 | *rt = r; |
| 2093 | *ct = r > 1 ? (nt % 4u == 0 ? 4u : nt % 2u == 0 ? 2u : 1u) : 1u; |
| 2094 | } |
| 2095 | |
| 2096 | /* loads per pass at or under 8 (see the kernel comment): unroll 2 only when 2*(rt+ct) fits and KS is even */ |
| 2097 | uint32_t omega_matmul_frag_block_unroll(uint32_t kp, uint32_t rt, uint32_t ct) { |
| 2098 | const uint32_t ks = kp / 16u; |
| 2099 | return (ks % 2u == 0 && 2u * (rt + ct) <= 8u) ? 2u : 1u; |
| 2100 | } |
| 2101 | |
| 2102 | uint32_t omega_matmul_frag_unroll(uint32_t kp) { |
| 2103 | const uint32_t ks = kp / 16u; |
| 2104 | return ks % 4u == 0 ? 4u : ks % 2u == 0 ? 2u : 1u; |
| 2105 | } |
| 2106 | |
| 2107 | /* A block (mt, ks) holds rows 16mt..16mt+15 and columns 16ks..16ks+15 as 256 bf16 at element |
| 2108 | * (mt*KS + ks)*256; inside the block, element (rr, cc) sits at lane*8 + e with lane = (rr%8)*4 + (cc%8)/2 |
| 2109 | * and e = (cc/8)*4 + (rr/8)*2 + cc%2 (the HMMA.16816 A fragment order, header comment). The 256-entry |
| 2110 | * table makes the pack a tile loop with no per-element division (2026-10-12: the per-element form with |
| 2111 | * six divisions and a conversion call per element was ~2.5 ms per MB of activations at m = 128). */ |
| 2112 | static uint16_t g_frag_a_tab[256]; |
| 2113 | static void frag_a_tab_init(void) { |
| 2114 | if (g_frag_a_tab[255]) return; |
| 2115 | for (uint32_t rr = 0; rr < 16; rr++) |
| 2116 | for (uint32_t cc = 0; cc < 16; cc++) |
| 2117 | g_frag_a_tab[rr * 16 + cc] = (uint16_t)((((rr & 7u) * 4u + ((cc & 7u) >> 1)) * 8u) + (cc >> 3) * 4u + (rr >> 3) * 2u + (cc & 1u)); |
| 2118 | } |
| 2119 | /* bit for bit what the compiler's (__bf16)f cast gives (gpu_matmul_api_test checks it against |
| 2120 | * omega_fp32_to_bf16 on ties, denormals, inf and NaN payloads): round to nearest even, a NaN keeps |
| 2121 | * its top payload bits and is made quiet. gcc 13 on aarch64 emits the cast as a call to the |
| 2122 | * soft-float routine __truncsfbf2 per element, which was most of the packing cost. */ |
| 2123 | static inline uint16_t frag_f32_to_bf16(float f) { |
| 2124 | uint32_t u; |
| 2125 | memcpy(&u, &f, 4); |
| 2126 | if ((u & 0x7f800000u) == 0x7f800000u) return (uint16_t)((u >> 16) | ((u & 0x007fffffu) ? 0x0040u : 0u)); |
| 2127 | return (uint16_t)((u + 0x7fffu + ((u >> 16) & 1u)) >> 16); |
| 2128 | } |
| 2129 | #define FRAG_PACK_A_BODY(LOAD) \ |
| 2130 | do { \ |
| 2131 | const uint32_t KS = kp / 16u; \ |
| 2132 | frag_a_tab_init(); \ |
| 2133 | memset(dst, 0, (size_t)mp * kp * sizeof *dst); \ |
| 2134 | for (uint32_t mt = 0; mt < (m + 15u) / 16u; mt++) { \ |
| 2135 | const uint32_t r1 = m - mt * 16u < 16u ? m - mt * 16u : 16u; \ |
| 2136 | for (uint32_t ks = 0; ks < (k + 15u) / 16u; ks++) { \ |
| 2137 | const uint32_t c1 = k - ks * 16u < 16u ? k - ks * 16u : 16u; \ |
| 2138 | uint16_t *blk = dst + ((size_t)mt * KS + ks) * 256u; \ |
| 2139 | for (uint32_t rr = 0; rr < r1; rr++) { \ |
| 2140 | const uint16_t *tab = g_frag_a_tab + rr * 16u; \ |
| 2141 | const size_t src = (size_t)(mt * 16u + rr) * k + ks * 16u; \ |
| 2142 | for (uint32_t cc = 0; cc < c1; cc++) blk[tab[cc]] = LOAD(a[src + cc]); \ |
| 2143 | } \ |
| 2144 | } \ |
| 2145 | } \ |
| 2146 | } while (0) |
| 2147 | #define FRAG_ID(x) (x) |
| 2148 | 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) { |
| 2149 | FRAG_PACK_A_BODY(FRAG_ID); |
| 2150 | } |
| 2151 | 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) { |
| 2152 | FRAG_PACK_A_BODY(frag_f32_to_bf16); |
| 2153 | } |
| 2154 | |
| 2155 | /* Row 0 only, every column of every K block (zeros past k): for a one-row call whose buffer already |
| 2156 | * holds zeros in rows 1..15 of every block from an earlier full pack of the same width. */ |
| 2157 | #define FRAG_PACK_A_ROW0_BODY(LOAD) \ |
| 2158 | do { \ |
| 2159 | const uint32_t KS = kp / 16u; \ |
| 2160 | frag_a_tab_init(); \ |
| 2161 | const uint16_t *tab = g_frag_a_tab; \ |
| 2162 | for (uint32_t ks = 0; ks < KS; ks++) { \ |
| 2163 | uint16_t *blk = dst + (size_t)ks * 256u; \ |
| 2164 | const uint32_t c0 = ks * 16u, c1 = k - c0 < 16u ? k - c0 : 16u; \ |
| 2165 | for (uint32_t cc = 0; cc < c1; cc++) blk[tab[cc]] = LOAD(a[c0 + cc]); \ |
| 2166 | for (uint32_t cc = c1; cc < 16u; cc++) blk[tab[cc]] = 0; \ |
| 2167 | } \ |
| 2168 | } while (0) |
| 2169 | void omega_matmul_frag_pack_a_row0(const uint16_t *a, uint32_t k, uint32_t kp, uint16_t *dst) { |
| 2170 | FRAG_PACK_A_ROW0_BODY(FRAG_ID); |
| 2171 | } |
| 2172 | size_t omega_matmul_result_sentinels(float *cd, uint32_t m, uint32_t n, uint32_t np, float poison, int set) { |
| 2173 | /* one word per 16 x 8 fragment tile that holds a real output word: the tile's last real row and column */ |
| 2174 | size_t count = 0; |
| 2175 | if (m == 0 || n == 0) return 0; |
| 2176 | const uint32_t mt = (m + 15u) / 16u, nt = (n + 7u) / 8u; |
| 2177 | for (uint32_t ti = 0; ti < mt; ti++) { |
| 2178 | uint32_t r = ti * 16u + 15u; if (r >= m) r = m - 1u; |
| 2179 | float *row = cd + (size_t)r * np; |
| 2180 | for (uint32_t tj = 0; tj < nt; tj++) { |
| 2181 | uint32_t c = tj * 8u + 7u; if (c >= n) c = n - 1u; |
| 2182 | if (set) { row[c] = poison; count++; } |
| 2183 | else if (row[c] == poison) count++; |
| 2184 | } |
| 2185 | } |
| 2186 | return count; |
| 2187 | } |
| 2188 | |
| 2189 | void omega_matmul_frag_pack_a_row0_f32(const float *a, uint32_t k, uint32_t kp, uint16_t *dst) { |
| 2190 | FRAG_PACK_A_ROW0_BODY(frag_f32_to_bf16); |
| 2191 | } |
| 2192 | |
| 2193 | void omega_matmul_frag_pack_b(const uint16_t *b, uint32_t k, uint32_t n, uint32_t kp, |
| 2194 | uint32_t nt0, uint32_t nt1, uint16_t *dst) { |
| 2195 | const uint32_t KS = kp / 16u; |
| 2196 | memset(dst, 0, (size_t)(nt1 - nt0) * KS * 128u * sizeof *dst); |
| 2197 | for (uint32_t row = 0; row < k; row++) { |
| 2198 | const uint32_t ks = row / 16u, rr = row % 16u, khi = rr / 8u, tig = (rr % 8u) / 2u, lo = rr % 2u; |
| 2199 | const uint16_t *src = &b[(size_t)row * n]; |
| 2200 | for (uint32_t nt = nt0; nt < nt1; nt++) { |
| 2201 | const uint32_t c0 = nt * 8u; |
| 2202 | for (uint32_t group = 0; group < 8u && c0 + group < n; group++) { |
| 2203 | const size_t lane = group * 4u + tig; |
| 2204 | dst[(((size_t)(nt - nt0) * KS + ks) * 32u + lane) * 4u + khi * 2u + lo] = src[c0 + group]; |
| 2205 | } |
| 2206 | } |
| 2207 | } |
| 2208 | } |
| 2209 | |
| 2210 | int omega_blackwell_verify_codegen_fixtures(void) { |
| 2211 | OmegaRegAlloc ra; |
| 2212 | memset(&ra, 0, sizeof(ra)); |
| 2213 | ra.vreg_to_phys[0] = 2; |
| 2214 | ra.vreg_to_phys[1] = 4; |
| 2215 | ra.vreg_to_phys[2] = 6; |
| 2216 | ra.vreg_to_phys[3] = 7; |
| 2217 | ra.uvreg_to_phys[0] = 4; |
| 2218 | |
| 2219 | uint32_t w[4]; |
| 2220 | |
| 2221 | /* 1. MOV R7, RZ */ |
| 2222 | BlackwellIRInsn insn_mov_rz = { .op = BW_IR_MOV_RZ, .dst_vreg = 3 }; |
| 2223 | if (encode_single_insn(&insn_mov_rz, &ra, w) != 0) return -1; |
| 2224 | if (w[0] != (0x7202 | (7 << 16)) || w[1] != 0xff || w[2] != 0x00000f00) return -2; |
| 2225 | |
| 2226 | /* 2. MOV R2, 1 */ |
| 2227 | BlackwellIRInsn insn_mov_imm = { .op = BW_IR_MOV_IMM, .dst_vreg = 0, .imm = 1 }; |
| 2228 | if (encode_single_insn(&insn_mov_imm, &ra, w) != 0) return -3; |
| 2229 | if (w[0] != (0x7802 | (2 << 16)) || w[1] != 1 || w[2] != 0x00000f00) return -4; |
| 2230 | |
| 2231 | /* 3. Register-Register IMAD: IMAD R7, R2, R6, R7 */ |
| 2232 | BlackwellIRInsn insn_imad_rr = { .op = BW_IR_IMAD, .dst_vreg = 3, .src1_vreg = 0, .src2_vreg = 2, .src3_vreg = 3 }; |
| 2233 | if (encode_single_insn(&insn_imad_rr, &ra, w) != 0) return -5; |
| 2234 | if (w[0] != (0x7224 | (7 << 16) | (2 << 24)) || w[1] != 6 || w[2] != (0x078e0200 | 7)) return -6; |
| 2235 | |
| 2236 | /* 4. Immediate IMAD: IMAD R6, R2, 16, RZ */ |
| 2237 | BlackwellIRInsn insn_imad_imm = { .op = BW_IR_IMAD, .dst_vreg = 2, .src1_vreg = 0, .src2_vreg = -1, .imm = 16, .src3_vreg = -1 }; |
| 2238 | if (encode_single_insn(&insn_imad_imm, &ra, w) != 0) return -7; |
| 2239 | if (w[0] != (0x7824 | (6 << 16) | (2 << 24)) || w[1] != 16 || w[2] != 0x078e02ff) return -8; |
| 2240 | |
| 2241 | /* 5. IMAD.WIDE: IMAD.WIDE R4, R2, 4, R4 */ |
| 2242 | BlackwellIRInsn insn_imad_wide = { .op = BW_IR_IMAD_WIDE, .dst_vreg = 1, .src1_vreg = 0, .imm = 4, .src3_vreg = 1 }; |
| 2243 | if (encode_single_insn(&insn_imad_wide, &ra, w) != 0) return -9; |
| 2244 | if (w[0] != (0x7825 | (4 << 16) | (2 << 24)) || w[1] != 4 || w[2] != (0x078e0000 | 4)) return -10; |
| 2245 | |
| 2246 | /* 6. LDC.64 R4, c[0x0][0x380] */ |
| 2247 | BlackwellIRInsn insn_ldc64 = { .op = BW_IR_LDC64, .dst_vreg = 1, .imm = 0x380 }; |
| 2248 | if (encode_single_insn(&insn_ldc64, &ra, w) != 0) return -11; |
| 2249 | if (w[0] != (0xff007b82 | (4 << 16)) || w[1] != ((0x380 << 6) & 0x00ffff00) || w[2] != 0x00000a00) return -12; |
| 2250 | |
| 2251 | /* 7. LDCU.64 UR4, c[0x0][0x358] */ |
| 2252 | BlackwellIRInsn insn_ldcu64 = { .op = BW_IR_LDCU64, .dst_vreg = 0, .imm = 0x358, .is_uniform = true }; |
| 2253 | if (encode_single_insn(&insn_ldcu64, &ra, w) != 0) return -13; |
| 2254 | if (w[0] != (0xff0077ac | (4 << 16)) || w[1] != ((0x358 << 5) & 0x00ffff00) || w[2] != 0x08000a00) return -14; |
| 2255 | |
| 2256 | /* 8. LDG.E R2, desc[UR4][R4.64] */ |
| 2257 | BlackwellIRInsn insn_ldg = { .op = BW_IR_LDG_E, .dst_vreg = 0, .src1_vreg = 1, .ureg = 0 }; |
| 2258 | if (encode_single_insn(&insn_ldg, &ra, w) != 0) return -15; |
| 2259 | if (w[0] != (0x7981 | (2 << 16) | (4 << 24)) || w[1] != 4 || w[2] != 0x0c1e1900) return -16; |
| 2260 | |
| 2261 | /* 9. STG.E desc[UR4][R4.64], R7 */ |
| 2262 | BlackwellIRInsn insn_stg = { .op = BW_IR_STG_E, .src1_vreg = 1, .src2_vreg = 3, .ureg = 0 }; |
| 2263 | if (encode_single_insn(&insn_stg, &ra, w) != 0) return -17; |
| 2264 | if (w[0] != (0x7986 | (4 << 24)) || w[1] != 7 || w[2] != (0x0c101900 | 4)) return -18; |
| 2265 | |
| 2266 | /* 10. S2R R2, SR_CTAID.X */ |
| 2267 | BlackwellIRInsn insn_s2r = { .op = BW_IR_S2R, .dst_vreg = 0, .imm = BW_SR_CTAID_X }; |
| 2268 | if (encode_single_insn(&insn_s2r, &ra, w) != 0) return -19; |
| 2269 | if (w[0] != (0x7919 | (2 << 16)) || w[2] != (BW_SR_CTAID_X << 8)) return -20; |
| 2270 | |
| 2271 | /* 11. HMMA.16816.F32 R4, R4, R2, RZ */ |
| 2272 | BlackwellIRInsn insn_hmma_f16 = { .op = BW_IR_HMMA_F16, .dst_vreg = 1, .src1_vreg = 1, .src2_vreg = 0, .src3_vreg = -1 }; |
| 2273 | if (encode_single_insn(&insn_hmma_f16, &ra, w) != 0) return -21; |
| 2274 | if (w[0] != 0x0404723c || w[1] != 0x00000002 || w[2] != 0x000018ff || w[3] != 0x000fe200) return -22; |
| 2275 | |
| 2276 | /* 12. HMMA.16816.F32.BF16 R4, R4, R2, RZ */ |
| 2277 | BlackwellIRInsn insn_hmma_bf16 = { .op = BW_IR_HMMA_BF16, .dst_vreg = 1, .src1_vreg = 1, .src2_vreg = 0, .src3_vreg = -1 }; |
| 2278 | uint32_t w_bf16[4]; |
| 2279 | if (encode_single_insn(&insn_hmma_bf16, &ra, w_bf16) != 0) return -23; |
| 2280 | if (w_bf16[0] != 0x0404723c || w_bf16[1] != 0x00000002 || w_bf16[2] != 0x000418ff || w_bf16[3] != 0x000fe200) return -24; |
| 2281 | |
| 2282 | /* 13. Differential Invariant: w[2] bit 18 toggles strictly between FP16 and BF16 */ |
| 2283 | if ((w[2] ^ w_bf16[2]) != 0x00040000) return -25; |
| 2284 | if (w[0] != w_bf16[0] || w[1] != w_bf16[1] || w[3] != w_bf16[3]) return -26; |
| 2285 | |
| 2286 | /* 14. SHF.R.U32.HI R4, RZ, 2, R2 */ |
| 2287 | BlackwellIRInsn insn_shf = { .op = BW_IR_SHF_R, .dst_vreg = 1, .src1_vreg = 0, .imm = 2 }; |
| 2288 | if (encode_single_insn(&insn_shf, &ra, w) != 0) return -27; |
| 2289 | if (w[0] != 0xff047819 || w[1] != 2 || w[2] != 0x00011602 || w[3] != 0x001fca00) return -28; |
| 2290 | |
| 2291 | /* 15. LOP3.LUT R4, R2, 3, RZ, 0xc0, !PT */ |
| 2292 | BlackwellIRInsn insn_lop3 = { .op = BW_IR_LOP3_AND, .dst_vreg = 1, .src1_vreg = 0, .imm = 3 }; |
| 2293 | if (encode_single_insn(&insn_lop3, &ra, w) != 0) return -29; |
| 2294 | if (w[0] != 0x02047812 || w[1] != 3 || w[2] != 0x078ec0ff || w[3] != 0x001fca00) return -30; |
| 2295 | |
| 2296 | /* 16. LDG.E.U16 R2, desc[UR4][R4.64] */ |
| 2297 | BlackwellIRInsn insn_ldg16 = { .op = BW_IR_LDG_E_U16, .dst_vreg = 0, .src1_vreg = 1, .ureg = 0 }; |
| 2298 | if (encode_single_insn(&insn_ldg16, &ra, w) != 0) return -31; |
| 2299 | if (w[0] != 0x04027981 || w[1] != 4 || w[2] != 0x0c1e1500 || w[3] != 0x000f2200) return -32; |
| 2300 | |
| 2301 | /* A zero-delta branch stays the self-branch the matmul kernels already emit. */ |
| 2302 | BlackwellIRInsn insn_bra0 = { .op = BW_IR_BRA }; |
| 2303 | if (encode_single_insn(&insn_bra0, &ra, w) != 0) return -33; |
| 2304 | if (w[0] != 0x00fc7947 || w[1] != 0xfffffffc || w[2] != 0x0383ffff || w[3] != 0x000fc000) return -34; |
| 2305 | |
| 2306 | BlackwellIRInsn insn_ge = { .op = BW_IR_ISETP_GE_U32, .src1_vreg = 1, .src2_vreg = 2 }; |
| 2307 | if (encode_single_insn(&insn_ge, &ra, w) != 0) return -35; |
| 2308 | if (w[0] != (0x720c | (4 << 24)) || w[1] != 6 || w[2] != 0x03f06070) return -36; |
| 2309 | |
| 2310 | BlackwellIRInsn insn_xor = { .op = BW_IR_LOP3_XOR, .dst_vreg = 1, .src1_vreg = 0, .src2_vreg = 2 }; |
| 2311 | if (encode_single_insn(&insn_xor, &ra, w) != 0) return -37; |
| 2312 | if (w[0] != (0x7212 | (4 << 16) | (2 << 24)) || w[1] != 6 || w[2] != 0x078e3cff) return -38; |
| 2313 | |
| 2314 | BlackwellIRInsn insn_ldg_ss = { .op = BW_IR_LDG_STRONG_SYS, .dst_vreg = 0, .src1_vreg = 1, .ureg = 0 }; |
| 2315 | if (encode_single_insn(&insn_ldg_ss, &ra, w) != 0) return -39; |
| 2316 | if (w[0] != (0x7981 | (2 << 16) | (4 << 24)) || w[1] != 4 || w[2] != 0x0c1f5900) return -40; |
| 2317 | /* 17. FADD R7, R2, R4 */ |
| 2318 | BlackwellIRInsn insn_fadd = { .op = BW_IR_FADD, .dst_vreg = 3, .src1_vreg = 0, .src2_vreg = 1 }; |
| 2319 | if (encode_single_insn(&insn_fadd, &ra, w) != 0) return -133; |
| 2320 | if (w[0] != 0x02077221 || w[1] != 4 || w[2] != 0 || w[3] != 0x004fc400) return -134; |
| 2321 | |
| 2322 | /* 18. FSUB R7, R2, R4 (negated src2) */ |
| 2323 | BlackwellIRInsn insn_fsub = { .op = BW_IR_FSUB, .dst_vreg = 3, .src1_vreg = 0, .src2_vreg = 1 }; |
| 2324 | if (encode_single_insn(&insn_fsub, &ra, w) != 0) return -135; |
| 2325 | if (w[0] != 0x02077221 || w[1] != 0x80000004 || w[2] != 0 || w[3] != 0x004fc400) return -136; |
| 2326 | |
| 2327 | /* 19. FMUL R7, R2, R4 */ |
| 2328 | BlackwellIRInsn insn_fmul = { .op = BW_IR_FMUL, .dst_vreg = 3, .src1_vreg = 0, .src2_vreg = 1 }; |
| 2329 | if (encode_single_insn(&insn_fmul, &ra, w) != 0) return -137; |
| 2330 | if (w[0] != 0x02077220 || w[1] != 4 || w[2] != 0x00400000 || w[3] != 0x004fc400) return -138; |
| 2331 | |
| 2332 | /* 20. FFMA R7, R2, R4, R6 */ |
| 2333 | BlackwellIRInsn insn_ffma = { .op = BW_IR_FFMA, .dst_vreg = 3, .src1_vreg = 0, .src2_vreg = 1, .src3_vreg = 2 }; |
| 2334 | if (encode_single_insn(&insn_ffma, &ra, w) != 0) return -139; |
| 2335 | if (w[0] != 0x02077223 || w[1] != 4 || w[2] != 6 || w[3] != 0x004fc400) return -140; |
| 2336 | |
| 2337 | /* 21. FMNMX_MIN R7, R2, R4 */ |
| 2338 | BlackwellIRInsn insn_min = { .op = BW_IR_FMNMX_MIN, .dst_vreg = 3, .src1_vreg = 0, .src2_vreg = 1 }; |
| 2339 | if (encode_single_insn(&insn_min, &ra, w) != 0) return -141; |
| 2340 | if (w[0] != 0x02077209 || w[1] != 4 || w[2] != 0x03800000 || w[3] != 0x004fc400) return -142; |
| 2341 | |
| 2342 | /* 22. FMNMX_MAX R7, R2, R4 */ |
| 2343 | BlackwellIRInsn insn_max = { .op = BW_IR_FMNMX_MAX, .dst_vreg = 3, .src1_vreg = 0, .src2_vreg = 1 }; |
| 2344 | if (encode_single_insn(&insn_max, &ra, w) != 0) return -143; |
| 2345 | if (w[0] != 0x02077209 || w[1] != 4 || w[2] != 0x07800000 || w[3] != 0x004fc400) return -144; |
| 2346 | |
| 2347 | /* 23. I2FP R7, R2 */ |
| 2348 | BlackwellIRInsn insn_i2f = { .op = BW_IR_I2FP, .dst_vreg = 3, .src1_vreg = 0 }; |
| 2349 | if (encode_single_insn(&insn_i2f, &ra, w) != 0) return -145; |
| 2350 | if (w[0] != 0x00077245 || w[1] != 2 || w[2] != 0x00201400 || w[3] != 0x004fe200) return -146; |
| 2351 | |
| 2352 | /* 24. F2I R7, R2 */ |
| 2353 | BlackwellIRInsn insn_f2i = { .op = BW_IR_F2I, .dst_vreg = 3, .src1_vreg = 0 }; |
| 2354 | if (encode_single_insn(&insn_f2i, &ra, w) != 0) return -147; |
| 2355 | if (w[0] != 0x00077305 || w[1] != 2 || w[2] != 0x0020f100 || w[3] != 0x004e2200) return -148; |
| 2356 | |
| 2357 | /* 25. MUFU_RCP R7, R2 */ |
| 2358 | BlackwellIRInsn insn_mufu_rcp = { .op = BW_IR_MUFU_RCP, .dst_vreg = 3, .src1_vreg = 0 }; |
| 2359 | if (encode_single_insn(&insn_mufu_rcp, &ra, w) != 0) return -149; |
| 2360 | if (w[0] != 0x00077308 || w[1] != 2 || w[2] != 0x00001000 || w[3] != 0x000e2400) return -150; |
| 2361 | |
| 2362 | /* 26. MUFU_RSQ R7, R2 */ |
| 2363 | BlackwellIRInsn insn_mufu_rsq = { .op = BW_IR_MUFU_RSQ, .dst_vreg = 3, .src1_vreg = 0 }; |
| 2364 | if (encode_single_insn(&insn_mufu_rsq, &ra, w) != 0) return -151; |
| 2365 | if (w[0] != 0x00077308 || w[1] != 2 || w[2] != 0x00001400 || w[3] != 0x000e2400) return -152; |
| 2366 | |
| 2367 | /* 27. SHFL_DOWN R7, R2, 16 */ |
| 2368 | BlackwellIRInsn insn_shfl = { .op = BW_IR_SHFL_DOWN, .dst_vreg = 3, .src1_vreg = 0, .imm = 16 }; |
| 2369 | if (encode_single_insn(&insn_shfl, &ra, w) != 0) return -153; |
| 2370 | if (w[0] != 0x02077f89 || w[1] != 0x0a001f00 || w[2] != 0x000e0000 || w[3] != 0x000e2400) return -154; |
| 2371 | |
| 2372 | /* 28. LDS R7, [R2] */ |
| 2373 | BlackwellIRInsn insn_lds = { .op = BW_IR_LDS, .dst_vreg = 3, .src1_vreg = 0, .imm = 0 }; |
| 2374 | if (encode_single_insn(&insn_lds, &ra, w) != 0) return -155; |
| 2375 | if (w[0] != 0x02077984 || w[1] != 0 || w[2] != 0 || w[3] != 0x000fe200) return -156; |
| 2376 | |
| 2377 | /* 29. STS [R2], R4 */ |
| 2378 | BlackwellIRInsn insn_sts = { .op = BW_IR_STS, .src1_vreg = 0, .src2_vreg = 1, .imm = 0 }; |
| 2379 | if (encode_single_insn(&insn_sts, &ra, w) != 0) return -157; |
| 2380 | if (w[0] != 0x02007388 || w[1] != 4 || w[2] != 0 || w[3] != 0x000fe200) return -158; |
| 2381 | |
| 2382 | return 0; |
| 2383 | } |
| 2384 | |
| 2385 | int omega_blackwell_test_regalloc_bounds(void) { |
| 2386 | BlackwellIRProgram prog; |
| 2387 | omega_bw_ir_init(&prog); |
| 2388 | |
| 2389 | int v0 = omega_bw_ir_alloc_vreg64(&prog); |
| 2390 | int v1 = omega_bw_ir_alloc_vreg(&prog); |
| 2391 | int v2 = omega_bw_ir_alloc_vreg64(&prog); |
| 2392 | int v3 = omega_bw_ir_alloc_vreg128(&prog); /* Test 128-bit quad allocation */ |
| 2393 | |
| 2394 | BlackwellIRInsn i0 = { .op = BW_IR_LDC64, .dst_vreg = v0, .imm = 0x380 }; |
| 2395 | omega_bw_ir_append(&prog, &i0); |
| 2396 | BlackwellIRInsn i1 = { .op = BW_IR_MOV_IMM, .dst_vreg = v1, .imm = 10 }; |
| 2397 | omega_bw_ir_append(&prog, &i1); |
| 2398 | BlackwellIRInsn i2 = { .op = BW_IR_LDC64, .dst_vreg = v2, .imm = 0x388 }; |
| 2399 | omega_bw_ir_append(&prog, &i2); |
| 2400 | BlackwellIRInsn i3 = { .op = BW_IR_HMMA_F16, .dst_vreg = v3, .src1_vreg = v3, .src2_vreg = v0, .src3_vreg = -1 }; |
| 2401 | omega_bw_ir_append(&prog, &i3); |
| 2402 | /* i4 uses v1, v2, v3 simultaneously so all intervals are concurrently active */ |
| 2403 | int v_sink = omega_bw_ir_alloc_vreg(&prog); |
| 2404 | BlackwellIRInsn i4 = { .op = BW_IR_IADD3, .dst_vreg = v_sink, .src1_vreg = v1, .src2_vreg = v2, .src3_vreg = v3 }; |
| 2405 | omega_bw_ir_append(&prog, &i4); |
| 2406 | |
| 2407 | if (omega_bw_regalloc_solve(&prog) != 0) return -1; |
| 2408 | |
| 2409 | int phys_v0 = prog.regalloc.vreg_to_phys[v0]; |
| 2410 | int phys_v1 = prog.regalloc.vreg_to_phys[v1]; |
| 2411 | int phys_v2 = prog.regalloc.vreg_to_phys[v2]; |
| 2412 | int phys_v3 = prog.regalloc.vreg_to_phys[v3]; |
| 2413 | |
| 2414 | if (phys_v0 % 2 != 0) return -2; |
| 2415 | if (phys_v2 % 2 != 0) return -3; |
| 2416 | if (phys_v3 % 4 != 0) return -4; /* 4-alignment for 128-bit quad */ |
| 2417 | if (phys_v1 == phys_v0 || phys_v1 == phys_v0 + 1) return -5; |
| 2418 | if (phys_v2 == phys_v0 || phys_v2 == phys_v0 + 1) return -6; |
| 2419 | for (int k = 0; k < 4; k++) { |
| 2420 | if (phys_v3 + k == phys_v0 || phys_v3 + k == phys_v0 + 1) return -7; |
| 2421 | if (phys_v3 + k == phys_v2 || phys_v3 + k == phys_v2 + 1) return -8; |
| 2422 | if (phys_v3 + k == phys_v1) return -9; |
| 2423 | } |
| 2424 | |
| 2425 | /* Test bounds refusal: intentionally attempt to allocate more registers than hardware capacity */ |
| 2426 | BlackwellIRProgram overflow_prog; |
| 2427 | omega_bw_ir_init(&overflow_prog); |
| 2428 | for (int i = 0; i < BW_PHYS_GPR_MAX + 5; i++) { |
| 2429 | int v = omega_bw_ir_alloc_vreg(&overflow_prog); |
| 2430 | if (v < 0) break; |
| 2431 | BlackwellIRInsn insn = { .op = BW_IR_MOV_IMM, .dst_vreg = v, .imm = (uint32_t)i }; |
| 2432 | omega_bw_ir_append(&overflow_prog, &insn); |
| 2433 | } |
| 2434 | for (size_t i = 0; i < overflow_prog.count; i++) { |
| 2435 | for (int v = 0; v < overflow_prog.regalloc.num_vregs; v++) { |
| 2436 | overflow_prog.regalloc.intervals[v].last_use = (int)overflow_prog.count - 1; |
| 2437 | } |
| 2438 | } |
| 2439 | if (omega_bw_regalloc_solve(&overflow_prog) == 0) { |
| 2440 | return -6; |
| 2441 | } |
| 2442 | |
| 2443 | return 0; |
| 2444 | } |
| 2445 | |
| 2446 | #ifndef OMEGA_GPU_NO_POSIX /* prints: hosted only (the freestanding build has no stdio) */ |
| 2447 | int omega_blackwell_test_codegen_variation(void) { |
| 2448 | OmegaMatMulSpec spec1, spec2; |
| 2449 | if (omega_matmul_spec_init(&spec1, 16, 16, 16, OMEGA_MATMUL_PRECISION_INT32) != 0) { |
| 2450 | printf("FAIL: spec1 init failed\n"); |
| 2451 | return -1; |
| 2452 | } |
| 2453 | if (omega_matmul_spec_init(&spec2, 32, 16, 64, OMEGA_MATMUL_PRECISION_INT32) != 0) { |
| 2454 | printf("FAIL: spec2 init failed\n"); |
| 2455 | return -1; |
| 2456 | } |
| 2457 | |
| 2458 | OmegaBlackwellKernel k1, k2; |
| 2459 | memset(&k1, 0, sizeof(k1)); |
| 2460 | memset(&k2, 0, sizeof(k2)); |
| 2461 | |
| 2462 | if (omega_blackwell_codegen_matmul(&spec1, &k1) != 0) { |
| 2463 | printf("FAIL: k1 codegen failed\n"); |
| 2464 | return -1; |
| 2465 | } |
| 2466 | if (omega_blackwell_codegen_matmul(&spec2, &k2) != 0) { |
| 2467 | printf("FAIL: k2 codegen failed\n"); |
| 2468 | omega_blackwell_kernel_free(&k1); |
| 2469 | return -1; |
| 2470 | } |
| 2471 | |
| 2472 | OmegaBlackwellRealizationIdentity id1, id2; |
| 2473 | omega_blackwell_bind_matmul_realization(&spec1, &k1, &id1); |
| 2474 | omega_blackwell_bind_matmul_realization(&spec2, &k2, &id2); |
| 2475 | |
| 2476 | printf("================================================================================\n"); |
| 2477 | printf(" OMEGA BLACKWELL CODEGEN VARIATION DEMONSTRATION (GATE 7 STAGE-1)\n"); |
| 2478 | printf("================================================================================\n"); |
| 2479 | printf("Config 1: 16x16x16 INT32\n"); |
| 2480 | printf(" Instructions: %zu (%zu bytes, %u GPRs, %u UGPRs)\n", k1.insn_count, k1.code_size, k1.gpr_count, k1.uniform_gpr_count); |
| 2481 | printf(" Spec ID: "); |
| 2482 | for (int i = 0; i < 16; i++) printf("%02x", spec1.spec_id[i]); |
| 2483 | printf("...\n"); |
| 2484 | printf(" Code Digest: "); |
| 2485 | for (int i = 0; i < 16; i++) printf("%02x", k1.code_digest[i]); |
| 2486 | printf("...\n"); |
| 2487 | printf(" Realization: "); |
| 2488 | for (int i = 0; i < 16; i++) printf("%02x", id1.realization_id[i]); |
| 2489 | printf("...\n\n"); |
| 2490 | |
| 2491 | printf("Config 2: 32x16x64 INT32\n"); |
| 2492 | printf(" Instructions: %zu (%zu bytes, %u GPRs, %u UGPRs)\n", k2.insn_count, k2.code_size, k2.gpr_count, k2.uniform_gpr_count); |
| 2493 | printf(" Spec ID: "); |
| 2494 | for (int i = 0; i < 16; i++) printf("%02x", spec2.spec_id[i]); |
| 2495 | printf("...\n"); |
| 2496 | printf(" Code Digest: "); |
| 2497 | for (int i = 0; i < 16; i++) printf("%02x", k2.code_digest[i]); |
| 2498 | printf("...\n"); |
| 2499 | printf(" Realization: "); |
| 2500 | for (int i = 0; i < 16; i++) printf("%02x", id2.realization_id[i]); |
| 2501 | printf("...\n"); |
| 2502 | printf("================================================================================\n"); |
| 2503 | |
| 2504 | /* Validation checks */ |
| 2505 | if (memcmp(spec1.spec_id, spec2.spec_id, 32) == 0) { |
| 2506 | printf("FAIL: spec IDs match (expected distinct)\n"); |
| 2507 | return -2; |
| 2508 | } |
| 2509 | if (memcmp(k1.code_digest, k2.code_digest, 32) == 0) { |
| 2510 | printf("FAIL: code digests match (expected distinct)\n"); |
| 2511 | return -3; |
| 2512 | } |
| 2513 | if (memcmp(id1.realization_id, id2.realization_id, 32) == 0) { |
| 2514 | printf("FAIL: realization IDs match (expected distinct)\n"); |
| 2515 | return -4; |
| 2516 | } |
| 2517 | |
| 2518 | printf("PASS: Dynamic codegen variation verified (distinct specifications, code digests, and realization IDs).\n"); |
| 2519 | |
| 2520 | omega_blackwell_kernel_free(&k1); |
| 2521 | omega_blackwell_kernel_free(&k2); |
| 2522 | return 0; |
| 2523 | } |
| 2524 | #endif |
| 2525 | |
| 2526 | /* Declared registers: peak + 2 rounded up to 8 (see omega_blackwell_codegen.h for the chip evidence). */ |
| 2527 | uint32_t omega_bw_gpr_declared(uint32_t peak) { return ((peak + 2u + 7u) / 8u) * 8u; } |
| 2528 | int omega_bw_gpr_declared_ok(uint32_t declared, uint32_t peak) { return declared % 8u == 0u && declared >= peak + 2u; } |