Shaders: leave out branches no lane takes before lowering (works around an out-of-memory pipeline creation) #172

Closed
github-bot wants to merge 5 commits from github-bot/gh-172 into main AGit
Owner

@⁠adrianmjim on GitHub · 2026-11-25 18:48 UTC · original
adrianmjim:shaders/prune-constant-branches → main · +645 −13 · 5 files · 5 commits · closed without merging

The structure pass proves many branch conditions constant per lane: the console compiler's scalar mask tests, s_cbranch_vccz on exec & mask where every lane holds the mask, and so on. Lowering emitted them as br i1 true / br i1 false with 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 back edges. If the side every lane takes is a loop's back edge, the branch stays. Lowering counts loop iterations, and at LOOP_LIMIT a lane leaves through the branch's other side. As a jump, it would leave the program instead.
  • Programs that do not lower as they are, unthreaded. 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.
  • The console flow. If the program as it is asks for the console flow (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_d60186e6 is the one compute program in the title's shaders.rpf whose host pipeline could not be created on an RX 7900 XT:

  • In a --pipelines run, CreateComputePipelineState returned E_OUTOFMEMORY.
  • Run alone, the process passed 20 GB private before it was stopped.

Its module on main is 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_ActivateProbes has 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).
  • New tests use synthetic GFX10 words only and run on the default adapter:
    • a branch no lane takes one way of is left out: the DXIL is smaller and validates, with the same per-lane results as unpruned;
    • a loop stopped at the iteration limit leaves through its exit: the same stored values pruned and unpruned (with the back edge made a jump, it stored nothing);
    • a program asking for the console flow only through dead code is emitted byte-identical to the unpruned path;
    • a program asking for the console flow only once pruned (its permutation was dead) is emitted as it is;
    • structure::a_branch_no_lane_takes_is_cut covers one_way and retarget.
    • Each of the first three fails when the corresponding exclusion is switched off locally. The "unthreaded first" rule has no unit test; the corpus run below shows its effect.
  • victory translate --validate --list --host-lanes 32-64 over the title's shaders.rpf: every count is the same as on main (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).
  • With a local, uncommitted per-program DXIL hash list:
    • 2,566 programs change (1,752 PS, 809 CS, 3 VS, 2 MS), and all of them validate.
    • Pipelines were created for every changed program (--pipelines) and for all 2,581 compute programs named CS_*, ActivateRays included.
    • 4 programs whose originals need threading keep main's output byte for byte.

Not tested

  • No game run. The game was not run here (no executable), so this shows only that the shaders translate, validate and create pipelines, not that they render the same. Equal results pruned and unpruned are shown for the synthetic test programs only.
  • Driver shader cache. Most pipeline creations in these runs probably hit the AMD driver's shader cache (a repeat of the same DXIL takes seconds). The ActivateRays timing above is from an uncached compile.
  • Other hardware. Only an AMD RDNA3 host (32-64 lanes) was used. The new GPU tests are Windows-only, like the other emit GPU tests.

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 as br 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:

DXIL bytes validates
main 1,152,156 yes
this PR 236,444 yes
#97 alone, all passes 147,544 yes
both, all passes 120,216 yes

One thing for #97: with FOLD alone (VICTORY_SHADER_OPT=2) or 0x1e, this shader fails validation ("Instructions should not read uninitialized value"), with and without this PR. FOLD seems to need DEAD there.

**@⁠adrianmjim** on GitHub · 2026-11-25 18:48 UTC · [original](https://github.com/Belobug/VIctory-Belobug-AMD/pull/172) `adrianmjim:shaders/prune-constant-branches` → `main` · +645 −13 · 5 files · 5 commits · closed without merging The structure pass proves many branch conditions constant per lane: the console compiler's scalar mask tests, `s_cbranch_vccz` on `exec & mask` where every lane holds the mask, and so on. Lowering emitted them as `br i1 true` / `br i1 false` with 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 back edges.** If the side every lane takes is a loop's back edge, the branch stays. Lowering counts loop iterations, and at `LOOP_LIMIT` a lane leaves through the branch's other side. As a jump, it would leave the program instead. - **Programs that do not lower as they are, unthreaded.** `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. - **The console flow.** If the program as it is asks for the console flow (`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_d60186e6` is the one compute program in the title's `shaders.rpf` whose host pipeline could not be created on an RX 7900 XT: - In a `--pipelines` run, `CreateComputePipelineState` returned `E_OUTOFMEMORY`. - Run alone, the process passed 20 GB private before it was stopped. Its module on `main` is 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_ActivateProbes` has 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). - New tests use synthetic GFX10 words only and run on the default adapter: - a branch no lane takes one way of is left out: the DXIL is smaller and validates, with the same per-lane results as unpruned; - a loop stopped at the iteration limit leaves through its exit: the same stored values pruned and unpruned (with the back edge made a jump, it stored nothing); - a program asking for the console flow only through dead code is emitted byte-identical to the unpruned path; - a program asking for the console flow only once pruned (its permutation was dead) is emitted as it is; - `structure::a_branch_no_lane_takes_is_cut` covers `one_way` and `retarget`. - Each of the first three fails when the corresponding exclusion is switched off locally. The "unthreaded first" rule has no unit test; the corpus run below shows its effect. - `victory translate --validate --list --host-lanes 32-64` over the title's `shaders.rpf`: every count is the same as on `main` (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). - With a local, uncommitted per-program DXIL hash list: - 2,566 programs change (1,752 PS, 809 CS, 3 VS, 2 MS), and all of them validate. - Pipelines were created for every changed program (`--pipelines`) and for all 2,581 compute programs named `CS_*`, ActivateRays included. - 4 programs whose originals need threading keep `main`'s output byte for byte. ## Not tested - **No game run.** The game was not run here (no executable), so this shows only that the shaders translate, validate and create pipelines, not that they render the same. Equal results pruned and unpruned are shown for the synthetic test programs only. - **Driver shader cache.** Most pipeline creations in these runs probably hit the AMD driver's shader cache (a repeat of the same DXIL takes seconds). The ActivateRays timing above is from an uncached compile. - **Other hardware.** Only an AMD RDNA3 host (32-64 lanes) was used. The new GPU tests are Windows-only, like the other emit GPU tests. ## 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 as `br 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: | | DXIL bytes | validates | |---|---|---| | `main` | 1,152,156 | yes | | this PR | 236,444 | yes | | #97 alone, all passes | 147,544 | yes | | both, all passes | 120,216 | yes | One thing for #97: with FOLD alone (`VICTORY_SHADER_OPT=2`) or `0x1e`, this shader fails validation ("Instructions should not read uninitialized value"), with and without this PR. FOLD seems to need DEAD there.
The structure pass proves many branch conditions constant per lane (the
console compiler's scalar mask tests, vccz on exec & a mask every lane
has). They were lowered as `br i1 true/false`, so the code behind the
other way stayed in the DXIL for the host compiler to remove.

emit() now makes those branches jumps, empties the blocks that are then
unreachable, rebuilds SSA, structure, I/O and resource tracking, and
emits that program in the per-lane form. It does not do so for:

- a branch whose way every lane takes is a loop's back edge: at the
  iteration limit a lane leaves such a loop through the other way, as it
  does without this change; as a jump it would leave the program;
- a program that does not lower as it is, unthreaded (it is threaded
  as before), or whose lowering asks for the console flow (checked
  before pruning) or whose pruned form does: the console flow runs
  blocks no lane reaches.

Where pruning fails, the program is emitted as before.

This works around, and does not explain, the AMD driver running out of
memory on CS_ScreenProbes_ActivateRays (Grand Theft Auto VI): creating
its pipeline grew past 20 GB (E_OUTOFMEMORY in a --pipelines run). With
the dead code left out its module is about a fifth of the size and the
pipeline is created. CS_ScreenProbes_ActivateProbes has a larger share
of such dead code and compiles in seconds without this change, so the
dead code alone is not what the driver chokes on; it only brings
ActivateRays under whatever the trigger is.

Tests: a branch no lane takes is left out with the same results on the
GPU; a loop stopped at the iteration limit stores the same values as
before (it stored nothing when its back edge became a jump); a program
asking for the console flow only through dead code is emitted as it is,
and so is one that asks for it only pruned (its permutation was dead).
emit() prunes only a program that lowers as it is, unthreaded; no test
held it to that. The new program has a branch no lane takes one way of
and a uniform fork whose one way sets m0 and a flag the join tests: as it
is, the join reads m0 from both ways, from the other as the hardware left
it (unsupported in a compute program), and threading makes it lower.
emit() gives exactly what emit_threaded() does, while pruning alone would
give a different program, so the test fails without the check.
The module was built only on Windows, so its translation checks, the
one-way branch counts, the DXIL differences and the check that a program
lowering only threaded is emitted threaded, did not run on Linux, where
none of them needs D3D12. The module now builds everywhere; running the
programs on the default adapter and the DXIL validator stay Windows only
(skipped elsewhere, as without an SM 6.6 device).
The clean-up passes main runs over the emitted module fold a branch on a
constant, and the writer lays out only the blocks still reached, so for
the small programs here the module emitted as it is ends byte for byte
the pruned one. The one-way, loop and threaded tests are about what the
lowering makes, so their emissions run with the passes off, through a
test-only override of shader_opt on the current thread.

A new test checks on default passes that pruning still changes the
module emitted: a scalar from the group's x set on both ways of a branch
no lane takes one way of is joined with wave operations as it is, which
the passes keep, and not pruned. It is validated and run on the GPU both
ways.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 03:37:10 +00:00
Author
Owner

@⁠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):

  • Linux cargo test --locked -p victory-shader: 186 passed, 0 failed, 6 ignored (lib, including structure::tests::a_branch_no_lane_takes_is_cut) + 3 passed, 0 failed, 1 ignored (tests/resource_tracking.rs).
  • Windows GNU victory-shader test 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.rpf corpus translation was performed here; this verifies the Linux unit tests and the 4 synthetic GPU execution tests on NVIDIA RTX 3090.

**@⁠KernelSmoker** on GitHub · 2026-11-25 18:57 UTC · [original](https://github.com/Belobug/VIctory-Belobug-AMD/pull/172#issuecomment-7171638717) Independent verification on PR head `bfe84ee` (Bazzite Linux 45 x86_64, Rust 1.98.1, NVIDIA RTX 3090 driver 620.73.04): - Linux `cargo test --locked -p victory-shader`: **186 passed, 0 failed, 6 ignored** (lib, including `structure::tests::a_branch_no_lane_takes_is_cut`) + **3 passed, 0 failed, 1 ignored** (`tests/resource_tracking.rs`). - Windows GNU `victory-shader` test 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.rpf` corpus translation was performed here; this verifies the Linux unit tests and the 4 synthetic GPU execution tests on NVIDIA RTX 3090.
Author
Owner

@⁠adrianmjim on GitHub · 2026-11-25 19:06 UTC · original

Thanks @⁠KernelSmoker, good to have it on a 32-lane NVIDIA card. Since then 891a7ad adds 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.

**@⁠adrianmjim** on GitHub · 2026-11-25 19:06 UTC · [original](https://github.com/Belobug/VIctory-Belobug-AMD/pull/172#issuecomment-7171830738) Thanks @⁠KernelSmoker, good to have it on a 32-lane NVIDIA card. Since then `891a7ad` adds 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.
Author
Owner

@⁠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):

  • Windows GNU victory-shader test 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 new a_program_that_lowers_only_threaded_is_emitted_threaded_not_pruned.
  • Linux cargo test --locked -p victory-shader: 186 passed, 0 failed, 6 ignored (lib) + 3 passed, 0 failed, 1 ignored (resource_tracking).
  • Quick heads-up: mod prune_tests; in crates/victory-shader/src/emit/mod.rs:101 is gated by #[cfg(all(test, windows))], so a_program_that_lowers_only_threaded_is_emitted_threaded_not_pruned does not currently run on Linux (0 matched when filtering for it on Linux). Gating only the GPU-running tests/helpers inside prune_tests.rs with #[cfg(windows)] would let the non-GPU tests run on Linux too.
**@⁠KernelSmoker** on GitHub · 2026-11-25 19:16 UTC · [original](https://github.com/Belobug/VIctory-Belobug-AMD/pull/172#issuecomment-7172073578) Re-verified updated PR head `891a7ad` (Bazzite Linux 45 x86_64, Rust 1.98.1, NVIDIA RTX 3090): - Windows GNU `victory-shader` test 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 new `a_program_that_lowers_only_threaded_is_emitted_threaded_not_pruned`. - Linux `cargo test --locked -p victory-shader`: **186 passed, 0 failed, 6 ignored** (lib) + **3 passed, 0 failed, 1 ignored** (`resource_tracking`). - Quick heads-up: `mod prune_tests;` in `crates/victory-shader/src/emit/mod.rs:101` is gated by `#[cfg(all(test, windows))]`, so `a_program_that_lowers_only_threaded_is_emitted_threaded_not_pruned` does not currently run on Linux (0 matched when filtering for it on Linux). Gating only the GPU-running tests/helpers inside `prune_tests.rs` with `#[cfg(windows)]` would let the non-GPU tests run on Linux too.
Author
Owner

@⁠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. 7304965 builds prune_tests everywhere (#[cfg(test)]); only the GPU runs (one runs() helper, None off 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, including a_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=1 272 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 two cfgs 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.

**@⁠adrianmjim** on GitHub · 2026-11-25 19:20 UTC · [original](https://github.com/Belobug/VIctory-Belobug-AMD/pull/172#issuecomment-7172151138) Thanks, you're right: I claimed the Linux run would cover it and it didn't. `7304965` builds `prune_tests` everywhere (`#[cfg(test)]`); only the GPU runs (one `runs()` helper, `None` off 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, including `a_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=1` 272 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 two `cfg`s 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.
Author
Owner

@⁠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 print no D3D12 host off Windows; skipped, all 5 translation checks including a_program_that_lowers_only_threaded_is_emitted_threaded_not_pruned run and pass, and no new warnings in prune_tests.rs).
  • Full cargo test --locked -p victory-shader: 191 passed, 0 failed, 6 ignored (lib) + 3 passed, 0 failed, 1 ignored (tests/resource_tracking.rs).
**@⁠KernelSmoker** on GitHub · 2026-11-25 19:24 UTC · [original](https://github.com/Belobug/VIctory-Belobug-AMD/pull/172#issuecomment-7172245566) 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 print `no D3D12 host off Windows; skipped`, all 5 translation checks including `a_program_that_lowers_only_threaded_is_emitted_threaded_not_pruned` run and pass, and no new warnings in `prune_tests.rs`). - Full `cargo test --locked -p victory-shader`: **191 passed, 0 failed, 6 ignored** (lib) + **3 passed, 0 failed, 1 ignored** (`tests/resource_tracking.rs`).
Author
Owner

@⁠adrianmjim on GitHub · 2026-11-25 23:30 UTC · original

Update after main moved to 4f62c2a, which changes what this PR is for.

The out-of-memory case is gone on main. The clean-up passes (ae75a66, on by default) bring CS_ScreenProbes_ActivateRays from 1,152,156 to 147,544 bytes of DXIL on their own, and the pipeline is created. Without VICTORY_RAYS, 6986a57 also 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.rpf with translate --validate --list --host-lanes 32-64, 4f62c2a against 4f62c2a + this branch, with the passes on and every program's DXIL hashed and dumped:

  • 693 of 25,623 programs change: 599 pixel, 89 compute, 3 vertex and 2 mesh. Every one of them still validates, and every one whose size changes gets smaller. The validated counts and refusals are the same on both.
  • The total is small: about 65 KB less DXIL. ActivateRays gives 27 KB of it, going from 147,544 to 120,216 bytes (−18.5%).
  • What the passes leave behind, in compute programs: the cross-lane join code that the lowering emits for a dead predecessor (join_scalars: a rank, WaveActiveMax, a select per scalar). In ActivateRays there are 986 waveActiveOp calls as it is and 405 pruned. The passes fold the branch but keep those calls.
  • What the passes leave behind, in pixel programs: module-level declarations that come from the dead code (samplers, an SRV, SV_Barycentrics and its feature flag, interpolation modes). For example, ptfx PS_Transmission_alphathreshold_mv_uvdistort goes from 6,328 to 3,632 bytes. Pruning also changes the pixel input signature there (those inputs become linear, and SV_Barycentrics is gone), but only for inputs that the live code doesn't read.
  • The cost: a program with a forward one-way branch is lowered and optimised twice, once as it is (the original.is_ok() check) and once pruned.
  • The pipeline: creating ActivateRays on 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. The FOLD pass 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 merges main (no conflicts) and dc66cba does two things:

  • The one-way, loop and threaded tests compare what the lowering makes, with the passes off. A #[cfg(test)] per-thread override of shader_opt() does this; production is unchanged.
  • A new test checks with the passes on that pruning still changes the module. It uses a scalar taken from the group id, so a later fold of wave operations on constants would not remove the difference.

cargo test -p victory-shader --lib -- --test-threads=1: 284 passed, 0 failed, 6 ignored. Two mutations were checked:

  • turning pruning off fails the one-way, loop, threaded and new tests;
  • replacing the original.is_ok() check with true fails 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.

**@⁠adrianmjim** on GitHub · 2026-11-25 23:30 UTC · [original](https://github.com/Belobug/VIctory-Belobug-AMD/pull/172#issuecomment-7177654774) Update after `main` moved to `4f62c2a`, which changes what this PR is for. **The out-of-memory case is gone on `main`.** The clean-up passes (`ae75a66`, on by default) bring `CS_ScreenProbes_ActivateRays` from 1,152,156 to 147,544 bytes of DXIL on their own, and the pipeline is created. Without `VICTORY_RAYS`, `6986a57` also 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.rpf` with `translate --validate --list --host-lanes 32-64`, `4f62c2a` against `4f62c2a` + this branch, with the passes on and every program's DXIL hashed and dumped: - **693 of 25,623 programs change:** 599 pixel, 89 compute, 3 vertex and 2 mesh. Every one of them still validates, and every one whose size changes gets smaller. The validated counts and refusals are the same on both. - **The total is small:** about 65 KB less DXIL. `ActivateRays` gives 27 KB of it, going from 147,544 to 120,216 bytes (−18.5%). - **What the passes leave behind, in compute programs:** the cross-lane join code that the lowering emits for a dead predecessor (`join_scalars`: a rank, `WaveActiveMax`, a select per scalar). In `ActivateRays` there are 986 `waveActiveOp` calls as it is and 405 pruned. The passes fold the branch but keep those calls. - **What the passes leave behind, in pixel programs:** module-level declarations that come from the dead code (samplers, an SRV, `SV_Barycentrics` and its feature flag, interpolation modes). For example, `ptfx` `PS_Transmission_alphathreshold_mv_uvdistort` goes from 6,328 to 3,632 bytes. Pruning also changes the pixel input signature there (those inputs become linear, and `SV_Barycentrics` is gone), but only for inputs that the live code doesn't read. - **The cost:** a program with a forward one-way branch is lowered and optimised twice, once as it is (the `original.is_ok()` check) and once pruned. - **The pipeline:** creating `ActivateRays` on 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. The `FOLD` pass 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 merges `main` (no conflicts) and `dc66cba` does two things: - The one-way, loop and threaded tests compare what the lowering makes, with the passes off. A `#[cfg(test)]` per-thread override of `shader_opt()` does this; production is unchanged. - A new test checks with the passes on that pruning still changes the module. It uses a scalar taken from the group id, so a later fold of wave operations on constants would not remove the difference. `cargo test -p victory-shader --lib -- --test-threads=1`: 284 passed, 0 failed, 6 ignored. Two mutations were checked: - turning pruning off fails the one-way, loop, threaded and new tests; - replacing the `original.is_ok()` check with `true` fails 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.
Author
Owner

@⁠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 on main 4f62c2a. Command: cargo test --release -p victory-shader --lib, two runs.

  • main: 275 passed, 2 failed.
  • This PR: 282 passed, 2 failed. The PR's 7 new tests pass on this hardware. (One of the two runs ended without a result line. This test binary does that now and then on this machine, on main too.)

The 2 failures are the same on main: scalar_counted_in_a_divergent_loop_read_after_it and scalar_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.

**@⁠MamaLeVrai** on GitHub · 2026-11-26 00:18 UTC · [original](https://github.com/Belobug/VIctory-Belobug-AMD/pull/172#issuecomment-7178615072) RDNA2 result. Hardware: RX 6950 XT, Windows 11 build 26300, Rust 1.98.1. This PR (`dc66cba`) merged on `main` `4f62c2a`. Command: `cargo test --release -p victory-shader --lib`, two runs. - `main`: 275 passed, 2 failed. - This PR: **282 passed, 2 failed**. The PR's 7 new tests pass on this hardware. (One of the two runs ended without a result line. This test binary does that now and then on this machine, on `main` too.) The 2 failures are the same on `main`: `scalar_counted_in_a_divergent_loop_read_after_it` and `scalar_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)
Author
Owner

@⁠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.rs tests only the IR transformation. Please add a synthetic program with a prunable branch that fails unthreaded emission but succeeds after threading, and assert that emit() stays byte-identical to emit_threaded(..., true). This would protect the compatibility rule currently checked only by the external shader corpus.

**@⁠Copilot** on GitHub · 2026-11-25 18:51 UTC · [original](https://github.com/Belobug/VIctory-Belobug-AMD/pull/172#discussion_r4699433965) The unthreaded-success gate has no regression test: the new tests cover pruning and both console-flow exclusions, while `threading.rs` tests only the IR transformation. Please add a synthetic program with a prunable branch that fails unthreaded emission but succeeds after threading, and assert that `emit()` stays byte-identical to `emit_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)
Author
Owner

@⁠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, in emit/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, so emit_program(.., true) fails with Unsupported { 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, and emit_threaded(.., true) lowers it.

The test asserts that emit() returns the same HostShader as emit_threaded(.., true), and that emit_without_dead_branches would have given a different program (it prunes, then threads the pruned form). With the check replaced by true the test fails. cargo test -p victory-shader --lib -- --test-threads=1: 272 passed, 0 failed, 6 ignored (RX 7900 XT, Windows 11).

**@⁠adrianmjim** on GitHub · 2026-11-25 19:06 UTC · [original](https://github.com/Belobug/VIctory-Belobug-AMD/pull/172#discussion_r4699591590) Added in `891a7ad`: `a_program_that_lowers_only_threaded_is_emitted_threaded_not_pruned` (GPU-free, in `emit/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, so `emit_program(.., true)` fails with `Unsupported { 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, and `emit_threaded(.., true)` lowers it. The test asserts that `emit()` returns the same `HostShader` as `emit_threaded(.., true)`, and that `emit_without_dead_branches` would have given a different program (it prunes, then threads the pruned form). With the check replaced by `true` the test fails. `cargo test -p victory-shader --lib -- --test-threads=1`: 272 passed, 0 failed, 6 ignored (RX 7900 XT, Windows 11).
Author
Owner

@⁠copilot-pull-request-reviewer[bot] on GitHub · 2026-11-25 18:51 UTC · original · reviewed

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:

  • Detects constant per-lane branches, removes unreachable code, and rebuilds shader analyses.
  • Preserves loop-limit exits, console-flow behavior, and the existing fallback when pruning cannot safely apply.
  • Adds synthetic tests for pruning, equivalent GPU results, and compatibility exclusions.
File Description
crates/​victory-shader/​src/​structure.rs Identifies one-way lane branches and tests branch retargeting.
crates/​victory-shader/​src/​ir.rs Retargets branches, empties unreachable blocks, and rebuilds SSA.
crates/​victory-shader/​src/​emit/​prune_tests.rs Tests pruning results, loop exits, and console-flow exclusions.
crates/​victory-shader/​src/​emit/​mod.rs Integrates guarded pruning while preserving the original emission fallback.
crates/​victory-shader/​src/​emit/​lower.rs Exposes a conservative console-flow eligibility check.

Review effort: Balanced

**@⁠copilot-pull-request-reviewer[bot]** on GitHub · 2026-11-25 18:51 UTC · [original](https://github.com/Belobug/VIctory-Belobug-AMD/pull/172#pullrequestreview-6063211823) · **reviewed** <!-- ccr-overview-v2 --> ### 🟡 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. <details open> <summary><strong>1 open issue</strong></summary> - <picture><source media="(prefers-color-scheme: dark)" srcset="https://github.githubassets.com/static/images/icons/copilot-code-review/low-v2-dark.svg"><source media="(prefers-color-scheme: light)" srcset="https://github.githubassets.com/static/images/icons/copilot-code-review/low-v2-light.svg"><img src="https://github.githubassets.com/static/images/icons/copilot-code-review/low-v2-light.png" alt="Low severity" width="62" height="18" align="texttop"></picture> [Add regression test for unthreaded emission compatibility](https://git.gtahub.st/github/Belobug--VIctory-Belobug-AMD/pulls/172) · New </details> <details> <summary><strong>What changed in this PR</strong></summary> 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:** - Detects constant per-lane branches, removes unreachable code, and rebuilds shader analyses. - Preserves loop-limit exits, console-flow behavior, and the existing fallback when pruning cannot safely apply. - Adds synthetic tests for pruning, equivalent GPU results, and compatibility exclusions. | File | Description | | ---- | ----------- | | `crates/​victory-shader/​src/​structure.rs` | Identifies one-way lane branches and tests branch retargeting. | | `crates/​victory-shader/​src/​ir.rs` | Retargets branches, empties unreachable blocks, and rebuilds SSA. | | `crates/​victory-shader/​src/​emit/​prune_tests.rs` | Tests pruning results, loop exits, and console-flow exclusions. | | `crates/​victory-shader/​src/​emit/​mod.rs` | Integrates guarded pruning while preserving the original emission fallback. | | `crates/​victory-shader/​src/​emit/​lower.rs` | Exposes a conservative console-flow eligibility check. | </details> <sub>Review effort: Balanced</sub>
github-bot closed this pull request 2026-11-27 10:41:36 +00:00
Author
Owner

@⁠adrianmjim on GitHub · 2026-11-27 09:25 UTC · original

Closing, since main is no longer the line being developed. On a-publier the 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_ActivateRays 119,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.

**@⁠adrianmjim** on GitHub · 2026-11-27 09:25 UTC · [original](https://github.com/Belobug/VIctory-Belobug-AMD/pull/172#issuecomment-7214668879) Closing, since `main` is no longer the line being developed. On `a-publier` the 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_ActivateRays` 119,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

This discussion has been locked. Commenting is limited to contributors.
No description provided.