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_dec.c

697 lines · 38927 bytes Raw Blame History
1/* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception */
2/*
3 * omega_blackwell_codegen_dec.c: decode glue kernels (see the header). The emitter helpers are the router's
4 * (omega_blackwell_codegen_router.c): control words, read barrier on every load, loop live ranges, lane-0 regions.
5 */
6#include "omega_blackwell_codegen_dec.h"
7#include <string.h>
8#include "omega_gpu_om_ir.h"
9#include "cudapp_native.h"
10
11#if defined(CUDAPP_MIGRATION_PENDING_C_CODEGEN_DEC)
12#define CW(stall, wbar, rbar, wait) \
13 (((uint32_t)(stall) << 9) | ((uint32_t)(wbar) << 14) | ((uint32_t)(rbar) << 17) | ((uint32_t)(wait) << 20))
14#define CW_YIELD 0x2000u
15#define SB_NONE 7u
16#define SB_GPR 0u
17#define SB_RD 1u
18#define SB_UGPR 2u
19#define NOREG (-1)
20#define MAX_LOOPS 4
21
22enum { K_FIXED, K_PRED, K_VAR, K_VAR_MORE, K_UVAR, K_STORE, K_SYNC, K_EXIT, K_BRA, K_BSSY, K_BSYNC };
23
24typedef struct {
25 BlackwellIRProgram *p;
26 uint32_t pending;
27 int err;
28 int open_start[MAX_LOOPS];
29 int depth;
30} Em;
31
32static void emit(Em *e, BlackwellIRInsn in, int kind) {
33 uint32_t wait = e->pending;
34 switch (kind) {
35 case K_FIXED: in.control = CW(6, SB_NONE, SB_NONE, wait); e->pending = 0; break;
36 case K_PRED: in.control = CW(14, SB_NONE, SB_NONE, wait); e->pending = 0; break;
37 case K_VAR: in.control = CW(4, SB_GPR, SB_RD, wait) | CW_YIELD; e->pending = (1u << SB_GPR) | (1u << SB_RD); break;
38 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;
39 case K_UVAR: in.control = CW(4, SB_UGPR, SB_NONE, wait) | CW_YIELD; e->pending = 1u << SB_UGPR; break;
40 case K_STORE: in.control = CW(4, SB_NONE, SB_RD, wait) | CW_YIELD; e->pending = 1u << SB_RD; break;
41 case K_SYNC: in.control = CW(6, SB_NONE, SB_NONE, wait); e->pending = 0; break;
42 case K_EXIT: in.control = CW(5, SB_NONE, SB_NONE, wait); e->pending = in.predicate_p0 ? wait : 0; break;
43 case K_BRA: in.control = CW(5, SB_NONE, SB_NONE, wait); e->pending = in.predicate_p0 ? wait : 0; break;
44 case K_BSSY: in.control = CW(1, SB_NONE, SB_NONE, wait) | CW_YIELD; e->pending = 0; break;
45 case K_BSYNC: in.control = CW(5, SB_NONE, SB_NONE, wait) | CW_YIELD; e->pending = 0; break;
46 default: e->err = -1; return;
47 }
48 if (omega_bw_ir_append(e->p, &in) < 0) e->err = -1;
49}
50
51static BlackwellIRInsn I0(BlackwellIROpcode op) {
52 BlackwellIRInsn i; memset(&i, 0, sizeof i);
53 i.op = op; i.dst_vreg = NOREG; i.src1_vreg = NOREG; i.src2_vreg = NOREG; i.src3_vreg = NOREG; i.ureg = NOREG;
54 return i;
55}
56static int V(Em *e) { int v = omega_bw_ir_alloc_vreg(e->p); if (v < 0) e->err = -1; return v; }
57static int V64(Em *e) { int v = omega_bw_ir_alloc_vreg64(e->p); if (v < 0) e->err = -1; return v; }
58static int V128(Em *e) { int v = omega_bw_ir_alloc_vreg128(e->p); if (v < 0) e->err = -1; return v; }
59static int UV64(Em *e) { int v = omega_bw_ir_alloc_uvreg64(e->p); if (v < 0) e->err = -1; return v; }
60static int here(const Em *e) { return (int)e->p->count; }
61
62typedef struct { int v; unsigned s; } Op;
63static Op R(int v) { Op o = { v, 0 }; return o; }
64static Op S(int v, unsigned s) { Op o = { v, s }; return o; }
65
66static 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); }
67static 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); }
68static 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); }
69static 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); }
70static 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); }
71static 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); }
72static 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); }
73static 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); }
74static void mov(Em *e, Op d, Op a) { imadi(e, d, a, 1u, NOREG); }
75static 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); }
76static 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); }
77static void xorr(Em *e, Op d, Op a, Op b) { BlackwellIRInsn i = I0(BW_IR_LOP3_XOR); 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.src2_vreg = b.v; i.src2_subreg = (uint8_t)b.s; emit(e, i, K_FIXED); }
78static void lop3(Em *e, int d, int a, int b, int c, uint32_t lut) { BlackwellIRInsn i = I0(BW_IR_LOP3_LUT); i.dst_vreg = d; i.src1_vreg = a; i.src2_vreg = b; i.src3_vreg = c; i.imm = lut; emit(e, i, K_FIXED); }
79static void ldg(Em *e, BlackwellIROpcode op, int d, int addr64, uint32_t imm, int udesc, int first) {
80 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);
81}
82static void stg(Em *e, int addr64, 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.ureg = udesc; emit(e, i, K_STORE); }
83static 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); }
84static 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); }
85static void bar_sync(Em *e) { emit(e, I0(BW_IR_BAR_SYNC), K_SYNC); }
86static void fop(Em *e, BlackwellIROpcode op, Op d, Op a, Op b, Op c) {
87 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;
88 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);
89}
90static void fadd(Em *e, Op d, Op a, Op b) { fop(e, BW_IR_FADD, d, a, b, R(NOREG)); }
91static void fmul(Em *e, Op d, Op a, Op b) { fop(e, BW_IR_FMUL, d, a, b, R(NOREG)); }
92static void ffma(Em *e, Op d, Op a, Op b, Op c) { fop(e, BW_IR_FFMA, d, a, b, c); }
93static void fmaxx(Em *e, Op d, Op a, Op b) { fop(e, BW_IR_FMNMX_MAX, d, a, b, R(NOREG)); }
94static void fminn(Em *e, Op d, Op a, Op b) { fop(e, BW_IR_FMNMX_MIN, d, a, b, R(NOREG)); }
95static 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); }
96static 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); }
97static 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); }
98static void bra(Em *e, int target) { BlackwellIRInsn i = I0(BW_IR_BRA); i.imm = (uint32_t)(target - here(e)); emit(e, i, K_BRA); }
99static 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; }
100static void patch_bra(Em *e, int at, int target) { if (!e->err) e->p->insns[at].imm = (uint32_t)(target - at); }
101static 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; }
102static void bsync(Em *e) { BlackwellIRInsn i = I0(BW_IR_BSYNC); i.bar_reg = 0; emit(e, i, K_BSYNC); }
103static void exit_k(Em *e) { emit(e, I0(BW_IR_EXIT), K_EXIT); }
104static void tail(Em *e) {
105 exit_k(e);
106 BlackwellIRInsn self = I0(BW_IR_BRA);
107 self.control = 0x000fc000u;
108 if (omega_bw_ir_append(e->p, &self) < 0) e->err = -1;
109}
110static void loop_begin(Em *e) { if (e->depth >= MAX_LOOPS) { e->err = -1; return; } e->open_start[e->depth++] = here(e); }
111static void loop_end(Em *e) {
112 if (e->depth <= 0) { e->err = -1; return; }
113 int start = e->open_start[--e->depth], end = here(e) - 1;
114 OmegaRegAlloc *ra = &e->p->regalloc;
115 for (int v = 0; v < ra->num_vregs; v++) {
116 OmegaLiveInterval *iv = &ra->intervals[v];
117 if (iv->first_def >= 0 && iv->first_def < start && iv->last_use >= start && iv->last_use < end) iv->last_use = end;
118 }
119}
120typedef struct { int bssy, bra; } Region;
121static Region region_skip_if_p0(Em *e) { Region r; r.bssy = bssy_fwd(e); r.bra = bra_if_p0_fwd(e); return r; }
122static 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); }
123static void ne_of(Em *e, int ne, Op a, Op b, int t, int t2) {
124 xorr(e, R(t), a, b);
125 imadi(e, R(t2), R(t), 0xffffffffu, NOREG);
126 lop3(e, t, t, t2, NOREG, 0xfcu);
127 shr(e, ne, t, 31);
128}
129static 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)); } }
130static void warp_max(Em *e, int v, int t) { for (uint32_t o = 16; o >= 1; o /= 2) { shfl_down(e, t, v, o); fmaxx(e, R(v), R(v), R(t)); } }
131static void warp_min(Em *e, int v, int t) { for (uint32_t o = 16; o >= 1; o /= 2) { shfl_down(e, t, v, o); fminn(e, R(v), R(v), R(t)); } }
132static void lane0_sts(Em *e, int lane, int one, int addr, Op v) {
133 isetp_ge_u32(e, lane, one);
134 Region r = region_skip_if_p0(e); sts32(e, addr, v); region_end(e, r);
135}
136/* the 8 per-warp partials at shared [base]: reduce with op (0 sum, 1 max, 2 min) into d (every thread) */
137static void cta_reduce8(Em *e, int d, uint32_t base, int op, int a0) {
138 int q0 = V128(e), q1 = V128(e);
139 if (e->err) return;
140 movi(e, a0, base);
141 lds128(e, q0, a0, 0, 1); lds128(e, q1, a0, 16, 0);
142 mov(e, R(d), S(q0, 0));
143 for (unsigned i = 1; i < 8; i++) {
144 Op s = i < 4 ? S(q0, i) : S(q1, i - 4);
145 if (op == 0) fadd(e, R(d), R(d), s); else if (op == 1) fmaxx(e, R(d), R(d), s); else fminn(e, R(d), R(d), s);
146 }
147}
148
149static const uint32_t C_INV2048 = 0x3A000000u; /* 1/2048 */
150static const uint32_t C_NLOG2E = 0xBFB8AA3Bu; /* -1.4426950f */
151static const uint32_t C_ONE = 0x3F800000u;
152static const uint32_t C_2P23 = 0x4B000000u;
153static uint32_t f32_bits(float f) { uint32_t u; memcpy(&u, &f, sizeof u); return u; }
154
155static int gen_axpy(Em *e, const OmegaDecSpec *sp) {
156 if (sp->stride == 0 || sp->stride % OMEGA_DEC_THREADS) return -1;
157 int udesc = UV64(e), px = V64(e), pa = V64(e), iters = V(e), tid = V(e), cta = V(e), gid = V(e), it = V(e), one = V(e);
158 int xa = V64(e), aa = V64(e), xv = V(e), av = V(e);
159 if (e->err) return -1;
160 ldcu64(e, udesc, 0x358);
161 ldc64(e, px, OMEGA_DEC_AXPY_X); ldc64(e, pa, OMEGA_DEC_AXPY_A); ldc32(e, iters, OMEGA_DEC_AXPY_ITERS);
162 s2r(e, tid, BW_SR_TID_X); s2r(e, cta, BW_SR_CTAID_X);
163 imadi(e, R(gid), R(cta), OMEGA_DEC_THREADS, tid);
164 imadw(e, xa, gid, 4u, px); imadw(e, aa, gid, 4u, pa);
165 movrz(e, R(it)); movi(e, one, 1);
166 const int L = here(e);
167 loop_begin(e);
168 isetp_ge_u32(e, it, iters);
169 const int done = bra_if_p0_fwd(e);
170 ldg(e, BW_IR_LDG_E, xv, xa, 0, udesc, 1); ldg(e, BW_IR_LDG_E, av, aa, 0, udesc, 0);
171 fadd(e, R(xv), R(xv), R(av));
172 stg(e, xa, R(xv), udesc);
173 imadw(e, xa, one, sp->stride * 4u, xa); imadw(e, aa, one, sp->stride * 4u, aa);
174 imadi(e, R(it), R(one), 1u, it);
175 bra(e, L);
176 loop_end(e);
177 patch_bra(e, done, here(e));
178 tail(e);
179 return e->err;
180}
181
182/* n quads of a thread's contiguous run at a + 16 q: one read barrier chain (the first load opens it) */
183static void ldg_quads(Em *e, const int *q, unsigned n, int a, uint32_t base, int udesc) {
184 for (unsigned k = 0; k < n; k++) ldg(e, BW_IR_LDG_E_128, q[k], a, base + 16u * k, udesc, k == 0);
185}
186static int new_quads(Em *e, int *q, unsigned n) { for (unsigned k = 0; k < n; k++) q[k] = V128(e); return e->err; }
187
188#define RES_MAX_VALS 32u /* width 8192 */
189
190static int gen_res(Em *e, const OmegaDecSpec *sp) {
191 const uint32_t M = sp->mode, H = sp->width ? sp->width : OMEGA_DEC_HIDDEN, N = H / OMEGA_DEC_THREADS, Q = N / 4u;
192 if ((M & 7u) == 0 || M > 15 || ((M & OMEGA_DEC_RES_ADD) && (M & OMEGA_DEC_RES_COMBINE))) return -1;
193 /* COMBINE in the rows form (L356): row ctaid.y also moves the routing weights by stride bytes (16-aligned, the 128-bit loads)
194 * and the down outputs by groups * 2048 floats; every row's arithmetic is the one-row program's */
195 if ((M & OMEGA_DEC_RES_ROWS) && (M & OMEGA_DEC_RES_COMBINE) && (sp->stride == 0 || sp->stride % 16u)) return -1;
196 if ((sp->groups && (sp->groups != 8u && sp->groups != 9u)) || (sp->groups == 9u && !(M & OMEGA_DEC_RES_COMBINE)) || (sp->mutant == 3 && sp->groups != 9u)) return -1;
197 if (H % (4u * OMEGA_DEC_THREADS) || N > RES_MAX_VALS || ((M & OMEGA_DEC_RES_COMBINE) && H != OMEGA_DEC_HIDDEN)) return -1;
198 int udesc = UV64(e), px = V64(e), pa = V64(e), pw = V64(e), pnw = V64(e), py = V64(e), eps = V(e), scale = V(e);
199 int tid = V(e), warp = V(e), lane = V(e), one = V(e), toff = V(e), t = V(e), t2 = V(e), a0 = V(e);
200 int xa = V64(e), xq[RES_MAX_VALS / 4];
201 if (new_quads(e, xq, Q)) return -1;
202 ldcu64(e, udesc, 0x358);
203 ldc64(e, px, OMEGA_DEC_RES_X); ldc64(e, pa, OMEGA_DEC_RES_A); ldc64(e, pw, OMEGA_DEC_RES_W); ldc64(e, pnw, OMEGA_DEC_RES_NW); ldc64(e, py, OMEGA_DEC_RES_Y);
204 ldc32(e, eps, OMEGA_DEC_RES_EPS); ldc32(e, scale, OMEGA_DEC_RES_SCALE);
205 s2r(e, tid, BW_SR_TID_X);
206 movi(e, one, 1);
207 shr(e, warp, tid, 5);
208 andi(e, R(lane), R(tid), 31u);
209 imadi(e, R(toff), R(tid), 4u * N, NOREG);
210 int cy = -1;
211 int roff = toff; /* the row's x, a and y offset: toff, plus ctaid.y * width in the rows form */
212 if (M & OMEGA_DEC_RES_ROWS) {
213 cy = V(e); roff = V(e);
214 if (e->err) return -1;
215 s2r(e, cy, BW_SR_CTAID_Y);
216 imadi(e, R(roff), R(cy), 4u * H, toff);
217 }
218 imadw(e, xa, roff, 1u, px);
219 ldg_quads(e, xq, Q, xa, 0, udesc);
220 Op x[RES_MAX_VALS];
221 for (unsigned i = 0; i < N; i++) x[i] = S(xq[i / 4], i % 4);
222 if (M & OMEGA_DEC_RES_ADD) {
223 int aa = V64(e), aq[RES_MAX_VALS / 4];
224 if (new_quads(e, aq, Q)) return -1;
225 imadw(e, aa, roff, 1u, pa);
226 ldg_quads(e, aq, Q, aa, 0, udesc);
227 for (unsigned i = 0; i < N; i++) fadd(e, x[i], x[i], S(aq[i / 4], i % 4));
228 }
229 if (M & OMEGA_DEC_RES_COMBINE) {
230 const unsigned GR = sp->groups ? sp->groups : OMEGA_DEC_TOPK; /* 8, or 9 with the shared expert (Qwen3.6-35B-A3B) */
231 int wq0 = V128(e), wq1 = V128(e), wq8 = GR > 8u ? V(e) : -1, da = V64(e), dq0 = V128(e), dq1 = V128(e), m[8];
232 for (unsigned i = 0; i < 8; i++) m[i] = V(e);
233 if (e->err) return -1;
234 int pwr = pw, doff = toff; /* the row's weights and down outputs: row 0's, moved by ctaid.y in the rows form */
235 if (M & OMEGA_DEC_RES_ROWS) {
236 pwr = V64(e); doff = V(e);
237 if (e->err) return -1;
238 imadw(e, pwr, cy, sp->stride, pw);
239 imadi(e, R(doff), R(cy), GR * OMEGA_DEC_HIDDEN * 4u, toff);
240 }
241 ldg(e, BW_IR_LDG_E_128, wq0, pwr, 0, udesc, 1); ldg(e, BW_IR_LDG_E_128, wq1, pwr, 16, udesc, 0);
242 if (GR > 8u) { /* the 32-bit LDG.E carries no immediate offset: its own address */
243 int pw8 = V64(e); if (e->err) return -1;
244 imadw(e, pw8, one, 32u, pwr); ldg(e, BW_IR_LDG_E, wq8, pw8, 0, udesc, 0);
245 }
246 imadw(e, da, doff, 1u, pa);
247 const unsigned G = sp->mutant == 2 ? 7u : sp->mutant == 3 ? 8u : GR;
248 for (unsigned g = 0; g < G; g++) {
249 ldg(e, BW_IR_LDG_E_128, dq0, da, g * OMEGA_DEC_HIDDEN * 4u, udesc, 1); ldg(e, BW_IR_LDG_E_128, dq1, da, g * OMEGA_DEC_HIDDEN * 4u + 16u, udesc, 0);
250 Op w = g < 4 ? S(wq0, g) : g < 8 ? S(wq1, g - 4) : R(wq8);
251 for (unsigned i = 0; i < 8; i++) {
252 Op d = i < 4 ? S(dq0, i) : S(dq1, i - 4);
253 if (g == 0) fmul(e, R(m[i]), w, d);
254 else { fmul(e, R(t), w, d); fadd(e, R(m[i]), R(m[i]), R(t)); }
255 }
256 }
257 for (unsigned i = 0; i < 8; i++) fadd(e, x[i], x[i], R(m[i]));
258 }
259 if (M & (OMEGA_DEC_RES_ADD | OMEGA_DEC_RES_COMBINE)) {
260 int sa = V64(e);
261 if (e->err) return -1;
262 for (unsigned i = 0; i < N; i++) { if (i == 0) imadw(e, sa, roff, 1u, px); else imadw(e, sa, one, 4u, sa); stg(e, sa, x[i], udesc); }
263 }
264 if (M & OMEGA_DEC_RES_NORM) {
265 int sq = V(e), rs = V(e), na = V64(e), nq[RES_MAX_VALS / 4];
266 new_quads(e, nq, Q);
267 int ya = V64(e), yv = V(e); /* allocated after the quads, as before the width field: the same program at 2048 */
268 if (e->err) return -1;
269 imadw(e, na, toff, 1u, pnw);
270 ldg_quads(e, nq, Q, na, 0, udesc);
271 fmul(e, R(sq), x[0], x[0]);
272 for (unsigned i = 1; i < N; i++) ffma(e, R(sq), x[i], x[i], R(sq));
273 warp_sum(e, sq, t);
274 imadi(e, R(a0), R(warp), 4u, NOREG); imadi(e, R(a0), R(one), OMEGA_DEC_SM_RED, a0);
275 lane0_sts(e, lane, one, a0, R(sq));
276 bar_sync(e);
277 cta_reduce8(e, sq, OMEGA_DEC_SM_RED, 0, a0);
278 movi(e, t2, H == OMEGA_DEC_HIDDEN ? C_INV2048 : f32_bits(1.0f / (float)H));
279 ffma(e, R(rs), R(sq), R(t2), R(eps));
280 mufu(e, BW_IR_MUFU_RSQ, R(rs), R(rs));
281 imadw(e, ya, roff, 1u, py);
282 for (unsigned i = 0; i < N; i++) {
283 fmul(e, R(yv), x[i], R(rs));
284 if (sp->mutant != 1) fmul(e, R(yv), R(yv), S(nq[i / 4], i % 4));
285 fmul(e, R(yv), R(yv), R(scale));
286 if (i) imadw(e, ya, one, 4u, ya);
287 stg(e, ya, R(yv), udesc);
288 }
289 }
290 tail(e);
291 return e->err;
292}
293
294static int gen_swiglu(Em *e, const OmegaDecSpec *sp) {
295 const uint32_t I = sp->width ? sp->width : OMEGA_DEC_INTER;
296 if (I % OMEGA_DEC_THREADS) return -1;
297 int udesc = UV64(e), pgu = V64(e), pact = V64(e), scale = V(e), tid = V(e), cx = V(e), cy = V(e), c = V(e), gi = V(e), ai = V(e);
298 int ga = V64(e), ua = V64(e), aa = V64(e), gv = V(e), uv = V(e), t = V(e), cst = V(e), ui = V(e);
299 if (e->err) return -1;
300 ldcu64(e, udesc, 0x358);
301 ldc64(e, pgu, OMEGA_DEC_SW_GU); ldc64(e, pact, OMEGA_DEC_SW_ACT); ldc32(e, scale, OMEGA_DEC_SW_SCALE);
302 s2r(e, tid, BW_SR_TID_X); s2r(e, cx, BW_SR_CTAID_X); s2r(e, cy, BW_SR_CTAID_Y);
303 imadi(e, R(c), R(cx), OMEGA_DEC_THREADS, tid);
304 imadi(e, R(gi), R(cy), 2u * I, c);
305 imadi(e, R(ai), R(cy), I, c);
306 /* the up half sits INTER floats after the gate: its own address (the 32-bit LDG.E encoding carries no immediate
307 * offset, omega_blackwell_codegen.c BW_IR_LDG_E; the warp simulator caught the dropped offset, 2026-10-13) */
308 movi(e, cst, sp->mutant == 1 ? I - 1u : I);
309 imadi(e, R(ui), R(cst), 1u, gi);
310 imadw(e, ga, gi, 4u, pgu);
311 imadw(e, ua, ui, 4u, pgu);
312 imadw(e, aa, ai, 4u, pact);
313 ldg(e, BW_IR_LDG_E, gv, ga, 0, udesc, 1);
314 ldg(e, BW_IR_LDG_E, uv, ua, 0, udesc, 0);
315 movi(e, cst, C_NLOG2E);
316 fmul(e, R(t), R(gv), R(cst));
317 mufu(e, BW_IR_MUFU_EX2, R(t), R(t));
318 movi(e, cst, C_ONE);
319 fadd(e, R(t), R(t), R(cst));
320 mufu(e, BW_IR_MUFU_RCP, R(t), R(t));
321 fmul(e, R(t), R(gv), R(t));
322 fmul(e, R(t), R(t), R(uv));
323 fmul(e, R(t), R(t), R(scale));
324 stg(e, aa, R(t), udesc);
325 tail(e);
326 return e->err;
327}
328
329static int gen_argmax(Em *e, const OmegaDecSpec *sp) {
330 const int indexed = sp->mode == 1;
331 int udesc = UV64(e), pv = V64(e), pi = V64(e), pov = V64(e), poi = V64(e);
332 int tid = V(e), cta = V(e), warp = V(e), lane = V(e), one = V(e), gid = V(e), off = V(e), t = V(e), t2 = V(e), a0 = V(e);
333 int va = V64(e), vq0 = V128(e), vq1 = V128(e), iq0 = 0, iq1 = 0, m = V(e), mx = V(e), ne = V(e), cand = V(e), best = V(e), c2p23 = V(e), idx = V(e);
334 if (indexed) { iq0 = V128(e); iq1 = V128(e); }
335 if (e->err) return -1;
336 ldcu64(e, udesc, 0x358);
337 ldc64(e, pv, OMEGA_DEC_AM_V); ldc64(e, pi, OMEGA_DEC_AM_I); ldc64(e, pov, OMEGA_DEC_AM_OV); ldc64(e, poi, OMEGA_DEC_AM_OI);
338 s2r(e, tid, BW_SR_TID_X); s2r(e, cta, BW_SR_CTAID_X);
339 movi(e, one, 1); movi(e, c2p23, C_2P23);
340 shr(e, warp, tid, 5);
341 andi(e, R(lane), R(tid), 31u);
342 imadi(e, R(gid), R(cta), OMEGA_DEC_THREADS, tid);
343 imadi(e, R(off), R(gid), 32u, NOREG);
344 imadw(e, va, off, 1u, pv);
345 ldg(e, BW_IR_LDG_E_128, vq0, va, 0, udesc, 1); ldg(e, BW_IR_LDG_E_128, vq1, va, 16, udesc, 0);
346 if (indexed) {
347 imadw(e, va, off, 1u, pi);
348 ldg(e, BW_IR_LDG_E_128, iq0, va, 0, udesc, 0); ldg(e, BW_IR_LDG_E_128, iq1, va, 16, udesc, 0);
349 }
350 Op v[8];
351 for (unsigned j = 0; j < 8; j++) v[j] = j < 4 ? S(vq0, j) : S(vq1, j - 4);
352 const unsigned J = sp->mutant == 1 ? 7u : 8u;
353 mov(e, R(m), v[0]);
354 for (unsigned j = 1; j < J; j++) fmaxx(e, R(m), R(m), v[j]);
355 warp_max(e, m, t);
356 imadi(e, R(a0), R(warp), 4u, NOREG); imadi(e, R(a0), R(one), OMEGA_DEC_SM_RED, a0);
357 lane0_sts(e, lane, one, a0, R(m));
358 bar_sync(e);
359 cta_reduce8(e, mx, OMEGA_DEC_SM_RED, 1, a0);
360 /* lowest index holding mx: candidate float bits 0x4B000000 | idx, lifted by 0x30000000 where v != mx */
361 for (unsigned j = 0; j < J; j++) {
362 if (indexed) mov(e, R(idx), j < 4 ? S(iq0, j) : S(iq1, j - 4));
363 else imadi(e, R(idx), R(gid), 8u, NOREG), imadi(e, R(idx), R(one), j, idx);
364 lop3(e, idx, idx, c2p23, NOREG, 0xfcu);
365 ne_of(e, ne, v[j], R(mx), t, t2);
366 imadi(e, R(cand), R(ne), 0x30000000u, idx);
367 if (j == 0) mov(e, R(best), R(cand)); else fminn(e, R(best), R(best), R(cand));
368 }
369 warp_min(e, best, t);
370 imadi(e, R(a0), R(warp), 4u, NOREG); imadi(e, R(a0), R(one), OMEGA_DEC_SM_RED2, a0);
371 lane0_sts(e, lane, one, a0, R(best));
372 bar_sync(e);
373 cta_reduce8(e, best, OMEGA_DEC_SM_RED2, 2, a0);
374 andi(e, R(best), R(best), 0x7fffffu);
375 isetp_ge_u32(e, tid, one);
376 {
377 Region r = region_skip_if_p0(e);
378 int oa = V64(e);
379 if (e->err) return -1;
380 imadw(e, oa, cta, 4u, pov); stg(e, oa, R(mx), udesc);
381 imadw(e, oa, cta, 4u, poi); stg(e, oa, R(best), udesc);
382 region_end(e, r);
383 }
384 tail(e);
385 return e->err;
386}
387
388static int gen_gather(Em *e, const OmegaDecSpec *sp) {
389 const uint32_t N = sp->width, IS = sp->stride;
390 if (N == 0 || IS == 0) return -1;
391 int udesc = UV64(e), pin = V64(e), ptab = V64(e), pout = V64(e), tid = V(e), cx = V(e), cy = V(e), n = V(e), lim = V(e);
392 int ta = V64(e), tv = V(e), idx = V(e), ia = V64(e), val = V(e), o = V(e), oa = V64(e);
393 if (e->err) return -1;
394 ldcu64(e, udesc, 0x358);
395 ldc64(e, pin, OMEGA_DEC_GA_IN); ldc64(e, ptab, OMEGA_DEC_GA_TAB); ldc64(e, pout, OMEGA_DEC_GA_OUT);
396 s2r(e, tid, BW_SR_TID_X); s2r(e, cx, BW_SR_CTAID_X); s2r(e, cy, BW_SR_CTAID_Y);
397 imadi(e, R(n), R(cx), OMEGA_DEC_THREADS, tid);
398 movi(e, lim, N);
399 isetp_ge_u32(e, n, lim);
400 Region r = region_skip_if_p0(e);
401 imadw(e, ta, n, 4u, ptab);
402 ldg(e, BW_IR_LDG_E, tv, ta, 0, udesc, 1);
403 imadi(e, R(idx), R(cy), IS, tv);
404 if (sp->mutant == 1) { movi(e, lim, 1); imadi(e, R(idx), R(lim), 1u, idx); }
405 imadw(e, ia, idx, 4u, pin);
406 ldg(e, BW_IR_LDG_E, val, ia, 0, udesc, 1);
407 imadi(e, R(o), R(cy), N, n);
408 imadw(e, oa, o, 4u, pout);
409 stg(e, oa, R(val), udesc);
410 region_end(e, r);
411 tail(e);
412 return e->err;
413}
414
415static int gen_gate(Em *e, const OmegaDecSpec *sp) {
416 int udesc = UV64(e), pa = V64(e), pg = V64(e), pout = V64(e), lim = V(e), tid = V(e), cx = V(e), n = V(e);
417 int aa = V64(e), ga = V64(e), oa = V64(e), av = V(e), gv = V(e), t = V(e), cst = V(e);
418 if (e->err) return -1;
419 ldcu64(e, udesc, 0x358);
420 ldc64(e, pa, OMEGA_DEC_GT_A); ldc64(e, pg, OMEGA_DEC_GT_G); ldc64(e, pout, OMEGA_DEC_GT_OUT); ldc32(e, lim, OMEGA_DEC_GT_N);
421 s2r(e, tid, BW_SR_TID_X); s2r(e, cx, BW_SR_CTAID_X);
422 imadi(e, R(n), R(cx), OMEGA_DEC_THREADS, tid);
423 isetp_ge_u32(e, n, lim);
424 Region r = region_skip_if_p0(e);
425 if (sp->mode & 1u) { /* rows (L369): row ctaid.y; a and out move by width floats a row, g by stride floats */
426 int cy = V(e), ro = V(e); if (e->err) return -1;
427 s2r(e, cy, BW_SR_CTAID_Y);
428 imadi(e, R(ro), R(cy), sp->width, n); imadw(e, aa, ro, 4u, pa); imadw(e, oa, ro, 4u, pout);
429 imadi(e, R(ro), R(cy), sp->stride, n); imadw(e, ga, ro, 4u, pg);
430 } else { imadw(e, aa, n, 4u, pa); imadw(e, ga, n, 4u, pg); imadw(e, oa, n, 4u, pout); }
431 ldg(e, BW_IR_LDG_E, av, aa, 0, udesc, 1);
432 ldg(e, BW_IR_LDG_E, gv, ga, 0, udesc, 0);
433 if (sp->mutant == 1) mov(e, R(t), R(av));
434 else {
435 movi(e, cst, C_NLOG2E);
436 fmul(e, R(t), R(gv), R(cst));
437 mufu(e, BW_IR_MUFU_EX2, R(t), R(t));
438 movi(e, cst, C_ONE);
439 fadd(e, R(t), R(t), R(cst));
440 mufu(e, BW_IR_MUFU_RCP, R(t), R(t));
441 fmul(e, R(t), R(av), R(t));
442 }
443 stg(e, oa, R(t), udesc);
444 region_end(e, r);
445 tail(e);
446 return e->err;
447}
448
449/* XBOUND mode 1: lo[n] = s[n] * 2^64, n < count (the screen's x' row from the plain hidden: exact, a power of two) */
450static int gen_xscale(Em *e) {
451 int udesc = UV64(e), ps = V64(e), plo = V64(e), lim = V(e), tid = V(e), cx = V(e), n = V(e), sa = V64(e), la = V64(e), v = V(e), cst = V(e);
452 if (e->err) return -1;
453 ldcu64(e, udesc, 0x358);
454 ldc64(e, ps, OMEGA_DEC_XB_S); ldc64(e, plo, OMEGA_DEC_XB_LO); ldc32(e, lim, OMEGA_DEC_XB_N);
455 s2r(e, tid, BW_SR_TID_X); s2r(e, cx, BW_SR_CTAID_X);
456 imadi(e, R(n), R(cx), OMEGA_DEC_THREADS, tid);
457 isetp_ge_u32(e, n, lim);
458 Region r = region_skip_if_p0(e);
459 imadw(e, sa, n, 4u, ps); imadw(e, la, n, 4u, plo);
460 ldg(e, BW_IR_LDG_E, v, sa, 0, udesc, 1);
461 movi(e, cst, 0x5F800000u); /* 2^64 */
462 fmul(e, R(v), R(v), R(cst));
463 stg(e, la, R(v), udesc);
464 region_end(e, r);
465 tail(e);
466 return e->err;
467}
468
469static int gen_xbound(Em *e, const OmegaDecSpec *sp) {
470 if (sp->mode > 1 || sp->mutant < 0 || sp->mutant > 1 || (sp->mode == 1 && sp->mutant)) return -1;
471 if (sp->mode == 1) return gen_xscale(e);
472 int udesc = UV64(e), ps = V64(e), pc = V64(e), ph = V64(e), plo = V64(e), phi = V64(e), lim = V(e);
473 int tid = V(e), cx = V(e), warp = V(e), lane = V(e), one = V(e), t = V(e), a0 = V(e), acc = V(e), tot = V(e), r = V(e);
474 int hn = V(e), cst = V(e), n = V(e), ha = V64(e), sa = V64(e), ca = V64(e), la = V64(e), hia = V64(e);
475 int sv = V(e), cv = V(e), b = V(e), ab = V(e), lo = V(e), hi = V(e), hq[2];
476 if (new_quads(e, hq, 2)) return -1;
477 ldcu64(e, udesc, 0x358);
478 ldc64(e, ps, OMEGA_DEC_XB_S); ldc64(e, pc, OMEGA_DEC_XB_C); ldc64(e, ph, OMEGA_DEC_XB_H);
479 ldc64(e, plo, OMEGA_DEC_XB_LO); ldc64(e, phi, OMEGA_DEC_XB_HI); ldc32(e, lim, OMEGA_DEC_XB_N);
480 s2r(e, tid, BW_SR_TID_X); s2r(e, cx, BW_SR_CTAID_X);
481 movi(e, one, 1);
482 shr(e, warp, tid, 5);
483 andi(e, R(lane), R(tid), 31u);
484 /* |h|: 8 values per thread, warp sums, the 8 warp partials in shared memory */
485 imadw(e, ha, tid, 32u, ph);
486 ldg_quads(e, hq, 2, ha, 0, udesc);
487 movrz(e, R(acc));
488 for (unsigned i = 0; i < 8; i++) ffma(e, R(acc), S(hq[i / 4], i % 4), S(hq[i / 4], i % 4), R(acc));
489 warp_sum(e, acc, t);
490 imadi(e, R(a0), R(warp), 4u, NOREG);
491 lane0_sts(e, lane, one, a0, R(acc));
492 bar_sync(e);
493 cta_reduce8(e, tot, OMEGA_DEC_SM_RED, 0, a0);
494 movi(e, cst, 0x0D800000u); /* 2^-100: t > 0, so RSQ(t) is finite */
495 fadd(e, R(tot), R(tot), R(cst));
496 mufu(e, BW_IR_MUFU_RSQ, R(r), R(tot));
497 fmul(e, R(hn), R(tot), R(r));
498 movi(e, cst, 0x3F802000u); /* 1 + 2^-10 */
499 fmul(e, R(hn), R(hn), R(cst));
500 /* this thread's row */
501 imadi(e, R(n), R(cx), OMEGA_DEC_THREADS, tid);
502 isetp_ge_u32(e, n, lim);
503 Region rg = region_skip_if_p0(e);
504 imadw(e, sa, n, 4u, ps); imadw(e, ca, n, 4u, pc); imadw(e, la, n, 4u, plo); imadw(e, hia, n, 4u, phi);
505 ldg(e, BW_IR_LDG_E, sv, sa, 0, udesc, 1);
506 ldg(e, BW_IR_LDG_E, cv, ca, 0, udesc, 0);
507 if (sp->mutant == 1) movrz(e, R(b));
508 else fmul(e, R(b), R(cv), R(hn));
509 andi(e, R(ab), R(sv), 0x7fffffffu);
510 movi(e, cst, 0x35800000u); /* 2^-20 */
511 ffma(e, R(b), R(ab), R(cst), R(b));
512 movi(e, cst, 0x0D800000u); /* 2^-100 */
513 fadd(e, R(b), R(b), R(cst));
514 fadd(e, R(hi), R(sv), R(b));
515 movi(e, cst, 0x80000000u);
516 xorr(e, R(b), R(b), R(cst));
517 fadd(e, R(lo), R(sv), R(b));
518 stg(e, la, R(lo), udesc);
519 stg(e, hia, R(hi), udesc);
520 region_end(e, rg);
521 tail(e);
522 return e->err;
523}
524
525/* ---- GROUP: the stroke's segment table (L312 v2) ---- */
526static 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); }
527static 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); }
528/* store v at base64 + off (tmp64 scratch; one = a register holding 1) */
529static void stg_at(Em *e, int tmp64, int one, int base64, uint32_t off, Op v, int udesc) { imadw(e, tmp64, one, off, base64); stg(e, tmp64, v, udesc); }
530/* d = 1 iff a < b (unsigned, both < 2^31): (a - b) >> 31 */
531static void lt_u(Em *e, int d, int a, int b) { imadi(e, R(d), R(b), 0xffffffffu, a); shr(e, d, d, 31); }
532/* d = both: (a + b) >> 1 for 0/1 flags */
533static void and_flag(Em *e, int d, int a, int b) { imadi(e, R(d), R(a), 1u, b); shr(e, d, d, 1); }
534
535/* One CTA. Shared: ids [R] u32 at 0, first-of-id flags [R] at 512 (R <= 128). Every thread runs both counting passes with
536 * the uniform trip count R (no divergence around the barriers); only threads t < R hold a row. Row t = h * S + j. */
537static int gen_group(Em *e, const OmegaDecSpec *sp) {
538 const uint32_t SL = sp->groups ? sp->groups : OMEGA_DEC_TOPK; /* slots per head */
539 if (SL < 1u || SL > 16u || sp->mode || sp->width || sp->stride) return -1;
540 const uint32_t magic = (65536u + SL - 1u) / SL; /* h = (t * magic) >> 16 = t / SL exactly for t < 256 (the error t * (magic * SL - 65536) / 65536 stays below 1 / SL) */
541 if (magic * SL - 65536u >= 256u) return -1;
542 int udesc = UV64(e), pids = V64(e), ptgu = V64(e), ptdn = V64(e), psgu = V64(e), psdn = V64(e), pxm = V64(e), pym = V64(e);
543 int stride = V(e), heads = V(e), tid = V(e), Rr = V(e), one = V(e), zero = V(e), c512 = V(e);
544 int h = V(e), j = V(e), off = V(e), id = V(e), key = V(e), sa = V(e), sa2 = V(e), inr = V(e);
545 int q = V(e), idq = V(e), keyq = V(e), t1 = V(e), t2 = V(e), eq = V(e), p = V(e), cnt = V(e), nb = V(e), first = V(e), fq = V(e), sidx = V(e), D = V(e), fl = V(e);
546 int ia = V64(e), oa = V64(e), ta = V64(e), wv = V64(e), sv = V64(e);
547 if (e->err) return -1;
548 ldcu64(e, udesc, 0x358);
549 ldc64(e, pids, OMEGA_DEC_GR_IDS); ldc64(e, ptgu, OMEGA_DEC_GR_TGU); ldc64(e, ptdn, OMEGA_DEC_GR_TDN);
550 ldc64(e, psgu, OMEGA_DEC_GR_SGU); ldc64(e, psdn, OMEGA_DEC_GR_SDN); ldc64(e, pxm, OMEGA_DEC_GR_XMAP); ldc64(e, pym, OMEGA_DEC_GR_YMAP);
551 ldc32(e, stride, OMEGA_DEC_GR_STRIDE); ldc32(e, heads, OMEGA_DEC_GR_HEADS);
552 s2r(e, tid, BW_SR_TID_X);
553 movi(e, one, 1); movrz(e, R(zero)); movi(e, c512, 512);
554 imadi(e, R(Rr), R(heads), SL, NOREG); /* R = N * S rows */
555 lt_u(e, inr, tid, Rr); /* 1 iff this thread holds a row */
556 imadi(e, R(h), R(tid), magic, NOREG); shr(e, h, h, 16); /* h = t / S */
557 imadi(e, R(j), R(h), 0u - SL, tid); /* j = t - h * S */
558 movi(e, id, 0xffffffffu);
559 imadi(e, R(sa), R(tid), 4u, NOREG);
560 /* pass 0 (t < R): id and key; the id to shared */
561 isetp_ge_u32(e, tid, Rr);
562 Region r0 = region_skip_if_p0(e);
563 imadr(e, off, h, stride, NOREG); imadi(e, R(off), R(j), 4u, off);
564 imadw(e, ia, off, 1u, pids);
565 ldg(e, BW_IR_LDG_E, id, ia, 0, udesc, 1);
566 sts32(e, sa, R(id));
567 region_end(e, r0);
568 if (sp->mutant == 1) imadi(e, R(key), R(id), 128u, NOREG); else imadi(e, R(key), R(id), 128u, tid);
569 bar_sync(e);
570 /* pass 1 (all threads, q over the R rows): p = rows with a smaller key; cnt = rows with this id; nb = of those, rows before t */
571 movrz(e, R(p)); movrz(e, R(cnt)); movrz(e, R(nb)); movrz(e, R(q)); movrz(e, R(sa2));
572 const int L1 = here(e);
573 loop_begin(e);
574 isetp_ge_u32(e, q, Rr);
575 const int done1 = bra_if_p0_fwd(e);
576 lds32(e, idq, sa2);
577 imadi(e, R(keyq), R(idq), 128u, q);
578 lt_u(e, t1, keyq, key); imadi(e, R(p), R(t1), 1u, p);
579 lt_u(e, t1, idq, id); lt_u(e, t2, id, idq);
580 imadi(e, R(eq), R(t1), 0xffffffffu, one); imadi(e, R(eq), R(t2), 0xffffffffu, eq); /* eq = 1 - lt - gt */
581 imadi(e, R(cnt), R(eq), 1u, cnt);
582 lt_u(e, t1, q, tid); and_flag(e, t1, t1, eq); imadi(e, R(nb), R(t1), 1u, nb);
583 imadi(e, R(q), R(one), 1u, q); imadi(e, R(sa2), R(one), 4u, sa2);
584 bra(e, L1);
585 loop_end(e);
586 patch_bra(e, done1, here(e));
587 /* first = (nb == 0) and the thread holds a row */
588 imadi(e, R(first), R(one), 0xffffffffu, nb); shr(e, first, first, 31); and_flag(e, first, first, inr);
589 imadi(e, R(sa), R(tid), 4u, c512);
590 isetp_ge_u32(e, tid, Rr);
591 Region r1 = region_skip_if_p0(e);
592 sts32(e, sa, R(first));
593 region_end(e, r1);
594 bar_sync(e);
595 /* pass 2 (all threads): sidx = distinct ids below this one; D = distinct ids */
596 movrz(e, R(sidx)); movrz(e, R(D)); movrz(e, R(q)); movrz(e, R(sa2));
597 const int L2 = here(e);
598 loop_begin(e);
599 isetp_ge_u32(e, q, Rr);
600 const int done2 = bra_if_p0_fwd(e);
601 lds32(e, idq, sa2);
602 imadi(e, R(sa), R(one), 512u, sa2);
603 lds32(e, fq, sa);
604 imadi(e, R(D), R(fq), 1u, D);
605 lt_u(e, t1, idq, id); and_flag(e, t1, t1, fq); imadi(e, R(sidx), R(t1), 1u, sidx);
606 imadi(e, R(q), R(one), 1u, q); imadi(e, R(sa2), R(one), 4u, sa2);
607 bra(e, L2);
608 loop_end(e);
609 patch_bra(e, done2, here(e));
610 /* maps (t < R): XMAP[p] = h, YMAP[p] = t */
611 isetp_ge_u32(e, tid, Rr);
612 Region r2 = region_skip_if_p0(e);
613 imadw(e, oa, p, 4u, pxm); stg(e, oa, R(h), udesc);
614 imadw(e, oa, p, 4u, pym); stg(e, oa, R(tid), udesc);
615 region_end(e, r2);
616 /* segment of a first-of-id row: {w_va, s_va, row0 = p, cnt, 0, 0} from the head's directory entry j */
617 if (sp->mutant == 2) imadi(e, R(cnt), R(one), 0xffffffffu, cnt);
618 isetp_ge_u32(e, zero, first);
619 Region r3 = region_skip_if_p0(e);
620 imadr(e, off, h, stride, NOREG); imadi(e, R(off), R(j), 16u, off);
621 imadw(e, ia, off, 1u, ptgu);
622 ldg(e, BW_IR_LDG_E_64, wv, ia, 0, udesc, 1); ldg(e, BW_IR_LDG_E_64, sv, ia, 8, udesc, 0);
623 imadw(e, oa, sidx, OMEGA_DEC_GR_SEG_BYTES, psgu);
624 stg_at(e, ta, one, oa, 0, S(wv, 0), udesc); stg_at(e, ta, one, oa, 4, S(wv, 1), udesc);
625 stg_at(e, ta, one, oa, 8, S(sv, 0), udesc); stg_at(e, ta, one, oa, 12, S(sv, 1), udesc);
626 stg_at(e, ta, one, oa, 16, R(p), udesc); stg_at(e, ta, one, oa, 20, R(cnt), udesc);
627 stg_at(e, ta, one, oa, 24, R(zero), udesc); stg_at(e, ta, one, oa, 28, R(zero), udesc);
628 imadw(e, ia, off, 1u, ptdn);
629 ldg(e, BW_IR_LDG_E_64, wv, ia, 0, udesc, 1); ldg(e, BW_IR_LDG_E_64, sv, ia, 8, udesc, 0);
630 imadw(e, oa, sidx, OMEGA_DEC_GR_SEG_BYTES, psdn);
631 stg_at(e, ta, one, oa, 0, S(wv, 0), udesc); stg_at(e, ta, one, oa, 4, S(wv, 1), udesc);
632 stg_at(e, ta, one, oa, 8, S(sv, 0), udesc); stg_at(e, ta, one, oa, 12, S(sv, 1), udesc);
633 stg_at(e, ta, one, oa, 16, R(p), udesc); stg_at(e, ta, one, oa, 20, R(cnt), udesc);
634 stg_at(e, ta, one, oa, 24, R(zero), udesc); stg_at(e, ta, one, oa, 28, R(zero), udesc);
635 region_end(e, r3);
636 /* trailing empty segments: D <= t < R write zeros at SEG[t] */
637 if (sp->mutant != 3) {
638 lt_u(e, t1, tid, D); imadi(e, R(t1), R(t1), 0xffffffffu, one); and_flag(e, fl, t1, inr); /* (t >= D) & (t < R) */
639 isetp_ge_u32(e, zero, fl);
640 Region r4 = region_skip_if_p0(e);
641 imadw(e, oa, tid, OMEGA_DEC_GR_SEG_BYTES, psgu);
642 for (uint32_t w = 0; w < OMEGA_DEC_GR_SEG_BYTES; w += 4) stg_at(e, ta, one, oa, w, R(zero), udesc);
643 imadw(e, oa, tid, OMEGA_DEC_GR_SEG_BYTES, psdn);
644 for (uint32_t w = 0; w < OMEGA_DEC_GR_SEG_BYTES; w += 4) stg_at(e, ta, one, oa, w, R(zero), udesc);
645 region_end(e, r4);
646 }
647 /* map padding: R <= t < R + 8 write zeros */
648 imadi(e, R(t2), R(one), OMEGA_DEC_GR_PAD, Rr);
649 lt_u(e, t1, tid, t2); imadi(e, R(t2), R(inr), 0xffffffffu, one); and_flag(e, fl, t1, t2); /* (t < R + 8) & (t >= R) */
650 isetp_ge_u32(e, zero, fl);
651 Region r5 = region_skip_if_p0(e);
652 imadw(e, oa, tid, 4u, pxm); stg(e, oa, R(zero), udesc);
653 imadw(e, oa, tid, 4u, pym); stg(e, oa, R(zero), udesc);
654 region_end(e, r5);
655 tail(e);
656 return e->err;
657}
658
659int omega_blackwell_codegen_dec_prog(const OmegaDecSpec *spec, BlackwellIRProgram *prog) {
660 if (!spec || !prog) return -1;
661 omega_bw_ir_init(prog);
662 Em em; memset(&em, 0, sizeof em); em.p = prog;
663 int rc;
664 switch (spec->kind) {
665 case OMEGA_DEC_AXPY: rc = gen_axpy(&em, spec); break;
666 case OMEGA_DEC_RES: rc = gen_res(&em, spec); break;
667 case OMEGA_DEC_SWIGLU: rc = gen_swiglu(&em, spec); break;
668 case OMEGA_DEC_ARGMAX: rc = gen_argmax(&em, spec); break;
669 case OMEGA_DEC_GATHER: rc = gen_gather(&em, spec); break;
670 case OMEGA_DEC_GATE: rc = gen_gate(&em, spec); break;
671 case OMEGA_DEC_GROUP: rc = gen_group(&em, spec); break;
672 case OMEGA_DEC_XBOUND: rc = gen_xbound(&em, spec); break;
673 default: return -1;
674 }
675 if (rc != 0 || em.err || em.depth != 0) return -1;
676 return omega_bw_regalloc_solve(prog);
677}
678
679#else
680/* The builder is om/codegen/cg_dec.om (unit cg_dec); this adapter packs the spec and replays its records. */
681int omega_blackwell_codegen_dec_prog(const OmegaDecSpec *spec, BlackwellIRProgram *prog) {
682 uint64_t error = 0;
683 if (!spec || !prog) return -1;
684 uint64_t a = (uint64_t)spec->mode | (uint64_t)spec->width << 32;
685 uint64_t b = (uint64_t)spec->stride | (uint64_t)spec->groups << 32;
686 return omega_gpu_om_ir_replay((OmegaOmIrUnit)omega_cg_dec, (uint64_t)(uint32_t)spec->kind, a, b,
687 (uint64_t)(uint32_t)spec->mutant, prog, &error) == 0 ? 0 : -1;
688}
689#endif
690
691int omega_dec_prog_stats(const OmegaDecSpec *spec, uint32_t *insns, uint32_t *gprs) {
692 static BlackwellIRProgram prog;
693 if (omega_blackwell_codegen_dec_prog(spec, &prog) != 0) return -1;
694 if (insns) *insns = (uint32_t)prog.count;
695 if (gprs) *gprs = prog.regalloc.peak_gpr_usage;
696 return 0;
697}