fix(recompiler): accept optional wave bodies of several blocks in stencil replay #9030

Closed
TheSovietWaffle wants to merge 1 commit from upstream-pr/9030 into main
TheSovietWaffle commented 2026-10-21 10:06:36 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by TheSovietWaffle on 2026-10-21T10:06:36Z.


What

On devices without VK_EXT_shader_stencil_export, MaskReplaySafety::control accepts a non-uniform s_cbranch_execz/vccz (or execnz/vccnz) branch when the skipped code is an optional body (optionalBody): inactive lanes see the same values whether or not the wave runs it. optionalBody only accepted a body that is one block branching straight to the merge. A body with its own branches was refused, which is the shape Onimusha: Way of the Sword (PPSA27836, its stencil-exporting pixel shader skips blocks 2 to 8 behind one s_cbranch_execz, see my note on #7006) and Monster Hunter Wilds (PPSA24191, reported by @Lindamea1 on #8145) hit after their other refusals.

optionalBody now takes the region of blocks reached from the body before the merge, and accepts it when:

  • it is single-entry: the body is entered only from the branch, every other block only from the region;
  • no block in it returns (every path reaches the merge), and the walk stops at 64 blocks;
  • nothing in it is exported (SetAttribute) or discarded, and its values are used only inside the region or by merge phis;
  • every merge phi's incoming values from the region equal the value from the branch on inactive lanes (inactiveEquals), which now also follows the region's own phis (each incoming value of such a phi must satisfy it).

The single-block case is the same as before. Every branch inside the region still goes through control() like any other branch, and the merge phis are still checked with quadObserved.

Together with #9017 (EXEC copies), this covers the two gaps @Lindamea1 listed for Monster Hunter Wilds. Onimusha still needs a third one (a lane's own bit of a ballot of the predicate, at its EXEC restore), so no title is claimed to be unblocked.

Tested

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

  • agc_masked_select, two new cases: an EXEC-skipped body made of a uniform branch into two arms that rejoin before the merge, each writing select(predicate, value, old), is replay-safe; the same body with one arm writing an unmasked constant is refused. On main the first fails (an optional wave body spanning several blocks preserves inactive values through its own phis), and with only the region walk (without inactiveEquals following region phis) it still fails. The existing single-block cases (an optional wave body preserves inactive values consumed only within a lane and the two derivative-consumer refusals) 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).
  • Not run: the Khronos validation layer (not installed on this host), a title (the retained Onimusha request predates the current request format).
  • tools/check_conventions.py --base origin/main and git diff --check pass. tools/pr_overlap.py was not run (no gh on this host); #9017 touches the same file in helpers(), not here.

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 with #9017 per the gaps reported on #8145; PPSA27836 needs one more; #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:4949849356 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9030). Originally posted by [TheSovietWaffle](https://github.com/TheSovietWaffle) on 2026-10-21T10:06:36Z. --- ### What On devices without `VK_EXT_shader_stencil_export`, `MaskReplaySafety::control` accepts a non-uniform `s_cbranch_execz`/`vccz` (or `execnz`/`vccnz`) branch when the skipped code is an optional body (`optionalBody`): inactive lanes see the same values whether or not the wave runs it. `optionalBody` only accepted a body that is one block branching straight to the merge. A body with its own branches was refused, which is the shape Onimusha: Way of the Sword (PPSA27836, its stencil-exporting pixel shader skips blocks 2 to 8 behind one `s_cbranch_execz`, see my note on #7006) and Monster Hunter Wilds (PPSA24191, reported by @Lindamea1 on #8145) hit after their other refusals. `optionalBody` now takes the region of blocks reached from the body before the merge, and accepts it when: - it is single-entry: the body is entered only from the branch, every other block only from the region; - no block in it returns (every path reaches the merge), and the walk stops at 64 blocks; - nothing in it is exported (`SetAttribute`) or discarded, and its values are used only inside the region or by merge phis; - every merge phi's incoming values from the region equal the value from the branch on inactive lanes (`inactiveEquals`), which now also follows the region's own phis (each incoming value of such a phi must satisfy it). The single-block case is the same as before. Every branch inside the region still goes through `control()` like any other branch, and the merge phis are still checked with `quadObserved`. Together with #9017 (EXEC copies), this covers the two gaps @Lindamea1 listed for Monster Hunter Wilds. Onimusha still needs a third one (a lane's own bit of a ballot of the predicate, at its EXEC restore), so no title is claimed to be unblocked. ### Tested Windows 10 Pro 22H2 (19045), WinLibs GCC 15.2.0 r7, Release, on main `b5d763770`. - `agc_masked_select`, two new cases: an EXEC-skipped body made of a uniform branch into two arms that rejoin before the merge, each writing `select(predicate, value, old)`, is replay-safe; the same body with one arm writing an unmasked constant is refused. On main the first fails (`an optional wave body spanning several blocks preserves inactive values through its own phis`), and with only the region walk (without `inactiveEquals` following region phis) it still fails. The existing single-block cases (`an optional wave body preserves inactive values consumed only within a lane` and the two derivative-consumer refusals) 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). - Not run: the Khronos validation layer (not installed on this host), a title (the retained Onimusha request predates the current request format). - `tools/check_conventions.py --base origin/main` and `git diff --check` pass. `tools/pr_overlap.py` was not run (no `gh` on this host); #9017 touches the same file in `helpers()`, not here. ### 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 with #9017 per the gaps reported on #8145; PPSA27836 needs one more; #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)
oneandonlydean commented 2026-10-21 17:43:29 +00:00 (Migrated from github.com)

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


@TheSovietWaffle a question from testing this with #9017 on a wave64 pixel shader from PPSA28183 that exports stencil. Once the EXEC copy halves are paired (see #9017), the next refusal is quadObserved(phi) in optionalBody, on a three-block optional body that this PR now accepts. The phi merges a texture sample result with a computed value.

The merged value reaches two places:

  • s_cbranch_execz skip conditions. quadObserved counts every branch condition, while helperObserved exempts the Exec/Vcc kinds.
  • With those exempted in a local experiment, a Ballot of an FP compare that is ANDed with the own-lane bit of a saved & WQM(exec) mask. That's a v_cmp under a WQM mask, so helper lanes take part in the ballot.

Is it intended that this stays refused? Or is it safe for a merged optional-body value to reach a ballot restricted by an own-lane mask whose helper provenance is known? If it's safe, it seems quadObserved would also need the Exec/Vcc branch exemption that helperObserved has.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6322588842 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9030#issuecomment-6322588842). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-10-21T17:43:29Z. --- @TheSovietWaffle a question from testing this with #9017 on a wave64 pixel shader from PPSA28183 that exports stencil. Once the EXEC copy halves are paired (see #9017), the next refusal is `quadObserved(phi)` in `optionalBody`, on a three-block optional body that this PR now accepts. The phi merges a texture sample result with a computed value. The merged value reaches two places: - `s_cbranch_execz` skip conditions. `quadObserved` counts every branch condition, while `helperObserved` exempts the Exec/Vcc kinds. - With those exempted in a local experiment, a `Ballot` of an FP compare that is ANDed with the own-lane bit of a `saved & WQM(exec)` mask. That's a `v_cmp` under a WQM mask, so helper lanes take part in the ballot. Is it intended that this stays refused? Or is it safe for a merged optional-body value to reach a ballot restricted by an own-lane mask whose helper provenance is known? If it's safe, it seems `quadObserved` would also need the Exec/Vcc branch exemption that `helperObserved` has.
oneandonlydean commented 2026-10-22 02:31:13 +00:00 (Migrated from github.com)

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


@TheSovietWaffle I have a question about how #9030's optional bodies meet quadObserved, from replaying one logged stencil-replay request of PPSA28183 offline (agc_shader_replay --prepare on a build with #9017 and #9030, then with #9266 added on top).

With #9017 + #9030 the request refuses at the maskStable check. #9266 clears that, and it now refuses in control(), through optionalBody, at:

if (!skipped || taken.empty() || quadObserved(instruction)) return false;

The optional body has three blocks, which #9030 accepts. Its merge phi combines a texture-sample result with a computed value, and quadObserved returns true for that phi in two ways:

  1. The phi reaches the conditions of ExecZero / ExecNonZero skip branches. quadObserved counts every branch condition, while helperObserved exempts the Exec and Vcc kinds.
  2. With (1) exempted in a scratch build, it still reaches a Ballot of FPUnordGreaterThan(...) && ownLane(saved & WQM(exec)): a v_cmp result restricted to a whole-quad mask. Since that mask includes helper lanes, the ballot can see the phi's value in helper lanes.

My questions:

  • Should the Exec/Vcc branch kinds be exempt in quadObserved too, as they are in helperObserved? A branch on whether any lane is live doesn't seem to expose a per-lane value to me, but you know why the two checks differ.
  • May a merged optional-body value reach a ballot under a whole-quad mask? That is where I'm not sure. If the replay computes the skipped side for helper lanes differently from the hardware, the ballot result can differ. If helper lanes take the same path as on hardware, it looks safe. I'd keep refusing it unless you can show the helper lanes match.

I didn't relax the ballot rule, so I don't know what refuses after it.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6331252575 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9030#issuecomment-6331252575). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-10-22T02:31:13Z. --- @TheSovietWaffle I have a question about how #9030's optional bodies meet `quadObserved`, from replaying one logged stencil-replay request of PPSA28183 offline (`agc_shader_replay --prepare` on a build with #9017 and #9030, then with #9266 added on top). With #9017 + #9030 the request refuses at the `maskStable` check. #9266 clears that, and it now refuses in `control()`, through `optionalBody`, at: ``` if (!skipped || taken.empty() || quadObserved(instruction)) return false; ``` The optional body has three blocks, which #9030 accepts. Its merge phi combines a texture-sample result with a computed value, and `quadObserved` returns true for that phi in two ways: 1. The phi reaches the conditions of ExecZero / ExecNonZero skip branches. `quadObserved` counts every branch condition, while `helperObserved` exempts the Exec and Vcc kinds. 2. With (1) exempted in a scratch build, it still reaches a `Ballot` of `FPUnordGreaterThan(...) && ownLane(saved & WQM(exec))`: a v_cmp result restricted to a whole-quad mask. Since that mask includes helper lanes, the ballot can see the phi's value in helper lanes. My questions: - Should the Exec/Vcc branch kinds be exempt in `quadObserved` too, as they are in `helperObserved`? A branch on whether any lane is live doesn't seem to expose a per-lane value to me, but you know why the two checks differ. - May a merged optional-body value reach a ballot under a whole-quad mask? That is where I'm not sure. If the replay computes the skipped side for helper lanes differently from the hardware, the ballot result can differ. If helper lanes take the same path as on hardware, it looks safe. I'd keep refusing it unless you can show the helper lanes match. I didn't relax the ballot rule, so I don't know what refuses after it.
akkadbakkad89 commented 2026-10-22 05:05:07 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by akkadbakkad89 on 2026-10-22T05:05:07Z.


@oneandonlydean both of your quadObserved cases are what open PR #9317 implements on this stack (main + #9017 + #9030 + #9266). On PPSA20397's 0x407325e400 request the full stack reaches prepared: 96161 SPIR-V words under agc_shader_replay --prepare, i.e. the verifier clears end to end.

How each of your questions maps:

  1. Exec/Vcc branch conditions: #9317 does not blanket-exempt them. It runs a memoized controlProven() pre-pass over conditional branches (with a visiting set for cycles) and exempts a condition use only when the consuming branch's optional body itself validates under control(). So a merged phi feeding s_cbranch_execz is fine exactly when that branch's region is already replay-proven — the exemption stays conditional on the proof rather than on the branch kind. Your "a branch on whether any lane is live doesn't expose a per-lane value" intuition holds, but only because the region is independently proven; we keep rejecting conditions of unsafe branches.

  2. Ballot of P && ownLane(WQM(exec)): #9317 exempts a Ballot only when every use funnels through the strict own-lane read — CompositeExtract(half) → optional lane-local And mask → Select(lane<32, lo, hi) → ShiftRight(·, lane&31) → And(·,1). Then the ballot is only ever read as ballot[L], so cross-lane information — including what helper lanes computed — cannot escape the funnel; their divergence in P is unobservable. If the ballot result reaches anything else, it's still rejected, which matches your "keep refusing unless you can show the helper lanes match" — with the funnel, they provably can't be observed at all.

Two extra details worth knowing: optionalBody had to keep its region set local so nested control() proofs don't union regions across recursion (in the PR), and explicit-LOD samples (ImageSampleExplicitLod flags) are exempted as lane-local since they don't compute implicit quad derivatives.

Worth replaying your PPSA28183 request on the full stack — if anything still refuses after that, it's a gate this title's shader doesn't exercise and worth a new finding.

AI-assisted: yes (Devin).

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6333179499 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9030#issuecomment-6333179499). Originally posted by [akkadbakkad89](https://github.com/akkadbakkad89) on 2026-10-22T05:05:07Z. --- @oneandonlydean both of your `quadObserved` cases are what open PR #9317 implements on this stack (`main` + #9017 + #9030 + #9266). On PPSA20397's `0x407325e400` request the full stack reaches `prepared: 96161 SPIR-V words` under `agc_shader_replay --prepare`, i.e. the verifier clears end to end. How each of your questions maps: 1. **Exec/Vcc branch conditions**: #9317 does *not* blanket-exempt them. It runs a memoized `controlProven()` pre-pass over conditional branches (with a visiting set for cycles) and exempts a condition *use* only when the consuming branch's optional body itself validates under `control()`. So a merged phi feeding `s_cbranch_execz` is fine exactly when that branch's region is already replay-proven — the exemption stays conditional on the proof rather than on the branch kind. Your "a branch on whether any lane is live doesn't expose a per-lane value" intuition holds, but only because the region is independently proven; we keep rejecting conditions of unsafe branches. 2. **Ballot of `P && ownLane(WQM(exec))`**: #9317 exempts a `Ballot` only when *every* use funnels through the strict own-lane read — `CompositeExtract(half)` → optional lane-local `And` mask → `Select(lane<32, lo, hi)` → `ShiftRight(·, lane&31)` → `And(·,1)`. Then the ballot is only ever read as `ballot[L]`, so cross-lane information — including what helper lanes computed — cannot escape the funnel; their divergence in `P` is unobservable. If the ballot result reaches anything else, it's still rejected, which matches your "keep refusing unless you can show the helper lanes match" — with the funnel, they provably can't be observed at all. Two extra details worth knowing: `optionalBody` had to keep its region set local so nested `control()` proofs don't union regions across recursion (in the PR), and explicit-LOD samples (`ImageSampleExplicitLod` flags) are exempted as lane-local since they don't compute implicit quad derivatives. Worth replaying your PPSA28183 request on the full stack — if anything still refuses after that, it's a gate this title's shader doesn't exercise and worth a new finding. AI-assisted: yes (Devin).
oneandonlydean commented 2026-10-22 05:12:38 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-22T05:12:38Z.


@akkadbakkad89 thanks, that's a clear answer to both questions, and keeping the condition exemption tied to the branch's own proof is what I hoped for. I'll replay our logged request on main + #9017 + #9030 + #9266 + #9317 and report back here, and if it clears, run it in the title.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6333276486 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9030#issuecomment-6333276486). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-10-22T05:12:38Z. --- @akkadbakkad89 thanks, that's a clear answer to both questions, and keeping the condition exemption tied to the branch's own proof is what I hoped for. I'll replay our logged request on main + #9017 + #9030 + #9266 + #9317 and report back here, and if it clears, run it in the title.

Pull request closed

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!9030
No description provided.