fix(shader): read a MIMG sampler only for ops that sample #22076

Merged
B-Howell merged 1 commit from upstream-pr/22076 into main 2026-11-17 20:59:31 +00:00
B-Howell commented 2026-11-17 04:11:53 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by B-Howell on 2026-11-17T04:11:53Z.


What

The scalar call analysis in GraphBuilder.cpp (readsLinkRegister and readsScalarDword) counted the SSAMP field of every MIMG instruction as a read of four scalar registers. Only sampling ops read a sampler: sample, gather and image_get_lod, the same set DecodeRdnaMimg already range-checks SSAMP for. image_load*, image_store*, image_get_resinfo and the image atomics ignore the field, and compilers leave it 0. So a shader that calls a subroutine with s_call_b64 s[0:1] and then runs image_load_mip with SSAMP 0 was refused with scalar call link register s[0] ... escapes the constant-offset call/return model, although nothing reads s[0:1] after the return.

Changes:

  • DecodeRdnaMimg records whether the op takes a sampler in the new RdnaInstruction::imageSampler. It computed this already as samplerOp.
  • readsLinkRegister and readsScalarDword count SSAMP only when imageSampler is set.
  • pairOverlapsRegister reports no overlap for an empty range. Before, a count of 0 still matched a register one above the link pair.

imageMemoryInfoFromInstruction still reads source2 for every MIMG op, so translation is unchanged.

ScalarCall.cpp adds CheckImageSamplerField. With link pairs s[0:1] and s[2:3], a call returns to a 2D MIMG instruction whose SSAMP is s[0:3]:

  • image_load, image_load_mip, image_store and image_get_resinfo must build;
  • image_sample and image_get_lod must still be refused as link escapes.

Tested

Windows 11 Pro 10.0.26300, WinLibs GCC 15.2.0 r7, Release, NVIDIA GeForce RTX 4080 SUPER (driver 621.14). Run with ANYPS5_REQUIRE_VULKAN=1 ANYPS5_NO_SHADER_CACHE=1 VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation VK_LOADER_DEBUG=layer, using the LunarG layer 1.4.363.1 through VK_ADD_LAYER_PATH:
ctest -R '^(agc_driver_gcn_.*|agc_driver_guarded_null_pointer|agc_driver_guarded_direct_root|agc_gcn_color_target|scalar_call|scalar_swappc|control_flow_graph|image_by|image_msaa_load|agc_driver_scalar_swappc|agc_driver_scalar_swappc_fetch|agc_driver_image_by)$' -E '_trigger$' -V

  • Without the fix (the new test only): 23/24 pass. scalar_call fails with scalar call link register s[0] written at program counter 0x00000000 escapes the constant-offset call/return model (used at program counter 0x00000004), the title's message below.

  • This change: 24/24 pass. scalar_call passes 3 of 3 runs.

  • Validation layer: both logs have 18 Insert instance layer "VK_LAYER_KHRONOS_validation" lines, one per Vulkan test. Neither has was not found but was requested by env var VK_INSTANCE_LAYERS, failed to load or VUID-.

  • Not run: agc_shader_control_flow does not link on Windows here, because objcopy fails on the test executable, as it does for agc_shader_memory.

  • Build tree: the tests ran in my Bloodborne tree. Its GraphBuilder.cpp, RdnaImageOpDecoder.cpp, RdnaInstruction.hpp and ScalarCall.cpp match main's.

  • Title: Bloodborne CUSA00900 01.09 (my own copy), on main f2a25e5b42a plus #17105, #17711, #20156, #21944, #21969, #21977, #22033, #18177 and #22060.

    • Before: 2 of 2 runs stop at about 144-146 s, when character creation opens, on vertex shader 0x2d20eb300: scalar call link register s[0] written at program counter 0x00000008 escapes the constant-offset call/return model (used at program counter 0x00000018). At 0x8 the shader runs s_call_b64 s[0:1] to a subroutine that returns with s_setpc_b64 s[0:1]. At 0x18 it runs image_load_mip with resource s[8:15] and SSAMP 0.
    • After: 2 of 2 runs get past that shader. The next stop is #18177's own refusal, a GCN CMASK that fast clears only some tiles of its color target is not implemented, which is not related to this change.
  • Tested by @sharveshprabhu on eb71f241 merged onto main a72186d3cf: the same ctest filter plus agc_shader_control_flow.*, --repeat until-fail:3, 24/24 on both main and this change on a Radeon 780M (AMD 26.10.2) and an RTX 5060 Laptop (NVIDIA 617.42), Windows 11 26300, WinLibs GCC 15.2.0 r7, validation layer on, 0 VUID-. agc_shader_control_flow and its three variants pass on both GPUs; the objcopy link failure was Windows Defender scanning the new executables (comment).

python3 tools/check_conventions.py --base origin/main passes. tools/pr_overlap.py --branch fix/shader-mimg-sampler-link-reads reports no overlap with open pull requests.

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 and the licence notice of code adapted from upstream (see AGENTS.md)
  • 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: CUSA00900 Bloodborne #3094
  • GPUs tested: NVIDIA GeForce RTX 4080 SUPER, driver 621.14, Windows 11 Pro 10.0.26300
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5350496846 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/22076). Originally posted by [B-Howell](https://github.com/B-Howell) on 2026-11-17T04:11:53Z. --- ### What The scalar call analysis in `GraphBuilder.cpp` (`readsLinkRegister` and `readsScalarDword`) counted the SSAMP field of every MIMG instruction as a read of four scalar registers. Only sampling ops read a sampler: sample, gather and `image_get_lod`, the same set `DecodeRdnaMimg` already range-checks SSAMP for. `image_load*`, `image_store*`, `image_get_resinfo` and the image atomics ignore the field, and compilers leave it 0. So a shader that calls a subroutine with `s_call_b64 s[0:1]` and then runs `image_load_mip` with SSAMP 0 was refused with `scalar call link register s[0] ... escapes the constant-offset call/return model`, although nothing reads `s[0:1]` after the return. Changes: - `DecodeRdnaMimg` records whether the op takes a sampler in the new `RdnaInstruction::imageSampler`. It computed this already as `samplerOp`. - `readsLinkRegister` and `readsScalarDword` count SSAMP only when `imageSampler` is set. - `pairOverlapsRegister` reports no overlap for an empty range. Before, a count of 0 still matched a register one above the link pair. `imageMemoryInfoFromInstruction` still reads `source2` for every MIMG op, so translation is unchanged. `ScalarCall.cpp` adds `CheckImageSamplerField`. With link pairs `s[0:1]` and `s[2:3]`, a call returns to a 2D MIMG instruction whose SSAMP is `s[0:3]`: - `image_load`, `image_load_mip`, `image_store` and `image_get_resinfo` must build; - `image_sample` and `image_get_lod` must still be refused as link escapes. ### Tested Windows 11 Pro 10.0.26300, WinLibs GCC 15.2.0 r7, Release, NVIDIA GeForce RTX 4080 SUPER (driver 621.14). Run with `ANYPS5_REQUIRE_VULKAN=1 ANYPS5_NO_SHADER_CACHE=1 VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation VK_LOADER_DEBUG=layer`, using the LunarG layer 1.4.363.1 through `VK_ADD_LAYER_PATH`: `ctest -R '^(agc_driver_gcn_.*|agc_driver_guarded_null_pointer|agc_driver_guarded_direct_root|agc_gcn_color_target|scalar_call|scalar_swappc|control_flow_graph|image_by|image_msaa_load|agc_driver_scalar_swappc|agc_driver_scalar_swappc_fetch|agc_driver_image_by)$' -E '_trigger$' -V` - **Without the fix (the new test only):** 23/24 pass. `scalar_call` fails with `scalar call link register s[0] written at program counter 0x00000000 escapes the constant-offset call/return model (used at program counter 0x00000004)`, the title's message below. - **This change:** 24/24 pass. `scalar_call` passes 3 of 3 runs. - **Validation layer:** both logs have 18 `Insert instance layer "VK_LAYER_KHRONOS_validation"` lines, one per Vulkan test. Neither has `was not found but was requested by env var VK_INSTANCE_LAYERS`, `failed to load` or `VUID-`. - **Not run:** `agc_shader_control_flow` does not link on Windows here, because `objcopy` fails on the test executable, as it does for `agc_shader_memory`. - **Build tree:** the tests ran in my Bloodborne tree. Its `GraphBuilder.cpp`, `RdnaImageOpDecoder.cpp`, `RdnaInstruction.hpp` and `ScalarCall.cpp` match `main`'s. - **Title:** Bloodborne CUSA00900 01.09 (my own copy), on `main` `f2a25e5b42a` plus #17105, #17711, #20156, #21944, #21969, #21977, #22033, #18177 and #22060. - Before: 2 of 2 runs stop at about 144-146 s, when character creation opens, on vertex shader `0x2d20eb300`: `scalar call link register s[0] written at program counter 0x00000008 escapes the constant-offset call/return model (used at program counter 0x00000018)`. At 0x8 the shader runs `s_call_b64 s[0:1]` to a subroutine that returns with `s_setpc_b64 s[0:1]`. At 0x18 it runs `image_load_mip` with resource `s[8:15]` and SSAMP 0. - After: 2 of 2 runs get past that shader. The next stop is #18177's own refusal, `a GCN CMASK that fast clears only some tiles of its color target is not implemented`, which is not related to this change. - **Tested by @sharveshprabhu on `eb71f241` merged onto `main` `a72186d3cf`:** the same `ctest` filter plus `agc_shader_control_flow.*`, `--repeat until-fail:3`, 24/24 on both `main` and this change on a Radeon 780M (AMD 26.10.2) and an RTX 5060 Laptop (NVIDIA 617.42), Windows 11 26300, WinLibs GCC 15.2.0 r7, validation layer on, 0 `VUID-`. `agc_shader_control_flow` and its three variants pass on both GPUs; the `objcopy` link failure was Windows Defender scanning the new executables ([comment](https://github.com/boykopovar/AnyPS5/pull/22076#issuecomment-6939561924)). `python3 tools/check_conventions.py --base origin/main` passes. `tools/pr_overlap.py --branch fix/shader-mimg-sampler-link-reads` reports no overlap with open pull requests. ### 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) and the licence notice of code adapted from upstream (see AGENTS.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: CUSA00900 Bloodborne #3094 - [x] GPUs tested: NVIDIA GeForce RTX 4080 SUPER, driver 621.14, Windows 11 Pro 10.0.26300 - [x] AI-assisted: yes
MarcZX10 commented 2026-11-17 06:09:36 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-17T06:09:36Z.

Review: APPROVED


Reviewed — looks good

The change stops treating the MIMG sampler field as a scalar read for ops that do not sample, the translator uses that sampler only on the sampling paths, and I found no blocking defect at head eb71f24180239bda2451f1c73cf9ece652e7ef90.

What I ran

  • Head: eb71f24180239bda2451f1c73cf9ece652e7ef90. Base: main. No merged-preview check.
  • Environment: static reading only, in a Linux review environment. Nothing was built or run.
  • Commands (repository reader, pinned to the head):
python3 repo_read.py search samplerOp
python3 repo_read.py search imageSampler
python3 repo_read.py search DecodeRdnaMimg core/shader/recompiler/RdnaDecoder
python3 repo_read.py search sampler core/shader/recompiler/Translation
python3 repo_read.py cat core/shader/recompiler/ControlFlow/src/GraphBuilder.cpp 380 420
python3 repo_read.py cat core/shader/recompiler/ControlFlow/src/GraphBuilder.cpp 640 668
python3 repo_read.py cat core/shader/recompiler/RdnaDecoder/src/RdnaImageOpDecoder.cpp 330 346
python3 repo_read.py cat core/shader/recompiler/Translation/src/MemoryInstructions/ImageInstructions.cpp 9 40
python3 repo_read.py cat core/libs/tests/ScalarCall.cpp 1 40

The author's test runs (ctest on Windows, the LunarG validation layer, Bloodborne) are reported in the description. I did not run them.

Results

Check Result Note
Diff against the description passed Four files, changed hunks read
Translator uses of the sampler field passed Only the sampling paths materialise it
Call-analysis guard for empty ranges passed count != 0u check added
ctest filter from the description not run Windows build, not available here
check_conventions.py not run Not available here

Findings

Blocking

None.

Non-blocking

  • core/libs/tests/ScalarCall.cpp:571 checks only image_sample (0x20) and image_get_lod (0x60) as still refused. Gather ops also set imageSampler through info.gather (core/shader/recompiler/RdnaDecoder/src/RdnaImageOpDecoder.cpp:340), and no new test covers them. A gather op is still refused by the new code path, but a test for it would lock that in.

Sources

  • Empty-range guard: core/shader/recompiler/ControlFlow/src/GraphBuilder.cpp:385 at eb71f24180239bda2451f1c73cf9ece652e7ef90.
  • MIMG source2 count for call analysis: core/shader/recompiler/ControlFlow/src/GraphBuilder.cpp:407 and :655 at the same commit.
  • Sampler flag set from samplerOp: core/shader/recompiler/RdnaDecoder/src/RdnaImageOpDecoder.cpp:340 and :445 at the same commit.
  • Sampler resource used only on sampling paths: core/shader/recompiler/Translation/src/MemoryInstructions/ImageInstructions.cpp:28, :134, :204, :224 at the same commit.
  • New test: core/libs/tests/ScalarCall.cpp:560 to :578 at the same commit.

Not verified

  • The build, the ctest filter, the validation-layer runs and check_conventions.py were not run here. The author's Windows results are not my run.
  • The negative control (without the fix, scalar_call fails) is author-reported only and was not run.
  • Files reviewed: all four in files.json (core/libs/tests/ScalarCall.cpp, core/shader/recompiler/ControlFlow/src/GraphBuilder.cpp, core/shader/recompiler/RdnaDecoder/include/RdnaDecoder/RdnaInstruction.hpp, core/shader/recompiler/RdnaDecoder/src/RdnaImageOpDecoder.cpp). I read their changed hunks with surrounding context, not every line of each file.
  • Excluded by rule: none.
  • Bloodborne before-and-after claims in the description: not verified.
  • ResourceMaterializer.cpp and the SPIR-V image emitter also include the MIMG decoder; I did not check whether they read the sampler field for non-sampling ops.

To get this to a good state

Nothing is required.

Optional: add a gather opcode (for example image_gather4) to the refused list in CheckImageSamplerField.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5933824288 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/22076#pullrequestreview-5933824288). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-11-17T06:09:36Z. **Review:** APPROVED --- # Reviewed — looks good The change stops treating the MIMG sampler field as a scalar read for ops that do not sample, the translator uses that sampler only on the sampling paths, and I found no blocking defect at head `eb71f24180239bda2451f1c73cf9ece652e7ef90`. ## What I ran - Head: `eb71f24180239bda2451f1c73cf9ece652e7ef90`. Base: `main`. No merged-preview check. - Environment: static reading only, in a Linux review environment. Nothing was built or run. - Commands (repository reader, pinned to the head): ``` python3 repo_read.py search samplerOp python3 repo_read.py search imageSampler python3 repo_read.py search DecodeRdnaMimg core/shader/recompiler/RdnaDecoder python3 repo_read.py search sampler core/shader/recompiler/Translation python3 repo_read.py cat core/shader/recompiler/ControlFlow/src/GraphBuilder.cpp 380 420 python3 repo_read.py cat core/shader/recompiler/ControlFlow/src/GraphBuilder.cpp 640 668 python3 repo_read.py cat core/shader/recompiler/RdnaDecoder/src/RdnaImageOpDecoder.cpp 330 346 python3 repo_read.py cat core/shader/recompiler/Translation/src/MemoryInstructions/ImageInstructions.cpp 9 40 python3 repo_read.py cat core/libs/tests/ScalarCall.cpp 1 40 ``` The author's test runs (ctest on Windows, the LunarG validation layer, Bloodborne) are reported in the description. I did not run them. ## Results | Check | Result | Note | | --- | --- | --- | | Diff against the description | passed | Four files, changed hunks read | | Translator uses of the sampler field | passed | Only the sampling paths materialise it | | Call-analysis guard for empty ranges | passed | `count != 0u` check added | | ctest filter from the description | not run | Windows build, not available here | | `check_conventions.py` | not run | Not available here | ## Findings ### Blocking None. ### Non-blocking - `core/libs/tests/ScalarCall.cpp:571` checks only `image_sample` (0x20) and `image_get_lod` (0x60) as still refused. Gather ops also set `imageSampler` through `info.gather` (`core/shader/recompiler/RdnaDecoder/src/RdnaImageOpDecoder.cpp:340`), and no new test covers them. A gather op is still refused by the new code path, but a test for it would lock that in. ## Sources - Empty-range guard: `core/shader/recompiler/ControlFlow/src/GraphBuilder.cpp:385` at `eb71f24180239bda2451f1c73cf9ece652e7ef90`. - MIMG source2 count for call analysis: `core/shader/recompiler/ControlFlow/src/GraphBuilder.cpp:407` and `:655` at the same commit. - Sampler flag set from `samplerOp`: `core/shader/recompiler/RdnaDecoder/src/RdnaImageOpDecoder.cpp:340` and `:445` at the same commit. - Sampler resource used only on sampling paths: `core/shader/recompiler/Translation/src/MemoryInstructions/ImageInstructions.cpp:28`, `:134`, `:204`, `:224` at the same commit. - New test: `core/libs/tests/ScalarCall.cpp:560` to `:578` at the same commit. ## Not verified - The build, the ctest filter, the validation-layer runs and `check_conventions.py` were not run here. The author's Windows results are not my run. - The negative control (without the fix, `scalar_call` fails) is author-reported only and was not run. - Files reviewed: all four in `files.json` (`core/libs/tests/ScalarCall.cpp`, `core/shader/recompiler/ControlFlow/src/GraphBuilder.cpp`, `core/shader/recompiler/RdnaDecoder/include/RdnaDecoder/RdnaInstruction.hpp`, `core/shader/recompiler/RdnaDecoder/src/RdnaImageOpDecoder.cpp`). I read their changed hunks with surrounding context, not every line of each file. - Excluded by rule: none. - Bloodborne before-and-after claims in the description: not verified. - `ResourceMaterializer.cpp` and the SPIR-V image emitter also include the MIMG decoder; I did not check whether they read the sampler field for non-sampling ops. ## To get this to a good state Nothing is required. Optional: add a gather opcode (for example `image_gather4`) to the refused list in `CheckImageSamplerField`.
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!22076
No description provided.