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_gemm.c
| 1 | /* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception */ |
| 2 | /* omega_blackwell_codegen_gemm.c: FP8 e4m3 block-scaled grouped GEMM for prefill (OWN-30B builder F item 26). See the |
| 3 | * header for the contract. The emitter helpers are copies of omega_blackwell_codegen_moe.c's (same chip-learned |
| 4 | * scheduling rules) plus one addition: weight and scale prefetch loads use scoreboards 3 (buffer A) and 4 (buffer B) |
| 5 | * and are waited for only by the step that decodes them, so the next step's weights stream while this step computes. */ |
| 6 | #include "omega_blackwell_codegen_gemm.h" |
| 7 | #ifndef OMEGA_GPU_NO_POSIX |
| 8 | #include <stdlib.h> |
| 9 | #endif |
| 10 | #include <string.h> |
| 11 | |
| 12 | #ifndef CUDAPP_MIGRATION_PENDING_C_CODEGEN_GEMM |
| 13 | #define CUDAPP_MIGRATION_PENDING_C_CODEGEN_GEMM 0 |
| 14 | #endif |
| 15 | #if CUDAPP_MIGRATION_PENDING_C_CODEGEN_GEMM |
| 16 | #define CW(stall, wbar, rbar, wait) \ |
| 17 | (((uint32_t)(stall) << 9) | ((uint32_t)(wbar) << 14) | ((uint32_t)(rbar) << 17) | ((uint32_t)(wait) << 20)) |
| 18 | #define CW_YIELD 0x2000u |
| 19 | #define SB_NONE 7u |
| 20 | #define SB_GPR 0u |
| 21 | #define SB_RD 1u |
| 22 | #define SB_UGPR 2u |
| 23 | #define NOREG (-1) |
| 24 | #define MAX_LOOPS 4 |
| 25 | |
| 26 | enum { K_FIXED, K_PRED, K_VAR, K_VAR_MORE, K_UVAR, K_STORE, K_SYNC, K_EXIT, K_BRA, K_BSSY, K_BSYNC }; |
| 27 | |
| 28 | typedef struct { |
| 29 | BlackwellIRProgram *p; |
| 30 | uint32_t pending, held; |
| 31 | int err; |
| 32 | int open_start[MAX_LOOPS]; |
| 33 | int depth; |
| 34 | } Em; |
| 35 | |
| 36 | static void emit(Em *e, BlackwellIRInsn in, int kind) { |
| 37 | uint32_t wait = e->pending; |
| 38 | switch (kind) { |
| 39 | case K_FIXED: in.control = CW(6, SB_NONE, SB_NONE, wait); e->pending = 0; break; |
| 40 | case K_PRED: in.control = CW(14, SB_NONE, SB_NONE, wait); e->pending = 0; break; |
| 41 | /* Loads read their address operands late (the read barrier SB_RD): a later instruction must not overwrite an |
| 42 | * in-flight load's address register. Every load sets SB_RD and the next load of a batch waits on it (cheap: |
| 43 | * the operand read, not the data). Found on the chip: the deep k=2048 kernel hung when the allocator reused a |
| 44 | * dead scale address register as the next load's destination (evidence OWN30B-FP8-GEMV-c4006391). */ |
| 45 | case K_VAR: in.control = CW(4, SB_GPR, SB_RD, wait) | CW_YIELD; e->pending = (1u << SB_GPR) | (1u << SB_RD); break; |
| 46 | /* a further load of the same group: waits on nothing, joins the counting scoreboard */ |
| 47 | case K_VAR_MORE: in.control = CW(4, SB_GPR, SB_RD, (e->pending & (1u << SB_RD))) | CW_YIELD; e->pending |= (1u << SB_GPR) | (1u << SB_RD); break; |
| 48 | case K_UVAR: in.control = CW(4, SB_UGPR, SB_NONE, wait) | CW_YIELD; e->pending = 1u << SB_UGPR; break; |
| 49 | case K_STORE: in.control = CW(4, SB_NONE, SB_RD, wait) | CW_YIELD; e->pending = 1u << SB_RD; break; |
| 50 | case K_SYNC: in.control = CW(6, SB_NONE, SB_NONE, wait); e->pending = 0; break; /* BAR.SYNC after the STS: the attention kernel's chip-proven pattern */ |
| 51 | case K_EXIT: in.control = CW(5, SB_NONE, SB_NONE, wait); e->pending = in.predicate_p0 ? wait : 0; break; |
| 52 | case K_BRA: in.control = CW(5, SB_NONE, SB_NONE, wait); e->pending = in.predicate_p0 ? wait : 0; break; |
| 53 | case K_BSSY: in.control = CW(1, SB_NONE, SB_NONE, wait) | CW_YIELD; e->pending = 0; break; |
| 54 | case K_BSYNC: in.control = CW(5, SB_NONE, SB_NONE, wait) | CW_YIELD; e->pending = 0; break; |
| 55 | default: e->err = -1; return; |
| 56 | } |
| 57 | if (omega_bw_ir_append(e->p, &in) < 0) e->err = -1; |
| 58 | } |
| 59 | |
| 60 | static BlackwellIRInsn I0(BlackwellIROpcode op) { |
| 61 | BlackwellIRInsn in; memset(&in, 0, sizeof in); |
| 62 | in.op = op; in.dst_vreg = NOREG; in.src1_vreg = NOREG; in.src2_vreg = NOREG; in.src3_vreg = NOREG; in.ureg = NOREG; |
| 63 | return in; |
| 64 | } |
| 65 | static int V(Em *e) { int v = omega_bw_ir_alloc_vreg(e->p); if (v < 0) e->err = -1; return v; } |
| 66 | static int V64(Em *e) { int v = omega_bw_ir_alloc_vreg64(e->p); if (v < 0) e->err = -1; return v; } |
| 67 | static int V128(Em *e) { int v = omega_bw_ir_alloc_vreg128(e->p); if (v < 0) e->err = -1; return v; } |
| 68 | static int UV64(Em *e) { int v = omega_bw_ir_alloc_uvreg64(e->p); if (v < 0) e->err = -1; return v; } |
| 69 | static int here(const Em *e) { return (int)e->p->count; } |
| 70 | |
| 71 | static void ldcu64(Em *e, int ud, uint32_t off) { BlackwellIRInsn i = I0(BW_IR_LDCU64); i.dst_vreg = ud; i.imm = off; i.is_uniform = true; emit(e, i, K_UVAR); } |
| 72 | static void ldc64(Em *e, int d, uint32_t off) { BlackwellIRInsn i = I0(BW_IR_LDC64); i.dst_vreg = d; i.imm = off; emit(e, i, K_VAR); } |
| 73 | static void ldc32(Em *e, int d, uint32_t off) { BlackwellIRInsn i = I0(BW_IR_LDC); i.dst_vreg = d; i.imm = off; emit(e, i, K_VAR); } |
| 74 | static void s2r(Em *e, int d, uint32_t sr) { BlackwellIRInsn i = I0(BW_IR_S2R); i.dst_vreg = d; i.imm = sr; emit(e, i, K_VAR); } |
| 75 | static void movi(Em *e, int d, uint32_t imm) { BlackwellIRInsn i = I0(BW_IR_MOV_IMM); i.dst_vreg = d; i.imm = imm; emit(e, i, K_FIXED); } |
| 76 | static void movrz(Em *e, int d) { BlackwellIRInsn i = I0(BW_IR_MOV_RZ); i.dst_vreg = d; emit(e, i, K_FIXED); } |
| 77 | /* d = a * imm + c (c NOREG = RZ); a may be a sub-register of a quad */ |
| 78 | static void imadi(Em *e, int d, int a, unsigned asub, uint32_t imm, int c) { BlackwellIRInsn i = I0(BW_IR_IMAD); i.dst_vreg = d; i.src1_vreg = a; i.src1_subreg = (uint8_t)asub; i.imm = imm; i.src3_vreg = c; emit(e, i, K_FIXED); } |
| 79 | /* d = a * b + c (registers) */ |
| 80 | static void imadr(Em *e, int d, int a, int b, int c) { BlackwellIRInsn i = I0(BW_IR_IMAD); i.dst_vreg = d; i.src1_vreg = a; i.src2_vreg = b; i.src3_vreg = c; emit(e, i, K_FIXED); } |
| 81 | /* d64 = a * imm + c64 */ |
| 82 | static void imadw(Em *e, int d64, int a, uint32_t imm, int c64) { BlackwellIRInsn i = I0(BW_IR_IMAD_WIDE); i.dst_vreg = d64; i.src1_vreg = a; i.imm = imm; i.src3_vreg = c64; emit(e, i, K_FIXED); } |
| 83 | static void shr(Em *e, int d, int a, unsigned asub, uint32_t imm) { BlackwellIRInsn i = I0(BW_IR_SHF_R); i.dst_vreg = d; i.src1_vreg = a; i.src1_subreg = (uint8_t)asub; i.imm = imm; emit(e, i, K_FIXED); } |
| 84 | static void andi(Em *e, int d, int a, unsigned asub, uint32_t imm) { BlackwellIRInsn i = I0(BW_IR_LOP3_AND); i.dst_vreg = d; i.src1_vreg = a; i.src1_subreg = (uint8_t)asub; i.imm = imm; emit(e, i, K_FIXED); } |
| 85 | static void ldg_wide(Em *e, BlackwellIROpcode op, int d, int addr64, uint32_t imm, int udesc, int first) { |
| 86 | BlackwellIRInsn i = I0(op); i.dst_vreg = d; i.src1_vreg = addr64; i.imm = imm; i.ureg = udesc; emit(e, i, first ? K_VAR : K_VAR_MORE); |
| 87 | } |
| 88 | static void ldg32(Em *e, int d, int addr64, int udesc, int first) { BlackwellIRInsn i = I0(BW_IR_LDG_E); i.dst_vreg = d; i.src1_vreg = addr64; i.ureg = udesc; emit(e, i, first ? K_VAR : K_VAR_MORE); } |
| 89 | static void stg(Em *e, int addr64, uint32_t imm, int val, int udesc) { BlackwellIRInsn i = I0(BW_IR_STG_E); i.src1_vreg = addr64; i.src2_vreg = val; i.imm = imm; i.ureg = udesc; emit(e, i, K_STORE); } |
| 90 | static void lds128(Em *e, int d128, int addr, uint32_t imm, int first) { BlackwellIRInsn i = I0(BW_IR_LDS128); i.dst_vreg = d128; i.src1_vreg = addr; i.imm = imm; emit(e, i, first ? K_VAR : K_VAR_MORE); } |
| 91 | static void sts32(Em *e, int addr, int val) { BlackwellIRInsn i = I0(BW_IR_STS32); i.src1_vreg = addr; i.src2_vreg = val; emit(e, i, K_STORE); } |
| 92 | static void bar_sync(Em *e) { emit(e, I0(BW_IR_BAR_SYNC), K_SYNC); } |
| 93 | static void fadd(Em *e, int d, int a, int b) { BlackwellIRInsn i = I0(BW_IR_FADD); i.dst_vreg = d; i.src1_vreg = a; i.src2_vreg = b; emit(e, i, K_FIXED); } |
| 94 | /* d = a * b[bsub] + c */ |
| 95 | static void ffma(Em *e, int d, int a, int b, unsigned bsub, int c) { BlackwellIRInsn i = I0(BW_IR_FFMA); i.dst_vreg = d; i.src1_vreg = a; i.src2_vreg = b; i.src2_subreg = (uint8_t)bsub; i.src3_vreg = c; emit(e, i, K_FIXED); } |
| 96 | static void shfl_down(Em *e, int d, int a, uint32_t off) { BlackwellIRInsn i = I0(BW_IR_SHFL_DOWN); i.dst_vreg = d; i.src1_vreg = a; i.imm = off; emit(e, i, K_VAR); } |
| 97 | static void isetp_ge_u32(Em *e, int a, int b) { BlackwellIRInsn i = I0(BW_IR_ISETP_GE_U32); i.src1_vreg = a; i.src2_vreg = b; emit(e, i, K_PRED); } |
| 98 | static void bra_if_p0(Em *e, int target) { BlackwellIRInsn i = I0(BW_IR_BRA); i.predicate_p0 = true; i.imm = (uint32_t)(target - here(e)); emit(e, i, K_BRA); } |
| 99 | static void bra(Em *e, int target) { BlackwellIRInsn i = I0(BW_IR_BRA); i.imm = (uint32_t)(target - here(e)); emit(e, i, K_BRA); } |
| 100 | /* a forward branch whose target is patched when known: returns the instruction index */ |
| 101 | static int bra_if_p0_fwd(Em *e) { int at = here(e); BlackwellIRInsn i = I0(BW_IR_BRA); i.predicate_p0 = true; i.imm = 1; emit(e, i, K_BRA); return at; } |
| 102 | static void patch_bra(Em *e, int at, int target) { if (!e->err) e->p->insns[at].imm = (uint32_t)(target - at); } |
| 103 | static int bssy_fwd(Em *e) { int at = here(e); BlackwellIRInsn i = I0(BW_IR_BSSY); i.bar_reg = 0; i.imm = 2; emit(e, i, K_BSSY); return at; } |
| 104 | static void bsync(Em *e) { BlackwellIRInsn i = I0(BW_IR_BSYNC); i.bar_reg = 0; emit(e, i, K_BSYNC); } |
| 105 | static void tail(Em *e) { |
| 106 | emit(e, I0(BW_IR_EXIT), K_EXIT); |
| 107 | BlackwellIRInsn self = I0(BW_IR_BRA); |
| 108 | self.control = 0x000fc000u; |
| 109 | if (omega_bw_ir_append(e->p, &self) < 0) e->err = -1; |
| 110 | } |
| 111 | |
| 112 | /* Loops: a value defined before the loop and read inside it stays live until the back-branch |
| 113 | * (the linear-scan allocator knows no back edges; same rule as the elementwise emitter). */ |
| 114 | static void loop_begin(Em *e) { if (e->depth >= MAX_LOOPS) { e->err = -1; return; } e->open_start[e->depth++] = here(e); } |
| 115 | static void loop_end(Em *e) { |
| 116 | if (e->depth <= 0) { e->err = -1; return; } |
| 117 | int start = e->open_start[--e->depth], end = here(e) - 1; |
| 118 | OmegaRegAlloc *ra = &e->p->regalloc; |
| 119 | for (int v = 0; v < ra->num_vregs; v++) { |
| 120 | OmegaLiveInterval *iv = &ra->intervals[v]; |
| 121 | if (iv->first_def >= 0 && iv->first_def < start && iv->last_use >= start && iv->last_use < end) iv->last_use = end; |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | |
| 126 | /* A weight/scale prefetch load: scoreboard `sb` (3 or 4) is held, not waited for, until consume() */ |
| 127 | static void ldw_pf(Em *e, BlackwellIROpcode op, int d, int addr64, uint32_t imm, int udesc, unsigned sb) { |
| 128 | BlackwellIRInsn in = I0(op); in.dst_vreg = d; in.src1_vreg = addr64; in.imm = imm; in.ureg = udesc; |
| 129 | in.control = CW(4, sb, SB_RD, (e->pending & (1u << SB_RD))) | CW_YIELD; |
| 130 | e->pending |= 1u << SB_RD; e->held |= 1u << sb; |
| 131 | if (omega_bw_ir_append(e->p, &in) < 0) e->err = -1; |
| 132 | } |
| 133 | static void consume(Em *e, unsigned sb) { e->pending |= e->held & (1u << sb); e->held &= ~(1u << sb); } |
| 134 | |
| 135 | static int log2u(uint32_t v) { int l = 0; while ((1u << l) < v) l++; return (1u << l) == v ? l : -1; } |
| 136 | |
| 137 | static int spec_ok(const OmegaFp8GemmSpec *s) { |
| 138 | if (!s) return 0; |
| 139 | if (s->lane_bytes != 8 && s->lane_bytes != 16) return 0; |
| 140 | /* Check the bound before multiplying: a full-width mt could wrap the old |
| 141 | * product to zero and then overrun the fixed accumulator arrays. */ |
| 142 | if (s->mt == 0 || (s->mt & (s->mt - 1)) || s->mt > OMEGA_GEMM_MAX_SHARED / (128u * s->lane_bytes)) return 0; |
| 143 | if (s->warps < 1 || s->warps > 32 || (s->warps & (s->warps - 1)) || (s->mt * s->lane_bytes) % s->warps) return 0; /* power-of-two warps: the staging words per thread are whole */ |
| 144 | if (s->k == 0 || s->k > 16384u || s->k % 128u || s->k % (32u * s->lane_bytes)) return 0; |
| 145 | uint32_t ks = s->k / (32u * s->lane_bytes); |
| 146 | if (ks % 2u && ks > 3u) return 0; /* odd step counts are fully unrolled: at most 3 */ |
| 147 | if (s->mutant < 0 || s->mutant > 4) return 0; |
| 148 | if (s->rowmap > 1u || (s->rowmap && s->mt > 4u)) return 0; /* the mapped form keeps NJ row bases and MT y rows in registers: mt 8 needs 135 at 16 warps, past the file */ |
| 149 | return 1; |
| 150 | } |
| 151 | uint32_t omega_fp8gemm_steps(const OmegaFp8GemmSpec *spec) { return spec_ok(spec) ? spec->k / (32u * spec->lane_bytes) : 0; } |
| 152 | uint32_t omega_fp8gemm_shared_bytes(const OmegaFp8GemmSpec *spec) { return spec_ok(spec) ? spec->mt * 128u * spec->lane_bytes : 0u; } |
| 153 | |
| 154 | /* e4m3 byte i of word w -> f32 bits (value * 2^-120): ((b & 0x7F) << 20) | ((b & 0x80) << 24) */ |
| 155 | static void decode_byte(Em *e, int f, int wq, unsigned word, unsigned i, int u, int a, int s) { |
| 156 | const uint32_t M = 0x07F00000u; |
| 157 | if (i < 3) imadi(e, u, wq, word, 1u << (20u - 8u * i), NOREG); |
| 158 | else shr(e, u, wq, word, 4); |
| 159 | andi(e, a, u, 0, M); |
| 160 | andi(e, s, wq, word, 0x80u << (8u * i)); |
| 161 | if (i < 3) imadi(e, f, s, 0, 1u << (24u - 8u * i), a); |
| 162 | else imadi(e, f, s, 0, 1u, a); |
| 163 | } |
| 164 | |
| 165 | int omega_blackwell_codegen_fp8gemm_prog(const OmegaFp8GemmSpec *spec, BlackwellIRProgram *prog) { |
| 166 | if (!spec_ok(spec) || !prog) return -1; |
| 167 | const uint32_t K = spec->k, LB = spec->lane_bytes, MT = spec->mt, W = spec->warps; |
| 168 | const uint32_t KS = K / (32u * LB), BPS = 32u * LB / 128u, LPB = 128u / LB, KB = K / 128u, WORDS = LB / 4u; |
| 169 | const uint32_t U = KS % 2u == 0 ? 2u : KS; /* K steps per loop iteration (the A/B register sets alternate) */ |
| 170 | const uint32_t NJ = MT * LB / W, ROWW = 32u * LB, T = 32u * W; /* staging words per thread, rows per staging j, words per row slab */ |
| 171 | const int log2lpb = log2u(LPB), log2mt = log2u(MT), log2roww = log2u(ROWW); |
| 172 | const BlackwellIROpcode ldw = LB == 16 ? BW_IR_LDG_E_128 : BW_IR_LDG_E_64; |
| 173 | if (log2lpb < 0 || log2mt < 0 || log2roww < 0) return -1; |
| 174 | omega_bw_ir_init(prog); |
| 175 | Em em; memset(&em, 0, sizeof em); em.p = prog; |
| 176 | Em *e = &em; |
| 177 | |
| 178 | /* ---- prologue ---- */ |
| 179 | int udesc = UV64(e), pseg = V64(e), px = V64(e), py = V64(e), nreg = V(e), stride = V(e); |
| 180 | int tid = V(e), cx = V(e), cy = V(e), warp = V(e), lane = V(e), one = V(e), tent = V64(e); |
| 181 | int wbase = V64(e), sbase = V64(e), rc = V64(e), row0 = V(e), cnt = V(e), tpass = V(e), passes = V(e); |
| 182 | int wlane = V(e), slane = V(e), xl = V(e), nm1 = V(e), ysn = V(e), mrow = V(e), off4 = V(e), sa0 = V(e), base = V(e), z = V(e); |
| 183 | const int RM = spec->rowmap ? 1 : 0; |
| 184 | int pxm = RM ? V64(e) : NOREG, pym = RM ? V64(e) : NOREG; |
| 185 | if (e->err) return -1; |
| 186 | ldcu64(e, udesc, 0x358); |
| 187 | ldc64(e, pseg, OMEGA_GEMM_ARG_SEG); ldc64(e, px, OMEGA_GEMM_ARG_X); ldc64(e, py, OMEGA_GEMM_ARG_Y); |
| 188 | ldc32(e, nreg, OMEGA_GEMM_ARG_N); ldc32(e, stride, OMEGA_GEMM_ARG_STRIDE); |
| 189 | if (RM) { ldc64(e, pxm, OMEGA_GEMM_ARG_XMAP); ldc64(e, pym, OMEGA_GEMM_ARG_YMAP); } |
| 190 | s2r(e, tid, BW_SR_TID_X); s2r(e, cx, BW_SR_CTAID_X); s2r(e, cy, BW_SR_CTAID_Y); |
| 191 | movi(e, one, 1); |
| 192 | shr(e, warp, tid, 0, 5); |
| 193 | andi(e, lane, tid, 0, 31); |
| 194 | imadw(e, tent, cy, OMEGA_GEMM_SEG_BYTES, pseg); |
| 195 | ldg_wide(e, BW_IR_LDG_E_64, wbase, tent, 0, udesc, 1); |
| 196 | ldg_wide(e, BW_IR_LDG_E_64, sbase, tent, 8, udesc, 0); |
| 197 | ldg_wide(e, BW_IR_LDG_E_64, rc, tent, 16, udesc, 0); |
| 198 | imadi(e, row0, rc, 0, 1, NOREG); imadi(e, cnt, rc, 1, 1, NOREG); |
| 199 | imadi(e, tpass, one, 0, MT - 1u, cnt); shr(e, passes, tpass, 0, (uint32_t)log2mt); |
| 200 | /* an empty segment (cnt 0, a stroke table slot without an expert): the whole CTA leaves before the loops. Without this the |
| 201 | * pass loop (do-while, pc = passes - 1 wraps) runs 2^32 passes: the warp simulator's step budget caught it (2026-10-21). */ |
| 202 | int bra_empty = -1; |
| 203 | if (spec->mutant != 4) { /* mutant 4 (tests): the guard dropped */ |
| 204 | imadi(e, z, one, 0, 0xffffffffu, cnt); shr(e, z, z, 0, 31); /* 1 iff cnt == 0 */ |
| 205 | isetp_ge_u32(e, z, one); |
| 206 | bra_empty = bra_if_p0_fwd(e); |
| 207 | } |
| 208 | imadi(e, wlane, lane, 0, LB, NOREG); |
| 209 | shr(e, slane, lane, 0, (uint32_t)log2lpb); imadi(e, slane, slane, 0, 4, NOREG); |
| 210 | imadi(e, xl, lane, 0, 16, NOREG); |
| 211 | imadi(e, nm1, one, 0, 0xffffffffu, nreg); |
| 212 | imadi(e, ysn, nreg, 0, 4, NOREG); |
| 213 | shr(e, mrow, tid, 0, (uint32_t)log2roww); |
| 214 | andi(e, off4, tid, 0, ROWW - 1u); imadi(e, off4, off4, 0, 4, NOREG); |
| 215 | imadi(e, sa0, tid, 0, 4, NOREG); |
| 216 | imadi(e, base, cx, 0, W, NOREG); |
| 217 | |
| 218 | /* loop-wide registers */ |
| 219 | int rg = V(e), over = V(e), t1 = V(e), d = V(e), tmp = V(e), rgc = V(e), lo = V(e); |
| 220 | int pw = V64(e), ps = V64(e), pyrg = V64(e), rowp = V(e), rem = V(e), pc = V(e); |
| 221 | int xrp = V64(e), xs = V64(e), pwc = V64(e), psc = V64(e), cntk = V(e), pyr = V64(e), pya = V64(e), t = V(e), tq = V(e), fl = V(e), sv = V(e); |
| 222 | int xb[16], yi[16], xso = NOREG, ma = NOREG; /* rowmap: x' base per staged quad (row through XMAP), the step byte offset, y rows through YMAP */ |
| 223 | if (RM) { if (NJ > 16u) return -1; for (uint32_t j = 0; j < NJ; j++) xb[j] = V64(e); for (uint32_t m = 0; m < MT; m++) yi[m] = V(e); xso = V(e); ma = V64(e); } |
| 224 | int acc[16], dot[16], xq[4], wq[2], sc[2], fr[4], u = V(e), a = V(e), sg = V(e), onef = V(e), dum = V(e); |
| 225 | for (uint32_t m = 0; m < MT; m++) { acc[m] = V(e); dot[m] = V(e); } |
| 226 | for (int q = 0; q < 4; q++) xq[q] = V128(e); |
| 227 | for (int b = 0; b < 2; b++) { wq[b] = LB == 16 ? V128(e) : V64(e); sc[b] = V(e); } |
| 228 | for (int i = 0; i < 4; i++) fr[i] = V(e); |
| 229 | if (e->err) return -1; |
| 230 | |
| 231 | /* ---- weight-row loop (uniform per CTA) ---- */ |
| 232 | const int L_rg = here(e); |
| 233 | loop_begin(e); |
| 234 | isetp_ge_u32(e, base, nreg); |
| 235 | const int bra_end = bra_if_p0_fwd(e); |
| 236 | imadi(e, rg, base, 0, 1, warp); |
| 237 | imadi(e, t1, rg, 0, 0xffffffffu, nm1); shr(e, over, t1, 0, 31); /* over = (rg > n - 1) */ |
| 238 | imadi(e, d, nm1, 0, 0xffffffffu, rg); /* rg - (n - 1) */ |
| 239 | imadr(e, tmp, over, d, NOREG); imadi(e, rgc, tmp, 0, 0xffffffffu, rg); /* rg clamped to n - 1 */ |
| 240 | imadw(e, pw, rgc, K, wbase); imadw(e, pw, wlane, 1, pw); |
| 241 | shr(e, tmp, rgc, 0, 7); |
| 242 | imadw(e, ps, tmp, KB * 4u, sbase); imadw(e, ps, slane, 1, ps); |
| 243 | imadw(e, pyrg, rg, 4, py); |
| 244 | imadi(e, lo, over, 0, 1, lane); |
| 245 | imadi(e, rowp, row0, 0, 1, NOREG); imadi(e, rem, cnt, 0, 1, NOREG); imadi(e, pc, passes, 0, 1, NOREG); |
| 246 | |
| 247 | /* ---- pass loop: MT rows of x per pass ---- */ |
| 248 | const int L_pass = here(e); |
| 249 | loop_begin(e); |
| 250 | imadi(e, t, rowp, 0, 1, mrow); |
| 251 | if (!RM) { imadw(e, xrp, t, K * 4u, px); imadw(e, xrp, off4, 1, xrp); imadw(e, xs, one, 0, xrp); } |
| 252 | else { |
| 253 | /* sorted row p = rowp + mrow + pos / ROWW of each staged quad: its x' row is XMAP[p] (one u32 load per quad per pass) */ |
| 254 | for (uint32_t j = 0; j < NJ; j++) { /* the NJ index loads back to back (one batch), the index in the low word of xb[j] */ |
| 255 | imadi(e, tq, one, 0, (j * T) / ROWW, t); imadw(e, ma, tq, 4u, pxm); |
| 256 | ldg32(e, xb[j], ma, udesc, j == 0); |
| 257 | } |
| 258 | for (uint32_t j = 0; j < NJ; j++) { |
| 259 | const uint32_t pos = j * T; |
| 260 | imadw(e, xb[j], xb[j], K * 4u, px); imadw(e, xb[j], off4, 1, xb[j]); imadw(e, xb[j], one, (pos % ROWW) * 4u, xb[j]); |
| 261 | } |
| 262 | movi(e, xso, 0); |
| 263 | } |
| 264 | imadw(e, pwc, one, 0, pw); imadw(e, psc, one, 0, ps); |
| 265 | for (uint32_t m = 0; m < MT; m++) movrz(e, acc[m]); |
| 266 | ldw_pf(e, ldw, wq[0], pwc, 0, udesc, 3); |
| 267 | ldw_pf(e, BW_IR_LDG_E, sc[0], psc, 0, udesc, 3); |
| 268 | movi(e, cntk, KS / U); |
| 269 | |
| 270 | /* ---- K loop: U steps per iteration ---- */ |
| 271 | const int L_k = here(e); |
| 272 | loop_begin(e); |
| 273 | for (uint32_t us = 0; us < U; us++) { |
| 274 | const int cur = (int)(us & 1u), nxt = 1 - cur; |
| 275 | const unsigned sbc = cur ? 4u : 3u, sbn = nxt ? 4u : 3u; |
| 276 | /* addresses first (fixed-latency ops would otherwise wait for the loads below), then loads back to back; the |
| 277 | * staging words go in rounds of 4 so the temporaries stay few */ |
| 278 | const int last = us + 1 == U, prefetch = !last || KS > U; |
| 279 | int pwn = -1, psn = -1; |
| 280 | if (prefetch) { pwn = V64(e); psn = V64(e); } |
| 281 | int sa = V(e); |
| 282 | if (e->err) return -1; |
| 283 | if (prefetch && !last) { |
| 284 | imadw(e, pwn, one, (us + 1) * 32u * LB, pwc); imadw(e, psn, one, spec->mutant == 2 ? 0u : (us + 1) * BPS * 4u, psc); |
| 285 | } else if (prefetch) { /* the next iteration's step 0; the last iteration re-reads its own step (harmless, drained after the loop) */ |
| 286 | int nf = V(e), inc = V(e), inc2 = V(e); |
| 287 | if (e->err) return -1; |
| 288 | imadi(e, nf, one, 0, 0xfffffffeu, cntk); /* cntk - 2 */ |
| 289 | shr(e, nf, nf, 0, 31); /* 1 iff cntk == 1 (the last iteration) */ |
| 290 | imadi(e, nf, nf, 0, 0xffffffffu, one); /* 1 iff another iteration follows */ |
| 291 | imadi(e, inc, nf, 0, U * 32u * LB, NOREG); imadi(e, inc2, nf, 0, spec->mutant == 2 ? 0u : U * BPS * 4u, NOREG); |
| 292 | imadw(e, pwn, inc, 1, pwc); imadw(e, psn, inc2, 1, psc); |
| 293 | } |
| 294 | for (uint32_t j0 = 0; j0 < NJ; j0 += 4) { |
| 295 | const uint32_t nj = NJ - j0 < 4 ? NJ - j0 : 4; |
| 296 | int xv[4], xa[4]; |
| 297 | for (uint32_t q = 0; q < nj; q++) { xv[q] = V(e); xa[q] = V64(e); } |
| 298 | if (e->err) return -1; |
| 299 | for (uint32_t q = 0; q < nj; q++) { const uint32_t pos = (j0 + q) * T; if (RM) imadw(e, xa[q], xso, 1u, xb[j0 + q]); else imadw(e, xa[q], one, ((pos / ROWW) * K + pos % ROWW) * 4u, xs); } |
| 300 | for (uint32_t q = 0; q < nj; q++) ldg32(e, xv[q], xa[q], udesc, q == 0); |
| 301 | if (prefetch && j0 == 0) { |
| 302 | ldw_pf(e, ldw, wq[nxt], pwn, 0, udesc, sbn); |
| 303 | ldw_pf(e, BW_IR_LDG_E, sc[nxt], psn, 0, udesc, sbn); |
| 304 | } |
| 305 | for (uint32_t q = 0; q < nj; q++) { imadi(e, sa, one, 0, (j0 + q) * T * 4u, sa0); sts32(e, sa, xv[q]); } |
| 306 | } |
| 307 | bar_sync(e); |
| 308 | consume(e, sbc); |
| 309 | /* compute */ |
| 310 | for (uint32_t m = 0; m < MT; m++) movrz(e, dot[m]); |
| 311 | for (uint32_t word = 0; word < WORDS; word++) { |
| 312 | for (unsigned i = 0; i < 4; i++) decode_byte(e, fr[i], wq[cur], word, i, u, a, sg); |
| 313 | for (uint32_t m0 = 0; m0 < MT; m0 += 4) { |
| 314 | const uint32_t nb = MT - m0 < 4 ? MT - m0 : 4; |
| 315 | for (uint32_t q = 0; q < nb; q++) { |
| 316 | const uint32_t m = m0 + q, mm = spec->mutant == 3 ? (m ^ 1u) % MT : m; |
| 317 | lds128(e, xq[q], xl, mm * ROWW * 4u + word * 512u, q == 0); |
| 318 | } |
| 319 | for (uint32_t q = 0; q < nb; q++) for (unsigned i = 0; i < 4; i++) ffma(e, dot[m0 + q], fr[i], xq[q], i, dot[m0 + q]); |
| 320 | } |
| 321 | } |
| 322 | if (spec->mutant == 1) { movi(e, onef, 0x3f800000u); ffma(e, dum, sc[cur], onef, 0, onef); } /* scales dropped, but the scale load stays consumed */ |
| 323 | for (uint32_t m = 0; m < MT; m++) { |
| 324 | if (spec->mutant == 1) ffma(e, acc[m], dot[m], onef, 0, acc[m]); |
| 325 | else ffma(e, acc[m], dot[m], sc[cur], 0, acc[m]); |
| 326 | } |
| 327 | bar_sync(e); |
| 328 | if (RM) imadi(e, xso, one, 0, ROWW * 4u, xso); else imadw(e, xs, one, ROWW * 4u, xs); |
| 329 | } |
| 330 | imadw(e, pwc, one, U * 32u * LB, pwc); |
| 331 | imadw(e, psc, one, spec->mutant == 2 ? 0u : U * BPS * 4u, psc); |
| 332 | imadi(e, cntk, one, 0, 0xffffffffu, cntk); |
| 333 | isetp_ge_u32(e, cntk, one); |
| 334 | bra_if_p0(e, L_k); |
| 335 | loop_end(e); |
| 336 | consume(e, 3); consume(e, 4); |
| 337 | |
| 338 | /* ---- reduce and store, per row of the pass ---- */ |
| 339 | for (uint32_t m = 0; m < MT; m++) |
| 340 | for (uint32_t o = 16; o >= 1; o /= 2) { shfl_down(e, t, acc[m], o); fadd(e, acc[m], acc[m], t); } |
| 341 | imadr(e, tq, rowp, ysn, NOREG); imadw(e, pyr, tq, 1, pyrg); |
| 342 | if (RM) { /* y row of sorted row rowp + m: YMAP[rowp + m], uniform loads, before the predicated stores */ |
| 343 | for (uint32_t m = 0; m < MT; m++) { imadi(e, tq, one, 0, m, rowp); imadw(e, ma, tq, 4u, pym); ldg32(e, yi[m], ma, udesc, m == 0); } |
| 344 | } |
| 345 | for (uint32_t m = 0; m < MT; m++) { |
| 346 | imadi(e, tq, one, 0, (uint32_t)(0u - 1u - m), rem); shr(e, fl, tq, 0, 31); /* 1 iff m >= rem: past this segment's rows */ |
| 347 | imadi(e, sv, fl, 0, 1, lo); |
| 348 | isetp_ge_u32(e, sv, one); |
| 349 | const int at_bssy = bssy_fwd(e); |
| 350 | const int bra_skip = bra_if_p0_fwd(e); |
| 351 | if (RM) { imadr(e, tq, yi[m], ysn, NOREG); imadw(e, pya, tq, 1, pyrg); } else imadw(e, pya, ysn, m, pyr); |
| 352 | stg(e, pya, 0, acc[m], udesc); |
| 353 | const int at_bsync = here(e); |
| 354 | bsync(e); |
| 355 | patch_bra(e, bra_skip, at_bsync); |
| 356 | patch_bra(e, at_bssy, at_bsync + 1); |
| 357 | } |
| 358 | imadi(e, rowp, one, 0, MT, rowp); imadi(e, rem, one, 0, (uint32_t)(0u - MT), rem); |
| 359 | imadi(e, pc, one, 0, 0xffffffffu, pc); |
| 360 | isetp_ge_u32(e, pc, one); |
| 361 | bra_if_p0(e, L_pass); |
| 362 | loop_end(e); |
| 363 | |
| 364 | imadi(e, base, stride, 0, 1, base); |
| 365 | bra(e, L_rg); |
| 366 | loop_end(e); |
| 367 | const int L_end = here(e); |
| 368 | patch_bra(e, bra_end, L_end); |
| 369 | if (bra_empty >= 0) patch_bra(e, bra_empty, L_end); |
| 370 | tail(e); |
| 371 | if (e->err) return -1; |
| 372 | return omega_bw_regalloc_solve(prog); |
| 373 | } |
| 374 | |
| 375 | #else /* default: first-party seeded Omega; C above is differential-only */ |
| 376 | #include "omega_codegen_rest_wire.h" |
| 377 | uint32_t omega_fp8gemm_steps(const OmegaFp8GemmSpec *spec) { |
| 378 | uint64_t meta[OMEGA_OM_IR_META] = {0}; |
| 379 | if (omega_rest_query(omega_rest_gemm_wire, 0, spec, NULL, meta) != 0) return 0; |
| 380 | return (uint32_t)(int64_t)meta[48]; |
| 381 | } |
| 382 | uint32_t omega_fp8gemm_shared_bytes(const OmegaFp8GemmSpec *spec) { |
| 383 | uint64_t meta[OMEGA_OM_IR_META] = {0}; |
| 384 | if (omega_rest_query(omega_rest_gemm_wire, 1, spec, NULL, meta) != 0) return 0; |
| 385 | return (uint32_t)(int64_t)meta[48]; |
| 386 | } |
| 387 | int omega_blackwell_codegen_fp8gemm_prog(const OmegaFp8GemmSpec *spec, BlackwellIRProgram *prog) { |
| 388 | return omega_gpu_om_ir_replay(omega_rest_gemm_wire, 2, (uintptr_t)spec, 0, 0, prog, NULL) == 0 ? 0 : -1; |
| 389 | } |
| 390 | #endif /* CUDAPP_MIGRATION_PENDING_C_CODEGEN_GEMM */ |
| 391 | |
| 392 | int omega_fp8gemm_prog_stats(const OmegaFp8GemmSpec *spec, uint32_t *insns, uint32_t *gprs) { |
| 393 | static BlackwellIRProgram prog; |
| 394 | if (omega_blackwell_codegen_fp8gemm_prog(spec, &prog) != 0) return -1; |
| 395 | if (insns) *insns = (uint32_t)prog.count; |
| 396 | if (gprs) *gprs = prog.regalloc.peak_gpr_usage; |
| 397 | return 0; |
| 398 | } |