perf(agc): free a registered shader's decoded program once it is planned #5910

Merged
milescoviello merged 1 commit from upstream-pr/5910 into main 2026-10-15 08:44:12 +00:00
milescoviello commented 2026-10-14 15:30:12 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by milescoviello on 2026-10-14T15:30:12Z.


What

Borderlands 4 (PPSA01494) registers shaders in a burst about 190 s into a warm run, and its anonymous resident memory grows from about 1 GB to 8–9 GB within six seconds. A jemalloc heap profile taken in that burst shows 638 MiB held by RDNA programs that PlanRegistered decoded (RdnaInstructionDecoder::Decode ← PlanRegistered ← Driver::RegisterShader ← sceAgcCreateShader).

Since #2583 prepares registered shaders in the background, PlanRegistered decodes the program into the RegisteredPreparation it queues for the registration prepare pool. Only PlanRegistered reads that decoded program, for the s_swappc_b64 check; PrepareRegistered does not use it, and the preparation decodes the program again on its own (ShaderPreparationContext::AcquireFrontend). So every shader waiting in the pool's queue holds a decoded copy of its program until a worker has prepared it.

PlanRegistered decodes into a local again, as it did before #2583, and the queued preparation no longer carries it.

Tested

Ubuntu 24.04, GCC 13.3 (CI's compiler), Release with CI's configure flags, on main 84b01f9cd, then rebased onto main a453108cf (which changes only libSceVoice since):

  • All 541 tests in libSceAgcDriver pass. agc_driver_stencil_ref_export is skipped because the device lacks VK_EXT_shader_stencil_export, and agc_host_import_sparse and agc_driver_sparse_dma_buf_import because /dev/udmabuf is not accessible here.

  • Borderlands 4 (PPSA01494), relinked with --to-intel and run on Linux (RTX 4070 Ti, NVIDIA 580.142) with a warm shader cache under a 15 GB memory cap, once per build under jemalloc 5.3 with sampled heap profiles (prof:true,lg_prof_interval:32,lg_prof_sample:19). The two runs differ only in libSceAgcDriver.prx. Live heap in the last profile before the run reached the cap (about 181 s):

    live heap, by allocating caller main this PR
    RDNA programs decoded by PlanRegistered 638 MiB none
    RDNA programs decoded by AcquireFrontend (preparations in progress) 42 MiB 45 MiB

    Most of the rest of the heap at that point is resource plans decoded from the shader cache (1.7–1.9 GB) and SPIR-V optimization in progress, which this change does not touch.

  • Without jemalloc the title still reaches the 15 GB cap in that burst, at 201–209 s, with and without this change: 3 of 3 runs with this PR, and 2 of 3 on main, where the third stopped earlier at a glibc heap check that this machine's memory produces with and without changes (see #5777). So this change alone does not let it through on that budget.

  • tools/check_conventions.py --base upstream/main passes, and tools/pr_overlap.py --branch finds no overlap.

Not checked on Windows or on a console.

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

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4860265355 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/5910). Originally posted by [milescoviello](https://github.com/milescoviello) on 2026-10-14T15:30:12Z. --- ### What Borderlands 4 (PPSA01494) registers shaders in a burst about 190 s into a warm run, and its anonymous resident memory grows from about 1 GB to 8–9 GB within six seconds. A jemalloc heap profile taken in that burst shows 638 MiB held by RDNA programs that `PlanRegistered` decoded (`RdnaInstructionDecoder::Decode` ← `PlanRegistered` ← `Driver::RegisterShader` ← `sceAgcCreateShader`). Since #2583 prepares registered shaders in the background, `PlanRegistered` decodes the program into the `RegisteredPreparation` it queues for the registration prepare pool. Only `PlanRegistered` reads that decoded program, for the `s_swappc_b64` check; `PrepareRegistered` does not use it, and the preparation decodes the program again on its own (`ShaderPreparationContext::AcquireFrontend`). So every shader waiting in the pool's queue holds a decoded copy of its program until a worker has prepared it. `PlanRegistered` decodes into a local again, as it did before #2583, and the queued preparation no longer carries it. ### Tested Ubuntu 24.04, GCC 13.3 (CI's compiler), Release with CI's configure flags, on `main` 84b01f9cd, then rebased onto `main` a453108cf (which changes only `libSceVoice` since): - All 541 tests in `libSceAgcDriver` pass. `agc_driver_stencil_ref_export` is skipped because the device lacks `VK_EXT_shader_stencil_export`, and `agc_host_import_sparse` and `agc_driver_sparse_dma_buf_import` because `/dev/udmabuf` is not accessible here. - Borderlands 4 (PPSA01494), relinked with `--to-intel` and run on Linux (RTX 4070 Ti, NVIDIA 580.142) with a warm shader cache under a 15 GB memory cap, once per build under jemalloc 5.3 with sampled heap profiles (`prof:true,lg_prof_interval:32,lg_prof_sample:19`). The two runs differ only in `libSceAgcDriver.prx`. Live heap in the last profile before the run reached the cap (about 181 s): | live heap, by allocating caller | `main` | this PR | |---|---|---| | RDNA programs decoded by `PlanRegistered` | 638 MiB | none | | RDNA programs decoded by `AcquireFrontend` (preparations in progress) | 42 MiB | 45 MiB | Most of the rest of the heap at that point is resource plans decoded from the shader cache (1.7–1.9 GB) and SPIR-V optimization in progress, which this change does not touch. - Without jemalloc the title still reaches the 15 GB cap in that burst, at 201–209 s, with and without this change: 3 of 3 runs with this PR, and 2 of 3 on `main`, where the third stopped earlier at a glibc heap check that this machine's memory produces with and without changes (see #5777). So this change alone does not let it through on that budget. - `tools/check_conventions.py --base upstream/main` passes, and `tools/pr_overlap.py --branch` finds no overlap. Not checked on Windows or on a console. ### 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) - [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 🤖 Generated with [Claude Code](https://claude.com/claude-code)
CnRJay commented 2026-10-14 19:16:57 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by CnRJay on 2026-10-14T19:16:57Z.

Review: COMMENTED


Checked: on main, RegisteredPreparation::decoded is only read by the UnresolvableSwappcTarget call in PlanRegistered, and the preparation decodes again in AcquireFrontend. So every queued shader held a decoded copy it never used, and decoding into a local keeps behaviour identical. Clean change with a clear heap profile behind it.

It should also help #5872, which reports the same registration burst pushing Demon's Souls out of memory.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5523372439 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/5910#pullrequestreview-5523372439). Originally posted by [CnRJay](https://github.com/CnRJay) on 2026-10-14T19:16:57Z. **Review:** COMMENTED --- Checked: on `main`, `RegisteredPreparation::decoded` is only read by the `UnresolvableSwappcTarget` call in `PlanRegistered`, and the preparation decodes again in `AcquireFrontend`. So every queued shader held a decoded copy it never used, and decoding into a local keeps behaviour identical. Clean change with a clear heap profile behind it. It should also help #5872, which reports the same registration burst pushing Demon's Souls out of memory.
milescoviello commented 2026-10-14 21:26:01 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by milescoviello on 2026-10-14T21:26:01Z.


A second title: Forspoken (PPSA03984) on Linux (RTX 4070 Ti, 595.99.02, GCC 13.3, Release). The base is main fa539dd9b with #5937, #5975 and #5982 merged, since Forspoken stops earlier without them. Each run used a warm shader cache under a 15 GiB cap, and jemalloc heap profiles were taken every 2 GiB allocated (prof:true,lg_prof_interval:31,lg_prof_sample:19). This compares the last profile of each run:

live heap by first frame without this PR with it
ShaderRecompiler::DecodeRdnaProgram 1,208 MiB 20 MiB
total 12,524 MiB 11,949 MiB

The total barely moves because the run with this PR got further before the cap stopped it: its last profile came after 82 dumps instead of 61. Its resource plans (decodePlan, IrValue::AddArgument, ownPlanValues) grew from 3.1 to 4.0 GiB in that time.

AI-assisted: yes

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6178857743 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/5910#issuecomment-6178857743). Originally posted by [milescoviello](https://github.com/milescoviello) on 2026-10-14T21:26:01Z. --- A second title: Forspoken (PPSA03984) on Linux (RTX 4070 Ti, 595.99.02, GCC 13.3, Release). The base is `main` fa539dd9b with #5937, #5975 and #5982 merged, since Forspoken stops earlier without them. Each run used a warm shader cache under a 15 GiB cap, and jemalloc heap profiles were taken every 2 GiB allocated (`prof:true,lg_prof_interval:31,lg_prof_sample:19`). This compares the last profile of each run: | live heap by first frame | without this PR | with it | |---|---|---| | `ShaderRecompiler::DecodeRdnaProgram` | 1,208 MiB | 20 MiB | | total | 12,524 MiB | 11,949 MiB | The total barely moves because the run with this PR got further before the cap stopped it: its last profile came after 82 dumps instead of 61. Its resource plans (`decodePlan`, `IrValue::AddArgument`, `ownPlanValues`) grew from 3.1 to 4.0 GiB in that time. AI-assisted: yes
github-actions[bot] commented 2026-10-15 00:41:01 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-15T00:41:01Z.


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
#6178 core/libs/prx/libSceAgcDriver/Execution/src/Driver/Shaders/ShaderRegistry.cpp L727-728, L772
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6181689770 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/5910#issuecomment-6181689770). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-10-15T00:41:01Z. --- <!-- 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 | |---|---|---|---| | #6178 | `core/libs/prx/libSceAgcDriver/Execution/src/Driver/Shaders/ShaderRegistry.cpp` [L727-728](https://github.com/boykopovar/AnyPS5/blob/839d242405aee2c0efa7d167d9fe1a89fd5e1e9d/core/libs/prx/libSceAgcDriver/Execution/src/Driver/Shaders/ShaderRegistry.cpp#L727-L728), [L772](https://github.com/boykopovar/AnyPS5/blob/839d242405aee2c0efa7d167d9fe1a89fd5e1e9d/core/libs/prx/libSceAgcDriver/Execution/src/Driver/Shaders/ShaderRegistry.cpp#L772) | | |
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!5910
No description provided.