From 8aaee61f1264a10b54f04a7edcf244b2b294bcb7 Mon Sep 17 00:00:00 2001 From: Drake Stapleton Date: Fri, 30 Oct 2026 09:44:28 -0500 Subject: [PATCH] warp simulators: model IMAD.HI.U32 register Rc as the 64-bit pair Rc+1:Rc (refs #492) The four tests/bw_warp_sim*.h simulators and the prime_race per-thread simulator now compute hi32(Ra*Rb + Rc+1:Rc) for an even register Rc (nvcc 13.0 oracle, evidence/IMADHI-RC-ORACLE-nvcc13.0-20261016) and refuse an odd Rc (no evidence). tests/gpu_imadhi_rc_test.c replays the PR #487 odd-column pack: IMAD.HI.U32 with a stale (and with a zero) upper register must not match the emulation, and the SHF.R + IADD3 fix (5b10b824) must. Not chip-probed. --- .crumb | 22 ++-- Makefile | 3 +- bench/prime_race/.crumb | 8 +- bench/prime_race/tests/gb10_sieve_host_test.c | 25 +++- src/.crumb | 8 +- src/omega_blackwell_codegen.h | 13 +- tests/.crumb | 8 +- tests/bw_warp_sim.h | 17 ++- tests/bw_warp_sim_k.h | 17 ++- tests/bw_warp_sim_pa.h | 17 ++- tests/bw_warp_sim_tc.h | 17 ++- tests/gpu_imadhi_rc_test.c | 118 ++++++++++++++---- 12 files changed, 195 insertions(+), 78 deletions(-) diff --git a/.crumb b/.crumb index f3e51fe46..836b4d59b 100644 --- a/.crumb +++ b/.crumb @@ -72,38 +72,38 @@ }, { "digest": "f765ad87fdddd9b47f690b07298c951a0f7e7675dc5de4ffe44e33a0c14b1986", - "last_commit": "a8a6a3d3c", + "last_commit": "62d1d0d66", "name": "spec", "purpose": "Directory spec. Purpose not yet described by a human." }, { - "digest": "aaf5acb82fd4c720cd60c85fd6087f636c72a98e2f3740c9295dcfa525dc64d7", - "last_commit": "bf54b19fa", + "digest": "8d4d4cb520be8b978fbd9d1fecb5689efb657b482e1f262c8d3ac5982233981d", + "last_commit": "089e5b053", "name": "src", "purpose": "Directory src. Purpose not yet described by a human." }, { - "digest": "5395ff8ff9afceebe8e453788f80a3064b831513cc4cb92fd55869115e06b874", - "last_commit": "a8a6a3d3c", + "digest": "7367ec2262d1b19abab01e3b23ae9493f403958a7d89efeae152bc301d092062", + "last_commit": "62d1d0d66", "name": "tests", "purpose": "Directory tests. Purpose not yet described by a human." }, { "digest": "308db0e6cf45c0a48f01f5f5f228450f191389de68006915b9ecf9c65b9a2e29", - "last_commit": "a8a6a3d3c", + "last_commit": "62d1d0d66", "name": "tools", "purpose": "Directory tools. Purpose not yet described by a human." } ], - "children_root": "sha256:07bf1706dcffbad66c0353871ff67a6749055da03cee8d8ebdf9a4c91e36bddc", - "compiled_at": "2026-10-30T12:31:50Z", + "children_root": "sha256:0c52aff7fea3c8fa25227c8dc76d8845289cde90365cb4e8fe161f0ac8f4db14", + "compiled_at": "2026-10-30T14:44:24Z", "compiler": "crumb-compile/1", - "digest": "sha256:904fca8c0e0cf3e3a5e5adb73393e01c7018b920bb542d289904240f70e400d6", + "digest": "sha256:824f0fe91cffa7946f7a8275308cf82a65c888da9d1a83147d4e064bb774b638", "evidence_files": 12622, "evidence_root": "sha256:b1a08e2ce56e6feada0d8a31f06fbd7adee57adc1c1ad8dec220293ee5d439f9", "files": 17849, - "generated_at_commit": "52a82f392fa513ebbc2d9d719a0f14c385289b06", - "source_tree": "sha256:75458da6553a0ee5ea2af95f46afb658e8a3f6e53cca6e3b4b9874035f78c2ed" + "generated_at_commit": "62d1d0d668f897386a0e311374f8dbcc5c32dde8", + "source_tree": "sha256:de0b3e44ee1cc929437f9f50e441fd7f541aacd2d4415ba2b38066696c987280" }, "owner_lane": "", "provenance": { diff --git a/Makefile b/Makefile index 6b53cd124..fc69597c2 100644 --- a/Makefile +++ b/Makefile @@ -2498,7 +2498,8 @@ test-gpu-reconv: $(GPU_RECONV_TEST) # omega #492: IMAD.HI.U32 with a register Rc. The chip reads Rc as a 64-bit register pair (nvcc 13.0 # sm_121 oracle, evidence/IMADHI-RC-ORACLE-nvcc13.0-20261016; chip probe pending), so the four warp -# simulators refuse a non-RZ Rc instead of modelling a 32-bit one. One binary per simulator header. +# simulators model an even Rc as the pair Rc+1:Rc and refuse an odd Rc, and replay the PR #487 +# odd-column pack (it must come out wrong, as on the chip). One binary per simulator header. .PHONY: test-gpu-imadhi-rc GPU_IMADHI_SIMS = bw_warp_sim bw_warp_sim_tc bw_warp_sim_k bw_warp_sim_pa GPU_IMADHI_TESTS = $(GPU_IMADHI_SIMS:%=$(OUT_DIR)/gpu_imadhi_rc_test_%) diff --git a/bench/prime_race/.crumb b/bench/prime_race/.crumb index 128e73ef0..1ae34e220 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-24T23:35:21Z", + "compiled_at": "2026-10-30T14:44:24Z", "compiler": "crumb-compile/1", - "digest": "sha256:240194f8f186535e206c565515c998391eb37d494cca4bf792211935e2f9261a", + "digest": "sha256:1f7a3bb9ef03521b534108bd03543386d07db65c89f230a221f1a2b69543a61a", "evidence_files": 0, "evidence_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", "files": 10, - "generated_at_commit": "c8d7d52946c22a7a8a730897dfff9e1cd9ad069a", - "source_tree": "sha256:771f2a7383539ae41acbcad002e02f199aa8fdfab190f8eae66b0e555d910a23" + "generated_at_commit": "62d1d0d668f897386a0e311374f8dbcc5c32dde8", + "source_tree": "sha256:ef8d3258e507585ea68932927c34be743668225d38a067da6b80023b760da83b" } }, "invariants": [ diff --git a/bench/prime_race/tests/gb10_sieve_host_test.c b/bench/prime_race/tests/gb10_sieve_host_test.c index adac8bec8..6b171571e 100644 --- a/bench/prime_race/tests/gb10_sieve_host_test.c +++ b/bench/prime_race/tests/gb10_sieve_host_test.c @@ -155,7 +155,10 @@ static void sim_thread(const BlackwellIRProgram *p, Thr *tp, uint32_t tid, uint3 case BW_IR_MOV_RZ: wr(&t, in->dst_vreg, 0, p); break; case BW_IR_IMAD: wr(&t, in->dst_vreg, in->src2_vreg >= 0 ? a * b + c : a * in->imm + c, p); break; case BW_IR_IMAD_WIDE: wr64(&t, in->dst_vreg, (uint64_t)a * in->imm + rd64(&t, in->src3_vreg, p), p); break; - case BW_IR_IMAD_HI_U32: wr(&t, in->dst_vreg, (uint32_t)(((uint64_t)a * b) >> 32) + c, p); break; + case BW_IR_IMAD_HI_U32: { /* hi32(a * b + Rc+1:Rc): a register Rc is a 64-bit pair (omega #492, nvcc oracle); odd Rc: err 9 */ + uint64_t c64 = 0; + if (in->src3_vreg >= 0 && p->regalloc.vreg_to_phys[in->src3_vreg] >= 0) { if (p->regalloc.vreg_to_phys[in->src3_vreg] & 1) { g_sim_err = 9; return; } c64 = rd64(&t, in->src3_vreg, p); } + wr(&t, in->dst_vreg, (uint32_t)(((uint64_t)a * b + c64) >> 32), p); break; } case BW_IR_IADD3: wr(&t, in->dst_vreg, a + b, p); break; /* the encoder's IADD3 has Rc = RZ */ case BW_IR_SHF_R: wr(&t, in->dst_vreg, a >> (in->imm & 31), p); break; case BW_IR_SHF_L_U32: if (b >= 32) { g_sim_err = 8; return; } wr(&t, in->dst_vreg, a << b, p); break; @@ -492,6 +495,26 @@ int main(int argc, char **argv) { /* 1. golden words */ int fx = omega_blackwell_verify_codegen_fixtures_intops(); CHECK(fx == 0, "golden words LOP3_LUT / SHF_L_U32 / IMAD_HI_U32 (rc %d)", fx); + /* omega #492: this per-thread simulator reads a register Rc of IMAD.HI.U32 as the pair Rc+1:Rc + * (nvcc oracle evidence/IMADHI-RC-ORACLE-nvcc13.0-20261016) and refuses an odd Rc (err 9). */ + { + static BlackwellIRProgram ip; + memset(&ip, 0, sizeof ip); + ip.insns[0] = (BlackwellIRInsn){ .op = BW_IR_IMAD_HI_U32, .dst_vreg = 3, .src1_vreg = 0, .src2_vreg = 1, .src3_vreg = 2, .ureg = -1 }; + ip.insns[1] = (BlackwellIRInsn){ .op = BW_IR_EXIT, .dst_vreg = -1, .src1_vreg = -1, .src2_vreg = -1, .src3_vreg = -1, .ureg = -1 }; + ip.count = 2; ip.regalloc.num_vregs = 4; + const int ph[4] = { 4, 5, 6, 8 }; + for (int v = 0; v < 4; v++) ip.regalloc.vreg_to_phys[v] = ph[v]; + Thr t; memset(&t, 0, sizeof t); + t.r[4] = 0xdeadbeefu; t.r[5] = 0x10000u; t.r[6] = 0x56780000u; t.r[7] = 0x3f803f80u; + const uint32_t want = (uint32_t)(((uint64_t)0xdeadbeefu * 0x10000u + (((uint64_t)0x3f803f80u << 32) | 0x56780000u)) >> 32); + g_sim_err = 0; sim_thread(&ip, &t, 0, 0); + CHECK(g_sim_err == 0 && t.done && t.r[8] == want, "IMAD.HI.U32 even Rc: err %d out 0x%08x want pair 0x%08x", g_sim_err, t.r[8], want); + ip.regalloc.vreg_to_phys[2] = 7; memset(&t, 0, sizeof t); + g_sim_err = 0; sim_thread(&ip, &t, 0, 0); + CHECK(g_sim_err == 9, "IMAD.HI.U32 odd Rc not refused (err %d)", g_sim_err); + g_sim_err = 0; + } /* The scheduling pass is shared by every elementwise family. This is * codegen only: no public launcher or device session is called. */ diff --git a/src/.crumb b/src/.crumb index 7c4b83da7..31974700d 100644 --- a/src/.crumb +++ b/src/.crumb @@ -188,14 +188,14 @@ } ], "children_root": "sha256:2f729945214be989a74d7c2064ae4ba3c2deb52ebea3f334b459198a7bd032ed", - "compiled_at": "2026-10-30T02:49:13Z", + "compiled_at": "2026-10-30T14:44:24Z", "compiler": "crumb-compile/1", - "digest": "sha256:aaf5acb82fd4c720cd60c85fd6087f636c72a98e2f3740c9295dcfa525dc64d7", + "digest": "sha256:8d4d4cb520be8b978fbd9d1fecb5689efb657b482e1f262c8d3ac5982233981d", "evidence_files": 0, "evidence_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", "files": 568, - "generated_at_commit": "2cb1ccb5d3c1a1ce1cb94a7a3dd3c221395c4e15", - "source_tree": "sha256:7f1fef46a27e8baf5cf567bb590d417ede3767c5f07e714d0f0812af0645bb4b" + "generated_at_commit": "62d1d0d668f897386a0e311374f8dbcc5c32dde8", + "source_tree": "sha256:55ca08b371173cc7a9d3f99f75be6eced72c21d3ce6a219e1d5943180f532df6" }, "owner_lane": "", "provenance": { diff --git a/src/omega_blackwell_codegen.h b/src/omega_blackwell_codegen.h index 1f0b70eec..d1d65f013 100644 --- a/src/omega_blackwell_codegen.h +++ b/src/omega_blackwell_codegen.h @@ -101,12 +101,13 @@ typedef enum { * truth table over Ra = 0xf0, Rb = 0xcc, Rc = 0xaa (a|b = 0xfc, ~(a|b) = 0x03) */ BW_IR_SHF_L_U32, /* SHF.L.U32 Rd, Ra, Rb, RZ: Rd = Ra << Rb, register amount (the form nvcc emits * for PTX shl.b32). Amounts >= 32 are NOT chip-verified: callers keep Rb < 32 */ - BW_IR_IMAD_HI_U32, /* IMAD.HI.U32 Rd, Ra, Rb, Rc: Rd = hi32(Ra * Rb + Rc:Rc+1). Rc is a 64-bit register pair, - * NOT one 32-bit word: nvcc 13.0 zeroes the upper half before using a 32-bit c, puts - * __umulhi(a, b) + c through the pair {0, c}, and emits only even pair bases (R2, R4, UR4 - * in evidence/IMADHI-RC-ORACLE-nvcc13.0-20261016). Odd-Rc chip behaviour is unknown until - * the chip probe (omega #492); until then the warp simulators refuse a non-RZ Rc. - * Callers pass Rc = RZ (src3_vreg = -1) */ + BW_IR_IMAD_HI_U32, /* IMAD.HI.U32 Rd, Ra, Rb, Rc: Rd = hi32(Ra * Rb + Rc+1:Rc), low word Rc. Rc is a 64-bit register + * pair, NOT one 32-bit word: nvcc 13.0 zeroes the upper half before using a 32-bit c, puts + * __umulhi(a, b) + c through the pair {lo 0, hi c}, and emits only even pair bases (R2, R4, UR4 + * in evidence/IMADHI-RC-ORACLE-nvcc13.0-20261016). Compiler reading, not chip-probed (omega #492). + * The warp simulators model an even Rc as that pair and refuse an odd Rc (no evidence). + * hi32(Ra * Rb) + c32 is NOT this instruction with Rc = c32: use RZ then IADD3, or SHF.R + IADD3 + * (the PR #487 odd-column bug, tests/gpu_imadhi_rc_test.c). Rc = RZ: src3_vreg = -1 */ /* Structured warp reconvergence (omega #308, 2026-10-05), additive. Words matched against * nvcc 13.0.88 -arch=sm_121 output (BSSY.RECONVERGENT / BSYNC.RECONVERGENT; the oracle * kernels are listed in docs/gpu-reconvergence-308.md) and the Mesa NAK encoder diff --git a/tests/.crumb b/tests/.crumb index 5fbfcb29c..9cf6b75a9 100644 --- a/tests/.crumb +++ b/tests/.crumb @@ -302,14 +302,14 @@ } ], "children_root": "sha256:0c57282ff8cf1d34e5685e2729755f2978917e66debb5423961033be9d25c902", - "compiled_at": "2026-10-30T12:31:50Z", + "compiled_at": "2026-10-30T14:44:24Z", "compiler": "crumb-compile/1", - "digest": "sha256:5395ff8ff9afceebe8e453788f80a3064b831513cc4cb92fd55869115e06b874", + "digest": "sha256:7367ec2262d1b19abab01e3b23ae9493f403958a7d89efeae152bc301d092062", "evidence_files": 16, "evidence_root": "sha256:96d08097822ad95f4e562c19e0ba5df5a035774c48632f1832912c40a27bd27a", "files": 1386, - "generated_at_commit": "52a82f392fa513ebbc2d9d719a0f14c385289b06", - "source_tree": "sha256:a3b853bb9c0edfc5f1413ccaa2698cc6ea920f2e8795756121667cc9d37f6679" + "generated_at_commit": "62d1d0d668f897386a0e311374f8dbcc5c32dde8", + "source_tree": "sha256:3058c6438b3ac03b2a7d7befdcca39cbef8febf8be5be5ac88d6ef3683c5e6a2" }, "owner_lane": "", "provenance": { diff --git a/tests/bw_warp_sim.h b/tests/bw_warp_sim.h index 6d93828f3..2d02bcbfc 100644 --- a/tests/bw_warp_sim.h +++ b/tests/bw_warp_sim.h @@ -44,8 +44,8 @@ * 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 - * WS_E_IMADHI_RC IMAD.HI.U32 with a register Rc: the chip reads Rc as a 64-bit pair (omega #492, - * nvcc oracle, chip probe pending), so a non-RZ Rc is refused, not modelled + * WS_E_IMADHI_RC IMAD.HI.U32 with an odd register Rc. An even Rc is modelled as the 64-bit pair + * Rc+1:Rc the chip reads (omega #492, nvcc oracle, chip probe pending); odd has no evidence * 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. @@ -159,9 +159,16 @@ static void ws_lane_exec(WsSim *s, WsLane *l, const BlackwellIRInsn *in, int pc, case BW_IR_MOV_RZ: ws_wrs(l, in->dst_vreg, in->dst_subreg, 0, p); break; case BW_IR_IMAD: ws_wrs(l, in->dst_vreg, in->dst_subreg, 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: /* Rd = hi32(Ra * Rb) with Rc = RZ. The chip reads a register Rc as a 64-bit pair (omega #492): refused until a chip probe pins the model */ - if (in->src3_vreg >= 0) { ws_fail(s, WS_E_IMADHI_RC, pc, "IMAD.HI.U32 with a register Rc (the chip reads Rc as a 64-bit pair, not chip-probed): use RZ, or SHF.R + IADD3"); return; } - ws_wrs(l, in->dst_vreg, in->dst_subreg, (uint32_t)(((uint64_t)a * b) >> 32), p); break; + case BW_IR_IMAD_HI_U32: { /* Rd = hi32(Ra * Rb + Rc+1:Rc). A register Rc is a 64-bit pair, low word Rc (omega #492, nvcc 13.0 + * oracle evidence/IMADHI-RC-ORACLE-nvcc13.0-20261016, not chip-probed); an odd Rc has no evidence: refused */ + uint64_t c64 = 0; + int rc = in->src3_vreg >= 0 ? p->regalloc.vreg_to_phys[in->src3_vreg] : -1; + if (rc >= 0) { + rc = (rc + in->src3_subreg) & 0xff; + if (rc & 1) { ws_fail(s, WS_E_IMADHI_RC, pc, "IMAD.HI.U32 with an odd register Rc (the chip reads a 64-bit pair; nvcc emits only even bases, odd not chip-probed): use RZ or an even pair"); return; } + c64 = (uint64_t)l->r[rc] | ((uint64_t)l->r[rc + 1] << 32); + } + ws_wrs(l, in->dst_vreg, in->dst_subreg, (uint32_t)(((uint64_t)a * b + c64) >> 32), p); break; } case BW_IR_IADD3: ws_wrs(l, in->dst_vreg, in->dst_subreg, a + b, p); break; /* the encoder's IADD3 has Rc = RZ */ case BW_IR_SHF_R: ws_wrs(l, in->dst_vreg, in->dst_subreg, 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_wrs(l, in->dst_vreg, in->dst_subreg, a << b, p); break; diff --git a/tests/bw_warp_sim_k.h b/tests/bw_warp_sim_k.h index 993875647..79c243be6 100644 --- a/tests/bw_warp_sim_k.h +++ b/tests/bw_warp_sim_k.h @@ -44,8 +44,8 @@ * 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 - * WS_E_IMADHI_RC IMAD.HI.U32 with a register Rc: the chip reads Rc as a 64-bit pair (omega #492, - * nvcc oracle, chip probe pending), so a non-RZ Rc is refused, not modelled + * WS_E_IMADHI_RC IMAD.HI.U32 with an odd register Rc. An even Rc is modelled as the 64-bit pair + * Rc+1:Rc the chip reads (omega #492, nvcc oracle, chip probe pending); odd has no evidence * 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. @@ -166,9 +166,16 @@ static void ws_lane_exec(WsSim *s, WsLane *l, const BlackwellIRInsn *in, int pc, case BW_IR_MOV_RZ: ws_wrs(l, in->dst_vreg, in->dst_subreg, 0, p); break; case BW_IR_IMAD: ws_wrs(l, in->dst_vreg, in->dst_subreg, 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: /* Rd = hi32(Ra * Rb) with Rc = RZ. The chip reads a register Rc as a 64-bit pair (omega #492): refused until a chip probe pins the model */ - if (in->src3_vreg >= 0) { ws_fail(s, WS_E_IMADHI_RC, pc, "IMAD.HI.U32 with a register Rc (the chip reads Rc as a 64-bit pair, not chip-probed): use RZ, or SHF.R + IADD3"); return; } - ws_wrs(l, in->dst_vreg, in->dst_subreg, (uint32_t)(((uint64_t)a * b) >> 32), p); break; + case BW_IR_IMAD_HI_U32: { /* Rd = hi32(Ra * Rb + Rc+1:Rc). A register Rc is a 64-bit pair, low word Rc (omega #492, nvcc 13.0 + * oracle evidence/IMADHI-RC-ORACLE-nvcc13.0-20261016, not chip-probed); an odd Rc has no evidence: refused */ + uint64_t c64 = 0; + int rc = in->src3_vreg >= 0 ? p->regalloc.vreg_to_phys[in->src3_vreg] : -1; + if (rc >= 0) { + rc = (rc + in->src3_subreg) & 0xff; + if (rc & 1) { ws_fail(s, WS_E_IMADHI_RC, pc, "IMAD.HI.U32 with an odd register Rc (the chip reads a 64-bit pair; nvcc emits only even bases, odd not chip-probed): use RZ or an even pair"); return; } + c64 = (uint64_t)l->r[rc] | ((uint64_t)l->r[rc + 1] << 32); + } + ws_wrs(l, in->dst_vreg, in->dst_subreg, (uint32_t)(((uint64_t)a * b + c64) >> 32), p); break; } case BW_IR_IADD3: ws_wrs(l, in->dst_vreg, in->dst_subreg, a + b, p); break; /* the encoder's IADD3 has Rc = RZ */ case BW_IR_SHF_R: ws_wrs(l, in->dst_vreg, in->dst_subreg, 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_wrs(l, in->dst_vreg, in->dst_subreg, a << b, p); break; diff --git a/tests/bw_warp_sim_pa.h b/tests/bw_warp_sim_pa.h index 157e396cd..3417910cc 100644 --- a/tests/bw_warp_sim_pa.h +++ b/tests/bw_warp_sim_pa.h @@ -44,8 +44,8 @@ * 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 - * WS_E_IMADHI_RC IMAD.HI.U32 with a register Rc: the chip reads Rc as a 64-bit pair (omega #492, - * nvcc oracle, chip probe pending), so a non-RZ Rc is refused, not modelled + * WS_E_IMADHI_RC IMAD.HI.U32 with an odd register Rc. An even Rc is modelled as the 64-bit pair + * Rc+1:Rc the chip reads (omega #492, nvcc oracle, chip probe pending); odd has no evidence * 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. @@ -175,9 +175,16 @@ static void ws_lane_exec(WsSim *s, WsLane *l, const BlackwellIRInsn *in, int pc, case BW_IR_MOV_RZ: ws_wrs(l, in->dst_vreg, in->dst_subreg, 0, p); break; case BW_IR_IMAD: ws_wrs(l, in->dst_vreg, in->dst_subreg, 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: /* Rd = hi32(Ra * Rb) with Rc = RZ. The chip reads a register Rc as a 64-bit pair (omega #492): refused until a chip probe pins the model */ - if (in->src3_vreg >= 0) { ws_fail(s, WS_E_IMADHI_RC, pc, "IMAD.HI.U32 with a register Rc (the chip reads Rc as a 64-bit pair, not chip-probed): use RZ, or SHF.R + IADD3"); return; } - ws_wrs(l, in->dst_vreg, in->dst_subreg, (uint32_t)(((uint64_t)a * b) >> 32), p); break; + case BW_IR_IMAD_HI_U32: { /* Rd = hi32(Ra * Rb + Rc+1:Rc). A register Rc is a 64-bit pair, low word Rc (omega #492, nvcc 13.0 + * oracle evidence/IMADHI-RC-ORACLE-nvcc13.0-20261016, not chip-probed); an odd Rc has no evidence: refused */ + uint64_t c64 = 0; + int rc = in->src3_vreg >= 0 ? p->regalloc.vreg_to_phys[in->src3_vreg] : -1; + if (rc >= 0) { + rc = (rc + in->src3_subreg) & 0xff; + if (rc & 1) { ws_fail(s, WS_E_IMADHI_RC, pc, "IMAD.HI.U32 with an odd register Rc (the chip reads a 64-bit pair; nvcc emits only even bases, odd not chip-probed): use RZ or an even pair"); return; } + c64 = (uint64_t)l->r[rc] | ((uint64_t)l->r[rc + 1] << 32); + } + ws_wrs(l, in->dst_vreg, in->dst_subreg, (uint32_t)(((uint64_t)a * b + c64) >> 32), p); break; } case BW_IR_IADD3: ws_wrs(l, in->dst_vreg, in->dst_subreg, a + b, p); break; /* the encoder's IADD3 has Rc = RZ */ case BW_IR_SHF_R: ws_wrs(l, in->dst_vreg, in->dst_subreg, 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_wrs(l, in->dst_vreg, in->dst_subreg, a << b, p); break; diff --git a/tests/bw_warp_sim_tc.h b/tests/bw_warp_sim_tc.h index a08e38b6a..95da503cd 100644 --- a/tests/bw_warp_sim_tc.h +++ b/tests/bw_warp_sim_tc.h @@ -44,8 +44,8 @@ * 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 - * WS_E_IMADHI_RC IMAD.HI.U32 with a register Rc: the chip reads Rc as a 64-bit pair (omega #492, - * nvcc oracle, chip probe pending), so a non-RZ Rc is refused, not modelled + * WS_E_IMADHI_RC IMAD.HI.U32 with an odd register Rc. An even Rc is modelled as the 64-bit pair + * Rc+1:Rc the chip reads (omega #492, nvcc oracle, chip probe pending); odd has no evidence * 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. @@ -184,9 +184,16 @@ static void ws_lane_exec(WsSim *s, WsLane *l, const BlackwellIRInsn *in, int pc, case BW_IR_MOV_RZ: ws_wrs(l, in->dst_vreg, in->dst_subreg, 0, p); break; case BW_IR_IMAD: ws_wrs(l, in->dst_vreg, in->dst_subreg, 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: /* Rd = hi32(Ra * Rb) with Rc = RZ. The chip reads a register Rc as a 64-bit pair (omega #492): refused until a chip probe pins the model */ - if (in->src3_vreg >= 0) { ws_fail(s, WS_E_IMADHI_RC, pc, "IMAD.HI.U32 with a register Rc (the chip reads Rc as a 64-bit pair, not chip-probed): use RZ, or SHF.R + IADD3"); return; } - ws_wrs(l, in->dst_vreg, in->dst_subreg, (uint32_t)(((uint64_t)a * b) >> 32), p); break; + case BW_IR_IMAD_HI_U32: { /* Rd = hi32(Ra * Rb + Rc+1:Rc). A register Rc is a 64-bit pair, low word Rc (omega #492, nvcc 13.0 + * oracle evidence/IMADHI-RC-ORACLE-nvcc13.0-20261016, not chip-probed); an odd Rc has no evidence: refused */ + uint64_t c64 = 0; + int rc = in->src3_vreg >= 0 ? p->regalloc.vreg_to_phys[in->src3_vreg] : -1; + if (rc >= 0) { + rc = (rc + in->src3_subreg) & 0xff; + if (rc & 1) { ws_fail(s, WS_E_IMADHI_RC, pc, "IMAD.HI.U32 with an odd register Rc (the chip reads a 64-bit pair; nvcc emits only even bases, odd not chip-probed): use RZ or an even pair"); return; } + c64 = (uint64_t)l->r[rc] | ((uint64_t)l->r[rc + 1] << 32); + } + ws_wrs(l, in->dst_vreg, in->dst_subreg, (uint32_t)(((uint64_t)a * b + c64) >> 32), p); break; } case BW_IR_IADD3: ws_wrs(l, in->dst_vreg, in->dst_subreg, a + b, p); break; /* the encoder's IADD3 has Rc = RZ */ case BW_IR_SHF_R: ws_wrs(l, in->dst_vreg, in->dst_subreg, 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_wrs(l, in->dst_vreg, in->dst_subreg, a << b, p); break; diff --git a/tests/gpu_imadhi_rc_test.c b/tests/gpu_imadhi_rc_test.c index 179596a27..efdb4f1a5 100644 --- a/tests/gpu_imadhi_rc_test.c +++ b/tests/gpu_imadhi_rc_test.c @@ -4,21 +4,27 @@ * Receipt (manufacturer's compiler, not a chip run): nvcc 13.0 for sm_121 emits, for * hi32(a * b + c64) IMAD.HI.U32 R7, R7, R6, R2 with R2:R3 = c64 (no separate add), * hi32(a * b + (u64)c32) UMOV UR5, URZ ; IMAD.HI.U32 R5, R7, R6, UR4 (upper half zeroed first), - * __umulhi(a, b) + c32 Rc pair = {lo 0, hi c32}, so the add lands in the high word. - * Listing and source: evidence/IMADHI-RC-ORACLE-nvcc13.0-20261016/. The first version of the - * trainer GEMM (PR #487) used IMAD.HI.U32 with a register Rc, passed the simulators (32-bit Rc) - * and gave wrong odd columns on the chip. A chip probe of the exact pair semantics is still - * pending (lane E3), so the simulators refuse a non-RZ Rc instead of modelling it. + * __umulhi(a, b) + c32 HFMA2 R4 = 0 ; R5 = c32 ; IMAD.HI.U32 R5, R8, R9, R4 (pair {lo 0, hi c32}), + * so the low word is Rc and the high word Rc+1, and every base is even. Listing and source: + * evidence/IMADHI-RC-ORACLE-nvcc13.0-20261016/. The first version of the trainer GEMM (PR #487) + * packed the odd-column B fragment with IMAD.HI.U32 O, W0, 0x10000, tw (meaning (W0 >> 16) + tw), + * passed the simulators (then hi32(Ra * Rb) + Rc) and gave wrong, non-repeatable odd columns on + * the chip (the stale upper register of the pair); commit 5b10b824 replaced it with SHF.R + IADD3. + * The simulators now model an even Rc as the pair and refuse an odd Rc (no evidence for it). + * The pair model is the compiler's reading, not chip-probed. * * This file is compiled once per simulator header: -DSIM_HEADER='"bw_warp_sim_k.h"' and so on * (Makefile target test-gpu-imadhi-rc). The four headers share their static names, so one * translation unit holds one simulator. * - * Checks (each runs the one-instruction kernel on one warp and stores Rd): - * 1. Rc = RZ: the simulator runs and Rd = hi32(Ra * Rb) (control, the one live user today) - * 2. Rc = register: the encoder emits a non-RZ Rc (the chip would read a pair) and the - * simulator refuses with WS_E_IMADHI_RC naming the pc. Before the fix it returned - * hi32(Ra * Rb) + Rc.lo, the 32-bit model the chip does not implement. + * Checks (each runs a straight-line kernel on one warp and stores one word per lane): + * 1. Rc = RZ: Rd = hi32(Ra * Rb) (the one live user today) + * 2. Rc = R6 (even): the encoder emits Rc byte 06 and Rd = hi32(Ra * Rb + R7:R6) + * 3. Rc = R7 (odd): refused with WS_E_IMADHI_RC naming the pc + * 4. PR #487 replay: the IMAD.HI.U32 odd-column pack must NOT match the emulation + * (W0 >> 16) + (W1 & 0xffff0000); it must give the pair value, for a stale upper register + * and for a zero one. The old 32-bit model matched the emulation here: the #487 false pass. + * 5. The SHF.R + IADD3 pack of commit 5b10b824 matches the emulation (control). */ #include #include @@ -30,7 +36,7 @@ #endif #include SIM_HEADER -enum { V_OUT = 0, V_A, V_B, V_C, V_D, NV }; +enum { V_OUT = 0, V_A, V_B, V_C, V_D, V_W1, V_MASK, V_T, NV }; enum { CB_OUT = 0x380 }; static void ins(BlackwellIRProgram *p, BlackwellIROpcode op, int d, int a, int b, int c, uint32_t imm, uint8_t dsub) { @@ -39,8 +45,15 @@ static void ins(BlackwellIRProgram *p, BlackwellIROpcode op, int d, int a, int b i->op = op; i->dst_vreg = d; i->src1_vreg = a; i->src2_vreg = b; i->src3_vreg = c; i->ureg = -1; i->imm = imm; i->dst_subreg = dsub; } -/* out[tid] = IMAD.HI.U32(a, b, rc ? {chi:clo} : RZ) */ -static void build(BlackwellIRProgram *p, uint32_t a, uint32_t b, uint32_t clo, uint32_t chi, int rc) { +/* physical registers: out pair R2:R3, a R4, b R5, c pair R6:R7 (base = c_phys), d R8, w1 R10, mask R11, t R12 */ +static void regs(BlackwellIRProgram *p, int c_phys) { + const int phys[NV] = { 2, 4, 5, c_phys, 8, 10, 11, 12 }; + p->regalloc.num_vregs = NV; + for (int v = 0; v < NV; v++) p->regalloc.vreg_to_phys[v] = phys[v]; +} + +/* out[tid] = IMAD.HI.U32(a, b, rc ? {chi:clo} : RZ), Rc at physical c_phys */ +static void build(BlackwellIRProgram *p, uint32_t a, uint32_t b, uint32_t clo, uint32_t chi, int rc, int c_phys) { memset(p, 0, sizeof *p); ins(p, BW_IR_LDC64, V_OUT, -1, -1, -1, CB_OUT, 0); ins(p, BW_IR_MOV_IMM, V_A, -1, -1, -1, a, 0); @@ -50,10 +63,30 @@ static void build(BlackwellIRProgram *p, uint32_t a, uint32_t b, uint32_t clo, u ins(p, BW_IR_IMAD_HI_U32, V_D, V_A, V_B, rc ? V_C : -1, 0, 0); ins(p, BW_IR_STG_E, -1, V_OUT, V_D, -1, 0, 0); ins(p, BW_IR_EXIT, -1, -1, -1, -1, 0, 0); - /* physical registers: out pair R2:R3, a R4, b R5, c pair R6:R7 (even-aligned), d R8 */ - static const int phys[NV] = { 2, 4, 5, 6, 8 }; - p->regalloc.num_vregs = NV; - for (int v = 0; v < NV; v++) p->regalloc.vreg_to_phys[v] = phys[v]; + regs(p, c_phys); +} + +/* The #487 odd-column B-fragment pack. tw = W1 & 0xffff0000 lands in R6; R7 holds whatever was there + * (stale). imadhi = 1: O = IMAD.HI.U32(W0, 0x10000, tw), the first tgemm version. + * imadhi = 0: O = SHF.R.U32.HI(W0, 16) + tw, the fix in 5b10b824. */ +static void build_pack(BlackwellIRProgram *p, uint32_t w0, uint32_t w1, uint32_t stale, int imadhi) { + memset(p, 0, sizeof *p); + ins(p, BW_IR_LDC64, V_OUT, -1, -1, -1, CB_OUT, 0); + ins(p, BW_IR_MOV_IMM, V_C, -1, -1, -1, stale, 1); /* an earlier value left in R7 */ + ins(p, BW_IR_MOV_IMM, V_A, -1, -1, -1, w0, 0); + ins(p, BW_IR_MOV_IMM, V_W1, -1, -1, -1, w1, 0); + ins(p, BW_IR_MOV_IMM, V_MASK, -1, -1, -1, 0xffff0000u, 0); + ins(p, BW_IR_LOP3_LUT, V_C, V_W1, V_MASK, -1, 0xc0u, 0); /* tw = W1 & mask (a & b = 0xf0 & 0xcc) */ + if (imadhi) { + ins(p, BW_IR_MOV_IMM, V_B, -1, -1, -1, 0x10000u, 0); + ins(p, BW_IR_IMAD_HI_U32, V_D, V_A, V_B, V_C, 0, 0); + } else { + ins(p, BW_IR_SHF_R, V_T, V_A, -1, -1, 16u, 0); + ins(p, BW_IR_IADD3, V_D, V_T, V_C, -1, 0, 0); + } + ins(p, BW_IR_STG_E, -1, V_OUT, V_D, -1, 0, 0); + ins(p, BW_IR_EXIT, -1, -1, -1, -1, 0, 0); + regs(p, 6); } static int run(const BlackwellIRProgram *p, uint32_t *out, char *msg, size_t msglen) { @@ -68,37 +101,68 @@ static int run(const BlackwellIRProgram *p, uint32_t *out, char *msg, size_t msg return err ? s.err : 0; } +static uint32_t hi32_pair(uint32_t a, uint32_t b, uint32_t lo, uint32_t hi) { + return (uint32_t)((((uint64_t)a * b) + (((uint64_t)hi << 32) | lo)) >> 32); +} + int main(void) { const uint32_t A = 0xdeadbeefu, B = 0x10000u, CLO = 0x1234u, CHI = 0x5678u; /* B = 2^16 is the tgemm pack */ const uint32_t hi_ab = (uint32_t)(((uint64_t)A * B) >> 32); - const uint32_t pair_model = (uint32_t)((((uint64_t)A * B) + (((uint64_t)CHI << 32) | CLO)) >> 32); + const uint32_t pair_model = hi32_pair(A, B, CLO, CHI); int fails = 0; char msg[200]; uint32_t out; BlackwellIRProgram *p = calloc(1, sizeof *p); printf("sim %s: hi32(a*b) 0x%08x, nvcc pair model 0x%08x (chip probe pending), old 32-bit model 0x%08x\n", SIM_HEADER, hi_ab, pair_model, hi_ab + CLO); /* 1. Rc = RZ: runs, Rd = hi32(a * b) */ - build(p, A, B, CLO, CHI, 0); out = 0xffbadbadu; + build(p, A, B, CLO, CHI, 0, 6); out = 0xffbadbadu; int err = run(p, &out, msg, sizeof msg); if (err || out != hi_ab) { fails++; printf("FAIL rc=RZ: err %d (%s) out 0x%08x want 0x%08x\n", err, msg, out, hi_ab); } else printf("PASS rc=RZ: Rd = hi32(a*b) = 0x%08x\n", out); - /* 2. Rc = register pair: the word carries a non-RZ Rc, the simulator must refuse */ - build(p, A, B, CLO, CHI, 1); + /* 2. Rc = R6 (even): the word carries Rc = R6 and the simulator reads the pair R7:R6 */ + build(p, A, B, CLO, CHI, 1, 6); BlackwellIRInsn e = p->insns[5]; e.dst_vreg = 8; e.src1_vreg = 4; e.src2_vreg = 5; e.src3_vreg = 6; /* physical numbers for the encoder */ uint32_t w[4] = { 0, 0, 0, 0 }; if (omega_blackwell_encode_one(&e, w) != 0 || (w[0] & 0xffffu) != 0x7227u || (w[2] & 0xffu) != 6u) { fails++; printf("FAIL encode: IMAD.HI.U32 R8, R4, R5, R6 -> %08x %08x %08x %08x (want opcode 7227, Rc byte 06)\n", w[0], w[1], w[2], w[3]); - } else printf("PASS encode: IMAD.HI.U32 R8, R4, R5, R6 carries Rc = R6 (the chip reads R6:R7)\n"); + } else printf("PASS encode: IMAD.HI.U32 R8, R4, R5, R6 carries Rc = R6 (the chip reads R7:R6)\n"); out = 0xffbadbadu; err = run(p, &out, msg, sizeof msg); + if (err || out != pair_model) { + fails++; printf("FAIL rc=R6: err %d (%s) out 0x%08x want pair 0x%08x (old 32-bit model 0x%08x)\n", err, msg, out, pair_model, hi_ab + CLO); + } else printf("PASS rc=R6: Rd = hi32(a*b + R7:R6) = 0x%08x\n", out); + + /* 3. Rc = R7 (odd): no evidence of what the chip reads, refused */ + build(p, A, B, CLO, CHI, 1, 7); out = 0xffbadbadu; + err = run(p, &out, msg, sizeof msg); if (err != WS_E_IMADHI_RC) { - fails++; - printf("FAIL rc=R6: simulator did not refuse (err %d%s%s), out 0x%08x: a 32-bit Rc model the chip does not implement\n", - err, err ? " " : "", err ? msg : "", out); + fails++; printf("FAIL rc=R7: simulator did not refuse an odd Rc (err %d%s%s), out 0x%08x\n", err, err ? " " : "", err ? msg : "", out); } else if (!strstr(msg, "IMAD.HI.U32") || !strstr(msg, "at pc 5")) { - fails++; printf("FAIL rc=R6: refused but the message does not name the instruction and pc: %s\n", msg); - } else printf("PASS rc=R6: refused: %s\n", msg); + fails++; printf("FAIL rc=R7: refused but the message does not name the instruction and pc: %s\n", msg); + } else printf("PASS rc=R7: refused: %s\n", msg); + + /* 4 and 5. PR #487 replay: the odd-column pack, stale and zero upper register */ + static const struct { uint32_t w0, w1, stale; } pk[] = { + { 0x3f80bf80u, 0x40004040u, 0x3f803f80u }, /* bf16 pairs, a live value left in R7 */ + { 0xdeadbeefu, 0x5678cafeu, 0x00000000u }, /* zero upper: still wrong, tw lands in the low word (carry here) */ + { 0x12340001u, 0xabcd0000u, 0x00000000u }, /* zero upper, no carry */ + }; + for (unsigned k = 0; k < sizeof pk / sizeof pk[0]; k++) { + const uint32_t tw = pk[k].w1 & 0xffff0000u, emul = (pk[k].w0 >> 16) + tw; + const uint32_t chip = hi32_pair(pk[k].w0, 0x10000u, tw, pk[k].stale); + build_pack(p, pk[k].w0, pk[k].w1, pk[k].stale, 1); out = 0xffbadbadu; + err = run(p, &out, msg, sizeof msg); + if (err || out != chip || out == emul) { + fails++; + printf("FAIL pack %u IMAD.HI.U32: err %d (%s) out 0x%08x, want the pair value 0x%08x != emulation 0x%08x%s\n", k, err, msg, out, chip, emul, + (!err && out == emul) ? " (matches the emulation: the 32-bit Rc model of the #487 false pass)" : ""); + } else printf("PASS pack %u IMAD.HI.U32: odd column 0x%08x != emulation 0x%08x (stale R7 0x%08x), as on the chip in #487\n", k, out, emul, pk[k].stale); + build_pack(p, pk[k].w0, pk[k].w1, pk[k].stale, 0); out = 0xffbadbadu; + err = run(p, &out, msg, sizeof msg); + if (err || out != emul) { fails++; printf("FAIL pack %u SHF.R+IADD3: err %d (%s) out 0x%08x want 0x%08x\n", k, err, msg, out, emul); } + else printf("PASS pack %u SHF.R+IADD3: odd column 0x%08x = emulation\n", k, out); + } free(p); printf("%s %s\n", fails ? "FAIL" : "PASS", SIM_HEADER);