fix(shader): link table callees that read their program counter #8140

Merged
tv7 merged 4 commits from upstream-pr/8140 into main 2026-11-01 14:03:51 +00:00
tv7 commented 2026-10-19 23:56:50 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by tv7 on 2026-10-19T23:56:50Z.


What

A compute shader that calls through a table (FindTableCalls / LinkCalls) is refused when one of the table's callees reads its program counter:

AGC driver: compute shader 0x11d049aa00 calls a target loaded from a table at program counter 0x2470, but user-data callee reads its program counter at callee offset 0x00000e34, which a linked copy does not keep

LinkCalls copies each callee into the caller, so an s_getpc_b64 inside the copy would return the copy's address instead of the callee's. For a table callee the original address is known (TableCallee::target), so the copy can keep the value the instruction reads on hardware:

  • LinkCalls expands each s_getpc_b64 s[n:n+1] of a table callee into s_mov_b32 s[n], lo and s_mov_b32 s[n+1], hi of target + offset + 4, the address the instruction returns at its original place. The callee's direct branches are re-targeted to the moved instructions, so the copy stays inside its call region.
  • UserDataCalleeLength takes allowGetpc; ReadTableCallees passes it, so table callees with s_getpc_b64 are read instead of refused. User-data callees (no fixed address at link time) are still refused as before.
  • A callee whose s_getpc_b64 writes something other than an SGPR pair, or that branches outside its own instructions, is refused with its own message.

Tested

  • Windows 11, MinGW-w64 GCC (WinLibs 15.2), Release, BUILD_TESTING=ON, on main eb7500680, rebased onto 3aa35dc01:
    • scalar_call gains CheckTableCalleeProgramCounter: a callee s_branch +1; s_getpc_b64 s[16:17]; s_setpc_b64 s[14:15] at 0x1122334400 links to s_branch +4; s_mov_b32 s16, 0x22334408; s_mov_b32 s17, 0x11; s_setpc_b64 s[14:15], the linked program builds its call graph and translates; a callee without s_getpc_b64 is copied unchanged; s_getpc_b64 vcc is refused.
    • With the expansion disabled (callee copied as before) the new check fails, with it it passes.
    • Full build and full ctest on eb7500680: everything passes except 8 tests that fail the same way on plain eb7500680 on this machine (shader_cache_runtime_sources cannot find Ninja, patched_libraries cannot load the MinGW runtime, agc_driver_msaa_dcc_clear_4, agc_driver_indirect_arguments_pending_producer, agc_driver_pass_input_hazard, agc_shader_replay_tool, agc_driver_pm4_replay_tool, guest_memory). After the rebase onto 3aa35dc01: scalar_call and every table/user-data call, swappc and shader-memory test pass.
  • Marvel's Wolverine (PPSA03671), RTX 3070 8 GB, Windows: the refusal above (shader 0x11d049aa00, table call at 0x2470, callee offset 0xe34) stops the title after its intro movies. With this change the shader links and translates; its next stop is a separate one (a sampler read through a data-dependent pointer chain, #6191). Not a stock-main run: that tree also carries #7816, #7006, #7679 and local changes, so it shows only that this refusal is gone.

Checklist

  • Based on current main (3aa35dc01); no open PR changes LinkCalls callee copying or the s_getpc_b64 refusal
  • One topic per PR
  • No comments in code except technical debt
  • Unimplemented paths throw (callees that cannot be expanded are refused with a message)
  • No notes, investigation .md files or images added
  • New third-party code is a submodule built from source (none added)
  • Generic behaviour, not specific to one title
  • Depends on: none
  • Fixes: none (related: #6191, #6099)
  • Unblocks: PPSA03671
  • GPUs tested: NVIDIA GeForce RTX 3070 (title run); the test is CPU-only
  • AI-assisted: yes

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4928993106 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/8140). Originally posted by [tv7](https://github.com/tv7) on 2026-10-19T23:56:50Z. --- ### What A compute shader that calls through a table (`FindTableCalls` / `LinkCalls`) is refused when one of the table's callees reads its program counter: ``` AGC driver: compute shader 0x11d049aa00 calls a target loaded from a table at program counter 0x2470, but user-data callee reads its program counter at callee offset 0x00000e34, which a linked copy does not keep ``` `LinkCalls` copies each callee into the caller, so an `s_getpc_b64` inside the copy would return the copy's address instead of the callee's. For a table callee the original address is known (`TableCallee::target`), so the copy can keep the value the instruction reads on hardware: - `LinkCalls` expands each `s_getpc_b64 s[n:n+1]` of a table callee into `s_mov_b32 s[n], lo` and `s_mov_b32 s[n+1], hi` of `target + offset + 4`, the address the instruction returns at its original place. The callee's direct branches are re-targeted to the moved instructions, so the copy stays inside its call region. - `UserDataCalleeLength` takes `allowGetpc`; `ReadTableCallees` passes it, so table callees with `s_getpc_b64` are read instead of refused. User-data callees (no fixed address at link time) are still refused as before. - A callee whose `s_getpc_b64` writes something other than an SGPR pair, or that branches outside its own instructions, is refused with its own message. ### Tested - **Windows 11, MinGW-w64 GCC (WinLibs 15.2), Release, `BUILD_TESTING=ON`**, on `main` eb7500680, rebased onto 3aa35dc01: - `scalar_call` gains `CheckTableCalleeProgramCounter`: a callee `s_branch +1; s_getpc_b64 s[16:17]; s_setpc_b64 s[14:15]` at `0x1122334400` links to `s_branch +4; s_mov_b32 s16, 0x22334408; s_mov_b32 s17, 0x11; s_setpc_b64 s[14:15]`, the linked program builds its call graph and translates; a callee without `s_getpc_b64` is copied unchanged; `s_getpc_b64 vcc` is refused. - With the expansion disabled (callee copied as before) the new check fails, with it it passes. - Full build and full `ctest` on eb7500680: everything passes except 8 tests that fail the same way on plain eb7500680 on this machine (`shader_cache_runtime_sources` cannot find Ninja, `patched_libraries` cannot load the MinGW runtime, `agc_driver_msaa_dcc_clear_4`, `agc_driver_indirect_arguments_pending_producer`, `agc_driver_pass_input_hazard`, `agc_shader_replay_tool`, `agc_driver_pm4_replay_tool`, `guest_memory`). After the rebase onto 3aa35dc01: `scalar_call` and every table/user-data call, swappc and shader-memory test pass. - **Marvel's Wolverine (PPSA03671)**, RTX 3070 8 GB, Windows: the refusal above (shader `0x11d049aa00`, table call at `0x2470`, callee offset `0xe34`) stops the title after its intro movies. With this change the shader links and translates; its next stop is a separate one (a sampler read through a data-dependent pointer chain, #6191). Not a stock-`main` run: that tree also carries #7816, #7006, #7679 and local changes, so it shows only that this refusal is gone. ### Checklist - [x] Based on current `main` (3aa35dc01); no open PR changes `LinkCalls` callee copying or the `s_getpc_b64` refusal - [x] One topic per PR - [x] No comments in code except technical debt - [x] Unimplemented paths throw (callees that cannot be expanded are refused with a message) - [x] No notes, investigation `.md` files or images added - [x] New third-party code is a submodule built from source (none added) - [x] Generic behaviour, not specific to one title - [x] Depends on: none - [x] Fixes: none (related: #6191, #6099) - [x] Unblocks: PPSA03671 - [x] GPUs tested: NVIDIA GeForce RTX 3070 (title run); the test is CPU-only - [x] AI-assisted: yes 🤖 Generated with [Claude Code](https://claude.com/claude-code)
wachi35676 commented 2026-10-20 12:31:41 +00:00 (Migrated from github.com)

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


Windows 11 26300, WinLibs GCC 15.2.0, Release, BUILD_TESTING=ON, main 02fb1ce53 with this PR (9a0bdcdde) 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.

  • The value matches the ISA: RDNA 2 S_GETPC_B64 is D.u64 = PC + 4, and the expansion writes target + offset + 4.
  • scalar_call and the other call/swappc/user-data/shader-memory tests (ctest -R "scalar_call|swappc|user_data_call|table_call|shader_memory"): 9/9 pass. With LinkCalls inserting the callee unchanged (the expansion disabled, everything else from the PR kept), scalar_call fails.
  • Extra probe, not part of the PR: a backward branch onto an expanded instruction. The callee is s_getpc_b64 s[16:17]; s_cbranch_scc0 -2; s_setpc_b64 s[14:15] at 0x1122334400. Linked through TableCaller(), it gives s_mov_b32 s16, 0x22334404; s_mov_b32 s17, 0x11; s_cbranch_scc0 -5; s_setpc_b64 s[14:15] (0xbf84fffb): the branch lands on the first s_mov_b32, and BuildCode and TranslateCode accept it.
  • ctest -R "agc|shader|spirv|recomp|scalar_call" -j6, 834 tests: RTX, only agc_driver_pass_input_hazard fails, as on main here. iGPU: agc_driver_mul_lo_u16_high_half and agc_driver_sgpr_pattern_fill_keys fail as on main, plus agc_driver_queued_index_read_order, which passed 3 of 3 when run alone. No new failures.

No title here (no PPSA03671).

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

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6290900165 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/8140#issuecomment-6290900165). Originally posted by [wachi35676](https://github.com/wachi35676) on 2026-10-20T12:31:41Z. --- Windows 11 26300, WinLibs GCC 15.2.0, Release, `BUILD_TESTING=ON`, `main` `02fb1ce53` with this PR (`9a0bdcdde`) 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`. - The value matches the ISA: RDNA 2 `S_GETPC_B64` is `D.u64 = PC + 4`, and the expansion writes `target + offset + 4`. - `scalar_call` and the other call/swappc/user-data/shader-memory tests (`ctest -R "scalar_call|swappc|user_data_call|table_call|shader_memory"`): 9/9 pass. With `LinkCalls` inserting the callee unchanged (the expansion disabled, everything else from the PR kept), `scalar_call` fails. - Extra probe, not part of the PR: a backward branch onto an expanded instruction. The callee is `s_getpc_b64 s[16:17]; s_cbranch_scc0 -2; s_setpc_b64 s[14:15]` at `0x1122334400`. Linked through `TableCaller()`, it gives `s_mov_b32 s16, 0x22334404; s_mov_b32 s17, 0x11; s_cbranch_scc0 -5; s_setpc_b64 s[14:15]` (`0xbf84fffb`): the branch lands on the first `s_mov_b32`, and `BuildCode` and `TranslateCode` accept it. - `ctest -R "agc|shader|spirv|recomp|scalar_call" -j6`, 834 tests: RTX, only `agc_driver_pass_input_hazard` fails, as on `main` here. iGPU: `agc_driver_mul_lo_u16_high_half` and `agc_driver_sgpr_pattern_fill_keys` fail as on `main`, plus `agc_driver_queued_index_read_order`, which passed 3 of 3 when run alone. No new failures. No title here (no PPSA03671). AI-assisted: yes (Claude Code); results above were run on this machine.
ahmadmuj commented 2026-10-25 16:45:07 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by ahmadmuj on 2026-10-25T16:45:07Z.

Review: APPROVED


Reviewed head aa656b2ee5 merged onto main 0119103fa9 (clean merge). Linux x86_64 (WSL2), GCC 13.3, Release, lavapipe (llvmpipe, LLVM 20.1.2).

What I ran:

  • scalar_call, scalar_swappc, agc_shader_control_flow (plus _iteration_selection_off and _forced_fallback), agc_driver_compute_table_calls, agc_driver_compute_fs_table_calls, agc_driver_compute_user_data_calls (plus _registered), agc_driver_scalar_swappc: 10/10 pass. The driver table-call tests really ran on lavapipe, not skipped.
  • Red: LinkCalls with codes taken straight from callee.code (expansion off, rest of the PR kept): scalar_call fails. Back to the PR: passes.
  • tools/check_conventions.py --base 0119103fa9: pass.
  • Extra probe, not part of the PR: a callee where the s_getpc actually runs and feeds a PC-relative load, s_getpc_b64 s[16:17]; s_add_u32 s16, s16, 0x40; s_addc_u32 s17, s17, 0; s_load_dword s20, s[16:17], 0; s_waitcnt lgkmcnt(0); s_setpc_b64 s[14:15] at 0x1122334400, through TableCaller(). It links to s_mov_b32 s16, 0x22334404; s_mov_b32 s17, 0x11; ..., and BuildCode/TranslateCode accept it. The IR loads through GetAddressResource(s16, s17) after the add, so it reads guest 0x1122334444, same as on hardware.

Code walk:

  • target + pc + 4 matches the recompiler's own s_getpc_b64 model (sGetpcB64: shader base + pc + 4).
  • Branch remap: moved[] only marks instruction starts, so a branch into a literal throws, and branchWord gets expanded offsets on both ends. A branch past the return is already refused by UserDataCalleeLength.
  • A computed jump off the PC is still refused: s_setpc_b64 through anything but the link throws in UserDataCalleeLength.
  • Caching: the prepared-entry match compares the full linked code (ShaderRegistry.cpp, std::ranges::equal(..., linked.code)), and the baked literal is part of it, so same-byte callees at different addresses don't share a translation.
  • User-data callees still refuse s_getpc_b64 (default false).

One optional thing: in CheckTableCalleeProgramCounter the callee starts with s_branch +1 (0xbf820001), which jumps over the s_getpc. So the expanded s_mov pair is dead code when BuildCode/TranslateCode run on it. The word check and the red run still pin the expansion. If you want the translate step to cover a live expanded read too, a callee like the probe above would do: {0xbe901f00, 0x8010ff10, 0x00000040, 0x82118011, 0xf4000508, 0xfa000000, 0xbf8cc07f, 0xbe80200e}.

Not checked: the title (no PPSA03671), and the AMD ISA PDF itself (wachi35676 quoted it above; I compared against the existing translation instead).

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5644185134 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/8140#pullrequestreview-5644185134). Originally posted by [ahmadmuj](https://github.com/ahmadmuj) on 2026-10-25T16:45:07Z. **Review:** APPROVED --- Reviewed head `aa656b2ee5` merged onto main `0119103fa9` (clean merge). Linux x86_64 (WSL2), GCC 13.3, Release, lavapipe (llvmpipe, LLVM 20.1.2). What I ran: - `scalar_call`, `scalar_swappc`, `agc_shader_control_flow` (plus `_iteration_selection_off` and `_forced_fallback`), `agc_driver_compute_table_calls`, `agc_driver_compute_fs_table_calls`, `agc_driver_compute_user_data_calls` (plus `_registered`), `agc_driver_scalar_swappc`: 10/10 pass. The driver table-call tests really ran on lavapipe, not skipped. - Red: `LinkCalls` with `codes` taken straight from `callee.code` (expansion off, rest of the PR kept): `scalar_call` fails. Back to the PR: passes. - `tools/check_conventions.py --base 0119103fa9`: pass. - Extra probe, not part of the PR: a callee where the `s_getpc` actually runs and feeds a PC-relative load, `s_getpc_b64 s[16:17]; s_add_u32 s16, s16, 0x40; s_addc_u32 s17, s17, 0; s_load_dword s20, s[16:17], 0; s_waitcnt lgkmcnt(0); s_setpc_b64 s[14:15]` at `0x1122334400`, through `TableCaller()`. It links to `s_mov_b32 s16, 0x22334404; s_mov_b32 s17, 0x11; ...`, and `BuildCode`/`TranslateCode` accept it. The IR loads through `GetAddressResource(s16, s17)` after the add, so it reads guest `0x1122334444`, same as on hardware. Code walk: - `target + pc + 4` matches the recompiler's own `s_getpc_b64` model (`sGetpcB64`: shader base + pc + 4). - Branch remap: `moved[]` only marks instruction starts, so a branch into a literal throws, and `branchWord` gets expanded offsets on both ends. A branch past the return is already refused by `UserDataCalleeLength`. - A computed jump off the PC is still refused: `s_setpc_b64` through anything but the link throws in `UserDataCalleeLength`. - Caching: the prepared-entry match compares the full linked code (`ShaderRegistry.cpp`, `std::ranges::equal(..., linked.code)`), and the baked literal is part of it, so same-byte callees at different addresses don't share a translation. - User-data callees still refuse `s_getpc_b64` (default `false`). One optional thing: in `CheckTableCalleeProgramCounter` the callee starts with `s_branch +1` (`0xbf820001`), which jumps over the `s_getpc`. So the expanded `s_mov` pair is dead code when `BuildCode`/`TranslateCode` run on it. The word check and the red run still pin the expansion. If you want the translate step to cover a live expanded read too, a callee like the probe above would do: `{0xbe901f00, 0x8010ff10, 0x00000040, 0x82118011, 0xf4000508, 0xfa000000, 0xbf8cc07f, 0xbe80200e}`. Not checked: the title (no PPSA03671), and the AMD ISA PDF itself (wachi35676 quoted it above; I compared against the existing translation instead).
MarcZX10 commented 2026-10-29 09:44:14 +00:00 (Migrated from github.com)

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

Review: CHANGES_REQUESTED


Re-check — changes requested for current-main integration

The new test exercises the table-callee s_getpc_b64 rewrite, verifies the materialized address and remapped direct branch, and retains rejection of unsupported VCC destinations. I found no new source-level defect in that scoped change.

The reported NVIDIA run is on the PR head merged with main 52c09d5, while current main is aaccd0ba39b7d40fdbf19664575a7e567d8542f7. Current main has since changed GraphBuilder.hpp and GraphBuilder.cpp, which this PR also modifies. The current merge preview is clean, but the old-base GPU/test run does not validate the resulting integration.

Please refresh the branch onto current main, preserve the new table-callee behavior alongside main's GraphBuilder changes, and rerun the focused scalar-call/recompiler tests and the relevant GPU/validation checks on that exact head. The current hosted product jobs are skipped. I did not run these tests locally.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5695688042 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/8140#pullrequestreview-5695688042). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-29T09:44:14Z. **Review:** CHANGES_REQUESTED --- # Re-check — changes requested for current-main integration The new test exercises the table-callee `s_getpc_b64` rewrite, verifies the materialized address and remapped direct branch, and retains rejection of unsupported VCC destinations. I found no new source-level defect in that scoped change. The reported NVIDIA run is on the PR head merged with `main` `52c09d5`, while current `main` is `aaccd0ba39b7d40fdbf19664575a7e567d8542f7`. Current main has since changed `GraphBuilder.hpp` and `GraphBuilder.cpp`, which this PR also modifies. The current merge preview is clean, but the old-base GPU/test run does not validate the resulting integration. Please refresh the branch onto current main, preserve the new table-callee behavior alongside main's GraphBuilder changes, and rerun the focused scalar-call/recompiler tests and the relevant GPU/validation checks on that exact head. The current hosted product jobs are skipped. 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!8140
No description provided.