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_bw_em.h # 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_bw_em.h` 229 lines · 13534 bytes Raw Blame History 1 /* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception */ 2 /* omega_bw_em.h: the IR emitter helpers shared by the OWN-30B tensor-core generators (T3/T3b; private, static, header-only). 3 * Moved verbatim out of omega_blackwell_codegen_tc.c (T3 cut 1, chip-proven scheduling rules); see that file for the history. */ 4 #ifndef OMEGA_BW_EM_H 5 #define OMEGA_BW_EM_H 6 #include "omega_blackwell_codegen_tc.h" 7 #include 8 #pragma GCC diagnostic ignored "-Wunused-function" 9 #if defined(OMEGA_GPU_NO_POSIX) || defined(CUDAPP_MIGRATION_PENDING_C_EM) 10 #define BW_EM_C_PRIMARY 1 11 #else 12 #include "cudapp_native.h" 13 #endif 14 15 #define CW(stall, wbar, rbar, wait) \ 16 (((uint32_t)(stall) << 9) | ((uint32_t)(wbar) << 14) | ((uint32_t)(rbar) << 17) | ((uint32_t)(wait) << 20)) 17 #define CW_YIELD 0x2000u 18 #define SB_NONE 7u 19 #define SB_GPR 0u 20 #define SB_RD 1u 21 #define SB_UGPR 2u 22 #define NOREG (-1) 23 #define MAX_LOOPS 4 24 25 enum { K_FIXED, K_PRED, K_VAR, K_VAR_MORE, K_UVAR, K_STORE, K_SYNC, K_EXIT, K_BRA, K_BSSY, K_BSYNC }; 26 27 typedef struct { 28 BlackwellIRProgram *p; 29 uint32_t pending, held; 30 int err; 31 int open_start[MAX_LOOPS]; 32 int depth; 33 } Em; 34 35 #ifndef BW_EM_C_PRIMARY 36 /* Fixed wire carries only the mutable scoreboard and control fields. */ 37 static int64_t em_step(Em *e, uint64_t act, int64_t a, int64_t b, int64_t c, int end, uint32_t *cw) { 38 uint64_t w[4] = {e->pending, e->held, 0, (uint32_t)end}, r = UINT64_MAX; 39 uint32_t status = omega_cg_em(&r, act, a, b, c, w, 4); 40 e->pending = (uint32_t)w[0]; e->held = (uint32_t)w[1]; 41 if (cw) *cw = (uint32_t)w[2]; 42 return status ? -1 : (int64_t)r; 43 } 44 #endif 45 46 static void emit(Em *e, BlackwellIRInsn in, int kind) { 47 #ifdef BW_EM_C_PRIMARY 48 uint32_t wait = e->pending; 49 switch (kind) { 50 case K_FIXED: in.control = CW(6, SB_NONE, SB_NONE, wait); e->pending = 0; break; 51 case K_PRED: in.control = CW(14, SB_NONE, SB_NONE, wait); e->pending = 0; break; 52 /* Loads read their address operands late (the read barrier SB_RD): a later instruction must not overwrite an 53 * in-flight load's address register. Every load sets SB_RD and the next load of a batch waits on it (cheap: 54 * the operand read, not the data). Found on the chip: the deep k=2048 kernel hung when the allocator reused a 55 * dead scale address register as the next load's destination (evidence OWN30B-FP8-GEMV-`c4006391`). */ 56 case K_VAR: in.control = CW(4, SB_GPR, SB_RD, wait) | CW_YIELD; e->pending = (1u << SB_GPR) | (1u << SB_RD); break; 57 /* a further load of the same group: waits on nothing, joins the counting scoreboard */ 58 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; 59 case K_UVAR: in.control = CW(4, SB_UGPR, SB_NONE, wait) | CW_YIELD; e->pending = 1u << SB_UGPR; break; 60 case K_STORE: in.control = CW(4, SB_NONE, SB_RD, wait) | CW_YIELD; e->pending = 1u << SB_RD; break; 61 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 */ 62 case K_EXIT: in.control = CW(5, SB_NONE, SB_NONE, wait); e->pending = in.predicate_p0 ? wait : 0; break; 63 case K_BRA: in.control = CW(5, SB_NONE, SB_NONE, wait); e->pending = in.predicate_p0 ? wait : 0; break; 64 case K_BSSY: in.control = CW(1, SB_NONE, SB_NONE, wait) | CW_YIELD; e->pending = 0; break; 65 case K_BSYNC: in.control = CW(5, SB_NONE, SB_NONE, wait) | CW_YIELD; e->pending = 0; break; 66 default: e->err = -1; return; 67 } 68 #else 69 if (em_step(e, 0, kind, in.predicate_p0, 0, 0, &in.control) < 0) { e->err = -1; return; } 70 #endif 71 if (omega_bw_ir_append(e->p, &in) < 0) e->err = -1; 72 } 73 74 static BlackwellIRInsn I0(BlackwellIROpcode op) { 75 BlackwellIRInsn in; memset(&in, 0, sizeof in); 76 in.op = op; in.dst_vreg = NOREG; in.src1_vreg = NOREG; in.src2_vreg = NOREG; in.src3_vreg = NOREG; in.ureg = NOREG; 77 return in; 78 } 79 static int V(Em *e) { int v = omega_bw_ir_alloc_vreg(e->p); if (v < 0) e->err = -1; return v; } 80 static int V64(Em *e) { int v = omega_bw_ir_alloc_vreg64(e->p); if (v < 0) e->err = -1; return v; } 81 static int V128(Em *e) { int v = omega_bw_ir_alloc_vreg128(e->p); if (v < 0) e->err = -1; return v; } 82 static int UV64(Em *e) { int v = omega_bw_ir_alloc_uvreg64(e->p); if (v < 0) e->err = -1; return v; } 83 static int here(const Em *e) { return (int)e->p->count; } 84 85 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); } 86 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); } 87 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); } 88 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); } 89 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); } 90 static void movrz(Em *e, int d) { BlackwellIRInsn i = I0(BW_IR_MOV_RZ); i.dst_vreg = d; emit(e, i, K_FIXED); } 91 /* d = a * imm + c (c NOREG = RZ); a may be a sub-register of a quad */ 92 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); } 93 /* d = a * b + c (registers) */ 94 static void imadr(Em *e, int d, int a, int b, int c) { BlackwellIRInsn i = I0(BW_IR_IMAD); i.dst_vreg = d; i.src1_vreg = a; i.src2_vreg = b; i.src3_vreg = c; emit(e, i, K_FIXED); } 95 /* d64 = a * imm + c64 */ 96 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); } 97 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); } 98 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); } 99 static void ldg_wide(Em *e, BlackwellIROpcode op, int d, int addr64, uint32_t imm, int udesc, int first) { 100 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); 101 } 102 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); } 103 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); } 104 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); } 105 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); } 106 static void bar_sync(Em *e) { emit(e, I0(BW_IR_BAR_SYNC), K_SYNC); } 107 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); } 108 /* d = a * b[bsub] + c */ 109 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); } 110 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); } 111 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); } 112 static uint32_t em_rel(Em *e, int target, int at) { 113 #ifdef BW_EM_C_PRIMARY 114 (void)e; 115 return (uint32_t)(target - at); 116 #else 117 return (uint32_t)em_step(e, 8, target, at, 0, 0, NULL); 118 #endif 119 } 120 static void bra_if_p0(Em *e, int target) { BlackwellIRInsn i = I0(BW_IR_BRA); i.predicate_p0 = true; i.imm = em_rel(e, target, here(e)); emit(e, i, K_BRA); } 121 static void bra(Em *e, int target) { BlackwellIRInsn i = I0(BW_IR_BRA); i.imm = em_rel(e, target, here(e)); emit(e, i, K_BRA); } 122 /* a forward branch whose target is patched when known: returns the instruction index */ 123 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; } 124 static void patch_bra(Em *e, int at, int target) { if (!e->err) e->p->insns[at].imm = em_rel(e, target, at); } 125 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; } 126 static void bsync(Em *e) { BlackwellIRInsn i = I0(BW_IR_BSYNC); i.bar_reg = 0; emit(e, i, K_BSYNC); } 127 static void tail(Em *e) { 128 emit(e, I0(BW_IR_EXIT), K_EXIT); 129 BlackwellIRInsn self = I0(BW_IR_BRA); 130 self.control = 0x000fc000u; 131 if (omega_bw_ir_append(e->p, &self) < 0) e->err = -1; 132 } 133 134 /* Loops: a value defined before the loop and read inside it stays live until the back-branch 135 * (the linear-scan allocator knows no back edges; same rule as the elementwise emitter). */ 136 static void loop_begin(Em *e) { 137 #ifdef BW_EM_C_PRIMARY 138 if (e->depth >= MAX_LOOPS) { e->err = -1; return; } 139 e->open_start[e->depth++] = here(e); 140 #else 141 int d = e->depth; 142 int next = (int)em_step(e, 3, d, 0, 0, 0, NULL); 143 if (next < 0) { e->err = -1; return; } 144 e->open_start[d] = here(e); e->depth = next; 145 #endif 146 } 147 static void loop_end(Em *e) { 148 #ifdef BW_EM_C_PRIMARY 149 if (e->depth <= 0) { e->err = -1; return; } 150 int start = e->open_start[--e->depth], end = here(e) - 1; 151 #else 152 int d = (int)em_step(e, 4, e->depth, 0, 0, 0, NULL); 153 if (d < 0) { e->err = -1; return; } 154 e->depth = d; 155 int start = e->open_start[d], end = here(e) - 1; 156 #endif 157 OmegaRegAlloc *ra = &e->p->regalloc; 158 for (int v = 0; v < ra->num_vregs; v++) { 159 OmegaLiveInterval *iv = &ra->intervals[v]; 160 #ifdef BW_EM_C_PRIMARY 161 if (iv->first_def >= 0 && iv->first_def < start && iv->last_use >= start && iv->last_use < end) iv->last_use = end; 162 #else 163 iv->last_use = (int)em_step(e, 5, iv->first_def, start, iv->last_use, end, NULL); 164 #endif 165 } 166 } 167 168 169 /* A weight/scale prefetch load: scoreboard `sb` (3 or 4) is held, not waited for, until consume() */ 170 static void ldw_pf(Em *e, BlackwellIROpcode op, int d, int addr64, uint32_t imm, int udesc, unsigned sb) { 171 BlackwellIRInsn in = I0(op); in.dst_vreg = d; in.src1_vreg = addr64; in.imm = imm; in.ureg = udesc; 172 #ifdef BW_EM_C_PRIMARY 173 in.control = CW(4, sb, SB_RD, (e->pending & (1u << SB_RD))) | CW_YIELD; 174 e->pending |= 1u << SB_RD; e->held |= 1u << sb; 175 #else 176 if (em_step(e, 1, sb, 0, 0, 0, &in.control) < 0) { e->err = -1; return; } 177 #endif 178 if (omega_bw_ir_append(e->p, &in) < 0) e->err = -1; 179 } 180 static void consume(Em *e, unsigned sb) { 181 #ifdef BW_EM_C_PRIMARY 182 e->pending |= e->held & (1u << sb); e->held &= ~(1u << sb); 183 #else 184 if (em_step(e, 2, sb, 0, 0, 0, NULL) < 0) e->err = -1; 185 #endif 186 } 187 188 static int log2u(uint32_t v) { 189 #ifdef BW_EM_C_PRIMARY 190 int l = 0; while (l < 31 && (1u << l) < v) l++; return (1u << l) == v ? l : -1; 191 #else 192 Em e = {0}; 193 return (int)em_step(&e, 6, v, 0, 0, 0, NULL); 194 #endif 195 } 196 197 /* HMMA d = a * b + c (a quad, b pair, c/d quad; c NOREG = RZ). stall 8: the next instruction must not read d (nvcc's 198 * spacing for independent HMMAs); hmma_wait15 + nop14 before any reader of d. */ 199 static void hmma(Em *e, int d, int a, int b, int c, uint32_t stall) { 200 BlackwellIRInsn i = I0(BW_IR_HMMA_BF16); i.dst_vreg = d; i.src1_vreg = a; i.src2_vreg = b; i.src3_vreg = c; 201 #ifdef BW_EM_C_PRIMARY 202 i.control = CW(stall, SB_NONE, SB_NONE, e->pending); e->pending = 0; 203 #else 204 if (em_step(e, 7, stall, 0, 0, 0, &i.control) < 0) { e->err = -1; return; } 205 #endif 206 if (omega_bw_ir_append(e->p, &i) < 0) e->err = -1; 207 } 208 static void nop(Em *e, uint32_t stall) { 209 BlackwellIRInsn i = I0(BW_IR_NOP); 210 #ifdef BW_EM_C_PRIMARY 211 i.control = CW(stall, SB_NONE, SB_NONE, e->pending); e->pending = 0; 212 #else 213 if (em_step(e, 7, stall, 0, 0, 0, &i.control) < 0) { e->err = -1; return; } 214 #endif 215 if (omega_bw_ir_append(e->p, &i) < 0) e->err = -1; 216 } 217 static void lop3(Em *e, int d, int a, int b, int c, uint32_t lut) { BlackwellIRInsn i = I0(BW_IR_LOP3_LUT); i.dst_vreg = d; i.src1_vreg = a; i.src2_vreg = b; i.src3_vreg = c; i.imm = lut; emit(e, i, K_FIXED); } 218 static void iadd(Em *e, int d, int a, int b) { BlackwellIRInsn i = I0(BW_IR_IADD3); i.dst_vreg = d; i.src1_vreg = a; i.src2_vreg = b; emit(e, i, K_FIXED); } 219 static void shfl_bfly(Em *e, int d, int a, uint32_t lane) { 220 BlackwellIRInsn i = I0(BW_IR_SHFL_BFLY); i.dst_vreg = d; i.src1_vreg = a; 221 #ifdef BW_EM_C_PRIMARY 222 i.imm = lane & 31u; 223 #else 224 i.imm = (uint32_t)em_step(e, 9, lane, 0, 0, 0, NULL); 225 #endif 226 emit(e, i, K_VAR); 227 } 228 static void f2fp_pack(Em *e, int d, int hi, int lo) { BlackwellIRInsn i = I0(BW_IR_F2FP_BF16_PACK); i.dst_vreg = d; i.src1_vreg = hi; i.src2_vreg = lo; emit(e, i, K_FIXED); } 229 #endif