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 / gpu-reconv-308 / T1b-GPU-RECONV-0a1812f-observe-dropped-join / omega_blackwell_codegen.c
| 1 | #include "omega_blackwell_codegen.h" |
| 2 | #include "omega_blackwell_submit.h" |
| 3 | #include "sha256.h" |
| 4 | #include <stdio.h> |
| 5 | #include <stdlib.h> |
| 6 | #include <string.h> |
| 7 | |
| 8 | void omega_bw_ir_init(BlackwellIRProgram *prog) { |
| 9 | if (!prog) return; |
| 10 | memset(prog, 0, sizeof(*prog)); |
| 11 | } |
| 12 | |
| 13 | int omega_bw_ir_alloc_vreg(BlackwellIRProgram *prog) { |
| 14 | if (!prog || prog->regalloc.num_vregs >= BW_MAX_VREGS) return -1; |
| 15 | int v = prog->regalloc.num_vregs++; |
| 16 | prog->regalloc.intervals[v].vreg = v; |
| 17 | prog->regalloc.intervals[v].first_def = -1; |
| 18 | prog->regalloc.intervals[v].last_use = -1; |
| 19 | prog->regalloc.intervals[v].phys_reg = -1; |
| 20 | prog->regalloc.intervals[v].bundle_size = 1; |
| 21 | prog->regalloc.intervals[v].is_pair = false; |
| 22 | prog->regalloc.intervals[v].is_quad = false; |
| 23 | prog->regalloc.intervals[v].active = false; |
| 24 | prog->regalloc.vreg_to_phys[v] = -1; |
| 25 | return v; |
| 26 | } |
| 27 | |
| 28 | int omega_bw_ir_alloc_vreg64(BlackwellIRProgram *prog) { |
| 29 | if (!prog || prog->regalloc.num_vregs >= BW_MAX_VREGS) return -1; |
| 30 | int v = prog->regalloc.num_vregs++; |
| 31 | prog->regalloc.intervals[v].vreg = v; |
| 32 | prog->regalloc.intervals[v].first_def = -1; |
| 33 | prog->regalloc.intervals[v].last_use = -1; |
| 34 | prog->regalloc.intervals[v].phys_reg = -1; |
| 35 | prog->regalloc.intervals[v].bundle_size = 2; |
| 36 | prog->regalloc.intervals[v].is_pair = true; |
| 37 | prog->regalloc.intervals[v].is_quad = false; |
| 38 | prog->regalloc.intervals[v].active = false; |
| 39 | prog->regalloc.vreg_to_phys[v] = -1; |
| 40 | return v; |
| 41 | } |
| 42 | |
| 43 | int omega_bw_ir_alloc_vreg128(BlackwellIRProgram *prog) { |
| 44 | if (!prog || prog->regalloc.num_vregs >= BW_MAX_VREGS) return -1; |
| 45 | int v = prog->regalloc.num_vregs++; |
| 46 | prog->regalloc.intervals[v].vreg = v; |
| 47 | prog->regalloc.intervals[v].first_def = -1; |
| 48 | prog->regalloc.intervals[v].last_use = -1; |
| 49 | prog->regalloc.intervals[v].phys_reg = -1; |
| 50 | prog->regalloc.intervals[v].bundle_size = 4; |
| 51 | prog->regalloc.intervals[v].is_pair = false; |
| 52 | prog->regalloc.intervals[v].is_quad = true; |
| 53 | prog->regalloc.intervals[v].active = false; |
| 54 | prog->regalloc.vreg_to_phys[v] = -1; |
| 55 | return v; |
| 56 | } |
| 57 | |
| 58 | int omega_bw_ir_alloc_uvreg(BlackwellIRProgram *prog) { |
| 59 | if (!prog || prog->regalloc.num_uvregs >= BW_MAX_UVREGS) return -1; |
| 60 | int uv = prog->regalloc.num_uvregs++; |
| 61 | prog->regalloc.uintervals[uv].vreg = uv; |
| 62 | prog->regalloc.uintervals[uv].first_def = -1; |
| 63 | prog->regalloc.uintervals[uv].last_use = -1; |
| 64 | prog->regalloc.uintervals[uv].phys_reg = -1; |
| 65 | prog->regalloc.uintervals[uv].is_pair = false; |
| 66 | prog->regalloc.uintervals[uv].active = false; |
| 67 | prog->regalloc.uvreg_to_phys[uv] = -1; |
| 68 | return uv; |
| 69 | } |
| 70 | |
| 71 | int omega_bw_ir_alloc_uvreg64(BlackwellIRProgram *prog) { |
| 72 | if (!prog || prog->regalloc.num_uvregs >= BW_MAX_UVREGS) return -1; |
| 73 | int uv = prog->regalloc.num_uvregs++; |
| 74 | prog->regalloc.uintervals[uv].vreg = uv; |
| 75 | prog->regalloc.uintervals[uv].first_def = -1; |
| 76 | prog->regalloc.uintervals[uv].last_use = -1; |
| 77 | prog->regalloc.uintervals[uv].phys_reg = -1; |
| 78 | prog->regalloc.uintervals[uv].is_pair = true; |
| 79 | prog->regalloc.uintervals[uv].active = false; |
| 80 | prog->regalloc.uvreg_to_phys[uv] = -1; |
| 81 | return uv; |
| 82 | } |
| 83 | |
| 84 | int omega_bw_ir_append(BlackwellIRProgram *prog, const BlackwellIRInsn *insn) { |
| 85 | if (!prog || !insn || prog->count >= BW_MAX_IR_INSNS) return -1; |
| 86 | int idx = (int)prog->count++; |
| 87 | prog->insns[idx] = *insn; |
| 88 | |
| 89 | /* Update live-interval def/use tracking */ |
| 90 | if (insn->dst_vreg >= 0 && insn->dst_vreg < prog->regalloc.num_vregs) { |
| 91 | if (!insn->is_uniform) { |
| 92 | OmegaLiveInterval *iv = &prog->regalloc.intervals[insn->dst_vreg]; |
| 93 | if (iv->first_def < 0) iv->first_def = idx; |
| 94 | if (idx > iv->last_use) iv->last_use = idx; |
| 95 | } |
| 96 | } |
| 97 | if (insn->src1_vreg >= 0 && insn->src1_vreg < prog->regalloc.num_vregs) { |
| 98 | OmegaLiveInterval *iv = &prog->regalloc.intervals[insn->src1_vreg]; |
| 99 | if (iv->first_def < 0) iv->first_def = idx; |
| 100 | if (idx > iv->last_use) iv->last_use = idx; |
| 101 | } |
| 102 | if (insn->src2_vreg >= 0 && insn->src2_vreg < prog->regalloc.num_vregs) { |
| 103 | OmegaLiveInterval *iv = &prog->regalloc.intervals[insn->src2_vreg]; |
| 104 | if (iv->first_def < 0) iv->first_def = idx; |
| 105 | if (idx > iv->last_use) iv->last_use = idx; |
| 106 | } |
| 107 | if (insn->src3_vreg >= 0 && insn->src3_vreg < prog->regalloc.num_vregs) { |
| 108 | OmegaLiveInterval *iv = &prog->regalloc.intervals[insn->src3_vreg]; |
| 109 | if (iv->first_def < 0) iv->first_def = idx; |
| 110 | if (idx > iv->last_use) iv->last_use = idx; |
| 111 | } |
| 112 | |
| 113 | /* Uniform register def/use tracking */ |
| 114 | if (insn->is_uniform && insn->dst_vreg >= 0 && insn->dst_vreg < prog->regalloc.num_uvregs) { |
| 115 | OmegaLiveInterval *uiv = &prog->regalloc.uintervals[insn->dst_vreg]; |
| 116 | if (uiv->first_def < 0) uiv->first_def = idx; |
| 117 | if (idx > uiv->last_use) uiv->last_use = idx; |
| 118 | } |
| 119 | if (insn->ureg >= 0 && insn->ureg < prog->regalloc.num_uvregs) { |
| 120 | OmegaLiveInterval *uiv = &prog->regalloc.uintervals[insn->ureg]; |
| 121 | if (uiv->first_def < 0) uiv->first_def = idx; |
| 122 | if (idx > uiv->last_use) uiv->last_use = idx; |
| 123 | } |
| 124 | |
| 125 | return idx; |
| 126 | } |
| 127 | |
| 128 | int omega_bw_regalloc_solve(BlackwellIRProgram *prog) { |
| 129 | if (!prog) return -1; |
| 130 | OmegaRegAlloc *ra = &prog->regalloc; |
| 131 | |
| 132 | /* |
| 133 | * Bounded deterministic linear-scan register allocator. |
| 134 | * Computes genuine live intervals across virtual registers and assigns |
| 135 | * available physical GPRs (R2..R127) and UGPRs (UR4..UR31). |
| 136 | * Enforces even-alignment and dual-register reservation for 64-bit pairs. |
| 137 | */ |
| 138 | bool gpr_busy[BW_PHYS_GPR_MAX]; |
| 139 | memset(gpr_busy, 0, sizeof(gpr_busy)); |
| 140 | |
| 141 | int max_phys_used = BW_PHYS_GPR_START; |
| 142 | |
| 143 | /* Sort virtual register indices chronologically by first_def (Poletto & Sarkar Linear Scan) */ |
| 144 | int order[BW_MAX_VREGS]; |
| 145 | int count = 0; |
| 146 | for (int v = 0; v < ra->num_vregs; v++) { |
| 147 | if (ra->intervals[v].first_def >= 0) { |
| 148 | order[count++] = v; |
| 149 | } else { |
| 150 | ra->intervals[v].phys_reg = BW_PHYS_GPR_START; |
| 151 | ra->vreg_to_phys[v] = ra->intervals[v].phys_reg; |
| 152 | } |
| 153 | } |
| 154 | for (int i = 0; i < count - 1; i++) { |
| 155 | for (int j = i + 1; j < count; j++) { |
| 156 | if (ra->intervals[order[i]].first_def > ra->intervals[order[j]].first_def) { |
| 157 | int tmp = order[i]; |
| 158 | order[i] = order[j]; |
| 159 | order[j] = tmp; |
| 160 | } |
| 161 | } |
| 162 | } |
| 163 | |
| 164 | for (int idx = 0; idx < count; idx++) { |
| 165 | int v = order[idx]; |
| 166 | OmegaLiveInterval *iv = &ra->intervals[v]; |
| 167 | |
| 168 | /* Free physical registers whose virtual live intervals have ended */ |
| 169 | for (int p = 0; p < ra->num_vregs; p++) { |
| 170 | if (ra->intervals[p].active && ra->intervals[p].last_use < iv->first_def) { |
| 171 | int freed = ra->intervals[p].phys_reg; |
| 172 | if (freed >= 0 && freed < BW_PHYS_GPR_MAX) { |
| 173 | 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)); |
| 174 | for (uint32_t k = 0; k < bsize && (freed + (int)k) < BW_PHYS_GPR_MAX; k++) { |
| 175 | gpr_busy[freed + k] = false; |
| 176 | } |
| 177 | } |
| 178 | ra->intervals[p].active = false; |
| 179 | } |
| 180 | } |
| 181 | |
| 182 | /* Find lowest available physical register */ |
| 183 | int assigned = -1; |
| 184 | uint32_t bsize = iv->bundle_size ? iv->bundle_size : (iv->is_pair ? 2 : (iv->is_quad ? 4 : 1)); |
| 185 | if (bsize == 4) { |
| 186 | /* 128-bit quad requires 4-alignment and 4 contiguous free registers */ |
| 187 | int start = (BW_PHYS_GPR_START + 3) & ~3; |
| 188 | for (int p = start; p <= BW_PHYS_GPR_MAX - 4; p += 4) { |
| 189 | if (!gpr_busy[p] && !gpr_busy[p + 1] && !gpr_busy[p + 2] && !gpr_busy[p + 3]) { |
| 190 | assigned = p; |
| 191 | for (int k = 0; k < 4; k++) gpr_busy[p + k] = true; |
| 192 | break; |
| 193 | } |
| 194 | } |
| 195 | } else if (bsize == 2) { |
| 196 | /* 64-bit pair requires even alignment and contiguous free registers */ |
| 197 | int start = (BW_PHYS_GPR_START + 1) & ~1; |
| 198 | for (int p = start; p <= BW_PHYS_GPR_MAX - 2; p += 2) { |
| 199 | if (!gpr_busy[p] && !gpr_busy[p + 1]) { |
| 200 | assigned = p; |
| 201 | gpr_busy[p] = true; |
| 202 | gpr_busy[p + 1] = true; |
| 203 | break; |
| 204 | } |
| 205 | } |
| 206 | } else { |
| 207 | for (int p = BW_PHYS_GPR_START; p < BW_PHYS_GPR_MAX; p++) { |
| 208 | if (!gpr_busy[p]) { |
| 209 | assigned = p; |
| 210 | gpr_busy[p] = true; |
| 211 | break; |
| 212 | } |
| 213 | } |
| 214 | } |
| 215 | |
| 216 | if (assigned < 0) { |
| 217 | /* Hardware register capacity exceeded: fail closed loudly */ |
| 218 | return -1; |
| 219 | } |
| 220 | |
| 221 | iv->phys_reg = assigned; |
| 222 | iv->active = true; |
| 223 | ra->vreg_to_phys[v] = assigned; |
| 224 | |
| 225 | int top = assigned + (int)bsize - 1; |
| 226 | if (top > max_phys_used) { |
| 227 | max_phys_used = top; |
| 228 | } |
| 229 | } |
| 230 | ra->peak_gpr_usage = (uint32_t)(max_phys_used + 1); |
| 231 | |
| 232 | /* Allocate uniform registers */ |
| 233 | int cur_u = BW_PHYS_UGPR_START; |
| 234 | for (int uv = 0; uv < ra->num_uvregs; uv++) { |
| 235 | OmegaLiveInterval *uiv = &ra->uintervals[uv]; |
| 236 | if (uiv->is_pair) { |
| 237 | if (cur_u % 2 != 0) cur_u++; |
| 238 | if (cur_u + 1 >= BW_PHYS_UGPR_MAX) return -1; |
| 239 | uiv->phys_reg = cur_u; |
| 240 | ra->uvreg_to_phys[uv] = cur_u; |
| 241 | cur_u += 2; |
| 242 | } else { |
| 243 | if (cur_u >= BW_PHYS_UGPR_MAX) return -1; |
| 244 | uiv->phys_reg = cur_u; |
| 245 | ra->uvreg_to_phys[uv] = cur_u++; |
| 246 | } |
| 247 | } |
| 248 | ra->peak_ugpr_usage = (uint32_t)cur_u; |
| 249 | |
| 250 | return 0; |
| 251 | } |
| 252 | |
| 253 | static int encode_single_insn(const BlackwellIRInsn *insn, const OmegaRegAlloc *ra, uint32_t w[4]) { |
| 254 | memset(w, 0, 16); |
| 255 | |
| 256 | int dst = (insn->dst_vreg >= 0) ? (ra->vreg_to_phys[insn->dst_vreg] + insn->dst_subreg) : 0; |
| 257 | int src1 = (insn->src1_vreg >= 0) ? (ra->vreg_to_phys[insn->src1_vreg] + insn->src1_subreg) : 0; |
| 258 | int src2 = (insn->src2_vreg >= 0) ? (ra->vreg_to_phys[insn->src2_vreg] + insn->src2_subreg) : 0; |
| 259 | int src3 = (insn->src3_vreg >= 0) ? (ra->vreg_to_phys[insn->src3_vreg] + insn->src3_subreg) : 0xff; /* Default to RZ (0xff) */ |
| 260 | int ureg = (insn->ureg >= 0) ? ra->uvreg_to_phys[insn->ureg] : 0; |
| 261 | |
| 262 | if (insn->is_uniform && insn->dst_vreg >= 0) { |
| 263 | dst = ra->uvreg_to_phys[insn->dst_vreg]; |
| 264 | } |
| 265 | |
| 266 | switch (insn->op) { |
| 267 | case BW_IR_NOP: |
| 268 | w[0] = 0x00007918; |
| 269 | w[1] = 0x00000000; |
| 270 | w[2] = 0x00000000; |
| 271 | w[3] = 0x000fc000; |
| 272 | break; |
| 273 | |
| 274 | case BW_IR_MOV_RZ: |
| 275 | /* MOV Rd, RZ */ |
| 276 | w[0] = 0x7202 | ((uint32_t)(dst & 0xff) << 16); |
| 277 | w[1] = 0x000000ff; |
| 278 | w[2] = 0x00000f00; |
| 279 | w[3] = insn->control ? insn->control : 0x000fe200; |
| 280 | break; |
| 281 | |
| 282 | case BW_IR_MOV_IMM: |
| 283 | /* MOV Rd, imm32 */ |
| 284 | w[0] = 0x7802 | ((uint32_t)(dst & 0xff) << 16); |
| 285 | w[1] = insn->imm; |
| 286 | w[2] = 0x00000f00; |
| 287 | w[3] = insn->control ? insn->control : 0x000fe200; |
| 288 | break; |
| 289 | |
| 290 | case BW_IR_S2R: |
| 291 | /* S2R Rd, SR */ |
| 292 | w[0] = 0x7919 | ((uint32_t)(dst & 0xff) << 16); |
| 293 | w[1] = 0x0; |
| 294 | w[2] = (insn->imm & 0xff) << 8; |
| 295 | w[3] = insn->control ? insn->control : 0x000e2200; |
| 296 | break; |
| 297 | |
| 298 | case BW_IR_LDC: |
| 299 | /* LDC Rd, c[0x0][imm] */ |
| 300 | w[0] = 0xff007b82 | ((uint32_t)(dst & 0xff) << 16); |
| 301 | w[1] = (insn->imm << 6) & 0x00ffff00; |
| 302 | w[2] = 0x00000800; |
| 303 | w[3] = insn->control ? insn->control : 0x000fe200; |
| 304 | break; |
| 305 | |
| 306 | case BW_IR_LDC64: |
| 307 | /* LDC.64 Rd:Rd+1, c[0x0][imm] */ |
| 308 | w[0] = 0xff007b82 | ((uint32_t)(dst & 0xff) << 16); |
| 309 | w[1] = (insn->imm << 6) & 0x00ffff00; |
| 310 | w[2] = 0x00000a00; |
| 311 | w[3] = insn->control ? insn->control : 0x000e2200; |
| 312 | break; |
| 313 | |
| 314 | case BW_IR_LDCU: |
| 315 | /* LDCU URd, c[0x0][imm] */ |
| 316 | w[0] = 0xff0077ac | ((uint32_t)(dst & 0xff) << 16); |
| 317 | w[1] = (insn->imm << 5) & 0x00ffff00; |
| 318 | w[2] = 0x08000800; |
| 319 | w[3] = insn->control ? insn->control : 0x000e2200; |
| 320 | break; |
| 321 | |
| 322 | case BW_IR_LDCU64: |
| 323 | /* LDCU.64 URd:URd+1, c[0x0][imm] */ |
| 324 | w[0] = 0xff0077ac | ((uint32_t)(dst & 0xff) << 16); |
| 325 | w[1] = (insn->imm << 5) & 0x00ffff00; |
| 326 | w[2] = 0x08000a00; |
| 327 | w[3] = insn->control ? insn->control : 0x000e6e00; |
| 328 | break; |
| 329 | |
| 330 | case BW_IR_IMAD: |
| 331 | if (insn->src2_vreg >= 0) { |
| 332 | /* Register-register: IMAD Rd, Ra, Rb, Rc (0x7224) */ |
| 333 | w[0] = 0x7224 | ((uint32_t)(dst & 0xff) << 16) | ((uint32_t)(src1 & 0xff) << 24); |
| 334 | w[1] = (uint32_t)(src2 & 0xff); |
| 335 | w[2] = 0x078e0200 | (uint32_t)(src3 & 0xff); |
| 336 | w[3] = insn->control ? insn->control : 0x001fca00; |
| 337 | } else if (insn->is_uniform && insn->ureg >= 0) { |
| 338 | /* Uniform register multiplier: IMAD Rd, Ra, URb, Rc (0x7c24) */ |
| 339 | w[0] = 0x7c24 | ((uint32_t)(dst & 0xff) << 16) | ((uint32_t)(src1 & 0xff) << 24); |
| 340 | w[1] = (uint32_t)(ureg & 0xff); |
| 341 | w[2] = 0x0f8e0200 | (uint32_t)(src3 & 0xff); |
| 342 | w[3] = insn->control ? insn->control : 0x001fca00; |
| 343 | } else { |
| 344 | /* Immediate multiplier: IMAD Rd, Ra, imm, Rc (0x7824) */ |
| 345 | w[0] = 0x7824 | ((uint32_t)(dst & 0xff) << 16) | ((uint32_t)(src1 & 0xff) << 24); |
| 346 | w[1] = insn->imm; |
| 347 | w[2] = 0x078e0200 | (uint32_t)(src3 & 0xff); |
| 348 | w[3] = insn->control ? insn->control : 0x001fca00; |
| 349 | } |
| 350 | break; |
| 351 | |
| 352 | case BW_IR_IMAD_WIDE: |
| 353 | /* IMAD.WIDE.U32 Rd:Rd+1, Ra, imm, Rc:Rc+1 */ |
| 354 | w[0] = 0x7825 | ((uint32_t)(dst & 0xff) << 16) | ((uint32_t)(src1 & 0xff) << 24); |
| 355 | w[1] = insn->imm; |
| 356 | w[2] = 0x078e0000 | (uint32_t)(src3 & 0xff); |
| 357 | w[3] = insn->control ? insn->control : 0x001fcc00; |
| 358 | break; |
| 359 | |
| 360 | case BW_IR_ISETP_GE: |
| 361 | if (insn->is_uniform && insn->ureg >= 0) { |
| 362 | w[0] = 0x7c0c | ((uint32_t)(dst & 0xff) << 16) | ((uint32_t)(src1 & 0xff) << 24); |
| 363 | w[1] = (uint32_t)(ureg & 0xff); |
| 364 | w[2] = 0x0bf06070; |
| 365 | w[3] = 0x002fda00; |
| 366 | } else { |
| 367 | w[0] = 0x780c | ((uint32_t)(dst & 0xff) << 16) | ((uint32_t)(src1 & 0xff) << 24); |
| 368 | w[1] = insn->imm; |
| 369 | w[2] = 0x03f06270; |
| 370 | w[3] = 0x001fda00; |
| 371 | } |
| 372 | break; |
| 373 | |
| 374 | case BW_IR_LDG_E: |
| 375 | /* LDG.E Rd, desc[URd][Ra.64] */ |
| 376 | w[0] = 0x7981 | ((uint32_t)(dst & 0xff) << 16) | ((uint32_t)(src1 & 0xff) << 24); |
| 377 | w[1] = (uint32_t)(ureg & 0xff); |
| 378 | w[2] = 0x0c1e1900; |
| 379 | w[3] = insn->control ? insn->control : 0x000f2200; |
| 380 | break; |
| 381 | |
| 382 | case BW_IR_STG_E: |
| 383 | /* STG.E desc[URd][Ra.64], Rb */ |
| 384 | w[0] = 0x7986 | ((uint32_t)(src1 & 0xff) << 24); |
| 385 | w[1] = (uint32_t)(src2 & 0xff); |
| 386 | w[2] = 0x0c101900 | (uint32_t)(ureg & 0xff); |
| 387 | w[3] = insn->control ? insn->control : 0x000fe200; |
| 388 | break; |
| 389 | |
| 390 | case BW_IR_STG_EF: |
| 391 | /* STG.E.MMIO.GPU. Uncached store, meant to be visible without a fence. */ |
| 392 | w[0] = 0x7986 | ((uint32_t)(src1 & 0xff) << 24); |
| 393 | w[1] = (uint32_t)(src2 & 0xff); |
| 394 | w[2] = 0x0c111900 | (uint32_t)(ureg & 0xff); |
| 395 | w[3] = insn->control ? insn->control : 0x000fe200; |
| 396 | break; |
| 397 | |
| 398 | case BW_IR_IADD3: |
| 399 | /* IADD3 Rd, PT, PT, Ra, Rb, Rc */ |
| 400 | w[0] = 0x7210 | ((uint32_t)(dst & 0xff) << 16) | ((uint32_t)(src1 & 0xff) << 24); |
| 401 | w[1] = (uint32_t)(src2 & 0xff); |
| 402 | w[2] = 0x07ffe0ff; |
| 403 | w[3] = 0x010fca00; |
| 404 | break; |
| 405 | |
| 406 | case BW_IR_HMMA_F16: |
| 407 | case BW_IR_HMMA_BF16: { |
| 408 | /* HMMA.16816.F32[.BF16] Rd, Ra, Rb, Rc */ |
| 409 | w[0] = 0x723c | ((uint32_t)(dst & 0xff) << 16) | ((uint32_t)(src1 & 0xff) << 24); |
| 410 | w[1] = (uint32_t)(src2 & 0xff); |
| 411 | uint32_t prec_bit = (insn->op == BW_IR_HMMA_BF16) ? 0x00040000 : 0x00000000; |
| 412 | w[2] = (uint32_t)(src3 & 0xff) | (0x18 << 8) | prec_bit; |
| 413 | w[3] = insn->control ? insn->control : 0x000fe200; |
| 414 | break; |
| 415 | } |
| 416 | |
| 417 | case BW_IR_SHF_R: |
| 418 | /* SHF.R.U32.HI Rd, RZ, imm, Ra */ |
| 419 | w[0] = 0x7819U | ((uint32_t)(dst & 0xff) << 16) | (0xffU << 24); |
| 420 | w[1] = (uint32_t)insn->imm; |
| 421 | w[2] = 0x00011600 | (uint32_t)(src1 & 0xff); |
| 422 | w[3] = insn->control ? insn->control : 0x001fca00; |
| 423 | break; |
| 424 | |
| 425 | case BW_IR_LOP3_AND: |
| 426 | /* LOP3.LUT Rd, Ra, imm, RZ, 0xc0, !PT */ |
| 427 | w[0] = 0x7812U | ((uint32_t)(dst & 0xff) << 16) | ((uint32_t)(src1 & 0xff) << 24); |
| 428 | w[1] = (uint32_t)insn->imm; |
| 429 | w[2] = 0x078ec0ff; |
| 430 | w[3] = insn->control ? insn->control : 0x001fca00; |
| 431 | break; |
| 432 | |
| 433 | case BW_IR_LDG_E_U16: |
| 434 | /* LDG.E.U16 Rd, desc[URd][Ra.64] */ |
| 435 | w[0] = 0x7981U | ((uint32_t)(dst & 0xff) << 16) | ((uint32_t)(src1 & 0xff) << 24); |
| 436 | w[1] = (uint32_t)(ureg & 0xff); |
| 437 | w[2] = 0x0c1e1500; |
| 438 | w[3] = insn->control ? insn->control : 0x000f2200; |
| 439 | break; |
| 440 | |
| 441 | case BW_IR_LDG_STRONG_SYS: |
| 442 | /* LDG.E.STRONG.SYS. Same shape as LDG.E; w[2] carries the |
| 443 | * system-scope bits (0x0c1e1900 -> 0x0c1f5900). */ |
| 444 | w[0] = 0x7981 | ((uint32_t)(dst & 0xff) << 16) | ((uint32_t)(src1 & 0xff) << 24); |
| 445 | w[1] = (uint32_t)(ureg & 0xff); |
| 446 | w[2] = 0x0c1f5900; |
| 447 | w[3] = insn->control ? insn->control : 0x000f2200; |
| 448 | break; |
| 449 | |
| 450 | case BW_IR_LDG_MMIO: |
| 451 | /* LDG.E.EF. Drop the cached line, then load. */ |
| 452 | w[0] = 0x7981 | ((uint32_t)(dst & 0xff) << 16) | ((uint32_t)(src1 & 0xff) << 24); |
| 453 | w[1] = (uint32_t)(ureg & 0xff); |
| 454 | w[2] = 0x0c0e1900; |
| 455 | w[3] = insn->control ? insn->control : 0x000f2200; |
| 456 | break; |
| 457 | |
| 458 | case BW_IR_STG_STRONG_SYS: |
| 459 | w[0] = 0x7986 | ((uint32_t)(src1 & 0xff) << 24); |
| 460 | if (insn->predicate_p0) { |
| 461 | uint32_t pred = insn->predicate_not ? 0x8u : 0x0u; |
| 462 | w[0] = (w[0] & ~0xF000u) | (pred << 12); |
| 463 | } |
| 464 | w[1] = (uint32_t)(src2 & 0xff); |
| 465 | w[2] = 0x0c115900 | (uint32_t)(ureg & 0xff); |
| 466 | w[3] = insn->control ? insn->control : 0x000fe200; |
| 467 | break; |
| 468 | |
| 469 | case BW_IR_MEMBAR_ALL_SYS: |
| 470 | w[0] = 0x00007992; |
| 471 | w[1] = 0x00000000; |
| 472 | w[2] = 0x0000b000; |
| 473 | w[3] = insn->control ? insn->control : 0x000fec00; |
| 474 | break; |
| 475 | |
| 476 | case BW_IR_MEMBAR_SC_SYS: |
| 477 | w[0] = 0x00007992; |
| 478 | w[1] = 0x00000000; |
| 479 | w[2] = 0x00003000; |
| 480 | w[3] = insn->control ? insn->control : 0x000fec00; |
| 481 | break; |
| 482 | |
| 483 | case BW_IR_CCTL_IVALL: |
| 484 | w[0] = 0xff00798f; |
| 485 | w[1] = 0x00000000; |
| 486 | w[2] = 0x02000000; |
| 487 | w[3] = insn->control ? insn->control : 0x000fe800; |
| 488 | break; |
| 489 | |
| 490 | case BW_IR_ATOMG_ADD_STRONG_SYS: |
| 491 | w[0] = 0x79a8 | ((uint32_t)(dst & 0xff) << 16) | ((uint32_t)(src1 & 0xff) << 24); |
| 492 | w[1] = (uint32_t)(src2 & 0xff) | 0x80000000u; |
| 493 | w[2] = 0x081f5100 | (uint32_t)(ureg & 0xff); |
| 494 | w[3] = insn->control ? insn->control : 0x00321e00; |
| 495 | break; |
| 496 | |
| 497 | case BW_IR_ATOMG_EXCH_STRONG_SYS: |
| 498 | w[0] = 0x79a8 | ((uint32_t)(dst & 0xff) << 16) | ((uint32_t)(src1 & 0xff) << 24); |
| 499 | w[1] = (uint32_t)(src2 & 0xff) | 0x80000000u; |
| 500 | w[2] = 0x0c1f5100 | (uint32_t)(ureg & 0xff); |
| 501 | w[3] = insn->control ? insn->control : 0x00321e00; |
| 502 | break; |
| 503 | |
| 504 | case BW_IR_ISETP_GE_U32: |
| 505 | /* ISETP.GE.U32.AND P0, PT, Ra, Rb, PT. Checked with nvdisasm -b SM121. */ |
| 506 | w[0] = 0x720c | ((uint32_t)(src1 & 0xff) << 24); |
| 507 | w[1] = (uint32_t)(src2 & 0xff); |
| 508 | w[2] = 0x03f06070; |
| 509 | w[3] = insn->control ? insn->control : 0x001fda00; |
| 510 | break; |
| 511 | |
| 512 | case BW_IR_LOP3_XOR: |
| 513 | /* LOP3.LUT Rd, Ra, Rb, RZ, 0x3c, !PT. Checked with nvdisasm -b SM121. */ |
| 514 | w[0] = 0x7212 | ((uint32_t)(dst & 0xff) << 16) | ((uint32_t)(src1 & 0xff) << 24); |
| 515 | w[1] = (uint32_t)(src2 & 0xff); |
| 516 | w[2] = 0x078e3cff; |
| 517 | w[3] = insn->control ? insn->control : 0x001fca00; |
| 518 | break; |
| 519 | |
| 520 | case BW_IR_FADD: |
| 521 | /* FADD Rd, Ra, Rb */ |
| 522 | w[0] = 0x7221U | ((uint32_t)(dst & 0xff) << 16) | ((uint32_t)(src1 & 0xff) << 24); |
| 523 | w[1] = (uint32_t)(src2 & 0xff); |
| 524 | w[2] = 0x00000000; |
| 525 | w[3] = insn->control ? insn->control : 0x004fc400; |
| 526 | break; |
| 527 | |
| 528 | case BW_IR_FSUB: |
| 529 | /* FSUB Rd, Ra, Rb (FADD Rd, Ra, -Rb) */ |
| 530 | w[0] = 0x7221U | ((uint32_t)(dst & 0xff) << 16) | ((uint32_t)(src1 & 0xff) << 24); |
| 531 | w[1] = (uint32_t)(src2 & 0xff) | 0x80000000U; |
| 532 | w[2] = 0x00000000; |
| 533 | w[3] = insn->control ? insn->control : 0x004fc400; |
| 534 | break; |
| 535 | |
| 536 | case BW_IR_FMUL: |
| 537 | /* FMUL Rd, Ra, Rb */ |
| 538 | w[0] = 0x7220U | ((uint32_t)(dst & 0xff) << 16) | ((uint32_t)(src1 & 0xff) << 24); |
| 539 | w[1] = (uint32_t)(src2 & 0xff); |
| 540 | w[2] = 0x00400000; |
| 541 | w[3] = insn->control ? insn->control : 0x004fc400; |
| 542 | break; |
| 543 | |
| 544 | case BW_IR_FFMA: |
| 545 | /* FFMA Rd, Ra, Rb, Rc */ |
| 546 | w[0] = 0x7223U | ((uint32_t)(dst & 0xff) << 16) | ((uint32_t)(src1 & 0xff) << 24); |
| 547 | w[1] = (uint32_t)(src2 & 0xff); |
| 548 | w[2] = (uint32_t)(src3 & 0xff); |
| 549 | w[3] = insn->control ? insn->control : 0x004fc400; |
| 550 | break; |
| 551 | |
| 552 | case BW_IR_FSETP: |
| 553 | /* FSETP.cond.AND P0, PT, Ra, Rb, PT */ |
| 554 | w[0] = 0x720bU | ((uint32_t)(dst & 0x7) << 16) | ((uint32_t)(src1 & 0xff) << 24); |
| 555 | w[1] = (uint32_t)(src2 & 0xff); |
| 556 | w[2] = insn->imm ? insn->imm : 0x03f06000; /* Default GE condition */ |
| 557 | w[3] = insn->control ? insn->control : 0x004fc400; |
| 558 | break; |
| 559 | |
| 560 | case BW_IR_FSEL: |
| 561 | /* FSEL Rd, Ra, Rb, P0 */ |
| 562 | w[0] = 0x7208U | ((uint32_t)(dst & 0xff) << 16) | ((uint32_t)(src1 & 0xff) << 24); |
| 563 | w[1] = (uint32_t)(src2 & 0xff); |
| 564 | w[2] = (uint32_t)(src3 & 0x7); |
| 565 | w[3] = insn->control ? insn->control : 0x000fca00; |
| 566 | break; |
| 567 | |
| 568 | case BW_IR_FMNMX_MIN: |
| 569 | /* FMNMX Rd, Ra, Rb, PT (minimum) */ |
| 570 | w[0] = 0x7209U | ((uint32_t)(dst & 0xff) << 16) | ((uint32_t)(src1 & 0xff) << 24); |
| 571 | w[1] = (uint32_t)(src2 & 0xff); |
| 572 | w[2] = 0x03800000; |
| 573 | w[3] = insn->control ? insn->control : 0x004fc400; |
| 574 | break; |
| 575 | |
| 576 | case BW_IR_FMNMX_MAX: |
| 577 | /* FMNMX Rd, Ra, Rb, !PT (maximum) */ |
| 578 | w[0] = 0x7209U | ((uint32_t)(dst & 0xff) << 16) | ((uint32_t)(src1 & 0xff) << 24); |
| 579 | w[1] = (uint32_t)(src2 & 0xff); |
| 580 | w[2] = 0x07800000; |
| 581 | w[3] = insn->control ? insn->control : 0x004fc400; |
| 582 | break; |
| 583 | |
| 584 | case BW_IR_I2FP: |
| 585 | /* I2FP.F32.S32 Rd, Ra */ |
| 586 | w[0] = 0x7245U | ((uint32_t)(dst & 0xff) << 16); |
| 587 | w[1] = (uint32_t)(src1 & 0xff); |
| 588 | w[2] = 0x00201400; |
| 589 | w[3] = insn->control ? insn->control : 0x004fe200; |
| 590 | break; |
| 591 | |
| 592 | case BW_IR_F2I: |
| 593 | /* F2I.TRUNC.NTZ Rd, Ra */ |
| 594 | w[0] = 0x7305U | ((uint32_t)(dst & 0xff) << 16); |
| 595 | w[1] = (uint32_t)(src1 & 0xff); |
| 596 | w[2] = 0x0020f100; |
| 597 | w[3] = insn->control ? insn->control : 0x004e2200; |
| 598 | break; |
| 599 | |
| 600 | case BW_IR_MUFU_RCP: |
| 601 | /* MUFU.RCP Rd, Ra */ |
| 602 | w[0] = 0x7308U | ((uint32_t)(dst & 0xff) << 16); |
| 603 | w[1] = (uint32_t)(src1 & 0xff); |
| 604 | w[2] = 0x00001000; |
| 605 | w[3] = insn->control ? insn->control : 0x000e2400; |
| 606 | break; |
| 607 | |
| 608 | case BW_IR_MUFU_RSQ: |
| 609 | /* MUFU.RSQ Rd, Ra */ |
| 610 | w[0] = 0x7308U | ((uint32_t)(dst & 0xff) << 16); |
| 611 | w[1] = (uint32_t)(src1 & 0xff); |
| 612 | w[2] = 0x00001400; |
| 613 | w[3] = insn->control ? insn->control : 0x000e2400; |
| 614 | break; |
| 615 | |
| 616 | case BW_IR_SHFL_DOWN: |
| 617 | /* SHFL.DOWN PT, Rd, Ra, offset, 0x1f */ |
| 618 | w[0] = 0x7f89U | ((uint32_t)(dst & 0xff) << 16) | ((uint32_t)(src1 & 0xff) << 24); |
| 619 | w[1] = 0x08001f00U | (((uint32_t)insn->imm & 0x1f) << 21); |
| 620 | w[2] = 0x000e0000; |
| 621 | w[3] = insn->control ? insn->control : 0x000e2400; |
| 622 | break; |
| 623 | |
| 624 | case BW_IR_LDS: |
| 625 | /* LDS Rd, [Ra] */ |
| 626 | w[0] = 0x7984U | ((uint32_t)(dst & 0xff) << 16) | ((uint32_t)(src1 & 0xff) << 24); |
| 627 | w[1] = 0x00000000; |
| 628 | w[2] = (uint32_t)(insn->imm & 0xffff); |
| 629 | w[3] = insn->control ? insn->control : 0x000fe200; |
| 630 | break; |
| 631 | |
| 632 | case BW_IR_STS: |
| 633 | /* STS [Ra], Rb */ |
| 634 | w[0] = 0x7388U | ((uint32_t)(src1 & 0xff) << 24); |
| 635 | w[1] = (uint32_t)(src2 & 0xff); |
| 636 | w[2] = (uint32_t)(insn->imm & 0xffff); |
| 637 | w[3] = insn->control ? insn->control : 0x000fe200; |
| 638 | break; |
| 639 | |
| 640 | case BW_IR_EXIT: |
| 641 | w[0] = insn->predicate_p0 ? 0x0000094d : 0x0000794d; |
| 642 | w[1] = 0x00000000; |
| 643 | w[2] = 0x03800000; |
| 644 | w[3] = 0x000fea00; |
| 645 | break; |
| 646 | |
| 647 | case BW_IR_BRA: { |
| 648 | /* sm_121 BRA. Distance is (target - this) - 1, split across the |
| 649 | * low six bits at instruction bits 18..23 and the rest at bit 34. |
| 650 | * Predicate bits 12..15: 7 = always, 0 = @P0, 8 = @!P0. |
| 651 | * A zero delta with no predicate keeps the self-branch word the |
| 652 | * existing kernels already emit after EXIT. */ |
| 653 | int32_t delta = (int32_t)insn->imm; |
| 654 | int32_t rel = delta - 1; |
| 655 | uint32_t pred_field = 0x7u; |
| 656 | if (insn->predicate_p0) |
| 657 | pred_field = insn->predicate_not ? 0x8u : 0x0u; |
| 658 | w[0] = 0x947u | (pred_field << 12) | (((uint32_t)rel & 0x3fu) << 18); |
| 659 | w[1] = (uint32_t)(rel >> 6) << 2; |
| 660 | w[2] = (rel < 0) ? 0x0383ffffu : 0x03800000u; |
| 661 | if (insn->control) |
| 662 | w[3] = insn->control; |
| 663 | else if (delta == 0 && !insn->predicate_p0) |
| 664 | w[3] = 0x000fc000u; |
| 665 | else |
| 666 | w[3] = 0x000fea00u; |
| 667 | break; |
| 668 | } |
| 669 | |
| 670 | case BW_IR_BSSY: { |
| 671 | /* BSSY.RECONVERGENT Bn, target (omega #308). nvcc 13.0.88 -arch=sm_121: |
| 672 | * [0070] BSSY.RECONVERGENT B0, 0x140 ; 0x000000c000007945 0x000fe20003800200 |
| 673 | * opcode 0x945 (NAK sm70_encode.rs:4165), barrier id at bits 16..19 (set_bar_dst |
| 674 | * 16..20), guard predicate at bits 12..15 like BRA, relative byte offset |
| 675 | * target - (this + 16) at bit 32 (NAK: field 34.. in 4-byte units), the cond |
| 676 | * predicate PT at bits 87..89 (0x0380 << 16 in w[2]) and bit 73 (0x200 in w[2]), |
| 677 | * which the disassembler prints as .RECONVERGENT; nvcc sets it on every BSSY/BSYNC |
| 678 | * for sm_121 and Mesa NAK does not. We follow nvcc. The target is the instruction |
| 679 | * after the matching BSYNC, so the delta is at least 2; smaller is refused. */ |
| 680 | int32_t delta = (int32_t)insn->imm; |
| 681 | if (delta < 2 || insn->bar_reg >= BW_RECONV_MAX_BAR) return -1; |
| 682 | uint32_t pred_field = 0x7u; |
| 683 | if (insn->predicate_p0) |
| 684 | pred_field = insn->predicate_not ? 0x8u : 0x0u; |
| 685 | w[0] = 0x945u | (pred_field << 12) | ((uint32_t)insn->bar_reg << 16); |
| 686 | w[1] = (uint32_t)(delta - 1) * 16u; |
| 687 | w[2] = 0x03800200u; |
| 688 | w[3] = insn->control ? insn->control : 0x000fe200u; |
| 689 | break; |
| 690 | } |
| 691 | |
| 692 | case BW_IR_BSYNC: { |
| 693 | /* BSYNC.RECONVERGENT Bn: [0130] BSYNC.RECONVERGENT B0 ; 0x0000000000007941 0x000fea0003800200 |
| 694 | * opcode 0x941 (NAK sm70_encode.rs:4179), barrier id bits 16..19, cond PT, bit 73. */ |
| 695 | if (insn->bar_reg >= BW_RECONV_MAX_BAR) return -1; |
| 696 | uint32_t pred_field = 0x7u; |
| 697 | if (insn->predicate_p0) |
| 698 | pred_field = insn->predicate_not ? 0x8u : 0x0u; |
| 699 | w[0] = 0x941u | (pred_field << 12) | ((uint32_t)insn->bar_reg << 16); |
| 700 | w[1] = 0; |
| 701 | w[2] = 0x03800200u; |
| 702 | w[3] = insn->control ? insn->control : 0x000fea00u; |
| 703 | break; |
| 704 | } |
| 705 | |
| 706 | case BW_IR_MUFU_EX2: |
| 707 | /* MUFU.EX2 Rd, Ra. nvdisasm 13.0 -b SM121: 0x7308 with sub-op 0x0800 decodes |
| 708 | * "MUFU.EX2 R2, R4" (RCP is 0x1000, RSQ 0x1400 above). */ |
| 709 | w[0] = 0x7308U | ((uint32_t)(dst & 0xff) << 16); |
| 710 | w[1] = (uint32_t)(src1 & 0xff); |
| 711 | w[2] = 0x00000800; |
| 712 | w[3] = insn->control ? insn->control : 0x000e2400; |
| 713 | break; |
| 714 | |
| 715 | case BW_IR_BAR_SYNC: |
| 716 | /* BAR.SYNC.DEFER_BLOCKING 0x0: the exact words src/omega_numeric.c:885 runs on |
| 717 | * the chip (nvdisasm: "BAR.SYNC.DEFER_BLOCKING 0x0"). No registers. */ |
| 718 | w[0] = 0x00007b1dU; |
| 719 | w[1] = 0x00000000; |
| 720 | w[2] = 0x00010000; |
| 721 | w[3] = insn->control ? insn->control : 0x000fec00; |
| 722 | break; |
| 723 | |
| 724 | case BW_IR_LDS32: |
| 725 | /* LDS Rd, [Ra+URZ] (32-bit), the src/omega_numeric.c:886 form. */ |
| 726 | w[0] = 0x7984U | ((uint32_t)(dst & 0xff) << 16) | ((uint32_t)(src1 & 0xff) << 24); |
| 727 | w[1] = 0x000000ff; |
| 728 | w[2] = 0x08000800; |
| 729 | w[3] = insn->control ? insn->control : 0x000e2800; |
| 730 | break; |
| 731 | |
| 732 | case BW_IR_STS32: |
| 733 | /* STS [Ra+URZ], Rb (32-bit), the src/omega_numeric.c:884 form. */ |
| 734 | w[0] = 0x7988U | ((uint32_t)(src1 & 0xff) << 24); |
| 735 | w[1] = (uint32_t)(src2 & 0xff); |
| 736 | w[2] = 0x080008ffU; |
| 737 | w[3] = insn->control ? insn->control : 0x000fe200; |
| 738 | break; |
| 739 | |
| 740 | /* Prime race cut (2026-10-05). An absent operand is RZ for these three ops. */ |
| 741 | case BW_IR_LOP3_LUT: { |
| 742 | /* LOP3.LUT Rd, Ra, Rb, Rc, imm8, !PT. LOP3_XOR above is this form with Rc = RZ, LUT 0x3c. */ |
| 743 | uint32_t a = (insn->src1_vreg >= 0) ? (uint32_t)(src1 & 0xff) : 0xffU; |
| 744 | uint32_t b = (insn->src2_vreg >= 0) ? (uint32_t)(src2 & 0xff) : 0xffU; |
| 745 | w[0] = 0x7212U | ((uint32_t)(dst & 0xff) << 16) | (a << 24); |
| 746 | w[1] = b; |
| 747 | w[2] = 0x078e0000U | ((insn->imm & 0xffU) << 8) | (uint32_t)(src3 & 0xff); |
| 748 | w[3] = insn->control ? insn->control : 0x001fca00; |
| 749 | break; |
| 750 | } |
| 751 | |
| 752 | case BW_IR_SHF_L_U32: { |
| 753 | /* SHF.L.U32 Rd, Ra, Rb, Rc: register-amount form (0x7219) of SHF_R's 0x7819, with the |
| 754 | * .R (w[2] bit 12) and .HI (bit 16) flags clear. Rc is the funnel high word (RZ). */ |
| 755 | uint32_t a = (insn->src1_vreg >= 0) ? (uint32_t)(src1 & 0xff) : 0xffU; |
| 756 | uint32_t b = (insn->src2_vreg >= 0) ? (uint32_t)(src2 & 0xff) : 0xffU; |
| 757 | w[0] = 0x7219U | ((uint32_t)(dst & 0xff) << 16) | (a << 24); |
| 758 | w[1] = b; |
| 759 | w[2] = 0x00000600U | (uint32_t)(src3 & 0xff); |
| 760 | w[3] = insn->control ? insn->control : 0x001fca00; |
| 761 | break; |
| 762 | } |
| 763 | |
| 764 | case BW_IR_IMAD_HI_U32: { |
| 765 | /* IMAD.HI.U32 Rd, Ra, Rb, Rc: opcode 0x227 with the IMAD.WIDE.U32 modifier word. */ |
| 766 | uint32_t a = (insn->src1_vreg >= 0) ? (uint32_t)(src1 & 0xff) : 0xffU; |
| 767 | uint32_t b = (insn->src2_vreg >= 0) ? (uint32_t)(src2 & 0xff) : 0xffU; |
| 768 | w[0] = 0x7227U | ((uint32_t)(dst & 0xff) << 16) | (a << 24); |
| 769 | w[1] = b; |
| 770 | w[2] = 0x078e0000U | (uint32_t)(src3 & 0xff); |
| 771 | w[3] = insn->control ? insn->control : 0x001fca00; |
| 772 | break; |
| 773 | } |
| 774 | |
| 775 | default: |
| 776 | return -1; |
| 777 | } |
| 778 | return 0; |
| 779 | } |
| 780 | |
| 781 | /* Prime race cut (2026-10-05): golden words for LOP3_LUT, SHF_L_U32 and IMAD_HI_U32. |
| 782 | * Each expected (w0, w1, w2) is the low 96 bits of an instruction nvcc 13.0.88 emitted |
| 783 | * for sm_121 (nvcc -arch=sm_121 -cubin, cuobjdump -sass; source and dump in |
| 784 | * bench/prime_race/tests/encoding_oracle.md). Control words (w3) are scheduler output and |
| 785 | * not compared. nvcc is an offline oracle only; nothing here links CUDA. 0 = all good. */ |
| 786 | int omega_blackwell_verify_codegen_fixtures_intops(void) { |
| 787 | OmegaRegAlloc ra; |
| 788 | memset(&ra, 0, sizeof(ra)); |
| 789 | static const int phys[] = { 0, 2, 6, 7, 9, 11, 13 }; |
| 790 | for (int i = 0; i < (int)(sizeof phys / sizeof phys[0]); i++) ra.vreg_to_phys[i] = phys[i]; |
| 791 | enum { R0 = 0, R2, R6, R7, R9, R11, R13, RZ = -1 }; |
| 792 | static const struct { |
| 793 | BlackwellIROpcode op; int d, a, b, c; uint32_t imm; uint32_t w0, w1, w2; const char *text; |
| 794 | } fx[] = { |
| 795 | { BW_IR_LOP3_LUT, R9, R7, R0, RZ, 0xfc, 0x07097212U, 0x00000000U, 0x078efcffU, "LOP3.LUT R9, R7, R0, RZ, 0xfc, !PT" }, |
| 796 | { BW_IR_LOP3_LUT, R11, R6, R7, R0, 0x78, 0x060b7212U, 0x00000007U, 0x078e7800U, "LOP3.LUT R11, R6, R7, R0, 0x78, !PT" }, |
| 797 | { BW_IR_LOP3_LUT, R13, R6, R7, R0, 0x10, 0x060d7212U, 0x00000007U, 0x078e1000U, "LOP3.LUT R13, R6, R7, R0, 0x10, !PT" }, |
| 798 | { BW_IR_LOP3_LUT, R7, RZ, R9, RZ, 0x33, 0xff077212U, 0x00000009U, 0x078e33ffU, "LOP3.LUT R7, RZ, R9, RZ, 0x33, !PT" }, |
| 799 | { BW_IR_SHF_L_U32, R7, R9, R2, RZ, 0, 0x09077219U, 0x00000002U, 0x000006ffU, "SHF.L.U32 R7, R9, R2, RZ" }, |
| 800 | { BW_IR_SHF_L_U32, R7, R0, R7, RZ, 0, 0x00077219U, 0x00000007U, 0x000006ffU, "SHF.L.U32 R7, R0, R7, RZ" }, |
| 801 | { BW_IR_IMAD_HI_U32, R9, R9, R0, R6, 0, 0x09097227U, 0x00000000U, 0x078e0006U, "IMAD.HI.U32 R9, R9, R0, R6" }, |
| 802 | { BW_IR_IMAD_HI_U32, R7, R0, R7, RZ, 0, 0x00077227U, 0x00000007U, 0x078e00ffU, "IMAD.HI.U32 R7, R0, R7, RZ" }, |
| 803 | }; |
| 804 | for (int i = 0; i < (int)(sizeof fx / sizeof fx[0]); i++) { |
| 805 | BlackwellIRInsn in; |
| 806 | memset(&in, 0, sizeof in); |
| 807 | in.op = fx[i].op; in.dst_vreg = fx[i].d; in.src1_vreg = fx[i].a; in.src2_vreg = fx[i].b; |
| 808 | in.src3_vreg = fx[i].c; in.ureg = -1; in.imm = fx[i].imm; |
| 809 | uint32_t w[4]; |
| 810 | if (encode_single_insn(&in, &ra, w) != 0) return -(2 * i + 1); |
| 811 | if (w[0] != fx[i].w0 || w[1] != fx[i].w1 || w[2] != fx[i].w2) return -(2 * i + 2); |
| 812 | } |
| 813 | return 0; |
| 814 | } |
| 815 | |
| 816 | int omega_blackwell_encode_one(const BlackwellIRInsn *insn, uint32_t w[4]) { |
| 817 | if (!insn || !w) return -1; |
| 818 | OmegaRegAlloc ra; |
| 819 | memset(&ra, 0, sizeof ra); |
| 820 | for (int i = 0; i < BW_MAX_VREGS; i++) ra.vreg_to_phys[i] = i; |
| 821 | for (int i = 0; i < BW_MAX_UVREGS; i++) ra.uvreg_to_phys[i] = i; |
| 822 | return encode_single_insn(insn, &ra, w); |
| 823 | } |
| 824 | |
| 825 | /* omega #308: BSSY / BSYNC golden words. Oracle: nvcc 13.0.88 -arch=sm_121 -cubin -O3 on the |
| 826 | * kernels in docs/gpu-reconvergence-308.md, cuobjdump -sass (low 64-bit word, then high; the |
| 827 | * top 23 bits of the high word are the control word and are not compared here): |
| 828 | * k_loop [0070] BSSY.RECONVERGENT B0, 0x140 ; 0x000000c000007945 0x000fe20003800200 (delta 13) |
| 829 | * k_nested [0070] BSSY.RECONVERGENT B0, 0x300 ; 0x0000028000007945 0x000fe20003800200 (delta 41) |
| 830 | * k_break [0070] BSSY.RECONVERGENT B0, 0x110 ; 0x0000009000007945 0x000fe20003800200 (delta 10) |
| 831 | * k_loop [0130] BSYNC.RECONVERGENT B0 ; 0x0000000000007941 0x000fea0003800200 |
| 832 | * Barrier ids other than B0 and the predicated forms are decoded by tools/reconv_nvdisasm_check.sh |
| 833 | * (nvdisasm -b SM121 text), not by this table. */ |
| 834 | int omega_blackwell_verify_codegen_fixtures_reconv(void) { |
| 835 | static const struct { BlackwellIROpcode op; int delta; int bar; uint32_t w0, w1, w2; } fx[] = { |
| 836 | { BW_IR_BSSY, 13, 0, 0x00007945U, 0x000000c0U, 0x03800200U }, |
| 837 | { BW_IR_BSSY, 41, 0, 0x00007945U, 0x00000280U, 0x03800200U }, |
| 838 | { BW_IR_BSSY, 10, 0, 0x00007945U, 0x00000090U, 0x03800200U }, |
| 839 | { BW_IR_BSYNC, 0, 0, 0x00007941U, 0x00000000U, 0x03800200U }, |
| 840 | }; |
| 841 | for (int i = 0; i < (int)(sizeof fx / sizeof fx[0]); i++) { |
| 842 | BlackwellIRInsn in; |
| 843 | memset(&in, 0, sizeof in); |
| 844 | in.op = fx[i].op; in.dst_vreg = -1; in.src1_vreg = -1; in.src2_vreg = -1; in.src3_vreg = -1; in.ureg = -1; |
| 845 | in.imm = (uint32_t)fx[i].delta; in.bar_reg = (uint8_t)fx[i].bar; |
| 846 | uint32_t w[4]; |
| 847 | if (omega_blackwell_encode_one(&in, w) != 0) return -(2 * i + 1); |
| 848 | if (w[0] != fx[i].w0 || w[1] != fx[i].w1 || w[2] != fx[i].w2) return -(2 * i + 2); |
| 849 | /* control word defaults mirror nvcc's: BSSY 0x000fe200, BSYNC 0x000fea00 */ |
| 850 | if (w[3] != (fx[i].op == BW_IR_BSSY ? 0x000fe200U : 0x000fea00U)) return -(2 * i + 2); |
| 851 | } |
| 852 | /* refusals: a BSSY must name a later instruction (delta >= 2), barrier ids stop at B15 */ |
| 853 | BlackwellIRInsn bad; uint32_t w[4]; |
| 854 | 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; |
| 855 | bad.imm = 1; bad.bar_reg = 0; |
| 856 | if (omega_blackwell_encode_one(&bad, w) == 0) return -101; |
| 857 | bad.imm = (uint32_t)-3; |
| 858 | if (omega_blackwell_encode_one(&bad, w) == 0) return -102; |
| 859 | bad.imm = 5; bad.bar_reg = 16; |
| 860 | if (omega_blackwell_encode_one(&bad, w) == 0) return -103; |
| 861 | bad.op = BW_IR_BSYNC; bad.imm = 0; bad.bar_reg = 16; |
| 862 | if (omega_blackwell_encode_one(&bad, w) == 0) return -104; |
| 863 | bad.bar_reg = 15; |
| 864 | if (omega_blackwell_encode_one(&bad, w) != 0 || w[0] != (0x00007941U | (15U << 16))) return -105; |
| 865 | return 0; |
| 866 | } |
| 867 | |
| 868 | /* FB-1 cut 4: word-level fixtures for the four additive ops (checked against the |
| 869 | * nvdisasm decodes recorded in docs/numeric/FB1_CUT4_ELEMENTWISE.md). 0 = all good. */ |
| 870 | int omega_blackwell_verify_codegen_fixtures_fb1cut4(void) { |
| 871 | OmegaRegAlloc ra; |
| 872 | memset(&ra, 0, sizeof(ra)); |
| 873 | ra.vreg_to_phys[0] = 2; /* R2 */ |
| 874 | ra.vreg_to_phys[1] = 4; /* R4 */ |
| 875 | ra.vreg_to_phys[2] = 9; /* R9 */ |
| 876 | ra.vreg_to_phys[3] = 10; /* R10 */ |
| 877 | ra.vreg_to_phys[4] = 8; /* R8 */ |
| 878 | uint32_t w[4]; |
| 879 | |
| 880 | /* MUFU.EX2 R2, R4 */ |
| 881 | BlackwellIRInsn ex2 = { .op = BW_IR_MUFU_EX2, .dst_vreg = 0, .src1_vreg = 1 }; |
| 882 | if (encode_single_insn(&ex2, &ra, w) != 0) return -1; |
| 883 | if (w[0] != 0x00027308U || w[1] != 4 || w[2] != 0x00000800U) return -2; |
| 884 | /* BAR.SYNC.DEFER_BLOCKING 0x0 */ |
| 885 | BlackwellIRInsn bar = { .op = BW_IR_BAR_SYNC }; |
| 886 | if (encode_single_insn(&bar, &ra, w) != 0) return -3; |
| 887 | if (w[0] != 0x00007b1dU || w[1] != 0 || w[2] != 0x00010000U || w[3] != 0x000fec00U) return -4; |
| 888 | /* LDS R9, [R10+URZ] */ |
| 889 | BlackwellIRInsn lds = { .op = BW_IR_LDS32, .dst_vreg = 2, .src1_vreg = 3 }; |
| 890 | if (encode_single_insn(&lds, &ra, w) != 0) return -5; |
| 891 | if (w[0] != 0x0a097984U || w[1] != 0xff || w[2] != 0x08000800U) return -6; |
| 892 | /* STS [R8+URZ], R2 */ |
| 893 | BlackwellIRInsn sts = { .op = BW_IR_STS32, .src1_vreg = 4, .src2_vreg = 0 }; |
| 894 | if (encode_single_insn(&sts, &ra, w) != 0) return -7; |
| 895 | if (w[0] != 0x08007988U || w[1] != 2 || w[2] != 0x080008ffU) return -8; |
| 896 | return 0; |
| 897 | } |
| 898 | |
| 899 | int omega_bw_encode_program(const BlackwellIRProgram *prog, uint8_t *code_buf, size_t max_len, size_t *out_len) { |
| 900 | if (!prog || !code_buf || !out_len) return -1; |
| 901 | |
| 902 | size_t padded_insns = (prog->count + 7) & ~7ULL; |
| 903 | if (padded_insns < 32) padded_insns = 32; |
| 904 | |
| 905 | size_t req_bytes = padded_insns * 16; |
| 906 | if (max_len < req_bytes) return -1; |
| 907 | |
| 908 | uint8_t *p = code_buf; |
| 909 | for (size_t i = 0; i < padded_insns; i++) { |
| 910 | uint32_t w[4]; |
| 911 | if (i < prog->count) { |
| 912 | if (encode_single_insn(&prog->insns[i], &prog->regalloc, w) != 0) { |
| 913 | return -1; |
| 914 | } |
| 915 | } else { |
| 916 | /* Pad with NOP */ |
| 917 | w[0] = 0x00007918; |
| 918 | w[1] = 0x00000000; |
| 919 | w[2] = 0x00000000; |
| 920 | w[3] = 0x000fc000; |
| 921 | } |
| 922 | |
| 923 | p[0] = (uint8_t)(w[0] & 0xff); |
| 924 | p[1] = (uint8_t)((w[0] >> 8) & 0xff); |
| 925 | p[2] = (uint8_t)((w[0] >> 16) & 0xff); |
| 926 | p[3] = (uint8_t)((w[0] >> 24) & 0xff); |
| 927 | |
| 928 | p[4] = (uint8_t)(w[1] & 0xff); |
| 929 | p[5] = (uint8_t)((w[1] >> 8) & 0xff); |
| 930 | p[6] = (uint8_t)((w[1] >> 16) & 0xff); |
| 931 | p[7] = (uint8_t)((w[1] >> 24) & 0xff); |
| 932 | |
| 933 | p[8] = (uint8_t)(w[2] & 0xff); |
| 934 | p[9] = (uint8_t)((w[2] >> 8) & 0xff); |
| 935 | p[10] = (uint8_t)((w[2] >> 16) & 0xff); |
| 936 | p[11] = (uint8_t)((w[2] >> 24) & 0xff); |
| 937 | |
| 938 | p[12] = (uint8_t)(w[3] & 0xff); |
| 939 | p[13] = (uint8_t)((w[3] >> 8) & 0xff); |
| 940 | p[14] = (uint8_t)((w[3] >> 16) & 0xff); |
| 941 | p[15] = (uint8_t)((w[3] >> 24) & 0xff); |
| 942 | |
| 943 | p += 16; |
| 944 | } |
| 945 | |
| 946 | *out_len = req_bytes; |
| 947 | return 0; |
| 948 | } |
| 949 | |
| 950 | int omega_blackwell_codegen_matmul_i32(const OmegaMatMulSpec *spec, OmegaBlackwellKernel *kernel) { |
| 951 | if (!spec || !kernel) return -1; |
| 952 | if (spec->m == 0 || spec->k == 0 || spec->n == 0) return -1; |
| 953 | |
| 954 | /* Fail closed loudly if K exceeds bounded unroll limit */ |
| 955 | if (spec->k > 32) { |
| 956 | fprintf(stderr, "ERROR: MatMul integer codegen exceeds bounded unroll limit (K=%u > 32)\n", spec->k); |
| 957 | return -1; |
| 958 | } |
| 959 | |
| 960 | BlackwellIRProgram prog; |
| 961 | omega_bw_ir_init(&prog); |
| 962 | |
| 963 | /* 1. Allocate virtual registers */ |
| 964 | /* 64-bit base pointers */ |
| 965 | int v_ptr_a = omega_bw_ir_alloc_vreg64(&prog); |
| 966 | int v_ptr_b = omega_bw_ir_alloc_vreg64(&prog); |
| 967 | int v_ptr_c = omega_bw_ir_alloc_vreg64(&prog); |
| 968 | int uv_desc = omega_bw_ir_alloc_uvreg64(&prog); |
| 969 | |
| 970 | /* 32-bit CTA / Thread indexing */ |
| 971 | int v_ctaid_x = omega_bw_ir_alloc_vreg(&prog); |
| 972 | int v_ctaid_y = omega_bw_ir_alloc_vreg(&prog); |
| 973 | int v_tid_x = omega_bw_ir_alloc_vreg(&prog); |
| 974 | int v_tid_y = omega_bw_ir_alloc_vreg(&prog); |
| 975 | |
| 976 | /* 2D Coordinates & loop invariants */ |
| 977 | int v_col = omega_bw_ir_alloc_vreg(&prog); |
| 978 | int v_row = omega_bw_ir_alloc_vreg(&prog); |
| 979 | int v_row_k = omega_bw_ir_alloc_vreg(&prog); |
| 980 | int v_const_1 = omega_bw_ir_alloc_vreg(&prog); |
| 981 | int v_acc = omega_bw_ir_alloc_vreg(&prog); |
| 982 | |
| 983 | /* Working registers for memory address and operands */ |
| 984 | int v_idx_a = omega_bw_ir_alloc_vreg(&prog); |
| 985 | int v_off_a = omega_bw_ir_alloc_vreg64(&prog); |
| 986 | int v_val_a = omega_bw_ir_alloc_vreg(&prog); |
| 987 | |
| 988 | int v_idx_b = omega_bw_ir_alloc_vreg(&prog); |
| 989 | int v_off_b = omega_bw_ir_alloc_vreg64(&prog); |
| 990 | int v_val_b = omega_bw_ir_alloc_vreg(&prog); |
| 991 | |
| 992 | int v_idx_c = omega_bw_ir_alloc_vreg(&prog); |
| 993 | int v_off_c = omega_bw_ir_alloc_vreg64(&prog); |
| 994 | |
| 995 | if (v_off_c < 0) return -1; |
| 996 | |
| 997 | /* 2. Load 64-bit global memory descriptor into uniform register pair */ |
| 998 | /* LDCU.64 uv_desc, c[0x0][0x358] */ |
| 999 | BlackwellIRInsn insn_desc = { .op = BW_IR_LDCU64, .dst_vreg = uv_desc, .imm = 0x358, .is_uniform = true }; |
| 1000 | omega_bw_ir_append(&prog, &insn_desc); |
| 1001 | |
| 1002 | /* 3. Load 64-bit base pointers from Constant Bank 0 */ |
| 1003 | /* LDC.64 v_ptr_a, c[0x0][0x380] */ |
| 1004 | BlackwellIRInsn insn_ptra = { .op = BW_IR_LDC64, .dst_vreg = v_ptr_a, .imm = 0x380 }; |
| 1005 | omega_bw_ir_append(&prog, &insn_ptra); |
| 1006 | |
| 1007 | /* LDC.64 v_ptr_b, c[0x0][0x388] */ |
| 1008 | BlackwellIRInsn insn_ptrb = { .op = BW_IR_LDC64, .dst_vreg = v_ptr_b, .imm = 0x388 }; |
| 1009 | omega_bw_ir_append(&prog, &insn_ptrb); |
| 1010 | |
| 1011 | /* LDC.64 v_ptr_c, c[0x0][0x390] */ |
| 1012 | BlackwellIRInsn insn_ptrc = { .op = BW_IR_LDC64, .dst_vreg = v_ptr_c, .imm = 0x390 }; |
| 1013 | omega_bw_ir_append(&prog, &insn_ptrc); |
| 1014 | |
| 1015 | /* 4. Query 2D CTA and Thread coordinates */ |
| 1016 | /* S2R v_ctaid_x, SR_CTAID.X */ |
| 1017 | BlackwellIRInsn insn_ctax = { .op = BW_IR_S2R, .dst_vreg = v_ctaid_x, .imm = BW_SR_CTAID_X }; |
| 1018 | omega_bw_ir_append(&prog, &insn_ctax); |
| 1019 | |
| 1020 | /* S2R v_ctaid_y, SR_CTAID.Y */ |
| 1021 | BlackwellIRInsn insn_ctay = { .op = BW_IR_S2R, .dst_vreg = v_ctaid_y, .imm = BW_SR_CTAID_Y }; |
| 1022 | omega_bw_ir_append(&prog, &insn_ctay); |
| 1023 | |
| 1024 | /* S2R v_tid_x, SR_TID.X */ |
| 1025 | BlackwellIRInsn insn_tidx = { .op = BW_IR_S2R, .dst_vreg = v_tid_x, .imm = BW_SR_TID_X }; |
| 1026 | omega_bw_ir_append(&prog, &insn_tidx); |
| 1027 | |
| 1028 | /* S2R v_tid_y, SR_TID.Y */ |
| 1029 | BlackwellIRInsn insn_tidy = { .op = BW_IR_S2R, .dst_vreg = v_tid_y, .imm = BW_SR_TID_Y }; |
| 1030 | omega_bw_ir_append(&prog, &insn_tidy); |
| 1031 | |
| 1032 | /* 5. Compute 2D (row, col) coordinates */ |
| 1033 | /* Standard 16x16 tile threads: col = ctaid_x * 16 + tid_x */ |
| 1034 | 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 }; |
| 1035 | omega_bw_ir_append(&prog, &insn_col); |
| 1036 | |
| 1037 | /* row = ctaid_y * 16 + tid_y */ |
| 1038 | 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 }; |
| 1039 | omega_bw_ir_append(&prog, &insn_row); |
| 1040 | |
| 1041 | /* 6. Precompute row base index: v_row_k = v_row * K + RZ */ |
| 1042 | 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 }; |
| 1043 | omega_bw_ir_append(&prog, &insn_row_k); |
| 1044 | |
| 1045 | /* 7. Initialize v_const_1 = 1 for immediate linear indexing */ |
| 1046 | BlackwellIRInsn insn_c1 = { .op = BW_IR_MOV_IMM, .dst_vreg = v_const_1, .imm = 1 }; |
| 1047 | omega_bw_ir_append(&prog, &insn_c1); |
| 1048 | |
| 1049 | /* 8. Initialize accumulator to zero: MOV v_acc, RZ */ |
| 1050 | BlackwellIRInsn insn_init_acc = { .op = BW_IR_MOV_RZ, .dst_vreg = v_acc }; |
| 1051 | omega_bw_ir_append(&prog, &insn_init_acc); |
| 1052 | |
| 1053 | /* 9. Synthesize inner accumulation loop across K iterations */ |
| 1054 | for (uint32_t step = 0; step < spec->k; step++) { |
| 1055 | /* A[row, step]: idx_a = v_row_k + step = v_const_1 * step + v_row_k */ |
| 1056 | 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 }; |
| 1057 | omega_bw_ir_append(&prog, &insn_idxa); |
| 1058 | |
| 1059 | /* off_a = v_ptr_a + idx_a * 4 */ |
| 1060 | 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 }; |
| 1061 | omega_bw_ir_append(&prog, &insn_offa); |
| 1062 | |
| 1063 | /* Load A[row, step]: LDG.E v_val_a, desc[uv_desc][v_off_a.64] */ |
| 1064 | 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 }; |
| 1065 | omega_bw_ir_append(&prog, &insn_lda); |
| 1066 | |
| 1067 | /* B[step, col]: idx_b = step * N + col = v_const_1 * (step * N) + v_col */ |
| 1068 | uint32_t step_n = step * spec->n; |
| 1069 | 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 }; |
| 1070 | omega_bw_ir_append(&prog, &insn_idxb); |
| 1071 | |
| 1072 | /* off_b = v_ptr_b + idx_b * 4 */ |
| 1073 | 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 }; |
| 1074 | omega_bw_ir_append(&prog, &insn_offb); |
| 1075 | |
| 1076 | /* Load B[step, col]: LDG.E v_val_b, desc[uv_desc][v_off_b.64] */ |
| 1077 | 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 }; |
| 1078 | omega_bw_ir_append(&prog, &insn_ldb); |
| 1079 | |
| 1080 | /* Multiply and accumulate: IMAD v_acc, v_val_a, v_val_b, v_acc */ |
| 1081 | 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 }; |
| 1082 | omega_bw_ir_append(&prog, &insn_acc); |
| 1083 | } |
| 1084 | |
| 1085 | /* 10. Compute output index: idx_c = row * N + col */ |
| 1086 | 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 }; |
| 1087 | omega_bw_ir_append(&prog, &insn_idxc); |
| 1088 | |
| 1089 | /* off_c = v_ptr_c + idx_c * 4 */ |
| 1090 | 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 }; |
| 1091 | omega_bw_ir_append(&prog, &insn_offc); |
| 1092 | |
| 1093 | /* Store C[row, col]: STG.E desc[uv_desc][v_off_c.64], v_acc */ |
| 1094 | BlackwellIRInsn insn_stc = { .op = BW_IR_STG_E, .src1_vreg = v_off_c, .src2_vreg = v_acc, .src3_vreg = -1, .ureg = uv_desc }; |
| 1095 | omega_bw_ir_append(&prog, &insn_stc); |
| 1096 | |
| 1097 | /* 11. Kernel termination */ |
| 1098 | BlackwellIRInsn insn_exit = { .op = BW_IR_EXIT, .predicate_p0 = false }; |
| 1099 | omega_bw_ir_append(&prog, &insn_exit); |
| 1100 | |
| 1101 | BlackwellIRInsn insn_bra = { .op = BW_IR_BRA }; |
| 1102 | omega_bw_ir_append(&prog, &insn_bra); |
| 1103 | |
| 1104 | /* 12. Solve bounded deterministic register allocation */ |
| 1105 | if (omega_bw_regalloc_solve(&prog) != 0) { |
| 1106 | return -1; |
| 1107 | } |
| 1108 | |
| 1109 | /* 13. Allocate code buffer and encode instructions */ |
| 1110 | size_t padded_insns = (prog.count + 7) & ~7ULL; |
| 1111 | if (padded_insns < 32) padded_insns = 32; |
| 1112 | size_t alloc_size = padded_insns * 16; |
| 1113 | |
| 1114 | kernel->code = (uint8_t *)malloc(alloc_size); |
| 1115 | if (!kernel->code) return -1; |
| 1116 | |
| 1117 | size_t emitted_size = 0; |
| 1118 | if (omega_bw_encode_program(&prog, kernel->code, alloc_size, &emitted_size) != 0) { |
| 1119 | free(kernel->code); |
| 1120 | kernel->code = NULL; |
| 1121 | return -1; |
| 1122 | } |
| 1123 | |
| 1124 | kernel->code_size = emitted_size; |
| 1125 | kernel->insn_count = emitted_size / 16; |
| 1126 | kernel->gpr_count = prog.regalloc.peak_gpr_usage; |
| 1127 | kernel->uniform_gpr_count = prog.regalloc.peak_ugpr_usage; |
| 1128 | |
| 1129 | /* 14. Compute runtime SHA-256 digest of emitted machine code */ |
| 1130 | sha256_hash(kernel->code, kernel->code_size, kernel->code_digest); |
| 1131 | return 0; |
| 1132 | } |
| 1133 | |
| 1134 | int omega_blackwell_codegen_matmul_tensor_prog(const OmegaMatMulSpec *spec, BlackwellIRProgram *prog) { |
| 1135 | if (!spec || !prog) return -1; |
| 1136 | if (spec->m == 0 || spec->k == 0 || spec->n == 0) return -1; |
| 1137 | if (spec->precision != OMEGA_MATMUL_PRECISION_FP16 && spec->precision != OMEGA_MATMUL_PRECISION_BF16) return -1; |
| 1138 | if (spec->m % 16 != 0 || spec->n % 8 != 0 || spec->k != 16) { |
| 1139 | fprintf(stderr, "ERROR: MatMul tensor codegen requires M multiple of 16, N multiple of 8, K=16 (got %ux%ux%u)\n", |
| 1140 | spec->m, spec->k, spec->n); |
| 1141 | return -1; |
| 1142 | } |
| 1143 | |
| 1144 | omega_bw_ir_init(prog); |
| 1145 | |
| 1146 | /* 1. Allocate virtual registers */ |
| 1147 | /* 64-bit base pointers */ |
| 1148 | int v_ptr_a = omega_bw_ir_alloc_vreg64(prog); |
| 1149 | int v_ptr_b = omega_bw_ir_alloc_vreg64(prog); |
| 1150 | int v_ptr_c = omega_bw_ir_alloc_vreg64(prog); |
| 1151 | int uv_desc = omega_bw_ir_alloc_uvreg64(prog); |
| 1152 | |
| 1153 | /* 32-bit CTA / Thread indexing */ |
| 1154 | int v_tid = omega_bw_ir_alloc_vreg(prog); |
| 1155 | int v_ctaid_x = omega_bw_ir_alloc_vreg(prog); |
| 1156 | int v_ctaid_y = omega_bw_ir_alloc_vreg(prog); |
| 1157 | |
| 1158 | /* Tile and thread coordinates */ |
| 1159 | int v_group_id = omega_bw_ir_alloc_vreg(prog); |
| 1160 | int v_tid_in_grp = omega_bw_ir_alloc_vreg(prog); |
| 1161 | int v_row_base = omega_bw_ir_alloc_vreg(prog); |
| 1162 | int v_col_base = omega_bw_ir_alloc_vreg(prog); |
| 1163 | int v_r0 = omega_bw_ir_alloc_vreg(prog); |
| 1164 | int v_r1 = omega_bw_ir_alloc_vreg(prog); |
| 1165 | int v_col_b = omega_bw_ir_alloc_vreg(prog); |
| 1166 | int v_tid_col2 = omega_bw_ir_alloc_vreg(prog); |
| 1167 | int v_const_1 = omega_bw_ir_alloc_vreg(prog); |
| 1168 | |
| 1169 | /* Bundles for HMMA */ |
| 1170 | int v_ra = omega_bw_ir_alloc_vreg128(prog); /* Quad: Ra[0]..Ra[3] */ |
| 1171 | int v_rb = omega_bw_ir_alloc_vreg64(prog); /* Pair: Rb[0]..Rb[1] */ |
| 1172 | int v_rd = omega_bw_ir_alloc_vreg128(prog); /* Quad: Rd[0]..Rd[3] */ |
| 1173 | |
| 1174 | /* Scratch registers for indexing and loads */ |
| 1175 | int v_idx = omega_bw_ir_alloc_vreg(prog); |
| 1176 | int v_off = omega_bw_ir_alloc_vreg64(prog); |
| 1177 | int v_b_tmp0 = omega_bw_ir_alloc_vreg(prog); |
| 1178 | int v_b_tmp1 = omega_bw_ir_alloc_vreg(prog); |
| 1179 | |
| 1180 | if (v_b_tmp1 < 0) return -1; |
| 1181 | |
| 1182 | /* 2. Load 64-bit global memory descriptor into uniform register pair */ |
| 1183 | /* LDCU.64 uv_desc, c[0x0][0x358] */ |
| 1184 | BlackwellIRInsn insn_desc = { .op = BW_IR_LDCU64, .dst_vreg = uv_desc, .imm = 0x358, .is_uniform = true }; |
| 1185 | omega_bw_ir_append(prog, &insn_desc); |
| 1186 | |
| 1187 | /* 3. Load 64-bit base pointers from Constant Bank 0 */ |
| 1188 | /* LDC.64 v_ptr_a, c[0x0][0x380] */ |
| 1189 | BlackwellIRInsn insn_ptra = { .op = BW_IR_LDC64, .dst_vreg = v_ptr_a, .imm = 0x380 }; |
| 1190 | omega_bw_ir_append(prog, &insn_ptra); |
| 1191 | |
| 1192 | /* LDC.64 v_ptr_b, c[0x0][0x388] */ |
| 1193 | BlackwellIRInsn insn_ptrb = { .op = BW_IR_LDC64, .dst_vreg = v_ptr_b, .imm = 0x388 }; |
| 1194 | omega_bw_ir_append(prog, &insn_ptrb); |
| 1195 | |
| 1196 | /* LDC.64 v_ptr_c, c[0x0][0x390] */ |
| 1197 | BlackwellIRInsn insn_ptrc = { .op = BW_IR_LDC64, .dst_vreg = v_ptr_c, .imm = 0x390 }; |
| 1198 | omega_bw_ir_append(prog, &insn_ptrc); |
| 1199 | |
| 1200 | /* 4. Query Special Registers */ |
| 1201 | /* S2R v_tid, SR_TID.X */ |
| 1202 | BlackwellIRInsn insn_tidx = { .op = BW_IR_S2R, .dst_vreg = v_tid, .imm = BW_SR_TID_X }; |
| 1203 | omega_bw_ir_append(prog, &insn_tidx); |
| 1204 | |
| 1205 | /* S2R v_ctaid_x, SR_CTAID.X */ |
| 1206 | BlackwellIRInsn insn_cta_x = { .op = BW_IR_S2R, .dst_vreg = v_ctaid_x, .imm = BW_SR_CTAID_X }; |
| 1207 | omega_bw_ir_append(prog, &insn_cta_x); |
| 1208 | |
| 1209 | /* S2R v_ctaid_y, SR_CTAID.Y */ |
| 1210 | BlackwellIRInsn insn_cta_y = { .op = BW_IR_S2R, .dst_vreg = v_ctaid_y, .imm = BW_SR_CTAID_Y }; |
| 1211 | omega_bw_ir_append(prog, &insn_cta_y); |
| 1212 | |
| 1213 | /* 5. Set up constant 1 */ |
| 1214 | BlackwellIRInsn insn_c1 = { .op = BW_IR_MOV_IMM, .dst_vreg = v_const_1, .imm = 1 }; |
| 1215 | omega_bw_ir_append(prog, &insn_c1); |
| 1216 | |
| 1217 | /* 6. Extract warp-level tile and thread coordinates */ |
| 1218 | /* group_id = tid >> 2 */ |
| 1219 | BlackwellIRInsn insn_grp = { .op = BW_IR_SHF_R, .dst_vreg = v_group_id, .src1_vreg = v_tid, .imm = 2 }; |
| 1220 | omega_bw_ir_append(prog, &insn_grp); |
| 1221 | |
| 1222 | /* tid_in_grp = tid & 3 */ |
| 1223 | BlackwellIRInsn insn_tig = { .op = BW_IR_LOP3_AND, .dst_vreg = v_tid_in_grp, .src1_vreg = v_tid, .imm = 3 }; |
| 1224 | omega_bw_ir_append(prog, &insn_tig); |
| 1225 | |
| 1226 | /* row_base = ctaid_y * 16 */ |
| 1227 | 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 }; |
| 1228 | omega_bw_ir_append(prog, &insn_rbase); |
| 1229 | |
| 1230 | /* col_base = ctaid_x * 8 */ |
| 1231 | 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 }; |
| 1232 | omega_bw_ir_append(prog, &insn_cbase); |
| 1233 | |
| 1234 | /* r0 = row_base + group_id */ |
| 1235 | BlackwellIRInsn insn_r0 = { .op = BW_IR_IADD3, .dst_vreg = v_r0, .src1_vreg = v_row_base, .src2_vreg = v_group_id, .src3_vreg = -1 }; |
| 1236 | omega_bw_ir_append(prog, &insn_r0); |
| 1237 | |
| 1238 | /* r1 = r0 + 8 */ |
| 1239 | 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 }; |
| 1240 | omega_bw_ir_append(prog, &insn_r1); |
| 1241 | |
| 1242 | /* col_b = col_base + group_id */ |
| 1243 | 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 }; |
| 1244 | omega_bw_ir_append(prog, &insn_colb); |
| 1245 | |
| 1246 | /* tid_col2 = tid_in_grp * 2 */ |
| 1247 | 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 }; |
| 1248 | omega_bw_ir_append(prog, &insn_tcol2); |
| 1249 | |
| 1250 | /* 7. Load A fragments (16x16 row-major, 2 bytes/element, row stride 32 bytes) */ |
| 1251 | /* Pair 0: a0, a1 at r0 * 32 + tid_col2 * 2 */ |
| 1252 | BlackwellIRInsn insn_a0_idx1 = { .op = BW_IR_IMAD, .dst_vreg = v_idx, .src1_vreg = v_r0, .src2_vreg = -1, .imm = 32, .src3_vreg = -1 }; |
| 1253 | omega_bw_ir_append(prog, &insn_a0_idx1); |
| 1254 | 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 }; |
| 1255 | omega_bw_ir_append(prog, &insn_a0_idx2); |
| 1256 | 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 }; |
| 1257 | omega_bw_ir_append(prog, &insn_a0_off); |
| 1258 | 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 }; |
| 1259 | omega_bw_ir_append(prog, &insn_lda0); |
| 1260 | |
| 1261 | /* Pair 1: a2, a3 at r1 * 32 + tid_col2 * 2 */ |
| 1262 | BlackwellIRInsn insn_a1_idx1 = { .op = BW_IR_IMAD, .dst_vreg = v_idx, .src1_vreg = v_r1, .src2_vreg = -1, .imm = 32, .src3_vreg = -1 }; |
| 1263 | omega_bw_ir_append(prog, &insn_a1_idx1); |
| 1264 | 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 }; |
| 1265 | omega_bw_ir_append(prog, &insn_a1_idx2); |
| 1266 | 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 }; |
| 1267 | omega_bw_ir_append(prog, &insn_a1_off); |
| 1268 | 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 }; |
| 1269 | omega_bw_ir_append(prog, &insn_lda1); |
| 1270 | |
| 1271 | /* Pair 2: a4, a5 at r0 * 32 + tid_col2 * 2 + 16 */ |
| 1272 | BlackwellIRInsn insn_a2_idx1 = { .op = BW_IR_IMAD, .dst_vreg = v_idx, .src1_vreg = v_r0, .src2_vreg = -1, .imm = 32, .src3_vreg = -1 }; |
| 1273 | omega_bw_ir_append(prog, &insn_a2_idx1); |
| 1274 | 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 }; |
| 1275 | omega_bw_ir_append(prog, &insn_a2_idx2); |
| 1276 | 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 }; |
| 1277 | omega_bw_ir_append(prog, &insn_a2_idx3); |
| 1278 | 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 }; |
| 1279 | omega_bw_ir_append(prog, &insn_a2_off); |
| 1280 | 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 }; |
| 1281 | omega_bw_ir_append(prog, &insn_lda2); |
| 1282 | |
| 1283 | /* Pair 3: a6, a7 at r1 * 32 + tid_col2 * 2 + 16 */ |
| 1284 | BlackwellIRInsn insn_a3_idx1 = { .op = BW_IR_IMAD, .dst_vreg = v_idx, .src1_vreg = v_r1, .src2_vreg = -1, .imm = 32, .src3_vreg = -1 }; |
| 1285 | omega_bw_ir_append(prog, &insn_a3_idx1); |
| 1286 | 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 }; |
| 1287 | omega_bw_ir_append(prog, &insn_a3_idx2); |
| 1288 | 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 }; |
| 1289 | omega_bw_ir_append(prog, &insn_a3_idx3); |
| 1290 | 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 }; |
| 1291 | omega_bw_ir_append(prog, &insn_a3_off); |
| 1292 | 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 }; |
| 1293 | omega_bw_ir_append(prog, &insn_lda3); |
| 1294 | |
| 1295 | /* 8. Load and pack B fragments (16xN row-major, 2 bytes/element) */ |
| 1296 | /* b0 at tid_col2 * N + col_b */ |
| 1297 | 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 }; |
| 1298 | omega_bw_ir_append(prog, &insn_b0_idx); |
| 1299 | 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 }; |
| 1300 | omega_bw_ir_append(prog, &insn_b0_off); |
| 1301 | 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 }; |
| 1302 | omega_bw_ir_append(prog, &insn_ldb0); |
| 1303 | |
| 1304 | /* b1 at (tid_col2 + 1) * N + col_b = b0_idx + N */ |
| 1305 | 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 }; |
| 1306 | omega_bw_ir_append(prog, &insn_b1_idx); |
| 1307 | 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 }; |
| 1308 | omega_bw_ir_append(prog, &insn_b1_off); |
| 1309 | 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 }; |
| 1310 | omega_bw_ir_append(prog, &insn_ldb1); |
| 1311 | |
| 1312 | /* Pack Rb[0] = (b1 << 16) | b0 (wait on barrier 2) */ |
| 1313 | 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 }; |
| 1314 | omega_bw_ir_append(prog, &insn_pack_b0); |
| 1315 | |
| 1316 | /* b2 at (tid_col2 + 8) * N + col_b */ |
| 1317 | 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 }; |
| 1318 | omega_bw_ir_append(prog, &insn_b2_idx1); |
| 1319 | 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 }; |
| 1320 | omega_bw_ir_append(prog, &insn_b2_idx2); |
| 1321 | 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 }; |
| 1322 | omega_bw_ir_append(prog, &insn_b2_off); |
| 1323 | 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 }; |
| 1324 | omega_bw_ir_append(prog, &insn_ldb2); |
| 1325 | |
| 1326 | /* b3 at (tid_col2 + 9) * N + col_b = b2_idx + N */ |
| 1327 | 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 }; |
| 1328 | omega_bw_ir_append(prog, &insn_b3_idx); |
| 1329 | 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 }; |
| 1330 | omega_bw_ir_append(prog, &insn_b3_off); |
| 1331 | 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 }; |
| 1332 | omega_bw_ir_append(prog, &insn_ldb3); |
| 1333 | |
| 1334 | /* Pack Rb[1] = (b3 << 16) | b2 (wait on barrier 2) */ |
| 1335 | 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 }; |
| 1336 | omega_bw_ir_append(prog, &insn_pack_b1); |
| 1337 | |
| 1338 | /* 9. Execute HMMA Tensor Core instruction (wait on barrier 1 for A loads) */ |
| 1339 | BlackwellIROpcode mma_op = (spec->precision == OMEGA_MATMUL_PRECISION_BF16) ? BW_IR_HMMA_BF16 : BW_IR_HMMA_F16; |
| 1340 | BlackwellIRInsn insn_hmma = { |
| 1341 | .op = mma_op, |
| 1342 | .dst_vreg = v_rd, |
| 1343 | .dst_subreg = 0, |
| 1344 | .src1_vreg = v_ra, |
| 1345 | .src1_subreg = 0, |
| 1346 | .src2_vreg = v_rb, |
| 1347 | .src2_subreg = 0, |
| 1348 | .src3_vreg = -1, /* RZ accumulator input */ |
| 1349 | .control = 0x002fca00 /* Wait on barrier 1 */ |
| 1350 | }; |
| 1351 | omega_bw_ir_append(prog, &insn_hmma); |
| 1352 | |
| 1353 | /* 10. Store C output fragments (16xN row-major, 4 bytes/float element) */ |
| 1354 | /* d0: row r0, col col_base + tid_col2 + 0 */ |
| 1355 | 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 }; |
| 1356 | omega_bw_ir_append(prog, &insn_d0_idx1); |
| 1357 | BlackwellIRInsn insn_d0_idx2 = { .op = BW_IR_IADD3, .dst_vreg = v_idx, .src1_vreg = v_idx, .src2_vreg = v_tid_col2, .src3_vreg = -1 }; |
| 1358 | omega_bw_ir_append(prog, &insn_d0_idx2); |
| 1359 | 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 }; |
| 1360 | omega_bw_ir_append(prog, &insn_d0_off); |
| 1361 | BlackwellIRInsn insn_std0 = { .op = BW_IR_STG_E, .src1_vreg = v_off, .src2_vreg = v_rd, .src2_subreg = 0, .ureg = uv_desc, .control = 0x000fe200 }; |
| 1362 | omega_bw_ir_append(prog, &insn_std0); |
| 1363 | |
| 1364 | /* d1: row r0, col col_base + tid_col2 + 1 */ |
| 1365 | 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 }; |
| 1366 | omega_bw_ir_append(prog, &insn_d1_idx); |
| 1367 | 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 }; |
| 1368 | omega_bw_ir_append(prog, &insn_d1_off); |
| 1369 | BlackwellIRInsn insn_std1 = { .op = BW_IR_STG_E, .src1_vreg = v_off, .src2_vreg = v_rd, .src2_subreg = 1, .ureg = uv_desc, .control = 0x000fe200 }; |
| 1370 | omega_bw_ir_append(prog, &insn_std1); |
| 1371 | |
| 1372 | /* d2: row r1, col col_base + tid_col2 + 0 */ |
| 1373 | 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 }; |
| 1374 | omega_bw_ir_append(prog, &insn_d2_idx1); |
| 1375 | BlackwellIRInsn insn_d2_idx2 = { .op = BW_IR_IADD3, .dst_vreg = v_idx, .src1_vreg = v_idx, .src2_vreg = v_tid_col2, .src3_vreg = -1 }; |
| 1376 | omega_bw_ir_append(prog, &insn_d2_idx2); |
| 1377 | 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 }; |
| 1378 | omega_bw_ir_append(prog, &insn_d2_off); |
| 1379 | BlackwellIRInsn insn_std2 = { .op = BW_IR_STG_E, .src1_vreg = v_off, .src2_vreg = v_rd, .src2_subreg = 2, .ureg = uv_desc, .control = 0x000fe200 }; |
| 1380 | omega_bw_ir_append(prog, &insn_std2); |
| 1381 | |
| 1382 | /* d3: row r1, col col_base + tid_col2 + 1 */ |
| 1383 | 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 }; |
| 1384 | omega_bw_ir_append(prog, &insn_d3_idx); |
| 1385 | 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 }; |
| 1386 | omega_bw_ir_append(prog, &insn_d3_off); |
| 1387 | BlackwellIRInsn insn_std3 = { .op = BW_IR_STG_E, .src1_vreg = v_off, .src2_vreg = v_rd, .src2_subreg = 3, .ureg = uv_desc, .control = 0x000fe200 }; |
| 1388 | omega_bw_ir_append(prog, &insn_std3); |
| 1389 | |
| 1390 | /* 11. Program Exit */ |
| 1391 | BlackwellIRInsn insn_exit = { .op = BW_IR_EXIT }; |
| 1392 | omega_bw_ir_append(prog, &insn_exit); |
| 1393 | BlackwellIRInsn insn_bra = { .op = BW_IR_BRA }; |
| 1394 | omega_bw_ir_append(prog, &insn_bra); |
| 1395 | |
| 1396 | /* 12. Run bounded register allocation */ |
| 1397 | if (omega_bw_regalloc_solve(prog) != 0) { |
| 1398 | return -1; |
| 1399 | } |
| 1400 | return 0; |
| 1401 | } |
| 1402 | |
| 1403 | int omega_blackwell_codegen_matmul_tensor(const OmegaMatMulSpec *spec, OmegaBlackwellKernel *kernel) { |
| 1404 | if (!spec || !kernel) return -1; |
| 1405 | BlackwellIRProgram prog; |
| 1406 | if (omega_blackwell_codegen_matmul_tensor_prog(spec, &prog) != 0) { |
| 1407 | return -1; |
| 1408 | } |
| 1409 | |
| 1410 | size_t max_bytes = 4096; |
| 1411 | kernel->code = malloc(max_bytes); |
| 1412 | if (!kernel->code) return -1; |
| 1413 | |
| 1414 | size_t emitted_size = 0; |
| 1415 | if (omega_bw_encode_program(&prog, kernel->code, max_bytes, &emitted_size) != 0) { |
| 1416 | free(kernel->code); |
| 1417 | kernel->code = NULL; |
| 1418 | return -1; |
| 1419 | } |
| 1420 | |
| 1421 | kernel->code_size = emitted_size; |
| 1422 | kernel->insn_count = emitted_size / 16; |
| 1423 | kernel->gpr_count = prog.regalloc.peak_gpr_usage; |
| 1424 | kernel->uniform_gpr_count = prog.regalloc.peak_ugpr_usage; |
| 1425 | |
| 1426 | sha256_hash(kernel->code, kernel->code_size, kernel->code_digest); |
| 1427 | return 0; |
| 1428 | } |
| 1429 | |
| 1430 | /* ---- FB-1 cut 1b: looped tensor matmul ------------------------------------------ |
| 1431 | * |
| 1432 | * Control words (bundle bits 105..125, decoded from the chip-proven kernel and from |
| 1433 | * the vendor compiler's SASS for an equivalent HMMA loop on this chip, 2026-10-04): |
| 1434 | * stall = bits 9..12 of w[3], yield = 13, write barrier = 14..16, read barrier = |
| 1435 | * 17..19, wait mask = 20..25. 7 in a barrier field means "none". |
| 1436 | * LDG (A) 0x001ea800 stall 4, yield, sets SB2 |
| 1437 | * LDG.U16 (B) 0x002ea800 stall 4, yield, sets SB2 |
| 1438 | * pack IMAD 0x004fca00 stall 5, waits SB2 (every outstanding load has landed) |
| 1439 | * HMMA 0x002fca00 stall 5 (fixed latency: the vendor compiler sets no |
| 1440 | * write barrier on HMMA either; consumers wait by stall count) |
| 1441 | * STG 0x0007e200 stall 1, yield, read barrier SB3 (the store may read its |
| 1442 | * data/address registers late; the next writer waits SB3) |
| 1443 | * wait SB3 mask bit 23 (0x00800000) on the first instruction that overwrites a |
| 1444 | * register a store reads: the C pointer advance. |
| 1445 | * Loop-carried registers are kept live to the end of their loop (keep_alive) because |
| 1446 | * the allocator is a straight-line linear scan and knows nothing about back edges. |
| 1447 | */ |
| 1448 | static void bw_keep_alive(BlackwellIRProgram *prog, int from_idx, int to_idx, int last_idx) { |
| 1449 | /* every vreg first defined in [from_idx, to_idx) stays live until last_idx */ |
| 1450 | for (int v = 0; v < prog->regalloc.num_vregs; v++) { |
| 1451 | OmegaLiveInterval *iv = &prog->regalloc.intervals[v]; |
| 1452 | if (iv->first_def >= from_idx && iv->first_def < to_idx && iv->last_use < last_idx) |
| 1453 | iv->last_use = last_idx; |
| 1454 | } |
| 1455 | } |
| 1456 | |
| 1457 | #define BW_APPEND(prog, ...) do { BlackwellIRInsn _i = { __VA_ARGS__ }; if (omega_bw_ir_append((prog), &_i) < 0) return -1; } while (0) |
| 1458 | |
| 1459 | int omega_blackwell_codegen_matmul_tensor_loop_prog(const OmegaMatMulSpec *spec, uint32_t grid_x, |
| 1460 | int mutant, BlackwellIRProgram *prog) { |
| 1461 | if (!spec || !prog) return -1; |
| 1462 | if (spec->m == 0 || spec->k == 0 || spec->n == 0) return -1; |
| 1463 | if (spec->precision != OMEGA_MATMUL_PRECISION_FP16 && spec->precision != OMEGA_MATMUL_PRECISION_BF16) return -1; |
| 1464 | if (spec->m % 16 != 0 || spec->n % 8 != 0 || spec->k % 16 != 0) { |
| 1465 | fprintf(stderr, "ERROR: looped MatMul tensor codegen requires M%%16, N%%8, K%%16 (got %ux%ux%u)\n", |
| 1466 | spec->m, spec->k, spec->n); |
| 1467 | return -1; |
| 1468 | } |
| 1469 | const uint32_t K = spec->k, N = spec->n, NT = N / 8, KSTEPS = K / 16; |
| 1470 | if (grid_x == 0 || grid_x > NT) return -1; |
| 1471 | /* every immediate below must fit 32 bits: 32 * N (B row-block stride in bytes) is the largest */ |
| 1472 | if ((uint64_t)N * 32u > 0xffffffffull || (uint64_t)grid_x * 32u > 0xffffffffull) return -1; |
| 1473 | if (mutant && KSTEPS < 2) return -1; |
| 1474 | |
| 1475 | omega_bw_ir_init(prog); |
| 1476 | |
| 1477 | 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); |
| 1478 | int uv_desc = omega_bw_ir_alloc_uvreg64(prog); |
| 1479 | 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); |
| 1480 | 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); |
| 1481 | 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); |
| 1482 | 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); |
| 1483 | 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); |
| 1484 | int v_pc0 = omega_bw_ir_alloc_vreg64(prog), v_pc1 = omega_bw_ir_alloc_vreg64(prog); |
| 1485 | 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); |
| 1486 | int v_ta2 = omega_bw_ir_alloc_vreg64(prog), v_ta3 = omega_bw_ir_alloc_vreg64(prog); |
| 1487 | 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); |
| 1488 | int v_tc1 = omega_bw_ir_alloc_vreg64(prog), v_tc3 = omega_bw_ir_alloc_vreg64(prog); |
| 1489 | 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); |
| 1490 | int v_b0 = omega_bw_ir_alloc_vreg(prog), v_b1 = omega_bw_ir_alloc_vreg(prog); |
| 1491 | if (v_b1 < 0 || uv_desc < 0) return -1; |
| 1492 | |
| 1493 | const uint32_t CTRL_LDA = 0x001ea800u, CTRL_LDB = 0x002ea800u, CTRL_PACK = 0x004fca00u; |
| 1494 | const uint32_t CTRL_HMMA = 0x002fca00u, CTRL_STG_RDBAR3 = 0x0007e200u, CTRL_IMADW_WAIT3 = 0x009fcc00u; |
| 1495 | const uint32_t CTRL_MOVRZ_WAIT3 = 0x008fe200u; |
| 1496 | |
| 1497 | /* prologue: descriptor, pointers, ids */ |
| 1498 | BW_APPEND(prog, .op = BW_IR_LDCU64, .dst_vreg = uv_desc, .imm = 0x358, .is_uniform = true); |
| 1499 | BW_APPEND(prog, .op = BW_IR_LDC64, .dst_vreg = v_ptr_a, .imm = 0x380); |
| 1500 | BW_APPEND(prog, .op = BW_IR_LDC64, .dst_vreg = v_ptr_b, .imm = 0x388); |
| 1501 | BW_APPEND(prog, .op = BW_IR_LDC64, .dst_vreg = v_ptr_c, .imm = 0x390); |
| 1502 | BW_APPEND(prog, .op = BW_IR_S2R, .dst_vreg = v_tid, .imm = BW_SR_TID_X); |
| 1503 | BW_APPEND(prog, .op = BW_IR_S2R, .dst_vreg = v_ctaid_x, .imm = BW_SR_CTAID_X); |
| 1504 | BW_APPEND(prog, .op = BW_IR_S2R, .dst_vreg = v_ctaid_y, .imm = BW_SR_CTAID_Y); |
| 1505 | BW_APPEND(prog, .op = BW_IR_MOV_IMM, .dst_vreg = v_c1, .imm = 1); |
| 1506 | BW_APPEND(prog, .op = BW_IR_SHF_R, .dst_vreg = v_group, .src1_vreg = v_tid, .imm = 2); |
| 1507 | BW_APPEND(prog, .op = BW_IR_LOP3_AND, .dst_vreg = v_tig, .src1_vreg = v_tid, .imm = 3); |
| 1508 | BW_APPEND(prog, .op = BW_IR_IMAD, .dst_vreg = v_tcol2, .src1_vreg = v_tig, .src2_vreg = -1, .imm = 2, .src3_vreg = -1); |
| 1509 | 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); |
| 1510 | BW_APPEND(prog, .op = BW_IR_IMAD, .dst_vreg = v_r1, .src1_vreg = v_c1, .src2_vreg = -1, .imm = 8, .src3_vreg = v_r0); |
| 1511 | /* pa0 = A + (r0*K + tcol2)*2 ; pa1 likewise with r1 (A row-major, K bf16 per row) */ |
| 1512 | BW_APPEND(prog, .op = BW_IR_IMAD, .dst_vreg = v_idx, .src1_vreg = v_r0, .src2_vreg = -1, .imm = K, .src3_vreg = v_tcol2); |
| 1513 | 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); |
| 1514 | BW_APPEND(prog, .op = BW_IR_IMAD, .dst_vreg = v_idx, .src1_vreg = v_r1, .src2_vreg = -1, .imm = K, .src3_vreg = v_tcol2); |
| 1515 | 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); |
| 1516 | /* col = ctaid_x (first column tile of this CTA) */ |
| 1517 | BW_APPEND(prog, .op = BW_IR_IMAD, .dst_vreg = v_col, .src1_vreg = v_ctaid_x, .src2_vreg = -1, .imm = 1, .src3_vreg = -1); |
| 1518 | /* pbcol = B + (tcol2*N + col*8 + group)*2 */ |
| 1519 | BW_APPEND(prog, .op = BW_IR_IMAD, .dst_vreg = v_idx, .src1_vreg = v_tcol2, .src2_vreg = -1, .imm = N, .src3_vreg = v_group); |
| 1520 | BW_APPEND(prog, .op = BW_IR_IMAD, .dst_vreg = v_idx, .src1_vreg = v_col, .src2_vreg = -1, .imm = 8, .src3_vreg = v_idx); |
| 1521 | 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); |
| 1522 | /* pc0 = C + (r0*N + col*8 + tcol2)*4 ; pc1 with r1 */ |
| 1523 | BW_APPEND(prog, .op = BW_IR_IMAD, .dst_vreg = v_idx, .src1_vreg = v_r0, .src2_vreg = -1, .imm = N, .src3_vreg = v_tcol2); |
| 1524 | BW_APPEND(prog, .op = BW_IR_IMAD, .dst_vreg = v_idx, .src1_vreg = v_col, .src2_vreg = -1, .imm = 8, .src3_vreg = v_idx); |
| 1525 | 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); |
| 1526 | BW_APPEND(prog, .op = BW_IR_IMAD, .dst_vreg = v_idx, .src1_vreg = v_r1, .src2_vreg = -1, .imm = N, .src3_vreg = v_tcol2); |
| 1527 | BW_APPEND(prog, .op = BW_IR_IMAD, .dst_vreg = v_idx, .src1_vreg = v_col, .src2_vreg = -1, .imm = 8, .src3_vreg = v_idx); |
| 1528 | 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); |
| 1529 | |
| 1530 | /* ---- column-tile loop ---- */ |
| 1531 | const int L_col = (int)prog->count; |
| 1532 | /* accumulator = 0 (the first write waits SB3: the previous tile's stores may still read it) */ |
| 1533 | BW_APPEND(prog, .op = BW_IR_MOV_RZ, .dst_vreg = v_rd, .dst_subreg = 0, .control = CTRL_MOVRZ_WAIT3); |
| 1534 | BW_APPEND(prog, .op = BW_IR_MOV_RZ, .dst_vreg = v_rd, .dst_subreg = 1); |
| 1535 | BW_APPEND(prog, .op = BW_IR_MOV_RZ, .dst_vreg = v_rd, .dst_subreg = 2); |
| 1536 | BW_APPEND(prog, .op = BW_IR_MOV_RZ, .dst_vreg = v_rd, .dst_subreg = 3); |
| 1537 | /* K cursors start at the row/column bases (IMAD.WIDE with imm 0 is a 64-bit copy) */ |
| 1538 | 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); |
| 1539 | 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); |
| 1540 | 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); |
| 1541 | BW_APPEND(prog, .op = BW_IR_MOV_IMM, .dst_vreg = v_cnt, .imm = mutant ? KSTEPS - 1 : KSTEPS); |
| 1542 | |
| 1543 | /* ---- K loop: one 16x16 A fragment and one 16x8 B fragment per step ---- */ |
| 1544 | const int L_k = (int)prog->count; |
| 1545 | 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); |
| 1546 | 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); |
| 1547 | 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); |
| 1548 | 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); |
| 1549 | 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); |
| 1550 | /* A: a0,a1 at (r0, kk+tcol2) ; a2,a3 at (r1, ...) ; a4,a5 at (r0, +8) ; a6,a7 at (r1, +8) */ |
| 1551 | 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); |
| 1552 | 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); |
| 1553 | 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); |
| 1554 | 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); |
| 1555 | /* B: rows kk+tcol2, +1, +8, +9 at column col*8+group */ |
| 1556 | 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); |
| 1557 | 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); |
| 1558 | 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); |
| 1559 | 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); |
| 1560 | 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); |
| 1561 | 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); |
| 1562 | /* rd += ra * rb (accumulate in place) */ |
| 1563 | BW_APPEND(prog, .op = (spec->precision == OMEGA_MATMUL_PRECISION_BF16) ? BW_IR_HMMA_BF16 : BW_IR_HMMA_F16, |
| 1564 | .dst_vreg = v_rd, .dst_subreg = 0, .src1_vreg = v_ra, .src1_subreg = 0, .src2_vreg = v_rb, .src2_subreg = 0, |
| 1565 | .src3_vreg = v_rd, .src3_subreg = 0, .control = CTRL_HMMA); |
| 1566 | /* advance K: A by 16 bf16 (32 bytes), B by 16 rows (32 N bytes); every load above has landed (packs waited SB2) */ |
| 1567 | 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); |
| 1568 | 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); |
| 1569 | 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); |
| 1570 | BW_APPEND(prog, .op = BW_IR_IMAD, .dst_vreg = v_cnt, .src1_vreg = v_c1, .src2_vreg = -1, .imm = 0xffffffffu, .src3_vreg = v_cnt); |
| 1571 | BW_APPEND(prog, .op = BW_IR_ISETP_GE, .dst_vreg = -1, .src1_vreg = v_cnt, .src2_vreg = -1, .imm = 1, .src3_vreg = -1); |
| 1572 | { |
| 1573 | int here = (int)prog->count; |
| 1574 | BW_APPEND(prog, .op = BW_IR_BRA, .imm = (uint32_t)(int32_t)(L_k - here), .predicate_p0 = true); |
| 1575 | } |
| 1576 | const int L_k_end = (int)prog->count - 1; |
| 1577 | |
| 1578 | /* ---- store the 16x8 tile: d0 (r0, c), d1 (r0, c+1), d2 (r1, c), d3 (r1, c+1) ---- */ |
| 1579 | 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); |
| 1580 | 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); |
| 1581 | 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); |
| 1582 | 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); |
| 1583 | 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); |
| 1584 | 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); |
| 1585 | /* next column tile of this CTA: col += grid_x ; B by grid_x*8 bf16 ; C by grid_x*8 f32 (waits SB3 first) */ |
| 1586 | 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); |
| 1587 | 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); |
| 1588 | 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); |
| 1589 | 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); |
| 1590 | BW_APPEND(prog, .op = BW_IR_ISETP_GE, .dst_vreg = -1, .src1_vreg = v_col, .src2_vreg = -1, .imm = NT, .src3_vreg = -1); |
| 1591 | { |
| 1592 | int here = (int)prog->count; |
| 1593 | BW_APPEND(prog, .op = BW_IR_BRA, .imm = (uint32_t)(int32_t)(L_col - here), .predicate_p0 = true, .predicate_not = true); |
| 1594 | } |
| 1595 | const int L_col_end = (int)prog->count - 1; |
| 1596 | BW_APPEND(prog, .op = BW_IR_EXIT); |
| 1597 | BW_APPEND(prog, .op = BW_IR_BRA); |
| 1598 | |
| 1599 | /* loop-carried values live to the end of their loop */ |
| 1600 | bw_keep_alive(prog, 0, L_col, L_col_end); |
| 1601 | bw_keep_alive(prog, L_col, L_k, L_k_end); |
| 1602 | /* K-loop temporaries too: a load's address pair must never be recycled by a |
| 1603 | * later instruction of the same iteration (the load may read it late). */ |
| 1604 | bw_keep_alive(prog, L_k, L_k_end, L_k_end); |
| 1605 | /* the accumulator is read by the stores after the K loop */ |
| 1606 | if (prog->regalloc.intervals[v_rd].last_use < L_col_end) prog->regalloc.intervals[v_rd].last_use = L_col_end; |
| 1607 | |
| 1608 | return omega_bw_regalloc_solve(prog); |
| 1609 | } |
| 1610 | |
| 1611 | int omega_blackwell_codegen_matmul_tensor_loop(const OmegaMatMulSpec *spec, uint32_t grid_x, |
| 1612 | int mutant, OmegaBlackwellKernel *kernel) { |
| 1613 | if (!spec || !kernel) return -1; |
| 1614 | BlackwellIRProgram prog; |
| 1615 | if (omega_blackwell_codegen_matmul_tensor_loop_prog(spec, grid_x, mutant, &prog) != 0) return -1; |
| 1616 | size_t max_bytes = 4096; |
| 1617 | kernel->code = malloc(max_bytes); |
| 1618 | if (!kernel->code) return -1; |
| 1619 | size_t emitted_size = 0; |
| 1620 | if (omega_bw_encode_program(&prog, kernel->code, max_bytes, &emitted_size) != 0) { |
| 1621 | free(kernel->code); |
| 1622 | kernel->code = NULL; |
| 1623 | return -1; |
| 1624 | } |
| 1625 | kernel->code_size = emitted_size; |
| 1626 | kernel->insn_count = emitted_size / 16; |
| 1627 | kernel->gpr_count = prog.regalloc.peak_gpr_usage; |
| 1628 | kernel->uniform_gpr_count = prog.regalloc.peak_ugpr_usage; |
| 1629 | sha256_hash(kernel->code, kernel->code_size, kernel->code_digest); |
| 1630 | return 0; |
| 1631 | } |
| 1632 | |
| 1633 | int omega_blackwell_verify_codegen_fixtures(void) { |
| 1634 | OmegaRegAlloc ra; |
| 1635 | memset(&ra, 0, sizeof(ra)); |
| 1636 | ra.vreg_to_phys[0] = 2; |
| 1637 | ra.vreg_to_phys[1] = 4; |
| 1638 | ra.vreg_to_phys[2] = 6; |
| 1639 | ra.vreg_to_phys[3] = 7; |
| 1640 | ra.uvreg_to_phys[0] = 4; |
| 1641 | |
| 1642 | uint32_t w[4]; |
| 1643 | |
| 1644 | /* 1. MOV R7, RZ */ |
| 1645 | BlackwellIRInsn insn_mov_rz = { .op = BW_IR_MOV_RZ, .dst_vreg = 3 }; |
| 1646 | if (encode_single_insn(&insn_mov_rz, &ra, w) != 0) return -1; |
| 1647 | if (w[0] != (0x7202 | (7 << 16)) || w[1] != 0xff || w[2] != 0x00000f00) return -2; |
| 1648 | |
| 1649 | /* 2. MOV R2, 1 */ |
| 1650 | BlackwellIRInsn insn_mov_imm = { .op = BW_IR_MOV_IMM, .dst_vreg = 0, .imm = 1 }; |
| 1651 | if (encode_single_insn(&insn_mov_imm, &ra, w) != 0) return -3; |
| 1652 | if (w[0] != (0x7802 | (2 << 16)) || w[1] != 1 || w[2] != 0x00000f00) return -4; |
| 1653 | |
| 1654 | /* 3. Register-Register IMAD: IMAD R7, R2, R6, R7 */ |
| 1655 | BlackwellIRInsn insn_imad_rr = { .op = BW_IR_IMAD, .dst_vreg = 3, .src1_vreg = 0, .src2_vreg = 2, .src3_vreg = 3 }; |
| 1656 | if (encode_single_insn(&insn_imad_rr, &ra, w) != 0) return -5; |
| 1657 | if (w[0] != (0x7224 | (7 << 16) | (2 << 24)) || w[1] != 6 || w[2] != (0x078e0200 | 7)) return -6; |
| 1658 | |
| 1659 | /* 4. Immediate IMAD: IMAD R6, R2, 16, RZ */ |
| 1660 | BlackwellIRInsn insn_imad_imm = { .op = BW_IR_IMAD, .dst_vreg = 2, .src1_vreg = 0, .src2_vreg = -1, .imm = 16, .src3_vreg = -1 }; |
| 1661 | if (encode_single_insn(&insn_imad_imm, &ra, w) != 0) return -7; |
| 1662 | if (w[0] != (0x7824 | (6 << 16) | (2 << 24)) || w[1] != 16 || w[2] != 0x078e02ff) return -8; |
| 1663 | |
| 1664 | /* 5. IMAD.WIDE: IMAD.WIDE R4, R2, 4, R4 */ |
| 1665 | BlackwellIRInsn insn_imad_wide = { .op = BW_IR_IMAD_WIDE, .dst_vreg = 1, .src1_vreg = 0, .imm = 4, .src3_vreg = 1 }; |
| 1666 | if (encode_single_insn(&insn_imad_wide, &ra, w) != 0) return -9; |
| 1667 | if (w[0] != (0x7825 | (4 << 16) | (2 << 24)) || w[1] != 4 || w[2] != (0x078e0000 | 4)) return -10; |
| 1668 | |
| 1669 | /* 6. LDC.64 R4, c[0x0][0x380] */ |
| 1670 | BlackwellIRInsn insn_ldc64 = { .op = BW_IR_LDC64, .dst_vreg = 1, .imm = 0x380 }; |
| 1671 | if (encode_single_insn(&insn_ldc64, &ra, w) != 0) return -11; |
| 1672 | if (w[0] != (0xff007b82 | (4 << 16)) || w[1] != ((0x380 << 6) & 0x00ffff00) || w[2] != 0x00000a00) return -12; |
| 1673 | |
| 1674 | /* 7. LDCU.64 UR4, c[0x0][0x358] */ |
| 1675 | BlackwellIRInsn insn_ldcu64 = { .op = BW_IR_LDCU64, .dst_vreg = 0, .imm = 0x358, .is_uniform = true }; |
| 1676 | if (encode_single_insn(&insn_ldcu64, &ra, w) != 0) return -13; |
| 1677 | if (w[0] != (0xff0077ac | (4 << 16)) || w[1] != ((0x358 << 5) & 0x00ffff00) || w[2] != 0x08000a00) return -14; |
| 1678 | |
| 1679 | /* 8. LDG.E R2, desc[UR4][R4.64] */ |
| 1680 | BlackwellIRInsn insn_ldg = { .op = BW_IR_LDG_E, .dst_vreg = 0, .src1_vreg = 1, .ureg = 0 }; |
| 1681 | if (encode_single_insn(&insn_ldg, &ra, w) != 0) return -15; |
| 1682 | if (w[0] != (0x7981 | (2 << 16) | (4 << 24)) || w[1] != 4 || w[2] != 0x0c1e1900) return -16; |
| 1683 | |
| 1684 | /* 9. STG.E desc[UR4][R4.64], R7 */ |
| 1685 | BlackwellIRInsn insn_stg = { .op = BW_IR_STG_E, .src1_vreg = 1, .src2_vreg = 3, .ureg = 0 }; |
| 1686 | if (encode_single_insn(&insn_stg, &ra, w) != 0) return -17; |
| 1687 | if (w[0] != (0x7986 | (4 << 24)) || w[1] != 7 || w[2] != (0x0c101900 | 4)) return -18; |
| 1688 | |
| 1689 | /* 10. S2R R2, SR_CTAID.X */ |
| 1690 | BlackwellIRInsn insn_s2r = { .op = BW_IR_S2R, .dst_vreg = 0, .imm = BW_SR_CTAID_X }; |
| 1691 | if (encode_single_insn(&insn_s2r, &ra, w) != 0) return -19; |
| 1692 | if (w[0] != (0x7919 | (2 << 16)) || w[2] != (BW_SR_CTAID_X << 8)) return -20; |
| 1693 | |
| 1694 | /* 11. HMMA.16816.F32 R4, R4, R2, RZ */ |
| 1695 | BlackwellIRInsn insn_hmma_f16 = { .op = BW_IR_HMMA_F16, .dst_vreg = 1, .src1_vreg = 1, .src2_vreg = 0, .src3_vreg = -1 }; |
| 1696 | if (encode_single_insn(&insn_hmma_f16, &ra, w) != 0) return -21; |
| 1697 | if (w[0] != 0x0404723c || w[1] != 0x00000002 || w[2] != 0x000018ff || w[3] != 0x000fe200) return -22; |
| 1698 | |
| 1699 | /* 12. HMMA.16816.F32.BF16 R4, R4, R2, RZ */ |
| 1700 | BlackwellIRInsn insn_hmma_bf16 = { .op = BW_IR_HMMA_BF16, .dst_vreg = 1, .src1_vreg = 1, .src2_vreg = 0, .src3_vreg = -1 }; |
| 1701 | uint32_t w_bf16[4]; |
| 1702 | if (encode_single_insn(&insn_hmma_bf16, &ra, w_bf16) != 0) return -23; |
| 1703 | if (w_bf16[0] != 0x0404723c || w_bf16[1] != 0x00000002 || w_bf16[2] != 0x000418ff || w_bf16[3] != 0x000fe200) return -24; |
| 1704 | |
| 1705 | /* 13. Differential Invariant: w[2] bit 18 toggles strictly between FP16 and BF16 */ |
| 1706 | if ((w[2] ^ w_bf16[2]) != 0x00040000) return -25; |
| 1707 | if (w[0] != w_bf16[0] || w[1] != w_bf16[1] || w[3] != w_bf16[3]) return -26; |
| 1708 | |
| 1709 | /* 14. SHF.R.U32.HI R4, RZ, 2, R2 */ |
| 1710 | BlackwellIRInsn insn_shf = { .op = BW_IR_SHF_R, .dst_vreg = 1, .src1_vreg = 0, .imm = 2 }; |
| 1711 | if (encode_single_insn(&insn_shf, &ra, w) != 0) return -27; |
| 1712 | if (w[0] != 0xff047819 || w[1] != 2 || w[2] != 0x00011602 || w[3] != 0x001fca00) return -28; |
| 1713 | |
| 1714 | /* 15. LOP3.LUT R4, R2, 3, RZ, 0xc0, !PT */ |
| 1715 | BlackwellIRInsn insn_lop3 = { .op = BW_IR_LOP3_AND, .dst_vreg = 1, .src1_vreg = 0, .imm = 3 }; |
| 1716 | if (encode_single_insn(&insn_lop3, &ra, w) != 0) return -29; |
| 1717 | if (w[0] != 0x02047812 || w[1] != 3 || w[2] != 0x078ec0ff || w[3] != 0x001fca00) return -30; |
| 1718 | |
| 1719 | /* 16. LDG.E.U16 R2, desc[UR4][R4.64] */ |
| 1720 | BlackwellIRInsn insn_ldg16 = { .op = BW_IR_LDG_E_U16, .dst_vreg = 0, .src1_vreg = 1, .ureg = 0 }; |
| 1721 | if (encode_single_insn(&insn_ldg16, &ra, w) != 0) return -31; |
| 1722 | if (w[0] != 0x04027981 || w[1] != 4 || w[2] != 0x0c1e1500 || w[3] != 0x000f2200) return -32; |
| 1723 | |
| 1724 | /* A zero-delta branch stays the self-branch the matmul kernels already emit. */ |
| 1725 | BlackwellIRInsn insn_bra0 = { .op = BW_IR_BRA }; |
| 1726 | if (encode_single_insn(&insn_bra0, &ra, w) != 0) return -33; |
| 1727 | if (w[0] != 0x00fc7947 || w[1] != 0xfffffffc || w[2] != 0x0383ffff || w[3] != 0x000fc000) return -34; |
| 1728 | |
| 1729 | BlackwellIRInsn insn_ge = { .op = BW_IR_ISETP_GE_U32, .src1_vreg = 1, .src2_vreg = 2 }; |
| 1730 | if (encode_single_insn(&insn_ge, &ra, w) != 0) return -35; |
| 1731 | if (w[0] != (0x720c | (4 << 24)) || w[1] != 6 || w[2] != 0x03f06070) return -36; |
| 1732 | |
| 1733 | BlackwellIRInsn insn_xor = { .op = BW_IR_LOP3_XOR, .dst_vreg = 1, .src1_vreg = 0, .src2_vreg = 2 }; |
| 1734 | if (encode_single_insn(&insn_xor, &ra, w) != 0) return -37; |
| 1735 | if (w[0] != (0x7212 | (4 << 16) | (2 << 24)) || w[1] != 6 || w[2] != 0x078e3cff) return -38; |
| 1736 | |
| 1737 | BlackwellIRInsn insn_ldg_ss = { .op = BW_IR_LDG_STRONG_SYS, .dst_vreg = 0, .src1_vreg = 1, .ureg = 0 }; |
| 1738 | if (encode_single_insn(&insn_ldg_ss, &ra, w) != 0) return -39; |
| 1739 | if (w[0] != (0x7981 | (2 << 16) | (4 << 24)) || w[1] != 4 || w[2] != 0x0c1f5900) return -40; |
| 1740 | /* 17. FADD R7, R2, R4 */ |
| 1741 | BlackwellIRInsn insn_fadd = { .op = BW_IR_FADD, .dst_vreg = 3, .src1_vreg = 0, .src2_vreg = 1 }; |
| 1742 | if (encode_single_insn(&insn_fadd, &ra, w) != 0) return -133; |
| 1743 | if (w[0] != 0x02077221 || w[1] != 4 || w[2] != 0 || w[3] != 0x004fc400) return -134; |
| 1744 | |
| 1745 | /* 18. FSUB R7, R2, R4 (negated src2) */ |
| 1746 | BlackwellIRInsn insn_fsub = { .op = BW_IR_FSUB, .dst_vreg = 3, .src1_vreg = 0, .src2_vreg = 1 }; |
| 1747 | if (encode_single_insn(&insn_fsub, &ra, w) != 0) return -135; |
| 1748 | if (w[0] != 0x02077221 || w[1] != 0x80000004 || w[2] != 0 || w[3] != 0x004fc400) return -136; |
| 1749 | |
| 1750 | /* 19. FMUL R7, R2, R4 */ |
| 1751 | BlackwellIRInsn insn_fmul = { .op = BW_IR_FMUL, .dst_vreg = 3, .src1_vreg = 0, .src2_vreg = 1 }; |
| 1752 | if (encode_single_insn(&insn_fmul, &ra, w) != 0) return -137; |
| 1753 | if (w[0] != 0x02077220 || w[1] != 4 || w[2] != 0x00400000 || w[3] != 0x004fc400) return -138; |
| 1754 | |
| 1755 | /* 20. FFMA R7, R2, R4, R6 */ |
| 1756 | BlackwellIRInsn insn_ffma = { .op = BW_IR_FFMA, .dst_vreg = 3, .src1_vreg = 0, .src2_vreg = 1, .src3_vreg = 2 }; |
| 1757 | if (encode_single_insn(&insn_ffma, &ra, w) != 0) return -139; |
| 1758 | if (w[0] != 0x02077223 || w[1] != 4 || w[2] != 6 || w[3] != 0x004fc400) return -140; |
| 1759 | |
| 1760 | /* 21. FMNMX_MIN R7, R2, R4 */ |
| 1761 | BlackwellIRInsn insn_min = { .op = BW_IR_FMNMX_MIN, .dst_vreg = 3, .src1_vreg = 0, .src2_vreg = 1 }; |
| 1762 | if (encode_single_insn(&insn_min, &ra, w) != 0) return -141; |
| 1763 | if (w[0] != 0x02077209 || w[1] != 4 || w[2] != 0x03800000 || w[3] != 0x004fc400) return -142; |
| 1764 | |
| 1765 | /* 22. FMNMX_MAX R7, R2, R4 */ |
| 1766 | BlackwellIRInsn insn_max = { .op = BW_IR_FMNMX_MAX, .dst_vreg = 3, .src1_vreg = 0, .src2_vreg = 1 }; |
| 1767 | if (encode_single_insn(&insn_max, &ra, w) != 0) return -143; |
| 1768 | if (w[0] != 0x02077209 || w[1] != 4 || w[2] != 0x07800000 || w[3] != 0x004fc400) return -144; |
| 1769 | |
| 1770 | /* 23. I2FP R7, R2 */ |
| 1771 | BlackwellIRInsn insn_i2f = { .op = BW_IR_I2FP, .dst_vreg = 3, .src1_vreg = 0 }; |
| 1772 | if (encode_single_insn(&insn_i2f, &ra, w) != 0) return -145; |
| 1773 | if (w[0] != 0x00077245 || w[1] != 2 || w[2] != 0x00201400 || w[3] != 0x004fe200) return -146; |
| 1774 | |
| 1775 | /* 24. F2I R7, R2 */ |
| 1776 | BlackwellIRInsn insn_f2i = { .op = BW_IR_F2I, .dst_vreg = 3, .src1_vreg = 0 }; |
| 1777 | if (encode_single_insn(&insn_f2i, &ra, w) != 0) return -147; |
| 1778 | if (w[0] != 0x00077305 || w[1] != 2 || w[2] != 0x0020f100 || w[3] != 0x004e2200) return -148; |
| 1779 | |
| 1780 | /* 25. MUFU_RCP R7, R2 */ |
| 1781 | BlackwellIRInsn insn_mufu_rcp = { .op = BW_IR_MUFU_RCP, .dst_vreg = 3, .src1_vreg = 0 }; |
| 1782 | if (encode_single_insn(&insn_mufu_rcp, &ra, w) != 0) return -149; |
| 1783 | if (w[0] != 0x00077308 || w[1] != 2 || w[2] != 0x00001000 || w[3] != 0x000e2400) return -150; |
| 1784 | |
| 1785 | /* 26. MUFU_RSQ R7, R2 */ |
| 1786 | BlackwellIRInsn insn_mufu_rsq = { .op = BW_IR_MUFU_RSQ, .dst_vreg = 3, .src1_vreg = 0 }; |
| 1787 | if (encode_single_insn(&insn_mufu_rsq, &ra, w) != 0) return -151; |
| 1788 | if (w[0] != 0x00077308 || w[1] != 2 || w[2] != 0x00001400 || w[3] != 0x000e2400) return -152; |
| 1789 | |
| 1790 | /* 27. SHFL_DOWN R7, R2, 16 */ |
| 1791 | BlackwellIRInsn insn_shfl = { .op = BW_IR_SHFL_DOWN, .dst_vreg = 3, .src1_vreg = 0, .imm = 16 }; |
| 1792 | if (encode_single_insn(&insn_shfl, &ra, w) != 0) return -153; |
| 1793 | if (w[0] != 0x02077f89 || w[1] != 0x0a001f00 || w[2] != 0x000e0000 || w[3] != 0x000e2400) return -154; |
| 1794 | |
| 1795 | /* 28. LDS R7, [R2] */ |
| 1796 | BlackwellIRInsn insn_lds = { .op = BW_IR_LDS, .dst_vreg = 3, .src1_vreg = 0, .imm = 0 }; |
| 1797 | if (encode_single_insn(&insn_lds, &ra, w) != 0) return -155; |
| 1798 | if (w[0] != 0x02077984 || w[1] != 0 || w[2] != 0 || w[3] != 0x000fe200) return -156; |
| 1799 | |
| 1800 | /* 29. STS [R2], R4 */ |
| 1801 | BlackwellIRInsn insn_sts = { .op = BW_IR_STS, .src1_vreg = 0, .src2_vreg = 1, .imm = 0 }; |
| 1802 | if (encode_single_insn(&insn_sts, &ra, w) != 0) return -157; |
| 1803 | if (w[0] != 0x02007388 || w[1] != 4 || w[2] != 0 || w[3] != 0x000fe200) return -158; |
| 1804 | |
| 1805 | return 0; |
| 1806 | } |
| 1807 | |
| 1808 | int omega_blackwell_test_regalloc_bounds(void) { |
| 1809 | BlackwellIRProgram prog; |
| 1810 | omega_bw_ir_init(&prog); |
| 1811 | |
| 1812 | int v0 = omega_bw_ir_alloc_vreg64(&prog); |
| 1813 | int v1 = omega_bw_ir_alloc_vreg(&prog); |
| 1814 | int v2 = omega_bw_ir_alloc_vreg64(&prog); |
| 1815 | int v3 = omega_bw_ir_alloc_vreg128(&prog); /* Test 128-bit quad allocation */ |
| 1816 | |
| 1817 | BlackwellIRInsn i0 = { .op = BW_IR_LDC64, .dst_vreg = v0, .imm = 0x380 }; |
| 1818 | omega_bw_ir_append(&prog, &i0); |
| 1819 | BlackwellIRInsn i1 = { .op = BW_IR_MOV_IMM, .dst_vreg = v1, .imm = 10 }; |
| 1820 | omega_bw_ir_append(&prog, &i1); |
| 1821 | BlackwellIRInsn i2 = { .op = BW_IR_LDC64, .dst_vreg = v2, .imm = 0x388 }; |
| 1822 | omega_bw_ir_append(&prog, &i2); |
| 1823 | BlackwellIRInsn i3 = { .op = BW_IR_HMMA_F16, .dst_vreg = v3, .src1_vreg = v3, .src2_vreg = v0, .src3_vreg = -1 }; |
| 1824 | omega_bw_ir_append(&prog, &i3); |
| 1825 | /* i4 uses v1, v2, v3 simultaneously so all intervals are concurrently active */ |
| 1826 | int v_sink = omega_bw_ir_alloc_vreg(&prog); |
| 1827 | BlackwellIRInsn i4 = { .op = BW_IR_IADD3, .dst_vreg = v_sink, .src1_vreg = v1, .src2_vreg = v2, .src3_vreg = v3 }; |
| 1828 | omega_bw_ir_append(&prog, &i4); |
| 1829 | |
| 1830 | if (omega_bw_regalloc_solve(&prog) != 0) return -1; |
| 1831 | |
| 1832 | int phys_v0 = prog.regalloc.vreg_to_phys[v0]; |
| 1833 | int phys_v1 = prog.regalloc.vreg_to_phys[v1]; |
| 1834 | int phys_v2 = prog.regalloc.vreg_to_phys[v2]; |
| 1835 | int phys_v3 = prog.regalloc.vreg_to_phys[v3]; |
| 1836 | |
| 1837 | if (phys_v0 % 2 != 0) return -2; |
| 1838 | if (phys_v2 % 2 != 0) return -3; |
| 1839 | if (phys_v3 % 4 != 0) return -4; /* 4-alignment for 128-bit quad */ |
| 1840 | if (phys_v1 == phys_v0 || phys_v1 == phys_v0 + 1) return -5; |
| 1841 | if (phys_v2 == phys_v0 || phys_v2 == phys_v0 + 1) return -6; |
| 1842 | for (int k = 0; k < 4; k++) { |
| 1843 | if (phys_v3 + k == phys_v0 || phys_v3 + k == phys_v0 + 1) return -7; |
| 1844 | if (phys_v3 + k == phys_v2 || phys_v3 + k == phys_v2 + 1) return -8; |
| 1845 | if (phys_v3 + k == phys_v1) return -9; |
| 1846 | } |
| 1847 | |
| 1848 | /* Test bounds refusal: intentionally attempt to allocate more registers than hardware capacity */ |
| 1849 | BlackwellIRProgram overflow_prog; |
| 1850 | omega_bw_ir_init(&overflow_prog); |
| 1851 | for (int i = 0; i < BW_PHYS_GPR_MAX + 5; i++) { |
| 1852 | int v = omega_bw_ir_alloc_vreg(&overflow_prog); |
| 1853 | if (v < 0) break; |
| 1854 | BlackwellIRInsn insn = { .op = BW_IR_MOV_IMM, .dst_vreg = v, .imm = (uint32_t)i }; |
| 1855 | omega_bw_ir_append(&overflow_prog, &insn); |
| 1856 | } |
| 1857 | for (size_t i = 0; i < overflow_prog.count; i++) { |
| 1858 | for (int v = 0; v < overflow_prog.regalloc.num_vregs; v++) { |
| 1859 | overflow_prog.regalloc.intervals[v].last_use = (int)overflow_prog.count - 1; |
| 1860 | } |
| 1861 | } |
| 1862 | if (omega_bw_regalloc_solve(&overflow_prog) == 0) { |
| 1863 | return -6; |
| 1864 | } |
| 1865 | |
| 1866 | return 0; |
| 1867 | } |
| 1868 | |
| 1869 | int omega_blackwell_test_codegen_variation(void) { |
| 1870 | OmegaMatMulSpec spec1, spec2; |
| 1871 | if (omega_matmul_spec_init(&spec1, 16, 16, 16, OMEGA_MATMUL_PRECISION_INT32) != 0) { |
| 1872 | printf("FAIL: spec1 init failed\n"); |
| 1873 | return -1; |
| 1874 | } |
| 1875 | if (omega_matmul_spec_init(&spec2, 32, 16, 64, OMEGA_MATMUL_PRECISION_INT32) != 0) { |
| 1876 | printf("FAIL: spec2 init failed\n"); |
| 1877 | return -1; |
| 1878 | } |
| 1879 | |
| 1880 | OmegaBlackwellKernel k1, k2; |
| 1881 | memset(&k1, 0, sizeof(k1)); |
| 1882 | memset(&k2, 0, sizeof(k2)); |
| 1883 | |
| 1884 | if (omega_blackwell_codegen_matmul(&spec1, &k1) != 0) { |
| 1885 | printf("FAIL: k1 codegen failed\n"); |
| 1886 | return -1; |
| 1887 | } |
| 1888 | if (omega_blackwell_codegen_matmul(&spec2, &k2) != 0) { |
| 1889 | printf("FAIL: k2 codegen failed\n"); |
| 1890 | omega_blackwell_kernel_free(&k1); |
| 1891 | return -1; |
| 1892 | } |
| 1893 | |
| 1894 | OmegaBlackwellRealizationIdentity id1, id2; |
| 1895 | omega_blackwell_bind_matmul_realization(&spec1, &k1, &id1); |
| 1896 | omega_blackwell_bind_matmul_realization(&spec2, &k2, &id2); |
| 1897 | |
| 1898 | printf("================================================================================\n"); |
| 1899 | printf(" OMEGA BLACKWELL CODEGEN VARIATION DEMONSTRATION (GATE 7 STAGE-1)\n"); |
| 1900 | printf("================================================================================\n"); |
| 1901 | printf("Config 1: 16x16x16 INT32\n"); |
| 1902 | printf(" Instructions: %zu (%zu bytes, %u GPRs, %u UGPRs)\n", k1.insn_count, k1.code_size, k1.gpr_count, k1.uniform_gpr_count); |
| 1903 | printf(" Spec ID: "); |
| 1904 | for (int i = 0; i < 16; i++) printf("%02x", spec1.spec_id[i]); |
| 1905 | printf("...\n"); |
| 1906 | printf(" Code Digest: "); |
| 1907 | for (int i = 0; i < 16; i++) printf("%02x", k1.code_digest[i]); |
| 1908 | printf("...\n"); |
| 1909 | printf(" Realization: "); |
| 1910 | for (int i = 0; i < 16; i++) printf("%02x", id1.realization_id[i]); |
| 1911 | printf("...\n\n"); |
| 1912 | |
| 1913 | printf("Config 2: 32x16x64 INT32\n"); |
| 1914 | printf(" Instructions: %zu (%zu bytes, %u GPRs, %u UGPRs)\n", k2.insn_count, k2.code_size, k2.gpr_count, k2.uniform_gpr_count); |
| 1915 | printf(" Spec ID: "); |
| 1916 | for (int i = 0; i < 16; i++) printf("%02x", spec2.spec_id[i]); |
| 1917 | printf("...\n"); |
| 1918 | printf(" Code Digest: "); |
| 1919 | for (int i = 0; i < 16; i++) printf("%02x", k2.code_digest[i]); |
| 1920 | printf("...\n"); |
| 1921 | printf(" Realization: "); |
| 1922 | for (int i = 0; i < 16; i++) printf("%02x", id2.realization_id[i]); |
| 1923 | printf("...\n"); |
| 1924 | printf("================================================================================\n"); |
| 1925 | |
| 1926 | /* Validation checks */ |
| 1927 | if (memcmp(spec1.spec_id, spec2.spec_id, 32) == 0) { |
| 1928 | printf("FAIL: spec IDs match (expected distinct)\n"); |
| 1929 | return -2; |
| 1930 | } |
| 1931 | if (memcmp(k1.code_digest, k2.code_digest, 32) == 0) { |
| 1932 | printf("FAIL: code digests match (expected distinct)\n"); |
| 1933 | return -3; |
| 1934 | } |
| 1935 | if (memcmp(id1.realization_id, id2.realization_id, 32) == 0) { |
| 1936 | printf("FAIL: realization IDs match (expected distinct)\n"); |
| 1937 | return -4; |
| 1938 | } |
| 1939 | |
| 1940 | printf("PASS: Dynamic codegen variation verified (distinct specifications, code digests, and realization IDs).\n"); |
| 1941 | |
| 1942 | omega_blackwell_kernel_free(&k1); |
| 1943 | omega_blackwell_kernel_free(&k2); |
| 1944 | return 0; |
| 1945 | } |