upleb.uk

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

summaryrefslogtreecommitdiff
path: root/tests/test_rdna2spirv_cli.py
diff options
context:
space:
mode:
authorKacper Serewiś <kacper@serewis.nl>2026-11-23 15:22:37 +0100
committerGitHub <noreply@github.com>2026-11-23 08:22:37 -0600
commite835ce671a6edcbd64fa223dcde5e346bbd7af94 (patch)
tree705ca8727a8a3618919545c7befaaec7a2d84abe /tests/test_rdna2spirv_cli.py
parenta31e3c68391ae81f8140a0bab9e3b06f99fba95b (diff)
Report missing required translator inputs and retire stale outputs (#6292)
Squash-merged after diff review of PR #6292 - Report missing required translator inputs and retire stale outputs
Diffstat (limited to 'tests/test_rdna2spirv_cli.py')
-rw-r--r--tests/test_rdna2spirv_cli.py105
1 files changed, 103 insertions, 2 deletions
diff --git a/tests/test_rdna2spirv_cli.py b/tests/test_rdna2spirv_cli.py
index 7cd90ffcb..e62bbbbe2 100644
--- a/tests/test_rdna2spirv_cli.py
+++ b/tests/test_rdna2spirv_cli.py
@@ -2,12 +2,13 @@
2 2
3 python -I tests/test_rdna2spirv_cli.py 3 python -I tests/test_rdna2spirv_cli.py
4 4
5Standard library only: the output tests substitute the SPIR-V tools and use synthetic input. 5Synthetic inputs only. The end-to-end retry test also needs SPIRV-Tools on PATH.
6""" 6"""
7import argparse 7import argparse
8import io 8import io
9import json 9import json
10import os 10import os
11import shutil
11import subprocess 12import subprocess
12import sys 13import sys
13import tempfile 14import tempfile
@@ -347,12 +348,112 @@ class ValidatedOutput(unittest.TestCase):
347 output = io.StringIO() 348 output = io.StringIO()
348 with mock.patch.object(sys, "argv", [TOOL, str(self.src), "--out", str(self.out)]), \ 349 with mock.patch.object(sys, "argv", [TOOL, str(self.src), "--out", str(self.out)]), \
349 mock.patch.object(rdna2spirv.subprocess, "run", side_effect=spirv_tool), redirect_stdout(output): 350 mock.patch.object(rdna2spirv.subprocess, "run", side_effect=spirv_tool), redirect_stdout(output):
350 rdna2spirv.main() 351 self.exit_code = rdna2spirv.main()
351 return output.getvalue() 352 return output.getvalue()
352 353
353 def test_existing_single_s_file_still_translates(self): 354 def test_existing_single_s_file_still_translates(self):
354 self.assertIn("1 pass spirv-val", self.translate()) 355 self.assertIn("1 pass spirv-val", self.translate())
355 356
357 def test_missing_required_inputs_retire_stale_outputs_and_continue(self):
358 listing_dir = self.src.parent
359 root = listing_dir.parent
360 missing = ("cs_dispatch", "ms_reanchor")
361 for name in missing:
362 (root / (name + ".dxbc")).write_bytes((root / "ps_fixture.dxbc").read_bytes())
363 (listing_dir / (name + ".s")).write_bytes(self.src.read_bytes())
364 (self.out / (name + ".spv")).write_bytes(b"previous module")
365 (listing_dir / "ms_reanchor.s").write_bytes(
366 b"\ts_endpgm ; encoding: [0x00,0x00,0x81,0xbf]\n; invalid instruction encoding\n")
367 self.index.write_bytes(json.dumps(dict.fromkeys(missing, {})).encode())
368 self.src = listing_dir
369 err = io.StringIO()
370 with redirect_stderr(err):
371 output = self.translate()
372 self.assertEqual(self.exit_code, 1)
373 self.assertIn("3 shaders: 1 translated, 1 pass spirv-val", output)
374 self.assertIn("2 of 3 selected shaders missing required inputs", err.getvalue())
375 for name in missing:
376 self.assertIn(name, err.getvalue())
377 self.assertFalse((self.out / (name + ".spv")).exists())
378 self.assertTrue(self.module.exists())
379 self.assertEqual(set(json.loads(self.index.read_bytes())), {"ps_fixture"})
380
381 def test_missing_dispatch_invalidates_completed_shard(self):
382 src = self.src.with_name("cs_dispatch.s")
383 src.write_bytes(self.src.read_bytes())
384 container = self.src.parent.parent / "ps_fixture.dxbc"
385 shutil.copyfile(container, container.with_name("cs_dispatch.dxbc"))
386 part = self.out / "index.part0.json"
387 stale = self.out / "cs_dispatch.spv"
388 for interrupted in (False, True):
389 with self.subTest(interrupted=interrupted):
390 selection = src
391 if interrupted:
392 # The compute input retires its module, then the next listing hits a read error.
393 container.unlink()
394 container.mkdir()
395 selection = src.parent
396 stale.write_bytes(b"old compute")
397 self.module.write_bytes(b"previous pixel")
398 part.write_bytes(b'{"cs_dispatch": {}, "ps_fixture": {}}')
399 self.index.write_bytes(part.read_bytes())
400 result = subprocess.run([sys.executable, "-I", TOOL, str(selection), "--out", str(self.out),
401 "--shard", "0/1"], capture_output=True, text=True)
402 self.assertEqual(result.returncode, 1, result.stderr)
403 self.assertIn("missing translation input", result.stderr)
404 if interrupted:
405 self.assertIn("ps_fixture.dxbc", result.stderr)
406 self.assertFalse(part.exists())
407 self.assertFalse(self.index.exists())
408 self.assertFalse(stale.exists())
409 self.assertEqual(self.module.read_bytes(), b"previous pixel")
410
411 @unittest.skipUnless(shutil.which("spirv-as") and shutil.which("spirv-val"), "requires SPIRV-Tools")
412 def test_failed_shard_retry_cannot_be_merged(self):
413 def run(out, *args, hide_tools=False):
414 env = os.environ.copy()
415 if hide_tools:
416 env["PATH"] = str(self.out)
417 return subprocess.run([sys.executable, "-I", TOOL, str(self.src.parent),
418 "--out", str(out), *args], env=env, capture_output=True, text=True)
419
420 def success(out, *args):
421 result = run(out, *args)
422 self.assertEqual(result.returncode, 0, result.stderr)
423
424 def snapshot(out):
425 return {p.name: p.read_bytes() for p in out.iterdir() if p.is_file()}
426
427 self.src.with_name("ps_other.s").write_bytes(self.src.read_bytes())
428 shutil.copyfile(self.src.parent.parent / "ps_fixture.dxbc", self.src.parent.parent / "ps_other.dxbc")
429 for shard in ("0/2", "1/2"):
430 success(self.out, "--shard", shard)
431 success(self.out, "--merge")
432 baseline = snapshot(self.out)
433 self.assertEqual(set(json.loads(self.index.read_bytes())), {"ps_fixture", "ps_other"})
434 for assembler in ("spirv-as", "python"):
435 with self.subTest(assembler=assembler):
436 value = 1 if assembler == "spirv-as" else 2
437 self.src.write_bytes(f"\tv_mov_b32_e32 v0, {value}\n\ts_endpgm\n".encode())
438 result = run(self.out, "--shard", "0/2", "--assembler", assembler, hide_tools=True)
439 self.assertFalse(self.index.exists())
440 merge = run(self.out, "--merge")
441 self.assertEqual(merge.returncode, 2, merge.stdout)
442 self.assertEqual(result.returncode, 2, result.stderr)
443 self.assertIn("SPIRV-Tools", result.stderr)
444 self.assertFalse((self.out / "index.part0.json").exists())
445 for name in ("ps_fixture.spv", "ps_other.spv", "shard1.json", "index.part1.json"):
446 self.assertEqual((self.out / name).read_bytes(), baseline[name], name)
447 success(self.out, "--shard", "0/2")
448 success(self.out, "--merge")
449 self.assertNotEqual(self.module.read_bytes(), baseline[self.module.name])
450 baseline = snapshot(self.out)
451 fresh = self.out.with_name("fresh")
452 for shard in ("0/2", "1/2"):
453 success(fresh, "--shard", shard)
454 success(fresh, "--merge")
455 self.assertEqual(snapshot(self.out), snapshot(fresh))
456
356 def test_missing_assembler_is_one_setup_error(self): 457 def test_missing_assembler_is_one_setup_error(self):
357 err = io.StringIO() 458 err = io.StringIO()
358 with mock.patch.object(sys, "argv", [TOOL, str(self.src), "--out", str(self.out)]), \ 459 with mock.patch.object(sys, "argv", [TOOL, str(self.src), "--out", str(self.out)]), \