Merge pull request #1252 from Shadman PC

This commit is contained in:
ItsNotRexed
2026-11-22 04:07:53 -05:00
3 changed files with 75 additions and 6 deletions

View File

@ -30,7 +30,7 @@ def _bare_open_calls(path: pathlib.Path) -> list[tuple[int, str]]:
class ResourceHygieneTest(unittest.TestCase):
def test_no_unmanaged_open_calls(self):
for rel in ("tools/rdna2spirv.py", "tools/amd_swizzle.py", "tools/awc_opus.py"):
for rel in ("tools/rdna2spirv.py", "tools/amd_swizzle.py", "tools/awc_opus.py", "tools/wave64_graphics_scan.py"):
with self.subTest(file=rel):
bare = _bare_open_calls(ROOT / rel)
self.assertEqual(bare, [], f"unmanaged open() calls in {rel}: {bare}")

View File

@ -2,6 +2,8 @@
python -I tests/test_wave64_graphics_scan.py -v
"""
import contextlib
import io
import json
import os
from pathlib import Path
@ -129,12 +131,67 @@ class ScanTests(unittest.TestCase):
report = scan.scan_directory(directory)
with open(out, "w", encoding="utf-8") as f:
json.dump(report, f)
blob = open(out, encoding="utf-8").read()
with open(out, encoding="utf-8") as f:
blob = f.read()
self.assertNotIn(".s", blob)
self.assertNotIn("ps_w64a", blob)
self.assertNotIn("comment", blob)
self.assertIn("v_mbcnt_hi_u32_b32", blob)
class CliTests(unittest.TestCase):
def test_missing_source_directory_raises_error(self):
with self.assertRaises(SystemExit) as cm:
with contextlib.redirect_stderr(io.StringIO()):
scan.main(["nonexistent_shaders_dir_xyz"])
self.assertNotEqual(cm.exception.code, 0)
def test_out_is_directory_rejected(self):
with tempfile.TemporaryDirectory() as src, tempfile.TemporaryDirectory() as out_dir:
with self.assertRaises(SystemExit) as cm:
with contextlib.redirect_stderr(io.StringIO()):
scan.main([src, "--out", out_dir])
self.assertNotEqual(cm.exception.code, 0)
def test_out_parent_directory_missing_rejected(self):
with tempfile.TemporaryDirectory() as src:
missing_dir = os.path.join(src, "no_such_folder", "out.json")
with self.assertRaises(SystemExit) as cm:
with contextlib.redirect_stderr(io.StringIO()):
scan.main([src, "--out", missing_dir])
self.assertNotEqual(cm.exception.code, 0)
def test_out_same_as_source_rejected(self):
with tempfile.TemporaryDirectory() as src:
with self.assertRaises(SystemExit) as cm:
with contextlib.redirect_stderr(io.StringIO()):
scan.main([src, "--out", src])
self.assertNotEqual(cm.exception.code, 0)
def test_cli_execution_with_out_json(self):
with tempfile.TemporaryDirectory() as src, tempfile.TemporaryDirectory() as outdir:
for name, text in (PS_W64_SENSITIVE, VS_W64):
with open(os.path.join(src, name), "w", encoding="utf-8") as f:
f.write(text)
out = os.path.join(outdir, "report.json")
stdout = io.StringIO()
with contextlib.redirect_stdout(stdout):
code = scan.main([src, "--out", out])
self.assertEqual(code, 0)
self.assertIn("ps: 1 shaders, 1 wave64", stdout.getvalue())
self.assertIn("wrote " + out, stdout.getvalue())
with open(out, encoding="utf-8") as f:
saved = json.load(f)
self.assertIn("per_stage", saved)
self.assertIn("other_stages", saved)
self.assertEqual(saved["per_stage"]["ps"]["shaders"], 1)
self.assertEqual(saved["per_stage"]["vs"]["shaders"], 1)
with open(out, encoding="utf-8") as f:
blob = f.read()
self.assertNotIn(".s", blob)
self.assertNotIn("ps_w64a", blob)
self.assertIn("v_mbcnt_hi_u32_b32", blob)
if __name__ == "__main__":
unittest.main(verbosity=2)

View File

@ -126,14 +126,25 @@ def scan_directory(disasm):
return {"per_stage": per_stage, "other_stages": other}
def main():
def main(argv=None) -> int:
ap = argparse.ArgumentParser(
description="Count candidate cross-half wave ops in wave64 graphics shaders (counts only, no names)")
ap.add_argument("src", help="<work>/shaders/dxbc/disasm from the shaders stage")
ap.add_argument("--out", help="write the same aggregates to a JSON file")
args = ap.parse_args()
args = ap.parse_args(argv)
if not os.path.isdir(args.src):
raise SystemExit("not a directory: %s" % args.src)
ap.error(f"not a directory: {args.src}")
if args.out:
if os.path.isdir(args.out):
ap.error(f"{args.out} is a directory, not a JSON file")
out_dir = os.path.dirname(os.path.abspath(args.out))
if not os.path.isdir(out_dir):
ap.error(f"output directory does not exist: {out_dir}")
try:
if os.path.samefile(os.path.abspath(args.src), os.path.abspath(args.out)):
ap.error("output must not overwrite the source directory")
except (FileNotFoundError, OSError):
pass
report = scan_directory(args.src)
for stage, b in report["per_stage"].items():
occ = ", ".join("%s x%d" % (op, n) for op, n in b["occurrences"].items()) or "none"
@ -150,7 +161,8 @@ def main():
with open(args.out, "w", encoding="utf-8") as f:
json.dump(report, f, indent=1)
print("wrote %s" % args.out)
return 0
if __name__ == "__main__":
main()
sys.exit(main())