Skip to content

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

Merged
ThatRecompDev merged 1 commit into
ThatRecompDev:mainfrom
Sr-0w:shaders/dxil-simplifier
Nov 25, 2026
Merged

ThatRecompDev merged 1 commit into
ThatRecompDev:mainfrom
Sr-0w:shaders/dxil-simplifier

Conversation

@Sr-0w

@Sr-0w Sr-0w commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

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.ai/code/session_01SKtJsfjasPuTLtS3mEXFru

@ThatRecompDev

Copy link
Copy Markdown
Owner

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

…umbering

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 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01SKtJsfjasPuTLtS3mEXFru
@Sr-0w
Sr-0w force-pushed the shaders/dxil-simplifier branch from 846f363 to ef74563 Compare November 25, 2026 10:03
@Sr-0w Sr-0w changed the title Shaders: the DXIL backend simplifies the entry function before writing it Shaders: the DXIL backend simplifies the entry function after value numbering Nov 25, 2026
@Sr-0w

Sr-0w commented Nov 25, 2026

Copy link
Copy Markdown
Contributor Author

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.

@ThatRecompDev

Copy link
Copy Markdown
Owner

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 (AMD driver compiles take 3-8x the budget's estimate and grow quadratically: one compute program cannot be built on 64 GB #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
ThatRecompDev merged commit 3f786df into ThatRecompDev:main Nov 25, 2026
2 checks passed
@Sr-0w

Sr-0w commented Nov 25, 2026

Copy link
Copy Markdown
Contributor Author

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.

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.

2 participants