rdna2spirv: cache shader stages per carve root #2154

Merged
leonida/mandy
whoalin1 merged 3 commits from shader-stage-cache-crossroot into main 2026-11-29 15:44:05 +00:00

Pull request successfully merged and closed

whoalin1 commented 2026-11-26 00:20:15 +00:00

Claimed in the coordination issue as: SHADER-STAGE-CACHE-CROSS-ROOT (https://github.com/ubiquitarianism/leonida/issues/1608#issuecomment-7178575111)

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).

Claimed in the coordination issue as: SHADER-STAGE-CACHE-CROSS-ROOT (https://github.com/ubiquitarianism/leonida/issues/1608#issuecomment-7178575111) ### 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`).
whoalin1 commented 2026-11-26 00:28:43 +00:00

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).

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).
qhukz13 commented 2026-11-26 00:40:41 +00:00

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).

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).
whoalin1 commented 2026-11-26 09:19:06 +00:00

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.

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.
real-rusher commented 2026-11-26 10:20:14 +00:00

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)

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)
wavehs reviewed 2026-11-26 19:48:17 +00:00
wavehs left a comment
No description provided.
**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.
zmasi commented 2026-11-28 11:09:08 +00:00

#2154 | head c5c367a2a85e25e514a99107e66443d002257938 (merged cleanly with main 039870ea2) | python -I tests/test_rdna2spirv_stage_cache.py | red on main: FAILED (failures=2) ('ps' != 'cs' across distinct carve roots and '?' != 'cs' after missing index.json) | green on PR+main: 3 tests OK | Windows 11, Python 3.12.10 | AI-assisted: yes

Audited stage_of per-carve-root _STAGES caching (os.path.normcase(os.path.realpath(idx)) on Windows 11 NTFS, skipping _STAGES[key] assignment when index.json is absent) in tools/rdna2spirv.py and tests/test_rdna2spirv_stage_cache.py. Also verified 0 merge conflicts with #2011 and #2239, and passed tests/test_ci_registration.py (18 OK), tests/test_text_write_line_endings.py (3 OK), tests/test_runner_calls_all_tests.py (3 OK), and scripts/check_no_game_data.py.

#2154 | head c5c367a2a85e25e514a99107e66443d002257938 (merged cleanly with main 039870ea2) | `python -I tests/test_rdna2spirv_stage_cache.py` | red on main: `FAILED (failures=2)` (`'ps' != 'cs'` across distinct carve roots and `'?' != 'cs'` after missing `index.json`) | green on PR+main: `3 tests OK` | Windows 11, Python 3.12.10 | AI-assisted: yes Audited `stage_of` per-carve-root `_STAGES` caching (`os.path.normcase(os.path.realpath(idx))` on Windows 11 NTFS, skipping `_STAGES[key]` assignment when `index.json` is absent) in `tools/rdna2spirv.py` and `tests/test_rdna2spirv_stage_cache.py`. Also verified `0` merge conflicts with #2011 and #2239, and passed `tests/test_ci_registration.py` (`18 OK`), `tests/test_text_write_line_endings.py` (`3 OK`), `tests/test_runner_calls_all_tests.py` (`3 OK`), and `scripts/check_no_game_data.py`.
zmasi commented 2026-11-29 02:12:46 +00:00

#2154 | head b001cfa5ee78c2ba55c3e896c8f49e5eb976827b (merged cleanly onto origin/main 791899f652) | Windows 11 (10.0.26200.9974), Python 3.12.10 | AI-assisted: yes

Independent Verification (Windows 11 + NTFS)

  • Diff & safety audit: Inspected 100% of the diff on current head b001cfa5ee across tools/rdna2spirv.py (keying _STAGES per carve root via os.path.normcase(os.path.realpath(idx)) and only caching when os.path.isfile(idx) succeeds) and tests/test_rdna2spirv_stage_cache.py. python -I scripts/check_no_game_data.py tools/rdna2spirv.py tests/test_rdna2spirv_stage_cache.py passed (rc=0).
  • Red -> Green on Windows 11 + NTFS:
    • Red (origin/main 791899f652 tools/rdna2spirv.py): Running #2154's tests/test_rdna2spirv_stage_cache.py -v against unpatched tools/rdna2spirv.py failed 2 of 3 tests (FAILED (failures=2): test_a_missing_index_is_not_cached and test_second_root_does_not_reuse_the_first_roots_index, 'ps' != 'cs').
    • Green (#2154 + origin/main):
      • python -I tests/test_rdna2spirv_stage_cache.py -v -> 3/3 passed (OK, 0.024s)
      • NTFS case-folding & directory junction (mklink /J) probe: Verified on Windows 11 NTFS that calling rdna2spirv.stage_of(...) through different letter-case spellings (listing.upper()) and through an NTFS junction (mklink /J) resolves to the exact same os.path.normcase(os.path.realpath(idx)) cache entry (len(rdna2spirv._STAGES) == 1) and continues returning 'vs' after index.json is removed.
      • python -I tests/test_ci_registration.py -> 18/18 passed (OK).
#2154 | head b001cfa5ee78c2ba55c3e896c8f49e5eb976827b (merged cleanly onto origin/main 791899f652) | Windows 11 (10.0.26200.9974), Python 3.12.10 | AI-assisted: yes ### Independent Verification (Windows 11 + NTFS) - **Diff & safety audit**: Inspected 100% of the diff on current head `b001cfa5ee` across `tools/rdna2spirv.py` (keying `_STAGES` per carve root via `os.path.normcase(os.path.realpath(idx))` and only caching when `os.path.isfile(idx)` succeeds) and `tests/test_rdna2spirv_stage_cache.py`. `python -I scripts/check_no_game_data.py tools/rdna2spirv.py tests/test_rdna2spirv_stage_cache.py` passed (`rc=0`). - **Red -> Green on Windows 11 + NTFS**: - **Red (`origin/main` `791899f652` `tools/rdna2spirv.py`)**: Running `#2154`'s `tests/test_rdna2spirv_stage_cache.py -v` against unpatched `tools/rdna2spirv.py` failed `2` of `3` tests (`FAILED (failures=2)`: `test_a_missing_index_is_not_cached` and `test_second_root_does_not_reuse_the_first_roots_index`, `'ps' != 'cs'`). - **Green (`#2154 + origin/main`)**: - `python -I tests/test_rdna2spirv_stage_cache.py -v` -> `3/3` passed (`OK`, `0.024s`) - **NTFS case-folding & directory junction (`mklink /J`) probe**: Verified on Windows 11 NTFS that calling `rdna2spirv.stage_of(...)` through different letter-case spellings (`listing.upper()`) and through an NTFS junction (`mklink /J`) resolves to the exact same `os.path.normcase(os.path.realpath(idx))` cache entry (`len(rdna2spirv._STAGES) == 1`) and continues returning `'vs'` after `index.json` is removed. - `python -I tests/test_ci_registration.py` -> `18/18` passed (`OK`).
MoonEyes commented 2026-11-29 15:44:08 +00:00

Merged. Tested by @zmasi on head b001cfa5e (CPU only (Windows 11, Python 3.12)): test_rdna2spirv_stage_cache: red on main (2 of 3 fail, 'ps' != 'cs'); green 3/3 on head. The CI jobs that are red on this head were also red on main at the time of the run. Thanks @whoalin1!

AI-assisted: yes

Merged. Tested by @zmasi on head b001cfa5e (CPU only (Windows 11, Python 3.12)): test_rdna2spirv_stage_cache: red on main (2 of 3 fail, 'ps' != 'cs'); green 3/3 on head. The CI jobs that are red on this head were also red on main at the time of the run. Thanks @whoalin1! AI-assisted: yes
Sign in to join this conversation.
No description provided.