fix(shader): resolve table column pointers through Phi nodes #7880

Merged
klosixgaming-arch merged 2 commits from upstream-pr/7880 into main 2026-10-24 00:22:58 +00:00
klosixgaming-arch commented 2026-10-19 12:10:05 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by klosixgaming-arch on 2026-10-19T12:10:05Z.


What

When an indirect table column descriptor pointer has its dword components resolved through a Phi node (e.g. when control flow merges equivalent scalar reads or invariant values), TryMakeIndirectColumn() fails to match the scalar memory read because pointer->Argument(dword)->Resolve() remains a Phi node.

Traverse through Phi operands in UnwrapTableRead() to resolve the scalar memory read, verifying that all non-Phi branches converge on an equivalent descriptor read. Any non-Phi leaf that is not a matching table read makes the function return nullptr.

Tested

  • ctest -R ^agc_shader_memory$ (agc_shader_memory_tests target): passed locally on Windows (x86_64 mingw-w64). CI build jobs (Windows, Linux, relinker) were skipped; only the conventions, labels and eligibility jobs ran in CI.
  • Positive Phi test added in two places: verifyPointerImageTable() and verifyPointerChainImageTable().
  • Two negative tests added in verifyPointerChainImageTable(): a Phi with different table offsets and a Phi with a non-table branch, both verified using expectFailure on GetResourcePlan() expecting "GetImageResource dword 0 is not a valid runtime value".
  • Verified that on plain main without the fix, the positive tests fail.
  • Verified that temporarily reverting the leaf rule to skip non-matching leaves causes the negative non-table-branch test to fail with "pointer chain image table: a phi with a non-table branch was planned as a table".

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
  • GPUs tested: n/a
  • AI-assisted: yes. Written with AI assistance and reviewed by me.
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4920701582 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/7880). Originally posted by [klosixgaming-arch](https://github.com/klosixgaming-arch) on 2026-10-19T12:10:05Z. --- ### What When an indirect table column descriptor pointer has its dword components resolved through a Phi node (e.g. when control flow merges equivalent scalar reads or invariant values), `TryMakeIndirectColumn()` fails to match the scalar memory read because `pointer->Argument(dword)->Resolve()` remains a Phi node. Traverse through Phi operands in `UnwrapTableRead()` to resolve the scalar memory read, verifying that all non-Phi branches converge on an equivalent descriptor read. Any non-Phi leaf that is not a matching table read makes the function return `nullptr`. ### Tested - `ctest -R ^agc_shader_memory$` (`agc_shader_memory_tests` target): passed locally on Windows (x86_64 mingw-w64). CI build jobs (Windows, Linux, relinker) were skipped; only the conventions, labels and eligibility jobs ran in CI. - Positive Phi test added in two places: `verifyPointerImageTable()` and `verifyPointerChainImageTable()`. - Two negative tests added in `verifyPointerChainImageTable()`: a Phi with different table offsets and a Phi with a non-table branch, both verified using `expectFailure` on `GetResourcePlan()` expecting `"GetImageResource dword 0 is not a valid runtime value"`. - Verified that on plain `main` without the fix, the positive tests fail. - Verified that temporarily reverting the leaf rule to skip non-matching leaves causes the negative non-table-branch test to fail with `"pointer chain image table: a phi with a non-table branch was planned as a table"`. ### 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: none - [x] Unblocks: none - [x] GPUs tested: n/a - [x] AI-assisted: yes. Written with AI assistance and reviewed by me.
savvaniss commented 2026-10-19 14:12:33 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by savvaniss on 2026-10-19T14:12:33Z.


AMD and Intel Windows data: the 15 tests built from tests/ShaderMemory.cpp pass on an AMD RX 9070 XT and an Intel UHD 770, with and without the change.

Windows 11 Pro 26300, WinLibs GCC 15.2.0 r7, Release, every execution test built, ANYPS5_NO_SHADER_CACHE=1, serial CTest. Intel UHD Graphics 770 (8086:a780), driver 32.0.101.7088. AMD Radeon RX 9070 XT (1002:7550), driver 32.0.31041.1004, with VkCache emptied first. This head 72e97735e merged into main 85029a00f. "Without" means only this PR's diff to tests/ShaderMemory.cpp, applied to main 85029a00f.

Tests, ×3 UHD 770 RX 9070 XT
This head: the 15 tests built from tests/ShaderMemory.cpp 15 of 15 pass 15 of 15 pass
Without 15 of 15 pass 15 of 15 pass

So the test changes also pass on main without the fix. On these GPUs they show no regression, but they don't reproduce the case the fix resolves.

AI-assisted: yes (Claude Code)

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6269289705 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/7880#issuecomment-6269289705). Originally posted by [savvaniss](https://github.com/savvaniss) on 2026-10-19T14:12:33Z. --- AMD and Intel Windows data: the 15 tests built from `tests/ShaderMemory.cpp` pass on an AMD RX 9070 XT and an Intel UHD 770, with and without the change. Windows 11 Pro 26300, WinLibs GCC 15.2.0 r7, Release, every execution test built, `ANYPS5_NO_SHADER_CACHE=1`, serial CTest. Intel UHD Graphics 770 (`8086:a780`), driver 32.0.101.7088. AMD Radeon RX 9070 XT (`1002:7550`), driver 32.0.31041.1004, with `VkCache` emptied first. This head `72e97735e` merged into `main` `85029a00f`. "Without" means only this PR's diff to `tests/ShaderMemory.cpp`, applied to `main` `85029a00f`. | Tests, ×3 | UHD 770 | RX 9070 XT | |---|---|---| | This head: the 15 tests built from `tests/ShaderMemory.cpp` | 15 of 15 pass | 15 of 15 pass | | Without | 15 of 15 pass | 15 of 15 pass | So the test changes also pass on `main` without the fix. On these GPUs they show no regression, but they don't reproduce the case the fix resolves. AI-assisted: yes (Claude Code)
DavidAmsellem commented 2026-10-19 15:44:30 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by DavidAmsellem on 2026-10-19T15:44:30Z.


First AMD-on-Linux (RADV) run of this PR. Merged onto main 43a2abaf7 (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): 1636/1638 with this PR, against 1636/1638 on main. The failures are the same two on both, so this PR adds none: agc_driver_image_table_vouched_lookups (#7766, fixed by #7916) and agc_driver_guest_memory_lifetime (#7784). The PR adds no new ctest entry; every existing test it touches passed.

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:6271205715 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/7880#issuecomment-6271205715). Originally posted by [DavidAmsellem](https://github.com/DavidAmsellem) on 2026-10-19T15:44:30Z. --- First AMD-on-Linux (RADV) run of this PR. Merged onto `main` `43a2abaf7` (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`): **1636/1638 with this PR**, against 1636/1638 on `main`. The failures are the same two on both, so this PR adds none: `agc_driver_image_table_vouched_lookups` (#7766, fixed by #7916) and `agc_driver_guest_memory_lifetime` (#7784). The PR adds no new ctest entry; every existing test it touches passed. 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
oneandonlydean commented 2026-10-20 21:37:40 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-20T21:37:40Z.


@klosixgaming-arch two things before this can go in:

  • The new ShaderMemory cases don't exercise the change: savvaniss's run above shows all 15 pass on main without the fix as well. Could you add a case that fails on main (the refusal this PR targets) and passes with it?
  • UnwrapTableRead returns the read from one phi arm, and TryMakeIndirectColumn keeps that read's operands: bufferHandle = read->Argument(0) and plan.offset = read->Argument(1). The materializer then uses plan.offset at the image read (column.selected ? Constant(addend) : plan.offset). If an arm computes its offset or buffer handle inside the arm, those values don't dominate the read after the merge, and the module is invalid even though EquivalentValue matches them. Could you require them to dominate the image use (and return nullptr otherwise), or rematerialize them there, and add a case where the offset is computed in each arm?
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6302087361 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/7880#issuecomment-6302087361). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-10-20T21:37:40Z. --- @klosixgaming-arch two things before this can go in: - The new ShaderMemory cases don't exercise the change: savvaniss's run above shows all 15 pass on `main` without the fix as well. Could you add a case that fails on `main` (the refusal this PR targets) and passes with it? - `UnwrapTableRead` returns the read from one phi arm, and `TryMakeIndirectColumn` keeps that read's operands: `bufferHandle = read->Argument(0)` and `plan.offset = read->Argument(1)`. The materializer then uses `plan.offset` at the image read (`column.selected ? Constant(addend) : plan.offset`). If an arm computes its offset or buffer handle inside the arm, those values don't dominate the read after the merge, and the module is invalid even though `EquivalentValue` matches them. Could you require them to dominate the image use (and return `nullptr` otherwise), or rematerialize them there, and add a case where the offset is computed in each arm?
klosixgaming-arch commented 2026-10-21 17:24:34 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by klosixgaming-arch on 2026-10-21T17:24:34Z.


@oneandonlydean pushed 64675a375 (one commit on top of 2e5004d2a) for both points.

1. A case that fails on main. The two positive cases already in this PR are those cases, and I could reproduce that they fail without the fix. I built main (cecb540a6) with only this PR's tests/ShaderMemory.cpp diff applied and ran agc_shader_memory (Windows 11, WinLibs GCC 15.2.0, Release, RTX 5090): Failed, with
GetImageResource dword 0 is not a valid runtime value; chain: LoadAddressU32@pc=0x00000060(GetAddressResource(LoadAddressU32@pc=0x00000058(GetAddressResource(Phi(ReadConstBuffer@pc=0x00000044(...
That is the refusal this PR targets. I can't explain why savvaniss's run passed without the fix; his table has all 15 tests built from tests/ShaderMemory.cpp passing on main with and without the change, and I only ran the one ctest entry agc_shader_memory, which is where the failure is.

2. Dominance. You are right. Taking plan.offset from one arm is wrong when the arm computes it, because it is substituted into the image handle after the merge. TryMakeIndirectColumn now returns false unless the offset is an immediate, has no parent block, or its parent block dominates the handle's block (BlockDominates walks predecessors back to a root block and reports no dominance if it can get there without passing the dominator). I did not add the same check for bufferHandle: it is only passed to MakeRuntimeSource, which builds a DescriptorSource from the handle's constant description and does not keep the IR value, and UnwrapTableRead already requires the arms' Argument(0) to be EquivalentValue. In the existing positive cases each arm has its own GetBufferResource, so requiring it to dominate would refuse them (I tried that first and agc_shader_memory failed).

New case in verifyPointerChainImageTable: each arm computes the offset (s_lshl_b32 s18, s17, 4) before its own load. Results of agc_shader_memory:

  • previous head plus only the new case: Failed, pointer chain image table: a phi whose arms compute their own table offset was planned as a table
  • 64675a375: Passed (0.52 s)

python tools/check_conventions.py --base origin/main passes. I did not run the validation layer on this one (it is a planning change with no Vulkan calls) and did not run the other test targets.

AI-assisted: yes. Written with AI assistance and reviewed by me.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6322209746 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/7880#issuecomment-6322209746). Originally posted by [klosixgaming-arch](https://github.com/klosixgaming-arch) on 2026-10-21T17:24:34Z. --- @oneandonlydean pushed 64675a375 (one commit on top of 2e5004d2a) for both points. **1. A case that fails on `main`.** The two positive cases already in this PR are those cases, and I could reproduce that they fail without the fix. I built `main` (cecb540a6) with only this PR's `tests/ShaderMemory.cpp` diff applied and ran `agc_shader_memory` (Windows 11, WinLibs GCC 15.2.0, Release, RTX 5090): `Failed`, with `GetImageResource dword 0 is not a valid runtime value; chain: LoadAddressU32@pc=0x00000060(GetAddressResource(LoadAddressU32@pc=0x00000058(GetAddressResource(Phi(ReadConstBuffer@pc=0x00000044(...` That is the refusal this PR targets. I can't explain why savvaniss's run passed without the fix; his table has all 15 tests built from `tests/ShaderMemory.cpp` passing on `main` with and without the change, and I only ran the one ctest entry `agc_shader_memory`, which is where the failure is. **2. Dominance.** You are right. Taking `plan.offset` from one arm is wrong when the arm computes it, because it is substituted into the image handle after the merge. `TryMakeIndirectColumn` now returns `false` unless the offset is an immediate, has no parent block, or its parent block dominates the handle's block (`BlockDominates` walks predecessors back to a root block and reports no dominance if it can get there without passing the dominator). I did not add the same check for `bufferHandle`: it is only passed to `MakeRuntimeSource`, which builds a `DescriptorSource` from the handle's constant description and does not keep the IR value, and `UnwrapTableRead` already requires the arms' `Argument(0)` to be `EquivalentValue`. In the existing positive cases each arm has its own `GetBufferResource`, so requiring it to dominate would refuse them (I tried that first and `agc_shader_memory` failed). New case in `verifyPointerChainImageTable`: each arm computes the offset (`s_lshl_b32 s18, s17, 4`) before its own load. Results of `agc_shader_memory`: - previous head plus only the new case: Failed, `pointer chain image table: a phi whose arms compute their own table offset was planned as a table` - 64675a375: Passed (0.52 s) `python tools/check_conventions.py --base origin/main` passes. I did not run the validation layer on this one (it is a planning change with no Vulkan calls) and did not run the other test targets. AI-assisted: yes. Written with AI assistance and reviewed by me.
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!7880
No description provided.