perf(shader): allocate a program's IR values in chunks #11798
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/11798"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Imported from boykopovar/AnyPS5. Originally posted by milescoviello on 2026-10-27T07:54:34Z.
What
IrProgram::CreateValueallocates every IR value separately withstd::make_unique, and~IrProgramfrees them one at a time. On the Crimson Desert replay set it is the biggest allocator caller onmain: 3.5 % of the profile's samples are inmallocunder it, and another 2.2 % are infreeunder~IrProgram(the SRT evaluator, #11719/#11755, is next at 3.4 %). Values are allocated during every shader preparation, and each draw or dispatch with a new shader goes through it.This change keeps a program's values in chunks, starting at 64 values per chunk and doubling up to 4096:
CreateValueconstructs each new value in the next free slot.ReclaimUnusedValuesdestroys the unused values and keeps their slots for the values the program creates next.On 152 Crimson Desert (PPSA25286 01.000.568) shader requests,
agc_shader_replay --prepareuses 18.2 % less CPU time (10 of 10 rounds, both orders). All 65 Crimson Desert and 302 MXGP 26 (PPSA30839 01.003.000) SPIR-V outputs are byte-identical, and max RSS stays the same.How
IrValuePool(inIrProgram.hpp) replacesstd::vector<std::unique_ptr<IrValue>>:::operator newwithIrValue's alignment, asIrValueArenaalready does for plans. The pool also keeps the live values in creation order and a list of free slots.Create: takes a freed slot if there is one, otherwise the next slot of the newest chunk. If theIrValueconstructor throws, nothing is recorded.EraseIf: destroys the matching values, keeps their slots, and leaves the live list in order, asstd::erase_ifon the old vector did.agc_ir_reclaimcovers move construction and move assignment.ReclaimUnusedValuesiterates in the same order as before, and IDs still come fromnextValueIdand are never reused.Memory:
compileVariantkeeps creating values after the plan's reclaim, and those new values reuse the freed slots, much asfreefollowed bymallocdid before. A program holds at most one partly used chunk of up to 4096 values.Where the time goes: one DWARF profile per side (
perf record --call-graph dwarf -F 199, same input) shows:_int_malloc,unlink_chunk,free,_int_free_merge_chunkand friends) fall from 778 to 512.IrValueaccessors (HasUses,Parent,Id,Resolve,IsPhiand others) fall from 433 to 266. The values now sit next to each other, so passes that walk them miss the cache less often.Tested
Linux, i7-13700K, GCC 15.2, Release with
ANYPS5_ENABLE_SPIRV_TOOLS=OFF. I builtcmake --build build-rel --target agc_shader_replayatmain13926067acand at this change (c31e8e0fe3).ANYPS5_NO_SHADER_CACHE=1 agc_shader_replay --spvover the same 152 Crimson Desert and 305 MXGP 26 requests as #11719 (input hashes in #11719). Each binary ran in an empty directory per set, and I diffed thesha256sum * | sort -k2lists. All 65 and 302 SPIR-V outputs are byte-identical tomain's.ANYPS5_NO_SHADER_CACHE=1 /usr/bin/time -f %M perf stat -x, -e task-clock agc_shader_replay --prepare <the 152 requests>in a fresh empty directory, with the order alternating per round:main(ms)mainmax RSS (KB)Over the 10 rounds, the means are 15.96 s for
mainand 13.06 s for this PR, an 18.2 % drop (medians 15.77 and 12.93 s, SD 0.45 and 0.37 s). This PR is lower in all 10 rounds. Mean max RSS is 271.4 MB formainand 269.7 MB for this PR.The binaries' sha256 prefixes are
50c1220f47cc(main) and8c3d6925612d(this PR). These numbers use glibc's allocator, which titles use on Linux. I haven't measured Windows' default heap orAPS5_MIMALLOC=1, where the allocator share of the gain may differ.IrProgram:agc_ir_reclaim,agc_validate_program,agc_masked_select,agc_redundant_value_elimination,agc_constant_bit_test,agc_constant_phi,agc_constant_select,agc_denormal_flush_elimination,agc_flat_scratch,agc_guarded_memory_loads,agc_integer_folding,agc_integer_narrowing,agc_placeholder_images,agc_scalar_select_mask,agc_shader_device_profile,agc_shader_mode_register,agc_spirv_failure_text,agc_tessellation_loweringandagc_vector_window(20 ctest entries). Each passes 3 of 3 (--repeat until-fail:3) on an RTX 4070 Ti (NVIDIA 615.71.09) at this head. On Windows 11 (WinLibs GCC 15.2, Release, GT 1030 with NVIDIA R580), the same tests exceptagc_flat_scratch(19 entries) pass 3 of 3 at this head and at its base13926067ac.agc_ir_reclaimcovers what this change has to keep: reclaim counts, IDs that are never reused, and values staying valid across program move construction and move assignment. Under AddressSanitizer (ANYPS5_ENABLE_ASAN=ON, RelWithDebInfo),agc_ir_reclaimandagc_validate_programpass 3 of 3 at this head with no ASan report.This is a CPU-only shader-compilation change with byte-identical SPIR-V, like #11719, #9132 and #9198. It therefore uses the paired CPU replay that oneandonlydean accepted for those (#11719) instead of title captures.
Rebase and a kept-program fix (10-30)
#12039 landed before this PR, so it is now rebased onto
main91db3f79f7. The one conflict was the allocation line ofIrProgram::CreateValue. It is resolved the way #12039's description validated:IrValuePool::Createtakes the use arena andCreateValuepassesuseArena.Resource().useArenais declared beforevalues, so the pool still destroys its values while the arena is alive, asmain's vector did.The rebased A/B found a memory problem, which the second commit (
cb2159e48f) fixes:getSourcekeeps the program it built a plan from, for the source's first variant compile. WhenMaterializethen throws, no compile runs, so the program stays with theSourceEntryfor the life of the process.mainkeeps it too, but compactly.SrtWalker::EvaluateDescriptorSources), and their kept programs held about 1 GB (jemalloc peak profile: all of it undergetSource->ResourceProgram).recompileOncenow releases the kept program when materialization throws. A later request for the source prepares it again.Replay A/B at
cb2159e48fagainst91db3f79f7:agc_shader_replay --spvover the 152 Crimson Desert and 305 MXGP requests,ANYPS5_NO_SHADER_CACHE=1, 10 interleaved rounds, task-clock and max RSS:mainand 17.11 s here (-13.3 %), lower in every round.mainand 663 MiB here (-51 %), lower in every round. Most of that is the released programs, whichmainalso keeps. I didn't measuremainwith only the second commit.At
fa13b1cc64(before the fix), the same A/B gave -12.8 % CPU but +44 % max RSS (comment). On the 367 requests that compile, the pool alone was already lower: 598 vs 617 MiB.Tests at
cb2159e48f: the 20 ctest entries listed under Tested pass 3 of 3 on the RTX 4070 Ti (NVIDIA 615.71.09). On Windows 11 (WinLibs GCC 15.2, Release, GT 1030 with NVIDIA R580), the same tests exceptagc_flat_scratch(19 entries) pass 3 of 3 atcb2159e48fand atmain91db3f79f7. The replay timings in the Tested list and the title captures below were measured against the earlier base13926067ac.Title captures: Super Meat Boy 3D,
main9af563f33d+ #11580 (608dd76843) against the same + this PR (f4aa08df21), 3 interleaved rounds, recompiler cache off.Shader.PrepareArtifactworker time is 12,257 ms → 9,732 (-20.6 %) ms (table and traces). Round 2's summary lines:Checklist
main; no other open PR implements the same functionsNotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file.mdfiles or images added to the repository (attach them to this PR)🤖 Generated with Claude Code
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-27T08:41:34Z.
Review: APPROVED
Reviewed head c31e8e0fe3ff08f37d63a08a8ee2ab7d9a59dd5b. The pool keeps IRValue addresses stable across chunks and moves, preserves creation-order iteration and IDs, destroys reclaimed values exactly once, reuses only their slots, and rolls back the live-list insertion if allocation/construction fails. The existing reclaim tests cover move construction/assignment and stable IDs. The author reports byte-identical SPIR-V over 367 shader outputs, 10 interleaved CPU-replay rounds all favoring the change, 19 focused AGC test targets repeated on Linux/Windows, and ASan runs; hosted product jobs are skipped and I did not rerun them. I accept paired CPU replay for this CPU-only compilation change under the maintainer precedent recorded on #11719/#9132; no game capture is needed for this specific class of change. The automated
perf numbers missinglabel remains an intake/reporting status for maintainers to handle.