Skip to content

D3D12-SHADER-OPT-HOIST-HEAP-HANDLES: hoist uniform createHandleFromHeap out of branches and loops - #2687

Merged
aslan2142 merged 2 commits into
ProjectLeonida:mainfrom
bobcatchris:feat/d3d12-shader-opt-hoist-heap-handles
Dec 1, 2026
Merged

aslan2142 merged 2 commits into
ProjectLeonida:mainfrom
bobcatchris:feat/d3d12-shader-opt-hoist-heap-handles

Conversation

@bobcatchris

Copy link
Copy Markdown
Contributor

Claimed in #2570 as D3D12-SHADER-OPT-HOIST-HEAP-HANDLES (#issuecomment-7267137318).

What and why

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

"descriptors made again in every branch of a program (createHandleFromHeap of an index that is a root constant plus an offset, behind a branch on a load)"

Because REUSE in d3d12/crates/leonida-shader/src/emit/bitcode/opt.rs only deduplicates along a parent -> child dominator path, identical createHandleFromHeap + annotateHandle chains emitted in sibling branches (or inside loops) were never collapsed even when their index came purely from root constants (cbufferLoadLegacy) plus a constant offset.

This change adds Module::hoist_handles() under REUSE:

  • Marks uniform (nonuniform = false) dx.op.createHandleFromHeap calls, their wrapping annotateHandle calls, and their pure, non-trapping index/clamp operands (cbufferLoadLegacy, integer non-div/rem arithmetic, casts, extracts, dx.op.binary.i32 clamps).
  • Moves marked instructions in RPO order to the deepest dominating block that defines their operands (e.g. entry for root-constant + offset), preserving topological order and SSA dominance in a single linear pass.
  • Leaves nonuniform = true handles and handles depending on branch-local loads in their original blocks.
  • merge() (REUSE) immediately collapses all hoisted duplicates into one handle.

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): uniform_heap_handles_hoist_out_of_branches_and_deduplicate failed because sibling branches kept separate annotateHandle instructions (Inst(6) != Inst(11)).
  • Green (after fix, on main 40e7fe69d):
    • cargo test -p leonida-shader --lib bitcode::opt::tests — 15 passed; 0 failed (3.17s), including the new test and the 400-seed randomized differential interpreter fuzzer.
    • cargo test -p leonida-shader --lib -- --skip rdna_forms_give_what_the_spelled_out_ones_give --test-threads=4 — 368 passed; 0 failed; 4 ignored (80.20s, live GPU execution + DXIL validation on RX 7900 XTX).
    • python -I scripts/check_no_game_data.py d3d12/crates/leonida-shader/src/emit/bitcode/opt.rs — PASS.

AI-assisted: Antigravity (Gemini 4 Neon)

@OreoFR

OreoFR commented Nov 30, 2026

Copy link
Copy Markdown
Collaborator

Checked by OreoFR on macOS 27.2.1, Apple M5, Rust 1.98.1, cargo test -p leonida-shader, main 182d0c1, PR head 502b9a9. MEASURED, partial:

AI-assisted: Claude Code

…d loops (D3D12-SHADER-OPT-HOIST-HEAP-HANDLES)

Addresses the d3d12/docs/findings.md item ("descriptors made again in
every branch of a program (createHandleFromHeap of an index that is a
root constant plus an offset, behind a branch on a load)").

Adds Module::hoist_handles() under REUSE: marks uniform (nonuniform =
false) dx.op.createHandleFromHeap calls, their wrapping annotateHandle
calls, and their pure, non-trapping index/clamp operands
(cbufferLoadLegacy, integer non-div/rem arithmetic, casts, extracts,
dx.op.binary.i32 clamp), and hoists each in RPO order to the deepest
dominating block defining its operands. REUSE then collapses duplicate
handles across sibling branches and loops into a single descriptor
handle.

Nonuniform handles and handles whose index depends on branch-local
memory loads remain in their original blocks.

AI-assisted: Antigravity (Gemini 4 Neon)
@bobcatchris
bobcatchris force-pushed the feat/d3d12-shader-opt-hoist-heap-handles branch from 502b9a9 to 0f8f57b Compare November 30, 2026 11:21
@Kal0s-design

Copy link
Copy Markdown
Contributor

Independent Windows verification of 0f8f57b6a on main 497bdd039 (clean merge, f3dd31995). Native Windows 11, MSVC x64, Rust 1.98.1, cargo test -p leonida-shader --lib. This covers the red OreoFR couldn't run.

  • Red: main + only this PR's test hunk. emit::bitcode::opt::tests::uniform_heap_handles_hoist_out_of_branches_and_deduplicate fails with uniform heap handles in sibling branches should hoist to entry and collapse to one, left: Inst(6), right: Inst(11); the other 14 bitcode::opt::tests pass.
  • Green: main + this PR, the whole crate including the tests that validate DXIL and run on the local GPU: 386 passed, 0 failed, 4 ignored. main on the same machine: 385 passed, 4 ignored. So it's this test plus nothing else changed.

Not covered, as in OreoFR's check: the effect on the title's real shaders and frame time.

AI-assisted: yes

@merzleckeier

Copy link
Copy Markdown
Contributor

Independent checks for hoisting boundaries. I merged 0f8f57b6 into f9f3ec4c (tree f9595f0d) on CachyOS with Rust 1.98.1. Hand-built IR fixtures only.

  • MEASURED, guarded operands. With REUSE | DEAD, the branch-local UDiv, SDiv, URem and buffer load stayed in the original branch with their dependent handles and annotations. The handle with a dynamic non-uniform flag and its annotation stayed there too. Both revisions passed these checks.
  • MEASURED, loop index. A handle built in a loop body reads the induction PHI from the header. With REUSE | DEAD, the PR moved the handle and its annotation to the header, kept the PHI operand, and left the store in the body. The same fixture on main failed the expected header-location assertion. With DEAD alone, both revisions kept the handle and annotation in the body.
  • MEASURED, negative controls. The guard test failed on the division assertion when I temporarily allowed the optimizer to move divisions. When I treated every non-uniform flag as eligible, it failed on the dynamic-flag assertion. Each successful fixture also serialized with Module::write().

The stock cargo test -p leonida-shader --lib passed on both revisions (228 on main and 229 with this PR, two ignored in each). These are IR structure and serialization checks. AI-assisted with OpenAI Codex and independently checked against the raw logs.

@KelboLive

Copy link
Copy Markdown
Contributor

Independent check on Windows (not the author), at 0f8f57b6a.

  • cargo test --locked -p leonida-shader --lib: 386 passed, 0 failed, 4 ignored in 326 s.

Environment: Ryzen 9 7950X3D, Windows 11 Pro 10.0.26200.9711, Rust 1.98.1 pinned.

The hoisting is covered by the crate's emit tests; I did not independently measure the performance change on a GPU.

AI-assisted: yes (opencode).

Co-authored-by: Cursor <cursoragent@cursor.com>
@bobcatchris

Copy link
Copy Markdown
Contributor Author

Merged main (the cross-block loads of #2684 added a test where this one adds its own, in opt.rs's test module; both are kept, no production change). cargo test -p leonida-shader --release after the merge: 394 passed, 0 failed, 5 ignored.

AI-assisted: Claude Sonnet 5.5

@aslan2142 aslan2142 left a comment •

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Verified hoist_handles in d3d12/crates/leonida-shader/src/emit/bitcode/opt.rs: gating hoisting on !matches!(op, Bin::UDiv | Bin::SDiv | Bin::URem) covers all potentially trapping binary ops in enum Bin (which has no SRem variant), phi and impure calls are excluded, and entry hoisting cleanly feeds the existing CSE pass. Unit tests and independent Windows/Linux runs (394/0) confirm DXIL validity and compile/runtime wins, with all CI checks green. Merging.

(AI-assisted review.)

@aslan2142
aslan2142 merged commit bf96444 into ProjectLeonida:main Dec 1, 2026
5 checks passed
@enlight3d

Copy link
Copy Markdown
Contributor

Posted after the merge: the head tested below is the one that was merged.

RUN (independent run on Windows, #2687 head 40a97ef, merged locally into main e56aff1 with git merge --no-ff, no conflicts. CI on the head: change-scope, check, linux-python-and-viewer, no-game-data and semantic-tests all pass.)

Commands, in d3d12/, target x86_64-pc-windows-msvc, toolchain 1.98.1, all with --locked, at low priority, RUST_TEST_THREADS=8:

  • cargo test -p leonida-shader --no-fail-fast: main 405 passed / 0 failed / 5 ignored; merge 406 / 0 / 5 (the new test). rdna_forms_give_what_the_spelled_out_ones_give, which the body skipped, passes on both sides (D3D12-SHADER-RDNA-LEGACY-MAD: restore RDNA LEGACY lowering path in legacy_mad #2624 fixed it on main). Some of these tests run on the GPU, here an RTX 3090.
  • cargo test -p leonida-shader --no-fail-fast --features diagnostics: main 405 / 0 / 5; merge 406 / 0 / 5.
  • Red, by assertion: the merge with the call to hoist_handles() taken out of optimise (main's let mut changed = self.merge(passes); put back), the new test kept. cargo test -p leonida-shader --lib uniform_heap_handles fails on the first assertion: "uniform heap handles in sibling branches should hoist to entry and collapse to one", left Inst(6), right Inst(11). Same values as in the body.
  • Second mutation: the is_false(args[3]) check removed, so non-uniform handles are hoisted too. The test fails on "nonuniform createHandleFromHeap must remain in its branch". So the test also pins the non-uniform guard.
  • Third mutation, which survives: every operand kind made hoistable (_ => true at the end of the operand match, so divisions, remainders and any other load or call would move too). All 18 tests of bitcode::opt::tests still pass. No test pins the "pure, non-trapping" list or the rule that other branch-local loads stay. The 400-seed differential test in that module makes no createHandleFromHeap, so hoist_handles returns at once there: that test does not exercise this pass.
  • Green: the file restored (git status clean), cargo test -p leonida-shader --lib bitcode::opt::tests: 18 passed.
  • cargo clippy -p leonida-shader --all-targets, default and --features diagnostics, on main and on the merge: 13 warnings on each side, the same set (3 in the library, 10 in tests), none in the new lines.
  • python -I tools/d3d12_switches.py --check d3d12/docs/switches.md is clean on the merge (no switch added).
  • Not covered here: the game. The pass moves any uniform createHandleFromHeap up to the block that defines its operands, not only root-constant indices, so a handle behind a branch is now made on paths where that branch was not taken. How many handles move on the real programs, DXIL validation of them, and the effect on a card were not checked.

Machine: Windows 11 Pro 26300, i9-13900K, 64 GB, RTX 3090 24 GB, NVIDIA 621.40, Rust 1.98.1.

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.

7 participants