1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
|
"""rdna2spirv.py command-line validation and SPIR-V output tests.
python -I tests/test_rdna2spirv_cli.py
Synthetic inputs only. The end-to-end retry test also needs SPIRV-Tools on PATH.
"""
import argparse
import io
import json
import os
import shutil
import subprocess
import sys
import tempfile
import unittest
from contextlib import redirect_stderr, redirect_stdout
from pathlib import Path
from unittest import mock
ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.insert(0, os.path.join(ROOT, "tools"))
TOOL = os.path.join(ROOT, "tools", "rdna2spirv.py")
import rdna2spirv # noqa: E402
class ArgumentTypes(unittest.TestCase):
def test_limit(self):
self.assertEqual(rdna2spirv._limit_arg("200"), 200)
self.assertEqual(rdna2spirv._limit_arg("1"), 1)
for text in ("0", "-1", "-200", "x", ""):
with self.subTest(text=text), self.assertRaises(argparse.ArgumentTypeError):
rdna2spirv._limit_arg(text)
def test_shard(self):
self.assertEqual(rdna2spirv._shard_arg("0/8"), (0, 8))
self.assertEqual(rdna2spirv._shard_arg("7/8"), (7, 8))
self.assertEqual(rdna2spirv._shard_arg("0/1"), (0, 1))
for text in ("0/0", "8/8", "9/8", "-1/8", "1/-8", "a/b", "0", "0/8/1", "", "/8"):
with self.subTest(text=text), self.assertRaises(argparse.ArgumentTypeError):
rdna2spirv._shard_arg(text)
def test_max_structured_depth(self):
self.assertEqual(rdna2spirv._depth_arg("0"), 0)
self.assertEqual(rdna2spirv._depth_arg("64"), 64)
for text in ("-1", "x", "", "1.5"):
with self.subTest(text=text), self.assertRaises(argparse.ArgumentTypeError):
rdna2spirv._depth_arg(text)
def test_max_structured_depth_sets_the_bound(self):
# SHADER-427: the option replaces MAX_STRUCTURED_DEPTH for the run; without it the default is 64, written out
# here rather than read from the module so that changing the default fails this test.
self.assertEqual(rdna2spirv.MAX_STRUCTURED_DEPTH, 64)
with tempfile.TemporaryDirectory() as tmp, \
mock.patch.object(rdna2spirv, "MAX_STRUCTURED_DEPTH", rdna2spirv.MAX_STRUCTURED_DEPTH):
for extra, expected in (([], 64), (["--max-structured-depth", "0"], 0),
(["--max-structured-depth", "40"], 40)):
with self.subTest(extra=extra), \
mock.patch.object(sys, "argv", [TOOL, tmp, "--out", os.path.join(tmp, "o")] + extra), \
redirect_stdout(io.StringIO()):
rdna2spirv.MAX_STRUCTURED_DEPTH = 64
rdna2spirv.main()
self.assertEqual(rdna2spirv.MAX_STRUCTURED_DEPTH, expected)
class SourceDiscovery(unittest.TestCase):
"""The src argument is a .s file or a folder of .s files; other names are not inputs."""
def run_main(self, src, out):
def spirv_tool(cmd, **kwargs):
tool = cmd[0]
if tool == "spirv-as":
Path(kwargs.get("cwd", ""), cmd[3]).write_bytes(b"new module")
elif tool == "spirv-val":
self.assertEqual(Path(kwargs.get("cwd", ""), cmd[-1]).read_bytes(), b"new module")
else:
self.fail(f"unexpected tool: {tool}")
return subprocess.CompletedProcess(cmd, 0, "", "")
output = io.StringIO()
with mock.patch.object(sys, "argv", [TOOL, str(src), "--out", str(out)]), \
mock.patch.object(rdna2spirv.subprocess, "run", side_effect=spirv_tool), \
redirect_stdout(output):
rdna2spirv.main()
return output.getvalue()
def test_missing_src_is_refused_with_usage_error(self):
with tempfile.TemporaryDirectory() as tmp:
out = os.path.join(tmp, "spirv")
# The .s suffix matters: without isfile() a missing shader path would be trusted as a file.
p = subprocess.run([sys.executable, "-I", TOOL, os.path.join(tmp, "absent.s"), "--out", out],
capture_output=True, text=True, errors="replace")
self.assertEqual(p.returncode, 2, p.stderr)
self.assertIn("not a .s file or a folder of .s files", p.stderr)
self.assertNotIn("Traceback", p.stderr)
self.assertFalse(os.path.exists(out))
def test_directory_ending_in_s_as_src_is_a_directory(self):
# A .s suffix alone is not a shader: an existing directory is still scanned as a folder.
with tempfile.TemporaryDirectory() as tmp:
src = os.path.join(tmp, "shader.s")
os.mkdir(src)
p = subprocess.run([sys.executable, "-I", TOOL, src, "--out", os.path.join(tmp, "spirv")],
capture_output=True, text=True, errors="replace")
self.assertEqual(p.returncode, 0, p.stderr)
self.assertNotIn("Traceback", p.stderr)
self.assertIn("0 shaders:", p.stdout)
def test_non_file_non_dir_entry_is_skipped(self):
# Covers entries that are neither files nor directories (broken symlinks, FIFOs, devices).
# is_file() rejects them; `not is_dir()` would feed one to translate() and crash.
class OddEntry:
name = "foo.s"
path = os.path.join("not-a-file", "foo.s")
def is_file(self):
return False
def is_dir(self):
return False
class FakeScandir:
def __enter__(self):
return iter([OddEntry()])
def __exit__(self, *exc):
return False
with tempfile.TemporaryDirectory() as tmp:
src = os.path.join(tmp, "disasm")
os.mkdir(src)
output = io.StringIO()
with mock.patch.object(sys, "argv", [TOOL, src, "--out", os.path.join(tmp, "spirv")]), \
mock.patch.object(rdna2spirv.os, "scandir", lambda path: FakeScandir()), \
redirect_stdout(output):
rdna2spirv.main()
self.assertIn("0 shaders:", output.getvalue())
def test_directory_ending_in_s_is_skipped(self):
with tempfile.TemporaryDirectory() as tmp:
root = Path(tmp)
src = root / "disasm"
src.mkdir()
(src / "ps_ok.s").write_text("\ts_endpgm\n", encoding="utf-8")
(root / "ps_ok.dxbc").write_bytes(b"DXBC" + bytes(28))
(src / "nested.s").mkdir() # a directory that looks like a shader
out = root / "spirv"
out.mkdir()
stdout = self.run_main(src, out)
self.assertIn("1 pass spirv-val", stdout)
self.assertTrue((out / "ps_ok.spv").is_file())
self.assertFalse((out / "nested.spv").exists())
def test_a_new_output_folder_with_missing_parents_still_works(self):
# The ancestor walk in the --out guard must not reject folders that simply don't exist yet.
with tempfile.TemporaryDirectory() as tmp:
root = Path(tmp)
src = root / "disasm"
src.mkdir()
(src / "ps_ok.s").write_text("\ts_endpgm\n", encoding="utf-8")
(root / "ps_ok.dxbc").write_bytes(b"DXBC" + bytes(28))
out = root / "new" / "deep" / "spirv"
stdout = self.run_main(src, out)
self.assertIn("1 pass spirv-val", stdout)
self.assertTrue((out / "ps_ok.spv").is_file())
def test_non_s_file_as_src_is_refused_with_usage_error(self):
with tempfile.TemporaryDirectory() as tmp:
src = os.path.join(tmp, "notes.txt")
Path(src).write_text("not a shader", encoding="utf-8")
p = subprocess.run([sys.executable, "-I", TOOL, src, "--out", os.path.join(tmp, "spirv")],
capture_output=True, text=True, errors="replace")
self.assertEqual(p.returncode, 2, p.stderr)
self.assertNotIn("Traceback", p.stderr)
class CommandLine(unittest.TestCase):
def test_bad_limit_or_shard_is_refused_before_out_is_made(self):
with tempfile.TemporaryDirectory() as tmp:
src = os.path.join(tmp, "disasm")
os.mkdir(src)
with open(os.path.join(src, "ps_x.s"), "w", encoding="utf-8") as f:
f.write("\ts_endpgm\n")
for args, message in ((["--limit", "0"], "must be at least 1"),
(["--limit", "-5"], "must be at least 1"),
(["--shard", "0/0"], "need n >= 1"),
(["--shard", "9/8"], "need n >= 1 and 0 <= i < n"),
(["--shard", "a/b"], "expected i/n")):
out = os.path.join(tmp, "spirv")
with self.subTest(args=args):
p = subprocess.run([sys.executable, "-I", TOOL, src, "--out", out, *args],
capture_output=True, text=True, errors="replace")
self.assertEqual(p.returncode, 2)
self.assertIn(message, p.stderr)
self.assertNotIn("Traceback", p.stderr)
self.assertFalse(os.path.exists(out)) # 9/8 used to write an empty index.part9.json
class OutputFolderValidation(unittest.TestCase):
"""--out must be a folder path: a file there is a usage error, never a traceback."""
def setUp(self):
self.tmp = Path(self.enterContext(tempfile.TemporaryDirectory()))
self.src = self.tmp / "disasm"
self.src.mkdir()
(self.src / "ps_x.s").write_text("\ts_endpgm\n", encoding="utf-8")
def run_tool(self, out, *extra, timeout=None):
return subprocess.run([sys.executable, "-I", TOOL, str(self.src), "--out", os.fspath(out), *extra],
capture_output=True, text=True, errors="replace", timeout=timeout)
def assert_usage_error(self, p, message):
self.assertEqual(p.returncode, 2, p.stderr)
self.assertNotIn("Traceback", p.stderr)
# argparse prints its usage and one error line; no exception text besides that line.
self.assertEqual(len([line for line in p.stderr.splitlines() if "error:" in line]), 1, p.stderr)
self.assertIn(message, p.stderr)
def test_a_file_at_out_is_refused_and_left_alone(self):
out = self.tmp / "out.txt"
out.write_text("keep", encoding="utf-8")
p = self.run_tool(out)
self.assert_usage_error(p, "is a file, not a folder")
self.assertEqual(out.read_text(encoding="utf-8"), "keep")
def test_an_out_under_a_file_is_refused_and_the_file_is_left_alone(self):
blocker = self.tmp / "blocker.txt"
blocker.write_text("keep", encoding="utf-8")
p = self.run_tool(blocker / "sub")
self.assert_usage_error(p, "cannot create")
self.assertEqual(blocker.read_text(encoding="utf-8"), "keep")
self.assertFalse((blocker / "sub").exists())
def test_a_file_at_out_is_refused_with_merge(self):
out = self.tmp / "out.txt"
out.write_text("keep", encoding="utf-8")
p = self.run_tool(out, "--merge")
self.assert_usage_error(p, "is a file, not a folder")
self.assertEqual(out.read_text(encoding="utf-8"), "keep")
def test_a_file_at_out_with_a_trailing_separator_is_refused(self):
out = self.tmp / "out.txt"
out.write_text("keep", encoding="utf-8")
p = self.run_tool(str(out) + os.sep)
self.assertEqual(p.returncode, 2, p.stderr)
self.assertNotIn("Traceback", p.stderr)
self.assertIn(str(out), p.stderr)
self.assertEqual(out.read_text(encoding="utf-8"), "keep")
def test_an_unmapped_drive_root_is_refused_without_hanging(self):
if os.name != "nt":
self.skipTest("unmapped drive roots are a Windows path case")
drive = next((f"{c}:\\" for c in "ZYXWVUTSRQPONMLKJIHGFEDCBA" if not os.path.exists(f"{c}:\\")), None)
if drive is None:
self.skipTest("every drive letter exists on this machine")
out = os.path.join(drive, "shaders")
p = self.run_tool(out, timeout=30)
# A root that does not exist used to make the ancestor walk spin forever.
self.assert_usage_error(p, "cannot create")
def test_a_symlink_to_a_file_at_out_is_refused(self):
target = self.tmp / "target.txt"
target.write_text("keep", encoding="utf-8")
link = self.tmp / "link-out"
try:
os.symlink(target, link)
except (OSError, NotImplementedError) as e:
self.skipTest(f"symlinks are not available here: {e}")
p = self.run_tool(link)
self.assert_usage_error(p, "is a file, not a folder")
self.assertEqual(target.read_text(encoding="utf-8"), "keep")
class OutputInsideInput(unittest.TestCase):
"""--out equal to the input folder, or inside it, is refused before anything is written (BUILD-1044)."""
def setUp(self):
self.tmp = Path(self.enterContext(tempfile.TemporaryDirectory()))
self.src = self.tmp / "disasm"
self.src.mkdir()
(self.src / "ps_x.s").write_text("\ts_endpgm\n", encoding="utf-8")
(self.tmp / "ps_x.dxbc").write_bytes(b"DXBC" + bytes(28))
def run_tool(self, out):
return subprocess.run([sys.executable, "-I", TOOL, str(self.src), "--out", os.fspath(out)],
capture_output=True, text=True, timeout=120)
def assert_refused(self, out):
p = self.run_tool(out)
self.assertEqual(p.returncode, 2, p.stdout + p.stderr)
self.assertNotIn("Traceback", p.stderr)
self.assertEqual(len(p.stderr.strip().splitlines()), 1, p.stderr)
self.assertTrue(p.stderr.startswith("ERROR: --out "), p.stderr)
self.assertIn("separate output folder", p.stderr)
self.assertFalse((self.src / "ps_x.spvasm").exists())
self.assertFalse((self.src / "ps_x.spv").exists())
self.assertFalse((self.src / "index.json").exists())
def test_out_equal_to_the_input_folder_is_refused(self):
self.assert_refused(self.src)
def test_out_inside_the_input_folder_is_refused(self):
out = self.src / "spirv"
self.assert_refused(out)
self.assertFalse(out.exists()) # refused before mkdir, not left as an empty folder
def test_a_distinct_out_folder_still_translates(self):
out = self.tmp / "spirv"
def spirv_tool(cmd, **kwargs):
if cmd[0] == "spirv-as":
Path(kwargs.get("cwd", ""), cmd[3]).write_bytes(b"new module")
return subprocess.CompletedProcess(cmd, 0, "", "")
with mock.patch.object(sys, "argv", [TOOL, str(self.src), "--out", str(out)]), \
mock.patch.object(rdna2spirv.subprocess, "run", side_effect=spirv_tool):
self.assertIsNone(rdna2spirv.main())
self.assertTrue((out / "ps_x.spv").is_file())
self.assertTrue((out / "index.json").is_file())
class ValidatedOutput(unittest.TestCase):
def setUp(self):
root = Path(self.enterContext(tempfile.TemporaryDirectory()))
self.src = root / "disasm" / "ps_fixture.s"
self.src.parent.mkdir()
self.src.write_text("\ts_endpgm\n", encoding="utf-8")
(root / "ps_fixture.dxbc").write_bytes(b"DXBC" + bytes(28))
self.out = root / "spirv"
self.out.mkdir()
self.module = self.out / "ps_fixture.spv"
self.index = self.out / "index.json"
def translate(self, *, reject_at=None):
def spirv_tool(cmd, **kwargs):
tool = cmd[0]
if tool == "spirv-as":
Path(kwargs.get("cwd", ""), cmd[3]).write_bytes(b"new module")
elif tool == "spirv-val":
self.assertEqual(Path(kwargs.get("cwd", ""), cmd[-1]).read_bytes(), b"new module")
if reject_at is None:
self.assertFalse(self.module.exists(), "the binary was published before validation")
else:
self.fail(f"unexpected tool: {tool}")
rejected = tool == reject_at
return subprocess.CompletedProcess(cmd, int(rejected), "", "synthetic rejection" if rejected else "")
output = io.StringIO()
with mock.patch.object(sys, "argv", [TOOL, str(self.src), "--out", str(self.out)]), \
mock.patch.object(rdna2spirv.subprocess, "run", side_effect=spirv_tool), redirect_stdout(output):
self.exit_code = rdna2spirv.main()
return output.getvalue()
def test_existing_single_s_file_still_translates(self):
self.assertIn("1 pass spirv-val", self.translate())
def test_missing_required_inputs_retire_stale_outputs_and_continue(self):
listing_dir = self.src.parent
root = listing_dir.parent
missing = ("cs_dispatch", "ms_reanchor")
for name in missing:
(root / (name + ".dxbc")).write_bytes((root / "ps_fixture.dxbc").read_bytes())
(listing_dir / (name + ".s")).write_bytes(self.src.read_bytes())
(self.out / (name + ".spv")).write_bytes(b"previous module")
(listing_dir / "ms_reanchor.s").write_bytes(
b"\ts_endpgm ; encoding: [0x00,0x00,0x81,0xbf]\n; invalid instruction encoding\n")
self.index.write_bytes(json.dumps(dict.fromkeys(missing, {})).encode())
self.src = listing_dir
err = io.StringIO()
with redirect_stderr(err):
output = self.translate()
self.assertEqual(self.exit_code, 1)
self.assertIn("3 shaders: 1 translated, 1 pass spirv-val", output)
self.assertIn("2 of 3 selected shaders missing required inputs", err.getvalue())
for name in missing:
self.assertIn(name, err.getvalue())
self.assertFalse((self.out / (name + ".spv")).exists())
self.assertTrue(self.module.exists())
self.assertEqual(set(json.loads(self.index.read_bytes())), {"ps_fixture"})
def test_missing_dispatch_invalidates_completed_shard(self):
src = self.src.with_name("cs_dispatch.s")
src.write_bytes(self.src.read_bytes())
container = self.src.parent.parent / "ps_fixture.dxbc"
shutil.copyfile(container, container.with_name("cs_dispatch.dxbc"))
part = self.out / "index.part0.json"
stale = self.out / "cs_dispatch.spv"
for interrupted in (False, True):
with self.subTest(interrupted=interrupted):
selection = src
if interrupted:
# The compute input retires its module, then the next listing hits a read error.
container.unlink()
container.mkdir()
selection = src.parent
stale.write_bytes(b"old compute")
self.module.write_bytes(b"previous pixel")
part.write_bytes(b'{"cs_dispatch": {}, "ps_fixture": {}}')
self.index.write_bytes(part.read_bytes())
result = subprocess.run([sys.executable, "-I", TOOL, str(selection), "--out", str(self.out),
"--shard", "0/1"], capture_output=True, text=True)
self.assertEqual(result.returncode, 1, result.stderr)
self.assertIn("missing translation input", result.stderr)
if interrupted:
self.assertIn("ps_fixture.dxbc", result.stderr)
self.assertFalse(part.exists())
self.assertFalse(self.index.exists())
self.assertFalse(stale.exists())
self.assertEqual(self.module.read_bytes(), b"previous pixel")
@unittest.skipUnless(shutil.which("spirv-as") and shutil.which("spirv-val"), "requires SPIRV-Tools")
def test_failed_shard_retry_cannot_be_merged(self):
def run(out, *args, hide_tools=False):
env = os.environ.copy()
if hide_tools:
env["PATH"] = str(self.out)
return subprocess.run([sys.executable, "-I", TOOL, str(self.src.parent),
"--out", str(out), *args], env=env, capture_output=True, text=True)
def success(out, *args):
result = run(out, *args)
self.assertEqual(result.returncode, 0, result.stderr)
def snapshot(out):
return {p.name: p.read_bytes() for p in out.iterdir() if p.is_file()}
self.src.with_name("ps_other.s").write_bytes(self.src.read_bytes())
shutil.copyfile(self.src.parent.parent / "ps_fixture.dxbc", self.src.parent.parent / "ps_other.dxbc")
for shard in ("0/2", "1/2"):
success(self.out, "--shard", shard)
success(self.out, "--merge")
baseline = snapshot(self.out)
self.assertEqual(set(json.loads(self.index.read_bytes())), {"ps_fixture", "ps_other"})
for assembler in ("spirv-as", "python"):
with self.subTest(assembler=assembler):
value = 1 if assembler == "spirv-as" else 2
self.src.write_bytes(f"\tv_mov_b32_e32 v0, {value}\n\ts_endpgm\n".encode())
result = run(self.out, "--shard", "0/2", "--assembler", assembler, hide_tools=True)
self.assertFalse(self.index.exists())
merge = run(self.out, "--merge")
self.assertEqual(merge.returncode, 2, merge.stdout)
self.assertEqual(result.returncode, 2, result.stderr)
self.assertIn("SPIRV-Tools", result.stderr)
self.assertFalse((self.out / "index.part0.json").exists())
for name in ("ps_fixture.spv", "ps_other.spv", "shard1.json", "index.part1.json"):
self.assertEqual((self.out / name).read_bytes(), baseline[name], name)
success(self.out, "--shard", "0/2")
success(self.out, "--merge")
self.assertNotEqual(self.module.read_bytes(), baseline[self.module.name])
baseline = snapshot(self.out)
fresh = self.out.with_name("fresh")
for shard in ("0/2", "1/2"):
success(fresh, "--shard", shard)
success(fresh, "--merge")
self.assertEqual(snapshot(self.out), snapshot(fresh))
def test_missing_assembler_is_one_setup_error(self):
err = io.StringIO()
with mock.patch.object(sys, "argv", [TOOL, str(self.src), "--out", str(self.out)]), \
mock.patch.object(rdna2spirv.subprocess, "run",
side_effect=FileNotFoundError(2, "No such file or directory", "spirv-as")), \
redirect_stdout(io.StringIO()), redirect_stderr(err):
code = rdna2spirv.main()
message = err.getvalue()
self.assertEqual(code, 2, message)
self.assertIn("spirv-as", message)
self.assertIn("SPIRV-Tools", message)
self.assertNotIn("Traceback", message)
self.assertEqual(len(message.strip().splitlines()), 1, message)
self.assertFalse(self.module.exists())
self.assertEqual(list(self.out.glob("*.tmp")), [])
def test_missing_validator_does_not_publish_the_module(self):
def spirv_tool(cmd, **kwargs):
if cmd[0] == "spirv-as":
Path(kwargs.get("cwd", ""), cmd[3]).write_bytes(b"new module")
return subprocess.CompletedProcess(cmd, 0, "", "")
raise FileNotFoundError(2, "No such file or directory", "spirv-val")
err = io.StringIO()
with mock.patch.object(sys, "argv", [TOOL, str(self.src), "--out", str(self.out)]), \
mock.patch.object(rdna2spirv.subprocess, "run", side_effect=spirv_tool), \
redirect_stdout(io.StringIO()), redirect_stderr(err):
code = rdna2spirv.main()
message = err.getvalue()
self.assertEqual(code, 2, message)
self.assertIn("spirv-val", message)
self.assertIn("SPIRV-Tools", message)
self.assertNotIn("Traceback", message)
self.assertEqual(len(message.strip().splitlines()), 1, message)
self.assertFalse(self.module.exists())
self.assertFalse(self.index.exists())
self.assertEqual(list(self.out.glob("*.tmp")), [])
def test_validator_rejection_does_not_publish_binary(self):
self.assertIn("INVALID: synthetic rejection", self.translate(reject_at="spirv-val"))
self.assertFalse(self.module.exists())
self.assertEqual(json.loads(self.index.read_text(encoding="utf-8")), {})
self.assertTrue((self.out / "ps_fixture.spvasm").is_file())
self.assertEqual(list(self.out.glob("*.tmp")), [])
def test_rejection_clears_retried_shader_but_preserves_unrelated_output(self):
other = self.out / "cs_other.spv"
other.write_bytes(b"unrelated module")
previous = {"cs_other": {"stage": "cs"}}
for tool in ("spirv-as", "spirv-val"):
with self.subTest(tool=tool):
self.module.write_bytes(b"old module")
self.index.write_text(json.dumps({**previous, "ps_fixture": {"stage": "ps"}}), encoding="utf-8")
self.assertIn("INVALID: synthetic rejection", self.translate(reject_at=tool))
self.assertFalse(self.module.exists())
self.assertEqual(json.loads(self.index.read_text(encoding="utf-8")), previous)
self.assertEqual(other.read_bytes(), b"unrelated module")
def test_unsupported_clears_retried_shader_but_preserves_unrelated_output(self):
# s_barrier is compute-only, so this PS is Unsupported before any SPIR-V tool runs. A module an
# earlier run published for it must not survive: the index is make_game.py's proof of completion.
self.src.write_text("\ts_barrier\n\ts_endpgm\n", encoding="utf-8")
other = self.out / "cs_other.spv"
other.write_bytes(b"unrelated module")
previous = {"cs_other": {"stage": "cs"}}
self.module.write_bytes(b"old module")
self.index.write_text(json.dumps({**previous, "ps_fixture": {"stage": "ps"}}), encoding="utf-8")
output = self.translate()
self.assertIn("s_barrier outside compute", output)
self.assertFalse(self.module.exists())
self.assertEqual(json.loads(self.index.read_text(encoding="utf-8")), previous)
self.assertEqual(other.read_bytes(), b"unrelated module")
self.assertEqual(list(self.out.glob("*.tmp")), [])
def test_translator_error_clears_retried_shader(self):
self.module.write_bytes(b"old module")
self.index.write_text(json.dumps({"ps_fixture": {"stage": "ps"}}), encoding="utf-8")
output = io.StringIO()
with mock.patch.object(rdna2spirv, "translate", side_effect=ValueError("synthetic translator error")), \
mock.patch.object(sys, "argv", [TOOL, str(self.src), "--out", str(self.out)]), \
redirect_stdout(output):
rdna2spirv.main()
self.assertIn("translator error ValueError", output.getvalue())
self.assertFalse(self.module.exists())
self.assertEqual(json.loads(self.index.read_text(encoding="utf-8")), {})
def test_translator_bug_in_one_program_does_not_end_the_run(self):
for error in (AssertionError("synthetic assert"), TypeError("synthetic"), AttributeError("synthetic")):
with self.subTest(error=type(error).__name__):
self.module.write_bytes(b"old module")
self.index.write_text(json.dumps({"ps_fixture": {"stage": "ps"}}), encoding="utf-8")
output = io.StringIO()
with mock.patch.object(rdna2spirv, "translate", side_effect=error), \
mock.patch.object(sys, "argv", [TOOL, str(self.src), "--out", str(self.out)]), \
redirect_stdout(output):
rdna2spirv.main()
self.assertIn(f"translator error {type(error).__name__}", output.getvalue())
self.assertFalse(self.module.exists())
self.assertEqual(json.loads(self.index.read_text(encoding="utf-8")), {})
def test_validation_success_publishes_binary_and_metadata(self):
self.assertIn("1 pass spirv-val", self.translate())
self.assertEqual(self.module.read_bytes(), b"new module")
self.assertIn("ps_fixture", json.loads(self.index.read_text(encoding="utf-8")))
class EmptyShader(unittest.TestCase):
"""A .s with no tab-indented instruction parses to an empty shader.
It used to translate to a module that passes spirv-val, lands in index.json and does no work at all, which is
what a hand-written or re-indented .s looks like: found while writing a fixture with spaces instead of tabs.
"""
def test_no_instructions_is_reported_and_no_module_is_published(self):
with tempfile.TemporaryDirectory() as tmp:
src = os.path.join(tmp, "disasm")
os.mkdir(src)
with open(os.path.join(src, "ps_empty.s"), "w", encoding="utf-8") as f:
f.write("; ps_empty code 0x0..0x0 (synthetic), gfx1013, 0 invalid words\n s_endpgm\n")
with open(os.path.join(tmp, "ps_empty.dxbc"), "wb") as f:
f.write(b"DXBC" + bytes(28))
out = os.path.join(tmp, "spirv")
p = subprocess.run([sys.executable, "-I", TOOL, src, "--out", out], capture_output=True, text=True, errors="replace")
self.assertEqual(p.returncode, 0, p.stderr)
self.assertIn("0 translated", p.stdout, p.stdout)
self.assertIn("no instructions (nothing tab-indented?)", p.stdout, p.stdout)
self.assertFalse(os.path.exists(os.path.join(out, "ps_empty.spv")))
class PopsInvocationInterlock(unittest.TestCase):
"""SHADER-350: POPS pixel interlock shaders must emit OpBeginInvocationInterlockEXT and OpEndInvocationInterlockEXT."""
def test_straight_line_pops_pixel_shader_emits_begin_and_end_interlock(self):
gen = rdna2spirv.Gen("ps")
gen.uses_pops = True
gen.body = ["OpNoOp"]
spv = rdna2spirv.assemble(gen)
self.assertEqual(spv.count("OpBeginInvocationInterlockEXT"), 1)
self.assertEqual(spv.count("OpEndInvocationInterlockEXT"), 1)
self.assertLess(spv.index("OpBeginInvocationInterlockEXT"), spv.index("OpEndInvocationInterlockEXT"))
def test_branching_pops_pixel_shader_emits_begin_and_end_interlock(self):
gen = rdna2spirv.Gen("ps")
gen.uses_pops = True
gen.blocks = [["OpNoOp"]]
gen.succ = [[]]
spv = rdna2spirv.assemble(gen)
self.assertEqual(spv.count("OpBeginInvocationInterlockEXT"), 1)
self.assertEqual(spv.count("OpEndInvocationInterlockEXT"), 1)
self.assertLess(spv.index("OpBeginInvocationInterlockEXT"), spv.index("OpEndInvocationInterlockEXT"))
def test_non_pops_pixel_shader_omits_interlock_ops(self):
gen = rdna2spirv.Gen("ps")
gen.uses_pops = False
gen.body = ["OpNoOp"]
spv = rdna2spirv.assemble(gen)
self.assertEqual(spv.count("OpBeginInvocationInterlockEXT"), 0)
self.assertEqual(spv.count("OpEndInvocationInterlockEXT"), 0)
if __name__ == "__main__":
unittest.main()
|