perf(shader): build a shader's front end once on first use #471

Merged
adriadam10 merged 1 commit from upstream-pr/471 into main 2026-10-04 09:55:07 +00:00
adriadam10 commented 2026-10-03 16:45:29 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-03T16:45:29Z.


What

On the first use of a shader through the cache the front end (decode, translate, SSA, fold, SRT plan, resource tracking) ran twice: getSource built the program to extract the resource plan, and findOrCompileVariant rebuilt it to compile the first variant. The program could not be reused because the plan pointed into it (descriptor dwords, SRT reads, control-flow conditions, uniform fill), Materialize walks those values on every draw, and compileVariant rewrites the program (Apply, dead-code elimination invalidates values). So makeResourcePlan kept the whole program alive next to the plan.

  • ExtractPlan now copies the values the plan reaches (through arguments, phis included) into IrResourcePlan::valueStorage, which existed and was unused, with one placeholder block per phi predecessor in a new blockStorage. A copy keeps opcode, type, id, flags, immediate, register and arguments: what the walk, ResolveInvariantPhi and EquivalentValue read. The plan no longer points into the program.
  • getSource parks the program it built in the source entry, and the first variant compiled for it takes it instead of running the front end again. The entry drops it after its first variant (compiled or loaded from the disk cache), so it no longer keeps a whole program alive per shader for the life of the cache.

Callgrind, one Recompile with the cache on (first use), on compute shaders assembled with llvm-mc -mcpu=gfx1030 (ALU chains with a branch every 40 instructions, results stored):

Shader Before After Without cache, for reference
145 words 13.20M 8.60M 8.58M
587 words 42.45M 24.52M 24.49M
1754 words 126.56M 70.44M 70.40M

First use now costs one recompile; copying the plan's values is within noise.

Tested

  • Linux (CachyOS), GCC 16, Release: full ctest passes except agc_driver_pixel_helper_lanes, which fails the same way on main on this machine.
  • SPIR-V is byte-identical before and after for all 88 distinct shaders the test suite compiles (hash of every compileVariant output, temporary logging not included).
  • All 37 agc_* tests under AddressSanitizer (-fsanitize=address): no errors from this change. agc_shader_memory reports a stack-buffer-overflow in GuestMemory::Read on main too (the page read around a stack table in verifyBindlessTable); with replace_intrin=0 it passes on this branch.
  • Not tested with a title.

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: none
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4728107397 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/471). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-03T16:45:29Z. --- ### What On the first use of a shader through the cache the front end (decode, translate, SSA, fold, SRT plan, resource tracking) ran twice: `getSource` built the program to extract the resource plan, and `findOrCompileVariant` rebuilt it to compile the first variant. The program could not be reused because the plan pointed into it (descriptor dwords, SRT reads, control-flow conditions, uniform fill), `Materialize` walks those values on every draw, and `compileVariant` rewrites the program (`Apply`, dead-code elimination invalidates values). So `makeResourcePlan` kept the whole program alive next to the plan. - `ExtractPlan` now copies the values the plan reaches (through arguments, phis included) into `IrResourcePlan::valueStorage`, which existed and was unused, with one placeholder block per phi predecessor in a new `blockStorage`. A copy keeps opcode, type, id, flags, immediate, register and arguments: what the walk, `ResolveInvariantPhi` and `EquivalentValue` read. The plan no longer points into the program. - `getSource` parks the program it built in the source entry, and the first variant compiled for it takes it instead of running the front end again. The entry drops it after its first variant (compiled or loaded from the disk cache), so it no longer keeps a whole program alive per shader for the life of the cache. Callgrind, one `Recompile` with the cache on (first use), on compute shaders assembled with `llvm-mc -mcpu=gfx1030` (ALU chains with a branch every 40 instructions, results stored): | Shader | Before | After | Without cache, for reference | |---|---|---|---| | 145 words | 13.20M | 8.60M | 8.58M | | 587 words | 42.45M | 24.52M | 24.49M | | 1754 words | 126.56M | 70.44M | 70.40M | First use now costs one recompile; copying the plan's values is within noise. ### Tested - Linux (CachyOS), GCC 16, Release: full ctest passes except `agc_driver_pixel_helper_lanes`, which fails the same way on `main` on this machine. - SPIR-V is byte-identical before and after for all 88 distinct shaders the test suite compiles (hash of every `compileVariant` output, temporary logging not included). - All 37 `agc_*` tests under AddressSanitizer (`-fsanitize=address`): no errors from this change. `agc_shader_memory` reports a stack-buffer-overflow in `GuestMemory::Read` on `main` too (the page read around a stack table in `verifyBindlessTable`); with `replace_intrin=0` it passes on this branch. - Not tested with a title. ### 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: none - [x] AI-assisted: yes
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!471
No description provided.