Shaders: a constant branch through a folded empty block leaves the phi two entries for one predecessor #2621
Labels
No labels
accessibility
area: build
area: formats
area: gpu
area: loader
area: runtime
area: shader
bug
documentation
duplicate
enhancement
good first issue
gpu: amd
gpu: intel
gpu: nvidia
help wanted
host: wine
invalid
out of scope
question
request
run report
triage: confirmed
triage: has patch
triage: merge candidate
triage: needs info
triage: needs rebase
triage: needs work
triage: proposed close
wontfix
No milestone
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
github/Marvisak--VIctory#2621
Loading…
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
@BastienRodz on GitHub · 2026-11-25 17:00 UTC · original
What happens
Since
286a00e("the DXIL writer leaves out what no edge or reader reaches"), the 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(crates/victory-shader/src/emit/bitcode.rs) decides whether a phi of blockxkeeps its entry fromb:It follows
b's successors through folded blocks (to). Takeentry: CondBr(true, empty, join), whereemptyhas nothing live and ends inBr(join), andjoinhasphi [(one, empty), (two, entry)]. Then:term(entry)becomesBr(empty), so the direct edgeentry → joinis gone;emptyis folded intoentry → join;emptyis kept and written fromentry, which is right. But the entry fromentry(the untaken direct edge) is kept too, becauseto(empty) == join.The phi ends up with two entries for its one predecessor
entry, which is invalid IR for the validator and drivers.Expected
286a00e: "phis lose the entries of edges that are gone (their other entries stay)".Plan::edge's own comment: the entry stays if "the edge is still there (not a constant branch's other edge ...)". After folding,joinshould have one entry, fromentry, with the value that came throughempty.Where
crates/victory-shader/src/emit/bitcode.rs,Plan::edge.AI-assisted (Gemini 4 Neon found it, Claude Code reviewed and verified it); synthetic test only, no game data.
@studiostephe on GitHub · 2026-11-25 17:26 UTC · original
A regression test for the bug, plus the one-line fix it isolates.
The fix —
Plan::edge(bitcode.rs:1061) keeps a phi entry frombwhen any ofb's successors, through folded targets, lands inx. Whenentry: CondBr(true, empty, join)folds the empty block intoentry -> join, the untaken direct edgeentry → joinis gone, butto(empty) == joinstill keepsentry's own entry — so the phi gets two entries for its one predecessor, which is invalid IR. The edge must also not arrive through a block folded fromb(andbmust not itself be folded):The test — the exact scenario from the issue (
entry: CondBr(true, empty, join),emptyempty with one predecessor,join's phi[(one, empty), (two, entry)]):Verified:
Plan::edgeunchanged,plan.edge(&m, entry, join)istrue(the untaken direct edge's entry is kept becauseto(empty) == join), so theassert!(!plan.edge(&m, entry, join))panics. With the fix, both assertions pass.Plan::edgetests (a_constant_branch_drops_its_other_side_and_that_side_s_phi_entry,a_loop_whose_back_edge_is_never_taken,empty_blocks_before_a_phi_fold_only_where_the_entry_can_move) still hold: in each, every kept entry's source and successor are unfolded (or the successor is folded into the phi's block from a different predecessor), sosrc(b) == b && src(s) == bis satisfied.cargo check -p victory-shader --profile test --target x86_64-pc-windows-msvc --locked— clean (the test module is#[cfg(all(test, windows))], so it does not compile on this Mac; the cross-compile confirms it type-checks and thePlan::edge/plan.fold/plan.src/plan.toAPIs used are the ones the existing tests use).rustfmt --edition 2024 --check— no new diffs versus unpatched main (19 pre-existing line-width diffs in this file on main; my lines add none).cargo clippy -p victory-shader --target x86_64-pc-windows-msvc --locked— 0 new warnings.Not covered: I could not run the test (no Windows; the
bitcodetest module is Windows-only because the writer'sm.write()path and the sibling tests pull in Windows-gated helpers). The failure-on-unpatched / pass-on-patched behavior is verified by tracingPlan::edgethrough the fold: unpatched,successors(term(entry)) = [empty, join](theCondBr(true,..)keeps both successors inm.term), andto(empty) == join, soedge(entry, join)is true; patched,src(empty) == entry != join's requiredb, and for theemptyentrysrc(empty) == entryso it is kept from entry whileedge(entry, join)requires a successorswithsrc(s) == entryandto(s) == join—emptyqualifies for the empty entry but the entry from entry has no such successor (its direct edge is gone, and the only successor that reachesjoinisempty, which is folded fromentry, i.e. it is the same edge, not a second one).qwen3.8_27b running locally on an M1 Mac
shout out to Digital Debris Los Angeles
@Marvisak on GitHub · 2026-11-25 18:46 UTC · original · edited 2026-11-26 06:48 UTC
Automated triage for the maintainers.
Confirmed on
main(63bcca4):Module::termturnsCondBr(true, empty, join)intoBr(empty), the planner foldsemptyintoentry -> join, andPlan::edgeincrates/victory-shader/src/emit/bitcode.rsstill keeps the entry fromentrybecauseto(empty) == join, sojoin's phi gets two entries for its one predecessor. The DXIL validator refuses the module, so the program is lost (not miscomputed).Fix PRs: #2622 (one-line change plus a regression test that fails on
main) and #2657 (same predicate plus a broader liveness change; conflicts with #2622 on that line). #2692 was closed in favour of #2622; its self-loop test is worth carrying. New since the last triage: on an RTX 4070 the end-to-end repro fails validation onmainand validates and draws correctly with either PR, with no new test failures. #2657's extra tests cover its separate liveness change, so landing #2622 and reducing #2657 to that part is one option.Generated by Claude Code
@Kajanos on GitHub · 2026-11-25 18:59 UTC · original
Review of the fix PRs for #2621 (main
13f91ca), by reading the code:Plan::edgewas never needed: a foldedshas exactly one predecessorb, andx's phi has an entry frombonly ifbbranches toxdirectly. Soto(s) == xonly adds the untaken constant-branch edge, which is this bug.contains(&x)is the right rule.s,src(s) == src(b)always holds. Its self-loop test passes under #2622's rule and is worth carrying.self.src(b) == b && ...) is wrong: it also drops the folded block's own entry (src(empty) == entry), sojoin's phi has no entry left, and its own first assertion (plan.edge(&m, empty, join)) fails.has_edge) and conflicts on that line. Its liveness change is separate. It also leavesworklist_liveness_matches_passescomparing againstlive_by_passes, which still uses the old semantics; that test passes only because the synthetic modules have no constant branches. Suggest landing #2622 first and rebasing #2657 to the liveness part only.AI-assisted (Claude Code). Reasoning only; tests not run (no Windows host).
@KainanP on GitHub · 2026-11-25 22:53 UTC · original
The #2621 PRs, measured end to end on an RTX 4060, Intel UHD and WARP
There are three PRs for this bug: #2622 and #2657 (open), and #2692 (closed, kept here for reference). I ran the same end-to-end reproduction against each one, so they can be compared directly. No game, no game data.
Machine: Windows 11 Home 26200. DXGI adapter 0: RTX 4060 Laptop GPU (driver 32.0.16.1047). Adapter 1: Intel UHD Graphics (8086:a78b, 31.0.101.5445, a 16-lane part). Also WARP (10.0.26100.9818). DXIL validator from the Windows SDK.
mainat13f91cae, Rust 1.98.1 as pinned.Reproduction (on
main, no game)Commit
6ab53c88onKainanP/VIctory@test/2621-folded-phi-reproadds one test toemit/rect.rs,repro_2621_folded_constant_branch_keeps_one_phi_entry. It builds the shape from this issue as a pixel shader:entry: CondBr(true, taken, join),takenempty, andjoin: phi [(0.25, taken), (0.75, entry)]written to the red channel. It runs the module through the DXIL validator, then draws it on the hardware adapter and on WARP. The shape comes from @andrewpozdnakov7-jpg's 0e74a15, which only drew on WARP; I extended it to the hardware adapter.mainPHINode should have one entry for each predecessor of its parent basic block!1fcd4d45)d52b05f7)1f3b25ab, closed)So the practical effect on
mainis a validation refusal: the translated program is lost. It does not produce a wrong value. When the unvalidated module is handed to the drivers anyway, all three of them pick 0.25, the value throughtaken.Per PR (RTX 4060 unless noted)
"Suite" means
cargo test -p victory-shaderwithheap_direct_texture_reads_mark_their_heap_indexskipped (#2571). On this RTX,mainpasses 266 and fails 3. The 3 failures are the known NVIDIA ones:a_heap_buffer_read_by_element_takes_the_views_stride,a_heap_buffer_read_where_no_buffer_view_is_reads_zeros,gathers_at_a_level_match_the_hardware. None of these PRs touchvictory-gpu.13f91caemaina_constant_branch_through_a_folded_block_drops_the_direct_phi_entrythe untaken direct edge goes)Plan::edgelinedropped_direct_phi_edge_from_reachable_pred_does_not_keep_value_alive)livesignature)Intel UHD (local adapter hook,
main+ #2587 + #2622, andmain+ #2596 + #2657): the same 9 tests fail as onmain, and the repro passes. Onmainthere are 10 failures, the 9 Intel-known ones plus the repro. The 9 are the 3 BVH tests,arithmetic_matches_the_hardware,a_heap_buffer_read_where_no_buffer_view_is_reads_zeros,a_mask_joined_with_data_at_a_uniform_loop_is_its_number,permutes_read_the_lane_named_and_zero_from_lanes_left_out,fspx_programs_run_with_the_hardwares_dot_productsandimage_atomics_of_64_bits.WARP (
VICTORY_WARP=1, same two trees):mainfails the 3 WARP-known tests (a_mask_joined_with_data_at_a_uniform_loop_is_its_number,gathers_at_a_level_match_the_hardware,permutes_read_the_lane_named_and_zero_from_lanes_left_out) plus the repro. Both trees fail only the 3.Semantic differences
Plan::edge, toself.seen[b] && successors(m.term(b)).contains(&x). A phi entry frombnow stays only ifb's own simplified terminator still namesx. A folded empty block still namesx, so its entry stays and is written from the block it was folded into. The untaken direct edge no longer matches. Output changes only for modules that have this shape.Module::has_edge, whichPlan::edgenow calls), so for this bug it behaves like #2622. It also scopesModule::liveto reachable blocks and kept phi edges, andModule::keptto kept entries. Producers that only a dropped edge or an unreachable block read are no longer written, so feature flags such asWAVE_OPS,INT64orRESOURCE_HEAPcan clear and more blocks fold. That is a broader output change. It passeswritten_bytes_unchangedand the whole suite here, but I could not measure it on title shaders.self.to(s) == x && (self.src(s) == s || self.src(s) != from). That is equivalent for this shape. Its self-loop test passes under both #2622 and #2657, so it is worth carrying, as the triage note suggested.self.src(b) == b && ...) is not in any PR. As @Kajanos noted, it would also drop the folded block's own entry.Since
mainmoved tof3578e38(74 commits, while I was testing)f3578e38: the validator refuses the module with the same message.f3578e38+ #2622 (+ #2596 for #2571), the repro passes. The wholevictory-shadersuite on the RTX passes 301 and fails the same 3 known ones (mainpasses 298 and fails those 3 plus the repro).f3578e38also makes the repro pass. On the RTX the suite passes 303 (298 plus its 4 tests and the repro) and fails the same 3.f3578e38+ #2622 (+ #2596): the repro passes. The suite fails only whatmainfails there, which now includesmain's newpermutes_mask_high_address_bits_and_divergent_branches("no pipeline for a wave of 32"; it fails onmaintoo on both).Recommendation
mainand passes with it, and it showed no regression on RTX, Intel or WARP. If you want an end-to-end check as well, carry #2692'sa_self_looping_header_keeps_its_back_edgeand either 0e74a15 or the repro above.keptscoping. It passed everything here as is, so it can replace #2622 if you prefer one PR. But it changes more of the emitted output than this bug needs.Reproduce
The Intel runs used a local adapter-selection hook (
VICTORY_TEST_ADAPTER=Intel, which logs the adapter it picks). That hook is not upstream.Tested with the help of an AI assistant (Claude Code).
@pawrenity on GitHub · 2026-11-26 04:29 UTC · original
#2621: #2622 and #2657 compared
Each PR was merged onto current
main(63bcca43) and tested there. Hardware: NVIDIA GeForce RTX 4070, Windows 11, with the Windows SDKdxil.dllvalidator. Pinned toolchain,--locked.main(test hunks only)-D warnings63bcca43victory-shadersuite (merged)1fcd4d45a_constant_branch_through_a_folded_block_drops_the_direct_phi_entry: pass!at(dead_sum) …,!plan.live[i]), which #2622 does not change;worklist_liveness_matches_passeswith #2657's helper: pass--all-features, all targets)d52b05f7End-to-end repro. This is the test from commit
6ab53c88(posted above), applied as a test only. It builds the #2621 shape as a pixel shader, validates it, and draws it on the hardware adapter and on WARP.main, the validator refuses the module:PHINode should have one entry for each predecessor of its parent basic block!,%2 = phi float [ 2.500000e-01, %0 ], [ 7.500000e-01, %0 ]. Both adapters still draw 0.25.[0.25, 0, 0, 1].Suite.
mainalone gives 298 passed and the same 3 failures:a_heap_buffer_read_by_element_takes_the_views_stride,a_heap_buffer_read_where_no_buffer_view_is_reads_zerosandgathers_at_a_level_match_the_hardware. They still fail when run alone with--test-threads=1, onmainas well. All suites were run with--skip heap_direct_texture_reads_mark_their_heap_index(#2571).The two PRs conflict with each other on the
Plan::edgeline inbitcode.rs.What differs
The #2621 fix itself is identical.
Plan::edgebecomesself.seen[b] && successors(m.term(b)).contains(&x)(bitcode.rs:1062at its head).Module::has_edge(:585) is the same expression, andPlan::edgecalls it (:1094).Dropping the
to()mapping is safe in both.planalready refuses a fold that would leave a predecessor with two edges into the target (bitcode.rs:707onmain), so a folded block's ownBr(x)is the only path through it. That is why #2622's test passes against #2657's fix.#2657 also changes liveness, which is a separate change from #2621:
Module::livenow runs after reachability.Module::keptreads a phi's globals only through kept entries.As a result, pure values used only behind an untaken constant branch, and the
INT64,WAVE_OPSand heap flags they set, are no longer written. #2657's four tests cover exactly this, which is why they fail with #2622's fix alone.Without that part, #2622 still writes such producers, but
uses()readsplan.live, so the flags still match what is written. The module stays valid. It just carries dead code.Recommendation
#2622 is the minimal fix for #2621. Its test fails on
mainand passes with it, the end-to-end repro passes, and I saw no regression. I suggest landing it.#2657 is correct as far as I checked, and it fixes #2621 equally. It also changes more of the emitted output: it passes
written_bytes_unchangedand the suite, but I did not measure it on title shaders. If the maintainers want the liveness change, it can land instead of #2622. Or #2622 can land first, with #2657 rebased onto it, which leaves the liveness part and the four tests as its only diff.Not tested: title shaders, in game.
AI-assisted (Claude Code).