Shaders: every DXIL loop gets one latch, and every block one back edge #1524

Closed
github-bot wants to merge 1 commit from gh-pr/1524/head into gh-pr/1524/base
Owner

@⁠brunofgmag on GitHub · 2026-11-24 18:45 UTC · original
brunofgmag:shader/single-latch-loops → main · +473 −50 · 1 files · 1 commits · closed without merging

vkd3d-proton's dxil-spirv expects loops in the shape DXC emits: each loop header has one back edge, and each block branches back to at most one header. The DXIL writer did not guarantee either one, and a few console shaders produce both shapes.

What breaks. On Linux under Proton, creating the pipeline for such a shader never returns:

  • A header with several back edges makes CFGStructurizer::build_immediate_dominators recurse forever between CFGNode::recompute_immediate_dominator and find_common_dominator.
  • A block that branches back to two headers (a "continue" of the outer loop from inside the inner one) trips the one-back-edge-per-block assert in CFGStructurizer::visit (cfg_structurizer.cpp:3486). Release builds skip the assert and then recurse forever in LoopMergeTracer::trace_from_parent.

Either way, the thread that creates the pipeline overflows its stack. Wine ends that thread without a message, and anything waiting for the pipeline waits forever. A bigger stack does not help: 2, 16 and 256 MiB all overflow at the same address.

Where it comes from. lower.rs keeps each console back edge as its own latch ("Back edges stay as they are: the exiting block is the latch"). Module::write folds an empty latch into its predecessor. A console loop with two continue paths therefore ends up with two latches.

The change. Module::write now normalizes the module before it writes anything:

  1. A block that branches back to several headers keeps its edge to the innermost one, so it stays the exiting latch of the inner loop. Each other back edge goes through a new block that ends in a branch to its header, and that header's phis read the old values from the new block. The empty-latch fold leaves these blocks alone (Block::keeps_own_block), so it cannot bring the second back edge back.
  2. A header with two or more back edges gets one new latch block. Each of its phis gets a partner phi there that holds the per-latch values.

Headers and blocks with zero or one back edge are not touched. The pinned written_bytes_unchanged test still passes, and a new test checks that a single-latch loop writes the same bytes as before. write now takes &mut self.

How it was checked.

  • New tests in bitcode.rs cover three latches (one of them a conditional branch with both arms to the header), a self-loop with a second latch, a block with back edges to an inner and an outer header, and that block's new latch joining the other latches of the outer header.
  • Each test goes red when its part of the change is undone. That covers the normalization call, retargeting only one arm, keeping the outer back edge instead of the inner one, and folding the new block.
  • I built dxil-spirv from the vkd3d-proton commit that Proton Experimental ships (2576c0b4a, dxil-spirv 114e664e), in a debug build so its asserts are on. Then I ran victory translate from this branch's base (main at ffe310e) over a GTA VI dump and fed every emitted shader to it:
    • Before: 268 of 21,882 shaders fail the assert at cfg_structurizer.cpp:3492 (several back edges into one header). That is 229 PS_MirrorReflectionDeferredDecalTextured, 38 other ped pixel shaders and the compute vfx_proj_decal_vbuffer:CS_ProjDecalResolve_Scalarized.
    • After: all 21,882 convert with exit 0. translate emits the same number of programs per stage either way.
  • The hang itself, and the second shape (a block with back edges to two headers), showed up on a local tree that also carries the open Wave64-on-Wave32 lowering (#189), which lets 13 more compute shaders through. With this change those 13 convert, pass spirv-val --target-env vulkan1.3, and victory translate --pipelines creates all 13 pipelines on an RTX 4080 under Proton, where before 7 of them never returned.
  • Clippy with -D warnings (with and without --all-features) is clean for the Windows target. The workspace tests ran under Proton: the bitcode tests pass, and the 6 tests that fail there also fail on plain main in the same setup.
**@⁠brunofgmag** on GitHub · 2026-11-24 18:45 UTC · [original](https://github.com/Marvisak/VIctory/pull/1524) `brunofgmag:shader/single-latch-loops` → `main` · +473 −50 · 1 files · 1 commits · closed without merging vkd3d-proton's dxil-spirv expects loops in the shape DXC emits: each loop header has one back edge, and each block branches back to at most one header. The DXIL writer did not guarantee either one, and a few console shaders produce both shapes. **What breaks.** On Linux under Proton, creating the pipeline for such a shader never returns: - A header with several back edges makes `CFGStructurizer::build_immediate_dominators` recurse forever between `CFGNode::recompute_immediate_dominator` and `find_common_dominator`. - A block that branches back to two headers (a "continue" of the outer loop from inside the inner one) trips the one-back-edge-per-block assert in `CFGStructurizer::visit` (cfg_structurizer.cpp:3486). Release builds skip the assert and then recurse forever in `LoopMergeTracer::trace_from_parent`. Either way, the thread that creates the pipeline overflows its stack. Wine ends that thread without a message, and anything waiting for the pipeline waits forever. A bigger stack does not help: 2, 16 and 256 MiB all overflow at the same address. **Where it comes from.** `lower.rs` keeps each console back edge as its own latch ("Back edges stay as they are: the exiting block is the latch"). `Module::write` folds an empty latch into its predecessor. A console loop with two `continue` paths therefore ends up with two latches. **The change.** `Module::write` now normalizes the module before it writes anything: 1. A block that branches back to several headers keeps its edge to the innermost one, so it stays the exiting latch of the inner loop. Each other back edge goes through a new block that ends in a branch to its header, and that header's phis read the old values from the new block. The empty-latch fold leaves these blocks alone (`Block::keeps_own_block`), so it cannot bring the second back edge back. 2. A header with two or more back edges gets one new latch block. Each of its phis gets a partner phi there that holds the per-latch values. Headers and blocks with zero or one back edge are not touched. The pinned `written_bytes_unchanged` test still passes, and a new test checks that a single-latch loop writes the same bytes as before. `write` now takes `&mut self`. **How it was checked.** - New tests in `bitcode.rs` cover three latches (one of them a conditional branch with both arms to the header), a self-loop with a second latch, a block with back edges to an inner and an outer header, and that block's new latch joining the other latches of the outer header. - Each test goes red when its part of the change is undone. That covers the normalization call, retargeting only one arm, keeping the outer back edge instead of the inner one, and folding the new block. - I built dxil-spirv from the vkd3d-proton commit that Proton Experimental ships (2576c0b4a, dxil-spirv 114e664e), in a debug build so its asserts are on. Then I ran `victory translate` from this branch's base (`main` at ffe310e) over a GTA VI dump and fed every emitted shader to it: - Before: 268 of 21,882 shaders fail the assert at cfg_structurizer.cpp:3492 (several back edges into one header). That is 229 `PS_MirrorReflectionDeferredDecalTextured`, 38 other ped pixel shaders and the compute `vfx_proj_decal_vbuffer:CS_ProjDecalResolve_Scalarized`. - After: all 21,882 convert with exit 0. `translate` emits the same number of programs per stage either way. - The hang itself, and the second shape (a block with back edges to two headers), showed up on a local tree that also carries the open Wave64-on-Wave32 lowering (#189), which lets 13 more compute shaders through. With this change those 13 convert, pass `spirv-val --target-env vulkan1.3`, and `victory translate --pipelines` creates all 13 pipelines on an RTX 4080 under Proton, where before 7 of them never returned. - Clippy with `-D warnings` (with and without `--all-features`) is clean for the Windows target. The workspace tests ran under Proton: the bitcode tests pass, and the 6 tests that fail there also fail on plain `main` in the same setup.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 04:35:06 +00:00
github-bot closed this pull request 2026-11-26 04:35:06 +00:00
Author
Owner

@⁠brunofgmag on GitHub · 2026-11-24 18:47 UTC · original

This touches the same code as #1145 and #1471: both edit the empty-latch fold in Module::write, which this PR moves into Module::folded_latches. Neither one covers a header with several back edges or a block with two back edges. I'll rebase onto whichever of them lands first.

**@⁠brunofgmag** on GitHub · 2026-11-24 18:47 UTC · [original](https://github.com/Marvisak/VIctory/pull/1524#issuecomment-7141153343) This touches the same code as #1145 and #1471: both edit the empty-latch fold in `Module::write`, which this PR moves into `Module::folded_latches`. Neither one covers a header with several back edges or a block with two back edges. I'll rebase onto whichever of them lands first.
Author
Owner

@⁠sinan-cmyk on GitHub · 2026-11-24 19:33 UTC · original

Offline measurement on a retail dump, and the shader tests on Ada

Result: no count changes against main, and the tests pass on Ada.

Offline. I merged head 8e14d23 onto main ffe310e (clean merge) and ran victory translate --validate --list on shaders.rpf (1.0.2.1), with dxil.dll 1.8.2502.11. I compared the result with main program by program (25,889 programs). Validated: 21,724 on both, 0 gained, 0 lost. No program changes its failure reason either.

This is the expected result for a correctness fix whose instructions sit behind the earlier failures on main. The count shows that the change rejects nothing. It does not show whether the output is right.

Tests. With the same merge, cargo test --locked -p victory-shader --lib passed 125, failed 0. The bitcode benchmark is ignored, as on main, where 120 pass. The run was on an RTX 4070 SUPER (Ada, 32-lane waves, driver 32.0.16.1088), the default adapter, with VICTORY_WARP unset.

No shader names or data are included, per the asset policy. AI-assisted (Claude Code).

**@⁠sinan-cmyk** on GitHub · 2026-11-24 19:33 UTC · [original](https://github.com/Marvisak/VIctory/pull/1524#issuecomment-7142276489) ### Offline measurement on a retail dump, and the shader tests on Ada **Result: no count changes against `main`, and the tests pass on Ada.** **Offline.** I merged head 8e14d23 onto `main` ffe310e (clean merge) and ran `victory translate --validate --list` on `shaders.rpf` (1.0.2.1), with dxil.dll 1.8.2502.11. I compared the result with `main` program by program (25,889 programs). Validated: 21,724 on both, 0 gained, 0 lost. No program changes its failure reason either. This is the expected result for a correctness fix whose instructions sit behind the earlier failures on `main`. The count shows that the change rejects nothing. It does not show whether the output is right. **Tests.** With the same merge, `cargo test --locked -p victory-shader --lib` passed 125, failed 0. The bitcode benchmark is ignored, as on `main`, where 120 pass. The run was on an RTX 4070 SUPER (Ada, 32-lane waves, driver 32.0.16.1088), the default adapter, with `VICTORY_WARP` unset. No shader names or data are included, per the asset policy. AI-assisted (Claude Code).
Author
Owner

@⁠geiyiliyabumo on GitHub · 2026-11-24 19:34 UTC · original

Verified commit 8e14d23 on Windows 11 / AMD Radeon RX 9060 XT (RDNA4, gfx1200, SM 6.6) + WARP:

  • cargo test -p victory-shader --lib: 125 passed, 0 failed, 1 ignored (all new single-latch / single-back-edge DXIL bitcode tests pass alongside the full GPU VALU & waterfall suites).

AI-assisted; synthetic tests only, no game files.

**@⁠geiyiliyabumo** on GitHub · 2026-11-24 19:34 UTC · [original](https://github.com/Marvisak/VIctory/pull/1524#issuecomment-7142290794) Verified commit `8e14d23` on **Windows 11 / AMD Radeon RX 9060 XT (RDNA4, `gfx1200`, SM 6.6)** + **WARP**: - `cargo test -p victory-shader --lib`: **125 passed, 0 failed, 1 ignored** (all new single-latch / single-back-edge DXIL bitcode tests pass alongside the full GPU VALU & waterfall suites). *AI-assisted; synthetic tests only, no game files.*
Author
Owner

@⁠Marvisak on GitHub · 2026-11-25 00:42 UTC · original

Automated triage for the maintainers. Closing because it is outside the project's scope (VIctory targets Windows hosts; ports, launchers and tooling for other platforms are not being taken).

Rewrites every multi-latch loop so vkd3d-proton's dxil-spirv does not hang: Linux/Proton support, outside the project's scope. It funnels back edges through a new non-exiting latch block, the shape main folds away because the AMD driver hangs on it, and changes Module::write for all hosts (470 lines).

If this is mistaken, reply here with what was missed (or push a change and say so) and it will be looked at again. Thank you for the contribution.

**@⁠Marvisak** on GitHub · 2026-11-25 00:42 UTC · [original](https://github.com/Marvisak/VIctory/pull/1524#issuecomment-7148786100) Automated triage for the maintainers. Closing because it is outside the project's scope (VIctory targets Windows hosts; ports, launchers and tooling for other platforms are not being taken). Rewrites every multi-latch loop so vkd3d-proton's dxil-spirv does not hang: Linux/Proton support, outside the project's scope. It funnels back edges through a new non-exiting latch block, the shape main folds away because the AMD driver hangs on it, and changes Module::write for all hosts (470 lines). If this is mistaken, reply here with what was missed (or push a change and say so) and it will be looked at again. Thank you for the contribution.

Pull request closed

This discussion has been locked. Commenting is limited to contributors.
No reviewers
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.

Dependencies

No dependencies set

Reference
github/Marvisak--VIctory!1524
No description provided.