upleb.uk

Public git repos — served from a NIP-34 GRASP relay at git.upleb.uk

summaryrefslogtreecommitdiff
path: root/tests/test_run_gpu_tests_python_assembler.py
diff options
context:
space:
mode:
authorsanchaton <138723000+sanchaton@users.noreply.github.com>2026-11-23 17:05:36 +0100
committerGitHub <noreply@github.com>2026-11-23 10:05:36 -0600
commitee93d8ce2a37ca1a5b32e72cd8b25e699a6d860c (patch)
tree3b3560aa491f11b426145019458d22a26619135b /tests/test_run_gpu_tests_python_assembler.py
parentd510dce18ef18331cb4f7af3378123beec6a0ffd (diff)
run_gpu_tests, rdna2spirv: support --assembler python, --no-validate, and --disasm-dir (#6676)
Squash-merged after diff review of PR #6676 - run_gpu_tests, rdna2spirv: support --assembler python, --no-validate, and --disasm-dir
Diffstat (limited to 'tests/test_run_gpu_tests_python_assembler.py')
-rw-r--r--tests/test_run_gpu_tests_python_assembler.py104
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."""
2import io
3import json
4import os
5from pathlib import Path
6import struct
7import sys
8import tempfile
9import unittest
10from unittest.mock import patch
11
12ROOT = Path(__file__).resolve().parents[1]
13sys.path.insert(0, str(ROOT / "scripts"))
14sys.path.insert(0, str(ROOT / "tools"))
15
16import rdna2spirv
17import run_gpu_tests
18
19
20class 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
103if __name__ == "__main__":
104 unittest.main()