Shaders: a constant branch through a folded block drops its untaken phi entry #2622

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

@⁠BastienRodz on GitHub · 2026-11-25 17:00 UTC · original
BastienRodz:shader/folded-phi-direct-edge-pr → main · +39 −1 · 1 files · 1 commits · open

What's wrong

Plan::edge (286a00e) keeps a phi entry from b when one of b's successors, followed through folded blocks, is the phi's block. Take a constant branch CondBr(true, empty, join) whose taken side empty is folded into entry → join. The entry carried through empty is kept and written from entry, but the entry for the untaken direct edge entry → join passes as well (to(empty) == join). The phi then has two entries for its one predecessor, which is invalid IR.

What the fix does

Plan::edge checks b's own terminator, with constant branches taken as branches: successors(m.term(b)).contains(&x). A folded empty block still has x as its successor, so its entry stays and is written from the block it was folded into (from/src, unchanged). The untaken direct edge no longer matches and is dropped. The existing Plan::edge tests (constant branch, never-taken back edge, empty-block folding) pass unchanged.

Tests

New device-free emit::bitcode::tests::a_constant_branch_through_a_folded_block_drops_the_direct_phi_entry: CondBr(true, empty, join) with an empty empty and a phi in join. It checks that empty folds, that its entry stays, and that the untaken direct entry goes.

Not tested

No dxil.dll here, so not run through the validator, and no title shaders. How often lowering produces this shape (a guarded arm with nothing live behind a condition known to be constant) is not measured.

Fixes #2621

AI-assisted (Gemini 4 Neon found and wrote it, Claude Code reviewed and verified it). No game data used.

🤖 Generated with Claude Code

**@⁠BastienRodz** on GitHub · 2026-11-25 17:00 UTC · [original](https://github.com/Marvisak/VIctory/pull/2622) `BastienRodz:shader/folded-phi-direct-edge-pr` → `main` · +39 −1 · 1 files · 1 commits · open ### What's wrong `Plan::edge` (286a00e) keeps a phi entry from `b` when one of `b`'s successors, followed through folded blocks, is the phi's block. Take a constant branch `CondBr(true, empty, join)` whose taken side `empty` is folded into `entry → join`. The entry carried through `empty` is kept and written from `entry`, but the entry for the untaken direct edge `entry → join` passes as well (`to(empty) == join`). The phi then has two entries for its one predecessor, which is invalid IR. ### What the fix does `Plan::edge` checks `b`'s own terminator, with constant branches taken as branches: `successors(m.term(b)).contains(&x)`. A folded empty block still has `x` as its successor, so its entry stays and is written from the block it was folded into (`from`/`src`, unchanged). The untaken direct edge no longer matches and is dropped. The existing `Plan::edge` tests (constant branch, never-taken back edge, empty-block folding) pass unchanged. ### Tests New device-free `emit::bitcode::tests::a_constant_branch_through_a_folded_block_drops_the_direct_phi_entry`: `CondBr(true, empty, join)` with an empty `empty` and a phi in `join`. It checks that `empty` folds, that its entry stays, and that the untaken direct entry goes. - On `main` (13f91ca) it fails: `the untaken direct edge goes`. - With the fix, `emit::bitcode::tests` pass (13, 1 benchmark ignored). Run under Wine 10, x86_64-pc-windows-msvc. - windows-latest run on the fork: https://github.com/BastienRodz/VIctory/actions/runs/45659933597 ### Not tested No `dxil.dll` here, so not run through the validator, and no title shaders. How often lowering produces this shape (a guarded arm with nothing live behind a condition known to be constant) is not measured. Fixes #2621 AI-assisted (Gemini 4 Neon found and wrote it, Claude Code reviewed and verified it). No game data used. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
Since 286a00e the DXIL writer takes a branch on a constant as a branch to
its one target, drops phi entries of edges that are gone, and folds an
empty block with one predecessor into it. Plan::edge decided whether a
phi of block x keeps its entry from b by following b's successors
through folded blocks (`to`). For `CondBr(true, empty, join)` with
`empty` folded (`empty` branches to `join`) and a phi in `join` with
entries from `empty` and from the untaken direct edge, the direct entry
was kept as well: the phi got two entries for its one predecessor,
which is invalid IR.

An entry stays when b's own terminator, constant branches taken as
branches, still has x among its successors; a folded block's entry
still stays and is written from the block it was folded into.

Co-Authored-By: Gemini 4 Neon <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 05:10:32 +00:00
Author
Owner

@⁠cloph-dsp on GitHub · 2026-11-25 17:30 UTC · original

Independent Windows verification of PR #2622 (head 1fcd4d4), device-free bitcode regression plus broader CPU/hardware suite.

Toolchain: rustc/cargo 1.100.0, Windows MSVC (repo pins 1.98.1; no clippy or rustup used). Synthetic inputs only, no game data, no source edits, no competing PR.

  • cargo test --locked -p victory-shader --lib emit::bitcode::tests -- --test-threads=1: 13 passed, 1 ignored (bench), 260 filtered. The new a_constant_branch_through_a_folded_block_drops_the_direct_phi_entry passes alongside the existing constant-branch, loop and empty-block folding controls.
  • Negative control on merge base / current origin/main 13f91ca: the new test name reports 0 tests (test absent).
  • Full crate: cargo test --locked -p victory-shader --lib -- --test-threads=1 --skip heap_direct_texture_reads_mark_their_heap_index -> 267 passed, 3 failed, 3 ignored, 1 filtered. The three failures are hardware-specific and unrelated to this change: emit::stride_tests::a_heap_buffer_read_by_element_takes_the_views_stride, emit::stride_tests::a_heap_buffer_read_where_no_buffer_view_is_reads_zeros, emit::synthetic_tests::gathers_at_a_level_match_the_hardware. All three reproduce with identical assertions on unmodified base 13f91ca.
  • The skipped emit::memory_tests::heap_direct_texture_reads_mark_their_heap_index terminates with STATUS_ACCESS_VIOLATION on both the PR head and unmodified base 13f91ca, so it is environmental, not caused by this PR.

No regression attributable to the PR found. AI-assisted (Hy4 flash).

**@⁠cloph-dsp** on GitHub · 2026-11-25 17:30 UTC · [original](https://github.com/Marvisak/VIctory/pull/2622#issuecomment-7169599472) Independent Windows verification of PR #2622 (head `1fcd4d4`), device-free bitcode regression plus broader CPU/hardware suite. Toolchain: rustc/cargo 1.100.0, Windows MSVC (repo pins 1.98.1; no clippy or rustup used). Synthetic inputs only, no game data, no source edits, no competing PR. - `cargo test --locked -p victory-shader --lib emit::bitcode::tests -- --test-threads=1`: **13 passed**, 1 ignored (bench), 260 filtered. The new `a_constant_branch_through_a_folded_block_drops_the_direct_phi_entry` passes alongside the existing constant-branch, loop and empty-block folding controls. - Negative control on merge base / current `origin/main` `13f91ca`: the new test name reports **0 tests** (test absent). - Full crate: `cargo test --locked -p victory-shader --lib -- --test-threads=1 --skip heap_direct_texture_reads_mark_their_heap_index` -> **267 passed**, 3 failed, 3 ignored, 1 filtered. The three failures are hardware-specific and unrelated to this change: `emit::stride_tests::a_heap_buffer_read_by_element_takes_the_views_stride`, `emit::stride_tests::a_heap_buffer_read_where_no_buffer_view_is_reads_zeros`, `emit::synthetic_tests::gathers_at_a_level_match_the_hardware`. All three reproduce with identical assertions on unmodified base `13f91ca`. - The skipped `emit::memory_tests::heap_direct_texture_reads_mark_their_heap_index` terminates with `STATUS_ACCESS_VIOLATION` on both the PR head and unmodified base `13f91ca`, so it is environmental, not caused by this PR. No regression attributable to the PR found. AI-assisted (Hy4 flash).
Author
Owner

@⁠kotekompotek on GitHub · 2026-11-25 17:49 UTC · original

Reviewed: the fix is correct — edge() must test literal successors, not plan-mapped ones. With a constant CondBr(t, empty, join), successors() returns only the taken edge, so contains(&x) is false for the dead direct edge (dropping its phi entry) while the folded-through edge still reports true. The old to(s) == x form counted the folded path as a second edge from the same predecessor — exactly the duplicate-entry bug the test pins. CI green.

**@⁠kotekompotek** on GitHub · 2026-11-25 17:49 UTC · [original](https://github.com/Marvisak/VIctory/pull/2622#issuecomment-7170048305) Reviewed: the fix is correct — `edge()` must test literal successors, not plan-mapped ones. With a constant `CondBr(t, empty, join)`, `successors()` returns only the taken edge, so `contains(&x)` is false for the dead direct edge (dropping its phi entry) while the folded-through edge still reports true. The old `to(s) == x` form counted the folded path as a second edge from the same predecessor — exactly the duplicate-entry bug the test pins. CI green.
Author
Owner

@⁠fuzeofficlal on GitHub · 2026-11-25 18:07 UTC · original

Tested 1fcd4d45 merged cleanly onto main (13f91ca) on Windows 11 with NVIDIA GeForce RTX 5090 (32.0.16.1656) and D3D12 WARP + Windows SDK dxil.dll:

  • cargo test -p victory-shader --lib emit::bitcode::tests: 13 passed, 0 failed, 1 ignored (a_constant_branch_through_a_folded_block_drops_the_direct_phi_entry passes)
  • VICTORY_WARP=1 cargo test -p victory-shader --lib: 268 passed, 3 failed, 3 ignored (all bitcode, flow, memory, valu, semantics, lower, and dxil.dll validation tests pass; the 3 failures are the expected 32-lane/hardware-only synthetic_tests under forced WARP)
  • cargo clippy -p victory-shader --all-targets -- -D warnings: clean (0 warnings)

Checked Plan::edge (self.seen[b] && successors(m.term(b)).contains(&x)): cleanly handles both single and chained collapsed empty blocks while dropping untaken direct edges from constant branches.

**@⁠fuzeofficlal** on GitHub · 2026-11-25 18:07 UTC · [original](https://github.com/Marvisak/VIctory/pull/2622#issuecomment-7170468706) Tested `1fcd4d45` merged cleanly onto `main` (`13f91ca`) on Windows 11 with **NVIDIA GeForce RTX 5090** (`32.0.16.1656`) and **D3D12 WARP** + Windows SDK `dxil.dll`: - `cargo test -p victory-shader --lib emit::bitcode::tests`: **13 passed, 0 failed, 1 ignored** (`a_constant_branch_through_a_folded_block_drops_the_direct_phi_entry` passes) - `VICTORY_WARP=1 cargo test -p victory-shader --lib`: **268 passed, 3 failed, 3 ignored** (all `bitcode`, `flow`, `memory`, `valu`, `semantics`, `lower`, and `dxil.dll` validation tests pass; the 3 failures are the expected 32-lane/hardware-only `synthetic_tests` under forced WARP) - `cargo clippy -p victory-shader --all-targets -- -D warnings`: clean (0 warnings) Checked `Plan::edge` (`self.seen[b] && successors(m.term(b)).contains(&x)`): cleanly handles both single and chained collapsed empty blocks while dropping untaken direct edges from constant branches.
Author
Owner

@⁠andrewpozdnakov7-jpg on GitHub · 2026-11-25 18:16 UTC · original

I tested the emitted pixel shader against DXIL validator 1.9.2609 and drew it on WARP. On main 13f91caee2, validation reports two entries for one phi predecessor, with 0.25 and 0.75 both coming from the same block. On this PR at 1fcd4d450b, the same shader validates and all four pixels read back as [0.25, 0, 0, 1]. I ran six paired checks across both debug settings.

The bitcode tests passed 13 with one benchmark ignored; both draw tests passed. The serial shader suite moved from 267 passed, 4 failed, 3 ignored to 269 passed, 3 failed, 3 ignored. The three remaining failures are the same gather, permute, and edge-flagged mesh tests on both revisions. Strict shader Clippy and asset policy passed.

Test-only commit 0e74a15 is directly on your current head if you want the validator and pixel readback in addition to the existing structural test. Please retain the Git author if you cherry-pick it, or add this trailer if you fold it into another commit:

Co-authored-by: andrewpozdnakov7-jpg [email protected]

**@⁠andrewpozdnakov7-jpg** on GitHub · 2026-11-25 18:16 UTC · [original](https://github.com/Marvisak/VIctory/pull/2622#issuecomment-7170688621) I tested the emitted pixel shader against DXIL validator 1.9.2609 and drew it on WARP. On main 13f91caee25f2fdf2c062be5f7a27f058e51cbca, validation reports two entries for one phi predecessor, with 0.25 and 0.75 both coming from the same block. On this PR at 1fcd4d450b0a5c0ede77beef50e0381b9a969f0a, the same shader validates and all four pixels read back as [0.25, 0, 0, 1]. I ran six paired checks across both debug settings. The bitcode tests passed 13 with one benchmark ignored; both draw tests passed. The serial shader suite moved from 267 passed, 4 failed, 3 ignored to 269 passed, 3 failed, 3 ignored. The three remaining failures are the same gather, permute, and edge-flagged mesh tests on both revisions. Strict shader Clippy and asset policy passed. [Test-only commit 0e74a15](https://github.com/andrewpozdnakov7-jpg/VIctory/commit/0e74a15b1198903a1af8fdd22e969c9711eec137) is directly on your current head if you want the validator and pixel readback in addition to the existing structural test. Please retain the Git author if you cherry-pick it, or add this trailer if you fold it into another commit: Co-authored-by: andrewpozdnakov7-jpg <[email protected]>
Author
Owner

@⁠BastienRodz on GitHub · 2026-11-25 18:17 UTC · original

Thanks for checking it end to end — the validator result on main (two entries for one predecessor, 0.25 and 0.75) and the readback on this head are exactly the before/after the PR claims.

I'll keep the PR to bitcode.rs and its structural test, so it stays a one-file change. Your test lives in emit/rect.rs beside the rectangle draws, and it needs a validator or a host device to say anything. If the maintainer would rather have the end-to-end check in this PR, I'll cherry-pick 0e74a15 with your authorship kept.

**@⁠BastienRodz** on GitHub · 2026-11-25 18:17 UTC · [original](https://github.com/Marvisak/VIctory/pull/2622#issuecomment-7170699292) Thanks for checking it end to end — the validator result on main (two entries for one predecessor, 0.25 and 0.75) and the readback on this head are exactly the before/after the PR claims. I'll keep the PR to `bitcode.rs` and its structural test, so it stays a one-file change. Your test lives in `emit/rect.rs` beside the rectangle draws, and it needs a validator or a host device to say anything. If the maintainer would rather have the end-to-end check in this PR, I'll cherry-pick 0e74a15 with your authorship kept.
Author
Owner

@⁠Marvisak on GitHub · 2026-11-25 22:19 UTC · original

Automated triage for the maintainers.

Recommended for a maintainer's look. Plan::edge in crates/victory-shader/src/emit/bitcode.rs now asks whether x is a successor of b's own terminator instead of following successors through folded blocks. I checked that Module::term already turns a constant CondBr into a Br to the taken side, so for CondBr(true, empty, join) with empty folded, the entry from entry (the untaken direct edge) is dropped while the entry from the folded empty stays and is written from entry; on main both passed, giving the phi two entries for one predecessor. A reviewer in the thread confirmed the DXIL validator rejects the shader on main and accepts it with this change. One-line fix plus a device-free test; merges cleanly, CI check passing. Fixes #2621.


Generated by Claude Code

**@⁠Marvisak** on GitHub · 2026-11-25 22:19 UTC · [original](https://github.com/Marvisak/VIctory/pull/2622#issuecomment-7176214977) <!-- victory-triage v1 head=1fcd4d450b0a5c0ede77beef50e0381b9a969f0a verdict=merge-candidate at=2026-11-25T22:27:30Z --> Automated triage for the maintainers. Recommended for a maintainer's look. `Plan::edge` in `crates/victory-shader/src/emit/bitcode.rs` now asks whether `x` is a successor of `b`'s own terminator instead of following successors through folded blocks. I checked that `Module::term` already turns a constant `CondBr` into a `Br` to the taken side, so for `CondBr(true, empty, join)` with `empty` folded, the entry from `entry` (the untaken direct edge) is dropped while the entry from the folded `empty` stays and is written from `entry`; on `main` both passed, giving the phi two entries for one predecessor. A reviewer in the thread confirmed the DXIL validator rejects the shader on `main` and accepts it with this change. One-line fix plus a device-free test; merges cleanly, CI `check` passing. Fixes #2621. --- _Generated by [Claude Code](https://claude.ai/code)_
Author
Owner

@⁠KainanP on GitHub · 2026-11-25 22:53 UTC · original

Measured against #2657 and #2692 with an end-to-end repro (DXIL validator plus a draw on RTX 4060, Intel UHD and WARP). The repro passes, your test fails on main and passes here, and there are no regressions, also on f3578e38. Recommended in the comparison: #2621 (AI-assisted)

**@⁠KainanP** on GitHub · 2026-11-25 22:53 UTC · [original](https://github.com/Marvisak/VIctory/pull/2622#issuecomment-7176915037) Measured against #2657 and #2692 with an end-to-end repro (DXIL validator plus a draw on RTX 4060, Intel UHD and WARP). The repro passes, your test fails on `main` and passes here, and there are no regressions, also on `f3578e38`. Recommended in the comparison: https://git.gtahub.st/github/Marvisak--VIctory/issues/2621 (AI-assisted)
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/2622/head:github-bot/gh-2622'
git switch 'github-bot/gh-2622'

Merge

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