benchmarks-mv
Measurement harnesses and evidence bundles for AIEN on the NVIDIA DGX Spark GB10. Speed is reported against the machine's physical limit and my own previous run. Attachment of AienOS.
Brought by drakestapleton
benchmarks-mv / prime-drag-race / evidence / 2026-10-05-campaign / c5 / eltwise / FB1-CUT4-c505e7f / omega_gpu_elementwise_api.c
| 1 | /* omega_gpu_elementwise_api.c: FB-1 cut 4 (2026-10-04). See the header. |
| 2 | * |
| 3 | * Layout of this file: (1) kernel codegen on top of the BlackwellIR, with a |
| 4 | * small scheduler that writes every control word; (2) one launcher cloned from |
| 5 | * the chip-proven tensor matmul path (omega_blackwell_execute_matmul_tensor); |
| 6 | * (3) the public entry points, which split work into <= OMEGA_GPU_EW_MAX_CTAS |
| 7 | * launches and run the host-side bookkeeping. |
| 8 | * |
| 9 | * Scheduling rules (chip-learned, src/omega_numeric.c omega_numeric_check_patch): |
| 10 | * - a variable-latency result (LDG, LDS, LDC, LDCU, S2R, SHFL, MUFU) arrives through |
| 11 | * a scoreboard; the producer stalls >= 2 and the consumer waits on that SB; |
| 12 | * - a fixed-latency result (FADD FMUL FFMA IMAD IADD3 MOV LOP3 SHF) has no |
| 13 | * scoreboard: the producer stalls >= 5 before its consumer (we use 6); |
| 14 | * - a predicate producer (ISETP) stalls 14 before @P0 use (NAK SM120 padding); |
| 15 | * - stores (STG, STS) set a read barrier so the next instruction cannot overwrite |
| 16 | * the source register early. |
| 17 | * The emitter below serialises: every instruction waits on everything still |
| 18 | * pending. A final local pass tightens supported adjacent ALU pairs to 5 cycles. |
| 19 | */ |
| 20 | #include "omega_gpu_elementwise_api.h" |
| 21 | #include "omega_blackwell_codegen.h" |
| 22 | #include "omega_blackwell_qmd.h" |
| 23 | #include "omega_blackwell_submit.h" |
| 24 | #include "omega_gpu_session.h" |
| 25 | #include "sha256.h" |
| 26 | #include <stdio.h> |
| 27 | #include <stdlib.h> |
| 28 | #include <string.h> |
| 29 | #include <time.h> |
| 30 | |
| 31 | /* ------------------------------------------------------------------ codegen */ |
| 32 | |
| 33 | #define CW(stall, wbar, rbar, wait) \ |
| 34 | (((uint32_t)(stall) << 9) | ((uint32_t)(wbar) << 14) | ((uint32_t)(rbar) << 17) | ((uint32_t)(wait) << 20)) |
| 35 | #define CW_YIELD 0x2000u |
| 36 | #define SB_NONE 7u |
| 37 | #define SB_GPR 0u /* variable-latency GPR results */ |
| 38 | #define SB_RD 1u /* store source-register read barrier */ |
| 39 | #define SB_UGPR 2u /* uniform-register results (LDCU) */ |
| 40 | |
| 41 | enum { K_FIXED, K_PRED, K_VAR, K_UVAR, K_STORE, K_SYNC, K_EXIT, K_BRA }; |
| 42 | |
| 43 | #define EW_MAX_LOOPS 4 |
| 44 | typedef struct { |
| 45 | BlackwellIRProgram *p; |
| 46 | uint32_t pending; /* scoreboards set and not yet waited on */ |
| 47 | int err; |
| 48 | int loop_start[EW_MAX_LOOPS], loop_end[EW_MAX_LOOPS]; |
| 49 | int n_loops; |
| 50 | int open_start[EW_MAX_LOOPS]; /* starts of loops begun and not yet ended (nesting stack) */ |
| 51 | int depth; |
| 52 | } Em; |
| 53 | |
| 54 | static void emit(Em *e, BlackwellIRInsn in, int kind) { |
| 55 | uint32_t wait = e->pending; |
| 56 | switch (kind) { |
| 57 | case K_FIXED: in.control = CW(6, SB_NONE, SB_NONE, wait); e->pending = 0; break; |
| 58 | case K_PRED: in.control = CW(14, SB_NONE, SB_NONE, wait); e->pending = 0; break; |
| 59 | case K_VAR: in.control = CW(4, SB_GPR, SB_NONE, wait) | CW_YIELD; e->pending = 1u << SB_GPR; break; |
| 60 | case K_UVAR: in.control = CW(4, SB_UGPR, SB_NONE, wait) | CW_YIELD; e->pending = 1u << SB_UGPR; break; |
| 61 | case K_STORE: in.control = CW(4, SB_NONE, SB_RD, wait) | CW_YIELD; e->pending = 1u << SB_RD; break; |
| 62 | case K_SYNC: in.control = CW(6, SB_NONE, SB_NONE, wait); e->pending = 0; break; |
| 63 | /* a predicated EXIT or BRA that falls through must not be the only waiter */ |
| 64 | case K_EXIT: in.control = CW(5, SB_NONE, SB_NONE, wait); e->pending = in.predicate_p0 ? wait : 0; break; |
| 65 | case K_BRA: in.control = CW(5, SB_NONE, SB_NONE, wait); e->pending = in.predicate_p0 ? wait : 0; break; |
| 66 | default: e->err = -1; return; |
| 67 | } |
| 68 | if (omega_bw_ir_append(e->p, &in) < 0) e->err = -1; /* append returns the index */ |
| 69 | } |
| 70 | |
| 71 | static int V(Em *e) { int v = omega_bw_ir_alloc_vreg(e->p); if (v < 0) e->err = -1; return v; } |
| 72 | static int V64(Em *e) { int v = omega_bw_ir_alloc_vreg64(e->p); if (v < 0) e->err = -1; return v; } |
| 73 | static int UV64(Em *e) { int v = omega_bw_ir_alloc_uvreg64(e->p); if (v < 0) e->err = -1; return v; } |
| 74 | static int here(const Em *e) { return (int)e->p->count; } |
| 75 | |
| 76 | #define NOREG -1 |
| 77 | static BlackwellIRInsn I0(BlackwellIROpcode op) { |
| 78 | BlackwellIRInsn in; memset(&in, 0, sizeof in); |
| 79 | in.op = op; in.dst_vreg = NOREG; in.src1_vreg = NOREG; in.src2_vreg = NOREG; in.src3_vreg = NOREG; in.ureg = NOREG; |
| 80 | return in; |
| 81 | } |
| 82 | static void ldcu64(Em *e, int udst, uint32_t off) { BlackwellIRInsn i = I0(BW_IR_LDCU64); i.dst_vreg = udst; i.imm = off; i.is_uniform = true; emit(e, i, K_UVAR); } |
| 83 | static void ldc64(Em *e, int dst64, uint32_t off) { BlackwellIRInsn i = I0(BW_IR_LDC64); i.dst_vreg = dst64; i.imm = off; emit(e, i, K_VAR); } |
| 84 | static void ldc32(Em *e, int dst, uint32_t off) { BlackwellIRInsn i = I0(BW_IR_LDC); i.dst_vreg = dst; i.imm = off; emit(e, i, K_VAR); } |
| 85 | static void s2r(Em *e, int dst, uint32_t sr) { BlackwellIRInsn i = I0(BW_IR_S2R); i.dst_vreg = dst; i.imm = sr; emit(e, i, K_VAR); } |
| 86 | static void movi(Em *e, int dst, uint32_t imm) { BlackwellIRInsn i = I0(BW_IR_MOV_IMM); i.dst_vreg = dst; i.imm = imm; emit(e, i, K_FIXED); } |
| 87 | static void movf(Em *e, int dst, float f) { uint32_t u; memcpy(&u, &f, 4); movi(e, dst, u); } |
| 88 | static void movrz(Em *e, int dst) { BlackwellIRInsn i = I0(BW_IR_MOV_RZ); i.dst_vreg = dst; emit(e, i, K_FIXED); } |
| 89 | /* dst = a * imm + c (c may be NOREG for RZ) */ |
| 90 | static void imadi(Em *e, int dst, int a, uint32_t imm, int c) { BlackwellIRInsn i = I0(BW_IR_IMAD); i.dst_vreg = dst; i.src1_vreg = a; i.imm = imm; i.src3_vreg = c; emit(e, i, K_FIXED); } |
| 91 | /* dst = a * b + c (registers) */ |
| 92 | static void imadr(Em *e, int dst, int a, int b, int c) { BlackwellIRInsn i = I0(BW_IR_IMAD); i.dst_vreg = dst; i.src1_vreg = a; i.src2_vreg = b; i.src3_vreg = c; emit(e, i, K_FIXED); } |
| 93 | static void iadd3(Em *e, int dst, int a, int b) { BlackwellIRInsn i = I0(BW_IR_IADD3); i.dst_vreg = dst; i.src1_vreg = a; i.src2_vreg = b; emit(e, i, K_FIXED); } |
| 94 | static void lop3xor(Em *e, int dst, int a, int b) { BlackwellIRInsn i = I0(BW_IR_LOP3_XOR); i.dst_vreg = dst; i.src1_vreg = a; i.src2_vreg = b; emit(e, i, K_FIXED); } |
| 95 | /* dst64 = idx * 4 + ptr64 (byte address of element idx) */ |
| 96 | static void addr4(Em *e, int dst64, int idx, int ptr64) { BlackwellIRInsn i = I0(BW_IR_IMAD_WIDE); i.dst_vreg = dst64; i.src1_vreg = idx; i.imm = 4; i.src3_vreg = ptr64; emit(e, i, K_FIXED); } |
| 97 | static void ldg(Em *e, int dst, int addr64, int udesc) { BlackwellIRInsn i = I0(BW_IR_LDG_E); i.dst_vreg = dst; i.src1_vreg = addr64; i.ureg = udesc; emit(e, i, K_VAR); } |
| 98 | /* Independent loads (LDG_E from global, or LDS32 from shared memory with ureg NOREG) issued |
| 99 | * back to back (campaign C2a, 2026-10-05). The first load |
| 100 | * waits on whatever is pending; the others wait on nothing, so all n are in flight together. |
| 101 | * Every load sets SB_GPR (a counting scoreboard), so the next instruction waits once for all |
| 102 | * of them instead of once per load. Fails the build if a load address is a destination of the |
| 103 | * group or a destination repeats (the later loads would read an operand still in flight). */ |
| 104 | static void ld_group(Em *e, BlackwellIROpcode op, int n, const int *dst, const int *addr, int ureg) { |
| 105 | for (int i = 0; i < n; i++) |
| 106 | for (int k = 0; k < n; k++) |
| 107 | if (addr[i] == dst[k] || (k != i && dst[i] == dst[k])) { e->err = -1; return; } |
| 108 | for (int i = 0; i < n; i++) { |
| 109 | if (op != BW_IR_LDG_E && op != BW_IR_LDS32) { e->err = -1; return; } |
| 110 | BlackwellIRInsn in = I0(op); in.dst_vreg = dst[i]; in.src1_vreg = addr[i]; in.ureg = ureg; |
| 111 | if (i == 0) { emit(e, in, K_VAR); continue; } |
| 112 | in.control = CW(4, SB_GPR, SB_NONE, 0) | CW_YIELD; /* nonzero, so the encoder keeps it */ |
| 113 | if (omega_bw_ir_append(e->p, &in) < 0) e->err = -1; |
| 114 | } |
| 115 | } |
| 116 | static void stg(Em *e, int addr64, int val, int udesc) { BlackwellIRInsn i = I0(BW_IR_STG_E); i.src1_vreg = addr64; i.src2_vreg = val; i.ureg = udesc; emit(e, i, K_STORE); } |
| 117 | static void fadd(Em *e, int d, int a, int b) { BlackwellIRInsn i = I0(BW_IR_FADD); i.dst_vreg = d; i.src1_vreg = a; i.src2_vreg = b; emit(e, i, K_FIXED); } |
| 118 | static void fsub(Em *e, int d, int a, int b) { BlackwellIRInsn i = I0(BW_IR_FSUB); i.dst_vreg = d; i.src1_vreg = a; i.src2_vreg = b; emit(e, i, K_FIXED); } |
| 119 | static void fmul(Em *e, int d, int a, int b) { BlackwellIRInsn i = I0(BW_IR_FMUL); i.dst_vreg = d; i.src1_vreg = a; i.src2_vreg = b; emit(e, i, K_FIXED); } |
| 120 | static void ffma(Em *e, int d, int a, int b, int c) { BlackwellIRInsn i = I0(BW_IR_FFMA); i.dst_vreg = d; i.src1_vreg = a; i.src2_vreg = b; i.src3_vreg = c; emit(e, i, K_FIXED); } |
| 121 | static void mufu(Em *e, BlackwellIROpcode op, int d, int a) { BlackwellIRInsn i = I0(op); i.dst_vreg = d; i.src1_vreg = a; emit(e, i, K_VAR); } |
| 122 | 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); } |
| 123 | static void sts32(Em *e, int addr, int val) { BlackwellIRInsn i = I0(BW_IR_STS32); i.src1_vreg = addr; i.src2_vreg = val; emit(e, i, K_STORE); } |
| 124 | static 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); } |
| 125 | static void bar_sync(Em *e) { emit(e, I0(BW_IR_BAR_SYNC), K_SYNC); } |
| 126 | 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); } |
| 127 | static void exit_if_p0(Em *e) { BlackwellIRInsn i = I0(BW_IR_EXIT); i.predicate_p0 = true; emit(e, i, K_EXIT); } |
| 128 | static void bra_back_if_not_p0(Em *e, int target) { BlackwellIRInsn i = I0(BW_IR_BRA); i.predicate_p0 = true; i.predicate_not = true; i.imm = (uint32_t)(target - here(e)); emit(e, i, K_BRA); } |
| 129 | static void tail(Em *e) { |
| 130 | emit(e, I0(BW_IR_EXIT), K_EXIT); |
| 131 | BlackwellIRInsn self = I0(BW_IR_BRA); /* the self-branch every kernel here ends with */ |
| 132 | self.control = 0x000fc000u; |
| 133 | if (omega_bw_ir_append(e->p, &self) < 0) e->err = -1; |
| 134 | } |
| 135 | /* Loops may nest (prime race cut): begin pushes the start, end pops it, so the inner |
| 136 | * loop is closed (and its liveness fixed) first. For the single-level loops above this |
| 137 | * is exactly the old behaviour. */ |
| 138 | static void loop_begin(Em *e) { if (e->depth >= EW_MAX_LOOPS) { e->err = -1; return; } e->open_start[e->depth++] = here(e); } |
| 139 | /* Closing a loop: every value defined before the loop and read inside it stays |
| 140 | * live until the back-branch, otherwise the linear-scan allocator (which knows |
| 141 | * nothing about back-edges) would hand its register to a body temporary whose |
| 142 | * first definition comes after the value's last textual use (e.g. the scale y |
| 143 | * in rmsnorm pass 2), and the next iteration would read garbage. */ |
| 144 | static void loop_end(Em *e) { |
| 145 | if (e->n_loops >= EW_MAX_LOOPS || e->depth <= 0) { e->err = -1; return; } |
| 146 | int start = e->open_start[--e->depth], end = here(e) - 1; |
| 147 | e->loop_start[e->n_loops] = start; |
| 148 | e->loop_end[e->n_loops] = end; |
| 149 | e->n_loops++; |
| 150 | OmegaRegAlloc *ra = &e->p->regalloc; |
| 151 | for (int v = 0; v < ra->num_vregs; v++) { |
| 152 | OmegaLiveInterval *iv = &ra->intervals[v]; |
| 153 | if (iv->first_def >= 0 && iv->first_def < start && iv->last_use >= start && iv->last_use < end) iv->last_use = end; |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | /* Kernel argument words (constant bank 0, same slots as the matmul path): |
| 158 | * 0x380 a, 0x388 b, 0x390 c (64-bit each), then four 32-bit parameters. */ |
| 159 | #define ARG_A 0x380u |
| 160 | #define ARG_B 0x388u |
| 161 | #define ARG_C 0x390u |
| 162 | #define ARG_P0 0x398u |
| 163 | #define ARG_P1 0x39cu |
| 164 | #define ARG_P2 0x3a0u |
| 165 | #define ARG_P3 0x3a4u |
| 166 | #define ARG_P4 0x3a8u |
| 167 | #define DESC_OFF 0x358u |
| 168 | |
| 169 | typedef struct { int udesc, pa, pb, pc, tid, cta; } Pro; |
| 170 | static Pro prologue(Em *e) { |
| 171 | Pro r; |
| 172 | r.udesc = UV64(e); r.pa = V64(e); r.pb = V64(e); r.pc = V64(e); r.tid = V(e); r.cta = V(e); |
| 173 | ldcu64(e, r.udesc, DESC_OFF); |
| 174 | ldc64(e, r.pa, ARG_A); ldc64(e, r.pb, ARG_B); ldc64(e, r.pc, ARG_C); |
| 175 | s2r(e, r.tid, BW_SR_TID_X); s2r(e, r.cta, BW_SR_CTAID_X); |
| 176 | return r; |
| 177 | } |
| 178 | |
| 179 | /* out[i] = 2^a[i] for i < n (n = P0). Threads 128, grid ceil(n/128). Mutant: 2^(a+1). */ |
| 180 | static void gen_ex2(Em *e, int mutant) { |
| 181 | Pro p = prologue(e); |
| 182 | int n = V(e), i = V(e), off = V64(e), x = V(e), y = V(e); |
| 183 | ldc32(e, n, ARG_P0); |
| 184 | imadi(e, i, p.cta, OMEGA_GPU_EW_THREADS, p.tid); |
| 185 | isetp_ge_u32(e, i, n); |
| 186 | exit_if_p0(e); |
| 187 | addr4(e, off, i, p.pa); |
| 188 | ldg(e, x, off, p.udesc); |
| 189 | if (mutant) { int one = V(e); movf(e, one, 1.0f); fadd(e, x, x, one); } |
| 190 | mufu(e, BW_IR_MUFU_EX2, y, x); |
| 191 | addr4(e, off, i, p.pc); |
| 192 | stg(e, off, y, p.udesc); |
| 193 | tail(e); |
| 194 | } |
| 195 | |
| 196 | /* out[i] = a[i ^ 127] within each CTA of 128 (n % 128 == 0). STS own word, BAR.SYNC, |
| 197 | * LDS the partner word. Mutant: LDS own word (no exchange). */ |
| 198 | static void gen_xchg(Em *e, int mutant) { |
| 199 | Pro p = prologue(e); |
| 200 | int i = V(e), off = V64(e), x = V(e), saddr = V(e), paddr = V(e), mask = V(e), y = V(e); |
| 201 | imadi(e, i, p.cta, OMEGA_GPU_EW_THREADS, p.tid); |
| 202 | addr4(e, off, i, p.pa); |
| 203 | ldg(e, x, off, p.udesc); |
| 204 | imadi(e, saddr, p.tid, 4, NOREG); |
| 205 | sts32(e, saddr, x); |
| 206 | bar_sync(e); |
| 207 | if (mutant) { |
| 208 | lds32(e, y, saddr); |
| 209 | } else { |
| 210 | movi(e, mask, OMEGA_GPU_EW_THREADS - 1); |
| 211 | lop3xor(e, paddr, p.tid, mask); |
| 212 | imadi(e, paddr, paddr, 4, NOREG); |
| 213 | lds32(e, y, paddr); |
| 214 | } |
| 215 | addr4(e, off, i, p.pc); |
| 216 | stg(e, off, y, p.udesc); |
| 217 | tail(e); |
| 218 | } |
| 219 | |
| 220 | /* One CTA of 128 threads per row. P0 = dim/128 (loop count), P1 = bits(1/dim), |
| 221 | * P2 = bits(eps), P3 = dim. Mutant: the warp tree drops the last SHFL step. */ |
| 222 | static void gen_rmsnorm(Em *e, int mutant) { |
| 223 | Pro p = prologue(e); |
| 224 | int nk = V(e), invd = V(e), eps = V(e), dim = V(e); |
| 225 | int rowbase = V(e), idx = V(e), col = V(e), k = V(e), acc = V(e), cT = V(e), c1 = V(e); |
| 226 | int off = V64(e), x = V(e), t = V(e); |
| 227 | ldc32(e, nk, ARG_P0); ldc32(e, invd, ARG_P1); ldc32(e, eps, ARG_P2); ldc32(e, dim, ARG_P3); |
| 228 | imadr(e, rowbase, p.cta, dim, NOREG); |
| 229 | iadd3(e, idx, rowbase, p.tid); |
| 230 | movrz(e, acc); movrz(e, k); |
| 231 | movi(e, cT, OMEGA_GPU_EW_THREADS); movi(e, c1, 1); |
| 232 | loop_begin(e); |
| 233 | int l1 = here(e); |
| 234 | addr4(e, off, idx, p.pa); |
| 235 | ldg(e, x, off, p.udesc); |
| 236 | ffma(e, acc, x, x, acc); |
| 237 | iadd3(e, idx, idx, cT); |
| 238 | iadd3(e, k, k, c1); |
| 239 | isetp_ge_u32(e, k, nk); |
| 240 | bra_back_if_not_p0(e, l1); |
| 241 | loop_end(e); |
| 242 | static const uint32_t steps[5] = { 16, 8, 4, 2, 1 }; |
| 243 | for (int s = 0; s < (mutant ? 4 : 5); s++) { shfl_down(e, t, acc, steps[s]); fadd(e, acc, acc, t); } |
| 244 | int saddr = V(e); |
| 245 | imadi(e, saddr, p.tid, 4, NOREG); |
| 246 | sts32(e, saddr, acc); |
| 247 | bar_sync(e); |
| 248 | int a0 = V(e), a1 = V(e), a2 = V(e), a3 = V(e), s0 = V(e), s1 = V(e), s2 = V(e), s3 = V(e), sum = V(e); |
| 249 | movrz(e, a0); movi(e, a1, 128); movi(e, a2, 256); movi(e, a3, 384); |
| 250 | lds32(e, s0, a0); lds32(e, s1, a1); lds32(e, s2, a2); lds32(e, s3, a3); |
| 251 | fadd(e, sum, s0, s1); fadd(e, sum, sum, s2); fadd(e, sum, sum, s3); |
| 252 | int v = V(e), y = V(e), u = V(e), cmh = V(e), c15 = V(e); |
| 253 | fmul(e, v, sum, invd); |
| 254 | fadd(e, v, v, eps); |
| 255 | mufu(e, BW_IR_MUFU_RSQ, y, v); |
| 256 | movf(e, cmh, -0.5f); movf(e, c15, 1.5f); |
| 257 | fmul(e, u, v, y); fmul(e, u, u, y); ffma(e, u, u, cmh, c15); fmul(e, y, y, u); |
| 258 | int offw = V64(e), offc = V64(e), w = V(e), o = V(e); |
| 259 | iadd3(e, idx, rowbase, p.tid); |
| 260 | imadi(e, col, p.tid, 1, NOREG); /* col = tid (the encoder maps a missing src2 to R0, not RZ, so no IADD3 here) */ |
| 261 | movrz(e, k); |
| 262 | loop_begin(e); |
| 263 | int l2 = here(e); |
| 264 | addr4(e, off, idx, p.pa); |
| 265 | ldg(e, x, off, p.udesc); |
| 266 | addr4(e, offw, col, p.pb); |
| 267 | ldg(e, w, offw, p.udesc); |
| 268 | fmul(e, o, x, y); |
| 269 | fmul(e, o, o, w); |
| 270 | addr4(e, offc, idx, p.pc); |
| 271 | stg(e, offc, o, p.udesc); |
| 272 | iadd3(e, idx, idx, cT); |
| 273 | iadd3(e, col, col, cT); |
| 274 | iadd3(e, k, k, c1); |
| 275 | isetp_ge_u32(e, k, nk); |
| 276 | bra_back_if_not_p0(e, l2); |
| 277 | loop_end(e); |
| 278 | tail(e); |
| 279 | } |
| 280 | |
| 281 | /* One CTA of head_dim/2 threads per head. P0 = head_dim, P1 = head_dim/2. |
| 282 | * a = vector, b = table (cos in the first half of each head, sin in the second). |
| 283 | * Mutant: out0 = q0*cos + q1*sin. */ |
| 284 | static void gen_rope(Em *e, int mutant) { |
| 285 | Pro p = prologue(e); |
| 286 | int hd = V(e), half = V(e), i0 = V(e), i1 = V(e), off0 = V64(e), off1 = V64(e); |
| 287 | int q0 = V(e), q1 = V(e), c = V(e), s = V(e), t0 = V(e), t1 = V(e), o0 = V(e), o1 = V(e); |
| 288 | ldc32(e, hd, ARG_P0); ldc32(e, half, ARG_P1); |
| 289 | imadr(e, i0, p.cta, hd, p.tid); |
| 290 | iadd3(e, i1, i0, half); |
| 291 | addr4(e, off0, i0, p.pa); ldg(e, q0, off0, p.udesc); |
| 292 | addr4(e, off1, i1, p.pa); ldg(e, q1, off1, p.udesc); |
| 293 | addr4(e, off0, i0, p.pb); ldg(e, c, off0, p.udesc); |
| 294 | addr4(e, off1, i1, p.pb); ldg(e, s, off1, p.udesc); |
| 295 | fmul(e, t0, q0, c); fmul(e, t1, q1, s); |
| 296 | if (mutant) fadd(e, o0, t0, t1); else fsub(e, o0, t0, t1); |
| 297 | fmul(e, t0, q0, s); fmul(e, t1, q1, c); |
| 298 | fadd(e, o1, t0, t1); |
| 299 | addr4(e, off0, i0, p.pc); stg(e, off0, o0, p.udesc); |
| 300 | addr4(e, off1, i1, p.pc); stg(e, off1, o1, p.udesc); |
| 301 | tail(e); |
| 302 | } |
| 303 | |
| 304 | /* out[i] = g/(1+2^(g*-log2e)) * up for i < n (P0). Mutant: 2^(g*+log2e). */ |
| 305 | static void gen_swiglu(Em *e, int mutant) { |
| 306 | Pro p = prologue(e); |
| 307 | int n = V(e), i = V(e), off = V64(e), g = V(e), u = V(e), k = V(e), t = V(e), ex = V(e), d = V(e), r = V(e), one = V(e), o = V(e); |
| 308 | ldc32(e, n, ARG_P0); |
| 309 | imadi(e, i, p.cta, OMEGA_GPU_EW_THREADS, p.tid); |
| 310 | isetp_ge_u32(e, i, n); |
| 311 | exit_if_p0(e); |
| 312 | addr4(e, off, i, p.pa); ldg(e, g, off, p.udesc); |
| 313 | addr4(e, off, i, p.pb); ldg(e, u, off, p.udesc); |
| 314 | movf(e, k, mutant ? 1.4426950408889634f : -1.4426950408889634f); |
| 315 | movf(e, one, 1.0f); |
| 316 | fmul(e, t, g, k); |
| 317 | mufu(e, BW_IR_MUFU_EX2, ex, t); |
| 318 | fadd(e, d, ex, one); |
| 319 | mufu(e, BW_IR_MUFU_RCP, r, d); |
| 320 | fmul(e, o, g, r); |
| 321 | fmul(e, o, o, u); |
| 322 | addr4(e, off, i, p.pc); |
| 323 | stg(e, off, o, p.udesc); |
| 324 | tail(e); |
| 325 | } |
| 326 | |
| 327 | /* ------------------------------------------- prime race sieve (2026-10-05) */ |
| 328 | static void lop3lut(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 & 0xffu; emit(e, i, K_FIXED); } |
| 329 | static void shl_r(Em *e, int d, int a, int n) { BlackwellIRInsn i = I0(BW_IR_SHF_L_U32); i.dst_vreg = d; i.src1_vreg = a; i.src2_vreg = n; emit(e, i, K_FIXED); } |
| 330 | static void shr_i(Em *e, int d, int a, uint32_t n) { BlackwellIRInsn i = I0(BW_IR_SHF_R); i.dst_vreg = d; i.src1_vreg = a; i.imm = n; emit(e, i, K_FIXED); } |
| 331 | static void imadhi(Em *e, int d, int a, int b) { BlackwellIRInsn i = I0(BW_IR_IMAD_HI_U32); i.dst_vreg = d; i.src1_vreg = a; i.src2_vreg = b; emit(e, i, K_FIXED); } |
| 332 | /* dst64 = a * imm + c64 (IMAD.WIDE.U32 immediate form) */ |
| 333 | static void widei(Em *e, int dst64, int a, uint32_t imm, int c64) { BlackwellIRInsn i = I0(BW_IR_IMAD_WIDE); i.dst_vreg = dst64; i.src1_vreg = a; i.imm = imm; i.src3_vreg = c64; emit(e, i, K_FIXED); } |
| 334 | /* @P0 BRA forward; returns the instruction index for bra_patch_here */ |
| 335 | static int bra_fwd_if_p0(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; } |
| 336 | static void bra_patch_here(Em *e, int at) { if (e->err == 0 && at >= 0 && at < here(e)) e->p->insns[at].imm = (uint32_t)(here(e) - at); else e->err = -1; } |
| 337 | static void bra_always(Em *e, int target) { BlackwellIRInsn i = I0(BW_IR_BRA); i.imm = (uint32_t)(target - here(e)); emit(e, i, K_BRA); } |
| 338 | |
| 339 | /* Word-parallel odd-only sieve (docs: handoff 2026-10-05 DESIGN "GPU kernel (native)"). |
| 340 | * Thread t owns 32-bit output words w = t, t + S, t + 2S, ... (S = P1 = threads launched; |
| 341 | * the I42 envelope caps a launch at OMEGA_GPU_EW_MAX_CTAS CTAs, so a grid-stride loop |
| 342 | * replaces one thread per word). Every word has exactly one writer; no atomics. |
| 343 | * Bit j of word w is the odd number 2(32w + j) + 1. |
| 344 | * Arguments: a (0x380) = prime table, entries {p, magic = ceil(2^32 / p), k0 = (p*p - 1)/2} |
| 345 | * (12 bytes, ascending p, odd primes <= isqrt(limit)) followed by ONE sentinel entry with |
| 346 | * k0 = 0xffffffff that ends every scan; c (0x390) = output words; |
| 347 | * P0 = nwords, P1 = S, P2 = last word index, P3 = ~tailmask (bits past the last odd). |
| 348 | * Range: limit < 2^32 (so base, k0 < 2^31 and the sign-bit tricks below hold). |
| 349 | * Per word: base = 32w; for each prime while k0 < base + 32: |
| 350 | * d = base - k0; neg = d >> 31 (k0 > base); dpos = neg ? 0 : d; jadd = neg ? k0 - base : 0 |
| 351 | * q = hi32(dpos * magic) (= floor(dpos/p) or one more: magic is rounded up) |
| 352 | * r = dpos - q*p (q one too big -> r = (dpos mod p) - p, wrapped) |
| 353 | * j = p - r; j -= p * ((r - 1) >> 31) (r == 0 -> 0; wrapped r -> p - (dpos mod p)) |
| 354 | * One fix covers both cases: with q one too big, dpos mod p >= p - dpos*e/2^32 > p/2 > 0 |
| 355 | * (e = magic*p - 2^32 < p, dpos < 2^31), so the true remainder is never 0 there and |
| 356 | * p - r - p = p - (dpos mod p) is the right offset. A separate "r += p if wrapped" step was |
| 357 | * removed after a negative control showed it changes no output (host sim, limit 1e7, where |
| 358 | * the overshoot occurs 268 times; bench/prime_race/tests magic_model). |
| 359 | * j += jadd; while j < 32 { acc |= 1 << j; j += p } |
| 360 | * then acc |= (w == 0) (the number 1), word = ~(acc | (w == last ? ~tailmask : 0)). |
| 361 | * Branch-free except the loop branches. Mutant: drops the j fix (j = p - r unreduced), so words where |
| 362 | * base - k0 is a multiple of p (or q overshot) mark the wrong bits. |
| 363 | * |
| 364 | * staged = 1 (OMEGA_GPU_EW_PRIME_SIEVE_SHARED, campaign C3): P4 = table words, sentinel included |
| 365 | * (3 per entry). The CTA first copies the table into shared memory at offset 0 (thread t copies |
| 366 | * words t, t + 128, ...), BAR_SYNC, then the prime loop reads shared memory (LDS) instead of |
| 367 | * uncached global memory. Every thread reaches the barrier: the w >= nwords exit comes after |
| 368 | * it. The launch must declare at least 4 * P4 bytes of shared memory. */ |
| 369 | static void gen_prime_sieve(Em *e, int mutant, int staged) { |
| 370 | Pro pr = prologue(e); |
| 371 | int nwords = V(e), stride = V(e), lastw = V(e), tailinv = V(e); |
| 372 | int one = V(e), c32 = V(e), cm1 = V(e), w = V(e); |
| 373 | ldc32(e, nwords, ARG_P0); ldc32(e, stride, ARG_P1); ldc32(e, lastw, ARG_P2); ldc32(e, tailinv, ARG_P3); |
| 374 | movi(e, one, 1); movi(e, c32, 32); movi(e, cm1, 0xffffffffu); |
| 375 | int c4 = -1, c8 = -1, c12 = -1; |
| 376 | if (staged) { |
| 377 | int twords = V(e), i = V(e), ga = V64(e), val = V(e), sa = V(e); |
| 378 | c4 = V(e); c8 = V(e); c12 = V(e); |
| 379 | ldc32(e, twords, ARG_P4); |
| 380 | movi(e, c4, 4); movi(e, c8, 8); movi(e, c12, 12); |
| 381 | imadi(e, i, pr.tid, 1, NOREG); |
| 382 | loop_begin(e); /* staging loop */ |
| 383 | int ls = here(e); |
| 384 | isetp_ge_u32(e, i, twords); |
| 385 | int sdone = bra_fwd_if_p0(e); |
| 386 | addr4(e, ga, i, pr.pa); |
| 387 | ldg(e, val, ga, pr.udesc); |
| 388 | imadi(e, sa, i, 4, NOREG); |
| 389 | sts32(e, sa, val); |
| 390 | imadi(e, i, one, OMEGA_GPU_EW_THREADS, i); |
| 391 | bra_always(e, ls); |
| 392 | loop_end(e); |
| 393 | bra_patch_here(e, sdone); |
| 394 | bar_sync(e); |
| 395 | } |
| 396 | imadi(e, w, pr.cta, OMEGA_GPU_EW_THREADS, pr.tid); |
| 397 | isetp_ge_u32(e, w, nwords); |
| 398 | exit_if_p0(e); |
| 399 | |
| 400 | /* ptr, a64, a64m: global addresses, or (staged) 32-bit shared offsets */ |
| 401 | int base = V(e), basep32 = V(e), acc = V(e); |
| 402 | int ptr = staged ? V(e) : V64(e), a64 = staged ? V(e) : V64(e), a64m = staged ? V(e) : V64(e); |
| 403 | int p = V(e), magic = V(e), k0 = V(e), negp = V(e), d = V(e), neg = V(e), nd = V(e), dpos = V(e), jadd = V(e); |
| 404 | int q = V(e), r = V(e), j = V(e), t = V(e), z = V(e), bit = V(e); |
| 405 | int e1 = V(e), m = V(e), word = V(e), oaddr = V64(e); |
| 406 | int tt = V(e), jj = V(e), sh = V(e), c31 = V(e), cm32 = V(e); |
| 407 | movi(e, c31, 31); movi(e, cm32, 0xffffffe0u); |
| 408 | |
| 409 | loop_begin(e); /* word loop */ |
| 410 | int lw = here(e); |
| 411 | imadi(e, base, w, 32, NOREG); |
| 412 | iadd3(e, basep32, base, c32); |
| 413 | movrz(e, acc); |
| 414 | if (staged) movrz(e, ptr); else widei(e, ptr, one, 0, pr.pa); |
| 415 | |
| 416 | loop_begin(e); /* prime loop */ |
| 417 | int lp = here(e); |
| 418 | /* one memory round trip per prime: all three entry words load together (C2a). The |
| 419 | * sentinel entry is a whole entry, so loading p and magic before the k0 test is in bounds. */ |
| 420 | if (staged) { iadd3(e, a64, ptr, c8); iadd3(e, a64m, ptr, c4); } |
| 421 | else { widei(e, a64, one, 8, ptr); widei(e, a64m, one, 4, ptr); } |
| 422 | { |
| 423 | const int dsts[3] = { k0, p, magic }, adrs[3] = { a64, ptr, a64m }; |
| 424 | if (staged) ld_group(e, BW_IR_LDS32, 3, dsts, adrs, NOREG); |
| 425 | else ld_group(e, BW_IR_LDG_E, 3, dsts, adrs, pr.udesc); |
| 426 | } |
| 427 | isetp_ge_u32(e, k0, basep32); |
| 428 | int brk = bra_fwd_if_p0(e); |
| 429 | imadi(e, negp, p, 0xffffffffu, NOREG); /* -p */ |
| 430 | imadi(e, d, k0, 0xffffffffu, base); /* base - k0 */ |
| 431 | shr_i(e, neg, d, 31); |
| 432 | imadi(e, nd, d, 0xffffffffu, NOREG); /* -d */ |
| 433 | imadr(e, dpos, neg, nd, d); /* d - neg*d */ |
| 434 | imadr(e, jadd, neg, nd, NOREG); /* neg * (k0 - base) */ |
| 435 | imadhi(e, q, dpos, magic); |
| 436 | imadr(e, r, q, negp, dpos); /* dpos - q*p */ |
| 437 | imadi(e, j, r, 0xffffffffu, p); /* p - r */ |
| 438 | if (!mutant) { |
| 439 | iadd3(e, t, r, cm1); |
| 440 | shr_i(e, z, t, 31); /* r == 0, or r wrapped (q one too big) */ |
| 441 | imadr(e, j, z, negp, j); /* j - p */ |
| 442 | } |
| 443 | iadd3(e, j, j, jadd); |
| 444 | |
| 445 | /* mark loop (campaign C5): the trip count depends only on p, so every active lane runs the |
| 446 | * same ceil(32/p) steps and the warp never splits here (all active lanes are on the same |
| 447 | * prime). Bits past this word are masked out instead of branched around. The old loop ran |
| 448 | * "while j < 32", a lane-dependent branch with no reconvergence point. */ |
| 449 | movrz(e, tt); |
| 450 | loop_begin(e); |
| 451 | int li = here(e); |
| 452 | isetp_ge_u32(e, tt, c32); |
| 453 | int done = bra_fwd_if_p0(e); |
| 454 | iadd3(e, jj, j, tt); |
| 455 | lop3lut(e, sh, jj, c31, NOREG, 0xc0); /* jj & 31 */ |
| 456 | shl_r(e, bit, one, sh); |
| 457 | iadd3(e, t, jj, cm32); |
| 458 | shr_i(e, z, t, 31); /* jj < 32 (j <= p + 31, far below 2^31) */ |
| 459 | imadi(e, m, z, 0xffffffffu, NOREG); /* all ones when jj < 32, else 0 */ |
| 460 | lop3lut(e, acc, acc, bit, m, 0xf8); /* acc | (bit & m) */ |
| 461 | iadd3(e, tt, tt, p); |
| 462 | bra_always(e, li); |
| 463 | loop_end(e); |
| 464 | bra_patch_here(e, done); |
| 465 | |
| 466 | if (staged) iadd3(e, ptr, ptr, c12); else widei(e, ptr, one, 12, ptr); |
| 467 | bra_always(e, lp); |
| 468 | loop_end(e); |
| 469 | bra_patch_here(e, brk); |
| 470 | |
| 471 | iadd3(e, t, w, cm1); |
| 472 | shr_i(e, z, t, 31); /* w == 0: the number 1 is not prime */ |
| 473 | lop3lut(e, acc, acc, z, NOREG, 0xfc); |
| 474 | lop3xor(e, e1, w, lastw); |
| 475 | iadd3(e, t, e1, cm1); |
| 476 | shr_i(e, z, t, 31); /* w == last word */ |
| 477 | imadr(e, m, z, tailinv, NOREG); |
| 478 | lop3lut(e, word, acc, m, NOREG, 0x03); /* ~(acc | m) */ |
| 479 | addr4(e, oaddr, w, pr.pc); |
| 480 | stg(e, oaddr, word, pr.udesc); |
| 481 | /* IMAD, not IADD3: the IADD3 encoder ignores the scheduler control word (fixed |
| 482 | * 0x010fca00), so right after a store it would not wait on the read barrier */ |
| 483 | imadr(e, w, stride, one, w); |
| 484 | isetp_ge_u32(e, w, nwords); |
| 485 | bra_back_if_not_p0(e, lw); |
| 486 | loop_end(e); |
| 487 | tail(e); |
| 488 | } |
| 489 | |
| 490 | /* A value first defined inside a loop body may not be read after the loop |
| 491 | * before it is written again: the allocator could have let a later body |
| 492 | * temporary share its register, and the last iteration's value would be lost. */ |
| 493 | static int check_loop_invariant(const Em *e) { |
| 494 | const OmegaRegAlloc *ra = &e->p->regalloc; |
| 495 | for (int L = 0; L < e->n_loops; L++) |
| 496 | for (int v = 0; v < ra->num_vregs; v++) { |
| 497 | int fd = ra->intervals[v].first_def; |
| 498 | if (fd < e->loop_start[L] || fd > e->loop_end[L]) continue; |
| 499 | for (size_t i = (size_t)e->loop_end[L] + 1; i < e->p->count; i++) { |
| 500 | const BlackwellIRInsn *in = &e->p->insns[i]; |
| 501 | if (in->src1_vreg == v || in->src2_vreg == v || in->src3_vreg == v) return -1; |
| 502 | if (in->dst_vreg == v && !in->is_uniform) break; |
| 503 | } |
| 504 | } |
| 505 | return 0; |
| 506 | } |
| 507 | |
| 508 | #define EW_GPR_BUDGET 64u /* the QMD declares 64 registers, as the matmul launcher does */ |
| 509 | |
| 510 | /* Mesa NAK sm120_instr_latencies.rs + sm100/reg_raw.csv: ALU/dual-ALU pairs |
| 511 | * and FMA/FMA pairs need 4 + 1 cycles; mixed pairs need 5 + 1. CSV rows are |
| 512 | * READERS, columns WRITERS (see lat_rs_gen.py), not the reverse. IMAD.WIDE |
| 513 | * has operand-specific latencies and is deliberately excluded here. |
| 514 | * |
| 515 | * Retain at least 5 cycles after every fixed producer. Thus the next reader |
| 516 | * uses the pair-specific bound and later readers are >= 9 cycles away (the |
| 517 | * next instruction in a tightened pair is itself fixed). No instruction, |
| 518 | * branch target, wait, barrier assignment or register allocation is changed. |
| 519 | * IADD3's encoder still supplies its historical control word; do not pretend |
| 520 | * to schedule that producer here. Its existing 5-cycle delay is unchanged. |
| 521 | * |
| 522 | * Predicate-to-guard uses NAK paw_latency -> raw(write, None): reader |
| 523 | * RedirectedFp64, writer Dualalu = 13, plus SM120's 1 = 14, in emit above. |
| 524 | * NVIDIA's public ISA docs are silent about SASS control-word semantics. |
| 525 | */ |
| 526 | static int fixed_pair_class(BlackwellIROpcode op) { |
| 527 | switch (op) { |
| 528 | case BW_IR_MOV_RZ: case BW_IR_MOV_IMM: case BW_IR_IADD3: |
| 529 | case BW_IR_LOP3_XOR: case BW_IR_LOP3_LUT: |
| 530 | case BW_IR_SHF_R: case BW_IR_SHF_L_U32: return 1; |
| 531 | case BW_IR_IMAD: case BW_IR_IMAD_HI_U32: |
| 532 | case BW_IR_FADD: case BW_IR_FSUB: case BW_IR_FMUL: case BW_IR_FFMA: return 2; |
| 533 | default: return 0; |
| 534 | } |
| 535 | } |
| 536 | |
| 537 | static void schedule_fixed_pairs(BlackwellIRProgram *p) { |
| 538 | for (size_t i = 0; i + 1 < p->count; i++) { |
| 539 | BlackwellIRInsn *a = &p->insns[i]; |
| 540 | const BlackwellIRInsn *b = &p->insns[i + 1]; |
| 541 | int cls = fixed_pair_class(a->op); |
| 542 | if (!cls || cls != fixed_pair_class(b->op) || a->op == BW_IR_IADD3 || |
| 543 | a->predicate_p0 || b->predicate_p0 || ((a->control >> 9) & 15u) != 6) |
| 544 | continue; |
| 545 | a->control = (a->control & ~(15u << 9)) | (5u << 9); |
| 546 | } |
| 547 | } |
| 548 | |
| 549 | static int build_kernel(OmegaGpuEwOp op, int mutant, OmegaBlackwellKernel *kernel, BlackwellIRProgram *prog_out) { |
| 550 | BlackwellIRProgram *prog = calloc(1, sizeof *prog); |
| 551 | if (!prog) return OMEGA_GPU_EW_CODEGEN_FAIL; |
| 552 | omega_bw_ir_init(prog); |
| 553 | Em e; memset(&e, 0, sizeof e); e.p = prog; |
| 554 | switch (op) { |
| 555 | case OMEGA_GPU_EW_EX2: gen_ex2(&e, mutant); break; |
| 556 | case OMEGA_GPU_EW_XCHG: gen_xchg(&e, mutant); break; |
| 557 | case OMEGA_GPU_EW_RMSNORM: gen_rmsnorm(&e, mutant); break; |
| 558 | case OMEGA_GPU_EW_ROPE: gen_rope(&e, mutant); break; |
| 559 | case OMEGA_GPU_EW_SWIGLU: gen_swiglu(&e, mutant); break; |
| 560 | case OMEGA_GPU_EW_PRIME_SIEVE: gen_prime_sieve(&e, mutant, 0); break; |
| 561 | case OMEGA_GPU_EW_PRIME_SIEVE_SHARED: gen_prime_sieve(&e, mutant, 1); break; |
| 562 | default: e.err = -1; break; |
| 563 | } |
| 564 | schedule_fixed_pairs(prog); |
| 565 | int rc = OMEGA_GPU_EW_CODEGEN_FAIL; |
| 566 | int ra_rc = (e.err == 0) ? omega_bw_regalloc_solve(prog) : -1; |
| 567 | int loop_rc = (ra_rc == 0) ? check_loop_invariant(&e) : -1; |
| 568 | #ifdef OMEGA_EW_DEBUG |
| 569 | fprintf(stderr, "ew codegen op=%d mutant=%d emit_err=%d insns=%zu vregs=%d regalloc=%d loops=%d peak_gpr=%u\n", |
| 570 | (int)op, mutant, e.err, prog->count, prog->regalloc.num_vregs, ra_rc, loop_rc, prog->regalloc.peak_gpr_usage); |
| 571 | #endif |
| 572 | if (e.err == 0 && ra_rc == 0 && loop_rc == 0 && prog->regalloc.peak_gpr_usage <= EW_GPR_BUDGET) { |
| 573 | size_t max_bytes = (size_t)BW_MAX_IR_INSNS * 16; |
| 574 | memset(kernel, 0, sizeof *kernel); |
| 575 | kernel->code = malloc(max_bytes); |
| 576 | size_t emitted = 0; |
| 577 | if (kernel->code && omega_bw_encode_program(prog, kernel->code, max_bytes, &emitted) == 0) { |
| 578 | kernel->code_size = emitted; |
| 579 | kernel->insn_count = emitted / 16; |
| 580 | kernel->gpr_count = prog->regalloc.peak_gpr_usage; |
| 581 | kernel->uniform_gpr_count = prog->regalloc.peak_ugpr_usage; |
| 582 | sha256_hash(kernel->code, kernel->code_size, kernel->code_digest); |
| 583 | if (prog_out) *prog_out = *prog; |
| 584 | rc = OMEGA_GPU_EW_OK; |
| 585 | } else { |
| 586 | #ifdef OMEGA_EW_DEBUG |
| 587 | fprintf(stderr, "ew codegen op=%d: encode failed (an instruction form the encoder refuses)\n", (int)op); |
| 588 | #endif |
| 589 | free(kernel->code); kernel->code = NULL; |
| 590 | } |
| 591 | } |
| 592 | free(prog); |
| 593 | return rc; |
| 594 | } |
| 595 | |
| 596 | /* --------------------------------------------------------- kernel cache */ |
| 597 | |
| 598 | /* One kernel per (op, mutant); the device copy is uploaded once and stays |
| 599 | * resident for the process (cut 4b: no per-call code page). */ |
| 600 | typedef struct { int used; int op; int mutant; OmegaBlackwellKernel kernel; NvrmMem code; } Slot; |
| 601 | static int g_mutant_op; /* 0 = none */ |
| 602 | static Slot g_cache[8]; |
| 603 | static OmegaGpuScratch g_a, g_b, g_c; /* staging, grown to the high-water mark, reused per call */ |
| 604 | static bool g_hooked; |
| 605 | #define LOCK() omega_gpu_session_lock() |
| 606 | #define UNLOCK() omega_gpu_session_unlock() |
| 607 | |
| 608 | static void cache_clear_locked(void) { |
| 609 | for (size_t i = 0; i < sizeof g_cache / sizeof g_cache[0]; i++) |
| 610 | if (g_cache[i].used) { free(g_cache[i].kernel.code); omega_gpu_session_free(&g_cache[i].code); memset(&g_cache[i], 0, sizeof g_cache[i]); } |
| 611 | } |
| 612 | |
| 613 | void omega_gpu_elementwise_cache_clear(void) { |
| 614 | LOCK(); |
| 615 | cache_clear_locked(); |
| 616 | UNLOCK(); |
| 617 | } |
| 618 | |
| 619 | /* Called by the session (lock held, device still open) right before it closes. */ |
| 620 | static void on_session_close(void) { |
| 621 | cache_clear_locked(); |
| 622 | omega_gpu_session_scratch_free(&g_a); omega_gpu_session_scratch_free(&g_b); omega_gpu_session_scratch_free(&g_c); |
| 623 | } |
| 624 | |
| 625 | void omega_gpu_elementwise_test_set_mutant(int op) { |
| 626 | LOCK(); |
| 627 | g_mutant_op = op; |
| 628 | cache_clear_locked(); |
| 629 | UNLOCK(); |
| 630 | } |
| 631 | |
| 632 | /* Caller holds the lock. Builds the kernel if needed; uploads the device copy when a |
| 633 | * device is open (upload = false lets the host-only codegen path run without a chip). */ |
| 634 | static int kernel_for(OmegaGpuEwOp op, bool upload, Slot **out, bool *hit) { |
| 635 | int mutant = (g_mutant_op == (int)op); |
| 636 | Slot *s = NULL; |
| 637 | for (size_t i = 0; i < sizeof g_cache / sizeof g_cache[0]; i++) |
| 638 | if (g_cache[i].used && g_cache[i].op == (int)op && g_cache[i].mutant == mutant) { s = &g_cache[i]; *hit = true; break; } |
| 639 | if (!s) { |
| 640 | OmegaBlackwellKernel k; |
| 641 | int rc = build_kernel(op, mutant, &k, NULL); |
| 642 | if (rc != OMEGA_GPU_EW_OK) return rc; |
| 643 | size_t slot = 0; |
| 644 | for (size_t i = 0; i < sizeof g_cache / sizeof g_cache[0]; i++) if (!g_cache[i].used) { slot = i; break; } |
| 645 | s = &g_cache[slot]; |
| 646 | if (s->used) { free(s->kernel.code); omega_gpu_session_free(&s->code); } /* full: evict slot 0 */ |
| 647 | memset(s, 0, sizeof *s); |
| 648 | s->used = 1; s->op = (int)op; s->mutant = mutant; s->kernel = k; |
| 649 | *hit = false; |
| 650 | } |
| 651 | if (upload && !s->code.cpu) { |
| 652 | if (omega_gpu_session_alloc(s->kernel.code_size, &s->code) != 0) return OMEGA_GPU_EW_CHIP_FAIL; |
| 653 | memcpy(s->code.cpu, s->kernel.code, s->kernel.code_size); |
| 654 | __asm__ volatile("dsb sy" ::: "memory"); |
| 655 | } |
| 656 | *out = s; |
| 657 | return OMEGA_GPU_EW_OK; |
| 658 | } |
| 659 | |
| 660 | int omega_gpu_elementwise_codegen(OmegaGpuEwOp op, const uint32_t *dims, float eps, OmegaBlackwellKernel *kernel) { |
| 661 | (void)dims; (void)eps; /* kernels read their shape from the argument words */ |
| 662 | if (!kernel) return OMEGA_GPU_EW_BAD_ARGS; |
| 663 | LOCK(); |
| 664 | int mutant = (g_mutant_op == (int)op); |
| 665 | UNLOCK(); |
| 666 | return build_kernel(op, mutant, kernel, NULL); |
| 667 | } |
| 668 | |
| 669 | int omega_gpu_elementwise_codegen_ir(OmegaGpuEwOp op, int mutant, void *prog, OmegaBlackwellKernel *kernel) { |
| 670 | if (!kernel) return OMEGA_GPU_EW_BAD_ARGS; |
| 671 | return build_kernel(op, mutant ? 1 : 0, kernel, (BlackwellIRProgram *)prog); |
| 672 | } |
| 673 | |
| 674 | const char *omega_gpu_elementwise_last_error(void) { return omega_gpu_session_last_error(); } |
| 675 | |
| 676 | /* ------------------------------------------------------------ launcher */ |
| 677 | |
| 678 | #define EW_POISON 0xffbadbadu /* a NaN pattern no kernel here produces on purpose */ |
| 679 | #define EW_WAIT_MS 600000ull /* like main's load/store launchers; a 5 s wait failed under contention (omega#198) */ |
| 680 | |
| 681 | static uint64_t now_ns(void) { |
| 682 | struct timespec t; clock_gettime(CLOCK_MONOTONIC, &t); |
| 683 | return (uint64_t)t.tv_sec * 1000000000ULL + (uint64_t)t.tv_nsec; |
| 684 | } |
| 685 | |
| 686 | /* Caller holds the lock. Opens the shared device on first use. */ |
| 687 | static int dev_open_locked(void) { |
| 688 | if (!g_hooked) { (void)omega_gpu_session_on_close(on_session_close); g_hooked = true; } |
| 689 | return omega_gpu_session_open() ? OMEGA_GPU_EW_OK : OMEGA_GPU_EW_CHIP_FAIL; |
| 690 | } |
| 691 | |
| 692 | /* One launch: a and b staged in, c out (c_bytes/4 words, poison-filled first), through |
| 693 | * the shared session (omega_gpu_session_launch: shader-cache invalidate, constant bank, |
| 694 | * QMD0/QMD1, first marker, L2_FLUSH_DIRTY, second marker), then readback and the |
| 695 | * poison scan. b may be NULL when b_fill wrote the staging buffer already. */ |
| 696 | static int run_abc(const Slot *ks, const void *a, size_t a_bytes, const void *b, size_t b_bytes, |
| 697 | void *c, size_t c_bytes, uint32_t threads_x, uint32_t grid_x, |
| 698 | const uint32_t params[4], OmegaGpuEwInfo *info) { |
| 699 | if (omega_gpu_session_scratch(&g_a, a_bytes) || omega_gpu_session_scratch(&g_b, b_bytes) || omega_gpu_session_scratch(&g_c, c_bytes)) |
| 700 | return OMEGA_GPU_EW_CHIP_FAIL; |
| 701 | memcpy(g_a.mem.cpu, a, a_bytes); |
| 702 | if (b) memcpy(g_b.mem.cpu, b, b_bytes); |
| 703 | size_t c_words = c_bytes / 4; |
| 704 | uint32_t *c_dev = (uint32_t *)g_c.mem.cpu; |
| 705 | for (size_t i = 0; i < c_words; i++) c_dev[i] = EW_POISON; |
| 706 | __asm__ volatile("dsb sy" ::: "memory"); |
| 707 | |
| 708 | uint32_t args[OMEGA_BW_CBANK_MATMUL_ARGS_WORDS]; |
| 709 | args[0] = (uint32_t)g_a.mem.va; args[1] = (uint32_t)(g_a.mem.va >> 32); |
| 710 | args[2] = (uint32_t)g_b.mem.va; args[3] = (uint32_t)(g_b.mem.va >> 32); |
| 711 | args[4] = (uint32_t)g_c.mem.va; args[5] = (uint32_t)(g_c.mem.va >> 32); |
| 712 | args[6] = params[0]; args[7] = params[1]; args[8] = params[2]; args[9] = params[3]; |
| 713 | OmegaGpuLaunch L = { |
| 714 | .code_va = ks->code.va, .gpr_count = EW_GPR_BUDGET, .threads_x = threads_x, .threads_y = 1, |
| 715 | .grid_x = grid_x, .grid_y = 1, .num_elements = c_words, |
| 716 | .args = args, .n_args = OMEGA_BW_CBANK_MATMUL_ARGS_WORDS, .timeout_ms = EW_WAIT_MS, |
| 717 | }; |
| 718 | uint64_t elapsed = 0; uint32_t marker = 0; |
| 719 | if (omega_gpu_session_launch(&L, &elapsed, &marker) != 0) return OMEGA_GPU_EW_CHIP_FAIL; |
| 720 | |
| 721 | memcpy(c, g_c.mem.cpu, c_bytes); |
| 722 | uint32_t unwritten = 0; |
| 723 | for (size_t i = 0; i < c_words; i++) if (((uint32_t *)c)[i] == EW_POISON) unwritten++; |
| 724 | if (info) { |
| 725 | info->chip_calls++; |
| 726 | info->elapsed_ns += elapsed; |
| 727 | info->completion_marker = marker; |
| 728 | info->unwritten_words += unwritten; |
| 729 | info->threads_per_cta = threads_x; |
| 730 | info->ctas_last_launch = grid_x; |
| 731 | } |
| 732 | return unwritten ? OMEGA_GPU_EW_UNWRITTEN : OMEGA_GPU_EW_OK; |
| 733 | } |
| 734 | |
| 735 | /* ------------------------------------------------------- public entries */ |
| 736 | |
| 737 | static void info_begin(OmegaGpuEwInfo *info, const OmegaBlackwellKernel *k, bool hit) { |
| 738 | if (!info) return; |
| 739 | memset(info, 0, sizeof *info); |
| 740 | info->kernel_cache_hit = hit; |
| 741 | info->gpr_count = k->gpr_count; |
| 742 | info->insn_count = (uint32_t)k->insn_count; |
| 743 | snprintf(info->target_chip, sizeof info->target_chip, "NVIDIA DGX Spark (Grace Blackwell GB10)"); |
| 744 | info->sm_architecture = 121; |
| 745 | } |
| 746 | static uint32_t f2u(float f) { uint32_t u; memcpy(&u, &f, 4); return u; } |
| 747 | static void info_end(OmegaGpuEwInfo *info, uint64_t t0) { if (info) info->call_ns = now_ns() - t0; } |
| 748 | |
| 749 | /* Common prologue of every chip entry: lock, open, kernel (resident), info. */ |
| 750 | static int begin(OmegaGpuEwOp op, Slot **ks, OmegaGpuEwInfo *info) { |
| 751 | LOCK(); |
| 752 | int rc = dev_open_locked(); |
| 753 | bool hit = false; |
| 754 | if (rc == OMEGA_GPU_EW_OK) rc = kernel_for(op, true, ks, &hit); |
| 755 | if (rc != OMEGA_GPU_EW_OK) { UNLOCK(); return rc; } |
| 756 | info_begin(info, &(*ks)->kernel, hit); |
| 757 | return OMEGA_GPU_EW_OK; |
| 758 | } |
| 759 | |
| 760 | int omega_gpu_rmsnorm_f32(uint32_t rows, uint32_t dim, const float *x, const float *weight, |
| 761 | float eps, float *out, OmegaGpuEwInfo *info) { |
| 762 | uint64_t t0 = now_ns(); |
| 763 | if (!x || !weight || !out || rows == 0 || dim == 0 || dim % OMEGA_GPU_EW_THREADS != 0) return OMEGA_GPU_EW_BAD_ARGS; |
| 764 | if (dim > OMEGA_GPU_EW_MAX_DIM) return OMEGA_GPU_EW_TOO_LARGE; |
| 765 | Slot *k = NULL; |
| 766 | int rc = begin(OMEGA_GPU_EW_RMSNORM, &k, info); |
| 767 | if (rc != OMEGA_GPU_EW_OK) return rc; |
| 768 | uint32_t params[4] = { dim / OMEGA_GPU_EW_THREADS, f2u(1.0f / (float)dim), f2u(eps), dim }; |
| 769 | for (uint32_t r0 = 0; r0 < rows && rc == OMEGA_GPU_EW_OK; r0 += OMEGA_GPU_EW_MAX_CTAS) { |
| 770 | uint32_t nr = rows - r0 < OMEGA_GPU_EW_MAX_CTAS ? rows - r0 : OMEGA_GPU_EW_MAX_CTAS; |
| 771 | size_t bytes = (size_t)nr * dim * 4; |
| 772 | rc = run_abc(k, x + (size_t)r0 * dim, bytes, weight, (size_t)dim * 4, out + (size_t)r0 * dim, bytes, |
| 773 | OMEGA_GPU_EW_THREADS, nr, params, info); |
| 774 | } |
| 775 | UNLOCK(); |
| 776 | info_end(info, t0); |
| 777 | return rc; |
| 778 | } |
| 779 | |
| 780 | int omega_gpu_rope_f32(uint32_t heads, uint32_t head_dim, const float *v, |
| 781 | const float *cos_half, const float *sin_half, float *out, OmegaGpuEwInfo *info) { |
| 782 | uint64_t t0 = now_ns(); |
| 783 | if (!v || !cos_half || !sin_half || !out || heads == 0 || head_dim < 2 || (head_dim & 1)) return OMEGA_GPU_EW_BAD_ARGS; |
| 784 | if (head_dim > 2048) return OMEGA_GPU_EW_TOO_LARGE; |
| 785 | Slot *k = NULL; |
| 786 | int rc = begin(OMEGA_GPU_EW_ROPE, &k, info); |
| 787 | if (rc != OMEGA_GPU_EW_OK) return rc; |
| 788 | uint32_t half = head_dim / 2; |
| 789 | uint32_t params[4] = { head_dim, half, 0, 0 }; |
| 790 | for (uint32_t h0 = 0; h0 < heads && rc == OMEGA_GPU_EW_OK; h0 += OMEGA_GPU_EW_MAX_CTAS) { |
| 791 | uint32_t nh = heads - h0 < OMEGA_GPU_EW_MAX_CTAS ? heads - h0 : OMEGA_GPU_EW_MAX_CTAS; |
| 792 | size_t bytes = (size_t)nh * head_dim * 4; |
| 793 | /* table: cos in the first half of each head, sin in the second, written straight into |
| 794 | * the staging buffer (one copy per head of this launch) */ |
| 795 | if (omega_gpu_session_scratch(&g_b, bytes) != 0) { rc = OMEGA_GPU_EW_CHIP_FAIL; break; } |
| 796 | float *table = g_b.mem.cpu; |
| 797 | for (uint32_t h = 0; h < nh; h++) { |
| 798 | memcpy(table + (size_t)h * head_dim, cos_half, half * sizeof *table); |
| 799 | memcpy(table + (size_t)h * head_dim + half, sin_half, half * sizeof *table); |
| 800 | } |
| 801 | /* out may alias v: v is copied into the staging buffer before the result lands in out */ |
| 802 | rc = run_abc(k, v + (size_t)h0 * head_dim, bytes, NULL, bytes, out + (size_t)h0 * head_dim, bytes, half, nh, params, info); |
| 803 | } |
| 804 | UNLOCK(); |
| 805 | info_end(info, t0); |
| 806 | return rc; |
| 807 | } |
| 808 | |
| 809 | static int run_1d(OmegaGpuEwOp op, uint32_t n, const void *a, const void *b, void *c, OmegaGpuEwInfo *info) { |
| 810 | uint64_t t0 = now_ns(); |
| 811 | Slot *k = NULL; |
| 812 | int rc = begin(op, &k, info); |
| 813 | if (rc != OMEGA_GPU_EW_OK) return rc; |
| 814 | const uint32_t chunk = OMEGA_GPU_EW_MAX_CTAS * OMEGA_GPU_EW_THREADS; |
| 815 | for (uint32_t i0 = 0; i0 < n && rc == OMEGA_GPU_EW_OK; i0 += chunk) { |
| 816 | uint32_t ni = n - i0 < chunk ? n - i0 : chunk; |
| 817 | uint32_t grid = (ni + OMEGA_GPU_EW_THREADS - 1) / OMEGA_GPU_EW_THREADS; |
| 818 | uint32_t params[4] = { ni, 0, 0, 0 }; |
| 819 | size_t bytes = (size_t)ni * 4; |
| 820 | rc = run_abc(k, (const uint8_t *)a + (size_t)i0 * 4, bytes, b ? (const uint8_t *)b + (size_t)i0 * 4 : a, bytes, |
| 821 | (uint8_t *)c + (size_t)i0 * 4, bytes, OMEGA_GPU_EW_THREADS, grid, params, info); |
| 822 | } |
| 823 | UNLOCK(); |
| 824 | info_end(info, t0); |
| 825 | return rc; |
| 826 | } |
| 827 | |
| 828 | int omega_gpu_swiglu_f32(uint32_t n, const float *gate, const float *up, float *out, OmegaGpuEwInfo *info) { |
| 829 | if (!gate || !up || !out || n == 0) return OMEGA_GPU_EW_BAD_ARGS; |
| 830 | return run_1d(OMEGA_GPU_EW_SWIGLU, n, gate, up, out, info); |
| 831 | } |
| 832 | int omega_gpu_ex2_f32(uint32_t n, const float *x, float *out, OmegaGpuEwInfo *info) { |
| 833 | if (!x || !out || n == 0) return OMEGA_GPU_EW_BAD_ARGS; |
| 834 | return run_1d(OMEGA_GPU_EW_EX2, n, x, NULL, out, info); |
| 835 | } |
| 836 | int omega_gpu_shared_xchg_u32(uint32_t n, const uint32_t *x, uint32_t *out, OmegaGpuEwInfo *info) { |
| 837 | if (!x || !out || n == 0 || n % OMEGA_GPU_EW_THREADS != 0) return OMEGA_GPU_EW_BAD_ARGS; |
| 838 | return run_1d(OMEGA_GPU_EW_XCHG, n, x, NULL, out, info); |
| 839 | } |
| 840 | |
| 841 | const char *omega_gpu_elementwise_rc_name(int rc) { |
| 842 | switch (rc) { |
| 843 | case OMEGA_GPU_EW_OK: return "OK"; |
| 844 | case OMEGA_GPU_EW_BAD_ARGS: return "BAD_ARGS"; |
| 845 | case OMEGA_GPU_EW_TOO_LARGE: return "TOO_LARGE"; |
| 846 | case OMEGA_GPU_EW_CODEGEN_FAIL: return "CODEGEN_FAIL"; |
| 847 | case OMEGA_GPU_EW_CHIP_FAIL: return "CHIP_FAIL"; |
| 848 | case OMEGA_GPU_EW_UNWRITTEN: return "UNWRITTEN"; |
| 849 | default: return "UNKNOWN"; |
| 850 | } |
| 851 | } |