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