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

Merged
bobcatchris merged 2 commits from upstream-pr/2687 into main 2026-12-01 11:11:26 +00:00
bobcatchris commented 2026-11-30 02:38:43 +00:00 (Migrated from github.com)

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


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)

<!-- greyfinch-sync:ubiquitarianism/leonida:issue:5557982799 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/2687). Originally posted by [bobcatchris](https://github.com/bobcatchris) on 2026-11-30T02:38:43Z. --- 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 commented 2026-11-30 02:50:39 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by OreoFR on 2026-11-30T02:50:39Z.


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

  • main: 224 passed / 2 ignored; main + this PR: 225 passed / 2 ignored. The new test emit::bitcode::opt::tests::uniform_heap_handles_hoist_out_of_branches_and_deduplicate passes; nothing else changes.
  • No red: the test is in the same file as the change, so I could not run it against main's code.
  • Not covered: the effect on real shaders (DXIL validation, GPU dispatch, frame time).
  • Merges cleanly with main and with #2566.

AI-assisted: Claude Code

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7267365955 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/2687#issuecomment-7267365955). Originally posted by [OreoFR](https://github.com/OreoFR) on 2026-11-30T02:50:39Z. --- Checked by OreoFR on macOS 27.2.1, Apple M5, Rust 1.98.1, `cargo test -p leonida-shader`, main 182d0c174, PR head 502b9a97b. MEASURED, partial: - main: 224 passed / 2 ignored; main + this PR: **225 passed / 2 ignored**. The new test `emit::bitcode::opt::tests::uniform_heap_handles_hoist_out_of_branches_and_deduplicate` passes; nothing else changes. - No red: the test is in the same file as the change, so I could not run it against main's code. - Not covered: the effect on real shaders (DXIL validation, GPU dispatch, frame time). - Merges cleanly with main and with #2566. AI-assisted: Claude Code
Kal0s-design commented 2026-11-30 12:43:49 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by Kal0s-design on 2026-11-30T12:43:49Z.


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

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7278842633 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/2687#issuecomment-7278842633). Originally posted by [Kal0s-design](https://github.com/Kal0s-design) on 2026-11-30T12:43:49Z. --- **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 commented 2026-11-30 13:03:56 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by merzleckeier on 2026-11-30T13:03:56Z.


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.

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7279306042 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/2687#issuecomment-7279306042). Originally posted by [merzleckeier](https://github.com/merzleckeier) on 2026-11-30T13:03:56Z. --- **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 commented 2026-11-30 16:05:45 +00:00 (Migrated from github.com)

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


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

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7284105404 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/2687#issuecomment-7284105404). Originally posted by [KelboLive](https://github.com/KelboLive) on 2026-11-30T16:05:45Z. --- 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).
bobcatchris commented 2026-11-30 19:12:28 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by bobcatchris on 2026-11-30T19:12:28Z.


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

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7289013928 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/2687#issuecomment-7289013928). Originally posted by [bobcatchris](https://github.com/bobcatchris) on 2026-11-30T19:12:28Z. --- 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 commented 2026-12-01 11:11:23 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by aslan2142 on 2026-12-01T11:11:23Z.

Review: APPROVED


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

<!-- greyfinch-sync:ubiquitarianism/leonida:review:6130074024 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/2687#pullrequestreview-6130074024). Originally posted by [aslan2142](https://github.com/aslan2142) on 2026-12-01T11:11:23Z. **Review:** APPROVED --- 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.)*
enlight3d commented 2026-12-01 11:17:57 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by enlight3d on 2026-12-01T11:17:57Z.


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

RUN (independent run on Windows, #2687 head 40a97ef09, merged locally into main e56aff1ce 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 (#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.

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7309447313 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/2687#issuecomment-7309447313). Originally posted by [enlight3d](https://github.com/enlight3d) on 2026-12-01T11:17:57Z. --- Posted after the merge: the head tested below is the one that was merged. RUN (independent run on Windows, #2687 head 40a97ef09, merged locally into main e56aff1ce 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 (#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 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!2687
No description provided.