diff --git a/tests/test_crosslane_fuzz.py b/tests/test_crosslane_fuzz.py index e82e05bc39..8aacaf5ef2 100644 --- a/tests/test_crosslane_fuzz.py +++ b/tests/test_crosslane_fuzz.py @@ -12,7 +12,9 @@ import subprocess import sys import tempfile +import types import unittest +from pathlib import Path from unittest import mock TOOLS = os.path.join(os.path.dirname(os.path.abspath(__file__)), "..", "tools") @@ -597,7 +599,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 +630,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 +647,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 +657,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 +669,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) @@ -945,5 +947,97 @@ def test_native_kernel_has_a_label_for_every_loop(self): self.assertGreater(loops, 10) + +class PcTailUbo(unittest.TestCase): + """LEONIDA_PC_TAIL_UBO / --pc-tail-ubo, as tools/lds_fuzz.py has it (TEST-873): on a device whose + maxPushConstantsSize is 128 the translated programs read the tail from a uniform block, and vk_compute_test + must be told the same. No GPU, no llvm-mc.""" + + def test_pc_tail_ubo_forwarded_to_translator_and_vk_compute_test(self): + import crosslane_fuzz as f + with tempfile.TemporaryDirectory() as tmp_str: + tmp = Path(tmp_str) + cmds = [] + + def fake_run(cmd, **kw): + cmds.append((list(cmd), kw.get("env"))) + env = kw.get("env") or {} + if env.get("VKC_DUMP"): + with open(env["VKC_DUMP"], "wb") as fh: # one word: enough for run_gpu to unpack + fh.write(b"\x00\x00\x00\x00") + (tmp / "spirv_nat").mkdir(parents=True, exist_ok=True) + (tmp / "spirv_emu").mkdir(parents=True, exist_ok=True) + for d in ("spirv_nat", "spirv_emu"): + (tmp / d / "index.json").write_text("{}", encoding="utf-8") + return subprocess.CompletedProcess(cmd, 0, "", "") + + with mock.patch.object(f.subprocess, "run", side_effect=fake_run): + f.translate(tmp, False) + f.translate(tmp, False, pc_tail=True) + self.assertNotIn("--pc-tail-ubo", cmds[0][0]) + self.assertIn("--pc-tail-ubo", cmds[1][0]) + + prog = types.SimpleNamespace(group=64, wave=32) + 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(f.subprocess, "run", side_effect=fake_run): + f.run_gpu("vk_compute_test", "a.spv", prog, str(tmp), False) + f.run_gpu("vk_compute_test", "a.spv", prog, str(tmp), 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): + import crosslane_fuzz as f + with tempfile.TemporaryDirectory() as tmp_str: + tmp = Path(tmp_str) + exe = tmp / ("vk_compute_test" + (".exe" if os.name == "nt" else "")) + exe.touch() + 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): + seen_t, seen_g = [], [] + + def fake_translate(work, _emulate, pc_tail=False): + seen_t.append(pc_tail) + out = os.path.join(str(work), "spirv_emu" if _emulate else "spirv_nat") + os.makedirs(out, exist_ok=True) + with open(os.path.join(out, "p100003.spv"), "wb"): + pass + return out, "" + + def fake_run_gpu(_exe, _spv, prog, _work, _emulate, pc_tail=False): + seen_g.append(pc_tail) + exp, _hyp = prog.expected() + return [v for _, v in sorted(exp.items())] + + def fake_assemble(_mc, _prog, _src, _disasm, _fssm): + pass + + def fake_mc(): + return ["llvm-mc"] + + argv = ["crosslane_fuzz.py", "--programs", "1", "--wave", "32", "--modes", "native", + "--exe", str(exe), *extra] + with mock.patch.dict(os.environ, {"LEONIDA_PC_TAIL_UBO": env_val}), \ + mock.patch.object(sys, "argv", argv), \ + mock.patch.object(f, "translate", side_effect=fake_translate), \ + mock.patch.object(f, "run_gpu", side_effect=fake_run_gpu), \ + mock.patch.object(f, "assemble", side_effect=fake_assemble), \ + mock.patch.object(f, "make_program", side_effect=lambda seed, wave, ops, **kw: + types.SimpleNamespace(name=f"p{seed}", wave=wave, group=32, + expected=lambda ops=None: ({}, False), + source=lambda: "", partial_wave=False)), \ + mock.patch("mc_command.mc_command", side_effect=fake_mc), \ + contextlib.redirect_stdout(io.StringIO()): + f.main() + self.assertEqual(seen_t, [want]) + self.assertEqual(seen_g, [want]) + + if __name__ == "__main__": unittest.main() diff --git a/tools/crosslane_fuzz.py b/tools/crosslane_fuzz.py index 6be4461806..a567260881 100644 --- a/tools/crosslane_fuzz.py +++ b/tools/crosslane_fuzz.py @@ -1305,11 +1305,18 @@ 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 @@ -1340,11 +1347,13 @@ 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) + if pc_tail: + env["LEONIDA_PC_TAIL_UBO"] = "1" try: r = subprocess.run([exe, spv, "1", "dump"], capture_output=True, text=True, env=env, timeout=GPU_TIMEOUT) except subprocess.TimeoutExpired: @@ -1480,8 +1489,12 @@ def main(): ap.add_argument("--no-hw-guard", action="store_true", help="no s_nop 7 after each instruction in the native kernels (hazards then show up as mismatches)") ap.add_argument("--translator", help="rdna2spirv.py to test (default: the one next to this file)") + 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("--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"): @@ -1684,7 +1697,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=bool(args.pc_tail_ubo)) for p in progs: if mode == "emulate" and p.wave != 64: continue @@ -1700,7 +1713,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=bool(args.pc_tail_ubo))) 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.