Skip to content

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

Open
BastienRodz wants to merge 1 commit into
Marvisak:mainfrom
BastienRodz:shader/folded-phi-direct-edge-pr
Open

BastienRodz wants to merge 1 commit into
Marvisak:mainfrom
BastienRodz:shader/folded-phi-direct-edge-pr

Conversation

@BastienRodz

Copy link
Copy Markdown
Contributor

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

…hi entry

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 <noreply@google.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@cloph-dsp

Copy link
Copy Markdown

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).

@kotekompotek

Copy link
Copy Markdown

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.

@fuzeofficlal

Copy link
Copy Markdown
Contributor

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.

@andrewpozdnakov7-jpg

Copy link
Copy Markdown
Contributor

I tested the emitted pixel shader against DXIL validator 1.9.2609 and drew it on WARP. On main 13f91ca, validation reports two entries for one phi predecessor, with 0.25 and 0.75 both coming from the same block. On this PR at 1fcd4d4, 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 andrewpozdnakov7-jpg@users.noreply.github.com

@BastienRodz

Copy link
Copy Markdown
Contributor Author

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.

Airamek added a commit to Airamek/VIctory that referenced this pull request Nov 25, 2026
…t per slot

Applied manually (context drifted upstream): Once version of the mirror() warning, past_heap_step fn + test removed. Marvisak#2622 skipped as not-applicable (no Plan machinery locally).
github-actions Bot added a commit to KevinPratap/VIctory that referenced this pull request Nov 25, 2026

Copy link
Copy Markdown
Owner

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

@KainanP

KainanP commented Nov 25, 2026

Copy link
Copy Markdown

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 (comment) (AI-assisted)

@Dukestar69 Dukestar69 left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tested locally merged onto origin/main (63bcca4) on Windows 11 x64:

  • Diff review: Plan::edge previously checked successors(m.term(b)).iter().any(|&s| self.to(s) == x), which misclassified an untaken direct edge from a constant branch as live whenever its taken branch went through an empty block that folded into x. Switching to successors(m.term(b)).contains(&x) cleanly fixes the duplicate-predecessor phi emission without affecting folded-block edges.
  • cargo test -p victory-shader emit::bitcode::tests: 13 passed, 0 failed (including a_constant_branch_through_a_folded_block_drops_the_direct_phi_entry).
  • cargo clippy -p victory-shader --all-targets -- -D warnings: clean.
  • cargo xtask policy: 191 files ok.

Synthetic tests only (no game files used). AI-assisted (Gemini 4 Neon).

github-actions Bot added a commit to KevinPratap/VIctory that referenced this pull request Nov 26, 2026

@fuzeofficlal fuzeofficlal left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed at 1fcd4d450b0a5c0ede77beef50e0381b9a969f0a — APPROVE.

Verification

  • cargo xtask policy (191 files ok), cargo clippy -p victory-shader --all-targets --all-features -- -D warnings, and rustfmt --edition 2024 --check (zero new formatting diffs) all passed.
  • Verified Plan::edge (crates/victory-shader/src/emit/bitcode.rs:1073) across multi-level folded block chains (P -> A -> B -> J), loop backedges, and the plan() fold guard: to()/src() keep folded phi incoming edges paired with their surviving predecessor while dropping untaken direct edges after constant branch simplification. Unlike full liveness pruning, live() is unchanged here, so uses() and emitted shader flags stay consistent for DXIL validation.
  • Passed emit::bitcode::tests (13/13, 1 benchmark ignored, including a_constant_branch_through_a_folded_block_drops_the_direct_phi_entry), emit::shader::tests (1/1), emit::lower::tests (33/33), and emit::tests::constant_color_validates.

Non-blocking notes

  • kept() (bitcode.rs:622) still scans all Op::Phi operands regardless of plan.edge (harmless for current lowering, which does not place internal globals directly in phi incoming slots).
  • Extending the new test (bitcode.rs:1529) with a two-block empty latch chain would also exercise transitive to()/src() remapping directly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Shaders: a constant branch through a folded empty block leaves the phi two entries for one predecessor

8 participants