Shaders: leave out branches no lane takes before lowering (works around an out-of-memory pipeline creation) #172
No reviewers
Labels
No labels
accessibility
bug
documentation
duplicate
enhancement
good first issue
help wanted
invalid
question
wontfix
No milestone
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
github/Belobug--VIctory-Belobug-AMD!172
Loading…
Reference in a new issue
No description provided.
Delete branch "github-bot/gh-172"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
@adrianmjim on GitHub · 2026-11-25 18:48 UTC · original
adrianmjim:shaders/prune-constant-branches→main· +645 −13 · 5 files · 5 commits · closed without mergingThe structure pass proves many branch conditions constant per lane: the console compiler's scalar mask tests,
s_cbranch_vcczonexec & maskwhere every lane holds the mask, and so on. Lowering emitted them asbr i1 true/br i1 falsewith both successors, so the code behind the side no lane takes stayed in the DXIL for the host compiler to remove.emit()now turns such branches into jumps (Structured::one_way,ir::retarget). It then empties the blocks that become unreachable, reruns SSA renaming, SGPR spill forwarding, structure, I/O and resource tracking, and emits the result in the per-lane form. Some cases are left alone:LOOP_LIMITa lane leaves through the branch's other side. As a jump, it would leave the program instead.emit()first runs the existing unthreaded emit of the program as it is, and prunes only if that succeeds. A program that needs threading is threaded exactly as before.lower::wants_console_flow, with the wider numeric-select test, which can only flag more programs), nothing is pruned. The pruned form is emitted without the console flow. If it would ask for it, that emit fails and the original is used. The console flow runs blocks that no lane reaches, so pruning should not change which form a program gets.If anything on the pruned path fails, the program is emitted exactly as before.
Why
CS_ScreenProbes_ActivateRays_d60186e6is the one compute program in the title'sshaders.rpfwhose host pipeline could not be created on an RX 7900 XT:--pipelinesrun,CreateComputePipelineStatereturnedE_OUTOFMEMORY.Its module on
mainis 1,152,156 bytes. Pruned, it is 236,444 bytes, it validates, and the pipeline is created (about 210 s and a 3.9 GB peak on an uncached compile).This is a workaround, not a root-cause fix.
CS_ScreenProbes_ActivateProbeshas an even larger dead share (about 905 KB down to 153 KB) and compiles in under 10 s without this change. So dead code alone is not what the driver chokes on. Pruning brings ActivateRays under whatever the actual trigger is, which I have not found.Tested
On an RX 7900 XT (Windows 11 build 26300, Rust 1.98.1, dxil.dll from Windows Kits 10.0.26100.0), on
000ffdd:cargo test -p victory-shader --lib -- --test-threads=1: 271 passed, 0 failed, 6 ignored (main: 266, plus the 5 tests below).structure::a_branch_no_lane_takes_is_cutcoversone_wayandretarget.victory translate --validate --list --host-lanes 32-64over the title'sshaders.rpf: every count is the same as onmain(VS/PS 14,247/14,247, CS 2,682/2,682, MS 8,694 of 8,765 emitted and validated, the same 195 + 71 refusals).--pipelines) and for all 2,581 compute programs namedCS_*, ActivateRays included.main's output byte for byte.Not tested
Relation to #97
#97 adds opt-in clean-up passes over the finished bitcode (
VICTORY_SHADER_OPT, off by default). Its FOLD pass also prunes constant branches, but it only sees branches the lowering already wrote asbr i1 <constant>. This change works one stage earlier and is always on: it uses the structure pass's per-lane proof, and the loop-limit and console-flow cases stay explicit.The two compose. #97 applies cleanly on top of this branch, and the combined tree passes all 295 victory-shader lib tests. On ActivateRays:
mainOne thing for #97: with FOLD alone (
VICTORY_SHADER_OPT=2) or0x1e, this shader fails validation ("Instructions should not read uninitialized value"), with and without this PR. FOLD seems to need DEAD there.@KernelSmoker on GitHub · 2026-11-25 18:57 UTC · original
Independent verification on PR head
bfe84ee(Bazzite Linux 45 x86_64, Rust 1.98.1, NVIDIA RTX 3090 driver 620.73.04):cargo test --locked -p victory-shader: 186 passed, 0 failed, 6 ignored (lib, includingstructure::tests::a_branch_no_lane_takes_is_cut) + 3 passed, 0 failed, 1 ignored (tests/resource_tracking.rs).victory-shadertest binary run under GE-Proton 10-34 / Wine 10.0 / vkd3d-proton 3.0.0 on NVIDIA RTX 3090 (32-lane warp):emit::prune_tests(--test-threads=1): 4 passed, 0 failed (a_branch_no_lane_takes_one_way_of_is_left_out,a_loop_stopped_at_the_iteration_limit_leaves_through_its_exit,a_program_asking_for_the_console_flow_is_emitted_as_it_is,a_program_asking_for_the_console_flow_only_pruned_is_emitted_as_it_is).cargo check --workspace --all-targets --locked --target x86_64-pc-windows-gnu: passed.No game run or
shaders.rpfcorpus translation was performed here; this verifies the Linux unit tests and the 4 synthetic GPU execution tests on NVIDIA RTX 3090.@adrianmjim on GitHub · 2026-11-25 19:06 UTC · original
Thanks @KernelSmoker, good to have it on a 32-lane NVIDIA card. Since then
891a7adadds one test,a_program_that_lowers_only_threaded_is_emitted_threaded_not_pruned, for the check that prunes only programs that lower unthreaded. It needs no GPU, so the Linux run covers it too, if you rerun.@KernelSmoker on GitHub · 2026-11-25 19:16 UTC · original
Re-verified updated PR head
891a7ad(Bazzite Linux 45 x86_64, Rust 1.98.1, NVIDIA RTX 3090):victory-shadertest binary under GE-Proton 10-34 / Wine 10.0 / vkd3d-proton 3.0.0 on NVIDIA RTX 3090:emit::prune_tests(--test-threads=1): 5 passed, 0 failed, including the newa_program_that_lowers_only_threaded_is_emitted_threaded_not_pruned.cargo test --locked -p victory-shader: 186 passed, 0 failed, 6 ignored (lib) + 3 passed, 0 failed, 1 ignored (resource_tracking).mod prune_tests;incrates/victory-shader/src/emit/mod.rs:101is gated by#[cfg(all(test, windows))], soa_program_that_lowers_only_threaded_is_emitted_threaded_not_pruneddoes not currently run on Linux (0 matched when filtering for it on Linux). Gating only the GPU-running tests/helpers insideprune_tests.rswith#[cfg(windows)]would let the non-GPU tests run on Linux too.@adrianmjim on GitHub · 2026-11-25 19:20 UTC · original
Thanks, you're right: I claimed the Linux run would cover it and it didn't.
7304965buildsprune_testseverywhere (#[cfg(test)]); only the GPU runs (oneruns()helper,Noneoff Windows, as without an SM 6.6 device) and the DXIL validator stay Windows only. So on Linux all five tests now run their translation checks, includinga_program_that_lowers_only_threaded_is_emitted_threaded_not_pruned, which needs no GPU at all.Checked here on Windows (RX 7900 XT):
cargo test -p victory-shader --lib -- --test-threads=1272 passed, 0 failed, 6 ignored; the five prune tests pass with the GPU runs. I have no Linux target installed, so I checked the off-Windows path by building it on Windows with the twocfgs swapped: the five pass, the two GPU runs report skipped, and no new warnings. A Linux run of the new head would confirm it for real.@KernelSmoker on GitHub · 2026-11-25 19:24 UTC · original
Confirmed on Linux at PR head
7304965(Bazzite Linux 45 x86_64, Rust 1.98.1):cargo test --locked -p victory-shader prune_tests -- --nocapture: 5 passed, 0 failed (the two GPU runs printno D3D12 host off Windows; skipped, all 5 translation checks includinga_program_that_lowers_only_threaded_is_emitted_threaded_not_prunedrun and pass, and no new warnings inprune_tests.rs).cargo test --locked -p victory-shader: 191 passed, 0 failed, 6 ignored (lib) + 3 passed, 0 failed, 1 ignored (tests/resource_tracking.rs).@adrianmjim on GitHub · 2026-11-25 23:30 UTC · original
Update after
mainmoved to4f62c2a, which changes what this PR is for.The out-of-memory case is gone on
main. The clean-up passes (ae75a66, on by default) bringCS_ScreenProbes_ActivateRaysfrom 1,152,156 to 147,544 bytes of DXIL on their own, and the pipeline is created. WithoutVICTORY_RAYS,6986a57also leaves that program out in the game. So the description's "Why" no longer holds as written.What pruning still changes on top of the passes. Measured over
shaders.rpfwithtranslate --validate --list --host-lanes 32-64,4f62c2aagainst4f62c2a+ this branch, with the passes on and every program's DXIL hashed and dumped:ActivateRaysgives 27 KB of it, going from 147,544 to 120,216 bytes (−18.5%).join_scalars: a rank,WaveActiveMax, a select per scalar). InActivateRaysthere are 986waveActiveOpcalls as it is and 405 pruned. The passes fold the branch but keep those calls.SV_Barycentricsand its feature flag, interpolation modes). For example,ptfxPS_Transmission_alphathreshold_mv_uvdistortgoes from 6,328 to 3,632 bytes. Pruning also changes the pixel input signature there (those inputs become linear, andSV_Barycentricsis gone), but only for inputs that the live code doesn't read.original.is_ok()check) and once pruned.ActivateRayson an RX 7900 XT with a cold driver cache took 168 s against 195 s and about 2.9 GB against 3.1 GB of peak memory. That is one run each, on a loaded machine, with a diagnostics build whose loop limit was changed so the driver cache was cold, and the times include translation. So read it only as "somewhat less".The tests. Merged with
4f62c2a, three of the prune tests failed. TheFOLDpass alone makes the unpruned module of those small programs byte-identical to the pruned one, and with the passes off they all pass. The branch now mergesmain(no conflicts) anddc66cbadoes two things:#[cfg(test)]per-thread override ofshader_opt()does this; production is unchanged.cargo test -p victory-shader --lib -- --test-threads=1: 284 passed, 0 failed, 6 ignored. Two mutations were checked:original.is_ok()check withtruefails the threaded test.So this is now a smaller and somewhat cheaper-to-compile module for about 700 programs, not a fix for a pipeline that could not be created. Whether that is worth a second lowering for those programs is your call. I'm fine closing it if not.
@MamaLeVrai on GitHub · 2026-11-26 00:18 UTC · original
RDNA2 result. Hardware: RX 6950 XT, Windows 11 build 26300, Rust 1.98.1. This PR (
dc66cba) merged onmain4f62c2a. Command:cargo test --release -p victory-shader --lib, two runs.main: 275 passed, 2 failed.maintoo.)The 2 failures are the same on
main:scalar_counted_in_a_divergent_loop_read_after_itandscalar_counted_in_nested_divergent_loops, the RDNA2 wave-size tests that #110 and #204 fix. So nothing regresses on this card. Not run in the game.harness machine: rx6950xt-w11 | AMD Radeon RX 6950 XT (RDNA2), driver 32.0.21045.29003 | 32 GB RAM | Windows 11 build 26300
AI-assisted (Claude Code); no game data.
@ -364,0 +378,4 @@// Pruned only where the program lowers as it is, unthreaded: what the// console flow check below looks at is then the program emitted.if original.is_ok()&& let Some(made) = emit_without_dead_branches(c, native, p, st, io, res, target)@Copilot on GitHub · 2026-11-25 18:51 UTC · original
The unthreaded-success gate has no regression test: the new tests cover pruning and both console-flow exclusions, while
threading.rstests only the IR transformation. Please add a synthetic program with a prunable branch that fails unthreaded emission but succeeds after threading, and assert thatemit()stays byte-identical toemit_threaded(..., true). This would protect the compatibility rule currently checked only by the external shader corpus.@ -364,0 +378,4 @@// Pruned only where the program lowers as it is, unthreaded: what the// console flow check below looks at is then the program emitted.if original.is_ok()&& let Some(made) = emit_without_dead_branches(c, native, p, st, io, res, target)@adrianmjim on GitHub · 2026-11-25 19:06 UTC · original
Added in
891a7ad:a_program_that_lowers_only_threaded_is_emitted_threaded_not_pruned(GPU-free, inemit/prune_tests.rs).The program is
ONE_WAY(one branch no lane takes one way of) followed by a uniform fork on the group's x: one way sets m0 and a flag, and the join reads m0 only where the flag is set. As it is, the join takes m0 from both ways, from the other as the hardware left it, soemit_program(.., true)fails withUnsupported { what: "a hardware SGPR input" }(the error 3 of the 4 programs of the title's archive that meet this check hit). Threading sends the fork's other way past the read, andemit_threaded(.., true)lowers it.The test asserts that
emit()returns the sameHostShaderasemit_threaded(.., true), and thatemit_without_dead_brancheswould have given a different program (it prunes, then threads the pruned form). With the check replaced bytruethe test fails.cargo test -p victory-shader --lib -- --test-threads=1: 272 passed, 0 failed, 6 ignored (RX 7900 XT, Windows 11).@copilot-pull-request-reviewer[bot] on GitHub · 2026-11-25 18:51 UTC · original · reviewed
🟡 Changes recommended
The unthreaded-success gate lacks a regression test confirming that a program which fails unthreaded emission but succeeds after threading preserves byte-identical output.
1 open issue
What changed in this PR
This PR removes branches that no shader lane takes before DXIL lowering, reducing generated code to work around excessive host compiler memory use during pipeline creation.
Changes:
crates/victory-shader/src/structure.rscrates/victory-shader/src/ir.rscrates/victory-shader/src/emit/prune_tests.rscrates/victory-shader/src/emit/mod.rscrates/victory-shader/src/emit/lower.rsReview effort: Balanced
@adrianmjim on GitHub · 2026-11-27 09:25 UTC · original
Closing, since
mainis no longer the line being developed. Ona-publierthe clean-up passes are on as well, and this pruning would change 709 of 25,694 programs, all but 8 smaller, about 44 KB in total (CS_ScreenProbes_ActivateRays119,148 → 106,264 bytes). The out-of-memory case that motivated it no longer happens. Not worth a second lowering per program for that. Thanks to everyone who tested it.Pull request closed