fix(shader): preserve distinct SSA definitions when folding Phi inputs #3951

Merged
Scramblrrrr merged 1 commit from upstream-pr/3951 into main 2026-10-12 04:30:04 +00:00
Scramblrrrr commented 2026-10-11 19:43:33 +00:00 (Migrated from github.com)

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 edge or value 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 on a11c8f7f):

  • Freshly built agc_constant_phi, agc_structurizer and agc_shader_control_flow: 3/3 passed (0.25 s after restoring the fixed implementation).
  • Negative control: rebuilt the new fixture with main's original ConstantFolder. The diamond fails with value IR instruction definition does not dominate its use. Restored the fix, rebuilt, and all three pass again.
  • Conventions and git diff --check pass.

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

  • Based on current main; no other open PR implements the same functions
  • One topic per PR; follow-ups go in a new PR
  • No comments in code except technical debt
  • Unimplemented paths throw; no new stubs
  • No notes, investigation files or images added to the repository
  • New third-party code is a submodule built from source (none added)
  • Generic behaviour, not specific to one title
  • Depends on: none
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4821814589 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/3951). Originally posted by [Scramblrrrr](https://github.com/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 edge` or `value 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 **4be9c255192bca142dfa349b81caf71b08ebb607** (initial negative control on a11c8f7f): - Freshly built `agc_constant_phi`, `agc_structurizer` and `agc_shader_control_flow`: **3/3 passed** (0.25 s after restoring the fixed implementation). - Negative control: rebuilt the new fixture with main's original ConstantFolder. The diamond fails with `value IR instruction definition does not dominate its use`. Restored the fix, rebuilt, and all three pass again. - Conventions and `git diff --check` pass. 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 - [x] Based on current `main`; no other open PR implements the same functions - [x] One topic per PR; follow-ups go in a new PR - [x] No comments in code except technical debt - [x] Unimplemented paths throw; no new stubs - [x] No notes, investigation files or images added to the repository - [x] New third-party code is a submodule built from source (none added) - [x] Generic behaviour, not specific to one title - [x] Depends on: none - [x] AI-assisted: yes
Dextroid17 commented 2026-10-12 01:18:54 +00:00 (Migrated from github.com)

Imported 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 — ValidateProgram runs 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 (checking Parent() == nullptr, or asserting it) would remove that one indirection.

Written with AI assistance.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6116049955 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/3951#issuecomment-6116049955). Originally posted by [Dextroid17](https://github.com/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 — `ValidateProgram` runs 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 (checking `Parent() == nullptr`, or asserting it) would remove that one indirection. Written with AI assistance.
Sign in to join this conversation.
No reviewers
No labels
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.

Reference
boykopovar/AnyPS5!3951
No description provided.