Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/omega-mv/patches/89405e34a6028871 # omega-mv Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance. Brought by drakestapleton Sign in to star 1 stars 0 watching About Files History Releases Proposed changes Issues For developers ## GitHub aien-dev/omega-mv#310 (code only) closed derived `89405e34a6028871` #310 Imported from GitHub aien-dev/omega-mv#310 (code only) GitHub's metadata for this pull request is unavailable: its state is DERIVED from the commits (merged: the head is reachable from this project's main; open: GitHub kept a merge ref; else closed). Base `ed7f994afcf525c76428e95a3d57a6f3528faaf6` · Head `ccd4a1fa34f26b6c93f70a60573280563b47c05a` ### Apply it ``` git checkout -b patch-89405e34a6028871 ed7f994afcf525c76428e95a3d57a6f3528faaf6 curl -fsSL https://interplane.io/drakestapleton/omega-mv/patches/89405e34a6028871.patch | git am ``` Raw patch (mbox) · Diff ### Provenance This proposal is imported from GitHub; it is not signed by its author. Its import statement and source record: ``` github-import 1 aien-dev/omega-mv 310 ccd4a1fa34f26b6c93f70a60573280563b47c05a ``` ``` {"kind":"github-import","owner":"aien-dev","repo":"omega-mv","number":310,"url":"https://github.com/aien-dev/omega-mv/pull/310","export_sha256":"967c79e26a893657b5cdaa45efd442be5dbd2ecfe68e25f9a8c1b5da4d60d452","imported_utc":"2026-11-18T16:37:15Z","metadata":"unavailable","state_derived":true} ``` ### Changes ``` diff --git a/.crumb b/.crumb index e245c5a26..c7ae2b8ef 100644 --- a/.crumb +++ b/.crumb @@ -51,8 +51,8 @@ "purpose": "Directory calibration. Purpose not yet described by a human." }, { - "digest": "01f9bcaf892355eff231425a96bf8f625fc9d9be4c1480991db92b6fefd6cd49", - "last_commit": "f816473", + "digest": "bb89b5d99f5816b19958cdca790ea2f2066d87c00a0d78f4db6955f1540ed821", + "last_commit": "1f4a3c7", "name": "docs", "purpose": "Directory docs. Purpose not yet described by a human." }, @@ -63,8 +63,8 @@ "purpose": "Directory doctrine. Purpose not yet described by a human." }, { - "digest": "243c8e51a2a001d63e898e6853e3ef522a65a92d6ad7344e74f5d54c8e575cb9", - "last_commit": "8190bad", + "digest": "ba7d7c6e399d6a92a34be467f1d5501a9620a63839d38c27bcf8fb90b7448a02", + "last_commit": "197566a", "name": "mk", "purpose": "Directory mk. Purpose not yet described by a human." }, @@ -87,33 +87,33 @@ "purpose": "Directory spec. Purpose not yet described by a human." }, { - "digest": "6d49877f41b071c4f28e00bb47293fd6c483791242e7a7fc54460a5e0f6ae3ca", - "last_commit": "7f36700", + "digest": "e96259ccad1aad77187347965341897722b089df3ee84a17145a32e8d308a4a6", + "last_commit": "197566a", "name": "src", "purpose": "Directory src. Purpose not yet described by a human." }, { - "digest": "c9825de4f77620c738f1cac290238b55040200959e0938ac03fa41a3c95c488d", - "last_commit": "f816473", + "digest": "03e780fc46936d8b534e4992f9d87e3ce95711a40fdfe0128a96328410e3af5b", + "last_commit": "197566a", "name": "tests", "purpose": "Directory tests. Purpose not yet described by a human." }, { - "digest": "ef22f063312bab17b11019f16b48d0e30d907c7bd1af458b6dbc6923907b517f", - "last_commit": "f816473", + "digest": "808edb958b46d236f503828399cbdb7f1cb62f8eebfd1604d4768b42734b459f", + "last_commit": "197566a", "name": "tools", "purpose": "Directory tools. Purpose not yet described by a human." } ], - "children_root": "sha256:2c5c4c1dd77a1bf73e662f3cb0c5d707b294bbdc63f5821947f4bc62d8ebaa17", - "compiled_at": "2026-10-06T01:43:54Z", + "children_root": "sha256:a1b9d0223f485c3d79afd990b37b8a8575e6668d77f9a85ccbe77f136f7388d1", + "compiled_at": "2026-10-06T01:57:59Z", "compiler": "crumb-compile/1", - "digest": "sha256:77c5261e0f8d0b6a3cfee57862bced9d40b8c2002774fcbbc332821556e6f121", - "evidence_files": 3304, - "evidence_root": "sha256:9594e3e2a05603747e60ab08376dcf98a6857a89e43720f33126d5b68e41f4d1", - "files": 5127, - "generated_at_commit": "df9f40a0435a70d680f6b49297815e53e3497ea9", - "source_tree": "sha256:7a3871434c18631c02fc551dce23a09b61df1fdfc8852b4fff3b3154a9f80c38" + "digest": "sha256:be23f82039edc46a35eabd6a56e179f144df76e656d6fe46a6de3641d4db8bbc", + "evidence_files": 3415, + "evidence_root": "sha256:1b9ca1bba224bd584685de23bd348db4924906fcbbef61d70265a267c57c0ab1", + "files": 5243, + "generated_at_commit": "1f4a3c7821d769a99ce1fcc896ac8ee30a33ea69", + "source_tree": "sha256:eba61a1b9111b0ed16e83ca6a589174e89d646facd904ca708b3993116ae3d41" }, "owner_lane": "", "provenance": { diff --git a/Makefile b/Makefile index 0de1d65b8..39639f824 100644 --- a/Makefile +++ b/Makefile @@ -2246,6 +2246,17 @@ $(GPU_EW_TEST): tests/gpu_elementwise_test.c src/omega_gpu_elementwise_api.h $(O test-gpu-elementwise: $(GPU_EW_TEST) ./$(GPU_EW_TEST) --host-only +# omega #308: structured warp reconvergence (BSSY/BSYNC regions). test-gpu-reconv is +# host-only (encoder goldens, region bookkeeping, refusals, nvdisasm listing, every probe +# kernel through the SIMT warp simulator tests/bw_warp_sim.h against the oracle, mutation +# controls); the chip gate is tools/run_gpu_reconv_chip.sh. +.PHONY: test-gpu-reconv +GPU_RECONV_TEST = $(OUT_DIR)/gpu_reconv_test +$(GPU_RECONV_TEST): tests/gpu_reconv_test.c tests/bw_warp_sim.h src/omega_gpu_elementwise_api.h src/omega_bw_reconv.h $(OUT_DIR)/libomega_gpu.a + $(CC) $(CFLAGS) -Itests -o $@ tests/gpu_reconv_test.c $(OUT_DIR)/libomega_gpu.a -lpthread -lm +test-gpu-reconv: $(GPU_RECONV_TEST) + ./$(GPU_RECONV_TEST) --host-only + # FB-1 cut 5: native gqa_attention (f32 KV) and paged_attention (bf16 KV, + batch). # test-gpu-attention runs host-only (refusals, codegen, nvdisasm listing) and then the # whole parity battery through the host IR simulator (--sim, no chip); the chip @@ -2253,8 +2264,8 @@ test-gpu-elementwise: $(GPU_EW_TEST) # (tools/run_gpu_attention_chip.sh). -ffp-contract=off keeps the oracle free of FMA. .PHONY: test-gpu-attention GPU_ATTN_TEST = $(OUT_DIR)/gpu_attention_test -$(GPU_ATTN_TEST): tests/gpu_attention_test.c src/omega_gpu_attention_api.h $(OUT_DIR)/libomega_gpu.a - $(CC) $(CFLAGS) -ffp-contract=off -o $@ tests/gpu_attention_test.c $(OUT_DIR)/libomega_gpu.a -lpthread -lm +$(GPU_ATTN_TEST): tests/gpu_attention_test.c tests/bw_warp_sim.h src/omega_gpu_attention_api.h $(OUT_DIR)/libomega_gpu.a + $(CC) $(CFLAGS) -ffp-contract=off -Itests -o $@ tests/gpu_attention_test.c $(OUT_DIR)/libomega_gpu.a -lpthread -lm test-gpu-attention: $(GPU_ATTN_TEST) ./$(GPU_ATTN_TEST) --host-only ./$(GPU_ATTN_TEST) --sim diff --git a/bench/prime_race/.crumb b/bench/prime_race/.crumb index 6673818e8..7f2f6bcbd 100644 --- a/bench/prime_race/.crumb +++ b/bench/prime_race/.crumb @@ -3,14 +3,14 @@ "generated": { "children": [], "children_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", - "compiled_at": "2026-10-05T21:43:28Z", + "compiled_at": "2026-10-05T23:21:14Z", "compiler": "crumb-compile/1", - "digest": "sha256:e48fd63d20aa45947aecd81b6481d5edfce2fdd5f88f5e1f70ae18026a19e679", + "digest": "sha256:2ff0bcbc1cfcfd4b5c4572225f063ff0f542447b9cc85b413a183eebc6d26cf0", "evidence_files": 0, "evidence_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", "files": 10, - "generated_at_commit": "7b961a5590f1087971321490a2655757d72cd31e", - "source_tree": "sha256:819b99c3125bfc48608d2177983d34efa8c81aab38b7a3cc57c218fbdfe37c5a" + "generated_at_commit": "2f8b6f85971afc782d986abb888e55fa200ca3d0", + "source_tree": "sha256:eb185f001ff6c322c33228fe0021cf8bd22ebf3183d990ca54a48ad3cceba033" } }, "invariants": [ diff --git a/bench/prime_race/gb10_native.c b/bench/prime_race/gb10_native.c index b735e046d..5772600d9 100644 --- a/bench/prime_race/gb10_native.c +++ b/bench/prime_race/gb10_native.c @@ -22,6 +22,10 @@ * sleeping for up to N us before the session's usual 50 us sleep-poll; unset or 0 keeps the old * wait. Any other value fails setup. Reported in the detail JSON as spin_us. * + * Mark loop (omega #308): PR_GB10_MARK=uniform (default, campaign C5), divergent (the original + * lane-dependent mark loop inside a BSSY/BSYNC reconvergence region, regression R1) or divergent2 + * (the prime loop in a region as well, nested). Any other value fails setup. Reported as mark. + * * Labels (handoff 2026-10-05 DESIGN): algorithm=other, faithful=no, bits=1, * environment linux-hosted-gb10-native. Build with -DPR_GB10_MUTANT=1 only for the chip * test's negative control (the r == 0 fix is dropped; the oracle must catch it). @@ -77,11 +81,24 @@ static int gb_table_global(void) { return -1; } +/* PR_GB10_MARK (omega #308): "uniform" or unset = the C5 warp-uniform mark loop; "divergent" = + * the original lane-dependent mark loop inside a BSSY/BSYNC region (regression R1); "divergent2" = + * the prime loop in a region as well (nested). -1 on any other value. */ +static int gb_mark_mode(void) { + const char *v = getenv("PR_GB10_MARK"); + if (!v || strcmp(v, "uniform") == 0) return 0; + if (strcmp(v, "divergent") == 0) return 1; + if (strcmp(v, "divergent2") == 0) return 2; + return -1; +} +static const char *const GB_MARK_NAMES[3] = { "uniform", "divergent", "divergent2" }; + typedef struct { uint64_t limit, odd; uint32_t nwords, ctas, budget, stride, lastw, tailinv; uint32_t spin_us; /* marker wait spin window (campaign C4) */ int table_global; /* PR_GB10_TABLE=global: never stage the table (campaign C6) */ + int mark; /* PR_GB10_MARK: 0 uniform (C5), 1 regioned mark loop, 2 nested regions (omega #308) */ uint32_t root; /* isqrt(limit) */ uint32_t nprimes; /* odd primes in the table of the last pass (sentinel excluded) */ size_t table_cap; /* entries, sentinel included */ @@ -145,6 +162,8 @@ static int gb_setup(void **state, uint64_t limit) { if (s->spin_us == UINT32_MAX) { fprintf(stderr, "gb10-native: PR_GB10_SPIN_US must be 0..%u\n", OMEGA_GPU_SESSION_MAX_SPIN_US); return -1; } s->table_global = gb_table_global(); if (s->table_global < 0) { fprintf(stderr, "gb10-native: PR_GB10_TABLE must be auto or global\n"); return -1; } + s->mark = gb_mark_mode(); + if (s->mark < 0) { fprintf(stderr, "gb10-native: PR_GB10_MARK must be uniform, divergent or divergent2\n"); return -1; } uint32_t blocks = (s->nwords + GB_THREADS - 1) / GB_THREADS; s->ctas = blocks == 0 ? 1 : (blocks > s->budget ? s->budget : blocks); s->stride = s->ctas * GB_THREADS; @@ -161,6 +180,8 @@ static int gb_setup(void **state, uint64_t limit) { /* campaign C3: stage the table in shared memory whenever it fits the QMD bound */ if (!s->table_global && (uint64_t)s->entries * 12 <= OMEGA_BW_QMD_MAX_SHARED_BYTES) s->shared_bytes = s->entries * 12; OmegaGpuEwOp op = s->shared_bytes ? OMEGA_GPU_EW_PRIME_SIEVE_SHARED : OMEGA_GPU_EW_PRIME_SIEVE; + if (s->mark == 1) op = s->shared_bytes ? OMEGA_GPU_EW_PRIME_SIEVE_SHARED_DIV1 : OMEGA_GPU_EW_PRIME_SIEVE_DIV1; + if (s->mark == 2) op = s->shared_bytes ? OMEGA_GPU_EW_PRIME_SIEVE_SHARED_DIV2 : OMEGA_GPU_EW_PRIME_SIEVE_DIV2; if (omega_gpu_elementwise_codegen_ir(op, PR_GB10_MUTANT, NULL, &s->k) != OMEGA_GPU_EW_OK) { fprintf(stderr, "gb10-native: kernel codegen failed\n"); return -1; @@ -242,8 +263,8 @@ static uint64_t gb_threads(void *state) { gb_state *s = state; return s ? s->str static int gb_detail(void *state, FILE *f) { gb_state *s = state; if (!s) return -1; - fprintf(f, "{\"grid\": %u, \"cta_budget\": %u, \"block\": %u, \"words\": %u, \"nprimes\": %u, \"table\": \"%s\", \"shared_bytes\": %u, \"spin_us\": %u, \"kernel_code_sha256\": \"", - s->ctas, s->budget, GB_THREADS, s->nwords, s->nprimes, s->shared_bytes ? "shared" : "global", s->shared_bytes, s->spin_us); + fprintf(f, "{\"grid\": %u, \"cta_budget\": %u, \"block\": %u, \"words\": %u, \"nprimes\": %u, \"table\": \"%s\", \"shared_bytes\": %u, \"spin_us\": %u, \"mark\": \"%s\", \"kernel_code_sha256\": \"", + s->ctas, s->budget, GB_THREADS, s->nwords, s->nprimes, s->shared_bytes ? "shared" : "global", s->shared_bytes, s->spin_us, GB_MARK_NAMES[s->mark]); for (int i = 0; i < 32; i++) fprintf(f, "%02x", s->k.code_digest[i]); fprintf(f, "\", \"kernel_insns\": %zu, \"kernel_gpr\": %u, \"mutant\": %d, \"launches\": %" PRIu64 ", \"diag_kernel_ns_total\": %" PRIu64 diff --git a/bench/prime_race/tests/gb10_sieve_host_test.c b/bench/prime_race/tests/gb10_sieve_host_test.c index cf82a6007..28cc73768 100644 --- a/bench/prime_race/tests/gb10_sieve_host_test.c +++ b/bench/prime_race/tests/gb10_sieve_host_test.c @@ -17,6 +17,7 @@ #include "prime_race_impl.h" #include "omega_blackwell_codegen.h" #include "omega_gpu_elementwise_api.h" +#include "bw_warp_sim.h" /* SIMT warp simulator (omega #308): the regioned sieves run through it */ #include #include #include @@ -172,6 +173,24 @@ static void sim_cta(const BlackwellIRProgram *p, uint32_t cta) { } } +/* omega #308: when g_ws_order >= 0, sim_sieve runs the CTAs through the SIMT warp simulator + * (tests/bw_warp_sim.h: lane masks, divergence, BSSY/BSYNC) in that fragment order instead of + * the per-thread simulator above; a simulator error lands in g_sim_err as 200 + its code. */ +static int g_ws_order = -1; +static WsSim g_ws_last; +static uint8_t g_ws_smem[65536]; +static void ws_sieve_run(const BlackwellIRProgram *p, uint32_t ctas) { + WsSim s; memset(&s, 0, sizeof s); + s.p = p; s.threads = OMEGA_GPU_EW_THREADS; s.order = g_ws_order; + memcpy(s.cbank, g_cb, sizeof s.cbank); + s.smem = g_ws_smem; s.smem_bytes = g_smem_bytes > sizeof g_ws_smem ? (uint32_t)sizeof g_ws_smem : g_smem_bytes; + s.ranges[0] = (WsRange){ g_ranges[0].lo, g_ranges[0].hi }; s.ranges[1] = (WsRange){ g_ranges[1].lo, g_ranges[1].hi }; s.nranges = 2; + int err = ws_run(&s, ctas, 1); + g_ws_last = s; + g_steps += s.warp_issues; + if (err) g_sim_err = 200 + err; +} + /* Mirrors gb10_native.c's pass on the host: same table, same argument words; `budget` * is the CTA cap (PR_GB10_CTA_BUDGET, default OMEGA_GPU_EW_MAX_CTAS). */ static int sim_sieve(const BlackwellIRProgram *p, uint64_t limit, uint32_t budget, int staged, uint64_t *words) { @@ -205,8 +224,8 @@ static int sim_sieve(const BlackwellIRProgram *p, uint64_t limit, uint32_t budge g_ranges[0] = (Range){ (uintptr_t)tab, (uintptr_t)tab + cap * 12 }; g_ranges[1] = (Range){ (uintptr_t)out, (uintptr_t)out + (size_t)nwords * 4 }; g_sim_err = 0; - for (uint32_t cta = 0; cta < ctas && !g_sim_err; cta++) - sim_cta(p, cta); + if (g_ws_order >= 0) ws_sieve_run(p, ctas); + else for (uint32_t cta = 0; cta < ctas && !g_sim_err; cta++) sim_cta(p, cta); size_t nw64 = pr_words64(limit); memset(words, 0, nw64 * 8); memcpy(words, out, (size_t)nwords * 4); @@ -648,7 +667,115 @@ int main(int argc, char **argv) { free(sk.code); free(smk.code); free(sp); free(smp); free(nobar); } - /* 6. division model */ + /* 6. structured reconvergence (omega #308, regression R1): the sieve with its lane-dependent + * loops put back inside BSSY/BSYNC regions. DIV1 = the 888a011 mark loop in a region, DIV2 = + * the prime loop in an outer region too (nested). Each variant: codegen, hazard walks, + * listing, the oracle sweep through the warp simulator in both fragment orders, the math + * mutant caught, and the dropped-join / corrupted-target mutants rejected by the simulator. + * Then the warp-issue model of the C5 kernel against the regioned ones (a host model, not + * a measurement: the chip number comes from gb10_native PR_GB10_MARK=divergent). */ + { + static const struct { OmegaGpuEwOp op; int staged; const char *name; int regions; } dv[] = { + { OMEGA_GPU_EW_PRIME_SIEVE_DIV1, 0, "div1", 1 }, { OMEGA_GPU_EW_PRIME_SIEVE_SHARED_DIV1, 1, "shared_div1", 2 }, + { OMEGA_GPU_EW_PRIME_SIEVE_DIV2, 0, "div2", 2 }, { OMEGA_GPU_EW_PRIME_SIEVE_SHARED_DIV2, 1, "shared_div2", 3 }, + }; /* the staged variants also put the staging loop (lane-dependent trip count) in a region */ + static const char *const MUST_DIV[] = { "BSSY", "BSYNC", "IMAD.HI.U32", "@P0 BRA", "@!P0 BRA", "@P0 EXIT", "STG.E", "LDG.E" }; + /* positive control for the adapter: the production (C5) kernel through the warp simulator */ + { + uint64_t L = 1000000, *want = oracle_bitmap(L), *got = calloc(pr_words64(L) + 1, 8); + g_ws_order = 0; + int r = want && got ? sim_sieve(prog, L, OMEGA_GPU_EW_MAX_CTAS, 0, got) : -1; + g_ws_order = -1; + CHECK(r == 0 && want && memcmp(got, want, pr_words64(L) * 8) == 0, "C5 kernel through the warp simulator at 1e6 (rc %d err %d)", r, g_sim_err); + printf("warp-sim model C5 (global) 1e6: warp_issues=%" PRIu64 " lane_util=%.3f splits=%" PRIu64 " max_frags=%d\n", + g_ws_last.warp_issues, g_ws_last.warp_issues ? (double)g_ws_last.lane_issues / (32.0 * (double)g_ws_last.warp_issues) : 0.0, g_ws_last.splits, g_ws_last.max_frags); + /* the production staged kernel too: its staging loop splits the warp before BAR.SYNC + * (allowed on the chip since Volta; counted here, and the fragments stay split) */ + BlackwellIRProgram *cp = calloc(1, sizeof *cp); OmegaBlackwellKernel ck; memset(&ck, 0, sizeof ck); + if (cp && omega_gpu_elementwise_codegen_ir(OMEGA_GPU_EW_PRIME_SIEVE_SHARED, 0, cp, &ck) == OMEGA_GPU_EW_OK) { + want = oracle_bitmap(L); got = calloc(pr_words64(L) + 1, 8); + g_ws_order = 0; + r = want && got ? sim_sieve(cp, L, OMEGA_GPU_EW_MAX_CTAS, 1, got) : -1; + g_ws_order = -1; + CHECK(r == 0 && want && memcmp(got, want, pr_words64(L) * 8) == 0, "C5 staged kernel through the warp simulator at 1e6 (rc %d err %d)", r, g_sim_err); + printf("warp-sim model C5 (staged) 1e6: warp_issues=%" PRIu64 " lane_util=%.3f splits=%" PRIu64 " max_frags=%d bar_split_arrivals=%" PRIu64 "\n", + g_ws_last.warp_issues, g_ws_last.warp_issues ? (double)g_ws_last.lane_issues / (32.0 * (double)g_ws_last.warp_issues) : 0.0, g_ws_last.splits, g_ws_last.max_frags, g_ws_last.bar_split_arrivals); + free(ck.code); + } else CHECK(0, "C5 staged codegen for the warp-sim control"); + free(cp); + free(want); free(got); + } + for (size_t v = 0; v < sizeof dv / sizeof dv[0]; v++) { + BlackwellIRProgram *dp = calloc(1, sizeof *dp); + OmegaBlackwellKernel dk; memset(&dk, 0, sizeof dk); + int drc = dp ? omega_gpu_elementwise_codegen_ir(dv[v].op, 0, dp, &dk) : -1; + CHECK(drc == OMEGA_GPU_EW_OK && dk.gpr_count <= 64, "%s codegen rc %d gprs %u", dv[v].name, drc, dk.gpr_count); + if (drc != OMEGA_GPU_EW_OK) { free(dp); continue; } + int nbssy = 0, nbsync = 0; + for (size_t i = 0; i < dp->count; i++) { nbssy += dp->insns[i].op == BW_IR_BSSY; nbsync += dp->insns[i].op == BW_IR_BSYNC; } + CHECK(nbssy == dv[v].regions && nbsync == dv[v].regions, "%s has %d BSSY / %d BSYNC (want %d)", dv[v].name, nbssy, nbsync, dv[v].regions); + printf("%s kernel: %zu IR insns, %u GPRs, %d regions, sha256 ", dv[v].name, dp->count, dk.gpr_count, nbssy); + for (int i = 0; i < 32; i++) printf("%02x", dk.code_digest[i]); + printf("\n"); + CHECK(sb_hazards(dp, 1) == 0, "%s scoreboard hazards", dv[v].name); + CHECK(fixed_hazards(dp, &dk) == 0, "%s encoded fixed RAW hazards", dv[v].name); + if (!dv[v].staged) nvdisasm_check(&dk, dp, MUST_DIV, dv[v].name); + int dfail = 0, dn = 0; + for (int order = 0; order < 2; order++) for (size_t i = 0; i < nl; i++, dn++) { + uint64_t L = lims[i], *want = oracle_bitmap(L), *got = calloc(pr_words64(L) + 1, 8); + if (!want || !got) { CHECK(0, "alloc"); free(want); free(got); break; } + g_ws_order = order; + int r = sim_sieve(dp, L, OMEGA_GPU_EW_MAX_CTAS, dv[v].staged, got); + g_ws_order = -1; + if (r != 0 || memcmp(got, want, pr_words64(L) * 8) != 0) { + if (!dfail++) printf(" %s order %d limit %" PRIu64 ": sim rc %d err %d (%s), differs\n", dv[v].name, order, L, r, g_sim_err, g_sim_err >= 200 ? ws_err_name(g_sim_err - 200) : "-"); + } + if (L == 1000000 && order == 0) { + CHECK(count_primes(got, L) == 78498, "%s pi(1e6) = %" PRIu64, dv[v].name, count_primes(got, L)); + printf("warp-sim model %s 1e6: warp_issues=%" PRIu64 " lane_util=%.3f splits=%" PRIu64 " max_frags=%d peak_bars=%d bar_split_arrivals=%" PRIu64 "\n", dv[v].name, + g_ws_last.warp_issues, g_ws_last.warp_issues ? (double)g_ws_last.lane_issues / (32.0 * (double)g_ws_last.warp_issues) : 0.0, g_ws_last.splits, g_ws_last.max_frags, g_ws_last.peak_bars, g_ws_last.bar_split_arrivals); + CHECK(g_ws_last.bar_split_arrivals == 0, "%s reaches BAR.SYNC as a whole warp", dv[v].name); + } + free(want); free(got); + } + static const uint32_t budgets[] = { 1, 2, 128, 256 }; + for (size_t b = 0; b < sizeof budgets / sizeof budgets[0]; b++) { + uint64_t edge = 64ull * budgets[b] * OMEGA_GPU_EW_THREADS; + const uint64_t ls[] = { edge - 1, edge, edge + 1 }; + for (size_t i = 0; i < 3; i++, dn++) { + uint64_t L = ls[i], *want = oracle_bitmap(L), *got = calloc(pr_words64(L) + 1, 8); + if (!want || !got) { CHECK(0, "alloc"); free(want); free(got); break; } + g_ws_order = (int)(i & 1); + int r = sim_sieve(dp, L, budgets[b], dv[v].staged, got); + g_ws_order = -1; + if (r != 0 || memcmp(got, want, pr_words64(L) * 8) != 0) { if (!dfail++) printf(" %s budget %u limit %" PRIu64 ": sim rc %d err %d\n", dv[v].name, budgets[b], L, r, g_sim_err); } + free(want); free(got); + } + } + CHECK(dfail == 0, "%s differs from the oracle in %d of %d warp-sim cases", dv[v].name, dfail, dn); + printf("%s warp simulator: %d cases (both orders), %d differ\n", dv[v].name, dn, dfail); + /* mutants: 1 math (caught by the oracle), 2 join dropped and 3 target corrupted (rejected by the simulator) */ + for (int m = 1; m <= 3; m++) { + BlackwellIRProgram *mp2 = calloc(1, sizeof *mp2); OmegaBlackwellKernel mk2; memset(&mk2, 0, sizeof mk2); + int mrc = mp2 ? omega_gpu_elementwise_codegen_ir(dv[v].op, m, mp2, &mk2) : -1; + CHECK(mrc == OMEGA_GPU_EW_OK && memcmp(mk2.code_digest, dk.code_digest, 32) != 0, "%s mutant %d builds and differs (rc %d)", dv[v].name, m, mrc); + if (mrc == OMEGA_GPU_EW_OK) { + uint64_t L = 100000, *want = oracle_bitmap(L), *got = calloc(pr_words64(L) + 1, 8); + g_ws_order = 0; + int r = want && got ? sim_sieve(mp2, L, OMEGA_GPU_EW_MAX_CTAS, dv[v].staged, got) : -1; + g_ws_order = -1; + if (m == 1) CHECK(r == 0 && want && memcmp(got, want, pr_words64(L) * 8) != 0, "%s math mutant not caught at 1e5 (rc %d err %d)", dv[v].name, r, g_sim_err); + else CHECK(r != 0 && g_sim_err >= 200, "%s mutant %d (%s) not rejected by the warp simulator (rc %d err %d)", dv[v].name, m, m == 2 ? "BSYNC dropped" : "BSSY target corrupted", r, g_sim_err); + if (m > 1) printf("%s mutant %d: warp simulator says %s\n", dv[v].name, m, g_sim_err >= 200 ? ws_err_name(g_sim_err - 200) : "nothing"); + free(want); free(got); free(mk2.code); + } + free(mp2); + } + free(dk.code); free(dp); + } + } + + /* 7. division model */ magic_model(1000000); if (big) magic_model(10000000); diff --git a/docs/.crumb b/docs/.crumb index 8c941367a..fdf65c8f2 100644 --- a/docs/.crumb +++ b/docs/.crumb @@ -118,14 +118,14 @@ } ], "children_root": "sha256:ce53c9c7f75a7d122d6a926bb304b2df1bd0730693b54887b842ad3559bafe83", - "compiled_at": "2026-10-06T00:23:42Z", + "compiled_at": "2026-10-06T01:57:59Z", "compiler": "crumb-compile/1", - "digest": "sha256:01f9bcaf892355eff231425a96bf8f625fc9d9be4c1480991db92b6fefd6cd49", + "digest": "sha256:bb89b5d99f5816b19958cdca790ea2f2066d87c00a0d78f4db6955f1540ed821", "evidence_files": 34, "evidence_root": "sha256:589d8b3706e6d7a202ab20b278dac5fc5bafc973f1c2a867b2668aa7210b65cc", - "files": 113, - "generated_at_commit": "fa18e58a86a7402de785e07044f5f444a1dcca3c", - "source_tree": "sha256:0a688cde0c483735c8aee62f8ae2162bc7fbc30456961d7fc031b897c64e1587" + "files": 114, + "generated_at_commit": "1f4a3c7821d769a99ce1fcc896ac8ee30a33ea69", + "source_tree": "sha256:c1905c7445c800667d356e7bd34b303d6decbacc3630effc2ad13e48f5ae4fc7" }, "owner_lane": "", "provenance": { diff --git a/docs/gpu-reconvergence-308.md b/docs/gpu-reconvergence-308.md new file mode 100644 index 000000000..3081e06bd --- /dev/null +++ b/docs/gpu-reconvergence-308.md @@ -0,0 +1,154 @@ +# GPU structured warp reconvergence (omega #308) + +Branch feat/gpu-reconvergence-308, code at 0a1812f, receipts in evidence/gpu-reconv-308/. +Evidence classes used below: PROVEN BY TEST (a test in this repo passes, command and receipt +named), MEASURED (read off the real chip or the real nvcc), DERIVED FROM REFERENCE (taken from +Mesa NAK source, not checked by us on the chip), INFERRED (reasoned from documentation, not +checked), UNKNOWN. + +## 1. The failure mode + +On the GB10 (sm_121) a warp is 32 lanes. When lanes take different paths (a lane-dependent +branch or loop exit) and the code then runs a warp-wide step (SHFL, or a shared-memory exchange +guarded by BAR.SYNC), the lanes must first be brought back together. Without an explicit +reconvergence point the chip may run the later step with only some lanes active and reads the +inactive lanes' registers, which are undefined. + +Two places in AIEN hit this and were worked around by removing the divergence: +- The original divergent prime sieve (omega 888a011, lane-dependent mark loop) gave wrong + results; the production C5 sieve avoids it with a warp-uniform loop. +- Attention with a `j >= ctx` branch gave wrong results in the SHFL reduction after it + (2026-10-04); production attention is branch-free. + +nvcc brackets such code with BSSY (arm a barrier register, name the join point) and BSYNC (wait +for every armed lane, continue as one warp). Omega's Blackwell code generator had neither. #308 +adds them, plus a host simulator that rejects code which misuses them. +Chip observation (MEASURED, T1b): the diamond probe with its BSYNC dropped runs on the chip +without error and returns 466 of 1000 words wrong. + +## 2. Encodings (BSSY / BSYNC, sm_121) + +| Item | Value | Class | +|---|---|---| +| BSSY.RECONVERGENT | w0 = 0x945 \| pred<<12 \| bar<<16; w1 = (delta_insns - 1) * 16; w2 = 0x03800200; control 0x000fe200 (stall 1, yield) | MEASURED (nvcc 13.0.88 -arch=sm_121, evidence/gpu-reconv-308/oracle/) | +| BSYNC.RECONVERGENT | w0 = 0x941 \| pred<<12 \| bar<<16; w1 = 0; w2 = 0x03800200; control 0x000fea00 (stall 5, yield) | MEASURED (same oracle) | +| Opcodes 0x945 / 0x941, barrier id in bits 16..19 | Mesa NAK sm70_encode.rs OpBSSy / OpBSync | DERIVED FROM REFERENCE | +| RECONVERGENT bit (bit 73) | set by nvcc, omitted by NAK | MEASURED (nvcc) / DERIVED (NAK). We emit nvcc's form. | +| Branch target of BSSY | the instruction after the BSYNC; loop exits and breaks jump to the BSYNC itself (where nvcc sends them) | MEASURED (oracle k_break, k_loop, k_nested) | + +Golden fixtures for both words and refusals -101..-105 pass in `build/gpu_reconv_test` +(PROVEN BY TEST, 220 checks, T1a). nvdisasm decodes every emitted listing with zero +undecodable words (PROVEN BY TEST, same run). + +## 3. Region API and fail-closed rules + +Bookkeeping in src/omega_bw_reconv.h (`bw_regions_*`), wrapped by region_begin / region_exit / +region_join in src/omega_gpu_elementwise_api.c and src/omega_gpu_attention_api.c. + +- begin allocates barrier register Bn with n = nesting depth, then emits BSSY. 16 barrier + registers (B0..B15) are encodable; a 17th nested region is refused and the build fails. + Whether the chip really provides 16 is not documented to us; we ran 16 deep on the chip + (rc_depth16, MEASURED, T1a) and no deeper. +- exit records a forward branch that leaves the region; join patches it to the BSYNC and patches + the BSSY target to the instruction after the BSYNC. +- Refused at codegen: SHFL, BAR.SYNC or EXIT inside an open region; an unclosed region; a BSYNC + with no BSSY; on the launch path, SHFL or BAR.SYNC after a predicated EXIT (lanes already gone). + (PROVEN BY TEST: RC_REFUSE_* probes and RC_DEPTH16 in `build/gpu_reconv_test`.) +- This is why R6 (lanes exit early, then BAR.SYNC/SHFL on the rest) is refused at codegen rather + than proven correct on the chip. + +## 4. Host warp simulator (tests/bw_warp_sim.h) + +A warp is a set of fragments (pc, lane mask). A predicated branch whose lanes disagree splits the +fragment. Fragments run one at a time in a chosen order; tests run both lowest-pc-first and +highest-pc-first. Fragments merge only at a BSYNC. + +Errors (first wins, each names the pc): SHFL_SPLIT, SHFL_EXITED, BSYNC_UNARMED, BSSY_REARM, +JOIN_MISMATCH, NONMEMBER, STUCK, NEVER_JOINED, plus range and resource errors. + +BAR.SYNC reached by a split warp is allowed and counted (`bar_split_arrivals`). Basis: the CUDA +programming guide says the CTA barrier counts arriving threads since Volta. Class: INFERRED for +sm_121, not verified by us as a rule. Supporting observation (MEASURED earlier, prime-race +receipts): the production C5 staged sieve has 128 split arrivals at 1e6 in the simulator and is +chip-correct. Fragments stay split after the barrier (a barrier is not a reconvergence point), so a +later SHFL without a BSYNC still fails with SHFL_SPLIT. + +Limits: EX2 and RCP are exact in the simulator, approximate on the chip. LDCU64 is not modelled. +Warp issue counts are a model, not chip time (UNKNOWN how well they track the chip). + +## 5. Hazard / scheduling metadata + +From NAK sm120: BSSY, BSYNC and BClear are Decoupled (variable latency, tracked by the +scoreboard). DERIVED FROM REFERENCE. Omega does not enforce this: `schedule_fixed_pairs` ignores +control ops, and the control words above (stall 1 / stall 5, both yield) are copied from nvcc. +Chip correctness of the shapes we emit is MEASURED (T1, T2, T4); a different placement is not covered. + +## 6. Evidence table + +| # | Claim | Class | Command / receipt | +|---|---|---|---| +| 1 | BSSY/BSYNC words match nvcc; refusals -101..-105 | PROVEN BY TEST + MEASURED | `build/gpu_reconv_test` PASS 220; oracle/cf.{cu,sass} | +| 2 | Region API bounds and refusals (16 ok, 17 refused, SHFL/BAR/EXIT in region, unclosed) | PROVEN BY TEST | gpu_reconv_test PASS 220 | +| 3 | Simulator catches dropped BSYNC, corrupted target, exit past join, early exit then SHFL, split SHFL | PROVEN BY TEST | gpu_reconv_test; `gpu_attention_test --sim --divergent` PASS 124 | +| 4 | R3 diamond, R4 loop break, R5 nested, R6 exit+BAR+SHFL, 16-deep nest: 19 sizes each (incl. partial warps), bit-exact vs CPU oracle on the chip | PROVEN BY TEST (chip) | T1a/GPU-RECONV-0a1812f/receipt.json: OMEGA_GPU_RECONV_PASS, 220 checks, 95 chip cases, 0 failed. Repeat T1b PASS 220 | +| 5 | Chip catches math mutants; dropped BSYNC gives wrong words | MEASURED | T1 receipts: mutants diamond 755, loopbreak 377, nested 110 violations, exit_bar CODEGEN_FAIL; dropped BSYNC 466/1000 words wrong (T1b) | +| 6 | R2 attention j>=ctx branch inside a region, SHFL after the join, bit-correct incl. partial warps | PROVEN BY TEST (chip) | T2-attention: production run PASS 122, sweep PASS 73; `--divergent` run PASS 122, sweep PASS 73; timing PASS 2 | +| 7 | Existing gates unchanged | PROVEN BY TEST (chip) | T3: elementwise 31 checks PASS; cut4b (elementwise, attention, matmul 18 + timing 4, session probe 0/30 first-call failures) PASS; prime-race run_gb10_chip.sh VERDICT PASS (585 audit runs, 1172 passes, mutant caught) in T3b | +| 8 | R1 original divergent sieve (888a011 mark loop in a region) and nested variant bit-identical to oracle | PROVEN BY TEST (chip) | T4: PR_GB10_MARK=divergent and divergent2, 585 runs each (every DESIGN limit plus 1e7), 0 mismatches | +| 9 | BAR.SYNC on a split warp is legal on sm_121 | INFERRED | see section 4 | +| 10 | 16 barrier registers exist on the chip beyond depth 16 / refusal of 17 matches hardware | UNKNOWN | only depth 16 tested | +| 11 | R6 correctness after early exit | not proven: refused at codegen | section 3 | +| 12 | Simulator issue counts predict chip time | UNKNOWN | model only | + +## 7. Performance (R1, honest) + +Setup: `PR_GB10_CTA_BUDGET=64`, limit 1e6, 5 s per run, same session, three marks interleaved, +8 rounds (T4/perf*.json, perf64.log, perf64-repeat.log). Unit: passes per second. MEASURED. + +| mark | per-round passes/s | note | +|---|---|---| +| uniform (C5, production) | 7618 7530 5213 6354 6914 7485 7936 6960 | noisy, 5213..7936 | +| divergent (888a011 loop in a region) | 9155 9147 9134 9127 9158 9107 9113 9162 | 9107..9162 | +| divergent2 (plus prime loop, nested) | 9092 9247 9119 9263 9004 9081 9126 9183 | 9004..9263 | + +The brief's C5 baseline was 9139 passes/s. In this session the C5 uniform build measured lower +and much noisier than that figure, so the table does not support a claim that divergent beats C5. +What it does show: the regioned divergent kernels lose no throughput against the 9139 baseline and +sit flat near 9140. A flat figure that matches the old baseline to within 0.2 percent suggests a +fixed per-pass cost (launch, host base-prime sieve, marker wait) dominates at this size +(INFERRED, not investigated; out of scope). Why the uniform run is noisier is UNKNOWN. +Kernel size: 88 instructions for all three; registers 29 (uniform) vs 26 (divergent). +The warp-simulator model predicts divergent warp issues 2.53M (util .656) and divergent2 2.01M +(util .830) against C5 global 2.89M (util .774) at 1e6 (model only, not chip time). +Correctness was the objective; this section is reported for completeness. + +## 7a. Invariant for new GPU code (Drake, 2026-10-05) + +Any new lane-divergent code emitted for the Blackwell backend must either + +1. wrap the divergent stretch in `region_begin` -> `region_exit` -> `region_join` and pass the host warp simulator (`build/gpu_reconv_test` plus the suite that exercises the kernel), or +2. show in the kernel comment why every branch is provably warp-uniform (all 32 lanes take the same path), as the C5 sieve mark loop does. + +A SHFL or BAR.SYNC that depends on a lane-divergent branch without an enclosing region is a defect, not a style choice: on the chip the dropped-BSYNC mutant returned 466 of 1000 words wrong with no error (evidence/gpu-reconv-308/T1b-*). The emitter refuses the cases it can see (section 3); the simulator catches the rest. Reviewers should ask for one of the two proofs on every PR that adds a predicated branch. + +## 8. What was not changed + +Production C5 sieve, branch-free attention, test thresholds, omega main. Divergent kernels are +opt-in (`PR_GB10_MARK`, `--divergent`). + +## 9. Limitations + +- Only the shapes in the probes are chip-verified. BSSY/BSYNC placement other than "BSSY before + the divergence, every exit to the BSYNC, SHFL/BAR after the join" is not covered. +- R6 is refused, not proven. Barrier registers beyond 16 deep untested (and refused at 17). +- Control-op latency metadata is documented, not enforced. +- Chip runs were taken on one machine (GB10, driver and kernel in each receipt's device.txt). + +## 10. Reproduce + + make build/gpu_reconv_test PHYSICS_DIR=../physics-reconv-lock + sh tools/run_gpu_reconv_chip.sh [OUT] [-- --observe-dropped-join] + build/gpu_attention_test [--divergent] [--sweep] --out receipt.json + PR_GB10_MARK=divergent bench/prime_race/build/gb10_native --limit 1000000 --audit-passes 5 + +Chip runs need a quiet window and the GPU lock (/tmp/aien-gb10.lock). diff --git a/evidence/gpu-reconv-308/T1a-GPU-RECONV-0a1812f/SHA256SUMS b/evidence/gpu-reconv-308/T1a-GPU-RECONV-0a1812f/SHA256SUMS new file mode 100644 index 000000000..25b5cd116 --- /dev/null +++ b/evidence/gpu-reconv-308/T1a-GPU-RECONV-0a1812f/SHA256SUMS @@ -0,0 +1,10 @@ +2455c607827def7a1cc5d96ce3759e8280bb254d6ed7e9409072faf9cc949a56 build.log +8ca14df583da96c66c4d7dc1a301c39e0a19747c1b0842fdee62a5e7e2cf1260 bw_warp_sim.h +6ecff2b05540a8a79777b86298a87ea06240f57cd5a6e0132fb05ee78774915d device.txt +0cfa2a3922fa89022a2a0b77734a09914ccb4aef9221b14a182bb436cbd6e744 gpu_reconv_test.c +ce6aa4077426ff76120276424103843f5e6641d312255df17b0ef91057ffefce omega_blackwell_codegen.c +0da10397dd4ee813633afa425192d126d056684fdd998585fea666995a7c50ea omega_bw_reconv.h +bd2346fb86eed5e3be35c49c970c25adf1621043d6e4e4ba02e990375e5859c7 omega_gpu_elementwise_api.c +4d9eb1b5fd55e168f0cd4a96e906957376d88bb5efbe1f08633fcdfa52ce8fba omega_gpu_elementwise_api.h +51cfb8bf97e6abd41b51dbdac32cb57c97eb7cff586d26d1d143a7e64aac0fd0 receipt.json +e39d8ee59c455f39b85b89a4d2b14af4542c4f2b9a9b66ea06b0740d5f6e3d21 run.log diff --git a/evidence/gpu-reconv-308/T1a-GPU-RECONV-0a1812f/build.log b/evidence/gpu-reconv-308/T1a-GPU-RECONV-0a1812f/build.log new file mode 100644 index 000000000..8eab8a45c --- /dev/null +++ b/evidence/gpu-reconv-308/T1a-GPU-RECONV-0a1812f/build.log @@ -0,0 +1,2 @@ +ar rcs build/libomega_gpu.a build/sha256.o build/omega_canonical.o build/omega_validate.o build/omega_core.o build/omega_codec.o build/aarch64_encoder.o build/aarch64_decoder.o build/omega_realize.o build/omega_exec.o build/omega_self_host.o build/omega_verify.o build/omega_program.o build/omega_synthesis.o build/omega_library.o build/omega_discovery.o build/omega_machine.o build/omega_realize_synth.o build/omega_matvec.o build/omega_accelerator.o build/omega_accelerator_world.o build/omega_vector.o build/omega_blackwell_encoder.o build/omega_blackwell_qmd.o build/omega_blackwell_realize.o build/omega_blackwell_submit.o build/omega_blackwell_engine.o build/omega_gpu_engine.o build/omega_blackwell_matmul.o build/omega_blackwell_codegen.o build/omega_gpu_session.o build/omega_gpu_matmul_api.o build/omega_gpu_elementwise_api.o build/omega_gpu_attention_api.o build/omega_gpu_wait.o build/omega_evidence.o build/m16_native.o build/nvrm.o build/omega_program_ir.o build/omega_vc_bridge.o build/omega_resolve.o build/omega_receipt.o build/omega_blake3.o build/omega_vcstore.o +cc -std=gnu11 -Wall -Wextra -Werror -MMD -MP -D_GNU_SOURCE -O2 -Isrc -I../physics-reconv-lock/m16 -I../physics-reconv-lock/nvrm -I../physics-reconv-lock/third_party/nvidia-open-580.173.02/src/common/sdk/nvidia/inc -I../physics-reconv-lock/third_party/nvidia-open-580.173.02/kernel-open/common/inc -I../physics-reconv-lock/third_party/nvidia-open-580.173.02/kernel-open/nvidia-uvm -I../physics-reconv-lock/third_party/nvidia-open-580.173.02/src/nvidia/arch/nvalloc/unix/include -Itests -o build/gpu_reconv_test tests/gpu_reconv_test.c build/libomega_gpu.a -lpthread -lm diff --git a/evidence/gpu-reconv-308/T1a-GPU-RECONV-0a1812f/bw_warp_sim.h b/evidence/gpu-reconv-308/T1a-GPU-RECONV-0a1812f/bw_warp_sim.h new file mode 100644 index 000000000..328094cb0 --- /dev/null +++ b/evidence/gpu-reconv-308/T1a-GPU-RECONV-0a1812f/bw_warp_sim.h @@ -0,0 +1,313 @@ +/* bw_warp_sim.h: host SIMT simulator for the Blackwell IR (omega #308). + * + * Models what the earlier per-thread simulators could not: a warp of 32 lanes with an + * active mask, divergence at predicated branches, structured reconvergence through the + * barrier registers (BSSY arms Bn for the lanes executing it and names the join; BSYNC + * parks arriving lanes until every armed lane that has not exited is there, then the + * warp continues as one), nested regions, and lane participation in SHFL and BAR.SYNC. + * + * A warp is a set of fragments (pc, lane mask). A predicated branch whose lanes disagree + * splits the fragment; the scheduler then runs one fragment at a time in a chosen order + * (lowest pc first or highest pc first; tests run both), which is the freedom the chip's + * scheduler also has. Fragments only merge at a BSYNC. Errors (all fatal, first one wins): + * WS_E_SHFL_SPLIT a SHFL reached by a fragment that is not the whole live warp + * (the chip reads inactive lanes: undefined; attention 2026-10-04) + * WS_E_SHFL_EXITED a SHFL on a warp some of whose lanes already exited (undefined too) + * WS_E_BAR_SPLIT (defined, never raised) a BAR.SYNC reached by a split warp is allowed: + * since Volta the CTA barrier counts arriving threads, not warps (CUDA + * programming guide, independent thread scheduling; INFERRED for sm_121, + * not chip-verified by us). The simulator parks each fragment, releases + * them together, counts the event in bar_split_arrivals, and leaves the + * fragments split: a barrier is not a reconvergence point, so a SHFL after + * it still fails with SHFL_SPLIT unless a BSYNC rejoined the warp. + * WS_E_BSYNC_UNARMED BSYNC on a barrier no BSSY armed + * WS_E_BSSY_REARM BSSY on a barrier still armed (lanes may still be heading to its join) + * WS_E_JOIN_MISMATCH BSYNC at an address other than the one the BSSY named minus one + * WS_E_NONMEMBER a lane reached BSYNC Bn without having executed its BSSY + * WS_E_STUCK lanes parked at a BSYNC that can never complete (a join was dropped, + * an exit jumped past it, or a target was corrupted) + * WS_E_NEVER_JOINED the warp finished with a barrier still armed + * WS_E_SELF_BRANCH, WS_E_PC, WS_E_OP, WS_E_SHIFT, WS_E_MEM, WS_E_SMEM, WS_E_FRAGS + * Every error names the pc. Memory: global addresses are host pointers (optionally range + * checked); shared memory is a per-CTA buffer poisoned at CTA start so a read of a word + * nobody wrote shows up in the oracle comparison. + * + * Per-lane ALU semantics are those of the two earlier simulators (tests/gpu_attention_test.c, + * bench/prime_race/tests/gb10_sieve_host_test.c). EX2 and RCP are exact here; the chip's + * are approximate. LDCU64 (memory descriptor) is not modelled. */ +#ifndef BW_WARP_SIM_H +#define BW_WARP_SIM_H + +#include "omega_blackwell_codegen.h" +#include +#include +#include +#include +#include + +#define WS_WARP 32 +#define WS_MAX_THREADS 256 +#define WS_MAX_WARPS (WS_MAX_THREADS / WS_WARP) +#define WS_MAX_FRAGS 64 +#define WS_NBAR BW_RECONV_MAX_BAR +#define WS_MAX_RANGES 8 + +enum { + WS_OK = 0, WS_E_SHFL_SPLIT, WS_E_SHFL_EXITED, WS_E_BAR_SPLIT, WS_E_BSYNC_UNARMED, WS_E_BSSY_REARM, + WS_E_JOIN_MISMATCH, WS_E_NONMEMBER, WS_E_STUCK, WS_E_NEVER_JOINED, WS_E_SELF_BRANCH, WS_E_PC, + WS_E_OP, WS_E_SHIFT, WS_E_MEM, WS_E_SMEM, WS_E_FRAGS, WS_E_BAR_TIMEOUT, WS_E_STEPS +}; +static const char *ws_err_name(int e) { + static const char *const n[] = { "OK", "SHFL_SPLIT", "SHFL_EXITED", "BAR_SPLIT", "BSYNC_UNARMED", "BSSY_REARM", + "JOIN_MISMATCH", "NONMEMBER", "STUCK", "NEVER_JOINED", "SELF_BRANCH", "PC", "OP", "SHIFT", "MEM", "SMEM", + "FRAGS", "BAR_TIMEOUT", "STEPS" }; + return (e >= 0 && e < (int)(sizeof n / sizeof n[0])) ? n[e] : "?"; +} + +typedef struct { uint32_t r[256]; uint8_t p0; } WsLane; +typedef struct { int pc; uint32_t mask; int parked; /* waiting at a BAR.SYNC */ } WsFrag; +typedef struct { int valid; int target; uint32_t members, arrived; } WsBar; +typedef struct { + WsLane lane[WS_WARP]; + WsFrag frag[WS_MAX_FRAGS]; + int nfrag; + WsBar bar[WS_NBAR]; + uint32_t exited; +} WsWarp; +typedef struct { uintptr_t lo, hi; } WsRange; + +typedef struct { + const BlackwellIRProgram *p; + uint32_t cbank[256]; /* constant bank 0 words (byte offset / 4) */ + uint32_t threads; /* per CTA, a multiple of 32, <= WS_MAX_THREADS */ + uint8_t *smem; /* per-CTA shared memory buffer (poisoned at CTA start) */ + uint32_t smem_bytes; + WsRange ranges[WS_MAX_RANGES]; int nranges; /* when > 0, every global access must fall in one */ + int order; /* 0 = lowest pc first, 1 = highest pc first */ + int trace; /* print every issue of warp 0 of CTA (0,0) */ + /* results */ + int err; int err_pc; char msg[200]; + uint64_t warp_issues; /* fragment-instruction issues (the chip's cost unit for divergence) */ + uint64_t lane_issues; /* lanes active over those issues */ + uint64_t splits; /* branches that split a fragment */ + uint64_t bar_split_arrivals; /* BAR.SYNC reached by a fragment that is not the whole live warp */ + int max_frags, peak_bars; +} WsSim; + +static void ws_fail(WsSim *s, int err, int pc, const char *what) { + if (s->err) return; + s->err = err; s->err_pc = pc; + snprintf(s->msg, sizeof s->msg, "%s at pc %d: %s", ws_err_name(err), pc, what ? what : ""); +} + +static uint32_t ws_rd(const WsLane *l, int v, const BlackwellIRProgram *p) { if (v < 0) return 0; int ph = p->regalloc.vreg_to_phys[v]; return ph < 0 ? 0 : l->r[ph & 0xff]; } +static uint64_t ws_rd64(const WsLane *l, int v, const BlackwellIRProgram *p) { if (v < 0) return 0; int ph = p->regalloc.vreg_to_phys[v] & 0xff; return (uint64_t)l->r[ph] | ((uint64_t)l->r[(ph + 1) & 0xff] << 32); } +static void ws_wr(WsLane *l, int v, uint32_t x, const BlackwellIRProgram *p) { if (v < 0) return; int ph = p->regalloc.vreg_to_phys[v]; if (ph >= 0) l->r[ph & 0xff] = x; } +static void ws_wr64(WsLane *l, int v, uint64_t x, const BlackwellIRProgram *p) { if (v < 0) return; int ph = p->regalloc.vreg_to_phys[v] & 0xff; l->r[ph] = (uint32_t)x; l->r[(ph + 1) & 0xff] = (uint32_t)(x >> 32); } +static float ws_u2f(uint32_t u) { float f; memcpy(&f, &u, 4); return f; } +static uint32_t ws_f2u(float f) { uint32_t u; memcpy(&u, &f, 4); return u; } +static uint32_t ws_lut3(uint32_t lut, uint32_t a, uint32_t b, uint32_t c) { + uint32_t r = 0; + for (int i = 0; i < 8; i++) if (lut >> i & 1) r |= ((i & 4) ? a : ~a) & ((i & 2) ? b : ~b) & ((i & 1) ? c : ~c); + return r; +} +static int ws_mem_ok(WsSim *s, uint64_t a, unsigned bytes, int pc) { + if (s->nranges == 0) return 1; + for (int i = 0; i < s->nranges; i++) if (a >= s->ranges[i].lo && a + bytes <= s->ranges[i].hi) return 1; + ws_fail(s, WS_E_MEM, pc, "global access outside the declared buffers"); return 0; +} +static uint32_t *ws_smem(WsSim *s, uint32_t a, int pc) { + if ((a & 3) || a + 4 > s->smem_bytes) { ws_fail(s, WS_E_SMEM, pc, "shared access past the declared size"); return NULL; } + return (uint32_t *)(s->smem + a); +} +static int ws_popc(uint32_t m) { return __builtin_popcount(m); } + +/* One lane, one non-control instruction. */ +static void ws_lane_exec(WsSim *s, WsLane *l, const BlackwellIRInsn *in, int pc, uint32_t tid, uint32_t cx, uint32_t cy) { + const BlackwellIRProgram *p = s->p; + uint32_t a = ws_rd(l, in->src1_vreg, p), b = ws_rd(l, in->src2_vreg, p), c = ws_rd(l, in->src3_vreg, p); + switch (in->op) { + case BW_IR_NOP: break; + case BW_IR_S2R: ws_wr(l, in->dst_vreg, in->imm == BW_SR_TID_X ? tid : in->imm == BW_SR_CTAID_X ? cx : in->imm == BW_SR_CTAID_Y ? cy : 0, p); break; + case BW_IR_LDC: ws_wr(l, in->dst_vreg, s->cbank[(in->imm / 4) & 0xff], p); break; + case BW_IR_LDC64: ws_wr64(l, in->dst_vreg, (uint64_t)s->cbank[(in->imm / 4) & 0xff] | ((uint64_t)s->cbank[(in->imm / 4 + 1) & 0xff] << 32), p); break; + case BW_IR_LDCU64: case BW_IR_LDCU: break; /* memory descriptor: not modelled */ + case BW_IR_MOV_IMM: ws_wr(l, in->dst_vreg, in->imm, p); break; + case BW_IR_MOV_RZ: ws_wr(l, in->dst_vreg, 0, p); break; + case BW_IR_IMAD: ws_wr(l, in->dst_vreg, in->src2_vreg >= 0 ? a * b + c : a * in->imm + c, p); break; + case BW_IR_IMAD_WIDE: ws_wr64(l, in->dst_vreg, (uint64_t)a * in->imm + ws_rd64(l, in->src3_vreg, p), p); break; + case BW_IR_IMAD_HI_U32: ws_wr(l, in->dst_vreg, (uint32_t)(((uint64_t)a * b) >> 32) + c, p); break; + case BW_IR_IADD3: ws_wr(l, in->dst_vreg, a + b, p); break; /* the encoder's IADD3 has Rc = RZ */ + case BW_IR_SHF_R: ws_wr(l, in->dst_vreg, a >> (in->imm & 31), p); break; + case BW_IR_SHF_L_U32: if (b >= 32) { ws_fail(s, WS_E_SHIFT, pc, "SHF.L.U32 amount >= 32 (not chip-verified)"); return; } ws_wr(l, in->dst_vreg, a << b, p); break; + case BW_IR_LOP3_AND: ws_wr(l, in->dst_vreg, a & in->imm, p); break; + case BW_IR_LOP3_XOR: ws_wr(l, in->dst_vreg, a ^ b, p); break; + case BW_IR_LOP3_LUT: ws_wr(l, in->dst_vreg, ws_lut3(in->imm & 0xff, a, b, c), p); break; + case BW_IR_ISETP_GE_U32: l->p0 = a >= b; break; + case BW_IR_LDG_E: { uint64_t ad = ws_rd64(l, in->src1_vreg, p); if (!ws_mem_ok(s, ad, 4, pc)) return; uint32_t x; memcpy(&x, (const void *)(uintptr_t)ad, 4); ws_wr(l, in->dst_vreg, x, p); break; } + case BW_IR_LDG_E_U16: { uint64_t ad = ws_rd64(l, in->src1_vreg, p); if (!ws_mem_ok(s, ad, 2, pc)) return; uint16_t x; memcpy(&x, (const void *)(uintptr_t)ad, 2); ws_wr(l, in->dst_vreg, x, p); break; } + case BW_IR_STG_E: { uint64_t ad = ws_rd64(l, in->src1_vreg, p); if (!ws_mem_ok(s, ad, 4, pc)) return; memcpy((void *)(uintptr_t)ad, &b, 4); break; } + case BW_IR_LDS32: { uint32_t *m = ws_smem(s, a, pc); if (!m) return; ws_wr(l, in->dst_vreg, *m, p); break; } + case BW_IR_STS32: { uint32_t *m = ws_smem(s, a, pc); if (!m) return; *m = b; break; } + case BW_IR_FADD: ws_wr(l, in->dst_vreg, ws_f2u(ws_u2f(a) + ws_u2f(b)), p); break; + case BW_IR_FSUB: ws_wr(l, in->dst_vreg, ws_f2u(ws_u2f(a) - ws_u2f(b)), p); break; + case BW_IR_FMUL: ws_wr(l, in->dst_vreg, ws_f2u(ws_u2f(a) * ws_u2f(b)), p); break; + case BW_IR_FFMA: ws_wr(l, in->dst_vreg, ws_f2u(fmaf(ws_u2f(a), ws_u2f(b), ws_u2f(c))), p); break; + case BW_IR_FMNMX_MAX: ws_wr(l, in->dst_vreg, ws_f2u(fmaxf(ws_u2f(a), ws_u2f(b))), p); break; + case BW_IR_FMNMX_MIN: ws_wr(l, in->dst_vreg, ws_f2u(fminf(ws_u2f(a), ws_u2f(b))), p); break; + case BW_IR_MUFU_EX2: ws_wr(l, in->dst_vreg, ws_f2u(exp2f(ws_u2f(a))), p); break; + case BW_IR_MUFU_RCP: ws_wr(l, in->dst_vreg, ws_f2u(1.0f / ws_u2f(a)), p); break; + case BW_IR_MUFU_RSQ: ws_wr(l, in->dst_vreg, ws_f2u(1.0f / sqrtf(ws_u2f(a))), p); break; + default: ws_fail(s, WS_E_OP, pc, "opcode not modelled"); return; + } +} + +static int ws_pick(const WsSim *s, const WsWarp *w) { + int best = -1; + for (int i = 0; i < w->nfrag; i++) { + if (w->frag[i].mask == 0 || w->frag[i].parked) continue; + if (best < 0) { best = i; continue; } + if (s->order == 0 ? w->frag[i].pc < w->frag[best].pc : w->frag[i].pc > w->frag[best].pc) best = i; + } + return best; +} +static void ws_drop_frag(WsWarp *w, int i) { w->frag[i] = w->frag[--w->nfrag]; } +static int ws_add_frag(WsSim *s, WsWarp *w, int pc, uint32_t mask) { + if (mask == 0) return 0; + if (w->nfrag >= WS_MAX_FRAGS) { ws_fail(s, WS_E_FRAGS, pc, "too many fragments"); return -1; } + w->frag[w->nfrag].pc = pc; w->frag[w->nfrag].mask = mask; w->frag[w->nfrag].parked = 0; w->nfrag++; + if (w->nfrag > s->max_frags) s->max_frags = w->nfrag; + return 0; +} + +/* Runs one warp until it has no runnable fragment (finished, parked at BAR.SYNC, or stuck). + * Returns 1 when parked at BAR.SYNC, 0 when finished, -1 on error. */ +static int ws_run_warp(WsSim *s, WsWarp *w, uint32_t warp, uint32_t cx, uint32_t cy, int trace) { + const BlackwellIRProgram *p = s->p; + const uint32_t live_all = 0xffffffffu; + for (uint64_t guard = 0;; guard++) { + if (s->err) return -1; + if (guard > 2000000000ull) { ws_fail(s, WS_E_STEPS, -1, "step budget"); return -1; } + int fi = ws_pick(s, w); + if (fi < 0) { + /* nothing runnable: parked at the CTA barrier, finished, or lanes waiting at a BSYNC + * that can never complete */ + for (int i = 0; i < w->nfrag; i++) if (w->frag[i].parked) return 1; + for (int b = 0; b < WS_NBAR; b++) if (w->bar[b].valid) { + if (w->bar[b].arrived) { ws_fail(s, WS_E_STUCK, w->bar[b].target - 1, "lanes parked at BSYNC, the rest never arrive"); return -1; } + ws_fail(s, WS_E_NEVER_JOINED, w->bar[b].target - 1, "warp finished with a barrier still armed"); return -1; + } + return 0; + } + WsFrag *f = &w->frag[fi]; + int pc = f->pc; + if (pc < 0 || (size_t)pc >= p->count) { ws_fail(s, WS_E_PC, pc, "pc outside the program"); return -1; } + const BlackwellIRInsn *in = &p->insns[pc]; + uint32_t mask = f->mask; + s->warp_issues++; s->lane_issues += (uint64_t)ws_popc(mask); + if (trace) printf("W%u pc=%d op=%d mask=%08x frags=%d\n", warp, pc, (int)in->op, mask, w->nfrag); + uint32_t live = live_all & ~w->exited; + uint32_t pmask = 0; + for (int L = 0; L < WS_WARP; L++) if ((mask >> L & 1) && w->lane[L].p0) pmask |= 1u << L; + uint32_t cond = in->predicate_p0 ? (in->predicate_not ? (mask & ~pmask) : (mask & pmask)) : mask; + switch (in->op) { + case BW_IR_EXIT: { + w->exited |= cond; + f->mask = mask & ~cond; + if (f->mask == 0) ws_drop_frag(w, fi); else f->pc = pc + 1; + break; + } + case BW_IR_BRA: { + int32_t delta = (int32_t)in->imm; + uint32_t taken = cond, fall = mask & ~cond; + if (taken && delta == 0) { ws_fail(s, WS_E_SELF_BRANCH, pc, "branch to itself"); return -1; } + if (taken && fall) s->splits++; + if (taken == 0) { f->pc = pc + 1; break; } + if (fall == 0) { f->pc = pc + delta; break; } + f->mask = fall; f->pc = pc + 1; + if (ws_add_frag(s, w, pc + delta, taken) < 0) return -1; + break; + } + case BW_IR_BSSY: { + WsBar *b = &w->bar[in->bar_reg & (WS_NBAR - 1)]; + if (in->bar_reg >= WS_NBAR) { ws_fail(s, WS_E_OP, pc, "barrier id"); return -1; } + if (b->valid) { ws_fail(s, WS_E_BSSY_REARM, pc, "BSSY on an armed barrier"); return -1; } + b->valid = 1; b->target = pc + (int32_t)in->imm; b->members = cond; b->arrived = 0; + int armed = 0; for (int k = 0; k < WS_NBAR; k++) armed += w->bar[k].valid; + if (armed > s->peak_bars) s->peak_bars = armed; + f->pc = pc + 1; + break; + } + case BW_IR_BSYNC: { + if (in->bar_reg >= WS_NBAR) { ws_fail(s, WS_E_OP, pc, "barrier id"); return -1; } + WsBar *b = &w->bar[in->bar_reg]; + if (!b->valid) { ws_fail(s, WS_E_BSYNC_UNARMED, pc, "BSYNC without a BSSY"); return -1; } + if (cond & ~b->members) { ws_fail(s, WS_E_NONMEMBER, pc, "a lane at BSYNC did not execute the BSSY"); return -1; } + b->arrived |= cond; + uint32_t rest = mask & ~cond; /* a predicated-off lane walks on */ + if (rest) { f->mask = rest; f->pc = pc + 1; } else ws_drop_frag(w, fi); + if (b->arrived == (b->members & ~w->exited)) { + if (b->target != pc + 1) { ws_fail(s, WS_E_JOIN_MISMATCH, pc, "BSSY named a different join point"); return -1; } + uint32_t joined = b->arrived; b->valid = 0; b->arrived = 0; b->members = 0; + if (ws_add_frag(s, w, pc + 1, joined) < 0) return -1; + } + break; + } + case BW_IR_SHFL_DOWN: { + if (w->exited) { ws_fail(s, WS_E_SHFL_EXITED, pc, "SHFL on a warp with exited lanes"); return -1; } + if (mask != live) { ws_fail(s, WS_E_SHFL_SPLIT, pc, "SHFL on a split warp"); return -1; } + uint32_t src[WS_WARP]; + for (int L = 0; L < WS_WARP; L++) src[L] = ws_rd(&w->lane[L], in->src1_vreg, p); + for (int L = 0; L < WS_WARP; L++) { int from = L + (int)(in->imm & 31); ws_wr(&w->lane[L], in->dst_vreg, from < WS_WARP ? src[from] : src[L], p); } + f->pc = pc + 1; + break; + } + case BW_IR_BAR_SYNC: { + if (mask != live) s->bar_split_arrivals++; + f->parked = 1; /* released by ws_run when every live lane of the CTA has arrived */ + break; + } + default: { + for (int L = 0; L < WS_WARP; L++) if (mask >> L & 1) { + ws_lane_exec(s, &w->lane[L], in, pc, warp * WS_WARP + (uint32_t)L, cx, cy); + if (s->err) return -1; + } + f->pc = pc + 1; + break; + } + } + } +} + +/* Runs every CTA of the grid. Returns s->err (0 = clean). */ +static int ws_run(WsSim *s, uint32_t grid_x, uint32_t grid_y) { + s->err = 0; s->msg[0] = 0; s->warp_issues = s->lane_issues = s->splits = s->bar_split_arrivals = 0; s->max_frags = 0; s->peak_bars = 0; + if (!s->p || s->threads == 0 || s->threads % WS_WARP || s->threads > WS_MAX_THREADS) { ws_fail(s, WS_E_OP, -1, "bad simulator configuration"); return s->err; } + static WsWarp warps[WS_MAX_WARPS]; + uint32_t nw = s->threads / WS_WARP; + for (uint32_t cy = 0; cy < grid_y && !s->err; cy++) for (uint32_t cx = 0; cx < grid_x && !s->err; cx++) { + memset(warps, 0, sizeof(WsWarp) * nw); + if (s->smem && s->smem_bytes) for (uint32_t i = 0; i + 4 <= s->smem_bytes; i += 4) { uint32_t poison = 0xfeedfaceu; memcpy(s->smem + i, &poison, 4); } + for (uint32_t w = 0; w < nw; w++) { warps[w].nfrag = 1; warps[w].frag[0].pc = 0; warps[w].frag[0].mask = 0xffffffffu; warps[w].frag[0].parked = 0; } + int trace = s->trace && cx == 0 && cy == 0; + /* Barrier phases: every warp runs until all of its fragments are parked at BAR.SYNC or it + * has finished (exited lanes do not hold the barrier up: INFERRED, see WS_E_BAR_SPLIT). + * Then every parked fragment is released and the next phase starts. */ + for (int phase = 0; ; phase++) { + int parked = 0; + for (uint32_t w = 0; w < nw && !s->err; w++) { + if (warps[w].nfrag == 0) continue; + int r = ws_run_warp(s, &warps[w], w, cx, cy, trace && w == 0); + if (r < 0) break; + if (r == 1) parked++; + } + if (s->err || parked == 0) break; /* error, or every warp finished */ + for (uint32_t w = 0; w < nw; w++) for (int i = 0; i < warps[w].nfrag; i++) + if (warps[w].frag[i].parked) { warps[w].frag[i].parked = 0; warps[w].frag[i].pc++; } + if (phase > 100000) { ws_fail(s, WS_E_BAR_TIMEOUT, -1, "barrier phases"); break; } + } + } + return s->err; +} + +#endif /* BW_WARP_SIM_H */ diff --git a/evidence/gpu-reconv-308/T1a-GPU-RECONV-0a1812f/device.txt b/evidence/gpu-reconv-308/T1a-GPU-RECONV-0a1812f/device.txt new file mode 100644 index 000000000..fe6e247aa --- /dev/null +++ b/evidence/gpu-reconv-308/T1a-GPU-RECONV-0a1812f/device.txt @@ -0,0 +1,5 @@ +Linux spark-b87b 7.0.0-1019-nvidia #19~24.04.2-Ubuntu SMP PREEMPT_DYNAMIC Sun Sep 6 18:35:11 UTC 2 aarch64 aarch64 aarch64 GNU/Linux +NVIDIA GB10, 580.173.02 +omega 0a1812f44eaa3dcf8fe5a914d1dd65cfaccbbcd7 (1 uncommitted paths) +physics 6d7cf0d4d8eb2cda7b512100ff6058e25dbb3ddf +2026-10-05T23:14:40Z diff --git a/evidence/gpu-reconv-308/T1a-GPU-RECONV-0a1812f/gpu_reconv_test.c b/evidence/gpu-reconv-308/T1a-GPU-RECONV-0a1812f/gpu_reconv_test.c new file mode 100644 index 000000000..227e3b658 --- /dev/null +++ b/evidence/gpu-reconv-308/T1a-GPU-RECONV-0a1812f/gpu_reconv_test.c @@ -0,0 +1,470 @@ +/* gpu_reconv_test: structured warp reconvergence (omega #308) on Omega's own Blackwell codegen. + * + * ./gpu_reconv_test --host-only encoder golden words, region bookkeeping, codegen refusals, + * nvdisasm listing (when /usr/local/cuda/bin/nvdisasm exists), + * every probe kernel through the SIMT warp simulator against + * the host oracle under both fragment orders, the mutation + * controls, scoreboard and control-edge walks. No device. + * ./gpu_reconv_test --out receipt.json chip gate: host part first (a kernel the simulator rejects + * never gets chip time), then every probe on the chip against + * the oracle, bit-exact, then each math mutant must be caught. + * --observe-dropped-join chip only, optional: also launch the dropped-BSYNC mutant of + * one probe and record what the chip did (an observation, not + * a gate; the simulator rejects that kernel first). + * + * Probes (src/omega_gpu_elementwise_api.c, gen_rc_*): R3 diamond, R4 loop break, R5 nested, + * R6 lanes past the end with STS/BAR/LDS + SHFL, and the 16-deep barrier-register bound. + * Oracle: rc_oracle below; every comparison is exact (u32). Launcher chunking (64 CTAs x 128 + * threads per launch, P0 = words in the chunk) is part of the oracle, so a clamp to the wrong + * chunk end shows up. */ +#include "omega_gpu_elementwise_api.h" +#include "omega_blackwell_codegen.h" +#include "omega_bw_reconv.h" +#include "omega_gpu_session.h" +#include "bw_warp_sim.h" +#include +#include +#include +#include +#include + +static int g_checks, g_failed; +#define CHECK(cond, ...) do { g_checks++; if (!(cond)) { g_failed++; printf("FAIL: "); printf(__VA_ARGS__); printf("\n"); } } while (0) + +static uint32_t lcg(uint32_t *s) { *s = *s * 1664525u + 1013904223u; return *s; } + +typedef struct { OmegaGpuEwOp op; const char *name; const char *reg; } Probe; +static const Probe PROBES[] = { + { OMEGA_GPU_EW_RC_DIAMOND, "rc_diamond", "R3" }, + { OMEGA_GPU_EW_RC_LOOPBREAK, "rc_loopbreak", "R4" }, + { OMEGA_GPU_EW_RC_NESTED, "rc_nested", "R5" }, + { OMEGA_GPU_EW_RC_EXIT_BAR, "rc_exit_bar", "R6" }, + { OMEGA_GPU_EW_RC_DEPTH16, "rc_depth16", "B0..B15" }, +}; +#define NPROBES (sizeof PROBES / sizeof PROBES[0]) +static const uint32_t NS[] = { 1, 5, 31, 32, 33, 63, 64, 65, 96, 127, 128, 129, 200, 1000, 4096, 8191, 8192, 8193, 20000 }; +#define NNS (sizeof NS / sizeof NS[0]) +#define CHUNK (OMEGA_GPU_EW_MAX_CTAS * OMEGA_GPU_EW_THREADS) + +/* ----------------------------------------------------------------- oracle */ +static uint32_t count_loop(uint32_t x, uint32_t y, int math, uint32_t *v_out) { + uint32_t v = x & 255u, st = (y & 15u) + 1u, cnt = 0, bound = math ? 255u : 256u; + while (v < bound) { v += st; cnt++; } + *v_out = v; return cnt; +} +static uint32_t rc_r(OmegaGpuEwOp op, int math, uint32_t x, uint32_t y, uint32_t partner_x) { + uint32_t v, cnt; + switch (op) { + case OMEGA_GPU_EW_RC_DIAMOND: + if (x >= y) { uint32_t t = 3u * x + y, u = t ^ x, r = 5u * u + 1u; r += r >> 3; return math ? r + 1u : r; } + return x + 1u; + case OMEGA_GPU_EW_RC_LOOPBREAK: cnt = count_loop(x, y, math, &v); return cnt * 256u + v; + case OMEGA_GPU_EW_RC_NESTED: if (x & 1u) return 7u; cnt = count_loop(x, y, math, &v); return cnt * 256u + v + 100u; + case OMEGA_GPU_EW_RC_EXIT_BAR: return 3u * x + (partner_x | 1u); + case OMEGA_GPU_EW_RC_DEPTH16: return x + (x < y ? (uint32_t)BW_RECONV_MAX_BAR : 0u); + default: return 0xdeadbeefu; + } +} +static void rc_oracle(OmegaGpuEwOp op, int math, uint32_t n, const uint32_t *a, const uint32_t *b, uint32_t *out) { + for (uint32_t i0 = 0; i0 < n; i0 += CHUNK) { + uint32_t ni = n - i0 < CHUNK ? n - i0 : CHUNK; + uint32_t threads = ((ni + OMEGA_GPU_EW_THREADS - 1) / OMEGA_GPU_EW_THREADS) * OMEGA_GPU_EW_THREADS; + uint32_t *r = malloc((size_t)threads * 4); + for (uint32_t t = 0; t < threads; t++) { + uint32_t idx = t < ni ? t : ni - 1, pt = t ^ 1u, pidx = pt < ni ? pt : ni - 1; + r[t] = rc_r(op, math, a[i0 + idx], b[i0 + idx], a[i0 + pidx]); + } + 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]; } + free(r); + } +} +/* An LCG's low bits cycle quickly (bit 0 has period 2), and the probes key on low bits + * (x & 255, y & 15, x & 1), so each draw goes through a mixing step. */ +static uint32_t mix(uint32_t r) { r ^= r >> 16; r *= 0x45d9f3bu; r ^= r >> 16; r *= 0x45d9f3bu; r ^= r >> 16; return r; } +static void fill(uint32_t *a, uint32_t *b, uint32_t n, uint32_t seed) { + for (uint32_t i = 0; i < n; i++) { a[i] = mix(lcg(&seed)); b[i] = mix(lcg(&seed)); } +} +static size_t diff_count(const uint32_t *got, const uint32_t *want, uint32_t n, uint32_t *first) { + size_t bad = 0; *first = n; + for (uint32_t i = 0; i < n; i++) if (got[i] != want[i]) { if (!bad) *first = i; bad++; } + return bad; +} + +/* --------------------------------------------------------- simulator driver */ +static uint8_t g_smem[1024]; +/* Mirrors run_1d: 64 CTAs x 128 threads per launch, P0 = words in the chunk. Returns the + * simulator's error code (0 clean); the output words are written to out. */ +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) { + for (uint32_t i = 0; i < n; i++) out[i] = 0xffbadbadu; + uint64_t wi = 0, li = 0, sp = 0; int mf = 0, pb = 0; + for (uint32_t i0 = 0; i0 < n; i0 += CHUNK) { + uint32_t ni = n - i0 < CHUNK ? n - i0 : CHUNK; + WsSim s; memset(&s, 0, sizeof s); + s.p = p; s.threads = OMEGA_GPU_EW_THREADS; s.smem = g_smem; s.smem_bytes = sizeof g_smem; s.order = order; + uint64_t aa = (uintptr_t)(a + i0), ba = (uintptr_t)(b + i0), ca = (uintptr_t)(out + i0); + s.cbank[0x380 / 4] = (uint32_t)aa; s.cbank[0x380 / 4 + 1] = (uint32_t)(aa >> 32); + s.cbank[0x388 / 4] = (uint32_t)ba; s.cbank[0x388 / 4 + 1] = (uint32_t)(ba >> 32); + s.cbank[0x390 / 4] = (uint32_t)ca; s.cbank[0x390 / 4 + 1] = (uint32_t)(ca >> 32); + s.cbank[0x398 / 4] = ni; + s.ranges[0] = (WsRange){ (uintptr_t)aa, (uintptr_t)aa + (size_t)ni * 4 }; + s.ranges[1] = (WsRange){ (uintptr_t)ba, (uintptr_t)ba + (size_t)ni * 4 }; + s.ranges[2] = (WsRange){ (uintptr_t)ca, (uintptr_t)ca + (size_t)ni * 4 }; + s.nranges = 3; + int err = ws_run(&s, (ni + OMEGA_GPU_EW_THREADS - 1) / OMEGA_GPU_EW_THREADS, 1); + wi += s.warp_issues; li += s.lane_issues; sp += s.splits; + if (s.max_frags > mf) mf = s.max_frags; + if (s.peak_bars > pb) pb = s.peak_bars; + if (err) { if (stats) *stats = s; return err; } + } + 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; } + return 0; +} +static int codegen(OmegaGpuEwOp op, int mutant, BlackwellIRProgram *p, OmegaBlackwellKernel *k) { + memset(k, 0, sizeof *k); + return omega_gpu_elementwise_codegen_ir(op, mutant, p, k); +} +static int is_reconv_err(int e) { + 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 || + e == WS_E_BSYNC_UNARMED || e == WS_E_BSSY_REARM || e == WS_E_NONMEMBER || e == WS_E_SHFL_EXITED; +} + +/* --------------------------------------------------------- static walks */ +/* Scoreboard walk (as bench/prime_race/tests/gb10_sieve_host_test.c sb_hazards): a destination + * written under write scoreboard 0 is in flight until a wait mask that includes it. */ +static int sb_hazards(const BlackwellIRProgram *p) { + int inflight[64], n = 0, bad = 0; + for (size_t i = 0; i < p->count; i++) { + const BlackwellIRInsn *in = &p->insns[i]; + uint32_t c = in->control; + if ((c >> 20) & 1u) n = 0; + const int src[3] = { in->src1_vreg, in->src2_vreg, in->src3_vreg }; + for (int s = 0; s < 3; s++) for (int k = 0; src[s] >= 0 && k < n; k++) if (inflight[k] == src[s]) bad++; + if (c != 0 && ((c >> 14) & 7u) == 0 && in->dst_vreg >= 0 && n < 64) inflight[n++] = in->dst_vreg; + } + return bad; +} +/* Control-edge walk: the straight-line scoreboard reasoning above holds only if nothing is in + * flight across a branch, barrier op or exit. Every BRA / EXIT / BSSY / BSYNC must wait on every + * scoreboard set since the last wait. Returns the number of control ops that do not. */ +static int edge_waits(const BlackwellIRProgram *p) { + uint32_t pending = 0; int bad = 0; + for (size_t i = 0; i < p->count; i++) { + const BlackwellIRInsn *in = &p->insns[i]; + uint32_t c = in->control, wait = (c >> 20) & 63u, wb = (c >> 14) & 7u, rb = (c >> 17) & 7u; + pending &= ~wait; + if (in->op == BW_IR_BRA || in->op == BW_IR_EXIT || in->op == BW_IR_BSSY || in->op == BW_IR_BSYNC) { if (pending) bad++; } + if (wb != 7u) pending |= 1u << wb; + if (rb != 7u) pending |= 1u << rb; + } + return bad; +} +static int count_op(const BlackwellIRProgram *p, BlackwellIROpcode op, int bar) { + int n = 0; + for (size_t i = 0; i < p->count; i++) if (p->insns[i].op == op && (bar < 0 || p->insns[i].bar_reg == bar)) n++; + return n; +} +static int find_op(const BlackwellIRProgram *p, BlackwellIROpcode op, int nth) { + for (size_t i = 0; i < p->count; i++) if (p->insns[i].op == op && nth-- == 0) return (int)i; + return -1; +} + +/* --------------------------------------------------------- nvdisasm listing */ +static int nvdisasm_bytes(const uint8_t *code, size_t bytes, const char *name, const char *const *must, int n_must) { + const char *nv = "/usr/local/cuda/bin/nvdisasm"; + if (access(nv, X_OK) != 0) { printf("nvdisasm: not present, listing check skipped for %s\n", name); return 0; } + char bin[96], lst[96], cmd[320]; + snprintf(bin, sizeof bin, "/tmp/omega_rc_%s_%d.bin", name, (int)getpid()); + snprintf(lst, sizeof lst, "/tmp/omega_rc_%s_%d.sass", name, (int)getpid()); + FILE *f = fopen(bin, "wb"); if (!f) return -1; + fwrite(code, 1, bytes, f); fclose(f); + snprintf(cmd, sizeof cmd, "%s -b SM121 %s > %s 2>&1", nv, bin, lst); + int rc = system(cmd); + FILE *l = fopen(lst, "r"); if (!l) return -1; + char line[512]; int seen[16] = {0}, bad = 0, insns = 0; + while (fgets(line, sizeof line, l)) { + if (!strstr(line, "/*0")) continue; + insns++; + if (strstr(line, "?") || strstr(line, "error")) bad++; + for (int i = 0; i < n_must && i < 16; i++) if (strstr(line, must[i])) seen[i] = 1; + } + fclose(l); + int miss = 0; + for (int i = 0; i < n_must && i < 16; i++) if (!seen[i]) { printf(" %s: nvdisasm listing lacks %s\n", name, must[i]); miss++; } + printf("nvdisasm %s: rc=%d insns=%d undecodable=%d missing=%d (%s)\n", name, rc, insns, bad, miss, lst); + unlink(bin); + return (rc == 0 && bad == 0 && miss == 0) ? 0 : -1; +} +static int nvdisasm_kernel(OmegaGpuEwOp op, int mutant, const char *name, const char *const *must, int n_must) { + BlackwellIRProgram *p = calloc(1, sizeof *p); OmegaBlackwellKernel k; + if (!p || codegen(op, mutant, p, &k) != OMEGA_GPU_EW_OK) { free(p); return -1; } + int rc = nvdisasm_bytes(k.code, k.code_size, name, must, n_must); + free(k.code); free(p); + return rc; +} + +/* --------------------------------------------------------- host part */ +typedef struct { const char *name; uint64_t warp_issues, lane_issues, splits; int max_frags, peak_bars; } Model; +static Model g_model[NPROBES * 2]; static int g_nmodel; + +static int host_part(void) { + int before = g_failed; + /* 1. encoder golden words (nvcc 13.0.88 -arch=sm_121 oracle, docs/gpu-reconvergence-308.md) */ + int fx = omega_blackwell_verify_codegen_fixtures_reconv(); + CHECK(fx == 0, "encoder golden words BSSY / BSYNC and refusals (rc %d)", fx); + + /* 2. region bookkeeping on a hand-built program */ + { + BlackwellIRProgram *p = calloc(1, sizeof *p); BwRegions r; memset(&r, 0, sizeof r); + omega_bw_ir_init(p); + BlackwellIRInsn in; memset(&in, 0, sizeof in); in.dst_vreg = in.src1_vreg = in.src2_vreg = in.src3_vreg = -1; + in.op = BW_IR_BSSY; in.bar_reg = 0; in.imm = 2; omega_bw_ir_append(p, &in); /* 0 */ + CHECK(bw_regions_next_bar(&r) == 0 && bw_regions_begin(&r, 0) == 0, "region begin returns B0"); + in.op = BW_IR_BRA; in.imm = 1; in.predicate_p0 = true; omega_bw_ir_append(p, &in); /* 1 */ + CHECK(bw_regions_exit(&r, 1) == 0, "region exit recorded"); + in.op = BW_IR_NOP; in.predicate_p0 = false; omega_bw_ir_append(p, &in); /* 2 */ + in.op = BW_IR_BSYNC; in.bar_reg = 0; omega_bw_ir_append(p, &in); /* 3 */ + CHECK(bw_regions_join(&r, p, 3) == 0, "region join returns B0"); + 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); + CHECK(bw_regions_join(&r, p, 3) == -1, "join with no open region refused"); + CHECK(bw_regions_exit(&r, 1) == -1, "exit with no open region refused"); + /* exits table full */ + memset(&r, 0, sizeof r); bw_regions_begin(&r, 0); + int ok = 1; for (int i = 0; i < BW_RECONV_MAX_EXITS; i++) ok &= bw_regions_exit(&r, 1) == 0; + CHECK(ok && bw_regions_exit(&r, 1) == -1, "exit table bound (%d) fails closed", BW_RECONV_MAX_EXITS); + /* join index inconsistent: BSYNC before BSSY, wrong barrier */ + memset(&r, 0, sizeof r); bw_regions_begin(&r, 3); + CHECK(bw_regions_join(&r, p, 0) == -1, "join with BSSY after BSYNC refused"); + memset(&r, 0, sizeof r); bw_regions_begin(&r, 0); p->insns[3].bar_reg = 1; + CHECK(bw_regions_join(&r, p, 3) == -1, "join with the wrong barrier register refused"); + p->insns[3].bar_reg = 0; + /* depth bound */ + memset(&r, 0, sizeof r); ok = 1; + for (int d = 0; d < BW_RECONV_MAX_DEPTH; d++) ok &= bw_regions_next_bar(&r) == d && bw_regions_begin(&r, d) == d; + 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); + free(p); + } + + /* 3. codegen: production probes build, refusals refuse, mutants differ */ + static const struct { OmegaGpuEwOp op; const char *name; } refuse[] = { + { OMEGA_GPU_EW_RC_REFUSE_DEPTH17, "17 nested regions" }, { OMEGA_GPU_EW_RC_REFUSE_EXIT_SHFL, "EXIT then BAR/SHFL" }, + { 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" }, + { OMEGA_GPU_EW_RC_REFUSE_SHFL_IN_REGION, "SHFL inside a region" }, { OMEGA_GPU_EW_RC_REFUSE_UNCLOSED, "region never joined" }, + { OMEGA_GPU_EW_RC_REFUSE_JOIN_ONLY, "BSYNC with no BSSY" }, + }; + for (size_t i = 0; i < sizeof refuse / sizeof refuse[0]; i++) { + BlackwellIRProgram *p = calloc(1, sizeof *p); OmegaBlackwellKernel k; + int rc = codegen(refuse[i].op, 0, p, &k); + CHECK(rc == OMEGA_GPU_EW_CODEGEN_FAIL, "%s refused at codegen (rc %s)", refuse[i].name, omega_gpu_elementwise_rc_name(rc)); + if (rc == OMEGA_GPU_EW_OK) free(k.code); + free(p); + } + for (size_t i = 0; i < NPROBES; i++) { + BlackwellIRProgram *p = calloc(1, sizeof *p); OmegaBlackwellKernel k; + int rc = codegen(PROBES[i].op, 0, p, &k); + 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); + if (rc != OMEGA_GPU_EW_OK) { free(p); continue; } + int nb = count_op(p, BW_IR_BSSY, -1), ns = count_op(p, BW_IR_BSYNC, -1); + int want = PROBES[i].op == OMEGA_GPU_EW_RC_NESTED ? 2 : PROBES[i].op == OMEGA_GPU_EW_RC_DEPTH16 ? BW_RECONV_MAX_BAR : 1; + CHECK(nb == want && ns == want, "%s has %d BSSY and %d BSYNC (want %d)", PROBES[i].name, nb, ns, want); + if (PROBES[i].op == OMEGA_GPU_EW_RC_DEPTH16) { + 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; + CHECK(each, "depth16 uses every barrier register B0..B15 exactly once"); + } + CHECK(sb_hazards(p) == 0, "%s scoreboard walk clean", PROBES[i].name); + CHECK(edge_waits(p) == 0, "%s every control op waits on everything in flight", PROBES[i].name); + printf("codegen %s: %zu insns, %u gprs, %d regions\n", PROBES[i].name, p->count, k.gpr_count, nb); + uint8_t d0[32]; memcpy(d0, k.code_digest, 32); free(k.code); + for (int m = 1; m <= 3; m++) { + BlackwellIRProgram *mp = calloc(1, sizeof *mp); OmegaBlackwellKernel mk; + int mrc = codegen(PROBES[i].op, m, mp, &mk); + int same_ok = m == 1 && PROBES[i].op == OMEGA_GPU_EW_RC_DEPTH16; /* depth16 has no math mutant */ + 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)); + if (mrc == OMEGA_GPU_EW_OK) { + 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); + if (m == 3) CHECK(count_op(mp, BW_IR_BSYNC, -1) == ns, "%s mutant 3 keeps its BSYNCs", PROBES[i].name); + free(mk.code); + } + free(mp); + } + free(p); + } + /* the four regioned sieves build too (their sweep runs in gb10_sieve_host_test) */ + for (int op = OMEGA_GPU_EW_PRIME_SIEVE_DIV1; op <= OMEGA_GPU_EW_PRIME_SIEVE_SHARED_DIV2; op++) { + BlackwellIRProgram *p = calloc(1, sizeof *p); OmegaBlackwellKernel k; + int rc = codegen((OmegaGpuEwOp)op, 0, p, &k); + int staged = op == OMEGA_GPU_EW_PRIME_SIEVE_SHARED_DIV1 || op == OMEGA_GPU_EW_PRIME_SIEVE_SHARED_DIV2; + 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; + 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); + if (rc == OMEGA_GPU_EW_OK) free(k.code); + free(p); + } + + /* 4. nvdisasm: the words decode as BSSY/BSYNC on the real barrier registers, and the predicated + * and B15 forms the encoder can produce decode too (built by hand: encode_one) */ + { + static const char *const m0[] = { "BSSY", "BSYNC", "B0", "SHFL.DOWN", "@P0 BRA" }; + CHECK(nvdisasm_kernel(OMEGA_GPU_EW_RC_DIAMOND, 0, "diamond", m0, 5) == 0, "diamond listing decodes with BSSY/BSYNC B0"); + static const char *const m1[] = { "BSSY", "BSYNC", "B15", "B7" }; + CHECK(nvdisasm_kernel(OMEGA_GPU_EW_RC_DEPTH16, 0, "depth16", m1, 4) == 0, "depth16 listing decodes with B15"); + static const char *const m2[] = { "BSSY", "B1", "BSYNC" }; + CHECK(nvdisasm_kernel(OMEGA_GPU_EW_PRIME_SIEVE_DIV2, 0, "sieve_div2", m2, 3) == 0, "nested sieve listing decodes with B1"); + 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; + 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); + 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); + 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); + in.op = BW_IR_EXIT; omega_blackwell_encode_one(&in, w); memcpy(code + 48, w, 16); + static const char *const m3[] = { "@P0 BSSY", "B3", "@!P0 BSYNC", "BSYNC.RECONVERGENT B15" }; + CHECK(nvdisasm_bytes(code, 64, "forms", m3, 4) == 0, "predicated BSSY/BSYNC and B15 decode"); + } + + /* 5. simulator: every probe, every n, both fragment orders, exact against the oracle */ + uint32_t *a = malloc(20000 * 4), *b = malloc(20000 * 4), *got = malloc(20000 * 4), *want = malloc(20000 * 4); + for (size_t i = 0; i < NPROBES; i++) { + BlackwellIRProgram *p = calloc(1, sizeof *p); OmegaBlackwellKernel k; + if (codegen(PROBES[i].op, 0, p, &k) != OMEGA_GPU_EW_OK) { free(p); continue; } + int fails = 0; uint64_t cases = 0; + for (size_t j = 0; j < NNS; j++) for (int order = 0; order < 2; order++) { + uint32_t n = NS[j]; fill(a, b, n, 0x308u + n * 7u + (uint32_t)order); + rc_oracle(PROBES[i].op, 0, n, a, b, want); + WsSim st; int err = ws_probe(p, n, a, b, got, order, &st); + uint32_t first; size_t bad = diff_count(got, want, n, &first); + cases++; + 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); } + 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; } + } + CHECK(fails == 0, "%s simulator vs oracle: %d of %" PRIu64 " cases differ", PROBES[i].name, fails, cases); + /* math mutant: simulator clean, result wrong (the oracle sees it) */ + BlackwellIRProgram *mp = calloc(1, sizeof *mp); OmegaBlackwellKernel mk; + if (codegen(PROBES[i].op, 1, mp, &mk) == OMEGA_GPU_EW_OK) { + uint32_t n = 1000; fill(a, b, n, 0x1001u); rc_oracle(PROBES[i].op, 0, n, a, b, want); + 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); + 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); + 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); + else CHECK(err == 0 && bad > 0, "%s math mutant: simulator clean, %zu words differ (err %d)", PROBES[i].name, bad, err); + free(mk.code); + } + free(mp); + /* dropped join and corrupted target: the simulator must reject both, in both orders */ + for (int m = 2; m <= 3; m++) for (int order = 0; order < 2; order++) { + BlackwellIRProgram *xp = calloc(1, sizeof *xp); OmegaBlackwellKernel xk; + if (codegen(PROBES[i].op, m, xp, &xk) == OMEGA_GPU_EW_OK) { + uint32_t n = 1000; fill(a, b, n, 0x2002u + (uint32_t)m); + WsSim st; int err = ws_probe(xp, n, a, b, got, order, &st); + 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"); + 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)); + free(xk.code); + } + free(xp); + } + free(k.code); free(p); + } + /* 6. hand mutations of the production diamond (controls on the simulator itself, independent of + * the emitter's mutant hooks): exit jumping past the BSYNC, BSYNC on the wrong barrier, a BSSY + * on an armed barrier. And the unmodified program as the positive control. */ + { + BlackwellIRProgram *p = calloc(1, sizeof *p), *q = calloc(1, sizeof *q); OmegaBlackwellKernel k; + if (codegen(OMEGA_GPU_EW_RC_DIAMOND, 0, p, &k) == OMEGA_GPU_EW_OK) { + uint32_t n = 500; fill(a, b, n, 0x3003u); WsSim st; + int bssy = find_op(p, BW_IR_BSSY, 0), bsync = find_op(p, BW_IR_BSYNC, 0), ex = -1; + for (int i = bssy + 1; i < bsync; i++) if (p->insns[i].op == BW_IR_BRA && !p->insns[i].predicate_p0) ex = i; + CHECK(bssy >= 0 && bsync > bssy && ex > bssy, "diamond shape: BSSY %d, exit %d, BSYNC %d", bssy, ex, bsync); + CHECK(ws_probe(p, n, a, b, got, 0, &st) == 0, "positive control: untouched diamond runs clean"); + *q = *p; q->insns[ex].imm += 1; /* exit lands after the BSYNC */ + int e1 = ws_probe(q, n, a, b, got, 0, &st); CHECK(is_reconv_err(e1), "exit past the BSYNC rejected: %s", st.msg); + *q = *p; q->insns[bsync].bar_reg = 1; /* BSYNC on a barrier nobody armed */ + 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); + *q = *p; q->insns[bsync].op = BW_IR_NOP; /* join dropped by hand */ + int e3 = ws_probe(q, n, a, b, got, 0, &st); CHECK(is_reconv_err(e3), "join replaced by NOP rejected: %s", st.msg); + *q = *p; q->insns[bssy].imm += 1; /* names the wrong join */ + int e4 = ws_probe(q, n, a, b, got, 0, &st); CHECK(e4 == WS_E_JOIN_MISMATCH, "wrong join target rejected: %s", st.msg); + *q = *p; q->insns[bssy + 1] = p->insns[bssy]; /* second BSSY on the armed barrier (overwrites the ISETP) */ + 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); + free(k.code); + } + free(p); free(q); + } + free(a); free(b); free(got); free(want); + 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); + return g_failed == before ? 0 : -1; +} + +/* --------------------------------------------------------- chip part */ +static int chip(const char *out_path, int observe) { + FILE *out = out_path ? fopen(out_path, "w") : NULL; + if (out_path && !out) { printf("cannot open %s\n", out_path); return 2; } + if (out) fprintf(out, "{\"schema\":\"OMEGA_GPU_RECONV_V1\",\"issue\":308,\"comparison\":\"exact u32\",\"cases\":["); + int first = 1; + uint32_t *a = malloc(20000 * 4), *b = malloc(20000 * 4), *got = malloc(20000 * 4), *want = malloc(20000 * 4); + for (size_t i = 0; i < NPROBES; i++) { + for (size_t j = 0; j < NNS; j++) { + uint32_t n = NS[j]; fill(a, b, n, 0x5050u + n); + rc_oracle(PROBES[i].op, 0, n, a, b, want); + OmegaGpuEwInfo info; memset(&info, 0, sizeof info); + int rc = omega_gpu_reconv_probe_u32(PROBES[i].op, n, a, b, got, &info); + uint32_t fi; size_t bad = rc == OMEGA_GPU_EW_OK ? diff_count(got, want, n, &fi) : n; + int pass = rc == OMEGA_GPU_EW_OK && bad == 0; + 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()); + 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}", + 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"); + first = 0; + if (!pass) { printf("STOP: %s failed on the chip at n=%u; no further launches of this probe\n", PROBES[i].name, n); break; } + } + } + if (out) fprintf(out, "],\"mutants\":["); + first = 1; + for (size_t i = 0; i < NPROBES; i++) { + if (PROBES[i].op == OMEGA_GPU_EW_RC_DEPTH16) continue; /* no math mutant */ + uint32_t n = 1000; fill(a, b, n, 0x6060u); rc_oracle(PROBES[i].op, 0, n, a, b, want); + omega_gpu_elementwise_test_set_mutant((int)PROBES[i].op); + /* R6's math mutant is the early-exit kernel: the emitter refuses it on the launch path + * (the bypass exists only for codegen_ir), so the catch is CODEGEN_FAIL: no undefined + * SHFL ever reaches the chip. */ + OmegaGpuEwInfo info; memset(&info, 0, sizeof info); + int rc = omega_gpu_reconv_probe_u32(PROBES[i].op, n, a, b, got, &info); + uint32_t fi; size_t bad = rc == OMEGA_GPU_EW_OK ? diff_count(got, want, n, &fi) : n; + int caught = rc != OMEGA_GPU_EW_OK || bad > 0; + omega_gpu_elementwise_test_set_mutant(0); + CHECK(caught, "%s math mutant caught (rc %s, violations %zu)", PROBES[i].name, omega_gpu_elementwise_rc_name(rc), bad); + 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)); + 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"); + first = 0; + } + if (out) fprintf(out, "],\"observations\":["); + if (observe) { + /* The dropped-join kernel (rejected by the simulator above) on the chip once, small n: + * recorded as an observation of what the hardware does without the BSYNC. Not a gate. */ + uint32_t n = 1000; fill(a, b, n, 0x7070u); rc_oracle(OMEGA_GPU_EW_RC_DIAMOND, 0, n, a, b, want); + omega_gpu_elementwise_test_set_mutant_level(OMEGA_GPU_EW_RC_DIAMOND, 2); + OmegaGpuEwInfo info; memset(&info, 0, sizeof info); + int rc = omega_gpu_reconv_probe_u32(OMEGA_GPU_EW_RC_DIAMOND, n, a, b, got, &info); + uint32_t fi; size_t bad = rc == OMEGA_GPU_EW_OK ? diff_count(got, want, n, &fi) : n; + omega_gpu_elementwise_test_set_mutant(0); + 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); + 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); + } + CHECK(omega_gpu_session_open_count() == 1, "device opened once for the battery (opens=%u)", omega_gpu_session_open_count()); + if (out) { + fprintf(out, "],\"sim_model_n4096\":["); + 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); + 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"); + fclose(out); + } + free(a); free(b); free(got); free(want); + return 0; +} + +int main(int argc, char **argv) { + const char *out_path = NULL; int host = 0, observe = 0; + for (int i = 1; i < argc; i++) { + if (strcmp(argv[i], "--host-only") == 0) host = 1; + else if (strcmp(argv[i], "--observe-dropped-join") == 0) observe = 1; + else if (strcmp(argv[i], "--out") == 0 && i + 1 < argc) out_path = argv[++i]; + } + int hrc = host_part(); + if (!host) { + if (hrc != 0) { printf("host part failed: no chip launches\nFAIL: %d checks, %d failed\n", g_checks, g_failed); return 1; } + if (chip(out_path, observe) != 0) return 2; + } + printf("%s: %d checks, %d failed\n", g_failed ? "FAIL" : "PASS", g_checks, g_failed); + return g_failed ? 1 : 0; +} diff --git a/evidence/gpu-reconv-308/T1a-GPU-RECONV-0a1812f/omega_blackwell_codegen.c b/evidence/gpu-reconv-308/T1a-GPU-RECONV-0a1812f/omega_blackwell_codegen.c new file mode 100644 index 000000000..38f73412a --- /dev/null +++ b/evidence/gpu-reconv-308/T1a-GPU-RECONV-0a1812f/omega_blackwell_codegen.c @@ -0,0 +1,1945 @@ +#include "omega_blackwell_codegen.h" +#include "omega_blackwell_submit.h" +#include "sha256.h" +#include +#include +#include + +void omega_bw_ir_init(BlackwellIRProgram *prog) { + if (!prog) return; + memset(prog, 0, sizeof(*prog)); +} + +int omega_bw_ir_alloc_vreg(BlackwellIRProgram *prog) { + if (!prog || prog->regalloc.num_vregs >= BW_MAX_VREGS) return -1; + int v = prog->regalloc.num_vregs++; + prog->regalloc.intervals[v].vreg = v; + prog->regalloc.intervals[v].first_def = -1; + prog->regalloc.intervals[v].last_use = -1; + prog->regalloc.intervals[v].phys_reg = -1; + prog->regalloc.intervals[v].bundle_size = 1; + prog->regalloc.intervals[v].is_pair = false; + prog->regalloc.intervals[v].is_quad = false; + prog->regalloc.intervals[v].active = false; + prog->regalloc.vreg_to_phys[v] = -1; + return v; +} + +int omega_bw_ir_alloc_vreg64(BlackwellIRProgram *prog) { + if (!prog || prog->regalloc.num_vregs >= BW_MAX_VREGS) return -1; + int v = prog->regalloc.num_vregs++; + prog->regalloc.intervals[v].vreg = v; + prog->regalloc.intervals[v].first_def = -1; + prog->regalloc.intervals[v].last_use = -1; + prog->regalloc.intervals[v].phys_reg = -1; + prog->regalloc.intervals[v].bundle_size = 2; + prog->regalloc.intervals[v].is_pair = true; + prog->regalloc.intervals[v].is_quad = false; + prog->regalloc.intervals[v].active = false; + prog->regalloc.vreg_to_phys[v] = -1; + return v; +} + +int omega_bw_ir_alloc_vreg128(BlackwellIRProgram *prog) { + if (!prog || prog->regalloc.num_vregs >= BW_MAX_VREGS) return -1; + int v = prog->regalloc.num_vregs++; + prog->regalloc.intervals[v].vreg = v; + prog->regalloc.intervals[v].first_def = -1; + prog->regalloc.intervals[v].last_use = -1; + prog->regalloc.intervals[v].phys_reg = -1; + prog->regalloc.intervals[v].bundle_size = 4; + prog->regalloc.intervals[v].is_pair = false; + prog->regalloc.intervals[v].is_quad = true; + prog->regalloc.intervals[v].active = false; + prog->regalloc.vreg_to_phys[v] = -1; + return v; +} + +int omega_bw_ir_alloc_uvreg(BlackwellIRProgram *prog) { + if (!prog || prog->regalloc.num_uvregs >= BW_MAX_UVREGS) return -1; + int uv = prog->regalloc.num_uvregs++; + prog->regalloc.uintervals[uv].vreg = uv; + prog->regalloc.uintervals[uv].first_def = -1; + prog->regalloc.uintervals[uv].last_use = -1; + prog->regalloc.uintervals[uv].phys_reg = -1; + prog->regalloc.uintervals[uv].is_pair = false; + prog->regalloc.uintervals[uv].active = false; + prog->regalloc.uvreg_to_phys[uv] = -1; + return uv; +} + +int omega_bw_ir_alloc_uvreg64(BlackwellIRProgram *prog) { + if (!prog || prog->regalloc.num_uvregs >= BW_MAX_UVREGS) return -1; + int uv = prog->regalloc.num_uvregs++; + prog->regalloc.uintervals[uv].vreg = uv; + prog->regalloc.uintervals[uv].first_def = -1; + prog->regalloc.uintervals[uv].last_use = -1; + prog->regalloc.uintervals[uv].phys_reg = -1; + prog->regalloc.uintervals[uv].is_pair = true; + prog->regalloc.uintervals[uv].active = false; + prog->regalloc.uvreg_to_phys[uv] = -1; + return uv; +} + +int omega_bw_ir_append(BlackwellIRProgram *prog, const BlackwellIRInsn *insn) { + if (!prog || !insn || prog->count >= BW_MAX_IR_INSNS) return -1; + int idx = (int)prog->count++; + prog->insns[idx] = *insn; + + /* Update live-interval def/use tracking */ + if (insn->dst_vreg >= 0 && insn->dst_vreg < prog->regalloc.num_vregs) { + if (!insn->is_uniform) { + OmegaLiveInterval *iv = &prog->regalloc.intervals[insn->dst_vreg]; + if (iv->first_def < 0) iv->first_def = idx; + if (idx > iv->last_use) iv->last_use = idx; + } + } + if (insn->src1_vreg >= 0 && insn->src1_vreg < prog->regalloc.num_vregs) { + OmegaLiveInterval *iv = &prog->regalloc.intervals[insn->src1_vreg]; + if (iv->first_def < 0) iv->first_def = idx; + if (idx > iv->last_use) iv->last_use = idx; + } + if (insn->src2_vreg >= 0 && insn->src2_vreg < prog->regalloc.num_vregs) { + OmegaLiveInterval *iv = &prog->regalloc.intervals[insn->src2_vreg]; + if (iv->first_def < 0) iv->first_def = idx; + if (idx > iv->last_use) iv->last_use = idx; + } + if (insn->src3_vreg >= 0 && insn->src3_vreg < prog->regalloc.num_vregs) { + OmegaLiveInterval *iv = &prog->regalloc.intervals[insn->src3_vreg]; + if (iv->first_def < 0) iv->first_def = idx; + if (idx > iv->last_use) iv->last_use = idx; + } + + /* Uniform register def/use tracking */ + if (insn->is_uniform && insn->dst_vreg >= 0 && insn->dst_vreg < prog->regalloc.num_uvregs) { + OmegaLiveInterval *uiv = &prog->regalloc.uintervals[insn->dst_vreg]; + if (uiv->first_def < 0) uiv->first_def = idx; + if (idx > uiv->last_use) uiv->last_use = idx; + } + if (insn->ureg >= 0 && insn->ureg < prog->regalloc.num_uvregs) { + OmegaLiveInterval *uiv = &prog->regalloc.uintervals[insn->ureg]; + if (uiv->first_def < 0) uiv->first_def = idx; + if (idx > uiv->last_use) uiv->last_use = idx; + } + + return idx; +} + +int omega_bw_regalloc_solve(BlackwellIRProgram *prog) { + if (!prog) return -1; + OmegaRegAlloc *ra = &prog->regalloc; + + /* + * Bounded deterministic linear-scan register allocator. + * Computes genuine live intervals across virtual registers and assigns + * available physical GPRs (R2..R127) and UGPRs (UR4..UR31). + * Enforces even-alignment and dual-register reservation for 64-bit pairs. + */ + bool gpr_busy[BW_PHYS_GPR_MAX]; + memset(gpr_busy, 0, sizeof(gpr_busy)); + + int max_phys_used = BW_PHYS_GPR_START; + + /* Sort virtual register indices chronologically by first_def (Poletto & Sarkar Linear Scan) */ + int order[BW_MAX_VREGS]; + int count = 0; + for (int v = 0; v < ra->num_vregs; v++) { + if (ra->intervals[v].first_def >= 0) { + order[count++] = v; + } else { + ra->intervals[v].phys_reg = BW_PHYS_GPR_START; + ra->vreg_to_phys[v] = ra->intervals[v].phys_reg; + } + } + for (int i = 0; i < count - 1; i++) { + for (int j = i + 1; j < count; j++) { + if (ra->intervals[order[i]].first_def > ra->intervals[order[j]].first_def) { + int tmp = order[i]; + order[i] = order[j]; + order[j] = tmp; + } + } + } + + for (int idx = 0; idx < count; idx++) { + int v = order[idx]; + OmegaLiveInterval *iv = &ra->intervals[v]; + + /* Free physical registers whose virtual live intervals have ended */ + for (int p = 0; p < ra->num_vregs; p++) { + if (ra->intervals[p].active && ra->intervals[p].last_use < iv->first_def) { + int freed = ra->intervals[p].phys_reg; + if (freed >= 0 && freed < BW_PHYS_GPR_MAX) { + uint32_t bsize = ra->intervals[p].bundle_size ? ra->intervals[p].bundle_size : (ra->intervals[p].is_pair ? 2 : (ra->intervals[p].is_quad ? 4 : 1)); + for (uint32_t k = 0; k < bsize && (freed + (int)k) < BW_PHYS_GPR_MAX; k++) { + gpr_busy[freed + k] = false; + } + } + ra->intervals[p].active = false; + } + } + + /* Find lowest available physical register */ + int assigned = -1; + uint32_t bsize = iv->bundle_size ? iv->bundle_size : (iv->is_pair ? 2 : (iv->is_quad ? 4 : 1)); + if (bsize == 4) { + /* 128-bit quad requires 4-alignment and 4 contiguous free registers */ + int start = (BW_PHYS_GPR_START + 3) & ~3; + for (int p = start; p <= BW_PHYS_GPR_MAX - 4; p += 4) { + if (!gpr_busy[p] && !gpr_busy[p + 1] && !gpr_busy[p + 2] && !gpr_busy[p + 3]) { + assigned = p; + for (int k = 0; k < 4; k++) gpr_busy[p + k] = true; + break; + } + } + } else if (bsize == 2) { + /* 64-bit pair requires even alignment and contiguous free registers */ + int start = (BW_PHYS_GPR_START + 1) & ~1; + for (int p = start; p <= BW_PHYS_GPR_MAX - 2; p += 2) { + if (!gpr_busy[p] && !gpr_busy[p + 1]) { + assigned = p; + gpr_busy[p] = true; + gpr_busy[p + 1] = true; + break; + } + } + } else { + for (int p = BW_PHYS_GPR_START; p < BW_PHYS_GPR_MAX; p++) { + if (!gpr_busy[p]) { + assigned = p; + gpr_busy[p] = true; + break; + } + } + } + + if (assigned < 0) { + /* Hardware register capacity exceeded: fail closed loudly */ + return -1; + } + + iv->phys_reg = assigned; + iv->active = true; + ra->vreg_to_phys[v] = assigned; + + int top = assigned + (int)bsize - 1; + if (top > max_phys_used) { + max_phys_used = top; + } + } + ra->peak_gpr_usage = (uint32_t)(max_phys_used + 1); + + /* Allocate uniform registers */ + int cur_u = BW_PHYS_UGPR_START; + for (int uv = 0; uv < ra->num_uvregs; uv++) { + OmegaLiveInterval *uiv = &ra->uintervals[uv]; + if (uiv->is_pair) { + if (cur_u % 2 != 0) cur_u++; + if (cur_u + 1 >= BW_PHYS_UGPR_MAX) return -1; + uiv->phys_reg = cur_u; + ra->uvreg_to_phys[uv] = cur_u; + cur_u += 2; + } else { + if (cur_u >= BW_PHYS_UGPR_MAX) return -1; + uiv->phys_reg = cur_u; + ra->uvreg_to_phys[uv] = cur_u++; + } + } + ra->peak_ugpr_usage = (uint32_t)cur_u; + + return 0; +} + +static int encode_single_insn(const BlackwellIRInsn *insn, const OmegaRegAlloc *ra, uint32_t w[4]) { + memset(w, 0, 16); + + int dst = (insn->dst_vreg >= 0) ? (ra->vreg_to_phys[insn->dst_vreg] + insn->dst_subreg) : 0; + int src1 = (insn->src1_vreg >= 0) ? (ra->vreg_to_phys[insn->src1_vreg] + insn->src1_subreg) : 0; + int src2 = (insn->src2_vreg >= 0) ? (ra->vreg_to_phys[insn->src2_vreg] + insn->src2_subreg) : 0; + int src3 = (insn->src3_vreg >= 0) ? (ra->vreg_to_phys[insn->src3_vreg] + insn->src3_subreg) : 0xff; /* Default to RZ (0xff) */ + int ureg = (insn->ureg >= 0) ? ra->uvreg_to_phys[insn->ureg] : 0; + + if (insn->is_uniform && insn->dst_vreg >= 0) { + dst = ra->uvreg_to_phys[insn->dst_vreg]; + } + + switch (insn->op) { + case BW_IR_NOP: + w[0] = 0x00007918; + w[1] = 0x00000000; + w[2] = 0x00000000; + w[3] = 0x000fc000; + break; + + case BW_IR_MOV_RZ: + /* MOV Rd, RZ */ + w[0] = 0x7202 | ((uint32_t)(dst & 0xff) << 16); + w[1] = 0x000000ff; + w[2] = 0x00000f00; + w[3] = insn->control ? insn->control : 0x000fe200; + break; + + case BW_IR_MOV_IMM: + /* MOV Rd, imm32 */ + w[0] = 0x7802 | ((uint32_t)(dst & 0xff) << 16); + w[1] = insn->imm; + w[2] = 0x00000f00; + w[3] = insn->control ? insn->control : 0x000fe200; + break; + + case BW_IR_S2R: + /* S2R Rd, SR */ + w[0] = 0x7919 | ((uint32_t)(dst & 0xff) << 16); + w[1] = 0x0; + w[2] = (insn->imm & 0xff) << 8; + w[3] = insn->control ? insn->control : 0x000e2200; + break; + + case BW_IR_LDC: + /* LDC Rd, c[0x0][imm] */ + w[0] = 0xff007b82 | ((uint32_t)(dst & 0xff) << 16); + w[1] = (insn->imm << 6) & 0x00ffff00; + w[2] = 0x00000800; + w[3] = insn->control ? insn->control : 0x000fe200; + break; + + case BW_IR_LDC64: + /* LDC.64 Rd:Rd+1, c[0x0][imm] */ + w[0] = 0xff007b82 | ((uint32_t)(dst & 0xff) << 16); + w[1] = (insn->imm << 6) & 0x00ffff00; + w[2] = 0x00000a00; + w[3] = insn->control ? insn->control : 0x000e2200; + break; + + case BW_IR_LDCU: + /* LDCU URd, c[0x0][imm] */ + w[0] = 0xff0077ac | ((uint32_t)(dst & 0xff) << 16); + w[1] = (insn->imm << 5) & 0x00ffff00; + w[2] = 0x08000800; + w[3] = insn->control ? insn->control : 0x000e2200; + break; + + case BW_IR_LDCU64: + /* LDCU.64 URd:URd+1, c[0x0][imm] */ + w[0] = 0xff0077ac | ((uint32_t)(dst & 0xff) << 16); + w[1] = (insn->imm << 5) & 0x00ffff00; + w[2] = 0x08000a00; + w[3] = insn->control ? insn->control : 0x000e6e00; + break; + + case BW_IR_IMAD: + if (insn->src2_vreg >= 0) { + /* Register-register: IMAD Rd, Ra, Rb, Rc (0x7224) */ + w[0] = 0x7224 | ((uint32_t)(dst & 0xff) << 16) | ((uint32_t)(src1 & 0xff) << 24); + w[1] = (uint32_t)(src2 & 0xff); + w[2] = 0x078e0200 | (uint32_t)(src3 & 0xff); + w[3] = insn->control ? insn->control : 0x001fca00; + } else if (insn->is_uniform && insn->ureg >= 0) { + /* Uniform register multiplier: IMAD Rd, Ra, URb, Rc (0x7c24) */ + w[0] = 0x7c24 | ((uint32_t)(dst & 0xff) << 16) | ((uint32_t)(src1 & 0xff) << 24); + w[1] = (uint32_t)(ureg & 0xff); + w[2] = 0x0f8e0200 | (uint32_t)(src3 & 0xff); + w[3] = insn->control ? insn->control : 0x001fca00; + } else { + /* Immediate multiplier: IMAD Rd, Ra, imm, Rc (0x7824) */ + w[0] = 0x7824 | ((uint32_t)(dst & 0xff) << 16) | ((uint32_t)(src1 & 0xff) << 24); + w[1] = insn->imm; + w[2] = 0x078e0200 | (uint32_t)(src3 & 0xff); + w[3] = insn->control ? insn->control : 0x001fca00; + } + break; + + case BW_IR_IMAD_WIDE: + /* IMAD.WIDE.U32 Rd:Rd+1, Ra, imm, Rc:Rc+1 */ + w[0] = 0x7825 | ((uint32_t)(dst & 0xff) << 16) | ((uint32_t)(src1 & 0xff) << 24); + w[1] = insn->imm; + w[2] = 0x078e0000 | (uint32_t)(src3 & 0xff); + w[3] = insn->control ? insn->control : 0x001fcc00; + break; + + case BW_IR_ISETP_GE: + if (insn->is_uniform && insn->ureg >= 0) { + w[0] = 0x7c0c | ((uint32_t)(dst & 0xff) << 16) | ((uint32_t)(src1 & 0xff) << 24); + w[1] = (uint32_t)(ureg & 0xff); + w[2] = 0x0bf06070; + w[3] = 0x002fda00; + } else { + w[0] = 0x780c | ((uint32_t)(dst & 0xff) << 16) | ((uint32_t)(src1 & 0xff) << 24); + w[1] = insn->imm; + w[2] = 0x03f06270; + w[3] = 0x001fda00; + } + break; + + case BW_IR_LDG_E: + /* LDG.E Rd, desc[URd][Ra.64] */ + w[0] = 0x7981 | ((uint32_t)(dst & 0xff) << 16) | ((uint32_t)(src1 & 0xff) << 24); + w[1] = (uint32_t)(ureg & 0xff); + w[2] = 0x0c1e1900; + w[3] = insn->control ? insn->control : 0x000f2200; + break; + + case BW_IR_STG_E: + /* STG.E desc[URd][Ra.64], Rb */ + w[0] = 0x7986 | ((uint32_t)(src1 & 0xff) << 24); + w[1] = (uint32_t)(src2 & 0xff); + w[2] = 0x0c101900 | (uint32_t)(ureg & 0xff); + w[3] = insn->control ? insn->control : 0x000fe200; + break; + + case BW_IR_STG_EF: + /* STG.E.MMIO.GPU. Uncached store, meant to be visible without a fence. */ + w[0] = 0x7986 | ((uint32_t)(src1 & 0xff) << 24); + w[1] = (uint32_t)(src2 & 0xff); + w[2] = 0x0c111900 | (uint32_t)(ureg & 0xff); + w[3] = insn->control ? insn->control : 0x000fe200; + break; + + case BW_IR_IADD3: + /* IADD3 Rd, PT, PT, Ra, Rb, Rc */ + w[0] = 0x7210 | ((uint32_t)(dst & 0xff) << 16) | ((uint32_t)(src1 & 0xff) << 24); + w[1] = (uint32_t)(src2 & 0xff); + w[2] = 0x07ffe0ff; + w[3] = 0x010fca00; + break; + + case BW_IR_HMMA_F16: + case BW_IR_HMMA_BF16: { + /* HMMA.16816.F32[.BF16] Rd, Ra, Rb, Rc */ + w[0] = 0x723c | ((uint32_t)(dst & 0xff) << 16) | ((uint32_t)(src1 & 0xff) << 24); + w[1] = (uint32_t)(src2 & 0xff); + uint32_t prec_bit = (insn->op == BW_IR_HMMA_BF16) ? 0x00040000 : 0x00000000; + w[2] = (uint32_t)(src3 & 0xff) | (0x18 << 8) | prec_bit; + w[3] = insn->control ? insn->control : 0x000fe200; + break; + } + + case BW_IR_SHF_R: + /* SHF.R.U32.HI Rd, RZ, imm, Ra */ + w[0] = 0x7819U | ((uint32_t)(dst & 0xff) << 16) | (0xffU << 24); + w[1] = (uint32_t)insn->imm; + w[2] = 0x00011600 | (uint32_t)(src1 & 0xff); + w[3] = insn->control ? insn->control : 0x001fca00; + break; + + case BW_IR_LOP3_AND: + /* LOP3.LUT Rd, Ra, imm, RZ, 0xc0, !PT */ + w[0] = 0x7812U | ((uint32_t)(dst & 0xff) << 16) | ((uint32_t)(src1 & 0xff) << 24); + w[1] = (uint32_t)insn->imm; + w[2] = 0x078ec0ff; + w[3] = insn->control ? insn->control : 0x001fca00; + break; + + case BW_IR_LDG_E_U16: + /* LDG.E.U16 Rd, desc[URd][Ra.64] */ + w[0] = 0x7981U | ((uint32_t)(dst & 0xff) << 16) | ((uint32_t)(src1 & 0xff) << 24); + w[1] = (uint32_t)(ureg & 0xff); + w[2] = 0x0c1e1500; + w[3] = insn->control ? insn->control : 0x000f2200; + break; + + case BW_IR_LDG_STRONG_SYS: + /* LDG.E.STRONG.SYS. Same shape as LDG.E; w[2] carries the + * system-scope bits (0x0c1e1900 -> 0x0c1f5900). */ + w[0] = 0x7981 | ((uint32_t)(dst & 0xff) << 16) | ((uint32_t)(src1 & 0xff) << 24); + w[1] = (uint32_t)(ureg & 0xff); + w[2] = 0x0c1f5900; + w[3] = insn->control ? insn->control : 0x000f2200; + break; + + case BW_IR_LDG_MMIO: + /* LDG.E.EF. Drop the cached line, then load. */ + w[0] = 0x7981 | ((uint32_t)(dst & 0xff) << 16) | ((uint32_t)(src1 & 0xff) << 24); + w[1] = (uint32_t)(ureg & 0xff); + w[2] = 0x0c0e1900; + w[3] = insn->control ? insn->control : 0x000f2200; + break; + + case BW_IR_STG_STRONG_SYS: + w[0] = 0x7986 | ((uint32_t)(src1 & 0xff) << 24); + if (insn->predicate_p0) { + uint32_t pred = insn->predicate_not ? 0x8u : 0x0u; + w[0] = (w[0] & ~0xF000u) | (pred << 12); + } + w[1] = (uint32_t)(src2 & 0xff); + w[2] = 0x0c115900 | (uint32_t)(ureg & 0xff); + w[3] = insn->control ? insn->control : 0x000fe200; + break; + + case BW_IR_MEMBAR_ALL_SYS: + w[0] = 0x00007992; + w[1] = 0x00000000; + w[2] = 0x0000b000; + w[3] = insn->control ? insn->control : 0x000fec00; + break; + + case BW_IR_MEMBAR_SC_SYS: + w[0] = 0x00007992; + w[1] = 0x00000000; + w[2] = 0x00003000; + w[3] = insn->control ? insn->control : 0x000fec00; + break; + + case BW_IR_CCTL_IVALL: + w[0] = 0xff00798f; + w[1] = 0x00000000; + w[2] = 0x02000000; + w[3] = insn->control ? insn->control : 0x000fe800; + break; + + case BW_IR_ATOMG_ADD_STRONG_SYS: + w[0] = 0x79a8 | ((uint32_t)(dst & 0xff) << 16) | ((uint32_t)(src1 & 0xff) << 24); + w[1] = (uint32_t)(src2 & 0xff) | 0x80000000u; + w[2] = 0x081f5100 | (uint32_t)(ureg & 0xff); + w[3] = insn->control ? insn->control : 0x00321e00; + break; + + case BW_IR_ATOMG_EXCH_STRONG_SYS: + w[0] = 0x79a8 | ((uint32_t)(dst & 0xff) << 16) | ((uint32_t)(src1 & 0xff) << 24); + w[1] = (uint32_t)(src2 & 0xff) | 0x80000000u; + w[2] = 0x0c1f5100 | (uint32_t)(ureg & 0xff); + w[3] = insn->control ? insn->control : 0x00321e00; + break; + + case BW_IR_ISETP_GE_U32: + /* ISETP.GE.U32.AND P0, PT, Ra, Rb, PT. Checked with nvdisasm -b SM121. */ + w[0] = 0x720c | ((uint32_t)(src1 & 0xff) << 24); + w[1] = (uint32_t)(src2 & 0xff); + w[2] = 0x03f06070; + w[3] = insn->control ? insn->control : 0x001fda00; + break; + + case BW_IR_LOP3_XOR: + /* LOP3.LUT Rd, Ra, Rb, RZ, 0x3c, !PT. Checked with nvdisasm -b SM121. */ + w[0] = 0x7212 | ((uint32_t)(dst & 0xff) << 16) | ((uint32_t)(src1 & 0xff) << 24); + w[1] = (uint32_t)(src2 & 0xff); + w[2] = 0x078e3cff; + w[3] = insn->control ? insn->control : 0x001fca00; + break; + + case BW_IR_FADD: + /* FADD Rd, Ra, Rb */ + w[0] = 0x7221U | ((uint32_t)(dst & 0xff) << 16) | ((uint32_t)(src1 & 0xff) << 24); + w[1] = (uint32_t)(src2 & 0xff); + w[2] = 0x00000000; + w[3] = insn->control ? insn->control : 0x004fc400; + break; + + case BW_IR_FSUB: + /* FSUB Rd, Ra, Rb (FADD Rd, Ra, -Rb) */ + w[0] = 0x7221U | ((uint32_t)(dst & 0xff) << 16) | ((uint32_t)(src1 & 0xff) << 24); + w[1] = (uint32_t)(src2 & 0xff) | 0x80000000U; + w[2] = 0x00000000; + w[3] = insn->control ? insn->control : 0x004fc400; + break; + + case BW_IR_FMUL: + /* FMUL Rd, Ra, Rb */ + w[0] = 0x7220U | ((uint32_t)(dst & 0xff) << 16) | ((uint32_t)(src1 & 0xff) << 24); + w[1] = (uint32_t)(src2 & 0xff); + w[2] = 0x00400000; + w[3] = insn->control ? insn->control : 0x004fc400; + break; + + case BW_IR_FFMA: + /* FFMA Rd, Ra, Rb, Rc */ + w[0] = 0x7223U | ((uint32_t)(dst & 0xff) << 16) | ((uint32_t)(src1 & 0xff) << 24); + w[1] = (uint32_t)(src2 & 0xff); + w[2] = (uint32_t)(src3 & 0xff); + w[3] = insn->control ? insn->control : 0x004fc400; + break; + + case BW_IR_FSETP: + /* FSETP.cond.AND P0, PT, Ra, Rb, PT */ + w[0] = 0x720bU | ((uint32_t)(dst & 0x7) << 16) | ((uint32_t)(src1 & 0xff) << 24); + w[1] = (uint32_t)(src2 & 0xff); + w[2] = insn->imm ? insn->imm : 0x03f06000; /* Default GE condition */ + w[3] = insn->control ? insn->control : 0x004fc400; + break; + + case BW_IR_FSEL: + /* FSEL Rd, Ra, Rb, P0 */ + w[0] = 0x7208U | ((uint32_t)(dst & 0xff) << 16) | ((uint32_t)(src1 & 0xff) << 24); + w[1] = (uint32_t)(src2 & 0xff); + w[2] = (uint32_t)(src3 & 0x7); + w[3] = insn->control ? insn->control : 0x000fca00; + break; + + case BW_IR_FMNMX_MIN: + /* FMNMX Rd, Ra, Rb, PT (minimum) */ + w[0] = 0x7209U | ((uint32_t)(dst & 0xff) << 16) | ((uint32_t)(src1 & 0xff) << 24); + w[1] = (uint32_t)(src2 & 0xff); + w[2] = 0x03800000; + w[3] = insn->control ? insn->control : 0x004fc400; + break; + + case BW_IR_FMNMX_MAX: + /* FMNMX Rd, Ra, Rb, !PT (maximum) */ + w[0] = 0x7209U | ((uint32_t)(dst & 0xff) << 16) | ((uint32_t)(src1 & 0xff) << 24); + w[1] = (uint32_t)(src2 & 0xff); + w[2] = 0x07800000; + w[3] = insn->control ? insn->control : 0x004fc400; + break; + + case BW_IR_I2FP: + /* I2FP.F32.S32 Rd, Ra */ + w[0] = 0x7245U | ((uint32_t)(dst & 0xff) << 16); + w[1] = (uint32_t)(src1 & 0xff); + w[2] = 0x00201400; + w[3] = insn->control ? insn->control : 0x004fe200; + break; + + case BW_IR_F2I: + /* F2I.TRUNC.NTZ Rd, Ra */ + w[0] = 0x7305U | ((uint32_t)(dst & 0xff) << 16); + w[1] = (uint32_t)(src1 & 0xff); + w[2] = 0x0020f100; + w[3] = insn->control ? insn->control : 0x004e2200; + break; + + case BW_IR_MUFU_RCP: + /* MUFU.RCP Rd, Ra */ + w[0] = 0x7308U | ((uint32_t)(dst & 0xff) << 16); + w[1] = (uint32_t)(src1 & 0xff); + w[2] = 0x00001000; + w[3] = insn->control ? insn->control : 0x000e2400; + break; + + case BW_IR_MUFU_RSQ: + /* MUFU.RSQ Rd, Ra */ + w[0] = 0x7308U | ((uint32_t)(dst & 0xff) << 16); + w[1] = (uint32_t)(src1 & 0xff); + w[2] = 0x00001400; + w[3] = insn->control ? insn->control : 0x000e2400; + break; + + case BW_IR_SHFL_DOWN: + /* SHFL.DOWN PT, Rd, Ra, offset, 0x1f */ + w[0] = 0x7f89U | ((uint32_t)(dst & 0xff) << 16) | ((uint32_t)(src1 & 0xff) << 24); + w[1] = 0x08001f00U | (((uint32_t)insn->imm & 0x1f) << 21); + w[2] = 0x000e0000; + w[3] = insn->control ? insn->control : 0x000e2400; + break; + + case BW_IR_LDS: + /* LDS Rd, [Ra] */ + w[0] = 0x7984U | ((uint32_t)(dst & 0xff) << 16) | ((uint32_t)(src1 & 0xff) << 24); + w[1] = 0x00000000; + w[2] = (uint32_t)(insn->imm & 0xffff); + w[3] = insn->control ? insn->control : 0x000fe200; + break; + + case BW_IR_STS: + /* STS [Ra], Rb */ + w[0] = 0x7388U | ((uint32_t)(src1 & 0xff) << 24); + w[1] = (uint32_t)(src2 & 0xff); + w[2] = (uint32_t)(insn->imm & 0xffff); + w[3] = insn->control ? insn->control : 0x000fe200; + break; + + case BW_IR_EXIT: + w[0] = insn->predicate_p0 ? 0x0000094d : 0x0000794d; + w[1] = 0x00000000; + w[2] = 0x03800000; + w[3] = 0x000fea00; + break; + + case BW_IR_BRA: { + /* sm_121 BRA. Distance is (target - this) - 1, split across the + * low six bits at instruction bits 18..23 and the rest at bit 34. + * Predicate bits 12..15: 7 = always, 0 = @P0, 8 = @!P0. + * A zero delta with no predicate keeps the self-branch word the + * existing kernels already emit after EXIT. */ + int32_t delta = (int32_t)insn->imm; + int32_t rel = delta - 1; + uint32_t pred_field = 0x7u; + if (insn->predicate_p0) + pred_field = insn->predicate_not ? 0x8u : 0x0u; + w[0] = 0x947u | (pred_field << 12) | (((uint32_t)rel & 0x3fu) << 18); + w[1] = (uint32_t)(rel >> 6) << 2; + w[2] = (rel < 0) ? 0x0383ffffu : 0x03800000u; + if (insn->control) + w[3] = insn->control; + else if (delta == 0 && !insn->predicate_p0) + w[3] = 0x000fc000u; + else + w[3] = 0x000fea00u; + break; + } + + case BW_IR_BSSY: { + /* BSSY.RECONVERGENT Bn, target (omega #308). nvcc 13.0.88 -arch=sm_121: + * [0070] BSSY.RECONVERGENT B0, 0x140 ; 0x000000c000007945 0x000fe20003800200 + * opcode 0x945 (NAK sm70_encode.rs:4165), barrier id at bits 16..19 (set_bar_dst + * 16..20), guard predicate at bits 12..15 like BRA, relative byte offset + * target - (this + 16) at bit 32 (NAK: field 34.. in 4-byte units), the cond + * predicate PT at bits 87..89 (0x0380 << 16 in w[2]) and bit 73 (0x200 in w[2]), + * which the disassembler prints as .RECONVERGENT; nvcc sets it on every BSSY/BSYNC + * for sm_121 and Mesa NAK does not. We follow nvcc. The target is the instruction + * after the matching BSYNC, so the delta is at least 2; smaller is refused. */ + int32_t delta = (int32_t)insn->imm; + if (delta < 2 || insn->bar_reg >= BW_RECONV_MAX_BAR) return -1; + uint32_t pred_field = 0x7u; + if (insn->predicate_p0) + pred_field = insn->predicate_not ? 0x8u : 0x0u; + w[0] = 0x945u | (pred_field << 12) | ((uint32_t)insn->bar_reg << 16); + w[1] = (uint32_t)(delta - 1) * 16u; + w[2] = 0x03800200u; + w[3] = insn->control ? insn->control : 0x000fe200u; + break; + } + + case BW_IR_BSYNC: { + /* BSYNC.RECONVERGENT Bn: [0130] BSYNC.RECONVERGENT B0 ; 0x0000000000007941 0x000fea0003800200 + * opcode 0x941 (NAK sm70_encode.rs:4179), barrier id bits 16..19, cond PT, bit 73. */ + if (insn->bar_reg >= BW_RECONV_MAX_BAR) return -1; + uint32_t pred_field = 0x7u; + if (insn->predicate_p0) + pred_field = insn->predicate_not ? 0x8u : 0x0u; + w[0] = 0x941u | (pred_field << 12) | ((uint32_t)insn->bar_reg << 16); + w[1] = 0; + w[2] = 0x03800200u; + w[3] = insn->control ? insn->control : 0x000fea00u; + break; + } + + case BW_IR_MUFU_EX2: + /* MUFU.EX2 Rd, Ra. nvdisasm 13.0 -b SM121: 0x7308 with sub-op 0x0800 decodes + * "MUFU.EX2 R2, R4" (RCP is 0x1000, RSQ 0x1400 above). */ + w[0] = 0x7308U | ((uint32_t)(dst & 0xff) << 16); + w[1] = (uint32_t)(src1 & 0xff); + w[2] = 0x00000800; + w[3] = insn->control ? insn->control : 0x000e2400; + break; + + case BW_IR_BAR_SYNC: + /* BAR.SYNC.DEFER_BLOCKING 0x0: the exact words src/omega_numeric.c:885 runs on + * the chip (nvdisasm: "BAR.SYNC.DEFER_BLOCKING 0x0"). No registers. */ + w[0] = 0x00007b1dU; + w[1] = 0x00000000; + w[2] = 0x00010000; + w[3] = insn->control ? insn->control : 0x000fec00; + break; + + case BW_IR_LDS32: + /* LDS Rd, [Ra+URZ] (32-bit), the src/omega_numeric.c:886 form. */ + w[0] = 0x7984U | ((uint32_t)(dst & 0xff) << 16) | ((uint32_t)(src1 & 0xff) << 24); + w[1] = 0x000000ff; + w[2] = 0x08000800; + w[3] = insn->control ? insn->control : 0x000e2800; + break; + + case BW_IR_STS32: + /* STS [Ra+URZ], Rb (32-bit), the src/omega_numeric.c:884 form. */ + w[0] = 0x7988U | ((uint32_t)(src1 & 0xff) << 24); + w[1] = (uint32_t)(src2 & 0xff); + w[2] = 0x080008ffU; + w[3] = insn->control ? insn->control : 0x000fe200; + break; + + /* Prime race cut (2026-10-05). An absent operand is RZ for these three ops. */ + case BW_IR_LOP3_LUT: { + /* LOP3.LUT Rd, Ra, Rb, Rc, imm8, !PT. LOP3_XOR above is this form with Rc = RZ, LUT 0x3c. */ + uint32_t a = (insn->src1_vreg >= 0) ? (uint32_t)(src1 & 0xff) : 0xffU; + uint32_t b = (insn->src2_vreg >= 0) ? (uint32_t)(src2 & 0xff) : 0xffU; + w[0] = 0x7212U | ((uint32_t)(dst & 0xff) << 16) | (a << 24); + w[1] = b; + w[2] = 0x078e0000U | ((insn->imm & 0xffU) << 8) | (uint32_t)(src3 & 0xff); + w[3] = insn->control ? insn->control : 0x001fca00; + break; + } + + case BW_IR_SHF_L_U32: { + /* SHF.L.U32 Rd, Ra, Rb, Rc: register-amount form (0x7219) of SHF_R's 0x7819, with the + * .R (w[2] bit 12) and .HI (bit 16) flags clear. Rc is the funnel high word (RZ). */ + uint32_t a = (insn->src1_vreg >= 0) ? (uint32_t)(src1 & 0xff) : 0xffU; + uint32_t b = (insn->src2_vreg >= 0) ? (uint32_t)(src2 & 0xff) : 0xffU; + w[0] = 0x7219U | ((uint32_t)(dst & 0xff) << 16) | (a << 24); + w[1] = b; + w[2] = 0x00000600U | (uint32_t)(src3 & 0xff); + w[3] = insn->control ? insn->control : 0x001fca00; + break; + } + + case BW_IR_IMAD_HI_U32: { + /* IMAD.HI.U32 Rd, Ra, Rb, Rc: opcode 0x227 with the IMAD.WIDE.U32 modifier word. */ + uint32_t a = (insn->src1_vreg >= 0) ? (uint32_t)(src1 & 0xff) : 0xffU; + uint32_t b = (insn->src2_vreg >= 0) ? (uint32_t)(src2 & 0xff) : 0xffU; + w[0] = 0x7227U | ((uint32_t)(dst & 0xff) << 16) | (a << 24); + w[1] = b; + w[2] = 0x078e0000U | (uint32_t)(src3 & 0xff); + w[3] = insn->control ? insn->control : 0x001fca00; + break; + } + + default: + return -1; + } + return 0; +} + +/* Prime race cut (2026-10-05): golden words for LOP3_LUT, SHF_L_U32 and IMAD_HI_U32. + * Each expected (w0, w1, w2) is the low 96 bits of an instruction nvcc 13.0.88 emitted + * for sm_121 (nvcc -arch=sm_121 -cubin, cuobjdump -sass; source and dump in + * bench/prime_race/tests/encoding_oracle.md). Control words (w3) are scheduler output and + * not compared. nvcc is an offline oracle only; nothing here links CUDA. 0 = all good. */ +int omega_blackwell_verify_codegen_fixtures_intops(void) { + OmegaRegAlloc ra; + memset(&ra, 0, sizeof(ra)); + static const int phys[] = { 0, 2, 6, 7, 9, 11, 13 }; + for (int i = 0; i < (int)(sizeof phys / sizeof phys[0]); i++) ra.vreg_to_phys[i] = phys[i]; + enum { R0 = 0, R2, R6, R7, R9, R11, R13, RZ = -1 }; + static const struct { + BlackwellIROpcode op; int d, a, b, c; uint32_t imm; uint32_t w0, w1, w2; const char *text; + } fx[] = { + { BW_IR_LOP3_LUT, R9, R7, R0, RZ, 0xfc, 0x07097212U, 0x00000000U, 0x078efcffU, "LOP3.LUT R9, R7, R0, RZ, 0xfc, !PT" }, + { BW_IR_LOP3_LUT, R11, R6, R7, R0, 0x78, 0x060b7212U, 0x00000007U, 0x078e7800U, "LOP3.LUT R11, R6, R7, R0, 0x78, !PT" }, + { BW_IR_LOP3_LUT, R13, R6, R7, R0, 0x10, 0x060d7212U, 0x00000007U, 0x078e1000U, "LOP3.LUT R13, R6, R7, R0, 0x10, !PT" }, + { BW_IR_LOP3_LUT, R7, RZ, R9, RZ, 0x33, 0xff077212U, 0x00000009U, 0x078e33ffU, "LOP3.LUT R7, RZ, R9, RZ, 0x33, !PT" }, + { BW_IR_SHF_L_U32, R7, R9, R2, RZ, 0, 0x09077219U, 0x00000002U, 0x000006ffU, "SHF.L.U32 R7, R9, R2, RZ" }, + { BW_IR_SHF_L_U32, R7, R0, R7, RZ, 0, 0x00077219U, 0x00000007U, 0x000006ffU, "SHF.L.U32 R7, R0, R7, RZ" }, + { BW_IR_IMAD_HI_U32, R9, R9, R0, R6, 0, 0x09097227U, 0x00000000U, 0x078e0006U, "IMAD.HI.U32 R9, R9, R0, R6" }, + { BW_IR_IMAD_HI_U32, R7, R0, R7, RZ, 0, 0x00077227U, 0x00000007U, 0x078e00ffU, "IMAD.HI.U32 R7, R0, R7, RZ" }, + }; + for (int i = 0; i < (int)(sizeof fx / sizeof fx[0]); i++) { + BlackwellIRInsn in; + memset(&in, 0, sizeof in); + in.op = fx[i].op; in.dst_vreg = fx[i].d; in.src1_vreg = fx[i].a; in.src2_vreg = fx[i].b; + in.src3_vreg = fx[i].c; in.ureg = -1; in.imm = fx[i].imm; + uint32_t w[4]; + if (encode_single_insn(&in, &ra, w) != 0) return -(2 * i + 1); + if (w[0] != fx[i].w0 || w[1] != fx[i].w1 || w[2] != fx[i].w2) return -(2 * i + 2); + } + return 0; +} + +int omega_blackwell_encode_one(const BlackwellIRInsn *insn, uint32_t w[4]) { + if (!insn || !w) return -1; + OmegaRegAlloc ra; + memset(&ra, 0, sizeof ra); + for (int i = 0; i < BW_MAX_VREGS; i++) ra.vreg_to_phys[i] = i; + for (int i = 0; i < BW_MAX_UVREGS; i++) ra.uvreg_to_phys[i] = i; + return encode_single_insn(insn, &ra, w); +} + +/* omega #308: BSSY / BSYNC golden words. Oracle: nvcc 13.0.88 -arch=sm_121 -cubin -O3 on the + * kernels in docs/gpu-reconvergence-308.md, cuobjdump -sass (low 64-bit word, then high; the + * top 23 bits of the high word are the control word and are not compared here): + * k_loop [0070] BSSY.RECONVERGENT B0, 0x140 ; 0x000000c000007945 0x000fe20003800200 (delta 13) + * k_nested [0070] BSSY.RECONVERGENT B0, 0x300 ; 0x0000028000007945 0x000fe20003800200 (delta 41) + * k_break [0070] BSSY.RECONVERGENT B0, 0x110 ; 0x0000009000007945 0x000fe20003800200 (delta 10) + * k_loop [0130] BSYNC.RECONVERGENT B0 ; 0x0000000000007941 0x000fea0003800200 + * Barrier ids other than B0 and the predicated forms are decoded by tools/reconv_nvdisasm_check.sh + * (nvdisasm -b SM121 text), not by this table. */ +int omega_blackwell_verify_codegen_fixtures_reconv(void) { + static const struct { BlackwellIROpcode op; int delta; int bar; uint32_t w0, w1, w2; } fx[] = { + { BW_IR_BSSY, 13, 0, 0x00007945U, 0x000000c0U, 0x03800200U }, + { BW_IR_BSSY, 41, 0, 0x00007945U, 0x00000280U, 0x03800200U }, + { BW_IR_BSSY, 10, 0, 0x00007945U, 0x00000090U, 0x03800200U }, + { BW_IR_BSYNC, 0, 0, 0x00007941U, 0x00000000U, 0x03800200U }, + }; + for (int i = 0; i < (int)(sizeof fx / sizeof fx[0]); i++) { + BlackwellIRInsn in; + memset(&in, 0, sizeof in); + in.op = fx[i].op; in.dst_vreg = -1; in.src1_vreg = -1; in.src2_vreg = -1; in.src3_vreg = -1; in.ureg = -1; + in.imm = (uint32_t)fx[i].delta; in.bar_reg = (uint8_t)fx[i].bar; + uint32_t w[4]; + if (omega_blackwell_encode_one(&in, w) != 0) return -(2 * i + 1); + if (w[0] != fx[i].w0 || w[1] != fx[i].w1 || w[2] != fx[i].w2) return -(2 * i + 2); + /* control word defaults mirror nvcc's: BSSY 0x000fe200, BSYNC 0x000fea00 */ + if (w[3] != (fx[i].op == BW_IR_BSSY ? 0x000fe200U : 0x000fea00U)) return -(2 * i + 2); + } + /* refusals: a BSSY must name a later instruction (delta >= 2), barrier ids stop at B15 */ + BlackwellIRInsn bad; uint32_t w[4]; + memset(&bad, 0, sizeof bad); bad.op = BW_IR_BSSY; bad.dst_vreg = bad.src1_vreg = bad.src2_vreg = bad.src3_vreg = bad.ureg = -1; + bad.imm = 1; bad.bar_reg = 0; + if (omega_blackwell_encode_one(&bad, w) == 0) return -101; + bad.imm = (uint32_t)-3; + if (omega_blackwell_encode_one(&bad, w) == 0) return -102; + bad.imm = 5; bad.bar_reg = 16; + if (omega_blackwell_encode_one(&bad, w) == 0) return -103; + bad.op = BW_IR_BSYNC; bad.imm = 0; bad.bar_reg = 16; + if (omega_blackwell_encode_one(&bad, w) == 0) return -104; + bad.bar_reg = 15; + if (omega_blackwell_encode_one(&bad, w) != 0 || w[0] != (0x00007941U | (15U << 16))) return -105; + return 0; +} + +/* FB-1 cut 4: word-level fixtures for the four additive ops (checked against the + * nvdisasm decodes recorded in docs/numeric/FB1_CUT4_ELEMENTWISE.md). 0 = all good. */ +int omega_blackwell_verify_codegen_fixtures_fb1cut4(void) { + OmegaRegAlloc ra; + memset(&ra, 0, sizeof(ra)); + ra.vreg_to_phys[0] = 2; /* R2 */ + ra.vreg_to_phys[1] = 4; /* R4 */ + ra.vreg_to_phys[2] = 9; /* R9 */ + ra.vreg_to_phys[3] = 10; /* R10 */ + ra.vreg_to_phys[4] = 8; /* R8 */ + uint32_t w[4]; + + /* MUFU.EX2 R2, R4 */ + BlackwellIRInsn ex2 = { .op = BW_IR_MUFU_EX2, .dst_vreg = 0, .src1_vreg = 1 }; + if (encode_single_insn(&ex2, &ra, w) != 0) return -1; + if (w[0] != 0x00027308U || w[1] != 4 || w[2] != 0x00000800U) return -2; + /* BAR.SYNC.DEFER_BLOCKING 0x0 */ + BlackwellIRInsn bar = { .op = BW_IR_BAR_SYNC }; + if (encode_single_insn(&bar, &ra, w) != 0) return -3; + if (w[0] != 0x00007b1dU || w[1] != 0 || w[2] != 0x00010000U || w[3] != 0x000fec00U) return -4; + /* LDS R9, [R10+URZ] */ + BlackwellIRInsn lds = { .op = BW_IR_LDS32, .dst_vreg = 2, .src1_vreg = 3 }; + if (encode_single_insn(&lds, &ra, w) != 0) return -5; + if (w[0] != 0x0a097984U || w[1] != 0xff || w[2] != 0x08000800U) return -6; + /* STS [R8+URZ], R2 */ + BlackwellIRInsn sts = { .op = BW_IR_STS32, .src1_vreg = 4, .src2_vreg = 0 }; + if (encode_single_insn(&sts, &ra, w) != 0) return -7; + if (w[0] != 0x08007988U || w[1] != 2 || w[2] != 0x080008ffU) return -8; + return 0; +} + +int omega_bw_encode_program(const BlackwellIRProgram *prog, uint8_t *code_buf, size_t max_len, size_t *out_len) { + if (!prog || !code_buf || !out_len) return -1; + + size_t padded_insns = (prog->count + 7) & ~7ULL; + if (padded_insns < 32) padded_insns = 32; + + size_t req_bytes = padded_insns * 16; + if (max_len < req_bytes) return -1; + + uint8_t *p = code_buf; + for (size_t i = 0; i < padded_insns; i++) { + uint32_t w[4]; + if (i < prog->count) { + if (encode_single_insn(&prog->insns[i], &prog->regalloc, w) != 0) { + return -1; + } + } else { + /* Pad with NOP */ + w[0] = 0x00007918; + w[1] = 0x00000000; + w[2] = 0x00000000; + w[3] = 0x000fc000; + } + + p[0] = (uint8_t)(w[0] & 0xff); + p[1] = (uint8_t)((w[0] >> 8) & 0xff); + p[2] = (uint8_t)((w[0] >> 16) & 0xff); + p[3] = (uint8_t)((w[0] >> 24) & 0xff); + + p[4] = (uint8_t)(w[1] & 0xff); + p[5] = (uint8_t)((w[1] >> 8) & 0xff); + p[6] = (uint8_t)((w[1] >> 16) & 0xff); + p[7] = (uint8_t)((w[1] >> 24) & 0xff); + + p[8] = (uint8_t)(w[2] & 0xff); + p[9] = (uint8_t)((w[2] >> 8) & 0xff); + p[10] = (uint8_t)((w[2] >> 16) & 0xff); + p[11] = (uint8_t)((w[2] >> 24) & 0xff); + + p[12] = (uint8_t)(w[3] & 0xff); + p[13] = (uint8_t)((w[3] >> 8) & 0xff); + p[14] = (uint8_t)((w[3] >> 16) & 0xff); + p[15] = (uint8_t)((w[3] >> 24) & 0xff); + + p += 16; + } + + *out_len = req_bytes; + return 0; +} + +int omega_blackwell_codegen_matmul_i32(const OmegaMatMulSpec *spec, OmegaBlackwellKernel *kernel) { + if (!spec || !kernel) return -1; + if (spec->m == 0 || spec->k == 0 || spec->n == 0) return -1; + + /* Fail closed loudly if K exceeds bounded unroll limit */ + if (spec->k > 32) { + fprintf(stderr, "ERROR: MatMul integer codegen exceeds bounded unroll limit (K=%u > 32)\n", spec->k); + return -1; + } + + BlackwellIRProgram prog; + omega_bw_ir_init(&prog); + + /* 1. Allocate virtual registers */ + /* 64-bit base pointers */ + int v_ptr_a = omega_bw_ir_alloc_vreg64(&prog); + int v_ptr_b = omega_bw_ir_alloc_vreg64(&prog); + int v_ptr_c = omega_bw_ir_alloc_vreg64(&prog); + int uv_desc = omega_bw_ir_alloc_uvreg64(&prog); + + /* 32-bit CTA / Thread indexing */ + int v_ctaid_x = omega_bw_ir_alloc_vreg(&prog); + int v_ctaid_y = omega_bw_ir_alloc_vreg(&prog); + int v_tid_x = omega_bw_ir_alloc_vreg(&prog); + int v_tid_y = omega_bw_ir_alloc_vreg(&prog); + + /* 2D Coordinates & loop invariants */ + int v_col = omega_bw_ir_alloc_vreg(&prog); + int v_row = omega_bw_ir_alloc_vreg(&prog); + int v_row_k = omega_bw_ir_alloc_vreg(&prog); + int v_const_1 = omega_bw_ir_alloc_vreg(&prog); + int v_acc = omega_bw_ir_alloc_vreg(&prog); + + /* Working registers for memory address and operands */ + int v_idx_a = omega_bw_ir_alloc_vreg(&prog); + int v_off_a = omega_bw_ir_alloc_vreg64(&prog); + int v_val_a = omega_bw_ir_alloc_vreg(&prog); + + int v_idx_b = omega_bw_ir_alloc_vreg(&prog); + int v_off_b = omega_bw_ir_alloc_vreg64(&prog); + int v_val_b = omega_bw_ir_alloc_vreg(&prog); + + int v_idx_c = omega_bw_ir_alloc_vreg(&prog); + int v_off_c = omega_bw_ir_alloc_vreg64(&prog); + + if (v_off_c < 0) return -1; + + /* 2. Load 64-bit global memory descriptor into uniform register pair */ + /* LDCU.64 uv_desc, c[0x0][0x358] */ + BlackwellIRInsn insn_desc = { .op = BW_IR_LDCU64, .dst_vreg = uv_desc, .imm = 0x358, .is_uniform = true }; + omega_bw_ir_append(&prog, &insn_desc); + + /* 3. Load 64-bit base pointers from Constant Bank 0 */ + /* LDC.64 v_ptr_a, c[0x0][0x380] */ + BlackwellIRInsn insn_ptra = { .op = BW_IR_LDC64, .dst_vreg = v_ptr_a, .imm = 0x380 }; + omega_bw_ir_append(&prog, &insn_ptra); + + /* LDC.64 v_ptr_b, c[0x0][0x388] */ + BlackwellIRInsn insn_ptrb = { .op = BW_IR_LDC64, .dst_vreg = v_ptr_b, .imm = 0x388 }; + omega_bw_ir_append(&prog, &insn_ptrb); + + /* LDC.64 v_ptr_c, c[0x0][0x390] */ + BlackwellIRInsn insn_ptrc = { .op = BW_IR_LDC64, .dst_vreg = v_ptr_c, .imm = 0x390 }; + omega_bw_ir_append(&prog, &insn_ptrc); + + /* 4. Query 2D CTA and Thread coordinates */ + /* S2R v_ctaid_x, SR_CTAID.X */ + BlackwellIRInsn insn_ctax = { .op = BW_IR_S2R, .dst_vreg = v_ctaid_x, .imm = BW_SR_CTAID_X }; + omega_bw_ir_append(&prog, &insn_ctax); + + /* S2R v_ctaid_y, SR_CTAID.Y */ + BlackwellIRInsn insn_ctay = { .op = BW_IR_S2R, .dst_vreg = v_ctaid_y, .imm = BW_SR_CTAID_Y }; + omega_bw_ir_append(&prog, &insn_ctay); + + /* S2R v_tid_x, SR_TID.X */ + BlackwellIRInsn insn_tidx = { .op = BW_IR_S2R, .dst_vreg = v_tid_x, .imm = BW_SR_TID_X }; + omega_bw_ir_append(&prog, &insn_tidx); + + /* S2R v_tid_y, SR_TID.Y */ + BlackwellIRInsn insn_tidy = { .op = BW_IR_S2R, .dst_vreg = v_tid_y, .imm = BW_SR_TID_Y }; + omega_bw_ir_append(&prog, &insn_tidy); + + /* 5. Compute 2D (row, col) coordinates */ + /* Standard 16x16 tile threads: col = ctaid_x * 16 + tid_x */ + BlackwellIRInsn insn_col = { .op = BW_IR_IMAD, .dst_vreg = v_col, .src1_vreg = v_ctaid_x, .src2_vreg = -1, .imm = 16, .src3_vreg = v_tid_x }; + omega_bw_ir_append(&prog, &insn_col); + + /* row = ctaid_y * 16 + tid_y */ + BlackwellIRInsn insn_row = { .op = BW_IR_IMAD, .dst_vreg = v_row, .src1_vreg = v_ctaid_y, .src2_vreg = -1, .imm = 16, .src3_vreg = v_tid_y }; + omega_bw_ir_append(&prog, &insn_row); + + /* 6. Precompute row base index: v_row_k = v_row * K + RZ */ + BlackwellIRInsn insn_row_k = { .op = BW_IR_IMAD, .dst_vreg = v_row_k, .src1_vreg = v_row, .src2_vreg = -1, .imm = spec->k, .src3_vreg = -1 }; + omega_bw_ir_append(&prog, &insn_row_k); + + /* 7. Initialize v_const_1 = 1 for immediate linear indexing */ + BlackwellIRInsn insn_c1 = { .op = BW_IR_MOV_IMM, .dst_vreg = v_const_1, .imm = 1 }; + omega_bw_ir_append(&prog, &insn_c1); + + /* 8. Initialize accumulator to zero: MOV v_acc, RZ */ + BlackwellIRInsn insn_init_acc = { .op = BW_IR_MOV_RZ, .dst_vreg = v_acc }; + omega_bw_ir_append(&prog, &insn_init_acc); + + /* 9. Synthesize inner accumulation loop across K iterations */ + for (uint32_t step = 0; step < spec->k; step++) { + /* A[row, step]: idx_a = v_row_k + step = v_const_1 * step + v_row_k */ + BlackwellIRInsn insn_idxa = { .op = BW_IR_IMAD, .dst_vreg = v_idx_a, .src1_vreg = v_const_1, .src2_vreg = -1, .imm = step, .src3_vreg = v_row_k }; + omega_bw_ir_append(&prog, &insn_idxa); + + /* off_a = v_ptr_a + idx_a * 4 */ + BlackwellIRInsn insn_offa = { .op = BW_IR_IMAD_WIDE, .dst_vreg = v_off_a, .src1_vreg = v_idx_a, .src2_vreg = -1, .imm = 4, .src3_vreg = v_ptr_a }; + omega_bw_ir_append(&prog, &insn_offa); + + /* Load A[row, step]: LDG.E v_val_a, desc[uv_desc][v_off_a.64] */ + BlackwellIRInsn insn_lda = { .op = BW_IR_LDG_E, .dst_vreg = v_val_a, .src1_vreg = v_off_a, .src2_vreg = -1, .src3_vreg = -1, .ureg = uv_desc, .control = 0x001ea800 }; + omega_bw_ir_append(&prog, &insn_lda); + + /* B[step, col]: idx_b = step * N + col = v_const_1 * (step * N) + v_col */ + uint32_t step_n = step * spec->n; + BlackwellIRInsn insn_idxb = { .op = BW_IR_IMAD, .dst_vreg = v_idx_b, .src1_vreg = v_const_1, .src2_vreg = -1, .imm = step_n, .src3_vreg = v_col }; + omega_bw_ir_append(&prog, &insn_idxb); + + /* off_b = v_ptr_b + idx_b * 4 */ + BlackwellIRInsn insn_offb = { .op = BW_IR_IMAD_WIDE, .dst_vreg = v_off_b, .src1_vreg = v_idx_b, .src2_vreg = -1, .imm = 4, .src3_vreg = v_ptr_b }; + omega_bw_ir_append(&prog, &insn_offb); + + /* Load B[step, col]: LDG.E v_val_b, desc[uv_desc][v_off_b.64] */ + BlackwellIRInsn insn_ldb = { .op = BW_IR_LDG_E, .dst_vreg = v_val_b, .src1_vreg = v_off_b, .src2_vreg = -1, .src3_vreg = -1, .ureg = uv_desc, .control = 0x002ea800 }; + omega_bw_ir_append(&prog, &insn_ldb); + + /* Multiply and accumulate: IMAD v_acc, v_val_a, v_val_b, v_acc */ + BlackwellIRInsn insn_acc = { .op = BW_IR_IMAD, .dst_vreg = v_acc, .src1_vreg = v_val_a, .src2_vreg = v_val_b, .src3_vreg = v_acc, .control = 0x004fca00 }; + omega_bw_ir_append(&prog, &insn_acc); + } + + /* 10. Compute output index: idx_c = row * N + col */ + BlackwellIRInsn insn_idxc = { .op = BW_IR_IMAD, .dst_vreg = v_idx_c, .src1_vreg = v_row, .src2_vreg = -1, .imm = spec->n, .src3_vreg = v_col }; + omega_bw_ir_append(&prog, &insn_idxc); + + /* off_c = v_ptr_c + idx_c * 4 */ + BlackwellIRInsn insn_offc = { .op = BW_IR_IMAD_WIDE, .dst_vreg = v_off_c, .src1_vreg = v_idx_c, .src2_vreg = -1, .imm = 4, .src3_vreg = v_ptr_c }; + omega_bw_ir_append(&prog, &insn_offc); + + /* Store C[row, col]: STG.E desc[uv_desc][v_off_c.64], v_acc */ + BlackwellIRInsn insn_stc = { .op = BW_IR_STG_E, .src1_vreg = v_off_c, .src2_vreg = v_acc, .src3_vreg = -1, .ureg = uv_desc }; + omega_bw_ir_append(&prog, &insn_stc); + + /* 11. Kernel termination */ + BlackwellIRInsn insn_exit = { .op = BW_IR_EXIT, .predicate_p0 = false }; + omega_bw_ir_append(&prog, &insn_exit); + + BlackwellIRInsn insn_bra = { .op = BW_IR_BRA }; + omega_bw_ir_append(&prog, &insn_bra); + + /* 12. Solve bounded deterministic register allocation */ + if (omega_bw_regalloc_solve(&prog) != 0) { + return -1; + } + + /* 13. Allocate code buffer and encode instructions */ + size_t padded_insns = (prog.count + 7) & ~7ULL; + if (padded_insns < 32) padded_insns = 32; + size_t alloc_size = padded_insns * 16; + + kernel->code = (uint8_t *)malloc(alloc_size); + if (!kernel->code) return -1; + + size_t emitted_size = 0; + if (omega_bw_encode_program(&prog, kernel->code, alloc_size, &emitted_size) != 0) { + free(kernel->code); + kernel->code = NULL; + return -1; + } + + kernel->code_size = emitted_size; + kernel->insn_count = emitted_size / 16; + kernel->gpr_count = prog.regalloc.peak_gpr_usage; + kernel->uniform_gpr_count = prog.regalloc.peak_ugpr_usage; + + /* 14. Compute runtime SHA-256 digest of emitted machine code */ + sha256_hash(kernel->code, kernel->code_size, kernel->code_digest); + return 0; +} + +int omega_blackwell_codegen_matmul_tensor_prog(const OmegaMatMulSpec *spec, BlackwellIRProgram *prog) { + if (!spec || !prog) return -1; + if (spec->m == 0 || spec->k == 0 || spec->n == 0) return -1; + if (spec->precision != OMEGA_MATMUL_PRECISION_FP16 && spec->precision != OMEGA_MATMUL_PRECISION_BF16) return -1; + if (spec->m % 16 != 0 || spec->n % 8 != 0 || spec->k != 16) { + fprintf(stderr, "ERROR: MatMul tensor codegen requires M multiple of 16, N multiple of 8, K=16 (got %ux%ux%u)\n", + spec->m, spec->k, spec->n); + return -1; + } + + omega_bw_ir_init(prog); + + /* 1. Allocate virtual registers */ + /* 64-bit base pointers */ + int v_ptr_a = omega_bw_ir_alloc_vreg64(prog); + int v_ptr_b = omega_bw_ir_alloc_vreg64(prog); + int v_ptr_c = omega_bw_ir_alloc_vreg64(prog); + int uv_desc = omega_bw_ir_alloc_uvreg64(prog); + + /* 32-bit CTA / Thread indexing */ + int v_tid = omega_bw_ir_alloc_vreg(prog); + int v_ctaid_x = omega_bw_ir_alloc_vreg(prog); + int v_ctaid_y = omega_bw_ir_alloc_vreg(prog); + + /* Tile and thread coordinates */ + int v_group_id = omega_bw_ir_alloc_vreg(prog); + int v_tid_in_grp = omega_bw_ir_alloc_vreg(prog); + int v_row_base = omega_bw_ir_alloc_vreg(prog); + int v_col_base = omega_bw_ir_alloc_vreg(prog); + int v_r0 = omega_bw_ir_alloc_vreg(prog); + int v_r1 = omega_bw_ir_alloc_vreg(prog); + int v_col_b = omega_bw_ir_alloc_vreg(prog); + int v_tid_col2 = omega_bw_ir_alloc_vreg(prog); + int v_const_1 = omega_bw_ir_alloc_vreg(prog); + + /* Bundles for HMMA */ + int v_ra = omega_bw_ir_alloc_vreg128(prog); /* Quad: Ra[0]..Ra[3] */ + int v_rb = omega_bw_ir_alloc_vreg64(prog); /* Pair: Rb[0]..Rb[1] */ + int v_rd = omega_bw_ir_alloc_vreg128(prog); /* Quad: Rd[0]..Rd[3] */ + + /* Scratch registers for indexing and loads */ + int v_idx = omega_bw_ir_alloc_vreg(prog); + int v_off = omega_bw_ir_alloc_vreg64(prog); + int v_b_tmp0 = omega_bw_ir_alloc_vreg(prog); + int v_b_tmp1 = omega_bw_ir_alloc_vreg(prog); + + if (v_b_tmp1 < 0) return -1; + + /* 2. Load 64-bit global memory descriptor into uniform register pair */ + /* LDCU.64 uv_desc, c[0x0][0x358] */ + BlackwellIRInsn insn_desc = { .op = BW_IR_LDCU64, .dst_vreg = uv_desc, .imm = 0x358, .is_uniform = true }; + omega_bw_ir_append(prog, &insn_desc); + + /* 3. Load 64-bit base pointers from Constant Bank 0 */ + /* LDC.64 v_ptr_a, c[0x0][0x380] */ + BlackwellIRInsn insn_ptra = { .op = BW_IR_LDC64, .dst_vreg = v_ptr_a, .imm = 0x380 }; + omega_bw_ir_append(prog, &insn_ptra); + + /* LDC.64 v_ptr_b, c[0x0][0x388] */ + BlackwellIRInsn insn_ptrb = { .op = BW_IR_LDC64, .dst_vreg = v_ptr_b, .imm = 0x388 }; + omega_bw_ir_append(prog, &insn_ptrb); + + /* LDC.64 v_ptr_c, c[0x0][0x390] */ + BlackwellIRInsn insn_ptrc = { .op = BW_IR_LDC64, .dst_vreg = v_ptr_c, .imm = 0x390 }; + omega_bw_ir_append(prog, &insn_ptrc); + + /* 4. Query Special Registers */ + /* S2R v_tid, SR_TID.X */ + BlackwellIRInsn insn_tidx = { .op = BW_IR_S2R, .dst_vreg = v_tid, .imm = BW_SR_TID_X }; + omega_bw_ir_append(prog, &insn_tidx); + + /* S2R v_ctaid_x, SR_CTAID.X */ + BlackwellIRInsn insn_cta_x = { .op = BW_IR_S2R, .dst_vreg = v_ctaid_x, .imm = BW_SR_CTAID_X }; + omega_bw_ir_append(prog, &insn_cta_x); + + /* S2R v_ctaid_y, SR_CTAID.Y */ + BlackwellIRInsn insn_cta_y = { .op = BW_IR_S2R, .dst_vreg = v_ctaid_y, .imm = BW_SR_CTAID_Y }; + omega_bw_ir_append(prog, &insn_cta_y); + + /* 5. Set up constant 1 */ + BlackwellIRInsn insn_c1 = { .op = BW_IR_MOV_IMM, .dst_vreg = v_const_1, .imm = 1 }; + omega_bw_ir_append(prog, &insn_c1); + + /* 6. Extract warp-level tile and thread coordinates */ + /* group_id = tid >> 2 */ + BlackwellIRInsn insn_grp = { .op = BW_IR_SHF_R, .dst_vreg = v_group_id, .src1_vreg = v_tid, .imm = 2 }; + omega_bw_ir_append(prog, &insn_grp); + + /* tid_in_grp = tid & 3 */ + BlackwellIRInsn insn_tig = { .op = BW_IR_LOP3_AND, .dst_vreg = v_tid_in_grp, .src1_vreg = v_tid, .imm = 3 }; + omega_bw_ir_append(prog, &insn_tig); + + /* row_base = ctaid_y * 16 */ + BlackwellIRInsn insn_rbase = { .op = BW_IR_IMAD, .dst_vreg = v_row_base, .src1_vreg = v_ctaid_y, .src2_vreg = -1, .imm = 16, .src3_vreg = -1 }; + omega_bw_ir_append(prog, &insn_rbase); + + /* col_base = ctaid_x * 8 */ + BlackwellIRInsn insn_cbase = { .op = BW_IR_IMAD, .dst_vreg = v_col_base, .src1_vreg = v_ctaid_x, .src2_vreg = -1, .imm = 8, .src3_vreg = -1 }; + omega_bw_ir_append(prog, &insn_cbase); + + /* r0 = row_base + group_id */ + BlackwellIRInsn insn_r0 = { .op = BW_IR_IADD3, .dst_vreg = v_r0, .src1_vreg = v_row_base, .src2_vreg = v_group_id, .src3_vreg = -1 }; + omega_bw_ir_append(prog, &insn_r0); + + /* r1 = r0 + 8 */ + BlackwellIRInsn insn_r1 = { .op = BW_IR_IMAD, .dst_vreg = v_r1, .src1_vreg = v_const_1, .src2_vreg = -1, .imm = 8, .src3_vreg = v_r0 }; + omega_bw_ir_append(prog, &insn_r1); + + /* col_b = col_base + group_id */ + BlackwellIRInsn insn_colb = { .op = BW_IR_IADD3, .dst_vreg = v_col_b, .src1_vreg = v_col_base, .src2_vreg = v_group_id, .src3_vreg = -1 }; + omega_bw_ir_append(prog, &insn_colb); + + /* tid_col2 = tid_in_grp * 2 */ + BlackwellIRInsn insn_tcol2 = { .op = BW_IR_IMAD, .dst_vreg = v_tid_col2, .src1_vreg = v_tid_in_grp, .src2_vreg = -1, .imm = 2, .src3_vreg = -1 }; + omega_bw_ir_append(prog, &insn_tcol2); + + /* 7. Load A fragments (16x16 row-major, 2 bytes/element, row stride 32 bytes) */ + /* Pair 0: a0, a1 at r0 * 32 + tid_col2 * 2 */ + BlackwellIRInsn insn_a0_idx1 = { .op = BW_IR_IMAD, .dst_vreg = v_idx, .src1_vreg = v_r0, .src2_vreg = -1, .imm = 32, .src3_vreg = -1 }; + omega_bw_ir_append(prog, &insn_a0_idx1); + BlackwellIRInsn insn_a0_idx2 = { .op = BW_IR_IMAD, .dst_vreg = v_idx, .src1_vreg = v_tid_col2, .src2_vreg = -1, .imm = 2, .src3_vreg = v_idx }; + omega_bw_ir_append(prog, &insn_a0_idx2); + BlackwellIRInsn insn_a0_off = { .op = BW_IR_IMAD_WIDE, .dst_vreg = v_off, .src1_vreg = v_idx, .src2_vreg = -1, .imm = 1, .src3_vreg = v_ptr_a }; + omega_bw_ir_append(prog, &insn_a0_off); + BlackwellIRInsn insn_lda0 = { .op = BW_IR_LDG_E, .dst_vreg = v_ra, .dst_subreg = 0, .src1_vreg = v_off, .src2_vreg = -1, .src3_vreg = -1, .ureg = uv_desc, .control = 0x001ea800 }; + omega_bw_ir_append(prog, &insn_lda0); + + /* Pair 1: a2, a3 at r1 * 32 + tid_col2 * 2 */ + BlackwellIRInsn insn_a1_idx1 = { .op = BW_IR_IMAD, .dst_vreg = v_idx, .src1_vreg = v_r1, .src2_vreg = -1, .imm = 32, .src3_vreg = -1 }; + omega_bw_ir_append(prog, &insn_a1_idx1); + BlackwellIRInsn insn_a1_idx2 = { .op = BW_IR_IMAD, .dst_vreg = v_idx, .src1_vreg = v_tid_col2, .src2_vreg = -1, .imm = 2, .src3_vreg = v_idx }; + omega_bw_ir_append(prog, &insn_a1_idx2); + BlackwellIRInsn insn_a1_off = { .op = BW_IR_IMAD_WIDE, .dst_vreg = v_off, .src1_vreg = v_idx, .src2_vreg = -1, .imm = 1, .src3_vreg = v_ptr_a }; + omega_bw_ir_append(prog, &insn_a1_off); + BlackwellIRInsn insn_lda1 = { .op = BW_IR_LDG_E, .dst_vreg = v_ra, .dst_subreg = 1, .src1_vreg = v_off, .src2_vreg = -1, .src3_vreg = -1, .ureg = uv_desc, .control = 0x001ea800 }; + omega_bw_ir_append(prog, &insn_lda1); + + /* Pair 2: a4, a5 at r0 * 32 + tid_col2 * 2 + 16 */ + BlackwellIRInsn insn_a2_idx1 = { .op = BW_IR_IMAD, .dst_vreg = v_idx, .src1_vreg = v_r0, .src2_vreg = -1, .imm = 32, .src3_vreg = -1 }; + omega_bw_ir_append(prog, &insn_a2_idx1); + BlackwellIRInsn insn_a2_idx2 = { .op = BW_IR_IMAD, .dst_vreg = v_idx, .src1_vreg = v_tid_col2, .src2_vreg = -1, .imm = 2, .src3_vreg = v_idx }; + omega_bw_ir_append(prog, &insn_a2_idx2); + BlackwellIRInsn insn_a2_idx3 = { .op = BW_IR_IMAD, .dst_vreg = v_idx, .src1_vreg = v_const_1, .src2_vreg = -1, .imm = 16, .src3_vreg = v_idx }; + omega_bw_ir_append(prog, &insn_a2_idx3); + BlackwellIRInsn insn_a2_off = { .op = BW_IR_IMAD_WIDE, .dst_vreg = v_off, .src1_vreg = v_idx, .src2_vreg = -1, .imm = 1, .src3_vreg = v_ptr_a }; + omega_bw_ir_append(prog, &insn_a2_off); + BlackwellIRInsn insn_lda2 = { .op = BW_IR_LDG_E, .dst_vreg = v_ra, .dst_subreg = 2, .src1_vreg = v_off, .src2_vreg = -1, .src3_vreg = -1, .ureg = uv_desc, .control = 0x001ea800 }; + omega_bw_ir_append(prog, &insn_lda2); + + /* Pair 3: a6, a7 at r1 * 32 + tid_col2 * 2 + 16 */ + BlackwellIRInsn insn_a3_idx1 = { .op = BW_IR_IMAD, .dst_vreg = v_idx, .src1_vreg = v_r1, .src2_vreg = -1, .imm = 32, .src3_vreg = -1 }; + omega_bw_ir_append(prog, &insn_a3_idx1); + BlackwellIRInsn insn_a3_idx2 = { .op = BW_IR_IMAD, .dst_vreg = v_idx, .src1_vreg = v_tid_col2, .src2_vreg = -1, .imm = 2, .src3_vreg = v_idx }; + omega_bw_ir_append(prog, &insn_a3_idx2); + BlackwellIRInsn insn_a3_idx3 = { .op = BW_IR_IMAD, .dst_vreg = v_idx, .src1_vreg = v_const_1, .src2_vreg = -1, .imm = 16, .src3_vreg = v_idx }; + omega_bw_ir_append(prog, &insn_a3_idx3); + BlackwellIRInsn insn_a3_off = { .op = BW_IR_IMAD_WIDE, .dst_vreg = v_off, .src1_vreg = v_idx, .src2_vreg = -1, .imm = 1, .src3_vreg = v_ptr_a }; + omega_bw_ir_append(prog, &insn_a3_off); + BlackwellIRInsn insn_lda3 = { .op = BW_IR_LDG_E, .dst_vreg = v_ra, .dst_subreg = 3, .src1_vreg = v_off, .src2_vreg = -1, .src3_vreg = -1, .ureg = uv_desc, .control = 0x001ea800 }; + omega_bw_ir_append(prog, &insn_lda3); + + /* 8. Load and pack B fragments (16xN row-major, 2 bytes/element) */ + /* b0 at tid_col2 * N + col_b */ + BlackwellIRInsn insn_b0_idx = { .op = BW_IR_IMAD, .dst_vreg = v_idx, .src1_vreg = v_tid_col2, .src2_vreg = -1, .imm = spec->n, .src3_vreg = v_col_b }; + omega_bw_ir_append(prog, &insn_b0_idx); + BlackwellIRInsn insn_b0_off = { .op = BW_IR_IMAD_WIDE, .dst_vreg = v_off, .src1_vreg = v_idx, .src2_vreg = -1, .imm = 2, .src3_vreg = v_ptr_b }; + omega_bw_ir_append(prog, &insn_b0_off); + BlackwellIRInsn insn_ldb0 = { .op = BW_IR_LDG_E_U16, .dst_vreg = v_b_tmp0, .src1_vreg = v_off, .src2_vreg = -1, .src3_vreg = -1, .ureg = uv_desc, .control = 0x002ea800 }; + omega_bw_ir_append(prog, &insn_ldb0); + + /* b1 at (tid_col2 + 1) * N + col_b = b0_idx + N */ + BlackwellIRInsn insn_b1_idx = { .op = BW_IR_IMAD, .dst_vreg = v_idx, .src1_vreg = v_const_1, .src2_vreg = -1, .imm = spec->n, .src3_vreg = v_idx }; + omega_bw_ir_append(prog, &insn_b1_idx); + BlackwellIRInsn insn_b1_off = { .op = BW_IR_IMAD_WIDE, .dst_vreg = v_off, .src1_vreg = v_idx, .src2_vreg = -1, .imm = 2, .src3_vreg = v_ptr_b }; + omega_bw_ir_append(prog, &insn_b1_off); + BlackwellIRInsn insn_ldb1 = { .op = BW_IR_LDG_E_U16, .dst_vreg = v_b_tmp1, .src1_vreg = v_off, .src2_vreg = -1, .src3_vreg = -1, .ureg = uv_desc, .control = 0x002ea800 }; + omega_bw_ir_append(prog, &insn_ldb1); + + /* Pack Rb[0] = (b1 << 16) | b0 (wait on barrier 2) */ + BlackwellIRInsn insn_pack_b0 = { .op = BW_IR_IMAD, .dst_vreg = v_rb, .dst_subreg = 0, .src1_vreg = v_b_tmp1, .src2_vreg = -1, .imm = 0x10000, .src3_vreg = v_b_tmp0, .control = 0x004fca00 }; + omega_bw_ir_append(prog, &insn_pack_b0); + + /* b2 at (tid_col2 + 8) * N + col_b */ + BlackwellIRInsn insn_b2_idx1 = { .op = BW_IR_IMAD, .dst_vreg = v_idx, .src1_vreg = v_tid_col2, .src2_vreg = -1, .imm = spec->n, .src3_vreg = v_col_b }; + omega_bw_ir_append(prog, &insn_b2_idx1); + BlackwellIRInsn insn_b2_idx2 = { .op = BW_IR_IMAD, .dst_vreg = v_idx, .src1_vreg = v_const_1, .src2_vreg = -1, .imm = 8 * spec->n, .src3_vreg = v_idx }; + omega_bw_ir_append(prog, &insn_b2_idx2); + BlackwellIRInsn insn_b2_off = { .op = BW_IR_IMAD_WIDE, .dst_vreg = v_off, .src1_vreg = v_idx, .src2_vreg = -1, .imm = 2, .src3_vreg = v_ptr_b }; + omega_bw_ir_append(prog, &insn_b2_off); + BlackwellIRInsn insn_ldb2 = { .op = BW_IR_LDG_E_U16, .dst_vreg = v_b_tmp0, .src1_vreg = v_off, .src2_vreg = -1, .src3_vreg = -1, .ureg = uv_desc, .control = 0x002ea800 }; + omega_bw_ir_append(prog, &insn_ldb2); + + /* b3 at (tid_col2 + 9) * N + col_b = b2_idx + N */ + BlackwellIRInsn insn_b3_idx = { .op = BW_IR_IMAD, .dst_vreg = v_idx, .src1_vreg = v_const_1, .src2_vreg = -1, .imm = spec->n, .src3_vreg = v_idx }; + omega_bw_ir_append(prog, &insn_b3_idx); + BlackwellIRInsn insn_b3_off = { .op = BW_IR_IMAD_WIDE, .dst_vreg = v_off, .src1_vreg = v_idx, .src2_vreg = -1, .imm = 2, .src3_vreg = v_ptr_b }; + omega_bw_ir_append(prog, &insn_b3_off); + BlackwellIRInsn insn_ldb3 = { .op = BW_IR_LDG_E_U16, .dst_vreg = v_b_tmp1, .src1_vreg = v_off, .src2_vreg = -1, .src3_vreg = -1, .ureg = uv_desc, .control = 0x002ea800 }; + omega_bw_ir_append(prog, &insn_ldb3); + + /* Pack Rb[1] = (b3 << 16) | b2 (wait on barrier 2) */ + BlackwellIRInsn insn_pack_b1 = { .op = BW_IR_IMAD, .dst_vreg = v_rb, .dst_subreg = 1, .src1_vreg = v_b_tmp1, .src2_vreg = -1, .imm = 0x10000, .src3_vreg = v_b_tmp0, .control = 0x004fca00 }; + omega_bw_ir_append(prog, &insn_pack_b1); + + /* 9. Execute HMMA Tensor Core instruction (wait on barrier 1 for A loads) */ + BlackwellIROpcode mma_op = (spec->precision == OMEGA_MATMUL_PRECISION_BF16) ? BW_IR_HMMA_BF16 : BW_IR_HMMA_F16; + BlackwellIRInsn insn_hmma = { + .op = mma_op, + .dst_vreg = v_rd, + .dst_subreg = 0, + .src1_vreg = v_ra, + .src1_subreg = 0, + .src2_vreg = v_rb, + .src2_subreg = 0, + .src3_vreg = -1, /* RZ accumulator input */ + .control = 0x002fca00 /* Wait on barrier 1 */ + }; + omega_bw_ir_append(prog, &insn_hmma); + + /* 10. Store C output fragments (16xN row-major, 4 bytes/float element) */ + /* d0: row r0, col col_base + tid_col2 + 0 */ + BlackwellIRInsn insn_d0_idx1 = { .op = BW_IR_IMAD, .dst_vreg = v_idx, .src1_vreg = v_r0, .src2_vreg = -1, .imm = spec->n, .src3_vreg = v_col_base }; + omega_bw_ir_append(prog, &insn_d0_idx1); + BlackwellIRInsn insn_d0_idx2 = { .op = BW_IR_IADD3, .dst_vreg = v_idx, .src1_vreg = v_idx, .src2_vreg = v_tid_col2, .src3_vreg = -1 }; + omega_bw_ir_append(prog, &insn_d0_idx2); + BlackwellIRInsn insn_d0_off = { .op = BW_IR_IMAD_WIDE, .dst_vreg = v_off, .src1_vreg = v_idx, .src2_vreg = -1, .imm = 4, .src3_vreg = v_ptr_c }; + omega_bw_ir_append(prog, &insn_d0_off); + BlackwellIRInsn insn_std0 = { .op = BW_IR_STG_E, .src1_vreg = v_off, .src2_vreg = v_rd, .src2_subreg = 0, .ureg = uv_desc, .control = 0x000fe200 }; + omega_bw_ir_append(prog, &insn_std0); + + /* d1: row r0, col col_base + tid_col2 + 1 */ + BlackwellIRInsn insn_d1_idx = { .op = BW_IR_IMAD, .dst_vreg = v_idx, .src1_vreg = v_const_1, .src2_vreg = -1, .imm = 1, .src3_vreg = v_idx }; + omega_bw_ir_append(prog, &insn_d1_idx); + BlackwellIRInsn insn_d1_off = { .op = BW_IR_IMAD_WIDE, .dst_vreg = v_off, .src1_vreg = v_idx, .src2_vreg = -1, .imm = 4, .src3_vreg = v_ptr_c }; + omega_bw_ir_append(prog, &insn_d1_off); + BlackwellIRInsn insn_std1 = { .op = BW_IR_STG_E, .src1_vreg = v_off, .src2_vreg = v_rd, .src2_subreg = 1, .ureg = uv_desc, .control = 0x000fe200 }; + omega_bw_ir_append(prog, &insn_std1); + + /* d2: row r1, col col_base + tid_col2 + 0 */ + BlackwellIRInsn insn_d2_idx1 = { .op = BW_IR_IMAD, .dst_vreg = v_idx, .src1_vreg = v_r1, .src2_vreg = -1, .imm = spec->n, .src3_vreg = v_col_base }; + omega_bw_ir_append(prog, &insn_d2_idx1); + BlackwellIRInsn insn_d2_idx2 = { .op = BW_IR_IADD3, .dst_vreg = v_idx, .src1_vreg = v_idx, .src2_vreg = v_tid_col2, .src3_vreg = -1 }; + omega_bw_ir_append(prog, &insn_d2_idx2); + BlackwellIRInsn insn_d2_off = { .op = BW_IR_IMAD_WIDE, .dst_vreg = v_off, .src1_vreg = v_idx, .src2_vreg = -1, .imm = 4, .src3_vreg = v_ptr_c }; + omega_bw_ir_append(prog, &insn_d2_off); + BlackwellIRInsn insn_std2 = { .op = BW_IR_STG_E, .src1_vreg = v_off, .src2_vreg = v_rd, .src2_subreg = 2, .ureg = uv_desc, .control = 0x000fe200 }; + omega_bw_ir_append(prog, &insn_std2); + + /* d3: row r1, col col_base + tid_col2 + 1 */ + BlackwellIRInsn insn_d3_idx = { .op = BW_IR_IMAD, .dst_vreg = v_idx, .src1_vreg = v_const_1, .src2_vreg = -1, .imm = 1, .src3_vreg = v_idx }; + omega_bw_ir_append(prog, &insn_d3_idx); + BlackwellIRInsn insn_d3_off = { .op = BW_IR_IMAD_WIDE, .dst_vreg = v_off, .src1_vreg = v_idx, .src2_vreg = -1, .imm = 4, .src3_vreg = v_ptr_c }; + omega_bw_ir_append(prog, &insn_d3_off); + BlackwellIRInsn insn_std3 = { .op = BW_IR_STG_E, .src1_vreg = v_off, .src2_vreg = v_rd, .src2_subreg = 3, .ureg = uv_desc, .control = 0x000fe200 }; + omega_bw_ir_append(prog, &insn_std3); + + /* 11. Program Exit */ + BlackwellIRInsn insn_exit = { .op = BW_IR_EXIT }; + omega_bw_ir_append(prog, &insn_exit); + BlackwellIRInsn insn_bra = { .op = BW_IR_BRA }; + omega_bw_ir_append(prog, &insn_bra); + + /* 12. Run bounded register allocation */ + if (omega_bw_regalloc_solve(prog) != 0) { + return -1; + } + return 0; +} + +int omega_blackwell_codegen_matmul_tensor(const OmegaMatMulSpec *spec, OmegaBlackwellKernel *kernel) { + if (!spec || !kernel) return -1; + BlackwellIRProgram prog; + if (omega_blackwell_codegen_matmul_tensor_prog(spec, &prog) != 0) { + return -1; + } + + size_t max_bytes = 4096; + kernel->code = malloc(max_bytes); + if (!kernel->code) return -1; + + size_t emitted_size = 0; + if (omega_bw_encode_program(&prog, kernel->code, max_bytes, &emitted_size) != 0) { + free(kernel->code); + kernel->code = NULL; + return -1; + } + + kernel->code_size = emitted_size; + kernel->insn_count = emitted_size / 16; + kernel->gpr_count = prog.regalloc.peak_gpr_usage; + kernel->uniform_gpr_count = prog.regalloc.peak_ugpr_usage; + + sha256_hash(kernel->code, kernel->code_size, kernel->code_digest); + return 0; +} + +/* ---- FB-1 cut 1b: looped tensor matmul ------------------------------------------ + * + * Control words (bundle bits 105..125, decoded from the chip-proven kernel and from + * the vendor compiler's SASS for an equivalent HMMA loop on this chip, 2026-10-04): + * stall = bits 9..12 of w[3], yield = 13, write barrier = 14..16, read barrier = + * 17..19, wait mask = 20..25. 7 in a barrier field means "none". + * LDG (A) 0x001ea800 stall 4, yield, sets SB2 + * LDG.U16 (B) 0x002ea800 stall 4, yield, sets SB2 + * pack IMAD 0x004fca00 stall 5, waits SB2 (every outstanding load has landed) + * HMMA 0x002fca00 stall 5 (fixed latency: the vendor compiler sets no + * write barrier on HMMA either; consumers wait by stall count) + * STG 0x0007e200 stall 1, yield, read barrier SB3 (the store may read its + * data/address registers late; the next writer waits SB3) + * wait SB3 mask bit 23 (0x00800000) on the first instruction that overwrites a + * register a store reads: the C pointer advance. + * Loop-carried registers are kept live to the end of their loop (keep_alive) because + * the allocator is a straight-line linear scan and knows nothing about back edges. + */ +static void bw_keep_alive(BlackwellIRProgram *prog, int from_idx, int to_idx, int last_idx) { + /* every vreg first defined in [from_idx, to_idx) stays live until last_idx */ + for (int v = 0; v < prog->regalloc.num_vregs; v++) { + OmegaLiveInterval *iv = &prog->regalloc.intervals[v]; + if (iv->first_def >= from_idx && iv->first_def < to_idx && iv->last_use < last_idx) + iv->last_use = last_idx; + } +} + +#define BW_APPEND(prog, ...) do { BlackwellIRInsn _i = { __VA_ARGS__ }; if (omega_bw_ir_append((prog), &_i) < 0) return -1; } while (0) + +int omega_blackwell_codegen_matmul_tensor_loop_prog(const OmegaMatMulSpec *spec, uint32_t grid_x, + int mutant, BlackwellIRProgram *prog) { + if (!spec || !prog) return -1; + if (spec->m == 0 || spec->k == 0 || spec->n == 0) return -1; + if (spec->precision != OMEGA_MATMUL_PRECISION_FP16 && spec->precision != OMEGA_MATMUL_PRECISION_BF16) return -1; + if (spec->m % 16 != 0 || spec->n % 8 != 0 || spec->k % 16 != 0) { + fprintf(stderr, "ERROR: looped MatMul tensor codegen requires M%%16, N%%8, K%%16 (got %ux%ux%u)\n", + spec->m, spec->k, spec->n); + return -1; + } + const uint32_t K = spec->k, N = spec->n, NT = N / 8, KSTEPS = K / 16; + if (grid_x == 0 || grid_x > NT) return -1; + /* every immediate below must fit 32 bits: 32 * N (B row-block stride in bytes) is the largest */ + if ((uint64_t)N * 32u > 0xffffffffull || (uint64_t)grid_x * 32u > 0xffffffffull) return -1; + if (mutant && KSTEPS < 2) return -1; + + omega_bw_ir_init(prog); + + int v_ptr_a = omega_bw_ir_alloc_vreg64(prog), v_ptr_b = omega_bw_ir_alloc_vreg64(prog), v_ptr_c = omega_bw_ir_alloc_vreg64(prog); + int uv_desc = omega_bw_ir_alloc_uvreg64(prog); + int v_tid = omega_bw_ir_alloc_vreg(prog), v_ctaid_x = omega_bw_ir_alloc_vreg(prog), v_ctaid_y = omega_bw_ir_alloc_vreg(prog); + int v_group = omega_bw_ir_alloc_vreg(prog), v_tig = omega_bw_ir_alloc_vreg(prog), v_tcol2 = omega_bw_ir_alloc_vreg(prog); + int v_r0 = omega_bw_ir_alloc_vreg(prog), v_r1 = omega_bw_ir_alloc_vreg(prog), v_c1 = omega_bw_ir_alloc_vreg(prog); + int v_idx = omega_bw_ir_alloc_vreg(prog), v_col = omega_bw_ir_alloc_vreg(prog), v_cnt = omega_bw_ir_alloc_vreg(prog); + int v_pa0 = omega_bw_ir_alloc_vreg64(prog), v_pa1 = omega_bw_ir_alloc_vreg64(prog), v_pbcol = omega_bw_ir_alloc_vreg64(prog); + int v_pc0 = omega_bw_ir_alloc_vreg64(prog), v_pc1 = omega_bw_ir_alloc_vreg64(prog); + int v_pa0c = omega_bw_ir_alloc_vreg64(prog), v_pa1c = omega_bw_ir_alloc_vreg64(prog), v_pbc = omega_bw_ir_alloc_vreg64(prog); + int v_ta2 = omega_bw_ir_alloc_vreg64(prog), v_ta3 = omega_bw_ir_alloc_vreg64(prog); + int v_tb1 = omega_bw_ir_alloc_vreg64(prog), v_tb2 = omega_bw_ir_alloc_vreg64(prog), v_tb3 = omega_bw_ir_alloc_vreg64(prog); + int v_tc1 = omega_bw_ir_alloc_vreg64(prog), v_tc3 = omega_bw_ir_alloc_vreg64(prog); + int v_ra = omega_bw_ir_alloc_vreg128(prog), v_rb = omega_bw_ir_alloc_vreg64(prog), v_rd = omega_bw_ir_alloc_vreg128(prog); + int v_b0 = omega_bw_ir_alloc_vreg(prog), v_b1 = omega_bw_ir_alloc_vreg(prog); + if (v_b1 < 0 || uv_desc < 0) return -1; + + const uint32_t CTRL_LDA = 0x001ea800u, CTRL_LDB = 0x002ea800u, CTRL_PACK = 0x004fca00u; + const uint32_t CTRL_HMMA = 0x002fca00u, CTRL_STG_RDBAR3 = 0x0007e200u, CTRL_IMADW_WAIT3 = 0x009fcc00u; + const uint32_t CTRL_MOVRZ_WAIT3 = 0x008fe200u; + + /* prologue: descriptor, pointers, ids */ + BW_APPEND(prog, .op = BW_IR_LDCU64, .dst_vreg = uv_desc, .imm = 0x358, .is_uniform = true); + BW_APPEND(prog, .op = BW_IR_LDC64, .dst_vreg = v_ptr_a, .imm = 0x380); + BW_APPEND(prog, .op = BW_IR_LDC64, .dst_vreg = v_ptr_b, .imm = 0x388); + BW_APPEND(prog, .op = BW_IR_LDC64, .dst_vreg = v_ptr_c, .imm = 0x390); + BW_APPEND(prog, .op = BW_IR_S2R, .dst_vreg = v_tid, .imm = BW_SR_TID_X); + BW_APPEND(prog, .op = BW_IR_S2R, .dst_vreg = v_ctaid_x, .imm = BW_SR_CTAID_X); + BW_APPEND(prog, .op = BW_IR_S2R, .dst_vreg = v_ctaid_y, .imm = BW_SR_CTAID_Y); + BW_APPEND(prog, .op = BW_IR_MOV_IMM, .dst_vreg = v_c1, .imm = 1); + BW_APPEND(prog, .op = BW_IR_SHF_R, .dst_vreg = v_group, .src1_vreg = v_tid, .imm = 2); + BW_APPEND(prog, .op = BW_IR_LOP3_AND, .dst_vreg = v_tig, .src1_vreg = v_tid, .imm = 3); + BW_APPEND(prog, .op = BW_IR_IMAD, .dst_vreg = v_tcol2, .src1_vreg = v_tig, .src2_vreg = -1, .imm = 2, .src3_vreg = -1); + BW_APPEND(prog, .op = BW_IR_IMAD, .dst_vreg = v_r0, .src1_vreg = v_ctaid_y, .src2_vreg = -1, .imm = 16, .src3_vreg = v_group); + BW_APPEND(prog, .op = BW_IR_IMAD, .dst_vreg = v_r1, .src1_vreg = v_c1, .src2_vreg = -1, .imm = 8, .src3_vreg = v_r0); + /* pa0 = A + (r0*K + tcol2)*2 ; pa1 likewise with r1 (A row-major, K bf16 per row) */ + BW_APPEND(prog, .op = BW_IR_IMAD, .dst_vreg = v_idx, .src1_vreg = v_r0, .src2_vreg = -1, .imm = K, .src3_vreg = v_tcol2); + BW_APPEND(prog, .op = BW_IR_IMAD_WIDE, .dst_vreg = v_pa0, .src1_vreg = v_idx, .src2_vreg = -1, .imm = 2, .src3_vreg = v_ptr_a); + BW_APPEND(prog, .op = BW_IR_IMAD, .dst_vreg = v_idx, .src1_vreg = v_r1, .src2_vreg = -1, .imm = K, .src3_vreg = v_tcol2); + BW_APPEND(prog, .op = BW_IR_IMAD_WIDE, .dst_vreg = v_pa1, .src1_vreg = v_idx, .src2_vreg = -1, .imm = 2, .src3_vreg = v_ptr_a); + /* col = ctaid_x (first column tile of this CTA) */ + BW_APPEND(prog, .op = BW_IR_IMAD, .dst_vreg = v_col, .src1_vreg = v_ctaid_x, .src2_vreg = -1, .imm = 1, .src3_vreg = -1); + /* pbcol = B + (tcol2*N + col*8 + group)*2 */ + BW_APPEND(prog, .op = BW_IR_IMAD, .dst_vreg = v_idx, .src1_vreg = v_tcol2, .src2_vreg = -1, .imm = N, .src3_vreg = v_group); + BW_APPEND(prog, .op = BW_IR_IMAD, .dst_vreg = v_idx, .src1_vreg = v_col, .src2_vreg = -1, .imm = 8, .src3_vreg = v_idx); + BW_APPEND(prog, .op = BW_IR_IMAD_WIDE, .dst_vreg = v_pbcol, .src1_vreg = v_idx, .src2_vreg = -1, .imm = 2, .src3_vreg = v_ptr_b); + /* pc0 = C + (r0*N + col*8 + tcol2)*4 ; pc1 with r1 */ + BW_APPEND(prog, .op = BW_IR_IMAD, .dst_vreg = v_idx, .src1_vreg = v_r0, .src2_vreg = -1, .imm = N, .src3_vreg = v_tcol2); + BW_APPEND(prog, .op = BW_IR_IMAD, .dst_vreg = v_idx, .src1_vreg = v_col, .src2_vreg = -1, .imm = 8, .src3_vreg = v_idx); + BW_APPEND(prog, .op = BW_IR_IMAD_WIDE, .dst_vreg = v_pc0, .src1_vreg = v_idx, .src2_vreg = -1, .imm = 4, .src3_vreg = v_ptr_c); + BW_APPEND(prog, .op = BW_IR_IMAD, .dst_vreg = v_idx, .src1_vreg = v_r1, .src2_vreg = -1, .imm = N, .src3_vreg = v_tcol2); + BW_APPEND(prog, .op = BW_IR_IMAD, .dst_vreg = v_idx, .src1_vreg = v_col, .src2_vreg = -1, .imm = 8, .src3_vreg = v_idx); + BW_APPEND(prog, .op = BW_IR_IMAD_WIDE, .dst_vreg = v_pc1, .src1_vreg = v_idx, .src2_vreg = -1, .imm = 4, .src3_vreg = v_ptr_c); + + /* ---- column-tile loop ---- */ + const int L_col = (int)prog->count; + /* accumulator = 0 (the first write waits SB3: the previous tile's stores may still read it) */ + BW_APPEND(prog, .op = BW_IR_MOV_RZ, .dst_vreg = v_rd, .dst_subreg = 0, .control = CTRL_MOVRZ_WAIT3); + BW_APPEND(prog, .op = BW_IR_MOV_RZ, .dst_vreg = v_rd, .dst_subreg = 1); + BW_APPEND(prog, .op = BW_IR_MOV_RZ, .dst_vreg = v_rd, .dst_subreg = 2); + BW_APPEND(prog, .op = BW_IR_MOV_RZ, .dst_vreg = v_rd, .dst_subreg = 3); + /* K cursors start at the row/column bases (IMAD.WIDE with imm 0 is a 64-bit copy) */ + BW_APPEND(prog, .op = BW_IR_IMAD_WIDE, .dst_vreg = v_pa0c, .src1_vreg = v_c1, .src2_vreg = -1, .imm = 0, .src3_vreg = v_pa0); + BW_APPEND(prog, .op = BW_IR_IMAD_WIDE, .dst_vreg = v_pa1c, .src1_vreg = v_c1, .src2_vreg = -1, .imm = 0, .src3_vreg = v_pa1); + BW_APPEND(prog, .op = BW_IR_IMAD_WIDE, .dst_vreg = v_pbc, .src1_vreg = v_c1, .src2_vreg = -1, .imm = 0, .src3_vreg = v_pbcol); + BW_APPEND(prog, .op = BW_IR_MOV_IMM, .dst_vreg = v_cnt, .imm = mutant ? KSTEPS - 1 : KSTEPS); + + /* ---- K loop: one 16x16 A fragment and one 16x8 B fragment per step ---- */ + const int L_k = (int)prog->count; + BW_APPEND(prog, .op = BW_IR_IMAD_WIDE, .dst_vreg = v_ta2, .src1_vreg = v_c1, .src2_vreg = -1, .imm = 16, .src3_vreg = v_pa0c); + BW_APPEND(prog, .op = BW_IR_IMAD_WIDE, .dst_vreg = v_ta3, .src1_vreg = v_c1, .src2_vreg = -1, .imm = 16, .src3_vreg = v_pa1c); + BW_APPEND(prog, .op = BW_IR_IMAD_WIDE, .dst_vreg = v_tb1, .src1_vreg = v_c1, .src2_vreg = -1, .imm = 2u * N, .src3_vreg = v_pbc); + BW_APPEND(prog, .op = BW_IR_IMAD_WIDE, .dst_vreg = v_tb2, .src1_vreg = v_c1, .src2_vreg = -1, .imm = 16u * N, .src3_vreg = v_pbc); + BW_APPEND(prog, .op = BW_IR_IMAD_WIDE, .dst_vreg = v_tb3, .src1_vreg = v_c1, .src2_vreg = -1, .imm = 18u * N, .src3_vreg = v_pbc); + /* A: a0,a1 at (r0, kk+tcol2) ; a2,a3 at (r1, ...) ; a4,a5 at (r0, +8) ; a6,a7 at (r1, +8) */ + BW_APPEND(prog, .op = BW_IR_LDG_E, .dst_vreg = v_ra, .dst_subreg = 0, .src1_vreg = v_pa0c, .src2_vreg = -1, .src3_vreg = -1, .ureg = uv_desc, .control = CTRL_LDA); + BW_APPEND(prog, .op = BW_IR_LDG_E, .dst_vreg = v_ra, .dst_subreg = 1, .src1_vreg = v_pa1c, .src2_vreg = -1, .src3_vreg = -1, .ureg = uv_desc, .control = CTRL_LDA); + BW_APPEND(prog, .op = BW_IR_LDG_E, .dst_vreg = v_ra, .dst_subreg = 2, .src1_vreg = v_ta2, .src2_vreg = -1, .src3_vreg = -1, .ureg = uv_desc, .control = CTRL_LDA); + BW_APPEND(prog, .op = BW_IR_LDG_E, .dst_vreg = v_ra, .dst_subreg = 3, .src1_vreg = v_ta3, .src2_vreg = -1, .src3_vreg = -1, .ureg = uv_desc, .control = CTRL_LDA); + /* B: rows kk+tcol2, +1, +8, +9 at column col*8+group */ + BW_APPEND(prog, .op = BW_IR_LDG_E_U16, .dst_vreg = v_b0, .src1_vreg = v_pbc, .src2_vreg = -1, .src3_vreg = -1, .ureg = uv_desc, .control = CTRL_LDB); + BW_APPEND(prog, .op = BW_IR_LDG_E_U16, .dst_vreg = v_b1, .src1_vreg = v_tb1, .src2_vreg = -1, .src3_vreg = -1, .ureg = uv_desc, .control = CTRL_LDB); + BW_APPEND(prog, .op = BW_IR_IMAD, .dst_vreg = v_rb, .dst_subreg = 0, .src1_vreg = v_b1, .src2_vreg = -1, .imm = 0x10000, .src3_vreg = v_b0, .control = CTRL_PACK); + BW_APPEND(prog, .op = BW_IR_LDG_E_U16, .dst_vreg = v_b0, .src1_vreg = v_tb2, .src2_vreg = -1, .src3_vreg = -1, .ureg = uv_desc, .control = CTRL_LDB); + BW_APPEND(prog, .op = BW_IR_LDG_E_U16, .dst_vreg = v_b1, .src1_vreg = v_tb3, .src2_vreg = -1, .src3_vreg = -1, .ureg = uv_desc, .control = CTRL_LDB); + BW_APPEND(prog, .op = BW_IR_IMAD, .dst_vreg = v_rb, .dst_subreg = 1, .src1_vreg = v_b1, .src2_vreg = -1, .imm = 0x10000, .src3_vreg = v_b0, .control = CTRL_PACK); + /* rd += ra * rb (accumulate in place) */ + BW_APPEND(prog, .op = (spec->precision == OMEGA_MATMUL_PRECISION_BF16) ? BW_IR_HMMA_BF16 : BW_IR_HMMA_F16, + .dst_vreg = v_rd, .dst_subreg = 0, .src1_vreg = v_ra, .src1_subreg = 0, .src2_vreg = v_rb, .src2_subreg = 0, + .src3_vreg = v_rd, .src3_subreg = 0, .control = CTRL_HMMA); + /* advance K: A by 16 bf16 (32 bytes), B by 16 rows (32 N bytes); every load above has landed (packs waited SB2) */ + BW_APPEND(prog, .op = BW_IR_IMAD_WIDE, .dst_vreg = v_pa0c, .src1_vreg = v_c1, .src2_vreg = -1, .imm = 32, .src3_vreg = v_pa0c); + BW_APPEND(prog, .op = BW_IR_IMAD_WIDE, .dst_vreg = v_pa1c, .src1_vreg = v_c1, .src2_vreg = -1, .imm = 32, .src3_vreg = v_pa1c); + BW_APPEND(prog, .op = BW_IR_IMAD_WIDE, .dst_vreg = v_pbc, .src1_vreg = v_c1, .src2_vreg = -1, .imm = 32u * N, .src3_vreg = v_pbc); + BW_APPEND(prog, .op = BW_IR_IMAD, .dst_vreg = v_cnt, .src1_vreg = v_c1, .src2_vreg = -1, .imm = 0xffffffffu, .src3_vreg = v_cnt); + BW_APPEND(prog, .op = BW_IR_ISETP_GE, .dst_vreg = -1, .src1_vreg = v_cnt, .src2_vreg = -1, .imm = 1, .src3_vreg = -1); + { + int here = (int)prog->count; + BW_APPEND(prog, .op = BW_IR_BRA, .imm = (uint32_t)(int32_t)(L_k - here), .predicate_p0 = true); + } + const int L_k_end = (int)prog->count - 1; + + /* ---- store the 16x8 tile: d0 (r0, c), d1 (r0, c+1), d2 (r1, c), d3 (r1, c+1) ---- */ + BW_APPEND(prog, .op = BW_IR_IMAD_WIDE, .dst_vreg = v_tc1, .src1_vreg = v_c1, .src2_vreg = -1, .imm = 4, .src3_vreg = v_pc0); + BW_APPEND(prog, .op = BW_IR_IMAD_WIDE, .dst_vreg = v_tc3, .src1_vreg = v_c1, .src2_vreg = -1, .imm = 4, .src3_vreg = v_pc1); + BW_APPEND(prog, .op = BW_IR_STG_E, .src1_vreg = v_pc0, .src2_vreg = v_rd, .src2_subreg = 0, .src3_vreg = -1, .ureg = uv_desc, .control = CTRL_STG_RDBAR3); + BW_APPEND(prog, .op = BW_IR_STG_E, .src1_vreg = v_tc1, .src2_vreg = v_rd, .src2_subreg = 1, .src3_vreg = -1, .ureg = uv_desc, .control = CTRL_STG_RDBAR3); + BW_APPEND(prog, .op = BW_IR_STG_E, .src1_vreg = v_pc1, .src2_vreg = v_rd, .src2_subreg = 2, .src3_vreg = -1, .ureg = uv_desc, .control = CTRL_STG_RDBAR3); + BW_APPEND(prog, .op = BW_IR_STG_E, .src1_vreg = v_tc3, .src2_vreg = v_rd, .src2_subreg = 3, .src3_vreg = -1, .ureg = uv_desc, .control = CTRL_STG_RDBAR3); + /* next column tile of this CTA: col += grid_x ; B by grid_x*8 bf16 ; C by grid_x*8 f32 (waits SB3 first) */ + BW_APPEND(prog, .op = BW_IR_IMAD, .dst_vreg = v_col, .src1_vreg = v_c1, .src2_vreg = -1, .imm = grid_x, .src3_vreg = v_col); + BW_APPEND(prog, .op = BW_IR_IMAD_WIDE, .dst_vreg = v_pc0, .src1_vreg = v_c1, .src2_vreg = -1, .imm = grid_x * 32u, .src3_vreg = v_pc0, .control = CTRL_IMADW_WAIT3); + BW_APPEND(prog, .op = BW_IR_IMAD_WIDE, .dst_vreg = v_pc1, .src1_vreg = v_c1, .src2_vreg = -1, .imm = grid_x * 32u, .src3_vreg = v_pc1); + BW_APPEND(prog, .op = BW_IR_IMAD_WIDE, .dst_vreg = v_pbcol, .src1_vreg = v_c1, .src2_vreg = -1, .imm = grid_x * 16u, .src3_vreg = v_pbcol); + BW_APPEND(prog, .op = BW_IR_ISETP_GE, .dst_vreg = -1, .src1_vreg = v_col, .src2_vreg = -1, .imm = NT, .src3_vreg = -1); + { + int here = (int)prog->count; + BW_APPEND(prog, .op = BW_IR_BRA, .imm = (uint32_t)(int32_t)(L_col - here), .predicate_p0 = true, .predicate_not = true); + } + const int L_col_end = (int)prog->count - 1; + BW_APPEND(prog, .op = BW_IR_EXIT); + BW_APPEND(prog, .op = BW_IR_BRA); + + /* loop-carried values live to the end of their loop */ + bw_keep_alive(prog, 0, L_col, L_col_end); + bw_keep_alive(prog, L_col, L_k, L_k_end); + /* K-loop temporaries too: a load's address pair must never be recycled by a + * later instruction of the same iteration (the load may read it late). */ + bw_keep_alive(prog, L_k, L_k_end, L_k_end); + /* the accumulator is read by the stores after the K loop */ + if (prog->regalloc.intervals[v_rd].last_use < L_col_end) prog->regalloc.intervals[v_rd].last_use = L_col_end; + + return omega_bw_regalloc_solve(prog); +} + +int omega_blackwell_codegen_matmul_tensor_loop(const OmegaMatMulSpec *spec, uint32_t grid_x, + int mutant, OmegaBlackwellKernel *kernel) { + if (!spec || !kernel) return -1; + BlackwellIRProgram prog; + if (omega_blackwell_codegen_matmul_tensor_loop_prog(spec, grid_x, mutant, &prog) != 0) return -1; + size_t max_bytes = 4096; + kernel->code = malloc(max_bytes); + if (!kernel->code) return -1; + size_t emitted_size = 0; + if (omega_bw_encode_program(&prog, kernel->code, max_bytes, &emitted_size) != 0) { + free(kernel->code); + kernel->code = NULL; + return -1; + } + kernel->code_size = emitted_size; + kernel->insn_count = emitted_size / 16; + kernel->gpr_count = prog.regalloc.peak_gpr_usage; + kernel->uniform_gpr_count = prog.regalloc.peak_ugpr_usage; + sha256_hash(kernel->code, kernel->code_size, kernel->code_digest); + return 0; +} + +int omega_blackwell_verify_codegen_fixtures(void) { + OmegaRegAlloc ra; + memset(&ra, 0, sizeof(ra)); + ra.vreg_to_phys[0] = 2; + ra.vreg_to_phys[1] = 4; + ra.vreg_to_phys[2] = 6; + ra.vreg_to_phys[3] = 7; + ra.uvreg_to_phys[0] = 4; + + uint32_t w[4]; + + /* 1. MOV R7, RZ */ + BlackwellIRInsn insn_mov_rz = { .op = BW_IR_MOV_RZ, .dst_vreg = 3 }; + if (encode_single_insn(&insn_mov_rz, &ra, w) != 0) return -1; + if (w[0] != (0x7202 | (7 << 16)) || w[1] != 0xff || w[2] != 0x00000f00) return -2; + + /* 2. MOV R2, 1 */ + BlackwellIRInsn insn_mov_imm = { .op = BW_IR_MOV_IMM, .dst_vreg = 0, .imm = 1 }; + if (encode_single_insn(&insn_mov_imm, &ra, w) != 0) return -3; + if (w[0] != (0x7802 | (2 << 16)) || w[1] != 1 || w[2] != 0x00000f00) return -4; + + /* 3. Register-Register IMAD: IMAD R7, R2, R6, R7 */ + BlackwellIRInsn insn_imad_rr = { .op = BW_IR_IMAD, .dst_vreg = 3, .src1_vreg = 0, .src2_vreg = 2, .src3_vreg = 3 }; + if (encode_single_insn(&insn_imad_rr, &ra, w) != 0) return -5; + if (w[0] != (0x7224 | (7 << 16) | (2 << 24)) || w[1] != 6 || w[2] != (0x078e0200 | 7)) return -6; + + /* 4. Immediate IMAD: IMAD R6, R2, 16, RZ */ + BlackwellIRInsn insn_imad_imm = { .op = BW_IR_IMAD, .dst_vreg = 2, .src1_vreg = 0, .src2_vreg = -1, .imm = 16, .src3_vreg = -1 }; + if (encode_single_insn(&insn_imad_imm, &ra, w) != 0) return -7; + if (w[0] != (0x7824 | (6 << 16) | (2 << 24)) || w[1] != 16 || w[2] != 0x078e02ff) return -8; + + /* 5. IMAD.WIDE: IMAD.WIDE R4, R2, 4, R4 */ + BlackwellIRInsn insn_imad_wide = { .op = BW_IR_IMAD_WIDE, .dst_vreg = 1, .src1_vreg = 0, .imm = 4, .src3_vreg = 1 }; + if (encode_single_insn(&insn_imad_wide, &ra, w) != 0) return -9; + if (w[0] != (0x7825 | (4 << 16) | (2 << 24)) || w[1] != 4 || w[2] != (0x078e0000 | 4)) return -10; + + /* 6. LDC.64 R4, c[0x0][0x380] */ + BlackwellIRInsn insn_ldc64 = { .op = BW_IR_LDC64, .dst_vreg = 1, .imm = 0x380 }; + if (encode_single_insn(&insn_ldc64, &ra, w) != 0) return -11; + if (w[0] != (0xff007b82 | (4 << 16)) || w[1] != ((0x380 << 6) & 0x00ffff00) || w[2] != 0x00000a00) return -12; + + /* 7. LDCU.64 UR4, c[0x0][0x358] */ + BlackwellIRInsn insn_ldcu64 = { .op = BW_IR_LDCU64, .dst_vreg = 0, .imm = 0x358, .is_uniform = true }; + if (encode_single_insn(&insn_ldcu64, &ra, w) != 0) return -13; + if (w[0] != (0xff0077ac | (4 << 16)) || w[1] != ((0x358 << 5) & 0x00ffff00) || w[2] != 0x08000a00) return -14; + + /* 8. LDG.E R2, desc[UR4][R4.64] */ + BlackwellIRInsn insn_ldg = { .op = BW_IR_LDG_E, .dst_vreg = 0, .src1_vreg = 1, .ureg = 0 }; + if (encode_single_insn(&insn_ldg, &ra, w) != 0) return -15; + if (w[0] != (0x7981 | (2 << 16) | (4 << 24)) || w[1] != 4 || w[2] != 0x0c1e1900) return -16; + + /* 9. STG.E desc[UR4][R4.64], R7 */ + BlackwellIRInsn insn_stg = { .op = BW_IR_STG_E, .src1_vreg = 1, .src2_vreg = 3, .ureg = 0 }; + if (encode_single_insn(&insn_stg, &ra, w) != 0) return -17; + if (w[0] != (0x7986 | (4 << 24)) || w[1] != 7 || w[2] != (0x0c101900 | 4)) return -18; + + /* 10. S2R R2, SR_CTAID.X */ + BlackwellIRInsn insn_s2r = { .op = BW_IR_S2R, .dst_vreg = 0, .imm = BW_SR_CTAID_X }; + if (encode_single_insn(&insn_s2r, &ra, w) != 0) return -19; + if (w[0] != (0x7919 | (2 << 16)) || w[2] != (BW_SR_CTAID_X << 8)) return -20; + + /* 11. HMMA.16816.F32 R4, R4, R2, RZ */ + BlackwellIRInsn insn_hmma_f16 = { .op = BW_IR_HMMA_F16, .dst_vreg = 1, .src1_vreg = 1, .src2_vreg = 0, .src3_vreg = -1 }; + if (encode_single_insn(&insn_hmma_f16, &ra, w) != 0) return -21; + if (w[0] != 0x0404723c || w[1] != 0x00000002 || w[2] != 0x000018ff || w[3] != 0x000fe200) return -22; + + /* 12. HMMA.16816.F32.BF16 R4, R4, R2, RZ */ + BlackwellIRInsn insn_hmma_bf16 = { .op = BW_IR_HMMA_BF16, .dst_vreg = 1, .src1_vreg = 1, .src2_vreg = 0, .src3_vreg = -1 }; + uint32_t w_bf16[4]; + if (encode_single_insn(&insn_hmma_bf16, &ra, w_bf16) != 0) return -23; + if (w_bf16[0] != 0x0404723c || w_bf16[1] != 0x00000002 || w_bf16[2] != 0x000418ff || w_bf16[3] != 0x000fe200) return -24; + + /* 13. Differential Invariant: w[2] bit 18 toggles strictly between FP16 and BF16 */ + if ((w[2] ^ w_bf16[2]) != 0x00040000) return -25; + if (w[0] != w_bf16[0] || w[1] != w_bf16[1] || w[3] != w_bf16[3]) return -26; + + /* 14. SHF.R.U32.HI R4, RZ, 2, R2 */ + BlackwellIRInsn insn_shf = { .op = BW_IR_SHF_R, .dst_vreg = 1, .src1_vreg = 0, .imm = 2 }; + if (encode_single_insn(&insn_shf, &ra, w) != 0) return -27; + if (w[0] != 0xff047819 || w[1] != 2 || w[2] != 0x00011602 || w[3] != 0x001fca00) return -28; + + /* 15. LOP3.LUT R4, R2, 3, RZ, 0xc0, !PT */ + BlackwellIRInsn insn_lop3 = { .op = BW_IR_LOP3_AND, .dst_vreg = 1, .src1_vreg = 0, .imm = 3 }; + if (encode_single_insn(&insn_lop3, &ra, w) != 0) return -29; + if (w[0] != 0x02047812 || w[1] != 3 || w[2] != 0x078ec0ff || w[3] != 0x001fca00) return -30; + + /* 16. LDG.E.U16 R2, desc[UR4][R4.64] */ + BlackwellIRInsn insn_ldg16 = { .op = BW_IR_LDG_E_U16, .dst_vreg = 0, .src1_vreg = 1, .ureg = 0 }; + if (encode_single_insn(&insn_ldg16, &ra, w) != 0) return -31; + if (w[0] != 0x04027981 || w[1] != 4 || w[2] != 0x0c1e1500 || w[3] != 0x000f2200) return -32; + + /* A zero-delta branch stays the self-branch the matmul kernels already emit. */ + BlackwellIRInsn insn_bra0 = { .op = BW_IR_BRA }; + if (encode_single_insn(&insn_bra0, &ra, w) != 0) return -33; + if (w[0] != 0x00fc7947 || w[1] != 0xfffffffc || w[2] != 0x0383ffff || w[3] != 0x000fc000) return -34; + + BlackwellIRInsn insn_ge = { .op = BW_IR_ISETP_GE_U32, .src1_vreg = 1, .src2_vreg = 2 }; + if (encode_single_insn(&insn_ge, &ra, w) != 0) return -35; + if (w[0] != (0x720c | (4 << 24)) || w[1] != 6 || w[2] != 0x03f06070) return -36; + + BlackwellIRInsn insn_xor = { .op = BW_IR_LOP3_XOR, .dst_vreg = 1, .src1_vreg = 0, .src2_vreg = 2 }; + if (encode_single_insn(&insn_xor, &ra, w) != 0) return -37; + if (w[0] != (0x7212 | (4 << 16) | (2 << 24)) || w[1] != 6 || w[2] != 0x078e3cff) return -38; + + BlackwellIRInsn insn_ldg_ss = { .op = BW_IR_LDG_STRONG_SYS, .dst_vreg = 0, .src1_vreg = 1, .ureg = 0 }; + if (encode_single_insn(&insn_ldg_ss, &ra, w) != 0) return -39; + if (w[0] != (0x7981 | (2 << 16) | (4 << 24)) || w[1] != 4 || w[2] != 0x0c1f5900) return -40; + /* 17. FADD R7, R2, R4 */ + BlackwellIRInsn insn_fadd = { .op = BW_IR_FADD, .dst_vreg = 3, .src1_vreg = 0, .src2_vreg = 1 }; + if (encode_single_insn(&insn_fadd, &ra, w) != 0) return -133; + if (w[0] != 0x02077221 || w[1] != 4 || w[2] != 0 || w[3] != 0x004fc400) return -134; + + /* 18. FSUB R7, R2, R4 (negated src2) */ + BlackwellIRInsn insn_fsub = { .op = BW_IR_FSUB, .dst_vreg = 3, .src1_vreg = 0, .src2_vreg = 1 }; + if (encode_single_insn(&insn_fsub, &ra, w) != 0) return -135; + if (w[0] != 0x02077221 || w[1] != 0x80000004 || w[2] != 0 || w[3] != 0x004fc400) return -136; + + /* 19. FMUL R7, R2, R4 */ + BlackwellIRInsn insn_fmul = { .op = BW_IR_FMUL, .dst_vreg = 3, .src1_vreg = 0, .src2_vreg = 1 }; + if (encode_single_insn(&insn_fmul, &ra, w) != 0) return -137; + if (w[0] != 0x02077220 || w[1] != 4 || w[2] != 0x00400000 || w[3] != 0x004fc400) return -138; + + /* 20. FFMA R7, R2, R4, R6 */ + BlackwellIRInsn insn_ffma = { .op = BW_IR_FFMA, .dst_vreg = 3, .src1_vreg = 0, .src2_vreg = 1, .src3_vreg = 2 }; + if (encode_single_insn(&insn_ffma, &ra, w) != 0) return -139; + if (w[0] != 0x02077223 || w[1] != 4 || w[2] != 6 || w[3] != 0x004fc400) return -140; + + /* 21. FMNMX_MIN R7, R2, R4 */ + BlackwellIRInsn insn_min = { .op = BW_IR_FMNMX_MIN, .dst_vreg = 3, .src1_vreg = 0, .src2_vreg = 1 }; + if (encode_single_insn(&insn_min, &ra, w) != 0) return -141; + if (w[0] != 0x02077209 || w[1] != 4 || w[2] != 0x03800000 || w[3] != 0x004fc400) return -142; + + /* 22. FMNMX_MAX R7, R2, R4 */ + BlackwellIRInsn insn_max = { .op = BW_IR_FMNMX_MAX, .dst_vreg = 3, .src1_vreg = 0, .src2_vreg = 1 }; + if (encode_single_insn(&insn_max, &ra, w) != 0) return -143; + if (w[0] != 0x02077209 || w[1] != 4 || w[2] != 0x07800000 || w[3] != 0x004fc400) return -144; + + /* 23. I2FP R7, R2 */ + BlackwellIRInsn insn_i2f = { .op = BW_IR_I2FP, .dst_vreg = 3, .src1_vreg = 0 }; + if (encode_single_insn(&insn_i2f, &ra, w) != 0) return -145; + if (w[0] != 0x00077245 || w[1] != 2 || w[2] != 0x00201400 || w[3] != 0x004fe200) return -146; + + /* 24. F2I R7, R2 */ + BlackwellIRInsn insn_f2i = { .op = BW_IR_F2I, .dst_vreg = 3, .src1_vreg = 0 }; + if (encode_single_insn(&insn_f2i, &ra, w) != 0) return -147; + if (w[0] != 0x00077305 || w[1] != 2 || w[2] != 0x0020f100 || w[3] != 0x004e2200) return -148; + + /* 25. MUFU_RCP R7, R2 */ + BlackwellIRInsn insn_mufu_rcp = { .op = BW_IR_MUFU_RCP, .dst_vreg = 3, .src1_vreg = 0 }; + if (encode_single_insn(&insn_mufu_rcp, &ra, w) != 0) return -149; + if (w[0] != 0x00077308 || w[1] != 2 || w[2] != 0x00001000 || w[3] != 0x000e2400) return -150; + + /* 26. MUFU_RSQ R7, R2 */ + BlackwellIRInsn insn_mufu_rsq = { .op = BW_IR_MUFU_RSQ, .dst_vreg = 3, .src1_vreg = 0 }; + if (encode_single_insn(&insn_mufu_rsq, &ra, w) != 0) return -151; + if (w[0] != 0x00077308 || w[1] != 2 || w[2] != 0x00001400 || w[3] != 0x000e2400) return -152; + + /* 27. SHFL_DOWN R7, R2, 16 */ + BlackwellIRInsn insn_shfl = { .op = BW_IR_SHFL_DOWN, .dst_vreg = 3, .src1_vreg = 0, .imm = 16 }; + if (encode_single_insn(&insn_shfl, &ra, w) != 0) return -153; + if (w[0] != 0x02077f89 || w[1] != 0x0a001f00 || w[2] != 0x000e0000 || w[3] != 0x000e2400) return -154; + + /* 28. LDS R7, [R2] */ + BlackwellIRInsn insn_lds = { .op = BW_IR_LDS, .dst_vreg = 3, .src1_vreg = 0, .imm = 0 }; + if (encode_single_insn(&insn_lds, &ra, w) != 0) return -155; + if (w[0] != 0x02077984 || w[1] != 0 || w[2] != 0 || w[3] != 0x000fe200) return -156; + + /* 29. STS [R2], R4 */ + BlackwellIRInsn insn_sts = { .op = BW_IR_STS, .src1_vreg = 0, .src2_vreg = 1, .imm = 0 }; + if (encode_single_insn(&insn_sts, &ra, w) != 0) return -157; + if (w[0] != 0x02007388 || w[1] != 4 || w[2] != 0 || w[3] != 0x000fe200) return -158; + + return 0; +} + +int omega_blackwell_test_regalloc_bounds(void) { + BlackwellIRProgram prog; + omega_bw_ir_init(&prog); + + int v0 = omega_bw_ir_alloc_vreg64(&prog); + int v1 = omega_bw_ir_alloc_vreg(&prog); + int v2 = omega_bw_ir_alloc_vreg64(&prog); + int v3 = omega_bw_ir_alloc_vreg128(&prog); /* Test 128-bit quad allocation */ + + BlackwellIRInsn i0 = { .op = BW_IR_LDC64, .dst_vreg = v0, .imm = 0x380 }; + omega_bw_ir_append(&prog, &i0); + BlackwellIRInsn i1 = { .op = BW_IR_MOV_IMM, .dst_vreg = v1, .imm = 10 }; + omega_bw_ir_append(&prog, &i1); + BlackwellIRInsn i2 = { .op = BW_IR_LDC64, .dst_vreg = v2, .imm = 0x388 }; + omega_bw_ir_append(&prog, &i2); + BlackwellIRInsn i3 = { .op = BW_IR_HMMA_F16, .dst_vreg = v3, .src1_vreg = v3, .src2_vreg = v0, .src3_vreg = -1 }; + omega_bw_ir_append(&prog, &i3); + /* i4 uses v1, v2, v3 simultaneously so all intervals are concurrently active */ + int v_sink = omega_bw_ir_alloc_vreg(&prog); + BlackwellIRInsn i4 = { .op = BW_IR_IADD3, .dst_vreg = v_sink, .src1_vreg = v1, .src2_vreg = v2, .src3_vreg = v3 }; + omega_bw_ir_append(&prog, &i4); + + if (omega_bw_regalloc_solve(&prog) != 0) return -1; + + int phys_v0 = prog.regalloc.vreg_to_phys[v0]; + int phys_v1 = prog.regalloc.vreg_to_phys[v1]; + int phys_v2 = prog.regalloc.vreg_to_phys[v2]; + int phys_v3 = prog.regalloc.vreg_to_phys[v3]; + + if (phys_v0 % 2 != 0) return -2; + if (phys_v2 % 2 != 0) return -3; + if (phys_v3 % 4 != 0) return -4; /* 4-alignment for 128-bit quad */ + if (phys_v1 == phys_v0 || phys_v1 == phys_v0 + 1) return -5; + if (phys_v2 == phys_v0 || phys_v2 == phys_v0 + 1) return -6; + for (int k = 0; k < 4; k++) { + if (phys_v3 + k == phys_v0 || phys_v3 + k == phys_v0 + 1) return -7; + if (phys_v3 + k == phys_v2 || phys_v3 + k == phys_v2 + 1) return -8; + if (phys_v3 + k == phys_v1) return -9; + } + + /* Test bounds refusal: intentionally attempt to allocate more registers than hardware capacity */ + BlackwellIRProgram overflow_prog; + omega_bw_ir_init(&overflow_prog); + for (int i = 0; i < BW_PHYS_GPR_MAX + 5; i++) { + int v = omega_bw_ir_alloc_vreg(&overflow_prog); + if (v < 0) break; + BlackwellIRInsn insn = { .op = BW_IR_MOV_IMM, .dst_vreg = v, .imm = (uint32_t)i }; + omega_bw_ir_append(&overflow_prog, &insn); + } + for (size_t i = 0; i < overflow_prog.count; i++) { + for (int v = 0; v < overflow_prog.regalloc.num_vregs; v++) { + overflow_prog.regalloc.intervals[v].last_use = (int)overflow_prog.count - 1; + } + } + if (omega_bw_regalloc_solve(&overflow_prog) == 0) { + return -6; + } + + return 0; +} + +int omega_blackwell_test_codegen_variation(void) { + OmegaMatMulSpec spec1, spec2; + if (omega_matmul_spec_init(&spec1, 16, 16, 16, OMEGA_MATMUL_PRECISION_INT32) != 0) { + printf("FAIL: spec1 init failed\n"); + return -1; + } + if (omega_matmul_spec_init(&spec2, 32, 16, 64, OMEGA_MATMUL_PRECISION_INT32) != 0) { + printf("FAIL: spec2 init failed\n"); + return -1; + } + + OmegaBlackwellKernel k1, k2; + memset(&k1, 0, sizeof(k1)); + memset(&k2, 0, sizeof(k2)); + + if (omega_blackwell_codegen_matmul(&spec1, &k1) != 0) { + printf("FAIL: k1 codegen failed\n"); + return -1; + } + if (omega_blackwell_codegen_matmul(&spec2, &k2) != 0) { + printf("FAIL: k2 codegen failed\n"); + omega_blackwell_kernel_free(&k1); + return -1; + } + + OmegaBlackwellRealizationIdentity id1, id2; + omega_blackwell_bind_matmul_realization(&spec1, &k1, &id1); + omega_blackwell_bind_matmul_realization(&spec2, &k2, &id2); + + printf("================================================================================\n"); + printf(" OMEGA BLACKWELL CODEGEN VARIATION DEMONSTRATION (GATE 7 STAGE-1)\n"); + printf("================================================================================\n"); + printf("Config 1: 16x16x16 INT32\n"); + printf(" Instructions: %zu (%zu bytes, %u GPRs, %u UGPRs)\n", k1.insn_count, k1.code_size, k1.gpr_count, k1.uniform_gpr_count); + printf(" Spec ID: "); + for (int i = 0; i < 16; i++) printf("%02x", spec1.spec_id[i]); + printf("...\n"); + printf(" Code Digest: "); + for (int i = 0; i < 16; i++) printf("%02x", k1.code_digest[i]); + printf("...\n"); + printf(" Realization: "); + for (int i = 0; i < 16; i++) printf("%02x", id1.realization_id[i]); + printf("...\n\n"); + + printf("Config 2: 32x16x64 INT32\n"); + printf(" Instructions: %zu (%zu bytes, %u GPRs, %u UGPRs)\n", k2.insn_count, k2.code_size, k2.gpr_count, k2.uniform_gpr_count); + printf(" Spec ID: "); + for (int i = 0; i < 16; i++) printf("%02x", spec2.spec_id[i]); + printf("...\n"); + printf(" Code Digest: "); + for (int i = 0; i < 16; i++) printf("%02x", k2.code_digest[i]); + printf("...\n"); + printf(" Realization: "); + for (int i = 0; i < 16; i++) printf("%02x", id2.realization_id[i]); + printf("...\n"); + printf("================================================================================\n"); + + /* Validation checks */ + if (memcmp(spec1.spec_id, spec2.spec_id, 32) == 0) { + printf("FAIL: spec IDs match (expected distinct)\n"); + return -2; + } + if (memcmp(k1.code_digest, k2.code_digest, 32) == 0) { + printf("FAIL: code digests match (expected distinct)\n"); + return -3; + } + if (memcmp(id1.realization_id, id2.realization_id, 32) == 0) { + printf("FAIL: realization IDs match (expected distinct)\n"); + return -4; + } + + printf("PASS: Dynamic codegen variation verified (distinct specifications, code digests, and realization IDs).\n"); + + omega_blackwell_kernel_free(&k1); + omega_blackwell_kernel_free(&k2); + return 0; +} diff --git a/evidence/gpu-reconv-308/T1a-GPU-RECONV-0a1812f/omega_bw_reconv.h b/evidence/gpu-reconv-308/T1a-GPU-RECONV-0a1812f/omega_bw_reconv.h new file mode 100644 index 000000000..d432e5763 --- /dev/null +++ b/evidence/gpu-reconv-308/T1a-GPU-RECONV-0a1812f/omega_bw_reconv.h @@ -0,0 +1,83 @@ +#ifndef OMEGA_BW_RECONV_H +#define OMEGA_BW_RECONV_H +/* Structured warp reconvergence regions for the Blackwell IR emitters (omega #308). + * + * A region is the span between a BSSY (arm barrier Bn, name the join point) and the + * matching BSYNC (wait for every armed lane, continue as one warp). Inside a region the + * lanes of a warp may take different paths: a predicated forward branch, a loop whose + * trip count differs per lane, a break. Every path that leaves the region jumps to the + * BSYNC itself (that is where nvcc 13.0.88 sends loop exits on sm_121); the BSSY names the + * instruction after the BSYNC. Regions nest; the innermost open region owns Bn with + * n = depth, so nesting depth is bounded by the barrier register count and begin fails + * closed (returns -1) when that bound is reached. + * + * This header holds the bookkeeping only (indices, patching, bounds). Each emitter wraps + * it with its own control-word scheme: see region_begin / region_exit / region_join in + * src/omega_gpu_elementwise_api.c and src/omega_gpu_attention_api.c. + * + * Reference: Mesa NAK nak_nir_lower_cf.c (push_scope / pop_scope: bar_set_nv before a + * divergent if or loop, bar_sync_nv at its merge block, breaks jump to the sync) and the + * nvcc oracle listing in docs/gpu-reconvergence-308.md. */ + +#include "omega_blackwell_codegen.h" + +#define BW_RECONV_MAX_DEPTH BW_RECONV_MAX_BAR +#define BW_RECONV_MAX_EXITS 8 /* forward exits (breaks) recorded per region */ + +typedef struct { + int depth; /* open regions */ + int peak; /* deepest nesting seen (reported in receipts) */ + int opened; /* regions begun (for receipts) */ + int bssy_at[BW_RECONV_MAX_DEPTH]; + int exits[BW_RECONV_MAX_DEPTH][BW_RECONV_MAX_EXITS]; + int n_exits[BW_RECONV_MAX_DEPTH]; +} BwRegions; + +/* Called after the emitter appended the BSSY at index `bssy_index`. Returns the barrier + * register to use, or -1 when the nesting bound is reached (the caller must fail the + * build: no code with a missing barrier may be emitted). The caller allocates the + * barrier BEFORE emitting: use bw_regions_next_bar to pick it, then bw_regions_begin. */ +static inline int bw_regions_next_bar(const BwRegions *r) { + return r->depth < BW_RECONV_MAX_DEPTH ? r->depth : -1; +} +static inline int bw_regions_begin(BwRegions *r, int bssy_index) { + if (r->depth >= BW_RECONV_MAX_DEPTH || bssy_index < 0) return -1; + r->bssy_at[r->depth] = bssy_index; + r->n_exits[r->depth] = 0; + r->depth++; + r->opened++; + if (r->depth > r->peak) r->peak = r->depth; + return r->depth - 1; +} + +/* Records a forward branch (already appended at `bra_index`) that leaves the innermost + * open region; its target is patched to the BSYNC by bw_regions_join. -1 when no region + * is open or the per-region exit table is full. */ +static inline int bw_regions_exit(BwRegions *r, int bra_index) { + if (r->depth <= 0 || bra_index < 0) return -1; + int d = r->depth - 1; + if (r->n_exits[d] >= BW_RECONV_MAX_EXITS) return -1; + r->exits[d][r->n_exits[d]++] = bra_index; + return 0; +} + +/* Called after the emitter appended the BSYNC at index `bsync_index` for the innermost + * open region. Patches the BSSY to name the instruction after the BSYNC and every + * recorded exit to jump to the BSYNC. Returns the barrier register closed, or -1 when + * no region is open or an index is inconsistent (BSSY must precede the BSYNC). */ +static inline int bw_regions_join(BwRegions *r, BlackwellIRProgram *p, int bsync_index) { + if (r->depth <= 0 || !p || bsync_index < 0 || (size_t)bsync_index >= p->count) return -1; + int d = --r->depth; + int at = r->bssy_at[d]; + if (at < 0 || at >= bsync_index || p->insns[at].op != BW_IR_BSSY || p->insns[bsync_index].op != BW_IR_BSYNC) return -1; + if (p->insns[at].bar_reg != (uint8_t)d || p->insns[bsync_index].bar_reg != (uint8_t)d) return -1; + p->insns[at].imm = (uint32_t)(bsync_index + 1 - at); + for (int i = 0; i < r->n_exits[d]; i++) { + int b = r->exits[d][i]; + if (b <= at || b >= bsync_index || p->insns[b].op != BW_IR_BRA) return -1; + p->insns[b].imm = (uint32_t)(bsync_index - b); + } + return d; +} + +#endif /* OMEGA_BW_RECONV_H */ diff --git a/evidence/gpu-reconv-308/T1a-GPU-RECONV-0a1812f/omega_gpu_elementwise_api.c b/evidence/gpu-reconv-308/T1a-GPU-RECONV-0a1812f/omega_gpu_elementwise_api.c new file mode 100644 index 000000000..b16c70205 --- /dev/null +++ b/evidence/gpu-reconv-308/T1a-GPU-RECONV-0a1812f/omega_gpu_elementwise_api.c @@ -0,0 +1,1139 @@ +/* omega_gpu_elementwise_api.c: FB-1 cut 4 (2026-10-04). See the header. + * + * Layout of this file: (1) kernel codegen on top of the BlackwellIR, with a + * small scheduler that writes every control word; (2) one launcher cloned from + * the chip-proven tensor matmul path (omega_blackwell_execute_matmul_tensor); + * (3) the public entry points, which split work into <= OMEGA_GPU_EW_MAX_CTAS + * launches and run the host-side bookkeeping. + * + * Scheduling rules (chip-learned, src/omega_numeric.c omega_numeric_check_patch): + * - a variable-latency result (LDG, LDS, LDC, LDCU, S2R, SHFL, MUFU) arrives through + * a scoreboard; the producer stalls >= 2 and the consumer waits on that SB; + * - a fixed-latency result (FADD FMUL FFMA IMAD IADD3 MOV LOP3 SHF) has no + * scoreboard: the producer stalls >= 5 before its consumer (we use 6); + * - a predicate producer (ISETP) stalls 14 before @P0 use (NAK SM120 padding); + * - stores (STG, STS) set a read barrier so the next instruction cannot overwrite + * the source register early. + * The emitter below serialises: every instruction waits on everything still + * pending. A final local pass tightens supported adjacent ALU pairs to 5 cycles. + */ +#include "omega_gpu_elementwise_api.h" +#include "omega_blackwell_codegen.h" +#include "omega_bw_reconv.h" +#include "omega_blackwell_qmd.h" +#include "omega_blackwell_submit.h" +#include "omega_gpu_session.h" +#include "sha256.h" +#include +#include +#include +#include + +/* ------------------------------------------------------------------ codegen */ + +#define CW(stall, wbar, rbar, wait) \ + (((uint32_t)(stall) << 9) | ((uint32_t)(wbar) << 14) | ((uint32_t)(rbar) << 17) | ((uint32_t)(wait) << 20)) +#define CW_YIELD 0x2000u +#define SB_NONE 7u +#define SB_GPR 0u /* variable-latency GPR results */ +#define SB_RD 1u /* store source-register read barrier */ +#define SB_UGPR 2u /* uniform-register results (LDCU) */ + +enum { K_FIXED, K_PRED, K_VAR, K_UVAR, K_STORE, K_SYNC, K_EXIT, K_BRA, K_BSSY, K_BSYNC }; + +#define EW_MAX_LOOPS 4 +typedef struct { + BlackwellIRProgram *p; + uint32_t pending; /* scoreboards set and not yet waited on */ + int err; + int loop_start[EW_MAX_LOOPS], loop_end[EW_MAX_LOOPS]; + int n_loops; + int open_start[EW_MAX_LOOPS]; /* starts of loops begun and not yet ended (nesting stack) */ + int depth; + BwRegions rg; /* structured reconvergence regions (omega #308) */ + int rg_mutant; /* test only: 1 = drop the BSYNC at join, 2 = corrupt the BSSY target (host sim must catch both) */ + int exited; /* a predicated EXIT was emitted: some lanes of a warp may be gone */ + int exit_sync_ok; /* test only: lets a kernel put SHFL/BAR after a predicated EXIT (the negative control R6) */ + int test_path; /* building for omega_gpu_elementwise_codegen_ir (host tests), never for a launch */ +} Em; + +static void emit(Em *e, BlackwellIRInsn in, int kind) { + uint32_t wait = e->pending; + switch (kind) { + case K_FIXED: in.control = CW(6, SB_NONE, SB_NONE, wait); e->pending = 0; break; + case K_PRED: in.control = CW(14, SB_NONE, SB_NONE, wait); e->pending = 0; break; + case K_VAR: in.control = CW(4, SB_GPR, SB_NONE, wait) | CW_YIELD; e->pending = 1u << SB_GPR; break; + case K_UVAR: in.control = CW(4, SB_UGPR, SB_NONE, wait) | CW_YIELD; e->pending = 1u << SB_UGPR; break; + case K_STORE: in.control = CW(4, SB_NONE, SB_RD, wait) | CW_YIELD; e->pending = 1u << SB_RD; break; + case K_SYNC: in.control = CW(6, SB_NONE, SB_NONE, wait); e->pending = 0; break; + /* a predicated EXIT or BRA that falls through must not be the only waiter */ + case K_EXIT: in.control = CW(5, SB_NONE, SB_NONE, wait); e->pending = in.predicate_p0 ? wait : 0; break; + case K_BRA: in.control = CW(5, SB_NONE, SB_NONE, wait); e->pending = in.predicate_p0 ? wait : 0; break; + /* BSSY / BSYNC read no registers; the stall counts (1 and 5, both with yield) are the ones + * nvcc 13.0.88 writes for sm_121 (docs/gpu-reconvergence-308.md). They wait on everything + * pending so that, as for every branch here, nothing is in flight across a control edge + * and the straight-line scoreboard walk in the tests stays valid. */ + case K_BSSY: in.control = CW(1, SB_NONE, SB_NONE, wait) | CW_YIELD; e->pending = 0; break; + case K_BSYNC: in.control = CW(5, SB_NONE, SB_NONE, wait) | CW_YIELD; e->pending = 0; break; + default: e->err = -1; return; + } + if (omega_bw_ir_append(e->p, &in) < 0) e->err = -1; /* append returns the index */ +} + +static int V(Em *e) { int v = omega_bw_ir_alloc_vreg(e->p); if (v < 0) e->err = -1; return v; } +static int V64(Em *e) { int v = omega_bw_ir_alloc_vreg64(e->p); if (v < 0) e->err = -1; return v; } +static int UV64(Em *e) { int v = omega_bw_ir_alloc_uvreg64(e->p); if (v < 0) e->err = -1; return v; } +static int here(const Em *e) { return (int)e->p->count; } + +#define NOREG -1 +static BlackwellIRInsn I0(BlackwellIROpcode op) { + BlackwellIRInsn in; memset(&in, 0, sizeof in); + in.op = op; in.dst_vreg = NOREG; in.src1_vreg = NOREG; in.src2_vreg = NOREG; in.src3_vreg = NOREG; in.ureg = NOREG; + return in; +} +static void ldcu64(Em *e, int udst, uint32_t off) { BlackwellIRInsn i = I0(BW_IR_LDCU64); i.dst_vreg = udst; i.imm = off; i.is_uniform = true; emit(e, i, K_UVAR); } +static void ldc64(Em *e, int dst64, uint32_t off) { BlackwellIRInsn i = I0(BW_IR_LDC64); i.dst_vreg = dst64; i.imm = off; emit(e, i, K_VAR); } +static void ldc32(Em *e, int dst, uint32_t off) { BlackwellIRInsn i = I0(BW_IR_LDC); i.dst_vreg = dst; i.imm = off; emit(e, i, K_VAR); } +static void s2r(Em *e, int dst, uint32_t sr) { BlackwellIRInsn i = I0(BW_IR_S2R); i.dst_vreg = dst; i.imm = sr; emit(e, i, K_VAR); } +static void movi(Em *e, int dst, uint32_t imm) { BlackwellIRInsn i = I0(BW_IR_MOV_IMM); i.dst_vreg = dst; i.imm = imm; emit(e, i, K_FIXED); } +static void movf(Em *e, int dst, float f) { uint32_t u; memcpy(&u, &f, 4); movi(e, dst, u); } +static void movrz(Em *e, int dst) { BlackwellIRInsn i = I0(BW_IR_MOV_RZ); i.dst_vreg = dst; emit(e, i, K_FIXED); } +/* dst = a * imm + c (c may be NOREG for RZ) */ +static void imadi(Em *e, int dst, int a, uint32_t imm, int c) { BlackwellIRInsn i = I0(BW_IR_IMAD); i.dst_vreg = dst; i.src1_vreg = a; i.imm = imm; i.src3_vreg = c; emit(e, i, K_FIXED); } +/* dst = a * b + c (registers) */ +static void imadr(Em *e, int dst, int a, int b, int c) { BlackwellIRInsn i = I0(BW_IR_IMAD); i.dst_vreg = dst; i.src1_vreg = a; i.src2_vreg = b; i.src3_vreg = c; emit(e, i, K_FIXED); } +static void iadd3(Em *e, int dst, int a, int b) { BlackwellIRInsn i = I0(BW_IR_IADD3); i.dst_vreg = dst; i.src1_vreg = a; i.src2_vreg = b; emit(e, i, K_FIXED); } +static void lop3xor(Em *e, int dst, int a, int b) { BlackwellIRInsn i = I0(BW_IR_LOP3_XOR); i.dst_vreg = dst; i.src1_vreg = a; i.src2_vreg = b; emit(e, i, K_FIXED); } +/* dst64 = idx * 4 + ptr64 (byte address of element idx) */ +static void addr4(Em *e, int dst64, int idx, int ptr64) { BlackwellIRInsn i = I0(BW_IR_IMAD_WIDE); i.dst_vreg = dst64; i.src1_vreg = idx; i.imm = 4; i.src3_vreg = ptr64; emit(e, i, K_FIXED); } +static void ldg(Em *e, int dst, int addr64, int udesc) { BlackwellIRInsn i = I0(BW_IR_LDG_E); i.dst_vreg = dst; i.src1_vreg = addr64; i.ureg = udesc; emit(e, i, K_VAR); } +/* Independent loads (LDG_E from global, or LDS32 from shared memory with ureg NOREG) issued + * back to back (campaign C2a, 2026-10-05). The first load + * waits on whatever is pending; the others wait on nothing, so all n are in flight together. + * Every load sets SB_GPR (a counting scoreboard), so the next instruction waits once for all + * of them instead of once per load. Fails the build if a load address is a destination of the + * group or a destination repeats (the later loads would read an operand still in flight). */ +static void ld_group(Em *e, BlackwellIROpcode op, int n, const int *dst, const int *addr, int ureg) { + for (int i = 0; i < n; i++) + for (int k = 0; k < n; k++) + if (addr[i] == dst[k] || (k != i && dst[i] == dst[k])) { e->err = -1; return; } + for (int i = 0; i < n; i++) { + if (op != BW_IR_LDG_E && op != BW_IR_LDS32) { e->err = -1; return; } + BlackwellIRInsn in = I0(op); in.dst_vreg = dst[i]; in.src1_vreg = addr[i]; in.ureg = ureg; + if (i == 0) { emit(e, in, K_VAR); continue; } + in.control = CW(4, SB_GPR, SB_NONE, 0) | CW_YIELD; /* nonzero, so the encoder keeps it */ + if (omega_bw_ir_append(e->p, &in) < 0) e->err = -1; + } +} +static void stg(Em *e, int addr64, int val, int udesc) { BlackwellIRInsn i = I0(BW_IR_STG_E); i.src1_vreg = addr64; i.src2_vreg = val; i.ureg = udesc; emit(e, i, K_STORE); } +static void fadd(Em *e, int d, int a, int b) { BlackwellIRInsn i = I0(BW_IR_FADD); i.dst_vreg = d; i.src1_vreg = a; i.src2_vreg = b; emit(e, i, K_FIXED); } +static void fsub(Em *e, int d, int a, int b) { BlackwellIRInsn i = I0(BW_IR_FSUB); i.dst_vreg = d; i.src1_vreg = a; i.src2_vreg = b; emit(e, i, K_FIXED); } +static void fmul(Em *e, int d, int a, int b) { BlackwellIRInsn i = I0(BW_IR_FMUL); i.dst_vreg = d; i.src1_vreg = a; i.src2_vreg = b; emit(e, i, K_FIXED); } +static void ffma(Em *e, int d, int a, int b, int c) { BlackwellIRInsn i = I0(BW_IR_FFMA); i.dst_vreg = d; i.src1_vreg = a; i.src2_vreg = b; i.src3_vreg = c; emit(e, i, K_FIXED); } +static void mufu(Em *e, BlackwellIROpcode op, int d, int a) { BlackwellIRInsn i = I0(op); i.dst_vreg = d; i.src1_vreg = a; emit(e, i, K_VAR); } +/* Cross-lane and warp-ending ops are refused inside an open reconvergence region (omega #308): + * a SHFL or BAR.SYNC reached by a split warp reads inactive lanes / counts a partial warp + * (undefined; the 2026-10-04 attention bug), and a lane that EXITs inside a region can no + * longer be joined. The build fails closed instead of emitting such code. */ +/* Cross-lane instructions (SHFL, BAR.SYNC) need the whole warp present. The emitter refuses + * them (fails the build) inside an open reconvergence region, where lanes may be on different + * paths, and after a predicated EXIT, where lanes may be gone for good: a SHFL then reads + * inactive lanes (undefined on the chip, the attention failure of 2026-10-04). A kernel that + * must drop lanes before a cross-lane step clamps them instead and exits them afterwards. */ +static int warp_split(Em *e) { if (e->rg.depth > 0 || (e->exited && !e->exit_sync_ok)) { e->err = -1; return 1; } return 0; } +static void shfl_down(Em *e, int d, int a, uint32_t off) { if (warp_split(e)) return; BlackwellIRInsn i = I0(BW_IR_SHFL_DOWN); i.dst_vreg = d; i.src1_vreg = a; i.imm = off; emit(e, i, K_VAR); } +static void sts32(Em *e, int addr, int val) { BlackwellIRInsn i = I0(BW_IR_STS32); i.src1_vreg = addr; i.src2_vreg = val; emit(e, i, K_STORE); } +static void lds32(Em *e, int d, int addr) { BlackwellIRInsn i = I0(BW_IR_LDS32); i.dst_vreg = d; i.src1_vreg = addr; emit(e, i, K_VAR); } +static void bar_sync(Em *e) { if (warp_split(e)) return; emit(e, I0(BW_IR_BAR_SYNC), K_SYNC); } +static void isetp_ge_u32(Em *e, int a, int b) { BlackwellIRInsn i = I0(BW_IR_ISETP_GE_U32); i.src1_vreg = a; i.src2_vreg = b; emit(e, i, K_PRED); } +/* An EXIT inside a region would leave the barrier waiting for a lane that never comes: refused. */ +static void exit_if_p0(Em *e) { if (e->rg.depth > 0) { e->err = -1; return; } e->exited = 1; BlackwellIRInsn i = I0(BW_IR_EXIT); i.predicate_p0 = true; emit(e, i, K_EXIT); } +static void bra_back_if_not_p0(Em *e, int target) { BlackwellIRInsn i = I0(BW_IR_BRA); i.predicate_p0 = true; i.predicate_not = true; i.imm = (uint32_t)(target - here(e)); emit(e, i, K_BRA); } +static void tail(Em *e) { + emit(e, I0(BW_IR_EXIT), K_EXIT); + BlackwellIRInsn self = I0(BW_IR_BRA); /* the self-branch every kernel here ends with */ + self.control = 0x000fc000u; + if (omega_bw_ir_append(e->p, &self) < 0) e->err = -1; +} +/* Loops may nest (prime race cut): begin pushes the start, end pops it, so the inner + * loop is closed (and its liveness fixed) first. For the single-level loops above this + * is exactly the old behaviour. */ +static void loop_begin(Em *e) { if (e->depth >= EW_MAX_LOOPS) { e->err = -1; return; } e->open_start[e->depth++] = here(e); } +/* Closing a loop: every value defined before the loop and read inside it stays + * live until the back-branch, otherwise the linear-scan allocator (which knows + * nothing about back-edges) would hand its register to a body temporary whose + * first definition comes after the value's last textual use (e.g. the scale y + * in rmsnorm pass 2), and the next iteration would read garbage. */ +static void loop_end(Em *e) { + if (e->n_loops >= EW_MAX_LOOPS || e->depth <= 0) { e->err = -1; return; } + int start = e->open_start[--e->depth], end = here(e) - 1; + e->loop_start[e->n_loops] = start; + e->loop_end[e->n_loops] = end; + e->n_loops++; + OmegaRegAlloc *ra = &e->p->regalloc; + for (int v = 0; v < ra->num_vregs; v++) { + OmegaLiveInterval *iv = &ra->intervals[v]; + if (iv->first_def >= 0 && iv->first_def < start && iv->last_use >= start && iv->last_use < end) iv->last_use = end; + } +} + +/* Kernel argument words (constant bank 0, same slots as the matmul path): + * 0x380 a, 0x388 b, 0x390 c (64-bit each), then four 32-bit parameters. */ +#define ARG_A 0x380u +#define ARG_B 0x388u +#define ARG_C 0x390u +#define ARG_P0 0x398u +#define ARG_P1 0x39cu +#define ARG_P2 0x3a0u +#define ARG_P3 0x3a4u +#define ARG_P4 0x3a8u +#define DESC_OFF 0x358u + +typedef struct { int udesc, pa, pb, pc, tid, cta; } Pro; +static Pro prologue(Em *e) { + Pro r; + r.udesc = UV64(e); r.pa = V64(e); r.pb = V64(e); r.pc = V64(e); r.tid = V(e); r.cta = V(e); + ldcu64(e, r.udesc, DESC_OFF); + ldc64(e, r.pa, ARG_A); ldc64(e, r.pb, ARG_B); ldc64(e, r.pc, ARG_C); + s2r(e, r.tid, BW_SR_TID_X); s2r(e, r.cta, BW_SR_CTAID_X); + return r; +} + +/* out[i] = 2^a[i] for i < n (n = P0). Threads 128, grid ceil(n/128). Mutant: 2^(a+1). */ +static void gen_ex2(Em *e, int mutant) { + Pro p = prologue(e); + int n = V(e), i = V(e), off = V64(e), x = V(e), y = V(e); + ldc32(e, n, ARG_P0); + imadi(e, i, p.cta, OMEGA_GPU_EW_THREADS, p.tid); + isetp_ge_u32(e, i, n); + exit_if_p0(e); + addr4(e, off, i, p.pa); + ldg(e, x, off, p.udesc); + if (mutant) { int one = V(e); movf(e, one, 1.0f); fadd(e, x, x, one); } + mufu(e, BW_IR_MUFU_EX2, y, x); + addr4(e, off, i, p.pc); + stg(e, off, y, p.udesc); + tail(e); +} + +/* out[i] = a[i ^ 127] within each CTA of 128 (n % 128 == 0). STS own word, BAR.SYNC, + * LDS the partner word. Mutant: LDS own word (no exchange). */ +static void gen_xchg(Em *e, int mutant) { + Pro p = prologue(e); + int i = V(e), off = V64(e), x = V(e), saddr = V(e), paddr = V(e), mask = V(e), y = V(e); + imadi(e, i, p.cta, OMEGA_GPU_EW_THREADS, p.tid); + addr4(e, off, i, p.pa); + ldg(e, x, off, p.udesc); + imadi(e, saddr, p.tid, 4, NOREG); + sts32(e, saddr, x); + bar_sync(e); + if (mutant) { + lds32(e, y, saddr); + } else { + movi(e, mask, OMEGA_GPU_EW_THREADS - 1); + lop3xor(e, paddr, p.tid, mask); + imadi(e, paddr, paddr, 4, NOREG); + lds32(e, y, paddr); + } + addr4(e, off, i, p.pc); + stg(e, off, y, p.udesc); + tail(e); +} + +/* One CTA of 128 threads per row. P0 = dim/128 (loop count), P1 = bits(1/dim), + * P2 = bits(eps), P3 = dim. Mutant: the warp tree drops the last SHFL step. */ +static void gen_rmsnorm(Em *e, int mutant) { + Pro p = prologue(e); + int nk = V(e), invd = V(e), eps = V(e), dim = V(e); + int rowbase = V(e), idx = V(e), col = V(e), k = V(e), acc = V(e), cT = V(e), c1 = V(e); + int off = V64(e), x = V(e), t = V(e); + ldc32(e, nk, ARG_P0); ldc32(e, invd, ARG_P1); ldc32(e, eps, ARG_P2); ldc32(e, dim, ARG_P3); + imadr(e, rowbase, p.cta, dim, NOREG); + iadd3(e, idx, rowbase, p.tid); + movrz(e, acc); movrz(e, k); + movi(e, cT, OMEGA_GPU_EW_THREADS); movi(e, c1, 1); + loop_begin(e); + int l1 = here(e); + addr4(e, off, idx, p.pa); + ldg(e, x, off, p.udesc); + ffma(e, acc, x, x, acc); + iadd3(e, idx, idx, cT); + iadd3(e, k, k, c1); + isetp_ge_u32(e, k, nk); + bra_back_if_not_p0(e, l1); + loop_end(e); + static const uint32_t steps[5] = { 16, 8, 4, 2, 1 }; + for (int s = 0; s < (mutant ? 4 : 5); s++) { shfl_down(e, t, acc, steps[s]); fadd(e, acc, acc, t); } + int saddr = V(e); + imadi(e, saddr, p.tid, 4, NOREG); + sts32(e, saddr, acc); + bar_sync(e); + int a0 = V(e), a1 = V(e), a2 = V(e), a3 = V(e), s0 = V(e), s1 = V(e), s2 = V(e), s3 = V(e), sum = V(e); + movrz(e, a0); movi(e, a1, 128); movi(e, a2, 256); movi(e, a3, 384); + lds32(e, s0, a0); lds32(e, s1, a1); lds32(e, s2, a2); lds32(e, s3, a3); + fadd(e, sum, s0, s1); fadd(e, sum, sum, s2); fadd(e, sum, sum, s3); + int v = V(e), y = V(e), u = V(e), cmh = V(e), c15 = V(e); + fmul(e, v, sum, invd); + fadd(e, v, v, eps); + mufu(e, BW_IR_MUFU_RSQ, y, v); + movf(e, cmh, -0.5f); movf(e, c15, 1.5f); + fmul(e, u, v, y); fmul(e, u, u, y); ffma(e, u, u, cmh, c15); fmul(e, y, y, u); + int offw = V64(e), offc = V64(e), w = V(e), o = V(e); + iadd3(e, idx, rowbase, p.tid); + imadi(e, col, p.tid, 1, NOREG); /* col = tid (the encoder maps a missing src2 to R0, not RZ, so no IADD3 here) */ + movrz(e, k); + loop_begin(e); + int l2 = here(e); + addr4(e, off, idx, p.pa); + ldg(e, x, off, p.udesc); + addr4(e, offw, col, p.pb); + ldg(e, w, offw, p.udesc); + fmul(e, o, x, y); + fmul(e, o, o, w); + addr4(e, offc, idx, p.pc); + stg(e, offc, o, p.udesc); + iadd3(e, idx, idx, cT); + iadd3(e, col, col, cT); + iadd3(e, k, k, c1); + isetp_ge_u32(e, k, nk); + bra_back_if_not_p0(e, l2); + loop_end(e); + tail(e); +} + +/* One CTA of head_dim/2 threads per head. P0 = head_dim, P1 = head_dim/2. + * a = vector, b = table (cos in the first half of each head, sin in the second). + * Mutant: out0 = q0*cos + q1*sin. */ +static void gen_rope(Em *e, int mutant) { + Pro p = prologue(e); + int hd = V(e), half = V(e), i0 = V(e), i1 = V(e), off0 = V64(e), off1 = V64(e); + int q0 = V(e), q1 = V(e), c = V(e), s = V(e), t0 = V(e), t1 = V(e), o0 = V(e), o1 = V(e); + ldc32(e, hd, ARG_P0); ldc32(e, half, ARG_P1); + imadr(e, i0, p.cta, hd, p.tid); + iadd3(e, i1, i0, half); + addr4(e, off0, i0, p.pa); ldg(e, q0, off0, p.udesc); + addr4(e, off1, i1, p.pa); ldg(e, q1, off1, p.udesc); + addr4(e, off0, i0, p.pb); ldg(e, c, off0, p.udesc); + addr4(e, off1, i1, p.pb); ldg(e, s, off1, p.udesc); + fmul(e, t0, q0, c); fmul(e, t1, q1, s); + if (mutant) fadd(e, o0, t0, t1); else fsub(e, o0, t0, t1); + fmul(e, t0, q0, s); fmul(e, t1, q1, c); + fadd(e, o1, t0, t1); + addr4(e, off0, i0, p.pc); stg(e, off0, o0, p.udesc); + addr4(e, off1, i1, p.pc); stg(e, off1, o1, p.udesc); + tail(e); +} + +/* out[i] = g/(1+2^(g*-log2e)) * up for i < n (P0). Mutant: 2^(g*+log2e). */ +static void gen_swiglu(Em *e, int mutant) { + Pro p = prologue(e); + int n = V(e), i = V(e), off = V64(e), g = V(e), u = V(e), k = V(e), t = V(e), ex = V(e), d = V(e), r = V(e), one = V(e), o = V(e); + ldc32(e, n, ARG_P0); + imadi(e, i, p.cta, OMEGA_GPU_EW_THREADS, p.tid); + isetp_ge_u32(e, i, n); + exit_if_p0(e); + addr4(e, off, i, p.pa); ldg(e, g, off, p.udesc); + addr4(e, off, i, p.pb); ldg(e, u, off, p.udesc); + movf(e, k, mutant ? 1.4426950408889634f : -1.4426950408889634f); + movf(e, one, 1.0f); + fmul(e, t, g, k); + mufu(e, BW_IR_MUFU_EX2, ex, t); + fadd(e, d, ex, one); + mufu(e, BW_IR_MUFU_RCP, r, d); + fmul(e, o, g, r); + fmul(e, o, o, u); + addr4(e, off, i, p.pc); + stg(e, off, o, p.udesc); + tail(e); +} + +/* ------------------------------------------- prime race sieve (2026-10-05) */ +static void lop3lut(Em *e, int d, int a, int b, int c, uint32_t lut) { BlackwellIRInsn i = I0(BW_IR_LOP3_LUT); i.dst_vreg = d; i.src1_vreg = a; i.src2_vreg = b; i.src3_vreg = c; i.imm = lut & 0xffu; emit(e, i, K_FIXED); } +static void shl_r(Em *e, int d, int a, int n) { BlackwellIRInsn i = I0(BW_IR_SHF_L_U32); i.dst_vreg = d; i.src1_vreg = a; i.src2_vreg = n; emit(e, i, K_FIXED); } +static void shr_i(Em *e, int d, int a, uint32_t n) { BlackwellIRInsn i = I0(BW_IR_SHF_R); i.dst_vreg = d; i.src1_vreg = a; i.imm = n; emit(e, i, K_FIXED); } +static void imadhi(Em *e, int d, int a, int b) { BlackwellIRInsn i = I0(BW_IR_IMAD_HI_U32); i.dst_vreg = d; i.src1_vreg = a; i.src2_vreg = b; emit(e, i, K_FIXED); } +/* dst64 = a * imm + c64 (IMAD.WIDE.U32 immediate form) */ +static void widei(Em *e, int dst64, int a, uint32_t imm, int c64) { BlackwellIRInsn i = I0(BW_IR_IMAD_WIDE); i.dst_vreg = dst64; i.src1_vreg = a; i.imm = imm; i.src3_vreg = c64; emit(e, i, K_FIXED); } +/* @P0 BRA forward; returns the instruction index for bra_patch_here */ +static int bra_fwd_if_p0(Em *e) { int at = here(e); BlackwellIRInsn i = I0(BW_IR_BRA); i.predicate_p0 = true; i.imm = 1; emit(e, i, K_BRA); return at; } +static void bra_patch_here(Em *e, int at) { if (e->err == 0 && at >= 0 && at < here(e)) e->p->insns[at].imm = (uint32_t)(here(e) - at); else e->err = -1; } +static void bra_always(Em *e, int target) { BlackwellIRInsn i = I0(BW_IR_BRA); i.imm = (uint32_t)(target - here(e)); emit(e, i, K_BRA); } + +/* ---- structured reconvergence regions (omega #308; bookkeeping in omega_bw_reconv.h) ---- + * region_begin: BSSY Bd (d = nesting depth), fails closed when no barrier register is left. + * region_exit: a forward branch out of the innermost region, patched to its BSYNC at join + * (RX_ALWAYS, RX_IF_P0, RX_IF_NOT_P0). + * region_join: BSYNC Bd; patches the BSSY to the instruction after it and every exit to it. + * Usage: divergent loop = region_begin; L: isetp; region_exit(RX_IF_P0); body; bra_always(L); region_join. + * if/else diamond = region_begin; isetp; t = bra_fwd_if_p0; then; region_exit(RX_ALWAYS); + * bra_patch_here(t); else; region_join. */ +enum { RX_ALWAYS, RX_IF_P0, RX_IF_NOT_P0 }; +static int region_begin(Em *e) { + int bar = bw_regions_next_bar(&e->rg); + if (bar < 0) { e->err = -1; return -1; } + int at = here(e); + BlackwellIRInsn i = I0(BW_IR_BSSY); i.bar_reg = (uint8_t)bar; i.imm = 2; emit(e, i, K_BSSY); + if (bw_regions_begin(&e->rg, at) < 0) e->err = -1; + return bar; +} +static void region_exit(Em *e, int cond) { + int at = here(e); + BlackwellIRInsn i = I0(BW_IR_BRA); i.imm = 1; + if (cond != RX_ALWAYS) { i.predicate_p0 = true; i.predicate_not = (cond == RX_IF_NOT_P0); } + emit(e, i, K_BRA); + if (bw_regions_exit(&e->rg, at) < 0) e->err = -1; +} +static void region_join(Em *e) { + if (e->rg.depth <= 0) { e->err = -1; return; } + int d = e->rg.depth - 1, bsync = here(e); + BlackwellIRInsn i = I0(e->rg_mutant == 1 ? BW_IR_NOP : BW_IR_BSYNC); i.bar_reg = (uint8_t)d; emit(e, i, K_BSYNC); + if (e->rg_mutant == 1) { + /* negative control (host sim only): the join is a NOP; patch as the real join would */ + BwRegions *r = &e->rg; r->depth--; + e->p->insns[r->bssy_at[d]].imm = (uint32_t)(bsync + 1 - r->bssy_at[d]); + for (int k = 0; k < r->n_exits[d]; k++) e->p->insns[r->exits[d][k]].imm = (uint32_t)(bsync - r->exits[d][k]); + return; + } + if (bw_regions_join(&e->rg, e->p, bsync) != d) e->err = -1; + if (e->rg_mutant == 2) e->p->insns[e->rg.bssy_at[d]].imm += 1; /* negative control: BSSY names the wrong join */ +} + +/* Word-parallel odd-only sieve (docs: handoff 2026-10-05 DESIGN "GPU kernel (native)"). + * Thread t owns 32-bit output words w = t, t + S, t + 2S, ... (S = P1 = threads launched; + * the I42 envelope caps a launch at OMEGA_GPU_EW_MAX_CTAS CTAs, so a grid-stride loop + * replaces one thread per word). Every word has exactly one writer; no atomics. + * Bit j of word w is the odd number 2(32w + j) + 1. + * Arguments: a (0x380) = prime table, entries {p, magic = ceil(2^32 / p), k0 = (p*p - 1)/2} + * (12 bytes, ascending p, odd primes <= isqrt(limit)) followed by ONE sentinel entry with + * k0 = 0xffffffff that ends every scan; c (0x390) = output words; + * P0 = nwords, P1 = S, P2 = last word index, P3 = ~tailmask (bits past the last odd). + * Range: limit < 2^32 (so base, k0 < 2^31 and the sign-bit tricks below hold). + * Per word: base = 32w; for each prime while k0 < base + 32: + * d = base - k0; neg = d >> 31 (k0 > base); dpos = neg ? 0 : d; jadd = neg ? k0 - base : 0 + * q = hi32(dpos * magic) (= floor(dpos/p) or one more: magic is rounded up) + * r = dpos - q*p (q one too big -> r = (dpos mod p) - p, wrapped) + * j = p - r; j -= p * ((r - 1) >> 31) (r == 0 -> 0; wrapped r -> p - (dpos mod p)) + * One fix covers both cases: with q one too big, dpos mod p >= p - dpos*e/2^32 > p/2 > 0 + * (e = magic*p - 2^32 < p, dpos < 2^31), so the true remainder is never 0 there and + * p - r - p = p - (dpos mod p) is the right offset. A separate "r += p if wrapped" step was + * removed after a negative control showed it changes no output (host sim, limit 1e7, where + * the overshoot occurs 268 times; bench/prime_race/tests magic_model). + * j += jadd; while j < 32 { acc |= 1 << j; j += p } + * then acc |= (w == 0) (the number 1), word = ~(acc | (w == last ? ~tailmask : 0)). + * Branch-free except the loop branches. Mutant: drops the j fix (j = p - r unreduced), so words where + * base - k0 is a multiple of p (or q overshot) mark the wrong bits. + * + * staged = 1 (OMEGA_GPU_EW_PRIME_SIEVE_SHARED, campaign C3): P4 = table words, sentinel included + * (3 per entry). The CTA first copies the table into shared memory at offset 0 (thread t copies + * words t, t + 128, ...), BAR_SYNC, then the prime loop reads shared memory (LDS) instead of + * uncached global memory. Every thread reaches the barrier: the w >= nwords exit comes after + * it. The launch must declare at least 4 * P4 bytes of shared memory. + * + * divergent (omega #308, regression R1): 0 = the warp-uniform mark loop above (campaign C5). + * 1 = the original lane-dependent mark loop (omega 888a011: `while j < 32 { acc |= 1 << j; j += p }`) + * wrapped in a reconvergence region. 2 = as 1, and the prime loop (whose break `k0 >= base + 32` + * is also lane-dependent: every lane sieves a different word) wrapped in an outer region, so the + * mark loop's region nests inside it (B0 outer, B1 inner). */ +static void gen_prime_sieve(Em *e, int mutant, int staged, int divergent) { + Pro pr = prologue(e); + int nwords = V(e), stride = V(e), lastw = V(e), tailinv = V(e); + int one = V(e), c32 = V(e), cm1 = V(e), w = V(e); + ldc32(e, nwords, ARG_P0); ldc32(e, stride, ARG_P1); ldc32(e, lastw, ARG_P2); ldc32(e, tailinv, ARG_P3); + movi(e, one, 1); movi(e, c32, 32); movi(e, cm1, 0xffffffffu); + int c4 = -1, c8 = -1, c12 = -1; + if (staged) { + int twords = V(e), i = V(e), ga = V64(e), val = V(e), sa = V(e); + c4 = V(e); c8 = V(e); c12 = V(e); + ldc32(e, twords, ARG_P4); + movi(e, c4, 4); movi(e, c8, 8); movi(e, c12, 12); + imadi(e, i, pr.tid, 1, NOREG); + /* The staging loop's trip count differs per thread (thread t copies words t, t+128, ...), + * so its exit splits the warp. The divergent variants rejoin the warp before BAR.SYNC + * (a barrier is not a reconvergence point: the fragments would stay split for the rest + * of the kernel). The production kernel keeps the 2026-10-05 C3 form unchanged. */ + if (divergent) region_begin(e); + loop_begin(e); /* staging loop */ + int ls = here(e); + isetp_ge_u32(e, i, twords); + int sdone = -1; + if (divergent) region_exit(e, RX_IF_P0); else sdone = bra_fwd_if_p0(e); + addr4(e, ga, i, pr.pa); + ldg(e, val, ga, pr.udesc); + imadi(e, sa, i, 4, NOREG); + sts32(e, sa, val); + imadi(e, i, one, OMEGA_GPU_EW_THREADS, i); + bra_always(e, ls); + loop_end(e); + if (divergent) region_join(e); else bra_patch_here(e, sdone); + bar_sync(e); + } + imadi(e, w, pr.cta, OMEGA_GPU_EW_THREADS, pr.tid); + isetp_ge_u32(e, w, nwords); + exit_if_p0(e); + + /* ptr, a64, a64m: global addresses, or (staged) 32-bit shared offsets */ + int base = V(e), basep32 = V(e), acc = V(e); + int ptr = staged ? V(e) : V64(e), a64 = staged ? V(e) : V64(e), a64m = staged ? V(e) : V64(e); + int p = V(e), magic = V(e), k0 = V(e), negp = V(e), d = V(e), neg = V(e), nd = V(e), dpos = V(e), jadd = V(e); + int q = V(e), r = V(e), j = V(e), t = V(e), z = V(e), bit = V(e); + int e1 = V(e), m = V(e), word = V(e), oaddr = V64(e); + int tt = V(e), jj = V(e), sh = V(e), c31 = V(e), cm32 = V(e); + movi(e, c31, 31); movi(e, cm32, 0xffffffe0u); + + loop_begin(e); /* word loop */ + int lw = here(e); + imadi(e, base, w, 32, NOREG); + iadd3(e, basep32, base, c32); + movrz(e, acc); + if (staged) movrz(e, ptr); else widei(e, ptr, one, 0, pr.pa); + + if (divergent >= 2) region_begin(e); /* outer region: the prime loop's break is lane-dependent */ + loop_begin(e); /* prime loop */ + int lp = here(e); + /* one memory round trip per prime: all three entry words load together (C2a). The + * sentinel entry is a whole entry, so loading p and magic before the k0 test is in bounds. */ + if (staged) { iadd3(e, a64, ptr, c8); iadd3(e, a64m, ptr, c4); } + else { widei(e, a64, one, 8, ptr); widei(e, a64m, one, 4, ptr); } + { + const int dsts[3] = { k0, p, magic }, adrs[3] = { a64, ptr, a64m }; + if (staged) ld_group(e, BW_IR_LDS32, 3, dsts, adrs, NOREG); + else ld_group(e, BW_IR_LDG_E, 3, dsts, adrs, pr.udesc); + } + isetp_ge_u32(e, k0, basep32); + int brk = -1; + if (divergent >= 2) region_exit(e, RX_IF_P0); else brk = bra_fwd_if_p0(e); + imadi(e, negp, p, 0xffffffffu, NOREG); /* -p */ + imadi(e, d, k0, 0xffffffffu, base); /* base - k0 */ + shr_i(e, neg, d, 31); + imadi(e, nd, d, 0xffffffffu, NOREG); /* -d */ + imadr(e, dpos, neg, nd, d); /* d - neg*d */ + imadr(e, jadd, neg, nd, NOREG); /* neg * (k0 - base) */ + imadhi(e, q, dpos, magic); + imadr(e, r, q, negp, dpos); /* dpos - q*p */ + imadi(e, j, r, 0xffffffffu, p); /* p - r */ + if (!mutant) { + iadd3(e, t, r, cm1); + shr_i(e, z, t, 31); /* r == 0, or r wrapped (q one too big) */ + imadr(e, j, z, negp, j); /* j - p */ + } + iadd3(e, j, j, jadd); + + /* mark loop (campaign C5): the trip count depends only on p, so every active lane runs the + * same ceil(32/p) steps and the warp never splits here (all active lanes are on the same + * prime). Bits past this word are masked out instead of branched around. The old loop ran + * "while j < 32", a lane-dependent branch with no reconvergence point. */ + if (divergent) { + /* R1: the 888a011 mark loop, lane-dependent trip count, inside a region. Each lane + * leaves at its own iteration (the exit jumps to the BSYNC) and the warp reunites there. */ + region_begin(e); + loop_begin(e); + int li = here(e); + isetp_ge_u32(e, j, c32); + region_exit(e, RX_IF_P0); + shl_r(e, bit, one, j); + lop3lut(e, acc, acc, bit, NOREG, 0xfc); /* acc | bit */ + iadd3(e, j, j, p); + bra_always(e, li); + loop_end(e); + region_join(e); + } else { + movrz(e, tt); + loop_begin(e); + int li = here(e); + isetp_ge_u32(e, tt, c32); + int done = bra_fwd_if_p0(e); + iadd3(e, jj, j, tt); + lop3lut(e, sh, jj, c31, NOREG, 0xc0); /* jj & 31 */ + shl_r(e, bit, one, sh); + iadd3(e, t, jj, cm32); + shr_i(e, z, t, 31); /* jj < 32 (j <= p + 31, far below 2^31) */ + imadi(e, m, z, 0xffffffffu, NOREG); /* all ones when jj < 32, else 0 */ + lop3lut(e, acc, acc, bit, m, 0xf8); /* acc | (bit & m) */ + iadd3(e, tt, tt, p); + bra_always(e, li); + loop_end(e); + bra_patch_here(e, done); + } + + if (staged) iadd3(e, ptr, ptr, c12); else widei(e, ptr, one, 12, ptr); + bra_always(e, lp); + loop_end(e); + if (divergent >= 2) region_join(e); else bra_patch_here(e, brk); + + iadd3(e, t, w, cm1); + shr_i(e, z, t, 31); /* w == 0: the number 1 is not prime */ + lop3lut(e, acc, acc, z, NOREG, 0xfc); + lop3xor(e, e1, w, lastw); + iadd3(e, t, e1, cm1); + shr_i(e, z, t, 31); /* w == last word */ + imadr(e, m, z, tailinv, NOREG); + lop3lut(e, word, acc, m, NOREG, 0x03); /* ~(acc | m) */ + addr4(e, oaddr, w, pr.pc); + stg(e, oaddr, word, pr.udesc); + /* IMAD, not IADD3: the IADD3 encoder ignores the scheduler control word (fixed + * 0x010fca00), so right after a store it would not wait on the read barrier */ + imadr(e, w, stride, one, w); + isetp_ge_u32(e, w, nwords); + bra_back_if_not_p0(e, lw); + loop_end(e); + tail(e); +} + +/* ---- reconvergence probes (omega #308, regressions R3..R6 and the resource bound) ---- + * All: a, b = u32 inputs, out = u32, P0 = n; 128 threads, grid ceil(n/128); launched by + * omega_gpu_reconv_probe_u32. A lane past n clamps its index to n-1 so that it loads valid + * data and takes part in the warp-wide SHFL; it exits only after the SHFL, before its store. + * Each probe computes a per-lane r on divergent paths, joins, then + * out[i] = r + 4099 * s, s = r of lane (lane+1) in the same warp (SHFL.DOWN 1; lane 31: own r) + * so a lane that joined late, never, or with the wrong value shows in the neighbour's word too. + * The host oracle is rc_oracle in tests/gpu_reconv_test.c. */ +typedef struct { Pro pr; int n, i, idx, x, y, r, s, one, cm1; } Rp; +static Rp rp_begin(Em *e) { + Rp k; memset(&k, 0, sizeof k); + k.pr = prologue(e); + k.n = V(e); k.i = V(e); k.idx = V(e); k.x = V(e); k.y = V(e); k.r = V(e); k.s = V(e); k.one = V(e); k.cm1 = V(e); + int nm1 = V(e), d = V(e), neg = V(e), off = V64(e); + ldc32(e, k.n, ARG_P0); + movi(e, k.one, 1); movi(e, k.cm1, 0xffffffffu); + imadi(e, k.i, k.pr.cta, OMEGA_GPU_EW_THREADS, k.pr.tid); + iadd3(e, nm1, k.n, k.cm1); /* n - 1 */ + imadi(e, d, k.i, 0xffffffffu, nm1); /* (n - 1) - i, top bit set when i > n - 1 */ + shr_i(e, neg, d, 31); + imadr(e, k.idx, neg, d, k.i); /* i, or n - 1 when past the end */ + addr4(e, off, k.idx, k.pr.pa); ldg(e, k.x, off, k.pr.udesc); + addr4(e, off, k.idx, k.pr.pb); ldg(e, k.y, off, k.pr.udesc); + return k; +} +static void rp_end(Em *e, const Rp *k) { + int out = V(e), off = V64(e); + shfl_down(e, k->s, k->r, 1); + imadi(e, out, k->s, 4099, k->r); + isetp_ge_u32(e, k->i, k->n); + exit_if_p0(e); + addr4(e, off, k->i, k->pr.pc); + stg(e, off, out, k->pr.udesc); + tail(e); +} +/* R3: if/else with sides of different length. x >= y: r = f(x, y) (5 steps); else r = x + 1. + * Math mutant: the long side is one too big. */ +static void gen_rc_diamond(Em *e, int mutant) { + Rp k = rp_begin(e); + int t = V(e), u = V(e); + region_begin(e); + isetp_ge_u32(e, k.x, k.y); + int els = bra_fwd_if_p0(e); + iadd3(e, k.r, k.x, k.one); /* short side */ + region_exit(e, RX_ALWAYS); + bra_patch_here(e, els); + imadi(e, t, k.x, 3, k.y); /* long side: t = 3x + y */ + lop3xor(e, u, t, k.x); /* u = t ^ x */ + imadi(e, k.r, u, 5, k.one); /* r = 5u + 1 */ + shr_i(e, t, k.r, 3); + iadd3(e, k.r, k.r, t); /* r += r >> 3 */ + if (mutant) iadd3(e, k.r, k.r, k.one); + region_join(e); + rp_end(e, &k); +} +/* The lane-dependent loop shared by R4 and R5: v = x & 255, step = (y & 15) + 1, + * while v < 256 { v += step; cnt += 1 }. Math mutant: the bound is 255. Emits the region. */ +static void rc_count_loop(Em *e, const Rp *k, int mutant, int v, int cnt) { + int st = V(e), bound = V(e), c255 = V(e), c15 = V(e); + movi(e, c255, 255); movi(e, c15, 15); movi(e, bound, mutant ? 255 : 256); + lop3lut(e, v, k->x, c255, NOREG, 0xc0); + lop3lut(e, st, k->y, c15, NOREG, 0xc0); + iadd3(e, st, st, k->one); + movrz(e, cnt); + region_begin(e); + loop_begin(e); + int L = here(e); + isetp_ge_u32(e, v, bound); + region_exit(e, RX_IF_P0); + iadd3(e, v, v, st); + iadd3(e, cnt, cnt, k->one); + bra_always(e, L); + loop_end(e); + region_join(e); +} +/* R4: lane-dependent loop exit (break). r = 256 * cnt + v. */ +static void gen_rc_loopbreak(Em *e, int mutant) { + Rp k = rp_begin(e); + int v = V(e), cnt = V(e); + rc_count_loop(e, &k, mutant, v, cnt); + imadi(e, k.r, cnt, 256, v); + rp_end(e, &k); +} +/* R5: nested divergence. Odd x: r = 7 (skips the inner region). Even x: the R4 loop inside the + * outer region, r = 256 * cnt + v + 100. */ +static void gen_rc_nested(Em *e, int mutant) { + Rp k = rp_begin(e); + int v = V(e), cnt = V(e), low = V(e), c100 = V(e); + movi(e, k.r, 7); movi(e, c100, 100); + lop3lut(e, low, k.x, k.one, NOREG, 0xc0); /* x & 1 */ + region_begin(e); + isetp_ge_u32(e, low, k.one); + region_exit(e, RX_IF_P0); + rc_count_loop(e, &k, mutant, v, cnt); + imadi(e, k.r, cnt, 256, v); + iadd3(e, k.r, k.r, c100); + region_join(e); + rp_end(e, &k); +} +/* R6: lanes past the end, a divergent region, then a CTA-wide exchange and the warp SHFL. + * xv = x | 1 computed on divergent paths (even lanes take the branch), every thread stores xv + * to its shared slot, BAR.SYNC, reads its partner's (tid ^ 1), r = 3x + partner_xv. + * Production form: lanes past n are clamped and exit after the SHFL (correct). early_exit + * (mutant 1): lanes past n EXIT first, as a naive kernel would; the emitter refuses that + * (BAR/SHFL after a predicated EXIT). Only the host-test build path (codegen_ir) may bypass the + * refusal, so the simulator can show what the kernel would do (SHFL_EXITED); a launch never can. */ +static void gen_rc_exit_bar(Em *e, int early_exit) { + Rp k = rp_begin(e); + int saddr = V(e), paddr = V(e), yv = V(e), xv = V(e), low = V(e); + if (early_exit) { + e->exit_sync_ok = early_exit == 1 && e->test_path; + isetp_ge_u32(e, k.i, k.n); + exit_if_p0(e); + } + imadi(e, xv, k.x, 1, NOREG); + lop3lut(e, low, k.x, k.one, NOREG, 0xc0); /* x & 1 */ + region_begin(e); + isetp_ge_u32(e, low, k.one); + region_exit(e, RX_IF_P0); /* odd lanes leave */ + iadd3(e, xv, xv, k.one); /* even lanes: xv = x + 1 */ + region_join(e); + imadi(e, saddr, k.pr.tid, 4, NOREG); + sts32(e, saddr, xv); + bar_sync(e); + lop3xor(e, paddr, k.pr.tid, k.one); + imadi(e, paddr, paddr, 4, NOREG); + lds32(e, yv, paddr); + imadi(e, k.r, k.x, 3, yv); + rp_end(e, &k); +} +/* Barrier-register bound: `levels` nested regions, each leaving lanes with x >= y out and adding 1 + * for the others: r = x + levels * (x < y). 16 levels must build (B0..B15); 17 must be refused. */ +static void gen_rc_depth(Em *e, int levels) { + Rp k = rp_begin(e); + imadi(e, k.r, k.x, 1, NOREG); + for (int d = 0; d < levels; d++) { + region_begin(e); + isetp_ge_u32(e, k.x, k.y); + region_exit(e, RX_IF_P0); + iadd3(e, k.r, k.r, k.one); + } + for (int d = 0; d < levels; d++) region_join(e); + rp_end(e, &k); +} +/* Codegen refusals (each must come back CODEGEN_FAIL; a kernel with a split warp at a + * cross-lane step must never reach the chip). */ +static void gen_rc_refuse(Em *e, OmegaGpuEwOp which) { + Rp k = rp_begin(e); + switch (which) { + case OMEGA_GPU_EW_RC_REFUSE_EXIT_IN_REGION: region_begin(e); isetp_ge_u32(e, k.i, k.n); exit_if_p0(e); region_join(e); break; + case OMEGA_GPU_EW_RC_REFUSE_BAR_IN_REGION: region_begin(e); bar_sync(e); region_join(e); break; + case OMEGA_GPU_EW_RC_REFUSE_SHFL_IN_REGION: region_begin(e); shfl_down(e, k.s, k.x, 1); region_join(e); break; + case OMEGA_GPU_EW_RC_REFUSE_UNCLOSED: region_begin(e); isetp_ge_u32(e, k.x, k.y); region_exit(e, RX_IF_P0); break; /* no join */ + case OMEGA_GPU_EW_RC_REFUSE_JOIN_ONLY: region_join(e); break; /* BSYNC without BSSY */ + default: e->err = -1; break; + } + imadi(e, k.r, k.x, 1, NOREG); + rp_end(e, &k); +} + +/* A value first defined inside a loop body may not be read after the loop + * before it is written again: the allocator could have let a later body + * temporary share its register, and the last iteration's value would be lost. */ +static int check_loop_invariant(const Em *e) { + const OmegaRegAlloc *ra = &e->p->regalloc; + for (int L = 0; L < e->n_loops; L++) + for (int v = 0; v < ra->num_vregs; v++) { + int fd = ra->intervals[v].first_def; + if (fd < e->loop_start[L] || fd > e->loop_end[L]) continue; + for (size_t i = (size_t)e->loop_end[L] + 1; i < e->p->count; i++) { + const BlackwellIRInsn *in = &e->p->insns[i]; + if (in->src1_vreg == v || in->src2_vreg == v || in->src3_vreg == v) return -1; + if (in->dst_vreg == v && !in->is_uniform) break; + } + } + return 0; +} + +#define EW_GPR_BUDGET 64u /* the QMD declares 64 registers, as the matmul launcher does */ + +/* Mesa NAK sm120_instr_latencies.rs + sm100/reg_raw.csv: ALU/dual-ALU pairs + * and FMA/FMA pairs need 4 + 1 cycles; mixed pairs need 5 + 1. CSV rows are + * READERS, columns WRITERS (see lat_rs_gen.py), not the reverse. IMAD.WIDE + * has operand-specific latencies and is deliberately excluded here. + * + * Retain at least 5 cycles after every fixed producer. Thus the next reader + * uses the pair-specific bound and later readers are >= 9 cycles away (the + * next instruction in a tightened pair is itself fixed). No instruction, + * branch target, wait, barrier assignment or register allocation is changed. + * IADD3's encoder still supplies its historical control word; do not pretend + * to schedule that producer here. Its existing 5-cycle delay is unchanged. + * + * Predicate-to-guard uses NAK paw_latency -> raw(write, None): reader + * RedirectedFp64, writer Dualalu = 13, plus SM120's 1 = 14, in emit above. + * NVIDIA's public ISA docs are silent about SASS control-word semantics. + */ +static int fixed_pair_class(BlackwellIROpcode op) { + switch (op) { + case BW_IR_MOV_RZ: case BW_IR_MOV_IMM: case BW_IR_IADD3: + case BW_IR_LOP3_XOR: case BW_IR_LOP3_LUT: + case BW_IR_SHF_R: case BW_IR_SHF_L_U32: return 1; + case BW_IR_IMAD: case BW_IR_IMAD_HI_U32: + case BW_IR_FADD: case BW_IR_FSUB: case BW_IR_FMUL: case BW_IR_FFMA: return 2; + default: return 0; + } +} + +static void schedule_fixed_pairs(BlackwellIRProgram *p) { + for (size_t i = 0; i + 1 < p->count; i++) { + BlackwellIRInsn *a = &p->insns[i]; + const BlackwellIRInsn *b = &p->insns[i + 1]; + int cls = fixed_pair_class(a->op); + if (!cls || cls != fixed_pair_class(b->op) || a->op == BW_IR_IADD3 || + a->predicate_p0 || b->predicate_p0 || ((a->control >> 9) & 15u) != 6) + continue; + a->control = (a->control & ~(15u << 9)) | (5u << 9); + } +} + +static int build_kernel(OmegaGpuEwOp op, int mutant, OmegaBlackwellKernel *kernel, BlackwellIRProgram *prog_out, int test_path) { + BlackwellIRProgram *prog = calloc(1, sizeof *prog); + if (!prog) return OMEGA_GPU_EW_CODEGEN_FAIL; + omega_bw_ir_init(prog); + Em e; memset(&e, 0, sizeof e); e.p = prog; e.test_path = test_path; + /* mutant 1 = the op's math mutant (as before); 2 = the join (BSYNC) dropped; 3 = the BSSY + * names the wrong join. 2 and 3 only change kernels that open a region. */ + e.rg_mutant = mutant == 2 ? 1 : mutant == 3 ? 2 : 0; + int math = mutant == 1; + switch (op) { + case OMEGA_GPU_EW_EX2: gen_ex2(&e, math); break; + case OMEGA_GPU_EW_XCHG: gen_xchg(&e, math); break; + case OMEGA_GPU_EW_RMSNORM: gen_rmsnorm(&e, math); break; + case OMEGA_GPU_EW_ROPE: gen_rope(&e, math); break; + case OMEGA_GPU_EW_SWIGLU: gen_swiglu(&e, math); break; + case OMEGA_GPU_EW_PRIME_SIEVE: gen_prime_sieve(&e, math, 0, 0); break; + case OMEGA_GPU_EW_PRIME_SIEVE_SHARED: gen_prime_sieve(&e, math, 1, 0); break; + case OMEGA_GPU_EW_PRIME_SIEVE_DIV1: gen_prime_sieve(&e, math, 0, 1); break; + case OMEGA_GPU_EW_PRIME_SIEVE_SHARED_DIV1: gen_prime_sieve(&e, math, 1, 1); break; + case OMEGA_GPU_EW_PRIME_SIEVE_DIV2: gen_prime_sieve(&e, math, 0, 2); break; + case OMEGA_GPU_EW_PRIME_SIEVE_SHARED_DIV2: gen_prime_sieve(&e, math, 1, 2); break; + case OMEGA_GPU_EW_RC_DIAMOND: gen_rc_diamond(&e, math); break; + case OMEGA_GPU_EW_RC_LOOPBREAK: gen_rc_loopbreak(&e, math); break; + case OMEGA_GPU_EW_RC_NESTED: gen_rc_nested(&e, math); break; + case OMEGA_GPU_EW_RC_EXIT_BAR: gen_rc_exit_bar(&e, math); break; + case OMEGA_GPU_EW_RC_REFUSE_EXIT_SHFL: gen_rc_exit_bar(&e, 2); break; + case OMEGA_GPU_EW_RC_DEPTH16: gen_rc_depth(&e, BW_RECONV_MAX_BAR); break; + case OMEGA_GPU_EW_RC_REFUSE_DEPTH17: gen_rc_depth(&e, BW_RECONV_MAX_BAR + 1); break; + case OMEGA_GPU_EW_RC_REFUSE_EXIT_IN_REGION: case OMEGA_GPU_EW_RC_REFUSE_BAR_IN_REGION: + case OMEGA_GPU_EW_RC_REFUSE_SHFL_IN_REGION: case OMEGA_GPU_EW_RC_REFUSE_UNCLOSED: + case OMEGA_GPU_EW_RC_REFUSE_JOIN_ONLY: gen_rc_refuse(&e, op); break; + default: e.err = -1; break; + } + if (e.rg.depth != 0) e.err = -1; /* a region left open has no join: refuse the build */ + schedule_fixed_pairs(prog); + int rc = OMEGA_GPU_EW_CODEGEN_FAIL; + int ra_rc = (e.err == 0) ? omega_bw_regalloc_solve(prog) : -1; + int loop_rc = (ra_rc == 0) ? check_loop_invariant(&e) : -1; +#ifdef OMEGA_EW_DEBUG + fprintf(stderr, "ew codegen op=%d mutant=%d emit_err=%d insns=%zu vregs=%d regalloc=%d loops=%d peak_gpr=%u\n", + (int)op, mutant, e.err, prog->count, prog->regalloc.num_vregs, ra_rc, loop_rc, prog->regalloc.peak_gpr_usage); +#endif + if (e.err == 0 && ra_rc == 0 && loop_rc == 0 && prog->regalloc.peak_gpr_usage <= EW_GPR_BUDGET) { + size_t max_bytes = (size_t)BW_MAX_IR_INSNS * 16; + memset(kernel, 0, sizeof *kernel); + kernel->code = malloc(max_bytes); + size_t emitted = 0; + if (kernel->code && omega_bw_encode_program(prog, kernel->code, max_bytes, &emitted) == 0) { + kernel->code_size = emitted; + kernel->insn_count = emitted / 16; + kernel->gpr_count = prog->regalloc.peak_gpr_usage; + kernel->uniform_gpr_count = prog->regalloc.peak_ugpr_usage; + sha256_hash(kernel->code, kernel->code_size, kernel->code_digest); + if (prog_out) *prog_out = *prog; + rc = OMEGA_GPU_EW_OK; + } else { +#ifdef OMEGA_EW_DEBUG + fprintf(stderr, "ew codegen op=%d: encode failed (an instruction form the encoder refuses)\n", (int)op); +#endif + free(kernel->code); kernel->code = NULL; + } + } + free(prog); + return rc; +} + +/* --------------------------------------------------------- kernel cache */ + +/* One kernel per (op, mutant); the device copy is uploaded once and stays + * resident for the process (cut 4b: no per-call code page). */ +typedef struct { int used; int op; int mutant; OmegaBlackwellKernel kernel; NvrmMem code; } Slot; +static int g_mutant_op; /* 0 = none */ +static int g_mutant_level = 1; /* the mutant number built for g_mutant_op (1 math, 2 join dropped, 3 join corrupted) */ +static Slot g_cache[8]; +static OmegaGpuScratch g_a, g_b, g_c; /* staging, grown to the high-water mark, reused per call */ +static bool g_hooked; +#define LOCK() omega_gpu_session_lock() +#define UNLOCK() omega_gpu_session_unlock() + +static void cache_clear_locked(void) { + for (size_t i = 0; i < sizeof g_cache / sizeof g_cache[0]; i++) + if (g_cache[i].used) { free(g_cache[i].kernel.code); omega_gpu_session_free(&g_cache[i].code); memset(&g_cache[i], 0, sizeof g_cache[i]); } +} + +void omega_gpu_elementwise_cache_clear(void) { + LOCK(); + cache_clear_locked(); + UNLOCK(); +} + +/* Called by the session (lock held, device still open) right before it closes. */ +static void on_session_close(void) { + cache_clear_locked(); + omega_gpu_session_scratch_free(&g_a); omega_gpu_session_scratch_free(&g_b); omega_gpu_session_scratch_free(&g_c); +} + +void omega_gpu_elementwise_test_set_mutant(int op) { + LOCK(); + g_mutant_op = op; g_mutant_level = 1; + cache_clear_locked(); + UNLOCK(); +} +void omega_gpu_elementwise_test_set_mutant_level(int op, int level) { + LOCK(); + g_mutant_op = op; g_mutant_level = level < 1 ? 1 : level > 3 ? 3 : level; + cache_clear_locked(); + UNLOCK(); +} + +/* Caller holds the lock. Builds the kernel if needed; uploads the device copy when a + * device is open (upload = false lets the host-only codegen path run without a chip). */ +static int kernel_for(OmegaGpuEwOp op, bool upload, Slot **out, bool *hit) { + int mutant = (g_mutant_op == (int)op) ? g_mutant_level : 0; + Slot *s = NULL; + for (size_t i = 0; i < sizeof g_cache / sizeof g_cache[0]; i++) + if (g_cache[i].used && g_cache[i].op == (int)op && g_cache[i].mutant == mutant) { s = &g_cache[i]; *hit = true; break; } + if (!s) { + OmegaBlackwellKernel k; + int rc = build_kernel(op, mutant, &k, NULL, 0); + if (rc != OMEGA_GPU_EW_OK) return rc; + size_t slot = 0; + for (size_t i = 0; i < sizeof g_cache / sizeof g_cache[0]; i++) if (!g_cache[i].used) { slot = i; break; } + s = &g_cache[slot]; + if (s->used) { free(s->kernel.code); omega_gpu_session_free(&s->code); } /* full: evict slot 0 */ + memset(s, 0, sizeof *s); + s->used = 1; s->op = (int)op; s->mutant = mutant; s->kernel = k; + *hit = false; + } + if (upload && !s->code.cpu) { + if (omega_gpu_session_alloc(s->kernel.code_size, &s->code) != 0) return OMEGA_GPU_EW_CHIP_FAIL; + memcpy(s->code.cpu, s->kernel.code, s->kernel.code_size); + __asm__ volatile("dsb sy" ::: "memory"); + } + *out = s; + return OMEGA_GPU_EW_OK; +} + +int omega_gpu_elementwise_codegen(OmegaGpuEwOp op, const uint32_t *dims, float eps, OmegaBlackwellKernel *kernel) { + (void)dims; (void)eps; /* kernels read their shape from the argument words */ + if (!kernel) return OMEGA_GPU_EW_BAD_ARGS; + LOCK(); + int mutant = (g_mutant_op == (int)op) ? g_mutant_level : 0; + UNLOCK(); + return build_kernel(op, mutant, kernel, NULL, 0); +} + +int omega_gpu_elementwise_codegen_ir(OmegaGpuEwOp op, int mutant, void *prog, OmegaBlackwellKernel *kernel) { + if (!kernel) return OMEGA_GPU_EW_BAD_ARGS; + if (mutant < 0 || mutant > 3) return OMEGA_GPU_EW_BAD_ARGS; + return build_kernel(op, mutant, kernel, (BlackwellIRProgram *)prog, 1); +} + +const char *omega_gpu_elementwise_last_error(void) { return omega_gpu_session_last_error(); } + +/* ------------------------------------------------------------ launcher */ + +#define EW_POISON 0xffbadbadu /* a NaN pattern no kernel here produces on purpose */ +#define EW_WAIT_MS 600000ull /* like main's load/store launchers; a 5 s wait failed under contention (omega#198) */ + +static uint64_t now_ns(void) { + struct timespec t; clock_gettime(CLOCK_MONOTONIC, &t); + return (uint64_t)t.tv_sec * 1000000000ULL + (uint64_t)t.tv_nsec; +} + +/* Caller holds the lock. Opens the shared device on first use. */ +static int dev_open_locked(void) { + if (!g_hooked) { (void)omega_gpu_session_on_close(on_session_close); g_hooked = true; } + return omega_gpu_session_open() ? OMEGA_GPU_EW_OK : OMEGA_GPU_EW_CHIP_FAIL; +} + +/* One launch: a and b staged in, c out (c_bytes/4 words, poison-filled first), through + * the shared session (omega_gpu_session_launch: shader-cache invalidate, constant bank, + * QMD0/QMD1, first marker, L2_FLUSH_DIRTY, second marker), then readback and the + * poison scan. b may be NULL when b_fill wrote the staging buffer already. */ +static int run_abc(const Slot *ks, const void *a, size_t a_bytes, const void *b, size_t b_bytes, + void *c, size_t c_bytes, uint32_t threads_x, uint32_t grid_x, + const uint32_t params[4], OmegaGpuEwInfo *info) { + if (omega_gpu_session_scratch(&g_a, a_bytes) || omega_gpu_session_scratch(&g_b, b_bytes) || omega_gpu_session_scratch(&g_c, c_bytes)) + return OMEGA_GPU_EW_CHIP_FAIL; + memcpy(g_a.mem.cpu, a, a_bytes); + if (b) memcpy(g_b.mem.cpu, b, b_bytes); + size_t c_words = c_bytes / 4; + uint32_t *c_dev = (uint32_t *)g_c.mem.cpu; + for (size_t i = 0; i < c_words; i++) c_dev[i] = EW_POISON; + __asm__ volatile("dsb sy" ::: "memory"); + + uint32_t args[OMEGA_BW_CBANK_MATMUL_ARGS_WORDS]; + args[0] = (uint32_t)g_a.mem.va; args[1] = (uint32_t)(g_a.mem.va >> 32); + args[2] = (uint32_t)g_b.mem.va; args[3] = (uint32_t)(g_b.mem.va >> 32); + args[4] = (uint32_t)g_c.mem.va; args[5] = (uint32_t)(g_c.mem.va >> 32); + args[6] = params[0]; args[7] = params[1]; args[8] = params[2]; args[9] = params[3]; + OmegaGpuLaunch L = { + .code_va = ks->code.va, .gpr_count = EW_GPR_BUDGET, .threads_x = threads_x, .threads_y = 1, + .grid_x = grid_x, .grid_y = 1, .num_elements = c_words, + .args = args, .n_args = OMEGA_BW_CBANK_MATMUL_ARGS_WORDS, .timeout_ms = EW_WAIT_MS, + }; + uint64_t elapsed = 0; uint32_t marker = 0; + if (omega_gpu_session_launch(&L, &elapsed, &marker) != 0) return OMEGA_GPU_EW_CHIP_FAIL; + + memcpy(c, g_c.mem.cpu, c_bytes); + uint32_t unwritten = 0; + for (size_t i = 0; i < c_words; i++) if (((uint32_t *)c)[i] == EW_POISON) unwritten++; + if (info) { + info->chip_calls++; + info->elapsed_ns += elapsed; + info->completion_marker = marker; + info->unwritten_words += unwritten; + info->threads_per_cta = threads_x; + info->ctas_last_launch = grid_x; + } + return unwritten ? OMEGA_GPU_EW_UNWRITTEN : OMEGA_GPU_EW_OK; +} + +/* ------------------------------------------------------- public entries */ + +static void info_begin(OmegaGpuEwInfo *info, const OmegaBlackwellKernel *k, bool hit) { + if (!info) return; + memset(info, 0, sizeof *info); + info->kernel_cache_hit = hit; + info->gpr_count = k->gpr_count; + info->insn_count = (uint32_t)k->insn_count; + snprintf(info->target_chip, sizeof info->target_chip, "NVIDIA DGX Spark (Grace Blackwell GB10)"); + info->sm_architecture = 121; +} +static uint32_t f2u(float f) { uint32_t u; memcpy(&u, &f, 4); return u; } +static void info_end(OmegaGpuEwInfo *info, uint64_t t0) { if (info) info->call_ns = now_ns() - t0; } + +/* Common prologue of every chip entry: lock, open, kernel (resident), info. */ +static int begin(OmegaGpuEwOp op, Slot **ks, OmegaGpuEwInfo *info) { + LOCK(); + int rc = dev_open_locked(); + bool hit = false; + if (rc == OMEGA_GPU_EW_OK) rc = kernel_for(op, true, ks, &hit); + if (rc != OMEGA_GPU_EW_OK) { UNLOCK(); return rc; } + info_begin(info, &(*ks)->kernel, hit); + return OMEGA_GPU_EW_OK; +} + +int omega_gpu_rmsnorm_f32(uint32_t rows, uint32_t dim, const float *x, const float *weight, + float eps, float *out, OmegaGpuEwInfo *info) { + uint64_t t0 = now_ns(); + if (!x || !weight || !out || rows == 0 || dim == 0 || dim % OMEGA_GPU_EW_THREADS != 0) return OMEGA_GPU_EW_BAD_ARGS; + if (dim > OMEGA_GPU_EW_MAX_DIM) return OMEGA_GPU_EW_TOO_LARGE; + Slot *k = NULL; + int rc = begin(OMEGA_GPU_EW_RMSNORM, &k, info); + if (rc != OMEGA_GPU_EW_OK) return rc; + uint32_t params[4] = { dim / OMEGA_GPU_EW_THREADS, f2u(1.0f / (float)dim), f2u(eps), dim }; + for (uint32_t r0 = 0; r0 < rows && rc == OMEGA_GPU_EW_OK; r0 += OMEGA_GPU_EW_MAX_CTAS) { + uint32_t nr = rows - r0 < OMEGA_GPU_EW_MAX_CTAS ? rows - r0 : OMEGA_GPU_EW_MAX_CTAS; + size_t bytes = (size_t)nr * dim * 4; + rc = run_abc(k, x + (size_t)r0 * dim, bytes, weight, (size_t)dim * 4, out + (size_t)r0 * dim, bytes, + OMEGA_GPU_EW_THREADS, nr, params, info); + } + UNLOCK(); + info_end(info, t0); + return rc; +} + +int omega_gpu_rope_f32(uint32_t heads, uint32_t head_dim, const float *v, + const float *cos_half, const float *sin_half, float *out, OmegaGpuEwInfo *info) { + uint64_t t0 = now_ns(); + if (!v || !cos_half || !sin_half || !out || heads == 0 || head_dim < 2 || (head_dim & 1)) return OMEGA_GPU_EW_BAD_ARGS; + if (head_dim > 2048) return OMEGA_GPU_EW_TOO_LARGE; + Slot *k = NULL; + int rc = begin(OMEGA_GPU_EW_ROPE, &k, info); + if (rc != OMEGA_GPU_EW_OK) return rc; + uint32_t half = head_dim / 2; + uint32_t params[4] = { head_dim, half, 0, 0 }; + for (uint32_t h0 = 0; h0 < heads && rc == OMEGA_GPU_EW_OK; h0 += OMEGA_GPU_EW_MAX_CTAS) { + uint32_t nh = heads - h0 < OMEGA_GPU_EW_MAX_CTAS ? heads - h0 : OMEGA_GPU_EW_MAX_CTAS; + size_t bytes = (size_t)nh * head_dim * 4; + /* table: cos in the first half of each head, sin in the second, written straight into + * the staging buffer (one copy per head of this launch) */ + if (omega_gpu_session_scratch(&g_b, bytes) != 0) { rc = OMEGA_GPU_EW_CHIP_FAIL; break; } + float *table = g_b.mem.cpu; + for (uint32_t h = 0; h < nh; h++) { + memcpy(table + (size_t)h * head_dim, cos_half, half * sizeof *table); + memcpy(table + (size_t)h * head_dim + half, sin_half, half * sizeof *table); + } + /* out may alias v: v is copied into the staging buffer before the result lands in out */ + rc = run_abc(k, v + (size_t)h0 * head_dim, bytes, NULL, bytes, out + (size_t)h0 * head_dim, bytes, half, nh, params, info); + } + UNLOCK(); + info_end(info, t0); + return rc; +} + +static int run_1d(OmegaGpuEwOp op, uint32_t n, const void *a, const void *b, void *c, OmegaGpuEwInfo *info) { + uint64_t t0 = now_ns(); + Slot *k = NULL; + int rc = begin(op, &k, info); + if (rc != OMEGA_GPU_EW_OK) return rc; + const uint32_t chunk = OMEGA_GPU_EW_MAX_CTAS * OMEGA_GPU_EW_THREADS; + for (uint32_t i0 = 0; i0 < n && rc == OMEGA_GPU_EW_OK; i0 += chunk) { + uint32_t ni = n - i0 < chunk ? n - i0 : chunk; + uint32_t grid = (ni + OMEGA_GPU_EW_THREADS - 1) / OMEGA_GPU_EW_THREADS; + uint32_t params[4] = { ni, 0, 0, 0 }; + size_t bytes = (size_t)ni * 4; + rc = run_abc(k, (const uint8_t *)a + (size_t)i0 * 4, bytes, b ? (const uint8_t *)b + (size_t)i0 * 4 : a, bytes, + (uint8_t *)c + (size_t)i0 * 4, bytes, OMEGA_GPU_EW_THREADS, grid, params, info); + } + UNLOCK(); + info_end(info, t0); + return rc; +} + +int omega_gpu_swiglu_f32(uint32_t n, const float *gate, const float *up, float *out, OmegaGpuEwInfo *info) { + if (!gate || !up || !out || n == 0) return OMEGA_GPU_EW_BAD_ARGS; + return run_1d(OMEGA_GPU_EW_SWIGLU, n, gate, up, out, info); +} +int omega_gpu_ex2_f32(uint32_t n, const float *x, float *out, OmegaGpuEwInfo *info) { + if (!x || !out || n == 0) return OMEGA_GPU_EW_BAD_ARGS; + return run_1d(OMEGA_GPU_EW_EX2, n, x, NULL, out, info); +} +int omega_gpu_shared_xchg_u32(uint32_t n, const uint32_t *x, uint32_t *out, OmegaGpuEwInfo *info) { + if (!x || !out || n == 0 || n % OMEGA_GPU_EW_THREADS != 0) return OMEGA_GPU_EW_BAD_ARGS; + return run_1d(OMEGA_GPU_EW_XCHG, n, x, NULL, out, info); +} +int omega_gpu_reconv_probe_u32(OmegaGpuEwOp op, uint32_t n, const uint32_t *a, const uint32_t *b, uint32_t *out, OmegaGpuEwInfo *info) { + if (!a || !b || !out || n == 0) return OMEGA_GPU_EW_BAD_ARGS; + switch (op) { + case OMEGA_GPU_EW_RC_DIAMOND: case OMEGA_GPU_EW_RC_LOOPBREAK: case OMEGA_GPU_EW_RC_NESTED: + case OMEGA_GPU_EW_RC_EXIT_BAR: case OMEGA_GPU_EW_RC_DEPTH16: break; + default: return OMEGA_GPU_EW_BAD_ARGS; + } + return run_1d(op, n, a, b, out, info); +} + +const char *omega_gpu_elementwise_rc_name(int rc) { + switch (rc) { + case OMEGA_GPU_EW_OK: return "OK"; + case OMEGA_GPU_EW_BAD_ARGS: return "BAD_ARGS"; + case OMEGA_GPU_EW_TOO_LARGE: return "TOO_LARGE"; + case OMEGA_GPU_EW_CODEGEN_FAIL: return "CODEGEN_FAIL"; + case OMEGA_GPU_EW_CHIP_FAIL: return "CHIP_FAIL"; + case OMEGA_GPU_EW_UNWRITTEN: return "UNWRITTEN"; + default: return "UNKNOWN"; + } +} diff --git a/evidence/gpu-reconv-308/T1a-GPU-RECONV-0a1812f/omega_gpu_elementwise_api.h b/evidence/gpu-reconv-308/T1a-GPU-RECONV-0a1812f/omega_gpu_elementwise_api.h new file mode 100644 index 000000000..8b92c839d --- /dev/null +++ b/evidence/gpu-reconv-308/T1a-GPU-RECONV-0a1812f/omega_gpu_elementwise_api.h @@ -0,0 +1,177 @@ +#ifndef OMEGA_GPU_ELEMENTWISE_API_H +#define OMEGA_GPU_ELEMENTWISE_API_H +/* + * omega_gpu_elementwise_api: native (no CUDA) elementwise and row-reduction + * kernels for the AIEN inference stack (FB-1 cut 4, 2026-10-04). + * + * Three inference ops plus two probe ops, every kernel generated by our own + * codegen (src/omega_blackwell_codegen.c IR) and launched through the same + * chip-proven sequence as the tensor matmul path: all buffers GPU-uncached + * (nvos.h NVOS32_ATTR2_GPU_CACHEABLE: system memory cached by the GPU is not + * coherent with CPU mappings), first marker, L2_FLUSH_DIRTY, second marker + * (C3 tail), poison-filled output scanned for unwritten words after readback. + * + * The math follows aien-sovereign-core crates/aien-inference-abi/src/tensor.rs + * (ReferenceCpuBackend), read 2026-10-04 at main 5fdab70: + * rmsnorm: scale = 1/sqrt(mean(x^2) + eps) (oracle sums in f64; the chip + * sums in f32 and uses MUFU.RSQ refined by one Newton step), + * out[i] = (x[i] * scale) * weight[i] in that order. + * rope: rotate_half pairs (i, i + head_dim/2) per head, cos/sin tables + * supplied by the host as f32 (the oracle casts f64 cos/sin to f32 + * before the f32 math): out0 = q0*cos - q1*sin, out1 = q0*sin + q1*cos, + * emitted as FMUL, FMUL, FADD (no fused multiply-add), so the chip + * result is bit-identical to the f32 oracle. + * swiglu: out = silu(gate) * up, silu(g) = g / (1 + exp(-g)); the chip uses + * MUFU.EX2(g * -log2(e)) and MUFU.RCP (approximate units), the + * oracle computes in f64. Tolerance is stated per op in the test. + * + * Cut 4b (2026-10-04): the device, channel and launch scratch are the process-wide + * session shared with the matmul and attention APIs (omega_gpu_session.h): one open + * per process instead of one per call (~40 ms), kernels resident after first use, + * staging buffers reused at their high-water mark. Calls serialise on one lock. + * + * Launch envelope: at most OMEGA_GPU_EW_MAX_CTAS CTAs per launch (the matmul + * sweep of 2026-10-03 lost whole tiles at 128 CTAs, investigation I42); the + * API splits larger work on the host and makes several launches. + */ +#include +#include +#include +#include "omega_blackwell_matmul.h" /* OmegaBlackwellKernel */ + +#ifdef __cplusplus +extern "C" { +#endif + +enum { + OMEGA_GPU_EW_OK = 0, + OMEGA_GPU_EW_BAD_ARGS = -1, /* NULL pointer, zero dimension, or a shape rule below broken */ + OMEGA_GPU_EW_TOO_LARGE = -2, /* dimension outside the codegen limits */ + OMEGA_GPU_EW_CODEGEN_FAIL = -3, /* our codegen refused or exceeded the register budget */ + OMEGA_GPU_EW_CHIP_FAIL = -4, /* device open, submit, marker or release failed */ + OMEGA_GPU_EW_UNWRITTEN = -5 /* output words still equal to their poison after the run */ +}; + +/* Kernel families. The two probes exist to prove the new instructions on the + * chip with the smallest possible kernels (and are kept for regression). */ +typedef enum { + OMEGA_GPU_EW_EX2 = 1, /* out[i] = 2^a[i] (probe for MUFU.EX2) */ + OMEGA_GPU_EW_XCHG, /* out[i] = a[i ^ (T-1)] inside each CTA (probe for STS/BAR.SYNC/LDS) */ + OMEGA_GPU_EW_RMSNORM, + OMEGA_GPU_EW_ROPE, + OMEGA_GPU_EW_SWIGLU, + OMEGA_GPU_EW_PRIME_SIEVE, /* prime race (2026-10-05): word-parallel odd-only sieve, codegen only + * here; launched by bench/prime_race/gb10_native.c (argument words in + * gen_prime_sieve's comment) */ + OMEGA_GPU_EW_PRIME_SIEVE_SHARED, /* the same sieve with the table staged in shared memory (C3) */ + /* Structured warp reconvergence (omega #308). The four sieves below are the production sieve + * with its lane-dependent loops put back inside BSSY/BSYNC regions (regression R1): DIV1 wraps + * the original mark loop (omega 888a011), DIV2 also wraps the prime loop (nested regions). */ + OMEGA_GPU_EW_PRIME_SIEVE_DIV1, + OMEGA_GPU_EW_PRIME_SIEVE_SHARED_DIV1, + OMEGA_GPU_EW_PRIME_SIEVE_DIV2, + OMEGA_GPU_EW_PRIME_SIEVE_SHARED_DIV2, + /* Probes for omega_gpu_reconv_probe_u32 (u32 in a, b; u32 out; oracle in tests/gpu_reconv_test.c) */ + OMEGA_GPU_EW_RC_DIAMOND, /* R3: if/else, sides of different length, SHFL after the join */ + OMEGA_GPU_EW_RC_LOOPBREAK, /* R4: loop with a lane-dependent break */ + OMEGA_GPU_EW_RC_NESTED, /* R5: a lane-dependent loop region inside an if region */ + OMEGA_GPU_EW_RC_EXIT_BAR, /* R6: lanes past the end + STS/BAR.SYNC/LDS exchange + SHFL, lanes exit last */ + OMEGA_GPU_EW_RC_DEPTH16, /* 16 nested regions: every barrier register B0..B15 in use */ + /* Codegen refusals: omega_gpu_elementwise_codegen_ir must return CODEGEN_FAIL for each */ + OMEGA_GPU_EW_RC_REFUSE_DEPTH17, /* a 17th nested region: no barrier register left */ + OMEGA_GPU_EW_RC_REFUSE_EXIT_SHFL, /* lanes EXIT, then BAR.SYNC/SHFL on the rest */ + OMEGA_GPU_EW_RC_REFUSE_EXIT_IN_REGION, /* EXIT inside an open region */ + OMEGA_GPU_EW_RC_REFUSE_BAR_IN_REGION, /* BAR.SYNC inside an open region */ + OMEGA_GPU_EW_RC_REFUSE_SHFL_IN_REGION, /* SHFL inside an open region */ + OMEGA_GPU_EW_RC_REFUSE_UNCLOSED, /* region begun, never joined */ + OMEGA_GPU_EW_RC_REFUSE_JOIN_ONLY /* BSYNC with no BSSY */ +} OmegaGpuEwOp; + +/* Shape rules (checked, refused with BAD_ARGS / TOO_LARGE): + * rmsnorm: dim % OMEGA_GPU_EW_THREADS == 0, dim <= OMEGA_GPU_EW_MAX_DIM, rows >= 1 + * rope: head_dim even, 2 <= head_dim <= 2048 (head_dim/2 threads per CTA), heads >= 1 + * swiglu: n >= 1 (the kernel guards the tail with a predicated EXIT) + * ex2: n >= 1; xchg: n % OMEGA_GPU_EW_THREADS == 0 */ +#define OMEGA_GPU_EW_THREADS 128u +#define OMEGA_GPU_EW_MAX_DIM 16384u +#define OMEGA_GPU_EW_MAX_CTAS 64u + +typedef struct { + uint32_t chip_calls; /* launches made */ + uint64_t elapsed_ns; /* sum of launch-to-second-marker over the launches */ + uint32_t completion_marker; /* observed first marker word of the last launch */ + uint32_t unwritten_words; /* output words still equal to poison (0 on success) */ + bool kernel_cache_hit; /* the shape's kernel was already generated in this process */ + uint32_t gpr_count; /* registers the generated kernel uses */ + uint32_t insn_count; + uint32_t threads_per_cta; + uint32_t ctas_last_launch; + char target_chip[64]; + uint32_t sm_architecture; + uint64_t call_ns; /* cut 4b: wall time of the whole API call (staging, launches, copy-out) */ +} OmegaGpuEwInfo; + +/* out[r*dim + i] = x[r*dim + i] * scale_r * weight[i]; scale_r = 1/sqrt(mean_i(x^2) + eps). */ +int omega_gpu_rmsnorm_f32(uint32_t rows, uint32_t dim, const float *x, const float *weight, + float eps, float *out, OmegaGpuEwInfo *info); + +/* v holds `heads` heads of head_dim values back to back; cos_half and sin_half + * hold head_dim/2 values each (cos(pos*freq_i), sin(pos*freq_i)); out gets the + * rotated heads (may alias v: the API copies through its own buffers). */ +int omega_gpu_rope_f32(uint32_t heads, uint32_t head_dim, const float *v, + const float *cos_half, const float *sin_half, float *out, + OmegaGpuEwInfo *info); + +/* out[i] = silu(gate[i]) * up[i] for i < n. */ +int omega_gpu_swiglu_f32(uint32_t n, const float *gate, const float *up, float *out, + OmegaGpuEwInfo *info); + +/* Probes. ex2: out[i] = 2^x[i]. xchg: out[i] = x[i ^ (OMEGA_GPU_EW_THREADS-1)] within each CTA. */ +int omega_gpu_ex2_f32(uint32_t n, const float *x, float *out, OmegaGpuEwInfo *info); +int omega_gpu_shared_xchg_u32(uint32_t n, const uint32_t *x, uint32_t *out, OmegaGpuEwInfo *info); +/* Reconvergence probes (op = one of OMEGA_GPU_EW_RC_DIAMOND .. OMEGA_GPU_EW_RC_DEPTH16, else + * BAD_ARGS). n >= 1, any n: lanes past the end take part in the warp step and exit before + * their store. out[i] = r_i + 4099 * r_(i+1 in warp); r per op in src/omega_gpu_elementwise_api.c. */ +int omega_gpu_reconv_probe_u32(OmegaGpuEwOp op, uint32_t n, const uint32_t *a, const uint32_t *b, + uint32_t *out, OmegaGpuEwInfo *info); + +/* Generate (without launching) the kernel for one op. Kernels read their shape + * from the argument words, so one kernel per op serves every shape; dims and eps + * are accepted for forward compatibility and ignored today (may be NULL / 0). + * Lets tests disassemble the bytes offline (nvdisasm) and check the register + * budget. Caller frees kernel->code. */ +int omega_gpu_elementwise_codegen(OmegaGpuEwOp op, const uint32_t *dims, float eps, + OmegaBlackwellKernel *kernel); + +/* Same, with the mutant chosen by the caller (0 = production kernel, 1 = the op's math mutant, + * 2 = the reconvergence join (BSYNC) dropped, 3 = the BSSY names the wrong join; 2 and 3 change + * only kernels that open a region, the host simulator must catch both) and, when prog is + * not NULL, a copy of the allocated IR program (a BlackwellIRProgram, post register + * allocation) for a host IR simulator. Does not touch the device or the kernel cache. + * Caller frees kernel->code. */ +int omega_gpu_elementwise_codegen_ir(OmegaGpuEwOp op, int mutant, void *prog, + OmegaBlackwellKernel *kernel); + +/* TEST ONLY. Selects a deliberately wrong kernel for one op (0 clears). Each + * mutant changes the math by far more than the op's tolerance, so a parity + * check that still passes against it proves nothing. Clears the kernel cache. + * EX2: computes 2^(x+1). XCHG: reads its own shared word (no exchange). + * RMSNORM: the warp tree drops its last SHFL step (half the lanes go missing). + * ROPE: out0 = q0*cos + q1*sin. SWIGLU: exp(+g) instead of exp(-g). */ +void omega_gpu_elementwise_test_set_mutant(int op); +/* TEST ONLY. Like test_set_mutant, choosing the mutant number (1 math, 2 join dropped, 3 join + * corrupted) that the next launch of `op` builds; lets a chip test observe what a missing + * reconvergence does (the simulator must have flagged it first). */ +void omega_gpu_elementwise_test_set_mutant_level(int op, int level); + +const char *omega_gpu_elementwise_rc_name(int rc); +void omega_gpu_elementwise_cache_clear(void); + +/* Stage name of the most recent CHIP_FAIL ("" if none), shared with the other GPU APIs + * (omega_gpu_session_last_error); an open failure carries the driver text. */ +const char *omega_gpu_elementwise_last_error(void); + +#ifdef __cplusplus +} +#endif +#endif diff --git a/evidence/gpu-reconv-308/T1a-GPU-RECONV-0a1812f/receipt.json b/evidence/gpu-reconv-308/T1a-GPU-RECONV-0a1812f/receipt.json new file mode 100644 index 000000000..28f01d1fc --- /dev/null +++ b/evidence/gpu-reconv-308/T1a-GPU-RECONV-0a1812f/receipt.json @@ -0,0 +1 @@ +{"schema":"OMEGA_GPU_RECONV_V1","issue":308,"comparison":"exact u32","cases":[{"op":"rc_diamond","regression":"R3","n":1,"rc":"OK","violations":0,"pass":true,"chip_elapsed_ns":112065,"chip_calls":1,"unwritten_words":0,"cache_hit":false},{"op":"rc_diamond","regression":"R3","n":5,"rc":"OK","violations":0,"pass":true,"chip_elapsed_ns":104033,"chip_calls":1,"unwritten_words":0,"cache_hit":true},{"op":"rc_diamond","regression":"R3","n":31,"rc":"OK","violations":0,"pass":true,"chip_elapsed_ns":103185,"chip_calls":1,"unwritten_words":0,"cache_hit":true},{"op":"rc_diamond","regression":"R3","n":32,"rc":"OK","violations":0,"pass":true,"chip_elapsed_ns":103537,"chip_calls":1,"unwritten_words":0,"cache_hit":true},{"op":"rc_diamond","regression":"R3","n":33,"rc":"OK","violations":0,"pass":true,"chip_elapsed_ns":103265,"chip_calls":1,"unwritten_words":0,"cache_hit":true},{"op":"rc_diamond","regression":"R3","n":63,"rc":"OK","violations":0,"pass":true,"chip_elapsed_ns":102961,"chip_calls":1,"unwritten_words":0,"cache_hit":true},{"op":"rc_diamond","regression":"R3","n":64,"rc":"OK","violations":0,"pass":true,"chip_elapsed_ns":103473,"chip_calls":1,"unwritten_words":0,"cache_hit":true},{"op":"rc_diamond","regression":"R3","n":65,"rc":"OK","violations":0,"pass":true,"chip_elapsed_ns":103264,"chip_calls":1,"unwritten_words":0,"cache_hit":true},{"op":"rc_diamond","regression":"R3","n":96,"rc":"OK","violations":0,"pass":true,"chip_elapsed_ns":103601,"chip_calls":1,"unwritten_words":0,"cache_hit":true},{"op":"rc_diamond","regression":"R3","n":127,"rc":"OK","violations":0,"pass":true,"chip_elapsed_ns":103281,"chip_calls":1,"unwritten_words":0,"cache_hit":true},{"op":"rc_diamond","regression":"R3","n":128,"rc":"OK","violations":0,"pass":true,"chip_elapsed_ns":103265,"chip_calls":1,"unwritten_words":0,"cache_hit":true},{"op":"rc_diamond","regression":"R3","n":129,"rc":"OK","violations":0,"pass":true,"chip_elapsed_ns":103233,"chip_calls":1,"unwritten_words":0,"cache_hit":true},{"op":"rc_diamond","regression":"R3","n":200,"rc":"OK","violations":0,"pass":true,"chip_elapsed_ns":102993,"chip_calls":1,"unwritten_words":0,"cache_hit":true},{"op":"rc_diamond","regression":"R3","n":1000,"rc":"OK","violations":0,"pass":true,"chip_elapsed_ns":103249,"chip_calls":1,"unwritten_words":0,"cache_hit":true},{"op":"rc_diamond","regression":"R3","n":4096,"rc":"OK","violations":0,"pass":true,"chip_elapsed_ns":104209,"chip_calls":1,"unwritten_words":0,"cache_hit":true},{"op":"rc_diamond","regression":"R3","n":8191,"rc":"OK","violations":0,"pass":true,"chip_elapsed_ns":104209,"chip_calls":1,"unwritten_words":0,"cache_hit":true},{"op":"rc_diamond","regression":"R3","n":8192,"rc":"OK","violations":0,"pass":true,"chip_elapsed_ns":103537,"chip_calls":1,"unwritten_words":0,"cache_hit":true},{"op":"rc_diamond","regression":"R3","n":8193,"rc":"OK","violations":0,"pass":true,"chip_elapsed_ns":206594,"chip_calls":2,"unwritten_words":0,"cache_hit":true},{"op":"rc_diamond","regression":"R3","n":20000,"rc":"OK","violations":0,"pass":true,"chip_elapsed_ns":310067,"chip_calls":3,"unwritten_words":0,"cache_hit":true},{"op":"rc_loopbreak","regression":"R4","n":1,"rc":"OK","violations":0,"pass":true,"chip_elapsed_ns":103217,"chip_calls":1,"unwritten_words":0,"cache_hit":false},{"op":"rc_loopbreak","regression":"R4","n":5,"rc":"OK","violations":0,"pass":true,"chip_elapsed_ns":103041,"chip_calls":1,"unwritten_words":0,"cache_hit":true},{"op":"rc_loopbreak","regression":"R4","n":31,"rc":"OK","violations":0,"pass":true,"chip_elapsed_ns":102961,"chip_calls":1,"unwritten_words":0,"cache_hit":true},{"op":"rc_loopbreak","regression":"R4","n":32,"rc":"OK","violations":0,"pass":true,"chip_elapsed_ns":103297,"chip_calls":1,"unwritten_words":0,"cache_hit":true},{"op":"rc_loopbreak","regression":"R4","n":33,"rc":"OK","violations":0,"pass":true,"chip_elapsed_ns":102977,"chip_calls":1,"unwritten_words":0,"cache_hit":true},{"op":"rc_loopbreak","regression":"R4","n":63,"rc":"OK","violations":0,"pass":true,"chip_elapsed_ns":103777,"chip_calls":1,"unwritten_words":0,"cache_hit":true},{"op":"rc_loopbreak","regression":"R4","n":64,"rc":"OK","violations":0,"pass":true,"chip_elapsed_ns":104849,"chip_calls":1,"unwritten_words":0,"cache_hit":true},{"op":"rc_loopbreak","regression":"R4","n":65,"rc":"OK","violations":0,"pass":true,"chip_elapsed_ns":103217,"chip_calls":1,"unwritten_words":0,"cache_hit":true},{"op":"rc_loopbreak","regression":"R4","n":96,"rc":"OK","violations":0,"pass":true,"chip_elapsed_ns":103137,"chip_calls":1,"unwritten_words":0,"cache_hit":true},{"op":"rc_loopbreak","regression":"R4","n":127,"rc":"OK","violations":0,"pass":true,"chip_elapsed_ns":102977,"chip_calls":1,"unwritten_words":0,"cache_hit":true},{"op":"rc_loopbreak","regression":"R4","n":128,"rc":"OK","violations":0,"pass":true,"chip_elapsed_ns":103089,"chip_calls":1,"unwritten_words":0,"cache_hit":true},{"op":"rc_loopbreak","regression":"R4","n":129,"rc":"OK","violations":0,"pass":true,"chip_elapsed_ns":103329,"chip_calls":1,"unwritten_words":0,"cache_hit":true},{"op":"rc_loopbreak","regression":"R4","n":200,"rc":"OK","violations":0,"pass":true,"chip_elapsed_ns":103265,"chip_calls":1,"unwritten_words":0,"cache_hit":true},{"op":"rc_loopbreak","regression":"R4","n":1000,"rc":"OK","violations":0,"pass":true,"chip_elapsed_ns":103073,"chip_calls":1,"unwritten_words":0,"cache_hit":true},{"op":"rc_loopbreak","regression":"R4","n":4096,"rc":"OK","violations":0,"pass":true,"chip_elapsed_ns":103024,"chip_calls":1,"unwritten_words":0,"cache_hit":true},{"op":"rc_loopbreak","regression":"R4","n":8191,"rc":"OK","violations":0,"pass":true,"chip_elapsed_ns":103377,"chip_calls":1,"unwritten_words":0,"cache_hit":true},{"op":"rc_loopbreak","regression":"R4","n":8192,"rc":"OK","violations":0,"pass":true,"chip_elapsed_ns":103009,"chip_calls":1,"unwritten_words":0,"cache_hit":true},{"op":"rc_loopbreak","regression":"R4","n":8193,"rc":"OK","violations":0,"pass":true,"chip_elapsed_ns":206866,"chip_calls":2,"unwritten_words":0,"cache_hit":true},{"op":"rc_loopbreak","regression":"R4","n":20000,"rc":"OK","violations":0,"pass":true,"chip_elapsed_ns":310819,"chip_calls":3,"unwritten_words":0,"cache_hit":true},{"op":"rc_nested","regression":"R5","n":1,"rc":"OK","violations":0,"pass":true,"chip_elapsed_ns":103089,"chip_calls":1,"unwritten_words":0,"cache_hit":false},{"op":"rc_nested","regression":"R5","n":5,"rc":"OK","violations":0,"pass":true,"chip_elapsed_ns":102945,"chip_calls":1,"unwritten_words":0,"cache_hit":true},{"op":"rc_nested","regression":"R5","n":31,"rc":"OK","violations":0,"pass":true,"chip_elapsed_ns":102961,"chip_calls":1,"unwritten_words":0,"cache_hit":true},{"op":"rc_nested","regression":"R5","n":32,"rc":"OK","violations":0,"pass":true,"chip_elapsed_ns":102961,"chip_calls":1,"unwritten_words":0,"cache_hit":true},{"op":"rc_nested","regression":"R5","n":33,"rc":"OK","violations":0,"pass":true,"chip_elapsed_ns":103313,"chip_calls":1,"unwritten_words":0,"cache_hit":true},{"op":"rc_nested","regression":"R5","n":63,"rc":"OK","violations":0,"pass":true,"chip_elapsed_ns":103248,"chip_calls":1,"unwritten_words":0,"cache_hit":true},{"op":"rc_nested","regression":"R5","n":64,"rc":"OK","violations":0,"pass":true,"chip_elapsed_ns":103185,"chip_calls":1,"unwritten_words":0,"cache_hit":true},{"op":"rc_nested","regression":"R5","n":65,"rc":"OK","violations":0,"pass":true,"chip_elapsed_ns":103153,"chip_calls":1,"unwritten_words":0,"cache_hit":true},{"op":"rc_nested","regression":"R5","n":96,"rc":"OK","violations":0,"pass":true,"chip_elapsed_ns":102945,"chip_calls":1,"unwritten_words":0,"cache_hit":true},{"op":"rc_nested","regression":"R5","n":127,"rc":"OK","violations":0,"pass":true,"chip_elapsed_ns":103441,"chip_calls":1,"unwritten_words":0,"cache_hit":true},{"op":"rc_nested","regression":"R5","n":128,"rc":"OK","violations":0,"pass":true,"chip_elapsed_ns":103665,"chip_calls":1,"unwritten_words":0,"cache_hit":true},{"op":"rc_nested","regression":"R5","n":129,"rc":"OK","violations":0,"pass":true,"chip_elapsed_ns":102881,"chip_calls":1,"unwritten_words":0,"cache_hit":true},{"op":"rc_nested","regression":"R5","n":200,"rc":"OK","violations":0,"pass":true,"chip_elapsed_ns":102817,"chip_calls":1,"unwritten_words":0,"cache_hit":true},{"op":"rc_nested","regression":"R5","n":1000,"rc":"OK","violations":0,"pass":true,"chip_elapsed_ns":103089,"chip_calls":1,"unwritten_words":0,"cache_hit":true},{"op":"rc_nested","regression":"R5","n":4096,"rc":"OK","violations":0,"pass":true,"chip_elapsed_ns":103152,"chip_calls":1,"unwritten_words":0,"cache_hit":true},{"op":"rc_nested","regression":"R5","n":8191,"rc":"OK","violations":0,"pass":true,"chip_elapsed_ns":103777,"chip_calls":1,"unwritten_words":0,"cache_hit":true},{"op":"rc_nested","regression":"R5","n":8192,"rc":"OK","violations":0,"pass":true,"chip_elapsed_ns":103025,"chip_calls":1,"unwritten_words":0,"cache_hit":true},{"op":"rc_nested","regression":"R5","n":8193,"rc":"OK","violations":0,"pass":true,"chip_elapsed_ns":206258,"chip_calls":2,"unwritten_words":0,"cache_hit":true},{"op":"rc_nested","regression":"R5","n":20000,"rc":"OK","violations":0,"pass":true,"chip_elapsed_ns":309651,"chip_calls":3,"unwritten_words":0,"cache_hit":true},{"op":"rc_exit_bar","regression":"R6","n":1,"rc":"OK","violations":0,"pass":true,"chip_elapsed_ns":103553,"chip_calls":1,"unwritten_words":0,"cache_hit":false},{"op":"rc_exit_bar","regression":"R6","n":5,"rc":"OK","violations":0,"pass":true,"chip_elapsed_ns":102785,"chip_calls":1,"unwritten_words":0,"cache_hit":true},{"op":"rc_exit_bar","regression":"R6","n":31,"rc":"OK","violations":0,"pass":true,"chip_elapsed_ns":102801,"chip_calls":1,"unwritten_words":0,"cache_hit":true},{"op":"rc_exit_bar","regression":"R6","n":32,"rc":"OK","violations":0,"pass":true,"chip_elapsed_ns":102801,"chip_calls":1,"unwritten_words":0,"cache_hit":true},{"op":"rc_exit_bar","regression":"R6","n":33,"rc":"OK","violations":0,"pass":true,"chip_elapsed_ns":102801,"chip_calls":1,"unwritten_words":0,"cache_hit":true},{"op":"rc_exit_bar","regression":"R6","n":63,"rc":"OK","violations":0,"pass":true,"chip_elapsed_ns":102849,"chip_calls":1,"unwritten_words":0,"cache_hit":true},{"op":"rc_exit_bar","regression":"R6","n":64,"rc":"OK","violations":0,"pass":true,"chip_elapsed_ns":102929,"chip_calls":1,"unwritten_words":0,"cache_hit":true},{"op":"rc_exit_bar","regression":"R6","n":65,"rc":"OK","violations":0,"pass":true,"chip_elapsed_ns":102993,"chip_calls":1,"unwritten_words":0,"cache_hit":true},{"op":"rc_exit_bar","regression":"R6","n":96,"rc":"OK","violations":0,"pass":true,"chip_elapsed_ns":103505,"chip_calls":1,"unwritten_words":0,"cache_hit":true},{"op":"rc_exit_bar","regression":"R6","n":127,"rc":"OK","violations":0,"pass":true,"chip_elapsed_ns":102865,"chip_calls":1,"unwritten_words":0,"cache_hit":true},{"op":"rc_exit_bar","regression":"R6","n":128,"rc":"OK","violations":0,"pass":true,"chip_elapsed_ns":102768,"chip_calls":1,"unwritten_words":0,"cache_hit":true},{"op":"rc_exit_bar","regression":"R6","n":129,"rc":"OK","violations":0,"pass":true,"chip_elapsed_ns":102753,"chip_calls":1,"unwritten_words":0,"cache_hit":true},{"op":"rc_exit_bar","regression":"R6","n":200,"rc":"OK","violations":0,"pass":true,"chip_elapsed_ns":102785,"chip_calls":1,"unwritten_words":0,"cache_hit":true},{"op":"rc_exit_bar","regression":"R6","n":1000,"rc":"OK","violations":0,"pass":true,"chip_elapsed_ns":102801,"chip_calls":1,"unwritten_words":0,"cache_hit":true},{"op":"rc_exit_bar","regression":"R6","n":4096,"rc":"OK","violations":0,"pass":true,"chip_elapsed_ns":102817,"chip_calls":1,"unwritten_words":0,"cache_hit":true},{"op":"rc_exit_bar","regression":"R6","n":8191,"rc":"OK","violations":0,"pass":true,"chip_elapsed_ns":102848,"chip_calls":1,"unwritten_words":0,"cache_hit":true},{"op":"rc_exit_bar","regression":"R6","n":8192,"rc":"OK","violations":0,"pass":true,"chip_elapsed_ns":103073,"chip_calls":1,"unwritten_words":0,"cache_hit":true},{"op":"rc_exit_bar","regression":"R6","n":8193,"rc":"OK","violations":0,"pass":true,"chip_elapsed_ns":206002,"chip_calls":2,"unwritten_words":0,"cache_hit":true},{"op":"rc_exit_bar","regression":"R6","n":20000,"rc":"OK","violations":0,"pass":true,"chip_elapsed_ns":310803,"chip_calls":3,"unwritten_words":0,"cache_hit":true},{"op":"rc_depth16","regression":"B0..B15","n":1,"rc":"OK","violations":0,"pass":true,"chip_elapsed_ns":103265,"chip_calls":1,"unwritten_words":0,"cache_hit":false},{"op":"rc_depth16","regression":"B0..B15","n":5,"rc":"OK","violations":0,"pass":true,"chip_elapsed_ns":103009,"chip_calls":1,"unwritten_words":0,"cache_hit":true},{"op":"rc_depth16","regression":"B0..B15","n":31,"rc":"OK","violations":0,"pass":true,"chip_elapsed_ns":103697,"chip_calls":1,"unwritten_words":0,"cache_hit":true},{"op":"rc_depth16","regression":"B0..B15","n":32,"rc":"OK","violations":0,"pass":true,"chip_elapsed_ns":103105,"chip_calls":1,"unwritten_words":0,"cache_hit":true},{"op":"rc_depth16","regression":"B0..B15","n":33,"rc":"OK","violations":0,"pass":true,"chip_elapsed_ns":102929,"chip_calls":1,"unwritten_words":0,"cache_hit":true},{"op":"rc_depth16","regression":"B0..B15","n":63,"rc":"OK","violations":0,"pass":true,"chip_elapsed_ns":103088,"chip_calls":1,"unwritten_words":0,"cache_hit":true},{"op":"rc_depth16","regression":"B0..B15","n":64,"rc":"OK","violations":0,"pass":true,"chip_elapsed_ns":102801,"chip_calls":1,"unwritten_words":0,"cache_hit":true},{"op":"rc_depth16","regression":"B0..B15","n":65,"rc":"OK","violations":0,"pass":true,"chip_elapsed_ns":102865,"chip_calls":1,"unwritten_words":0,"cache_hit":true},{"op":"rc_depth16","regression":"B0..B15","n":96,"rc":"OK","violations":0,"pass":true,"chip_elapsed_ns":103521,"chip_calls":1,"unwritten_words":0,"cache_hit":true},{"op":"rc_depth16","regression":"B0..B15","n":127,"rc":"OK","violations":0,"pass":true,"chip_elapsed_ns":103217,"chip_calls":1,"unwritten_words":0,"cache_hit":true},{"op":"rc_depth16","regression":"B0..B15","n":128,"rc":"OK","violations":0,"pass":true,"chip_elapsed_ns":103169,"chip_calls":1,"unwritten_words":0,"cache_hit":true},{"op":"rc_depth16","regression":"B0..B15","n":129,"rc":"OK","violations":0,"pass":true,"chip_elapsed_ns":103233,"chip_calls":1,"unwritten_words":0,"cache_hit":true},{"op":"rc_depth16","regression":"B0..B15","n":200,"rc":"OK","violations":0,"pass":true,"chip_elapsed_ns":103233,"chip_calls":1,"unwritten_words":0,"cache_hit":true},{"op":"rc_depth16","regression":"B0..B15","n":1000,"rc":"OK","violations":0,"pass":true,"chip_elapsed_ns":103713,"chip_calls":1,"unwritten_words":0,"cache_hit":true},{"op":"rc_depth16","regression":"B0..B15","n":4096,"rc":"OK","violations":0,"pass":true,"chip_elapsed_ns":103313,"chip_calls":1,"unwritten_words":0,"cache_hit":true},{"op":"rc_depth16","regression":"B0..B15","n":8191,"rc":"OK","violations":0,"pass":true,"chip_elapsed_ns":103313,"chip_calls":1,"unwritten_words":0,"cache_hit":true},{"op":"rc_depth16","regression":"B0..B15","n":8192,"rc":"OK","violations":0,"pass":true,"chip_elapsed_ns":103361,"chip_calls":1,"unwritten_words":0,"cache_hit":true},{"op":"rc_depth16","regression":"B0..B15","n":8193,"rc":"OK","violations":0,"pass":true,"chip_elapsed_ns":206546,"chip_calls":2,"unwritten_words":0,"cache_hit":true},{"op":"rc_depth16","regression":"B0..B15","n":20000,"rc":"OK","violations":0,"pass":true,"chip_elapsed_ns":310163,"chip_calls":3,"unwritten_words":0,"cache_hit":true}],"mutants":[{"op":"rc_diamond","mutant":1,"rc":"OK","violations":755,"caught":true},{"op":"rc_loopbreak","mutant":1,"rc":"OK","violations":377,"caught":true},{"op":"rc_nested","mutant":1,"rc":"OK","violations":110,"caught":true},{"op":"rc_exit_bar","mutant":1,"rc":"CODEGEN_FAIL","violations":1000,"caught":true}],"observations":[],"sim_model_n4096":[{"op":"rc_diamond","order":0,"warp_issues":4736,"lane_issues":133045,"splits":128,"max_frags":2,"peak_bars":1},{"op":"rc_diamond","order":1,"warp_issues":4736,"lane_issues":133096,"splits":128,"max_frags":2,"peak_bars":1},{"op":"rc_loopbreak","order":0,"warp_issues":115685,"lane_issues":713552,"splits":2889,"max_frags":28,"peak_bars":1},{"op":"rc_loopbreak","order":1,"warp_issues":116847,"lane_issues":728322,"splits":2876,"max_frags":2,"peak_bars":1},{"op":"rc_nested","order":0,"warp_issues":92819,"lane_issues":444474,"splits":1739,"max_frags":20,"peak_bars":2},{"op":"rc_nested","order":1,"warp_issues":91594,"lane_issues":433797,"splits":1694,"max_frags":2,"peak_bars":2},{"op":"rc_exit_bar","order":0,"warp_issues":5120,"lane_issues":157717,"splits":128,"max_frags":2,"peak_bars":1},{"op":"rc_exit_bar","order":1,"warp_issues":5120,"lane_issues":157638,"splits":128,"max_frags":2,"peak_bars":1},{"op":"rc_depth16","order":0,"warp_issues":13696,"lane_issues":280428,"splits":128,"max_frags":2,"peak_bars":16},{"op":"rc_depth16","order":1,"warp_issues":13696,"lane_issues":279136,"splits":128,"max_frags":2,"peak_bars":16}],"device_opens":1,"checks":220,"failed":0,"verdict":"OMEGA_GPU_RECONV_PASS"} diff --git a/evidence/gpu-reconv-308/T1a-GPU-RECONV-0a1812f/run.log b/evidence/gpu-reconv-308/T1a-GPU-RECONV-0a1812f/run.log new file mode 100644 index 000000000..74fe665e8 --- /dev/null +++ b/evidence/gpu-reconv-308/T1a-GPU-RECONV-0a1812f/run.log @@ -0,0 +1,20 @@ +codegen rc_diamond: 37 insns, 14 gprs, 1 regions +codegen rc_loopbreak: 41 insns, 14 gprs, 1 regions +codegen rc_nested: 49 insns, 15 gprs, 2 regions +codegen rc_exit_bar: 40 insns, 18 gprs, 1 regions +codegen rc_depth16: 107 insns, 14 gprs, 16 regions +nvdisasm diamond: rc=0 insns=40 undecodable=0 missing=0 (/tmp/omega_rc_diamond_2695930.sass) +nvdisasm depth16: rc=0 insns=112 undecodable=0 missing=0 (/tmp/omega_rc_depth16_2695930.sass) +nvdisasm sieve_div2: rc=0 insns=72 undecodable=0 missing=0 (/tmp/omega_rc_sieve_div2_2695930.sass) +nvdisasm forms: rc=0 insns=4 undecodable=0 missing=0 (/tmp/omega_rc_forms_2695930.sass) +model rc_diamond n=4096: warp_issues=4736 lane_util=0.878 splits=128 max_frags=2 peak_bars=1 +model rc_diamond n=4096: warp_issues=4736 lane_util=0.878 splits=128 max_frags=2 peak_bars=1 +model rc_loopbreak n=4096: warp_issues=115685 lane_util=0.193 splits=2889 max_frags=28 peak_bars=1 +model rc_loopbreak n=4096: warp_issues=116847 lane_util=0.195 splits=2876 max_frags=2 peak_bars=1 +model rc_nested n=4096: warp_issues=92819 lane_util=0.150 splits=1739 max_frags=20 peak_bars=2 +model rc_nested n=4096: warp_issues=91594 lane_util=0.148 splits=1694 max_frags=2 peak_bars=2 +model rc_exit_bar n=4096: warp_issues=5120 lane_util=0.963 splits=128 max_frags=2 peak_bars=1 +model rc_exit_bar n=4096: warp_issues=5120 lane_util=0.962 splits=128 max_frags=2 peak_bars=1 +model rc_depth16 n=4096: warp_issues=13696 lane_util=0.640 splits=128 max_frags=2 peak_bars=16 +model rc_depth16 n=4096: warp_issues=13696 lane_util=0.637 splits=128 max_frags=2 peak_bars=16 +PASS: 220 checks, 0 failed diff --git a/evidence/gpu-reconv-308/T1b-GPU-RECONV-0a1812f-observe-dropped-join/SHA256SUMS b/evidence/gpu-reconv-308/T1b-GPU-RECONV-0a1812f-observe-dropped-join/SHA256SUMS new file mode 100644 index 000000000..78b108a2b --- /dev/null +++ b/evidence/gpu-reconv-308/T1b-GPU-RECONV-0a1812f-observe-dropped-join/SHA256SUMS @@ -0,0 +1,10 @@ +2455c607827def7a1cc5d96ce3759e8280bb254d6ed7e9409072faf9cc949a56 build.log +8ca14df583da96c66c4d7dc1a301c39e0a19747c1b0842fdee62a5e7e2cf1260 bw_warp_sim.h +cf1431b7c25bfa84e4cd760017ed8b47c989bc47111a3bee1eac939127f7c814 device.txt +0cfa2a3922fa89022a2a0b77734a09914ccb4aef9221b14a182bb436cbd6e744 gpu_reconv_test.c +ce6aa4077426ff76120276424103843f5e6641d312255df17b0ef91057ffefce omega_blackwell_codegen.c +0da10397dd4ee813633afa425192d126d056684fdd998585fea666995a7c50ea omega_bw_reconv.h +bd2346fb86eed5e3be35c49c970c25adf1621043d6e4e4ba02e990375e5859c7 omega_gpu_elementwise_api.c +4d9eb1b5fd55e168f0cd4a96e906957376d88bb5efbe1f08633fcdfa52ce8fba omega_gpu_elementwise_api.h +d5fd8db12085c3f0e498c4c0f0b93b5cda721d613579e6f5e06e883958461328 receipt.json +f904886390c48a1c546b423c9d2b278576e4a57191db24ea292c25c5b562595d run.log diff --git a/evidence/gpu-reconv-308/T1b-GPU-RECONV-0a1812f-observe-dropped-join/build.log b/evidence/gpu-reconv-308/T1b-GPU-RECONV-0a1812f-observe-dropped-join/build.log new file mode 100644 index 000000000..8eab8a45c --- /dev/null +++ b/evidence/gpu-reconv-308/T1b-GPU-RECONV-0a1812f-observe-dropped-join/build.log @@ -0,0 +1,2 @@ +ar rcs build/libomega_gpu.a build/sha256.o build/omega_canonical.o build/omega_validate.o build/omega_core.o build/omega_codec.o build/aarch64_encoder.o build/aarch64_decoder.o build/omega_realize.o build/omega_exec.o build/omega_self_host.o build/omega_verify.o build/omega_program.o build/omega_synthesis.o build/omega_library.o build/omega_discovery.o build/omega_machine.o build/omega_realize_synth.o build/omega_matvec.o build/omega_accelerator.o build/omega_accelerator_world.o build/omega_vector.o build/omega_blackwell_encoder.o build/omega_blackwell_qmd.o build/omega_blackwell_realize.o build/omega_blackwell_submit.o build/omega_blackwell_engine.o build/omega_gpu_engine.o build/omega_blackwell_matmul.o build/omega_blackwell_codegen.o build/omega_gpu_session.o build/omega_gpu_matmul_api.o build/omega_gpu_elementwise_api.o build/omega_gpu_attention_api.o build/omega_gpu_wait.o build/omega_evidence.o build/m16_native.o build/nvrm.o build/omega_program_ir.o build/omega_vc_bridge.o build/omega_resolve.o build/omega_receipt.o build/omega_blake3.o build/omega_vcstore.o +cc -std=gnu11 -Wall -Wextra -Werror -MMD -MP -D_GNU_SOURCE -O2 -Isrc -I../physics-reconv-lock/m16 -I../physics-reconv-lock/nvrm -I../physics-reconv-lock/third_party/nvidia-open-580.173.02/src/common/sdk/nvidia/inc -I../physics-reconv-lock/third_party/nvidia-open-580.173.02/kernel-open/common/inc -I../physics-reconv-lock/third_party/nvidia-open-580.173.02/kernel-open/nvidia-uvm -I../physics-reconv-lock/third_party/nvidia-open-580.173.02/src/nvidia/arch/nvalloc/unix/include -Itests -o build/gpu_reconv_test tests/gpu_reconv_test.c build/libomega_gpu.a -lpthread -lm diff --git a/evidence/gpu-reconv-308/T1b-GPU-RECONV-0a1812f-observe-dropped-join/bw_warp_sim.h b/evidence/gpu-reconv-308/T1b-GPU-RECONV-0a1812f-observe-dropped-join/bw_warp_sim.h new file mode 100644 index 000000000..328094cb0 --- /dev/null +++ b/evidence/gpu-reconv-308/T1b-GPU-RECONV-0a1812f-observe-dropped-join/bw_warp_sim.h @@ -0,0 +1,313 @@ +/* bw_warp_sim.h: host SIMT simulator for the Blackwell IR (omega #308). + * + * Models what the earlier per-thread simulators could not: a warp of 32 lanes with an + * active mask, divergence at predicated branches, structured reconvergence through the + * barrier registers (BSSY arms Bn for the lanes executing it and names the join; BSYNC + * parks arriving lanes until every armed lane that has not exited is there, then the + * warp continues as one), nested regions, and lane participation in SHFL and BAR.SYNC. + * + * A warp is a set of fragments (pc, lane mask). A predicated branch whose lanes disagree + * splits the fragment; the scheduler then runs one fragment at a time in a chosen order + * (lowest pc first or highest pc first; tests run both), which is the freedom the chip's + * scheduler also has. Fragments only merge at a BSYNC. Errors (all fatal, first one wins): + * WS_E_SHFL_SPLIT a SHFL reached by a fragment that is not the whole live warp + * (the chip reads inactive lanes: undefined; attention 2026-10-04) + * WS_E_SHFL_EXITED a SHFL on a warp some of whose lanes already exited (undefined too) + * WS_E_BAR_SPLIT (defined, never raised) a BAR.SYNC reached by a split warp is allowed: + * since Volta the CTA barrier counts arriving threads, not warps (CUDA + * programming guide, independent thread scheduling; INFERRED for sm_121, + * not chip-verified by us). The simulator parks each fragment, releases + * them together, counts the event in bar_split_arrivals, and leaves the + * fragments split: a barrier is not a reconvergence point, so a SHFL after + * it still fails with SHFL_SPLIT unless a BSYNC rejoined the warp. + * WS_E_BSYNC_UNARMED BSYNC on a barrier no BSSY armed + * WS_E_BSSY_REARM BSSY on a barrier still armed (lanes may still be heading to its join) + * WS_E_JOIN_MISMATCH BSYNC at an address other than the one the BSSY named minus one + * WS_E_NONMEMBER a lane reached BSYNC Bn without having executed its BSSY + * WS_E_STUCK lanes parked at a BSYNC that can never complete (a join was dropped, + * an exit jumped past it, or a target was corrupted) + * WS_E_NEVER_JOINED the warp finished with a barrier still armed + * WS_E_SELF_BRANCH, WS_E_PC, WS_E_OP, WS_E_SHIFT, WS_E_MEM, WS_E_SMEM, WS_E_FRAGS + * Every error names the pc. Memory: global addresses are host pointers (optionally range + * checked); shared memory is a per-CTA buffer poisoned at CTA start so a read of a word + * nobody wrote shows up in the oracle comparison. + * + * Per-lane ALU semantics are those of the two earlier simulators (tests/gpu_attention_test.c, + * bench/prime_race/tests/gb10_sieve_host_test.c). EX2 and RCP are exact here; the chip's + * are approximate. LDCU64 (memory descriptor) is not modelled. */ +#ifndef BW_WARP_SIM_H +#define BW_WARP_SIM_H + +#include "omega_blackwell_codegen.h" +#include +#include +#include +#include +#include + +#define WS_WARP 32 +#define WS_MAX_THREADS 256 +#define WS_MAX_WARPS (WS_MAX_THREADS / WS_WARP) +#define WS_MAX_FRAGS 64 +#define WS_NBAR BW_RECONV_MAX_BAR +#define WS_MAX_RANGES 8 + +enum { + WS_OK = 0, WS_E_SHFL_SPLIT, WS_E_SHFL_EXITED, WS_E_BAR_SPLIT, WS_E_BSYNC_UNARMED, WS_E_BSSY_REARM, + WS_E_JOIN_MISMATCH, WS_E_NONMEMBER, WS_E_STUCK, WS_E_NEVER_JOINED, WS_E_SELF_BRANCH, WS_E_PC, + WS_E_OP, WS_E_SHIFT, WS_E_MEM, WS_E_SMEM, WS_E_FRAGS, WS_E_BAR_TIMEOUT, WS_E_STEPS +}; +static const char *ws_err_name(int e) { + static const char *const n[] = { "OK", "SHFL_SPLIT", "SHFL_EXITED", "BAR_SPLIT", "BSYNC_UNARMED", "BSSY_REARM", + "JOIN_MISMATCH", "NONMEMBER", "STUCK", "NEVER_JOINED", "SELF_BRANCH", "PC", "OP", "SHIFT", "MEM", "SMEM", + "FRAGS", "BAR_TIMEOUT", "STEPS" }; + return (e >= 0 && e < (int)(sizeof n / sizeof n[0])) ? n[e] : "?"; +} + +typedef struct { uint32_t r[256]; uint8_t p0; } WsLane; +typedef struct { int pc; uint32_t mask; int parked; /* waiting at a BAR.SYNC */ } WsFrag; +typedef struct { int valid; int target; uint32_t members, arrived; } WsBar; +typedef struct { + WsLane lane[WS_WARP]; + WsFrag frag[WS_MAX_FRAGS]; + int nfrag; + WsBar bar[WS_NBAR]; + uint32_t exited; +} WsWarp; +typedef struct { uintptr_t lo, hi; } WsRange; + +typedef struct { + const BlackwellIRProgram *p; + uint32_t cbank[256]; /* constant bank 0 words (byte offset / 4) */ + uint32_t threads; /* per CTA, a multiple of 32, <= WS_MAX_THREADS */ + uint8_t *smem; /* per-CTA shared memory buffer (poisoned at CTA start) */ + uint32_t smem_bytes; + WsRange ranges[WS_MAX_RANGES]; int nranges; /* when > 0, every global access must fall in one */ + int order; /* 0 = lowest pc first, 1 = highest pc first */ + int trace; /* print every issue of warp 0 of CTA (0,0) */ + /* results */ + int err; int err_pc; char msg[200]; + uint64_t warp_issues; /* fragment-instruction issues (the chip's cost unit for divergence) */ + uint64_t lane_issues; /* lanes active over those issues */ + uint64_t splits; /* branches that split a fragment */ + uint64_t bar_split_arrivals; /* BAR.SYNC reached by a fragment that is not the whole live warp */ + int max_frags, peak_bars; +} WsSim; + +static void ws_fail(WsSim *s, int err, int pc, const char *what) { + if (s->err) return; + s->err = err; s->err_pc = pc; + snprintf(s->msg, sizeof s->msg, "%s at pc %d: %s", ws_err_name(err), pc, what ? what : ""); +} + +static uint32_t ws_rd(const WsLane *l, int v, const BlackwellIRProgram *p) { if (v < 0) return 0; int ph = p->regalloc.vreg_to_phys[v]; return ph < 0 ? 0 : l->r[ph & 0xff]; } +static uint64_t ws_rd64(const WsLane *l, int v, const BlackwellIRProgram *p) { if (v < 0) return 0; int ph = p->regalloc.vreg_to_phys[v] & 0xff; return (uint64_t)l->r[ph] | ((uint64_t)l->r[(ph + 1) & 0xff] << 32); } +static void ws_wr(WsLane *l, int v, uint32_t x, const BlackwellIRProgram *p) { if (v < 0) return; int ph = p->regalloc.vreg_to_phys[v]; if (ph >= 0) l->r[ph & 0xff] = x; } +static void ws_wr64(WsLane *l, int v, uint64_t x, const BlackwellIRProgram *p) { if (v < 0) return; int ph = p->regalloc.vreg_to_phys[v] & 0xff; l->r[ph] = (uint32_t)x; l->r[(ph + 1) & 0xff] = (uint32_t)(x >> 32); } +static float ws_u2f(uint32_t u) { float f; memcpy(&f, &u, 4); return f; } +static uint32_t ws_f2u(float f) { uint32_t u; memcpy(&u, &f, 4); return u; } +static uint32_t ws_lut3(uint32_t lut, uint32_t a, uint32_t b, uint32_t c) { + uint32_t r = 0; + for (int i = 0; i < 8; i++) if (lut >> i & 1) r |= ((i & 4) ? a : ~a) & ((i & 2) ? b : ~b) & ((i & 1) ? c : ~c); + return r; +} +static int ws_mem_ok(WsSim *s, uint64_t a, unsigned bytes, int pc) { + if (s->nranges == 0) return 1; + for (int i = 0; i < s->nranges; i++) if (a >= s->ranges[i].lo && a + bytes <= s->ranges[i].hi) return 1; + ws_fail(s, WS_E_MEM, pc, "global access outside the declared buffers"); return 0; +} +static uint32_t *ws_smem(WsSim *s, uint32_t a, int pc) { + if ((a & 3) || a + 4 > s->smem_bytes) { ws_fail(s, WS_E_SMEM, pc, "shared access past the declared size"); return NULL; } + return (uint32_t *)(s->smem + a); +} +static int ws_popc(uint32_t m) { return __builtin_popcount(m); } + +/* One lane, one non-control instruction. */ +static void ws_lane_exec(WsSim *s, WsLane *l, const BlackwellIRInsn *in, int pc, uint32_t tid, uint32_t cx, uint32_t cy) { + const BlackwellIRProgram *p = s->p; + uint32_t a = ws_rd(l, in->src1_vreg, p), b = ws_rd(l, in->src2_vreg, p), c = ws_rd(l, in->src3_vreg, p); + switch (in->op) { + case BW_IR_NOP: break; + case BW_IR_S2R: ws_wr(l, in->dst_vreg, in->imm == BW_SR_TID_X ? tid : in->imm == BW_SR_CTAID_X ? cx : in->imm == BW_SR_CTAID_Y ? cy : 0, p); break; + case BW_IR_LDC: ws_wr(l, in->dst_vreg, s->cbank[(in->imm / 4) & 0xff], p); break; + case BW_IR_LDC64: ws_wr64(l, in->dst_vreg, (uint64_t)s->cbank[(in->imm / 4) & 0xff] | ((uint64_t)s->cbank[(in->imm / 4 + 1) & 0xff] << 32), p); break; + case BW_IR_LDCU64: case BW_IR_LDCU: break; /* memory descriptor: not modelled */ + case BW_IR_MOV_IMM: ws_wr(l, in->dst_vreg, in->imm, p); break; + case BW_IR_MOV_RZ: ws_wr(l, in->dst_vreg, 0, p); break; + case BW_IR_IMAD: ws_wr(l, in->dst_vreg, in->src2_vreg >= 0 ? a * b + c : a * in->imm + c, p); break; + case BW_IR_IMAD_WIDE: ws_wr64(l, in->dst_vreg, (uint64_t)a * in->imm + ws_rd64(l, in->src3_vreg, p), p); break; + case BW_IR_IMAD_HI_U32: ws_wr(l, in->dst_vreg, (uint32_t)(((uint64_t)a * b) >> 32) + c, p); break; + case BW_IR_IADD3: ws_wr(l, in->dst_vreg, a + b, p); break; /* the encoder's IADD3 has Rc = RZ */ + case BW_IR_SHF_R: ws_wr(l, in->dst_vreg, a >> (in->imm & 31), p); break; + case BW_IR_SHF_L_U32: if (b >= 32) { ws_fail(s, WS_E_SHIFT, pc, "SHF.L.U32 amount >= 32 (not chip-verified)"); return; } ws_wr(l, in->dst_vreg, a << b, p); break; + case BW_IR_LOP3_AND: ws_wr(l, in->dst_vreg, a & in->imm, p); break; + case BW_IR_LOP3_XOR: ws_wr(l, in->dst_vreg, a ^ b, p); break; + case BW_IR_LOP3_LUT: ws_wr(l, in->dst_vreg, ws_lut3(in->imm & 0xff, a, b, c), p); break; + case BW_IR_ISETP_GE_U32: l->p0 = a >= b; break; + case BW_IR_LDG_E: { uint64_t ad = ws_rd64(l, in->src1_vreg, p); if (!ws_mem_ok(s, ad, 4, pc)) return; uint32_t x; memcpy(&x, (const void *)(uintptr_t)ad, 4); ws_wr(l, in->dst_vreg, x, p); break; } + case BW_IR_LDG_E_U16: { uint64_t ad = ws_rd64(l, in->src1_vreg, p); if (!ws_mem_ok(s, ad, 2, pc)) return; uint16_t x; memcpy(&x, (const void *)(uintptr_t)ad, 2); ws_wr(l, in->dst_vreg, x, p); break; } + case BW_IR_STG_E: { uint64_t ad = ws_rd64(l, in->src1_vreg, p); if (!ws_mem_ok(s, ad, 4, pc)) return; memcpy((void *)(uintptr_t)ad, &b, 4); break; } + case BW_IR_LDS32: { uint32_t *m = ws_smem(s, a, pc); if (!m) return; ws_wr(l, in->dst_vreg, *m, p); break; } + case BW_IR_STS32: { uint32_t *m = ws_smem(s, a, pc); if (!m) return; *m = b; break; } + case BW_IR_FADD: ws_wr(l, in->dst_vreg, ws_f2u(ws_u2f(a) + ws_u2f(b)), p); break; + case BW_IR_FSUB: ws_wr(l, in->dst_vreg, ws_f2u(ws_u2f(a) - ws_u2f(b)), p); break; + case BW_IR_FMUL: ws_wr(l, in->dst_vreg, ws_f2u(ws_u2f(a) * ws_u2f(b)), p); break; + case BW_IR_FFMA: ws_wr(l, in->dst_vreg, ws_f2u(fmaf(ws_u2f(a), ws_u2f(b), ws_u2f(c))), p); break; + case BW_IR_FMNMX_MAX: ws_wr(l, in->dst_vreg, ws_f2u(fmaxf(ws_u2f(a), ws_u2f(b))), p); break; + case BW_IR_FMNMX_MIN: ws_wr(l, in->dst_vreg, ws_f2u(fminf(ws_u2f(a), ws_u2f(b))), p); break; + case BW_IR_MUFU_EX2: ws_wr(l, in->dst_vreg, ws_f2u(exp2f(ws_u2f(a))), p); break; + case BW_IR_MUFU_RCP: ws_wr(l, in->dst_vreg, ws_f2u(1.0f / ws_u2f(a)), p); break; + case BW_IR_MUFU_RSQ: ws_wr(l, in->dst_vreg, ws_f2u(1.0f / sqrtf(ws_u2f(a))), p); break; + default: ws_fail(s, WS_E_OP, pc, "opcode not modelled"); return; + } +} + +static int ws_pick(const WsSim *s, const WsWarp *w) { + int best = -1; + for (int i = 0; i < w->nfrag; i++) { + if (w->frag[i].mask == 0 || w->frag[i].parked) continue; + if (best < 0) { best = i; continue; } + if (s->order == 0 ? w->frag[i].pc < w->frag[best].pc : w->frag[i].pc > w->frag[best].pc) best = i; + } + return best; +} +static void ws_drop_frag(WsWarp *w, int i) { w->frag[i] = w->frag[--w->nfrag]; } +static int ws_add_frag(WsSim *s, WsWarp *w, int pc, uint32_t mask) { + if (mask == 0) return 0; + if (w->nfrag >= WS_MAX_FRAGS) { ws_fail(s, WS_E_FRAGS, pc, "too many fragments"); return -1; } + w->frag[w->nfrag].pc = pc; w->frag[w->nfrag].mask = mask; w->frag[w->nfrag].parked = 0; w->nfrag++; + if (w->nfrag > s->max_frags) s->max_frags = w->nfrag; + return 0; +} + +/* Runs one warp until it has no runnable fragment (finished, parked at BAR.SYNC, or stuck). + * Returns 1 when parked at BAR.SYNC, 0 when finished, -1 on error. */ +static int ws_run_warp(WsSim *s, WsWarp *w, uint32_t warp, uint32_t cx, uint32_t cy, int trace) { + const BlackwellIRProgram *p = s->p; + const uint32_t live_all = 0xffffffffu; + for (uint64_t guard = 0;; guard++) { + if (s->err) return -1; + if (guard > 2000000000ull) { ws_fail(s, WS_E_STEPS, -1, "step budget"); return -1; } + int fi = ws_pick(s, w); + if (fi < 0) { + /* nothing runnable: parked at the CTA barrier, finished, or lanes waiting at a BSYNC + * that can never complete */ + for (int i = 0; i < w->nfrag; i++) if (w->frag[i].parked) return 1; + for (int b = 0; b < WS_NBAR; b++) if (w->bar[b].valid) { + if (w->bar[b].arrived) { ws_fail(s, WS_E_STUCK, w->bar[b].target - 1, "lanes parked at BSYNC, the rest never arrive"); return -1; } + ws_fail(s, WS_E_NEVER_JOINED, w->bar[b].target - 1, "warp finished with a barrier still armed"); return -1; + } + return 0; + } + WsFrag *f = &w->frag[fi]; + int pc = f->pc; + if (pc < 0 || (size_t)pc >= p->count) { ws_fail(s, WS_E_PC, pc, "pc outside the program"); return -1; } + const BlackwellIRInsn *in = &p->insns[pc]; + uint32_t mask = f->mask; + s->warp_issues++; s->lane_issues += (uint64_t)ws_popc(mask); + if (trace) printf("W%u pc=%d op=%d mask=%08x frags=%d\n", warp, pc, (int)in->op, mask, w->nfrag); + uint32_t live = live_all & ~w->exited; + uint32_t pmask = 0; + for (int L = 0; L < WS_WARP; L++) if ((mask >> L & 1) && w->lane[L].p0) pmask |= 1u << L; + uint32_t cond = in->predicate_p0 ? (in->predicate_not ? (mask & ~pmask) : (mask & pmask)) : mask; + switch (in->op) { + case BW_IR_EXIT: { + w->exited |= cond; + f->mask = mask & ~cond; + if (f->mask == 0) ws_drop_frag(w, fi); else f->pc = pc + 1; + break; + } + case BW_IR_BRA: { + int32_t delta = (int32_t)in->imm; + uint32_t taken = cond, fall = mask & ~cond; + if (taken && delta == 0) { ws_fail(s, WS_E_SELF_BRANCH, pc, "branch to itself"); return -1; } + if (taken && fall) s->splits++; + if (taken == 0) { f->pc = pc + 1; break; } + if (fall == 0) { f->pc = pc + delta; break; } + f->mask = fall; f->pc = pc + 1; + if (ws_add_frag(s, w, pc + delta, taken) < 0) return -1; + break; + } + case BW_IR_BSSY: { + WsBar *b = &w->bar[in->bar_reg & (WS_NBAR - 1)]; + if (in->bar_reg >= WS_NBAR) { ws_fail(s, WS_E_OP, pc, "barrier id"); return -1; } + if (b->valid) { ws_fail(s, WS_E_BSSY_REARM, pc, "BSSY on an armed barrier"); return -1; } + b->valid = 1; b->target = pc + (int32_t)in->imm; b->members = cond; b->arrived = 0; + int armed = 0; for (int k = 0; k < WS_NBAR; k++) armed += w->bar[k].valid; + if (armed > s->peak_bars) s->peak_bars = armed; + f->pc = pc + 1; + break; + } + case BW_IR_BSYNC: { + if (in->bar_reg >= WS_NBAR) { ws_fail(s, WS_E_OP, pc, "barrier id"); return -1; } + WsBar *b = &w->bar[in->bar_reg]; + if (!b->valid) { ws_fail(s, WS_E_BSYNC_UNARMED, pc, "BSYNC without a BSSY"); return -1; } + if (cond & ~b->members) { ws_fail(s, WS_E_NONMEMBER, pc, "a lane at BSYNC did not execute the BSSY"); return -1; } + b->arrived |= cond; + uint32_t rest = mask & ~cond; /* a predicated-off lane walks on */ + if (rest) { f->mask = rest; f->pc = pc + 1; } else ws_drop_frag(w, fi); + if (b->arrived == (b->members & ~w->exited)) { + if (b->target != pc + 1) { ws_fail(s, WS_E_JOIN_MISMATCH, pc, "BSSY named a different join point"); return -1; } + uint32_t joined = b->arrived; b->valid = 0; b->arrived = 0; b->members = 0; + if (ws_add_frag(s, w, pc + 1, joined) < 0) return -1; + } + break; + } + case BW_IR_SHFL_DOWN: { + if (w->exited) { ws_fail(s, WS_E_SHFL_EXITED, pc, "SHFL on a warp with exited lanes"); return -1; } + if (mask != live) { ws_fail(s, WS_E_SHFL_SPLIT, pc, "SHFL on a split warp"); return -1; } + uint32_t src[WS_WARP]; + for (int L = 0; L < WS_WARP; L++) src[L] = ws_rd(&w->lane[L], in->src1_vreg, p); + for (int L = 0; L < WS_WARP; L++) { int from = L + (int)(in->imm & 31); ws_wr(&w->lane[L], in->dst_vreg, from < WS_WARP ? src[from] : src[L], p); } + f->pc = pc + 1; + break; + } + case BW_IR_BAR_SYNC: { + if (mask != live) s->bar_split_arrivals++; + f->parked = 1; /* released by ws_run when every live lane of the CTA has arrived */ + break; + } + default: { + for (int L = 0; L < WS_WARP; L++) if (mask >> L & 1) { + ws_lane_exec(s, &w->lane[L], in, pc, warp * WS_WARP + (uint32_t)L, cx, cy); + if (s->err) return -1; + } + f->pc = pc + 1; + break; + } + } + } +} + +/* Runs every CTA of the grid. Returns s->err (0 = clean). */ +static int ws_run(WsSim *s, uint32_t grid_x, uint32_t grid_y) { + s->err = 0; s->msg[0] = 0; s->warp_issues = s->lane_issues = s->splits = s->bar_split_arrivals = 0; s->max_frags = 0; s->peak_bars = 0; + if (!s->p || s->threads == 0 || s->threads % WS_WARP || s->threads > WS_MAX_THREADS) { ws_fail(s, WS_E_OP, -1, "bad simulator configuration"); return s->err; } + static WsWarp warps[WS_MAX_WARPS]; + uint32_t nw = s->threads / WS_WARP; + for (uint32_t cy = 0; cy < grid_y && !s->err; cy++) for (uint32_t cx = 0; cx < grid_x && !s->err; cx++) { + memset(warps, 0, sizeof(WsWarp) * nw); + if (s->smem && s->smem_bytes) for (uint32_t i = 0; i + 4 <= s->smem_bytes; i += 4) { uint32_t poison = 0xfeedfaceu; memcpy(s->smem + i, &poison, 4); } + for (uint32_t w = 0; w < nw; w++) { warps[w].nfrag = 1; warps[w].frag[0].pc = 0; warps[w].frag[0].mask = 0xffffffffu; warps[w].frag[0].parked = 0; } + int trace = s->trace && cx == 0 && cy == 0; + /* Barrier phases: every warp runs until all of its fragments are parked at BAR.SYNC or it + * has finished (exited lanes do not hold the barrier up: INFERRED, see WS_E_BAR_SPLIT). + * Then every parked fragment is released and the next phase starts. */ + for (int phase = 0; ; phase++) { + int parked = 0; + for (uint32_t w = 0; w < nw && !s->err; w++) { + if (warps[w].nfrag == 0) continue; + int r = ws_run_warp(s, &warps[w], w, cx, cy, trace && w == 0); + if (r < 0) break; + if (r == 1) parked++; + } + if (s->err || parked == 0) break; /* error, or every warp finished */ + for (uint32_t w = 0; w < nw; w++) for (int i = 0; i < warps[w].nfrag; i++) + if (warps[w].frag[i].parked) { warps[w].frag[i].parked = 0; warps[w].frag[i].pc++; } + if (phase > 100000) { ws_fail(s, WS_E_BAR_TIMEOUT, -1, "barrier phases"); break; } + } + } + return s->err; +} + +#endif /* BW_WARP_SIM_H */ diff --git a/evidence/gpu-reconv-308/T1b-GPU-RECONV-0a1812f-observe-dropped-join/device.txt b/evidence/gpu-reconv-308/T1b-GPU-RECONV-0a1812f-observe-dropped-join/device.txt new file mode 100644 index 000000000..135a7deb6 --- /dev/null +++ b/evidence/gpu-reconv-308/T1b-GPU-RECONV-0a1812f-observe-dropped-join/device.txt @@ -0,0 +1,5 @@ +Linux spark-b87b 7.0.0-1019-nvidia #19~24.04.2-Ubuntu SMP PREEMPT_DYNAMIC Sun Sep 6 18:35:11 UTC 2 aarch64 aarch64 aarch64 GNU/Linux +NVIDIA GB10, 580.173.02 +omega 0a1812f44eaa3dcf8fe5a914d1dd65cfaccbbcd7 (1 uncommitted paths) +physics 6d7cf0d4d8eb2cda7b512100ff6058e25dbb3ddf +2026-10-05T23:14:41Z diff --git a/evidence/gpu-reconv-308/T1b-GPU-RECONV-0a1812f-observe-dropped-join/gpu_reconv_test.c b/evidence/gpu-reconv-308/T1b-GPU-RECONV-0a1812f-observe-dropped-join/gpu_reconv_test.c new file mode 100644 index 000000000..227e3b658 --- /dev/null +++ b/evidence/gpu-reconv-308/T1b-GPU-RECONV-0a1812f-observe-dropped-join/gpu_reconv_test.c @@ -0,0 +1,470 @@ +/* gpu_reconv_test: structured warp reconvergence (omega #308) on Omega's own Blackwell codegen. + * + * ./gpu_reconv_test --host-only encoder golden words, region bookkeeping, codegen refusals, + * nvdisasm listing (when /usr/local/cuda/bin/nvdisasm exists), + * every probe kernel through the SIMT warp simulator against + * the host oracle under both fragment orders, the mutation + * controls, scoreboard and control-edge walks. No device. + * ./gpu_reconv_test --out receipt.json chip gate: host part first (a kernel the simulator rejects + * never gets chip time), then every probe on the chip against + * the oracle, bit-exact, then each math mutant must be caught. + * --observe-dropped-join chip only, optional: also launch the dropped-BSYNC mutant of + * one probe and record what the chip did (an observation, not + * a gate; the simulator rejects that kernel first). + * + * Probes (src/omega_gpu_elementwise_api.c, gen_rc_*): R3 diamond, R4 loop break, R5 nested, + * R6 lanes past the end with STS/BAR/LDS + SHFL, and the 16-deep barrier-register bound. + * Oracle: rc_oracle below; every comparison is exact (u32). Launcher chunking (64 CTAs x 128 + * threads per launch, P0 = words in the chunk) is part of the oracle, so a clamp to the wrong + * chunk end shows up. */ +#include "omega_gpu_elementwise_api.h" +#include "omega_blackwell_codegen.h" +#include "omega_bw_reconv.h" +#include "omega_gpu_session.h" +#include "bw_warp_sim.h" +#include +#include +#include +#include +#include + +static int g_checks, g_failed; +#define CHECK(cond, ...) do { g_checks++; if (!(cond)) { g_failed++; printf("FAIL: "); printf(__VA_ARGS__); printf("\n"); } } while (0) + +static uint32_t lcg(uint32_t *s) { *s = *s * 1664525u + 1013904223u; return *s; } + +typedef struct { OmegaGpuEwOp op; const char *name; const char *reg; } Probe; +static const Probe PROBES[] = { + { OMEGA_GPU_EW_RC_DIAMOND, "rc_diamond", "R3" }, + { OMEGA_GPU_EW_RC_LOOPBREAK, "rc_loopbreak", "R4" }, + { OMEGA_GPU_EW_RC_NESTED, "rc_nested", "R5" }, + { OMEGA_GPU_EW_RC_EXIT_BAR, "rc_exit_bar", "R6" }, + { OMEGA_GPU_EW_RC_DEPTH16, "rc_depth16", "B0..B15" }, +}; +#define NPROBES (sizeof PROBES / sizeof PROBES[0]) +static const uint32_t NS[] = { 1, 5, 31, 32, 33, 63, 64, 65, 96, 127, 128, 129, 200, 1000, 4096, 8191, 8192, 8193, 20000 }; +#define NNS (sizeof NS / sizeof NS[0]) +#define CHUNK (OMEGA_GPU_EW_MAX_CTAS * OMEGA_GPU_EW_THREADS) + +/* ----------------------------------------------------------------- oracle */ +static uint32_t count_loop(uint32_t x, uint32_t y, int math, uint32_t *v_out) { + uint32_t v = x & 255u, st = (y & 15u) + 1u, cnt = 0, bound = math ? 255u : 256u; + while (v < bound) { v += st; cnt++; } + *v_out = v; return cnt; +} +static uint32_t rc_r(OmegaGpuEwOp op, int math, uint32_t x, uint32_t y, uint32_t partner_x) { + uint32_t v, cnt; + switch (op) { + case OMEGA_GPU_EW_RC_DIAMOND: + if (x >= y) { uint32_t t = 3u * x + y, u = t ^ x, r = 5u * u + 1u; r += r >> 3; return math ? r + 1u : r; } + return x + 1u; + case OMEGA_GPU_EW_RC_LOOPBREAK: cnt = count_loop(x, y, math, &v); return cnt * 256u + v; + case OMEGA_GPU_EW_RC_NESTED: if (x & 1u) return 7u; cnt = count_loop(x, y, math, &v); return cnt * 256u + v + 100u; + case OMEGA_GPU_EW_RC_EXIT_BAR: return 3u * x + (partner_x | 1u); + case OMEGA_GPU_EW_RC_DEPTH16: return x + (x < y ? (uint32_t)BW_RECONV_MAX_BAR : 0u); + default: return 0xdeadbeefu; + } +} +static void rc_oracle(OmegaGpuEwOp op, int math, uint32_t n, const uint32_t *a, const uint32_t *b, uint32_t *out) { + for (uint32_t i0 = 0; i0 < n; i0 += CHUNK) { + uint32_t ni = n - i0 < CHUNK ? n - i0 : CHUNK; + uint32_t threads = ((ni + OMEGA_GPU_EW_THREADS - 1) / OMEGA_GPU_EW_THREADS) * OMEGA_GPU_EW_THREADS; + uint32_t *r = malloc((size_t)threads * 4); + for (uint32_t t = 0; t < threads; t++) { + uint32_t idx = t < ni ? t : ni - 1, pt = t ^ 1u, pidx = pt < ni ? pt : ni - 1; + r[t] = rc_r(op, math, a[i0 + idx], b[i0 + idx], a[i0 + pidx]); + } + 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]; } + free(r); + } +} +/* An LCG's low bits cycle quickly (bit 0 has period 2), and the probes key on low bits + * (x & 255, y & 15, x & 1), so each draw goes through a mixing step. */ +static uint32_t mix(uint32_t r) { r ^= r >> 16; r *= 0x45d9f3bu; r ^= r >> 16; r *= 0x45d9f3bu; r ^= r >> 16; return r; } +static void fill(uint32_t *a, uint32_t *b, uint32_t n, uint32_t seed) { + for (uint32_t i = 0; i < n; i++) { a[i] = mix(lcg(&seed)); b[i] = mix(lcg(&seed)); } +} +static size_t diff_count(const uint32_t *got, const uint32_t *want, uint32_t n, uint32_t *first) { + size_t bad = 0; *first = n; + for (uint32_t i = 0; i < n; i++) if (got[i] != want[i]) { if (!bad) *first = i; bad++; } + return bad; +} + +/* --------------------------------------------------------- simulator driver */ +static uint8_t g_smem[1024]; +/* Mirrors run_1d: 64 CTAs x 128 threads per launch, P0 = words in the chunk. Returns the + * simulator's error code (0 clean); the output words are written to out. */ +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) { + for (uint32_t i = 0; i < n; i++) out[i] = 0xffbadbadu; + uint64_t wi = 0, li = 0, sp = 0; int mf = 0, pb = 0; + for (uint32_t i0 = 0; i0 < n; i0 += CHUNK) { + uint32_t ni = n - i0 < CHUNK ? n - i0 : CHUNK; + WsSim s; memset(&s, 0, sizeof s); + s.p = p; s.threads = OMEGA_GPU_EW_THREADS; s.smem = g_smem; s.smem_bytes = sizeof g_smem; s.order = order; + uint64_t aa = (uintptr_t)(a + i0), ba = (uintptr_t)(b + i0), ca = (uintptr_t)(out + i0); + s.cbank[0x380 / 4] = (uint32_t)aa; s.cbank[0x380 / 4 + 1] = (uint32_t)(aa >> 32); + s.cbank[0x388 / 4] = (uint32_t)ba; s.cbank[0x388 / 4 + 1] = (uint32_t)(ba >> 32); + s.cbank[0x390 / 4] = (uint32_t)ca; s.cbank[0x390 / 4 + 1] = (uint32_t)(ca >> 32); + s.cbank[0x398 / 4] = ni; + s.ranges[0] = (WsRange){ (uintptr_t)aa, (uintptr_t)aa + (size_t)ni * 4 }; + s.ranges[1] = (WsRange){ (uintptr_t)ba, (uintptr_t)ba + (size_t)ni * 4 }; + s.ranges[2] = (WsRange){ (uintptr_t)ca, (uintptr_t)ca + (size_t)ni * 4 }; + s.nranges = 3; + int err = ws_run(&s, (ni + OMEGA_GPU_EW_THREADS - 1) / OMEGA_GPU_EW_THREADS, 1); + wi += s.warp_issues; li += s.lane_issues; sp += s.splits; + if (s.max_frags > mf) mf = s.max_frags; + if (s.peak_bars > pb) pb = s.peak_bars; + if (err) { if (stats) *stats = s; return err; } + } + 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; } + return 0; +} +static int codegen(OmegaGpuEwOp op, int mutant, BlackwellIRProgram *p, OmegaBlackwellKernel *k) { + memset(k, 0, sizeof *k); + return omega_gpu_elementwise_codegen_ir(op, mutant, p, k); +} +static int is_reconv_err(int e) { + 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 || + e == WS_E_BSYNC_UNARMED || e == WS_E_BSSY_REARM || e == WS_E_NONMEMBER || e == WS_E_SHFL_EXITED; +} + +/* --------------------------------------------------------- static walks */ +/* Scoreboard walk (as bench/prime_race/tests/gb10_sieve_host_test.c sb_hazards): a destination + * written under write scoreboard 0 is in flight until a wait mask that includes it. */ +static int sb_hazards(const BlackwellIRProgram *p) { + int inflight[64], n = 0, bad = 0; + for (size_t i = 0; i < p->count; i++) { + const BlackwellIRInsn *in = &p->insns[i]; + uint32_t c = in->control; + if ((c >> 20) & 1u) n = 0; + const int src[3] = { in->src1_vreg, in->src2_vreg, in->src3_vreg }; + for (int s = 0; s < 3; s++) for (int k = 0; src[s] >= 0 && k < n; k++) if (inflight[k] == src[s]) bad++; + if (c != 0 && ((c >> 14) & 7u) == 0 && in->dst_vreg >= 0 && n < 64) inflight[n++] = in->dst_vreg; + } + return bad; +} +/* Control-edge walk: the straight-line scoreboard reasoning above holds only if nothing is in + * flight across a branch, barrier op or exit. Every BRA / EXIT / BSSY / BSYNC must wait on every + * scoreboard set since the last wait. Returns the number of control ops that do not. */ +static int edge_waits(const BlackwellIRProgram *p) { + uint32_t pending = 0; int bad = 0; + for (size_t i = 0; i < p->count; i++) { + const BlackwellIRInsn *in = &p->insns[i]; + uint32_t c = in->control, wait = (c >> 20) & 63u, wb = (c >> 14) & 7u, rb = (c >> 17) & 7u; + pending &= ~wait; + if (in->op == BW_IR_BRA || in->op == BW_IR_EXIT || in->op == BW_IR_BSSY || in->op == BW_IR_BSYNC) { if (pending) bad++; } + if (wb != 7u) pending |= 1u << wb; + if (rb != 7u) pending |= 1u << rb; + } + return bad; +} +static int count_op(const BlackwellIRProgram *p, BlackwellIROpcode op, int bar) { + int n = 0; + for (size_t i = 0; i < p->count; i++) if (p->insns[i].op == op && (bar < 0 || p->insns[i].bar_reg == bar)) n++; + return n; +} +static int find_op(const BlackwellIRProgram *p, BlackwellIROpcode op, int nth) { + for (size_t i = 0; i < p->count; i++) if (p->insns[i].op == op && nth-- == 0) return (int)i; + return -1; +} + +/* --------------------------------------------------------- nvdisasm listing */ +static int nvdisasm_bytes(const uint8_t *code, size_t bytes, const char *name, const char *const *must, int n_must) { + const char *nv = "/usr/local/cuda/bin/nvdisasm"; + if (access(nv, X_OK) != 0) { printf("nvdisasm: not present, listing check skipped for %s\n", name); return 0; } + char bin[96], lst[96], cmd[320]; + snprintf(bin, sizeof bin, "/tmp/omega_rc_%s_%d.bin", name, (int)getpid()); + snprintf(lst, sizeof lst, "/tmp/omega_rc_%s_%d.sass", name, (int)getpid()); + FILE *f = fopen(bin, "wb"); if (!f) return -1; + fwrite(code, 1, bytes, f); fclose(f); + snprintf(cmd, sizeof cmd, "%s -b SM121 %s > %s 2>&1", nv, bin, lst); + int rc = system(cmd); + FILE *l = fopen(lst, "r"); if (!l) return -1; + char line[512]; int seen[16] = {0}, bad = 0, insns = 0; + while (fgets(line, sizeof line, l)) { + if (!strstr(line, "/*0")) continue; + insns++; + if (strstr(line, "?") || strstr(line, "error")) bad++; + for (int i = 0; i < n_must && i < 16; i++) if (strstr(line, must[i])) seen[i] = 1; + } + fclose(l); + int miss = 0; + for (int i = 0; i < n_must && i < 16; i++) if (!seen[i]) { printf(" %s: nvdisasm listing lacks %s\n", name, must[i]); miss++; } + printf("nvdisasm %s: rc=%d insns=%d undecodable=%d missing=%d (%s)\n", name, rc, insns, bad, miss, lst); + unlink(bin); + return (rc == 0 && bad == 0 && miss == 0) ? 0 : -1; +} +static int nvdisasm_kernel(OmegaGpuEwOp op, int mutant, const char *name, const char *const *must, int n_must) { + BlackwellIRProgram *p = calloc(1, sizeof *p); OmegaBlackwellKernel k; + if (!p || codegen(op, mutant, p, &k) != OMEGA_GPU_EW_OK) { free(p); return -1; } + int rc = nvdisasm_bytes(k.code, k.code_size, name, must, n_must); + free(k.code); free(p); + return rc; +} + +/* --------------------------------------------------------- host part */ +typedef struct { const char *name; uint64_t warp_issues, lane_issues, splits; int max_frags, peak_bars; } Model; +static Model g_model[NPROBES * 2]; static int g_nmodel; + +static int host_part(void) { + int before = g_failed; + /* 1. encoder golden words (nvcc 13.0.88 -arch=sm_121 oracle, docs/gpu-reconvergence-308.md) */ + int fx = omega_blackwell_verify_codegen_fixtures_reconv(); + CHECK(fx == 0, "encoder golden words BSSY / BSYNC and refusals (rc %d)", fx); + + /* 2. region bookkeeping on a hand-built program */ + { + BlackwellIRProgram *p = calloc(1, sizeof *p); BwRegions r; memset(&r, 0, sizeof r); + omega_bw_ir_init(p); + BlackwellIRInsn in; memset(&in, 0, sizeof in); in.dst_vreg = in.src1_vreg = in.src2_vreg = in.src3_vreg = -1; + in.op = BW_IR_BSSY; in.bar_reg = 0; in.imm = 2; omega_bw_ir_append(p, &in); /* 0 */ + CHECK(bw_regions_next_bar(&r) == 0 && bw_regions_begin(&r, 0) == 0, "region begin returns B0"); + in.op = BW_IR_BRA; in.imm = 1; in.predicate_p0 = true; omega_bw_ir_append(p, &in); /* 1 */ + CHECK(bw_regions_exit(&r, 1) == 0, "region exit recorded"); + in.op = BW_IR_NOP; in.predicate_p0 = false; omega_bw_ir_append(p, &in); /* 2 */ + in.op = BW_IR_BSYNC; in.bar_reg = 0; omega_bw_ir_append(p, &in); /* 3 */ + CHECK(bw_regions_join(&r, p, 3) == 0, "region join returns B0"); + 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); + CHECK(bw_regions_join(&r, p, 3) == -1, "join with no open region refused"); + CHECK(bw_regions_exit(&r, 1) == -1, "exit with no open region refused"); + /* exits table full */ + memset(&r, 0, sizeof r); bw_regions_begin(&r, 0); + int ok = 1; for (int i = 0; i < BW_RECONV_MAX_EXITS; i++) ok &= bw_regions_exit(&r, 1) == 0; + CHECK(ok && bw_regions_exit(&r, 1) == -1, "exit table bound (%d) fails closed", BW_RECONV_MAX_EXITS); + /* join index inconsistent: BSYNC before BSSY, wrong barrier */ + memset(&r, 0, sizeof r); bw_regions_begin(&r, 3); + CHECK(bw_regions_join(&r, p, 0) == -1, "join with BSSY after BSYNC refused"); + memset(&r, 0, sizeof r); bw_regions_begin(&r, 0); p->insns[3].bar_reg = 1; + CHECK(bw_regions_join(&r, p, 3) == -1, "join with the wrong barrier register refused"); + p->insns[3].bar_reg = 0; + /* depth bound */ + memset(&r, 0, sizeof r); ok = 1; + for (int d = 0; d < BW_RECONV_MAX_DEPTH; d++) ok &= bw_regions_next_bar(&r) == d && bw_regions_begin(&r, d) == d; + 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); + free(p); + } + + /* 3. codegen: production probes build, refusals refuse, mutants differ */ + static const struct { OmegaGpuEwOp op; const char *name; } refuse[] = { + { OMEGA_GPU_EW_RC_REFUSE_DEPTH17, "17 nested regions" }, { OMEGA_GPU_EW_RC_REFUSE_EXIT_SHFL, "EXIT then BAR/SHFL" }, + { 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" }, + { OMEGA_GPU_EW_RC_REFUSE_SHFL_IN_REGION, "SHFL inside a region" }, { OMEGA_GPU_EW_RC_REFUSE_UNCLOSED, "region never joined" }, + { OMEGA_GPU_EW_RC_REFUSE_JOIN_ONLY, "BSYNC with no BSSY" }, + }; + for (size_t i = 0; i < sizeof refuse / sizeof refuse[0]; i++) { + BlackwellIRProgram *p = calloc(1, sizeof *p); OmegaBlackwellKernel k; + int rc = codegen(refuse[i].op, 0, p, &k); + CHECK(rc == OMEGA_GPU_EW_CODEGEN_FAIL, "%s refused at codegen (rc %s)", refuse[i].name, omega_gpu_elementwise_rc_name(rc)); + if (rc == OMEGA_GPU_EW_OK) free(k.code); + free(p); + } + for (size_t i = 0; i < NPROBES; i++) { + BlackwellIRProgram *p = calloc(1, sizeof *p); OmegaBlackwellKernel k; + int rc = codegen(PROBES[i].op, 0, p, &k); + 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); + if (rc != OMEGA_GPU_EW_OK) { free(p); continue; } + int nb = count_op(p, BW_IR_BSSY, -1), ns = count_op(p, BW_IR_BSYNC, -1); + int want = PROBES[i].op == OMEGA_GPU_EW_RC_NESTED ? 2 : PROBES[i].op == OMEGA_GPU_EW_RC_DEPTH16 ? BW_RECONV_MAX_BAR : 1; + CHECK(nb == want && ns == want, "%s has %d BSSY and %d BSYNC (want %d)", PROBES[i].name, nb, ns, want); + if (PROBES[i].op == OMEGA_GPU_EW_RC_DEPTH16) { + 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; + CHECK(each, "depth16 uses every barrier register B0..B15 exactly once"); + } + CHECK(sb_hazards(p) == 0, "%s scoreboard walk clean", PROBES[i].name); + CHECK(edge_waits(p) == 0, "%s every control op waits on everything in flight", PROBES[i].name); + printf("codegen %s: %zu insns, %u gprs, %d regions\n", PROBES[i].name, p->count, k.gpr_count, nb); + uint8_t d0[32]; memcpy(d0, k.code_digest, 32); free(k.code); + for (int m = 1; m <= 3; m++) { + BlackwellIRProgram *mp = calloc(1, sizeof *mp); OmegaBlackwellKernel mk; + int mrc = codegen(PROBES[i].op, m, mp, &mk); + int same_ok = m == 1 && PROBES[i].op == OMEGA_GPU_EW_RC_DEPTH16; /* depth16 has no math mutant */ + 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)); + if (mrc == OMEGA_GPU_EW_OK) { + 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); + if (m == 3) CHECK(count_op(mp, BW_IR_BSYNC, -1) == ns, "%s mutant 3 keeps its BSYNCs", PROBES[i].name); + free(mk.code); + } + free(mp); + } + free(p); + } + /* the four regioned sieves build too (their sweep runs in gb10_sieve_host_test) */ + for (int op = OMEGA_GPU_EW_PRIME_SIEVE_DIV1; op <= OMEGA_GPU_EW_PRIME_SIEVE_SHARED_DIV2; op++) { + BlackwellIRProgram *p = calloc(1, sizeof *p); OmegaBlackwellKernel k; + int rc = codegen((OmegaGpuEwOp)op, 0, p, &k); + int staged = op == OMEGA_GPU_EW_PRIME_SIEVE_SHARED_DIV1 || op == OMEGA_GPU_EW_PRIME_SIEVE_SHARED_DIV2; + 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; + 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); + if (rc == OMEGA_GPU_EW_OK) free(k.code); + free(p); + } + + /* 4. nvdisasm: the words decode as BSSY/BSYNC on the real barrier registers, and the predicated + * and B15 forms the encoder can produce decode too (built by hand: encode_one) */ + { + static const char *const m0[] = { "BSSY", "BSYNC", "B0", "SHFL.DOWN", "@P0 BRA" }; + CHECK(nvdisasm_kernel(OMEGA_GPU_EW_RC_DIAMOND, 0, "diamond", m0, 5) == 0, "diamond listing decodes with BSSY/BSYNC B0"); + static const char *const m1[] = { "BSSY", "BSYNC", "B15", "B7" }; + CHECK(nvdisasm_kernel(OMEGA_GPU_EW_RC_DEPTH16, 0, "depth16", m1, 4) == 0, "depth16 listing decodes with B15"); + static const char *const m2[] = { "BSSY", "B1", "BSYNC" }; + CHECK(nvdisasm_kernel(OMEGA_GPU_EW_PRIME_SIEVE_DIV2, 0, "sieve_div2", m2, 3) == 0, "nested sieve listing decodes with B1"); + 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; + 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); + 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); + 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); + in.op = BW_IR_EXIT; omega_blackwell_encode_one(&in, w); memcpy(code + 48, w, 16); + static const char *const m3[] = { "@P0 BSSY", "B3", "@!P0 BSYNC", "BSYNC.RECONVERGENT B15" }; + CHECK(nvdisasm_bytes(code, 64, "forms", m3, 4) == 0, "predicated BSSY/BSYNC and B15 decode"); + } + + /* 5. simulator: every probe, every n, both fragment orders, exact against the oracle */ + uint32_t *a = malloc(20000 * 4), *b = malloc(20000 * 4), *got = malloc(20000 * 4), *want = malloc(20000 * 4); + for (size_t i = 0; i < NPROBES; i++) { + BlackwellIRProgram *p = calloc(1, sizeof *p); OmegaBlackwellKernel k; + if (codegen(PROBES[i].op, 0, p, &k) != OMEGA_GPU_EW_OK) { free(p); continue; } + int fails = 0; uint64_t cases = 0; + for (size_t j = 0; j < NNS; j++) for (int order = 0; order < 2; order++) { + uint32_t n = NS[j]; fill(a, b, n, 0x308u + n * 7u + (uint32_t)order); + rc_oracle(PROBES[i].op, 0, n, a, b, want); + WsSim st; int err = ws_probe(p, n, a, b, got, order, &st); + uint32_t first; size_t bad = diff_count(got, want, n, &first); + cases++; + 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); } + 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; } + } + CHECK(fails == 0, "%s simulator vs oracle: %d of %" PRIu64 " cases differ", PROBES[i].name, fails, cases); + /* math mutant: simulator clean, result wrong (the oracle sees it) */ + BlackwellIRProgram *mp = calloc(1, sizeof *mp); OmegaBlackwellKernel mk; + if (codegen(PROBES[i].op, 1, mp, &mk) == OMEGA_GPU_EW_OK) { + uint32_t n = 1000; fill(a, b, n, 0x1001u); rc_oracle(PROBES[i].op, 0, n, a, b, want); + 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); + 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); + 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); + else CHECK(err == 0 && bad > 0, "%s math mutant: simulator clean, %zu words differ (err %d)", PROBES[i].name, bad, err); + free(mk.code); + } + free(mp); + /* dropped join and corrupted target: the simulator must reject both, in both orders */ + for (int m = 2; m <= 3; m++) for (int order = 0; order < 2; order++) { + BlackwellIRProgram *xp = calloc(1, sizeof *xp); OmegaBlackwellKernel xk; + if (codegen(PROBES[i].op, m, xp, &xk) == OMEGA_GPU_EW_OK) { + uint32_t n = 1000; fill(a, b, n, 0x2002u + (uint32_t)m); + WsSim st; int err = ws_probe(xp, n, a, b, got, order, &st); + 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"); + 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)); + free(xk.code); + } + free(xp); + } + free(k.code); free(p); + } + /* 6. hand mutations of the production diamond (controls on the simulator itself, independent of + * the emitter's mutant hooks): exit jumping past the BSYNC, BSYNC on the wrong barrier, a BSSY + * on an armed barrier. And the unmodified program as the positive control. */ + { + BlackwellIRProgram *p = calloc(1, sizeof *p), *q = calloc(1, sizeof *q); OmegaBlackwellKernel k; + if (codegen(OMEGA_GPU_EW_RC_DIAMOND, 0, p, &k) == OMEGA_GPU_EW_OK) { + uint32_t n = 500; fill(a, b, n, 0x3003u); WsSim st; + int bssy = find_op(p, BW_IR_BSSY, 0), bsync = find_op(p, BW_IR_BSYNC, 0), ex = -1; + for (int i = bssy + 1; i < bsync; i++) if (p->insns[i].op == BW_IR_BRA && !p->insns[i].predicate_p0) ex = i; + CHECK(bssy >= 0 && bsync > bssy && ex > bssy, "diamond shape: BSSY %d, exit %d, BSYNC %d", bssy, ex, bsync); + CHECK(ws_probe(p, n, a, b, got, 0, &st) == 0, "positive control: untouched diamond runs clean"); + *q = *p; q->insns[ex].imm += 1; /* exit lands after the BSYNC */ + int e1 = ws_probe(q, n, a, b, got, 0, &st); CHECK(is_reconv_err(e1), "exit past the BSYNC rejected: %s", st.msg); + *q = *p; q->insns[bsync].bar_reg = 1; /* BSYNC on a barrier nobody armed */ + 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); + *q = *p; q->insns[bsync].op = BW_IR_NOP; /* join dropped by hand */ + int e3 = ws_probe(q, n, a, b, got, 0, &st); CHECK(is_reconv_err(e3), "join replaced by NOP rejected: %s", st.msg); + *q = *p; q->insns[bssy].imm += 1; /* names the wrong join */ + int e4 = ws_probe(q, n, a, b, got, 0, &st); CHECK(e4 == WS_E_JOIN_MISMATCH, "wrong join target rejected: %s", st.msg); + *q = *p; q->insns[bssy + 1] = p->insns[bssy]; /* second BSSY on the armed barrier (overwrites the ISETP) */ + 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); + free(k.code); + } + free(p); free(q); + } + free(a); free(b); free(got); free(want); + 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); + return g_failed == before ? 0 : -1; +} + +/* --------------------------------------------------------- chip part */ +static int chip(const char *out_path, int observe) { + FILE *out = out_path ? fopen(out_path, "w") : NULL; + if (out_path && !out) { printf("cannot open %s\n", out_path); return 2; } + if (out) fprintf(out, "{\"schema\":\"OMEGA_GPU_RECONV_V1\",\"issue\":308,\"comparison\":\"exact u32\",\"cases\":["); + int first = 1; + uint32_t *a = malloc(20000 * 4), *b = malloc(20000 * 4), *got = malloc(20000 * 4), *want = malloc(20000 * 4); + for (size_t i = 0; i < NPROBES; i++) { + for (size_t j = 0; j < NNS; j++) { + uint32_t n = NS[j]; fill(a, b, n, 0x5050u + n); + rc_oracle(PROBES[i].op, 0, n, a, b, want); + OmegaGpuEwInfo info; memset(&info, 0, sizeof info); + int rc = omega_gpu_reconv_probe_u32(PROBES[i].op, n, a, b, got, &info); + uint32_t fi; size_t bad = rc == OMEGA_GPU_EW_OK ? diff_count(got, want, n, &fi) : n; + int pass = rc == OMEGA_GPU_EW_OK && bad == 0; + 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()); + 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}", + 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"); + first = 0; + if (!pass) { printf("STOP: %s failed on the chip at n=%u; no further launches of this probe\n", PROBES[i].name, n); break; } + } + } + if (out) fprintf(out, "],\"mutants\":["); + first = 1; + for (size_t i = 0; i < NPROBES; i++) { + if (PROBES[i].op == OMEGA_GPU_EW_RC_DEPTH16) continue; /* no math mutant */ + uint32_t n = 1000; fill(a, b, n, 0x6060u); rc_oracle(PROBES[i].op, 0, n, a, b, want); + omega_gpu_elementwise_test_set_mutant((int)PROBES[i].op); + /* R6's math mutant is the early-exit kernel: the emitter refuses it on the launch path + * (the bypass exists only for codegen_ir), so the catch is CODEGEN_FAIL: no undefined + * SHFL ever reaches the chip. */ + OmegaGpuEwInfo info; memset(&info, 0, sizeof info); + int rc = omega_gpu_reconv_probe_u32(PROBES[i].op, n, a, b, got, &info); + uint32_t fi; size_t bad = rc == OMEGA_GPU_EW_OK ? diff_count(got, want, n, &fi) : n; + int caught = rc != OMEGA_GPU_EW_OK || bad > 0; + omega_gpu_elementwise_test_set_mutant(0); + CHECK(caught, "%s math mutant caught (rc %s, violations %zu)", PROBES[i].name, omega_gpu_elementwise_rc_name(rc), bad); + 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)); + 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"); + first = 0; + } + if (out) fprintf(out, "],\"observations\":["); + if (observe) { + /* The dropped-join kernel (rejected by the simulator above) on the chip once, small n: + * recorded as an observation of what the hardware does without the BSYNC. Not a gate. */ + uint32_t n = 1000; fill(a, b, n, 0x7070u); rc_oracle(OMEGA_GPU_EW_RC_DIAMOND, 0, n, a, b, want); + omega_gpu_elementwise_test_set_mutant_level(OMEGA_GPU_EW_RC_DIAMOND, 2); + OmegaGpuEwInfo info; memset(&info, 0, sizeof info); + int rc = omega_gpu_reconv_probe_u32(OMEGA_GPU_EW_RC_DIAMOND, n, a, b, got, &info); + uint32_t fi; size_t bad = rc == OMEGA_GPU_EW_OK ? diff_count(got, want, n, &fi) : n; + omega_gpu_elementwise_test_set_mutant(0); + 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); + 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); + } + CHECK(omega_gpu_session_open_count() == 1, "device opened once for the battery (opens=%u)", omega_gpu_session_open_count()); + if (out) { + fprintf(out, "],\"sim_model_n4096\":["); + 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); + 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"); + fclose(out); + } + free(a); free(b); free(got); free(want); + return 0; +} + +int main(int argc, char **argv) { + const char *out_path = NULL; int host = 0, observe = 0; + for (int i = 1; i < argc; i++) { + if (strcmp(argv[i], "--host-only") == 0) host = 1; + else if (strcmp(argv[i], "--observe-dropped-join") == 0) observe = 1; + else if (strcmp(argv[i], "--out") == 0 && i + 1 < argc) out_path = argv[++i]; + } + int hrc = host_part(); + if (!host) { + if (hrc != 0) { printf("host part failed: no chip launches\nFAIL: %d checks, %d failed\n", g_checks, g_failed); return 1; } + if (chip(out_path, observe) != 0) return 2; + } + printf("%s: %d checks, %d failed\n", g_failed ? "FAIL" : "PASS", g_checks, g_failed); + return g_failed ? 1 : 0; +} diff --git a/evidence/gpu-reconv-308/T1b-GPU-RECONV-0a1812f-observe-dropped-join/omega_blackwell_codegen.c b/evidence/gpu-reconv-308/T1b-GPU-RECONV-0a1812f-observe-dropped-join/omega_blackwell_codegen.c new file mode 100644 index 000000000..38f73412a --- /dev/null +++ b/evidence/gpu-reconv-308/T1b-GPU-RECONV-0a1812f-observe-dropped-join/omega_blackwell_codegen.c @@ -0,0 +1,1945 @@ +#include "omega_blackwell_codegen.h" +#include "omega_blackwell_submit.h" +#include "sha256.h" +#include +#include +#include + +void omega_bw_ir_init(BlackwellIRProgram *prog) { + if (!prog) return; + memset(prog, 0, sizeof(*prog)); +} + +int omega_bw_ir_alloc_vreg(BlackwellIRProgram *prog) { + if (!prog || prog->regalloc.num_vregs >= BW_MAX_VREGS) return -1; + int v = prog->regalloc.num_vregs++; + prog->regalloc.intervals[v].vreg = v; + prog->regalloc.intervals[v].first_def = -1; + prog->regalloc.intervals[v].last_use = -1; + prog->regalloc.intervals[v].phys_reg = -1; + prog->regalloc.intervals[v].bundle_size = 1; + prog->regalloc.intervals[v].is_pair = false; + prog->regalloc.intervals[v].is_quad = false; + prog->regalloc.intervals[v].active = false; + prog->regalloc.vreg_to_phys[v] = -1; + return v; +} + +int omega_bw_ir_alloc_vreg64(BlackwellIRProgram *prog) { + if (!prog || prog->regalloc.num_vregs >= BW_MAX_VREGS) return -1; + int v = prog->regalloc.num_vregs++; + prog->regalloc.intervals[v].vreg = v; + prog->regalloc.intervals[v].first_def = -1; + prog->regalloc.intervals[v].last_use = -1; + prog->regalloc.intervals[v].phys_reg = -1; + prog->regalloc.intervals[v].bundle_size = 2; + prog->regalloc.intervals[v].is_pair = true; + prog->regalloc.intervals[v].is_quad = false; + prog->regalloc.intervals[v].active = false; + prog->regalloc.vreg_to_phys[v] = -1; + return v; +} + +int omega_bw_ir_alloc_vreg128(BlackwellIRProgram *prog) { + if (!prog || prog->regalloc.num_vregs >= BW_MAX_VREGS) return -1; + int v = prog->regalloc.num_vregs++; + prog->regalloc.intervals[v].vreg = v; + prog->regalloc.intervals[v].first_def = -1; + prog->regalloc.intervals[v].last_use = -1; + prog->regalloc.intervals[v].phys_reg = -1; + prog->regalloc.intervals[v].bundle_size = 4; + prog->regalloc.intervals[v].is_pair = false; + prog->regalloc.intervals[v].is_quad = true; + prog->regalloc.intervals[v].active = false; + prog->regalloc.vreg_to_phys[v] = -1; + return v; +} + +int omega_bw_ir_alloc_uvreg(BlackwellIRProgram *prog) { + if (!prog || prog->regalloc.num_uvregs >= BW_MAX_UVREGS) return -1; + int uv = prog->regalloc.num_uvregs++; + prog->regalloc.uintervals[uv].vreg = uv; + prog->regalloc.uintervals[uv].first_def = -1; + prog->regalloc.uintervals[uv].last_use = -1; + prog->regalloc.uintervals[uv].phys_reg = -1; + prog->regalloc.uintervals[uv].is_pair = false; + prog->regalloc.uintervals[uv].active = false; + prog->regalloc.uvreg_to_phys[uv] = -1; + return uv; +} + +int omega_bw_ir_alloc_uvreg64(BlackwellIRProgram *prog) { + if (!prog || prog->regalloc.num_uvregs >= BW_MAX_UVREGS) return -1; + int uv = prog->regalloc.num_uvregs++; + prog->regalloc.uintervals[uv].vreg = uv; + prog->regalloc.uintervals[uv].first_def = -1; + prog->regalloc.uintervals[uv].last_use = -1; + prog->regalloc.uintervals[uv].phys_reg = -1; + prog->regalloc.uintervals[uv].is_pair = true; + prog->regalloc.uintervals[uv].active = false; + prog->regalloc.uvreg_to_phys[uv] = -1; + return uv; +} + +int omega_bw_ir_append(BlackwellIRProgram *prog, const BlackwellIRInsn *insn) { + if (!prog || !insn || prog->count >= BW_MAX_IR_INSNS) return -1; + int idx = (int)prog->count++; + prog->insns[idx] = *insn; + + /* Update live-interval def/use tracking */ + if (insn->dst_vreg >= 0 && insn->dst_vreg < prog->regalloc.num_vregs) { + if (!insn->is_uniform) { + OmegaLiveInterval *iv = &prog->regalloc.intervals[insn->dst_vreg]; + if (iv->first_def < 0) iv->first_def = idx; + if (idx > iv->last_use) iv->last_use = idx; + } + } + if (insn->src1_vreg >= 0 && insn->src1_vreg < prog->regalloc.num_vregs) { + OmegaLiveInterval *iv = &prog->regalloc.intervals[insn->src1_vreg]; + if (iv->first_def < 0) iv->first_def = idx; + if (idx > iv->last_use) iv->last_use = idx; + } + if (insn->src2_vreg >= 0 && insn->src2_vreg < prog->regalloc.num_vregs) { + OmegaLiveInterval *iv = &prog->regalloc.intervals[insn->src2_vreg]; + if (iv->first_def < 0) iv->first_def = idx; + if (idx > iv->last_use) iv->last_use = idx; + } + if (insn->src3_vreg >= 0 && insn->src3_vreg < prog->regalloc.num_vregs) { + OmegaLiveInterval *iv = &prog->regalloc.intervals[insn->src3_vreg]; + if (iv->first_def < 0) iv->first_def = idx; + if (idx > iv->last_use) iv->last_use = idx; + } + + /* Uniform register def/use tracking */ + if (insn->is_uniform && insn->dst_vreg >= 0 && insn->dst_vreg < prog->regalloc.num_uvregs) { + OmegaLiveInterval *uiv = &prog->regalloc.uintervals[insn->dst_vreg]; + if (uiv->first_def < 0) uiv->first_def = idx; + if (idx > uiv->last_use) uiv->last_use = idx; + } + if (insn->ureg >= 0 && insn->ureg < prog->regalloc.num_uvregs) { + OmegaLiveInterval *uiv = &prog->regalloc.uintervals[insn->ureg]; + if (uiv->first_def < 0) uiv->first_def = idx; + if (idx > uiv->last_use) uiv->last_use = idx; + } + + return idx; +} + +int omega_bw_regalloc_solve(BlackwellIRProgram *prog) { + if (!prog) return -1; + OmegaRegAlloc *ra = &prog->regalloc; + + /* + * Bounded deterministic linear-scan register allocator. + * Computes genuine live intervals across virtual registers and assigns + * available physical GPRs (R2..R127) and UGPRs (UR4..UR31). + * Enforces even-alignment and dual-register reservation for 64-bit pairs. + */ + bool gpr_busy[BW_PHYS_GPR_MAX]; + memset(gpr_busy, 0, sizeof(gpr_busy)); + + int max_phys_used = BW_PHYS_GPR_START; + + /* Sort virtual register indices chronologically by first_def (Poletto & Sarkar Linear Scan) */ + int order[BW_MAX_VREGS]; + int count = 0; + for (int v = 0; v < ra->num_vregs; v++) { + if (ra->intervals[v].first_def >= 0) { + order[count++] = v; + } else { + ra->intervals[v].phys_reg = BW_PHYS_GPR_START; + ra->vreg_to_phys[v] = ra->intervals[v].phys_reg; + } + } + for (int i = 0; i < count - 1; i++) { + for (int j = i + 1; j < count; j++) { + if (ra->intervals[order[i]].first_def > ra->intervals[order[j]].first_def) { + int tmp = order[i]; + order[i] = order[j]; + order[j] = tmp; + } + } + } + + for (int idx = 0; idx < count; idx++) { + int v = order[idx]; + OmegaLiveInterval *iv = &ra->intervals[v]; + + /* Free physical registers whose virtual live intervals have ended */ + for (int p = 0; p < ra->num_vregs; p++) { + if (ra->intervals[p].active && ra->intervals[p].last_use < iv->first_def) { + int freed = ra->intervals[p].phys_reg; + if (freed >= 0 && freed < BW_PHYS_GPR_MAX) { + uint32_t bsize = ra->intervals[p].bundle_size ? ra->intervals[p].bundle_size : (ra->intervals[p].is_pair ? 2 : (ra->intervals[p].is_quad ? 4 : 1)); + for (uint32_t k = 0; k < bsize && (freed + (int)k) < BW_PHYS_GPR_MAX; k++) { + gpr_busy[freed + k] = false; + } + } + ra->intervals[p].active = false; + } + } + + /* Find lowest available physical register */ + int assigned = -1; + uint32_t bsize = iv->bundle_size ? iv->bundle_size : (iv->is_pair ? 2 : (iv->is_quad ? 4 : 1)); + if (bsize == 4) { + /* 128-bit quad requires 4-alignment and 4 contiguous free registers */ + int start = (BW_PHYS_GPR_START + 3) & ~3; + for (int p = start; p <= BW_PHYS_GPR_MAX - 4; p += 4) { + if (!gpr_busy[p] && !gpr_busy[p + 1] && !gpr_busy[p + 2] && !gpr_busy[p + 3]) { + assigned = p; + for (int k = 0; k < 4; k++) gpr_busy[p + k] = true; + break; + } + } + } else if (bsize == 2) { + /* 64-bit pair requires even alignment and contiguous free registers */ + int start = (BW_PHYS_GPR_START + 1) & ~1; + for (int p = start; p <= BW_PHYS_GPR_MAX - 2; p += 2) { + if (!gpr_busy[p] && !gpr_busy[p + 1]) { + assigned = p; + gpr_busy[p] = true; + gpr_busy[p + 1] = true; + break; + } + } + } else { + for (int p = BW_PHYS_GPR_START; p < BW_PHYS_GPR_MAX; p++) { + if (!gpr_busy[p]) { + assigned = p; + gpr_busy[p] = true; + break; + } + } + } + + if (assigned < 0) { + /* Hardware register capacity exceeded: fail closed loudly */ + return -1; + } + + iv->phys_reg = assigned; + iv->active = true; + ra->vreg_to_phys[v] = assigned; + + int top = assigned + (int)bsize - 1; + if (top > max_phys_used) { + max_phys_used = top; + } + } + ra->peak_gpr_usage = (uint32_t)(max_phys_used + 1); + + /* Allocate uniform registers */ + int cur_u = BW_PHYS_UGPR_START; + for (int uv = 0; uv < ra->num_uvregs; uv++) { + OmegaLiveInterval *uiv = &ra->uintervals[uv]; + if (uiv->is_pair) { + if (cur_u % 2 != 0) cur_u++; + if (cur_u + 1 >= BW_PHYS_UGPR_MAX) return -1; + uiv->phys_reg = cur_u; + ra->uvreg_to_phys[uv] = cur_u; + cur_u += 2; + } else { + if (cur_u >= BW_PHYS_UGPR_MAX) return -1; + uiv->phys_reg = cur_u; + ra->uvreg_to_phys[uv] = cur_u++; + } + } + ra->peak_ugpr_usage = (uint32_t)cur_u; + + return 0; +} + +static int encode_single_insn(const BlackwellIRInsn *insn, const OmegaRegAlloc *ra, uint32_t w[4]) { + memset(w, 0, 16); + + int dst = (insn->dst_vreg >= 0) ? (ra->vreg_to_phys[insn->dst_vreg] + insn->dst_subreg) : 0; + int src1 = (insn->src1_vreg >= 0) ? (ra->vreg_to_phys[insn->src1_vreg] + insn->src1_subreg) : 0; + int src2 = (insn->src2_vreg >= 0) ? (ra->vreg_to_phys[insn->src2_vreg] + insn->src2_subreg) : 0; + int src3 = (insn->src3_vreg >= 0) ? (ra->vreg_to_phys[insn->src3_vreg] + insn->src3_subreg) : 0xff; /* Default to RZ (0xff) */ + int ureg = (insn->ureg >= 0) ? ra->uvreg_to_phys[insn->ureg] : 0; + + if (insn->is_uniform && insn->dst_vreg >= 0) { + dst = ra->uvreg_to_phys[insn->dst_vreg]; + } + + switch (insn->op) { + case BW_IR_NOP: + w[0] = 0x00007918; + w[1] = 0x00000000; + w[2] = 0x00000000; + w[3] = 0x000fc000; + break; + + case BW_IR_MOV_RZ: + /* MOV Rd, RZ */ + w[0] = 0x7202 | ((uint32_t)(dst & 0xff) << 16); + w[1] = 0x000000ff; + w[2] = 0x00000f00; + w[3] = insn->control ? insn->control : 0x000fe200; + break; + + case BW_IR_MOV_IMM: + /* MOV Rd, imm32 */ + w[0] = 0x7802 | ((uint32_t)(dst & 0xff) << 16); + w[1] = insn->imm; + w[2] = 0x00000f00; + w[3] = insn->control ? insn->control : 0x000fe200; + break; + + case BW_IR_S2R: + /* S2R Rd, SR */ + w[0] = 0x7919 | ((uint32_t)(dst & 0xff) << 16); + w[1] = 0x0; + w[2] = (insn->imm & 0xff) << 8; + w[3] = insn->control ? insn->control : 0x000e2200; + break; + + case BW_IR_LDC: + /* LDC Rd, c[0x0][imm] */ + w[0] = 0xff007b82 | ((uint32_t)(dst & 0xff) << 16); + w[1] = (insn->imm << 6) & 0x00ffff00; + w[2] = 0x00000800; + w[3] = insn->control ? insn->control : 0x000fe200; + break; + + case BW_IR_LDC64: + /* LDC.64 Rd:Rd+1, c[0x0][imm] */ + w[0] = 0xff007b82 | ((uint32_t)(dst & 0xff) << 16); + w[1] = (insn->imm << 6) & 0x00ffff00; + w[2] = 0x00000a00; + w[3] = insn->control ? insn->control : 0x000e2200; + break; + + case BW_IR_LDCU: + /* LDCU URd, c[0x0][imm] */ + w[0] = 0xff0077ac | ((uint32_t)(dst & 0xff) << 16); + w[1] = (insn->imm << 5) & 0x00ffff00; + w[2] = 0x08000800; + w[3] = insn->control ? insn->control : 0x000e2200; + break; + + case BW_IR_LDCU64: + /* LDCU.64 URd:URd+1, c[0x0][imm] */ + w[0] = 0xff0077ac | ((uint32_t)(dst & 0xff) << 16); + w[1] = (insn->imm << 5) & 0x00ffff00; + w[2] = 0x08000a00; + w[3] = insn->control ? insn->control : 0x000e6e00; + break; + + case BW_IR_IMAD: + if (insn->src2_vreg >= 0) { + /* Register-register: IMAD Rd, Ra, Rb, Rc (0x7224) */ + w[0] = 0x7224 | ((uint32_t)(dst & 0xff) << 16) | ((uint32_t)(src1 & 0xff) << 24); + w[1] = (uint32_t)(src2 & 0xff); + w[2] = 0x078e0200 | (uint32_t)(src3 & 0xff); + w[3] = insn->control ? insn->control : 0x001fca00; + } else if (insn->is_uniform && insn->ureg >= 0) { + /* Uniform register multiplier: IMAD Rd, Ra, URb, Rc (0x7c24) */ + w[0] = 0x7c24 | ((uint32_t)(dst & 0xff) << 16) | ((uint32_t)(src1 & 0xff) << 24); + w[1] = (uint32_t)(ureg & 0xff); + w[2] = 0x0f8e0200 | (uint32_t)(src3 & 0xff); + w[3] = insn->control ? insn->control : 0x001fca00; + } else { + /* Immediate multiplier: IMAD Rd, Ra, imm, Rc (0x7824) */ + w[0] = 0x7824 | ((uint32_t)(dst & 0xff) << 16) | ((uint32_t)(src1 & 0xff) << 24); + w[1] = insn->imm; + w[2] = 0x078e0200 | (uint32_t)(src3 & 0xff); + w[3] = insn->control ? insn->control : 0x001fca00; + } + break; + + case BW_IR_IMAD_WIDE: + /* IMAD.WIDE.U32 Rd:Rd+1, Ra, imm, Rc:Rc+1 */ + w[0] = 0x7825 | ((uint32_t)(dst & 0xff) << 16) | ((uint32_t)(src1 & 0xff) << 24); + w[1] = insn->imm; + w[2] = 0x078e0000 | (uint32_t)(src3 & 0xff); + w[3] = insn->control ? insn->control : 0x001fcc00; + break; + + case BW_IR_ISETP_GE: + if (insn->is_uniform && insn->ureg >= 0) { + w[0] = 0x7c0c | ((uint32_t)(dst & 0xff) << 16) | ((uint32_t)(src1 & 0xff) << 24); + w[1] = (uint32_t)(ureg & 0xff); + w[2] = 0x0bf06070; + w[3] = 0x002fda00; + } else { + w[0] = 0x780c | ((uint32_t)(dst & 0xff) << 16) | ((uint32_t)(src1 & 0xff) << 24); + w[1] = insn->imm; + w[2] = 0x03f06270; + w[3] = 0x001fda00; + } + break; + + case BW_IR_LDG_E: + /* LDG.E Rd, desc[URd][Ra.64] */ + w[0] = 0x7981 | ((uint32_t)(dst & 0xff) << 16) | ((uint32_t)(src1 & 0xff) << 24); + w[1] = (uint32_t)(ureg & 0xff); + w[2] = 0x0c1e1900; + w[3] = insn->control ? insn->control : 0x000f2200; + break; + + case BW_IR_STG_E: + /* STG.E desc[URd][Ra.64], Rb */ + w[0] = 0x7986 | ((uint32_t)(src1 & 0xff) << 24); + w[1] = (uint32_t)(src2 & 0xff); + w[2] = 0x0c101900 | (uint32_t)(ureg & 0xff); + w[3] = insn->control ? insn->control : 0x000fe200; + break; + + case BW_IR_STG_EF: + /* STG.E.MMIO.GPU. Uncached store, meant to be visible without a fence. */ + w[0] = 0x7986 | ((uint32_t)(src1 & 0xff) << 24); + w[1] = (uint32_t)(src2 & 0xff); + w[2] = 0x0c111900 | (uint32_t)(ureg & 0xff); + w[3] = insn->control ? insn->control : 0x000fe200; + break; + + case BW_IR_IADD3: + /* IADD3 Rd, PT, PT, Ra, Rb, Rc */ + w[0] = 0x7210 | ((uint32_t)(dst & 0xff) << 16) | ((uint32_t)(src1 & 0xff) << 24); + w[1] = (uint32_t)(src2 & 0xff); + w[2] = 0x07ffe0ff; + w[3] = 0x010fca00; + break; + + case BW_IR_HMMA_F16: + case BW_IR_HMMA_BF16: { + /* HMMA.16816.F32[.BF16] Rd, Ra, Rb, Rc */ + w[0] = 0x723c | ((uint32_t)(dst & 0xff) << 16) | ((uint32_t)(src1 & 0xff) << 24); + w[1] = (uint32_t)(src2 & 0xff); + uint32_t prec_bit = (insn->op == BW_IR_HMMA_BF16) ? 0x00040000 : 0x00000000; + w[2] = (uint32_t)(src3 & 0xff) | (0x18 << 8) | prec_bit; + w[3] = insn->control ? insn->control : 0x000fe200; + break; + } + + case BW_IR_SHF_R: + /* SHF.R.U32.HI Rd, RZ, imm, Ra */ + w[0] = 0x7819U | ((uint32_t)(dst & 0xff) << 16) | (0xffU << 24); + w[1] = (uint32_t)insn->imm; + w[2] = 0x00011600 | (uint32_t)(src1 & 0xff); + w[3] = insn->control ? insn->control : 0x001fca00; + break; + + case BW_IR_LOP3_AND: + /* LOP3.LUT Rd, Ra, imm, RZ, 0xc0, !PT */ + w[0] = 0x7812U | ((uint32_t)(dst & 0xff) << 16) | ((uint32_t)(src1 & 0xff) << 24); + w[1] = (uint32_t)insn->imm; + w[2] = 0x078ec0ff; + w[3] = insn->control ? insn->control : 0x001fca00; + break; + + case BW_IR_LDG_E_U16: + /* LDG.E.U16 Rd, desc[URd][Ra.64] */ + w[0] = 0x7981U | ((uint32_t)(dst & 0xff) << 16) | ((uint32_t)(src1 & 0xff) << 24); + w[1] = (uint32_t)(ureg & 0xff); + w[2] = 0x0c1e1500; + w[3] = insn->control ? insn->control : 0x000f2200; + break; + + case BW_IR_LDG_STRONG_SYS: + /* LDG.E.STRONG.SYS. Same shape as LDG.E; w[2] carries the + * system-scope bits (0x0c1e1900 -> 0x0c1f5900). */ + w[0] = 0x7981 | ((uint32_t)(dst & 0xff) << 16) | ((uint32_t)(src1 & 0xff) << 24); + w[1] = (uint32_t)(ureg & 0xff); + w[2] = 0x0c1f5900; + w[3] = insn->control ? insn->control : 0x000f2200; + break; + + case BW_IR_LDG_MMIO: + /* LDG.E.EF. Drop the cached line, then load. */ + w[0] = 0x7981 | ((uint32_t)(dst & 0xff) << 16) | ((uint32_t)(src1 & 0xff) << 24); + w[1] = (uint32_t)(ureg & 0xff); + w[2] = 0x0c0e1900; + w[3] = insn->control ? insn->control : 0x000f2200; + break; + + case BW_IR_STG_STRONG_SYS: + w[0] = 0x7986 | ((uint32_t)(src1 & 0xff) << 24); + if (insn->predicate_p0) { + uint32_t pred = insn->predicate_not ? 0x8u : 0x0u; + w[0] = (w[0] & ~0xF000u) | (pred << 12); + } + w[1] = (uint32_t)(src2 & 0xff); + w[2] = 0x0c115900 | (uint32_t)(ureg & 0xff); + w[3] = insn->control ? insn->control : 0x000fe200; + break; + + case BW_IR_MEMBAR_ALL_SYS: + w[0] = 0x00007992; + w[1] = 0x00000000; + w[2] = 0x0000b000; + w[3] = insn->control ? insn->control : 0x000fec00; + break; + + case BW_IR_MEMBAR_SC_SYS: + w[0] = 0x00007992; + w[1] = 0x00000000; + w[2] = 0x00003000; + w[3] = insn->control ? insn->control : 0x000fec00; + break; + + case BW_IR_CCTL_IVALL: + w[0] = 0xff00798f; + w[1] = 0x00000000; + w[2] = 0x02000000; + w[3] = insn->control ? insn->control : 0x000fe800; + break; + + case BW_IR_ATOMG_ADD_STRONG_SYS: + w[0] = 0x79a8 | ((uint32_t)(dst & 0xff) << 16) | ((uint32_t)(src1 & 0xff) << 24); + w[1] = (uint32_t)(src2 & 0xff) | 0x80000000u; + w[2] = 0x081f5100 | (uint32_t)(ureg & 0xff); + w[3] = insn->control ? insn->control : 0x00321e00; + break; + + case BW_IR_ATOMG_EXCH_STRONG_SYS: + w[0] = 0x79a8 | ((uint32_t)(dst & 0xff) << 16) | ((uint32_t)(src1 & 0xff) << 24); + w[1] = (uint32_t)(src2 & 0xff) | 0x80000000u; + w[2] = 0x0c1f5100 | (uint32_t)(ureg & 0xff); + w[3] = insn->control ? insn->control : 0x00321e00; + break; + + case BW_IR_ISETP_GE_U32: + /* ISETP.GE.U32.AND P0, PT, Ra, Rb, PT. Checked with nvdisasm -b SM121. */ + w[0] = 0x720c | ((uint32_t)(src1 & 0xff) << 24); + w[1] = (uint32_t)(src2 & 0xff); + w[2] = 0x03f06070; + w[3] = insn->control ? insn->control : 0x001fda00; + break; + + case BW_IR_LOP3_XOR: + /* LOP3.LUT Rd, Ra, Rb, RZ, 0x3c, !PT. Checked with nvdisasm -b SM121. */ + w[0] = 0x7212 | ((uint32_t)(dst & 0xff) << 16) | ((uint32_t)(src1 & 0xff) << 24); + w[1] = (uint32_t)(src2 & 0xff); + w[2] = 0x078e3cff; + w[3] = insn->control ? insn->control : 0x001fca00; + break; + + case BW_IR_FADD: + /* FADD Rd, Ra, Rb */ + w[0] = 0x7221U | ((uint32_t)(dst & 0xff) << 16) | ((uint32_t)(src1 & 0xff) << 24); + w[1] = (uint32_t)(src2 & 0xff); + w[2] = 0x00000000; + w[3] = insn->control ? insn->control : 0x004fc400; + break; + + case BW_IR_FSUB: + /* FSUB Rd, Ra, Rb (FADD Rd, Ra, -Rb) */ + w[0] = 0x7221U | ((uint32_t)(dst & 0xff) << 16) | ((uint32_t)(src1 & 0xff) << 24); + w[1] = (uint32_t)(src2 & 0xff) | 0x80000000U; + w[2] = 0x00000000; + w[3] = insn->control ? insn->control : 0x004fc400; + break; + + case BW_IR_FMUL: + /* FMUL Rd, Ra, Rb */ + w[0] = 0x7220U | ((uint32_t)(dst & 0xff) << 16) | ((uint32_t)(src1 & 0xff) << 24); + w[1] = (uint32_t)(src2 & 0xff); + w[2] = 0x00400000; + w[3] = insn->control ? insn->control : 0x004fc400; + break; + + case BW_IR_FFMA: + /* FFMA Rd, Ra, Rb, Rc */ + w[0] = 0x7223U | ((uint32_t)(dst & 0xff) << 16) | ((uint32_t)(src1 & 0xff) << 24); + w[1] = (uint32_t)(src2 & 0xff); + w[2] = (uint32_t)(src3 & 0xff); + w[3] = insn->control ? insn->control : 0x004fc400; + break; + + case BW_IR_FSETP: + /* FSETP.cond.AND P0, PT, Ra, Rb, PT */ + w[0] = 0x720bU | ((uint32_t)(dst & 0x7) << 16) | ((uint32_t)(src1 & 0xff) << 24); + w[1] = (uint32_t)(src2 & 0xff); + w[2] = insn->imm ? insn->imm : 0x03f06000; /* Default GE condition */ + w[3] = insn->control ? insn->control : 0x004fc400; + break; + + case BW_IR_FSEL: + /* FSEL Rd, Ra, Rb, P0 */ + w[0] = 0x7208U | ((uint32_t)(dst & 0xff) << 16) | ((uint32_t)(src1 & 0xff) << 24); + w[1] = (uint32_t)(src2 & 0xff); + w[2] = (uint32_t)(src3 & 0x7); + w[3] = insn->control ? insn->control : 0x000fca00; + break; + + case BW_IR_FMNMX_MIN: + /* FMNMX Rd, Ra, Rb, PT (minimum) */ + w[0] = 0x7209U | ((uint32_t)(dst & 0xff) << 16) | ((uint32_t)(src1 & 0xff) << 24); + w[1] = (uint32_t)(src2 & 0xff); + w[2] = 0x03800000; + w[3] = insn->control ? insn->control : 0x004fc400; + break; + + case BW_IR_FMNMX_MAX: + /* FMNMX Rd, Ra, Rb, !PT (maximum) */ + w[0] = 0x7209U | ((uint32_t)(dst & 0xff) << 16) | ((uint32_t)(src1 & 0xff) << 24); + w[1] = (uint32_t)(src2 & 0xff); + w[2] = 0x07800000; + w[3] = insn->control ? insn->control : 0x004fc400; + break; + + case BW_IR_I2FP: + /* I2FP.F32.S32 Rd, Ra */ + w[0] = 0x7245U | ((uint32_t)(dst & 0xff) << 16); + w[1] = (uint32_t)(src1 & 0xff); + w[2] = 0x00201400; + w[3] = insn->control ? insn->control : 0x004fe200; + break; + + case BW_IR_F2I: + /* F2I.TRUNC.NTZ Rd, Ra */ + w[0] = 0x7305U | ((uint32_t)(dst & 0xff) << 16); + w[1] = (uint32_t)(src1 & 0xff); + w[2] = 0x0020f100; + w[3] = insn->control ? insn->control : 0x004e2200; + break; + + case BW_IR_MUFU_RCP: + /* MUFU.RCP Rd, Ra */ + w[0] = 0x7308U | ((uint32_t)(dst & 0xff) << 16); + w[1] = (uint32_t)(src1 & 0xff); + w[2] = 0x00001000; + w[3] = insn->control ? insn->control : 0x000e2400; + break; + + case BW_IR_MUFU_RSQ: + /* MUFU.RSQ Rd, Ra */ + w[0] = 0x7308U | ((uint32_t)(dst & 0xff) << 16); + w[1] = (uint32_t)(src1 & 0xff); + w[2] = 0x00001400; + w[3] = insn->control ? insn->control : 0x000e2400; + break; + + case BW_IR_SHFL_DOWN: + /* SHFL.DOWN PT, Rd, Ra, offset, 0x1f */ + w[0] = 0x7f89U | ((uint32_t)(dst & 0xff) << 16) | ((uint32_t)(src1 & 0xff) << 24); + w[1] = 0x08001f00U | (((uint32_t)insn->imm & 0x1f) << 21); + w[2] = 0x000e0000; + w[3] = insn->control ? insn->control : 0x000e2400; + break; + + case BW_IR_LDS: + /* LDS Rd, [Ra] */ + w[0] = 0x7984U | ((uint32_t)(dst & 0xff) << 16) | ((uint32_t)(src1 & 0xff) << 24); + w[1] = 0x00000000; + w[2] = (uint32_t)(insn->imm & 0xffff); + w[3] = insn->control ? insn->control : 0x000fe200; + break; + + case BW_IR_STS: + /* STS [Ra], Rb */ + w[0] = 0x7388U | ((uint32_t)(src1 & 0xff) << 24); + w[1] = (uint32_t)(src2 & 0xff); + w[2] = (uint32_t)(insn->imm & 0xffff); + w[3] = insn->control ? insn->control : 0x000fe200; + break; + + case BW_IR_EXIT: + w[0] = insn->predicate_p0 ? 0x0000094d : 0x0000794d; + w[1] = 0x00000000; + w[2] = 0x03800000; + w[3] = 0x000fea00; + break; + + case BW_IR_BRA: { + /* sm_121 BRA. Distance is (target - this) - 1, split across the + * low six bits at instruction bits 18..23 and the rest at bit 34. + * Predicate bits 12..15: 7 = always, 0 = @P0, 8 = @!P0. + * A zero delta with no predicate keeps the self-branch word the + * existing kernels already emit after EXIT. */ + int32_t delta = (int32_t)insn->imm; + int32_t rel = delta - 1; + uint32_t pred_field = 0x7u; + if (insn->predicate_p0) + pred_field = insn->predicate_not ? 0x8u : 0x0u; + w[0] = 0x947u | (pred_field << 12) | (((uint32_t)rel & 0x3fu) << 18); + w[1] = (uint32_t)(rel >> 6) << 2; + w[2] = (rel < 0) ? 0x0383ffffu : 0x03800000u; + if (insn->control) + w[3] = insn->control; + else if (delta == 0 && !insn->predicate_p0) + w[3] = 0x000fc000u; + else + w[3] = 0x000fea00u; + break; + } + + case BW_IR_BSSY: { + /* BSSY.RECONVERGENT Bn, target (omega #308). nvcc 13.0.88 -arch=sm_121: + * [0070] BSSY.RECONVERGENT B0, 0x140 ; 0x000000c000007945 0x000fe20003800200 + * opcode 0x945 (NAK sm70_encode.rs:4165), barrier id at bits 16..19 (set_bar_dst + * 16..20), guard predicate at bits 12..15 like BRA, relative byte offset + * target - (this + 16) at bit 32 (NAK: field 34.. in 4-byte units), the cond + * predicate PT at bits 87..89 (0x0380 << 16 in w[2]) and bit 73 (0x200 in w[2]), + * which the disassembler prints as .RECONVERGENT; nvcc sets it on every BSSY/BSYNC + * for sm_121 and Mesa NAK does not. We follow nvcc. The target is the instruction + * after the matching BSYNC, so the delta is at least 2; smaller is refused. */ + int32_t delta = (int32_t)insn->imm; + if (delta < 2 || insn->bar_reg >= BW_RECONV_MAX_BAR) return -1; + uint32_t pred_field = 0x7u; + if (insn->predicate_p0) + pred_field = insn->predicate_not ? 0x8u : 0x0u; + w[0] = 0x945u | (pred_field << 12) | ((uint32_t)insn->bar_reg << 16); + w[1] = (uint32_t)(delta - 1) * 16u; + w[2] = 0x03800200u; + w[3] = insn->control ? insn->control : 0x000fe200u; + break; + } + + case BW_IR_BSYNC: { + /* BSYNC.RECONVERGENT Bn: [0130] BSYNC.RECONVERGENT B0 ; 0x0000000000007941 0x000fea0003800200 + * opcode 0x941 (NAK sm70_encode.rs:4179), barrier id bits 16..19, cond PT, bit 73. */ + if (insn->bar_reg >= BW_RECONV_MAX_BAR) return -1; + uint32_t pred_field = 0x7u; + if (insn->predicate_p0) + pred_field = insn->predicate_not ? 0x8u : 0x0u; + w[0] = 0x941u | (pred_field << 12) | ((uint32_t)insn->bar_reg << 16); + w[1] = 0; + w[2] = 0x03800200u; + w[3] = insn->control ? insn->control : 0x000fea00u; + break; + } + + case BW_IR_MUFU_EX2: + /* MUFU.EX2 Rd, Ra. nvdisasm 13.0 -b SM121: 0x7308 with sub-op 0x0800 decodes + * "MUFU.EX2 R2, R4" (RCP is 0x1000, RSQ 0x1400 above). */ + w[0] = 0x7308U | ((uint32_t)(dst & 0xff) << 16); + w[1] = (uint32_t)(src1 & 0xff); + w[2] = 0x00000800; + w[3] = insn->control ? insn->control : 0x000e2400; + break; + + case BW_IR_BAR_SYNC: + /* BAR.SYNC.DEFER_BLOCKING 0x0: the exact words src/omega_numeric.c:885 runs on + * the chip (nvdisasm: "BAR.SYNC.DEFER_BLOCKING 0x0"). No registers. */ + w[0] = 0x00007b1dU; + w[1] = 0x00000000; + w[2] = 0x00010000; + w[3] = insn->control ? insn->control : 0x000fec00; + break; + + case BW_IR_LDS32: + /* LDS Rd, [Ra+URZ] (32-bit), the src/omega_numeric.c:886 form. */ + w[0] = 0x7984U | ((uint32_t)(dst & 0xff) << 16) | ((uint32_t)(src1 & 0xff) << 24); + w[1] = 0x000000ff; + w[2] = 0x08000800; + w[3] = insn->control ? insn->control : 0x000e2800; + break; + + case BW_IR_STS32: + /* STS [Ra+URZ], Rb (32-bit), the src/omega_numeric.c:884 form. */ + w[0] = 0x7988U | ((uint32_t)(src1 & 0xff) << 24); + w[1] = (uint32_t)(src2 & 0xff); + w[2] = 0x080008ffU; + w[3] = insn->control ? insn->control : 0x000fe200; + break; + + /* Prime race cut (2026-10-05). An absent operand is RZ for these three ops. */ + case BW_IR_LOP3_LUT: { + /* LOP3.LUT Rd, Ra, Rb, Rc, imm8, !PT. LOP3_XOR above is this form with Rc = RZ, LUT 0x3c. */ + uint32_t a = (insn->src1_vreg >= 0) ? (uint32_t)(src1 & 0xff) : 0xffU; + uint32_t b = (insn->src2_vreg >= 0) ? (uint32_t)(src2 & 0xff) : 0xffU; + w[0] = 0x7212U | ((uint32_t)(dst & 0xff) << 16) | (a << 24); + w[1] = b; + w[2] = 0x078e0000U | ((insn->imm & 0xffU) << 8) | (uint32_t)(src3 & 0xff); + w[3] = insn->control ? insn->control : 0x001fca00; + break; + } + + case BW_IR_SHF_L_U32: { + /* SHF.L.U32 Rd, Ra, Rb, Rc: register-amount form (0x7219) of SHF_R's 0x7819, with the + * .R (w[2] bit 12) and .HI (bit 16) flags clear. Rc is the funnel high word (RZ). */ + uint32_t a = (insn->src1_vreg >= 0) ? (uint32_t)(src1 & 0xff) : 0xffU; + uint32_t b = (insn->src2_vreg >= 0) ? (uint32_t)(src2 & 0xff) : 0xffU; + w[0] = 0x7219U | ((uint32_t)(dst & 0xff) << 16) | (a << 24); + w[1] = b; + w[2] = 0x00000600U | (uint32_t)(src3 & 0xff); + w[3] = insn->control ? insn->control : 0x001fca00; + break; + } + + case BW_IR_IMAD_HI_U32: { + /* IMAD.HI.U32 Rd, Ra, Rb, Rc: opcode 0x227 with the IMAD.WIDE.U32 modifier word. */ + uint32_t a = (insn->src1_vreg >= 0) ? (uint32_t)(src1 & 0xff) : 0xffU; + uint32_t b = (insn->src2_vreg >= 0) ? (uint32_t)(src2 & 0xff) : 0xffU; + w[0] = 0x7227U | ((uint32_t)(dst & 0xff) << 16) | (a << 24); + w[1] = b; + w[2] = 0x078e0000U | (uint32_t)(src3 & 0xff); + w[3] = insn->control ? insn->control : 0x001fca00; + break; + } + + default: + return -1; + } + return 0; +} + +/* Prime race cut (2026-10-05): golden words for LOP3_LUT, SHF_L_U32 and IMAD_HI_U32. + * Each expected (w0, w1, w2) is the low 96 bits of an instruction nvcc 13.0.88 emitted + * for sm_121 (nvcc -arch=sm_121 -cubin, cuobjdump -sass; source and dump in + * bench/prime_race/tests/encoding_oracle.md). Control words (w3) are scheduler output and + * not compared. nvcc is an offline oracle only; nothing here links CUDA. 0 = all good. */ +int omega_blackwell_verify_codegen_fixtures_intops(void) { + OmegaRegAlloc ra; + memset(&ra, 0, sizeof(ra)); + static const int phys[] = { 0, 2, 6, 7, 9, 11, 13 }; + for (int i = 0; i < (int)(sizeof phys / sizeof phys[0]); i++) ra.vreg_to_phys[i] = phys[i]; + enum { R0 = 0, R2, R6, R7, R9, R11, R13, RZ = -1 }; + static const struct { + BlackwellIROpcode op; int d, a, b, c; uint32_t imm; uint32_t w0, w1, w2; const char *text; + } fx[] = { + { BW_IR_LOP3_LUT, R9, R7, R0, RZ, 0xfc, 0x07097212U, 0x00000000U, 0x078efcffU, "LOP3.LUT R9, R7, R0, RZ, 0xfc, !PT" }, + { BW_IR_LOP3_LUT, R11, R6, R7, R0, 0x78, 0x060b7212U, 0x00000007U, 0x078e7800U, "LOP3.LUT R11, R6, R7, R0, 0x78, !PT" }, + { BW_IR_LOP3_LUT, R13, R6, R7, R0, 0x10, 0x060d7212U, 0x00000007U, 0x078e1000U, "LOP3.LUT R13, R6, R7, R0, 0x10, !PT" }, + { BW_IR_LOP3_LUT, R7, RZ, R9, RZ, 0x33, 0xff077212U, 0x00000009U, 0x078e33ffU, "LOP3.LUT R7, RZ, R9, RZ, 0x33, !PT" }, + { BW_IR_SHF_L_U32, R7, R9, R2, RZ, 0, 0x09077219U, 0x00000002U, 0x000006ffU, "SHF.L.U32 R7, R9, R2, RZ" }, + { BW_IR_SHF_L_U32, R7, R0, R7, RZ, 0, 0x00077219U, 0x00000007U, 0x000006ffU, "SHF.L.U32 R7, R0, R7, RZ" }, + { BW_IR_IMAD_HI_U32, R9, R9, R0, R6, 0, 0x09097227U, 0x00000000U, 0x078e0006U, "IMAD.HI.U32 R9, R9, R0, R6" }, + { BW_IR_IMAD_HI_U32, R7, R0, R7, RZ, 0, 0x00077227U, 0x00000007U, 0x078e00ffU, "IMAD.HI.U32 R7, R0, R7, RZ" }, + }; + for (int i = 0; i < (int)(sizeof fx / sizeof fx[0]); i++) { + BlackwellIRInsn in; + memset(&in, 0, sizeof in); + in.op = fx[i].op; in.dst_vreg = fx[i].d; in.src1_vreg = fx[i].a; in.src2_vreg = fx[i].b; + in.src3_vreg = fx[i].c; in.ureg = -1; in.imm = fx[i].imm; + uint32_t w[4]; + if (encode_single_insn(&in, &ra, w) != 0) return -(2 * i + 1); + if (w[0] != fx[i].w0 || w[1] != fx[i].w1 || w[2] != fx[i].w2) return -(2 * i + 2); + } + return 0; +} + +int omega_blackwell_encode_one(const BlackwellIRInsn *insn, uint32_t w[4]) { + if (!insn || !w) return -1; + OmegaRegAlloc ra; + memset(&ra, 0, sizeof ra); + for (int i = 0; i < BW_MAX_VREGS; i++) ra.vreg_to_phys[i] = i; + for (int i = 0; i < BW_MAX_UVREGS; i++) ra.uvreg_to_phys[i] = i; + return encode_single_insn(insn, &ra, w); +} + +/* omega #308: BSSY / BSYNC golden words. Oracle: nvcc 13.0.88 -arch=sm_121 -cubin -O3 on the + * kernels in docs/gpu-reconvergence-308.md, cuobjdump -sass (low 64-bit word, then high; the + * top 23 bits of the high word are the control word and are not compared here): + * k_loop [0070] BSSY.RECONVERGENT B0, 0x140 ; 0x000000c000007945 0x000fe20003800200 (delta 13) + * k_nested [0070] BSSY.RECONVERGENT B0, 0x300 ; 0x0000028000007945 0x000fe20003800200 (delta 41) + * k_break [0070] BSSY.RECONVERGENT B0, 0x110 ; 0x0000009000007945 0x000fe20003800200 (delta 10) + * k_loop [0130] BSYNC.RECONVERGENT B0 ; 0x0000000000007941 0x000fea0003800200 + * Barrier ids other than B0 and the predicated forms are decoded by tools/reconv_nvdisasm_check.sh + * (nvdisasm -b SM121 text), not by this table. */ +int omega_blackwell_verify_codegen_fixtures_reconv(void) { + static const struct { BlackwellIROpcode op; int delta; int bar; uint32_t w0, w1, w2; } fx[] = { + { BW_IR_BSSY, 13, 0, 0x00007945U, 0x000000c0U, 0x03800200U }, + { BW_IR_BSSY, 41, 0, 0x00007945U, 0x00000280U, 0x03800200U }, + { BW_IR_BSSY, 10, 0, 0x00007945U, 0x00000090U, 0x03800200U }, + { BW_IR_BSYNC, 0, 0, 0x00007941U, 0x00000000U, 0x03800200U }, + }; + for (int i = 0; i < (int)(sizeof fx / sizeof fx[0]); i++) { + BlackwellIRInsn in; + memset(&in, 0, sizeof in); + in.op = fx[i].op; in.dst_vreg = -1; in.src1_vreg = -1; in.src2_vreg = -1; in.src3_vreg = -1; in.ureg = -1; + in.imm = (uint32_t)fx[i].delta; in.bar_reg = (uint8_t)fx[i].bar; + uint32_t w[4]; + if (omega_blackwell_encode_one(&in, w) != 0) return -(2 * i + 1); + if (w[0] != fx[i].w0 || w[1] != fx[i].w1 || w[2] != fx[i].w2) return -(2 * i + 2); + /* control word defaults mirror nvcc's: BSSY 0x000fe200, BSYNC 0x000fea00 */ + if (w[3] != (fx[i].op == BW_IR_BSSY ? 0x000fe200U : 0x000fea00U)) return -(2 * i + 2); + } + /* refusals: a BSSY must name a later instruction (delta >= 2), barrier ids stop at B15 */ + BlackwellIRInsn bad; uint32_t w[4]; + memset(&bad, 0, sizeof bad); bad.op = BW_IR_BSSY; bad.dst_vreg = bad.src1_vreg = bad.src2_vreg = bad.src3_vreg = bad.ureg = -1; + bad.imm = 1; bad.bar_reg = 0; + if (omega_blackwell_encode_one(&bad, w) == 0) return -101; + bad.imm = (uint32_t)-3; + if (omega_blackwell_encode_one(&bad, w) == 0) return -102; + bad.imm = 5; bad.bar_reg = 16; + if (omega_blackwell_encode_one(&bad, w) == 0) return -103; + bad.op = BW_IR_BSYNC; bad.imm = 0; bad.bar_reg = 16; + if (omega_blackwell_encode_one(&bad, w) == 0) return -104; + bad.bar_reg = 15; + if (omega_blackwell_encode_one(&bad, w) != 0 || w[0] != (0x00007941U | (15U << 16))) return -105; + return 0; +} + +/* FB-1 cut 4: word-level fixtures for the four additive ops (checked against the + * nvdisasm decodes recorded in docs/numeric/FB1_CUT4_ELEMENTWISE.md). 0 = all good. */ +int omega_blackwell_verify_codegen_fixtures_fb1cut4(void) { + OmegaRegAlloc ra; + memset(&ra, 0, sizeof(ra)); + ra.vreg_to_phys[0] = 2; /* R2 */ + ra.vreg_to_phys[1] = 4; /* R4 */ + ra.vreg_to_phys[2] = 9; /* R9 */ + ra.vreg_to_phys[3] = 10; /* R10 */ + ra.vreg_to_phys[4] = 8; /* R8 */ + uint32_t w[4]; + + /* MUFU.EX2 R2, R4 */ + BlackwellIRInsn ex2 = { .op = BW_IR_MUFU_EX2, .dst_vreg = 0, .src1_vreg = 1 }; + if (encode_single_insn(&ex2, &ra, w) != 0) return -1; + if (w[0] != 0x00027308U || w[1] != 4 || w[2] != 0x00000800U) return -2; + /* BAR.SYNC.DEFER_BLOCKING 0x0 */ + BlackwellIRInsn bar = { .op = BW_IR_BAR_SYNC }; + if (encode_single_insn(&bar, &ra, w) != 0) return -3; + if (w[0] != 0x00007b1dU || w[1] != 0 || w[2] != 0x00010000U || w[3] != 0x000fec00U) return -4; + /* LDS R9, [R10+URZ] */ + BlackwellIRInsn lds = { .op = BW_IR_LDS32, .dst_vreg = 2, .src1_vreg = 3 }; + if (encode_single_insn(&lds, &ra, w) != 0) return -5; + if (w[0] != 0x0a097984U || w[1] != 0xff || w[2] != 0x08000800U) return -6; + /* STS [R8+URZ], R2 */ + BlackwellIRInsn sts = { .op = BW_IR_STS32, .src1_vreg = 4, .src2_vreg = 0 }; + if (encode_single_insn(&sts, &ra, w) != 0) return -7; + if (w[0] != 0x08007988U || w[1] != 2 || w[2] != 0x080008ffU) return -8; + return 0; +} + +int omega_bw_encode_program(const BlackwellIRProgram *prog, uint8_t *code_buf, size_t max_len, size_t *out_len) { + if (!prog || !code_buf || !out_len) return -1; + + size_t padded_insns = (prog->count + 7) & ~7ULL; + if (padded_insns < 32) padded_insns = 32; + + size_t req_bytes = padded_insns * 16; + if (max_len < req_bytes) return -1; + + uint8_t *p = code_buf; + for (size_t i = 0; i < padded_insns; i++) { + uint32_t w[4]; + if (i < prog->count) { + if (encode_single_insn(&prog->insns[i], &prog->regalloc, w) != 0) { + return -1; + } + } else { + /* Pad with NOP */ + w[0] = 0x00007918; + w[1] = 0x00000000; + w[2] = 0x00000000; + w[3] = 0x000fc000; + } + + p[0] = (uint8_t)(w[0] & 0xff); + p[1] = (uint8_t)((w[0] >> 8) & 0xff); + p[2] = (uint8_t)((w[0] >> 16) & 0xff); + p[3] = (uint8_t)((w[0] >> 24) & 0xff); + + p[4] = (uint8_t)(w[1] & 0xff); + p[5] = (uint8_t)((w[1] >> 8) & 0xff); + p[6] = (uint8_t)((w[1] >> 16) & 0xff); + p[7] = (uint8_t)((w[1] >> 24) & 0xff); + + p[8] = (uint8_t)(w[2] & 0xff); + p[9] = (uint8_t)((w[2] >> 8) & 0xff); + p[10] = (uint8_t)((w[2] >> 16) & 0xff); + p[11] = (uint8_t)((w[2] >> 24) & 0xff); + + p[12] = (uint8_t)(w[3] & 0xff); + p[13] = (uint8_t)((w[3] >> 8) & 0xff); + p[14] = (uint8_t)((w[3] >> 16) & 0xff); + p[15] = (uint8_t)((w[3] >> 24) & 0xff); + + p += 16; + } + + *out_len = req_bytes; + return 0; +} + +int omega_blackwell_codegen_matmul_i32(const OmegaMatMulSpec *spec, OmegaBlackwellKernel *kernel) { + if (!spec || !kernel) return -1; + if (spec->m == 0 || spec->k == 0 || spec->n == 0) return -1; + + /* Fail closed loudly if K exceeds bounded unroll limit */ + if (spec->k > 32) { + fprintf(stderr, "ERROR: MatMul integer codegen exceeds bounded unroll limit (K=%u > 32)\n", spec->k); + return -1; + } + + BlackwellIRProgram prog; + omega_bw_ir_init(&prog); + + /* 1. Allocate virtual registers */ + /* 64-bit base pointers */ + int v_ptr_a = omega_bw_ir_alloc_vreg64(&prog); + int v_ptr_b = omega_bw_ir_alloc_vreg64(&prog); + int v_ptr_c = omega_bw_ir_alloc_vreg64(&prog); + int uv_desc = omega_bw_ir_alloc_uvreg64(&prog); + + /* 32-bit CTA / Thread indexing */ + int v_ctaid_x = omega_bw_ir_alloc_vreg(&prog); + int v_ctaid_y = omega_bw_ir_alloc_vreg(&prog); + int v_tid_x = omega_bw_ir_alloc_vreg(&prog); + int v_tid_y = omega_bw_ir_alloc_vreg(&prog); + + /* 2D Coordinates & loop invariants */ + int v_col = omega_bw_ir_alloc_vreg(&prog); + int v_row = omega_bw_ir_alloc_vreg(&prog); + int v_row_k = omega_bw_ir_alloc_vreg(&prog); + int v_const_1 = omega_bw_ir_alloc_vreg(&prog); + int v_acc = omega_bw_ir_alloc_vreg(&prog); + + /* Working registers for memory address and operands */ + int v_idx_a = omega_bw_ir_alloc_vreg(&prog); + int v_off_a = omega_bw_ir_alloc_vreg64(&prog); + int v_val_a = omega_bw_ir_alloc_vreg(&prog); + + int v_idx_b = omega_bw_ir_alloc_vreg(&prog); + int v_off_b = omega_bw_ir_alloc_vreg64(&prog); + int v_val_b = omega_bw_ir_alloc_vreg(&prog); + + int v_idx_c = omega_bw_ir_alloc_vreg(&prog); + int v_off_c = omega_bw_ir_alloc_vreg64(&prog); + + if (v_off_c < 0) return -1; + + /* 2. Load 64-bit global memory descriptor into uniform register pair */ + /* LDCU.64 uv_desc, c[0x0][0x358] */ + BlackwellIRInsn insn_desc = { .op = BW_IR_LDCU64, .dst_vreg = uv_desc, .imm = 0x358, .is_uniform = true }; + omega_bw_ir_append(&prog, &insn_desc); + + /* 3. Load 64-bit base pointers from Constant Bank 0 */ + /* LDC.64 v_ptr_a, c[0x0][0x380] */ + BlackwellIRInsn insn_ptra = { .op = BW_IR_LDC64, .dst_vreg = v_ptr_a, .imm = 0x380 }; + omega_bw_ir_append(&prog, &insn_ptra); + + /* LDC.64 v_ptr_b, c[0x0][0x388] */ + BlackwellIRInsn insn_ptrb = { .op = BW_IR_LDC64, .dst_vreg = v_ptr_b, .imm = 0x388 }; + omega_bw_ir_append(&prog, &insn_ptrb); + + /* LDC.64 v_ptr_c, c[0x0][0x390] */ + BlackwellIRInsn insn_ptrc = { .op = BW_IR_LDC64, .dst_vreg = v_ptr_c, .imm = 0x390 }; + omega_bw_ir_append(&prog, &insn_ptrc); + + /* 4. Query 2D CTA and Thread coordinates */ + /* S2R v_ctaid_x, SR_CTAID.X */ + BlackwellIRInsn insn_ctax = { .op = BW_IR_S2R, .dst_vreg = v_ctaid_x, .imm = BW_SR_CTAID_X }; + omega_bw_ir_append(&prog, &insn_ctax); + + /* S2R v_ctaid_y, SR_CTAID.Y */ + BlackwellIRInsn insn_ctay = { .op = BW_IR_S2R, .dst_vreg = v_ctaid_y, .imm = BW_SR_CTAID_Y }; + omega_bw_ir_append(&prog, &insn_ctay); + + /* S2R v_tid_x, SR_TID.X */ + BlackwellIRInsn insn_tidx = { .op = BW_IR_S2R, .dst_vreg = v_tid_x, .imm = BW_SR_TID_X }; + omega_bw_ir_append(&prog, &insn_tidx); + + /* S2R v_tid_y, SR_TID.Y */ + BlackwellIRInsn insn_tidy = { .op = BW_IR_S2R, .dst_vreg = v_tid_y, .imm = BW_SR_TID_Y }; + omega_bw_ir_append(&prog, &insn_tidy); + + /* 5. Compute 2D (row, col) coordinates */ + /* Standard 16x16 tile threads: col = ctaid_x * 16 + tid_x */ + BlackwellIRInsn insn_col = { .op = BW_IR_IMAD, .dst_vreg = v_col, .src1_vreg = v_ctaid_x, .src2_vreg = -1, .imm = 16, .src3_vreg = v_tid_x }; + omega_bw_ir_append(&prog, &insn_col); + + /* row = ctaid_y * 16 + tid_y */ + BlackwellIRInsn insn_row = { .op = BW_IR_IMAD, .dst_vreg = v_row, .src1_vreg = v_ctaid_y, .src2_vreg = -1, .imm = 16, .src3_vreg = v_tid_y }; + omega_bw_ir_append(&prog, &insn_row); + + /* 6. Precompute row base index: v_row_k = v_row * K + RZ */ + BlackwellIRInsn insn_row_k = { .op = BW_IR_IMAD, .dst_vreg = v_row_k, .src1_vreg = v_row, .src2_vreg = -1, .imm = spec->k, .src3_vreg = -1 }; + omega_bw_ir_append(&prog, &insn_row_k); + + /* 7. Initialize v_const_1 = 1 for immediate linear indexing */ + BlackwellIRInsn insn_c1 = { .op = BW_IR_MOV_IMM, .dst_vreg = v_const_1, .imm = 1 }; + omega_bw_ir_append(&prog, &insn_c1); + + /* 8. Initialize accumulator to zero: MOV v_acc, RZ */ + BlackwellIRInsn insn_init_acc = { .op = BW_IR_MOV_RZ, .dst_vreg = v_acc }; + omega_bw_ir_append(&prog, &insn_init_acc); + + /* 9. Synthesize inner accumulation loop across K iterations */ + for (uint32_t step = 0; step < spec->k; step++) { + /* A[row, step]: idx_a = v_row_k + step = v_const_1 * step + v_row_k */ + BlackwellIRInsn insn_idxa = { .op = BW_IR_IMAD, .dst_vreg = v_idx_a, .src1_vreg = v_const_1, .src2_vreg = -1, .imm = step, .src3_vreg = v_row_k }; + omega_bw_ir_append(&prog, &insn_idxa); + + /* off_a = v_ptr_a + idx_a * 4 */ + BlackwellIRInsn insn_offa = { .op = BW_IR_IMAD_WIDE, .dst_vreg = v_off_a, .src1_vreg = v_idx_a, .src2_vreg = -1, .imm = 4, .src3_vreg = v_ptr_a }; + omega_bw_ir_append(&prog, &insn_offa); + + /* Load A[row, step]: LDG.E v_val_a, desc[uv_desc][v_off_a.64] */ + BlackwellIRInsn insn_lda = { .op = BW_IR_LDG_E, .dst_vreg = v_val_a, .src1_vreg = v_off_a, .src2_vreg = -1, .src3_vreg = -1, .ureg = uv_desc, .control = 0x001ea800 }; + omega_bw_ir_append(&prog, &insn_lda); + + /* B[step, col]: idx_b = step * N + col = v_const_1 * (step * N) + v_col */ + uint32_t step_n = step * spec->n; + BlackwellIRInsn insn_idxb = { .op = BW_IR_IMAD, .dst_vreg = v_idx_b, .src1_vreg = v_const_1, .src2_vreg = -1, .imm = step_n, .src3_vreg = v_col }; + omega_bw_ir_append(&prog, &insn_idxb); + + /* off_b = v_ptr_b + idx_b * 4 */ + BlackwellIRInsn insn_offb = { .op = BW_IR_IMAD_WIDE, .dst_vreg = v_off_b, .src1_vreg = v_idx_b, .src2_vreg = -1, .imm = 4, .src3_vreg = v_ptr_b }; + omega_bw_ir_append(&prog, &insn_offb); + + /* Load B[step, col]: LDG.E v_val_b, desc[uv_desc][v_off_b.64] */ + BlackwellIRInsn insn_ldb = { .op = BW_IR_LDG_E, .dst_vreg = v_val_b, .src1_vreg = v_off_b, .src2_vreg = -1, .src3_vreg = -1, .ureg = uv_desc, .control = 0x002ea800 }; + omega_bw_ir_append(&prog, &insn_ldb); + + /* Multiply and accumulate: IMAD v_acc, v_val_a, v_val_b, v_acc */ + BlackwellIRInsn insn_acc = { .op = BW_IR_IMAD, .dst_vreg = v_acc, .src1_vreg = v_val_a, .src2_vreg = v_val_b, .src3_vreg = v_acc, .control = 0x004fca00 }; + omega_bw_ir_append(&prog, &insn_acc); + } + + /* 10. Compute output index: idx_c = row * N + col */ + BlackwellIRInsn insn_idxc = { .op = BW_IR_IMAD, .dst_vreg = v_idx_c, .src1_vreg = v_row, .src2_vreg = -1, .imm = spec->n, .src3_vreg = v_col }; + omega_bw_ir_append(&prog, &insn_idxc); + + /* off_c = v_ptr_c + idx_c * 4 */ + BlackwellIRInsn insn_offc = { .op = BW_IR_IMAD_WIDE, .dst_vreg = v_off_c, .src1_vreg = v_idx_c, .src2_vreg = -1, .imm = 4, .src3_vreg = v_ptr_c }; + omega_bw_ir_append(&prog, &insn_offc); + + /* Store C[row, col]: STG.E desc[uv_desc][v_off_c.64], v_acc */ + BlackwellIRInsn insn_stc = { .op = BW_IR_STG_E, .src1_vreg = v_off_c, .src2_vreg = v_acc, .src3_vreg = -1, .ureg = uv_desc }; + omega_bw_ir_append(&prog, &insn_stc); + + /* 11. Kernel termination */ + BlackwellIRInsn insn_exit = { .op = BW_IR_EXIT, .predicate_p0 = false }; + omega_bw_ir_append(&prog, &insn_exit); + + BlackwellIRInsn insn_bra = { .op = BW_IR_BRA }; + omega_bw_ir_append(&prog, &insn_bra); + + /* 12. Solve bounded deterministic register allocation */ + if (omega_bw_regalloc_solve(&prog) != 0) { + return -1; + } + + /* 13. Allocate code buffer and encode instructions */ + size_t padded_insns = (prog.count + 7) & ~7ULL; + if (padded_insns < 32) padded_insns = 32; + size_t alloc_size = padded_insns * 16; + + kernel->code = (uint8_t *)malloc(alloc_size); + if (!kernel->code) return -1; + + size_t emitted_size = 0; + if (omega_bw_encode_program(&prog, kernel->code, alloc_size, &emitted_size) != 0) { + free(kernel->code); + kernel->code = NULL; + return -1; + } + + kernel->code_size = emitted_size; + kernel->insn_count = emitted_size / 16; + kernel->gpr_count = prog.regalloc.peak_gpr_usage; + kernel->uniform_gpr_count = prog.regalloc.peak_ugpr_usage; + + /* 14. Compute runtime SHA-256 digest of emitted machine code */ + sha256_hash(kernel->code, kernel->code_size, kernel->code_digest); + return 0; +} + +int omega_blackwell_codegen_matmul_tensor_prog(const OmegaMatMulSpec *spec, BlackwellIRProgram *prog) { + if (!spec || !prog) return -1; + if (spec->m == 0 || spec->k == 0 || spec->n == 0) return -1; + if (spec->precision != OMEGA_MATMUL_PRECISION_FP16 && spec->precision != OMEGA_MATMUL_PRECISION_BF16) return -1; + if (spec->m % 16 != 0 || spec->n % 8 != 0 || spec->k != 16) { + fprintf(stderr, "ERROR: MatMul tensor codegen requires M multiple of 16, N multiple of 8, K=16 (got %ux%ux%u)\n", + spec->m, spec->k, spec->n); + return -1; + } + + omega_bw_ir_init(prog); + + /* 1. Allocate virtual registers */ + /* 64-bit base pointers */ + int v_ptr_a = omega_bw_ir_alloc_vreg64(prog); + int v_ptr_b = omega_bw_ir_alloc_vreg64(prog); + int v_ptr_c = omega_bw_ir_alloc_vreg64(prog); + int uv_desc = omega_bw_ir_alloc_uvreg64(prog); + + /* 32-bit CTA / Thread indexing */ + int v_tid = omega_bw_ir_alloc_vreg(prog); ``` The diff is longer than this page shows; the raw diff has all of it.