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 / evidence / gpu-reconv-308 / T1a-GPU-RECONV-0a1812f / gpu_reconv_test.c

470 lines · 32700 bytes Raw Blame History
1/* gpu_reconv_test: structured warp reconvergence (omega #308) on Omega's own Blackwell codegen.
2 *
3 * ./gpu_reconv_test --host-only encoder golden words, region bookkeeping, codegen refusals,
4 * nvdisasm listing (when /usr/local/cuda/bin/nvdisasm exists),
5 * every probe kernel through the SIMT warp simulator against
6 * the host oracle under both fragment orders, the mutation
7 * controls, scoreboard and control-edge walks. No device.
8 * ./gpu_reconv_test --out receipt.json chip gate: host part first (a kernel the simulator rejects
9 * never gets chip time), then every probe on the chip against
10 * the oracle, bit-exact, then each math mutant must be caught.
11 * --observe-dropped-join chip only, optional: also launch the dropped-BSYNC mutant of
12 * one probe and record what the chip did (an observation, not
13 * a gate; the simulator rejects that kernel first).
14 *
15 * Probes (src/omega_gpu_elementwise_api.c, gen_rc_*): R3 diamond, R4 loop break, R5 nested,
16 * R6 lanes past the end with STS/BAR/LDS + SHFL, and the 16-deep barrier-register bound.
17 * Oracle: rc_oracle below; every comparison is exact (u32). Launcher chunking (64 CTAs x 128
18 * threads per launch, P0 = words in the chunk) is part of the oracle, so a clamp to the wrong
19 * chunk end shows up. */
20#include "omega_gpu_elementwise_api.h"
21#include "omega_blackwell_codegen.h"
22#include "omega_bw_reconv.h"
23#include "omega_gpu_session.h"
24#include "bw_warp_sim.h"
25#include <inttypes.h>
26#include <stdio.h>
27#include <stdlib.h>
28#include <string.h>
29#include <unistd.h>
30
31static int g_checks, g_failed;
32#define CHECK(cond, ...) do { g_checks++; if (!(cond)) { g_failed++; printf("FAIL: "); printf(__VA_ARGS__); printf("\n"); } } while (0)
33
34static uint32_t lcg(uint32_t *s) { *s = *s * 1664525u + 1013904223u; return *s; }
35
36typedef struct { OmegaGpuEwOp op; const char *name; const char *reg; } Probe;
37static const Probe PROBES[] = {
38 { OMEGA_GPU_EW_RC_DIAMOND, "rc_diamond", "R3" },
39 { OMEGA_GPU_EW_RC_LOOPBREAK, "rc_loopbreak", "R4" },
40 { OMEGA_GPU_EW_RC_NESTED, "rc_nested", "R5" },
41 { OMEGA_GPU_EW_RC_EXIT_BAR, "rc_exit_bar", "R6" },
42 { OMEGA_GPU_EW_RC_DEPTH16, "rc_depth16", "B0..B15" },
43};
44#define NPROBES (sizeof PROBES / sizeof PROBES[0])
45static const uint32_t NS[] = { 1, 5, 31, 32, 33, 63, 64, 65, 96, 127, 128, 129, 200, 1000, 4096, 8191, 8192, 8193, 20000 };
46#define NNS (sizeof NS / sizeof NS[0])
47#define CHUNK (OMEGA_GPU_EW_MAX_CTAS * OMEGA_GPU_EW_THREADS)
48
49/* ----------------------------------------------------------------- oracle */
50static uint32_t count_loop(uint32_t x, uint32_t y, int math, uint32_t *v_out) {
51 uint32_t v = x & 255u, st = (y & 15u) + 1u, cnt = 0, bound = math ? 255u : 256u;
52 while (v < bound) { v += st; cnt++; }
53 *v_out = v; return cnt;
54}
55static uint32_t rc_r(OmegaGpuEwOp op, int math, uint32_t x, uint32_t y, uint32_t partner_x) {
56 uint32_t v, cnt;
57 switch (op) {
58 case OMEGA_GPU_EW_RC_DIAMOND:
59 if (x >= y) { uint32_t t = 3u * x + y, u = t ^ x, r = 5u * u + 1u; r += r >> 3; return math ? r + 1u : r; }
60 return x + 1u;
61 case OMEGA_GPU_EW_RC_LOOPBREAK: cnt = count_loop(x, y, math, &v); return cnt * 256u + v;
62 case OMEGA_GPU_EW_RC_NESTED: if (x & 1u) return 7u; cnt = count_loop(x, y, math, &v); return cnt * 256u + v + 100u;
63 case OMEGA_GPU_EW_RC_EXIT_BAR: return 3u * x + (partner_x | 1u);
64 case OMEGA_GPU_EW_RC_DEPTH16: return x + (x < y ? (uint32_t)BW_RECONV_MAX_BAR : 0u);
65 default: return 0xdeadbeefu;
66 }
67}
68static void rc_oracle(OmegaGpuEwOp op, int math, uint32_t n, const uint32_t *a, const uint32_t *b, uint32_t *out) {
69 for (uint32_t i0 = 0; i0 < n; i0 += CHUNK) {
70 uint32_t ni = n - i0 < CHUNK ? n - i0 : CHUNK;
71 uint32_t threads = ((ni + OMEGA_GPU_EW_THREADS - 1) / OMEGA_GPU_EW_THREADS) * OMEGA_GPU_EW_THREADS;
72 uint32_t *r = malloc((size_t)threads * 4);
73 for (uint32_t t = 0; t < threads; t++) {
74 uint32_t idx = t < ni ? t : ni - 1, pt = t ^ 1u, pidx = pt < ni ? pt : ni - 1;
75 r[t] = rc_r(op, math, a[i0 + idx], b[i0 + idx], a[i0 + pidx]);
76 }
77 for (uint32_t i = 0; i < ni; i++) { uint32_t nb = (i & 31u) == 31u ? i : i + 1u; out[i0 + i] = r[i] + 4099u * r[nb]; }
78 free(r);
79 }
80}
81/* An LCG's low bits cycle quickly (bit 0 has period 2), and the probes key on low bits
82 * (x & 255, y & 15, x & 1), so each draw goes through a mixing step. */
83static uint32_t mix(uint32_t r) { r ^= r >> 16; r *= 0x45d9f3bu; r ^= r >> 16; r *= 0x45d9f3bu; r ^= r >> 16; return r; }
84static void fill(uint32_t *a, uint32_t *b, uint32_t n, uint32_t seed) {
85 for (uint32_t i = 0; i < n; i++) { a[i] = mix(lcg(&seed)); b[i] = mix(lcg(&seed)); }
86}
87static size_t diff_count(const uint32_t *got, const uint32_t *want, uint32_t n, uint32_t *first) {
88 size_t bad = 0; *first = n;
89 for (uint32_t i = 0; i < n; i++) if (got[i] != want[i]) { if (!bad) *first = i; bad++; }
90 return bad;
91}
92
93/* --------------------------------------------------------- simulator driver */
94static uint8_t g_smem[1024];
95/* Mirrors run_1d: 64 CTAs x 128 threads per launch, P0 = words in the chunk. Returns the
96 * simulator's error code (0 clean); the output words are written to out. */
97static int ws_probe(const BlackwellIRProgram *p, uint32_t n, const uint32_t *a, const uint32_t *b, uint32_t *out, int order, WsSim *stats) {
98 for (uint32_t i = 0; i < n; i++) out[i] = 0xffbadbadu;
99 uint64_t wi = 0, li = 0, sp = 0; int mf = 0, pb = 0;
100 for (uint32_t i0 = 0; i0 < n; i0 += CHUNK) {
101 uint32_t ni = n - i0 < CHUNK ? n - i0 : CHUNK;
102 WsSim s; memset(&s, 0, sizeof s);
103 s.p = p; s.threads = OMEGA_GPU_EW_THREADS; s.smem = g_smem; s.smem_bytes = sizeof g_smem; s.order = order;
104 uint64_t aa = (uintptr_t)(a + i0), ba = (uintptr_t)(b + i0), ca = (uintptr_t)(out + i0);
105 s.cbank[0x380 / 4] = (uint32_t)aa; s.cbank[0x380 / 4 + 1] = (uint32_t)(aa >> 32);
106 s.cbank[0x388 / 4] = (uint32_t)ba; s.cbank[0x388 / 4 + 1] = (uint32_t)(ba >> 32);
107 s.cbank[0x390 / 4] = (uint32_t)ca; s.cbank[0x390 / 4 + 1] = (uint32_t)(ca >> 32);
108 s.cbank[0x398 / 4] = ni;
109 s.ranges[0] = (WsRange){ (uintptr_t)aa, (uintptr_t)aa + (size_t)ni * 4 };
110 s.ranges[1] = (WsRange){ (uintptr_t)ba, (uintptr_t)ba + (size_t)ni * 4 };
111 s.ranges[2] = (WsRange){ (uintptr_t)ca, (uintptr_t)ca + (size_t)ni * 4 };
112 s.nranges = 3;
113 int err = ws_run(&s, (ni + OMEGA_GPU_EW_THREADS - 1) / OMEGA_GPU_EW_THREADS, 1);
114 wi += s.warp_issues; li += s.lane_issues; sp += s.splits;
115 if (s.max_frags > mf) mf = s.max_frags;
116 if (s.peak_bars > pb) pb = s.peak_bars;
117 if (err) { if (stats) *stats = s; return err; }
118 }
119 if (stats) { memset(stats, 0, sizeof *stats); stats->warp_issues = wi; stats->lane_issues = li; stats->splits = sp; stats->max_frags = mf; stats->peak_bars = pb; }
120 return 0;
121}
122static int codegen(OmegaGpuEwOp op, int mutant, BlackwellIRProgram *p, OmegaBlackwellKernel *k) {
123 memset(k, 0, sizeof *k);
124 return omega_gpu_elementwise_codegen_ir(op, mutant, p, k);
125}
126static int is_reconv_err(int e) {
127 return e == WS_E_SHFL_SPLIT || e == WS_E_BAR_SPLIT || e == WS_E_STUCK || e == WS_E_NEVER_JOINED || e == WS_E_JOIN_MISMATCH ||
128 e == WS_E_BSYNC_UNARMED || e == WS_E_BSSY_REARM || e == WS_E_NONMEMBER || e == WS_E_SHFL_EXITED;
129}
130
131/* --------------------------------------------------------- static walks */
132/* Scoreboard walk (as bench/prime_race/tests/gb10_sieve_host_test.c sb_hazards): a destination
133 * written under write scoreboard 0 is in flight until a wait mask that includes it. */
134static int sb_hazards(const BlackwellIRProgram *p) {
135 int inflight[64], n = 0, bad = 0;
136 for (size_t i = 0; i < p->count; i++) {
137 const BlackwellIRInsn *in = &p->insns[i];
138 uint32_t c = in->control;
139 if ((c >> 20) & 1u) n = 0;
140 const int src[3] = { in->src1_vreg, in->src2_vreg, in->src3_vreg };
141 for (int s = 0; s < 3; s++) for (int k = 0; src[s] >= 0 && k < n; k++) if (inflight[k] == src[s]) bad++;
142 if (c != 0 && ((c >> 14) & 7u) == 0 && in->dst_vreg >= 0 && n < 64) inflight[n++] = in->dst_vreg;
143 }
144 return bad;
145}
146/* Control-edge walk: the straight-line scoreboard reasoning above holds only if nothing is in
147 * flight across a branch, barrier op or exit. Every BRA / EXIT / BSSY / BSYNC must wait on every
148 * scoreboard set since the last wait. Returns the number of control ops that do not. */
149static int edge_waits(const BlackwellIRProgram *p) {
150 uint32_t pending = 0; int bad = 0;
151 for (size_t i = 0; i < p->count; i++) {
152 const BlackwellIRInsn *in = &p->insns[i];
153 uint32_t c = in->control, wait = (c >> 20) & 63u, wb = (c >> 14) & 7u, rb = (c >> 17) & 7u;
154 pending &= ~wait;
155 if (in->op == BW_IR_BRA || in->op == BW_IR_EXIT || in->op == BW_IR_BSSY || in->op == BW_IR_BSYNC) { if (pending) bad++; }
156 if (wb != 7u) pending |= 1u << wb;
157 if (rb != 7u) pending |= 1u << rb;
158 }
159 return bad;
160}
161static int count_op(const BlackwellIRProgram *p, BlackwellIROpcode op, int bar) {
162 int n = 0;
163 for (size_t i = 0; i < p->count; i++) if (p->insns[i].op == op && (bar < 0 || p->insns[i].bar_reg == bar)) n++;
164 return n;
165}
166static int find_op(const BlackwellIRProgram *p, BlackwellIROpcode op, int nth) {
167 for (size_t i = 0; i < p->count; i++) if (p->insns[i].op == op && nth-- == 0) return (int)i;
168 return -1;
169}
170
171/* --------------------------------------------------------- nvdisasm listing */
172static int nvdisasm_bytes(const uint8_t *code, size_t bytes, const char *name, const char *const *must, int n_must) {
173 const char *nv = "/usr/local/cuda/bin/nvdisasm";
174 if (access(nv, X_OK) != 0) { printf("nvdisasm: not present, listing check skipped for %s\n", name); return 0; }
175 char bin[96], lst[96], cmd[320];
176 snprintf(bin, sizeof bin, "/tmp/omega_rc_%s_%d.bin", name, (int)getpid());
177 snprintf(lst, sizeof lst, "/tmp/omega_rc_%s_%d.sass", name, (int)getpid());
178 FILE *f = fopen(bin, "wb"); if (!f) return -1;
179 fwrite(code, 1, bytes, f); fclose(f);
180 snprintf(cmd, sizeof cmd, "%s -b SM121 %s > %s 2>&1", nv, bin, lst);
181 int rc = system(cmd);
182 FILE *l = fopen(lst, "r"); if (!l) return -1;
183 char line[512]; int seen[16] = {0}, bad = 0, insns = 0;
184 while (fgets(line, sizeof line, l)) {
185 if (!strstr(line, "/*0")) continue;
186 insns++;
187 if (strstr(line, "?") || strstr(line, "error")) bad++;
188 for (int i = 0; i < n_must && i < 16; i++) if (strstr(line, must[i])) seen[i] = 1;
189 }
190 fclose(l);
191 int miss = 0;
192 for (int i = 0; i < n_must && i < 16; i++) if (!seen[i]) { printf(" %s: nvdisasm listing lacks %s\n", name, must[i]); miss++; }
193 printf("nvdisasm %s: rc=%d insns=%d undecodable=%d missing=%d (%s)\n", name, rc, insns, bad, miss, lst);
194 unlink(bin);
195 return (rc == 0 && bad == 0 && miss == 0) ? 0 : -1;
196}
197static int nvdisasm_kernel(OmegaGpuEwOp op, int mutant, const char *name, const char *const *must, int n_must) {
198 BlackwellIRProgram *p = calloc(1, sizeof *p); OmegaBlackwellKernel k;
199 if (!p || codegen(op, mutant, p, &k) != OMEGA_GPU_EW_OK) { free(p); return -1; }
200 int rc = nvdisasm_bytes(k.code, k.code_size, name, must, n_must);
201 free(k.code); free(p);
202 return rc;
203}
204
205/* --------------------------------------------------------- host part */
206typedef struct { const char *name; uint64_t warp_issues, lane_issues, splits; int max_frags, peak_bars; } Model;
207static Model g_model[NPROBES * 2]; static int g_nmodel;
208
209static int host_part(void) {
210 int before = g_failed;
211 /* 1. encoder golden words (nvcc 13.0.88 -arch=sm_121 oracle, docs/gpu-reconvergence-308.md) */
212 int fx = omega_blackwell_verify_codegen_fixtures_reconv();
213 CHECK(fx == 0, "encoder golden words BSSY / BSYNC and refusals (rc %d)", fx);
214
215 /* 2. region bookkeeping on a hand-built program */
216 {
217 BlackwellIRProgram *p = calloc(1, sizeof *p); BwRegions r; memset(&r, 0, sizeof r);
218 omega_bw_ir_init(p);
219 BlackwellIRInsn in; memset(&in, 0, sizeof in); in.dst_vreg = in.src1_vreg = in.src2_vreg = in.src3_vreg = -1;
220 in.op = BW_IR_BSSY; in.bar_reg = 0; in.imm = 2; omega_bw_ir_append(p, &in); /* 0 */
221 CHECK(bw_regions_next_bar(&r) == 0 && bw_regions_begin(&r, 0) == 0, "region begin returns B0");
222 in.op = BW_IR_BRA; in.imm = 1; in.predicate_p0 = true; omega_bw_ir_append(p, &in); /* 1 */
223 CHECK(bw_regions_exit(&r, 1) == 0, "region exit recorded");
224 in.op = BW_IR_NOP; in.predicate_p0 = false; omega_bw_ir_append(p, &in); /* 2 */
225 in.op = BW_IR_BSYNC; in.bar_reg = 0; omega_bw_ir_append(p, &in); /* 3 */
226 CHECK(bw_regions_join(&r, p, 3) == 0, "region join returns B0");
227 CHECK(p->insns[0].imm == 4 && p->insns[1].imm == 2, "join patched BSSY to the insn after BSYNC (%u) and the exit to the BSYNC (%u)", p->insns[0].imm, p->insns[1].imm);
228 CHECK(bw_regions_join(&r, p, 3) == -1, "join with no open region refused");
229 CHECK(bw_regions_exit(&r, 1) == -1, "exit with no open region refused");
230 /* exits table full */
231 memset(&r, 0, sizeof r); bw_regions_begin(&r, 0);
232 int ok = 1; for (int i = 0; i < BW_RECONV_MAX_EXITS; i++) ok &= bw_regions_exit(&r, 1) == 0;
233 CHECK(ok && bw_regions_exit(&r, 1) == -1, "exit table bound (%d) fails closed", BW_RECONV_MAX_EXITS);
234 /* join index inconsistent: BSYNC before BSSY, wrong barrier */
235 memset(&r, 0, sizeof r); bw_regions_begin(&r, 3);
236 CHECK(bw_regions_join(&r, p, 0) == -1, "join with BSSY after BSYNC refused");
237 memset(&r, 0, sizeof r); bw_regions_begin(&r, 0); p->insns[3].bar_reg = 1;
238 CHECK(bw_regions_join(&r, p, 3) == -1, "join with the wrong barrier register refused");
239 p->insns[3].bar_reg = 0;
240 /* depth bound */
241 memset(&r, 0, sizeof r); ok = 1;
242 for (int d = 0; d < BW_RECONV_MAX_DEPTH; d++) ok &= bw_regions_next_bar(&r) == d && bw_regions_begin(&r, d) == d;
243 CHECK(ok && bw_regions_next_bar(&r) == -1 && bw_regions_begin(&r, 99) == -1, "nesting bound %d: the next begin fails closed", BW_RECONV_MAX_DEPTH);
244 free(p);
245 }
246
247 /* 3. codegen: production probes build, refusals refuse, mutants differ */
248 static const struct { OmegaGpuEwOp op; const char *name; } refuse[] = {
249 { OMEGA_GPU_EW_RC_REFUSE_DEPTH17, "17 nested regions" }, { OMEGA_GPU_EW_RC_REFUSE_EXIT_SHFL, "EXIT then BAR/SHFL" },
250 { OMEGA_GPU_EW_RC_REFUSE_EXIT_IN_REGION, "EXIT inside a region" }, { OMEGA_GPU_EW_RC_REFUSE_BAR_IN_REGION, "BAR.SYNC inside a region" },
251 { OMEGA_GPU_EW_RC_REFUSE_SHFL_IN_REGION, "SHFL inside a region" }, { OMEGA_GPU_EW_RC_REFUSE_UNCLOSED, "region never joined" },
252 { OMEGA_GPU_EW_RC_REFUSE_JOIN_ONLY, "BSYNC with no BSSY" },
253 };
254 for (size_t i = 0; i < sizeof refuse / sizeof refuse[0]; i++) {
255 BlackwellIRProgram *p = calloc(1, sizeof *p); OmegaBlackwellKernel k;
256 int rc = codegen(refuse[i].op, 0, p, &k);
257 CHECK(rc == OMEGA_GPU_EW_CODEGEN_FAIL, "%s refused at codegen (rc %s)", refuse[i].name, omega_gpu_elementwise_rc_name(rc));
258 if (rc == OMEGA_GPU_EW_OK) free(k.code);
259 free(p);
260 }
261 for (size_t i = 0; i < NPROBES; i++) {
262 BlackwellIRProgram *p = calloc(1, sizeof *p); OmegaBlackwellKernel k;
263 int rc = codegen(PROBES[i].op, 0, p, &k);
264 CHECK(rc == OMEGA_GPU_EW_OK && k.gpr_count <= 64, "%s builds (rc %s, %zu insns, %u gprs)", PROBES[i].name, omega_gpu_elementwise_rc_name(rc), k.insn_count, k.gpr_count);
265 if (rc != OMEGA_GPU_EW_OK) { free(p); continue; }
266 int nb = count_op(p, BW_IR_BSSY, -1), ns = count_op(p, BW_IR_BSYNC, -1);
267 int want = PROBES[i].op == OMEGA_GPU_EW_RC_NESTED ? 2 : PROBES[i].op == OMEGA_GPU_EW_RC_DEPTH16 ? BW_RECONV_MAX_BAR : 1;
268 CHECK(nb == want && ns == want, "%s has %d BSSY and %d BSYNC (want %d)", PROBES[i].name, nb, ns, want);
269 if (PROBES[i].op == OMEGA_GPU_EW_RC_DEPTH16) {
270 int each = 1; for (int b = 0; b < BW_RECONV_MAX_BAR; b++) each &= count_op(p, BW_IR_BSSY, b) == 1 && count_op(p, BW_IR_BSYNC, b) == 1;
271 CHECK(each, "depth16 uses every barrier register B0..B15 exactly once");
272 }
273 CHECK(sb_hazards(p) == 0, "%s scoreboard walk clean", PROBES[i].name);
274 CHECK(edge_waits(p) == 0, "%s every control op waits on everything in flight", PROBES[i].name);
275 printf("codegen %s: %zu insns, %u gprs, %d regions\n", PROBES[i].name, p->count, k.gpr_count, nb);
276 uint8_t d0[32]; memcpy(d0, k.code_digest, 32); free(k.code);
277 for (int m = 1; m <= 3; m++) {
278 BlackwellIRProgram *mp = calloc(1, sizeof *mp); OmegaBlackwellKernel mk;
279 int mrc = codegen(PROBES[i].op, m, mp, &mk);
280 int same_ok = m == 1 && PROBES[i].op == OMEGA_GPU_EW_RC_DEPTH16; /* depth16 has no math mutant */
281 CHECK(mrc == OMEGA_GPU_EW_OK && ((memcmp(d0, mk.code_digest, 32) != 0) != same_ok), "%s mutant %d builds and %s (rc %s)", PROBES[i].name, m, same_ok ? "equals production" : "differs", omega_gpu_elementwise_rc_name(mrc));
282 if (mrc == OMEGA_GPU_EW_OK) {
283 if (m == 2) CHECK(count_op(mp, BW_IR_BSYNC, -1) == 0 && count_op(mp, BW_IR_BSSY, -1) == nb, "%s mutant 2 has no BSYNC", PROBES[i].name);
284 if (m == 3) CHECK(count_op(mp, BW_IR_BSYNC, -1) == ns, "%s mutant 3 keeps its BSYNCs", PROBES[i].name);
285 free(mk.code);
286 }
287 free(mp);
288 }
289 free(p);
290 }
291 /* the four regioned sieves build too (their sweep runs in gb10_sieve_host_test) */
292 for (int op = OMEGA_GPU_EW_PRIME_SIEVE_DIV1; op <= OMEGA_GPU_EW_PRIME_SIEVE_SHARED_DIV2; op++) {
293 BlackwellIRProgram *p = calloc(1, sizeof *p); OmegaBlackwellKernel k;
294 int rc = codegen((OmegaGpuEwOp)op, 0, p, &k);
295 int staged = op == OMEGA_GPU_EW_PRIME_SIEVE_SHARED_DIV1 || op == OMEGA_GPU_EW_PRIME_SIEVE_SHARED_DIV2;
296 int nb = rc == OMEGA_GPU_EW_OK ? count_op(p, BW_IR_BSSY, -1) : -1, want = (op >= OMEGA_GPU_EW_PRIME_SIEVE_DIV2 ? 2 : 1) + staged;
297 CHECK(rc == OMEGA_GPU_EW_OK && k.gpr_count <= 64 && nb == want && edge_waits(p) == 0, "sieve op %d builds with %d regions (rc %s, %u gprs)", op, nb, omega_gpu_elementwise_rc_name(rc), k.gpr_count);
298 if (rc == OMEGA_GPU_EW_OK) free(k.code);
299 free(p);
300 }
301
302 /* 4. nvdisasm: the words decode as BSSY/BSYNC on the real barrier registers, and the predicated
303 * and B15 forms the encoder can produce decode too (built by hand: encode_one) */
304 {
305 static const char *const m0[] = { "BSSY", "BSYNC", "B0", "SHFL.DOWN", "@P0 BRA" };
306 CHECK(nvdisasm_kernel(OMEGA_GPU_EW_RC_DIAMOND, 0, "diamond", m0, 5) == 0, "diamond listing decodes with BSSY/BSYNC B0");
307 static const char *const m1[] = { "BSSY", "BSYNC", "B15", "B7" };
308 CHECK(nvdisasm_kernel(OMEGA_GPU_EW_RC_DEPTH16, 0, "depth16", m1, 4) == 0, "depth16 listing decodes with B15");
309 static const char *const m2[] = { "BSSY", "B1", "BSYNC" };
310 CHECK(nvdisasm_kernel(OMEGA_GPU_EW_PRIME_SIEVE_DIV2, 0, "sieve_div2", m2, 3) == 0, "nested sieve listing decodes with B1");
311 uint8_t code[64]; uint32_t w[4]; BlackwellIRInsn in; memset(&in, 0, sizeof in); in.dst_vreg = in.src1_vreg = in.src2_vreg = in.src3_vreg = -1;
312 in.op = BW_IR_BSSY; in.bar_reg = 3; in.imm = 2; in.predicate_p0 = true; omega_blackwell_encode_one(&in, w); memcpy(code, w, 16);
313 in.op = BW_IR_BSYNC; in.bar_reg = 3; in.predicate_p0 = true; in.predicate_not = true; omega_blackwell_encode_one(&in, w); memcpy(code + 16, w, 16);
314 in.op = BW_IR_BSYNC; in.bar_reg = 15; in.predicate_p0 = false; in.predicate_not = false; omega_blackwell_encode_one(&in, w); memcpy(code + 32, w, 16);
315 in.op = BW_IR_EXIT; omega_blackwell_encode_one(&in, w); memcpy(code + 48, w, 16);
316 static const char *const m3[] = { "@P0 BSSY", "B3", "@!P0 BSYNC", "BSYNC.RECONVERGENT B15" };
317 CHECK(nvdisasm_bytes(code, 64, "forms", m3, 4) == 0, "predicated BSSY/BSYNC and B15 decode");
318 }
319
320 /* 5. simulator: every probe, every n, both fragment orders, exact against the oracle */
321 uint32_t *a = malloc(20000 * 4), *b = malloc(20000 * 4), *got = malloc(20000 * 4), *want = malloc(20000 * 4);
322 for (size_t i = 0; i < NPROBES; i++) {
323 BlackwellIRProgram *p = calloc(1, sizeof *p); OmegaBlackwellKernel k;
324 if (codegen(PROBES[i].op, 0, p, &k) != OMEGA_GPU_EW_OK) { free(p); continue; }
325 int fails = 0; uint64_t cases = 0;
326 for (size_t j = 0; j < NNS; j++) for (int order = 0; order < 2; order++) {
327 uint32_t n = NS[j]; fill(a, b, n, 0x308u + n * 7u + (uint32_t)order);
328 rc_oracle(PROBES[i].op, 0, n, a, b, want);
329 WsSim st; int err = ws_probe(p, n, a, b, got, order, &st);
330 uint32_t first; size_t bad = diff_count(got, want, n, &first);
331 cases++;
332 if (err || bad) { fails++; printf(" %s n=%u order=%d: sim %s, %zu words differ (first %u: got %08x want %08x)\n", PROBES[i].name, n, order, err ? st.msg : "clean", bad, first, first < n ? got[first] : 0, first < n ? want[first] : 0); }
333 if (n == 4096) { Model *m = &g_model[g_nmodel++]; m->name = PROBES[i].name; m->warp_issues = st.warp_issues; m->lane_issues = st.lane_issues; m->splits = st.splits; m->max_frags = st.max_frags; m->peak_bars = st.peak_bars; }
334 }
335 CHECK(fails == 0, "%s simulator vs oracle: %d of %" PRIu64 " cases differ", PROBES[i].name, fails, cases);
336 /* math mutant: simulator clean, result wrong (the oracle sees it) */
337 BlackwellIRProgram *mp = calloc(1, sizeof *mp); OmegaBlackwellKernel mk;
338 if (codegen(PROBES[i].op, 1, mp, &mk) == OMEGA_GPU_EW_OK) {
339 uint32_t n = 1000; fill(a, b, n, 0x1001u); rc_oracle(PROBES[i].op, 0, n, a, b, want);
340 WsSim st; int err = ws_probe(mp, n, a, b, got, 0, &st); uint32_t first; size_t bad = diff_count(got, want, n, &first);
341 if (PROBES[i].op == OMEGA_GPU_EW_RC_EXIT_BAR) CHECK(err == WS_E_SHFL_EXITED || err == WS_E_BAR_SPLIT, "R6 early-exit kernel: simulator reports SHFL_EXITED (got %s)", st.msg);
342 else if (PROBES[i].op == OMEGA_GPU_EW_RC_DEPTH16) CHECK(err == 0 && bad == 0, "depth16 'mutant 1' is the production kernel: clean and exact (err %d bad %zu)", err, bad);
343 else CHECK(err == 0 && bad > 0, "%s math mutant: simulator clean, %zu words differ (err %d)", PROBES[i].name, bad, err);
344 free(mk.code);
345 }
346 free(mp);
347 /* dropped join and corrupted target: the simulator must reject both, in both orders */
348 for (int m = 2; m <= 3; m++) for (int order = 0; order < 2; order++) {
349 BlackwellIRProgram *xp = calloc(1, sizeof *xp); OmegaBlackwellKernel xk;
350 if (codegen(PROBES[i].op, m, xp, &xk) == OMEGA_GPU_EW_OK) {
351 uint32_t n = 1000; fill(a, b, n, 0x2002u + (uint32_t)m);
352 WsSim st; int err = ws_probe(xp, n, a, b, got, order, &st);
353 CHECK(is_reconv_err(err), "%s mutant %d (%s) order %d rejected by the simulator: %s", PROBES[i].name, m, m == 2 ? "BSYNC dropped" : "BSSY target corrupted", order, err ? st.msg : "NOT CAUGHT");
354 if (m == 3) CHECK(err == WS_E_JOIN_MISMATCH || err == WS_E_STUCK, "%s corrupted target shows as JOIN_MISMATCH or STUCK (%s)", PROBES[i].name, ws_err_name(err));
355 free(xk.code);
356 }
357 free(xp);
358 }
359 free(k.code); free(p);
360 }
361 /* 6. hand mutations of the production diamond (controls on the simulator itself, independent of
362 * the emitter's mutant hooks): exit jumping past the BSYNC, BSYNC on the wrong barrier, a BSSY
363 * on an armed barrier. And the unmodified program as the positive control. */
364 {
365 BlackwellIRProgram *p = calloc(1, sizeof *p), *q = calloc(1, sizeof *q); OmegaBlackwellKernel k;
366 if (codegen(OMEGA_GPU_EW_RC_DIAMOND, 0, p, &k) == OMEGA_GPU_EW_OK) {
367 uint32_t n = 500; fill(a, b, n, 0x3003u); WsSim st;
368 int bssy = find_op(p, BW_IR_BSSY, 0), bsync = find_op(p, BW_IR_BSYNC, 0), ex = -1;
369 for (int i = bssy + 1; i < bsync; i++) if (p->insns[i].op == BW_IR_BRA && !p->insns[i].predicate_p0) ex = i;
370 CHECK(bssy >= 0 && bsync > bssy && ex > bssy, "diamond shape: BSSY %d, exit %d, BSYNC %d", bssy, ex, bsync);
371 CHECK(ws_probe(p, n, a, b, got, 0, &st) == 0, "positive control: untouched diamond runs clean");
372 *q = *p; q->insns[ex].imm += 1; /* exit lands after the BSYNC */
373 int e1 = ws_probe(q, n, a, b, got, 0, &st); CHECK(is_reconv_err(e1), "exit past the BSYNC rejected: %s", st.msg);
374 *q = *p; q->insns[bsync].bar_reg = 1; /* BSYNC on a barrier nobody armed */
375 int e2 = ws_probe(q, n, a, b, got, 0, &st); CHECK(e2 == WS_E_BSYNC_UNARMED, "BSYNC on an unarmed barrier rejected: %s", st.msg);
376 *q = *p; q->insns[bsync].op = BW_IR_NOP; /* join dropped by hand */
377 int e3 = ws_probe(q, n, a, b, got, 0, &st); CHECK(is_reconv_err(e3), "join replaced by NOP rejected: %s", st.msg);
378 *q = *p; q->insns[bssy].imm += 1; /* names the wrong join */
379 int e4 = ws_probe(q, n, a, b, got, 0, &st); CHECK(e4 == WS_E_JOIN_MISMATCH, "wrong join target rejected: %s", st.msg);
380 *q = *p; q->insns[bssy + 1] = p->insns[bssy]; /* second BSSY on the armed barrier (overwrites the ISETP) */
381 int e5 = ws_probe(q, n, a, b, got, 0, &st); CHECK(e5 == WS_E_BSSY_REARM, "BSSY on an armed barrier rejected: %s", st.msg);
382 free(k.code);
383 }
384 free(p); free(q);
385 }
386 free(a); free(b); free(got); free(want);
387 for (int i = 0; i < g_nmodel; i++) printf("model %s n=4096: warp_issues=%" PRIu64 " lane_util=%.3f splits=%" PRIu64 " max_frags=%d peak_bars=%d\n", g_model[i].name, g_model[i].warp_issues, g_model[i].warp_issues ? (double)g_model[i].lane_issues / (32.0 * (double)g_model[i].warp_issues) : 0.0, g_model[i].splits, g_model[i].max_frags, g_model[i].peak_bars);
388 return g_failed == before ? 0 : -1;
389}
390
391/* --------------------------------------------------------- chip part */
392static int chip(const char *out_path, int observe) {
393 FILE *out = out_path ? fopen(out_path, "w") : NULL;
394 if (out_path && !out) { printf("cannot open %s\n", out_path); return 2; }
395 if (out) fprintf(out, "{\"schema\":\"OMEGA_GPU_RECONV_V1\",\"issue\":308,\"comparison\":\"exact u32\",\"cases\":[");
396 int first = 1;
397 uint32_t *a = malloc(20000 * 4), *b = malloc(20000 * 4), *got = malloc(20000 * 4), *want = malloc(20000 * 4);
398 for (size_t i = 0; i < NPROBES; i++) {
399 for (size_t j = 0; j < NNS; j++) {
400 uint32_t n = NS[j]; fill(a, b, n, 0x5050u + n);
401 rc_oracle(PROBES[i].op, 0, n, a, b, want);
402 OmegaGpuEwInfo info; memset(&info, 0, sizeof info);
403 int rc = omega_gpu_reconv_probe_u32(PROBES[i].op, n, a, b, got, &info);
404 uint32_t fi; size_t bad = rc == OMEGA_GPU_EW_OK ? diff_count(got, want, n, &fi) : n;
405 int pass = rc == OMEGA_GPU_EW_OK && bad == 0;
406 CHECK(pass, "%s n=%u chip rc=%s violations=%zu err=\"%s\"", PROBES[i].name, n, omega_gpu_elementwise_rc_name(rc), bad, omega_gpu_elementwise_last_error());
407 if (out) fprintf(out, "%s{\"op\":\"%s\",\"regression\":\"%s\",\"n\":%u,\"rc\":\"%s\",\"violations\":%zu,\"pass\":%s,\"chip_elapsed_ns\":%" PRIu64 ",\"chip_calls\":%u,\"unwritten_words\":%u,\"cache_hit\":%s}",
408 first ? "" : ",", PROBES[i].name, PROBES[i].reg, n, omega_gpu_elementwise_rc_name(rc), bad, pass ? "true" : "false", info.elapsed_ns, info.chip_calls, info.unwritten_words, info.kernel_cache_hit ? "true" : "false");
409 first = 0;
410 if (!pass) { printf("STOP: %s failed on the chip at n=%u; no further launches of this probe\n", PROBES[i].name, n); break; }
411 }
412 }
413 if (out) fprintf(out, "],\"mutants\":[");
414 first = 1;
415 for (size_t i = 0; i < NPROBES; i++) {
416 if (PROBES[i].op == OMEGA_GPU_EW_RC_DEPTH16) continue; /* no math mutant */
417 uint32_t n = 1000; fill(a, b, n, 0x6060u); rc_oracle(PROBES[i].op, 0, n, a, b, want);
418 omega_gpu_elementwise_test_set_mutant((int)PROBES[i].op);
419 /* R6's math mutant is the early-exit kernel: the emitter refuses it on the launch path
420 * (the bypass exists only for codegen_ir), so the catch is CODEGEN_FAIL: no undefined
421 * SHFL ever reaches the chip. */
422 OmegaGpuEwInfo info; memset(&info, 0, sizeof info);
423 int rc = omega_gpu_reconv_probe_u32(PROBES[i].op, n, a, b, got, &info);
424 uint32_t fi; size_t bad = rc == OMEGA_GPU_EW_OK ? diff_count(got, want, n, &fi) : n;
425 int caught = rc != OMEGA_GPU_EW_OK || bad > 0;
426 omega_gpu_elementwise_test_set_mutant(0);
427 CHECK(caught, "%s math mutant caught (rc %s, violations %zu)", PROBES[i].name, omega_gpu_elementwise_rc_name(rc), bad);
428 if (PROBES[i].op == OMEGA_GPU_EW_RC_EXIT_BAR) CHECK(rc == OMEGA_GPU_EW_CODEGEN_FAIL, "R6 early-exit form refused at codegen on the launch path (rc %s)", omega_gpu_elementwise_rc_name(rc));
429 if (out) fprintf(out, "%s{\"op\":\"%s\",\"mutant\":1,\"rc\":\"%s\",\"violations\":%zu,\"caught\":%s}", first ? "" : ",", PROBES[i].name, omega_gpu_elementwise_rc_name(rc), bad, caught ? "true" : "false");
430 first = 0;
431 }
432 if (out) fprintf(out, "],\"observations\":[");
433 if (observe) {
434 /* The dropped-join kernel (rejected by the simulator above) on the chip once, small n:
435 * recorded as an observation of what the hardware does without the BSYNC. Not a gate. */
436 uint32_t n = 1000; fill(a, b, n, 0x7070u); rc_oracle(OMEGA_GPU_EW_RC_DIAMOND, 0, n, a, b, want);
437 omega_gpu_elementwise_test_set_mutant_level(OMEGA_GPU_EW_RC_DIAMOND, 2);
438 OmegaGpuEwInfo info; memset(&info, 0, sizeof info);
439 int rc = omega_gpu_reconv_probe_u32(OMEGA_GPU_EW_RC_DIAMOND, n, a, b, got, &info);
440 uint32_t fi; size_t bad = rc == OMEGA_GPU_EW_OK ? diff_count(got, want, n, &fi) : n;
441 omega_gpu_elementwise_test_set_mutant(0);
442 printf("observation: diamond with the BSYNC dropped, n=%u: rc=%s, %zu of %u words differ from the oracle (first %u)\n", n, omega_gpu_elementwise_rc_name(rc), bad, n, fi);
443 if (out) fprintf(out, "{\"op\":\"rc_diamond\",\"mutant\":2,\"what\":\"BSYNC dropped\",\"n\":%u,\"rc\":\"%s\",\"violations\":%zu,\"first_bad\":%u,\"gate\":false}", n, omega_gpu_elementwise_rc_name(rc), bad, fi);
444 }
445 CHECK(omega_gpu_session_open_count() == 1, "device opened once for the battery (opens=%u)", omega_gpu_session_open_count());
446 if (out) {
447 fprintf(out, "],\"sim_model_n4096\":[");
448 for (int i = 0; i < g_nmodel; i++) fprintf(out, "%s{\"op\":\"%s\",\"order\":%d,\"warp_issues\":%" PRIu64 ",\"lane_issues\":%" PRIu64 ",\"splits\":%" PRIu64 ",\"max_frags\":%d,\"peak_bars\":%d}", i ? "," : "", g_model[i].name, i & 1, g_model[i].warp_issues, g_model[i].lane_issues, g_model[i].splits, g_model[i].max_frags, g_model[i].peak_bars);
449 fprintf(out, "],\"device_opens\":%u,\"checks\":%d,\"failed\":%d,\"verdict\":\"%s\"}\n", omega_gpu_session_open_count(), g_checks, g_failed, g_failed == 0 ? "OMEGA_GPU_RECONV_PASS" : "OMEGA_GPU_RECONV_FAIL");
450 fclose(out);
451 }
452 free(a); free(b); free(got); free(want);
453 return 0;
454}
455
456int main(int argc, char **argv) {
457 const char *out_path = NULL; int host = 0, observe = 0;
458 for (int i = 1; i < argc; i++) {
459 if (strcmp(argv[i], "--host-only") == 0) host = 1;
460 else if (strcmp(argv[i], "--observe-dropped-join") == 0) observe = 1;
461 else if (strcmp(argv[i], "--out") == 0 && i + 1 < argc) out_path = argv[++i];
462 }
463 int hrc = host_part();
464 if (!host) {
465 if (hrc != 0) { printf("host part failed: no chip launches\nFAIL: %d checks, %d failed\n", g_checks, g_failed); return 1; }
466 if (chip(out_path, observe) != 0) return 2;
467 }
468 printf("%s: %d checks, %d failed\n", g_failed ? "FAIL" : "PASS", g_checks, g_failed);
469 return g_failed ? 1 : 0;
470}