fix(agc): wait for a shader's registered preparation before taking the transaction lock #11873

Merged
JJenkx merged 1 commit from upstream-pr/11873 into main 2026-11-01 06:31:15 +00:00
JJenkx commented 2026-10-27 11:02:17 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by JJenkx on 2026-10-27T11:02:17Z.


What

ShaderPreparationTransaction serializes its writers with one static mutex (State::writers). The first Read, Edit or Hold of a transaction takes it, and the transaction keeps it until it ends. Read and Edit then call AwaitRegisteredPreparation for the shader. That call waits, possibly for seconds, for the shader's background registration preparation when it is still pending.

A thread whose shader is still queued for preparation therefore holds the global writers mutex while it waits. Every other thread that opens a transaction queues behind it, even for shaders that are already prepared. Such threads include sceAgcCreateShader → Driver::RegisterShader → Hold, and AgcDriverResolveShaderAbi → Read.

With this change, Read and Edit first wait for the shader's registered preparation under that shader's own mutex, when the transaction does not hold the writers mutex yet. Only then do they take the writers mutex. The existing wait inside the locked section stays. It now finds the preparation settled, except when a preparation was queued again in between, in which case it waits as before. A transaction that already holds the writers mutex, at its second and later shaders, is unchanged.

Why

Echoes of the End (PPSA25636) from empty shader and pipeline caches. During loading, the registration preparation pool holds a backlog of roughly 250-300 tasks (APS5_TRACE_PREPARATION_WAIT=1).

  • An all-thread stack sample at 26 s showed 12 game threads blocked on this mutex: 4 in RegisterShader → Hold and 8 in ResolveShaderAbi → Read.

Tested

  • PreparedShaders gains PendingPreparationOutsideTransactionLock:

    • One thread opens a transaction and reads a shader whose preparation is pending.
    • A second transaction on another thread reads a prepared shader.
    • The second must finish within 2 s while the first is still waiting.
    • The pending preparation is then settled, and both transactions commit.
  • Focused Vulkan gate. Machine: Linux 7.2.9, RX 6650 XT, RADV (Mesa 26.2.4). Command: ANYPS5_REQUIRE_VULKAN=1 ANYPS5_NO_SHADER_CACHE=1 VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation VK_LOADER_DEBUG=layer ctest -V -j1 -R '^agc_driver_prepared_shaders'.

    arm head result validation layer inserted VUIDs
    main 3fea2a937ba 22/22 49 0
    this branch with ShaderRegistry.cpp restored to main (new test only) bcaac162594 18/22: agc_driver_prepared_shaders, _indirect, _uncached and _debug fail with "a transaction on a prepared shader waited for another transaction's pending registration preparation" 49 0
    this branch bcaac162594 22/22 49 0

    Logs: https://github.com/JJenkx/AnyPS5/releases/tag/transaction-lock-evidence

  • This PR makes no performance claim. The Echoes observations above (the stack sample) explain why the convoy was found; they are not a measured speed result. Echoes still crashes about 30 s into a first launch from empty caches with this change alone.

Checklist

  • Based on current main; no other open PR implements the same functions. #6867 also changes ShaderRegistry.cpp and PreparedShaders.cpp, in different functions: it narrows the submit-time preparation wait in Driver::Submit. The two changes are independent.
  • 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 unimplemented path added)
  • 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 (not applicable)
  • Generic behaviour, not specific to one title
  • Depends on: none
  • Fixes: none
  • Unblocks: none claimed
  • GPUs tested: AMD Radeon RX 6650 XT, RADV Mesa 26.2.4, Linux 7.2.9
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5032795103 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/11873). Originally posted by [JJenkx](https://github.com/JJenkx) on 2026-10-27T11:02:17Z. --- ### What `ShaderPreparationTransaction` serializes its writers with one static mutex (`State::writers`). The first `Read`, `Edit` or `Hold` of a transaction takes it, and the transaction keeps it until it ends. `Read` and `Edit` then call `AwaitRegisteredPreparation` for the shader. That call waits, possibly for seconds, for the shader's background registration preparation when it is still pending. A thread whose shader is still queued for preparation therefore holds the global writers mutex while it waits. Every other thread that opens a transaction queues behind it, even for shaders that are already prepared. Such threads include `sceAgcCreateShader` → `Driver::RegisterShader` → `Hold`, and `AgcDriverResolveShaderAbi` → `Read`. With this change, `Read` and `Edit` first wait for the shader's registered preparation under that shader's own mutex, when the transaction does not hold the writers mutex yet. Only then do they take the writers mutex. The existing wait inside the locked section stays. It now finds the preparation settled, except when a preparation was queued again in between, in which case it waits as before. A transaction that already holds the writers mutex, at its second and later shaders, is unchanged. ### Why Echoes of the End (PPSA25636) from empty shader and pipeline caches. During loading, the registration preparation pool holds a backlog of roughly 250-300 tasks (`APS5_TRACE_PREPARATION_WAIT=1`). - An all-thread stack sample at 26 s showed 12 game threads blocked on this mutex: 4 in `RegisterShader` → `Hold` and 8 in `ResolveShaderAbi` → `Read`. ### Tested - `PreparedShaders` gains `PendingPreparationOutsideTransactionLock`: - One thread opens a transaction and reads a shader whose preparation is pending. - A second transaction on another thread reads a prepared shader. - The second must finish within 2 s while the first is still waiting. - The pending preparation is then settled, and both transactions commit. - Focused Vulkan gate. Machine: Linux 7.2.9, RX 6650 XT, RADV (Mesa 26.2.4). Command: `ANYPS5_REQUIRE_VULKAN=1 ANYPS5_NO_SHADER_CACHE=1 VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation VK_LOADER_DEBUG=layer ctest -V -j1 -R '^agc_driver_prepared_shaders'`. | arm | head | result | validation layer inserted | VUIDs | |---|---|---|---|---| | main | `3fea2a937ba` | 22/22 | 49 | 0 | | this branch with `ShaderRegistry.cpp` restored to main (new test only) | `bcaac162594` | 18/22: `agc_driver_prepared_shaders`, `_indirect`, `_uncached` and `_debug` fail with "a transaction on a prepared shader waited for another transaction's pending registration preparation" | 49 | 0 | | this branch | `bcaac162594` | 22/22 | 49 | 0 | Logs: https://github.com/JJenkx/AnyPS5/releases/tag/transaction-lock-evidence - This PR makes no performance claim. The Echoes observations above (the stack sample) explain why the convoy was found; they are not a measured speed result. Echoes still crashes about 30 s into a first launch from empty caches with this change alone. ### Checklist - [x] Based on current `main`; no other open PR implements the same functions. #6867 also changes `ShaderRegistry.cpp` and `PreparedShaders.cpp`, in different functions: it narrows the submit-time preparation wait in `Driver::Submit`. The two changes are independent. - [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 (no unimplemented path added) - [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 (not applicable) - [x] Generic behaviour, not specific to one title - [x] Depends on: none - [x] Fixes: none - [x] Unblocks: none claimed - [x] GPUs tested: AMD Radeon RX 6650 XT, RADV Mesa 26.2.4, Linux 7.2.9 - [x] AI-assisted: yes
MarcZX10 commented 2026-10-27 11:12:34 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-27T11:12:34Z.

Review: CHANGES_REQUESTED


The synchronization change and the new concurrency regression test look internally consistent on source review; the reported focused Vulkan gate also includes main, test-only and full-change arms with 49 layer insertions and zero VUIDs. I have not yet completed the local exact-head build/test, which is still running.\n\nOne repository requirement is not met by the report as written: CONTRIBUTING.md says a PR claiming a speed change must include valid aps5-summary.jsonl lines for main and the change from the same machine and scene, with both traces attached as described by the Performance report form. The description gives a quantified before/after compute-queue duration (1,420,772 µs versus 19,158/21,088 µs) and a dramatic per-submission improvement, but the linked release contains Vulkan test logs only; it has no summary lines or trace captures. Please add the paired performance-report artifacts, or remove the before/after speed claim and keep the report to the correctness/concurrency evidence.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5666130010 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/11873#pullrequestreview-5666130010). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-27T11:12:34Z. **Review:** CHANGES_REQUESTED --- The synchronization change and the new concurrency regression test look internally consistent on source review; the reported focused Vulkan gate also includes main, test-only and full-change arms with 49 layer insertions and zero VUIDs. I have not yet completed the local exact-head build/test, which is still running.\n\nOne repository requirement is not met by the report as written: `CONTRIBUTING.md` says a PR claiming a speed change must include valid `aps5-summary.jsonl` lines for main and the change from the same machine and scene, with both traces attached as described by the Performance report form. The description gives a quantified before/after compute-queue duration (1,420,772 µs versus 19,158/21,088 µs) and a dramatic per-submission improvement, but the linked release contains Vulkan test logs only; it has no summary lines or trace captures. Please add the paired performance-report artifacts, or remove the before/after speed claim and keep the report to the correctness/concurrency evidence.
MarcZX10 commented 2026-10-27 12:17:31 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-27T12:17:31Z.

Review: APPROVED


The requested change is addressed: the description no longer claims a measured speedup, and it now explicitly says there is no performance claim. The synchronization code and regression remain unchanged from the reviewed head. The new test checks that a transaction on a prepared shader completes while another waits on a pending registration; the linked RX 6650 XT gate logs provide main, test-only negative-control, and full-change arms. I checked those logs previously: main and full-change pass 22/22, the test-only arm fails exactly the four modes containing the regression assertion, the validation layer is inserted, and no VUID is reported.

I found no blocking source issue. My local exact-head test could not provide a GPU execution result: native CTest stops at this host's AVX2/FMA guard, and the QEMU/llvmpipe attempt failed on guest-memory readability, so I do not count either as a pass. The author-reported hardware gate is the successful runtime evidence. Hosted Windows, conventions, intake, labels, and report checks pass; the Linux product job is skipped by path policy.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5666892500 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/11873#pullrequestreview-5666892500). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-27T12:17:31Z. **Review:** APPROVED --- The requested change is addressed: the description no longer claims a measured speedup, and it now explicitly says there is no performance claim. The synchronization code and regression remain unchanged from the reviewed head. The new test checks that a transaction on a prepared shader completes while another waits on a pending registration; the linked RX 6650 XT gate logs provide main, test-only negative-control, and full-change arms. I checked those logs previously: main and full-change pass 22/22, the test-only arm fails exactly the four modes containing the regression assertion, the validation layer is inserted, and no VUID is reported. I found no blocking source issue. My local exact-head test could not provide a GPU execution result: native CTest stops at this host's AVX2/FMA guard, and the QEMU/llvmpipe attempt failed on guest-memory readability, so I do not count either as a pass. The author-reported hardware gate is the successful runtime evidence. Hosted Windows, conventions, intake, labels, and report checks pass; the Linux product job is skipped by path policy.
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!11873
No description provided.