diff options
| author | TreXito <81475944+TreXito@users.noreply.github.com> | 2026-11-23 16:10:24 +0000 |
|---|---|---|
| committer | TreXito <81475944+TreXito@users.noreply.github.com> | 2026-11-23 16:10:24 +0000 |
| commit | f8e16608ae510166b45d3c89de52b55c685e2da1 (patch) | |
| tree | 530563df8bb179f1768cb34e7ca68367db8a48bd /tests/test_run_gpu_tests_python_assembler.py | |
| parent | c57774bf4b483c1a45179ddbdbca1c79874130ff (diff) | |
| parent | 8575d36edc479218972497a7bded8651a42856c0 (diff) | |
Merge origin/main into fuzz/mode-diff
Conflict in docs/design.md: kept main's wording for the three rows and kept the v_ldexp_f16 sentence in the MODE.FP_ROUND row.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Diffstat (limited to 'tests/test_run_gpu_tests_python_assembler.py')
| -rw-r--r-- | tests/test_run_gpu_tests_python_assembler.py | 104 |
1 files changed, 104 insertions, 0 deletions
diff --git a/tests/test_run_gpu_tests_python_assembler.py b/tests/test_run_gpu_tests_python_assembler.py new file mode 100644 index 000000000..0f66fd2f3 --- /dev/null +++ b/tests/test_run_gpu_tests_python_assembler.py | |||
| @@ -0,0 +1,104 @@ | |||
| 1 | """Tests for pure-Python SPIR-V assembly and --disasm-dir / --no-validate in run_gpu_tests.py and rdna2spirv.py.""" | ||
| 2 | import io | ||
| 3 | import json | ||
| 4 | import os | ||
| 5 | from pathlib import Path | ||
| 6 | import struct | ||
| 7 | import sys | ||
| 8 | import tempfile | ||
| 9 | import unittest | ||
| 10 | from unittest.mock import patch | ||
| 11 | |||
| 12 | ROOT = Path(__file__).resolve().parents[1] | ||
| 13 | sys.path.insert(0, str(ROOT / "scripts")) | ||
| 14 | sys.path.insert(0, str(ROOT / "tools")) | ||
| 15 | |||
| 16 | import rdna2spirv | ||
| 17 | import run_gpu_tests | ||
| 18 | |||
| 19 | |||
| 20 | class PythonAssemblerTests(unittest.TestCase): | ||
| 21 | def test_resolve_assembler_auto_and_explicit(self) -> None: | ||
| 22 | self.assertEqual(run_gpu_tests.resolve_assembler("python"), "python") | ||
| 23 | self.assertEqual(run_gpu_tests.resolve_assembler("spirv-as"), "spirv-as") | ||
| 24 | with patch.object(run_gpu_tests.shutil, "which", return_value="/usr/bin/spirv-as"): | ||
| 25 | self.assertEqual(run_gpu_tests.resolve_assembler("auto"), "spirv-as") | ||
| 26 | with patch.object(run_gpu_tests.shutil, "which", return_value=None): | ||
| 27 | self.assertEqual(run_gpu_tests.resolve_assembler("auto"), "python") | ||
| 28 | with self.assertRaises(ValueError): | ||
| 29 | run_gpu_tests.resolve_assembler("bogus") | ||
| 30 | |||
| 31 | def test_spirv_as_python_assembles_helper_shaders(self) -> None: | ||
| 32 | with tempfile.TemporaryDirectory() as tmp: | ||
| 33 | for name, src in ( | ||
| 34 | ("vs_tri.spv", run_gpu_tests.VS_TRI_SPVASM), | ||
| 35 | ("fs_passthru.spv", run_gpu_tests.make_fs_passthru(0)), | ||
| 36 | ): | ||
| 37 | out = Path(tmp) / name | ||
| 38 | run_gpu_tests.spirv_as(src, str(out), assembler="python") | ||
| 39 | data = out.read_bytes() | ||
| 40 | self.assertGreaterEqual(len(data), 20) | ||
| 41 | self.assertEqual(struct.unpack("<I", data[:4])[0], 0x07230203) | ||
| 42 | |||
| 43 | def test_rdna2spirv_assemble_spirv_no_validate_skips_spirv_val(self) -> None: | ||
| 44 | with tempfile.TemporaryDirectory() as tmp: | ||
| 45 | spvasm = Path(tmp) / "fs_passthru.spvasm" | ||
| 46 | spv = Path(tmp) / "fs_passthru.spv" | ||
| 47 | asm = run_gpu_tests.make_fs_passthru(0) | ||
| 48 | with patch.object(rdna2spirv, "_run_spirv_tool", | ||
| 49 | side_effect=AssertionError("external spirv tool called")) as run_tool: | ||
| 50 | res = rdna2spirv.assemble_spirv(str(spvasm), str(spv), asm_text=asm, | ||
| 51 | assembler="python", validate=False) | ||
| 52 | run_tool.assert_not_called() | ||
| 53 | self.assertEqual(res.returncode, 0) | ||
| 54 | self.assertTrue(spv.is_file()) | ||
| 55 | self.assertEqual(struct.unpack("<I", spv.read_bytes()[:4])[0], 0x07230203) | ||
| 56 | |||
| 57 | def test_rdna2spirv_cli_python_assembler_and_no_validate(self) -> None: | ||
| 58 | with tempfile.TemporaryDirectory() as tmp: | ||
| 59 | root = Path(tmp) | ||
| 60 | disasm = root / "disasm" | ||
| 61 | fssm = root / "fssm" | ||
| 62 | out = root / "out" | ||
| 63 | disasm.mkdir() | ||
| 64 | fssm.mkdir() | ||
| 65 | # Minimal valid compute listing (s_endpgm) and synthetic 0x148-byte FSSM header. | ||
| 66 | (disasm / "cs_noop.s").write_text("; synthetic\n\ts_endpgm\n", encoding="utf-8", newline="\n") | ||
| 67 | hdr = bytearray(0x148) | ||
| 68 | hdr[0:4] = b"FSSM" | ||
| 69 | struct.pack_into("<4I", hdr, 0x138, 0x8088, 64, 1, 1) | ||
| 70 | (fssm / "cs_noop.fssm").write_bytes(bytes(hdr)) | ||
| 71 | stdout = io.StringIO() | ||
| 72 | with patch("sys.stdout", stdout), \ | ||
| 73 | patch.object(sys, "argv", [ | ||
| 74 | "rdna2spirv.py", str(disasm), "--out", str(out), "--assembler", "python", "--no-validate", | ||
| 75 | ]), \ | ||
| 76 | patch.object(rdna2spirv, "_run_spirv_tool", | ||
| 77 | side_effect=AssertionError("external spirv tool called")): | ||
| 78 | rc = rdna2spirv.main() | ||
| 79 | self.assertIn(rc, (0, None)) | ||
| 80 | self.assertIn("1 shaders: 1 translated, 1 assembled (spirv-val skipped)", stdout.getvalue()) | ||
| 81 | spv = out / "cs_noop.spv" | ||
| 82 | self.assertTrue(spv.is_file()) | ||
| 83 | self.assertEqual(struct.unpack("<I", spv.read_bytes()[:4])[0], 0x07230203) | ||
| 84 | index = json.loads((out / "index.json").read_text(encoding="utf-8")) | ||
| 85 | self.assertIn("cs_noop", index) | ||
| 86 | |||
| 87 | def test_missing_disasm_dir_exits_2(self) -> None: | ||
| 88 | with tempfile.TemporaryDirectory() as tmp: | ||
| 89 | build = Path(tmp) / "build" | ||
| 90 | build.mkdir() | ||
| 91 | ext = ".exe" if os.name == "nt" else "" | ||
| 92 | for prog in run_gpu_tests.TEST_PROGRAMS: | ||
| 93 | (build / (prog + ext)).write_bytes(b"") | ||
| 94 | missing = Path(tmp) / "no_such_disasm" | ||
| 95 | err = io.StringIO() | ||
| 96 | with patch("sys.stderr", err), \ | ||
| 97 | patch.object(sys, "argv", ["run_gpu_tests.py", "--build", str(build), "--disasm-dir", str(missing)]): | ||
| 98 | rc = run_gpu_tests.main() | ||
| 99 | self.assertEqual(rc, 2) | ||
| 100 | self.assertIn("--disasm-dir is not a directory", err.getvalue()) | ||
| 101 | |||
| 102 | |||
| 103 | if __name__ == "__main__": | ||
| 104 | unittest.main() | ||