D3D12-SHADER-OPT-CROSS-BLOCK-LOADS: propagate LOADS across write-free dominating blocks #2684

Merged
bobcatchris merged 2 commits from upstream-pr/2684 into main 2026-11-30 18:27:56 +00:00
bobcatchris commented 2026-11-30 02:29:10 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by bobcatchris on 2026-11-30T02:29:10Z.


Claimed in #2570 as D3D12-SHADER-OPT-CROSS-BLOCK-LOADS (#issuecomment-7266940327).

What and why

Addresses the open shader-performance item in d3d12/docs/findings.md:

"The card's time is the title's programs (47.2 ms/frame) ... What is left, by size: ... reads of read-only views repeated in blocks that follow one another (70 of 118 in 922e1d0ee1e19a85)."

In d3d12/crates/leonida-shader/src/emit/bitcode/opt.rs, merge() previously called read.clear() at the entry of every basic block, so LOADS could never reuse an Effect::Reads load across sequential blocks, single-predecessor branch targets, or write-free diamonds/loops.

This change splits Effect::Reads (memory loads) from Effect::Lanes (wave/quad ops, which remain strictly block-local because active lanes can change across branches) and propagates Effect::Reads along the dominator tree:

  • Single-predecessor blocks (preds[b] == [idom]): inherit dom_reads directly from idom in O(1).
  • Multi-predecessor blocks (merges and loop headers): check via reverse DFS back to idom whether any block on any path (including loop back-edges) executes Effect::Writes; preserve dom_reads only if no write can occur.
  • On Effect::Writes: clear dom_reads for the remainder of the block and its dominator subtree, restoring the pre-block map on Step::Leave so sibling branches are unaffected.

Verification (Red -> Green)

Hardware / OS: native Windows 11 Pro (10.0.26200) + AMD Radeon RX 7900 XTX (gfx1100, driver 32.0.32029.2010), pinned toolchain rustc 1.98.1.

  • Red (before fix): All 3 new cross-block tests (cross_block_reads_reuse_dominating_load_without_writes, cross_block_reads_invalidated_when_branch_writes, cross_block_reads_in_loop_respect_backedge_writes) failed because LOADS cleared reads at every block boundary.
  • Green (after fix, on main 40e7fe69d):
    • cargo test -p leonida-shader --lib bitcode::opt::tests — 17 passed; 0 failed (3.20s), including all 3 new tests and the 400-seed × 6-input × 12-pass randomized differential interpreter fuzzer (every_pass_leaves_what_random_programs_store).
    • cargo test -p leonida-shader --lib -- --skip rdna_forms_give_what_the_spelled_out_ones_give --test-threads=4 — 370 passed; 0 failed; 4 ignored (81.00s, live GPU execution + DXIL validation on RX 7900 XTX; the 1 skipped test is the pre-existing main failure fixed in #2624).
    • python -I scripts/check_no_game_data.py d3d12/crates/leonida-shader/src/emit/bitcode/opt.rs — PASS.

AI-assisted: Antigravity (Gemini 4 Neon)

<!-- greyfinch-sync:ubiquitarianism/leonida:issue:5557897560 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/2684). Originally posted by [bobcatchris](https://github.com/bobcatchris) on 2026-11-30T02:29:10Z. --- Claimed in #2570 as `D3D12-SHADER-OPT-CROSS-BLOCK-LOADS` (`#issuecomment-7266940327`). ### What and why Addresses the open shader-performance item in `d3d12/docs/findings.md`: > *"The card's time is the title's programs (47.2 ms/frame) ... What is left, by size: ... reads of read-only views repeated in blocks that follow one another (70 of 118 in `922e1d0ee1e19a85`)."* In `d3d12/crates/leonida-shader/src/emit/bitcode/opt.rs`, `merge()` previously called `read.clear()` at the entry of **every** basic block, so `LOADS` could never reuse an `Effect::Reads` load across sequential blocks, single-predecessor branch targets, or write-free diamonds/loops. This change splits `Effect::Reads` (memory loads) from `Effect::Lanes` (wave/quad ops, which remain strictly block-local because active lanes can change across branches) and propagates `Effect::Reads` along the dominator tree: - **Single-predecessor blocks (`preds[b] == [idom]`):** inherit `dom_reads` directly from `idom` in O(1). - **Multi-predecessor blocks (merges and loop headers):** check via reverse DFS back to `idom` whether any block on any path (including loop back-edges) executes `Effect::Writes`; preserve `dom_reads` only if no write can occur. - **On `Effect::Writes`:** clear `dom_reads` for the remainder of the block and its dominator subtree, restoring the pre-block map on `Step::Leave` so sibling branches are unaffected. ### Verification (Red -> Green) Hardware / OS: native Windows 11 Pro (`10.0.26200`) + **AMD Radeon RX 7900 XTX** (`gfx1100`, driver `32.0.32029.2010`), pinned toolchain `rustc 1.98.1`. - **Red (before fix):** All 3 new cross-block tests (`cross_block_reads_reuse_dominating_load_without_writes`, `cross_block_reads_invalidated_when_branch_writes`, `cross_block_reads_in_loop_respect_backedge_writes`) failed because `LOADS` cleared reads at every block boundary. - **Green (after fix, on `main` `40e7fe69d`):** - `cargo test -p leonida-shader --lib bitcode::opt::tests` — **`17 passed; 0 failed`** (`3.20s`), including all 3 new tests and the 400-seed × 6-input × 12-pass randomized differential interpreter fuzzer (`every_pass_leaves_what_random_programs_store`). - `cargo test -p leonida-shader --lib -- --skip rdna_forms_give_what_the_spelled_out_ones_give --test-threads=4` — **`370 passed; 0 failed; 4 ignored`** (`81.00s`, live GPU execution + DXIL validation on RX 7900 XTX; the 1 skipped test is the pre-existing `main` failure fixed in #2624). - `python -I scripts/check_no_game_data.py d3d12/crates/leonida-shader/src/emit/bitcode/opt.rs` — **PASS**. AI-assisted: Antigravity (Gemini 4 Neon)
merzleckeier commented 2026-11-30 02:38:18 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by merzleckeier on 2026-11-30T02:38:18Z.


Independent CachyOS check of d2be6ba70 merged into main 182d0c174 (merge 6659ccd57, tree d47886846). MEASURED The native leonida-shader --lib suites passed with 224 tests on main and 227 on the merge, with two ignored on each.

The positive diamond fixture currently uses both branch-local results in join as ordinary operands. Putting those results on the incoming edges of a PHI gives a valid test module before optimization as well. My fixture with the PHI executes both paths before and after LOADS | DEAD and compares their store traces. MEASURED The traces match. The merge reduces four loads to one for the write-free diamond and to three when one branch changes the loaded location. main keeps all four in both cases. The wave test keeps one call in each block and eliminates only the repeat inside the same block.

READ IN CODE The new LOADS block reaches key() for Pure and Writes instructions too. key() clones arguments for calls. Restoring the previous class filter skips those keys and keeps the same directed results.

MEASURED Three interleaved release runs per arm, pinned to one CPU. These are the per-run medians in ms per synthetic module for merge(LOADS | REUSE).

Fixture main PR PR with class filter
Arithmetic and pure calls 1.616 / 1.608 / 1.622 1.688 / 1.690 / 1.695 1.638 / 1.647 / 1.647
Same shape with repeated loads 1.671 / 1.666 / 1.666 1.759 / 1.758 / 1.764 1.706 / 1.711 / 1.708

The second fixture retains 192 loads on main and 12 on both PR variants. The timing covers the optimizer pass on synthetic IR. AI-assisted with OpenAI Codex.

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7267180789 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/2684#issuecomment-7267180789). Originally posted by [merzleckeier](https://github.com/merzleckeier) on 2026-11-30T02:38:18Z. --- Independent CachyOS check of `d2be6ba70` merged into `main` `182d0c174` (merge `6659ccd57`, tree `d47886846`). **MEASURED** The native `leonida-shader --lib` suites passed with 224 tests on `main` and 227 on the merge, with two ignored on each. The positive diamond fixture currently uses both branch-local results in `join` as ordinary operands. Putting those results on the incoming edges of a PHI gives a valid test module before optimization as well. My [fixture with the PHI](https://github.com/merzleckeier/mandy/commit/df2efa32eb43eae542e3bf69ad5b4d6f78e73c70) executes both paths before and after `LOADS | DEAD` and compares their store traces. **MEASURED** The traces match. The merge reduces four loads to one for the write-free diamond and to three when one branch changes the loaded location. `main` keeps all four in both cases. The wave test keeps one call in each block and eliminates only the repeat inside the same block. **READ IN CODE** The new `LOADS` block reaches `key()` for `Pure` and `Writes` instructions too. `key()` clones arguments for calls. [Restoring the previous class filter](https://github.com/merzleckeier/mandy/commit/ca757f2ce81a6bb7e8311c7d8e9af2c00f1892a7) skips those keys and keeps the same directed results. **MEASURED** Three interleaved release runs per arm, pinned to one CPU. These are the per-run medians in ms per synthetic module for `merge(LOADS | REUSE)`. | Fixture | `main` | PR | PR with class filter | | --- | --- | --- | --- | | Arithmetic and pure calls | 1.616 / 1.608 / 1.622 | 1.688 / 1.690 / 1.695 | 1.638 / 1.647 / 1.647 | | Same shape with repeated loads | 1.671 / 1.666 / 1.666 | 1.759 / 1.758 / 1.764 | 1.706 / 1.711 / 1.708 | The second fixture retains 192 loads on `main` and 12 on both PR variants. The timing covers the optimizer pass on synthetic IR. AI-assisted with OpenAI Codex.
bobcatchris commented 2026-11-30 06:17:29 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by bobcatchris on 2026-11-30T06:17:29Z.


Thanks for the careful review @merzleckeier! Restored the matches!(class, Effect::Reads | Effect::Lanes) filter before calling self.key(i) so Pure and Writes instructions skip cloning call arguments, and updated the positive diamond test to route branch reads through a Phi node before optimization (17/17 bitcode::opt::tests pass). AI-assisted: Antigravity (Gemini 4 Neon).

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7270496585 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/2684#issuecomment-7270496585). Originally posted by [bobcatchris](https://github.com/bobcatchris) on 2026-11-30T06:17:29Z. --- Thanks for the careful review @merzleckeier! Restored the `matches!(class, Effect::Reads | Effect::Lanes)` filter before calling `self.key(i)` so `Pure` and `Writes` instructions skip cloning call arguments, and updated the positive diamond test to route branch reads through a `Phi` node before optimization (`17/17` `bitcode::opt::tests` pass). AI-assisted: Antigravity (Gemini 4 Neon).
merzleckeier commented 2026-11-30 07:30:51 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by merzleckeier on 2026-11-30T07:30:51Z.


Thanks for updating both points. I retested 2edb15796 on GitHub merge 1a612697b (base bbd45f5cc) on CachyOS. The native leonida-shader --lib suite ran 228 passing tests on the base and 231 on the merge, with two ignored and zero failures on each. All three cross-block cases passed, including the updated PHI fixture, branch-store invalidation, and loop-backedge invalidation. I also verified that the Reads | Lanes check precedes key() in this revision.

AI-assisted with OpenAI Codex.

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7271816761 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/2684#issuecomment-7271816761). Originally posted by [merzleckeier](https://github.com/merzleckeier) on 2026-11-30T07:30:51Z. --- Thanks for updating both points. I retested `2edb15796` on GitHub merge `1a612697b` (base `bbd45f5cc`) on CachyOS. The native `leonida-shader --lib` suite ran 228 passing tests on the base and 231 on the merge, with two ignored and zero failures on each. All three cross-block cases passed, including the updated PHI fixture, branch-store invalidation, and loop-backedge invalidation. I also verified that the `Reads | Lanes` check precedes `key()` in this revision. AI-assisted with OpenAI Codex.
Kal0s-design commented 2026-11-30 15:11:23 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by Kal0s-design on 2026-11-30T15:11:23Z.


Native Windows check of bb68c6e19 on current main 232f9e603 (no conflict, 11fee995f). Windows 11, MSVC x64, Rust 1.98.1, cargo test -p leonida-shader --lib on a hardware GPU (not WARP), so the DXIL validation and the GPU dispatch/readback tests run too. That's what the two Linux runs above don't cover.

  • Red: main + only this PR's test hunk: the 3 new cross_block_reads_* tests fail (reuse_dominating_load_without_writes, invalidated_when_branch_writes, in_loop_respect_backedge_writes); the other 15 bitcode::opt tests pass.
  • Green: main + this PR, the whole crate: 388 passed, 0 failed, 4 ignored. main lists 389 tests (385 run + 4 ignored); the merge lists 392, so the 3 additions are all that changes and no existing test turns red with cross-block LOADS on.

Not covered: the effect on real shaders' DXIL and frame time.

AI-assisted: yes

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7282583167 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/2684#issuecomment-7282583167). Originally posted by [Kal0s-design](https://github.com/Kal0s-design) on 2026-11-30T15:11:23Z. --- **Native Windows check** of `bb68c6e19` on current `main` `232f9e603` (no conflict, `11fee995f`). Windows 11, MSVC x64, Rust 1.98.1, `cargo test -p leonida-shader --lib` on a hardware GPU (not WARP), so the DXIL validation and the GPU dispatch/readback tests run too. That's what the two Linux runs above don't cover. - Red: `main` + only this PR's test hunk: the 3 new `cross_block_reads_*` tests fail (`reuse_dominating_load_without_writes`, `invalidated_when_branch_writes`, `in_loop_respect_backedge_writes`); the other 15 `bitcode::opt` tests pass. - Green: `main` + this PR, the whole crate: 388 passed, 0 failed, 4 ignored. `main` lists 389 tests (385 run + 4 ignored); the merge lists 392, so the 3 additions are all that changes and no existing test turns red with cross-block `LOADS` on. Not covered: the effect on real shaders' DXIL and frame time. AI-assisted: yes
merzleckeier commented 2026-11-30 16:45:32 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by merzleckeier on 2026-11-30T16:45:32Z.


Pairwise merge check for the optimizer changes

MEASURED. I tested bb68c6e19 on main 08e42a516. This PR, #2687 (0f8f57b6a) and #2750 (55996ef0c) each merge cleanly into that baseline. Merging the resulting trees in pairs with git merge-tree --write-tree gives two conflicts in d3d12/crates/leonida-shader/src/emit/bitcode/opt.rs:

  • #2684 + #2687: both insert tests at the same spot. Keep the cross_block_reads_* tests and the uniform-heap-handle test as separate functions.
  • #2684 + #2750: the production hunk initializing the read maps overlaps. #2684 replaces the single read map with dom_reads and lane_reads. Preserve both roles if carrying #2750's FxHashMap conversion forward. #2750 also declares its dependency on #2745.

These are local merge checks. The two pairs still need tests after resolving the hunks. AI-assisted: Codex (GPT-6.2 Sol).

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7285181356 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/2684#issuecomment-7285181356). Originally posted by [merzleckeier](https://github.com/merzleckeier) on 2026-11-30T16:45:32Z. --- ### Pairwise merge check for the optimizer changes **MEASURED.** I tested `bb68c6e19` on `main` `08e42a516`. This PR, #2687 (`0f8f57b6a`) and #2750 (`55996ef0c`) each merge cleanly into that baseline. Merging the resulting trees in pairs with `git merge-tree --write-tree` gives two conflicts in `d3d12/crates/leonida-shader/src/emit/bitcode/opt.rs`: - **#2684 + #2687:** both insert tests at the same spot. Keep the `cross_block_reads_*` tests and the uniform-heap-handle test as separate functions. - **#2684 + #2750:** the production hunk initializing the read maps overlaps. #2684 replaces the single read map with `dom_reads` and `lane_reads`. Preserve both roles if carrying #2750's `FxHashMap` conversion forward. #2750 also declares its dependency on #2745. These are local merge checks. The two pairs still need tests after resolving the hunks. AI-assisted: Codex (GPT-6.2 Sol).
merzleckeier commented 2026-11-30 16:54:51 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by merzleckeier on 2026-11-30T16:54:51Z.


Both optimizer merge resolutions tested

MEASURED. Follow-up to the pairwise merge check. I tested the exact PR heads bb68c6e19, 0f8f57b6a, 88d0dbf9c and 55996ef0c from main 7663c34fc, with Rust 1.98.1 on CachyOS x86-64. I built cargo test --locked --offline -p leonida-shader --lib --no-run and ran each test binary in three fresh processes.

PRs applied to main Tree Passed per run
#2684 + #2687 ef5ebc8fb 233
#2684 + #2745 357d5506b 234
#2684 + #2745 + #2750 800995cb6 237
#2684 + #2687 + #2745 955e0e969 235
#2684 + #2687 + #2745 + #2750 b3759f7d1 238

Every run had zero failures and two ignored tests. For #2687, I kept all three cross-block load tests and the heap-handle test as complete functions. For #2750, I used FxHashMap for known, dom_reads, lane_reads, and the matching saved-map type in Step::Leave. The two A/B diffs each cover #2750's six files.

I captured optimizer bitcode and DXIL containers from the synthetic suite, keyed by test and emission ordinal. Adding #2750 preserved 4,923 bitcode captures and 100 DXIL containers byte for byte without #2687, and 4,924 bitcode captures and 100 DXIL containers with #2687. Each comparison agreed across all six runs.

Scope: local merge resolutions and CPU-side synthetic translation. AI-assisted: Codex (GPT-6.2 Sol).

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7285424982 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/2684#issuecomment-7285424982). Originally posted by [merzleckeier](https://github.com/merzleckeier) on 2026-11-30T16:54:51Z. --- ### Both optimizer merge resolutions tested **MEASURED.** Follow-up to the [pairwise merge check](https://github.com/ProjectLeonida/mandy/pull/2684#issuecomment-7285181356). I tested the exact PR heads `bb68c6e19`, `0f8f57b6a`, `88d0dbf9c` and `55996ef0c` from `main` `7663c34fc`, with Rust 1.98.1 on CachyOS x86-64. I built `cargo test --locked --offline -p leonida-shader --lib --no-run` and ran each test binary in three fresh processes. | PRs applied to `main` | Tree | Passed per run | | --- | --- | ---: | | #2684 + #2687 | `ef5ebc8fb` | 233 | | #2684 + #2745 | `357d5506b` | 234 | | #2684 + #2745 + #2750 | `800995cb6` | 237 | | #2684 + #2687 + #2745 | `955e0e969` | 235 | | #2684 + #2687 + #2745 + #2750 | `b3759f7d1` | 238 | Every run had zero failures and two ignored tests. For #2687, I kept all three cross-block load tests and the heap-handle test as complete functions. For #2750, I used `FxHashMap` for `known`, `dom_reads`, `lane_reads`, and the matching saved-map type in `Step::Leave`. The two A/B diffs each cover #2750's six files. I captured optimizer bitcode and DXIL containers from the synthetic suite, keyed by test and emission ordinal. Adding #2750 preserved **4,923 bitcode captures and 100 DXIL containers** byte for byte without #2687, and **4,924 bitcode captures and 100 DXIL containers** with #2687. Each comparison agreed across all six runs. Scope: local merge resolutions and CPU-side synthetic translation. AI-assisted: Codex (GPT-6.2 Sol).
MoonEyes commented 2026-11-30 18:24:31 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by MoonEyes on 2026-11-30T18:24:31Z.


RUN · VERIFY-2684-RTX4060 · by MoonEyes (another person than the author) on head bb68c6e19, together with #2684 (bb68c6e19), #2685 (903e3731e) and #2757 (fe7c76d3b) merged into main 99167bcaf. Windows 11 Pro 26300, RTX 4060 8 GB, -SeriesS -FrameGen 2, diag build.

  • cargo test -p leonida-shader --locked: 392 passed, 0 failed.
  • Game: two runs of the same build, the person walking in the world from 345 s. First with the shaders translated again (795 s before the title, about 185 s of steady frames), then warm: the steady world after 165 s, about 425 s of it, about 98 m walked, no device loss, no unhandled exception, the process alive at the end. PASS for the gate of 300 s.
  • The three pull requests were in one build, so a fault of one could have been hidden by another only if they cancel out; none was seen. No frame rate is claimed, other work ran on the PC and there is no arm without them.

AI-assisted (Claude Code).

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7287805032 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/2684#issuecomment-7287805032). Originally posted by [MoonEyes](https://github.com/MoonEyes) on 2026-11-30T18:24:31Z. --- RUN · VERIFY-2684-RTX4060 · by MoonEyes (another person than the author) on head `bb68c6e19`, together with #2684 (`bb68c6e19`), #2685 (`903e3731e`) and #2757 (`fe7c76d3b`) merged into `main` `99167bcaf`. Windows 11 Pro 26300, RTX 4060 8 GB, `-SeriesS -FrameGen 2`, diag build. - `cargo test -p leonida-shader --locked`: 392 passed, 0 failed. - Game: two runs of the same build, the person walking in the world from 345 s. First with the shaders translated again (795 s before the title, about 185 s of steady frames), then warm: the steady world after 165 s, about 425 s of it, about 98 m walked, no device loss, no unhandled exception, the process alive at the end. PASS for the gate of 300 s. - The three pull requests were in one build, so a fault of one could have been hidden by another only if they cancel out; none was seen. No frame rate is claimed, other work ran on the PC and there is no arm without them. AI-assisted (Claude Code).
MoonEyes commented 2026-11-30 18:27:52 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by MoonEyes on 2026-11-30T18:27:52Z.


Merging. Head bb68c6e19 merges cleanly with main 6cdc0a0d1 and does not force a rebase of #2566. Two runs in the game (RTX 4060, the second 425 s steady, no device loss) with #2684, #2685 and #2757 together; cargo test 392 passed; checked by other people too. The shaders are translated again at the first start. Merged by MoonEyes.

AI-assisted (Claude Code).

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7287887443 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/2684#issuecomment-7287887443). Originally posted by [MoonEyes](https://github.com/MoonEyes) on 2026-11-30T18:27:52Z. --- Merging. Head `bb68c6e19` merges cleanly with `main` `6cdc0a0d1` and does not force a rebase of #2566. Two runs in the game (RTX 4060, the second 425 s steady, no device loss) with #2684, #2685 and #2757 together; cargo test 392 passed; checked by other people too. The shaders are translated again at the first start. 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!2684
No description provided.