rdna2spirv: cache shader stages per carve root #2154

Open
github-bot wants to merge 2 commits from github-bot/gh-2154 into main AGit
Owner

@⁠whoalin1 on GitHub · 2026-11-26 00:20 UTC · original
whoalin1:shader-stage-cache-crossroot → main · +76 −4 · 2 files · 2 commits · open

Claimed in the coordination issue as: SHADER-STAGE-CACHE-CROSS-ROOT (#1608)

What and why

rdna2spirv.stage_of cached the first carve index.json it read in process-global _STAGES and reused that one mapping for every later shader in the process. When one process translates listings from multiple carve roots, a shader in the second root can inherit the first root's stage, changing stage-dependent translation decisions. This is a concrete cross-shader mutable-state hazard in the area of jean-melikian's shard-only invalid-OpSelect report.

The process-local cache is kept, but is now keyed by the resolved index.json path. Existing tests that reset _STAGES = None remain compatible.

Regression test

tests/test_rdna2spirv_stage_cache.py builds two synthetic roots with the same shader stem but different stages. Before the fix it failed with:

AssertionError: 'ps' != 'cs'

After the fix the second root resolves to its own cs stage. It also checks that repeated lookups in one root still use the cache.

Tested

Windows 11, Python 3.14, under the machine coordination lock:

  • python -m compileall -q tools/rdna2spirv.py tests/test_rdna2spirv_stage_cache.py — pass.
  • python -I tests/test_rdna2spirv_stage_cache.py — 3 tests, OK.
  • python -I tests/test_bvh_shaders_stage.py — 13 tests, OK.
  • python -I tests/test_exe_shaders_stage.py — 11 tests, OK.
  • python -I tests/test_shader_library.py — 22 tests, OK (1 skipped).

No GPU or game data used. AI-assisted with Devin.

Consolidated follow-up

This branch now also contains the missing-index follow-up formerly opened as #2194: only successful index.json reads are cached, so an index created later in the same process is probed again instead of being hidden by a cached empty table. The added regression covers missing-then-created index.json; the branch head is exactly the previously reviewed #2194 head (c5c367a2a).

**@⁠whoalin1** on GitHub · 2026-11-26 00:20 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/2154) `whoalin1:shader-stage-cache-crossroot` → `main` · +76 −4 · 2 files · 2 commits · open Claimed in the coordination issue as: SHADER-STAGE-CACHE-CROSS-ROOT (https://git.gtahub.st/github/ubiquitarianism--leonida/issues/1608) ### What and why `rdna2spirv.stage_of` cached the first carve `index.json` it read in process-global `_STAGES` and reused that one mapping for every later shader in the process. When one process translates listings from multiple carve roots, a shader in the second root can inherit the first root's stage, changing stage-dependent translation decisions. This is a concrete cross-shader mutable-state hazard in the area of jean-melikian's shard-only invalid-`OpSelect` report. The process-local cache is kept, but is now keyed by the resolved `index.json` path. Existing tests that reset `_STAGES = None` remain compatible. ### Regression test `tests/test_rdna2spirv_stage_cache.py` builds two synthetic roots with the same shader stem but different stages. Before the fix it failed with: ```text AssertionError: 'ps' != 'cs' ``` After the fix the second root resolves to its own `cs` stage. It also checks that repeated lookups in one root still use the cache. ### Tested Windows 11, Python 3.14, under the machine coordination lock: - `python -m compileall -q tools/rdna2spirv.py tests/test_rdna2spirv_stage_cache.py` — pass. - `python -I tests/test_rdna2spirv_stage_cache.py` — 3 tests, OK. - `python -I tests/test_bvh_shaders_stage.py` — 13 tests, OK. - `python -I tests/test_exe_shaders_stage.py` — 11 tests, OK. - `python -I tests/test_shader_library.py` — 22 tests, OK (1 skipped). No GPU or game data used. AI-assisted with Devin. ### Consolidated follow-up This branch now also contains the missing-index follow-up formerly opened as #2194: only successful `index.json` reads are cached, so an index created later in the same process is probed again instead of being hidden by a cached empty table. The added regression covers missing-then-created `index.json`; the branch head is exactly the previously reviewed #2194 head (`c5c367a2a`).
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>
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 10:20:28 +00:00
Author
Owner

@⁠whoalin1 on GitHub · 2026-11-26 00:28 UTC · original

Full semantic verification on the PR head c3ec92562 under the machine lock: 495 files, 6333 tests, 0 files failed (12 jobs). Focused compileall plus stage/cache/library unit tests were already green before opening. AI-assisted (Devin).

**@⁠whoalin1** on GitHub · 2026-11-26 00:28 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/2154#issuecomment-7178798558) Full semantic verification on the PR head `c3ec92562` under the machine lock: `495 files, 6333 tests, 0 files failed` (12 jobs). Focused compileall plus stage/cache/library unit tests were already green before opening. AI-assisted (Devin).
Author
Owner

@⁠qhukz13 on GitHub · 2026-11-26 00:40 UTC · original

Checked at c3ec92562 on Linux (Python 3).

  • The new key is compatible with the rest of the tree: every other use of rdna2spirv._STAGES (test_bvh_shaders_stage.py, test_exe_shaders_stage.py, test_shader_library.py) only resets it to None, which still works; nothing reads or fills the old name -> stage form directly.
  • test_rdna2spirv_stage_cache.py, test_bvh_shaders_stage.py, test_exe_shaders_stage.py, test_shader_library.py: all OK here.
  • One thing this keeps from before, not new: a missing index.json is cached as an empty dict for its key, so if the carve writes it later in the same process, stage_of falls back to the name guess for that root until _STAGES is reset. Not needed for this PR's case; just noting it in case someone hits it with an in-process carve + translate.

Looks right to me.

AI-assisted (Claude Code).

**@⁠qhukz13** on GitHub · 2026-11-26 00:40 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/2154#issuecomment-7179003013) Checked at `c3ec92562` on Linux (Python 3). - The new key is compatible with the rest of the tree: every other use of `rdna2spirv._STAGES` (`test_bvh_shaders_stage.py`, `test_exe_shaders_stage.py`, `test_shader_library.py`) only resets it to `None`, which still works; nothing reads or fills the old name -> stage form directly. - `test_rdna2spirv_stage_cache.py`, `test_bvh_shaders_stage.py`, `test_exe_shaders_stage.py`, `test_shader_library.py`: all OK here. - One thing this keeps from before, not new: a missing `index.json` is cached as an empty dict for its key, so if the carve writes it later in the same process, `stage_of` falls back to the name guess for that root until `_STAGES` is reset. Not needed for this PR's case; just noting it in case someone hits it with an in-process carve + translate. Looks right to me. AI-assisted (Claude Code).
Author
Owner

@⁠whoalin1 on GitHub · 2026-11-26 09:19 UTC · original

Thanks for the detailed check. The missing-index edge you called out is carried in the stacked follow-up #2194: it caches only successful index reads, so a root whose index.json appears later in the same process is probed again instead of keeping the empty table until _STAGES is reset. The regression case is covered in tests/test_rdna2spirv_stage_cache.py. AI-assisted with Devin.

**@⁠whoalin1** on GitHub · 2026-11-26 09:19 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/2154#issuecomment-7187775871) Thanks for the detailed check. The missing-index edge you called out is carried in the stacked follow-up #2194: it caches only successful index reads, so a root whose `index.json` appears later in the same process is probed again instead of keeping the empty table until `_STAGES` is reset. The regression case is covered in `tests/test_rdna2spirv_stage_cache.py`. AI-assisted with Devin.
Author
Owner

@⁠real-rusher on GitHub · 2026-11-26 10:20 UTC · original

Verified on main a73a57e1 (Ubuntu 24.04 x86-64, Python 3.13, no GPU). Merges cleanly. python -I tests/test_rdna2spirv_stage_cache.py: 2 tests OK with this PR; the same file against main's translator: 1 failure, so it fails on main and passes with the change. compileall clean.

#2194 (do not cache a missing stage index) touches the same cache: the two merge together cleanly, and with both merged, each PR's version of tests/test_rdna2spirv_stage_cache.py passes.

AI-assisted: yes (Claude Code)

**@⁠real-rusher** on GitHub · 2026-11-26 10:20 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/2154#issuecomment-7189230032) Verified on main a73a57e1 (Ubuntu 24.04 x86-64, Python 3.13, no GPU). Merges cleanly. python -I tests/test_rdna2spirv_stage_cache.py: 2 tests OK with this PR; the same file against main's translator: 1 failure, so it fails on main and passes with the change. compileall clean. #2194 (do not cache a missing stage index) touches the same cache: the two merge together cleanly, and with both merged, each PR's version of tests/test_rdna2spirv_stage_cache.py passes. AI-assisted: yes (Claude Code)
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.
Author
Owner

@⁠wavehs on GitHub · 2026-11-26 19:48 UTC · original · reviewed

macOS arm64 check of #2154 (rdna2spirv stage cache per carve root). A small tool change with a focused test; runs here without a GPU.

Tested head c5c367a2a merged onto main bdbc7e923 as merge 0a5f09382 (clean, no conflicts).

Environment: Apple M3 Pro arm64, macOS 27.2.1, CPython 3.15.2.

On the merge

  • python -I tests/test_rdna2spirv_stage_cache.py: 3 tests OK - two roots with the same shader name get their own stages (ps then cs), the same root stays cached (deleting its index.json afterwards still answers), and a missing index is not cached (creating it later is picked up).
  • Whole semantic suite: 500 files, 6367 tests, 0 files failed - no change, as expected.
  • The cache now keys on os.path.normcase(os.path.realpath(index.json)), so two spellings of one root share an entry on Windows and two carve roots cannot cross-contaminate. The bug was real in the in-process users (tools/image_bvh_census.py:753, tools/ptr_truncation_census.py:147, tools/wave32_pixel_scan.py:232, tools/wave64_graphics_scan.py:121): a census over several roots would answer the second root's same-named shaders from the first root's index.

Negative control (new test kept, tools/rdna2spirv.py reverted to main)

  • FAILED (failures=2) in the new file: the second-root case and the missing-index case both fail on the old global cache, which is exactly the regression.

Nits

  • The cache key is the path, not the file's mtime/size: if a process rewrites index.json after a first stage_of call, the answer stays stale. None of the current callers do that (they are one-shot census/scan tools, and _translate reads it once), so this is a note for future reuse rather than a defect; a comment on the function saying "the index is assumed immutable for the process lifetime" would make the assumption explicit.
  • normcase only folds case on Windows. macOS's default filesystem is case-insensitive too, so two case-different spellings of the same root create two cached copies here; harmless (an extra parse), and folding it would need more than normcase.
  • The module-global _STAGES reset in older tests (tests/test_bvh_shaders_stage.py:78, tests/test_exe_shaders_stage.py:87) still works because it is re-created as a dict; nothing to change.

Not run: the game corpus (needs a copy), and the census tools over multiple real roots, which is the case the fix targets - the new test reproduces it synthetically with four files.

python -I scripts/check_no_game_data.py on both changed files: clean.

No game files. AI-assisted.

**@⁠wavehs** on GitHub · 2026-11-26 19:48 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/2154#pullrequestreview-6079748288) · **reviewed** **macOS arm64 check of #2154** (rdna2spirv stage cache per carve root). A small tool change with a focused test; runs here without a GPU. Tested head `c5c367a2a` merged onto `main` `bdbc7e923` as merge `0a5f09382` (clean, no conflicts). Environment: Apple M3 Pro arm64, macOS 27.2.1, CPython 3.15.2. **On the merge** - `python -I tests/test_rdna2spirv_stage_cache.py`: 3 tests OK - two roots with the same shader name get their own stages (`ps` then `cs`), the same root stays cached (deleting its `index.json` afterwards still answers), and a missing index is not cached (creating it later is picked up). - Whole semantic suite: `500 files, 6367 tests, 0 files failed` - no change, as expected. - The cache now keys on `os.path.normcase(os.path.realpath(index.json))`, so two spellings of one root share an entry on Windows and two carve roots cannot cross-contaminate. The bug was real in the in-process users (`tools/image_bvh_census.py:753`, `tools/ptr_truncation_census.py:147`, `tools/wave32_pixel_scan.py:232`, `tools/wave64_graphics_scan.py:121`): a census over several roots would answer the second root's same-named shaders from the first root's index. **Negative control (new test kept, `tools/rdna2spirv.py` reverted to `main`)** - `FAILED (failures=2)` in the new file: the second-root case and the missing-index case both fail on the old global cache, which is exactly the regression. **Nits** - The cache key is the path, not the file's mtime/size: if a process rewrites `index.json` after a first `stage_of` call, the answer stays stale. None of the current callers do that (they are one-shot census/scan tools, and `_translate` reads it once), so this is a note for future reuse rather than a defect; a comment on the function saying "the index is assumed immutable for the process lifetime" would make the assumption explicit. - `normcase` only folds case on Windows. macOS's default filesystem is case-insensitive too, so two case-different spellings of the same root create two cached copies here; harmless (an extra parse), and folding it would need more than `normcase`. - The module-global `_STAGES` reset in older tests (`tests/test_bvh_shaders_stage.py:78`, `tests/test_exe_shaders_stage.py:87`) still works because it is re-created as a dict; nothing to change. **Not run**: the game corpus (needs a copy), and the census tools over multiple real roots, which is the case the fix targets - the new test reproduces it synthetically with four files. `python -I scripts/check_no_game_data.py` on both changed files: clean. No game files. AI-assisted.
This pull request can be merged automatically.
This branch is out-of-date with the base branch
You are not authorized to merge this pull request.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin '+refs/pull/2154/head:github-bot/gh-2154'
git switch 'github-bot/gh-2154'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-2154'
git switch 'github-bot/gh-2154'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-2154'
git switch 'github-bot/gh-2154'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-2154'
git switch 'main'
git merge --squash 'github-bot/gh-2154'
git switch 'main'
git merge --ff-only 'github-bot/gh-2154'
git switch 'main'
git merge 'github-bot/gh-2154'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No description provided.