Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/CUDApp/blob/main/src/omega_blackwell_codegen_gdn.c # 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 Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `CUDApp / src / omega_blackwell_codegen_gdn.c` 1163 lines · 66748 bytes Raw Blame History 1 /* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception */ 2 /* 3 * omega_blackwell_codegen_gdn.c: the one-launch Gated DeltaNet decode step (see the header). Emitter rules follow 4 * omega_blackwell_codegen_router.c (control words, read barrier on every load) and the held-scoreboard prefetch of 5 * omega_bw_em.h (the state loads are in flight through the conv, norm and scalar phases). 6 */ 7 #include "omega_blackwell_codegen_gdn.h" 8 #include "omega_blackwell_codegen_tc.h" 9 #include 10 11 /* Independent differential oracle only; the seeded Omega successor (om/codegen/cg_gdn*.om) is the default. */ 12 #ifndef CUDAPP_MIGRATION_PENDING_C_CODEGEN_GDN 13 #define CUDAPP_MIGRATION_PENDING_C_CODEGEN_GDN 0 14 #endif 15 #if CUDAPP_MIGRATION_PENDING_C_CODEGEN_GDN 16 17 #define CW(stall, wbar, rbar, wait) \ 18 (((uint32_t)(stall) << 9) | ((uint32_t)(wbar) << 14) | ((uint32_t)(rbar) << 17) | ((uint32_t)(wait) << 20)) 19 #define CW_YIELD 0x2000u 20 #define SB_NONE 7u 21 #define SB_GPR 0u 22 #define SB_RD 1u 23 #define SB_UGPR 2u 24 #define SB_STATE 3u /* held: the 16 state quads */ 25 #define SB_GATE 4u /* held: z and the norm weight */ 26 #define NOREG (-1) 27 28 enum { K_FIXED, K_PRED, K_VAR, K_VAR_MORE, K_UVAR, K_STORE, K_SYNC, K_EXIT, K_BRA, K_BSSY, K_BSYNC }; 29 30 #define MAX_LOOPS 2 31 32 typedef struct { 33 BlackwellIRProgram *p; 34 uint32_t pending, held; 35 int err; 36 int depth, open_start[MAX_LOOPS]; 37 } Em; 38 39 static void emit(Em *e, BlackwellIRInsn in, int kind) { 40 uint32_t wait = e->pending; 41 switch (kind) { 42 case K_FIXED: in.control = CW(6, SB_NONE, SB_NONE, wait); e->pending = 0; break; 43 case K_PRED: in.control = CW(14, SB_NONE, SB_NONE, wait); e->pending = 0; break; 44 case K_VAR: in.control = CW(4, SB_GPR, SB_RD, wait) | CW_YIELD; e->pending = (1u << SB_GPR) | (1u << SB_RD); break; 45 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; 46 case K_UVAR: in.control = CW(4, SB_UGPR, SB_NONE, wait) | CW_YIELD; e->pending = 1u << SB_UGPR; break; 47 case K_STORE: in.control = CW(4, SB_NONE, SB_RD, wait) | CW_YIELD; e->pending = 1u << SB_RD; break; 48 case K_SYNC: in.control = CW(6, SB_NONE, SB_NONE, wait); e->pending = 0; break; 49 case K_EXIT: in.control = CW(5, SB_NONE, SB_NONE, wait); e->pending = in.predicate_p0 ? wait : 0; break; 50 case K_BRA: in.control = CW(5, SB_NONE, SB_NONE, wait); e->pending = in.predicate_p0 ? wait : 0; break; 51 case K_BSSY: in.control = CW(1, SB_NONE, SB_NONE, wait) | CW_YIELD; e->pending = 0; break; 52 case K_BSYNC: in.control = CW(5, SB_NONE, SB_NONE, wait) | CW_YIELD; e->pending = 0; break; 53 default: e->err = -1; return; 54 } 55 if (omega_bw_ir_append(e->p, &in) < 0) e->err = -1; 56 } 57 58 static BlackwellIRInsn I0(BlackwellIROpcode op) { 59 BlackwellIRInsn i; memset(&i, 0, sizeof i); 60 i.op = op; i.dst_vreg = NOREG; i.src1_vreg = NOREG; i.src2_vreg = NOREG; i.src3_vreg = NOREG; i.ureg = NOREG; 61 return i; 62 } 63 static int V(Em *e) { int v = omega_bw_ir_alloc_vreg(e->p); if (v < 0) e->err = -1; return v; } 64 static int V64(Em *e) { int v = omega_bw_ir_alloc_vreg64(e->p); if (v < 0) e->err = -1; return v; } 65 static int V128(Em *e) { int v = omega_bw_ir_alloc_vreg128(e->p); if (v < 0) e->err = -1; return v; } 66 static int UV64(Em *e) { int v = omega_bw_ir_alloc_uvreg64(e->p); if (v < 0) e->err = -1; return v; } 67 static int here(const Em *e) { return (int)e->p->count; } 68 69 /* operand with subregister */ 70 typedef struct { int v; unsigned s; } Op; 71 static Op R(int v) { Op o = { v, 0 }; return o; } 72 static Op S(int v, unsigned s) { Op o = { v, s }; return o; } 73 74 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); } 75 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); } 76 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); } 77 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); } 78 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); } 79 static void movrz(Em *e, Op d) { BlackwellIRInsn i = I0(BW_IR_MOV_RZ); i.dst_vreg = d.v; i.dst_subreg = (uint8_t)d.s; emit(e, i, K_FIXED); } 80 /* d = a * imm + c */ 81 static void imadi(Em *e, Op d, Op a, uint32_t imm, int c) { BlackwellIRInsn i = I0(BW_IR_IMAD); i.dst_vreg = d.v; i.dst_subreg = (uint8_t)d.s; i.src1_vreg = a.v; i.src1_subreg = (uint8_t)a.s; i.imm = imm; i.src3_vreg = c; emit(e, i, K_FIXED); } 82 /* d64 = a * imm + c64 */ 83 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); } 84 static void shr(Em *e, int d, int a, uint32_t imm) { BlackwellIRInsn i = I0(BW_IR_SHF_R); i.dst_vreg = d; i.src1_vreg = a; i.imm = imm; emit(e, i, K_FIXED); } 85 static void andi(Em *e, Op d, Op a, uint32_t imm) { BlackwellIRInsn i = I0(BW_IR_LOP3_AND); i.dst_vreg = d.v; i.dst_subreg = (uint8_t)d.s; i.src1_vreg = a.v; i.src1_subreg = (uint8_t)a.s; i.imm = imm; emit(e, i, K_FIXED); } 86 static void i2fp(Em *e, int d, int a) { BlackwellIRInsn i = I0(BW_IR_I2FP); i.dst_vreg = d; i.src1_vreg = a; emit(e, i, K_FIXED); } 87 static void ldg_wide(Em *e, BlackwellIROpcode op, int d, int addr64, uint32_t imm, int udesc, int first) { 88 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); 89 } 90 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); } 91 /* A prefetch load: scoreboard `sb` is held, not waited for, until consume() */ 92 static void ldg_pf(Em *e, int d, int addr64, uint32_t imm, int udesc, unsigned sb) { 93 BlackwellIRInsn in = I0(BW_IR_LDG_E_128); in.dst_vreg = d; in.src1_vreg = addr64; in.imm = imm; in.ureg = udesc; 94 in.control = CW(4, sb, SB_RD, (e->pending & (1u << SB_RD))) | CW_YIELD; 95 e->pending |= 1u << SB_RD; e->held |= 1u << sb; 96 if (omega_bw_ir_append(e->p, &in) < 0) e->err = -1; 97 } 98 /* L313: the bf16 state prefetch, LDG.E.64 (four bf16 of one state row) under the held scoreboard `sb` */ 99 static void ldg_pf64(Em *e, int d, int addr64, uint32_t imm, int udesc, unsigned sb) { 100 BlackwellIRInsn in = I0(BW_IR_LDG_E_64); in.dst_vreg = d; in.src1_vreg = addr64; in.imm = imm; in.ureg = udesc; 101 in.control = CW(4, sb, SB_RD, (e->pending & (1u << SB_RD))) | CW_YIELD; 102 e->pending |= 1u << SB_RD; e->held |= 1u << sb; 103 if (omega_bw_ir_append(e->p, &in) < 0) e->err = -1; 104 } 105 static void consume(Em *e, unsigned sb) { e->pending |= e->held & (1u << sb); e->held &= ~(1u << sb); } 106 /* STG.E [addr64 + imm], val (imm: signed 24-bit byte offset) */ 107 static void stg(Em *e, int addr64, uint32_t imm, Op val, int udesc) { BlackwellIRInsn i = I0(BW_IR_STG_E); i.src1_vreg = addr64; i.src2_vreg = val.v; i.src2_subreg = (uint8_t)val.s; i.imm = imm; i.ureg = udesc; emit(e, i, K_STORE); } 108 /* L313 bf16 state. F2FP.BF16.F32.PACK_AB d = bf16_rn(hi) << 16 | bf16_rn(lo) (native encoder entry since training R5) */ 109 static void f2fp(Em *e, Op d, Op hi, Op lo) { 110 BlackwellIRInsn i = I0(BW_IR_F2FP_BF16_PACK); i.dst_vreg = d.v; i.dst_subreg = (uint8_t)d.s; i.src1_vreg = hi.v; i.src1_subreg = (uint8_t)hi.s; 111 i.src2_vreg = lo.v; i.src2_subreg = (uint8_t)lo.s; emit(e, i, K_FIXED); 112 } 113 /* Four f32 (q, a V128) from four bf16 (w, a V64; element 0 in the low half of word 0): the exact widen. */ 114 static void bf16x4_widen(Em *e, int q, int w) { 115 for (unsigned j = 0; j < 4; j++) { 116 if (j % 2 == 0) imadi(e, S(q, j), S(w, j / 2), 65536u, NOREG); 117 else andi(e, S(q, j), S(w, j / 2), 0xffff0000u); 118 } 119 } 120 /* Four f32 (q) to four bf16 (w), round to nearest even. */ 121 static void bf16x4_pack(Em *e, int w, int q) { 122 f2fp(e, S(w, 0), S(q, 1), S(q, 0)); 123 f2fp(e, S(w, 1), S(q, 3), S(q, 2)); 124 } 125 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); } 126 static void lds32(Em *e, Op d, int addr, int first) { BlackwellIRInsn i = I0(BW_IR_LDS32); i.dst_vreg = d.v; i.dst_subreg = (uint8_t)d.s; i.src1_vreg = addr; emit(e, i, first ? K_VAR : K_VAR_MORE); } 127 static void sts32(Em *e, int addr, Op val) { BlackwellIRInsn i = I0(BW_IR_STS32); i.src1_vreg = addr; i.src2_vreg = val.v; i.src2_subreg = (uint8_t)val.s; emit(e, i, K_STORE); } 128 static void bar_sync(Em *e) { emit(e, I0(BW_IR_BAR_SYNC), K_SYNC); } 129 static void fop(Em *e, BlackwellIROpcode op, Op d, Op a, Op b, Op c) { 130 BlackwellIRInsn i = I0(op); i.dst_vreg = d.v; i.dst_subreg = (uint8_t)d.s; i.src1_vreg = a.v; i.src1_subreg = (uint8_t)a.s; 131 i.src2_vreg = b.v; i.src2_subreg = (uint8_t)b.s; i.src3_vreg = c.v; i.src3_subreg = (uint8_t)c.s; emit(e, i, K_FIXED); 132 } 133 static void fadd(Em *e, Op d, Op a, Op b) { fop(e, BW_IR_FADD, d, a, b, R(NOREG)); } 134 static void fsub(Em *e, Op d, Op a, Op b) { fop(e, BW_IR_FSUB, d, a, b, R(NOREG)); } 135 static void fmul(Em *e, Op d, Op a, Op b) { fop(e, BW_IR_FMUL, d, a, b, R(NOREG)); } 136 static void ffma(Em *e, Op d, Op a, Op b, Op c) { fop(e, BW_IR_FFMA, d, a, b, c); } 137 static void fmaxx(Em *e, Op d, Op a, Op b) { fop(e, BW_IR_FMNMX_MAX, d, a, b, R(NOREG)); } 138 static void fminn(Em *e, Op d, Op a, Op b) { fop(e, BW_IR_FMNMX_MIN, d, a, b, R(NOREG)); } 139 static void mufu(Em *e, BlackwellIROpcode op, Op d, Op a) { BlackwellIRInsn i = I0(op); i.dst_vreg = d.v; i.dst_subreg = (uint8_t)d.s; i.src1_vreg = a.v; i.src1_subreg = (uint8_t)a.s; emit(e, i, K_VAR); } 140 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); } 141 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); } 142 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); } 143 static int bra_fwd(Em *e) { int at = here(e); BlackwellIRInsn i = I0(BW_IR_BRA); i.imm = 1; emit(e, i, K_BRA); return at; } 144 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; } 145 static void patch_bra(Em *e, int at, int target) { if (!e->err) e->p->insns[at].imm = (uint32_t)(target - at); } 146 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; } 147 static void bsync(Em *e) { BlackwellIRInsn i = I0(BW_IR_BSYNC); i.bar_reg = 0; emit(e, i, K_BSYNC); } 148 static void tail(Em *e) { 149 emit(e, I0(BW_IR_EXIT), K_EXIT); 150 BlackwellIRInsn self = I0(BW_IR_BRA); 151 self.control = 0x000fc000u; 152 if (omega_bw_ir_append(e->p, &self) < 0) e->err = -1; 153 } 154 155 /* A region only some lanes of a warp execute: BSSY ... @P0 BRA join ... BSYNC. P0 true = skip. */ 156 typedef struct { int bssy, bra; } Region; 157 static Region region_skip_if_p0(Em *e) { Region r; r.bssy = bssy_fwd(e); r.bra = bra_if_p0_fwd(e); return r; } 158 static void region_end(Em *e, Region r) { int at = here(e); bsync(e); patch_bra(e, r.bra, at); patch_bra(e, r.bssy, at + 1); } 159 /* Loops: a value defined before the loop and read inside it stays live until the back-branch (the linear-scan 160 * allocator knows no back edges; omega_blackwell_codegen_pattn.c). */ 161 static void loop_begin(Em *e) { if (e->depth >= MAX_LOOPS) { e->err = -1; return; } e->open_start[e->depth++] = here(e); } 162 static void loop_end(Em *e) { 163 if (e->depth <= 0) { e->err = -1; return; } 164 int start = e->open_start[--e->depth], end = here(e) - 1; 165 OmegaRegAlloc *ra = &e->p->regalloc; 166 for (int v = 0; v < ra->num_vregs; v++) { 167 OmegaLiveInterval *iv = &ra->intervals[v]; 168 if (iv->first_def >= 0 && iv->first_def < start && iv->last_use >= start && iv->last_use < end) iv->last_use = end; 169 } 170 } 171 static void mov(Em *e, Op d, Op a) { imadi(e, d, a, 1u, NOREG); } 172 static void shfl_bfly(Em *e, int d, int a, uint32_t lanes) { BlackwellIRInsn i = I0(BW_IR_SHFL_BFLY); i.dst_vreg = d; i.src1_vreg = a; i.imm = lanes & 31u; emit(e, i, K_VAR); } 173 /* butterfly sum over lane strides lo, 2 lo .. hi: every lane of a group ends with the same bits (a + b == b + a) */ 174 static void bfly_sum(Em *e, int v, int t, uint32_t lo, uint32_t hi) { for (uint32_t o = lo; o <= hi; o *= 2) { shfl_bfly(e, t, v, o); fadd(e, R(v), R(v), R(t)); } } 175 /* warp sum: the total lands in lane 0 */ 176 static void warp_sum(Em *e, int v, int t) { for (uint32_t o = 16; o >= 1; o /= 2) { shfl_down(e, t, v, o); fadd(e, R(v), R(v), R(t)); } } 177 /* lane 0 of every warp stores v to shared [addr] */ 178 static void lane0_sts(Em *e, int lane, int one, int addr, Op v) { 179 isetp_ge_u32(e, lane, one); 180 Region r = region_skip_if_p0(e); sts32(e, addr, v); region_end(e, r); 181 } 182 /* x = x * sigmoid(x) = x * RCP(1 + EX2(-x * log2 e)); t scratch */ 183 static void silu(Em *e, Op x, int t, int nlog2e, int fone) { 184 fmul(e, R(t), x, R(nlog2e)); mufu(e, BW_IR_MUFU_EX2, R(t), R(t)); fadd(e, R(t), R(t), R(fone)); 185 mufu(e, BW_IR_MUFU_RCP, R(t), R(t)); fmul(e, x, x, R(t)); 186 } 187 static uint32_t fbits(float f) { uint32_t u; memcpy(&u, &f, 4); return u; } 188 /* The 8-way cross-warp sum of acc[0..3] (columns 4L..4L+3 of each warp) through shared SM_P: pa = this thread's 189 * partial slot, la = lane * 16. Every warp ends with the same totals, summed in warp order 0..7. */ 190 static void cross_warp_sum(Em *e, int acc[4], int pa, int one, int la) { 191 int pj = V(e), b0 = V128(e), b1 = V128(e); 192 if (e->err) return; 193 sts32(e, pa, R(acc[0])); 194 for (unsigned j = 1; j < 4; j++) { imadi(e, R(pj), R(one), 4u * j, pa); sts32(e, pj, R(acc[j])); } 195 bar_sync(e); 196 for (unsigned w2 = 0; w2 < 8; w2 += 2) { 197 lds128(e, b0, la, OMEGA_GDN_SM_P + w2 * OMEGA_GDN_HEAD_DIM * 4u, 1); 198 lds128(e, b1, la, OMEGA_GDN_SM_P + (w2 + 1) * OMEGA_GDN_HEAD_DIM * 4u, 0); 199 for (unsigned j = 0; j < 4; j++) { 200 if (w2 == 0) fadd(e, R(acc[j]), S(b0, j), S(b1, j)); 201 else { fadd(e, R(acc[j]), R(acc[j]), S(b0, j)); fadd(e, R(acc[j]), R(acc[j]), S(b1, j)); } 202 } 203 } 204 } 205 206 int omega_gdn_shape(const OmegaGdnSpec *spec, OmegaGdnShape *s) { 207 uint32_t r = spec->ratio ? spec->ratio : 3u; 208 if (r < 1u || r > 3u) return -1; 209 s->r = r; 210 s->vh = OMEGA_GDN_KEY_HEADS * r; 211 s->vdim = s->vh * OMEGA_GDN_HEAD_DIM; 212 s->conv = 2u * OMEGA_GDN_KEY_HEADS * OMEGA_GDN_HEAD_DIM + s->vdim; 213 s->par_scal = s->conv * OMEGA_GDN_TAPS * 4u; 214 s->par_norm = s->par_scal + s->vh * 8u; 215 s->par_bytes = s->par_norm + OMEGA_GDN_HEAD_DIM * 4u; 216 s->tail_bytes = s->vh * OMEGA_GDN_TAIL_HEAD_BYTES; 217 s->state_bytes = spec->state_bf16 ? s->vh * (OMEGA_GDN_STATE_HEAD_BYTES / 2u) : s->vh * OMEGA_GDN_STATE_HEAD_BYTES; 218 s->post_grid_y = s->vh / 8u; 219 return 0; 220 } 221 222 int omega_blackwell_codegen_gdn_prog(const OmegaGdnSpec *spec, BlackwellIRProgram *prog) { 223 if (!spec || !prog || spec->copy || spec->mutant < 0 || spec->mutant > 4) return -1; 224 OmegaGdnShape G; 225 if (omega_gdn_shape(spec, &G)) return -1; 226 const uint32_t RR = G.r, NC = 2u + G.r; 227 (void)RR; (void)NC; 228 const int MUT = spec->mutant; 229 const uint32_t C_LOG2E = fbits(1.`44269504f`), C_NLOG2E = fbits(-1.`44269504f`), C_LN2 = fbits(0.`693147181f`); 230 const uint32_t C_ONE = fbits(1.0f), C_M1 = fbits(-1.0f), C_INV128 = fbits(1.0f / 128.0f); 231 const float qs = 0.`0883883476f`; /* 1 / sqrt(128) */ 232 omega_bw_ir_init(prog); 233 Em em; memset(&em, 0, sizeof em); em.p = prog; Em *e = &em; 234 235 /* ---- identities; the state address ---- */ 236 int udesc = UV64(e); 237 int tid = V(e), cx = V(e), cy = V(e), h = V(e), warp = V(e), lane = V(e), one = V(e); 238 int ps = V64(e), sa = V64(e), toff = V(e), sq[16]; 239 for (unsigned r = 0; r < 16; r++) sq[r] = V128(e); 240 if (e->err) return -1; 241 ldcu64(e, udesc, 0x358); 242 ldc64(e, ps, OMEGA_GDN_ARG_STATE); 243 s2r(e, tid, BW_SR_TID_X); s2r(e, cx, BW_SR_CTAID_X); s2r(e, cy, BW_SR_CTAID_Y); 244 movi(e, one, 1); 245 imadi(e, R(h), R(cy), RR, cx); 246 shr(e, warp, tid, 5); 247 andi(e, R(lane), R(tid), 31u); 248 /* the state element size: 4 (f32) or 2 (bf16, L313); this thread holds rows 16 warp .. +15, columns 4 lane .. +3 */ 249 const unsigned sel = spec->state_bf16 ? 2u : 4u; 250 imadi(e, R(toff), R(h), OMEGA_GDN_HEAD_DIM * OMEGA_GDN_HEAD_DIM * sel, NOREG); 251 imadi(e, R(toff), R(warp), 16u * OMEGA_GDN_HEAD_DIM * sel, toff); 252 imadi(e, R(toff), R(lane), 4u * sel, toff); 253 imadw(e, sa, toff, 1u, ps); 254 255 /* ---- phase A: conv + SiLU of channel A (q or k) and channel V ---- */ 256 int half = V(e), c = V(e), gi = V(e), slv = V(e); 257 int pqkv = V64(e), ppar = V64(e), ptail = V64(e); 258 int axA = V64(e), awA = V64(e), atA = V64(e), axV = V64(e), awV = V64(e), atV = V64(e); 259 int xA = V(e), xV = V(e), wA = V128(e), wV = V128(e), tA = V128(e), tV = V128(e); 260 int insc = V(e), accA = V(e), accV = V(e), t1 = V(e), cnl2e = V(e), fone = V(e); 261 if (e->err) return -1; 262 ldc64(e, pqkv, OMEGA_GDN_ARG_QKV); ldc64(e, ppar, OMEGA_GDN_ARG_PAR); ldc64(e, ptail, OMEGA_GDN_ARG_TAIL); 263 ldc32(e, insc, OMEGA_GDN_ARG_IN_SCALE); 264 shr(e, half, tid, 7); 265 andi(e, R(c), R(tid), 127u); 266 imadi(e, R(gi), R(cy), OMEGA_GDN_HEAD_DIM, tid); /* q channel hk*128 + t; k channel 2048 + hk*128 + (t - 128) */ 267 imadi(e, R(gi), R(half), 2048u - 128u, gi); 268 imadw(e, axA, gi, 4u, pqkv); imadw(e, awA, gi, 16u, ppar); 269 imadi(e, R(gi), R(h), OMEGA_GDN_HEAD_DIM, c); /* v channel 4096 + h*128 + c */ 270 imadi(e, R(gi), R(one), 2u * OMEGA_GDN_KEY_HEADS * OMEGA_GDN_HEAD_DIM, gi); 271 imadw(e, axV, gi, 4u, pqkv); imadw(e, awV, gi, 16u, ppar); 272 imadw(e, atA, tid, 16u, ptail); imadw(e, atA, h, OMEGA_GDN_TAIL_HEAD_BYTES, atA); 273 imadi(e, R(slv), R(one), 256u, c); 274 imadw(e, atV, slv, 16u, ptail); imadw(e, atV, h, OMEGA_GDN_TAIL_HEAD_BYTES, atV); 275 ldg32(e, xA, axA, udesc, 1); 276 ldg_wide(e, BW_IR_LDG_E_128, wA, awA, 0, udesc, 0); 277 ldg_wide(e, BW_IR_LDG_E_128, tA, atA, 0, udesc, 0); 278 ldg32(e, xV, axV, udesc, 0); 279 ldg_wide(e, BW_IR_LDG_E_128, wV, awV, 0, udesc, 0); 280 ldg_wide(e, BW_IR_LDG_E_128, tV, atV, 0, udesc, 0); 281 fmul(e, R(xA), R(xA), R(insc)); fmul(e, R(xV), R(xV), R(insc)); 282 fmul(e, R(accA), S(wA, 0), S(tA, 0)); ffma(e, R(accA), S(wA, 1), S(tA, 1), R(accA)); 283 ffma(e, R(accA), S(wA, 2), S(tA, 2), R(accA)); ffma(e, R(accA), S(wA, 3), R(xA), R(accA)); 284 fmul(e, R(accV), S(wV, 0), S(tV, 0)); ffma(e, R(accV), S(wV, 1), S(tV, 1), R(accV)); 285 ffma(e, R(accV), S(wV, 2), S(tV, 2), R(accV)); ffma(e, R(accV), S(wV, 3), R(xV), R(accV)); 286 movi(e, cnl2e, C_NLOG2E); movi(e, fone, C_ONE); 287 silu(e, R(accA), t1, cnl2e, fone); 288 silu(e, R(accV), t1, cnl2e, fone); 289 /* the state prefetch (here, not earlier: the conv phase is the register peak) */ 290 int sw[16]; 291 if (spec->state_bf16) { 292 for (unsigned r = 0; r < 16; r++) sw[r] = V64(e); 293 if (e->err) return -1; 294 for (unsigned r = 0; r < 16; r++) ldg_pf64(e, sw[r], sa, r * OMEGA_GDN_HEAD_DIM * 2u, udesc, SB_STATE); 295 } else { 296 for (unsigned r = 0; r < 16; r++) ldg_pf(e, sq[r], sa, r * OMEGA_GDN_HEAD_DIM * 4u, udesc, SB_STATE); 297 } 298 299 /* ---- phase B: l2 norms; q|k and v to shared; the tail shift ---- */ 300 int sqs = V(e), ts = V(e), ra = V(e); 301 if (e->err) return -1; 302 fmul(e, R(sqs), R(accA), R(accA)); 303 warp_sum(e, sqs, ts); 304 imadi(e, R(ra), R(warp), 4u, NOREG); imadi(e, R(ra), R(one), OMEGA_GDN_SM_RED, ra); 305 lane0_sts(e, lane, one, ra, R(sqs)); 306 imadi(e, R(ra), R(c), 4u, NOREG); imadi(e, R(ra), R(one), OMEGA_GDN_SM_V, ra); 307 sts32(e, ra, R(accV)); 308 bar_sync(e); 309 if (MUT != 4) { 310 stg(e, atA, 0, S(tA, 1), udesc); stg(e, atA, 4, S(tA, 2), udesc); stg(e, atA, 8, R(xA), udesc); 311 stg(e, atV, 0, S(tV, 1), udesc); stg(e, atV, 4, S(tV, 2), udesc); stg(e, atV, 8, R(xV), udesc); 312 } 313 { 314 int rq = V128(e), el2 = V(e), hf = V(e), fq = V(e); 315 if (e->err) return -1; 316 ldc32(e, el2, OMEGA_GDN_ARG_EPS_L2); 317 imadi(e, R(ra), R(half), 16u, NOREG); 318 lds128(e, rq, ra, OMEGA_GDN_SM_RED, 1); 319 fadd(e, R(sqs), S(rq, 0), S(rq, 1)); fadd(e, R(sqs), R(sqs), S(rq, 2)); fadd(e, R(sqs), R(sqs), S(rq, 3)); 320 fadd(e, R(sqs), R(sqs), R(el2)); 321 mufu(e, BW_IR_MUFU_RSQ, R(sqs), R(sqs)); 322 /* q gets 1/sqrt(128) more: factor = qs + half * (1 - qs) */ 323 i2fp(e, hf, half); 324 movi(e, fq, fbits(qs)); movi(e, t1, fbits(1.0f - qs)); 325 ffma(e, R(fq), R(hf), R(t1), R(fq)); 326 fmul(e, R(accA), R(accA), R(sqs)); fmul(e, R(accA), R(accA), R(fq)); 327 imadi(e, R(ra), R(tid), 4u, NOREG); /* SM_Q = 0, SM_K = SM_Q + 512 */ 328 sts32(e, ra, R(accA)); 329 bar_sync(e); /* q and k are read across warps in phase D (a zero state hides a missing barrier here: 0 * poison = 0) */ 330 } 331 332 /* ---- phase C: scalars ---- */ 333 int pab = V64(e), aab = V64(e), asc = V64(e), abq = V64(e), scq = V64(e); 334 int xs = V(e), mn = V(e), sp = V(e), dec = V(e), beta = V(e), cl2e = V(e); 335 if (e->err) return -1; 336 ldc64(e, pab, OMEGA_GDN_ARG_AB); 337 imadw(e, aab, h, 8u, pab); imadw(e, asc, h, 8u, ppar); 338 ldg_wide(e, BW_IR_LDG_E_64, abq, aab, 0, udesc, 1); 339 ldg_wide(e, BW_IR_LDG_E_64, scq, asc, G.par_scal, udesc, 0); 340 /* softplus(x) = max(x, 0) + ln2 * LG2(1 + EX2(-|x| * log2 e)), x = a + dt_bias */ 341 fadd(e, R(xs), S(abq, 0), S(scq, 1)); 342 movi(e, mn, C_M1); fmul(e, R(mn), R(xs), R(mn)); fminn(e, R(mn), R(xs), R(mn)); 343 movi(e, cl2e, C_LOG2E); 344 fmul(e, R(mn), R(mn), R(cl2e)); mufu(e, BW_IR_MUFU_EX2, R(mn), R(mn)); fadd(e, R(mn), R(mn), R(fone)); 345 mufu(e, BW_IR_MUFU_LG2, R(mn), R(mn)); 346 movrz(e, R(sp)); fmaxx(e, R(sp), R(xs), R(sp)); 347 movi(e, t1, C_LN2); ffma(e, R(sp), R(mn), R(t1), R(sp)); 348 /* decay = EX2(negA * softplus * log2 e) */ 349 if (MUT == 1) movi(e, dec, C_ONE); 350 else { fmul(e, R(dec), S(scq, 0), R(sp)); fmul(e, R(dec), R(dec), R(cl2e)); mufu(e, BW_IR_MUFU_EX2, R(dec), R(dec)); } 351 /* beta = sigmoid(b) */ 352 if (MUT == 2) movi(e, beta, C_ONE); 353 else { 354 fmul(e, R(beta), S(abq, 1), R(cnl2e)); mufu(e, BW_IR_MUFU_EX2, R(beta), R(beta)); 355 fadd(e, R(beta), R(beta), R(fone)); mufu(e, BW_IR_MUFU_RCP, R(beta), R(beta)); 356 } 357 358 /* ---- phase D: the state update (fresh registers per step: a reused name stays live across all its uses) ---- */ 359 int aw = V(e), pa = V(e), la = V(e), kq[4], kv[4], dl[4], vq = V128(e); 360 for (unsigned i = 0; i < 4; i++) { kq[i] = V128(e); kv[i] = V(e); dl[i] = V(e); } 361 if (e->err) return -1; 362 imadi(e, R(aw), R(warp), 16u * 4u, NOREG); 363 imadi(e, R(pa), R(warp), OMEGA_GDN_HEAD_DIM * 4u, NOREG); imadi(e, R(pa), R(lane), 16u, pa); imadi(e, R(pa), R(one), OMEGA_GDN_SM_P, pa); 364 imadi(e, R(la), R(lane), 16u, NOREG); 365 consume(e, SB_STATE); 366 if (spec->state_bf16) for (unsigned r = 0; r < 16; r++) bf16x4_widen(e, sq[r], sw[r]); 367 for (unsigned r = 0; r < 16; r++) for (unsigned j = 0; j < 4; j++) fmul(e, S(sq[r], j), S(sq[r], j), R(dec)); 368 /* kv partial over this warp's 16 rows: kv_j = sum_r S[r][j] k[r], then the 8-way cross-warp sum */ 369 for (unsigned i = 0; i < 4; i++) lds128(e, kq[i], aw, OMEGA_GDN_SM_K + i * 16u, i == 0); 370 for (unsigned j = 0; j < 4; j++) fmul(e, R(kv[j]), S(sq[0], j), S(kq[0], 0)); 371 for (unsigned r = 1; r < 16; r++) for (unsigned j = 0; j < 4; j++) ffma(e, R(kv[j]), S(sq[r], j), S(kq[r / 4], r % 4), R(kv[j])); 372 cross_warp_sum(e, kv, pa, one, la); 373 /* delta = (v - kv) * beta; S += k (outer) delta; S back to memory */ 374 lds128(e, vq, la, OMEGA_GDN_SM_V, 1); 375 for (unsigned j = 0; j < 4; j++) { fsub(e, R(dl[j]), S(vq, j), R(kv[j])); fmul(e, R(dl[j]), R(dl[j]), R(beta)); } 376 { 377 int k2[4]; 378 for (unsigned i = 0; i < 4; i++) k2[i] = V128(e); 379 if (e->err) return -1; 380 for (unsigned i = 0; i < 4; i++) lds128(e, k2[i], aw, OMEGA_GDN_SM_K + i * 16u, i == 0); 381 for (unsigned r = 0; r < 16; r++) for (unsigned j = 0; j < 4; j++) ffma(e, S(sq[r], j), S(k2[r / 4], r % 4), R(dl[j]), S(sq[r], j)); 382 } 383 if (spec->state_bf16) { 384 int pk[16]; 385 for (unsigned r = 0; r < 16; r++) pk[r] = V64(e); 386 if (e->err) return -1; 387 for (unsigned r = 0; r < 16; r++) { 388 bf16x4_pack(e, pk[r], sq[r]); 389 for (unsigned w = 0; w < 2; w++) stg(e, sa, r * OMEGA_GDN_HEAD_DIM * 2u + w * 4u, S(pk[r], w), udesc); 390 } 391 } else { 392 for (unsigned r = 0; r < 16; r++) for (unsigned j = 0; j < 4; j++) stg(e, sa, r * OMEGA_GDN_HEAD_DIM * 4u + j * 4u, S(sq[r], j), udesc); 393 } 394 /* the gate prefetch: z[h] and the norm weight, consumed in phase E */ 395 int pz = V64(e), ppar2 = V64(e), az = V64(e), an = V64(e), zq = V128(e), nq = V128(e); 396 if (e->err) return -1; 397 ldc64(e, pz, OMEGA_GDN_ARG_Z); ldc64(e, ppar2, OMEGA_GDN_ARG_PAR); 398 imadw(e, az, lane, 16u, pz); imadw(e, az, h, OMEGA_GDN_HEAD_DIM * 4u, az); 399 imadw(e, an, lane, 16u, ppar2); 400 ldg_pf(e, zq, az, 0, udesc, SB_GATE); 401 ldg_pf(e, nq, an, G.par_norm, udesc, SB_GATE); 402 /* o partial: o_j = sum_r S[r][j] q[r], then the cross-warp sum */ 403 int p[4]; 404 { 405 int q2[4]; 406 for (unsigned i = 0; i < 4; i++) { q2[i] = V128(e); p[i] = V(e); } 407 if (e->err) return -1; 408 for (unsigned i = 0; i < 4; i++) lds128(e, q2[i], aw, OMEGA_GDN_SM_Q + i * 16u, i == 0); 409 for (unsigned j = 0; j < 4; j++) fmul(e, R(p[j]), S(sq[0], j), S(q2[0], 0)); 410 for (unsigned r = 1; r < 16; r++) for (unsigned j = 0; j < 4; j++) ffma(e, R(p[j]), S(sq[r], j), S(q2[r / 4], r % 4), R(p[j])); 411 } 412 bar_sync(e); /* every warp has read the kv partials */ 413 cross_warp_sum(e, p, pa, one, la); 414 415 /* ---- phase E: gated RMSNorm, the output ---- */ 416 int ss = V(e), en = V(e), osc = V(e), pout = V64(e), ao = V64(e), g = V(e); 417 if (e->err) return -1; 418 fmul(e, R(ss), R(p[0]), R(p[0])); 419 for (unsigned j = 1; j < 4; j++) ffma(e, R(ss), R(p[j]), R(p[j]), R(ss)); 420 warp_sum(e, ss, ts); 421 imadi(e, R(ra), R(warp), 4u, NOREG); imadi(e, R(ra), R(one), OMEGA_GDN_SM_RED2, ra); 422 lane0_sts(e, lane, one, ra, R(ss)); 423 bar_sync(e); 424 ldc32(e, en, OMEGA_GDN_ARG_EPS_NORM); ldc32(e, osc, OMEGA_GDN_ARG_OUT_SCALE); ldc64(e, pout, OMEGA_GDN_ARG_OUT); 425 lds32(e, R(ss), ra, 0); 426 movi(e, t1, C_INV128); 427 ffma(e, R(ss), R(ss), R(t1), R(en)); 428 mufu(e, BW_IR_MUFU_RSQ, R(ss), R(ss)); 429 imadw(e, ao, lane, 16u, pout); imadw(e, ao, h, OMEGA_GDN_HEAD_DIM * 4u, ao); 430 consume(e, SB_GATE); 431 for (unsigned j = 0; j < 4; j++) { 432 fmul(e, R(g), S(zq, j), R(insc)); 433 if (MUT != 3) silu(e, R(g), t1, cnl2e, fone); 434 fmul(e, R(p[j]), R(p[j]), R(ss)); fmul(e, R(p[j]), R(p[j]), S(nq, j)); 435 fmul(e, R(p[j]), R(p[j]), R(g)); fmul(e, R(p[j]), R(p[j]), R(osc)); 436 } 437 for (unsigned j = 0; j < 4; j++) stg(e, ao, j * 4u, R(p[j]), udesc); 438 tail(e); 439 if (e->err) return -1; 440 return omega_bw_regalloc_solve(prog); 441 } 442 443 /* ======== state copy / zero (see the header) ======== */ 444 int omega_blackwell_codegen_gdn_copy_prog(const OmegaGdnSpec *spec, BlackwellIRProgram *prog) { 445 if (!spec || !prog || spec->mutant || (spec->copy != OMEGA_GDN_COPY && spec->copy != OMEGA_GDN_ZERO)) return -1; 446 omega_bw_ir_init(prog); 447 Em em; memset(&em, 0, sizeof em); em.p = prog; Em *e = &em; 448 int udesc = UV64(e), tid = V(e), cx = V(e), w = V(e), pd = V64(e), ad = V64(e), v[4]; 449 for (unsigned j = 0; j < 4; j++) v[j] = V(e); 450 if (e->err) return -1; 451 ldcu64(e, udesc, 0x358); 452 ldc64(e, pd, OMEGA_GDN_COPY_ARG_DST); 453 s2r(e, tid, BW_SR_TID_X); s2r(e, cx, BW_SR_CTAID_X); 454 imadi(e, R(w), R(cx), OMEGA_GDN_COPY_WORDS_PER_CTA, tid); 455 imadw(e, ad, w, 4u, pd); 456 if (spec->copy == OMEGA_GDN_COPY) { 457 /* the 32-bit LDG.E carries no immediate: one address per word */ 458 int ps = V64(e), one = V(e), wj[4], as[4]; 459 for (unsigned j = 0; j < 4; j++) { wj[j] = V(e); as[j] = V64(e); } 460 if (e->err) return -1; 461 ldc64(e, ps, OMEGA_GDN_COPY_ARG_SRC); 462 movi(e, one, 1); 463 for (unsigned j = 0; j < 4; j++) { imadi(e, R(wj[j]), R(one), j * OMEGA_GDN_COPY_THREADS, w); imadw(e, as[j], wj[j], 4u, ps); } 464 for (unsigned j = 0; j < 4; j++) ldg_wide(e, BW_IR_LDG_E, v[j], as[j], 0, udesc, j == 0); 465 } else { 466 for (unsigned j = 0; j < 4; j++) movrz(e, R(v[j])); 467 } 468 for (unsigned j = 0; j < 4; j++) stg(e, ad, j * OMEGA_GDN_COPY_THREADS * 4u, R(v[j]), udesc); 469 tail(e); 470 if (e->err) return -1; 471 return omega_bw_regalloc_solve(prog); 472 } 473 474 /* ======== prefill (omega#543): prep, scan, post (layouts in the header) ======== */ 475 476 /* softplus, decay and beta of one value head (the decode phase C maths): abq = {a, b}, scq = {negA, dt_bias} */ 477 static void gdn_scalars(Em *e, int MUT, int abq, int scq, int dec, int beta, int t1, int cnl2e, int fone) { 478 int xs = V(e), mn = V(e), sp = V(e), cl2e = V(e); 479 if (e->err) return; 480 fadd(e, R(xs), S(abq, 0), S(scq, 1)); 481 movi(e, mn, fbits(-1.0f)); fmul(e, R(mn), R(xs), R(mn)); fminn(e, R(mn), R(xs), R(mn)); 482 movi(e, cl2e, fbits(1.`44269504f`)); 483 fmul(e, R(mn), R(mn), R(cl2e)); mufu(e, BW_IR_MUFU_EX2, R(mn), R(mn)); fadd(e, R(mn), R(mn), R(fone)); 484 mufu(e, BW_IR_MUFU_LG2, R(mn), R(mn)); 485 movrz(e, R(sp)); fmaxx(e, R(sp), R(xs), R(sp)); 486 movi(e, t1, fbits(0.`693147181f`)); ffma(e, R(sp), R(mn), R(t1), R(sp)); 487 if (MUT == 1) movi(e, dec, fbits(1.0f)); 488 else { fmul(e, R(dec), S(scq, 0), R(sp)); fmul(e, R(dec), R(dec), R(cl2e)); mufu(e, BW_IR_MUFU_EX2, R(dec), R(dec)); } 489 if (MUT == 2) movi(e, beta, fbits(1.0f)); 490 else { 491 fmul(e, R(beta), S(abq, 1), R(cnl2e)); mufu(e, BW_IR_MUFU_EX2, R(beta), R(beta)); 492 fadd(e, R(beta), R(beta), R(fone)); mufu(e, BW_IR_MUFU_RCP, R(beta), R(beta)); 493 } 494 } 495 496 int omega_blackwell_codegen_gdn_prep_prog(const OmegaGdnSpec *spec, BlackwellIRProgram *prog) { 497 if (!spec || !prog || spec->copy || spec->mutant < 0 || spec->mutant > 4) return -1; 498 OmegaGdnShape G; 499 if (omega_gdn_shape(spec, &G)) return -1; 500 const uint32_t RR = G.r, NC = 2u + G.r; 501 (void)RR; (void)NC; 502 const int MUT = spec->mutant; 503 const float qs = 0.`0883883476f`; /* 1 / sqrt(128) */ 504 const uint32_t ROW = G.conv * 4u, REC_ROW = G.vh * OMEGA_GDN_REC_BYTES; 505 omega_bw_ir_init(prog); 506 Em em; memset(&em, 0, sizeof em); em.p = prog; Em *e = &em; 507 508 int udesc = UV64(e); 509 int c = V(e), cx = V(e), hk = V(e), warp = V(e), lane = V(e), one = V(e), T = V(e), t0 = V(e), n = V(e), t1 = V(e); 510 int pqkv = V64(e), ppar = V64(e), ptail = V64(e), prec = V64(e), pab = V64(e), insc = V(e); 511 int ch[5], tw[5], wq[5], ax[5]; 512 for (unsigned i = 0; i < NC; i++) { ch[i] = V(e); tw[i] = V128(e); wq[i] = V128(e); ax[i] = V64(e); } 513 if (e->err) return -1; 514 ldcu64(e, udesc, 0x358); 515 s2r(e, c, BW_SR_TID_X); s2r(e, cx, BW_SR_CTAID_X); s2r(e, hk, BW_SR_CTAID_Y); 516 ldc64(e, pqkv, OMEGA_GDN_PREP_ARG_QKV); ldc64(e, ppar, OMEGA_GDN_PREP_ARG_PAR); ldc64(e, ptail, OMEGA_GDN_PREP_ARG_TAIL); 517 ldc64(e, prec, OMEGA_GDN_PREP_ARG_REC); ldc64(e, pab, OMEGA_GDN_PREP_ARG_AB); 518 ldc32(e, T, OMEGA_GDN_PREP_ARG_T); ldc32(e, insc, OMEGA_GDN_PREP_ARG_IN_SCALE); 519 movi(e, one, 1); 520 shr(e, warp, c, 5); 521 andi(e, R(lane), R(c), 31u); 522 imadi(e, R(t0), R(cx), OMEGA_GDN_PREP_TOKENS, NOREG); 523 /* n = min(16, T - t0) >= 1 */ 524 imadi(e, R(n), R(t0), 0xffffffffu, T); 525 movi(e, t1, OMEGA_GDN_PREP_TOKENS); 526 isetp_ge_u32(e, t1, n); 527 { int b = bra_if_p0_fwd(e); movi(e, n, OMEGA_GDN_PREP_TOKENS); patch_bra(e, b, here(e)); } 528 /* channels: q hk*128 + c, k 2048 + hk*128 + c, v_i 4096 + (3 hk + i)*128 + c */ 529 imadi(e, R(ch[0]), R(hk), OMEGA_GDN_HEAD_DIM, c); 530 imadi(e, R(ch[1]), R(one), 2048u, ch[0]); 531 imadi(e, R(ch[2]), R(hk), RR * OMEGA_GDN_HEAD_DIM, c); imadi(e, R(ch[2]), R(one), 4096u, ch[2]); 532 for (unsigned i = 3; i < NC; i++) imadi(e, R(ch[i]), R(one), (i - 2u) * OMEGA_GDN_HEAD_DIM, ch[2]); 533 /* taps */ 534 { 535 int aq = V64(e), av = V64(e); 536 if (e->err) return -1; 537 imadw(e, aq, ch[0], 16u, ppar); imadw(e, av, ch[2], 16u, ppar); 538 ldg_wide(e, BW_IR_LDG_E_128, tw[0], aq, 0, udesc, 1); 539 ldg_wide(e, BW_IR_LDG_E_128, tw[1], aq, 2048u * 16u, udesc, 0); 540 for (unsigned i = 0; i < RR; i++) ldg_wide(e, BW_IR_LDG_E_128, tw[2 + i], av, i * OMEGA_GDN_HEAD_DIM * 16u, udesc, 0); 541 } 542 /* the window {x_{t0-3}, x_{t0-2}, x_{t0-1}}: CTA 0 from the tail, the others from the rows before t0 (uniform) */ 543 isetp_ge_u32(e, cx, one); 544 const int b_rows = bra_if_p0_fwd(e); 545 { 546 int at = V64(e); 547 if (e->err) return -1; 548 imadi(e, R(t1), R(hk), RR * OMEGA_GDN_TAIL_HEAD_BYTES, NOREG); 549 imadw(e, at, t1, 1u, ptail); imadw(e, at, c, 16u, at); 550 ldg_wide(e, BW_IR_LDG_E_128, wq[0], at, 0, udesc, 1); 551 ldg_wide(e, BW_IR_LDG_E_128, wq[1], at, OMEGA_GDN_HEAD_DIM * 16u, udesc, 0); 552 for (unsigned i = 0; i < RR; i++) ldg_wide(e, BW_IR_LDG_E_128, wq[2 + i], at, i * OMEGA_GDN_TAIL_HEAD_BYTES + 256u * 16u, udesc, 0); 553 } 554 const int b_join = bra_fwd(e); 555 patch_bra(e, b_rows, here(e)); 556 { 557 int base = V64(e), a[5][3]; 558 for (unsigned i = 0; i < NC; i++) for (unsigned r = 0; r < 3; r++) a[i][r] = V64(e); 559 if (e->err) return -1; 560 imadi(e, R(t1), R(one), 0xfffffffdu, t0); /* t0 - 3 (t0 >= 16 here) */ 561 imadw(e, base, t1, ROW, pqkv); 562 for (unsigned i = 0; i < NC; i++) { 563 imadw(e, a[i][0], ch[i], 4u, base); 564 imadw(e, a[i][1], one, ROW, a[i][0]); imadw(e, a[i][2], one, ROW, a[i][1]); 565 } 566 for (unsigned i = 0; i < NC; i++) for (unsigned r = 0; r < 3; r++) { 567 BlackwellIRInsn in = I0(BW_IR_LDG_E); in.dst_vreg = wq[i]; in.dst_subreg = (uint8_t)r; in.src1_vreg = a[i][r]; in.ureg = udesc; 568 emit(e, in, (i == 0 && r == 0) ? K_VAR : K_VAR_MORE); 569 } 570 for (unsigned i = 0; i < NC; i++) for (unsigned r = 0; r < 3; r++) fmul(e, S(wq[i], r), S(wq[i], r), R(insc)); 571 } 572 patch_bra(e, b_join, here(e)); 573 574 /* per-thread addresses at token t0 */ 575 int idx = V(e), arec = V64(e), asc = V64(e), aab = V64(e), scq = V64(e); 576 int cnl2e = V(e), fone = V(e), el2 = V(e), cqs = V(e), c3 = V(e), zero = V(e), raq = V(e), rak = V(e); 577 if (e->err) return -1; 578 { 579 int base = V64(e); 580 if (e->err) return -1; 581 imadw(e, base, t0, ROW, pqkv); 582 for (unsigned i = 0; i < NC; i++) imadw(e, ax[i], ch[i], 4u, base); 583 } 584 imadi(e, R(idx), R(t0), G.vh, NOREG); imadi(e, R(idx), R(hk), RR, idx); 585 imadw(e, arec, idx, OMEGA_GDN_REC_BYTES, prec); imadw(e, arec, c, 4u, arec); 586 imadi(e, R(t1), R(c), 1u, idx); /* the scalar threads' head index t0*48 + 3hk + c */ 587 imadw(e, asc, t1, OMEGA_GDN_REC_BYTES, prec); 588 imadw(e, aab, t1, 8u, pab); 589 movi(e, c3, RR); 590 isetp_ge_u32(e, c, c3); 591 { 592 Region r = region_skip_if_p0(e); 593 int as = V64(e); 594 if (e->err) return -1; 595 imadi(e, R(t1), R(hk), RR, c); 596 imadw(e, as, t1, 8u, ppar); 597 ldg_wide(e, BW_IR_LDG_E_64, scq, as, G.par_scal, udesc, 1); 598 region_end(e, r); 599 } 600 movi(e, cnl2e, fbits(-1.`44269504f`)); movi(e, fone, fbits(1.0f)); movi(e, cqs, fbits(qs)); 601 ldc32(e, el2, OMEGA_GDN_PREP_ARG_EPS_L2); 602 movi(e, zero, 0u); 603 imadi(e, R(raq), R(warp), 4u, NOREG); imadi(e, R(rak), R(one), 16u, raq); 604 605 const int L = here(e); 606 loop_begin(e); 607 { 608 int x[5], acc[5], sq = V(e), sk = V(e), ts = V(e), rq = V128(e), rk = V128(e), nq = V(e), nk = V(e); 609 for (unsigned i = 0; i < NC; i++) { x[i] = V(e); acc[i] = V(e); } 610 if (e->err) return -1; 611 for (unsigned i = 0; i < NC; i++) ldg32(e, x[i], ax[i], udesc, i == 0); 612 for (unsigned i = 0; i < NC; i++) { 613 fmul(e, R(x[i]), R(x[i]), R(insc)); 614 fmul(e, R(acc[i]), S(tw[i], 0), S(wq[i], 0)); ffma(e, R(acc[i]), S(tw[i], 1), S(wq[i], 1), R(acc[i])); 615 ffma(e, R(acc[i]), S(tw[i], 2), S(wq[i], 2), R(acc[i])); ffma(e, R(acc[i]), S(tw[i], 3), R(x[i]), R(acc[i])); 616 silu(e, R(acc[i]), t1, cnl2e, fone); 617 mov(e, S(wq[i], 0), S(wq[i], 1)); mov(e, S(wq[i], 1), S(wq[i], 2)); mov(e, S(wq[i], 2), R(x[i])); 618 } 619 /* l2 norms of q and k across the 4 warps (the decode order: warp sums, then partials 0 + 1 + 2 + 3) */ 620 fmul(e, R(sq), R(acc[0]), R(acc[0])); warp_sum(e, sq, ts); lane0_sts(e, lane, one, raq, R(sq)); 621 fmul(e, R(sk), R(acc[1]), R(acc[1])); warp_sum(e, sk, ts); lane0_sts(e, lane, one, rak, R(sk)); 622 bar_sync(e); 623 lds128(e, rq, zero, 0, 1); lds128(e, rk, zero, 16, 0); 624 fadd(e, R(nq), S(rq, 0), S(rq, 1)); fadd(e, R(nq), R(nq), S(rq, 2)); fadd(e, R(nq), R(nq), S(rq, 3)); fadd(e, R(nq), R(nq), R(el2)); 625 fadd(e, R(nk), S(rk, 0), S(rk, 1)); fadd(e, R(nk), R(nk), S(rk, 2)); fadd(e, R(nk), R(nk), S(rk, 3)); fadd(e, R(nk), R(nk), R(el2)); 626 mufu(e, BW_IR_MUFU_RSQ, R(nq), R(nq)); mufu(e, BW_IR_MUFU_RSQ, R(nk), R(nk)); 627 fmul(e, R(acc[0]), R(acc[0]), R(nq)); fmul(e, R(acc[0]), R(acc[0]), R(cqs)); 628 fmul(e, R(acc[1]), R(acc[1]), R(nk)); 629 for (unsigned i = 0; i < RR; i++) { 630 stg(e, arec, i * OMEGA_GDN_REC_BYTES, R(acc[0]), udesc); 631 stg(e, arec, i * OMEGA_GDN_REC_BYTES + OMEGA_GDN_REC_K, R(acc[1]), udesc); 632 stg(e, arec, i * OMEGA_GDN_REC_BYTES + OMEGA_GDN_REC_V, R(acc[2 + i]), udesc); 633 } 634 /* threads 0..2: decay and beta of value head 3 hk + c */ 635 isetp_ge_u32(e, c, c3); 636 { 637 Region r = region_skip_if_p0(e); 638 int abq = V64(e), dec = V(e), beta = V(e); 639 if (e->err) return -1; 640 ldg_wide(e, BW_IR_LDG_E_64, abq, aab, 0, udesc, 1); 641 gdn_scalars(e, MUT, abq, scq, dec, beta, t1, cnl2e, fone); 642 stg(e, asc, OMEGA_GDN_REC_SC, R(dec), udesc); stg(e, asc, OMEGA_GDN_REC_SC + 4u, R(beta), udesc); 643 region_end(e, r); 644 } 645 bar_sync(e); /* every warp has read the partials before the next token writes them */ 646 for (unsigned i = 0; i < NC; i++) imadw(e, ax[i], one, ROW, ax[i]); 647 imadw(e, arec, one, REC_ROW, arec); imadw(e, asc, one, REC_ROW, asc); 648 imadw(e, aab, one, G.vh * 8u, aab); 649 imadi(e, R(n), R(one), 0xffffffffu, n); 650 isetp_ge_u32(e, n, one); 651 bra_if_p0(e, L); 652 } 653 loop_end(e); 654 tail(e); 655 if (e->err) return -1; 656 return omega_bw_regalloc_solve(prog); 657 } 658 659 int omega_blackwell_codegen_gdn_scan_prog(const OmegaGdnSpec *spec, BlackwellIRProgram *prog) { 660 if (!spec || !prog || spec->copy || spec->mutant < 0 || spec->mutant > 4) return -1; 661 OmegaGdnShape G; 662 if (omega_gdn_shape(spec, &G)) return -1; 663 const uint32_t RR = G.r, NC = 2u + G.r; 664 (void)RR; (void)NC; 665 const int MUT = spec->mutant; 666 const uint32_t ROW = G.conv * 4u, CHUNK_ROW = OMEGA_GDN_SCAN_CHUNK * G.vh * OMEGA_GDN_REC_BYTES; 667 omega_bw_ir_init(prog); 668 Em em; memset(&em, 0, sizeof em); em.p = prog; Em *e = &em; 669 670 int udesc = UV64(e); 671 int tid = V(e), cx = V(e), cy = V(e), h = V(e), warp = V(e), lane = V(e), one = V(e), cq = V(e), rg = V(e), c0 = V(e); 672 int T = V(e), tm1 = V(e), t = V(e), t1 = V(e), sl = V(e), srec = V(e); 673 int ps = V64(e), sa = V64(e), prec = V64(e), grec = V64(e), gsc = V64(e), po = V64(e), sq[16]; 674 for (unsigned r = 0; r < 16; r++) sq[r] = V128(e); 675 if (e->err) return -1; 676 ldcu64(e, udesc, 0x358); 677 s2r(e, tid, BW_SR_TID_X); s2r(e, cx, BW_SR_CTAID_X); s2r(e, cy, BW_SR_CTAID_Y); 678 ldc64(e, ps, OMEGA_GDN_SCAN_ARG_STATE); ldc64(e, prec, OMEGA_GDN_SCAN_ARG_REC); ldc64(e, po, OMEGA_GDN_SCAN_ARG_O); 679 ldc32(e, T, OMEGA_GDN_SCAN_ARG_T); 680 movi(e, one, 1); 681 imadi(e, R(h), R(cy), RR, cx); 682 shr(e, warp, tid, 5); 683 andi(e, R(lane), R(tid), 31u); 684 andi(e, R(cq), R(lane), 3u); 685 shr(e, rg, lane, 2); 686 /* this thread's block of S: rows 16 rg .. +15, columns c0 = 16 warp + 4 cq .. +3 */ 687 imadi(e, R(c0), R(warp), 16u, NOREG); imadi(e, R(c0), R(cq), 4u, c0); 688 const unsigned sel = spec->state_bf16 ? 2u : 4u; /* f32 or bf16 (L313) state element */ 689 imadi(e, R(t1), R(h), OMEGA_GDN_HEAD_DIM * OMEGA_GDN_HEAD_DIM * sel, NOREG); 690 imadi(e, R(t1), R(rg), 16u * OMEGA_GDN_HEAD_DIM * sel, t1); 691 imadi(e, R(t1), R(c0), sel, t1); 692 imadw(e, sa, t1, 1u, ps); 693 if (spec->state_bf16) { 694 int sw[16]; 695 for (unsigned r = 0; r < 16; r++) sw[r] = V64(e); 696 if (e->err) return -1; 697 for (unsigned r = 0; r < 16; r++) ldg_wide(e, BW_IR_LDG_E_64, sw[r], sa, r * OMEGA_GDN_HEAD_DIM * 2u, udesc, r == 0); 698 for (unsigned r = 0; r < 16; r++) bf16x4_widen(e, sq[r], sw[r]); 699 } else { 700 for (unsigned r = 0; r < 16; r++) ldg_wide(e, BW_IR_LDG_E_128, sq[r], sa, r * OMEGA_GDN_HEAD_DIM * 4u, udesc, r == 0); 701 } 702 imadi(e, R(tm1), R(one), 0xffffffffu, T); 703 /* the record copy: warp w brings token tc + w into shared slot w */ 704 imadi(e, R(t1), R(warp), G.vh, h); 705 imadw(e, gsc, t1, OMEGA_GDN_REC_BYTES, prec); 706 imadw(e, grec, lane, 16u, gsc); 707 imadi(e, R(srec), R(warp), OMEGA_GDN_REC_BYTES, NOREG); 708 imadi(e, R(sl), R(lane), 16u, srec); 709 /* o row of (token 0, head h), columns c0 .. c0 + 3 */ 710 imadi(e, R(t1), R(h), OMEGA_GDN_HEAD_DIM, c0); 711 imadw(e, po, t1, 4u, po); 712 movrz(e, R(t)); 713 714 const int L = here(e); 715 loop_begin(e); 716 { 717 int tm = V(e), sb = V(e), ak = V(e), av = V(e), scq = V128(e), vq = V128(e), ts = V(e), kv[4], dl[4], p[4]; 718 for (unsigned j = 0; j < 4; j++) { kv[j] = V(e); dl[j] = V(e); p[j] = V(e); } 719 if (e->err) return -1; 720 andi(e, R(tm), R(t), OMEGA_GDN_SCAN_CHUNK - 1u); 721 isetp_ge_u32(e, tm, one); 722 const int b_copy = bra_if_p0_fwd(e); 723 { 724 int rr[4], a = V(e); 725 for (unsigned k = 0; k < 4; k++) rr[k] = V128(e); 726 if (e->err) return -1; 727 bar_sync(e); /* every warp is done with the last chunk */ 728 for (unsigned k = 0; k < 3; k++) ldg_wide(e, BW_IR_LDG_E_128, rr[k], grec, k * 512u, udesc, k == 0); 729 ldg_wide(e, BW_IR_LDG_E_128, rr[3], gsc, OMEGA_GDN_REC_SC, udesc, 0); 730 for (unsigned k = 0; k < 3; k++) for (unsigned j = 0; j < 4; j++) { 731 if (k == 0 && j == 0) sts32(e, sl, S(rr[0], 0)); 732 else { imadi(e, R(a), R(one), k * 512u + j * 4u, sl); sts32(e, a, S(rr[k], j)); } 733 } 734 for (unsigned j = 0; j < 4; j++) { imadi(e, R(a), R(one), OMEGA_GDN_REC_SC + j * 4u, srec); sts32(e, a, S(rr[3], j)); } 735 bar_sync(e); 736 imadw(e, grec, one, CHUNK_ROW, grec); imadw(e, gsc, one, CHUNK_ROW, gsc); 737 } 738 patch_bra(e, b_copy, here(e)); 739 imadi(e, R(sb), R(tm), OMEGA_GDN_REC_BYTES, NOREG); 740 imadi(e, R(ak), R(rg), 16u * 4u, sb); 741 imadi(e, R(av), R(c0), 4u, sb); 742 lds128(e, scq, sb, OMEGA_GDN_REC_SC, 1); 743 for (unsigned r = 0; r < 16; r++) for (unsigned j = 0; j < 4; j++) fmul(e, S(sq[r], j), S(sq[r], j), S(scq, 0)); 744 /* kv_j = sum over 128 rows of S[r][j] k[r]: 16 rows here, then lanes ^ 4, 8, 16 (the 8 row groups) */ 745 for (unsigned i = 0; i < 4; i++) { 746 int kq = V128(e); 747 if (e->err) return -1; 748 lds128(e, kq, ak, OMEGA_GDN_REC_K + i * 16u, 1); 749 for (unsigned r = 4 * i; r < 4 * i + 4; r++) for (unsigned j = 0; j < 4; j++) { 750 if (r == 0) fmul(e, R(kv[j]), S(sq[0], j), S(kq, 0)); 751 else ffma(e, R(kv[j]), S(sq[r], j), S(kq, r % 4), R(kv[j])); 752 } 753 } 754 for (unsigned j = 0; j < 4; j++) bfly_sum(e, kv[j], ts, 4u, 16u); 755 lds128(e, vq, av, OMEGA_GDN_REC_V, 1); 756 for (unsigned j = 0; j < 4; j++) { fsub(e, R(dl[j]), S(vq, j), R(kv[j])); fmul(e, R(dl[j]), R(dl[j]), S(scq, 1)); } 757 for (unsigned i = 0; i < 4; i++) { 758 int kq = V128(e); 759 if (e->err) return -1; 760 lds128(e, kq, ak, OMEGA_GDN_REC_K + i * 16u, 1); 761 for (unsigned r = 4 * i; r < 4 * i + 4; r++) for (unsigned j = 0; j < 4; j++) ffma(e, S(sq[r], j), S(kq, r % 4), R(dl[j]), S(sq[r], j)); 762 } 763 for (unsigned i = 0; i < 4; i++) { 764 int qq = V128(e); 765 if (e->err) return -1; 766 lds128(e, qq, ak, i * 16u, 1); 767 for (unsigned r = 4 * i; r < 4 * i + 4; r++) for (unsigned j = 0; j < 4; j++) { 768 if (r == 0) fmul(e, R(p[j]), S(sq[0], j), S(qq, 0)); 769 else ffma(e, R(p[j]), S(sq[r], j), S(qq, r % 4), R(p[j])); 770 } 771 } 772 for (unsigned j = 0; j < 4; j++) bfly_sum(e, p[j], ts, 4u, 16u); 773 isetp_ge_u32(e, rg, one); 774 { 775 Region r = region_skip_if_p0(e); 776 for (unsigned j = 0; j < 4; j++) stg(e, po, j * 4u, R(p[j]), udesc); 777 region_end(e, r); 778 } 779 imadw(e, po, one, G.vdim * 4u, po); 780 imadi(e, R(t), R(one), 1u, t); 781 isetp_ge_u32(e, tm1, t); 782 bra_if_p0(e, L); 783 } 784 loop_end(e); 785 if (spec->state_bf16) { 786 int pk[16]; 787 for (unsigned r = 0; r < 16; r++) pk[r] = V64(e); 788 if (e->err) return -1; 789 for (unsigned r = 0; r < 16; r++) { 790 bf16x4_pack(e, pk[r], sq[r]); 791 for (unsigned w = 0; w < 2; w++) stg(e, sa, r * OMEGA_GDN_HEAD_DIM * 2u + w * 4u, S(pk[r], w), udesc); 792 } 793 } else { 794 for (unsigned r = 0; r < 16; r++) for (unsigned j = 0; j < 4; j++) stg(e, sa, r * OMEGA_GDN_HEAD_DIM * 4u + j * 4u, S(sq[r], j), udesc); 795 } 796 797 /* the new tail: qkv rows T-3 .. T-1 (times in_scale) of slot A (tid) and slot V (256 + (tid & 127)), as decode */ 798 if (MUT != 4) { 799 int half = V(e), cc = V(e), gA = V(e), gV = V(e), insc = V(e), pqkv = V64(e), ptail = V64(e), base = V64(e); 800 int atA = V64(e), atV = V64(e), aA[3], aV[3], xA[3], xV[3]; 801 for (unsigned r = 0; r < 3; r++) { aA[r] = V64(e); aV[r] = V64(e); xA[r] = V(e); xV[r] = V(e); } 802 if (e->err) return -1; 803 ldc64(e, pqkv, OMEGA_GDN_SCAN_ARG_QKV); ldc64(e, ptail, OMEGA_GDN_SCAN_ARG_TAIL); ldc32(e, insc, OMEGA_GDN_SCAN_ARG_IN_SCALE); 804 shr(e, half, tid, 7); 805 andi(e, R(cc), R(tid), 127u); 806 imadi(e, R(gA), R(cy), OMEGA_GDN_HEAD_DIM, tid); imadi(e, R(gA), R(half), 2048u - 128u, gA); 807 imadi(e, R(gV), R(h), OMEGA_GDN_HEAD_DIM, cc); imadi(e, R(gV), R(one), 2u * OMEGA_GDN_KEY_HEADS * OMEGA_GDN_HEAD_DIM, gV); 808 imadi(e, R(t1), R(one), 0xfffffffdu, T); 809 imadw(e, base, t1, ROW, pqkv); 810 imadw(e, aA[0], gA, 4u, base); imadw(e, aV[0], gV, 4u, base); 811 for (unsigned r = 1; r < 3; r++) { imadw(e, aA[r], one, ROW, aA[r - 1]); imadw(e, aV[r], one, ROW, aV[r - 1]); } 812 for (unsigned r = 0; r < 3; r++) { ldg32(e, xA[r], aA[r], udesc, r == 0); ldg32(e, xV[r], aV[r], udesc, 0); } 813 for (unsigned r = 0; r < 3; r++) { fmul(e, R(xA[r]), R(xA[r]), R(insc)); fmul(e, R(xV[r]), R(xV[r]), R(insc)); } 814 imadw(e, atA, tid, 16u, ptail); imadw(e, atA, h, OMEGA_GDN_TAIL_HEAD_BYTES, atA); 815 imadi(e, R(t1), R(one), 256u, cc); 816 imadw(e, atV, t1, 16u, ptail); imadw(e, atV, h, OMEGA_GDN_TAIL_HEAD_BYTES, atV); 817 for (unsigned r = 0; r < 3; r++) { stg(e, atA, 4u * r, R(xA[r]), udesc); stg(e, atV, 4u * r, R(xV[r]), udesc); } 818 } 819 tail(e); 820 if (e->err) return -1; 821 return omega_bw_regalloc_solve(prog); 822 } 823 824 int omega_blackwell_codegen_gdn_post_prog(const OmegaGdnSpec *spec, BlackwellIRProgram *prog) { 825 if (!spec || !prog || spec->copy || spec->mutant < 0 || spec->mutant > 4) return -1; 826 OmegaGdnShape G; 827 if (omega_gdn_shape(spec, &G)) return -1; 828 const uint32_t RR = G.r, NC = 2u + G.r; 829 (void)RR; (void)NC; 830 const int MUT = spec->mutant; 831 omega_bw_ir_init(prog); 832 Em em; memset(&em, 0, sizeof em); em.p = prog; Em *e = &em; 833 834 int udesc = UV64(e); 835 int tid = V(e), t = V(e), cy = V(e), warp = V(e), lane = V(e), idx = V(e), ss = V(e), ts = V(e), g = V(e), t1 = V(e); 836 int en = V(e), osc = V(e), insc = V(e), cinv = V(e), cnl2e = V(e), fone = V(e); 837 int po = V64(e), pz = V64(e), ppar = V64(e), pout = V64(e), ao = V64(e), az = V64(e), an = V64(e), ay = V64(e); 838 int oq = V128(e), zq = V128(e), nq = V128(e); 839 if (e->err) return -1; 840 ldcu64(e, udesc, 0x358); 841 s2r(e, tid, BW_SR_TID_X); s2r(e, t, BW_SR_CTAID_X); s2r(e, cy, BW_SR_CTAID_Y); 842 ldc64(e, po, OMEGA_GDN_POST_ARG_O); ldc64(e, pz, OMEGA_GDN_POST_ARG_Z); ldc64(e, ppar, OMEGA_GDN_POST_ARG_PAR); 843 ldc64(e, pout, OMEGA_GDN_POST_ARG_OUT); 844 ldc32(e, en, OMEGA_GDN_POST_ARG_EPS_NORM); ldc32(e, insc, OMEGA_GDN_POST_ARG_IN_SCALE); ldc32(e, osc, OMEGA_GDN_POST_ARG_OUT_SCALE); 845 shr(e, warp, tid, 5); 846 andi(e, R(lane), R(tid), 31u); 847 /* row (token t, head 8 cy + warp): o, z and out share the [T][48][128] layout */ 848 imadi(e, R(idx), R(cy), 8u, warp); 849 imadi(e, R(idx), R(t), G.vh, idx); 850 imadw(e, ao, idx, OMEGA_GDN_HEAD_DIM * 4u, po); imadw(e, ao, lane, 16u, ao); 851 imadw(e, az, idx, OMEGA_GDN_HEAD_DIM * 4u, pz); imadw(e, az, lane, 16u, az); 852 imadw(e, ay, idx, OMEGA_GDN_HEAD_DIM * 4u, pout); imadw(e, ay, lane, 16u, ay); 853 imadw(e, an, lane, 16u, ppar); 854 ldg_wide(e, BW_IR_LDG_E_128, oq, ao, 0, udesc, 1); 855 ldg_wide(e, BW_IR_LDG_E_128, zq, az, 0, udesc, 0); 856 ldg_wide(e, BW_IR_LDG_E_128, nq, an, G.par_norm, udesc, 0); 857 fmul(e, R(ss), S(oq, 0), S(oq, 0)); 858 for (unsigned j = 1; j < 4; j++) ffma(e, R(ss), S(oq, j), S(oq, j), R(ss)); 859 bfly_sum(e, ss, ts, 1u, 16u); 860 movi(e, cinv, fbits(1.0f / 128.0f)); 861 ffma(e, R(ss), R(ss), R(cinv), R(en)); 862 mufu(e, BW_IR_MUFU_RSQ, R(ss), R(ss)); 863 movi(e, cnl2e, fbits(-1.`44269504f`)); movi(e, fone, fbits(1.0f)); 864 for (unsigned j = 0; j < 4; j++) { 865 fmul(e, R(g), S(zq, j), R(insc)); 866 if (MUT != 3) silu(e, R(g), t1, cnl2e, fone); 867 fmul(e, S(oq, j), S(oq, j), R(ss)); fmul(e, S(oq, j), S(oq, j), S(nq, j)); 868 fmul(e, S(oq, j), S(oq, j), R(g)); fmul(e, S(oq, j), S(oq, j), R(osc)); 869 } 870 for (unsigned j = 0; j < 4; j++) stg(e, ay, j * 4u, S(oq, j), udesc); 871 tail(e); 872 if (e->err) return -1; 873 return omega_bw_regalloc_solve(prog); 874 } 875 876 /* ======== multi-token decode (L369, see the header): T decode steps of one head in one CTA, state and tails on chip ======== 877 * Every token runs the decode program's phases A..E with the same instructions in the same order on the same values; the 878 * changes are where values live: S stays in the 64 registers between tokens (one load before the loop, one store after 879 * it), the conv tails are shifted in registers (one load, one store), and the loop-invariant loads (conv taps, negA and 880 * dt_bias, the norm weight) are issued once. */ 881 int omega_blackwell_codegen_gdn_multi_prog(const OmegaGdnSpec *spec, BlackwellIRProgram *prog) { 882 if (!spec || !prog || spec->copy || spec->mutant < 0 || spec->mutant > 4) return -1; 883 OmegaGdnShape G; 884 if (omega_gdn_shape(spec, &G)) return -1; 885 const uint32_t RR = G.r; 886 const int MUT = spec->mutant; 887 const uint32_t C_LOG2E = fbits(1.`44269504f`), C_NLOG2E = fbits(-1.`44269504f`), C_LN2 = fbits(0.`693147181f`); 888 const uint32_t C_ONE = fbits(1.0f), C_M1 = fbits(-1.0f), C_INV128 = fbits(1.0f / 128.0f); 889 const float qs = 0.`0883883476f`; /* 1 / sqrt(128) */ 890 omega_bw_ir_init(prog); 891 if (omega_bw_ir_set_gpr_limit(prog, BW_PHYS_GPR_HW_LIMIT) != 0) return -1; 892 Em em; memset(&em, 0, sizeof em); em.p = prog; Em *e = &em; 893 894 /* ---- identities; the state address; the loop bounds and strides ---- */ 895 int udesc = UV64(e); 896 int tid = V(e), cx = V(e), cy = V(e), h = V(e), warp = V(e), lane = V(e), one = V(e); 897 int ps = V64(e), sa = V64(e), toff = V(e), sq[16]; 898 int T = V(e), tm1 = V(e), t = V(e), xs = V(e), abs_ = V(e), os = V(e); 899 for (unsigned r = 0; r < 16; r++) sq[r] = V128(e); 900 if (e->err) return -1; 901 ldcu64(e, udesc, 0x358); 902 ldc64(e, ps, OMEGA_GDN_ARG_STATE); 903 s2r(e, tid, BW_SR_TID_X); s2r(e, cx, BW_SR_CTAID_X); s2r(e, cy, BW_SR_CTAID_Y); 904 movi(e, one, 1); 905 imadi(e, R(h), R(cy), RR, cx); 906 shr(e, warp, tid, 5); 907 andi(e, R(lane), R(tid), 31u); 908 imadi(e, R(toff), R(h), OMEGA_GDN_STATE_HEAD_BYTES, NOREG); 909 imadi(e, R(toff), R(warp), 16u * OMEGA_GDN_HEAD_DIM * 4u, toff); 910 imadi(e, R(toff), R(lane), 16u, toff); 911 imadw(e, sa, toff, 1u, ps); 912 ldc32(e, T, OMEGA_GDN_MULTI_ARG_T); ldc32(e, xs, OMEGA_GDN_MULTI_ARG_XSTRIDE); 913 ldc32(e, abs_, OMEGA_GDN_MULTI_ARG_ABSTRIDE); ldc32(e, os, OMEGA_GDN_MULTI_ARG_OSTRIDE); 914 imadi(e, R(tm1), R(one), 0xffffffffu, T); 915 916 /* ---- the loop-invariant loads: taps, tails, scalars, norm weight; the state ---- */ 917 int half = V(e), c = V(e), gi = V(e), slv = V(e); 918 int pqkv = V64(e), ppar = V64(e), ptail = V64(e); 919 int axA = V64(e), awA = V64(e), atA = V64(e), axV = V64(e), awV = V64(e), atV = V64(e); 920 int wA = V128(e), wV = V128(e), tA = V128(e), tV = V128(e); 921 int insc = V(e), t1 = V(e), cnl2e = V(e), fone = V(e), cl2e = V(e); 922 int pab = V64(e), aab = V64(e), asc = V64(e), scq = V64(e); 923 int pz = V64(e), az = V64(e), an = V64(e), nq = V128(e), pout = V64(e), ao = V64(e); 924 int el2 = V(e), en = V(e), osc = V(e); 925 if (e->err) return -1; 926 ldc64(e, pqkv, OMEGA_GDN_ARG_QKV); ldc64(e, ppar, OMEGA_GDN_ARG_PAR); ldc64(e, ptail, OMEGA_GDN_ARG_TAIL); 927 ldc32(e, insc, OMEGA_GDN_ARG_IN_SCALE); 928 shr(e, half, tid, 7); 929 andi(e, R(c), R(tid), 127u); 930 imadi(e, R(gi), R(cy), OMEGA_GDN_HEAD_DIM, tid); /* q channel hk*128 + t; k channel 2048 + hk*128 + (t - 128) */ 931 imadi(e, R(gi), R(half), 2048u - 128u, gi); 932 imadw(e, axA, gi, 4u, pqkv); imadw(e, awA, gi, 16u, ppar); 933 imadi(e, R(gi), R(h), OMEGA_GDN_HEAD_DIM, c); /* v channel 4096 + h*128 + c */ 934 imadi(e, R(gi), R(one), 2u * OMEGA_GDN_KEY_HEADS * OMEGA_GDN_HEAD_DIM, gi); 935 imadw(e, axV, gi, 4u, pqkv); imadw(e, awV, gi, 16u, ppar); 936 imadw(e, atA, tid, 16u, ptail); imadw(e, atA, h, OMEGA_GDN_TAIL_HEAD_BYTES, atA); 937 imadi(e, R(slv), R(one), 256u, c); 938 imadw(e, atV, slv, 16u, ptail); imadw(e, atV, h, OMEGA_GDN_TAIL_HEAD_BYTES, atV); 939 ldc64(e, pab, OMEGA_GDN_ARG_AB); 940 imadw(e, aab, h, 8u, pab); imadw(e, asc, h, 8u, ppar); 941 ldc64(e, pz, OMEGA_GDN_ARG_Z); 942 imadw(e, az, lane, 16u, pz); imadw(e, az, h, OMEGA_GDN_HEAD_DIM * 4u, az); 943 imadw(e, an, lane, 16u, ppar); 944 ldc64(e, pout, OMEGA_GDN_ARG_OUT); 945 imadw(e, ao, lane, 16u, pout); imadw(e, ao, h, OMEGA_GDN_HEAD_DIM * 4u, ao); 946 ldc32(e, el2, OMEGA_GDN_ARG_EPS_L2); ldc32(e, en, OMEGA_GDN_ARG_EPS_NORM); ldc32(e, osc, OMEGA_GDN_ARG_OUT_SCALE); 947 ldg_wide(e, BW_IR_LDG_E_128, wA, awA, 0, udesc, 1); 948 ldg_wide(e, BW_IR_LDG_E_128, tA, atA, 0, udesc, 0); 949 ldg_wide(e, BW_IR_LDG_E_128, wV, awV, 0, udesc, 0); 950 ldg_wide(e, BW_IR_LDG_E_128, tV, atV, 0, udesc, 0); 951 ldg_wide(e, BW_IR_LDG_E_64, scq, asc, G.par_scal, udesc, 0); 952 ldg_wide(e, BW_IR_LDG_E_128, nq, an, G.par_norm, udesc, 0); 953 for (unsigned r = 0; r < 16; r++) ldg_pf(e, sq[r], sa, r * OMEGA_GDN_HEAD_DIM * 4u, udesc, SB_STATE); 954 movi(e, cnl2e, C_NLOG2E); movi(e, fone, C_ONE); movi(e, cl2e, C_LOG2E); 955 consume(e, SB_STATE); 956 movrz(e, R(t)); /* waits for every load above: the loop is entered with nothing pending */ 957 958 const int L = here(e); 959 loop_begin(e); 960 { 961 /* ---- phase A: conv + SiLU of channel A (q or k) and channel V, token t ---- */ 962 int xA = V(e), xV = V(e), accA = V(e), accV = V(e); 963 if (e->err) return -1; 964 ldg32(e, xA, axA, udesc, 1); 965 ldg32(e, xV, axV, udesc, 0); 966 fmul(e, R(xA), R(xA), R(insc)); fmul(e, R(xV), R(xV), R(insc)); 967 fmul(e, R(accA), S(wA, 0), S(tA, 0)); ffma(e, R(accA), S(wA, 1), S(tA, 1), R(accA)); 968 ffma(e, R(accA), S(wA, 2), S(tA, 2), R(accA)); ffma(e, R(accA), S(wA, 3), R(xA), R(accA)); 969 fmul(e, R(accV), S(wV, 0), S(tV, 0)); ffma(e, R(accV), S(wV, 1), S(tV, 1), R(accV)); 970 ffma(e, R(accV), S(wV, 2), S(tV, 2), R(accV)); ffma(e, R(accV), S(wV, 3), R(xV), R(accV)); 971 silu(e, R(accA), t1, cnl2e, fone); 972 silu(e, R(accV), t1, cnl2e, fone); 973 974 /* ---- phase B: l2 norms; q|k and v to shared; the tails shift in registers ---- */ 975 int sqs = V(e), ts = V(e), ra = V(e); 976 if (e->err) return -1; 977 fmul(e, R(sqs), R(accA), R(accA)); 978 warp_sum(e, sqs, ts); 979 imadi(e, R(ra), R(warp), 4u, NOREG); imadi(e, R(ra), R(one), OMEGA_GDN_SM_RED, ra); 980 lane0_sts(e, lane, one, ra, R(sqs)); 981 imadi(e, R(ra), R(c), 4u, NOREG); imadi(e, R(ra), R(one), OMEGA_GDN_SM_V, ra); 982 sts32(e, ra, R(accV)); 983 bar_sync(e); 984 if (MUT != 4) { 985 mov(e, S(tA, 0), S(tA, 1)); mov(e, S(tA, 1), S(tA, 2)); mov(e, S(tA, 2), R(xA)); 986 mov(e, S(tV, 0), S(tV, 1)); mov(e, S(tV, 1), S(tV, 2)); mov(e, S(tV, 2), R(xV)); 987 } 988 { 989 int rq = V128(e), hf = V(e), fq = V(e); 990 if (e->err) return -1; 991 imadi(e, R(ra), R(half), 16u, NOREG); 992 lds128(e, rq, ra, OMEGA_GDN_SM_RED, 1); 993 fadd(e, R(sqs), S(rq, 0), S(rq, 1)); fadd(e, R(sqs), R(sqs), S(rq, 2)); fadd(e, R(sqs), R(sqs), S(rq, 3)); 994 fadd(e, R(sqs), R(sqs), R(el2)); 995 mufu(e, BW_IR_MUFU_RSQ, R(sqs), R(sqs)); 996 i2fp(e, hf, half); 997 movi(e, fq, fbits(qs)); movi(e, t1, fbits(1.0f - qs)); 998 ffma(e, R(fq), R(hf), R(t1), R(fq)); 999 fmul(e, R(accA), R(accA), R(sqs)); fmul(e, R(accA), R(accA), R(fq)); 1000 imadi(e, R(ra), R(tid), 4u, NOREG); 1001 sts32(e, ra, R(accA)); 1002 bar_sync(e); 1003 } 1004 1005 /* ---- phase C: scalars of token t ---- */ 1006 int abq = V64(e), xsc = V(e), mn = V(e), sp = V(e), dec = V(e), beta = V(e); 1007 if (e->err) return -1; 1008 ldg_wide(e, BW_IR_LDG_E_64, abq, aab, 0, udesc, 1); 1009 fadd(e, R(xsc), S(abq, 0), S(scq, 1)); 1010 movi(e, mn, C_M1); fmul(e, R(mn), R(xsc), R(mn)); fminn(e, R(mn), R(xsc), R(mn)); 1011 fmul(e, R(mn), R(mn), R(cl2e)); mufu(e, BW_IR_MUFU_EX2, R(mn), R(mn)); fadd(e, R(mn), R(mn), R(fone)); 1012 mufu(e, BW_IR_MUFU_LG2, R(mn), R(mn)); 1013 movrz(e, R(sp)); fmaxx(e, R(sp), R(xsc), R(sp)); 1014 movi(e, t1, C_LN2); ffma(e, R(sp), R(mn), R(t1), R(sp)); 1015 if (MUT == 1) movi(e, dec, C_ONE); 1016 else { fmul(e, R(dec), S(scq, 0), R(sp)); fmul(e, R(dec), R(dec), R(cl2e)); mufu(e, BW_IR_MUFU_EX2, R(dec), R(dec)); } 1017 if (MUT == 2) movi(e, beta, C_ONE); 1018 else { 1019 fmul(e, R(beta), S(abq, 1), R(cnl2e)); mufu(e, BW_IR_MUFU_EX2, R(beta), R(beta)); 1020 fadd(e, R(beta), R(beta), R(fone)); mufu(e, BW_IR_MUFU_RCP, R(beta), R(beta)); 1021 } 1022 1023 /* ---- phase D: the state update (S stays in registers) ---- */ 1024 int aw = V(e), pa = V(e), la = V(e), kq[4], kv[4], dl[4], vq = V128(e), zq = V128(e); 1025 for (unsigned i = 0; i < 4; i++) { kq[i] = V128(e); kv[i] = V(e); dl[i] = V(e); } 1026 if (e->err) return -1; 1027 imadi(e, R(aw), R(warp), 16u * 4u, NOREG); 1028 imadi(e, R(pa), R(warp), OMEGA_GDN_HEAD_DIM * 4u, NOREG); imadi(e, R(pa), R(lane), 16u, pa); imadi(e, R(pa), R(one), OMEGA_GDN_SM_P, pa); 1029 imadi(e, R(la), R(lane), 16u, NOREG); 1030 for (unsigned r = 0; r < 16; r++) for (unsigned j = 0; j < 4; j++) fmul(e, S(sq[r], j), S(sq[r], j), R(dec)); 1031 for (unsigned i = 0; i < 4; i++) lds128(e, kq[i], aw, OMEGA_GDN_SM_K + i * 16u, i == 0); 1032 for (unsigned j = 0; j < 4; j++) fmul(e, R(kv[j]), S(sq[0], j), S(kq[0], 0)); 1033 for (unsigned r = 1; r < 16; r++) for (unsigned j = 0; j < 4; j++) ffma(e, R(kv[j]), S(sq[r], j), S(kq[r / 4], r % 4), R(kv[j])); 1034 cross_warp_sum(e, kv, pa, one, la); 1035 lds128(e, vq, la, OMEGA_GDN_SM_V, 1); 1036 for (unsigned j = 0; j < 4; j++) { fsub(e, R(dl[j]), S(vq, j), R(kv[j])); fmul(e, R(dl[j]), R(dl[j]), R(beta)); } 1037 { 1038 int k2[4]; 1039 for (unsigned i = 0; i < 4; i++) k2[i] = V128(e); 1040 if (e->err) return -1; 1041 for (unsigned i = 0; i < 4; i++) lds128(e, k2[i], aw, OMEGA_GDN_SM_K + i * 16u, i == 0); 1042 for (unsigned r = 0; r < 16; r++) for (unsigned j = 0; j < 4; j++) ffma(e, S(sq[r], j), S(k2[r / 4], r % 4), R(dl[j]), S(sq[r], j)); 1043 } 1044 ldg_pf(e, zq, az, 0, udesc, SB_GATE); 1045 int p[4]; 1046 { 1047 int q2[4]; 1048 for (unsigned i = 0; i < 4; i++) { q2[i] = V128(e); p[i] = V(e); } 1049 if (e->err) return -1; 1050 for (unsigned i = 0; i < 4; i++) lds128(e, q2[i], aw, OMEGA_GDN_SM_Q + i * 16u, i == 0); 1051 for (unsigned j = 0; j < 4; j++) fmul(e, R(p[j]), S(sq[0], j), S(q2[0], 0)); 1052 for (unsigned r = 1; r < 16; r++) for (unsigned j = 0; j < 4; j++) ffma(e, R(p[j]), S(sq[r], j), S(q2[r / 4], r % 4), R(p[j])); 1053 } 1054 bar_sync(e); 1055 cross_warp_sum(e, p, pa, one, la); 1056 1057 /* ---- phase E: gated RMSNorm, out row t ---- */ 1058 int ss = V(e), g = V(e); 1059 if (e->err) return -1; 1060 fmul(e, R(ss), R(p[0]), R(p[0])); 1061 for (unsigned j = 1; j < 4; j++) ffma(e, R(ss), R(p[j]), R(p[j]), R(ss)); 1062 warp_sum(e, ss, ts); 1063 imadi(e, R(ra), R(warp), 4u, NOREG); imadi(e, R(ra), R(one), OMEGA_GDN_SM_RED2, ra); 1064 lane0_sts(e, lane, one, ra, R(ss)); 1065 bar_sync(e); 1066 lds32(e, R(ss), ra, 0); 1067 movi(e, t1, C_INV128); 1068 ffma(e, R(ss), R(ss), R(t1), R(en)); 1069 mufu(e, BW_IR_MUFU_RSQ, R(ss), R(ss)); 1070 consume(e, SB_GATE); 1071 for (unsigned j = 0; j < 4; j++) { 1072 fmul(e, R(g), S(zq, j), R(insc)); 1073 if (MUT != 3) silu(e, R(g), t1, cnl2e, fone); 1074 fmul(e, R(p[j]), R(p[j]), R(ss)); fmul(e, R(p[j]), R(p[j]), S(nq, j)); 1075 fmul(e, R(p[j]), R(p[j]), R(g)); fmul(e, R(p[j]), R(p[j]), R(osc)); 1076 } 1077 for (unsigned j = 0; j < 4; j++) stg(e, ao, j * 4u, R(p[j]), udesc); 1078 /* every warp is done with this token's shared memory before the next token writes it */ 1079 bar_sync(e); 1080 imadw(e, axA, xs, 1u, axA); imadw(e, axV, xs, 1u, axV); imadw(e, az, xs, 1u, az); 1081 imadw(e, aab, abs_, 1u, aab); imadw(e, ao, os, 1u, ao); 1082 imadi(e, R(t), R(one), 1u, t); 1083 isetp_ge_u32(e, tm1, t); 1084 bra_if_p0(e, L); 1085 } 1086 loop_end(e); 1087 1088 /* ---- the state and the tails back to memory, once ---- */ 1089 for (unsigned r = 0; r < 16; r++) for (unsigned j = 0; j < 4; j++) stg(e, sa, r * OMEGA_GDN_HEAD_DIM * 4u + j * 4u, S(sq[r], j), udesc); 1090 if (MUT != 4) { 1091 stg(e, atA, 0, S(tA, 0), udesc); stg(e, atA, 4, S(tA, 1), udesc); stg(e, atA, 8, S(tA, 2), udesc); 1092 stg(e, atV, 0, S(tV, 0), udesc); stg(e, atV, 4, S(tV, 1), udesc); stg(e, atV, 8, S(tV, 2), udesc); 1093 } 1094 tail(e); 1095 if (e->err) return -1; 1096 return omega_bw_regalloc_solve(prog); 1097 } 1098 #else 1099 #include "cudapp_native.h" 1100 #include "omega_gpu_om_ir.h" 1101 1102 /* C owns API/null handling and serialization only. The shape policy, every refusal, every allocation request, branch, 1103 * loop event and scheduling decision are seeded Omega, replayed through omega_gpu_om_ir_replay. */ 1104 static uint64_t gdn_policy(const OmegaGdnSpec *spec, uint64_t query) { 1105 uint64_t result = 0; 1106 if (omega_cg_gdn_policy(&result, query, spec->ratio, spec->state_bf16 != 0) != OMEGA_OK) return 0; 1107 return result; 1108 } 1109 int omega_gdn_shape(const OmegaGdnSpec *spec, OmegaGdnShape *s) { 1110 if (!gdn_policy(spec, 0)) return -1; 1111 s->r = (uint32_t)gdn_policy(spec, 1); 1112 s->vh = (uint32_t)gdn_policy(spec, 2); 1113 s->vdim = (uint32_t)gdn_policy(spec, 3); 1114 s->conv = (uint32_t)gdn_policy(spec, 4); 1115 s->par_scal = (uint32_t)gdn_policy(spec, 5); 1116 s->par_norm = (uint32_t)gdn_policy(spec, 6); 1117 s->par_bytes = (uint32_t)gdn_policy(spec, 7); 1118 s->tail_bytes = (uint32_t)gdn_policy(spec, 8); 1119 s->state_bytes = (uint32_t)gdn_policy(spec, 9); 1120 s->post_grid_y = (uint32_t)gdn_policy(spec, 10); 1121 return 0; 1122 } 1123 /* kind: 0 decode, 1 multi, 2 copy, 3 prep, 4 scan, 5 post (cg_gdn in om/codegen/cg_gdn.om). */ 1124 static int gdn_build(uint64_t kind, const OmegaGdnSpec *spec, BlackwellIRProgram *prog) { 1125 if (!spec || !prog) return -1; 1126 return omega_gpu_om_ir_replay(omega_cg_gdn, kind, (uint32_t)spec->mutant, (uint32_t)spec->copy, 1127 (uint64_t)spec->ratio | ((uint64_t)(spec->state_bf16 != 0) << 32), prog, NULL) 1128 == OMEGA_OM_IR_OK ? 0 : -1; 1129 } 1130 int omega_blackwell_codegen_gdn_prog(const OmegaGdnSpec *spec, BlackwellIRProgram *prog) { return gdn_build(0, spec, prog); } 1131 int omega_blackwell_codegen_gdn_multi_prog(const OmegaGdnSpec *spec, BlackwellIRProgram *prog) { return gdn_build(1, spec, prog); } 1132 int omega_blackwell_codegen_gdn_copy_prog(const OmegaGdnSpec *spec, BlackwellIRProgram *prog) { return gdn_build(2, spec, prog); } 1133 int omega_blackwell_codegen_gdn_prep_prog(const OmegaGdnSpec *spec, BlackwellIRProgram *prog) { return gdn_build(3, spec, prog); } 1134 int omega_blackwell_codegen_gdn_scan_prog(const OmegaGdnSpec *spec, BlackwellIRProgram *prog) { return gdn_build(4, spec, prog); } 1135 int omega_blackwell_codegen_gdn_post_prog(const OmegaGdnSpec *spec, BlackwellIRProgram *prog) { return gdn_build(5, spec, prog); } 1136 #endif 1137 1138 int omega_gdn_prog_stats(const OmegaGdnSpec *spec, uint32_t *insns, uint32_t *gprs) { 1139 static BlackwellIRProgram prog; 1140 if (omega_blackwell_codegen_gdn_prog(spec, &prog) != 0) return -1; 1141 if (insns) *insns = (uint32_t)prog.count; 1142 if (gprs) *gprs = prog.regalloc.peak_gpr_usage; 1143 return 0; 1144 } 1145 1146 int omega_gdn_pf_prog_stats(unsigned kind, const OmegaGdnSpec *spec, uint32_t *insns, uint32_t *gprs) { 1147 static BlackwellIRProgram prog; 1148 int rc = kind == 0 ? omega_blackwell_codegen_gdn_prep_prog(spec, &prog) 1149 : kind == 1 ? omega_blackwell_codegen_gdn_scan_prog(spec, &prog) 1150 : kind == 2 ? omega_blackwell_codegen_gdn_post_prog(spec, &prog) : -1; 1151 if (rc != 0) return -1; 1152 if (insns) *insns = (uint32_t)prog.count; 1153 if (gprs) *gprs = prog.regalloc.peak_gpr_usage; 1154 return 0; 1155 } 1156 1157 int omega_gdn_multi_prog_stats(const OmegaGdnSpec *spec, uint32_t *insns, uint32_t *gprs) { 1158 static BlackwellIRProgram prog; 1159 if (omega_blackwell_codegen_gdn_multi_prog(spec, &prog) != 0) return -1; 1160 if (insns) *insns = (uint32_t)prog.count; 1161 if (gprs) *gprs = prog.regalloc.peak_gpr_usage; 1162 return 0; 1163 }