Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/CUDApp/blob/main/src/omega_blackwell_codegen_tern.c # CUDApp CUDA++: AIEN’s GPU machine-code toolchain, runtime and driver client for NVIDIA GPUs without the CUDA toolkit. Hardware coverage and driver replacement limits are explicit. Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `CUDApp / src / omega_blackwell_codegen_tern.c` 677 lines · 41700 bytes Raw Blame History 1 /* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception */ 2 /* omega_blackwell_codegen_tern.c: KT1 W2A8 ternary GEMV kernel, ternary engine stage T0 (2026-10-15). See the header. 3 * The emitter helpers are copies of omega_blackwell_codegen_bf16.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; ISETP stalls 14 before a predicated branch; 6 * the loads of one iteration are issued back to back so a warp has all of them in flight at once). One change: the 7 * fixed-latency stall is no longer 6 on every instruction; tern_schedule_stalls sets each one from the physical register 8 * dependencies so every user still sits >= 6 cycles behind its producer (see the pass). */ 9 #include "omega_blackwell_codegen_tern.h" 10 #include 11 #include "cudapp_native.h" 12 #include "omega_gpu_om_ir.h" 13 14 /* Independent differential oracle only; the seeded Omega successor is the default. */ 15 #ifndef CUDAPP_MIGRATION_PENDING_C_CODEGEN_TERN 16 #define CUDAPP_MIGRATION_PENDING_C_CODEGEN_TERN 0 17 #endif 18 #if CUDAPP_MIGRATION_PENDING_C_CODEGEN_TERN 19 20 #define CW(stall, wbar, rbar, wait) \ 21 (((uint32_t)(stall) << 9) | ((uint32_t)(wbar) << 14) | ((uint32_t)(rbar) << 17) | ((uint32_t)(wait) << 20)) 22 #define CW_YIELD 0x2000u 23 #define SB_NONE 7u 24 #define SB_GPR 0u 25 #define SB_RD 1u 26 #define SB_UGPR 2u 27 #define NOREG (-1) 28 #define MAX_LOOPS 4 29 30 enum { K_FIXED, K_PRED, K_VAR, K_VAR_MORE, K_UVAR, K_STORE, K_SYNC, K_EXIT, K_BRA, K_BSSY, K_BSYNC }; 31 32 typedef struct { 33 BlackwellIRProgram *p; 34 uint32_t pending; 35 int err; 36 int open_start[MAX_LOOPS]; 37 int depth; 38 int i2f_fixed; /* mutant 4 only: the pre-Sass#4 I2FP (directly before its FMUL, OMEGA_TERN_FIXED_LAT behind) */ 39 } Em; 40 41 static void emit(Em *e, BlackwellIRInsn in, int kind) { 42 uint32_t wait = e->pending; 43 switch (kind) { 44 case K_FIXED: in.control = CW(6, SB_NONE, SB_NONE, wait); e->pending = 0; break; 45 case K_PRED: in.control = CW(14, SB_NONE, SB_NONE, wait); e->pending = 0; break; 46 case K_VAR: in.control = CW(4, SB_GPR, SB_RD, wait) | CW_YIELD; e->pending = (1u << SB_GPR) | (1u << SB_RD); break; 47 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; 48 case K_UVAR: in.control = CW(4, SB_UGPR, SB_NONE, wait) | CW_YIELD; e->pending = 1u << SB_UGPR; break; 49 case K_STORE: in.control = CW(4, SB_NONE, SB_RD, wait) | CW_YIELD; e->pending = 1u << SB_RD; break; 50 case K_SYNC: in.control = CW(6, SB_NONE, SB_NONE, wait); e->pending = 0; break; 51 case K_EXIT: in.control = CW(5, SB_NONE, SB_NONE, wait); e->pending = in.predicate_p0 ? wait : 0; break; 52 case K_BRA: in.control = CW(5, SB_NONE, SB_NONE, wait); e->pending = in.predicate_p0 ? wait : 0; break; 53 case K_BSSY: in.control = CW(1, SB_NONE, SB_NONE, wait) | CW_YIELD; e->pending = 0; break; 54 case K_BSYNC: in.control = CW(5, SB_NONE, SB_NONE, wait) | CW_YIELD; e->pending = 0; break; 55 default: e->err = -1; return; 56 } 57 if (omega_bw_ir_append(e->p, &in) < 0) e->err = -1; 58 } 59 60 static BlackwellIRInsn I0(BlackwellIROpcode op) { 61 BlackwellIRInsn in; memset(&in, 0, sizeof in); 62 in.op = op; in.dst_vreg = NOREG; in.src1_vreg = NOREG; in.src2_vreg = NOREG; in.src3_vreg = NOREG; in.ureg = NOREG; 63 return in; 64 } 65 static int V(Em *e) { int v = omega_bw_ir_alloc_vreg(e->p); if (v < 0) e->err = -1; return v; } 66 static int V64(Em *e) { int v = omega_bw_ir_alloc_vreg64(e->p); if (v < 0) e->err = -1; return v; } 67 static int V128(Em *e) { int v = omega_bw_ir_alloc_vreg128(e->p); if (v < 0) e->err = -1; return v; } 68 static int UV64(Em *e) { int v = omega_bw_ir_alloc_uvreg64(e->p); if (v < 0) e->err = -1; return v; } 69 static int here(const Em *e) { return (int)e->p->count; } 70 71 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); } 72 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); } 73 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); } 74 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); } 75 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); } 76 static void movrz(Em *e, int d) { BlackwellIRInsn i = I0(BW_IR_MOV_RZ); i.dst_vreg = d; emit(e, i, K_FIXED); } 77 /* d = a[asub] * imm + c (c NOREG = RZ) */ 78 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); } 79 /* d = a * b[bsub] + c (register form) */ 80 static void imadr(Em *e, int d, int a, int b, unsigned bsub, int c) { BlackwellIRInsn i = I0(BW_IR_IMAD); 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); } 81 /* d64 = a * imm + c64 */ 82 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); } 83 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); } 84 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); } 85 static void ldg_wide(Em *e, BlackwellIROpcode op, int d, int addr64, uint32_t imm, int udesc, int first) { 86 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); 87 } 88 static void ldg32(Em *e, int d, int addr64, uint32_t imm, int udesc, int first) { 89 BlackwellIRInsn i = I0(BW_IR_LDG_E); i.dst_vreg = d; i.src1_vreg = addr64; i.imm = imm; i.ureg = udesc; emit(e, i, first ? K_VAR : K_VAR_MORE); 90 } 91 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); } 92 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); } 93 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); } 94 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); } 95 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); } 96 static void bar_sync(Em *e) { emit(e, I0(BW_IR_BAR_SYNC), K_SYNC); } 97 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); } 98 /* I2FP is a fixed-latency write here: no scoreboard, and every reader sits >= OMEGA_TERN_I2FP_LAT cycles behind 99 * (tern_schedule_stalls). Sass#4 (`bc16eab`) put it on scoreboard 0 with the reader waiting at stall 4 instead; that was 100 * host-tested only, and on the GB10 it broke the f32 epilogue: omega-mv#924 window 2026-11-01T00:55Z, gpu_tern_test 101 * "f32 q|k|v segments" 2040 of 3840 rows off ((float)y * a) * scale[seg], while the stall-6 fixed encoding (omega-mv 102 * main `ba5559d9`) had 0 in the same window. A wait on an I2FP scoreboard does not hold the reader on this chip, so only 103 * distance counts. 19 is the Sass#4 sweep's i2f->fmul (lat-20261027T025808Z); it covers the chip-correct 6 as well. */ 104 static void i2f(Em *e, int d, int a) { BlackwellIRInsn i = I0(BW_IR_I2FP); i.dst_vreg = d; i.src1_vreg = a; emit(e, i, K_FIXED); } 105 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); } 106 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); } 107 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); } 108 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); } 109 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; } 110 static void patch_bra(Em *e, int at, int target) { if (!e->err) e->p->insns[at].imm = (uint32_t)(target - at); } 111 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; } 112 static void bsync(Em *e) { BlackwellIRInsn i = I0(BW_IR_BSYNC); i.bar_reg = 0; emit(e, i, K_BSYNC); } 113 static void tail(Em *e) { 114 emit(e, I0(BW_IR_EXIT), K_EXIT); 115 BlackwellIRInsn self = I0(BW_IR_BRA); 116 self.control = 0x000fc000u; 117 if (omega_bw_ir_append(e->p, &self) < 0) e->err = -1; 118 } 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 /* predicated region: lanes with P0 set skip to the join (bra_if_p0 inside BSSY .. BSYNC, the bf16 store pattern) */ 130 typedef struct { int bssy, bra; } Region; 131 static Region region_begin(Em *e) { Region r; r.bssy = bssy_fwd(e); r.bra = bra_if_p0_fwd(e); return r; } 132 static void region_end(Em *e, Region r) { int at = here(e); bsync(e); patch_bra(e, r.bra, at); patch_bra(e, r.bssy, at + 1); } 133 134 uint32_t omega_tern_gemv_chunks(const OmegaTernGemvSpec *s) { 135 if (!s || s->k == 0) return 0; 136 return (s->k + OMEGA_TERN_CHUNK_MAX - 1u) / OMEGA_TERN_CHUNK_MAX; 137 } 138 139 int omega_tern_gemv_spec_ok(const OmegaTernGemvSpec *s) { 140 if (!s) return 0; 141 if (s->k == 0 || s->k > OMEGA_TERN_MAX_K || s->k % 256u) return 0; 142 const uint32_t nch = omega_tern_gemv_chunks(s); 143 if (s->k % (256u * nch)) return 0; 144 if (s->layout == OMEGA_TERN_LAYOUT_ROWMAJOR) { if (s->rows != 1 && s->rows != 2 && s->rows != 4) return 0; } 145 else if (s->layout == OMEGA_TERN_LAYOUT_BLOCK) { if (s->rows != 2 && s->rows != 4) return 0; } 146 else return 0; 147 if (s->warps < 1 || s->warps > 32) return 0; 148 const uint32_t ksc = s->k / nch / 256u; 149 if (s->depth == 0 || s->depth > 16 || ksc % s->depth || s->rows * s->depth > 16u) return 0; 150 if (s->epi != OMEGA_TERN_EPI_I32 && s->epi != OMEGA_TERN_EPI_F32) return 0; 151 if (s->epi == OMEGA_TERN_EPI_F32 && nch != 1) return 0; 152 if (s->mutant < 0 || s->mutant > 6) return 0; 153 return 1; 154 } 155 uint32_t omega_tern_gemv_steps(const OmegaTernGemvSpec *s) { return omega_tern_gemv_spec_ok(s) ? s->k / 256u : 0; } 156 /* Output path. Chip finding (investigation 2026-10-16-kt1-speed, probe-nostore): one small GPU-uncached store per unit per 157 * warp cost 9 to 11 us per launch at 3840 x 2560 and 13824 x 2560, more than a third of the launch. So a single-chunk 158 * kernel gathers each round's 2 R W results in shared memory (two slot sets, alternating by round, one CTA barrier per 159 * round) and warp 0 writes them as one coalesced store of 2 R W consecutive words. Chunked kernels (k > 8192) keep the 160 * per-row store because they accumulate y in place across chunks. */ 161 int omega_tern_gemv_coalesced_out(const OmegaTernGemvSpec *s) { 162 if (!omega_tern_gemv_spec_ok(s) || omega_tern_gemv_chunks(s) != 1 || 2u * s->rows * s->warps > 32u) return 0; /* warp 0 stores the round */ 163 return s->k * 2u + 2u * omega_tern_gemv_out_slot_bytes(s) <= OMEGA_TERN_SHARED_MAX; 164 } 165 uint32_t omega_tern_gemv_out_slot_bytes(const OmegaTernGemvSpec *s) { return s ? 2u * s->rows * s->warps * 4u : 0; } 166 uint32_t omega_tern_gemv_shared_bytes(const OmegaTernGemvSpec *s) { 167 if (!omega_tern_gemv_spec_ok(s)) return 0; 168 const uint32_t x_bytes = (s->k / omega_tern_gemv_chunks(s)) * 2u; 169 return omega_tern_gemv_coalesced_out(s) ? x_bytes + 2u * omega_tern_gemv_out_slot_bytes(s) : x_bytes; 170 } 171 172 uint64_t omega_tern_word_offset(uint32_t layout, uint32_t R, uint32_t k, uint32_t r, uint32_t c) { 173 const uint64_t kw = k / 16u; 174 if (layout == OMEGA_TERN_LAYOUT_ROWMAJOR) return ((uint64_t)r * kw + c) * 4u; 175 const uint64_t blk = r / (2u * R), j = r % (2u * R); 176 return blk * 2u * R * kw * 4u + (uint64_t)c * 2u * R * 4u + j * 4u; 177 } 178 179 180 /* ---- dependency-driven stall counts (after register allocation) ---- 181 * The copied emitter gives every fixed-latency instruction stall 6, which keeps any user >= 6 cycles behind its 182 * producer but also makes independent instructions wait 6 cycles each. The repository's chip-proven rule is the 183 * distance, not the per-instruction count: a fixed-latency producer stalls >= 5 cycles before its consumer (we keep 6; 184 * omega_gpu_elementwise_api.c:13). This pass walks the program in order on physical registers and gives each 185 * instruction the smallest stall that keeps the next instruction >= OMEGA_TERN_FIXED_LAT cycles behind every 186 * fixed-latency write it reads or overwrites. Before a control instruction (BRA, EXIT, BSSY, BSYNC, BAR) or a branch 187 * target every fixed-latency write has completed, so straight-line timing never spans a jump. Instructions that are not 188 * fixed latency keep at least their emitted stall (loads, ISETP 14 before a predicated branch, the scoreboard rules); 189 * their wait masks are untouched. Hardware can only add cycles, which only lengthens every distance. */ 190 static int tern_fixed(BlackwellIROpcode op) { 191 switch (op) { 192 case BW_IR_IMAD: case BW_IR_IMAD_WIDE: case BW_IR_SHF_R: case BW_IR_LOP3_AND: case BW_IR_MOV_IMM: case BW_IR_MOV_RZ: 193 case BW_IR_FMUL: case BW_IR_I2FP: return 1; 194 default: return 0; 195 } 196 } 197 static int tern_control(BlackwellIROpcode op) { 198 return op == BW_IR_BRA || op == BW_IR_EXIT || op == BW_IR_BSSY || op == BW_IR_BSYNC || op == BW_IR_BAR_SYNC; 199 } 200 /* physical registers an operand covers: [*lo, *lo + *cnt) */ 201 static void tern_span(const BlackwellIRProgram *p, int v, int *lo, int *cnt) { 202 *lo = -1; *cnt = 0; 203 if (v < 0 || v >= p->regalloc.num_vregs) return; 204 const int ph = p->regalloc.vreg_to_phys[v]; 205 if (ph < 0) return; 206 *lo = ph; *cnt = (int)(p->regalloc.intervals[v].bundle_size ? p->regalloc.intervals[v].bundle_size : 1u); 207 } 208 static int tern_schedule_stalls(BlackwellIRProgram *p, uint64_t i2f_lat) { 209 static uint8_t target[BW_MAX_IR_INSNS + 1]; 210 uint64_t ready[BW_MAX_VREGS + 8]; 211 const size_t n = p->count; 212 memset(target, 0, sizeof target); 213 memset(ready, 0, sizeof ready); 214 for (size_t i = 0; i < n; i++) { 215 const BlackwellIRInsn *in = &p->insns[i]; 216 if (in->op == BW_IR_BRA || in->op == BW_IR_BSSY) { 217 const long t = (long)i + (long)(int32_t)in->imm; 218 if (t < 0 || t > (long)n) return -1; 219 target[t] = 1; 220 } 221 } 222 uint64_t T = 0, all = 0; 223 for (size_t i = 0; i < n; i++) { 224 BlackwellIRInsn *in = &p->insns[i]; 225 const uint32_t s_emit = (in->control >> 9) & 0xfu; 226 if (tern_fixed(in->op)) { 227 const uint64_t lat = in->op == BW_IR_I2FP ? i2f_lat : OMEGA_TERN_FIXED_LAT; 228 int lo, c; tern_span(p, in->dst_vreg, &lo, &c); 229 for (int k = 0; k < c; k++) { if (lo + k >= BW_MAX_VREGS + 8) return -1; ready[lo + k] = T + lat; } 230 if (c && T + lat > all) all = T + lat; 231 } 232 uint64_t need = T + (tern_fixed(in->op) ? 1u : s_emit); 233 if (i + 1 < n) { 234 const BlackwellIRInsn *nx = &p->insns[i + 1]; 235 const int ops[4] = { nx->dst_vreg, nx->src1_vreg, nx->src2_vreg, nx->src3_vreg }; 236 for (int q = 0; q < 4; q++) { 237 int lo, c; tern_span(p, ops[q], &lo, &c); 238 for (int k = 0; k < c; k++) if (ready[lo + k] > need) need = ready[lo + k]; 239 } 240 if ((tern_control(nx->op) || target[i + 1]) && all > need) need = all; 241 } 242 const uint64_t s = need - T; 243 if (s > 15 || (s == 0 && (tern_fixed(in->op) || s_emit != 0))) return -1; /* stall 0 only where emitted (the final self-branch) */ 244 if (tern_fixed(in->op) || s > s_emit) in->control = (in->control & ~(0xfu << 9)) | ((uint32_t)s << 9); 245 T = need; 246 } 247 return 0; 248 } 249 /* ---- I2FP consumer check (Sass#4), run on every generated program; the generator refuses a violation ---- 250 * For every I2FP write, walk forward summing stalls. The write is safe for an instruction once OMEGA_TERN_I2FP_LAT 251 * cycles have passed; a scoreboard wait does not count (see i2f). An instruction that reads or overwrites the I2FP's 252 * registers before that, or a control instruction or branch target reached before that, is a violation. Every other 253 * producer keeps its own rule (an FMUL at stall 6 is fine). */ 254 static int tern_overlap(const BlackwellIRProgram *p, int a, int b) { 255 int la, ca, lb, cb; tern_span(p, a, &la, &ca); tern_span(p, b, &lb, &cb); 256 return ca && cb && la < lb + cb && lb < la + ca; 257 } 258 long omega_tern_i2fp_violations(const BlackwellIRProgram *p) { 259 static uint8_t tgt[BW_MAX_IR_INSNS + 1]; 260 if (!p) return -1; 261 memset(tgt, 0, sizeof tgt); 262 for (size_t i = 0; i < p->count; i++) { 263 const BlackwellIRInsn *in = &p->insns[i]; 264 if (in->op == BW_IR_BRA || in->op == BW_IR_BSSY) { long t = (long)i + (long)(int32_t)in->imm; if (t >= 0 && t <= (long)p->count) tgt[t] = 1; } 265 } 266 long bad = 0; 267 for (size_t i = 0; i < p->count; i++) { 268 const BlackwellIRInsn *in = &p->insns[i]; 269 if (in->op != BW_IR_I2FP || in->dst_vreg < 0) continue; 270 uint32_t dist = 0; 271 for (size_t j = i + 1; j < p->count; j++) { 272 const BlackwellIRInsn *u = &p->insns[j]; 273 dist += (p->insns[j - 1].control >> 9) & 0xfu; 274 if (dist >= OMEGA_TERN_I2FP_LAT) break; 275 if (tern_control(u->op) || tgt[j]) { bad++; break; } 276 if (tern_overlap(p, in->dst_vreg, u->dst_vreg) || tern_overlap(p, in->dst_vreg, u->src1_vreg) || 277 tern_overlap(p, in->dst_vreg, u->src2_vreg) || tern_overlap(p, in->dst_vreg, u->src3_vreg)) { bad++; break; } 278 } 279 } 280 return bad; 281 } 282 283 /* Every weight load of one K iteration (D steps) from the lane pointer pw, issued back to back with immediate offsets 284 * (block: one wide load per step; row-major: one 32-bit load per row and step). */ 285 static void weight_loads(Em *e, int block, uint32_t R, uint32_t D, const int *wq, int wr[4][16], int pw, uint32_t step_bytes, 286 uint32_t row_bytes, int udesc) { 287 if (block) { 288 for (uint32_t d = 0; d < D; d++) ldg_wide(e, R == 4 ? BW_IR_LDG_E_128 : BW_IR_LDG_E_64, wq[d], pw, d * step_bytes, udesc, d == 0); 289 return; 290 } 291 for (uint32_t r = 0; r < R; r++) for (uint32_t d = 0; d < D; d++) ldg32(e, wr[r][d], pw, r * row_bytes + d * step_bytes, udesc, r == 0 && d == 0); 292 } 293 294 /* x staging address of word c, clamped to KWC - 1 so no lane reads past x (clamped lanes rewrite that word's staged values, 295 * identical bytes to the same place): cc = c < KWC ? c : KWC - 1, pxa = px + 16 cc, sb = 32 cc. kwm1 holds KWC - 1. */ 296 static void x_stage_addr(Em *e, int c, int kwm1, int one, int px, int cc, int pxa, int sb, int va, int vb) { 297 imadi(e, va, kwm1, 0, 0xffffffffu, c); /* c - (KWC - 1) */ 298 imadi(e, vb, one, 0, 0xffffffffu, va); /* c - KWC */ 299 shr(e, vb, vb, 0, 31); /* 1 if c < KWC */ 300 imadr(e, cc, vb, va, 0, kwm1); /* c < KWC ? c : KWC - 1 */ 301 imadw(e, pxa, cc, 16u, px); 302 imadi(e, sb, cc, 0, 32u, NOREG); 303 } 304 305 int omega_blackwell_codegen_tern_gemv_prog(const OmegaTernGemvSpec *spec, BlackwellIRProgram *prog) { 306 if (!omega_tern_gemv_spec_ok(spec) || !prog) return -1; 307 const uint32_t K = spec->k, R = spec->rows, W = spec->warps, D = spec->depth, TH = 32u * W; 308 const uint32_t NCH = omega_tern_gemv_chunks(spec), KC = K / NCH, KSC = KC / 256u, KWC = KC / 16u; 309 const int block = spec->layout == OMEGA_TERN_LAYOUT_BLOCK, f32 = spec->epi == OMEGA_TERN_EPI_F32; 310 const uint32_t row_bytes = K / 4u; /* packed bytes per row (whole k) */ 311 const uint32_t step_bytes = block ? 128u * R : 64u; /* bytes a lane's weight pointer advances per K step */ 312 const uint32_t chunk_bytes = block ? KC * R / 2u : KC / 4u; /* ... and per chunk */ 313 const uint32_t npairs = spec->mutant == 3 ? 7u : 8u; 314 omega_bw_ir_init(prog); 315 Em em; memset(&em, 0, sizeof em); em.p = prog; em.i2f_fixed = spec->mutant == 4; 316 Em *e = &em; 317 318 /* ---- prologue ---- */ 319 int udesc = UV64(e), pwb = V64(e), px = V64(e), py = V64(e), ureg = V(e), stride = V(e), negn = V(e); 320 int tid = V(e), cx = V(e), warp = V(e), lane = V(e), h = V(e), l = V(e), one = V(e), t = V(e), nzl1 = V(e); 321 int u0 = V(e), u = V(e), xl = V(e), hR = V(e), lo = V(e), pl = V64(e), k0 = V(e); 322 int pws = NOREG, pas = NOREG, asv = NOREG, nb[3] = { NOREG, NOREG, NOREG }; 323 int chc = NOREG, mul = NOREG, prev = NOREG; 324 if (f32) { pws = V64(e); pas = V64(e); asv = V(e); for (int i = 0; i < 3; i++) nb[i] = V(e); } 325 if (NCH > 1) { chc = V(e); mul = V(e); prev = V(e); } 326 if (e->err) return -1; 327 ldcu64(e, udesc, 0x358); 328 ldc64(e, pwb, OMEGA_TERN_ARG_W); ldc64(e, px, OMEGA_TERN_ARG_X); ldc64(e, py, OMEGA_TERN_ARG_Y); 329 ldc32(e, ureg, OMEGA_TERN_ARG_U); ldc32(e, stride, OMEGA_TERN_ARG_STRIDE); ldc32(e, negn, OMEGA_TERN_ARG_NEGN); 330 if (f32) { 331 ldc64(e, pws, OMEGA_TERN_ARG_WS); ldc64(e, pas, OMEGA_TERN_ARG_AS); 332 for (int i = 0; i < 3; i++) ldc32(e, nb[i], OMEGA_TERN_ARG_NEGB + 4u * (uint32_t)i); 333 } 334 s2r(e, tid, BW_SR_TID_X); s2r(e, cx, BW_SR_CTAID_X); 335 movi(e, one, 1); 336 shr(e, warp, tid, 0, 5); 337 andi(e, lane, tid, 0, 31); 338 shr(e, h, lane, 0, 4); 339 andi(e, l, lane, 0, 15); 340 imadi(e, t, l, 0, 0xffffffffu, NOREG); /* -l */ 341 shr(e, t, t, 0, 31); /* 1 if l > 0 */ 342 imadi(e, nzl1, t, 0, 1, one); /* (l > 0) + 1 */ 343 imadi(e, u0, cx, 0, W, warp); /* first unit of this warp */ 344 imadi(e, xl, l, 0, 32, NOREG); /* shared byte offset of word c = l */ 345 imadi(e, hR, h, 0, R, NOREG); 346 if (block) { imadi(e, lo, l, 0, 8u * R, NOREG); imadi(e, lo, h, 0, 4u * R, lo); } 347 else { imadi(e, lo, l, 0, 4u, NOREG); imadi(e, lo, h, 0, R * row_bytes, lo); } 348 imadw(e, pl, lo, 1, pwb); /* this lane's weight pointer in unit 0, chunk 0 */ 349 movi(e, k0, 0x80008000u); 350 if (f32) ldg32(e, asv, pas, 0, udesc, 1); 351 if (NCH > 1) { movi(e, chc, NCH); movrz(e, mul); } 352 353 /* ---- per-unit registers ---- */ 354 int rowbase = V(e), pw = V64(e), xlc = V(e), cnt = V(e); 355 int T[4], wq[16], wr[4][16], xs[8], Ut[4], P[4], M[4], ap[4], am[4]; 356 int row = V(e), okr = V(e), skip = V(e), yv = V(e), f = V(e), wsv = V(e), pya = V64(e), pwsa = V64(e), sum = V(e); 357 for (uint32_t r = 0; r < R; r++) { T[r] = V(e); Ut[r] = V(e); P[r] = V(e); M[r] = V(e); ap[r] = V(e); am[r] = V(e); } 358 if (block) { for (uint32_t d = 0; d < D; d++) wq[d] = R == 4 ? V128(e) : V64(e); } 359 else for (uint32_t r = 0; r < R; r++) for (uint32_t d = 0; d < D; d++) wr[r][d] = V(e); 360 for (uint32_t i = 0; i < (D <= 4u ? 2u * D : 2u); i++) xs[i] = V128(e); 361 /* prefetch flag (single chunk): 1 while this warp's first K iteration already has its weights in flight */ 362 const int pre = NCH == 1; 363 int pf = NOREG, pu = NOREG; 364 if (pre) { pf = V(e); pu = V(e); } 365 if (e->err) return -1; 366 367 /* ---- chunk loop (NCH > 1 only for k > OMEGA_TERN_CHUNK_MAX): x staged per chunk, y accumulated in place ---- */ 368 const int L_c = here(e); 369 if (NCH > 1) { loop_begin(e); bar_sync(e); } 370 371 /* ---- stage x: per word c of the chunk, X_j = x[16c+j] * 65536 + x[16c+j+8] (j < 8), 32 bytes. 372 * Single chunk: the weight loads of this warp's first unit (clamped to U - 1, always inside the tensor) go out first 373 * and the x load follows without waiting for them, so the x round trip and the first weight round trip overlap; the 374 * first instruction that uses x then waits for both. The K loop skips its loads once while pf is set. Measured 375 * (investigation 2026-10-16-kt1-speed): x staging alone took 2.3 to 3.2 us of every CTA before any weight moved. ---- */ 376 { 377 const uint32_t rounds = (KWC + TH - 1) / TH; 378 int c = V(e), kwm1 = V(e), rc = V(e), cc = V(e), pxa = V64(e), xq = V128(e), sb = V(e), va = V(e), vb = V(e), X = V(e), sad = V(e); 379 if (e->err) return -1; 380 movi(e, kwm1, KWC - 1u); 381 imadi(e, c, tid, 0, 1, NOREG); 382 movi(e, rc, rounds); 383 x_stage_addr(e, c, kwm1, one, px, cc, pxa, sb, va, vb); 384 if (pre) { 385 imadi(e, pu, ureg, 0, 0xffffffffu, u0); /* u0 - U */ 386 shr(e, pf, pu, 0, 31); /* 1 if u0 < U */ 387 imadi(e, pu, one, 0, 1, pu); /* u0 - U + 1 */ 388 imadi(e, t, one, 0, 0xffffffffu, ureg); /* U - 1 */ 389 imadr(e, pu, pf, pu, 0, t); /* u0 < U ? u0 : U - 1 */ 390 imadw(e, pw, pu, 2u * R * row_bytes, pl); 391 movi(e, pf, 1); 392 weight_loads(e, block, R, D, wq, wr, pw, step_bytes, row_bytes, udesc); 393 } 394 const int L_s = here(e); 395 loop_begin(e); 396 ldg_wide(e, BW_IR_LDG_E_128, xq, pxa, 0, udesc, !pre); /* behind the prefetch: no wait for its data */ 397 for (uint32_t j = 0; j < 8; j++) { 398 const unsigned sh = 8u * (j & 3u); 399 int src[2] = { va, vb }; const unsigned sub[2] = { j >> 2, 2u + (j >> 2) }; 400 for (int q = 0; q < 2; q++) { 401 if (sh) { shr(e, src[q], xq, sub[q], sh); andi(e, src[q], src[q], 0, 0xffu); } 402 else andi(e, src[q], xq, sub[q], 0xffu); 403 andi(e, t, src[q], 0, 0x80u); 404 imadi(e, src[q], t, 0, 0xfffffffeu, src[q]); /* byte - 2 * (byte & 0x80): sign extension */ 405 } 406 imadi(e, X, va, 0, 65536u, vb); 407 if (spec->mutant == 1) imadi(e, X, X, 0, 0xffffffffu, NOREG); /* flipped planes: plus acts as minus */ 408 imadi(e, sad, one, 0, 4u * j, sb); 409 sts32(e, sad, X); 410 } 411 imadi(e, c, one, 0, TH, c); 412 imadi(e, rc, one, 0, 0xffffffffu, rc); 413 x_stage_addr(e, c, kwm1, one, px, cc, pxa, sb, va, vb); /* next round (unused after the last) */ 414 isetp_ge_u32(e, rc, one); 415 bra_if_p0(e, L_s); 416 loop_end(e); 417 bar_sync(e); 418 } 419 420 /* ---- coalesced output (single chunk): per-CTA round variables, see the header ---- */ 421 const int coal = omega_tern_gemv_coalesced_out(spec); 422 const uint32_t slot_bytes = omega_tern_gemv_out_slot_bytes(spec), x_bytes = KC * 2u, nrow = 2u * R * W; 423 int uc = NOREG, ueff = NOREG, um1 = NOREG, negU = NOREG, okw = NOREG, tu = NOREG, lg0 = NOREG, sbase = NOREG, stog = NOREG; 424 int so = NOREG, sa = NOREG, wl1 = NOREG, val = NOREG; 425 if (coal) { 426 uc = V(e); ueff = V(e); um1 = V(e); negU = V(e); okw = V(e); tu = V(e); lg0 = V(e); sbase = V(e); stog = V(e); 427 so = V(e); sa = V(e); wl1 = V(e); val = V(e); 428 if (e->err) return -1; 429 if (x_bytes & slot_bytes) return -1; /* the XOR toggle of sbase needs disjoint bits: x_bytes % 512 == 0, slot_bytes <= 128 */ 430 imadi(e, lg0, nzl1, 0, 1, NOREG); imadi(e, lg0, one, 0, 0xffffffffu, lg0); /* (l > 0) */ 431 imadi(e, um1, one, 0, 0xffffffffu, ureg); /* U - 1 */ 432 imadi(e, negU, ureg, 0, 0xffffffffu, NOREG); /* -U */ 433 movi(e, sbase, x_bytes); /* slot set of this round (alternates) */ 434 movi(e, stog, spec->mutant == 5 ? 0u : slot_bytes); /* mutant 5: one slot set, the round race the second set prevents (omega train r4 mutant 4) */ 435 imadi(e, so, warp, 0, 2u * R * 4u, NOREG); imadi(e, so, h, 0, R * 4u, so); /* this half-warp's slot: (2 warp + h) R words */ 436 imadi(e, tu, lane, 0, 0xffffffffu, NOREG); imadi(e, tu, one, 0, nrow - 1u, tu); shr(e, tu, tu, 0, 31); /* 1 if lane >= 2 R W */ 437 imadi(e, wl1, warp, 0, 1, tu); imadi(e, wl1, one, 0, 1, wl1); /* warp + (lane >= 2 R W) + 1 */ 438 imadi(e, uc, cx, 0, W, NOREG); /* first unit of this CTA's round (uniform over the CTA) */ 439 } 440 441 /* ---- unit loop ---- */ 442 imadi(e, u, u0, 0, 1, NOREG); 443 const int L_u = here(e); 444 loop_begin(e); 445 isetp_ge_u32(e, coal ? uc : u, ureg); /* coalesced: the whole CTA leaves together (barriers inside) */ 446 const int bra_end = bra_if_p0_fwd(e); 447 if (coal) { 448 /* a warp past the last unit recomputes unit U - 1; its rows are >= n_padded >= n and are never stored */ 449 imadi(e, tu, u, 0, 1, negU); /* u - U */ 450 shr(e, okw, tu, 0, 31); /* 1 if u < U */ 451 imadi(e, tu, one, 0, 1, tu); /* u - U + 1 */ 452 if (spec->mutant == 6) imadi(e, ueff, u, 0, 1, NOREG); /* mutant 6: no clamp, a warp past U reads past the tensor (omega train r4 mutant 5) */ 453 else imadr(e, ueff, okw, tu, 0, um1); /* u < U ? u : U - 1 */ 454 } else ueff = u; 455 imadi(e, rowbase, ueff, 0, 2u * R, hR); 456 imadw(e, pw, ueff, 2u * R * row_bytes, pl); 457 imadi(e, xlc, xl, 0, 1, NOREG); 458 for (uint32_t r = 0; r < R; r++) movrz(e, T[r]); 459 movi(e, cnt, KSC / D); 460 461 /* ---- K loop: D steps per iteration, every weight load issued before any use ---- 462 * Rows are interleaved stage by stage (shift, two masks, two multiply-adds for every row before the next pair) so 463 * independent work sits between each producer and its user; tern_schedule_stalls then sets every stall from the 464 * real register dependencies. For D <= 4 the staged x of all D steps is read from shared memory together with the 465 * weight loads (one wait per iteration); above that per step as before. */ 466 const int lds_up = D <= 4u; 467 const int L_k = here(e); 468 loop_begin(e); 469 if (pre) { 470 /* the prefetched first iteration skips its loads (pf set); the staged x reads go out before the predicate */ 471 if (lds_up) for (uint32_t d = 0; d < D; d++) for (uint32_t i = 0; i < 2; i++) lds128(e, xs[2u * d + i], xlc, d * 512u + 16u * i, d == 0 && i == 0); 472 isetp_ge_u32(e, pf, one); 473 Region rl = region_begin(e); 474 weight_loads(e, block, R, D, wq, wr, pw, step_bytes, row_bytes, udesc); 475 region_end(e, rl); 476 movi(e, pf, 0); 477 } else { 478 weight_loads(e, block, R, D, wq, wr, pw, step_bytes, row_bytes, udesc); 479 if (lds_up) for (uint32_t d = 0; d < D; d++) for (uint32_t i = 0; i < 2; i++) lds128(e, xs[2u * d + i], xlc, d * 512u + 16u * i, 0); 480 } 481 for (uint32_t d = 0; d < D; d++) { 482 const int *xd = lds_up ? &xs[2u * d] : xs; 483 if (!lds_up) for (uint32_t i = 0; i < 2; i++) lds128(e, xs[i], xlc, d * 512u + 16u * i, i == 0); 484 for (uint32_t j = 0; j < npairs; j++) { 485 for (uint32_t r = 0; r < R && j; r++) shr(e, Ut[r], block ? wq[d] : wr[r][d], block ? r : 0u, 2u * j); 486 for (uint32_t r = 0; r < R; r++) andi(e, P[r], j ? Ut[r] : (block ? wq[d] : wr[r][d]), j ? 0u : (block ? r : 0u), 0x00010001u); 487 for (uint32_t r = 0; r < R; r++) andi(e, M[r], j ? Ut[r] : (block ? wq[d] : wr[r][d]), j ? 0u : (block ? r : 0u), 0x00020002u); 488 for (uint32_t r = 0; r < R; r++) imadr(e, ap[r], P[r], xd[j >> 2], j & 3u, j == 0 ? k0 : ap[r]); /* + P * X_j */ 489 for (uint32_t r = 0; r < R; r++) imadr(e, am[r], M[r], xd[j >> 2], j & 3u, j == 0 ? k0 : am[r]); /* + M * X_j (minus bits one higher) */ 490 } 491 for (uint32_t r = 0; r < R; r++) shr(e, P[r], ap[r], 0, 16); /* 2^15 + plus dot of this word */ 492 for (uint32_t r = 0; r < R; r++) shr(e, M[r], am[r], 0, 17); /* 2^14 + minus dot of this word */ 493 for (uint32_t r = 0; r < R; r++) imadi(e, T[r], P[r], 0, 1, T[r]); 494 for (uint32_t r = 0; r < R; r++) imadi(e, T[r], M[r], 0, 0xffffffffu, T[r]); 495 } 496 imadw(e, pw, one, D * step_bytes, pw); 497 imadi(e, xlc, one, 0, D * 512u, xlc); 498 imadi(e, cnt, one, 0, 0xffffffffu, cnt); 499 isetp_ge_u32(e, cnt, one); 500 bra_if_p0(e, L_k); 501 loop_end(e); 502 503 /* ---- half-warp reduction (lanes 0 and 16 hold the sums), lane l == 0 stores rows < n ---- */ 504 for (uint32_t r = 0; r < R; r++) 505 for (uint32_t o = 8; o >= 1; o /= 2) { shfl_down(e, t, T[r], o); imadi(e, T[r], t, 0, 1, T[r]); } 506 if (coal) { 507 /* lane l == 0 of each half-warp: finish its R rows and park them in this round's slot set */ 508 isetp_ge_u32(e, lg0, one); 509 Region rs = region_begin(e); 510 imadi(e, sa, sbase, 0, 1, so); /* sbase + so */ 511 for (uint32_t r = 0; r < R; r++) { 512 imadi(e, yv, one, 0, 0u - KSC * (1u << 18), T[r]); /* y = T - KSC * 2^18 (mod 2^32, exact) */ 513 if (f32) { 514 if (!e->i2f_fixed) i2f(e, f, yv); /* first, so the segment lookup covers its >= 19 cycles */ 515 imadi(e, row, one, 0, r, rowbase); 516 for (int i = 0; i < 3; i++) { 517 imadi(e, t, row, 0, 1, nb[i]); 518 shr(e, t, t, 0, 31); /* 1 if row < b_i */ 519 if (i == 0) imadi(e, sum, t, 0, 1, NOREG); else imadi(e, sum, t, 0, 1, sum); 520 } 521 imadw(e, pwsa, sum, 4u, pws); /* ws_rev[boundaries above row] = w_scale[seg] */ 522 ldg32(e, wsv, pwsa, 0, udesc, 1); 523 if (e->i2f_fixed) i2f(e, f, yv); 524 fmul(e, f, f, asv); 525 fmul(e, f, f, wsv); 526 if (r) imadi(e, sa, one, 0, 4u, sa); 527 sts32(e, sa, f); 528 } else { 529 if (r) imadi(e, sa, one, 0, 4u, sa); 530 sts32(e, sa, yv); 531 } 532 } 533 region_end(e, rs); 534 bar_sync(e); 535 /* warp 0, lanes < 2 R W: row = 2 R uc + lane, stored when < n, as one coalesced STG */ 536 imadi(e, row, uc, 0, 2u * R, lane); 537 imadi(e, okr, row, 0, 1, negn); 538 shr(e, okr, okr, 0, 31); /* 1 if row < n */ 539 imadi(e, skip, okr, 0, 0xffffffffu, wl1); /* warp + (lane >= 2 R W) + 1 - (row < n): 0 only for a store */ 540 isetp_ge_u32(e, skip, one); 541 Region rg = region_begin(e); 542 imadi(e, sa, lane, 0, 4u, sbase); 543 lds32(e, val, sa); 544 imadw(e, pya, row, 4u, py); 545 stg(e, pya, 0, val, udesc); 546 region_end(e, rg); 547 xorr(e, sbase, sbase, stog); /* next round parks in the other slot set */ 548 imadi(e, uc, stride, 0, 1, uc); 549 } 550 for (uint32_t r = 0; r < R && !coal; r++) { 551 imadi(e, row, one, 0, r, rowbase); 552 imadi(e, okr, row, 0, 1, negn); 553 shr(e, okr, okr, 0, 31); /* 1 if row < n */ 554 imadi(e, skip, okr, 0, 0xffffffffu, nzl1); /* (l > 0) + 1 - (row < n): 0 only for a store */ 555 isetp_ge_u32(e, skip, one); 556 Region rg = region_begin(e); 557 imadi(e, yv, one, 0, 0u - KSC * (1u << 18), T[r]); /* y = T - KSC * 2^18 (mod 2^32, exact) */ 558 imadw(e, pya, row, 4u, py); 559 if (NCH > 1) { ldg32(e, prev, pya, 0, udesc, 1); imadr(e, yv, prev, mul, 0, yv); } /* + earlier chunks */ 560 if (f32) { 561 if (!e->i2f_fixed) i2f(e, f, yv); /* first, so the segment lookup covers its >= 19 cycles */ 562 for (int i = 0; i < 3; i++) { 563 imadi(e, t, row, 0, 1, nb[i]); 564 shr(e, t, t, 0, 31); /* 1 if row < b_i */ 565 if (i == 0) imadi(e, sum, t, 0, 1, NOREG); else imadi(e, sum, t, 0, 1, sum); 566 } 567 imadw(e, pwsa, sum, 4u, pws); /* ws_rev[boundaries above row] = w_scale[seg] */ 568 ldg32(e, wsv, pwsa, 0, udesc, 1); 569 if (e->i2f_fixed) i2f(e, f, yv); 570 fmul(e, f, f, asv); 571 fmul(e, f, f, wsv); 572 stg(e, pya, 0, f, udesc); 573 } else stg(e, pya, 0, yv, udesc); 574 region_end(e, rg); 575 } 576 imadi(e, u, stride, 0, 1, u); 577 bra(e, L_u); 578 loop_end(e); 579 patch_bra(e, bra_end, here(e)); 580 581 if (NCH > 1) { 582 imadw(e, px, one, KC, px); 583 imadw(e, pl, one, chunk_bytes, pl); 584 imadi(e, mul, one, 0, 1, NOREG); 585 imadi(e, chc, one, 0, 0xffffffffu, chc); 586 isetp_ge_u32(e, chc, one); 587 bra_if_p0(e, L_c); 588 loop_end(e); 589 } 590 tail(e); 591 if (e->err) return -1; 592 if (omega_bw_regalloc_solve(prog) != 0) return -1; 593 if (tern_schedule_stalls(prog, spec->mutant == 4 ? OMEGA_TERN_FIXED_LAT : OMEGA_TERN_I2FP_LAT) != 0) return -1; 594 return omega_tern_i2fp_violations(prog) == 0 ? 0 : -1; 595 } 596 597 int omega_tern_gemv_prog_stats(const OmegaTernGemvSpec *spec, uint32_t *insns, uint32_t *gprs) { 598 static BlackwellIRProgram prog; 599 if (omega_blackwell_codegen_tern_gemv_prog(spec, &prog) != 0) return -1; 600 if (insns) *insns = (uint32_t)prog.count; 601 if (gprs) *gprs = prog.regalloc.peak_gpr_usage; 602 return 0; 603 } 604 #else 605 606 /* C owns API/null handling and serialization only. Policy, construction, branches, 607 * allocation requests, loop events and every scheduling decision are seeded Omega. */ 608 static uint64_t omega_tern_policy(uint64_t query, const OmegaTernGemvSpec *s) { 609 uint64_t result = 0; 610 if (!s) return 0; 611 if (omega_cg_tern_policy(&result, query, s->k, 612 (uint64_t)s->layout | ((uint64_t)s->rows << 32), 613 (uint64_t)s->warps | ((uint64_t)s->depth << 32), 614 (uint64_t)s->epi | ((uint64_t)(uint32_t)s->mutant << 32), 0) != OMEGA_OK) 615 return 0; 616 return result; 617 } 618 uint32_t omega_tern_gemv_chunks(const OmegaTernGemvSpec *s) { return (uint32_t)omega_tern_policy(1, s); } 619 int omega_tern_gemv_spec_ok(const OmegaTernGemvSpec *s) { return (int)omega_tern_policy(0, s); } 620 uint32_t omega_tern_gemv_steps(const OmegaTernGemvSpec *s) { return (uint32_t)omega_tern_policy(2, s); } 621 int omega_tern_gemv_coalesced_out(const OmegaTernGemvSpec *s) { return (int)omega_tern_policy(3, s); } 622 uint32_t omega_tern_gemv_out_slot_bytes(const OmegaTernGemvSpec *s) { return (uint32_t)omega_tern_policy(4, s); } 623 uint32_t omega_tern_gemv_shared_bytes(const OmegaTernGemvSpec *s) { return (uint32_t)omega_tern_policy(5, s); } 624 uint64_t omega_tern_word_offset(uint32_t layout, uint32_t rows, uint32_t k, uint32_t row, uint32_t col) { 625 uint64_t result = 0; 626 if (omega_cg_tern_policy(&result, 6, layout, rows, k, row, col) != OMEGA_OK) return 0; 627 return result; 628 } 629 630 static void omega_tern_schedule_pack(const BlackwellIRProgram *p, uint64_t *b, uint64_t *m) { 631 for (unsigned v = 0; v < BW_MAX_VREGS; ++v) m[v] = UINT64_MAX; 632 for (int v = 0; v < p->regalloc.num_vregs; ++v) { 633 m[v] = (uint64_t)(int64_t)p->regalloc.vreg_to_phys[v]; 634 m[BW_MAX_VREGS + v] = p->regalloc.intervals[v].bundle_size; 635 } 636 for (size_t i = 0; i < p->count; ++i) { 637 const BlackwellIRInsn *in = &p->insns[i]; 638 b[8*i] = in->op; b[8*i+1] = in->control; b[8*i+2] = in->imm; 639 b[8*i+3] = (uint64_t)(int64_t)in->dst_vreg; 640 b[8*i+4] = (uint64_t)(int64_t)in->src1_vreg; 641 b[8*i+5] = (uint64_t)(int64_t)in->src2_vreg; 642 b[8*i+6] = (uint64_t)(int64_t)in->src3_vreg; 643 } 644 } 645 long omega_tern_i2fp_violations(const BlackwellIRProgram *p) { 646 uint64_t b[8192] = {0}, m[512] = {0}, result = 0; 647 if (!p) return -1; 648 omega_tern_schedule_pack(p, b, m); 649 if (omega_cg_tern_sched(&result, 1, p->count, 19, b, 8192, m, 512) != OMEGA_OK || result == UINT64_MAX) 650 return -1; 651 return (long)result; 652 } 653 int omega_blackwell_codegen_tern_gemv_prog(const OmegaTernGemvSpec *s, BlackwellIRProgram *p) { 654 uint64_t b[8192] = {0}, m[512] = {0}, result = 0; 655 if (!s || !p) return -1; 656 if (omega_gpu_om_ir_replay(omega_cg_tern, s->k, 657 (uint64_t)s->layout | ((uint64_t)s->rows << 32), 658 (uint64_t)s->warps | ((uint64_t)s->depth << 32), 659 (uint64_t)s->epi | ((uint64_t)(uint32_t)s->mutant << 32), p, NULL) != OMEGA_OM_IR_OK) 660 return -1; 661 omega_tern_schedule_pack(p, b, m); 662 /* The mutant latency is selected inside Omega from the original selector. */ 663 if (omega_cg_tern_sched(&result, 0, p->count, (uint32_t)s->mutant, b, 8192, m, 512) != OMEGA_OK || result != 0) 664 return -1; 665 for (size_t i = 0; i < p->count; ++i) p->insns[i].control = (uint32_t)b[8*i+1]; 666 if (omega_cg_tern_sched(&result, 1, p->count, 19, b, 8192, m, 512) != OMEGA_OK || result != 0) 667 return -1; 668 return 0; 669 } 670 int omega_tern_gemv_prog_stats(const OmegaTernGemvSpec *s, uint32_t *insns, uint32_t *gprs) { 671 static BlackwellIRProgram p; 672 if (omega_blackwell_codegen_tern_gemv_prog(s, &p) != 0) return -1; 673 if (insns) *insns = (uint32_t)p.count; 674 if (gprs) *gprs = p.regalloc.peak_gpr_usage; 675 return 0; 676 } 677 #endif