From 10f1eb6a6f42e4589e9a0572500cc8fbba209736 Mon Sep 17 00:00:00 2001 From: PierreEbele <127682386+PierreEbele@users.noreply.github.com> Date: Wed, 25 Nov 2026 02:36:43 +0000 Subject: [PATCH] crosslane_fuzz: add --pc-tail-ubo like lds_fuzz and valu_fuzz (CROSSLANE-FUZZ-PC-TAIL-UBO) crosslane_fuzz.py gets a pc_tail_env() helper (LEONIDA_PC_TAIL_UBO in 1/true/yes/on) and a --pc-tail-ubo flag. The flag is threaded through translate(), gpu_env(), run_gpu(), run_translator() and the --minimize path (still_fails()). rdna2spirv.py gets --pc-tail-ubo, and vk_compute_test gets LEONIDA_PC_TAIL_UBO=1, only when the flag or the environment variable asks for it. Without it, every translated program is a harness error on a GPU with 128-byte push constants. The tests mirror tests/test_lds_fuzz_cli.py. The existing fakes in tests/test_crosslane_fuzz.py take the new pc_tail argument. AI-assisted. Co-Authored-By: Claude Opus 5.5 --- tests/test_crosslane_fuzz.py | 84 +++++++++++++++++++++++++++++++++--- tools/crosslane_fuzz.py | 38 +++++++++++----- 2 files changed, 106 insertions(+), 16 deletions(-) diff --git a/tests/test_crosslane_fuzz.py b/tests/test_crosslane_fuzz.py index e82e05bc39..f501555b29 100644 --- a/tests/test_crosslane_fuzz.py +++ b/tests/test_crosslane_fuzz.py @@ -512,6 +512,80 @@ def test_emulated_wave64_gives_its_words(self): self.assertEqual(len(words), prog.group * len(X.OUT_REGS)) +class PcTailUbo(unittest.TestCase): + """--pc-tail-ubo, as lds_fuzz.py and valu_fuzz.py have it (D3D-14): rdna2spirv.py gets its --pc-tail-ubo and + vk_compute_test LEONIDA_PC_TAIL_UBO=1, and only when the flag or the environment variable asks for them.""" + + def test_pc_tail_ubo_forwarded_to_translator_and_vk_compute_test(self): + prog = X.make_program(1, 32, 4) + with tempfile.TemporaryDirectory() as work: + cmds = [] + + def fake_run(cmd, **kw): + cmds.append((list(cmd), kw.get("env"))) + if kw.get("env"): # the vk_compute_test runs, not the translator's + with open(kw["env"]["VKC_DUMP"], "wb") as f: + f.write(bytes(4 * prog.group * len(X.OUT_REGS))) + return subprocess.CompletedProcess(cmd, 0, "", "") + + with mock.patch.object(X.subprocess, "run", side_effect=fake_run): + X.translate(work, False) + X.translate(work, False, pc_tail=True) + self.assertNotIn("--pc-tail-ubo", cmds[0][0]) + self.assertIn("--pc-tail-ubo", cmds[1][0]) + + clean_env = {k: v for k, v in os.environ.items() if k != "LEONIDA_PC_TAIL_UBO"} + with mock.patch.dict(os.environ, clean_env, clear=True), \ + mock.patch.object(X.subprocess, "run", side_effect=fake_run): + X.run_gpu("vk_compute_test", "p.spv", prog, work, False) + X.run_gpu("vk_compute_test", "p.spv", prog, work, False, pc_tail=True) + self.assertNotIn("LEONIDA_PC_TAIL_UBO", cmds[2][1]) + self.assertEqual(cmds[3][1]["LEONIDA_PC_TAIL_UBO"], "1") + + def test_pc_tail_ubo_from_flag_or_env(self): + for extra, env_val, want in ( + ([], "", False), + (["--pc-tail-ubo"], "", True), + ([], "1", True), + ([], "yes", True), + ([], "0", False), + ): + with self.subTest(extra=extra, env_val=env_val): + names, seen_t, seen_g = [], [], [] + + def fake_translate(work, _emulate, pc_tail=False): + seen_t.append(pc_tail) + spdir = os.path.join(work, "spirv_nat") + os.makedirs(spdir, exist_ok=True) + for name in names: + open(os.path.join(spdir, name + ".spv"), "wb").close() + return spdir, "" + + def fake_run_gpu(_exe, _spv, prog, _work, _emulate, pc_tail=False): + seen_g.append(pc_tail) + words = [0] * (prog.group * len(X.OUT_REGS)) + for (tid, j), v in prog.expected()[0].items(): + words[tid * len(X.OUT_REGS) + j] = v + return words + + with tempfile.TemporaryDirectory() as build: + exe = os.path.join(build, "vk_compute_test.exe") # only has to exist: run_gpu is replaced + open(exe, "wb").close() + argv = ["crosslane_fuzz.py", "--programs", "1", "--wave", "32", "--modes", "native", + "--exe", exe, *extra] + with mock.patch.dict(os.environ, {"LEONIDA_PC_TAIL_UBO": env_val}), \ + mock.patch.object(sys, "argv", argv), \ + mock.patch.object(X.mc_command, "mc_command", return_value=["llvm-mc"]), \ + mock.patch.object(X, "assemble", side_effect=lambda mc, p, *a: names.append(p.name)), \ + mock.patch.object(X, "translate", side_effect=fake_translate), \ + mock.patch.object(X, "run_gpu", side_effect=fake_run_gpu), \ + contextlib.redirect_stdout(io.StringIO()): + code = X.main() + self.assertEqual(code, 0) + self.assertEqual(seen_t, [want]) + self.assertEqual(seen_g, [want]) + + class HardwareOracle(unittest.TestCase): """--oracle hw: the native kernel text (no GPU needed) and the exit-77 contract.""" @@ -597,7 +671,7 @@ def test_missing_dump_is_a_harness_error_with_the_exit_code(self): def _main(self, run_gpu, argv): """main() with the assembler, translator and GPU replaced: every program 'translates'.""" - def translate(work, emulate): + def translate(work, emulate, pc_tail=False): spdir = os.path.join(work, "spirv_emu" if emulate else "spirv_nat") os.makedirs(spdir, exist_ok=True) for name in names: @@ -628,7 +702,7 @@ def _expected_words(prog): def test_run_goes_on_after_a_hang_and_exits_2(self): calls = [] - def run_gpu(exe, spv, prog, work, emulate): + def run_gpu(exe, spv, prog, work, emulate, pc_tail=False): calls.append(prog.name) if len(calls) == 2: raise X.HarnessError("vk_compute_test timed out after 60 s") @@ -645,7 +719,7 @@ def run_gpu(exe, spv, prog, work, emulate): self.assertEqual(kept, [f"{calls[1]}_native.asm"]) def test_every_program_skipped_exits_2(self): - def run_gpu(exe, spv, prog, work, emulate): + def run_gpu(exe, spv, prog, work, emulate, pc_tail=False): raise X.Unpinned("the device can't run it at 32 lanes") code, out = self._main(run_gpu, ["--programs", "4", "--wave", "32", "--modes", "native"]) @@ -655,7 +729,7 @@ def run_gpu(exe, spv, prog, work, emulate): self.assertNotIn("mismatch;", out) def test_skips_beside_matches_exit_0(self): - def run_gpu(exe, spv, prog, work, emulate): + def run_gpu(exe, spv, prog, work, emulate, pc_tail=False): if prog.wave == 32: raise X.Unpinned("the device can't run it at 32 lanes") return self._expected_words(prog) @@ -667,7 +741,7 @@ def run_gpu(exe, spv, prog, work, emulate): self.assertIn("wave64 emulate: programs 2, match 2", out) def test_clean_run_still_exits_0(self): - code, out = self._main(lambda exe, spv, prog, work, emulate: self._expected_words(prog), + code, out = self._main(lambda exe, spv, prog, work, emulate, pc_tail=False: self._expected_words(prog), ["--programs", "3", "--wave", "32", "--modes", "native", "--no-maskdata"]) self.assertEqual(code, 0) self.assertIn("match 3, mismatch 0, hyp_only 0, rejected 0, harness_errors 0", out) diff --git a/tools/crosslane_fuzz.py b/tools/crosslane_fuzz.py index 6be4461806..d011160bd5 100644 --- a/tools/crosslane_fuzz.py +++ b/tools/crosslane_fuzz.py @@ -1305,23 +1305,34 @@ def resolve_branches(cmd, prog, src): return "\n".join(branch_offsets(lines, dict(zip(insns, sizes)))) + "\n" -def translate(work, emulate): +def pc_tail_env(): + """True when LEONIDA_PC_TAIL_UBO asks for rdna2spirv.py --pc-tail-ubo (128-byte push constants).""" + return (os.environ.get("LEONIDA_PC_TAIL_UBO") or "").strip().lower() in ("1", "true", "yes", "on") + + +def translate(work, emulate, pc_tail=False): out = os.path.join(work, "spirv_emu" if emulate else "spirv_nat") cmd = [sys.executable, "-I", TRANSLATOR[0], os.path.join(work, "disasm"), "--out", out] if emulate: cmd.append("--wave64-emulate") + if pc_tail: + cmd.append("--pc-tail-ubo") r = subprocess.run(cmd, capture_output=True, text=True) return out, r.stdout + r.stderr -def gpu_env(prog, emulate, dump, base=None): +def gpu_env(prog, emulate, dump, base=None, pc_tail=False): """vk_compute_test's environment (FUZZ-6, tools/lds_fuzz.py): VKC_OUT_WORDS sizes the buffer, VKC_DUMP gets its raw bytes. A wave32 program, or a wave64 one translated with --wave64-emulate, is written for 32-wide subgroups, so it asks for them with VKC_SUBGROUP_SIZE=32, as lds_fuzz.py and valu_fuzz.py do (FUZZ-13): on AMD, whose compute default is 64, a group over 32 would otherwise run as one 64-wide subgroup and mismatch in wave 1. - A native wave64 program keeps the device default (--w64-native runs it on whatever the GPU has).""" + A native wave64 program keeps the device default (--w64-native runs it on whatever the GPU has). With pc_tail + it also gets LEONIDA_PC_TAIL_UBO=1, for a translation with --pc-tail-ubo (D3D-14), as lds_fuzz.py and + valu_fuzz.py do.""" env = dict(os.environ if base is None else base, VKC_OUT_WORDS=str(max(prog.group * len(OUT_REGS), 64)), VKC_DUMP=dump) + if pc_tail: + env["LEONIDA_PC_TAIL_UBO"] = "1" if prog.wave == 32 or emulate: env["VKC_SUBGROUP_SIZE"] = "32" return env @@ -1340,11 +1351,11 @@ class Unpinned(RuntimeError): GPU_TIMEOUT = 60 # seconds for one program; a translated program that never ends shows up as this -def run_gpu(exe, spv, prog, work, emulate): +def run_gpu(exe, spv, prog, work, emulate, pc_tail=False): dump = os.path.join(work, "dump.bin") if os.path.exists(dump): os.remove(dump) # never compare a stale dump - env = gpu_env(prog, emulate, dump) + env = gpu_env(prog, emulate, dump, pc_tail=pc_tail) try: r = subprocess.run([exe, spv, "1", "dump"], capture_output=True, text=True, env=env, timeout=GPU_TIMEOUT) except subprocess.TimeoutExpired: @@ -1396,7 +1407,7 @@ def minimize(prog, mode, ctx): def still_fails(prog, ops, mode, ctx): - mc, exe = ctx + mc, exe, pc_tail = ctx with tempfile.TemporaryDirectory(prefix="xlmin-") as work: for d in ("disasm", "fssm"): os.mkdir(os.path.join(work, d)) @@ -1404,12 +1415,12 @@ def still_fails(prog, ops, mode, ctx): assemble(mc, prog, prog.source(ops), os.path.join(work, "disasm"), os.path.join(work, "fssm")) except RuntimeError: return False - spdir, _ = translate(work, mode == "emulate") + spdir, _ = translate(work, mode == "emulate", pc_tail) spv = os.path.join(spdir, prog.name + ".spv") if not os.path.exists(spv): return False try: - bad, hyp = compare(prog, run_gpu(exe, spv, prog, work, mode == "emulate"), ops) + bad, hyp = compare(prog, run_gpu(exe, spv, prog, work, mode == "emulate", pc_tail), ops) except (HarnessError, Unpinned): return False # a reduction that hangs or crashes is not the mismatch being reduced return bool(bad) and not hyp @@ -1456,6 +1467,9 @@ def main(): ap.add_argument("--w64-native", action="store_true", help="also run wave64 programs without --wave64-emulate (on a 32-wide subgroup that is the " "documented cross-half approximation, so mismatches are expected there)") + ap.add_argument("--pc-tail-ubo", action="store_true", + help="translate with --pc-tail-ubo and run vk_compute_test with LEONIDA_PC_TAIL_UBO=1 " + "(128-byte push constants; also set by LEONIDA_PC_TAIL_UBO=1)") ap.add_argument("--no-maskdata", action="store_true", help="leave out masks read as data (#293)") ap.add_argument("--no-w32-hi-lanes", action="store_true", help="wave32 lane selects stay below 32") ap.add_argument("--exe", default=os.path.join(ROOT, "runtime", "build", "vk_compute_test" + @@ -1482,6 +1496,7 @@ def main(): ap.add_argument("--translator", help="rdna2spirv.py to test (default: the one next to this file)") ap.add_argument("--self-test", action="store_true") args = ap.parse_args() + args.pc_tail_ubo = args.pc_tail_ubo or pc_tail_env() if not os.path.isfile(args.exe): # a Visual Studio build puts it in a configuration subfolder folder, name = os.path.split(args.exe) for config in ("RelWithDebInfo", "Release"): @@ -1568,7 +1583,7 @@ def main(): if mode == "hw": ops = minimize_hw(p, oracle, not args.no_hw_guard) elif mc is not None: - ops = minimize(p, mode, (mc, args.exe)) + ops = minimize(p, mode, (mc, args.exe, args.pc_tail_ubo)) else: continue hyp_ops = p.expected(ops)[1] @@ -1675,6 +1690,7 @@ def fails_with(ops): def run_translator(args, mc, progs, waves, modes, stats, fails): + pc_tail = bool(getattr(args, "pc_tail_ubo", False)) with tempfile.TemporaryDirectory(prefix="crosslane-") as work: disasm, fssm = os.path.join(work, "disasm"), os.path.join(work, "fssm") os.mkdir(disasm) @@ -1684,7 +1700,7 @@ def run_translator(args, mc, progs, waves, modes, stats, fails): for mode in modes: if mode == "emulate" and 64 not in waves: continue - spdir, log = translate(work, mode == "emulate") + spdir, log = translate(work, mode == "emulate", pc_tail) for p in progs: if mode == "emulate" and p.wave != 64: continue @@ -1700,7 +1716,7 @@ def run_translator(args, mc, progs, waves, modes, stats, fails): fails.append((p, mode, "not translated", None)) continue try: - bad, hyp = compare(p, run_gpu(args.exe, spv, p, work, mode == "emulate")) + bad, hyp = compare(p, run_gpu(args.exe, spv, p, work, mode == "emulate", pc_tail)) except HarnessError as e: # One program that hangs or crashes the harness used to end the run with a traceback and # lose every result before it. It is reported like the other failures and the run goes on.