Skip to content

fix(recompiler): pair structurally equivalent whole-quad evaluations in stencil replay - #9266

Closed
akkadbakkad89 wants to merge 1 commit into
boykopovar:mainfrom
akkadbakkad89:fix/stencil-replay-wqm-duplicates
Closed

akkadbakkad89 wants to merge 1 commit into
boykopovar:mainfrom
akkadbakkad89:fix/stencil-replay-wqm-duplicates

Conversation

@akkadbakkad89

Copy link
Copy Markdown
Contributor

What

On devices without VK_EXT_shader_stencil_export, MaskReplaySafety only accepts a whole-quad (WqmU64) mask expansion when the low and high words extract the same WqmU64 node at indices 0 and 1. IrProgram::CreateValue does not intern values, so a title whose compiler emits s_wqm_b64 twice on equivalent ballot inputs produces two distinct WqmU64 nodes; a mask pairing extract(wqmA, 0) with extract(wqmB, 1) was refused even though the two evaluations are semantically identical.

This was observed on Assassin's Creed Shadows (PPSA20397, #8514): shader 0x407325e400 composes its own-lane mask from two same-block s_wqm_b64 instructions over ballot(exec) sources, captured via agc_shader_replay --materialize on a live RecompileRequest — the pairing gate reports paired=0 on object identity alone.

Both maskStable() and helpers() now pair the words through equivalentWqm(): two WqmU64s are equivalent when their source trees match opcode-for-opcode, treating same values and equal immediates as equal, and requiring any Ballot pair to sit in the same block with no execution-mask-affecting instruction (SetExec, SetExecLo, SetExecHi, Discard, Sendmsg, SetThreadBitScalarRegister, SetScalarMaskTag) between them. When a distinct high-side evaluation is accepted its node tree is added to restricted so the untracked-lane-op checks keep their strength.

This is one of several verifier gaps this shader hits; on its own it does not unblock the title (the EXEC-copy provenance is #9017's scope, multi-block optional bodies are #9030's, and a further quadObserved rejection of a merged phi feeding a nested execz condition remains — detail posted on #8514).

Tested

Windows 10 Pro 22H2, WinLibs GCC 15.2.0 r7, Debug, on main cecb540a6.

  • agc_masked_select, two new cases: a mask pairing extract(wqmA, 0) with extract(wqmB, 1) over structurally identical, same-block ballot trees is replay-safe (stencil replay pairs structurally equivalent whole-quad evaluations for split low and high words); the same pair separated by SetExecLo is refused (stencil replay refuses to pair whole-quad evaluations separated by an execution-mask change). Reverting the pairing relaxation makes the first fail while the second still passes; all pre-existing cases pass unchanged.
  • agc_shader_replay --materialize on the captured 0x407325e400 request (ANYPS5_NO_SHADER_CACHE=1): the two same-block WqmU64s now pair (paired=1, verified by instrumented build); the check then proceeds to the unrelated control()/optionalBody gate covered by fix(recompiler): accept optional wave bodies of several blocks in stencil replay #9030.
  • python3 tools/check_conventions.py --base origin/main: pass. python3 tools/pr_overlap.py --branch fix/stencil-replay-wqm-duplicates: no same-scope overlap (fix(recompiler): accept EXEC copies in stencil replay masks #9017 touches helpers()' ballot provenance, fix(recompiler): accept optional wave bodies of several blocks in stencil replay #9030 touches optionalBody — disjoint hunks).
  • Not run: Khronos validation layer (not installed), the title itself (still gated downstream).

Checklist

@fogydes

fogydes commented Oct 23, 2026

Copy link
Copy Markdown
Contributor

OK, with two coordination notes. Verified against the branch rather than taking the description on faith:

  1. The pairing machinery is what the title says: restrictSource / execEquivalent / equivalentSource / equivalentWqm in core/shader/recompiler/StencilReplay.cpp, used from both pairing sites (maskStable and the helpers site) via waveSize == 32u || (highWqm != nullptr && Immediate(mask.high->Argument(1), 1u) && equivalentWqm(wqm, highWqm, depth)) — the 32-wide fast path is unchanged, so no drift for 32-lane waves. Distinct highWqm evaluations get restrictSource(highWqm, 0u) so they stay inside the restricted set, which is consistent with the verifier's restrictions (any Ballot/WqmU64/LaneId reachable must be restricted). [verified by read]
  2. One piece is the author's analysis rather than something the tests can prove: the execEquivalent mask-writer opcode list. I enumerated the opcodes I could find that write exec or thread bits and it matches, but exhaustiveness — that no other opcode relocates exec state — can't be checked from the diff alone, and the added tests would pass even if the list were missing one writer. If that list ever needs a new opcode, the fix is silent, so it is the one part I'd keep an eye on in the future PRs in this file. [author-reported; my enumeration is consistent but not exhaustive]
  3. Tests: the two new MaskedSelect.cpp cases exist as named, and the agc_masked_select_tests target registration is the pre-existing loop. The "relaxing fails / strict passes" run numbers are author-reported only. [verified existence; runs author-reported]

Minor convention nit: the branch commit subject is fix(shader): pair structurally equivalent whole-quad evaluations while the PR title is fix(recompiler): … in stencil replay — bring the subject to match at the next push.

Freshness: current main is 0db47b263d; 1198 commits behind since base d34da5d694, and zero main commits since the base touch StencilReplay.cpp or tests/MaskedSelect.cpp; merges into main clean. [verified]

Cross-PR ordering (both #9017 and #9030 also touch these two files, both open — verified states):

Disclosure: source-only verification plus merge checks; no recompiler test compiled or run locally here (GPU test device absent), so all run claims including the paired=1 instrumentation observation are author-reported. No evidence in this PR cites PS5 hardware, and the PR makes no PS5 confirmation claim, so the debt rules are not triggered.

@akkadbakkad89

Copy link
Copy Markdown
Contributor Author

@fogydes thanks for the read-verification — your three points are accurate.

  1. Fair caveat on execEquivalent exhaustiveness: the writer list is enumerable (as you did) but not provably complete from the diff. Since this commit lands inside fix(recompiler): exempt proven control conditions and own-bit ballots in stencil replay #9317 under the agreed option-2 merge, I will carry this note forward — any future PR touching that list needs its own enumeration check, and a silent miss there degrades to a missed pairing rather than a wrong one (the paired proof still gates on equivalentWqm), so the failure mode stays conservative.
  2. On the subject nit: agreed it is inconsistent, but this PR closes as superseded (option 2 is unanimous across @oneandonlydean, @TheSovietWaffle and us) — its commit lands via fix(recompiler): exempt proven control conditions and own-bit ballots in stencil replay #9317 as eb61ff9c, and amending the message there would re-sha the head 92ebac241 that dean already tree-verified and two follow-up PRs (fix(recompiler): accept optional-body merges implied by the skip predicate in stencil replay #9393, fix(recompiler): accept optional bodies that end the wave through an exit that keeps no lane #9904) are stacked on. Leaving it as-is deliberately; noted for future stencil commits.
  3. On run numbers: the agc_masked_select results are not author-only anymore — xXPBSXx and the TheSovietWaffle/MarcZX10 paired validation-layer run on fix(recompiler): exempt proven control conditions and own-bit ballots in stencil replay #9317 cover this code on RTX 3080 + RDNA2 Windows with zero new VUID- lines, and dean replayed two title requests end-to-end through the full stack.

Your merge-tree findings match what we hit: the MaskedSelect.cpp conflict vs #9017 is already resolved inside #9317 (keep-all-test-blocks union), which is exactly why the sequential merge order needed the carried stack.

@github-actions github-actions Bot added the needs rebase Conflicts with main, rebase needed label Oct 23, 2026
@akkadbakkad89
akkadbakkad89 force-pushed the fix/stencil-replay-wqm-duplicates branch from 9b578d5 to 29bb5f0 Compare October 23, 2026 17:20
@akkadbakkad89

Copy link
Copy Markdown
Contributor Author

Rebased onto main @ 4883171 (post-#8145). Only conflict was the MaskedSelect test additions vs #8145's read-only BDA test — resolved as a union; both tests are kept. check_conventions passes.

@akkadbakkad89

akkadbakkad89 commented Oct 26, 2026 •

Copy link
Copy Markdown
Contributor Author

Heads-up from the #9317 review sweep: this PR's MaskedSelect.cpp diff anchors on bdaWrites = true; plus the BDA lambda's return !StencilReplaySafe(b.program);/}); and inserts wholeQuadEval/splitQuads/the new tests inside that captureless [] lambda — the file does not compile (verified: passed/pixelPredicate/exportUnder uncaptured, brace mismatch at int main()).

Fixed and rebased onto current main: pushed 80c925df72 to fix/stencil-replay-wqm-duplicates — the BDA lambda is closed after bdaWrites = true; and the helpers/tests sit at top-level scope. agc_masked_select_tests builds and passes on Windows (exit 0); the test is IR-level so no Vulkan device is involved.

@oneandonlydean

Copy link
Copy Markdown
Collaborator

@akkadbakkad89 Thanks for the scope fix in 80c925df72. One problem remains in equivalentSource (StencilReplay.cpp:169-177): it treats unequal immediate leaves as equivalent. sameValue(1, 2) returns false, but both constants have opcode Void and no arguments, so the opcode/argument loop that follows returns true. That also pairs different constants inside otherwise identical ballot trees, for example a low Ballot(P && helper == 0) with a high Ballot(P && helper == 1), and the helper check done on the low side is then reused for the high side.

Could you compare the immediate's kind/type and value before the structural recursion, and add a full-verifier refusal case where the two WQM source predicates differ only in a constant, including helper == 0 versus helper == 1? #9317 carries this helper, so please carry the fix there too.

IrProgram does not intern values, so titles that evaluate the same
whole-quad ballot twice emit distinct WqmU64 nodes for the low and
high mask words. maskStable and helpers paired those words by object
identity and rejected the split form. Compare the two evaluations
structurally instead: same-block ballots must agree on their source
tree and must not be separated by an execution-mask change, and every
newly accepted node is recorded as restricted. Assassins Creed
Shadows (PPSA20397) hits this in shader 0x407325e400.

Generated with [Devin](https://devin.ai)

Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
@akkadbakkad89
akkadbakkad89 force-pushed the fix/stencil-replay-wqm-duplicates branch from 80c925d to 46ff428 Compare October 26, 2026 08:51
@akkadbakkad89

Copy link
Copy Markdown
Contributor Author

Verified and fixed at 46ff428ea2.

Claim confirmed: sameValue(1, 2) correctly returns false, but both immediates are Void opcode with 0 arguments, so equivalentSource's opcode/argument loop returned true vacuously — the recursion fell through without comparing kind, type, or value. Concrete consequence verified in the pairing path: a low Ballot(P && helper == 0) and a high Ballot(P && helper == 1) paired, so the low side's RealOnly provenance was reused for the high side whose predicate actually includes helpers.

Fix: equivalentSource now compares immediate kind/type/value before the structural recursion — if (left->HasImmediate() || right->HasImmediate()) requires both immediate with equal Type() and ImmediateU64() (mirroring the canonical sameValue in IrProgram.cpp), otherwise not equivalent.

Test: added "stencil replay refuses to pair whole-quad evaluations whose predicates differ only in a constant" — exactly the helper == 0 vs helper == 1 shape you described. Red/green verified: pre-fix StencilReplay.cpp fails on this case (exit 1, the evals wrongly pair), fixed passes the full suite (exit 0).

The fix is carried into #9317's stack — its rebased head 4a59035543 sits on this commit.

@oneandonlydean

Copy link
Copy Markdown
Collaborator

Closing this as superseded: #9317 has merged and carries this change. Thanks!

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

Labels

agc libSceAgc* shader core/shader

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants