fix(recompiler): accept EXEC copies in stencil replay masks #9017

Merged
TheSovietWaffle merged 1 commit from upstream-pr/9017 into main 2026-10-23 23:23:52 +00:00
TheSovietWaffle commented 2026-10-21 09:50:57 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by TheSovietWaffle on 2026-10-21T09:50:57Z.


What

On devices without VK_EXT_shader_stencil_export, a stencil-exporting pixel shader is replayed only if MaskReplaySafety proves its own-lane masks stable. Since #6291 a pixel shader's EXEC copy (s_mov_b64 s[..], exec and the restores built from it) is read as the saved word ANDed with WQM(Ballot(true)). The proof accepts a whole-quad (WqmU64) expansion only of a ballot whose helper lanes it can classify (HelperMask::RealOnly or QuadStable), and a ballot of true was Uncertain because helper lanes vote true too. So any shader whose own-lane mask plumbing goes through an EXEC copy was refused with "own-lane mask not stable". Reported on #8145 for Monster Hunter Wilds (PPSA24191, by @Lindamea1) and Assassin's Creed Shadows (PPSA20397, by @akkadbakkad89), both at the s_mov_b64 exec, s[..] restore.

Every lane of a quad runs (covered pixels and helpers), so a ballot of true has the same bits within each quad as the all-ones mask, which helpers() already classifies as QuadStable. A ballot of an immediate true is now classified the same way. Ballots of anything else are unchanged, so helper-dependent predicates stay Uncertain and their whole-quad expansions stay refused. The stencil reference export replay entry in docs/dev/debt/libSceAgcDriver.md says so.

This removes one of the gaps those reports list. The other one they name, s_cbranch_execz regions with multi-block bodies (optionalBody accepts single-block bodies only), is not part of this PR, so neither title is claimed to be unblocked by this alone.

Tested

Windows 10 Pro 22H2 (19045), WinLibs GCC 15.2.0 r7, Release, on main b5d763770.

  • agc_masked_select, two new cases: an EXEC copy (a ballot word ANDed with the whole-quad expansion of a ballot of true) used as a stencil export guard is replay-safe; the same EXEC copy over a whole-quad expansion of a helper-dependent ballot is still refused. Without the StencilReplay.cpp line the first fails (an exec copy masked by the whole-quad active lanes is a stable export guard); the existing helper-uncertainty cases (helper participation cannot choose a kept lane's stencil through whole-quad expansion, nested active ballots do not erase helper uncertainty before whole-quad expansion) pass unchanged.
  • With ANYPS5_NO_SHADER_CACHE=1: agc_masked_select, agc_driver_stencil_replay, agc_driver_stencil_replay_no_gpl, agc_driver_stencil_ref_export, agc_driver_stencil_ref_export_unused, agc_driver_sample_mask_depth_stencil and agc_driver_stencil_null_feedback pass on both GPUs below, except agc_driver_stencil_ref_export skipped on the RTX 3080 (no VK_EXT_shader_stencil_export) and agc_driver_stencil_replay skipped on the Radeon (it has the extension; with ANYPS5_REQUIRE_VULKAN=1 that skip is reported as a failure).
  • Not run: the Khronos validation layer (not installed on this host), the titles above.
  • tools/check_conventions.py --base origin/main and git diff --check pass. tools/pr_overlap.py was not run (no gh on this host).

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 (NotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file
  • No notes, investigation .md files or images added to the repository (attach them to this PR)
  • New third-party code is a submodule built from source
  • Generic behaviour, not specific to one title
  • Depends on: none
  • Fixes: none
  • Unblocks: none (PPSA24191 and PPSA20397 also need the multi-block body gap; #4276)
  • GPUs tested: NVIDIA RTX 3080, 617.42, Windows 10 22H2; AMD Radeon RDNA 2 iGPU (1002:164e), 32.0.21045.5002, Windows 10 22H2
  • AI-assisted: yes

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4949658601 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9017). Originally posted by [TheSovietWaffle](https://github.com/TheSovietWaffle) on 2026-10-21T09:50:57Z. --- ### What On devices without `VK_EXT_shader_stencil_export`, a stencil-exporting pixel shader is replayed only if `MaskReplaySafety` proves its own-lane masks stable. Since #6291 a pixel shader's EXEC copy (`s_mov_b64 s[..], exec` and the restores built from it) is read as the saved word ANDed with `WQM(Ballot(true))`. The proof accepts a whole-quad (`WqmU64`) expansion only of a ballot whose helper lanes it can classify (`HelperMask::RealOnly` or `QuadStable`), and a ballot of `true` was `Uncertain` because helper lanes vote true too. So any shader whose own-lane mask plumbing goes through an EXEC copy was refused with "own-lane mask not stable". Reported on #8145 for Monster Hunter Wilds (PPSA24191, by @Lindamea1) and Assassin's Creed Shadows (PPSA20397, by @akkadbakkad89), both at the `s_mov_b64 exec, s[..]` restore. Every lane of a quad runs (covered pixels and helpers), so a ballot of `true` has the same bits within each quad as the all-ones mask, which `helpers()` already classifies as `QuadStable`. A ballot of an immediate `true` is now classified the same way. Ballots of anything else are unchanged, so helper-dependent predicates stay `Uncertain` and their whole-quad expansions stay refused. The stencil reference export replay entry in `docs/dev/debt/libSceAgcDriver.md` says so. This removes one of the gaps those reports list. The other one they name, `s_cbranch_execz` regions with multi-block bodies (`optionalBody` accepts single-block bodies only), is not part of this PR, so neither title is claimed to be unblocked by this alone. ### Tested Windows 10 Pro 22H2 (19045), WinLibs GCC 15.2.0 r7, Release, on main `b5d763770`. - `agc_masked_select`, two new cases: an EXEC copy (a ballot word ANDed with the whole-quad expansion of a ballot of `true`) used as a stencil export guard is replay-safe; the same EXEC copy over a whole-quad expansion of a helper-dependent ballot is still refused. Without the `StencilReplay.cpp` line the first fails (`an exec copy masked by the whole-quad active lanes is a stable export guard`); the existing helper-uncertainty cases (`helper participation cannot choose a kept lane's stencil through whole-quad expansion`, `nested active ballots do not erase helper uncertainty before whole-quad expansion`) pass unchanged. - With `ANYPS5_NO_SHADER_CACHE=1`: `agc_masked_select`, `agc_driver_stencil_replay`, `agc_driver_stencil_replay_no_gpl`, `agc_driver_stencil_ref_export`, `agc_driver_stencil_ref_export_unused`, `agc_driver_sample_mask_depth_stencil` and `agc_driver_stencil_null_feedback` pass on both GPUs below, except `agc_driver_stencil_ref_export` skipped on the RTX 3080 (no `VK_EXT_shader_stencil_export`) and `agc_driver_stencil_replay` skipped on the Radeon (it has the extension; with `ANYPS5_REQUIRE_VULKAN=1` that skip is reported as a failure). - Not run: the Khronos validation layer (not installed on this host), the titles above. - `tools/check_conventions.py --base origin/main` and `git diff --check` pass. `tools/pr_overlap.py` was not run (no `gh` on this host). ### Checklist <!-- Rules behind each item: https://github.com/boykopovar/AnyPS5/blob/main/CONTRIBUTING.md --> - [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](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/CONVENTIONS.md) - [x] Unimplemented paths throw (`NotImplemented_nid_no_patch`); silent stubs are listed under Silent stubs in their library's [technical debt](https://github.com/boykopovar/AnyPS5/tree/main/docs/dev/debt) file - [x] No notes, investigation `.md` files or images added to the repository (attach them to this PR) - [x] New third-party code is a submodule built from source - [x] Generic behaviour, not specific to one title - [x] Depends on: none - [x] Fixes: none - [x] Unblocks: none (PPSA24191 and PPSA20397 also need the multi-block body gap; #4276) - [x] GPUs tested: NVIDIA RTX 3080, 617.42, Windows 10 22H2; AMD Radeon RDNA 2 iGPU (`1002:164e`), 32.0.21045.5002, Windows 10 22H2 - [x] AI-assisted: yes 🤖 Generated with [Claude Code](https://claude.com/claude-code)
xXPBSXx commented 2026-10-21 15:14:11 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by xXPBSXx on 2026-10-21T15:14:11Z.


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 main is clean. I built only agc_masked_select_tests; ctest -N -R agc_masked_select lists exactly one test.

PR head tested: 027033f1c81812196e260fb8167807b5203b5ea6.

Results (3 runs each):

  • PR head: agc_masked_select passes 3/3.
  • Baseline: I kept the PR's test and replaced only core/shader/recompiler/StencilReplay.cpp with the version from the PR's merge base (b5d763770a), then rebuilt. The test fails 3/3 (ctest reports Failed). I restored the PR's files afterwards.

GPU: the run output has no Selected GPU line; 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).

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6319462519 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9017#issuecomment-6319462519). Originally posted by [xXPBSXx](https://github.com/xXPBSXx) on 2026-10-21T15:14:11Z. --- 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 `main` is clean. I built only `agc_masked_select_tests`; `ctest -N -R agc_masked_select` lists exactly one test. **PR head tested:** `027033f1c81812196e260fb8167807b5203b5ea6`. **Results (3 runs each):** - PR head: `agc_masked_select` passes 3/3. - Baseline: I kept the PR's test and replaced only `core/shader/recompiler/StencilReplay.cpp` with the version from the PR's merge base (`b5d763770a`), then rebuilt. The test fails 3/3 (ctest reports Failed). I restored the PR's files afterwards. **GPU:** the run output has no `Selected GPU` line; 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).
oneandonlydean commented 2026-10-21 17:43:27 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-21T17:43:27Z.


@TheSovietWaffle thanks for this. We tested it together with #9030 on a wave64 pixel shader from PPSA28183 that exports stencil. The shader still refuses in maskStable, in the own-mask loop of MaskReplaySafety::Check(), for a reason next to the one this PR fixes.

The EXEC copy there is translated once per 32-bit half. Each half of the saved mask is ballot(cond).half & extract(WqmU64(CompositeConstructU64(Ballot(true).x, Ballot(true).y)), half). But the low half and the high half use two separate WqmU64 nodes, built from two separate Ballot(true) in the same block. The WQM branch of maskStable (and the one in helpers()) needs mask.high->Argument(0) to be the same node as the low half's wqm, so no pairing is ever accepted. The shader has six such pairs.

In a local experiment, we treated two WqmU64(CompositeConstructU64(Ballot(imm).x, Ballot(imm).y)) with the same immediate in the same block as one mask, restricting the second tree too. All six pairs then pass. Reading EXEC once per 64-bit operand in the front end would also fix it. Would you like to cover this here, or should it be a separate PR?

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6322588276 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9017#issuecomment-6322588276). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-10-21T17:43:27Z. --- @TheSovietWaffle thanks for this. We tested it together with #9030 on a wave64 pixel shader from PPSA28183 that exports stencil. The shader still refuses in `maskStable`, in the own-mask loop of `MaskReplaySafety::Check()`, for a reason next to the one this PR fixes. The EXEC copy there is translated once per 32-bit half. Each half of the saved mask is `ballot(cond).half & extract(WqmU64(CompositeConstructU64(Ballot(true).x, Ballot(true).y)), half)`. But the low half and the high half use two separate `WqmU64` nodes, built from two separate `Ballot(true)` in the same block. The WQM branch of `maskStable` (and the one in `helpers()`) needs `mask.high->Argument(0)` to be the same node as the low half's `wqm`, so no pairing is ever accepted. The shader has six such pairs. In a local experiment, we treated two `WqmU64(CompositeConstructU64(Ballot(imm).x, Ballot(imm).y))` with the same immediate in the same block as one mask, restricting the second tree too. All six pairs then pass. Reading EXEC once per 64-bit operand in the front end would also fix it. Would you like to cover this here, or should it be a separate PR?
akkadbakkad89 commented 2026-10-21 18:25:47 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by akkadbakkad89 on 2026-10-21T18:25:47Z.


@oneandonlydean the shape you describe — one EXEC copy translated per 32-bit half, giving two same-block WqmU64(CompositeConstructU64(Ballot(true).x, Ballot(true).y)) nodes that fail paired on node identity — is exactly what open PR #9266 implements.

Its equivalentWqm() pairs two WqmU64s whose source trees match opcode-for-opcode (equal immediates count as equal), requiring any Ballot pair to sit in the same block with no execution-mask-affecting instruction (SetExec*, Discard, Sendmsg, SetThreadBitScalarRegister, SetScalarMaskTag) between them — the same "same immediate, same block, restrict the second tree" rule you tried. Both the maskStable() and helpers() WQM branches route through it. We hit the identical gate on PPSA20397 (0x407325e400, six same-block pairs, paired=0 on object identity) and verified paired=1 afterwards.

So a separate PR would duplicate #9266 — the cleaner path is testing it on your PPSA28183 shader. If it covers your six pairs, a datapoint on #9266 would help it land; if it doesn't, the gap would be good to know either way.

(Full chain for completeness: #9266 pairing -> this PR's provenance -> #9030 region bodies -> #9317 for the remaining quadObserved accounting of proven branch conditions and own-lane ballots.)

Generated with Devin

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6323428080 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9017#issuecomment-6323428080). Originally posted by [akkadbakkad89](https://github.com/akkadbakkad89) on 2026-10-21T18:25:47Z. --- @oneandonlydean the shape you describe — one EXEC copy translated per 32-bit half, giving two same-block `WqmU64(CompositeConstructU64(Ballot(true).x, Ballot(true).y))` nodes that fail `paired` on node identity — is exactly what open PR #9266 implements. Its `equivalentWqm()` pairs two `WqmU64`s whose source trees match opcode-for-opcode (equal immediates count as equal), requiring any `Ballot` pair to sit in the same block with no execution-mask-affecting instruction (`SetExec*`, `Discard`, `Sendmsg`, `SetThreadBitScalarRegister`, `SetScalarMaskTag`) between them — the same "same immediate, same block, restrict the second tree" rule you tried. Both the `maskStable()` and `helpers()` WQM branches route through it. We hit the identical gate on PPSA20397 (`0x407325e400`, six same-block pairs, `paired=0` on object identity) and verified `paired=1` afterwards. So a separate PR would duplicate #9266 — the cleaner path is testing it on your PPSA28183 shader. If it covers your six pairs, a datapoint on #9266 would help it land; if it doesn't, the gap would be good to know either way. (Full chain for completeness: #9266 pairing -> this PR's provenance -> #9030 region bodies -> #9317 for the remaining `quadObserved` accounting of proven branch conditions and own-lane ballots.) Generated with [Devin](https://devin.ai)
oneandonlydean commented 2026-10-22 02:31:15 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-22T02:31:15Z.


@akkadbakkad89 thanks. I replayed the logged request offline (agc_shader_replay --prepare, one request) with #9266 at 9b578d5dc7:

  • On top of #9017 + #9030, #9266 clears the maskStable refusal: the per-half whole-quad EXEC copies now pair as one mask, as you described.
  • On its own, #9266 still stops at maskStable for this shader, because it needs #9017's EXEC-copy acceptance underneath.
  • With all three, the next refusal is in control() → optionalBody (quadObserved on the optional body's merge phi). That's a question about #9030's rule, which I've asked there, not something in #9266.
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6331252973 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9017#issuecomment-6331252973). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-10-22T02:31:15Z. --- @akkadbakkad89 thanks. I replayed the logged request offline (`agc_shader_replay --prepare`, one request) with #9266 at 9b578d5dc7: - On top of #9017 + #9030, #9266 clears the `maskStable` refusal: the per-half whole-quad EXEC copies now pair as one mask, as you described. - On its own, #9266 still stops at `maskStable` for this shader, because it needs #9017's EXEC-copy acceptance underneath. - With all three, the next refusal is in `control()` → `optionalBody` (`quadObserved` on the optional body's merge phi). That's a question about #9030's rule, which I've asked there, not something in #9266.
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!9017
No description provided.