interplane.Get Omega
Read in your language

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

branch main Branches Tags History

omega-mv / src / omega_sm121_disasm.c

296 lines · 16814 bytes Raw Blame History
1/* omega_sm121_disasm.c -- see omega_sm121_disasm.h.
2 *
3 * Layout of a word (lo = bits 0..63, hi = bits 64..127): opcode bits 0..11; guard predicate bits 12..14 with its
4 * negation at 15 (7 and no negation = no guard); destination register 16..23, first source 24..31, second source
5 * 32..39 or a 32-bit immediate at 32..63; third source at hi 0..7; modifiers in hi 8..40; control from hi 41 on.
6 *
7 * A row of the table is one instruction form: its opcode and fixed bits (lov, hiv) and a format. Each operand code
8 * in the format names the bits it reads; every other bit of lo and of hi 0..40 (outside the guard) must equal the
9 * row's fixed value, or the row does not match. The rows come from the words Omega's builders write
10 * (src/omega_blackwell_codegen.c encode_single_insn, the em helpers, src/omega_osc_gpu.c); the text of each was
11 * checked against nvdisasm 13.0 -b SM121 by tools/sm121dis/check.sh. */
12#include "omega_sm121_disasm.h"
13#include <stdarg.h>
14#include <stdio.h>
15#include <string.h>
16
17#define HI_BITS 41
18#define HI_MASK ((1ULL << HI_BITS) - 1)
19
20typedef struct {
21 uint64_t lov, hiv; /* fixed bits (operand fields zero) */
22 const char *fmt; /* text; %x codes are operands, see field() */
23} Row;
24
25/* operand codes: which bits they read (lo or hi, first bit, width); several fields for the branch target */
26typedef struct { char code; int hi; int at; int n; } Field;
27static const Field FIELDS[] = {
28 {'d', 0, 16, 8}, {'a', 0, 24, 8}, {'b', 0, 32, 8}, {'c', 1, 0, 8}, /* R registers */
29 {'D', 0, 16, 8}, {'A', 0, 24, 8}, {'B', 0, 32, 8}, {'C', 1, 0, 8}, /* uniform registers */
30 {'i', 0, 32, 32}, {'s', 0, 32, 32}, {'f', 0, 32, 32}, /* 32-bit immediate: unsigned, signed, float */
31 {'K', 0, 38, 21}, /* c[bank][offset]: offset 38..53, bank 54..58 */
32 {'U', 0, 37, 22}, /* uniform load c[bank][offset]: offset 37..53 */
33 {'o', 0, 40, 24}, /* signed 24-bit address offset */
34 {'Y', 0, 32, 32}, /* BSSY target: signed byte distance */
35 {'y', 0, 16, 4}, /* convergence barrier B0..B15 */
36 {'P', 1, 17, 3}, {'Q', 1, 20, 3}, {'R', 1, 23, 4}, /* predicates; R carries its negation (hi 26) */
37 {'l', 1, 8, 8}, /* LOP3 truth table */
38 {'h', 0, 53, 5}, {'m', 0, 40, 13}, /* SHFL lane distance, clamp */
39 {'S', 1, 8, 8}, /* special register */
40 {'G', 1, 12, 3}, {'g', 1, 12, 4}, /* integer and float compare */
41 {'Z', 1, 9, 1}, {'z', 1, 9, 3}, /* ISETP signed flag; memory access size */
42 {'N', 1, 8, 1}, {'M', 0, 63, 1}, /* IADD3 negations: first, second source */
43 {'j', 0, 32, 32}, /* ISETP immediate (printed signed) */
44};
45
46static const Field *field_of(char c) {
47 for (size_t i = 0; i < sizeof FIELDS / sizeof FIELDS[0]; i++) if (FIELDS[i].code == c) return &FIELDS[i];
48 return NULL;
49}
50
51static uint64_t bits(uint64_t v, int at, int n) { return n >= 64 ? v >> at : (v >> at) & ((1ULL << n) - 1); }
52static uint64_t mask(int at, int n) { return (n >= 64 ? ~0ULL : ((1ULL << n) - 1)) << at; }
53
54/* the BRA distance: 4-byte units, low 8 bits at 16..23, the rest (signed) at 34..81 */
55#define BRA_LO_MASK (mask(16, 8) | mask(34, 30))
56#define BRA_HI_MASK mask(0, 18)
57
58typedef struct { char *o; size_t n, at; int ok; } Out;
59
60static void put(Out *w, const char *fmt, ...) {
61 if (w->at >= w->n) { w->ok = 0; return; }
62 va_list ap; va_start(ap, fmt);
63 int k = vsnprintf(w->o + w->at, w->n - w->at, fmt, ap);
64 va_end(ap);
65 if (k < 0 || (size_t)k >= w->n - w->at) { w->ok = 0; w->at = w->n; return; }
66 w->at += (size_t)k;
67}
68
69static void reg(Out *w, const char *p, uint64_t r) { if (r == 255) put(w, "%sZ", p); else put(w, "%s%u", p, (unsigned)r); }
70static void hex_s(Out *w, int64_t v) { if (v < 0) put(w, "-0x%llx", (unsigned long long)(-v)); else put(w, "0x%llx", (unsigned long long)v); }
71static void pred(Out *w, uint64_t p, int neg) { put(w, "%s", neg ? "!" : ""); if (p == 7) put(w, "PT"); else put(w, "P%u", (unsigned)p); }
72
73static const char *SR[256] = {
74 [0x21] = "SR_TID.X", [0x22] = "SR_TID.Y", [0x23] = "SR_TID.Z",
75 [0x25] = "SR_CTAID.X", [0x26] = "SR_CTAID.Y", [0x27] = "SR_CTAID.Z",
76 [0x00] = "SR_LANEID", [0x50] = "SR_CLOCKLO", [0x51] = "SR_CLOCKHI",
77};
78static const char *ICMP[8] = { "F", "LT", "EQ", "LE", "GT", "NE", "GE", "T" };
79static const char *FCMP[16] = { "F", "LT", "EQ", "LE", "GT", "NE", "GE", "NUM", "NAN", "LTU", "EQU", "LEU", "GTU", "NEU", "GEU", "T" };
80static const char *MSIZE[8] = { ".U8" , ".S8", ".U16", ".S16", "", ".64", ".128", ".U.128" };
81
82/* nvdisasm prints a float immediate the shortest way that reads back exactly, with INF and NAN by name */
83static void fimm(Out *w, uint32_t u) {
84 float f; memcpy(&f, &u, 4);
85 if ((u & 0x7f800000u) == 0x7f800000u) {
86 if (u & 0x007fffffu) put(w, "%sQNAN", (u >> 31) ? "-" : "+");
87 else put(w, "%sINF", (u >> 31) ? "-" : "+");
88 return;
89 }
90 char b[64];
91 for (int p = 1; p <= 20; p++) {
92 snprintf(b, sizeof b, "%.*g", p, (double)f);
93 float g; if (sscanf(b, "%g", &g) == 1 && memcmp(&g, &f, 4) == 0) break;
94 }
95 put(w, "%s", b);
96}
97
98/* Prints row's format for (lo, hi); 0 when every operand could be printed */
99static void render(Out *w, const char *fmt, uint64_t lo, uint64_t hi, uint64_t pc) {
100 for (const char *p = fmt; *p; p++) {
101 if (*p != '%') { put(w, "%c", *p); continue; }
102 char c = *++p;
103 const Field *f = field_of(c);
104 uint64_t v = f ? bits(f->hi ? hi : lo, f->at, f->n) : 0;
105 switch (c) {
106#ifdef SM121DIS_MUTANT
107 case 'a': reg(w, "R", v == 255 ? v : v ^ 1); break; /* the red check: first source register off by one bit */
108 case 'd': case 'b': case 'c': reg(w, "R", v); break;
109#else
110 case 'd': case 'a': case 'b': case 'c': reg(w, "R", v); break;
111#endif
112 case 'D': case 'A': case 'B': case 'C': reg(w, "UR", v); break;
113 case 'i': put(w, "0x%x", (unsigned)v); break;
114 case 's': hex_s(w, (int32_t)(uint32_t)v); break;
115 case 'f': fimm(w, (uint32_t)v); break;
116 case 'K': put(w, "c[0x%x][0x%x]", (unsigned)bits(lo, 54, 5), (unsigned)bits(lo, 38, 16)); break;
117 case 'U': put(w, "c[0x%x][0x%x]", (unsigned)bits(lo, 54, 5), (unsigned)bits(lo, 37, 17)); break;
118 case 'o': { int32_t off = (int32_t)((uint32_t)v << 8) >> 8; if (off) { put(w, "+"); hex_s(w, off); } /* nvdisasm writes a negative offset as +-0x4 */ break; }
119 case 'Y': put(w, "0x%llx", (unsigned long long)(pc + 16 + (int64_t)(int32_t)(uint32_t)v)); break;
120 case 'y': put(w, "B%u", (unsigned)v); break;
121 case 'P': case 'Q': pred(w, v, 0); break;
122 case 'R': pred(w, v & 7, (int)((v >> 3) & 1)); break;
123 case 'l': put(w, "0x%x", (unsigned)v); break;
124 case 'h': case 'm': put(w, "0x%x", (unsigned)v); break;
125 case 'S': if (SR[v]) put(w, "%s", SR[v]); else put(w, "SR%u", (unsigned)v); break;
126 case 'G': put(w, "%s", ICMP[v]); break;
127 case 'Z': if (!v) put(w, ".U32"); break;
128 case 'z': put(w, "%s", MSIZE[v]); break;
129 case 'N': case 'M': if (v) put(w, "-"); break;
130 case 'j': hex_s(w, (int32_t)(uint32_t)v); break; /* signed even in a .U32 compare, as nvdisasm prints it */
131 case 'g': put(w, "%s", FCMP[v]); break;
132 case 'T': {
133 uint64_t units = bits(lo, 16, 8) | (bits(lo, 34, 30) << 8) | (bits(hi, 0, 18) << 38);
134 int64_t s = (int64_t)(units << 8) >> 8; /* 56-bit signed */
135 put(w, "0x%llx", (unsigned long long)(pc + 16 + s * 4));
136 break;
137 }
138 default: w->ok = 0; return;
139 }
140 }
141}
142
143/* the bits a format reads */
144static void fields_mask(const char *fmt, uint64_t *lom, uint64_t *him) {
145 *lom = 0; *him = 0;
146 for (const char *p = fmt; *p; p++) {
147 if (*p != '%') continue;
148 char c = *++p;
149 if (c == 'T') { *lom |= BRA_LO_MASK; *him |= BRA_HI_MASK; continue; }
150 const Field *f = field_of(c);
151 if (!f) continue;
152 if (f->hi) *him |= mask(f->at, f->n); else *lom |= mask(f->at, f->n);
153 }
154}
155
156/* The forms. lo values include the opcode (bits 0..11); guard bits 12..15 are never part of a row. */
157static const Row ROWS[] = {
158 /* moves, constants, special registers */
159 { 0x0000000000007802ull, 0x00000f00ull, "MOV %d, %i" },
160 { 0x00000000ff007202ull, 0x00000f00ull, "MOV %d, %b" },
161 { 0x0000000000007202ull, 0x00000f00ull, "MOV %d, %b" },
162 { 0x0000000000007919ull, 0x00000000ull, "S2R %d, %S" },
163 { 0x00000000ff007b82ull, 0x00000800ull, "LDC %d, %K" },
164 { 0x00000000ff007b82ull, 0x00000a00ull, "LDC.64 %d, %K" },
165 { 0x00000000ff0077acull, 0x08000800ull, "LDCU %D, %U" },
166 { 0x00000000ff0077acull, 0x08000a00ull, "LDCU.64 %D, %U" },
167 /* integer */
168 { 0x0000000000007224ull, 0x078e0200ull, "IMAD %d, %a, %b, %c" },
169 { 0x0000000000007c24ull, 0x0f8e0200ull, "IMAD %d, %a, %B, %c" },
170 { 0x0000000000007825ull, 0x078e0000ull, "IMAD.WIDE.U32 %d, %a, %i, %c" },
171 { 0x0000000000007227ull, 0x078e0000ull, "IMAD.HI.U32 %d, %a, %b, %c" },
172 { 0x0000000000007210ull, 0x07ffe000ull, "IADD3 %d, PT, PT, %N%a, %M%b, %c" },
173 { 0x0000000000007810ull, 0x07ffe000ull, "IADD3 %d, PT, PT, %N%a, %s, %c" },
174 { 0x0000000000007212ull, 0x000e0000ull, "LOP3.LUT %d, %a, %b, %c, %l, %R" },
175 { 0x0000000000007812ull, 0x000e0000ull, "LOP3.LUT %d, %a, %i, %c, %l, %R" },
176 { 0x0000000000007819ull, 0x00011600ull, "SHF.R.U32.HI %d, %a, %i, %c" },
177 { 0x0000000000007219ull, 0x00011600ull, "SHF.R.U32.HI %d, %a, %b, %c" },
178 { 0x0000000000007219ull, 0x00000600ull, "SHF.L.U32 %d, %a, %b, %c" },
179 { 0x0000000000007819ull, 0x00000600ull, "SHF.L.U32 %d, %a, %i, %c" },
180 { 0x000000000000720cull, 0x00000070ull, "ISETP.%G%Z.AND %P, %Q, %a, %b, %R" },
181 { 0x000000000000780cull, 0x00000070ull, "ISETP.%G%Z.AND %P, %Q, %a, %j, %R" },
182 { 0x0000000000007c0cull, 0x08000070ull, "ISETP.%G%Z.AND %P, %Q, %a, %B, %R" },
183 { 0x0000000000007207ull, 0x00000000ull, "SEL %d, %a, %b, %R" },
184 { 0x0000000000007807ull, 0x00000000ull, "SEL %d, %a, %i, %R" },
185 /* float */
186 { 0x0000000000007221ull, 0x00000000ull, "FADD %d, %a, %b" },
187 { 0x8000000000007221ull, 0x00000000ull, "FADD %d, %a, -%b" },
188 { 0x0000000000007220ull, 0x00400000ull, "FMUL %d, %a, %b" },
189 { 0x0000000000007223ull, 0x00000000ull, "FFMA %d, %a, %b, %c" },
190 { 0x0000000000007209ull, 0x00000000ull, "FMNMX %d, %a, %b, %R" },
191 { 0x000000000000720bull, 0x00000000ull, "FSETP.%g.AND %P, %Q, %a, %b, %R" },
192 { 0x0000000000007245ull, 0x00201000ull, "I2FP.F32.U32 %d, %b" },
193 { 0x0000000000007245ull, 0x00201400ull, "I2FP.F32.S32 %d, %b" },
194 { 0x0000000000007305ull, 0x0020f000ull, "F2I.U32.TRUNC.NTZ %d, %b" },
195 { 0x000000000000723eull, 0x0004807000ull, "F2FP.SATFINITE.E4M3.F32.PACK_AB_MERGE_C %d, %a, %b, %c" },
196 { 0x000000000000723eull, 0x00000010ffull, "F2FP.BF16.F32.PACK_AB %d, %a, %b" },
197 /* tensor cores */
198 { 0x000000000000723cull, 0x00041800ull, "HMMA.16816.F32.BF16 %d, %a, %b, %c" },
199 { 0x000000000000723cull, 0x00001800ull, "HMMA.16816.F32 %d, %a, %b, %c" },
200 { 0x000000000000727aull, 0x00002c00ull, "QMMA.16832.F32.E4M3.E4M3 %d, %a, %b, %c" },
201 { 0x0000000000007308ull, 0x00000800ull, "MUFU.EX2 %d, %b" },
202 { 0x0000000000007308ull, 0x00001000ull, "MUFU.RCP %d, %b" },
203 { 0x0000000000007308ull, 0x00001400ull, "MUFU.RSQ %d, %b" },
204 /* memory */
205 { 0x0000000000007981ull, 0x0c1e1100ull, "LDG.E%z %d, desc[%B][%a.64%o]" },
206 { 0x0000000000007986ull, 0x0c101100ull, "STG.E%z desc[%C][%a.64%o], %b" },
207 /* the .STRONG.SYS forms (and ATOMG) are the ones the bw_hazard_test sweep builds (decode_attn); only the 32-bit
208 * size and the PT predicate seen there are rows, every other bit pattern stays reported, not guessed. The sweep
209 * has these forms only with a zero offset, so their %o is checked by the plain LDG/STG rows above (same opcode,
210 * same offset field, nonzero offsets throughout the corpus), not by a STRONG listing of its own */
211 { 0x0000000000007981ull, 0x0c1f5900ull, "LDG.E.STRONG.SYS %d, desc[%B][%a.64%o]" },
212 { 0x0000000000007986ull, 0x0c115900ull, "STG.E.STRONG.SYS desc[%C][%a.64%o], %b" },
213 { 0x80000000000079a8ull, 0x081f5100ull, "ATOMG.E.ADD.STRONG.SYS PT, %d, desc[%C][%a.64], %b" },
214 { 0x0000000000007984ull, 0x08000800ull, "LDS %d, [%a+%B]" },
215 { 0x0000000000007988ull, 0x08000800ull, "STS [%a+%C], %b" },
216 { 0x0000000000007984ull, 0x00000000ull, "LDS%z %d, [%a%o]" },
217 { 0x0000000000007992ull, 0x0000b000ull, "MEMBAR.ALL.SYS" },
218 { 0x0800000000007f89ull, 0x000e0000ull, "SHFL.DOWN PT, %d, %a, %h, %m" },
219 { 0x0c00000000007f89ull, 0x000e0000ull, "SHFL.BFLY PT, %d, %a, %h, %m" },
220 /* control */
221 { 0x0000000000007918ull, 0x00000000ull, "NOP" },
222 { 0x000000000000794dull, 0x03800000ull, "EXIT" },
223 { 0x0000000000007947ull, 0x03800000ull, "BRA %T" },
224 { 0x0000000000007945ull, 0x03800200ull, "BSSY.RECONVERGENT %y, %Y" },
225 { 0x0000000000007941ull, 0x03800200ull, "BSYNC.RECONVERGENT %y" },
226 { 0x0000000000007b1dull, 0x00010000ull, "BAR.SYNC.DEFER_BLOCKING 0x0" },
227};
228
229/* IMAD with an immediate: nvdisasm names the special cases. Returns 0 for another instruction, 1 when printed,
230 * -1 for a shift by a power of two whose name nvdisasm was never seen to print (so it is reported, not guessed). */
231static int imad_imm(Out *w, uint64_t lo, uint64_t hi) {
232 if (bits(lo, 0, 12) != 0x824 || (hi & HI_MASK & ~0xffull) != 0x078e0200ull) return 0;
233 uint32_t imm = (uint32_t)bits(lo, 32, 32);
234 uint64_t c = bits(hi, 0, 8);
235 const char *name = "IMAD";
236 if (imm == 0) name = "IMAD.MOV"; /* d = c */
237 else if (imm == 1) name = c == 255 ? "IMAD.MOV" : "IMAD.IADD"; /* d = a, d = a + c */
238 else if (c == 255 && (imm & (imm - 1)) == 0) {
239 /* a shift. Seen in the corpus: .SHL for 2^1..2^10, 2^12, 2^20; plain IMAD for 2^16 and 2^31 */
240 int n = __builtin_ctz(imm);
241 if (n == 16 || n == 31) name = "IMAD";
242 else if (n <= 10 || n == 12 || n == 20) name = "IMAD.SHL";
243 else return -1;
244 }
245 put(w, "%s ", name);
246 reg(w, "R", bits(lo, 16, 8)); put(w, ", "); reg(w, "R", bits(lo, 24, 8)); put(w, ", ");
247 hex_s(w, (int32_t)imm); put(w, ", "); reg(w, "R", c);
248 return 1;
249}
250
251int omega_sm121_disasm(uint64_t lo, uint64_t hi, uint64_t pc, char *out, size_t outn) {
252 if (!out || outn < 40) return -2;
253 Out w = { out, outn, 0, 1 };
254 out[0] = 0;
255 uint64_t g = bits(lo, 12, 3); int gneg = (int)bits(lo, 15, 1);
256 if (!(g == 7 && !gneg)) { put(&w, "@"); pred(&w, g, gneg); put(&w, " "); }
257 const uint64_t lo_body = lo & ~mask(12, 4), hi_body = hi & HI_MASK;
258 size_t start = w.at;
259 int im = imad_imm(&w, lo_body, hi_body);
260 if (im > 0) return w.ok ? 0 : -2;
261 for (size_t i = 0; im == 0 && i < sizeof ROWS / sizeof ROWS[0]; i++) {
262 const Row *r = &ROWS[i];
263 if ((lo_body & 0xfff) != (r->lov & 0xfff)) continue;
264 uint64_t lom, him; fields_mask(r->fmt, &lom, &him);
265 lom = ~(lom | mask(12, 4)); him = HI_MASK & ~him;
266 if ((lo_body & lom) != (r->lov & lom) || (hi_body & him) != (r->hiv & him)) continue;
267 w.at = start; render(&w, r->fmt, lo, hi, pc);
268 return w.ok ? 0 : -2;
269 }
270 w.at = 0;
271 put(&w, "?? 0x%016llx%016llx", (unsigned long long)hi, (unsigned long long)lo);
272 return w.ok ? -1 : -2;
273}
274
275static uint64_t word_half(const uint8_t *w) { uint64_t v = 0; for (int i = 7; i >= 0; i--) v = v << 8 | w[i]; return v; }
276
277size_t omega_sm121_padding_start(const uint8_t *code, size_t len) {
278 size_t end = 0;
279 for (size_t at = 0; code && at + 16 <= len; at += 16)
280 if ((word_half(code + at) & 0xfff) == 0x94d) end = at + 16; /* EXIT, any guard */
281 return end;
282}
283
284int omega_sm121_listing(const uint8_t *code, size_t len, FILE *out) {
285 if (!code || !out || len % 16) return -1;
286 size_t pad = omega_sm121_padding_start(code, len);
287 int unknown = 0;
288 for (size_t at = 0; at < len; at += 16) {
289 char t[256];
290 int rc = omega_sm121_disasm(word_half(code + at), word_half(code + at + 8), at, t, sizeof t);
291 if (rc == -2) return -1;
292 if (rc) unknown++;
293 fprintf(out, " /*%04llx*/ %s%s;\n", (unsigned long long)at, t, pad && at >= pad ? "" : " ");
294 }
295 return unknown;
296}