Skip to content

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

Closed
adriadam10 wants to merge 2 commits into
boykopovar:mainfrom
adriadam10:perf/compute-front-end-at-registration
Closed

adriadam10 wants to merge 2 commits into
boykopovar:mainfrom
adriadam10:perf/compute-front-end-at-registration

Conversation

@adriadam10

@adriadam10 adriadam10 commented Oct 3, 2026 •

Copy link
Copy Markdown
Collaborator

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: perf(shader): build a shader's front end once on first use #471
  • AI-assisted: yes

@oneandonlydean

Copy link
Copy Markdown
Collaborator

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

Copy link
Copy Markdown
Collaborator Author

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.

@adriadam10
adriadam10 force-pushed the perf/compute-front-end-at-registration branch from 0260c7c to 33555ae Compare October 4, 2026 07:16
@oneandonlydean

Copy link
Copy Markdown
Collaborator

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.

@github-actions github-actions Bot added the needs rebase Conflicts with main, rebase needed label Oct 5, 2026
adriadam10 and others added 2 commits October 5, 2026 10:58
Co-Authored-By: Claude <noreply@anthropic.com>
Since boykopovar#316 a wave32 dispatch recompiles for a 32-wide subgroup target,
so preparing for the device target built a source the dispatch never
looked up. PrepareSource is added to the shader cache's reviewed names.

Co-Authored-By: Claude <noreply@anthropic.com>
@adriadam10
adriadam10 force-pushed the perf/compute-front-end-at-registration branch from 33555ae to 3de13ca Compare October 5, 2026 09:01
@adriadam10

Copy link
Copy Markdown
Collaborator Author

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.

@github-actions github-actions Bot removed the needs rebase Conflicts with main, rebase needed label Oct 5, 2026
@adriadam10

Copy link
Copy Markdown
Collaborator Author

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

Copy link
Copy Markdown
Collaborator Author

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.

@adriadam10 adriadam10 closed this Oct 5, 2026
@boykopovar

Copy link
Copy Markdown
Owner

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.

@adriadam10
adriadam10 deleted the perf/compute-front-end-at-registration branch October 6, 2026 20:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

agc libSceAgc* build shader core/shader

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants