Skip to content

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

Merged
MoonEyes merged 2 commits into
ProjectLeonida:mainfrom
bobcatchris:feat/d3d12-shader-opt-cross-block-loads
Nov 30, 2026
Merged

MoonEyes merged 2 commits into
ProjectLeonida:mainfrom
bobcatchris:feat/d3d12-shader-opt-cross-block-loads

Conversation

@bobcatchris

Copy link
Copy Markdown
Contributor

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 D3D12-SHADER-RDNA-LEGACY-MAD: restore RDNA LEGACY lowering path in legacy_mad #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

Copy link
Copy Markdown
Contributor

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.

@bobcatchris

Copy link
Copy Markdown
Contributor Author

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

Copy link
Copy Markdown
Contributor

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.

…D3D12-SHADER-OPT-CROSS-BLOCK-LOADS)

Addresses the d3d12/docs/findings.md item ("reads of read-only views
repeated in blocks that follow one another").

Previously, merge() cleared the LOADS map at every basic-block boundary,
preventing reuse of Effect::Reads across sequential blocks,
single-predecessor branch targets, and write-free diamonds or loops.

Extend merge() to maintain dominator-scoped dom_reads for Effect::Reads
while keeping Effect::Lanes (wave/quad operations) strictly block-local:
- Single-predecessor blocks inherit dom_reads directly from their
  immediate dominator (O(1)).
- Multi-predecessor blocks (merges and loop headers) run a reverse walk
  back to the immediate dominator and preserve dom_reads only when no
  block on any path (including loop back-edges) executes Effect::Writes.
- Any Effect::Writes instruction clears dom_reads for the rest of its
  block and dominator subtree, restoring the pre-block map on Leave so
  siblings remain unaffected.

Verified with 3 new Red -> Green unit tests (write-free diamond,
branch-write invalidation + sibling reuse, loop back-edge write vs
write-free loop), the 400-seed randomized differential interpreter suite,
and 370/370 leonida-shader --lib tests on native Windows 11 + AMD Radeon
RX 7900 XTX.

AI-assisted: Antigravity (Gemini 4 Neon)
… SSA-valid

AI-assisted: Antigravity (Gemini 4 Neon)
@bobcatchris
bobcatchris force-pushed the feat/d3d12-shader-opt-cross-block-loads branch from 2edb157 to bb68c6e Compare November 30, 2026 11:20
@Kal0s-design

Copy link
Copy Markdown
Contributor

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

Copy link
Copy Markdown
Contributor

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:

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

@merzleckeier

Copy link
Copy Markdown
Contributor

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

@MoonEyes

Copy link
Copy Markdown
Collaborator

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

Copy link
Copy Markdown
Collaborator

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

@MoonEyes
MoonEyes merged commit 8c8ab17 into ProjectLeonida:main Nov 30, 2026
5 checks passed
djrolfes added a commit to djrolfes/leonida that referenced this pull request Nov 30, 2026
… 1D wave-base (ProjectLeonida#2685), NaN-exact, by_due texture queue, unlocked pool growth (ProjectLeonida#2693), pipeline wait queue (ProjectLeonida#2622)
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