Shaders: the DXIL backend simplifies the entry function after value numbering #24

Manually merged
github merged 1 commit from gh-pr/24/head into gh-pr/24/base 2026-11-27 03:14:14 +00:00
Owner

@⁠Sr-0w on GitHub · 2026-11-25 09:16 UTC · original
Sr-0w:shaders/dxil-simplifier → main · +508 −3 · 6 files · 1 commits · merged 2026-11-25 10:08 UTC

Rebased onto main after 9b01294: this is now only a Simplifier pass after ValueNumbering and DeadCode (both skipped by --no-optimize), with the parts main doesn't have:

  • a block reached only by its predecessor's unconditional branch joins it;
  • a branch on a constant or to one block twice becomes unconditional; a phi of one value is that value, and ConstantFolding folds what that decides;
  • wave and quad reads (nounwind, so nothing removed them) that nothing reads go;
  • one equal to an earlier one in its block, with no effect between, is shared.

My DominatorTree/ValueKey/NumberingWalk/ConstantFolder are gone; it reuses ConstantFolding, DominatorTree and ValueNumbering.Key (now internal). Instruction.Block gets an internal setter since merged instructions change block. My old folder had nothing yours misses that mattered on these programs, so ConstantFolding.cs is unchanged.

Program after ValueNumbering after Simplifier AMD RX 7800 XT pipeline
CS_RaytracingResolve (wave32) 1.57 MB 1.06 MB (-32%) 36.5 s / 8.4 GB -> 26.2 s / 3.6 GB
CS_ScreenProbes_ActivateProbes (wave64) 9.98 MB 8.50 MB (-15%) still over 50 GB

Tests: DxilSimplifierTests (WARP: same words optimized and plain, smaller module; IR: chain merge, constant branch fold, wave read shared and unread one dropped). Architecture and Shaders tests green. Four CompileBudgetTests fail on a clean main too (8531204), unrelated.

🤖 Generated with Claude Code

**@⁠Sr-0w** on GitHub · 2026-11-25 09:16 UTC · [original](https://github.com/ThatRecompDev/five-star-recomp/pull/24) `Sr-0w:shaders/dxil-simplifier` → `main` · +508 −3 · 6 files · 1 commits · merged 2026-11-25 10:08 UTC Rebased onto main after 9b01294: this is now only a `Simplifier` pass after `ValueNumbering` and `DeadCode` (both skipped by `--no-optimize`), with the parts main doesn't have: - a block reached only by its predecessor's unconditional branch joins it; - a branch on a constant or to one block twice becomes unconditional; a phi of one value is that value, and `ConstantFolding` folds what that decides; - wave and quad reads (nounwind, so nothing removed them) that nothing reads go; - one equal to an earlier one in its block, with no effect between, is shared. My DominatorTree/ValueKey/NumberingWalk/ConstantFolder are gone; it reuses `ConstantFolding`, `DominatorTree` and `ValueNumbering.Key` (now internal). `Instruction.Block` gets an internal setter since merged instructions change block. My old folder had nothing yours misses that mattered on these programs, so `ConstantFolding.cs` is unchanged. | Program | after `ValueNumbering` | after `Simplifier` | AMD RX 7800 XT pipeline | |---|---|---|---| | `CS_RaytracingResolve` (wave32) | 1.57 MB | 1.06 MB (-32%) | 36.5 s / 8.4 GB -> 26.2 s / 3.6 GB | | `CS_ScreenProbes_ActivateProbes` (wave64) | 9.98 MB | 8.50 MB (-15%) | still over 50 GB | Tests: `DxilSimplifierTests` (WARP: same words optimized and plain, smaller module; IR: chain merge, constant branch fold, wave read shared and unread one dropped). Architecture and Shaders tests green. Four `CompileBudgetTests` fail on a clean main too (8531204), unrelated. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
A Simplifier pass after ValueNumbering and DeadCode: block chains reached
only by an unconditional branch merge, branches on a constant or to one
block twice become unconditional, phis of one value are that value and
ConstantFolding folds what that decides. Wave and quad reads are nounwind,
so nothing removed them: unread ones go, and one equal to an earlier one in
its block with no effect between is shared. Reuses ConstantFolding,
DominatorTree and ValueNumbering.Key from main.

RaytracingResolve 1.57 -> 1.06 MB of DXIL, AMD pipeline 36.5 s / 8.4 GB
-> 26.2 s / 3.6 GB; ActivateProbes 9.98 -> 8.50 MB.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01SKtJsfjasPuTLtS3mEXFru
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-27 03:14:13 +00:00
github manually merged commit ef74563f86 into gh-pr/24/base 2026-11-27 03:14:14 +00:00
Author
Owner

@⁠ThatRecompDev on GitHub · 2026-11-25 09:34 UTC · original

thanks a lot for this! the numbers are great, especially the AMD timings. while this PR was open, main got an overlapping pass (9b01294): constant folding in IrBuilder and a dominator-scoped ValueNumbering with entry hoisting, so the two now conflict.

on the same programs yours still cuts noticeably more, mostly from the parts main doesn't have: merging block chains, folding constant and same-target branches, dropping unread wave reads (nice catch on nounwind keeping them all) and sharing wave reads within a block. could you rebase onto main and keep just those as a Simplifier pass after ValueNumbering? then your DominatorTree/ValueKey/NumberingWalk/ConstantFolder can go, and anything ConstantFolder folds that ours misses can move into Dxil/Ir/ConstantFolding.cs. happy to help with the rebase if it's a pain

**@⁠ThatRecompDev** on GitHub · 2026-11-25 09:34 UTC · [original](https://github.com/ThatRecompDev/five-star-recomp/pull/24#issuecomment-7158504005) thanks a lot for this! the numbers are great, especially the AMD timings. while this PR was open, main got an overlapping pass (9b01294): constant folding in IrBuilder and a dominator-scoped ValueNumbering with entry hoisting, so the two now conflict. on the same programs yours still cuts noticeably more, mostly from the parts main doesn't have: merging block chains, folding constant and same-target branches, dropping unread wave reads (nice catch on nounwind keeping them all) and sharing wave reads within a block. could you rebase onto main and keep just those as a Simplifier pass after ValueNumbering? then your DominatorTree/ValueKey/NumberingWalk/ConstantFolder can go, and anything ConstantFolder folds that ours misses can move into Dxil/Ir/ConstantFolding.cs. happy to help with the rebase if it's a pain
Author
Owner

@⁠Sr-0w on GitHub · 2026-11-25 10:03 UTC · original

Rebased onto main and cut down to just those parts as a Simplifier after ValueNumbering/DeadCode, reusing your ConstantFolding, DominatorTree and Key; description updated with fresh numbers (RaytracingResolve 1.57 -> 1.06 MB, AMD 36.5 s / 8.4 GB -> 26.2 s / 3.6 GB). Next I'd like to send a follow-up to ValueNumbering: hoisting a pure instruction to the deepest block defining its operands rather than only the entry. On ActivateProbes the formatted-load decode of one descriptor (loaded once, not in the entry) is recomputed in each of 3,281 sibling branches.

**@⁠Sr-0w** on GitHub · 2026-11-25 10:03 UTC · [original](https://github.com/ThatRecompDev/five-star-recomp/pull/24#issuecomment-7159158734) Rebased onto main and cut down to just those parts as a `Simplifier` after `ValueNumbering`/`DeadCode`, reusing your ConstantFolding, DominatorTree and Key; description updated with fresh numbers (RaytracingResolve 1.57 -> 1.06 MB, AMD 36.5 s / 8.4 GB -> 26.2 s / 3.6 GB). Next I'd like to send a follow-up to ValueNumbering: hoisting a pure instruction to the deepest block defining its operands rather than only the entry. On ActivateProbes the formatted-load decode of one descriptor (loaded once, not in the entry) is recomputed in each of 3,281 sibling branches.
Author
Owner

@⁠ThatRecompDev on GitHub · 2026-11-25 10:06 UTC · original

nice, that's exactly what i had in mind. the rebased version reads really clean and the AMD numbers speak for themselves (26 s / 3.6 GB vs 36.5 s / 8.4 GB). i'll merge once windows CI is green.

the hoisting follow-up sounds good, go for it. 3,281 copies of the same decode is a lot.

since you've got an AMD card and clearly know this area, here are a few more in the same category if you feel like it (no pressure, pick whatever sounds fun):

  1. the select(active, …) wave emulation. you noted this is what keeps ActivateProbes over 50 GB on AMD. a lot of those selects sit in regions where exec is known full or uniform, so they could be dropped there, or emitted once per region instead of per instruction. that's in the translator (Translation/Waves), not the backend.
  2. AMD compile estimates (#19). CompileBudget guesses AMD driver memory per program (quadratic, plus 40 GB per MB above 1 MB, capped at 48 GB). real measurements from a 7800 XT over a spread of program sizes, now with your Simplifier in, would let us fit the curve properly instead of guessing. a small table of DXIL size / blocks / create time / peak memory would be perfect.
  3. a backend benchmark command. something like fivestar shader dxil --bench <list> that builds a fixed set of the heaviest programs and prints size, blocks and pipeline create time/memory, so changes like yours can be compared in one run on any card.

thanks again, this is a really solid contribution!

**@⁠ThatRecompDev** on GitHub · 2026-11-25 10:06 UTC · [original](https://github.com/ThatRecompDev/five-star-recomp/pull/24#issuecomment-7159244447) nice, that's exactly what i had in mind. the rebased version reads really clean and the AMD numbers speak for themselves (26 s / 3.6 GB vs 36.5 s / 8.4 GB). i'll merge once windows CI is green. the hoisting follow-up sounds good, go for it. 3,281 copies of the same decode is a lot. since you've got an AMD card and clearly know this area, here are a few more in the same category if you feel like it (no pressure, pick whatever sounds fun): 1. **the `select(active, …)` wave emulation.** you noted this is what keeps ActivateProbes over 50 GB on AMD. a lot of those selects sit in regions where exec is known full or uniform, so they could be dropped there, or emitted once per region instead of per instruction. that's in the translator (Translation/Waves), not the backend. 2. **AMD compile estimates (#19).** CompileBudget guesses AMD driver memory per program (quadratic, plus 40 GB per MB above 1 MB, capped at 48 GB). real measurements from a 7800 XT over a spread of program sizes, now with your Simplifier in, would let us fit the curve properly instead of guessing. a small table of DXIL size / blocks / create time / peak memory would be perfect. 3. **a backend benchmark command.** something like `fivestar shader dxil --bench <list>` that builds a fixed set of the heaviest programs and prints size, blocks and pipeline create time/memory, so changes like yours can be compared in one run on any card. thanks again, this is a really solid contribution!
Author
Owner

@⁠Sr-0w on GitHub · 2026-11-25 10:16 UTC · original

Correction on the follow-up I mentioned: I tried hoisting pure instructions to the deepest block defining their operands, and it removes the duplicated descriptor decode, but AMD likes it less. RaytracingResolve went 26.2 s / 3.6 GB -> 55.6 s / 3.7 GB, and 45 GB together with a narrower formatted-load decode, because the live ranges get longer. So I'm not sending it; nothing changes for this PR.

**@⁠Sr-0w** on GitHub · 2026-11-25 10:16 UTC · [original](https://github.com/ThatRecompDev/five-star-recomp/pull/24#issuecomment-7159461732) Correction on the follow-up I mentioned: I tried hoisting pure instructions to the deepest block defining their operands, and it removes the duplicated descriptor decode, but AMD likes it less. RaytracingResolve went 26.2 s / 3.6 GB -> 55.6 s / 3.7 GB, and 45 GB together with a narrower formatted-load decode, because the live ranges get longer. So I'm not sending it; nothing changes for this PR.
This discussion has been locked. Commenting is limited to contributors.
No description provided.