perf(shader): index ValidateProgram by value and block id #470

Merged
adriadam10 merged 1 commit from upstream-pr/470 into main 2026-10-04 06:24:05 +00:00
adriadam10 commented 2026-10-03 16:28:44 +00:00 (Migrated from github.com)

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


What

ValidateProgram runs twice per recompile (end of Translate and in SpirvEmitter::Emit) and looked up every instruction and block in std::unordered_maps keyed by pointer. Value and block ids are dense per program (nextValueId/nextBlockId), so blockIndices and instructionPositions are now an index by id that stores the pointer next to the position. A node whose slot holds another pointer (a foreign value or block whose id collides with a local one) goes to a fallback map, so foreign definitions, duplicates and positions are detected exactly as before. No validation check is removed or changed.

Measured with callgrind/perf on compute shaders assembled with llvm-mc -mcpu=gfx1030 (ALU chains with a branch every 40 instructions, results stored, plus two loop shaders with phis), one Recompile each, cache off:

Shader Instructions before after ValidateProgram before after
145 words 8.56M 8.22M 1.13M 0.78M
587 words 24.48M 23.01M 4.68M 3.20M
1754 words 70.38M 65.84M 14.48M 9.91M

perf stat over 100 recompiles of the 1754-word shader: 3445–3484M cycles before, 2997–3230M after (-7 to -13 %). The rest of ValidateProgram is the per-instruction checks themselves.

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 and the 8 assembled shaders (hash of every compileVariant output, temporary logging not included).
  • New agc_validate_program test: a valid program, use before definition, a duplicated instruction, a foreign definition with a local id, and two values with the same id in one block (in order and out of order). It passes on main and 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:4728009107 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/470). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-03T16:28:44Z. --- ### What `ValidateProgram` runs twice per recompile (end of `Translate` and in `SpirvEmitter::Emit`) and looked up every instruction and block in `std::unordered_map`s keyed by pointer. Value and block ids are dense per program (`nextValueId`/`nextBlockId`), so `blockIndices` and `instructionPositions` are now an index by id that stores the pointer next to the position. A node whose slot holds another pointer (a foreign value or block whose id collides with a local one) goes to a fallback map, so foreign definitions, duplicates and positions are detected exactly as before. No validation check is removed or changed. Measured with callgrind/perf on compute shaders assembled with `llvm-mc -mcpu=gfx1030` (ALU chains with a branch every 40 instructions, results stored, plus two loop shaders with phis), one `Recompile` each, cache off: | Shader | Instructions before | after | `ValidateProgram` before | after | |---|---|---|---|---| | 145 words | 8.56M | 8.22M | 1.13M | 0.78M | | 587 words | 24.48M | 23.01M | 4.68M | 3.20M | | 1754 words | 70.38M | 65.84M | 14.48M | 9.91M | `perf stat` over 100 recompiles of the 1754-word shader: 3445–3484M cycles before, 2997–3230M after (-7 to -13 %). The rest of `ValidateProgram` is the per-instruction checks themselves. ### 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 and the 8 assembled shaders (hash of every `compileVariant` output, temporary logging not included). - New `agc_validate_program` test: a valid program, use before definition, a duplicated instruction, a foreign definition with a local id, and two values with the same id in one block (in order and out of order). It passes on `main` and 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
oneandonlydean commented 2026-10-04 05:46:05 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-04T05:46:05Z.


Thanks, indexing by id is a nice win and the validator still catches the same cases. The branch now conflicts with main in core/libs/prx/libSceAgcDriver/CMakeLists.txt (the agc_dcc_key_sharing_tests block from #466 landed where you add agc_validate_program_tests); a rebase should be all it needs.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:5977042149 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/470#issuecomment-5977042149). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-10-04T05:46:05Z. --- Thanks, indexing by id is a nice win and the validator still catches the same cases. The branch now conflicts with main in core/libs/prx/libSceAgcDriver/CMakeLists.txt (the agc_dcc_key_sharing_tests block from #466 landed where you add agc_validate_program_tests); a rebase should be all it needs.
adriadam10 commented 2026-10-04 06:19:38 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-04T06:19:38Z.


Thanks! Rebased onto current main; the CMakeLists.txt conflict was only placement, so agc_validate_program_tests now sits after the agc_dcc_key_sharing_tests block from #466. Both tests pass locally.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:5977257600 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/470#issuecomment-5977257600). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-04T06:19:38Z. --- Thanks! Rebased onto current main; the CMakeLists.txt conflict was only placement, so `agc_validate_program_tests` now sits after the `agc_dcc_key_sharing_tests` block from #466. Both tests pass locally.
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!470
No description provided.