From c3ec925623345b5ee2e923e2e83e887fbb68505b Mon Sep 17 00:00:00 2001 From: whoalin1 <207920491+whoalin1@users.noreply.github.com> Date: Thu, 26 Nov 2026 02:19:57 +0200 Subject: [PATCH 1/2] rdna2spirv: cache shader stages per carve root stage_of cached the first index.json it read in process-global _STAGES and reused it for every later shader. A process that translates programs from two carve roots can therefore pick the first root's stage for a shader in the second root, changing stage-dependent codegen. Keep the process-local cache, but key it by the resolved index.json path. The synthetic two-root regression test fails on main with the second root reported as the first root's stage and passes with the fix. Generated with [Devin](https://devin.ai) Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com> --- tests/test_rdna2spirv_stage_cache.py | 54 ++++++++++++++++++++++++++++ tools/rdna2spirv.py | 13 ++++--- 2 files changed, 63 insertions(+), 4 deletions(-) create mode 100644 tests/test_rdna2spirv_stage_cache.py diff --git a/tests/test_rdna2spirv_stage_cache.py b/tests/test_rdna2spirv_stage_cache.py new file mode 100644 index 0000000000..5ea20919b4 --- /dev/null +++ b/tests/test_rdna2spirv_stage_cache.py @@ -0,0 +1,54 @@ +"""Regression tests for rdna2spirv.stage_of's process-local carve-index cache. + +Standard library only; no game data. + + python -I tests/test_rdna2spirv_stage_cache.py +""" +import json +import os +import sys +import tempfile +import unittest + +ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) +sys.path.insert(0, os.path.join(ROOT, "tools")) +import rdna2spirv # noqa: E402 + + +def tree(root, stage): + """One synthetic carve root with /index.json and /disasm/shader.s.""" + disasm = os.path.join(root, "disasm") + os.makedirs(disasm) + with open(os.path.join(root, "index.json"), "w", encoding="utf-8", newline="\n") as f: + json.dump([{"file": "shader.dxbc", "stage": stage}], f) + listing = os.path.join(disasm, "shader.s") + with open(listing, "w", encoding="utf-8", newline="\n") as f: + f.write("\ts_endpgm\n") + return listing + + +class StageOfCacheTest(unittest.TestCase): + def setUp(self): + self.old = rdna2spirv._STAGES + rdna2spirv._STAGES = None + + def tearDown(self): + rdna2spirv._STAGES = self.old + + def test_second_root_does_not_reuse_the_first_roots_index(self): + with tempfile.TemporaryDirectory() as tmp: + first = tree(os.path.join(tmp, "first"), "ps") + second = tree(os.path.join(tmp, "second"), "cs") + self.assertEqual(rdna2spirv.stage_of("shader", first), "ps") + self.assertEqual(rdna2spirv.stage_of("shader", second), "cs") + + def test_same_root_is_still_cached(self): + with tempfile.TemporaryDirectory() as tmp: + listing = tree(os.path.join(tmp, "root"), "ms") + self.assertEqual(rdna2spirv.stage_of("shader", listing), "ms") + os.remove(os.path.join(os.path.dirname(listing), "..", "index.json")) + self.assertEqual(rdna2spirv.stage_of("shader", listing), "ms") + + +if __name__ == "__main__": + unittest.main() diff --git a/tools/rdna2spirv.py b/tools/rdna2spirv.py index 13fbd034db..e40a9020cb 100644 --- a/tools/rdna2spirv.py +++ b/tools/rdna2spirv.py @@ -17023,14 +17023,19 @@ def stage_of(name, path): """Shader stage from the carve index (the DXIL program header in each container), not the name.""" global _STAGES if _STAGES is None: - import json - idx = os.path.join(os.path.dirname(os.path.dirname(os.path.realpath(path))), "index.json") _STAGES = {} + idx = os.path.join(os.path.dirname(os.path.dirname(os.path.realpath(path))), "index.json") + key = os.path.normcase(os.path.realpath(idx)) + stages = _STAGES.get(key) + if stages is None: + import json + stages = {} if os.path.isfile(idx): with open(idx, encoding="utf-8") as f: for e in json.load(f): - _STAGES[e["file"][:-5]] = e["stage"] - return _STAGES.get(name) or (name[:2].lower() if name[2:3] == "_" else "?") + stages[e["file"][:-5]] = e["stage"] + _STAGES[key] = stages + return stages.get(name) or (name[:2].lower() if name[2:3] == "_" else "?") # Wave64 markers in the instruction text. Besides EXEC/VCC forms, 64-bit lane-mask SOP1/SOP2 ops From c5c367a2a85e25e514a99107e66443d002257938 Mon Sep 17 00:00:00 2001 From: whoalin1 <207920491+whoalin1@users.noreply.github.com> Date: Thu, 26 Nov 2026 06:46:45 +0200 Subject: [PATCH 2/2] rdna2spirv: do not cache a missing stage index A carve root without index.json cached an empty stage table for the rest of the process, so creating the index later still left every shader at its fallback stage. Cache only successful index reads; a missing file is probed again on the next lookup. Add a synthetic missing-then-created regression test on top of the per-root cache coverage. Depends on: #2154 AI-assisted. --- tests/test_rdna2spirv_stage_cache.py | 13 +++++++++++++ tools/rdna2spirv.py | 2 +- 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/tests/test_rdna2spirv_stage_cache.py b/tests/test_rdna2spirv_stage_cache.py index 5ea20919b4..2ec7af1264 100644 --- a/tests/test_rdna2spirv_stage_cache.py +++ b/tests/test_rdna2spirv_stage_cache.py @@ -49,6 +49,19 @@ def test_same_root_is_still_cached(self): os.remove(os.path.join(os.path.dirname(listing), "..", "index.json")) self.assertEqual(rdna2spirv.stage_of("shader", listing), "ms") + def test_a_missing_index_is_not_cached(self): + with tempfile.TemporaryDirectory() as tmp: + root = os.path.join(tmp, "root") + disasm = os.path.join(root, "disasm") + os.makedirs(disasm) + listing = os.path.join(disasm, "shader.s") + with open(listing, "w", encoding="utf-8", newline="\n") as f: + f.write("\ts_endpgm\n") + self.assertEqual(rdna2spirv.stage_of("shader", listing), "?") + with open(os.path.join(root, "index.json"), "w", encoding="utf-8", newline="\n") as f: + json.dump([{"file": "shader.dxbc", "stage": "cs"}], f) + self.assertEqual(rdna2spirv.stage_of("shader", listing), "cs") + if __name__ == "__main__": unittest.main() diff --git a/tools/rdna2spirv.py b/tools/rdna2spirv.py index e40a9020cb..d1529699a7 100644 --- a/tools/rdna2spirv.py +++ b/tools/rdna2spirv.py @@ -17034,7 +17034,7 @@ def stage_of(name, path): with open(idx, encoding="utf-8") as f: for e in json.load(f): stages[e["file"][:-5]] = e["stage"] - _STAGES[key] = stages + _STAGES[key] = stages return stages.get(name) or (name[:2].lower() if name[2:3] == "_" else "?")