perf(agc): build compute shaders' front end when they are registered #473

Closed
adriadam10 wants to merge 2 commits from upstream-pr/473 into main
adriadam10 commented 2026-10-03 17:06:34 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-03T17:06:34Z.


What

The first commit is #471; this pull request is the second one.

Builds a compute shader's front end when the title registers it, on a worker thread, instead of at its first dispatch (the move discussed in #361, for compute only). With #471 the first variant compiled for a cache entry takes the program the entry was built with, so a dispatch of a prepared shader only specializes and emits.

  • RegisterShader hands compute binaries (type 0) to ShaderPreparation. It predicts the request a dispatch will make from the header: the code at its registered address, the user SGPR count from COMPUTE_PGM_RSRC2, the stage info from COMPUTE_NUM_THREAD_*/COMPUTE_PGM_RSRC2 (DecodeComputeStageInfo, as the dispatch does), the wave size from specials->dispatch_modifier bit 15 (CS_W32_EN, the bit the dispatch reads from its initiator), and the current device's target.
  • The worker calls the new ShaderRecompiler::PrepareSource, which runs getSource. A dispatch that arrives while the entry is being built waits on the entry's mutex, as two dispatches do today, so it never skips or duplicates work.
  • The prediction is only a cache key. A dispatch that differs (another wave size, a partial group, a code offset inside the binary) misses that entry and builds its own as today; the output does not change either way. A failed build is memoized in the entry like a dispatch's and rethrown by the dispatch that uses it; preparation itself never reports errors.
  • Skipped when there is no device yet (the target is part of the key) or when the header has no specials or SH registers. APS5_NO_SHADER_PREPARATION=1 turns it off. The worker is stopped and joined in Driver::stop, before the device is released.
  • ShaderRecompiler::FrontEndBuilds() counts front-end runs; the test uses it.

Not covered: whether dispatch_modifier is what titles pass to sceAgcCbDispatch. I found no reference that confirms it; the name and the matching bit are the evidence. If it is not, entries are built for the wrong wave size and dispatches behave as today. Pixel and vertex shaders depend on draw state and are left for later.

Tested

  • Linux (CachyOS), GCC 16, Release, lavapipe/RADV. Full ctest passes except agc_driver_pixel_helper_lanes, which fails the same way on main on this machine.
  • New agc_driver_shader_preparation (execution test, skips without Vulkan): registers a compute shader through AgcDriverRegisterShader_nid_postfix, waits for exactly one front end, dispatches it through an ACB and checks the stored values and that the dispatch ran no front end; then dispatches it at the other wave size and checks it builds its own and still stores the right values. With APS5_NO_SHADER_PREPARATION=1 it fails at the wait; with a header that predicts the wrong wave size it fails with "ran the front end again". Passes 3/3 runs, and under AddressSanitizer.
  • Not tested with a title. On a title, comparing FrontEndBuilds() with and without APS5_NO_SHADER_PREPARATION=1 over the same scene would show how many dispatches it covers.

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 in TechnicalDebt
  • 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: #471
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4728234424 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/473). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-03T17:06:34Z. --- ### What The first commit is #471; this pull request is the second one. Builds a compute shader's front end when the title registers it, on a worker thread, instead of at its first dispatch (the move discussed in #361, for compute only). With #471 the first variant compiled for a cache entry takes the program the entry was built with, so a dispatch of a prepared shader only specializes and emits. - `RegisterShader` hands compute binaries (type 0) to `ShaderPreparation`. It predicts the request a dispatch will make from the header: the code at its registered address, the user SGPR count from `COMPUTE_PGM_RSRC2`, the stage info from `COMPUTE_NUM_THREAD_*`/`COMPUTE_PGM_RSRC2` (`DecodeComputeStageInfo`, as the dispatch does), the wave size from `specials->dispatch_modifier` bit 15 (`CS_W32_EN`, the bit the dispatch reads from its initiator), and the current device's target. - The worker calls the new `ShaderRecompiler::PrepareSource`, which runs `getSource`. A dispatch that arrives while the entry is being built waits on the entry's mutex, as two dispatches do today, so it never skips or duplicates work. - The prediction is only a cache key. A dispatch that differs (another wave size, a partial group, a code offset inside the binary) misses that entry and builds its own as today; the output does not change either way. A failed build is memoized in the entry like a dispatch's and rethrown by the dispatch that uses it; preparation itself never reports errors. - Skipped when there is no device yet (the target is part of the key) or when the header has no specials or SH registers. `APS5_NO_SHADER_PREPARATION=1` turns it off. The worker is stopped and joined in `Driver::stop`, before the device is released. - `ShaderRecompiler::FrontEndBuilds()` counts front-end runs; the test uses it. Not covered: whether `dispatch_modifier` is what titles pass to `sceAgcCbDispatch`. I found no reference that confirms it; the name and the matching bit are the evidence. If it is not, entries are built for the wrong wave size and dispatches behave as today. Pixel and vertex shaders depend on draw state and are left for later. ### Tested - Linux (CachyOS), GCC 16, Release, lavapipe/RADV. Full ctest passes except `agc_driver_pixel_helper_lanes`, which fails the same way on `main` on this machine. - New `agc_driver_shader_preparation` (execution test, skips without Vulkan): registers a compute shader through `AgcDriverRegisterShader_nid_postfix`, waits for exactly one front end, dispatches it through an ACB and checks the stored values and that the dispatch ran no front end; then dispatches it at the other wave size and checks it builds its own and still stores the right values. With `APS5_NO_SHADER_PREPARATION=1` it fails at the wait; with a header that predicts the wrong wave size it fails with "ran the front end again". Passes 3/3 runs, and under AddressSanitizer. - Not tested with a title. On a title, comparing `FrontEndBuilds()` with and without `APS5_NO_SHADER_PREPARATION=1` over the same scene would show how many dispatches it covers. ### 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 in [TechnicalDebt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/TechnicalDebt.md#silent-stubs) - [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: #471 - [x] AI-assisted: yes
oneandonlydean commented 2026-10-04 07:10:36 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-04T07:10:36Z.


Thanks for this. Since my #457 landed, the build checks every driver call into the recompiler against a reviewed list, and main plus this PR now stops at the shader cache version step:

core/libs/prx/libSceAgcDriver/Execution/src/Driver/Shaders/ShaderPreparation.cpp uses ShaderRecompiler::PrepareSource, which has not been reviewed for the shader disk cache key

PrepareSource only fills the source cache for the same request a later Recompile uses, so I think adding it to shaderCacheReviewedNames in core/shader/recompiler/CMakeLists.txt is all it needs. Could you add it?

Separately, @boykopovar, the background worker that builds compute shaders at registration is a design call for you. It reads specials->dispatch_modifier bit 15 as wave32, which the PR says is unconfirmed; a wrong guess only costs a second build, and the test covers that case.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:5977589784 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/473#issuecomment-5977589784). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-10-04T07:10:36Z. --- Thanks for this. Since my #457 landed, the build checks every driver call into the recompiler against a reviewed list, and main plus this PR now stops at the shader cache version step: ``` core/libs/prx/libSceAgcDriver/Execution/src/Driver/Shaders/ShaderPreparation.cpp uses ShaderRecompiler::PrepareSource, which has not been reviewed for the shader disk cache key ``` `PrepareSource` only fills the source cache for the same request a later `Recompile` uses, so I think adding it to `shaderCacheReviewedNames` in `core/shader/recompiler/CMakeLists.txt` is all it needs. Could you add it? Separately, @boykopovar, the background worker that builds compute shaders at registration is a design call for you. It reads `specials->dispatch_modifier` bit 15 as wave32, which the PR says is unconfirmed; a wrong guess only costs a second build, and the test covers that case.
adriadam10 commented 2026-10-04 07:16:57 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-04T07:16:57Z.


Thanks! Added PrepareSource to shaderCacheReviewedNames and rebased onto main.

The rebase also showed a real break: since #316 a wave32 dispatch recompiles for ComputeTarget(32) (32-wide subgroups), but the preparation still used Target(). The source key then differed in the target, so the prepared front end was never reused, and agc_driver_shader_preparation caught it (the dispatch of a prepared shader ran the front end again). The preparation now uses ComputeTarget(entry.waveSize), the same as Dispatch.cpp. Release build passes, and all agc_ tests pass on RADV except agc_driver_pixel_helper_lanes, which fails on local RADV on main too. Still Depends on #471.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:5977631328 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/473#issuecomment-5977631328). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-04T07:16:57Z. --- Thanks! Added `PrepareSource` to `shaderCacheReviewedNames` and rebased onto main. The rebase also showed a real break: since #316 a wave32 dispatch recompiles for `ComputeTarget(32)` (32-wide subgroups), but the preparation still used `Target()`. The source key then differed in the target, so the prepared front end was never reused, and `agc_driver_shader_preparation` caught it (`the dispatch of a prepared shader ran the front end again`). The preparation now uses `ComputeTarget(entry.waveSize)`, the same as `Dispatch.cpp`. Release build passes, and all `agc_` tests pass on RADV except `agc_driver_pixel_helper_lanes`, which fails on local RADV on main too. Still `Depends on #471`.
oneandonlydean commented 2026-10-04 22:38:07 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-04T22:38:07Z.


Thanks, both fixes look right to me. PrepareSource is in shaderCacheReviewedNames, and the preparation now builds for ComputeTarget(entry.waveSize), the same target Dispatch.cpp uses, so a wave32 dispatch finds the prepared front end again. Merged with current main it builds and the full ctest passes here, including agc_driver_shader_preparation, agc_driver_wave32_subgroup and agc_shader_disk_cache. CI is green on both jobs.

The first commit is the same patch as #471, which has since merged, so that part drops out on merge. Note that #469 also edits Driver::RegisterShader in ShaderRegistry.cpp; whichever lands second needs a small rebase there (keep the Enqueue and add the null program insert after it).

From my side only the design question is left: @boykopovar, the background worker that builds compute shaders at registration (reading specials->dispatch_modifier bit 15 as wave32) is your call.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:5985219930 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/473#issuecomment-5985219930). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-10-04T22:38:07Z. --- Thanks, both fixes look right to me. `PrepareSource` is in `shaderCacheReviewedNames`, and the preparation now builds for `ComputeTarget(entry.waveSize)`, the same target `Dispatch.cpp` uses, so a wave32 dispatch finds the prepared front end again. Merged with current main it builds and the full ctest passes here, including `agc_driver_shader_preparation`, `agc_driver_wave32_subgroup` and `agc_shader_disk_cache`. CI is green on both jobs. The first commit is the same patch as #471, which has since merged, so that part drops out on merge. Note that #469 also edits `Driver::RegisterShader` in `ShaderRegistry.cpp`; whichever lands second needs a small rebase there (keep the `Enqueue` and add the null program insert after it). From my side only the design question is left: @boykopovar, the background worker that builds compute shaders at registration (reading `specials->dispatch_modifier` bit 15 as wave32) is your call.
adriadam10 commented 2026-10-05 09:01:51 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-05T09:01:51Z.


Rebased on current main after #610, #471 and #469:

  • Driver::RegisterShader in ShaderRegistry.cpp: kept the Enqueue and added the null pixel program insert after it, as suggested.
  • All agc_ tests pass on RADV except agc_driver_pixel_helper_lanes, agc_driver_pixel_lds and agc_driver_pixel_lds_lane_slots, which fail the same on current main here.
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:5991349314 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/473#issuecomment-5991349314). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-05T09:01:51Z. --- Rebased on current `main` after #610, #471 and #469: - `Driver::RegisterShader` in `ShaderRegistry.cpp`: kept the `Enqueue` and added the null pixel program insert after it, as suggested. - All `agc_` tests pass on RADV except `agc_driver_pixel_helper_lanes`, `agc_driver_pixel_lds` and `agc_driver_pixel_lds_lane_slots`, which fail the same on current `main` here.
adriadam10 commented 2026-10-05 11:03:30 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-05T11:03:30Z.


On standby: #639 moves the whole recompilation to Driver::RegisterShader and makes this one obsolete, so please don't spend review time on it. I'll close it when #639 lands.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:5993153800 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/473#issuecomment-5993153800). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-05T11:03:30Z. --- On standby: #639 moves the whole recompilation to `Driver::RegisterShader` and makes this one obsolete, so please don't spend review time on it. I'll close it when #639 lands.
adriadam10 commented 2026-10-05 11:17:17 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-05T11:17:17Z.


Closing: #639 moves the whole shader recompilation to Driver::RegisterShader, compute and graphics, with no per-resource variants, which replaces this front-end preparation. See the discussion there.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:5993354920 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/473#issuecomment-5993354920). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-05T11:17:17Z. --- Closing: #639 moves the whole shader recompilation to `Driver::RegisterShader`, compute and graphics, with no per-resource variants, which replaces this front-end preparation. See the discussion there.
boykopovar commented 2026-10-05 11:19:35 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by boykopovar on 2026-10-05T11:19:35Z.


Sorry for the delay in responding. Let's avoid making architectural changes that conflict with shader recompilation during registration. Regarding changes that don't change the architecture, please don't worry about them interfering with #639. If #639 works, I'll fix the conflicts myself.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:5993388221 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/473#issuecomment-5993388221). Originally posted by [boykopovar](https://github.com/boykopovar) on 2026-10-05T11:19:35Z. --- Sorry for the delay in responding. Let's avoid making architectural changes that conflict with shader recompilation during registration. Regarding changes that don't change the architecture, please don't worry about them interfering with #639. If #639 works, I'll fix the conflicts myself.

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