Shaders: fold empty loop latches containing only dead phis #1470

Merged
Texbirds merged 1 commit from upstream-pr/1471 into main 2026-11-25 00:22:57 +00:00
Texbirds commented 2026-11-24 18:17:32 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Texbirds on 2026-11-24T18:17:32Z.


Problem

In crates/victory-shader/src/emit/bitcode.rs, Module::write folds empty loop latches into their single predecessor to prevent AMD driver hangs on divergent loops. However, the emptiness check tests blk.phis.is_empty() instead of checking whether any phi in the latch is actually live (live[i]).

Because unread phis are omitted when writing bitcode, a latch whose only instructions are dead phis is emitted as an empty block—defeating latch folding and leaving an empty back-edge latch for the driver.

Fix

  • Change the latch emptiness condition in Module::write to blk.phis.iter().all(|&i| !live[i as usize]) && blk.body.is_empty().
  • Add unit test empty_latch_with_dead_phis_is_folded verifying identical emitted bitcode with and without dead phis in an empty loop latch.
<!-- greyfinch-sync:Marvisak/VIctory:issue:6588478104 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1471). Originally posted by [Texbirds](https://github.com/Texbirds) on 2026-11-24T18:17:32Z. --- ### Problem In `crates/victory-shader/src/emit/bitcode.rs`, `Module::write` folds empty loop latches into their single predecessor to prevent AMD driver hangs on divergent loops. However, the emptiness check tests `blk.phis.is_empty()` instead of checking whether any phi in the latch is actually live (`live[i]`). Because unread phis are omitted when writing bitcode, a latch whose only instructions are dead phis is emitted as an empty block—defeating latch folding and leaving an empty back-edge latch for the driver. ### Fix - Change the latch emptiness condition in `Module::write` to `blk.phis.iter().all(|&i| !live[i as usize]) && blk.body.is_empty()`. - Add unit test `empty_latch_with_dead_phis_is_folded` verifying identical emitted bitcode with and without dead phis in an empty loop latch.
catminou4 commented 2026-11-24 19:03:30 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by catminou4 on 2026-11-24T18:42:17Z.

Review: APPROVED


Tested locally on macOS arm64 (rustc 1.98.1): PR head merged cleanly into current main (ffe310e), cargo test -p victory-shader --locked → 97 passed, 0 failed. Execution/valu tests are #[cfg(all(test, windows))] and were not run here; lowering/structure coverage only.

Empty latch blocks with only dead phis now fold: the condition changes from phis.is_empty() to "no LIVE phi", so two loops differing only by an unread latch phi emit identical bitcode. The new bitcode test asserts byte-identical output with/without the dead phi. Conservative direction (only live phis block folding). 97 passed on macOS (+1).

<!-- greyfinch-sync:Marvisak/VIctory:review:6045234893 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1471#pullrequestreview-6045234893). Originally posted by [catminou4](https://github.com/catminou4) on 2026-11-24T18:42:17Z. **Review:** APPROVED --- Tested locally on macOS arm64 (rustc 1.98.1): PR head merged cleanly into current main (ffe310e), `cargo test -p victory-shader --locked` → 97 passed, 0 failed. Execution/valu tests are `#[cfg(all(test, windows))]` and were not run here; lowering/structure coverage only. Empty latch blocks with only dead phis now fold: the condition changes from `phis.is_empty()` to "no LIVE phi", so two loops differing only by an unread latch phi emit identical bitcode. The new bitcode test asserts byte-identical output with/without the dead phi. Conservative direction (only live phis block folding). 97 passed on macOS (+1).
sinan-cmyk commented 2026-11-24 21:03:25 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by sinan-cmyk on 2026-11-24T20:14:26Z.


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

Result: no count change against main.

Offline. I merged head be3202d 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 main, 21,724 with this PR.

No program changes its failure reason either. That is the expected result for a 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 121, 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).

<!-- greyfinch-sync:Marvisak/VIctory:comment:7143237122 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1471#issuecomment-7143237122). Originally posted by [sinan-cmyk](https://github.com/sinan-cmyk) on 2026-11-24T20:14:26Z. --- ### Offline measurement on a retail dump, and the shader tests on Ada **Result: no count change against `main`.** **Offline.** I merged head be3202d 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 `main`, 21,724 with this PR. No program changes its failure reason either. That is the expected result for a 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 121, 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).
upstream-sync closed this pull request 2026-11-25 01:06:37 +00:00
Sign in to join this conversation.
No reviewers
No labels
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.

Reference
Marvisak/VIctory!1470
No description provided.