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/omega-mv/blob/main/seed/sm121/sm121list.c # omega-mv Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance. Brought by drakestapleton Sign in to star 1 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `omega-mv / seed / sm121 / sm121list.c` 478 lines ยท 33758 bytes Raw Blame History 1 /* 2 * sm121list.c -- REFERENCE CHECK ONLY for rung 6 (seed/sm121/README.md). It never makes a byte of seed/. 3 * It runs the repo's own GPU code generators and C encoder (src/omega_blackwell_codegen*.c, the referee) 4 * and writes, for every program they produce, two files side by side: 5 * NAME.s2 the program as a text listing in asm2's language, one line per 32-bit word, using the names 6 * of seed/sm121/sm121.tbl (instruction name and its registers and numbers, read from the 7 * generator's instruction list, never from the encoded words); 8 * NAME.c.bin the bytes the C encoder made for the same program (after the two patch steps the callers 9 * apply), the answer asm2 must reproduce. 10 * gate6.sh then feeds table + listing to asm2 and compares. Modes: 11 * sm121list kernels DIR every generator over a sweep of its settings (refused settings are skipped) 12 * sm121list edges DIR every instruction form with registers and numbers at the edges of their fields 13 * Notes on what the C encoder silently ignores or truncates go to DIR/notes.tsv. 14 */ 15 #include 16 #include 17 #include 18 #include 19 #include 20 #include 21 #include 22 #include "../../src/omega_blackwell_codegen.h" 23 #include "../../src/omega_blackwell_codegen_attn.h" 24 #include "../../src/omega_blackwell_codegen_bf16.h" 25 #include "../../src/omega_blackwell_codegen_dec.h" 26 #include "../../src/omega_blackwell_codegen_gemm.h" 27 #include "../../src/omega_blackwell_codegen_moe.h" 28 #include "../../src/omega_blackwell_codegen_moe2.h" 29 #include "../../src/omega_blackwell_codegen_pattn.h" 30 #include "../../src/omega_blackwell_codegen_pf.h" 31 #include "../../src/omega_blackwell_codegen_pfhost.h" 32 #include "../../src/omega_blackwell_codegen_qg.h" 33 #include "../../src/omega_blackwell_codegen_qmma.h" 34 #include "../../src/omega_blackwell_codegen_router.h" 35 #include "../../src/omega_blackwell_codegen_tc.h" 36 #include "../../src/omega_blackwell_codegen_tc2.h" 37 #include "../../src/omega_blackwell_codegen_tern.h" 38 #include "../../src/omega_blackwell_encoder.h" 39 40 static const char *g_dir; 41 static FILE *g_notes; 42 static unsigned long g_kernels, g_dups, g_refused, g_insns; 43 44 /* Generated outputs require fresh paths; never truncate an existing file or 45 * follow a final-component symlink. gate6.sh cleans only its build outputs. */ 46 static FILE *private_output(const char *path, const char *mode) { 47 int fd = open(path, O_WRONLY | O_CREAT | O_EXCL | O_NOFOLLOW | O_CLOEXEC, 0600); 48 if (fd < 0) return NULL; 49 FILE *f = fdopen(fd, mode); 50 if (!f) close(fd); 51 return f; 52 } 53 54 static void note(const char *kernel, size_t i, const char *what) { 55 fprintf(g_notes, "%s\t%zu\t%s\n", kernel, i, what); 56 } 57 58 /* physical operands exactly as encode_single_insn derives them (codegen.c:265-273) */ 59 typedef struct { int d, a, b, c, u; } Ops; 60 static Ops ops_of(const BlackwellIRInsn *in, const OmegaRegAlloc *ra) { 61 Ops o; 62 o.d = in->dst_vreg >= 0 ? ra->vreg_to_phys[in->dst_vreg] + in->dst_subreg : 0; 63 o.a = in->src1_vreg >= 0 ? ra->vreg_to_phys[in->src1_vreg] + in->src1_subreg : 0; 64 o.b = in->src2_vreg >= 0 ? ra->vreg_to_phys[in->src2_vreg] + in->src2_subreg : 0; 65 o.c = in->src3_vreg >= 0 ? ra->vreg_to_phys[in->src3_vreg] + in->src3_subreg : 0xff; 66 o.u = in->ureg >= 0 ? ra->uvreg_to_phys[in->ureg] : 0; 67 if (in->is_uniform && in->dst_vreg >= 0) o.d = ra->uvreg_to_phys[in->dst_vreg]; 68 return o; 69 } 70 71 static void L(FILE *f, const char *fmt, ...) { 72 va_list ap; va_start(ap, fmt); 73 fputs(" ", f); vfprintf(f, fmt, ap); fputc('\n', f); 74 va_end(ap); 75 } 76 /* word 3: the given control word, or the instruction's default line */ 77 static void ctl(FILE *f, const BlackwellIRInsn *in, const char *dflt) { 78 if (in->control) L(f, "ctl 0x%08x", in->control); else L(f, "%s", dflt); 79 } 80 static int pred_of(const BlackwellIRInsn *in) { return in->predicate_p0 ? (in->predicate_not ? 8 : 0) : 7; } 81 82 /* patched: 1 when the program went through omega_qmma_patch_code */ 83 static int list_insn(FILE *f, const BlackwellIRInsn *in, const OmegaRegAlloc *ra, int patched, const char *k, size_t i) { 84 Ops o = ops_of(in, ra); 85 switch (in->op) { 86 case BW_IR_NOP: L(f, "nop.0"); L(f, "nop.1"); L(f, "nop.2"); ctl(f, in, "nop.3"); break; 87 case BW_IR_MOV_RZ: L(f, "mov.rz.0 %d", o.d); L(f, "mov.rz.1"); L(f, "mov.rz.2"); ctl(f, in, "mov.rz.3"); break; 88 case BW_IR_MOV_IMM: L(f, "mov.i.0 %d", o.d); L(f, "mov.i.1 0x%08x", in->imm); L(f, "mov.i.2"); ctl(f, in, "mov.i.3"); break; 89 case BW_IR_S2R: L(f, "s2r.0 %d", o.d); L(f, "s2r.1"); L(f, "s2r.2 %u", in->imm); ctl(f, in, "s2r.3"); break; 90 case BW_IR_LDC: case BW_IR_LDC64: { 91 const char *n = in->op == BW_IR_LDC ? "ldc" : "ldc.64"; 92 if (in->imm & 3u) note(k, i, "LDC offset not a multiple of 4: the C encoder drops the low bits"); 93 L(f, "%s.0 %d", n, o.d); L(f, "%s.1 %u", n, in->imm >> 2); L(f, "%s.2", n); ctl(f, in, in->op == BW_IR_LDC ? "ldc.3" : "ldc.64.3"); 94 break; 95 } 96 case BW_IR_LDCU: case BW_IR_LDCU64: { 97 const char *n = in->op == BW_IR_LDCU ? "ldcu" : "ldcu.64"; 98 if (in->imm & 7u) note(k, i, "LDCU offset not a multiple of 8: the C encoder drops the low bits"); 99 L(f, "%s.0 %d", n, o.d); L(f, "%s.1 %u", n, in->imm >> 3); L(f, "%s.2", n); ctl(f, in, in->op == BW_IR_LDCU ? "ldcu.3" : "ldcu.64.3"); 100 break; 101 } 102 case BW_IR_IMAD: 103 if (in->src2_vreg >= 0) { L(f, "imad.0 %d %d", o.d, o.a); L(f, "imad.1 %d", o.b); L(f, "imad.2 %d", o.c); ctl(f, in, "imad.3"); } 104 else if (in->is_uniform && in->ureg >= 0) { L(f, "imad.u.0 %d %d", o.d, o.a); L(f, "imad.u.1 %d", o.u); L(f, "imad.u.2 %d", o.c); ctl(f, in, "imad.u.3"); } 105 else { L(f, "imad.i.0 %d %d", o.d, o.a); L(f, "imad.i.1 0x%08x", in->imm); L(f, "imad.i.2 %d", o.c); ctl(f, in, "imad.i.3"); } 106 break; 107 case BW_IR_IMAD_WIDE: L(f, "imad.wide.0 %d %d", o.d, o.a); L(f, "imad.wide.1 0x%08x", in->imm); L(f, "imad.wide.2 %d", o.c); ctl(f, in, "imad.wide.3"); break; 108 case BW_IR_ISETP_GE: /* keeps a scheduled control word since omega-mv#545 (`e3a26848`) */ 109 if (in->is_uniform && in->ureg >= 0) { L(f, "isetp.ge.u.0 %d %d", o.d, o.a); L(f, "isetp.ge.u.1 %d", o.u); L(f, "isetp.ge.u.2"); ctl(f, in, "isetp.ge.u.3"); } 110 else { L(f, "isetp.ge.i.0 %d %d", o.d, o.a); L(f, "isetp.ge.i.1 0x%08x", in->imm); L(f, "isetp.ge.i.2"); ctl(f, in, "isetp.ge.i.3"); } 111 break; 112 case BW_IR_LDG_E: case BW_IR_LDG_E_U16: case BW_IR_LDG_STRONG_SYS: case BW_IR_LDG_MMIO: { 113 const char *w2 = in->op == BW_IR_LDG_E ? "ldg.2" : in->op == BW_IR_LDG_E_U16 ? "ldg.u16.2" : in->op == BW_IR_LDG_STRONG_SYS ? "ldg.strong.sys.2" : "ldg.mmio.2"; 114 L(f, "ldg.0 %d %d", o.d, o.a); L(f, "ldg.1 %d", o.u); L(f, "%s", w2); ctl(f, in, "ldg.3"); 115 break; 116 } 117 case BW_IR_LDG_E_64: case BW_IR_LDG_E_128: 118 L(f, "ldg.0 %d %d", o.d, o.a); L(f, "ldg.w.1 %d %d", o.u, (int32_t)in->imm); 119 L(f, "%s", in->op == BW_IR_LDG_E_64 ? "ldg.64.2" : "ldg.128.2"); ctl(f, in, "ldg.3"); 120 break; 121 case BW_IR_STG_E: L(f, "stg.0 %d", o.a); L(f, "stg.1 %d %d", o.b, (int32_t)in->imm); L(f, "stg.2 %d", o.u); ctl(f, in, "stg.3"); break; 122 case BW_IR_STG_EF: L(f, "stg.0 %d", o.a); L(f, "stg.ef.1 %d", o.b); L(f, "stg.ef.2 %d", o.u); ctl(f, in, "w.3"); break; 123 case BW_IR_STG_STRONG_SYS: 124 L(f, "stg.strong.sys.0 %d %d", pred_of(in), o.a); L(f, "r.1 %d", o.b); L(f, "stg.strong.sys.2 %d", o.u); ctl(f, in, "w.3"); 125 break; 126 case BW_IR_MEMBAR_ALL_SYS: L(f, "membar.all.sys.0"); L(f, "z.1"); L(f, "membar.all.sys.2"); ctl(f, in, "membar.3"); break; 127 case BW_IR_MEMBAR_SC_SYS: L(f, "membar.all.sys.0"); L(f, "z.1"); L(f, "membar.sc.sys.2"); ctl(f, in, "membar.3"); break; 128 case BW_IR_CCTL_IVALL: L(f, "cctl.ivall.0"); L(f, "z.1"); L(f, "cctl.ivall.2"); ctl(f, in, "cctl.ivall.3"); break; 129 case BW_IR_ATOMG_ADD_STRONG_SYS: case BW_IR_ATOMG_EXCH_STRONG_SYS: 130 L(f, "atomg.0 %d %d", o.d, o.a); L(f, "atomg.1 %d", o.b); 131 L(f, "%s %d", in->op == BW_IR_ATOMG_ADD_STRONG_SYS ? "atomg.add.2" : "atomg.exch.2", o.u); ctl(f, in, "atomg.3"); 132 break; 133 case BW_IR_ISETP_GE_U32: L(f, "isetp.ge.u32.0 %d", o.a); L(f, "r.1 %d", o.b); L(f, "isetp.ge.u32.2"); ctl(f, in, "isetp.ge.u32.3"); break; 134 case BW_IR_IADD3: /* keeps a scheduled control word since omega-mv#545 (`e3a26848`) */ 135 if (in->src3_vreg >= 0) note(k, i, "IADD3: the C encoder ignores the third source (always RZ)"); 136 L(f, "iadd3.0 %d %d", o.d, o.a); L(f, "r.1 %d", o.b); L(f, "iadd3.2"); ctl(f, in, "iadd3.3"); 137 break; 138 case BW_IR_HMMA_F16: case BW_IR_HMMA_BF16: 139 if (patched && omega_qmma_is_mark(in)) { L(f, "qmma.0 %d %d", o.d, o.a); L(f, "r.1 %d", o.b); L(f, "qmma.2 %d", o.c); } 140 else { L(f, "hmma.0 %d %d", o.d, o.a); L(f, "r.1 %d", o.b); L(f, "%s %d", in->op == BW_IR_HMMA_BF16 ? "hmma.bf16.2" : "hmma.f16.2", o.c); } 141 ctl(f, in, "w.3"); 142 break; 143 case BW_IR_SHF_R: L(f, "shf.r.hi.0 %d", o.d); L(f, "n.1 0x%08x", in->imm); L(f, "shf.r.hi.2 %d", o.a); ctl(f, in, "alu.3"); break; 144 case BW_IR_LOP3_AND: L(f, "lop3.and.i.0 %d %d", o.d, o.a); L(f, "n.1 0x%08x", in->imm); L(f, "lop3.and.i.2"); ctl(f, in, "alu.3"); break; 145 case BW_IR_LOP3_XOR: L(f, "lop3.0 %d %d", o.d, o.a); L(f, "r.1 %d", o.b); L(f, "lop3.xor.2"); ctl(f, in, "alu.3"); break; 146 case BW_IR_LOP3_LUT: case BW_IR_SHF_L_U32: case BW_IR_IMAD_HI_U32: { 147 int a = in->src1_vreg >= 0 ? o.a : 0xff, b = in->src2_vreg >= 0 ? o.b : 0xff; 148 const char *n0 = in->op == BW_IR_LOP3_LUT ? "lop3.0" : in->op == BW_IR_SHF_L_U32 ? "shf.l.u32.0" : "imad.hi.0"; 149 L(f, "%s %d %d", n0, o.d, a); L(f, "r.1 %d", b); 150 if (in->op == BW_IR_LOP3_LUT) L(f, "lop3.lut.2 %u %d", in->imm & 0xffu, o.c); 151 else L(f, "%s %d", in->op == BW_IR_SHF_L_U32 ? "shf.l.u32.2" : "imad.hi.2", o.c); 152 if (in->op == BW_IR_LOP3_LUT && in->imm > 0xffu) note(k, i, "LOP3.LUT: truth table above 8 bits, the C encoder keeps the low 8"); 153 ctl(f, in, "alu.3"); 154 break; 155 } 156 case BW_IR_FADD: L(f, "fadd.0 %d %d", o.d, o.a); L(f, "r.1 %d", o.b); L(f, "f.2"); ctl(f, in, "f.3"); break; 157 case BW_IR_FSUB: L(f, "fadd.0 %d %d", o.d, o.a); L(f, "fsub.1 %d", o.b); L(f, "f.2"); ctl(f, in, "f.3"); break; 158 case BW_IR_FMUL: L(f, "fmul.0 %d %d", o.d, o.a); L(f, "r.1 %d", o.b); L(f, "fmul.2"); ctl(f, in, "f.3"); break; 159 case BW_IR_FFMA: L(f, "ffma.0 %d %d", o.d, o.a); L(f, "r.1 %d", o.b); L(f, "r.2 %d", o.c); ctl(f, in, "f.3"); break; 160 case BW_IR_FSETP: 161 L(f, "fsetp.0 %d %d", o.d, o.a); L(f, "r.1 %d", o.b); 162 if (in->imm) L(f, "fsetp.2 0x%08x", in->imm); else L(f, "fsetp.ge.2"); 163 ctl(f, in, "f.3"); 164 break; 165 case BW_IR_FSEL: L(f, "fsel.0 %d %d", o.d, o.a); L(f, "r.1 %d", o.b); L(f, "fsel.2 %d", o.c & (in->src3_vreg >= 0 ? 0xff : 7)); ctl(f, in, "fsel.3"); break; 166 case BW_IR_FMNMX_MIN: L(f, "fmnmx.0 %d %d", o.d, o.a); L(f, "r.1 %d", o.b); L(f, "fmnmx.min.2"); ctl(f, in, "f.3"); break; 167 case BW_IR_FMNMX_MAX: 168 if (patched && in->imm == OMEGA_QMMA_MARK_F2E4) { L(f, "f2fp.0 %d %d", o.d, o.a); L(f, "r.1 %d", o.b); L(f, "f2fp.e4m3.2"); } 169 else { L(f, "fmnmx.0 %d %d", o.d, o.a); L(f, "r.1 %d", o.b); L(f, "fmnmx.max.2"); } 170 ctl(f, in, "f.3"); 171 break; 172 case BW_IR_F2FP_BF16_PACK: L(f, "f2fp.0 %d %d", o.d, o.a); L(f, "r.1 %d", o.b); L(f, "f2fp.bf16.2"); ctl(f, in, "f.3"); break; 173 case BW_IR_I2FP: L(f, "i2fp.0 %d", o.d); L(f, "r.1 %d", o.a); L(f, "i2fp.2"); ctl(f, in, "i2fp.3"); break; 174 case BW_IR_F2I: L(f, "f2i.0 %d", o.d); L(f, "r.1 %d", o.a); L(f, "f2i.2"); ctl(f, in, "f2i.3"); break; 175 case BW_IR_MUFU_RCP: case BW_IR_MUFU_RSQ: case BW_IR_MUFU_EX2: 176 L(f, "mufu.0 %d", o.d); L(f, "r.1 %d", o.a); 177 L(f, "%s", in->op == BW_IR_MUFU_RCP ? "mufu.rcp.2" : in->op == BW_IR_MUFU_RSQ ? "mufu.rsq.2" : "mufu.ex2.2"); ctl(f, in, "mufu.3"); 178 break; 179 case BW_IR_SHFL_DOWN: case BW_IR_SHFL_BFLY: 180 L(f, "shfl.0 %d %d", o.d, o.a); L(f, "%s %u", in->op == BW_IR_SHFL_BFLY ? "shfl.bfly.1" : "shfl.down.1", in->imm & 31u); L(f, "shfl.2"); ctl(f, in, "shfl.3"); 181 break; 182 case BW_IR_LDS: L(f, "lds.0 %d %d", o.d, o.a); L(f, "z.1"); L(f, "lds.u8.2 %u", in->imm); ctl(f, in, "w.3"); break; 183 case BW_IR_STS: L(f, "sts.u8.0 %d", o.a); L(f, "r.1 %d", o.b); L(f, "sts.u8.2 %u", in->imm); ctl(f, in, "w.3"); break; 184 case BW_IR_LDS128: L(f, "lds.0 %d %d", o.d, o.a); L(f, "lds.128.1 %u", in->imm); L(f, "lds.128.2"); ctl(f, in, "lds.128.3"); break; 185 case BW_IR_LDS32: L(f, "lds.0 %d %d", o.d, o.a); L(f, "lds.32.1"); L(f, "lds.32.2"); ctl(f, in, "lds.32.3"); break; 186 case BW_IR_STS32: L(f, "sts.32.0 %d", o.a); L(f, "r.1 %d", o.b); L(f, "sts.32.2"); ctl(f, in, "w.3"); break; 187 case BW_IR_EXIT: /* the C encoder keeps a scheduled control word on EXIT (omega-mv#545, `e3a26848`) */ 188 if (in->predicate_p0 && in->predicate_not) note(k, i, "EXIT: @!P0 asked, the C encoder writes @P0"); 189 L(f, "%s", in->predicate_p0 ? "exit.p0.0" : "exit.0"); L(f, "z.1"); L(f, "exit.2"); ctl(f, in, "exit.3"); 190 break; 191 case BW_IR_BRA: { 192 int32_t delta = (int32_t)in->imm, rel = delta - 1; 193 L(f, "bra.0 %d %d", pred_of(in), rel & 0x3f); L(f, "bra.1 %d", rel >> 6); L(f, "%s", rel < 0 ? "bra.back.2" : "bra.fwd.2"); 194 if (in->control) L(f, "ctl 0x%08x", in->control); 195 else L(f, "%s", delta == 0 && !in->predicate_p0 ? "bra.self.3" : "bra.3"); 196 break; 197 } 198 case BW_IR_BSSY: 199 L(f, "bssy.0 %d %u", pred_of(in), in->bar_reg); L(f, "bssy.1 %d", (int32_t)in->imm - 1); L(f, "b.2"); ctl(f, in, "w.3"); 200 break; 201 case BW_IR_BSYNC: L(f, "bsync.0 %d %u", pred_of(in), in->bar_reg); L(f, "z.1"); L(f, "b.2"); ctl(f, in, "jmp.3"); break; 202 case BW_IR_BAR_SYNC: L(f, "bar.sync.0"); L(f, "z.1"); L(f, "bar.sync.2"); ctl(f, in, "membar.3"); break; 203 default: return -1; 204 } 205 return 0; 206 } 207 208 /* ---- kernels ---- */ 209 #define NSEEN 65536 210 static uint64_t g_seen[NSEEN]; 211 static size_t g_nseen; 212 static uint64_t fnv(const uint8_t *p, size_t n) { 213 uint64_t h = 1469598103934665603ULL; 214 for (size_t i = 0; i < n; i++) { h ^= p[i]; h *= 1099511628211ULL; } 215 return h; 216 } 217 static uint8_t g_code[OMEGA_BW_CODE_CAP_MAX]; 218 static FILE *g_tsv; 219 220 static void dump(const char *name, const BlackwellIRProgram *p) { 221 size_t len = 0; 222 if (omega_bw_encode_program(p, g_code, sizeof g_code, &len) != 0) { note(name, 0, "C encoder refused the program"); return; } 223 int nq = omega_qmma_patch_code(p, g_code, len); 224 if (nq < 0) { note(name, 0, "patch step refused the program"); return; } 225 uint64_t h = fnv(g_code, len) ^ (uint64_t)len; 226 for (size_t i = 0; i < g_nseen; i++) if (g_seen[i] == h) { g_dups++; return; } 227 if (g_nseen < NSEEN) g_seen[g_nseen++] = h; 228 char path[512]; 229 snprintf(path, sizeof path, "%s/k/%s.s2", g_dir, name); 230 FILE *f = private_output(path, "w"); 231 if (!f) { perror(path); exit(2); } 232 fprintf(f, "; %s: %zu instructions (+ NOP padding), listing written by sm121list from the generator's instruction list\n", name, p->count); 233 size_t padded = (p->count + 7) & ~(size_t)7; 234 if (padded < 32) padded = 32; 235 for (size_t i = 0; i < padded; i++) { 236 if (i < p->count) { 237 if (list_insn(f, &p->insns[i], &p->regalloc, nq > 0, name, i) != 0) { note(name, i, "opcode the listing writer does not know"); } 238 } else { L(f, "nop.0"); L(f, "nop.1"); L(f, "nop.2"); L(f, "nop.3"); } 239 } 240 fclose(f); 241 snprintf(path, sizeof path, "%s/k/%s.c.bin", g_dir, name); 242 f = private_output(path, "wb"); 243 if (!f || fwrite(g_code, 1, len, f) != len) { perror(path); exit(2); } 244 fclose(f); 245 fprintf(g_tsv, "%s\t%zu\t%zu\t%d\n", name, p->count, len, nq); 246 g_kernels++; g_insns += p->count; 247 } 248 249 static BlackwellIRProgram g_p; 250 #define TRY(call, ...) do { char nm_[256]; snprintf(nm_, sizeof nm_, __VA_ARGS__); omega_bw_ir_init(&g_p); \ 251 if ((call) == 0) dump(nm_, &g_p); else g_refused++; } while (0) 252 253 static OmegaMatMulSpec mm(uint32_t m, uint32_t k, uint32_t n, OmegaMatMulPrecision pr) { 254 OmegaMatMulSpec s; memset(&s, 0, sizeof s); s.m = m; s.k = k; s.n = n; s.precision = pr; return s; 255 } 256 257 static void kernels(void) { 258 static const uint32_t W6[] = { 1, 2, 4, 8, 16, 32 }; 259 /* integer matmul, single-tile and looped tensor matmuls */ 260 { static const uint32_t ks[] = { 1, 2, 8, 32 }; 261 for (int a = 0; a < 4; a++) for (uint32_t m = 1; m <= 16; m *= 4) for (uint32_t n = 1; n <= 16; n *= 4) { 262 OmegaMatMulSpec s = mm(m, ks[a], n, OMEGA_MATMUL_PRECISION_INT32); 263 TRY(omega_blackwell_codegen_matmul_i32_prog(&s, &g_p), "i32_m%u_k%u_n%u", m, ks[a], n); } } 264 for (int pr = 1; pr <= 2; pr++) { 265 OmegaMatMulSpec s = mm(16, 16, 8, (OmegaMatMulPrecision)pr); 266 TRY(omega_blackwell_codegen_matmul_tensor_prog(&s, &g_p), "tensor_p%d", pr); 267 static const uint32_t ks[] = { 16, 64, 256, 4096 }, ns[] = { 8, 64 }, gx[] = { 1, 2, 8 }; 268 for (int a = 0; a < 4; a++) for (int b = 0; b < 2; b++) for (int c = 0; c < 3; c++) for (int mu = 0; mu < 2; mu++) { 269 OmegaMatMulSpec t = mm(16, ks[a], ns[b], (OmegaMatMulPrecision)pr); 270 TRY(omega_blackwell_codegen_matmul_tensor_loop_prog(&t, gx[c], mu, &g_p), "tloop_p%d_k%u_n%u_g%u_m%d", pr, ks[a], ns[b], gx[c], mu); 271 for (uint32_t un = 1; un <= 8; un *= 2) { 272 TRY(omega_blackwell_codegen_matmul_frag_prog(&t, gx[c], un, mu, &g_p), "frag_p%d_k%u_n%u_g%u_u%u_m%d", pr, ks[a], ns[b], gx[c], un, mu); 273 for (uint32_t rt = 1; rt <= 4; rt *= 2) for (uint32_t ct = 1; ct <= 4; ct *= 2) 274 TRY(omega_blackwell_codegen_matmul_frag_block_prog(&t, gx[c], un, rt, ct, mu, &g_p), 275 "fragb_p%d_k%u_n%u_g%u_u%u_r%u_c%u_m%d", pr, ks[a], ns[b], gx[c], un, rt, ct, mu); 276 } 277 } 278 } 279 for (uint32_t d = 1; d <= 4; d++) for (int mu = 0; mu <= 4; mu++) { 280 OmegaDecodeAttnSpec s; memset(&s, 0, sizeof s); s.depth = d; s.mutant = mu; 281 TRY(omega_blackwell_codegen_decode_attn_prog(&s, &g_p), "dattn_d%u_m%d", d, mu); } 282 { static const uint32_t ks[] = { 256, 1024, 4096, 16384 }; 283 for (int a = 0; a < 4; a++) for (uint32_t r = 1; r <= 4; r *= 2) for (int w = 0; w < 6; w++) for (uint32_t d = 1; d <= 16; d *= 2) for (int mu = 0; mu <= 3; mu++) { 284 OmegaBf16GemvSpec s; memset(&s, 0, sizeof s); s.k = ks[a]; s.rows = r; s.warps = W6[w]; s.depth = d; s.mutant = mu; 285 TRY(omega_blackwell_codegen_bf16_gemv_prog(&s, &g_p), "bf16_k%u_r%u_w%u_d%u_m%d", ks[a], r, W6[w], d, mu); } } 286 { static const uint32_t st[] = { 256, 1024, 8192 }; 287 for (int kd = 1; kd <= 4; kd++) for (uint32_t mo = 0; mo < 8; mo++) for (int a = 0; a < 3; a++) for (int mu = 0; mu <= 2; mu++) { 288 OmegaDecSpec s; memset(&s, 0, sizeof s); s.kind = (OmegaDecKind)kd; s.mode = mo; s.stride = st[a]; s.mutant = mu; 289 TRY(omega_blackwell_codegen_dec_prog(&s, &g_p), "dec_k%d_mo%u_s%u_m%d", kd, mo, st[a], mu); } } 290 { static const uint32_t ks[] = { 128, 256, 512, 2048, 16384 }; 291 for (int a = 0; a < 5; a++) for (uint32_t lb = 8; lb <= 16; lb *= 2) for (uint32_t mt = 1; mt <= 16; mt *= 2) for (int w = 0; w < 6; w++) for (int mu = 0; mu <= 3; mu++) { 292 OmegaFp8GemmSpec s; memset(&s, 0, sizeof s); s.k = ks[a]; s.lane_bytes = lb; s.mt = mt; s.warps = W6[w]; s.mutant = mu; 293 TRY(omega_blackwell_codegen_fp8gemm_prog(&s, &g_p), "fp8gemm_k%u_l%u_t%u_w%u_m%d", ks[a], lb, mt, W6[w], mu); } } 294 { static const uint32_t ks[] = { 256, 512, 2048, 4096, 16384 }, ds[] = { 0, 1, 2, 4, 8 }; 295 for (int a = 0; a < 5; a++) for (uint32_t lb = 8; lb <= 16; lb *= 2) for (uint32_t r = 1; r <= 8; r *= 2) for (int w = 0; w < 6; w++) 296 for (int d = 0; d < 5; d++) for (int mu = 0; mu <= 3; mu++) { 297 OmegaFp8GemvSpec s; memset(&s, 0, sizeof s); s.k = ks[a]; s.lane_bytes = lb; s.rows = r; s.warps = W6[w]; s.depth = ds[d]; s.mutant = mu; 298 TRY(omega_blackwell_codegen_fp8_gemv_prog(&s, &g_p), "fp8gemv_k%u_l%u_r%u_w%u_d%u_m%d", ks[a], lb, r, W6[w], ds[d], mu); 299 TRY(omega_blackwell_codegen_fp8_gemv2_prog(&s, &g_p), "fp8gemv2_k%u_l%u_r%u_w%u_d%u_m%d", ks[a], lb, r, W6[w], ds[d], mu); } } 300 for (uint32_t w = 4; w <= 8; w *= 2) for (int mu = 0; mu <= 4; mu++) for (uint32_t mo = 0; mo <= 2; mo++) for (uint32_t mi = 0; mi <= 1; mi++) { 301 OmegaPrefillAttnSpec s; memset(&s, 0, sizeof s); s.warps = w; s.mutant = mu; s.mode = mo; s.mirror = mi; 302 TRY(omega_blackwell_codegen_prefill_attn_prog(&s, &g_p), "pattn_w%u_m%d_mo%u_mi%u", w, mu, mo, mi); } 303 { static const uint32_t ks[] = { 128, 256, 2048, 4096 }, es[] = { 8, 64, 128 }, ts[] = { 1, 8, 16, 64 }; 304 for (int a = 0; a < 4; a++) for (int b = 0; b < 3; b++) for (int c = 0; c < 4; c++) for (int mu = 0; mu <= 2; mu++) { 305 OmegaRlSpec s = { ks[a], es[b], ts[c], mu }; 306 if (omega_rl_spec_ok(&s)) TRY(omega_blackwell_codegen_rl_prog(&s, &g_p), "rl_k%u_e%u_t%u_m%d", ks[a], es[b], ts[c], mu); } } 307 { static const uint32_t hs[] = { 512, 2048, 4096 }; 308 for (int a = 0; a < 3; a++) for (uint32_t tk = 1; tk <= 8; tk *= 2) for (int mu = 0; mu <= 2; mu++) { 309 OmegaMcSpec s = { hs[a], tk, mu }; 310 if (omega_mc_spec_ok(&s)) TRY(omega_blackwell_codegen_mc_prog(&s, &g_p), "mc_h%u_t%u_m%d", hs[a], tk, mu); } } 311 TRY(omega_blackwell_codegen_pf_copy_prog(&g_p), "pf_copy"); 312 { static const uint32_t wn[] = { 0, 1, 4, 15 }; 313 for (int mu = 0; mu <= 3; mu++) for (int a = 0; a < 4; a++) TRY(omega_blackwell_codegen_qmma_probe_prog(mu, wn[a], &g_p), "qmma_probe_m%d_w%u", mu, wn[a]); } 314 { static const uint32_t its[] = { 1, 64 }, th[] = { 32, 128, 256 }; 315 for (int a = 0; a < 2; a++) for (uint32_t ch = 1; ch <= 8; ch *= 2) for (uint32_t sl = 0; sl <= 15; sl += 15) for (int t = 0; t < 3; t++) { 316 TRY(omega_blackwell_codegen_qmma_peak_prog(its[a], ch, sl, th[t], &g_p), "qmma_peak_i%u_c%u_s%u_t%u", its[a], ch, sl, th[t]); 317 TRY(omega_blackwell_codegen_tc_peak_prog(its[a], ch, sl, th[t], &g_p), "tc_peak_i%u_c%u_s%u_t%u", its[a], ch, sl, th[t]); } 318 for (int a = 0; a < 2; a++) for (uint32_t g = 1; g <= 4; g *= 4) for (int t = 0; t < 3; t++) 319 TRY(omega_blackwell_codegen_tc_bw_prog(its[a], g, th[t], &g_p), "tc_bw_i%u_g%u_t%u", its[a], g, th[t]); } 320 TRY(omega_blackwell_codegen_f2e4_probe_prog(&g_p), "f2e4_probe"); 321 for (int mu = 0; mu <= 3; mu++) TRY(omega_blackwell_codegen_tc_probe_prog(mu, &g_p), "tc_probe_m%d", mu); 322 { static const uint32_t ks[] = { 128, 512, 4096, 16384 }; 323 for (int a = 0; a < 4; a++) for (uint32_t rt = 1; rt <= 4; rt *= 2) for (uint32_t ct = 1; ct <= 4; ct *= 2) for (uint32_t w = 1; w <= 8; w *= 2) for (int mu = 0; mu <= 3; mu++) { 324 OmegaTcGemmSpec s; memset(&s, 0, sizeof s); s.k = ks[a]; s.rt = rt; s.ct = ct; s.warps = w; s.mutant = mu; 325 /* the generator accepts 4*rt/warps staging slots up to 16 but keeps them in 8-long arrays (gx, sa, stg): 326 * rt=4 with one warp overruns them and crashes. Fixer ledger L149. Skipped and named, never hidden. */ 327 if (omega_tcgemm_spec_ok(&s) && 4u * rt / w > 8u) { 328 char nm[96]; snprintf(nm, sizeof nm, "tcgemm_k%u_r%u_c%u_w%u_m%d", ks[a], rt, ct, w, mu); 329 note(nm, 0, "skipped: generator overruns its 8-slot staging arrays for this setting (fixer L149)"); continue; } 330 TRY(omega_blackwell_codegen_tcgemm_prog(&s, &g_p), "tcgemm_k%u_r%u_c%u_w%u_m%d", ks[a], rt, ct, w, mu); } } 331 { static const uint32_t ks[] = { 256, 512, 4096, 16384 }, ws[] = { 1, 4, 8, 32 }; static const int ms[] = { 0, 1, 3 }; 332 for (int a = 0; a < 4; a++) for (uint32_t lay = 0; lay <= 1; lay++) for (uint32_t r = 1; r <= 4; r *= 2) for (int w = 0; w < 4; w++) 333 for (uint32_t d = 1; d <= 8; d *= 2) for (uint32_t ep = 0; ep <= 1; ep++) for (int mu = 0; mu < 3; mu++) { 334 OmegaTernGemvSpec s; memset(&s, 0, sizeof s); s.k = ks[a]; s.layout = lay; s.rows = r; s.warps = ws[w]; s.depth = d; s.epi = ep; s.mutant = ms[mu]; 335 TRY(omega_blackwell_codegen_tern_gemv_prog(&s, &g_p), "tern_k%u_l%u_r%u_w%u_d%u_e%u_m%d", ks[a], lay, r, ws[w], d, ep, ms[mu]); } } 336 { static const uint32_t ks[] = { 128, 2048, 16384 }; 337 for (int a = 0; a < 3; a++) for (uint32_t it = 0; it <= 3; it++) for (int mu = 0; mu <= 2; mu++) { 338 OmegaPqSpec s = { ks[a], it, mu }; 339 if (omega_pq_spec_ok(&s)) TRY(omega_blackwell_codegen_pq_prog(&s, &g_p), "pq_k%u_i%u_m%d", ks[a], it, mu); } } 340 { static const uint32_t ks[] = { 128, 2048 }, ns[] = { 256, 768 }; 341 for (int a = 0; a < 2; a++) for (uint32_t w = 8; w <= 16; w *= 2) for (uint32_t ep = 0; ep <= 1; ep++) for (uint32_t di = 0; di <= 1; di++) 342 for (int b = 0; b < 2; b++) for (uint32_t wt = 0; wt <= 8; wt += 8) for (int mu = 0; mu <= 4; mu++) { 343 OmegaQgSpec s; memset(&s, 0, sizeof s); s.k = ks[a]; s.rt = 4; s.warps = w; s.epi = ep; s.direct = di; s.n = ns[b]; s.wait = wt; s.mutant = mu; 344 if (omega_qg_spec_ok(&s)) TRY(omega_blackwell_codegen_qgemm_prog(&s, &g_p), "qgemm_k%u_w%u_e%u_d%u_n%u_t%u_m%d", ks[a], w, ep, di, ns[b], wt, mu); } } 345 { static const uint32_t ks[] = { 128, 2048 }, ns[] = { 512, 2048 }; 346 for (int a = 0; a < 2; a++) for (uint32_t rt = 2; rt <= 8; rt *= 2) for (uint32_t w = 4; w <= 16; w *= 2) for (uint32_t aq = 1; aq <= 2; aq++) 347 for (uint32_t ep = 0; ep <= 1; ep++) for (uint32_t di = 0; di <= 1; di++) for (int b = 0; b < 2; b++) for (int mu = 0; mu <= 2; mu++) { 348 OmegaTc2Spec s; memset(&s, 0, sizeof s); s.k = ks[a]; s.rt = rt; s.ct = 2; s.warps = w; s.aqn = aq; s.epi = ep; s.direct = di; s.n = ns[b]; s.mutant = mu; 349 TRY(omega_blackwell_codegen_tc2_prog(&s, &g_p), "tc2_k%u_r%u_w%u_a%u_e%u_d%u_n%u_m%d", ks[a], rt, w, aq, ep, di, ns[b], mu); } } 350 for (uint32_t w = 8; w <= 32; w *= 2) for (uint32_t d = 1; d <= 8; d *= 2) for (int mu = 0; mu <= 4; mu++) { 351 OmegaRouterSpec s; memset(&s, 0, sizeof s); s.warps = w; s.depth = d; s.mutant = mu; 352 TRY(omega_blackwell_codegen_router_prog(&s, &g_p), "router_w%u_d%u_m%d", w, d, mu); } 353 } 354 355 #ifdef SM121_WITH_GPU_API 356 /* elementwise kernels: built inside omega_gpu_elementwise_api.c; omega_gpu_elementwise_codegen_ir hands out the 357 * program and the kernel bytes the API itself made (the oracle here is the API's own code buffer) */ 358 #include "../../src/omega_gpu_elementwise_api.h" 359 static void ew_kernels(void) { 360 for (int op = OMEGA_GPU_EW_EX2; op < OMEGA_GPU_EW_OP_END; op++) 361 for (int mu = 0; mu <= 3; mu++) { 362 OmegaBlackwellKernel kern; memset(&kern, 0, sizeof kern); 363 omega_bw_ir_init(&g_p); 364 if (omega_gpu_elementwise_codegen_ir((OmegaGpuEwOp)op, mu, &g_p, &kern) != 0 || !kern.code) { g_refused++; continue; } 365 char nm[64]; snprintf(nm, sizeof nm, "ew_op%d_m%d", op, mu); 366 size_t len = 0; 367 if (omega_bw_encode_program(&g_p, g_code, sizeof g_code, &len) != 0 || len != kern.code_size || memcmp(g_code, kern.code, len) != 0) 368 note(nm, 0, "elementwise API bytes differ from encoding its own program again (listing check skipped)"); 369 else dump(nm, &g_p); 370 omega_blackwell_kernel_free(&kern); 371 } 372 } 373 #else 374 static void ew_kernels(void) { fprintf(g_notes, "-\t0\telementwise kernels not in this build (needs -DSM121_WITH_GPU_API + libomega_gpu.a)\n"); } 375 #endif 376 377 /* ---- edges: every form, registers and numbers at the edges of their fields; words from omega_blackwell_encode_one ---- */ 378 static FILE *g_es2, *g_ebin; 379 static unsigned long g_edges; 380 static void edge(BlackwellIRInsn in) { 381 static BlackwellIRProgram p; 382 uint32_t w[4]; 383 if (omega_blackwell_encode_one(&in, w) != 0) return; /* the C encoder refuses it: not a case */ 384 memset(&p.regalloc, 0, sizeof p.regalloc); 385 for (int i = 0; i < BW_MAX_VREGS; i++) p.regalloc.vreg_to_phys[i] = i; 386 for (int i = 0; i < BW_MAX_UVREGS; i++) p.regalloc.uvreg_to_phys[i] = i; 387 p.insns[0] = in; p.count = 1; 388 uint8_t *b = (uint8_t *)w; /* host is little-endian AArch64, the encoder's byte order */ 389 int nq = omega_qmma_patch_code(&p, b, 16); 390 if (nq < 0) return; 391 char k[32]; snprintf(k, sizeof k, "edge%lu", g_edges); 392 if (list_insn(g_es2, &in, &p.regalloc, nq > 0, k, 0) != 0) { note(k, 0, "opcode the listing writer does not know"); return; } 393 fwrite(w, 4, 4, g_ebin); 394 g_edges++; 395 } 396 static BlackwellIRInsn I(BlackwellIROpcode op, int d, int a, int b, int c, int u, uint32_t imm) { 397 BlackwellIRInsn in; memset(&in, 0, sizeof in); 398 in.op = op; in.dst_vreg = d; in.src1_vreg = a; in.src2_vreg = b; in.src3_vreg = c; in.ureg = u; in.imm = imm; 399 return in; 400 } 401 static void edges(void) { 402 static const int R[] = { 0, 1, 2, 85, 170, 254, 255 }; 403 static const int U[] = { -1, 0, 1, 31 }; 404 static const uint32_t N32[] = { 0, 1, 0x7fffffffu, 0x80000000u, 0xffffffffu, 0x12345678u }; 405 static const uint32_t CTL[] = { 0, 0x000fe200u, 0xffffffffu }; 406 for (int op = BW_IR_NOP; op <= BW_IR_LDS128; op++) 407 for (int ci = 0; ci < 3; ci++) 408 for (int ri = 0; ri < 7; ri++) { 409 int r = R[ri], s = R[6 - ri], t = R[(ri + 3) % 7]; 410 for (int ui = 0; ui < 4; ui++) 411 for (int vi = 0; vi < 2; vi++) { /* vi 1: absent sources (-1) where the form allows */ 412 int a = vi ? -1 : s, b = vi ? -1 : t, c = vi ? -1 : r; 413 for (int ni = 0; ni < 6; ni++) { 414 uint32_t imm = N32[ni]; int eop = op; /* eop: SHFL.BFLY / F2FP.BF16 ride the SHFL / FMNMX rows */ 415 switch (op) { /* numbers kept inside each field's range (the C refusal or masking is a separate note) */ 416 case BW_IR_S2R: imm &= 0xffu; break; 417 case BW_IR_LDC: case BW_IR_LDC64: imm = (imm & 0xffffu) << 2; break; 418 case BW_IR_LDCU: case BW_IR_LDCU64: imm = (imm & 0xffffu) << 3; break; 419 case BW_IR_STG_E: case BW_IR_LDG_E_64: case BW_IR_LDG_E_128: imm = (uint32_t)((int32_t)(imm << 8) >> 8); break; 420 case BW_IR_LDS128: imm &= 0x7fffffu; break; 421 case BW_IR_LDS: case BW_IR_STS: imm &= 0xffffu; break; 422 case BW_IR_SHFL_DOWN: imm &= 0x1fu; if (ni == 3) eop = BW_IR_SHFL_BFLY; break; 423 case BW_IR_LOP3_LUT: imm &= 0xffu; break; 424 case BW_IR_FMNMX_MAX: if (ni == 4) { eop = BW_IR_F2FP_BF16_PACK; imm = 0; } else if (ni == 5) imm = OMEGA_QMMA_MARK_F2E4; break; 425 case BW_IR_BSSY: imm = ni == 0 ? 2u : ni == 1 ? 3u : ni == 2 ? 0x10000000u : ni == 3 ? 64u : ni == 4 ? 65u : 1000u; break; 426 case BW_IR_BRA: { static const int32_t D[] = { 0, 1, -1, 64, -63, 0x7fffffff }; imm = (uint32_t)D[ni]; break; } 427 default: break; 428 } 429 BlackwellIRInsn in = I((BlackwellIROpcode)eop, (op == BW_IR_FSETP && r > 7) ? r & 7 : r, a, b, op == BW_IR_FSEL && c > 7 ? -1 : c, 430 U[ui], imm); 431 in.control = CTL[ci]; 432 in.is_uniform = (ui & 1) != 0 && r < BW_MAX_UVREGS; /* a uniform destination indexes the 32 uniform registers */ 433 in.bar_reg = (uint8_t)(ri & 15); 434 if (op == BW_IR_IMAD && ui == 3) in.src2_vreg = -1; /* reach the uniform and immediate forms */ 435 if (op == BW_IR_IADD3 || op == BW_IR_EXIT) in.src3_vreg = -1; 436 if (op == BW_IR_HMMA_F16 || op == BW_IR_HMMA_BF16) in.imm = ni == 5 ? OMEGA_QMMA_MARK : 0; 437 for (int pi = 0; pi < 3; pi++) { 438 if (pi && op != BW_IR_BRA && op != BW_IR_BSSY && op != BW_IR_BSYNC && op != BW_IR_STG_STRONG_SYS && op != BW_IR_EXIT) break; 439 in.predicate_p0 = pi != 0; in.predicate_not = pi == 2 && op != BW_IR_EXIT; 440 edge(in); 441 } 442 if (op == BW_IR_NOP || op == BW_IR_MEMBAR_ALL_SYS || op == BW_IR_MEMBAR_SC_SYS || op == BW_IR_CCTL_IVALL || op == BW_IR_BAR_SYNC) break; 443 } 444 } 445 } 446 } 447 448 int main(int argc, char **argv) { 449 if (argc != 3) { fprintf(stderr, "usage: sm121list kernels|edges DIR\n"); return 2; } 450 g_dir = argv[2]; 451 char path[512]; 452 snprintf(path, sizeof path, "%s/notes.tsv", g_dir); 453 g_notes = fopen(path, strcmp(argv[1], "edges") ? "w" : "a"); 454 if (!g_notes) { perror(path); return 2; } 455 if (!strcmp(argv[1], "kernels")) { 456 snprintf(path, sizeof path, "%s/kernels.tsv", g_dir); 457 g_tsv = private_output(path, "w"); 458 if (!g_tsv) { perror(path); return 2; } 459 fprintf(g_tsv, "kernel\tinstructions\tcode_bytes\tqmma_patched\n"); 460 kernels(); 461 ew_kernels(); 462 { uint8_t va[OMEGA_BW_VECADD_CODE_SIZE]; size_t vl = 0; /* the chip-proven vector add, the rung-6 chip kernel */ 463 snprintf(path, sizeof path, "%s/vecadd.c.bin", g_dir); FILE *vf = private_output(path, "wb"); 464 if (!vf || omega_blackwell_encode_vecadd(va, sizeof va, &vl) != 0 || fwrite(va, 1, vl, vf) != vl) { perror(path); return 2; } 465 fclose(vf); } 466 fclose(g_tsv); 467 printf("kernels %lu (instructions %lu), identical repeats skipped %lu, settings refused %lu\n", g_kernels, g_insns, g_dups, g_refused); 468 } else if (!strcmp(argv[1], "edges")) { 469 snprintf(path, sizeof path, "%s/edges.s2", g_dir); g_es2 = private_output(path, "w"); 470 snprintf(path, sizeof path, "%s/edges.c.bin", g_dir); g_ebin = private_output(path, "wb"); 471 if (!g_es2 || !g_ebin) { perror(path); return 2; } 472 edges(); 473 fclose(g_es2); fclose(g_ebin); 474 printf("edge cases %lu\n", g_edges); 475 } else return 2; 476 fclose(g_notes); 477 return 0; 478 }