Skip to content

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

Merged
MoonEyes merged 1 commit into
ProjectLeonida:mainfrom
bobcatchris:fix/d3d12-shader-ir-analyse-once
Dec 1, 2026
Merged

MoonEyes merged 1 commit into
ProjectLeonida:mainfrom
bobcatchris:fix/d3d12-shader-ir-analyse-once

Conversation

@bobcatchris

Copy link
Copy Markdown
Contributor

Claim: D3D12-SHADER-IR-BUILD-ANALYSE-ONCE (#2570 (comment)).

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

For a program with v_writelane_b32/v_readlane_b32, unspill has to walk every
instruction to find parked scalars, decoding fields and computing effects for
each, and then build walks them again doing both. unspill now returns a cache
indexed by instruction (name, fields, effects), and build takes it.

The cache is sound because unspill only rewrites list in place and only after
it has already scanned everything; any instruction unspill rewrites has its
cache entry cleared, and build recomputes effects after its own
parked-scalar substitution. Programs without a lane save or restore, and any
program where unspill_scan bails, get no cache and decode exactly as before.

Same values, same errors, same order. DXIL-identity over the full shader
archive needs game files; checked here with the leonida-shader unit tests and
the hand-written translator semantic suite.

AI-assisted: yes
@fiestorra

Copy link
Copy Markdown
Contributor

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 KelboLive left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

MoonEyes commented Dec 1, 2026

Copy link
Copy Markdown
Collaborator

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

MoonEyes commented Dec 1, 2026

Copy link
Copy Markdown
Collaborator

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

@MoonEyes
MoonEyes merged commit 3bf2844 into ProjectLeonida:main Dec 1, 2026
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants