fix(recompiler): accept optional bodies that end the wave through an exit that keeps no lane #9904

Merged
TheSovietWaffle merged 1 commit from upstream-pr/9904 into main 2026-11-02 07:59:18 +00:00
TheSovietWaffle commented 2026-10-22 20:58:33 +00:00 (Migrated from github.com)

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


What

On devices without VK_EXT_shader_stencil_export, MaskReplaySafety::optionalBody refused any EXEC- or VCC-skipped region that reaches a return block: its merge reasoning assumes every path through the body reaches the merge.

A discard compiles to exactly that shape. Inside the skipped body the shader removes the killed lanes from its live mask and, when no live lane is left, branches to a block that exports with a false guard and returns (exp null, s_endpgm). Such an exit loses no kept lane:

  • its exports all have a false guard (keepsNothing), so no lane is kept on that path;
  • the branch into it goes through control() like any other conditional branch, so an any-active gate must pass guaranteed() (every lane kept on a path through the gate is in the gate's mask, so the exit is never taken while a kept lane exists), and a uniform branch is the same in the replay.

optionalBody now lets the region contain return blocks that keep no lane (it does not walk past them), and allows their false-guarded exports. A return block that keeps a lane is still refused, as is any other export or Discard in the region.

Onimusha: Way of the Sword (PPSA27836) needs this: its stencil-exporting pixel shader skips a discard's kill-and-exit behind an s_cbranch_execz.

This branch is rebased onto main 570089a80, which now carries #9317 (merged as def1f5dd9, with #9017, #9030 and #9266, so optionalBody takes several blocks); the diff is this PR's own commit.

Tested

  • Rebased 2026-10-26: on this head c15ccc280 (main 570089a80), with ANYPS5_REQUIRE_VULKAN=1 and ANYPS5_NO_SHADER_CACHE=1 on the RTX 3080, 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 and agc_driver_stencil_ref_export skips (no VK_EXT_shader_stencil_export); tools/check_conventions.py --base origin/main passes. The results below were taken on the earlier #9317 base.
    Windows 10 Pro 22H2 (19045), WinLibs GCC 15.2.0 r7, Release, on #9317's head 92ebac241.

  • agc_masked_select, two new cases on an EXEC-skipped body that branches, on a uniform condition, either to the merge or to a return block: an exit whose export has a false guard is replay-safe; an exit whose export keeps lanes is refused. With #9317's StencilReplay.cpp the first fails (an optional wave body may end the wave through an exit that keeps no lane); 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 + the scalar ballot-word merge change (my next PR) + the ballot exemption asked about on #9317, the request passes; without this change it refuses at the optional body around the discard (block 8).

  • With ANYPS5_REQUIRE_VULKAN=1 and ANYPS5_NO_SHADER_CACHE=1, on #9317 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 #9317 and on this change.

  • Not run: the Khronos validation layer (not installed on this host, so VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation has nothing to load; neither log contains a VUID- line), 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 #9393, the scalar ballot-word merge change 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:4974033055 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9904). Originally posted by [TheSovietWaffle](https://github.com/TheSovietWaffle) on 2026-10-22T20:58:33Z. --- ### What On devices without `VK_EXT_shader_stencil_export`, `MaskReplaySafety::optionalBody` refused any EXEC- or VCC-skipped region that reaches a return block: its merge reasoning assumes every path through the body reaches the merge. A `discard` compiles to exactly that shape. Inside the skipped body the shader removes the killed lanes from its live mask and, when no live lane is left, branches to a block that exports with a false guard and returns (`exp null`, `s_endpgm`). Such an exit loses no kept lane: - its exports all have a false guard (`keepsNothing`), so no lane is kept on that path; - the branch into it goes through `control()` like any other conditional branch, so an any-active gate must pass `guaranteed()` (every lane kept on a path through the gate is in the gate's mask, so the exit is never taken while a kept lane exists), and a uniform branch is the same in the replay. `optionalBody` now lets the region contain return blocks that keep no lane (it does not walk past them), and allows their false-guarded exports. A return block that keeps a lane is still refused, as is any other export or `Discard` in the region. Onimusha: Way of the Sword (PPSA27836) needs this: its stencil-exporting pixel shader skips a discard's kill-and-exit behind an `s_cbranch_execz`. This branch is rebased onto `main` `570089a80`, which now carries #9317 (merged as `def1f5dd9`, with #9017, #9030 and #9266, so `optionalBody` takes several blocks); the diff is this PR's own commit. ### Tested - Rebased 2026-10-26: on this head `c15ccc280` (main `570089a80`), with `ANYPS5_REQUIRE_VULKAN=1` and `ANYPS5_NO_SHADER_CACHE=1` on the RTX 3080, `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 and `agc_driver_stencil_ref_export` skips (no `VK_EXT_shader_stencil_export`); `tools/check_conventions.py --base origin/main` passes. The results below were taken on the earlier #9317 base. Windows 10 Pro 22H2 (19045), WinLibs GCC 15.2.0 r7, Release, on #9317's head `92ebac241`. - `agc_masked_select`, two new cases on an EXEC-skipped body that branches, on a uniform condition, either to the merge or to a return block: an exit whose export has a false guard is replay-safe; an exit whose export keeps lanes is refused. With #9317's `StencilReplay.cpp` the first fails (`an optional wave body may end the wave through an exit that keeps no lane`); 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 + the scalar ballot-word merge change (my next PR) + the ballot exemption asked about on #9317, the request passes; without this change it refuses at the optional body around the discard (block 8). - With `ANYPS5_REQUIRE_VULKAN=1` and `ANYPS5_NO_SHADER_CACHE=1`, on #9317 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 #9317 and on this change. - Not run: the Khronos validation layer (not installed on this host, so `VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation` has nothing to load; neither log contains a `VUID-` line), 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 #9393, the scalar ballot-word merge change 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:51 +00:00 (Migrated from github.com)

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

Review: APPROVED


Reviewed exact head ff0d9169ec2832b5fb5f4298c3edca8e61a35ad5 on the declared #9317 stack. The optional region is bounded to 64 blocks, checks single-entry predecessors and merge-phi values, rejects returning paths that keep lanes, and allows only false-guarded exports in no-lane return blocks; tests include positive and kept-lane negative paths. The author reports Vulkan validation-layer A/B on NVIDIA and AMD, with no added VUIDs, and title-request replay evidence as part of the full stencil stack. Current-main conventions pass with the inherited debt-length warning; sync is clean, with only the shared debt file changed since base. Preserve #9317 and the complementary #9905/#9906 changes on the final refreshed stack. I did not run the C++/GPU tests locally.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5624436274 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9904#pullrequestreview-5624436274). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-23T05:50:51Z. **Review:** APPROVED --- Reviewed exact head `ff0d9169ec2832b5fb5f4298c3edca8e61a35ad5` on the declared #9317 stack. The optional region is bounded to 64 blocks, checks single-entry predecessors and merge-phi values, rejects returning paths that keep lanes, and allows only false-guarded exports in no-lane return blocks; tests include positive and kept-lane negative paths. The author reports Vulkan validation-layer A/B on NVIDIA and AMD, with no added VUIDs, and title-request replay evidence as part of the full stencil stack. Current-main conventions pass with the inherited debt-length warning; sync is clean, with only the shared debt file changed since base. Preserve #9317 and the complementary #9905/#9906 changes on the final refreshed stack. I did not run the C++/GPU tests locally.
MarcZX10 commented 2026-10-29 09:01:06 +00:00 (Migrated from github.com)

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

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:5695154608 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9904#pullrequestreview-5695154608). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-29T09:01:06Z. **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.
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!9904
No description provided.