D3D12-SHADER-IR-BUILD-ANALYSE-ONCE: decode instruction fields and effects once #2835

Merged
bobcatchris merged 1 commit from upstream-pr/2835 into main 2026-12-01 09:09:26 +00:00
bobcatchris commented 2026-12-01 05:03:06 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by bobcatchris on 2026-12-01T05:03:06Z.


Claim: D3D12-SHADER-IR-BUILD-ANALYSE-ONCE (https://github.com/ProjectLeonida/mandy/issues/2570#issuecomment-7298302658).

What and why

ir::build walks every machine instruction, decoding fields(wave64) and computing effects(...). When a program has v_writelane_b32/v_readlane_b32, unspill_scan has to walk every instruction first to find parked scalars, decoding fields and computing effects for each, and then build does both again. A sampling profile put ir::build at 16.3% of samples under spill::build; this halves the decoding for spilling programs.

unspill now returns a cache indexed by instruction: Decoded { name, fields, effects }. The cache is sound:

  • unspill_scan only borrows fields/effects during its scan and rewrites list in place after scanning everything; list is never resized or reordered, and every put rewrite clears that entry's cache slot.
  • Programs without a lane save/restore get no cache (None); programs where unspill_scan bails (name() == None, effects error, no parked scalars, no pair) get no cache and decode exactly as before.
  • build recomputes effects after its own parked-scalar substitution (which mutates Fields::Sop scalar codes) and for rewritten instructions.

Same values, same errors, same order; the emitted DXIL is unchanged. No rendering or translation-rule change.

Verification (offline, no game files)

cargo test -p leonida-shader
  test result: ok. 404 passed; 0 failed; 5 ignored

python -I scripts/run_semantic_tests.py tests/semantic
  553 files, 6720 tests, 0 failed

cargo clippy -p leonida-shader --all-targets   # no new warnings
python -I scripts/check_no_game_data.py d3d12/crates/leonida-shader/src/ir.rs
git diff --check                               # clean

The existing ir::tests::unspill_looks_only_where_the_code_has_a_save_or_a_restore_of_a_lane still compares unspill with unspill_scan on synthetic streams and now reads the moved return type.

The DXIL-identity-over-full-archive check the claim mentions needs game shader data, which is not used here; the semantic corpus is hand-written.

AI-assisted: yes. Design direction from Gemini 4 Neon (one call, no game data).

<!-- greyfinch-sync:ubiquitarianism/leonida:issue:5579411608 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/2835). Originally posted by [bobcatchris](https://github.com/bobcatchris) on 2026-12-01T05:03:06Z. --- Claim: `D3D12-SHADER-IR-BUILD-ANALYSE-ONCE` (https://github.com/ProjectLeonida/mandy/issues/2570#issuecomment-7298302658). ## What and why `ir::build` walks every machine instruction, decoding `fields(wave64)` and computing `effects(...)`. When a program has `v_writelane_b32`/`v_readlane_b32`, `unspill_scan` has to walk every instruction first to find parked scalars, decoding `fields` and computing `effects` for each, and then `build` does both again. A sampling profile put `ir::build` at 16.3% of samples under `spill::build`; this halves the decoding for spilling programs. `unspill` now returns a cache indexed by instruction: `Decoded { name, fields, effects }`. The cache is sound: - `unspill_scan` only borrows `fields`/`effects` during its scan and rewrites `list` in place **after** scanning everything; `list` is never resized or reordered, and every `put` rewrite clears that entry's cache slot. - Programs without a lane save/restore get no cache (`None`); programs where `unspill_scan` bails (`name() == None`, `effects` error, no parked scalars, no pair) get no cache and decode exactly as before. - `build` recomputes `effects` after its own parked-scalar substitution (which mutates `Fields::Sop` scalar codes) and for rewritten instructions. Same values, same errors, same order; the emitted DXIL is unchanged. No rendering or translation-rule change. ## Verification (offline, no game files) ``` cargo test -p leonida-shader test result: ok. 404 passed; 0 failed; 5 ignored python -I scripts/run_semantic_tests.py tests/semantic 553 files, 6720 tests, 0 failed cargo clippy -p leonida-shader --all-targets # no new warnings python -I scripts/check_no_game_data.py d3d12/crates/leonida-shader/src/ir.rs git diff --check # clean ``` The existing `ir::tests::unspill_looks_only_where_the_code_has_a_save_or_a_restore_of_a_lane` still compares `unspill` with `unspill_scan` on synthetic streams and now reads the moved return type. The DXIL-identity-over-full-archive check the claim mentions needs game shader data, which is not used here; the semantic corpus is hand-written. AI-assisted: yes. Design direction from Gemini 4 Neon (one call, no game data).
fiestorra commented 2026-12-01 05:18:42 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by fiestorra on 2026-12-01T05:18:42Z.


Verified on Windows: the crate is green with it, and the suite does not distinguish it from main - which is the honest finding for a change that decodes an instruction once instead of twice.

main 10bed9ff2, cherry-picked (one commit, merge-base 86c30fcea), cargo 1.98.1:

cargo test -p leonida-shader
  with this pull request:   ok. 404 passed; 0 failed; 5 ignored   (360.35 s)
  with main's ir.rs:        ok. 404 passed; 0 failed; 5 ignored   (355.15 s)

So nothing is broken and nothing is added. ir.rs gains 51 lines and loses 26, and the same 404 tests pass in both trees.

What that means, and I would rather say it than dress it up: the suite proves the answers did not change, which is the property a refactor must have; it does not prove the instruction is decoded once. Only a count of decodes would, and there is no such counter in leonida-shader. If the pull request's description has a measurement, that is what to check.

And one thing worth noting from the shape: a change that only touches ir.rs and adds no test is the kind this repository has no workflow to run at all - cargo test -p leonida-shader takes six minutes and nothing runs it in CI. That is #2816's argument, and this pull request is the case it does not cover: a refactor with no new test is exactly what a suite catches by accident or not at all.

AI-assisted: yes - DeepSeek v4.1 (flash), two six-minute runs.

Written with an AI assistant.

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7301637372 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/2835#issuecomment-7301637372). Originally posted by [fiestorra](https://github.com/fiestorra) on 2026-12-01T05:18:42Z. --- **Verified on Windows: the crate is green with it, and the suite does not distinguish it from `main` - which is the honest finding for a change that decodes an instruction once instead of twice.** **`main` `10bed9ff2`, cherry-picked (one commit, `merge-base` `86c30fcea`), `cargo 1.98.1`:** ``` cargo test -p leonida-shader with this pull request: ok. 404 passed; 0 failed; 5 ignored (360.35 s) with main's ir.rs: ok. 404 passed; 0 failed; 5 ignored (355.15 s) ``` **So nothing is broken and nothing is added.** **`ir.rs` gains 51 lines and loses 26, and the same 404 tests pass in both trees.** **What that means, and I would rather say it than dress it up**: **the suite proves the answers did not change, which is the property a refactor must have; it does not prove the instruction is decoded once.** **Only a count of decodes would, and there is no such counter in `leonida-shader`.** **If the pull request's description has a measurement, that is what to check.** **And one thing worth noting from the shape**: **a change that only touches `ir.rs` and adds no test is the kind this repository has no workflow to run at all** - **`cargo test -p leonida-shader` takes six minutes and nothing runs it in CI.** **That is #2816's argument, and this pull request is the case it does not cover**: **a refactor with no new test is exactly what a suite catches by accident or not at all.** **AI-assisted:** yes - DeepSeek v4.1 (flash), two six-minute runs. Written with an AI assistant.
KelboLive commented 2026-12-01 06:09:54 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by KelboLive on 2026-12-01T06:09:54Z.

Review: COMMENTED


The key and the invalidation are right, and the differential is the strongest evidence: 800 random spilling builds (wave32 and wave64, all cache-hitting) with the cache on against the pre-PR path off gave identical IR, identical errors, identical panics. Keying the cache to n + 1 changes all 800 and the committed suite catches it (three resource/paired tests fail), so the wiring is pinned by real tests, not only by the differential.

The mechanism holds: a Vec<Option<Decoded>> indexed by the instruction's place in list, which is never resized or reordered (rewrites go through put, same length, and build consumes in order); put is the only rewrite and clears the slot (ir.rs:628); build takes each slot once (:177) and recomputes effects after its parked-scalar substitution (:197, :222); every bail-out (no lane instruction, unknown opcode, effects error, no parked scalar, no plan) returns None, so those programs decode exactly as before and reproduce the same error at the same offset. Nothing is global or per-thread — the cache is owned by one build and dropped with it, and a non-spilling program pays one Option check per instruction with no allocation.

Two small things:

  • cached_effects = None after the substitution is unreachable for a cached instruction — every parked index was already rewritten by put, whose slot is None — so the line is defensive. A comment saying so (or dropping it) would keep the next reader from assuming it is load-bearing.
  • The existing unspill test now compares only the moves (.0); the cache's contents are covered by the resource/paired value tests and by the differential, not by it. Fine, just worth knowing where the coverage lives.
<!-- greyfinch-sync:ubiquitarianism/leonida:review:6126026791 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/2835#pullrequestreview-6126026791). Originally posted by [KelboLive](https://github.com/KelboLive) on 2026-12-01T06:09:54Z. **Review:** COMMENTED --- The key and the invalidation are right, and the differential is the strongest evidence: 800 random spilling builds (wave32 and wave64, all cache-hitting) with the cache on against the pre-PR path off gave **identical IR, identical errors, identical panics**. Keying the cache to `n + 1` changes all 800 and the committed suite catches it (three `resource`/`paired` tests fail), so the wiring is pinned by real tests, not only by the differential. The mechanism holds: a `Vec<Option<Decoded>>` indexed by the instruction's place in `list`, which is never resized or reordered (rewrites go through `put`, same length, and `build` consumes in order); `put` is the only rewrite and clears the slot (`ir.rs:628`); `build` takes each slot once (`:177`) and recomputes `effects` after its parked-scalar substitution (`:197`, `:222`); every bail-out (no lane instruction, unknown opcode, effects error, no parked scalar, no plan) returns `None`, so those programs decode exactly as before and reproduce the same error at the same offset. Nothing is global or per-thread — the cache is owned by one `build` and dropped with it, and a non-spilling program pays one `Option` check per instruction with no allocation. Two small things: - `cached_effects = None` after the substitution is **unreachable for a cached instruction** — every parked index was already rewritten by `put`, whose slot is `None` — so the line is defensive. A comment saying so (or dropping it) would keep the next reader from assuming it is load-bearing. - The existing unspill test now compares only the moves (`.0`); the cache's contents are covered by the resource/paired value tests and by the differential, not by it. Fine, just worth knowing where the coverage lives.
MoonEyes commented 2026-12-01 08:33:28 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by MoonEyes on 2026-12-01T08:33:28Z.


RUN - VERIFY-2835-RTX4060 - by MoonEyes (another person than the author) on head 12ff308a8, with #2836, #2835, #2695, #2696, #2689, #2699 and #2705 merged together into main 9372a4d4f (clean).

  • Machine: RTX 4060 8 GB, Windows 11, diag profile, features diagnostics,mimalloc, Series S, BAR window off, texture budget 1024 MB.
  • In-game, no scripted action, 480 s asked, a session that loads into the world and stays: main 9372a4d4f and then main + the seven, one run each, same save: 0 device lost, 0 panics, 0 exceptions in both, about 490 s in the world each, alive at the time limit. Frame rate median 17.8 with the seven and 15.0 on main, not a comparison: other work ran on the PC during the main run.
  • Picture: screenshots every 60 s. The last four show the same scene in both runs (water, sky, the boat, the map) and look the same; the first four are cutscenes at different moments and were only looked at for broken textures (none seen). No screenshot is posted.
  • Tests on main 9372a4d4f + the seven (Windows): leonida-hle 243, leonida-gpu 602, leonida-formats 47, leonida 47, leonida-shader 403 of 405: two source_hash_tests failed on a stale build script of mine (a target folder shared with an older branch) and pass after a rebuild of it (3 of 3).

Limits: one card, one run per arm, two arms only, the seven together. Values only, no game data.

AI-assisted (Claude Code).

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7305475671 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/2835#issuecomment-7305475671). Originally posted by [MoonEyes](https://github.com/MoonEyes) on 2026-12-01T08:33:28Z. --- RUN - VERIFY-2835-RTX4060 - by MoonEyes (another person than the author) on head `12ff308a8`, with #2836, #2835, #2695, #2696, #2689, #2699 and #2705 merged together into `main` `9372a4d4f` (clean). - **Machine:** RTX 4060 8 GB, Windows 11, diag profile, features diagnostics,mimalloc, Series S, BAR window off, texture budget 1024 MB. - **In-game, no scripted action, 480 s asked, a session that loads into the world and stays:** `main` `9372a4d4f` and then `main` + the seven, one run each, same save: **0 device lost, 0 panics, 0 exceptions** in both, about 490 s in the world each, alive at the time limit. Frame rate median 17.8 with the seven and 15.0 on `main`, **not a comparison**: other work ran on the PC during the `main` run. - **Picture:** screenshots every 60 s. The last four show the same scene in both runs (water, sky, the boat, the map) and look the same; the first four are cutscenes at different moments and were only looked at for broken textures (none seen). No screenshot is posted. - **Tests on `main` `9372a4d4f` + the seven (Windows):** leonida-hle 243, leonida-gpu 602, leonida-formats 47, leonida 47, leonida-shader 403 of 405: two `source_hash_tests` failed on a stale build script of mine (a target folder shared with an older branch) and pass after a rebuild of it (3 of 3). Limits: one card, one run per arm, two arms only, the seven together. Values only, no game data. AI-assisted (Claude Code).
MoonEyes commented 2026-12-01 09:09:22 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by MoonEyes on 2026-12-01T09:09:22Z.


Merging. Head 12ff308a8 merges cleanly with main 76746d35b and does not force a rebase of #2566. CI green on this head and on main. In-game run on the RTX 4060 posted above (this head, with the six others, about 490 s in the world, 0 device lost, pictures compared with main). Tests on main + the seven: leonida-hle, leonida-gpu, leonida-formats, leonida, leonida-shader, 0 failed. Verified on it by fiestorra and KelboLive (800 random builds, same IR). Merged by MoonEyes.

AI-assisted (Claude Code).

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7306294078 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/2835#issuecomment-7306294078). Originally posted by [MoonEyes](https://github.com/MoonEyes) on 2026-12-01T09:09:22Z. --- Merging. Head `12ff308a8` merges cleanly with `main` `76746d35b` and does not force a rebase of #2566. CI green on this head and on main. In-game run on the RTX 4060 posted above (this head, with the six others, about 490 s in the world, 0 device lost, pictures compared with main). Tests on main + the seven: leonida-hle, leonida-gpu, leonida-formats, leonida, leonida-shader, 0 failed. Verified on it by fiestorra and KelboLive (800 random builds, same IR). Merged by MoonEyes. AI-assisted (Claude Code).
Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
ProjectLeonida/mandy!2835
No description provided.