CUDApp
CUDA++: AIEN’s GPU machine-code toolchain, runtime and driver client for NVIDIA GPUs without the CUDA toolkit. Hardware coverage and driver replacement limits are explicit.
Brought by drakestapleton
CUDApp / src / omega_blackwell_codegen_bf16.c
| 1 | /* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception */ |
| 2 | /* omega_blackwell_codegen_bf16.c: bf16 streaming GEMV kernel (the lm_head), OWN-30B 2026-10-13. See the header. |
| 3 | * The emitter helpers below are copies of omega_blackwell_codegen_moe.c's (same chip-learned scheduling rules: every |
| 4 | * instruction waits on everything still pending; variable-latency results go through scoreboard 0, stores set the read |
| 5 | * barrier 1, uniform loads scoreboard 2; fixed-latency producers stall 6; ISETP stalls 14 before a predicated branch; |
| 6 | * loads of one iteration are issued back to back so a warp has all of them in flight at once). */ |
| 7 | #include "omega_blackwell_codegen_bf16.h" |
| 8 | #include "omega_blackwell_qmd.h" /* OMEGA_BW_QMD_MAX_SHARED_BYTES: the XBATCH x stage must fit the launch descriptor */ |
| 9 | #ifndef OMEGA_GPU_NO_POSIX |
| 10 | #include <stdlib.h> |
| 11 | #endif |
| 12 | #include <string.h> |
| 13 | |
| 14 | #ifndef CUDAPP_MIGRATION_PENDING_C_CODEGEN_BF16 |
| 15 | #define CUDAPP_MIGRATION_PENDING_C_CODEGEN_BF16 0 |
| 16 | #endif |
| 17 | #if CUDAPP_MIGRATION_PENDING_C_CODEGEN_BF16 |
| 18 | #define CW(stall, wbar, rbar, wait) \ |
| 19 | (((uint32_t)(stall) << 9) | ((uint32_t)(wbar) << 14) | ((uint32_t)(rbar) << 17) | ((uint32_t)(wait) << 20)) |
| 20 | #define CW_YIELD 0x2000u |
| 21 | #define SB_NONE 7u |
| 22 | #define SB_GPR 0u |
| 23 | #define SB_RD 1u |
| 24 | #define SB_UGPR 2u |
| 25 | #define NOREG (-1) |
| 26 | #define MAX_LOOPS 4 |
| 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 | typedef struct { |
| 31 | BlackwellIRProgram *p; |
| 32 | uint32_t pending; |
| 33 | int err; |
| 34 | int open_start[MAX_LOOPS]; |
| 35 | int depth; |
| 36 | } Em; |
| 37 | |
| 38 | static void emit(Em *e, BlackwellIRInsn in, int kind) { |
| 39 | uint32_t wait = e->pending; |
| 40 | switch (kind) { |
| 41 | case K_FIXED: in.control = CW(6, SB_NONE, SB_NONE, wait); e->pending = 0; break; |
| 42 | case K_PRED: in.control = CW(14, SB_NONE, SB_NONE, wait); e->pending = 0; break; |
| 43 | /* Loads read their address operands late (the read barrier SB_RD): a later instruction must not overwrite an |
| 44 | * in-flight load's address register. Every load sets SB_RD and the next load of a batch waits on it (cheap: |
| 45 | * the operand read, not the data). Found on the chip: the deep k=2048 kernel hung when the allocator reused a |
| 46 | * dead scale address register as the next load's destination (evidence OWN30B-FP8-GEMV-c4006391). */ |
| 47 | case K_VAR: in.control = CW(4, SB_GPR, SB_RD, wait) | CW_YIELD; e->pending = (1u << SB_GPR) | (1u << SB_RD); break; |
| 48 | /* a further load of the same group: waits on nothing, joins the counting scoreboard */ |
| 49 | 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; |
| 50 | case K_UVAR: in.control = CW(4, SB_UGPR, SB_NONE, wait) | CW_YIELD; e->pending = 1u << SB_UGPR; break; |
| 51 | case K_STORE: in.control = CW(4, SB_NONE, SB_RD, wait) | CW_YIELD; e->pending = 1u << SB_RD; break; |
| 52 | case K_SYNC: in.control = CW(6, SB_NONE, SB_NONE, wait); e->pending = 0; break; /* BAR.SYNC after the STS: the attention kernel's chip-proven pattern */ |
| 53 | case K_EXIT: in.control = CW(5, SB_NONE, SB_NONE, wait); e->pending = in.predicate_p0 ? wait : 0; break; |
| 54 | case K_BRA: in.control = CW(5, SB_NONE, SB_NONE, wait); e->pending = in.predicate_p0 ? wait : 0; break; |
| 55 | case K_BSSY: in.control = CW(1, SB_NONE, SB_NONE, wait) | CW_YIELD; e->pending = 0; break; |
| 56 | case K_BSYNC: in.control = CW(5, SB_NONE, SB_NONE, wait) | CW_YIELD; e->pending = 0; break; |
| 57 | default: e->err = -1; return; |
| 58 | } |
| 59 | if (omega_bw_ir_append(e->p, &in) < 0) e->err = -1; |
| 60 | } |
| 61 | |
| 62 | static BlackwellIRInsn I0(BlackwellIROpcode op) { |
| 63 | BlackwellIRInsn in; memset(&in, 0, sizeof in); |
| 64 | in.op = op; in.dst_vreg = NOREG; in.src1_vreg = NOREG; in.src2_vreg = NOREG; in.src3_vreg = NOREG; in.ureg = NOREG; |
| 65 | return in; |
| 66 | } |
| 67 | static int V(Em *e) { int v = omega_bw_ir_alloc_vreg(e->p); if (v < 0) e->err = -1; return v; } |
| 68 | static int V64(Em *e) { int v = omega_bw_ir_alloc_vreg64(e->p); if (v < 0) e->err = -1; return v; } |
| 69 | static int V128(Em *e) { int v = omega_bw_ir_alloc_vreg128(e->p); if (v < 0) e->err = -1; return v; } |
| 70 | static int UV64(Em *e) { int v = omega_bw_ir_alloc_uvreg64(e->p); if (v < 0) e->err = -1; return v; } |
| 71 | static int here(const Em *e) { return (int)e->p->count; } |
| 72 | |
| 73 | 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); } |
| 74 | 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); } |
| 75 | 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); } |
| 76 | 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); } |
| 77 | 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); } |
| 78 | static void movrz(Em *e, int d) { BlackwellIRInsn i = I0(BW_IR_MOV_RZ); i.dst_vreg = d; emit(e, i, K_FIXED); } |
| 79 | /* d = a * imm + c (c NOREG = RZ); a may be a sub-register of a quad */ |
| 80 | static void imadi(Em *e, int d, int a, unsigned asub, uint32_t imm, int c) { BlackwellIRInsn i = I0(BW_IR_IMAD); i.dst_vreg = d; i.src1_vreg = a; i.src1_subreg = (uint8_t)asub; i.imm = imm; i.src3_vreg = c; emit(e, i, K_FIXED); } |
| 81 | |
| 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, unsigned asub, uint32_t imm) { BlackwellIRInsn i = I0(BW_IR_SHF_R); i.dst_vreg = d; i.src1_vreg = a; i.src1_subreg = (uint8_t)asub; i.imm = imm; emit(e, i, K_FIXED); } |
| 85 | static void andi(Em *e, int d, int a, unsigned asub, uint32_t imm) { BlackwellIRInsn i = I0(BW_IR_LOP3_AND); i.dst_vreg = d; i.src1_vreg = a; i.src1_subreg = (uint8_t)asub; i.imm = imm; emit(e, i, K_FIXED); } |
| 86 | static void ldg_wide(Em *e, BlackwellIROpcode op, int d, int addr64, uint32_t imm, int udesc, int first) { |
| 87 | 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); |
| 88 | } |
| 89 | 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); } |
| 90 | static void stg(Em *e, int addr64, uint32_t imm, int val, int udesc) { BlackwellIRInsn i = I0(BW_IR_STG_E); i.src1_vreg = addr64; i.src2_vreg = val; i.imm = imm; i.ureg = udesc; emit(e, i, K_STORE); } |
| 91 | 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); } |
| 92 | 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); } |
| 93 | static void bar_sync(Em *e) { emit(e, I0(BW_IR_BAR_SYNC), K_SYNC); } |
| 94 | 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); } |
| 95 | static void fmaxr(Em *e, int d, int a, int b) { BlackwellIRInsn i = I0(BW_IR_FMNMX_MAX); i.dst_vreg = d; i.src1_vreg = a; i.src2_vreg = b; emit(e, i, K_FIXED); } |
| 96 | static void xorr(Em *e, int d, int a, int b) { BlackwellIRInsn i = I0(BW_IR_LOP3_XOR); i.dst_vreg = d; i.src1_vreg = a; i.src2_vreg = b; emit(e, i, K_FIXED); } |
| 97 | /* d = a * b[bsub] + c */ |
| 98 | static void ffma(Em *e, int d, int a, int b, unsigned bsub, int c) { BlackwellIRInsn i = I0(BW_IR_FFMA); i.dst_vreg = d; i.src1_vreg = a; i.src2_vreg = b; i.src2_subreg = (uint8_t)bsub; i.src3_vreg = c; emit(e, i, K_FIXED); } |
| 99 | 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); } |
| 100 | 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); } |
| 101 | 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); } |
| 102 | static void bra(Em *e, int target) { BlackwellIRInsn i = I0(BW_IR_BRA); i.imm = (uint32_t)(target - here(e)); emit(e, i, K_BRA); } |
| 103 | /* a forward branch whose target is patched when known: returns the instruction index */ |
| 104 | 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; } |
| 105 | static int bra_ifnot_p0_fwd(Em *e) { int at = here(e); BlackwellIRInsn i = I0(BW_IR_BRA); i.predicate_p0 = true; i.predicate_not = true; i.imm = 1; emit(e, i, K_BRA); return at; } |
| 106 | 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; } |
| 107 | static void patch_bra(Em *e, int at, int target) { if (!e->err) e->p->insns[at].imm = (uint32_t)(target - at); } |
| 108 | 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; } |
| 109 | static void bsync(Em *e) { BlackwellIRInsn i = I0(BW_IR_BSYNC); i.bar_reg = 0; emit(e, i, K_BSYNC); } |
| 110 | static void tail(Em *e) { |
| 111 | emit(e, I0(BW_IR_EXIT), K_EXIT); |
| 112 | BlackwellIRInsn self = I0(BW_IR_BRA); |
| 113 | self.control = 0x000fc000u; |
| 114 | if (omega_bw_ir_append(e->p, &self) < 0) e->err = -1; |
| 115 | } |
| 116 | |
| 117 | /* Loops: a value defined before the loop and read inside it stays live until the back-branch |
| 118 | * (the linear-scan allocator knows no back edges; same rule as the elementwise emitter). */ |
| 119 | static void loop_begin(Em *e) { if (e->depth >= MAX_LOOPS) { e->err = -1; return; } e->open_start[e->depth++] = here(e); } |
| 120 | static void loop_end(Em *e) { |
| 121 | if (e->depth <= 0) { e->err = -1; return; } |
| 122 | int start = e->open_start[--e->depth], end = here(e) - 1; |
| 123 | OmegaRegAlloc *ra = &e->p->regalloc; |
| 124 | for (int v = 0; v < ra->num_vregs; v++) { |
| 125 | OmegaLiveInterval *iv = &ra->intervals[v]; |
| 126 | if (iv->first_def >= 0 && iv->first_def < start && iv->last_use >= start && iv->last_use < end) iv->last_use = end; |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | |
| 131 | static int spec_ok(const OmegaBf16GemvSpec *s) { |
| 132 | if (!s) return 0; |
| 133 | if (s->rows != 1 && s->rows != 2 && s->rows != 4) return 0; |
| 134 | if (s->warps < 1 || s->warps > 32) return 0; |
| 135 | if (s->k == 0 || s->k > OMEGA_BF16_MAX_K || s->k % 256u || s->k % (32u * s->warps)) return 0; |
| 136 | if (s->k > 512u * s->warps) return 0; /* at most 16 staging rounds */ |
| 137 | uint32_t ks = s->k / 256u; |
| 138 | if (s->depth == 0 || s->depth > 16 || ks % s->depth || s->rows * s->depth > 16u) return 0; |
| 139 | if (s->mask > 1) return 0; |
| 140 | if (s->mask && (s->rows != 1 || s->ystride || s->xbatch > 1)) return 0; /* MASK form: one row per warp, one x row */ |
| 141 | const uint32_t xb = s->xbatch ? s->xbatch : 1u; |
| 142 | if (xb > OMEGA_BF16_MAX_XBATCH) return 0; |
| 143 | if (xb > 1u && (s->ystride == 0 || s->rows * xb > 16u || (uint64_t)xb * s->k * 4u > OMEGA_BW_QMD_MAX_SHARED_BYTES)) return 0; |
| 144 | if (xb > 1u && ((uint64_t)xb * s->k > 512u * s->warps || (uint64_t)(xb - 1u) * s->ystride * 4u >= (1ull << 31))) return 0; /* 16 staging rounds; y row offsets as immediates */ |
| 145 | /* mutant 4: MASK inverts the skip, XBATCH reads row 0's x for every row (the forms never mix) */ |
| 146 | if (s->mutant < 0 || s->mutant > 4 || (s->mutant == 4 && !s->mask && xb < 2u)) return 0; |
| 147 | if (s->ystride > (1u << 28)) return 0; /* ystride * 4 fits the 32-bit IMAD.WIDE immediate */ |
| 148 | if ((uint64_t)(s->rows - 1) * s->k * 2u + 512u * s->depth >= (1u << 23)) return 0; /* 24-bit signed LDG immediate */ |
| 149 | return 1; |
| 150 | } |
| 151 | uint32_t omega_bf16_gemv_steps(const OmegaBf16GemvSpec *spec) { return spec_ok(spec) ? spec->k / 256u : 0; } |
| 152 | uint32_t omega_bf16_gemv_shared_bytes(const OmegaBf16GemvSpec *spec) { return spec_ok(spec) ? spec->k * 4u * (spec->xbatch ? spec->xbatch : 1u) : 0u; } |
| 153 | |
| 154 | int omega_blackwell_codegen_bf16_gemv_prog(const OmegaBf16GemvSpec *spec, BlackwellIRProgram *prog) { |
| 155 | if (!spec_ok(spec) || !prog) return -1; |
| 156 | const uint32_t K = spec->k, R = spec->rows, W = spec->warps, D = spec->depth, KS = K / 256u; |
| 157 | const uint32_t XB = spec->xbatch ? spec->xbatch : 1u; /* XBATCH form: x rows per CTA sharing every weight read */ |
| 158 | omega_bw_ir_init(prog); |
| 159 | Em em; memset(&em, 0, sizeof em); em.p = prog; |
| 160 | Em *e = &em; |
| 161 | |
| 162 | /* ---- prologue ---- */ |
| 163 | int udesc = UV64(e), pwb = V64(e), px = V64(e), py = V64(e), rgn = V(e), stride = V(e); |
| 164 | int tid = V(e), cx = V(e), warp = V(e), lane = V(e), one = V(e); |
| 165 | int wlane = V(e), rg = V(e), xl0 = V(e); |
| 166 | if (e->err) return -1; |
| 167 | ldcu64(e, udesc, 0x358); |
| 168 | ldc64(e, pwb, OMEGA_BF16_ARG_W); ldc64(e, px, OMEGA_BF16_ARG_X); ldc64(e, py, OMEGA_BF16_ARG_Y); |
| 169 | ldc32(e, rgn, OMEGA_BF16_ARG_RG); ldc32(e, stride, OMEGA_BF16_ARG_STRIDE); |
| 170 | s2r(e, tid, BW_SR_TID_X); s2r(e, cx, BW_SR_CTAID_X); |
| 171 | if (spec->ystride && XB == 1u) { /* ROWS form: this CTA row's x and y */ |
| 172 | int cy = V(e); |
| 173 | if (e->err) return -1; |
| 174 | s2r(e, cy, BW_SR_CTAID_Y); |
| 175 | imadw(e, px, cy, K * 4u, px); |
| 176 | imadw(e, py, cy, spec->ystride * 4u, py); |
| 177 | } |
| 178 | movi(e, one, 1); |
| 179 | shr(e, warp, tid, 0, 5); |
| 180 | andi(e, lane, tid, 0, 31); |
| 181 | imadi(e, wlane, lane, 0, 16, NOREG); /* byte offset of this lane's chunk in a row step */ |
| 182 | imadi(e, rg, cx, 0, W, warp); /* first row group of this warp */ |
| 183 | imadi(e, xl0, lane, 0, 32, NOREG); /* this lane's x chunk (8 floats) of step 0, shared byte offset */ |
| 184 | |
| 185 | /* ---- stage x (k floats, XB rows back to back) in shared memory: one 32-bit word per thread per round ---- */ |
| 186 | { |
| 187 | const uint32_t rounds = XB * K / (32u * W); |
| 188 | int pxt = V64(e), sa0 = V(e), sa = V(e), pxs[16], xst[16]; |
| 189 | for (uint32_t j = 0; j < rounds; j++) { pxs[j] = V64(e); xst[j] = V(e); } |
| 190 | if (e->err) return -1; |
| 191 | imadw(e, pxt, tid, 4, px); |
| 192 | imadi(e, sa0, tid, 0, 4, NOREG); |
| 193 | for (uint32_t j = 0; j < rounds; j++) imadw(e, pxs[j], one, j * 128u * W, pxt); |
| 194 | for (uint32_t j = 0; j < rounds; j++) ldg32(e, xst[j], pxs[j], udesc, j == 0); |
| 195 | for (uint32_t j = 0; j < rounds; j++) { imadi(e, sa, one, 0, j * 128u * W, sa0); sts32(e, sa, xst[j]); } |
| 196 | bar_sync(e); |
| 197 | } |
| 198 | |
| 199 | int pw = V64(e), pwr = V64(e), xl = V(e), cnt = V(e), pyr = V64(e), pya = V64(e), t = V(e), f = V(e); |
| 200 | int acc[4][4], wq[4][16], xq[4][2]; |
| 201 | for (uint32_t r = 0; r < R; r++) { for (uint32_t m = 0; m < XB; m++) acc[r][m] = V(e); for (uint32_t s = 0; s < D; s++) wq[r][s] = V128(e); } |
| 202 | for (uint32_t m = 0; m < XB; m++) { xq[m][0] = V128(e); xq[m][1] = V128(e); } |
| 203 | if (e->err) return -1; |
| 204 | /* ---- MASK form: tau once, normalized (-0 -> +0); -inf for the skipped rows ---- */ |
| 205 | int phi = NOREG, tn = NOREG, zero = NOREG, ninf = NOREG, phr = NOREG, hv = NOREG, mv = NOREG; |
| 206 | if (spec->mask) { |
| 207 | int ptau = V64(e); |
| 208 | phi = V64(e); tn = V(e); zero = V(e); ninf = V(e); phr = V64(e); hv = V(e); mv = V(e); |
| 209 | if (e->err) return -1; |
| 210 | ldc64(e, phi, OMEGA_BF16_ARG_HI); ldc64(e, ptau, OMEGA_BF16_ARG_TAU); |
| 211 | ldg32(e, tn, ptau, udesc, 1); |
| 212 | movrz(e, zero); |
| 213 | fadd(e, tn, tn, zero); |
| 214 | movi(e, ninf, 0xff800000u); |
| 215 | } |
| 216 | |
| 217 | /* ---- row-group loop ---- */ |
| 218 | const int L_rg = here(e); |
| 219 | loop_begin(e); |
| 220 | isetp_ge_u32(e, rg, rgn); |
| 221 | const int bra_end = bra_if_p0_fwd(e); |
| 222 | int bra_skip_row = -1; |
| 223 | if (spec->mask) { /* warp-uniform: every lane loads the same hi[rg] */ |
| 224 | imadw(e, phr, rg, 4u, phi); |
| 225 | ldg32(e, hv, phr, udesc, 1); |
| 226 | fadd(e, hv, hv, zero); |
| 227 | fmaxr(e, mv, hv, tn); |
| 228 | xorr(e, mv, mv, hv); |
| 229 | isetp_ge_u32(e, mv, one); /* P0: hi < tau (the max is not hi) */ |
| 230 | bra_skip_row = spec->mutant == 4 ? bra_ifnot_p0_fwd(e) : bra_if_p0_fwd(e); |
| 231 | } |
| 232 | imadw(e, pwr, rg, R * K * 2u, pwb); imadw(e, pw, wlane, 1, pwr); |
| 233 | imadi(e, xl, xl0, 0, 1, NOREG); |
| 234 | for (uint32_t r = 0; r < R; r++) for (uint32_t m = 0; m < XB; m++) movrz(e, acc[r][m]); |
| 235 | movi(e, cnt, KS / D); |
| 236 | |
| 237 | /* ---- K loop: D steps per iteration, every load issued before any use ---- */ |
| 238 | const int L_k = here(e); |
| 239 | loop_begin(e); |
| 240 | { |
| 241 | int first = 1; |
| 242 | for (uint32_t s = 0; s < D; s++) for (uint32_t r = 0; r < R; r++) { ldg_wide(e, BW_IR_LDG_E_128, wq[r][s], pw, r * K * 2u + s * OMEGA_BF16_STEP_BYTES, udesc, first); first = 0; } |
| 243 | } |
| 244 | for (uint32_t s = 0; s < D; s++) { |
| 245 | const uint32_t xoff = spec->mutant == 2 ? 0u : s * 1024u; |
| 246 | for (uint32_t m = 0; m < XB; m++) { /* x row m of this step (mutant 4: every row reads row 0) */ |
| 247 | const uint32_t moff = spec->mutant == 4 ? 0u : m * K * 4u; |
| 248 | lds128(e, xq[m][0], xl, xoff + moff, m == 0); lds128(e, xq[m][1], xl, xoff + moff + 16u, 0); |
| 249 | } |
| 250 | for (uint32_t r = 0; r < R; r++) { |
| 251 | const uint32_t words = spec->mutant == 3 ? 3u : 4u; |
| 252 | for (uint32_t word = 0; word < words; word++) { |
| 253 | const unsigned sub = (word & 1u) * 2u; |
| 254 | /* low half = element 2 word, high half = element 2 word + 1; the same FMA order per (r, m) as with one x row */ |
| 255 | imadi(e, f, wq[r][s], word, 65536u, NOREG); |
| 256 | for (uint32_t m = 0; m < XB; m++) ffma(e, acc[r][m], f, xq[m][word >> 1], spec->mutant == 1 ? sub + 1u : sub, acc[r][m]); |
| 257 | andi(e, f, wq[r][s], word, 0xffff0000u); |
| 258 | for (uint32_t m = 0; m < XB; m++) ffma(e, acc[r][m], f, xq[m][word >> 1], spec->mutant == 1 ? sub : sub + 1u, acc[r][m]); |
| 259 | } |
| 260 | } |
| 261 | } |
| 262 | imadw(e, pw, one, D * OMEGA_BF16_STEP_BYTES, pw); |
| 263 | imadi(e, xl, one, 0, spec->mutant == 2 ? 0u : D * 1024u, xl); |
| 264 | imadi(e, cnt, one, 0, 0xffffffffu, cnt); |
| 265 | isetp_ge_u32(e, cnt, one); |
| 266 | bra_if_p0(e, L_k); |
| 267 | loop_end(e); |
| 268 | |
| 269 | /* ---- warp reduction, lane 0 stores ---- */ |
| 270 | for (uint32_t r = 0; r < R; r++) |
| 271 | for (uint32_t m = 0; m < XB; m++) |
| 272 | for (uint32_t o = 16; o >= 1; o /= 2) { shfl_down(e, t, acc[r][m], o); fadd(e, acc[r][m], acc[r][m], t); } |
| 273 | imadw(e, pyr, rg, R * 4u, py); |
| 274 | isetp_ge_u32(e, lane, one); |
| 275 | const int at_bssy = bssy_fwd(e); |
| 276 | const int bra_skip = bra_if_p0_fwd(e); |
| 277 | for (uint32_t r = 0; r < R; r++) |
| 278 | for (uint32_t m = 0; m < XB; m++) { imadw(e, pya, one, r * 4u + m * spec->ystride * 4u, pyr); stg(e, pya, 0, acc[r][m], udesc); } |
| 279 | const int at_bsync = here(e); |
| 280 | bsync(e); |
| 281 | patch_bra(e, bra_skip, at_bsync); |
| 282 | patch_bra(e, at_bssy, at_bsync + 1); |
| 283 | if (spec->mask) { /* skipped row: every lane stores the same -inf to y[rg] */ |
| 284 | const int bra_next = bra_fwd(e); |
| 285 | patch_bra(e, bra_skip_row, here(e)); |
| 286 | imadw(e, pyr, rg, 4u, py); |
| 287 | stg(e, pyr, 0, ninf, udesc); |
| 288 | patch_bra(e, bra_next, here(e)); |
| 289 | } |
| 290 | imadi(e, rg, stride, 0, 1, rg); |
| 291 | bra(e, L_rg); |
| 292 | loop_end(e); |
| 293 | const int L_end = here(e); |
| 294 | patch_bra(e, bra_end, L_end); |
| 295 | tail(e); |
| 296 | if (e->err) return -1; |
| 297 | return omega_bw_regalloc_solve(prog); |
| 298 | } |
| 299 | |
| 300 | #else /* default: first-party seeded Omega; C above is differential-only */ |
| 301 | #include "omega_codegen_rest_wire.h" |
| 302 | uint32_t omega_bf16_gemv_steps(const OmegaBf16GemvSpec *spec) { |
| 303 | uint64_t meta[OMEGA_OM_IR_META] = {0}; |
| 304 | if (omega_rest_query(omega_rest_bf16_wire, 0, spec, NULL, meta) != 0) return 0; |
| 305 | return (uint32_t)(int64_t)meta[48]; |
| 306 | } |
| 307 | uint32_t omega_bf16_gemv_shared_bytes(const OmegaBf16GemvSpec *spec) { |
| 308 | uint64_t meta[OMEGA_OM_IR_META] = {0}; |
| 309 | if (omega_rest_query(omega_rest_bf16_wire, 1, spec, NULL, meta) != 0) return 0; |
| 310 | return (uint32_t)(int64_t)meta[48]; |
| 311 | } |
| 312 | int omega_blackwell_codegen_bf16_gemv_prog(const OmegaBf16GemvSpec *spec, BlackwellIRProgram *prog) { |
| 313 | return omega_gpu_om_ir_replay(omega_rest_bf16_wire, 2, (uintptr_t)spec, 0, 0, prog, NULL) == 0 ? 0 : -1; |
| 314 | } |
| 315 | #endif /* CUDAPP_MIGRATION_PENDING_C_CODEGEN_BF16 */ |
| 316 | |
| 317 | int omega_bf16_gemv_prog_stats(const OmegaBf16GemvSpec *spec, uint32_t *insns, uint32_t *gprs) { |
| 318 | static BlackwellIRProgram prog; |
| 319 | if (omega_blackwell_codegen_bf16_gemv_prog(spec, &prog) != 0) return -1; |
| 320 | if (insns) *insns = (uint32_t)prog.count; |
| 321 | if (gprs) *gprs = prog.regalloc.peak_gpr_usage; |
| 322 | return 0; |
| 323 | } |
| 324 | |
| 325 | #ifndef OMEGA_GPU_NO_POSIX |
| 326 | int omega_blackwell_codegen_bf16_gemv(const OmegaBf16GemvSpec *spec, OmegaBlackwellKernel *kernel) { |
| 327 | if (!kernel) return -1; |
| 328 | BlackwellIRProgram *prog = calloc(1, sizeof *prog); |
| 329 | if (!prog) return -1; |
| 330 | int rc = omega_blackwell_codegen_bf16_gemv_prog(spec, prog); |
| 331 | size_t need = rc == 0 ? omega_bw_program_code_bytes(prog) : 0; |
| 332 | uint8_t *buf = need ? malloc(need) : NULL; |
| 333 | if (!buf) { free(prog); return -1; } |
| 334 | rc = omega_blackwell_kernel_encode_into(prog, buf, need, kernel); |
| 335 | free(prog); |
| 336 | if (rc != 0) { free(buf); return -1; } |
| 337 | return 0; |
| 338 | } |
| 339 | #endif |