CUDApp
CUDA++: AIEN’s GPU machine-code toolchain, runtime and driver client for NVIDIA GPUs without the CUDA toolkit. Hardware coverage and driver replacement limits are explicit.
Brought by drakestapleton
CUDApp / src / omega_blackwell_codegen_tc.c
| 1 | /* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception */ |
| 2 | /* omega_blackwell_codegen_tc.c: tensor-core kernels for the OWN-30B one-pass prefill (T3). See the header. |
| 3 | * The emitter helpers below are copied from omega_blackwell_codegen_gemm.c (same chip-learned scheduling rules: every load |
| 4 | * sets the read barrier SB1 and the next load waits on it; prefetch loads hold scoreboards 3/4 until consumed). |
| 5 | * HMMA scheduling is taken from nvcc 13.0.88 -arch=sm_121 output (oracle sealed in |
| 6 | * ~/workspace/investigations/2026-10-13-t3-mma-oracle/oracle.{cu,sass}): |
| 7 | * independent HMMA.16816.F32.BF16 back to back: control 0x000ff000 / 0x020ff000 (stall 8, no barriers); |
| 8 | * a consumer of an HMMA result (dependent HMMA, store): HMMA stall 15 then NOP stall 13..14 (about 29 cycles), as |
| 9 | * omega_blackwell_verify_codegen_fixtures_fragmm records. */ |
| 10 | #include "omega_blackwell_codegen_tc.h" |
| 11 | #include <string.h> |
| 12 | #include "omega_gpu_om_ir.h" |
| 13 | #include "cudapp_native.h" |
| 14 | #if CUDAPP_MIGRATION_PENDING_C_CODEGEN_TC |
| 15 | #include "omega_bw_em.h" |
| 16 | #endif |
| 17 | #include "omega_gpu_fmt.h" |
| 18 | |
| 19 | #if CUDAPP_MIGRATION_PENDING_C_CODEGEN_TC |
| 20 | int omega_blackwell_codegen_tc_probe_prog(int mutant, BlackwellIRProgram *prog) { |
| 21 | if (!prog || mutant < 0 || mutant > 1) return -1; |
| 22 | omega_bw_ir_init(prog); |
| 23 | Em em; memset(&em, 0, sizeof em); em.p = prog; |
| 24 | Em *e = &em; |
| 25 | int udesc = UV64(e), pin = V64(e), pout = V64(e), tid = V(e), cx = V(e), pa = V64(e), po = V64(e); |
| 26 | int a = V128(e), b = V64(e), c = V128(e), d = V128(e); |
| 27 | if (e->err) return -1; |
| 28 | ldcu64(e, udesc, 0x358); |
| 29 | ldc64(e, pin, 0x380); ldc64(e, pout, 0x388); |
| 30 | s2r(e, tid, BW_SR_TID_X); s2r(e, cx, BW_SR_CTAID_X); |
| 31 | int pcase = V64(e), pb = V64(e); if (e->err) return -1; |
| 32 | imadw(e, pcase, cx, OMEGA_TC_PROBE_IN_BYTES, pin); imadw(e, pa, tid, 16, pcase); imadw(e, pb, tid, 8, pcase); |
| 33 | imadw(e, po, cx, OMEGA_TC_PROBE_OUT_BYTES, pout); imadw(e, po, tid, 16, po); |
| 34 | ldg_wide(e, BW_IR_LDG_E_128, a, pa, 0, udesc, 1); |
| 35 | ldg_wide(e, BW_IR_LDG_E_128, c, pa, 768, udesc, 0); |
| 36 | ldg_wide(e, BW_IR_LDG_E_64, b, pb, 512, udesc, 0); |
| 37 | /* wait for every load, then one HMMA, then 29 cycles before the stores read d */ |
| 38 | hmma(e, d, a, b, mutant == 1 ? NOREG : c, 15); |
| 39 | nop(e, 14); |
| 40 | for (unsigned q = 0; q < 4; q++) { BlackwellIRInsn i = I0(BW_IR_STG_E); i.src1_vreg = po; i.src2_vreg = d; i.src2_subreg = (uint8_t)q; i.imm = 4u * q; i.ureg = udesc; emit(e, i, K_STORE); } |
| 41 | tail(e); |
| 42 | if (e->err) return -1; |
| 43 | return omega_bw_regalloc_solve(prog); |
| 44 | } |
| 45 | #else |
| 46 | int omega_blackwell_codegen_tc_probe_prog(int mutant, BlackwellIRProgram *prog) { |
| 47 | if (!prog || mutant < 0 || mutant > 1) return -1; |
| 48 | return omega_gpu_om_ir_replay(omega_cg_tc, 0, (uint64_t)mutant, 0, 0, prog, NULL) == 0 ? 0 : -1; |
| 49 | } |
| 50 | #endif |
| 51 | |
| 52 | /* ======================= grouped FP8 GEMM on tensor cores (see the header) ======================= */ |
| 53 | |
| 54 | #if CUDAPP_MIGRATION_PENDING_C_CODEGEN_TC_HOST |
| 55 | int omega_tcgemm_spec_ok(const OmegaTcGemmSpec *s) { |
| 56 | if (!s) return 0; |
| 57 | if (s->k < 128u || s->k > OMEGA_TCGEMM_MAX_K || s->k % 128u) return 0; |
| 58 | if (s->rt != 1 && s->rt != 2 && s->rt != 4) return 0; |
| 59 | if (s->ct != 1 && s->ct != 2 && s->ct != 4) return 0; |
| 60 | if (s->rt * s->ct < 4u) return 0; /* >= 4 independent HMMAs between dependent ones (8 cycles each, nvcc's 29-cycle rule) */ |
| 61 | if (s->warps != 1 && s->warps != 2 && s->warps != 4 && s->warps != 8) return 0; |
| 62 | if ((16u * s->rt * 8u) % (32u * s->warps)) return 0; /* whole LDG.128 staging slots per thread */ |
| 63 | if (s->mutant < 0 || s->mutant > 4 || (s->mutant == 3 && s->rt < 2)) return 0; |
| 64 | return 1; |
| 65 | } |
| 66 | uint32_t omega_tcgemm_shared_bytes(const OmegaTcGemmSpec *s) { return omega_tcgemm_spec_ok(s) ? 2u * 16u * s->rt * 128u : 0u; } |
| 67 | uint32_t omega_tcgemm_span(const OmegaTcGemmSpec *s) { return omega_tcgemm_spec_ok(s) ? s->warps * 8u * s->ct : 0u; } |
| 68 | |
| 69 | static uint16_t bf16_rne(float f) { |
| 70 | uint32_t u; memcpy(&u, &f, 4); |
| 71 | if ((u & 0x7fffffffu) > 0x7f800000u) return (uint16_t)((u >> 16) | 0x40u); /* NaN stays NaN */ |
| 72 | u += 0x7fffu + ((u >> 16) & 1u); |
| 73 | return (uint16_t)(u >> 16); |
| 74 | } |
| 75 | void omega_tcgemm_permute_x(const float *x, uint32_t k, uint16_t *out) { |
| 76 | static const unsigned ord[4] = { 0, 2, 1, 3 }; |
| 77 | for (uint32_t q = 0; q + 4 <= k; q += 4) |
| 78 | for (unsigned i = 0; i < 4; i++) out[q + i] = bf16_rne(x[q + ord[i]] * 18446744073709551616.0f); |
| 79 | } |
| 80 | uint16_t omega_tcgemm_w_bf16(uint8_t c) { return (uint16_t)(((c & 0x7Fu) << 4) | ((c & 0x80u) << 8)); } |
| 81 | #else |
| 82 | static uint32_t tc_host_query(const OmegaTcGemmSpec *s, uint64_t op) { |
| 83 | if (!s) return 0; |
| 84 | uint64_t v[16] = {s->k,s->rt,s->ct,s->warps,(uint32_t)s->mutant}, r=0; |
| 85 | if (omega_tc_host(&r,op,v,16)) return 0; |
| 86 | return (uint32_t)r; |
| 87 | } |
| 88 | int omega_tcgemm_spec_ok(const OmegaTcGemmSpec *s) { return (int)tc_host_query(s,0); } |
| 89 | uint32_t omega_tcgemm_shared_bytes(const OmegaTcGemmSpec *s) { return tc_host_query(s,1); } |
| 90 | uint32_t omega_tcgemm_span(const OmegaTcGemmSpec *s) { return tc_host_query(s,2); } |
| 91 | void omega_tcgemm_permute_x(const float *x, uint32_t k, uint16_t *out) { |
| 92 | /* Only marshal complete groups; all scaling, rounding and ordering is Omega. */ |
| 93 | for (uint64_t q=0; q+4<=k; q+=4) { |
| 94 | uint32_t bits[4]; uint64_t v[16]={0},r=0; |
| 95 | memcpy(bits,x+q,sizeof bits); |
| 96 | for (unsigned j=0;j<4;j++) v[j]=bits[j]; |
| 97 | if (omega_tc_host(&r,6,v,16)) return; |
| 98 | out[q]=(uint16_t)r; out[q+1]=(uint16_t)(r>>16); |
| 99 | out[q+2]=(uint16_t)(r>>32); out[q+3]=(uint16_t)(r>>48); |
| 100 | } |
| 101 | } |
| 102 | uint16_t omega_tcgemm_w_bf16(uint8_t c) { |
| 103 | uint64_t v[16]={c},r=0; |
| 104 | if (omega_tc_host(&r,7,v,16)) return 0; |
| 105 | return (uint16_t)r; |
| 106 | } |
| 107 | #endif |
| 108 | |
| 109 | #if CUDAPP_MIGRATION_PENDING_C_CODEGEN_TC |
| 110 | /* one e4m3 word (bytes b0..b3) -> B register pair: lo = (bf(b0), bf(b2)), hi = (bf(b1), bf(b3)) */ |
| 111 | static void decode_word(Em *e, int bpair, int wq, unsigned word, int m, int s, int t) { |
| 112 | andi(e, m, wq, word, 0x007F007Fu); andi(e, s, wq, word, 0x00800080u); |
| 113 | imadi(e, t, m, 0, 16u, NOREG); |
| 114 | { BlackwellIRInsn i = I0(BW_IR_IMAD); i.dst_vreg = bpair; i.dst_subreg = 0; i.src1_vreg = s; i.imm = 256u; i.src3_vreg = t; emit(e, i, K_FIXED); } |
| 115 | andi(e, m, wq, word, 0x7F007F00u); andi(e, s, wq, word, 0x80008000u); |
| 116 | shr(e, t, m, 0, 4); |
| 117 | { BlackwellIRInsn i = I0(BW_IR_IADD3); i.dst_vreg = bpair; i.dst_subreg = 1; i.src1_vreg = t; i.src2_vreg = s; emit(e, i, K_FIXED); } |
| 118 | } |
| 119 | |
| 120 | int omega_blackwell_codegen_tcgemm_prog(const OmegaTcGemmSpec *spec, BlackwellIRProgram *prog) { |
| 121 | if (!omega_tcgemm_spec_ok(spec) || !prog) return -1; |
| 122 | const uint32_t K = spec->k, RT = spec->rt, CT = spec->ct, W = spec->warps, T = 32u * W; |
| 123 | const uint32_t AQN = RT * CT >= 8u ? 1u : 2u; /* A fragments in flight: one for the 8-tile shapes keeps peak <= 126 */ |
| 124 | const uint32_t BM = 16u * RT, KB = K / 128u, SPAN = W * 8u * CT, NJ = BM * 8u / T, BUF = BM * 128u; |
| 125 | omega_bw_ir_init(prog); |
| 126 | Em em; memset(&em, 0, sizeof em); em.p = prog; |
| 127 | Em *e = &em; |
| 128 | (void)lop3; |
| 129 | |
| 130 | /* ---- prologue: arguments, ids, tile entry ---- */ |
| 131 | int udesc = UV64(e), ptab = V64(e), pidx = V64(e), px = V64(e), py = V64(e), nreg = V(e); |
| 132 | int tid = V(e), cx = V(e), cy = V(e), warp = V(e), lane = V(e), g = V(e), tq = V(e), one = V(e), tent = V64(e); |
| 133 | int wbase = V64(e), sbase = V64(e), rc = V64(e), row0 = V(e), cnt = V(e); |
| 134 | int n0w = V(e), tmp = V(e), tmp2 = V(e), pw = V64(e), ps = V64(e), la = V(e), ysn = V(e), pyr = V64(e); |
| 135 | if (e->err) return -1; |
| 136 | ldcu64(e, udesc, 0x358); |
| 137 | ldc64(e, ptab, OMEGA_TCGEMM_ARG_TILES); ldc64(e, pidx, OMEGA_TCGEMM_ARG_ROWIDX); ldc64(e, px, OMEGA_TCGEMM_ARG_X); ldc64(e, py, OMEGA_TCGEMM_ARG_Y); |
| 138 | ldc32(e, nreg, OMEGA_TCGEMM_ARG_N); |
| 139 | s2r(e, tid, BW_SR_TID_X); s2r(e, cx, BW_SR_CTAID_X); s2r(e, cy, BW_SR_CTAID_Y); |
| 140 | movi(e, one, 1); |
| 141 | shr(e, warp, tid, 0, 5); andi(e, lane, tid, 0, 31); shr(e, g, lane, 0, 2); andi(e, tq, lane, 0, 3); |
| 142 | imadw(e, tent, cy, OMEGA_TCGEMM_TILE_BYTES, ptab); |
| 143 | ldg_wide(e, BW_IR_LDG_E_64, wbase, tent, 0, udesc, 1); |
| 144 | ldg_wide(e, BW_IR_LDG_E_64, sbase, tent, 8, udesc, 0); |
| 145 | ldg_wide(e, BW_IR_LDG_E_64, rc, tent, 16, udesc, 0); |
| 146 | imadi(e, row0, rc, 0, 1, NOREG); imadi(e, cnt, rc, 1, 1, NOREG); |
| 147 | /* this warp's first weight row; weight pointer row (n0w + g), byte 16 t; scale row block n0w / 128 */ |
| 148 | imadi(e, n0w, cx, 0, SPAN, NOREG); imadi(e, tmp, warp, 0, 8u * CT, NOREG); iadd(e, n0w, n0w, tmp); |
| 149 | iadd(e, tmp, n0w, g); imadw(e, pw, tmp, K, wbase); imadi(e, tmp2, tq, 0, 16u, NOREG); imadw(e, pw, tmp2, 1, pw); |
| 150 | shr(e, tmp, n0w, 0, 7); imadw(e, ps, tmp, KB * 4u, sbase); |
| 151 | imadi(e, la, lane, 0, 16u, NOREG); |
| 152 | imadi(e, ysn, nreg, 0, 4u, NOREG); |
| 153 | /* output pointer: y + ((row0 + g) * n + n0w + 2t) * 4 */ |
| 154 | iadd(e, tmp, row0, g); imadr(e, tmp, tmp, nreg, n0w); imadi(e, tmp2, tq, 0, 2u, NOREG); iadd(e, tmp, tmp, tmp2); imadw(e, pyr, tmp, 4u, py); |
| 155 | |
| 156 | /* ---- staging assignment: slot = tid + jj * T -> row r = slot / 8, words w0 = 4 * (tid % 8) of the 64-element chunk ---- */ |
| 157 | int gx[8], sa[8]; |
| 158 | for (uint32_t jj = 0; jj < NJ; jj++) { gx[jj] = V64(e); sa[jj] = V(e); } |
| 159 | int r = V(e), sidx = V64(e), srow = V(e), w0b = V(e), ts = V(e), s0 = V(e); |
| 160 | if (e->err) return -1; |
| 161 | andi(e, tmp, tid, 0, 7); |
| 162 | imadi(e, w0b, tmp, 0, 16u, NOREG); /* byte offset of word w0 in the row chunk: 4 words * 4 bytes */ |
| 163 | shr(e, ts, tmp, 0, 1); /* t of these words = w0 / 8 */ |
| 164 | andi(e, s0, tid, 0, 1); imadi(e, s0, s0, 0, 2u, NOREG); /* s0 = (w0 % 8) / 2 */ |
| 165 | for (uint32_t jj = 0; jj < NJ; jj++) { |
| 166 | shr(e, r, tid, 0, 3); imadi(e, r, one, 0, jj * T / 8u, r); |
| 167 | iadd(e, tmp, row0, r); imadw(e, sidx, tmp, 4u, pidx); |
| 168 | ldg32(e, srow, sidx, udesc, 1); |
| 169 | imadw(e, gx[jj], srow, K * 2u, px); imadw(e, gx[jj], w0b, 1, gx[jj]); |
| 170 | /* shared byte address ((i*4 + s0)*32 + g'*4 + t)*16 + h*4 with i = r/16, g' = r%8, h = (r/8)%2 */ |
| 171 | shr(e, tmp, r, 0, 4); imadi(e, tmp, tmp, 0, 4u, s0); imadi(e, tmp, tmp, 0, 32u, ts); |
| 172 | andi(e, tmp2, r, 0, 7); imadi(e, tmp2, tmp2, 0, 4u, tmp); imadi(e, sa[jj], tmp2, 0, 16u, NOREG); |
| 173 | shr(e, tmp2, r, 0, 3); andi(e, tmp2, tmp2, 0, 1); imadi(e, sa[jj], tmp2, 0, 4u, sa[jj]); |
| 174 | } |
| 175 | |
| 176 | /* ---- loop-wide registers ---- */ |
| 177 | int acc[16], tm[16], wa[4], wb[4], bp[4], aq[2], stg[8], sc = V(e), scur = V(e), cntk = V(e), dm = V(e), ds = V(e), dt = V(e), sv = V(e), onef = V(e); |
| 178 | for (uint32_t x = 0; x < RT * CT; x++) { acc[x] = V128(e); tm[x] = V128(e); } |
| 179 | for (uint32_t j = 0; j < CT; j++) { wa[j] = V128(e); wb[j] = V128(e); bp[j] = V64(e); } |
| 180 | for (uint32_t q = 0; q < AQN; q++) aq[q] = V128(e); |
| 181 | for (uint32_t jj = 0; jj < NJ; jj++) stg[jj] = V128(e); |
| 182 | if (e->err) return -1; |
| 183 | |
| 184 | /* issue the loads of one chunk: activations into stg (row chunk at gx + xoff), weights into wset (pw + woff) */ |
| 185 | #define ISSUE(wset, sb, xoff, woff, with_scale) do { \ |
| 186 | for (uint32_t jj = 0; jj < NJ; jj++) ldw_pf(e, BW_IR_LDG_E_128, stg[jj], gx[jj], (xoff), udesc, (sb)); \ |
| 187 | for (uint32_t j = 0; j < CT; j++) ldw_pf(e, BW_IR_LDG_E_128, (wset)[j], pw, (woff) + j * 8u * K, udesc, (sb)); \ |
| 188 | if (with_scale) ldw_pf(e, BW_IR_LDG_E, sc, ps, 0, udesc, (sb)); } while (0) |
| 189 | /* stage stg into buffer `buf` (byte offset): words w0, w0+1 -> s0 pair 0/1, w0+2, w0+3 -> s0+1 */ |
| 190 | #define STAGE(buf) do { for (uint32_t jj = 0; jj < NJ; jj++) { static const uint32_t off[4] = { 0, 8, 512, 520 }; \ |
| 191 | for (unsigned q = 0; q < 4; q++) { BlackwellIRInsn st = I0(BW_IR_STS32); imadi(e, tmp, one, 0, (buf) + off[q], sa[jj]); \ |
| 192 | st.src1_vreg = tmp; st.src2_vreg = stg[jj]; st.src2_subreg = (uint8_t)q; emit(e, st, K_STORE); } } bar_sync(e); } while (0) |
| 193 | |
| 194 | /* compute one 64-element chunk from shared buffer `buf` with weight set wset; first: the first chunk of a block */ |
| 195 | #define COMPUTE(buf, wset, first, odd) do { \ |
| 196 | for (uint32_t s = 0; s < 4; s++) { \ |
| 197 | if (spec->mutant == 4 && (odd) && s == 3) break; \ |
| 198 | for (uint32_t j = 0; j < CT; j++) decode_word(e, bp[j], (wset)[j], s, dm, ds, dt); \ |
| 199 | for (uint32_t i0 = 0; i0 < RT; i0 += AQN) { \ |
| 200 | const uint32_t nb = RT - i0 < AQN ? RT - i0 : AQN; \ |
| 201 | for (uint32_t q = 0; q < nb; q++) { uint32_t i = i0 + q, ii = spec->mutant == 3 ? (i ^ 1u) : i; \ |
| 202 | lds128(e, aq[q], la, (buf) + (ii * 4u + s) * 512u, q == 0); } \ |
| 203 | for (uint32_t q = 0; q < nb; q++) for (uint32_t j = 0; j < CT; j++) { uint32_t x = (i0 + q) * CT + j; \ |
| 204 | hmma(e, tm[x], aq[q], bp[j], ((first) && s == 0) ? NOREG : tm[x], 8); } \ |
| 205 | } \ |
| 206 | } } while (0) |
| 207 | |
| 208 | /* chunk 0 of block 0: activations to buffer 0, weights to set A, scale of block 0 */ |
| 209 | for (uint32_t x = 0; x < RT * CT; x++) for (unsigned q = 0; q < 4; q++) { BlackwellIRInsn mz = I0(BW_IR_MOV_RZ); mz.dst_vreg = acc[x]; mz.dst_subreg = (uint8_t)q; emit(e, mz, K_FIXED); } |
| 210 | ISSUE(wa, 3u, 0u, 0u, 1); |
| 211 | consume(e, 3); |
| 212 | STAGE(0u); |
| 213 | movi(e, cntk, KB); |
| 214 | if (spec->mutant == 1) movi(e, onef, 0x3f800000u); |
| 215 | |
| 216 | /* ---- K loop: one 128-wide block (two chunks) per iteration ---- */ |
| 217 | const int L_k = here(e); |
| 218 | loop_begin(e); |
| 219 | ISSUE(wb, 4u, 128u, 64u, 0); /* chunk 2kb+1 */ |
| 220 | consume(e, 3); |
| 221 | imadi(e, scur, sc, 0, 1u, NOREG); /* keep this block's scale: the next block's load reuses sc */ |
| 222 | COMPUTE(0u, wa, 1, 0); |
| 223 | consume(e, 4); |
| 224 | STAGE(BUF); |
| 225 | { /* chunk 2kb+2 (the next block's first chunk); the last iteration re-reads its own chunk (harmless, drained below) */ |
| 226 | int nf = V(e), inc = V(e); if (e->err) return -1; |
| 227 | imadi(e, nf, one, 0, 0xfffffffeu, cntk); shr(e, nf, nf, 0, 31); imadi(e, nf, nf, 0, 0xffffffffu, one); |
| 228 | for (uint32_t jj = 0; jj < NJ; jj++) imadw(e, gx[jj], nf, 256u, gx[jj]); |
| 229 | imadw(e, pw, nf, 128u, pw); |
| 230 | imadi(e, inc, nf, 0, spec->mutant == 2 ? 0u : 4u, NOREG); imadw(e, ps, inc, 1, ps); |
| 231 | } |
| 232 | ISSUE(wa, 3u, 0u, 0u, 1); |
| 233 | COMPUTE(BUF, wb, 0, 1); |
| 234 | nop(e, 15); nop(e, 14); /* the last HMMAs' results settle before the FFMAs read them */ |
| 235 | for (uint32_t x = 0; x < RT * CT; x++) for (unsigned q = 0; q < 4; q++) { |
| 236 | BlackwellIRInsn f = I0(BW_IR_FFMA); f.dst_vreg = acc[x]; f.dst_subreg = (uint8_t)q; f.src1_vreg = tm[x]; f.src1_subreg = (uint8_t)q; |
| 237 | f.src2_vreg = spec->mutant == 1 ? onef : scur; f.src3_vreg = acc[x]; f.src3_subreg = (uint8_t)q; emit(e, f, K_FIXED); |
| 238 | } |
| 239 | consume(e, 3); |
| 240 | STAGE(0u); |
| 241 | imadi(e, cntk, one, 0, 0xffffffffu, cntk); |
| 242 | isetp_ge_u32(e, cntk, one); |
| 243 | bra_if_p0(e, L_k); |
| 244 | loop_end(e); |
| 245 | #undef ISSUE |
| 246 | #undef STAGE |
| 247 | #undef COMPUTE |
| 248 | |
| 249 | /* ---- store: row tile i, half h (rows 16i + 8h + g) when < cnt; columns n0w + 8j + 2t (+1) ---- */ |
| 250 | int rr = V(e), pya = V64(e); |
| 251 | if (e->err) return -1; |
| 252 | for (uint32_t i = 0; i < RT; i++) for (uint32_t h = 0; h < 2; h++) { |
| 253 | imadi(e, rr, one, 0, 16u * i + 8u * h, g); |
| 254 | imadi(e, sv, rr, 0, 1u, NOREG); |
| 255 | isetp_ge_u32(e, sv, cnt); |
| 256 | const int at_bssy = bssy_fwd(e); |
| 257 | const int bra_skip = bra_if_p0_fwd(e); |
| 258 | imadi(e, tmp, ysn, 0, 16u * i + 8u * h, NOREG); imadw(e, pya, tmp, 1, pyr); |
| 259 | for (uint32_t j = 0; j < CT; j++) for (unsigned q = 0; q < 2; q++) { BlackwellIRInsn st = I0(BW_IR_STG_E); st.src1_vreg = pya; st.src2_vreg = acc[i * CT + j]; st.src2_subreg = (uint8_t)(2u * h + q); st.imm = j * 32u + 4u * q; st.ureg = udesc; emit(e, st, K_STORE); } |
| 260 | const int at_bsync = here(e); |
| 261 | bsync(e); |
| 262 | patch_bra(e, bra_skip, at_bsync); |
| 263 | patch_bra(e, at_bssy, at_bsync + 1); |
| 264 | } |
| 265 | tail(e); |
| 266 | if (e->err) return -1; |
| 267 | return omega_bw_regalloc_solve(prog); |
| 268 | } |
| 269 | |
| 270 | |
| 271 | int omega_blackwell_codegen_tc_peak_prog(uint32_t iters, uint32_t chains, uint32_t stall, uint32_t threads, BlackwellIRProgram *prog) { |
| 272 | if (!prog || iters == 0 || chains == 0 || chains > 16 || stall == 0 || stall > 15 || threads == 0 || threads % 32 || threads > 1024) return -1; |
| 273 | omega_bw_ir_init(prog); |
| 274 | Em em; memset(&em, 0, sizeof em); em.p = prog; Em *e = &em; |
| 275 | int udesc = UV64(e), py = V64(e), tid = V(e), cta = V(e), one = V(e), cnt = V(e), a = V128(e), b = V64(e), acc[16]; |
| 276 | for (uint32_t c = 0; c < chains; c++) acc[c] = V128(e); |
| 277 | if (e->err) return -1; |
| 278 | ldcu64(e, udesc, 0x358); ldc64(e, py, 0x380); s2r(e, tid, BW_SR_TID_X); s2r(e, cta, BW_SR_CTAID_X); |
| 279 | movi(e, one, 1); |
| 280 | for (unsigned q = 0; q < 4; q++) { BlackwellIRInsn m = I0(BW_IR_MOV_IMM); m.dst_vreg = a; m.dst_subreg = (uint8_t)q; m.imm = 0x3f803f80u; emit(e, m, K_FIXED); } |
| 281 | for (unsigned q = 0; q < 2; q++) { BlackwellIRInsn m = I0(BW_IR_MOV_IMM); m.dst_vreg = b; m.dst_subreg = (uint8_t)q; m.imm = 0x3f803f80u; emit(e, m, K_FIXED); } |
| 282 | for (uint32_t c = 0; c < chains; c++) for (unsigned q = 0; q < 4; q++) { BlackwellIRInsn mz = I0(BW_IR_MOV_RZ); mz.dst_vreg = acc[c]; mz.dst_subreg = (uint8_t)q; emit(e, mz, K_FIXED); } |
| 283 | movi(e, cnt, iters); |
| 284 | const int L = here(e); |
| 285 | loop_begin(e); |
| 286 | for (uint32_t c = 0; c < chains; c++) hmma(e, acc[c], a, b, acc[c], stall); |
| 287 | imadi(e, cnt, one, 0, 0xffffffffu, cnt); |
| 288 | isetp_ge_u32(e, cnt, one); |
| 289 | bra_if_p0(e, L); |
| 290 | loop_end(e); |
| 291 | nop(e, 15); nop(e, 15); |
| 292 | { int t = V(e); if (e->err) return -1; |
| 293 | imadi(e, t, cta, 0, threads, tid); imadw(e, py, t, 16u * chains, py); |
| 294 | for (uint32_t c = 0; c < chains; c++) for (unsigned q = 0; q < 4; q++) { BlackwellIRInsn st = I0(BW_IR_STG_E); st.src1_vreg = py; st.src2_vreg = acc[c]; st.src2_subreg = (uint8_t)q; st.imm = c * 16u + 4u * q; st.ureg = udesc; emit(e, st, K_STORE); } |
| 295 | } |
| 296 | tail(e); |
| 297 | if (e->err) return -1; |
| 298 | return omega_bw_regalloc_solve(prog); |
| 299 | } |
| 300 | |
| 301 | int omega_blackwell_codegen_tc_bw_prog(uint32_t iters, uint32_t groups, uint32_t threads, BlackwellIRProgram *prog) { |
| 302 | if (!prog || iters == 0 || groups == 0 || threads == 0 || threads % 32 || threads > 1024) return -1; |
| 303 | omega_bw_ir_init(prog); |
| 304 | Em em; memset(&em, 0, sizeof em); em.p = prog; Em *e = &em; |
| 305 | int udesc = UV64(e), px = V64(e), py = V64(e), tid = V(e), cta = V(e), one = V(e), cnt = V(e), grp = V(e), t = V(e), acc = V(e), ld[8]; |
| 306 | for (int q = 0; q < 8; q++) ld[q] = V128(e); |
| 307 | if (e->err) return -1; |
| 308 | ldcu64(e, udesc, 0x358); ldc64(e, px, 0x380); ldc64(e, py, 0x388); s2r(e, tid, BW_SR_TID_X); s2r(e, cta, BW_SR_CTAID_X); |
| 309 | movi(e, one, 1); movrz(e, acc); |
| 310 | /* grp = cta % groups (cta - (cta / groups) * groups via the unsigned high multiply is not needed: groups is 1 or the grid) */ |
| 311 | if (groups == 1) movrz(e, grp); else imadi(e, grp, cta, 0, 1u, NOREG); |
| 312 | imadw(e, px, grp, iters * threads * 128u, px); |
| 313 | imadi(e, t, tid, 0, 16u, NOREG); imadw(e, px, t, 1, px); |
| 314 | movi(e, cnt, iters); |
| 315 | const int L = here(e); |
| 316 | loop_begin(e); |
| 317 | for (int q = 0; q < 8; q++) ldg_wide(e, BW_IR_LDG_E_128, ld[q], px, (uint32_t)q * threads * 16u, udesc, q == 0); |
| 318 | for (int q = 0; q < 8; q++) for (unsigned s = 0; s < 4; s++) { BlackwellIRInsn x = I0(BW_IR_LOP3_XOR); x.dst_vreg = acc; x.src1_vreg = acc; x.src2_vreg = ld[q]; x.src2_subreg = (uint8_t)s; emit(e, x, K_FIXED); } |
| 319 | imadw(e, px, one, threads * 128u, px); |
| 320 | imadi(e, cnt, one, 0, 0xffffffffu, cnt); |
| 321 | isetp_ge_u32(e, cnt, one); |
| 322 | bra_if_p0(e, L); |
| 323 | loop_end(e); |
| 324 | imadi(e, t, cta, 0, threads, tid); imadw(e, py, t, 4u, py); |
| 325 | stg(e, py, 0, acc, udesc); |
| 326 | tail(e); |
| 327 | if (e->err) return -1; |
| 328 | return omega_bw_regalloc_solve(prog); |
| 329 | } |
| 330 | #else |
| 331 | int omega_blackwell_codegen_tcgemm_prog(const OmegaTcGemmSpec *s, BlackwellIRProgram *prog) { |
| 332 | if (!prog || !omega_tcgemm_spec_ok(s)) return -1; |
| 333 | return omega_gpu_om_ir_replay(omega_cg_tc, 3, s->k, |
| 334 | (uint64_t)s->rt | (uint64_t)s->ct << 32, |
| 335 | (uint64_t)s->warps | (uint64_t)(uint32_t)s->mutant << 32, prog, NULL) == 0 ? 0 : -1; |
| 336 | } |
| 337 | int omega_blackwell_codegen_tc_peak_prog(uint32_t iters, uint32_t chains, uint32_t stall, |
| 338 | uint32_t threads, BlackwellIRProgram *prog) { |
| 339 | if (!prog) return -1; |
| 340 | return omega_gpu_om_ir_replay(omega_cg_tc, 1, iters, |
| 341 | (uint64_t)chains | (uint64_t)stall << 32, threads, prog, NULL) == 0 ? 0 : -1; |
| 342 | } |
| 343 | int omega_blackwell_codegen_tc_bw_prog(uint32_t iters, uint32_t groups, uint32_t threads, |
| 344 | BlackwellIRProgram *prog) { |
| 345 | if (!prog) return -1; |
| 346 | return omega_gpu_om_ir_replay(omega_cg_tc, 2, iters, groups, threads, prog, NULL) == 0 ? 0 : -1; |
| 347 | } |
| 348 | #endif |