fix(shader): preserve read lane dominance across branches #8089

Merged
MarcZX10 merged 5 commits from upstream-pr/8089 into main 2026-11-14 03:28:21 +00:00
MarcZX10 commented 2026-10-19 20:57:20 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-19T20:57:20Z.


What

Fixes #8050. ReadLaneEliminator could replace a spilled ReadLane with a value written only inside a branch, even when that value did not dominate a read after the merge. The rewritten read now follows the value through control-flow phis, using UndefU32 on paths without a store. The direct rewrite remains for stores that dominate the read. The regression covers both cases.

Tested

  • Linux, current upstream/main 9127ea36: built agc_read_lane_elimination_tests. On the test-only commit, ctest -R '^agc_read_lane_elimination$' fails on the branch-local-store assertion. With the implementation commit, it passes (1/1).
  • git diff --check and python3 tools/check_conventions.py --base upstream/main --head HEAD: passed.
  • The overlap scan found no open PR modifying ReadLaneEliminator.cpp or ReadLaneElimination.cpp. It could not compare many PRs that also change the AGC CMakeLists.txt; I manually reviewed those patches and found unrelated source lists and test registrations.
  • The issue report records the same implementation patch preparing the retained Onimusha shader (PPSA27836) and 805/806 AGC tests passing on an NVIDIA RTX 3080 (driver 617.42) and an AMD RDNA 2 iGPU (1002:164e, driver 32.0.21045.5002); each device's remaining test fails on unchanged main. Those runs used base 8c9161eac, an ancestor of current main. The issue report does not state the OS, and I did not rerun GPU tests on this host. Its reported patch has the same patch ID as this implementation.

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: #8050
  • Unblocks: none
  • GPUs tested: issue report only; NVIDIA RTX 3080, driver 617.42, and AMD Radeon RDNA 2 iGPU, driver 32.0.21045.5002; OS not stated. No local GPU test.
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4927327750 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/8089). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-19T20:57:20Z. --- ### What Fixes #8050. `ReadLaneEliminator` could replace a spilled `ReadLane` with a value written only inside a branch, even when that value did not dominate a read after the merge. The rewritten read now follows the value through control-flow phis, using `UndefU32` on paths without a store. The direct rewrite remains for stores that dominate the read. The regression covers both cases. ### Tested - Linux, current `upstream/main` `9127ea36`: built `agc_read_lane_elimination_tests`. On the test-only commit, `ctest -R '^agc_read_lane_elimination$'` fails on the branch-local-store assertion. With the implementation commit, it passes (1/1). - `git diff --check` and `python3 tools/check_conventions.py --base upstream/main --head HEAD`: passed. - The overlap scan found no open PR modifying `ReadLaneEliminator.cpp` or `ReadLaneElimination.cpp`. It could not compare many PRs that also change the AGC `CMakeLists.txt`; I manually reviewed those patches and found unrelated source lists and test registrations. - The issue report records the same implementation patch preparing the retained Onimusha shader (PPSA27836) and 805/806 AGC tests passing on an NVIDIA RTX 3080 (driver 617.42) and an AMD RDNA 2 iGPU (1002:164e, driver 32.0.21045.5002); each device's remaining test fails on unchanged main. Those runs used base `8c9161eac`, an ancestor of current main. The issue report does not state the OS, and I did not rerun GPU tests on this host. Its reported patch has the same patch ID as this implementation. ### Checklist - [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: #8050 - [x] Unblocks: none - [x] GPUs tested: issue report only; NVIDIA RTX 3080, driver 617.42, and AMD Radeon RDNA 2 iGPU, driver 32.0.21045.5002; OS not stated. No local GPU test. - [x] AI-assisted: yes
wachi35676 commented 2026-10-20 12:31:32 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by wachi35676 on 2026-10-20T12:31:32Z.


Windows 11 26300, WinLibs GCC 15.2.0, Release, BUILD_TESTING=ON, main 02fb1ce53 with this PR (45c217d4a) merged. RTX 5070 Ti (617.42) and AMD Radeon iGPU 1002:13c0 (RDNA 2, AMD Windows driver 32.0.21042.62), ANYPS5_REQUIRE_VULKAN=1.

  • agc_read_lane_elimination passes. With ReadLaneEliminator.cpp reverted to main it fails on a lane value stored inside a branch reaches the read after the merge through a phi, and the dominating-store case still passes.
  • ctest -R "agc|shader|spirv|recomp" -j6: RTX 834 tests, only agc_driver_pass_input_hazard fails, which fails on main here too. iGPU 834 tests, only agc_driver_mul_lo_u16_high_half and agc_driver_sgpr_pattern_fill_keys fail, both also on main here. No new failures. (agc_driver_image_array_window_past_layer_limit doesn't compile with MinGW on main (alignas(65536)), so it is Not Run on both sides.)

One case the dominance check still lets through: dominates() returns true when the value and the read are in the same block, without checking their order. A single-block loop where the spill is read before the value is stored in the same iteration, built with this test's Builder:

entry: initial = LaneId; br loop
loop:  slot = phi [entry: initial] [loop: written]
       read = ReadLane slot, 5
       stored = LaneId
       written = WriteLane stored, 5, slot
       br loop / exit

With this PR, Eliminate rewrites the read (rewrittenReads=1) to stored itself, which is instruction 3 of loop while the read is instruction 1. So the read now uses stored before it is defined. The first iteration should read the uninitialized slot (UndefU32) and later iterations the previous iteration's stored, i.e. a phi in loop. main gives the same rewrite, so this is not a regression. It is the same dominance problem in a loop, though. dependsOn only rejects it when stored is computed from the read, as with a spilled loop counter. A fix probably needs At to tell a block's value at its start (for the read) from its value at its end (for a phi operand from a back edge).

AI-assisted: yes (Claude Code); results above were run on this machine.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6290897547 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/8089#issuecomment-6290897547). Originally posted by [wachi35676](https://github.com/wachi35676) on 2026-10-20T12:31:32Z. --- Windows 11 26300, WinLibs GCC 15.2.0, Release, `BUILD_TESTING=ON`, `main` `02fb1ce53` with this PR (`45c217d4a`) merged. RTX 5070 Ti (617.42) and AMD Radeon iGPU `1002:13c0` (RDNA 2, AMD Windows driver 32.0.21042.62), `ANYPS5_REQUIRE_VULKAN=1`. - `agc_read_lane_elimination` passes. With `ReadLaneEliminator.cpp` reverted to `main` it fails on `a lane value stored inside a branch reaches the read after the merge through a phi`, and the dominating-store case still passes. - `ctest -R "agc|shader|spirv|recomp" -j6`: RTX 834 tests, only `agc_driver_pass_input_hazard` fails, which fails on `main` here too. iGPU 834 tests, only `agc_driver_mul_lo_u16_high_half` and `agc_driver_sgpr_pattern_fill_keys` fail, both also on `main` here. No new failures. (`agc_driver_image_array_window_past_layer_limit` doesn't compile with MinGW on `main` (`alignas(65536)`), so it is Not Run on both sides.) One case the dominance check still lets through: `dominates()` returns true when the value and the read are in the same block, without checking their order. A single-block loop where the spill is read before the value is stored in the same iteration, built with this test's `Builder`: ``` entry: initial = LaneId; br loop loop: slot = phi [entry: initial] [loop: written] read = ReadLane slot, 5 stored = LaneId written = WriteLane stored, 5, slot br loop / exit ``` With this PR, `Eliminate` rewrites the read (`rewrittenReads=1`) to `stored` itself, which is instruction 3 of `loop` while the read is instruction 1. So the read now uses `stored` before it is defined. The first iteration should read the uninitialized slot (`UndefU32`) and later iterations the previous iteration's `stored`, i.e. a phi in `loop`. `main` gives the same rewrite, so this is not a regression. It is the same dominance problem in a loop, though. `dependsOn` only rejects it when `stored` is computed from the read, as with a spilled loop counter. A fix probably needs `At` to tell a block's value at its start (for the read) from its value at its end (for a phi operand from a back edge). AI-assisted: yes (Claude Code); results above were run on this machine.
DavidAmsellem commented 2026-10-20 19:00:46 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by DavidAmsellem on 2026-10-20T19:00:46Z.


First Linux run of this PR, on AMD with RADV. Head 45c217d4a merged onto main 2fee39f1b (clean merge), built in full and compared with the same main, on the same machine.

AMD Radeon RX 7900 XT (RADV NAVI31, gfx1100), Mesa 25.2.8, Linux Mint 22.3 (Ubuntu 24.04 base), kernel 7.0.0-38, GCC 13.3, Release.

Full suite (ANYPS5_REQUIRE_VULKAN=1 ctest -j8) at c4d6c686f: 1716/1717 with this PR, against 1715/1716 on main. The one failure is the same on both, so this PR adds none: agc_driver_guest_memory_lifetime (#7784, which #7849 addresses).

Comparing the JUnit output with main: the new entry, agc_read_lane_elimination, runs and passes. No other entry is added, and none is skipped or removed, so the change in read lane dominance doesn't alter any GPU test result here.

GPUs tested: AMD Radeon RX 7900 XT (RADV NAVI31, gfx1100), Mesa 25.2.8, Linux Mint 22.3 / kernel 7.0.0-38

AI-assisted: yes

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6298825253 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/8089#issuecomment-6298825253). Originally posted by [DavidAmsellem](https://github.com/DavidAmsellem) on 2026-10-20T19:00:46Z. --- First Linux run of this PR, on AMD with RADV. Head `45c217d4a` merged onto `main` `2fee39f1b` (clean merge), built in full and compared with the same `main`, on the same machine. AMD Radeon RX 7900 XT (RADV NAVI31, gfx1100), Mesa 25.2.8, Linux Mint 22.3 (Ubuntu 24.04 base), kernel 7.0.0-38, GCC 13.3, Release. Full suite (`ANYPS5_REQUIRE_VULKAN=1 ctest -j8`) at `c4d6c686f`: **1716/1717 with this PR**, against 1715/1716 on `main`. The one failure is the same on both, so this PR adds none: `agc_driver_guest_memory_lifetime` (#7784, which #7849 addresses). Comparing the JUnit output with `main`: the new entry, `agc_read_lane_elimination`, runs and passes. No other entry is added, and none is skipped or removed, so the change in read lane dominance doesn't alter any GPU test result here. GPUs tested: AMD Radeon RX 7900 XT (RADV NAVI31, gfx1100), Mesa 25.2.8, Linux Mint 22.3 / kernel 7.0.0-38 AI-assisted: yes
MarcZX10 commented 2026-10-20 19:22:43 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-20T19:22:43Z.


Follow-up to Wachi35676’s same-block loop example: I pushed 31630fba75 to make the unique-value rewrite point-sensitive within its block. A candidate instruction after the ReadLane no longer counts as available at the read; the rewrite builds a phi from the entry value and the loop back-edge store. The new regression checks the first-iteration UndefU32 input and the back-edge store input.

Validation on this head: tools/check_compile.py compiled both changed translation units; git diff --check and tools/check_conventions.py --base upstream/main pass. I could not execute agc_read_lane_elimination_tests locally: the stale build directory would rebuild 271 prerequisite objects and this filesystem has 21 MiB available, so I stopped that build before it could fill the disk. The runtime assertion result is pending CI or a supported build host.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6299327971 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/8089#issuecomment-6299327971). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-20T19:22:43Z. --- Follow-up to Wachi35676’s same-block loop example: I pushed `31630fba75` to make the unique-value rewrite point-sensitive within its block. A candidate instruction after the `ReadLane` no longer counts as available at the read; the rewrite builds a phi from the entry value and the loop back-edge store. The new regression checks the first-iteration `UndefU32` input and the back-edge store input. Validation on this head: `tools/check_compile.py` compiled both changed translation units; `git diff --check` and `tools/check_conventions.py --base upstream/main` pass. I could not execute `agc_read_lane_elimination_tests` locally: the stale build directory would rebuild 271 prerequisite objects and this filesystem has 21 MiB available, so I stopped that build before it could fill the disk. The runtime assertion result is pending CI or a supported build host.
oneandonlydean commented 2026-10-20 19:30:26 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-20T19:30:26Z.


Thanks for 31630fba75. Making availability point-sensitive is the right fix for the same-block loop case: a store after the ReadLane should no longer count as available at the read, and the back-edge lookup correctly picks up the store as the block's exit value.

One problem with where the new phi goes. At(block, before) now inserts it with InsertInstructionBefore(before, &phi), directly before the ReadLane, and before is passed for every rewrite, not only when the value is defined in the same block. So in the branch-merge case from 45c217d4af, if any non-phi instruction comes before the read in the merge block, the phi ends up after it. The SPIR-V emitter rejects that (SpirvFlowEmitter.cpp, "appears after a non-Phi instruction"), and I didn't find a later pass that moves phis back to the front. The current tests don't hit it: the new loop test's read sits right after the only phi, and neither test emits SPIR-V.

Since the value isn't defined earlier in the block in either case, the value at the read equals the value at block entry, so the phi can always go at the block front (after any existing phis), as before. Could you keep the availability check, place the phi at the front, and add a case where a non-phi instruction precedes the ReadLane, checking that the inserted phi comes before every non-phi (or emitting SPIR-V for it)?

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6299505860 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/8089#issuecomment-6299505860). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-10-20T19:30:26Z. --- Thanks for `31630fba75`. Making availability point-sensitive is the right fix for the same-block loop case: a store after the `ReadLane` should no longer count as available at the read, and the back-edge lookup correctly picks up the store as the block's exit value. One problem with where the new phi goes. `At(block, before)` now inserts it with `InsertInstructionBefore(before, &phi)`, directly before the `ReadLane`, and `before` is passed for every rewrite, not only when the value is defined in the same block. So in the branch-merge case from `45c217d4af`, if any non-phi instruction comes before the read in the merge block, the phi ends up after it. The SPIR-V emitter rejects that (`SpirvFlowEmitter.cpp`, "appears after a non-Phi instruction"), and I didn't find a later pass that moves phis back to the front. The current tests don't hit it: the new loop test's read sits right after the only phi, and neither test emits SPIR-V. Since the value isn't defined earlier in the block in either case, the value at the read equals the value at block entry, so the phi can always go at the block front (after any existing phis), as before. Could you keep the availability check, place the phi at the front, and add a case where a non-phi instruction precedes the `ReadLane`, checking that the inserted phi comes before every non-phi (or emitting SPIR-V for it)?
MarcZX10 commented 2026-10-20 19:48:58 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-20T19:48:58Z.


Thanks for catching this. I added a non-phi instruction before the branch-merge ReadLane regression and verified the generated phi is placed before the first non-phi instruction. The placement rule now inserts after any existing phis at the block front, including phis created recursively for predecessor paths. The fix and regression are two commits on this PR: 78acdedb76 and 204cbc558d. Both changed translation units compile with the existing /tmp/build-8050-current compile commands; git diff --check and tools/check_conventions.py --base upstream/main pass. I could not run the focused CTest: agc_read_lane_elimination is registered, but its executable is missing from that build directory, and only 31 MB is free for a rebuild. The updated hosted checks should provide runtime/build validation.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6299977941 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/8089#issuecomment-6299977941). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-20T19:48:58Z. --- Thanks for catching this. I added a non-phi instruction before the branch-merge `ReadLane` regression and verified the generated phi is placed before the first non-phi instruction. The placement rule now inserts after any existing phis at the block front, including phis created recursively for predecessor paths. The fix and regression are two commits on this PR: `78acdedb76` and `204cbc558d`. Both changed translation units compile with the existing `/tmp/build-8050-current` compile commands; `git diff --check` and `tools/check_conventions.py --base upstream/main` pass. I could not run the focused CTest: `agc_read_lane_elimination` is registered, but its executable is missing from that build directory, and only 31 MB is free for a rebuild. The updated hosted checks should provide runtime/build validation.
DavidAmsellem commented 2026-10-20 20:35:58 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by DavidAmsellem on 2026-10-20T20:35:58Z.


@MarcZX10 the run you couldn't do: current head 204cbc558 (with 31630fba75, 78acdedb76 and 204cbc558d) merged onto main aa1b1bfce (clean merge), built in full.

AMD Radeon RX 7900 XT (RADV NAVI31, gfx1100), Mesa 25.2.8, Linux Mint 22.3 (Ubuntu 24.04 base), kernel 7.0.0-38, GCC 13.3, Release.

  • agc_read_lane_elimination: passes 3 of 3, and again under the Khronos validation layer.
  • Full suite (ANYPS5_REQUIRE_VULKAN=1 ctest -j8) at 1df694e5b: 1734/1735. The one failure is agc_driver_guest_memory_lifetime (#7784), which fails on main too, so this head adds none.

GPUs tested: AMD Radeon RX 7900 XT (RADV NAVI31, gfx1100), Mesa 25.2.8, Linux Mint 22.3 / kernel 7.0.0-38

AI-assisted: yes

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6300910661 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/8089#issuecomment-6300910661). Originally posted by [DavidAmsellem](https://github.com/DavidAmsellem) on 2026-10-20T20:35:58Z. --- @MarcZX10 the run you couldn't do: current head `204cbc558` (with `31630fba75`, `78acdedb76` and `204cbc558d`) merged onto `main` `aa1b1bfce` (clean merge), built in full. AMD Radeon RX 7900 XT (RADV NAVI31, gfx1100), Mesa 25.2.8, Linux Mint 22.3 (Ubuntu 24.04 base), kernel 7.0.0-38, GCC 13.3, Release. - `agc_read_lane_elimination`: passes 3 of 3, and again under the Khronos validation layer. - Full suite (`ANYPS5_REQUIRE_VULKAN=1 ctest -j8`) at `1df694e5b`: **1734/1735**. The one failure is `agc_driver_guest_memory_lifetime` (#7784), which fails on `main` too, so this head adds none. GPUs tested: AMD Radeon RX 7900 XT (RADV NAVI31, gfx1100), Mesa 25.2.8, Linux Mint 22.3 / kernel 7.0.0-38 AI-assisted: yes
MarcZX10 commented 2026-10-20 21:27:14 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-20T21:27:14Z.


Thanks for building the current head and running the read-lane elimination case three times, including with the Khronos validation layer. The full-suite result also isolates the sole failure to the already known #7784 failure on main; I have not run these GPU tests locally.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6301856161 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/8089#issuecomment-6301856161). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-20T21:27:14Z. --- Thanks for building the current head and running the read-lane elimination case three times, including with the Khronos validation layer. The full-suite result also isolates the sole failure to the already known #7784 failure on main; I have not run these GPU 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!8089
No description provided.