fix(shader): preserve distinct SSA definitions when folding Phi inputs #3951
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/3951"
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?
Imported from boykopovar/AnyPS5. Originally posted by Scramblrrrr on 2026-10-11T19:43:33Z.
What
A Phi with identical-looking instructions defined in different branches can fold to one branch-local value. That definition does not dominate the other incoming edge or the merged use, so later IR validation fails with
value IR Phi incoming definition does not dominate its edgeorvalue IR instruction definition does not dominate its use.Compare the resolved SSA definitions directly, while allowing separately allocated equal immediate constants to fold. Distinct instruction definitions remain distinct even if their opcodes and operands are structurally equal. This preserves dominance without weakening validation.
The synthetic four-block diamond covers branch-local selects, separate LaneId definitions, one shared dominating definition, equal immediate constants and unequal constants. It contains no title data.
Tested
Validation on Windows, GCC 15.2 Release, rebased base main
4be9c25519(initial negative control ona11c8f7f):agc_constant_phi,agc_structurizerandagc_shader_control_flow: 3/3 passed (0.25 s after restoring the fixed implementation).value IR instruction definition does not dominate its use. Restored the fix, rebuilt, and all three pass again.git diff --checkpass.007 context on #2523: a separate integration stack 8fe3ff1f6 stopped at the Phi-edge dominance error during shader preparation. With this fix, integration candidate e430fcaab passes that stop and reaches 1,404 shader-cache writes in the ongoing run. The earlier locally retained failure capture also recompiles. These are integration observations, not a clean-main title run, full-suite result, visible-frame or playability claim. No game code or captures are included.
AI-assisted: yes (Codex).
Checklist
main; no other open PR implements the same functionsImported from boykopovar/AnyPS5. Originally posted by Dextroid17 on 2026-10-12T01:18:54Z.
The distinction this draws is the right one, and it's implemented precisely: the new condition only allows a fold when both resolved operands are the same object, or both are immediates that compare equal —
same != value && !(same->HasImmediate() && value->HasImmediate() && *same == *value). Immediates are always constructed block-less (no parent), which is exactly why folding two separately-allocated equal immediates together can never violate dominance, while two branch-local non-constant definitions that merely look identical can.This is a real bug, not a theoretical one, and it's reassuring that IR validation isn't a debug-only safety net here —
ValidateProgramruns unconditionally on the only production SPIR-V emit path, with no build-config guard. That caps the severity: before this fix, a shader that hit this could only fail loudly at recompile time, never silently mis-render with a wrong GPU result. It also means the fix can't make anything worse — the new condition only removes folds (the dominance-unsafe ones), so the only possible regression is a missed optimization opportunity, never a correctness loss, and the one case that matters for correctness (a single definition that genuinely dominates both edges) still folds via the identity check.The new four-block diamond test is the right shape — it exercises the failing case, the shared-dominator case, and both the equal- and unequal-immediate cases, and a negative control confirms the old code fails on the same fixture with the exact dominance-violation message this bug produces.
One suggestion, not a blocker: the exception is keyed on
HasImmediate()rather than directly on "this value has no parent block." That's safe today because every immediate in this tree is constructed block-less, but it's an implicit invariant rather than an explicit one — a future pass that ever attaches an immediate to a block could silently reopen this exact bug. Making that assumption explicit (checkingParent() == nullptr, or asserting it) would remove that one indirection.Written with AI assistance.