fix(recompiler): accept optional-body merges of ballot words of the skipped lanes' mask bits #9905

Merged
TheSovietWaffle merged 2 commits from upstream-pr/9905 into main 2026-11-02 07:59:24 +00:00
TheSovietWaffle commented 2026-10-22 20:59:24 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by TheSovietWaffle on 2026-10-22T20:59:24Z.


What

On devices without VK_EXT_shader_stencil_export, MaskReplaySafety::optionalBody accepts an EXEC- or VCC-skipped body when every merge phi's value from the body equals the skipped value on lanes inactive in the body (inactiveEquals, inactive meaning the skip predicate P is false).

A discard keeps its live mask in scalar words S (one per wave half). Inside a skipped body it removes the killed lanes with S' = Ballot(own(S) && !P), and the merge phi takes S when the body is skipped. The words are wave-wide, but every user reads them lane by lane through own-bit masks (maskStable and own() follow the phi), and on a lane inactive in the body its bit of Ballot(own(S) && !P) is its bit of S. So the two incoming values agree on every inactive lane's bit.

inactiveEquals now accepts a body value that is word h of Ballot(own(M) && !P) (either operand order), or of Ballot(own(M)), when the skipped value is word h of M. Since #11699 rewrites a shader's rebuilt lane bit into the HelperInvocation == 0 predicate L, the same discard reads Ballot(L && !P), and its skipped value is Ballot(L), possibly ANDed with WQM(Ballot(true)), so the rule also accepts a body ballot of L && !P (or L) against a skipped word of Ballot(L), alone or ANDed with a word that covers every lane: a lane that reaches the merge passed that ballot, so its bit is L. Any other ballot argument is refused as before; in particular Ballot(own(M) && Q) for another Q can clear an inactive lane's bit.

Onimusha: Way of the Sword (PPSA27836) needs this: its stencil export mask is the live-mask words after such a discard.

This branch is rebased onto main c3c45d73a. The first commit is the original rule; the second, 64e52b631, follows #11699's rewrite.

Tested

  • Rebased 2026-10-31: on this head 64e52b631 and on main c3c45d73a, with ANYPS5_REQUIRE_VULKAN=1, ANYPS5_NO_SHADER_CACHE=1 and VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation (nightly 2026-10-22), on the RTX 3080 and the Radeon iGPU: agc_masked_select, agc_driver_sample_mask_depth_stencil, agc_driver_stencil_known_replace, agc_driver_stencil_null_feedback, agc_driver_stencil_ref_export_unused, agc_driver_stencil_replay and agc_driver_stencil_replay_no_gpl pass on both trees and both GPUs; agc_driver_stencil_ref_export passes on the Radeon and skips on the RTX 3080 (no VK_EXT_shader_stencil_export). Neither tree's logs have a VUID- line or was not found but was requested by env var VK_INSTANCE_LAYERS. tools/check_conventions.py --base origin/main passes.

  • agc_masked_select, four new cases: a body ballot of L && !P merged with Ballot(L) and with Ballot(L) & Ballot(true) is replay-safe; Ballot(L && P) and a ballot of other lanes are refused. Without the second commit only the two accepted cases fail.

  • Onimusha's shader_2111c51800 on c3c45d73a + #9904 + this PR: refused without the second commit, prepared: 20050 SPIR-V words with it. Lindamea1's PPSA24191 requests are not replayed here.

  • The results below were taken before the rebase.
    Windows 10 Pro 22H2 (19045), WinLibs GCC 15.2.0 r7, Release, on #9393's head 5844ac98e (on #9317's 92ebac241).

  • agc_masked_select, two new cases on a single-block EXEC-skipped body whose merge phis take the live-mask words S (a ballot of the non-helper lanes) when skipped and Ballot(own(S) && !P) from the body, read through an own-bit export guard: replay-safe; the same with Ballot(own(S) && P), which clears inactive lanes' bits, is refused. With #9393's StencilReplay.cpp the first fails (an optional wave body may merge a ballot of the skipped lanes' own mask bits with that mask); with this change both pass.

  • The Onimusha request (shader_2111c51800, dumped with APS5_DUMP_SHADERS) checked offline with PrepareResourceProgram + StencilReplaySafe: on #9317 + #8145 + #9393 + this change + #9904 (the optional-body exit change) + the ballot exemption asked about on #9317, the request passes; without this change the export mask's merge after the discard is refused.

  • With ANYPS5_REQUIRE_VULKAN=1 and ANYPS5_NO_SHADER_CACHE=1, on #9393 and on this change: agc_masked_select, agc_driver_sample_mask_depth_stencil, agc_driver_stencil_known_replace, agc_driver_stencil_null_feedback, agc_driver_stencil_ref_export, agc_driver_stencil_ref_export_unused, agc_driver_stencil_replay and agc_driver_stencil_replay_no_gpl pass on both GPUs below, except agc_driver_stencil_ref_export skipped on the RTX 3080 (no VK_EXT_shader_stencil_export). Results are identical on #9393 and on this change.

  • Not run: a title.

  • tools/check_conventions.py --base origin/main passes with no errors (its one warning, debt-length on docs/dev/debt/libSceAgcDriver.md, is in #9317's entry, which this PR does not touch) and git diff --check passes. 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 (PPSA27836 #5769 needs this plus #9904 and the ballot exemption asked about on #9317)
  • 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:4974042782 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9905). Originally posted by [TheSovietWaffle](https://github.com/TheSovietWaffle) on 2026-10-22T20:59:24Z. --- ### What On devices without `VK_EXT_shader_stencil_export`, `MaskReplaySafety::optionalBody` accepts an EXEC- or VCC-skipped body when every merge phi's value from the body equals the skipped value on lanes inactive in the body (`inactiveEquals`, inactive meaning the skip predicate `P` is false). A `discard` keeps its live mask in scalar words `S` (one per wave half). Inside a skipped body it removes the killed lanes with `S' = Ballot(own(S) && !P)`, and the merge phi takes `S` when the body is skipped. The words are wave-wide, but every user reads them lane by lane through own-bit masks (`maskStable` and `own()` follow the phi), and on a lane inactive in the body its bit of `Ballot(own(S) && !P)` is its bit of `S`. So the two incoming values agree on every inactive lane's bit. `inactiveEquals` now accepts a body value that is word `h` of `Ballot(own(M) && !P)` (either operand order), or of `Ballot(own(M))`, when the skipped value is word `h` of `M`. Since #11699 rewrites a shader's rebuilt lane bit into the `HelperInvocation == 0` predicate `L`, the same discard reads `Ballot(L && !P)`, and its skipped value is `Ballot(L)`, possibly ANDed with `WQM(Ballot(true))`, so the rule also accepts a body ballot of `L && !P` (or `L`) against a skipped word of `Ballot(L)`, alone or ANDed with a word that covers every lane: a lane that reaches the merge passed that ballot, so its bit is `L`. Any other ballot argument is refused as before; in particular `Ballot(own(M) && Q)` for another `Q` can clear an inactive lane's bit. Onimusha: Way of the Sword (PPSA27836) needs this: its stencil export mask is the live-mask words after such a discard. This branch is rebased onto `main` `c3c45d73a`. The first commit is the original rule; the second, `64e52b631`, follows #11699's rewrite. ### Tested - Rebased 2026-10-31: on this head `64e52b631` and on `main` `c3c45d73a`, with `ANYPS5_REQUIRE_VULKAN=1`, `ANYPS5_NO_SHADER_CACHE=1` and `VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation` (nightly 2026-10-22), on the RTX 3080 and the Radeon iGPU: `agc_masked_select`, `agc_driver_sample_mask_depth_stencil`, `agc_driver_stencil_known_replace`, `agc_driver_stencil_null_feedback`, `agc_driver_stencil_ref_export_unused`, `agc_driver_stencil_replay` and `agc_driver_stencil_replay_no_gpl` pass on both trees and both GPUs; `agc_driver_stencil_ref_export` passes on the Radeon and skips on the RTX 3080 (no `VK_EXT_shader_stencil_export`). Neither tree's logs have a `VUID-` line or `was not found but was requested by env var VK_INSTANCE_LAYERS`. `tools/check_conventions.py --base origin/main` passes. - `agc_masked_select`, four new cases: a body ballot of `L && !P` merged with `Ballot(L)` and with `Ballot(L) & Ballot(true)` is replay-safe; `Ballot(L && P)` and a ballot of other lanes are refused. Without the second commit only the two accepted cases fail. - Onimusha's `shader_2111c51800` on `c3c45d73a` + #9904 + this PR: refused without the second commit, `prepared: 20050 SPIR-V words` with it. Lindamea1's PPSA24191 requests are not replayed here. - The results below were taken before the rebase. Windows 10 Pro 22H2 (19045), WinLibs GCC 15.2.0 r7, Release, on #9393's head `5844ac98e` (on #9317's `92ebac241`). - `agc_masked_select`, two new cases on a single-block EXEC-skipped body whose merge phis take the live-mask words `S` (a ballot of the non-helper lanes) when skipped and `Ballot(own(S) && !P)` from the body, read through an own-bit export guard: replay-safe; the same with `Ballot(own(S) && P)`, which clears inactive lanes' bits, is refused. With #9393's `StencilReplay.cpp` the first fails (`an optional wave body may merge a ballot of the skipped lanes' own mask bits with that mask`); with this change both pass. - The Onimusha request (`shader_2111c51800`, dumped with `APS5_DUMP_SHADERS`) checked offline with `PrepareResourceProgram` + `StencilReplaySafe`: on #9317 + #8145 + #9393 + this change + #9904 (the optional-body exit change) + the ballot exemption asked about on #9317, the request passes; without this change the export mask's merge after the discard is refused. - With `ANYPS5_REQUIRE_VULKAN=1` and `ANYPS5_NO_SHADER_CACHE=1`, on #9393 and on this change: `agc_masked_select`, `agc_driver_sample_mask_depth_stencil`, `agc_driver_stencil_known_replace`, `agc_driver_stencil_null_feedback`, `agc_driver_stencil_ref_export`, `agc_driver_stencil_ref_export_unused`, `agc_driver_stencil_replay` and `agc_driver_stencil_replay_no_gpl` pass on both GPUs below, except `agc_driver_stencil_ref_export` skipped on the RTX 3080 (no `VK_EXT_shader_stencil_export`). Results are identical on #9393 and on this change. - Not run: a title. - `tools/check_conventions.py --base origin/main` passes with no errors (its one warning, `debt-length` on `docs/dev/debt/libSceAgcDriver.md`, is in #9317's entry, which this PR does not touch) and `git diff --check` passes. `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 (PPSA27836 #5769 needs this plus #9904 and the ballot exemption asked about on #9317) - [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)
MarcZX10 commented 2026-10-23 05:50:53 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-23T05:50:53Z.

Review: APPROVED


Reviewed exact head 4be42c510c7795d81528427d9b5811782289112e on the #9317 stack. The change handles skipped-lane own-bit ballot words through merge phis, with tests for the accepted equality case and negative controls. The author reports paired validation-layer runs on NVIDIA and AMD with no new VUIDs; the combined title scenario also uses #9904 and #9906. Current-main diff-check passes; conventions have no errors and only the inherited #9317 debt-length warning. Sync is clean aside from the shared debt file. Preserve this distinct phi proof with #9904/#9906 when refreshing; no duplicate behavior found. I did not rerun C++/GPU tests locally.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5624436440 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9905#pullrequestreview-5624436440). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-23T05:50:53Z. **Review:** APPROVED --- Reviewed exact head `4be42c510c7795d81528427d9b5811782289112e` on the #9317 stack. The change handles skipped-lane own-bit ballot words through merge phis, with tests for the accepted equality case and negative controls. The author reports paired validation-layer runs on NVIDIA and AMD with no new VUIDs; the combined title scenario also uses #9904 and #9906. Current-main diff-check passes; conventions have no errors and only the inherited #9317 debt-length warning. Sync is clean aside from the shared debt file. Preserve this distinct phi proof with #9904/#9906 when refreshing; no duplicate behavior found. I did not rerun C++/GPU tests locally.
MarcZX10 commented 2026-10-29 09:01:04 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-29T09:01:04Z.

Review: CHANGES_REQUESTED


Re-check — changes requested\n\nThe previously reviewed focused cases and the paired validation-layer report are useful, but this branch is still based on an old main snapshot: its head is over 2,600 commits behind current main. A new overlap report also says it could not compare this PR with #6959 in StencilReplay.cpp and MaskedSelect.cpp.\n\nPlease update onto current main, manually inspect the #6959 interactions and keep the distinct optional-body behavior/tests, then rerun the focused recompiler and AGC masked-select tests with the validation layer where applicable. Record any required landing order or dependency. The earlier approval covered the 2026-10-22 head, not this integration point. I did not run these tests locally.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5695154225 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9905#pullrequestreview-5695154225). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-29T09:01:04Z. **Review:** CHANGES_REQUESTED --- ## Re-check — changes requested\n\nThe previously reviewed focused cases and the paired validation-layer report are useful, but this branch is still based on an old main snapshot: its head is over 2,600 commits behind current main. A new overlap report also says it could not compare this PR with #6959 in StencilReplay.cpp and MaskedSelect.cpp.\n\nPlease update onto current main, manually inspect the #6959 interactions and keep the distinct optional-body behavior/tests, then rerun the focused recompiler and AGC masked-select tests with the validation layer where applicable. Record any required landing order or dependency. The earlier approval covered the 2026-10-22 head, not this integration point. I did not run these tests locally.
MarcZX10 commented 2026-10-29 09:44:16 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-29T09:44:16Z.

Review: CHANGES_REQUESTED


Re-check — changes requested for current-main integration

The latest NVIDIA report is useful, but it tests this head merged onto main 52c09d5, not current main aaccd0ba39b7d40fdbf19664575a7e567d8542f7. The current merge preview conflicts in core/libs/prx/libSceAgcDriver/tests/MaskedSelect.cpp, core/shader/recompiler/StencilReplay.cpp, and docs/dev/debt/libSceAgcDriver.md; the overlap scan also could not compare #6959.

Please rebase onto current main, manually preserve the distinct ballot-word merge behavior and tests while resolving #6959's changes, and record the integration/landing order with the companion optional-body change in #9904 if both are intended to land together. Then rerun the focused recompiler and masked-select tests, including the validation-layer check where available, on the exact refreshed head. The current product jobs are skipped, and I did not run these tests locally.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5695688413 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9905#pullrequestreview-5695688413). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-29T09:44:16Z. **Review:** CHANGES_REQUESTED --- # Re-check — changes requested for current-main integration The latest NVIDIA report is useful, but it tests this head merged onto `main` `52c09d5`, not current `main` `aaccd0ba39b7d40fdbf19664575a7e567d8542f7`. The current merge preview conflicts in `core/libs/prx/libSceAgcDriver/tests/MaskedSelect.cpp`, `core/shader/recompiler/StencilReplay.cpp`, and `docs/dev/debt/libSceAgcDriver.md`; the overlap scan also could not compare #6959. Please rebase onto current main, manually preserve the distinct ballot-word merge behavior and tests while resolving #6959's changes, and record the integration/landing order with the companion optional-body change in #9904 if both are intended to land together. Then rerun the focused recompiler and masked-select tests, including the validation-layer check where available, on the exact refreshed head. The current product jobs are skipped, and I did not run these tests locally.
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!9905
No description provided.