fix(recompiler): exempt proven control conditions and own-bit ballots in stencil replay #9317
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/9317"
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 akkadbakkad89 on 2026-10-21T18:16:17Z.
What
MaskReplaySafetywalks downstream uses of mask-derived values and rejects any consumer that can observe the whole quad. On the stencil-replay shaders of Assassin's Creed Shadows (PPSA20397, #8514) and Onimusha (#7006) that accounting rejects three lane-local patterns:control()already proves replay-safe. A value merged by a region phi and reused as the predicate of a nestedexeczguard was refused even though that very branch passed its own control validation.Ballot, regardless of how its bits are read — including the compiler's own-lane funnel (CompositeExtractU32x4per word -> optional lane-local&mask->SelectU32(lane<32, low, high)->ShiftRight(., lane&31)->&1), which reads only the lane's own bit and never crosses lane boundaries.ImageSampleExplicitLodand raw sample/gather ops whose memory flags carry an explicit LOD do not compute implicit quad derivatives, so per-lane arguments stay lane-local — the same distinctionMaskedSelectEliminator'sisExplicitLodSamplealready makes.The check now:
controlProven()(with a visiting set, so nestedexeczcascades recurse safely) and exempts a condition use inquadObservedonly when the consuming branch is proven — an unproven branch still rejects.Ballotonly when every use completes the verified own-bit funnel above (correct half extraction, correct select arm,lane&31shift,&1isolate); the isolated per-lane bools continue through the normal traversal. Any other use rejects.quadObservedandhelperObserved; implicit sampler consumers still reject.optionalBody()'s region set local to each invocation — nested control proofs previously unioned inner and outer regions into one set.This is gap 2 of the three
MaskReplaySafetygaps described on #7006 (the pre-pass that runscontrol()validation before counting condition uses and own-lane ballots as quad observations), and it clears the last verifier gate documented for PPSA20397 on #8514 (phi ->LogicalNot-> nestedExecZero, with the same merged value feeding an own-bit ballot chain).Verified on shader
0x407325e400captured from PPSA20397: with #9017 + #9030 + #9266 applied,StencilReplaySaferefused the mask replay at these gates; with this changeagc_shader_replay --prepareon the same request emits 96,161 SPIR-V words. It then stops inSrtWalker::EvaluateRuntimeSourceson an unresolved descriptor source — a runtime-capture limitation of the offline replay, not the verifier.Tested
Windows 11, WinLibs GCC 15.2, Debug, on main
6b6c7016with #9017, #9030 and #9266 cherry-picked under this commit.agc_masked_select, five new cases: an optional-body merge may guard another optional wave body; an optional-body merge cannot guard a body with side effects; an optional-body merge cannot feed a ballot escaping the lane's own bit; an optional-body merge may feed an own-lane ballot read; an active mask may flag a sample with an explicit LOD. Reverting the source change makes the three positive cases fail while the negative ones still pass; all pre-existing cases pass unchanged.agc_shader_replay --prepareon the captured0x407325e400request (ANYPS5_NO_SHADER_CACHE=1): passesStencilReplaySafeand prepares 96,161 SPIR-V words.python3 tools/check_conventions.py --base origin/main: pass (1 pre-existing debt-length warning from #9017's entry).python3 tools/pr_overlap.py --branch fix/stencil-replay-quad-observations: no same-scope overlap (#7410 touches the opcode list only, different topic).Checklist
main; no other open PR implements the same functionsNotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file.mdfiles or images added to the repository (attach them to this PR)prepared: 96161 SPIR-V words); Windows 11Generated with Devin
Imported from boykopovar/AnyPS5. Originally posted by xXPBSXx on 2026-10-21T20:41:22Z.
Independent Windows run of this PR (data only).
Environment: Windows 10 Pro 10.0.19045, Ryzen 7 5800X3D, WinLibs GCC 15.2.0 r7 (MinGW-w64 UCRT), Release, BUILD_TESTING=ON. Merge-tree of the PR head against
mainis clean. I built onlyagc_masked_select_tests;ctest -N -R agc_masked_selectlists exactly one test.PR head tested:
5e91c441c4d86581d7d7e5bae820ba36d54591b5.Results (3 runs each):
agc_masked_selectpasses 3/3.core/shader/recompiler/StencilReplay.cppwith the version from the PR's merge base (6b6c701636), then rebuilt. The test fails 3/3 (ctest reports Failed). I restored the PR's files afterwards.GPU: the run output has no
Selected GPUline; no GPU claim is made.Not run: other platforms, the other tests in the same family, the full repository CTest, and any title or console run.
AI-assisted (Claude Code).
Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-22T05:36:06Z.
@akkadbakkad89 thanks for #9317. On our own logged request (PPSA28183, the program that reached
0x407a788000in our runs), main + #9017 + #9030 + #9266 + #9317 now replays end to end:prepared: 96941 SPIR-V words, and spirv-val passes. Without #9317 it still stops atquadObserved. The exemptions read right to me: each one is either lane-local or tied to the branch's owncontrol()proof. CI is green apart from one known flaky test, which is rerunning. Next we'll run the title on that stack.One thing for when you rebase: on top of #9030's current head (
9229bcec52), #9317 conflicts inStencilReplay.cpp. #9030 makesregiona member, and #9317 uses a local set. In our local merge, keeping the member broke the result, becauseoptionalBody -> quadObserved -> control -> optionalBodyre-enters, and the inner proof overwrote the outer proof's region. Keeping #9317's per-call local fixed it. Could you rebase #9317 onto #9030 with the local kept?Imported from boykopovar/AnyPS5. Originally posted by akkadbakkad89 on 2026-10-22T06:42:27Z.
@oneandonlydean great data — second title clearing the verifier end to end, and spirv-val on top, is exactly what the stack needed.
On the conflict: this PR's head
5e91c441already resolves it the way you landed on — it sits on #9030's head9229bcec52and turnsregionback into a per-call local insideoptionalBody(the member is removed entirely, andinactiveEqualstakes the local by const-ref so nestedcontrol()proofs can't bleed regions). So the resolution is "take this PR's version"; no rebase needed unless #9030 moves. The re-entrancy you hit —optionalBody → quadObserved → control → optionalBody— is precisely what forced the local on our side too: with the member, the inner walk'sinsertmutates the outer region and the outer predecessor check then rejects blocks it just added itself.Thanks for running the title as well — that's the piece neither of us could validate (we only have the offline request for PPSA20397, which passes
--preparebut stops later inSrtWalker::EvaluateRuntimeSources, a descriptor-materialization limitation of the harness, not the verifier).AI-assisted: yes (Devin).
Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-22T07:17:02Z.
@akkadbakkad89 thanks, that matches what we found. One detail on the base: GitHub's compare shows
5e91c441c4isn't on top of #9030's current head.9229bcec52("accept optional wave bodies of several blocks in stencil replay", which touchesStencilReplay.cppandMaskedSelect.cpp) is not in this branch's history; it's 1 commit behind it, and the same holds for #9266's head9b578d5dc7. So #9030 seems to have been amended after you based #9317 on it, and that's where our textual conflict came from. A rebase onto9229bcec52, with the per-call local kept as you describe, would let the stack merge in order. Our title run on the locally resolved stack is queued for later today.Imported from boykopovar/AnyPS5. Originally posted by akkadbakkad89 on 2026-10-22T07:26:31Z.
@oneandonlydean rebased — head is now
92ebac24, a clean four-commit chain onmaind6eafa54carrying each PR's current head diff in order (#9017027033f1→ #90309229bcec→ #92669b578d5d→ this change), so the stack merges in order. Each commit's diff is identical to its published head (verified file-for-file; #9017's debt entry lands at a different position because the file moved upstream, same added text).regionstays a per-call local —optionalBody's member is removed andinactiveEqualstakes the local by const-ref, exactly the resolution your merge needed.Re-verified on the new head:
agc_masked_selectpasses, andagc_shader_replay --prepareon the PPSA20397 request still reachesprepared: 96161 SPIR-V words.AI-assisted: yes (Devin).
Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-22T08:00:52Z.
Thanks for the rebase. I checked 92ebac2 against the three PRs it carries: each carried commit matches the published diff of #9017, #9030 and #9266, and your own commit is unchanged from the version I reviewed. Full CI on this head is green on Linux and Windows (agc_masked_select passes on both).
One thing for the merge order. Because the carried commits are re-created copies, git doesn't treat them as the same commits as the originals. Merging the originals first and then this PR conflicts (MaskedSelect.cpp, StencilReplay.cpp). I see two ways to land it, and both give the same final tree:
@TheSovietWaffle (#9017, #9030) and @akkadbakkad89 (#9266, #9317): which do you prefer? Option 2 is quicker, but it's your call, since it closes your PRs. The carried commits keep their original authors either way.
Imported from boykopovar/AnyPS5. Originally posted by akkadbakkad89 on 2026-10-22T08:10:57Z.
@oneandonlydean @TheSovietWaffle — option 2 gets my vote for #9266 + #9317.
The final tree is identical either way, this repo merges with merge commits so each carried commit keeps its author in history, and what lands is byte-for-byte the stack that already replayed both titles end to end. It also saves two more rebase+CI cycles and removes the intermediate states where the verifier still rejects real shaders.
Two things worth being explicit about:
From us: fine to close #9266 as superseded whenever the stack goes in.
AI-assisted: yes (Devin).
Imported from boykopovar/AnyPS5. Originally posted by TheSovietWaffle on 2026-10-22T09:10:49Z.
@akkadbakkad89 data point for PPSA27836 (Onimusha: Way of the Sword) on this branch (
92ebac241) + #8145, checked offline withPrepareResourceProgram+StencilReplaySafeon the logged requestshader_2111c51800:quadObserved, through aBallotthat is not only read as an own bit. It isBallot(ownLane(liveWords) && !killed), and its two words also feed an any-active early-exit gate ((lo | hi) != 0) and the merge phis of the export mask after the kill. So the own-bit funnel doesn't apply.That ballot's argument is false on every helper lane regardless of the merged value:
liveWordsisBallot(!helper) & WQM(active), soonHelper(argument) == 0. Would you accept exempting a ballot inquadObservedwhen it is restricted (its masks already passmaskStable/helpers) andonHelperproves its argument false on helper lanes? Then helper lanes can't contribute to any of its words, whatever the phi holds there. Locally that is one line before your funnel check:With that, #9393's
inactiveEqualsrule, and one more change for an early exit inside the optional body (a discard's "no live lanes left" return; I'll open it on top of this stack), the request passesStencilReplaySafe. Without #9393's rule it still refuses (the merged flag is an own bit ofBallot(P)merged withP).Since #9317 already covers what #9394 does in a stricter way, I'll close #9394 and rebase #9393 onto this PR (
Depends on: #9317).AI-assisted: yes (Claude Code).
Imported from boykopovar/AnyPS5. Originally posted by akkadbakkad89 on 2026-10-22T09:17:41Z.
@TheSovietWaffle yes — accept, and the rule is the clean dual of the funnel: we bound consumption (only the own bit is ever read), this bounds contribution (the argument is provably false on helpers, so helper lanes contribute 0 to every word whatever the merged phi holds there). With
onHelper(arg) == 0the ballot's words are a pure function of real-lane arguments, so their consumers — early-exit gates, export phis — can't observe anything about the helper instance. The guards are also right:restricted.containsscopes it to ballots already proven inside a stable mask chain, and theonHelperanswer here resolves throughown()/RealOnlyrather than the phi itself, which is exactly the independence the exemption needs. Skipping the walk through the ballot is safe for the same reason — nothing downstream of those words carries helper information.On placement: head
92ebac24is the verified merge candidate while the option-2 vote is open (oneandonlydean checked it byte-for-byte against the carried heads), so we'd rather not move it — the line lands cleanly as your own commit in your stack (#9393 or a small PR,Depends on: #9317), which also keeps the authorship and the onHelper-duality review with you. If you'd rather consolidate the quadObserved exemptions in #9317, say so and we'll take it in with you as author — either way the code lands.Also good news on the stack shape: #9393 rebasing onto this PR and #9394 closing as covered fits — the merged-flag case you describe (own bit of
Ballot(P)merged withP) is exactly the stricterinactiveEqualsdirection.AI-assisted: yes (Devin).