fix(shader): link table callees that together exceed s_call_b64 range #9068

Merged
frankea merged 3 commits from upstream-pr/9068 into main 2026-10-23 23:58:48 +00:00
frankea commented 2026-10-21 11:34:11 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by frankea on 2026-10-21T11:34:11Z.


What

Gran Turismo 7's World Map dispatches a compute shader whose s_swappc_b64 calls through a table of about 35 material functions, about 40,000 dwords in all. Linking it threw the table callees of the call at program counter 0x00000ae4 land out of s_call_b64 range. Once it linked, the call model refused it with scalar call link register s[14] written at program counter 0x000018e4 escapes the constant-offset call/return model (used at program counter 0x000009f0). This PR fixes both.

  • LinkCalls appended every table callee after a single block of call sites, so a callee's s_call_b64 had to reach past all the callees before it. SIMM16 is a signed dword offset, so it reaches at most 0x7fff dwords forward (AMD RDNA2 ISA 3.6.3, S_CALL_B64). Each callee now gets its own segment: its compares for SCC 0, its compares for SCC 1, an s_call_b64 with offset 1, a return s_branch, then the copy. A mismatch branches over the copy to the next target's compares. A return goes back one s_branch per earlier copy to the instruction after the original call. No branch or call spans more than one copy, and a copy is at most 16,384 dwords (ReadUserDataCallee's limit). SCC handling is unchanged: one chain per SCC value at the call, and SCC is set back to that value before the callee or the unlinked call runs.
  • overwritesScalar counted only the first destination dword of SOP2 results and of SOP1 results other than s_mov_b64 and s_getpc_b64. In the GT7 shader, s_andn2_b64 s[14:15], s[6:7], s[4:5] overwrites a link pair and s_nor_b64 vcc, s[14:15], vcc then reads it. The escape check still marked s15 as holding a return address and refused the read. It now uses the decoder's dataDwordCount, which is 2 for the 64-bit results. The conditional and partial writers (s_cmov, s_bitset, s_movreld, s_movrelsd_2) and s_swappc/s_setpc are still not counted as overwrites. The escape check is the only caller.
  • A second table call in the same shader (Ghostrunner's 0x200c520000, reported in the comments) had to branch past every copy already appended for the earlier call. Before the first of an earlier call's copies and after each one, LinkCalls leaves a pair of s_branch relays for every later call, one toward its chain and one back toward the instruction after it, so those branches also span at most one copy. A shader with a single table call links as before.
  • Still throws: a call more than 0x7fff dwords before the end of the program and its user-data callees. The debt entry says so instead of the old s_call_b64 range limit.

Tested

  • Refreshed onto main 0db47b263 (head daef563d8; no conflicts), RTX 4090 (NVIDIA 616.92, Windows 11): the 33 ctest entries of scalar_call, agc_shader_control_flow, agc_driver_graphics and agc_driver_compute_table_calls pass 33/33, each test run three times, and 33/33 under the validation layer (VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation, ANYPS5_NO_SHADER_CACHE=1, ANYPS5_REQUIRE_VULKAN=1, ctest -V), with no VUID- line; the layer reports nothing on this head. main 0db47b263 passes the union of the refreshed sets, 115/115, under the same layer settings with no VUID- line.
  • ScalarCall: the two-callee table now checks the interleaved layout word for word, plus the call and return edges of the graph built from it. Three 0x3001-dword callees and a small one link, which the old layout refused, and the last callee's call goes to its own copy and returns to its hop. A 0x8000-dword copy throws the 16-bit branch-range error. A call followed by s_andn2_b64 over the link pair and a 64-bit read builds; with s_and_b32 over only the low dword, the read of the high dword still throws.
  • ScalarCall: two table calls, the first with 36,890 dwords of copies: the second call reaches its chain and returns through the relays, and the graph built from the linked code has its call and return edges. Without the relays this case throws lands out of the 16-bit branch range.
  • RTX 4090 (NVIDIA 616.92, Windows 11, WinLibs GCC 15.2), head e5a02bd48: scalar_call, agc_shader_control_flow (all three variants) and agc_driver_graphics pass.
  • Ghostrunner (PPSA03685 01.000.005) on the RTX 4090, same integration branch: shader 0x200c520000 links its table calls at 0x434 and 0xc84, then stops on a third call at 0x89c that FindTableCalls doesn't match (its descriptor comes from an s_load_dwordx8 through a loaded pointer), which this PR doesn't touch.
  • Gran Turismo 7 (PPSA01317 01.710.000) on the RTX 4090, with these two commits on a local integration branch (#9020, #7978, #7243, #7256, #7550): the World Map shader 0x120aed7f00 links and passes the call model. Its request replayed with agc_shader_replay now stops in the resource tracker on GetImageResource dword 0 is not a valid runtime value: the table's V# is loop-carried through callees that use those registers as scratch, which this PR doesn't touch.

Checklist

  • Based on current main; no other open PR implements the same functions. Overlap: #8140 (tv7) rewrites the line in LinkCalls that appends a table callee, so that a callee's s_getpc_b64 is expanded into two s_mov_b32s, which makes the copy longer than the callee. Whichever PR lands second has to size each segment from the expanded copy. #8070 changes only how table calls are found.
  • 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: PPSA01317 #4697, PPSA03685 #7320 and PPSA10872 #9692, each past this table-call stop (PPSA03685 and PPSA10872 then need #8070 for their next call; PPSA10872 per @zaidhawk's run in the comments)
  • GPUs tested: NVIDIA GeForce RTX 4090, driver 616.92, Windows 11
  • AI-assisted: yes (Claude Code)
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4950870463 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9068). Originally posted by [frankea](https://github.com/frankea) on 2026-10-21T11:34:11Z. --- ### What Gran Turismo 7's World Map dispatches a compute shader whose `s_swappc_b64` calls through a table of about 35 material functions, about 40,000 dwords in all. Linking it threw `the table callees of the call at program counter 0x00000ae4 land out of s_call_b64 range`. Once it linked, the call model refused it with `scalar call link register s[14] written at program counter 0x000018e4 escapes the constant-offset call/return model (used at program counter 0x000009f0)`. This PR fixes both. - `LinkCalls` appended every table callee after a single block of call sites, so a callee's `s_call_b64` had to reach past all the callees before it. SIMM16 is a signed dword offset, so it reaches at most 0x7fff dwords forward ([AMD RDNA2 ISA](https://gpuopen.com/download/rdna2-shader-instruction-set-architecture.pdf) 3.6.3, `S_CALL_B64`). Each callee now gets its own segment: its compares for SCC 0, its compares for SCC 1, an `s_call_b64` with offset 1, a return `s_branch`, then the copy. A mismatch branches over the copy to the next target's compares. A return goes back one `s_branch` per earlier copy to the instruction after the original call. No branch or call spans more than one copy, and a copy is at most 16,384 dwords (`ReadUserDataCallee`'s limit). SCC handling is unchanged: one chain per SCC value at the call, and SCC is set back to that value before the callee or the unlinked call runs. - `overwritesScalar` counted only the first destination dword of SOP2 results and of SOP1 results other than `s_mov_b64` and `s_getpc_b64`. In the GT7 shader, `s_andn2_b64 s[14:15], s[6:7], s[4:5]` overwrites a link pair and `s_nor_b64 vcc, s[14:15], vcc` then reads it. The escape check still marked s15 as holding a return address and refused the read. It now uses the decoder's `dataDwordCount`, which is 2 for the 64-bit results. The conditional and partial writers (`s_cmov`, `s_bitset`, `s_movreld`, `s_movrelsd_2`) and `s_swappc`/`s_setpc` are still not counted as overwrites. The escape check is the only caller. - A second table call in the same shader (Ghostrunner's 0x200c520000, reported in the comments) had to branch past every copy already appended for the earlier call. Before the first of an earlier call's copies and after each one, `LinkCalls` leaves a pair of `s_branch` relays for every later call, one toward its chain and one back toward the instruction after it, so those branches also span at most one copy. A shader with a single table call links as before. - Still throws: a call more than 0x7fff dwords before the end of the program and its user-data callees. The debt entry says so instead of the old `s_call_b64` range limit. ### Tested - Refreshed onto `main` 0db47b263 (head daef563d8; no conflicts), RTX 4090 (NVIDIA 616.92, Windows 11): the 33 ctest entries of `scalar_call`, `agc_shader_control_flow`, `agc_driver_graphics` and `agc_driver_compute_table_calls` pass 33/33, each test run three times, and 33/33 under the validation layer (`VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation`, `ANYPS5_NO_SHADER_CACHE=1`, `ANYPS5_REQUIRE_VULKAN=1`, `ctest -V`), with no `VUID-` line; the layer reports nothing on this head. `main` 0db47b263 passes the union of the refreshed sets, 115/115, under the same layer settings with no `VUID-` line. - `ScalarCall`: the two-callee table now checks the interleaved layout word for word, plus the call and return edges of the graph built from it. Three 0x3001-dword callees and a small one link, which the old layout refused, and the last callee's call goes to its own copy and returns to its hop. A 0x8000-dword copy throws the 16-bit branch-range error. A call followed by `s_andn2_b64` over the link pair and a 64-bit read builds; with `s_and_b32` over only the low dword, the read of the high dword still throws. - `ScalarCall`: two table calls, the first with 36,890 dwords of copies: the second call reaches its chain and returns through the relays, and the graph built from the linked code has its call and return edges. Without the relays this case throws `lands out of the 16-bit branch range`. - RTX 4090 (NVIDIA 616.92, Windows 11, WinLibs GCC 15.2), head e5a02bd48: `scalar_call`, `agc_shader_control_flow` (all three variants) and `agc_driver_graphics` pass. - Ghostrunner (PPSA03685 01.000.005) on the RTX 4090, same integration branch: shader 0x200c520000 links its table calls at 0x434 and 0xc84, then stops on a third call at 0x89c that `FindTableCalls` doesn't match (its descriptor comes from an `s_load_dwordx8` through a loaded pointer), which this PR doesn't touch. - Gran Turismo 7 (PPSA01317 01.710.000) on the RTX 4090, with these two commits on a local integration branch (#9020, #7978, #7243, #7256, #7550): the World Map shader 0x120aed7f00 links and passes the call model. Its request replayed with `agc_shader_replay` now stops in the resource tracker on `GetImageResource dword 0 is not a valid runtime value`: the table's V# is loop-carried through callees that use those registers as scratch, which this PR doesn't touch. ### Checklist - [x] Based on current `main`; no other open PR implements the same functions. Overlap: #8140 (tv7) rewrites the line in `LinkCalls` that appends a table callee, so that a callee's `s_getpc_b64` is expanded into two `s_mov_b32`s, which makes the copy longer than the callee. Whichever PR lands second has to size each segment from the expanded copy. #8070 changes only how table calls are found. - [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: PPSA01317 #4697, PPSA03685 #7320 and PPSA10872 #9692, each past this table-call stop (PPSA03685 and PPSA10872 then need #8070 for their next call; PPSA10872 per @zaidhawk's run in the comments) - [x] GPUs tested: NVIDIA GeForce RTX 4090, driver 616.92, Windows 11 - [x] AI-assisted: yes (Claude Code)
xXPBSXx commented 2026-10-21 15:45:46 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by xXPBSXx on 2026-10-21T15:45:46Z.


Title data point for this PR (data only).

Title: PPSA03685, 300 s through the runner, ANYPS5_GPU=4080; both .out logs show Selected GPU name=NVIDIA GeForce RTX 4080 SUPER vendor=0x10de device=0x2702 subgroup=32. Windows 10 Pro 10.0.19045, WinLibs GCC 15.2.0, Release, relinker and all libs built from one tree.

Baseline, main 50efb5c405 (run_131429.err line 442, with a local print of the callee sizes added in the throw): compute shader 0x200c520000: the table callees of the call at program counter 0x00000434 land out of s_call_b64 range, callee 35 of 40, offset 33134; the 40 callees together are 36988 dwords (3 to 1724 each).

This PR (head 96e20522e6, PR base 80565c5942) merged locally into main 47fa6eecf8 (run_164357.err lines 440-441; the earlier head 84eb444119 gave the same result in run_151642.err): the first call is no longer refused, but the same shader stops at another table call:

what():  compute shader 0x200c520000: the table callees of the call at program counter 0x00000c84 lands out of the 16-bit branch range

The new message comes from branchWord, not from the s_call_b64 check. From reading LinkCalls on the PR head, linked[word] = branchWord(0xbf820000u, word, linked.size(), ...) jumps from the original s_swappc_b64 to the table's segments, which are appended at the end of the program, so a second large table call (0xc84) sits far from its dispatch once the first table's copies are in front of it. I did not print the sizes on the PR head to confirm that, so this is the likely reading, not a measurement.

One run per state. I did not look at the picture and I did not run the PR's tests on this machine for this comment.

Not run: the PR's tests, other GPUs for this title, the console.

AI-assisted (Claude Code).

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6320107018 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9068#issuecomment-6320107018). Originally posted by [xXPBSXx](https://github.com/xXPBSXx) on 2026-10-21T15:45:46Z. --- Title data point for this PR (data only). **Title:** PPSA03685, 300 s through the runner, `ANYPS5_GPU=4080`; both `.out` logs show `Selected GPU name=NVIDIA GeForce RTX 4080 SUPER vendor=0x10de device=0x2702 subgroup=32`. Windows 10 Pro 10.0.19045, WinLibs GCC 15.2.0, Release, relinker and all libs built from one tree. **Baseline, `main` `50efb5c405`** (run_131429.err line 442, with a local print of the callee sizes added in the throw): `compute shader 0x200c520000: the table callees of the call at program counter 0x00000434 land out of s_call_b64 range`, callee 35 of 40, offset 33134; the 40 callees together are 36988 dwords (3 to 1724 each). **This PR (head `96e20522e6`, PR base `80565c5942`) merged locally into `main` `47fa6eecf8`** (run_164357.err lines 440-441; the earlier head `84eb444119` gave the same result in run_151642.err): the first call is no longer refused, but the same shader stops at another table call: ``` what(): compute shader 0x200c520000: the table callees of the call at program counter 0x00000c84 lands out of the 16-bit branch range ``` The new message comes from `branchWord`, not from the `s_call_b64` check. From reading `LinkCalls` on the PR head, `linked[word] = branchWord(0xbf820000u, word, linked.size(), ...)` jumps from the original `s_swappc_b64` to the table's segments, which are appended at the end of the program, so a second large table call (0xc84) sits far from its dispatch once the first table's copies are in front of it. I did not print the sizes on the PR head to confirm that, so this is the likely reading, not a measurement. One run per state. I did not look at the picture and I did not run the PR's tests on this machine for this comment. **Not run:** the PR's tests, other GPUs for this title, the console. AI-assisted (Claude Code).
frankea commented 2026-10-21 20:55:44 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by frankea on 2026-10-21T20:55:44Z.


@xXPBSXx thanks, your reading is right. LinkCalls appended each table call's chain and copies after the previous call's, so the second call's branch to its chain, and its return, had to cross all of the first call's copies (about 37K dwords in your shader). That is the limit the description listed as still throwing.

e5a02bd48 (pushed here) removes it. Before the first of an earlier call's copies and after each one, LinkCalls now leaves a pair of s_branch relays for every later call: one forward toward that call's chain, one back toward the instruction after the call. A later call reaches its chain and returns in hops that each span at most one copy, the same bound the copies already have between them. A shader with a single table call links word for word as before.

  • ScalarCall: two table calls, the first with three 0x3001-dword copies and a small one (36,890 dwords). The second call reaches its chain through the 5 relays of the first call's copies, returns to the instruction after it the same way, and the graph built from the linked code has its call and return edges. Without the fix the same case throws the table callees of the call at program counter 0x0000004c lands out of the 16-bit branch range.
  • RTX 4090 (NVIDIA 616.92, Windows 11, WinLibs GCC 15.2), head e5a02bd48: scalar_call, agc_shader_control_flow (all three variants) and agc_driver_graphics pass.
  • Ghostrunner (PPSA03685 01.000.005) on the RTX 4090, with this commit on my local integration branch (the tree from the description): shader 0x200c520000 now links both table calls you hit, at 0x434 and 0xc84, and stops on a third call at 0x89c. FindTableCalls doesn't take that one: its table's descriptor is the first four dwords of an s_load_dwordx8 s[88:95], s[0:1] whose pointer pair is itself loaded, and the matcher accepts only an s_load_dwordx4 from a user-data or literal pointer pair. So the graph builder throws computed/data-dependent s_swappc_b64 call target at program counter 0x0000089c is not statically resolvable, which the range errors hid until now. That shape is a separate change from this PR.

AI-assisted: yes (Claude Code)

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6326358041 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9068#issuecomment-6326358041). Originally posted by [frankea](https://github.com/frankea) on 2026-10-21T20:55:44Z. --- @xXPBSXx thanks, your reading is right. `LinkCalls` appended each table call's chain and copies after the previous call's, so the second call's branch to its chain, and its return, had to cross all of the first call's copies (about 37K dwords in your shader). That is the limit the description listed as still throwing. e5a02bd48 (pushed here) removes it. Before the first of an earlier call's copies and after each one, `LinkCalls` now leaves a pair of `s_branch` relays for every later call: one forward toward that call's chain, one back toward the instruction after the call. A later call reaches its chain and returns in hops that each span at most one copy, the same bound the copies already have between them. A shader with a single table call links word for word as before. - `ScalarCall`: two table calls, the first with three 0x3001-dword copies and a small one (36,890 dwords). The second call reaches its chain through the 5 relays of the first call's copies, returns to the instruction after it the same way, and the graph built from the linked code has its call and return edges. Without the fix the same case throws `the table callees of the call at program counter 0x0000004c lands out of the 16-bit branch range`. - RTX 4090 (NVIDIA 616.92, Windows 11, WinLibs GCC 15.2), head e5a02bd48: `scalar_call`, `agc_shader_control_flow` (all three variants) and `agc_driver_graphics` pass. - Ghostrunner (PPSA03685 01.000.005) on the RTX 4090, with this commit on my local integration branch (the tree from the description): shader 0x200c520000 now links both table calls you hit, at 0x434 and 0xc84, and stops on a third call at 0x89c. `FindTableCalls` doesn't take that one: its table's descriptor is the first four dwords of an `s_load_dwordx8 s[88:95], s[0:1]` whose pointer pair is itself loaded, and the matcher accepts only an `s_load_dwordx4` from a user-data or literal pointer pair. So the graph builder throws `computed/data-dependent s_swappc_b64 call target at program counter 0x0000089c is not statically resolvable`, which the range errors hid until now. That shape is a separate change from this PR. AI-assisted: yes (Claude Code)
frankea commented 2026-10-22 01:20:12 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by frankea on 2026-10-22T01:20:12Z.


@klosixgaming-arch credit where it's due: on #7413 (comment) you proposed inserting intermediate s_branch relay hops, instead of far calls, whenever a callee or dispatch target is out of branch range. The relays in e5a02bd48 are that idea, applied to a later table call's branch to its chain and its return. I only found your comment after writing them. Thanks.

AI-assisted: yes (Claude Code)

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6330349460 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9068#issuecomment-6330349460). Originally posted by [frankea](https://github.com/frankea) on 2026-10-22T01:20:12Z. --- @klosixgaming-arch credit where it's due: on #7413 ([comment](https://github.com/boykopovar/AnyPS5/pull/7413#issuecomment-6266880776)) you proposed inserting intermediate `s_branch` relay hops, instead of far calls, whenever a callee or dispatch target is out of branch range. The relays in e5a02bd48 are that idea, applied to a later table call's branch to its chain and its return. I only found your comment after writing them. Thanks. AI-assisted: yes (Claude Code)
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!9068
No description provided.