fix(shader): verify materialized snapshot identity before memo reuse #4757

Closed
Ondra97 wants to merge 1 commit from upstream-pr/4757 into main
Ondra97 commented 2026-10-12 20:55:31 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Ondra97 on 2026-10-12T20:55:31Z.


What

Two different resource captures can have the same snapshot hash. The result memo accepted that hash as identity and returned the first capture's push constants for the second request. A CPU-only pixel-shader regression uses the colliding user-data pairs {0x252becb5, 0} and {0xb3d4134a, 0x94fffe4d}: on main the second request receives 0x252becb5 instead of 0xb3d4134a.

Keep the hash as a lookup prefilter, but require equality with an owned resource snapshot and the request state used to populate the result. Check both the initial lookup and the second lookup after materialization outside the mutex. A collision replaces the memo entry without changing already returned immutable results. The existing admission policy and 256-entry limit remain unchanged; #1298 proposes a separate admission policy in the old materialization path and does not fix snapshot identity.

The tradeoff is memory: with MinGW GCC 15.2 the inline entry grows from 32 to 280 bytes, at most about 62 KiB extra per source at the 256-entry limit, plus owned vector payloads and allocation overhead. Copies occur on insertion; matching hits do not allocate. No shader instruction semantics, cache format or NID exports change.

Tested

Windows, MinGW-w64 GCC 15.2, Release, BUILD_TESTING=ON. Full build exits 0 and the 14 relevant CPU shader/patched-library tests pass (0.53 s, no skips) on main c952c2b0e plus this commit. The new memo case takes 0.03 s. Registration finds 730 built test executables, none unregistered, and 1011 CTests. The new agc_shader_snapshot_memo case covers A -> B -> A, repeated-hit identity, retained-result immutability and 16 synchronized concurrent collision pairs, comparing the values to uncached references. It fails on the original recompiler and passes with this change. Tests disable the disk cache and use the existing Prepare/CaptureResources/MaterializeShader path.

Test-executable registration and patched/unpatched Driver export audits pass; no ResultMemoEntry, Matches or default-equality helpers become NIDs. git diff --check and the committed-change conventions check pass. No game, console, Vulkan device or performance workload was run; no title unblock or speed improvement is claimed.

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
  • Unblocks: none
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4834888954 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4757). Originally posted by [Ondra97](https://github.com/Ondra97) on 2026-10-12T20:55:31Z. --- ### What Two different resource captures can have the same snapshot hash. The result memo accepted that hash as identity and returned the first capture's push constants for the second request. A CPU-only pixel-shader regression uses the colliding user-data pairs `{0x252becb5, 0}` and `{0xb3d4134a, 0x94fffe4d}`: on main the second request receives `0x252becb5` instead of `0xb3d4134a`. Keep the hash as a lookup prefilter, but require equality with an owned resource snapshot and the request state used to populate the result. Check both the initial lookup and the second lookup after materialization outside the mutex. A collision replaces the memo entry without changing already returned immutable results. The existing admission policy and 256-entry limit remain unchanged; #1298 proposes a separate admission policy in the old materialization path and does not fix snapshot identity. The tradeoff is memory: with MinGW GCC 15.2 the inline entry grows from 32 to 280 bytes, at most about 62 KiB extra per source at the 256-entry limit, plus owned vector payloads and allocation overhead. Copies occur on insertion; matching hits do not allocate. No shader instruction semantics, cache format or NID exports change. ### Tested Windows, MinGW-w64 GCC 15.2, Release, BUILD_TESTING=ON. Full build exits 0 and the 14 relevant CPU shader/patched-library tests pass (0.53 s, no skips) on main c952c2b0e plus this commit. The new memo case takes 0.03 s. Registration finds 730 built test executables, none unregistered, and 1011 CTests. The new `agc_shader_snapshot_memo` case covers A -> B -> A, repeated-hit identity, retained-result immutability and 16 synchronized concurrent collision pairs, comparing the values to uncached references. It fails on the original recompiler and passes with this change. Tests disable the disk cache and use the existing Prepare/CaptureResources/MaterializeShader path. Test-executable registration and patched/unpatched Driver export audits pass; no ResultMemoEntry, Matches or default-equality helpers become NIDs. `git diff --check` and the committed-change conventions check pass. No game, console, Vulkan device or performance workload was run; no title unblock or speed improvement is claimed. ### 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] Unblocks: none - [x] AI-assisted: yes
github-actions[bot] commented 2026-10-13 01:56:35 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-13T01:56:35Z.


Other open pull requests touch the same code. Merging one will break the other, or both implement the same thing. If this one builds on them, list them in Depends on.

Pull request Conflicting files (lines on main) Same exports Same new files
#757 core/shader/recompiler/IntermediateRepresentation/include/IntermediateRepresentation/IrMetadata/ResourcePlan.hpp L45
#758 core/shader/recompiler/IntermediateRepresentation/include/IntermediateRepresentation/IrMetadata/ResourcePlan.hpp L45
#5473 core/libs/prx/libSceAgcDriver/CMakeLists.txt L1223
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6136389226 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4757#issuecomment-6136389226). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-10-13T01:56:35Z. --- <!-- pr-overlap --> Other open pull requests touch the same code. Merging one will break the other, or both implement the same thing. If this one builds on them, list them in **Depends on**. | Pull request | Conflicting files (lines on `main`) | Same exports | Same new files | |---|---|---|---| | #757 | `core/shader/recompiler/IntermediateRepresentation/include/IntermediateRepresentation/IrMetadata/ResourcePlan.hpp` [L45](https://github.com/boykopovar/AnyPS5/blob/0df6e295c52e7cd6afa394ed35513bd252045c74/core/shader/recompiler/IntermediateRepresentation/include/IntermediateRepresentation/IrMetadata/ResourcePlan.hpp#L45) | | | | #758 | `core/shader/recompiler/IntermediateRepresentation/include/IntermediateRepresentation/IrMetadata/ResourcePlan.hpp` [L45](https://github.com/boykopovar/AnyPS5/blob/0df6e295c52e7cd6afa394ed35513bd252045c74/core/shader/recompiler/IntermediateRepresentation/include/IntermediateRepresentation/IrMetadata/ResourcePlan.hpp#L45) | | | | #5473 | `core/libs/prx/libSceAgcDriver/CMakeLists.txt` [L1223](https://github.com/boykopovar/AnyPS5/blob/0df6e295c52e7cd6afa394ed35513bd252045c74/core/libs/prx/libSceAgcDriver/CMakeLists.txt#L1223) | | |
xXPBSXx commented 2026-10-13 03:27:11 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by xXPBSXx on 2026-10-13T03:27:11Z.


Windows test (MinGW GCC 15.2, Release, Windows 10, RTX 4080 SUPER). Built on current main 0c3dc9ca2 together with eight other open GPU PRs (their own commits cherry-picked, no merge commits), so only the tests that belong to this PR say something about it:

  • tests/ShaderMemory.cpp (agc_shader_memory): 3 of 3 runs passed.

Not tested: no game run. The batch was built once, not per PR; it had no failures, so no bisect was needed.

AI-assisted (Claude Code).

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6137528808 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4757#issuecomment-6137528808). Originally posted by [xXPBSXx](https://github.com/xXPBSXx) on 2026-10-13T03:27:11Z. --- Windows test (MinGW GCC 15.2, Release, Windows 10, RTX 4080 SUPER). Built on current main `0c3dc9ca2` together with eight other open GPU PRs (their own commits cherry-picked, no merge commits), so only the tests that belong to this PR say something about it: - `tests/ShaderMemory.cpp` (`agc_shader_memory`): 3 of 3 runs passed. Not tested: no game run. The batch was built once, not per PR; it had no failures, so no bisect was needed. AI-assisted (Claude Code).
oneandonlydean commented 2026-10-14 02:32:39 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-14T02:32:39Z.


Closing: no title is known to reach this. With the queue at 700+ open PRs, please hold off on new PRs that don't name a title needing the change until it's down. If a title stops on this on current main, reopen with the title ID and the log line.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6159584326 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4757#issuecomment-6159584326). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-10-14T02:32:39Z. --- Closing: no title is known to reach this. With the queue at 700+ open PRs, please hold off on new PRs that don't name a title needing the change until it's down. If a title stops on this on current main, reopen with the title ID and the log line.

Pull request closed

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!4757
No description provided.