Shaders: guard both back edges when a conditional branch targets two … #1540

Open
github-bot wants to merge 1 commit from github-bot/gh-1540 into main AGit
Owner

@⁠Texbirds on GitHub · 2026-11-24 18:58 UTC · original
Texbirds:fix/shader-dual-backedge-loop-counter → main · +97 −24 · 1 files · 1 commits · open

Problem

In crates/victory-shader/src/emit/lower.rs, LaneExit::Branch handles loop-counter increments with if back(self, taken) { ... } else if back(self, not_taken) { ... }, and the subsequent enter_loop pass skips any target where back(self, to) is true.

When both arms of a conditional branch are back edges (for example, an inner loop latch placed before an outer loop header where taken continues the inner loop and not_taken falls straight back into the outer loop header), not_taken never calls count_iteration: its LOOP_LIMIT counter phi is missing its incoming edge from that block (producing invalid DXIL) and that back edge has no iteration cap.

Fix

  • When both taken and not_taken are back edges (and distinct), emit dedicated per-edge guard blocks recorded in self.edges[(b, to)] that each increment and bound their target header's counter before branching to the header or abort_block().
  • When taken == not_taken is a back edge, emit a single bounded back-edge branch directly to the header or abort_block().
  • Add regression test dual_back_edge_branch_counts_both_loop_headers verifying both loop headers receive all counter-phi incoming edges and pass DXIL validation when dxil.dll is present.

Verification

  • Pending windows-latest CI (cargo clippy default + --all-features and cargo test --workspace --locked).
**@⁠Texbirds** on GitHub · 2026-11-24 18:58 UTC · [original](https://github.com/Marvisak/VIctory/pull/1540) `Texbirds:fix/shader-dual-backedge-loop-counter` → `main` · +97 −24 · 1 files · 1 commits · open ### Problem In `crates/victory-shader/src/emit/lower.rs`, `LaneExit::Branch` handles loop-counter increments with `if back(self, taken) { ... } else if back(self, not_taken) { ... }`, and the subsequent `enter_loop` pass skips any target where `back(self, to)` is true. When both arms of a conditional branch are back edges (for example, an inner loop latch placed before an outer loop header where `taken` continues the inner loop and `not_taken` falls straight back into the outer loop header), `not_taken` never calls `count_iteration`: its `LOOP_LIMIT` counter phi is missing its incoming edge from that block (producing invalid DXIL) and that back edge has no iteration cap. ### Fix - When both `taken` and `not_taken` are back edges (and distinct), emit dedicated per-edge guard blocks recorded in `self.edges[(b, to)]` that each increment and bound their target header's counter before branching to the header or `abort_block()`. - When `taken == not_taken` is a back edge, emit a single bounded back-edge branch directly to the header or `abort_block()`. - Add regression test `dual_back_edge_branch_counts_both_loop_headers` verifying both loop headers receive all counter-phi incoming edges and pass DXIL validation when `dxil.dll` is present. ### Verification - Pending `windows-latest` CI (`cargo clippy` default + `--all-features` and `cargo test --workspace --locked`).
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 04:35:39 +00:00
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 1ccae2d 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 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).

**@⁠sinan-cmyk** on GitHub · 2026-11-24 19:33 UTC · [original](https://github.com/Marvisak/VIctory/pull/1540#issuecomment-7142274789) ### 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 1ccae2d 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 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).
Author
Owner

@⁠Marvisak on GitHub · 2026-11-25 01:30 UTC · original · edited 2026-11-26 00:25 UTC

Automated triage for the maintainers.

Merge candidate. The rebase onto main is clean, and the logic is as reviewed before. In LaneExit::Branch (crates/victory-shader/src/emit/lower.rs), when both arms of a conditional branch are back edges, each arm now gets its own guard block. That block counts the iteration for its own header through count_iteration_from and branches to the header or to abort_block(), and it is recorded in self.edges so the header's other phis see it as the predecessor. When both arms go to the same header, there is a single guarded branch. Previously the not_taken header's counter phi was missing an incoming edge, and that edge had no iteration cap. dual_back_edge_branch_counts_both_loop_headers checks that all four counter-phi edges exist and that the DXIL validates.

On your reply: confirmed. It merges cleanly with current main. One thing for the maintainers: with this PR merged, emit/lower.rs is 524,196 bytes, only 92 bytes under the 524,288-byte limit in xtask/src/policy.rs. #1271 and #1315 also grow that file, so only one of them can land before lower.rs is split.

CI: passing (check).


Generated by Claude Code

**@⁠Marvisak** on GitHub · 2026-11-25 01:30 UTC · [original](https://github.com/Marvisak/VIctory/pull/1540#issuecomment-7149620853) · edited 2026-11-26 00:25 UTC <!-- victory-triage v1 head=f207efe21429387fa0d5b1b9e115ac3212338c6e verdict=merge-candidate at=2026-11-26T00:25:38Z --> Automated triage for the maintainers. **Merge candidate.** The rebase onto `main` is clean, and the logic is as reviewed before. In `LaneExit::Branch` (`crates/victory-shader/src/emit/lower.rs`), when both arms of a conditional branch are back edges, each arm now gets its own guard block. That block counts the iteration for its own header through `count_iteration_from` and branches to the header or to `abort_block()`, and it is recorded in `self.edges` so the header's other phis see it as the predecessor. When both arms go to the same header, there is a single guarded branch. Previously the `not_taken` header's counter phi was missing an incoming edge, and that edge had no iteration cap. `dual_back_edge_branch_counts_both_loop_headers` checks that all four counter-phi edges exist and that the DXIL validates. On your reply: confirmed. It merges cleanly with current `main`. One thing for the maintainers: with this PR merged, `emit/lower.rs` is 524,196 bytes, only 92 bytes under the 524,288-byte limit in `xtask/src/policy.rs`. #1271 and #1315 also grow that file, so only one of them can land before `lower.rs` is split. CI: passing (`check`). --- _Generated by [Claude Code](https://claude.ai/code)_
Author
Owner

@⁠Texbirds on GitHub · 2026-11-25 22:46 UTC · original

Rebased cleanly onto main (f3578e3) in f207efe. The conflicts were only adjacent #[cfg(test)] fields on Lowered (uses_lane_index / has_half_wave_mask) and the new ds_bpermute_* tests in crates/victory-shader/src/emit/lower.rs; the dual back-edge guard logic and dual_back_edge_branch_counts_both_loop_headers test are unchanged.

**@⁠Texbirds** on GitHub · 2026-11-25 22:46 UTC · [original](https://github.com/Marvisak/VIctory/pull/1540#issuecomment-7176772889) Rebased cleanly onto `main` (`f3578e3`) in `f207efe`. The conflicts were only adjacent `#[cfg(test)]` fields on `Lowered` (`uses_lane_index` / `has_half_wave_mask`) and the new `ds_bpermute_*` tests in `crates/victory-shader/src/emit/lower.rs`; the dual back-edge guard logic and `dual_back_edge_branch_counts_both_loop_headers` test are unchanged.
Author
Owner

@⁠fuzeofficlal on GitHub · 2026-11-26 00:44 UTC · original

Code review and RTX 5090 + WARP + DXIL verification at head f207efe21429 merged cleanly onto main (63bcca43c6a0):

  • Structured CFG & phi predecessor audit (crates/victory-shader/src/emit/lower.rs:10195-10220):
    • When both arms of LaneExit::Branch are back edges (self.counters.contains_key(&to) && self.rpo[to] <= self.rpo[b]): the taken == not_taken case guards once directly from self.cur (avoiding double-increment), while taken != not_taken allocates dedicated edge blocks checking each target's loop counter against LOOP_LIMIT and registers self.edges.insert((b, to), edge).
    • Audited all self.edges consumers in lower.rs (lines 5490, 5631, 5695, 10877, 10940): every one resolves predecessors via self.edges.get(&(q, b)).copied().unwrap_or(qb), keeping data/mask/descriptor/counter phis at both loop headers consistent without resetting back-edge counters via enter_loop.
  • 512 KiB lower.rs budget warning:
    • Merged onto main (63bcca43), crates/victory-shader/src/emit/lower.rs reaches 524,196 bytes — only 92 bytes below the 524,288-byte xtask policy limit. Worth noting for merge ordering alongside other lower.rs PRs (or trimming a few lines of comments / extracting a helper if another lower.rs PR lands first).
  • Verified (Windows 11, NVIDIA RTX 5090 + WARP, dxil.dll 10.0.26100.0, VICTORY_D3D12_DEBUG=1, Rust 1.98.1):
    • cargo xtask policy: 191 files ok; cargo clippy -p victory-shader --all-targets -- -D warnings (default + --all-features): 0 warnings
    • emit::lower::tests emit::tests structure::tests: 64/64 passed (1 ignored: bench_bools) on both RTX 5090 and WARP.

Logic and DXIL validation look solid (just watch the 92 B headroom in lower.rs); LGTM.

**@⁠fuzeofficlal** on GitHub · 2026-11-26 00:44 UTC · [original](https://github.com/Marvisak/VIctory/pull/1540#issuecomment-7179074270) Code review and RTX 5090 + WARP + DXIL verification at head `f207efe21429` merged cleanly onto `main` (`63bcca43c6a0`): - **Structured CFG & phi predecessor audit (`crates/victory-shader/src/emit/lower.rs:10195-10220`)**: - When both arms of `LaneExit::Branch` are back edges (`self.counters.contains_key(&to) && self.rpo[to] <= self.rpo[b]`): the `taken == not_taken` case guards once directly from `self.cur` (avoiding double-increment), while `taken != not_taken` allocates dedicated edge blocks checking each target's loop counter against `LOOP_LIMIT` and registers `self.edges.insert((b, to), edge)`. - Audited all `self.edges` consumers in `lower.rs` (lines 5490, 5631, 5695, 10877, 10940): every one resolves predecessors via `self.edges.get(&(q, b)).copied().unwrap_or(qb)`, keeping data/mask/descriptor/counter phis at both loop headers consistent without resetting back-edge counters via `enter_loop`. - **512 KiB `lower.rs` budget warning**: - Merged onto `main` (`63bcca43`), `crates/victory-shader/src/emit/lower.rs` reaches **`524,196` bytes** — only **`92` bytes** below the `524,288`-byte `xtask policy` limit. Worth noting for merge ordering alongside other `lower.rs` PRs (or trimming a few lines of comments / extracting a helper if another `lower.rs` PR lands first). - **Verified (Windows 11, NVIDIA RTX 5090 + WARP, `dxil.dll` 10.0.26100.0, `VICTORY_D3D12_DEBUG=1`, Rust 1.98.1)**: - `cargo xtask policy`: `191 files ok`; `cargo clippy -p victory-shader --all-targets -- -D warnings` (default + `--all-features`): 0 warnings - `emit::lower::tests emit::tests structure::tests`: **64/64 passed** (1 ignored: `bench_bools`) on both RTX 5090 and WARP. Logic and DXIL validation look solid (just watch the 92 B headroom in `lower.rs`); LGTM.
This pull request can be merged automatically.
This branch is out-of-date with the base branch
You are not authorized to merge this pull request.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin '+refs/pull/1540/head:github-bot/gh-1540'
git switch 'github-bot/gh-1540'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-1540'
git switch 'github-bot/gh-1540'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-1540'
git switch 'github-bot/gh-1540'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-1540'
git switch 'main'
git merge --squash 'github-bot/gh-1540'
git switch 'main'
git merge --ff-only 'github-bot/gh-1540'
git switch 'main'
git merge 'github-bot/gh-1540'
git push origin 'main'
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!1540
No description provided.