interplane.Get Omega
Read in your language

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

branch main Branches Tags History

CUDApp / src / omega_blackwell_codegen_moe.c

570 lines · 36006 bytes Raw Blame History
1/* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception */
2/* omega_blackwell_codegen_moe.c: FP8 e4m3 block-scaled GEMV kernel (OWN-30B Cut 6 / Cut 5).
3 * See the header for the contract and the argument words.
4 *
5 * Scheduling: the same chip-learned rules as omega_gpu_elementwise_api.c (every instruction
6 * waits on everything still pending; variable-latency results go through scoreboard 0, stores
7 * set the read barrier 1, uniform loads scoreboard 2; fixed-latency producers stall 6; ISETP
8 * stalls 14 before a predicated branch). Loads of one K step are issued back to back (all set
9 * SB0, only the first waits), so a warp has every load of the step in flight at once.
10 */
11#include "omega_blackwell_codegen_moe.h"
12#ifndef OMEGA_GPU_NO_POSIX
13#include <stdlib.h>
14#endif
15#include <string.h>
16
17#include "cudapp_native.h"
18#include "omega_gpu_om_ir.h"
19
20#ifndef CUDAPP_MIGRATION_PENDING_C_CODEGEN_MOE
21#define CUDAPP_MIGRATION_PENDING_C_CODEGEN_MOE 0
22#endif
23#if CUDAPP_MIGRATION_PENDING_C_CODEGEN_MOE
24#define CW(stall, wbar, rbar, wait) \
25 (((uint32_t)(stall) << 9) | ((uint32_t)(wbar) << 14) | ((uint32_t)(rbar) << 17) | ((uint32_t)(wait) << 20))
26#define CW_YIELD 0x2000u
27#define SB_NONE 7u
28#define SB_GPR 0u
29#define SB_RD 1u
30#define SB_UGPR 2u
31#define NOREG (-1)
32#define MAX_LOOPS 4
33
34enum { K_FIXED, K_PRED, K_VAR, K_VAR_MORE, K_UVAR, K_STORE, K_SYNC, K_EXIT, K_BRA, K_BSSY, K_BSYNC };
35
36typedef struct {
37 BlackwellIRProgram *p;
38 uint32_t pending;
39 int err;
40 int open_start[MAX_LOOPS];
41 int depth;
42} Em;
43
44static void emit(Em *e, BlackwellIRInsn in, int kind) {
45 uint32_t wait = e->pending;
46 switch (kind) {
47 case K_FIXED: in.control = CW(6, SB_NONE, SB_NONE, wait); e->pending = 0; break;
48 case K_PRED: in.control = CW(14, SB_NONE, SB_NONE, wait); e->pending = 0; break;
49 /* Loads read their address operands late (the read barrier SB_RD): a later instruction must not overwrite an
50 * in-flight load's address register. Every load sets SB_RD and the next load of a batch waits on it (cheap:
51 * the operand read, not the data). Found on the chip: the deep k=2048 kernel hung when the allocator reused a
52 * dead scale address register as the next load's destination (evidence OWN30B-FP8-GEMV-c4006391). */
53 case K_VAR: in.control = CW(4, SB_GPR, SB_RD, wait) | CW_YIELD; e->pending = (1u << SB_GPR) | (1u << SB_RD); break;
54 /* a further load of the same group: waits on nothing, joins the counting scoreboard */
55 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;
56 case K_UVAR: in.control = CW(4, SB_UGPR, SB_NONE, wait) | CW_YIELD; e->pending = 1u << SB_UGPR; break;
57 case K_STORE: in.control = CW(4, SB_NONE, SB_RD, wait) | CW_YIELD; e->pending = 1u << SB_RD; break;
58 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 */
59 case K_EXIT: in.control = CW(5, SB_NONE, SB_NONE, wait); e->pending = in.predicate_p0 ? wait : 0; break;
60 case K_BRA: in.control = CW(5, SB_NONE, SB_NONE, wait); e->pending = in.predicate_p0 ? wait : 0; break;
61 case K_BSSY: in.control = CW(1, SB_NONE, SB_NONE, wait) | CW_YIELD; e->pending = 0; break;
62 case K_BSYNC: in.control = CW(5, SB_NONE, SB_NONE, wait) | CW_YIELD; e->pending = 0; break;
63 default: e->err = -1; return;
64 }
65 if (omega_bw_ir_append(e->p, &in) < 0) e->err = -1;
66}
67
68static BlackwellIRInsn I0(BlackwellIROpcode op) {
69 BlackwellIRInsn in; memset(&in, 0, sizeof in);
70 in.op = op; in.dst_vreg = NOREG; in.src1_vreg = NOREG; in.src2_vreg = NOREG; in.src3_vreg = NOREG; in.ureg = NOREG;
71 return in;
72}
73static int V(Em *e) { int v = omega_bw_ir_alloc_vreg(e->p); if (v < 0) e->err = -1; return v; }
74static int V64(Em *e) { int v = omega_bw_ir_alloc_vreg64(e->p); if (v < 0) e->err = -1; return v; }
75static int V128(Em *e) { int v = omega_bw_ir_alloc_vreg128(e->p); if (v < 0) e->err = -1; return v; }
76static int UV64(Em *e) { int v = omega_bw_ir_alloc_uvreg64(e->p); if (v < 0) e->err = -1; return v; }
77static int here(const Em *e) { return (int)e->p->count; }
78
79static 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); }
80static 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); }
81static 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); }
82static 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); }
83static 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); }
84static void movrz(Em *e, int d) { BlackwellIRInsn i = I0(BW_IR_MOV_RZ); i.dst_vreg = d; emit(e, i, K_FIXED); }
85/* d = a * imm + c (c NOREG = RZ); a may be a sub-register of a quad */
86static 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); }
87/* d = a * b + c (registers) */
88static 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); }
89/* d64 = a * imm + c64 */
90static 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); }
91static 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); }
92static 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); }
93static void xorr(Em *e, int d, int a, int b) { BlackwellIRInsn i = I0(BW_IR_LOP3_XOR); i.dst_vreg = d; i.src1_vreg = a; i.src2_vreg = b; emit(e, i, K_FIXED); }
94static void ldg_wide(Em *e, BlackwellIROpcode op, int d, int addr64, uint32_t imm, int udesc, int first) {
95 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);
96}
97static 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); }
98static 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); }
99static 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); }
100static void lds32(Em *e, int d, int addr) { BlackwellIRInsn i = I0(BW_IR_LDS32); i.dst_vreg = d; i.src1_vreg = addr; emit(e, i, K_VAR); }
101static 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); }
102static void bar_sync(Em *e) { emit(e, I0(BW_IR_BAR_SYNC), K_SYNC); }
103static 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); }
104/* d = a * b[bsub] + c */
105static 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); }
106static 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); }
107static 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); }
108static 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); }
109static void bra(Em *e, int target) { BlackwellIRInsn i = I0(BW_IR_BRA); i.imm = (uint32_t)(target - here(e)); emit(e, i, K_BRA); }
110/* a forward branch whose target is patched when known: returns the instruction index */
111static 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; }
112static void patch_bra(Em *e, int at, int target) { if (!e->err) e->p->insns[at].imm = (uint32_t)(target - at); }
113static 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; }
114static void bsync(Em *e) { BlackwellIRInsn i = I0(BW_IR_BSYNC); i.bar_reg = 0; emit(e, i, K_BSYNC); }
115static void tail(Em *e) {
116 emit(e, I0(BW_IR_EXIT), K_EXIT);
117 BlackwellIRInsn self = I0(BW_IR_BRA);
118 self.control = 0x000fc000u;
119 if (omega_bw_ir_append(e->p, &self) < 0) e->err = -1;
120}
121
122/* Loops: a value defined before the loop and read inside it stays live until the back-branch
123 * (the linear-scan allocator knows no back edges; same rule as the elementwise emitter). */
124static void loop_begin(Em *e) { if (e->depth >= MAX_LOOPS) { e->err = -1; return; } e->open_start[e->depth++] = here(e); }
125static void loop_end(Em *e) {
126 if (e->depth <= 0) { e->err = -1; return; }
127 int start = e->open_start[--e->depth], end = here(e) - 1;
128 OmegaRegAlloc *ra = &e->p->regalloc;
129 for (int v = 0; v < ra->num_vregs; v++) {
130 OmegaLiveInterval *iv = &ra->intervals[v];
131 if (iv->first_def >= 0 && iv->first_def < start && iv->last_use >= start && iv->last_use < end) iv->last_use = end;
132 }
133}
134
135static int log2u(uint32_t v) { int l = 0; while ((1u << l) < v) l++; return (1u << l) == v ? l : -1; }
136
137static int spec_ok(const OmegaFp8GemvSpec *s) {
138 if (!s) return 0;
139 if (s->lane_bytes != 8 && s->lane_bytes != 16) return 0; /* LDG.E (32-bit) carries no immediate offset: no 4-byte lanes */
140 if (s->rows != 1 && s->rows != 2 && s->rows != 4 && s->rows != 8) return 0;
141 if (s->warps < 1 || s->warps > 32) return 0; /* threads_x = 32 * warps <= 1024 */
142 if (s->k == 0 || s->k > OMEGA_FP8_MAX_K || s->k % OMEGA_FP8_BLOCK || s->k % (32u * s->lane_bytes)) return 0;
143 if (s->mutant < 0 || s->mutant > 5 || (s->mutant >= 4 && !s->coal)) return 0;
144 if (s->sched > 1 || s->coal > 1 || s->wfirst > 1) return 0;
145 if (s->coal && s->warps * s->rows > 32u) return 0; /* warp 0 stores the round: one row per lane */
146 if (s->depth) { /* deep kernel: staging rounds (32-bit words, one per thread per round) must be exact and few */
147 uint32_t ks = s->k / (32u * s->lane_bytes);
148 if (s->depth > 16 || ks % s->depth || s->rows * s->depth * s->lane_bytes > 128u) return 0; /* <= 128 weight bytes per lane per iteration: instruction and register budgets */
149 if (s->k > 512u * s->warps) return 0; /* at most 16 staging rounds; a partial last round is clamped */
150 }
151 /* row offsets of a step must fit the 24-bit signed LDG immediate */
152 if ((uint64_t)(s->rows - 1) * s->k + s->lane_bytes >= (1u << 23)) return 0;
153 return 1;
154}
155
156uint32_t omega_fp8_gemv_steps(const OmegaFp8GemvSpec *spec) { return spec_ok(spec) ? spec->k / (32u * spec->lane_bytes) : 0; }
157/* deep: x' (k words) + one dummy slot for the clamped threads of a partial staging round; coal: then two output slot sets */
158static uint32_t x_stage_bytes(const OmegaFp8GemvSpec *s) { return s->depth ? s->k * 4u + 16u : 0u; }
159static uint32_t out_slot_bytes(const OmegaFp8GemvSpec *s) { return s->warps * s->rows * 4u; }
160uint32_t omega_fp8_gemv_shared_bytes(const OmegaFp8GemvSpec *spec) {
161 if (!spec_ok(spec)) return 0u;
162 return x_stage_bytes(spec) + (spec->coal ? 2u * out_slot_bytes(spec) : 0u);
163}
164
165/* e4m3 byte i of word w -> f32 bits (value * 2^-120): ((b & 0x7F) << 20) | ((b & 0x80) << 24) */
166static void decode_byte(Em *e, int f, int wq, unsigned word, unsigned i, int u, int a, int s) {
167 const uint32_t M = 0x07F00000u;
168 if (i < 3) imadi(e, u, wq, word, 1u << (20u - 8u * i), NOREG); /* b at bits 20..27 */
169 else shr(e, u, wq, word, 4);
170 andi(e, a, u, 0, M);
171 andi(e, s, wq, word, 0x80u << (8u * i));
172 if (i < 3) imadi(e, f, s, 0, 1u << (24u - 8u * i), a); /* sign to bit 31, plus the magnitude */
173 else imadi(e, f, s, 0, 1u, a);
174}
175
176/* ---- sched 1 (CUT5): pipelined byte decode ----
177 * Byte b of a K step (word b / 4, byte b % 4) goes through five instructions: E (shift the byte to bits 20..27),
178 * S (isolate its sign), M (mask the magnitude), F (sign to bit 31 plus magnitude = the f32 bits) and C (the FFMA into
179 * dot). Iteration c emits E(c), S(c), M(c - 2), F(c - 3), C(c - 5): every producer is at least 6 instructions ahead of
180 * its user (E->M 12, S->F 12+, M->F 6, F->C 11), so the stall pass below can give most instructions stall 1 while the
181 * FFMA chain (one per 5 instructions) runs at the 6-cycle fixed latency. t1 holds a byte from E to F (4 slots), t2 from
182 * S to C (6 slots). The FFMA order (byte 0, 1, ... of word 0, then word 1 ...) and every operand are those of the
183 * sched 0 loop, so y is bit-identical. */
184#define PIPE_T1 4
185#define PIPE_T2 6
186static void decode_pipe(Em *e, int wqv, uint32_t words, const int *xq, int dot, const int *t1, const int *t2) {
187 const uint32_t M = 0x07F00000u, NB = 4u * words;
188 for (uint32_t c = 0; c < NB + 5u; c++) {
189 if (c < NB) {
190 const uint32_t word = c / 4u, i = c % 4u;
191 const int u = t1[c % PIPE_T1], sg = t2[c % PIPE_T2];
192 if (i < 3) imadi(e, u, wqv, word, 1u << (20u - 8u * i), NOREG); else shr(e, u, wqv, word, 4);
193 andi(e, sg, wqv, word, 0x80u << (8u * i));
194 }
195 if (c >= 2 && c - 2u < NB) { const int u = t1[(c - 2u) % PIPE_T1]; andi(e, u, u, 0, M); }
196 if (c >= 3 && c - 3u < NB) {
197 const uint32_t b = c - 3u, i = b % 4u;
198 const int sg = t2[b % PIPE_T2], u = t1[b % PIPE_T1];
199 imadi(e, sg, sg, 0, i < 3 ? 1u << (24u - 8u * i) : 1u, u);
200 }
201 if (c >= 5 && c - 5u < NB) { const uint32_t b = c - 5u; ffma(e, dot, t2[b % PIPE_T2], xq[b / 4u], b % 4u, dot); }
202 }
203}
204
205/* ---- sched 1: dependency-driven stall counts (after register allocation) ----
206 * The emitter gives every fixed-latency instruction stall 6, which keeps any user >= 6 cycles behind its producer but
207 * also makes independent instructions wait 6 cycles each. The repository's chip-proven rule is the distance, not the
208 * per-instruction count: a fixed-latency producer stalls >= 5 cycles before its consumer (we keep 6;
209 * omega_gpu_elementwise_api.c:13). Same pass as omega_blackwell_codegen_tern.c (chip-proven at TERN-T0 2300f164) with
210 * this kernel's fixed-latency set, which adds FFMA, FADD and LOP3_XOR: walk the program in order on physical registers
211 * and give each instruction the smallest stall that keeps the next instruction >= OMEGA_FP8_FIXED_LAT cycles behind
212 * every fixed-latency write it reads or overwrites. Before a control instruction (BRA, EXIT, BSSY, BSYNC, BAR) or a
213 * branch target every fixed-latency write has completed, so straight-line timing never spans a jump. Other instructions
214 * keep at least their emitted stall (loads 4, ISETP 14 before a predicated branch); wait masks are untouched. */
215static int fp8_fixed(BlackwellIROpcode op) {
216 switch (op) {
217 case BW_IR_IMAD: case BW_IR_IMAD_WIDE: case BW_IR_SHF_R: case BW_IR_LOP3_AND: case BW_IR_LOP3_XOR: case BW_IR_MOV_IMM:
218 case BW_IR_MOV_RZ: case BW_IR_FFMA: case BW_IR_FADD: return 1;
219 default: return 0;
220 }
221}
222static int fp8_control(BlackwellIROpcode op) {
223 return op == BW_IR_BRA || op == BW_IR_EXIT || op == BW_IR_BSSY || op == BW_IR_BSYNC || op == BW_IR_BAR_SYNC;
224}
225static void fp8_span(const BlackwellIRProgram *p, int v, int *lo, int *cnt) {
226 *lo = -1; *cnt = 0;
227 if (v < 0 || v >= p->regalloc.num_vregs) return;
228 const int ph = p->regalloc.vreg_to_phys[v];
229 if (ph < 0) return;
230 *lo = ph; *cnt = (int)(p->regalloc.intervals[v].bundle_size ? p->regalloc.intervals[v].bundle_size : 1u);
231}
232static int fp8_schedule_stalls(BlackwellIRProgram *p) {
233 static uint8_t target[BW_MAX_IR_INSNS + 1];
234 uint64_t ready[BW_MAX_VREGS + 8];
235 const size_t n = p->count;
236 memset(target, 0, sizeof target);
237 memset(ready, 0, sizeof ready);
238 for (size_t i = 0; i < n; i++) {
239 const BlackwellIRInsn *in = &p->insns[i];
240 if (in->op == BW_IR_BRA || in->op == BW_IR_BSSY) {
241 const long t = (long)i + (long)(int32_t)in->imm;
242 if (t < 0 || t > (long)n) return -1;
243 target[t] = 1;
244 }
245 }
246 uint64_t T = 0, all = 0;
247 for (size_t i = 0; i < n; i++) {
248 BlackwellIRInsn *in = &p->insns[i];
249 const uint32_t s_emit = (in->control >> 9) & 0xfu;
250 const int fixed = fp8_fixed(in->op);
251 if (fixed) {
252 int lo, c; fp8_span(p, in->dst_vreg, &lo, &c);
253 for (int k = 0; k < c; k++) { if (lo + k >= BW_MAX_VREGS + 8) return -1; ready[lo + k] = T + OMEGA_FP8_FIXED_LAT; }
254 if (c && T + OMEGA_FP8_FIXED_LAT > all) all = T + OMEGA_FP8_FIXED_LAT;
255 }
256 uint64_t need = T + (fixed ? 1u : s_emit);
257 if (i + 1 < n) {
258 const BlackwellIRInsn *nx = &p->insns[i + 1];
259 const int ops[4] = { nx->dst_vreg, nx->src1_vreg, nx->src2_vreg, nx->src3_vreg };
260 for (int q = 0; q < 4; q++) {
261 int lo, c; fp8_span(p, ops[q], &lo, &c);
262 for (int k = 0; k < c; k++) if (lo + k < BW_MAX_VREGS + 8 && ready[lo + k] > need) need = ready[lo + k];
263 }
264 if ((fp8_control(nx->op) || target[i + 1]) && all > need) need = all;
265 }
266 const uint64_t s = need - T;
267 if (s > 15 || (s == 0 && (fixed || s_emit != 0))) return -1; /* stall 0 only where emitted (the final self-branch) */
268 if (fixed || s > s_emit) in->control = (in->control & ~(0xfu << 9)) | ((uint32_t)s << 9);
269 T = need;
270 }
271 return 0;
272}
273
274int omega_blackwell_codegen_fp8_gemv_prog(const OmegaFp8GemvSpec *spec, BlackwellIRProgram *prog) {
275 if (!spec_ok(spec) || !prog) return -1;
276 const uint32_t K = spec->k, LB = spec->lane_bytes, R = spec->rows, W = spec->warps;
277 const uint32_t KS = K / (32u * LB), BPS = 32u * LB / OMEGA_FP8_BLOCK, LPB = OMEGA_FP8_BLOCK / LB, KB = K / OMEGA_FP8_BLOCK;
278 const int log2lpb = log2u(LPB), log2rows_per_block = log2u(OMEGA_FP8_BLOCK / R);
279 const uint32_t WORDS = LB / 4u;
280 const BlackwellIROpcode ldw = LB == 16 ? BW_IR_LDG_E_128 : BW_IR_LDG_E_64;
281 if (log2lpb < 0 || log2rows_per_block < 0) return -1;
282 omega_bw_ir_init(prog);
283 Em em; memset(&em, 0, sizeof em); em.p = prog;
284 Em *e = &em;
285
286 /* ---- prologue ---- */
287 int udesc = UV64(e), ptab = V64(e), px = V64(e), py = V64(e);
288 int rgn = V(e), xs = V(e), ys = V(e), stride = V(e);
289 int tid = V(e), cx = V(e), cy = V(e), warp = V(e), lane = V(e), one = V(e), gidx = V(e);
290 int tent = V64(e), wbase = V64(e), sbase = V64(e), off = V(e), xrow = V64(e), yrow = V64(e), xlane = V64(e);
291 int wlane = V(e), slane = V(e), rg = V(e);
292 if (e->err) return -1;
293 ldcu64(e, udesc, 0x358);
294 ldc64(e, ptab, OMEGA_FP8_ARG_TAB); ldc64(e, px, OMEGA_FP8_ARG_X); ldc64(e, py, OMEGA_FP8_ARG_Y);
295 ldc32(e, rgn, OMEGA_FP8_ARG_RG); ldc32(e, xs, OMEGA_FP8_ARG_XSTRIDE); ldc32(e, ys, OMEGA_FP8_ARG_YSTRIDE); ldc32(e, stride, OMEGA_FP8_ARG_STRIDE);
296 s2r(e, tid, BW_SR_TID_X); s2r(e, cx, BW_SR_CTAID_X); s2r(e, cy, BW_SR_CTAID_Y);
297 movi(e, one, 1);
298 shr(e, warp, tid, 0, 5);
299 andi(e, lane, tid, 0, 31);
300 if (spec->mutant == 3) xorr(e, gidx, cy, one); else imadi(e, gidx, cy, 0, 1, NOREG);
301 imadw(e, tent, gidx, 16, ptab);
302 ldg_wide(e, BW_IR_LDG_E_64, wbase, tent, 0, udesc, 1);
303 ldg_wide(e, BW_IR_LDG_E_64, sbase, tent, 8, udesc, 0);
304 imadr(e, off, cy, xs, NOREG); imadw(e, xrow, off, 1, px);
305 imadr(e, off, cy, ys, NOREG); imadw(e, yrow, off, 1, py);
306 imadw(e, xlane, lane, LB * 4u, xrow); /* this lane's x' chunk of step 0 */
307 imadi(e, wlane, lane, 0, LB, NOREG); /* byte offset of this lane's chunk in a row */
308 shr(e, slane, lane, 0, (uint32_t)log2lpb); imadi(e, slane, slane, 0, 4, NOREG); /* scale word of this lane in a step */
309 imadi(e, rg, cx, 0, W, warp); /* first row group of this warp */
310
311 const uint32_t D = spec->depth ? spec->depth : 1u; /* K steps per loop iteration */
312 const int deep = spec->depth != 0;
313 int pw = V64(e), ps = V64(e), pxc = V64(e), pyr = V64(e), pya = V64(e), srg = V(e), cnt = V(e);
314 int acc[8], wq[8][16], xq[4], sc[16], psd[16], xl = -1, xl0 = -1;
315 /* xq[word]: the 4 x floats of weight word `word` (one 16-byte quad each); wq[r][s]: row r, step s of the iteration */
316 for (uint32_t r = 0; r < R; r++) { acc[r] = V(e); for (uint32_t s = 0; s < D; s++) wq[r][s] = LB == 16 ? V128(e) : V64(e); }
317 for (uint32_t q = 0; q < WORDS; q++) xq[q] = V128(e);
318 for (uint32_t s = 0; s < D; s++) { sc[s] = V(e); psd[s] = V64(e); }
319 int dot = V(e), u = V(e), a = V(e), sg = V(e), f = V(e), t = V(e);
320 int pt1[PIPE_T1], pt2[PIPE_T2]; /* sched 1 only: allocated after every sched 0 register, so sched 0 code is unchanged */
321 if (spec->sched) { for (int q = 0; q < PIPE_T1; q++) pt1[q] = V(e); for (int q = 0; q < PIPE_T2; q++) pt2[q] = V(e); }
322 /* coal 1 only, allocated last for the same reason: uc first row group of the CTA's round, rgx the clamped row group */
323 const int coal = spec->coal != 0;
324 int uc = NOREG, rgx = rg, um1 = NOREG, negU = NOREG, negn = NOREG, okw = NOREG, tq = NOREG, slb = NOREG, stog = NOREG;
325 int so = NOREG, sa = NOREG, wl1 = NOREG, val = NOREG, row = NOREG, skip = NOREG;
326 if (coal) {
327 uc = V(e); rgx = V(e); um1 = V(e); negU = V(e); negn = V(e); okw = V(e); tq = V(e); slb = V(e); stog = V(e);
328 so = V(e); sa = V(e); wl1 = V(e); val = V(e); row = V(e); skip = V(e);
329 }
330 if (e->err) return -1;
331
332 if (deep) {
333 /* ---- stage this group's x' row in shared memory (k floats), one 32-bit word per thread per round ---- */
334 const uint32_t rounds = (K + 32u * W - 1u) / (32u * W);
335 int pxt = V64(e), sa0 = V(e), sa = V(e), sap = V(e), pxs[16], xst[16], kq = V(e), over = V(e), tq = V(e), qs = V(e), qg = V(e);
336 for (uint32_t j = 0; j < rounds; j++) { pxs[j] = V64(e); xst[j] = V(e); }
337 xl = V(e); xl0 = V(e);
338 if (e->err) return -1;
339 imadw(e, pxt, tid, 4, xrow);
340 imadi(e, sa0, tid, 0, 4, NOREG);
341 for (uint32_t j = 0; j < rounds; j++) imadw(e, pxs[j], one, j * 128u * W, pxt);
342 if (K % (32u * W)) {
343 /* partial last round: word q = tid + j * 32 W may pass K. over = (q >= K) as 0/1 = (K - 1 - q) >> 31;
344 * the load is clamped to x[K - 1] and the store to the dummy slot K (never read). */
345 const uint32_t j = rounds - 1u;
346 imadi(e, kq, tid, 0, 1, NOREG); imadi(e, kq, one, 0, j * 32u * W, kq); /* q */
347 imadi(e, tq, kq, 0, 0xffffffffu, NOREG); imadi(e, tq, one, 0, K - 1u, tq); /* K - 1 - q */
348 shr(e, over, tq, 0, 31); /* 1 iff q >= K */
349 imadi(e, tq, kq, 0, 1, NOREG); imadi(e, tq, one, 0, (uint32_t)(0u - K), tq); /* q - K */
350 imadr(e, qs, over, tq, NOREG); imadi(e, qs, qs, 0, 0xffffffffu, kq); /* q - over * (q - K): K when over */
351 imadi(e, qg, tq, 0, 1, NOREG); imadi(e, qg, one, 0, 1, qg); /* q - K + 1 */
352 imadr(e, qg, over, qg, NOREG); imadi(e, qg, qg, 0, 0xffffffffu, kq); /* K - 1 when over */
353 imadw(e, pxs[j], qg, 4, xrow);
354 imadi(e, sap, qs, 0, 4, NOREG);
355 }
356 for (uint32_t j = 0; j < rounds; j++) ldg32(e, xst[j], pxs[j], udesc, j == 0);
357 for (uint32_t j = 0; j < rounds; j++) {
358 if (K % (32u * W) && j == rounds - 1u) { sts32(e, sap, xst[j]); continue; }
359 imadi(e, sa, one, 0, j * 128u * W, sa0); sts32(e, sa, xst[j]);
360 }
361 bar_sync(e);
362 imadi(e, xl0, lane, 0, LB * 4u, NOREG); /* this lane's x' chunk of step 0, shared byte offset */
363 }
364
365 if (coal) {
366 /* ---- coalesced output (variant A): per-CTA round variables. All values stay below 2^31 (RG, n <= 2^24),
367 * so (a - b) >> 31 is the 0/1 test a < b. ---- */
368 const uint32_t nrow = W * R;
369 imadi(e, um1, one, 0, 0xffffffffu, rgn); /* RG - 1 */
370 imadi(e, negU, rgn, 0, 0xffffffffu, NOREG); /* -RG */
371 imadi(e, negn, rgn, 0, 0u - R, NOREG); /* -n (n = RG * R) */
372 movi(e, slb, x_stage_bytes(spec)); /* slot set of this round (alternates) */
373 /* stog = sum of the two set bases: slb = stog - slb alternates (XOR would need a base aligned to the set size) */
374 movi(e, stog, 2u * x_stage_bytes(spec) + (spec->mutant == 4 ? 0u : out_slot_bytes(spec))); /* mutant 4: one slot set, the round race */
375 imadi(e, so, warp, 0, R * 4u, NOREG); /* this warp's slot: warp * R words */
376 imadi(e, tq, lane, 0, 0xffffffffu, NOREG); imadi(e, tq, one, 0, nrow - 1u, tq); shr(e, tq, tq, 0, 31); /* lane >= W R */
377 imadi(e, wl1, warp, 0, 1, tq); imadi(e, wl1, one, 0, 1, wl1); /* warp + (lane >= W R) + 1 */
378 imadi(e, uc, cx, 0, W, NOREG); /* first row group of this CTA's round (CTA-uniform) */
379 }
380
381 /* ---- row-group loop ---- */
382 const int L_rg = here(e);
383 loop_begin(e);
384 isetp_ge_u32(e, coal ? uc : rg, rgn); /* coal: the whole CTA leaves together (barriers inside) */
385 const int bra_end = bra_if_p0_fwd(e);
386 if (coal) {
387 /* a warp past the last row group recomputes row group RG - 1 (inside the tensor); its slot is never stored */
388 imadi(e, tq, rg, 0, 1, negU); /* rg - RG */
389 shr(e, okw, tq, 0, 31); /* 1 if rg < RG */
390 imadi(e, tq, one, 0, 1, tq); /* rg - RG + 1 */
391 if (spec->mutant == 5) imadi(e, rgx, rg, 0, 1, NOREG); /* mutant 5: no clamp, a warp past RG reads past the tensor */
392 else imadr(e, rgx, okw, tq, um1); /* rg < RG ? rg : RG - 1 */
393 }
394 imadw(e, pw, rgx, R * K, wbase); imadw(e, pw, wlane, 1, pw);
395 shr(e, srg, rgx, 0, (uint32_t)log2rows_per_block);
396 imadw(e, ps, srg, KB * 4u, sbase); imadw(e, ps, slane, 1, ps);
397 if (!deep) imadw(e, pxc, one, 0, xlane); else imadi(e, xl, xl0, 0, 1, NOREG);
398 for (uint32_t r = 0; r < R; r++) movrz(e, acc[r]);
399 movi(e, cnt, KS / D);
400
401 /* ---- K loop: D steps per iteration, every load of the iteration issued before any use ---- */
402 const int L_k = here(e);
403 loop_begin(e);
404 {
405 int first = 1;
406 const int xfirst = !deep && !spec->wfirst, xlast = !deep && spec->wfirst;
407 if (xfirst) { for (uint32_t q = 0; q < WORDS; q++) { ldg_wide(e, BW_IR_LDG_E_128, xq[q], pxc, q * 16u, udesc, first); first = 0; } }
408 for (uint32_t s = 0; s < D; s++) imadw(e, psd[s], one, spec->mutant == 2 ? 0u : s * BPS * 4u, ps);
409 for (uint32_t s = 0; s < D; s++) for (uint32_t r = 0; r < R; r++) { ldg_wide(e, ldw, wq[r][s], pw, r * K + s * 32u * LB, udesc, first); first = 0; }
410 for (uint32_t s = 0; s < D; s++) ldg32(e, sc[s], psd[s], udesc, 0);
411 if (xlast) for (uint32_t q = 0; q < WORDS; q++) ldg_wide(e, BW_IR_LDG_E_128, xq[q], pxc, q * 16u, udesc, 0); /* variant B */
412 }
413 for (uint32_t s = 0; s < D; s++) {
414 if (deep) { int first = 1; for (uint32_t q = 0; q < WORDS; q++) { lds128(e, xq[q], xl, s * 128u * LB + q * 16u, first); first = 0; } }
415 for (uint32_t r = 0; r < R; r++) {
416 movrz(e, dot);
417 if (spec->sched) decode_pipe(e, wq[r][s], WORDS, xq, dot, pt1, pt2);
418 else
419 for (uint32_t word = 0; word < WORDS; word++)
420 for (unsigned i = 0; i < 4; i++) {
421 decode_byte(e, f, wq[r][s], word, i, u, a, sg);
422 ffma(e, dot, f, xq[word], i, dot);
423 }
424 if (spec->mutant == 1) fadd(e, acc[r], acc[r], dot);
425 else ffma(e, acc[r], dot, sc[s], 0, acc[r]);
426 }
427 }
428 imadw(e, pw, one, D * 32u * LB, pw);
429 if (!deep) imadw(e, pxc, one, 32u * LB * 4u, pxc);
430 imadw(e, ps, one, spec->mutant == 2 ? 0u : D * BPS * 4u, ps);
431 if (deep) imadi(e, xl, one, 0, D * 128u * LB, xl);
432 imadi(e, cnt, one, 0, 0xffffffffu, cnt);
433 isetp_ge_u32(e, cnt, one);
434 bra_if_p0(e, L_k);
435 loop_end(e);
436
437 /* ---- warp reduction, lane 0 stores ---- */
438 for (uint32_t r = 0; r < R; r++)
439 for (uint32_t o = 16; o >= 1; o /= 2) { shfl_down(e, t, acc[r], o); fadd(e, acc[r], acc[r], t); }
440 if (!coal) {
441 imadw(e, pyr, rg, R * 4u, yrow);
442 isetp_ge_u32(e, lane, one);
443 const int at_bssy = bssy_fwd(e);
444 const int bra_skip = bra_if_p0_fwd(e);
445 for (uint32_t r = 0; r < R; r++) { imadw(e, pya, one, r * 4u, pyr); stg(e, pya, 0, acc[r], udesc); } /* STG.E with immediate 0 only (the chip-proven form) */
446 const int at_bsync = here(e);
447 bsync(e);
448 patch_bra(e, bra_skip, at_bsync);
449 patch_bra(e, at_bssy, at_bsync + 1);
450 } else {
451 /* lane 0 of each warp parks its R results in this round's slot set */
452 isetp_ge_u32(e, lane, one);
453 int at_bssy = bssy_fwd(e), bra_skip = bra_if_p0_fwd(e);
454 imadi(e, sa, slb, 0, 1, so); /* slot set + warp * R words */
455 for (uint32_t r = 0; r < R; r++) { if (r) imadi(e, sa, one, 0, 4u, sa); sts32(e, sa, acc[r]); }
456 int at_bsync = here(e);
457 bsync(e);
458 patch_bra(e, bra_skip, at_bsync);
459 patch_bra(e, at_bssy, at_bsync + 1);
460 bar_sync(e);
461 /* warp 0, lanes < W R: row = R uc + lane, stored when < n, one coalesced STG for the round */
462 imadi(e, row, uc, 0, R, lane);
463 imadi(e, okw, row, 0, 1, negn); shr(e, okw, okw, 0, 31); /* 1 if row < n */
464 imadi(e, skip, okw, 0, 0xffffffffu, wl1); /* warp + (lane >= W R) + 1 - (row < n): 0 only for a store */
465 isetp_ge_u32(e, skip, one);
466 at_bssy = bssy_fwd(e); bra_skip = bra_if_p0_fwd(e);
467 imadi(e, sa, lane, 0, 4u, slb);
468 lds32(e, val, sa);
469 imadw(e, pya, row, 4u, yrow);
470 stg(e, pya, 0, val, udesc);
471 at_bsync = here(e);
472 bsync(e);
473 patch_bra(e, bra_skip, at_bsync);
474 patch_bra(e, at_bssy, at_bsync + 1);
475 imadi(e, slb, slb, 0, 0xffffffffu, stog); /* next round parks in the other slot set: stog - slb */
476 imadi(e, uc, stride, 0, 1, uc);
477 }
478 imadi(e, rg, stride, 0, 1, rg);
479 bra(e, L_rg);
480 loop_end(e);
481 const int L_end = here(e);
482 patch_bra(e, bra_end, L_end);
483 tail(e);
484 if (e->err) return -1;
485 if (omega_bw_regalloc_solve(prog) != 0) return -1;
486 return spec->sched ? fp8_schedule_stalls(prog) : 0;
487}
488#else
489/* API/null handling and plain memory serialization only. All construction and
490 * validation decisions live in the seeded Omega unit. The callback carries the
491 * original spec pointer synchronously through the existing replay interface. */
492static uint32_t moe_unit(uint64_t *result, uint64_t ptr, uint64_t mode,
493 uint64_t unused_a, uint64_t unused_b,
494 void *records, uint64_t record_n, void *meta, uint64_t meta_n) {
495 const OmegaFp8GemvSpec *s = (const OmegaFp8GemvSpec *)(uintptr_t)ptr;
496 const uint64_t fields[16] = {s->k, s->lane_bytes, s->rows, s->warps,
497 s->depth, s->sched, s->coal, s->wfirst, (uint32_t)s->mutant, mode, unused_a}; /* field 10: ragged k allowed */
498 (void)unused_b;
499 return omega_cg_moe(result, fields, 16, records, record_n, meta, meta_n);
500}
501static uint32_t moe_policy(const OmegaFp8GemvSpec *s, uint64_t mode, uint64_t ragged) {
502 uint64_t records[8192], meta[512], result = 0;
503 if (!s || moe_unit(&result, (uintptr_t)s, mode, ragged, 0,
504 records, 8192, meta, 512) != OMEGA_OK) return 0;
505 return (uint32_t)result;
506}
507uint32_t omega_fp8_gemv_steps(const OmegaFp8GemvSpec *s) { return moe_policy(s, 2, 0); }
508uint32_t omega_fp8_gemv_shared_bytes(const OmegaFp8GemvSpec *s) { return moe_policy(s, 3, 0); }
509uint32_t omega_fp8_gemv_ragged_steps(const OmegaFp8GemvSpec *s) { return moe_policy(s, 2, 1); }
510uint32_t omega_fp8_gemv_ragged_shared_bytes(const OmegaFp8GemvSpec *s) { return moe_policy(s, 3, 1); }
511static int moe_schedule(BlackwellIRProgram *p) {
512 uint64_t records[8192] = {0}, meta[512] = {0}, result = 0;
513 for (unsigned v = 0; v < BW_MAX_VREGS; ++v) meta[v] = UINT64_MAX;
514 for (int v = 0; v < p->regalloc.num_vregs; ++v) {
515 meta[v] = (uint64_t)(int64_t)p->regalloc.vreg_to_phys[v];
516 meta[BW_MAX_VREGS + v] = p->regalloc.intervals[v].bundle_size;
517 }
518 for (size_t i = 0; i < p->count; ++i) {
519 const BlackwellIRInsn *in = &p->insns[i];
520 records[8*i] = in->op; records[8*i+1] = in->control; records[8*i+2] = in->imm;
521 records[8*i+3] = (uint64_t)(int64_t)in->dst_vreg;
522 records[8*i+4] = (uint64_t)(int64_t)in->src1_vreg;
523 records[8*i+5] = (uint64_t)(int64_t)in->src2_vreg;
524 records[8*i+6] = (uint64_t)(int64_t)in->src3_vreg;
525 }
526 if (omega_cg_moe_sched(&result, p->count, records, 8192, meta, 512) != OMEGA_OK || result) return -1;
527 for (size_t i = 0; i < p->count; ++i) p->insns[i].control = (uint32_t)records[8*i+1];
528 return 0;
529}
530
531static int moe_prog(const OmegaFp8GemvSpec *s, BlackwellIRProgram *p, uint64_t ragged) {
532 if (!s || !p) return -1;
533 if (omega_gpu_om_ir_replay(moe_unit, (uintptr_t)s, 0, ragged, 0, p, NULL)) return -1;
534 return s->sched ? moe_schedule(p) : 0;
535}
536int omega_blackwell_codegen_fp8_gemv_prog(const OmegaFp8GemvSpec *s, BlackwellIRProgram *p) { return moe_prog(s, p, 0); }
537int omega_blackwell_codegen_fp8_gemv_ragged_prog(const OmegaFp8GemvSpec *s, BlackwellIRProgram *p) { return moe_prog(s, p, 1); }
538int omega_fp8_gemv_ragged_prog_stats(const OmegaFp8GemvSpec *spec, uint32_t *insns, uint32_t *gprs) {
539 static BlackwellIRProgram prog;
540 if (omega_blackwell_codegen_fp8_gemv_ragged_prog(spec, &prog) != 0) return -1;
541 if (insns) *insns = (uint32_t)prog.count;
542 if (gprs) *gprs = prog.regalloc.peak_gpr_usage;
543 return 0;
544}
545/* (The frozen C path behind CUDAPP_MIGRATION_PENDING_C_CODEGEN_MOE, the differential oracle, has no ragged entry points.) */
546#endif
547
548int omega_fp8_gemv_prog_stats(const OmegaFp8GemvSpec *spec, uint32_t *insns, uint32_t *gprs) {
549 static BlackwellIRProgram prog; /* 1024 insns: too big for a stack frame in a test loop */
550 if (omega_blackwell_codegen_fp8_gemv_prog(spec, &prog) != 0) return -1;
551 if (insns) *insns = (uint32_t)prog.count;
552 if (gprs) *gprs = prog.regalloc.peak_gpr_usage;
553 return 0;
554}
555
556#ifndef OMEGA_GPU_NO_POSIX
557int omega_blackwell_codegen_fp8_gemv(const OmegaFp8GemvSpec *spec, OmegaBlackwellKernel *kernel) {
558 if (!kernel) return -1;
559 BlackwellIRProgram *prog = calloc(1, sizeof *prog);
560 if (!prog) return -1;
561 int rc = omega_blackwell_codegen_fp8_gemv_prog(spec, prog);
562 size_t need = rc == 0 ? omega_bw_program_code_bytes(prog) : 0;
563 uint8_t *buf = need ? malloc(need) : NULL;
564 if (!buf) { free(prog); return -1; }
565 rc = omega_blackwell_kernel_encode_into(prog, buf, need, kernel);
566 free(prog);
567 if (rc != 0) { free(buf); return -1; }
568 return 0; /* kernel->code points into buf; omega_blackwell_kernel_free releases it */
569}
570#endif