perf(shader): allocate a program's use lists from an arena #12039
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/12039"
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-27T17:24:51Z.
What
Performance evidence: this is a CPU-only change to shader compilation, and the SPIR-V is byte-identical. oneandonlydean accepted paired replay timings for this class of change instead of title captures (#9132, #9198), and so did MarcZX10 on #11719. Exact revisions, raw runs, the command and the request set are below.
At the current head
1d9eaeaa89(rebased ontomainaaccd0ba39), with the same replay on an otherwise idle host: 9.86 s → 9.00 s task-clock (−8.7 % median, lower in 10 of 10 rounds, −7.9 % to −9.6 % per round), and max RSS +4.4 MiB (+1.7 %). The SPIR-V is byte-identical: all 65 Crimson Desert modules and all 302 MXGP modules. The 82 focused tests pass 3 of 3 at the head and on thatmain, and Khronos validation over them has noVUID-messages on either side. Rounds and method. The previous heade7c7184498onmainedf6804cf5gave −8.6 % (rounds). The measurements below are from the earlier heads.Every
IrValuekeeps the list of instructions that read it (operandUses, astd::vector<IrUse>). EachAddArgument,ReplaceArgumentandReplaceAllUsesWithpushes onto a use list, and every growth is a heap reallocation, so a program's use lists churn throughmalloc/freewhile the passes run. All of a program's values die with the program, so freeing their use lists one by one buys nothing.With this change, a program's use lists come from a bump arena (
std::pmr::monotonic_buffer_resource) that the program owns. Growing a list bumps a pointer, and freeing the old buffer does nothing. The memory goes back when the program is destroyed.Measured with the shader replay over the 152 Crimson Desert (PPSA25286) pipeline requests in
--preparemode, the same corpus as #11798, #11820, #11967 and #12002. Both sides are exact upstream revisions:main9af563f33dand this PR's head9aaf9be4a0, which sits directly on it:main9af563f33d9aaf9be4a0All 10 rounds (task-clock and max RSS of each run)
Max RSS grows by about 4 MB (1.6 %). That's the arena's transient peak while programs are being built. A counting diagnostic build shows the arena peaking at 6.5 MiB in this replay, with no cached program holding arena memory (review reply). Use lists grow geometrically, so a program being built holds at most about twice its final use-list bytes.
Where the time goes, from
perf record -F 499of one replay of the same corpus on each side, taken at the earlier head787e3f6eaeagainstmainc547deaacb(samples, not percentages):mainmalloc,free,new,deleteand glibc's internals)ValidateProgramIrValue::HasUsesConstantFolder::FoldDeadCodeEliminator::EliminateIrValue::ParentMore than half of the saving is in the allocator. The passes that walk use lists gain a little more, because a program's lists now sit together in its arena.
The SPIR-V is byte-identical to
main's: all 65 modules from the Crimson Desert requests and all 302 from MXGP 26's (PPSA30839) dump. Only where use-list buffers live changes; the order of uses and every pass's behaviour stay the same.How
IrValue.hpp:IrUseAllocator<T>holds astd::pmr::memory_resource*. With an arena it allocates from it anddeallocatedoes nothing. Without one it isstd::allocator.operandUsesis anIrUseList(std::vector<IrUse, IrUseAllocator<IrUse>>), andOperandUses()returns it. A copy of a use list (select_on_container_copy_construction) goes back to the heap, so a copy can outlive the program. The allocator propagates on move and swap.IrValue(opcode, type, id, useArena = nullptr): values made outside a program (resource plans, the disk cache, tests) keep the heap.IrUseArenaowns the program'smonotonic_buffer_resource, created on the firstCreateValue.IrProgramdeclares it beforevalues, so values are destroyed before their arena. Its move assignment swaps, so ina = std::move(b)the old arena ofamoves intoband outlives the valuesadrops.deallocatenever touches the arena either way.IrValuegrows by the allocator's pointer, from 128 to 136 bytes;ShaderDiskCache.cpp's size check follows. The plan value encoder doesn't serialise use lists, so the disk-cache format is unchanged.ConstantFolder.cpp: the two places that copied a use list into astd::vector<IrUse>copy it as anIrUseList.UseRewrite.cppcompares againstIrUseList.monotonic_buffer_resourceisn't thread-safe. A program's values are only changed by the thread compiling it, asstd::vectoralready requires.IrReclaim.cppadds a case: 40 readers grow one value's use list in its program's arena and keep their order;ReplaceAllUsesWithmoves all of them to another value; then move assignment into that program keeps the assigned program's use lists intact.Cached programs (after review):
getSourcestores some programs in theirSourceEntry. Before it does,IrProgram::ReleaseUseArena()copies each surviving value's use list into a heap vector and frees the arena, so a cached program keeps none of the arena's buffers: neither the ones whose valuesReclaimUnusedValuesdropped, nor the old buffers growth left. This also holds on thegetSource(..., false)path, which caches without reclaiming. Values created after that get a new arena. Second commit:3d427e3e32.Merge order:
IrProgram::CreateValue. This PR lands after it and is rebased onto it, with the resolution validated below.IrValuesize check from 128 to 136. The two edits are identical, so git merges them without a conflict, but togetherIrValueis 144 bytes. This PR lands after #11820 and sets the check to 144 when it's rebased onto it (validated below).IrResourcePlansize check on the line before. That's a one-line textual conflict with no behavioural dependency. Agreed on #7689 (comment): #7689 lands first, and this PR is rebased after it, keeping both size values.Tested
Head
0fe2329ee8(6a98793e3bwithmain422f96b3a9merged in, which brings main'sRecompiler.cppchange), built from these exact revisions:shader_ir_use_rewrite,agc_ir_reclaim,agc_constant_select,agc_masked_selectandagc_validate_programpass 3 of 3.agc_shader_replay --spvgives byte-identical SPIR-V onmain422f96b3a9and on this head: all 65 Crimson Desert modules (152 requests) and all 302 MXGP modules (305 requests).--spvover all 457 requests, 10 alternating rounds: 26.61 s mean onmain, 19.83 s on this head (−25.5 %), lower in 10 of 10 (24.5–26.0 %). Median max RSS is 1,374,254 KB onmainand 1,391,460 KB on this head.This is a CPU-only change to IR allocation with byte-identical SPIR-V, so it has no GPU-side effect to capture. oneandonlydean accepted replay evidence in place of title captures for such changes on #9132 and #9198.
Earlier:
Exact head
6a98793e3bmerged withmaindc38224141(mergeb0bf86ca7d, clean), Linux, GCC 15.2, Release, SPIRV-Tools off:shader_ir_use_rewrite,agc_ir_reclaim,agc_constant_select,agc_masked_selectandagc_validate_programpass 3 of 3.agc_shader_replay --spvgives byte-identical SPIR-V onmainand on the merge: all 65 Crimson Desert modules (152 requests) and all 302 MXGP modules (305 requests).--spvover all 457 requests, 10 alternating rounds: 26.43 s mean onmain, 19.85 s on the merge (−24.9 %), lower in 10 of 10 (24.4–25.6 %). Median max RSS is 1,370,986 KB onmainand 1,392,566 KB on the merge. This workload includes SPIR-V emission, unlike the--preparetimings below.Earlier heads:
Linux (Ubuntu 26.04, GCC 15.2), Release:
787e3f6eae(this patch before the rebase onto9af563f33d, unchanged by it): the 47 test targets whose sources use the IR API,ctest3 runs, all 76 entries pass in every run. AddressSanitizer:agc_ir_reclaim,agc_shader_memory,agc_validate_programandshader_ir_use_rewrite, 3 runs, all pass with no AddressSanitizer or LeakSanitizer report.shader_ir_use_rewrite,agc_ir_reclaim,agc_validate_program,agc_shader_memoryandagc_constant_select, 3 runs each onmainc547deaacband at787e3f6eae: 5 of 5 pass in every run on both sides.At
3d427e3e32(after review), with the same replay and 10 rotating rounds againstmain9af563f33dand the previous head9aaf9be4a0, built from those exact revisions:main, 14.37 s on the previous head and 14.64 s on this head: −5.9 % againstmain, lower in 10 of 10. This head is 1.9 % slower than the previous one.mainfor all 65 Crimson Desert and 302 MXGP modules.shader_ir_use_rewrite,agc_ir_reclaim,agc_constant_select,agc_masked_selectandagc_validate_programpass 3 of 3. AReleaseUseArenathat does nothing failsagc_ir_reclaim. On the Windows 11 VM they pass 3 of 3 atmainand at this head.608dd768435,141 MB, + the previous head 4,856 MB, + this head 4,610 MB. That's within the roughly 600 MB spread between runs of one build.Combined layout with #11798 and #11820 (
033713cac5on my fork:main9af563f33d+ #11798c31e8e0fe3+ #11820e339f57e4d+ this change):values.Create(opcode, type, nextValueId, useArena.Resource()), withIrValuePool::Createtaking the arena and passing it to the value's constructor.IrValueis 144 bytes there. TheShaderDiskCache.cppcheck is set to 144 and builds.mainalone, and I haven't looked into why.All 6 rounds on the stack
How the numbers were taken:
agc_shader_replay(SPIR-V Tools off, as users build) of each exact revision. Replay binary SHA-256 prefixes:main0b1feee1a3a5, this PR531fdc2c2a97.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, alternating which side runs first.4fc9e044c1e59fb3. The 305 MXGP requests give7a681333ac4a2b4e. The requests themselves are game shader dumps and aren't published.agc_shader_replay --spvover both sets on each side, then a diff of the sortedsha256sumlists of the output modules.Supplementary: Black Ops II title captures with a cold recompiler cache (comment). Black Ops II stops at 8 s on
mainwithout #9656, so both sides there are local builds ofeece5da035+ #9656, with and without this commit. They aren't upstream revisions and aren't the evidence for this PR. They showed 14.5 % and 17.4 % less preparation time per artifact on the title thread, and no change on the workers.Title captures (after review): Super Meat Boy 3D,
main9af563f33d+ #11580 (608dd76843) against the same + this PR (561c7f952a), 3 interleaved rounds, recompiler cache off.Shader.PrepareArtifactworker time is 12,196 ms → 11,322 ms (−7.2 %), lower in 3 of 3 (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-27T18:53:25Z.
Review: CHANGES_REQUESTED
Reviewed exact head
787e3f6eae82e11b336343d94f450455bee6907d. The allocator and arena lifetime approach is coherent on inspection, and the PR reports extensive IR, ASan, and Windows/Linux test coverage. I have not independently rebuilt this large shader change in this pass.There are two repository-rule blockers:
The
perfreport does not currently meet the required evidence format. The linked title summary lines use commitsed3b6566a2c6and2bb6ee4de359; neither resolves as a commit in upstreamboykopovar/AnyPS5, and the report describes its other replay baseline asmainc547deaacb, rather than the current base. The requiredtools/trace_digest.pyoutput is also absent, and the trace assets are linked from a separate fork rather than attached here. Please provide the same-title/current-main and PR captures, both summary lines, trace assets and digest, or remove the performance claim/label and resolve the automated report check.The PR itself identifies unresolved overlapping work: exact pairwise
git merge-treeshows content conflicts with open #11798 inIrProgram.cppand #7689 inShaderDiskCache.cpp. #11820 also overlaps theIrValuesize check and the description says the combined layout requires changing the size from 136 to 144. The description says whichever lands second will be rebased, butCONTRIBUTING.mdrequires authors to agree the order and declare it withDepends on; the checklist currently saysnone. Please coordinate the order with those authors, declare dependencies, and validate the combined layout and cache size check after rebasing.Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-27T19:42:57Z.
Review: CHANGES_REQUESTED
Rechecked exact head
9aaf9be4a0d50baf45e59825107eb432e9e5a114. The new report substantially improves the technical evidence: it gives exact replay revisions, ten paired rounds, request-set digest, SPIR-V comparisons, memory cost, and the combined layout tests. The earlier conflicts with #11798 and #11820 now have declared dependencies, and the author of #7689 has proposed the same either-order resolution in a comment there.Two repository requirements are still unresolved:
This PR still claims a 7.2% speedup and retains the
perftitle/label. The current PR labels includeperf numbers missing, and the description still has no two validaps5-summary.jsonllines or attached traces for the same title, scene and machine. The replay timing table is useful supplemental evidence, butCONTRIBUTING.mdrequires those paired capture summaries and traces for a speed claim;AGENTS.mdalso requirestools/trace_digest.pysummaries. Please add the required report material, or remove the speed claim and perf label so the automated missing-numbers label clears.#12039 and #7689 both change the
ShaderDiskCache.cppsize-assert region. The new checklist declares #11798 and #11820, but still does not declare an agreed order with #7689. The current comment proposes either order, and the #7689 author has not replied to this specific proposal. Please get that order agreed and record it withDepends onas the overlap rule requires.The report and tests are author-provided; I have not independently rebuilt the shader changes. Current hosted Windows, Ubuntu relinker and ASan checks pass; two macOS relinker checks are still pending. These checks do not resolve the report or overlap items above.
Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-28T10:04:01Z.
Review: CHANGES_REQUESTED
Read the full diff; the arena lifetime (declared before
values, swap on move assignment, heap on copy) and the newIrReclaimcase look right. Three things before this can merge:Recompiler.cpp:536-538:source->programis kept in theSourceEntry, andIrProgram::ReclaimUnusedValues(57b2dc8584) exists to shrink exactly those cached programs. With the arena, the use-list buffers of the dropped values, and every old buffer left by growth, stay allocated until the entry is destroyed, so reclaim no longer returns that memory. Please bound this (for example keep the arena only for programs that are not cached, or move the surviving use lists to the heap after reclaim) and show peak RSS before/after on a title run that keeps cached programs, not only on the--preparereplay (+1.6 % there, +3.7 % on the stack with #11798/#11820, still unexplained).ShaderDiskCache.cppsize asserts is still not agreed; please get the reply there and record it inDepends on. Note that with #11820 theIrValuecheck has to be 144: two identical 136 edits merge without a conflict and fail the build.Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-29T07:16:07Z.
Review: CHANGES_REQUESTED
Re-checked — changes requested
The author’s latest update adds a focused arena-release test and an independent approval on this exact head. I rechecked the new head against current
main; the branch is still based on an older revision, and currentmainhas changed files this PR modifies. The reported performance captures also use the prior PR head, so the current combined change still needs validation.What I ran
gh.mainusinggit merge-base,git merge-tree --write-tree, andgit diff --check.Results
git diff --checkUnedited command output
Negative control
Not run. I did not build a deliberately failing control or compare test logs locally.
Findings
Blocking
mainand rerun the focused shader-recompiler tests on both the same current-main base and the exact PR head, with Vulkan validation configured as the repository requires. Currentmainhas changedIrProgram.hpp,IrProgram.cpp,ConstantFolder.cpp,Recompiler.cppandShaderDiskCache.cpp, all paths touched by this PR; the author’s latest test report names the older base9af563f33d.9aaf9be4a0; the current head addsReleaseUseArena(), so the existing capture pair does not measure the full change now under review. Keep the same title, scene and machine and include the required summary lines, traces and digests.Non-blocking
mainand this exact head.Sources
3d427e3e3283f576112072da3cbea2965063ce37mainedf6804cf52c62eacafc592dd6142cb4efe5bd05Not verified
To get this to a good state
Rebase onto current
main; run the same focused shader-recompiler/Vulkan-validation tests on that base and the exact new head; then refresh the paired performance evidence for those exact revisions.Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-29T08:36:39Z.
Review: CHANGES_REQUESTED
Re-reviewed exact head
e7c7184498e5441dc2e9412e8610fdc010991a49and your current update. The use-arena lifetime regression and the 82-test/3-run report onedf6804cf5address the earlier older-base test concern, and the new replay report is well described for that pair. The head still does not include currentmain4c7d275bef89d309313d16bc2bc05d37a1b50be2; current main changedcore/shader/recompiler/Recompiler.cpp, which this PR also changes. Please integrate current main and rerun the focused tests and paired performance report on that exact current integration. The merge preview is clean; diff check and conventions pass. I did not run the shader/GPU tests locally.Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-29T09:44:00Z.
Review: APPROVED
Re-checked — approved
The exact head is now rebased onto current
mainaaccd0ba39; the previously notedRecompiler.cppintegration is included, and the use-arena production diff remains unchanged. The author reports 82 focused tests passing in each of three runs on both the current-main baseline and this head, with the Vulkan validation layer reporting no VUIDs. The SPIR-V output is reported byte-identical across 152 replay requests.The CPU shader-replay benchmark is reported as 9.86 s to 9.00 s median task-clock (10 paired rounds) with +4.4 MiB maximum RSS. This supports the compiler-time claim; it is not a title frame-time claim. Hosted checks are pending after the update.
I reviewed the integration and evidence but did not rerun the tests or benchmark locally. No blocking issue found; approval covers exact head
1d9eaeaa8950c2ed0b07b068efecddde93a3addc.Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-29T21:15:19Z.
Review: CHANGES_REQUESTED
Re-checked — new head, changes requested
The two latest code commits are not covered by the performance and focused-test evidence currently reported for older heads.
What I ran
Reviewed exact PR head
6a98793e3b32994bd9f5481f37e925fc6af18a1b, whose GitHub base is951f374c8716fd69f7241a936fb944ca0e8bf354; current upstreammainis9624d8c6bb9e96b7aa92b815f55690f69b0a88d5. The current-main merge preview is clean. Environment: Linux x86-64, Git and GitHub CLI; no compiler or build was used.I read the exact-head diff, latest PR description/comments, and hosted check statuses. Reported benchmarks and focused test runs are author-provided; I did not rerun them.
Results
main19f6a2f34262f608a3cf0b39c07cad9ab2288176951f374c8716; currentmain9624d8c6bb9e, 13 commits ahead1d9eaeaa89and3d427e3e32; neither is the current head6a98793e3b. The current head addsc01d0bb24fand6a98793e3b, including the cached-program arena release and theIrValuesize update. I found no exact-head matched performance result or focused-run report for those changes.Unedited local command output
Findings
Blocking
IrValuesize assertion; those changes can affect the performance and memory behavior being reported. Please rerun the relevant focused tests and matched replay comparison on exact head6a98793e3band a current-main baseline.951f374c8716, while current upstreammainis9624d8c6bb9e. The merge preview is clean, but the repository requires branches to be based on and kept up to date with currentmain; this exact integration has not been tested by the reported runs.I found no additional source-level defect in the reviewed diff.
Sources
Not verified
To get this to a good state
Update the branch onto current
main, then rerun the focused tests and matched replay benchmark on the resulting exact head and current-main baseline; update the PR report with both exact revisions and results. I will recheck that head and repeat the non-build checks above.Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-30T04:25:33Z.
Review: CHANGES_REQUESTED
Re-checked — new head, changes requested
The new report adds useful replay and focused-test evidence, but it is based on an older main snapshot that has since changed a production file this PR also edits.
What I ran
Reviewed head
6a98793e3b32994bd9f5481f37e925fc6af18a1b; the author’s reported comparison base isdc38224141e0b6096007b876b4c42fc9144d11b8, while current main is5ecef095b61e7a406b279c97896f3a8a6fbd6353. The current-main merge preview is clean (tree04bb80575c5ba2585a7d094cb9c4331ee46bbea4), but shared paths changed since the report base includecore/shader/recompiler/Recompiler.cpp, which this PR also changes.Results
dc382241Findings
Blocking
Recompiler.cppis outside the PR’s reported test/benchmark base, so the reported qualification does not cover the exact integration under review.perf. The replay timing is useful evidence foragc_shader_replay --spv, but the PR still needs the performance-report evidence required by the repository for the claimed change, or a maintainer decision that this tool-only measurement satisfies that requirement.Not verified
I did not run a local build, replay benchmarks, tests, or GPU validation. The figures above are author-reported.
To get this to a good state
Please rebase or merge current main and rerun the focused tests and replay comparison on the exact resulting head. Please also attach the required matched performance-report artifacts, or record the maintainer’s accepted equivalent. I’ll re-check the same paths and commands on the new head.