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 / tests / gpu_reconv_test.c

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