perf(shader): keep one use list per IR value #7520
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/7520"
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-18T11:24:20Z.
What
Every
IrValuekept two lists of its users.uses(std::vector<IrValue*>) held the users, andoperandUses(std::vector<IrUse>) held each user with the operand index it reads.AddArgument,ReplaceArgumentandReplaceAllUsesWithupdated both, and every value allocated, grew and freed both. In a profile of a Release replay of 355 MXGP 26 (PPSA30839) shader requests, the allocator was 20% of all samples. Destroying IR programs,IrProgram::CreateValueandIrValue::AddArgumentwere its largest callers.Only
operandUsesis kept now:Uses()returns a view of the users inoperandUses(std::views::transform). Its six callers in the recompiler only iterate it or test it withall_of, exceptSsaPass::TryRemoveTrivialPhi, which copied it into a vector and still does.HasUses,UseCountandInvalidatereadoperandUses.ReplaceArgumentno longer searchesuses, andReplaceAllUsesWithreserves and fills one list.sizeof(IrValue)drops from 152 to 128 bytes. The plan value encoder inShaderDiskCache.cppwrites no use lists, so only its size check changes.The two lists held the same users in the same order in all but one case:
ReplaceArgumenton a user that took the value in more than one operand.usesthen dropped the user's first entry, andoperandUsesdropped the replaced operand's entry.Uses()now followsoperandUsesthere as well, which is the listReplaceAllUsesWithandIrUse-based passes already worked from.shader_ir_use_rewritecounted the allocations of both lists, so two of its expectations change:ReplaceAllUsesWith's two reserves in turn. It now fails the one reserve that is left.2 * (bit_width(count) + 1)allocations for 4,096 replacements. It now allowsbit_width(count) + 1.mainfails the new bound ("geometric use-list growth: IR use rewrite assertion failed"), and this PR passes it.Tested
Ubuntu 24.04 (Linux 6.8), GCC 13.3, NVIDIA RTX 4070 Ti, driver 595.99.02.
ctest -R "^agc_|^shader_ir"on9f25d0bf5b: 709 tests. 708 passed on the first run, with 3 skipped (host import sparse, sparse dma-buf import, stencil ref export).shader_ir_use_rewritefailed on the two expectations above, and passes with them updated. After that change, the 23 tests matching^shader_ir|^agc_ir|^agc_shaderpass.agc_shader_replay --spv(Release,ANYPS5_ENABLE_SPIRV_TOOLSoff,ANYPS5_NO_SHADER_CACHE=1) on 466 shader requests I had dumped withAPS5_DUMP_SHADERS: 355 from PPSA30839, 107 from PPSA31162 and 4 from another app. All 462.spvfiles (a few names repeat across the dumps) have the same SHA-256 onmain9f25d0bf5band with this PR, and the replay output is identical.Replay CPU time (
perf stat -e task-clock), interleaved:main9f25d0bf5b0x1e401c0000(1,056,287 SPIR-V words), 15 runs each0x10f9f30000(1,130,170 words), 15 runs eachThat is about 13% on the two large shaders and about 9% over the whole set.
Title level: no speed claim. This is background shader preparation. I didn't run a title for it.
This touches the same IR as #7503 (
ReclaimUnusedValues, which only callsHasUses) and #7518 (IrValue's constructor). They don't conflict, and each was measured on its own.tools/check_conventions.py --base upstream/mainpasses.tools/pr_overlap.pylists only conflicting PRs that it could not compare onShaderDiskCache.cpp. #6232, #6946 and #6947 have thesizeof(IrValue)check as a context line and don't changeIrValue.main0fbfd91e5: 1637/1639 with this PR and onmain, with the same two failures (#7766, #7784), and no test added, skipped or removed. That's correctness only; the speed-up wasn't measured there.Title capture
As the
perflabel asks, these are captures of one title and scene on one machine. MXGP26 (PPSA30839) ran from boot withAPS5_TRACE_SECONDS=0:60,ANYPS5_NO_SHADER_CACHE=1and a fresh run directory (cold AnyPS5 and NVIDIA caches), so the shaders compile inside the window. NVIDIA RTX 4070 Ti (595.99.02), Ubuntu 24.04, release libs, Xvfb display, no input.maindbac9fa15bwas captured five times: before the PR captures, after them, and three more times in a row after that. Each PR capture and the first twomaincaptures followed a rebuild of the release libraries; the last threemaincaptures did not, and they came out lower. The change ismainwith this PR merged on top as a local merge commit (c5944449ddce). The time is the wall time of theShader.PrepareArtifactslices on theshader preparethreads in each trace.main1main2main3main4main5The five
maincaptures span 17.7% in total prepare time, and the 13 prepare threads run in parallel with the title, so a title capture can't resolve a recompiler change of a few percent. The replay measurement above is this change's measurement. This PR's capture falls inside the range of the fivemaincaptures.main1 trace, digest,main2 trace, digest,main3 trace, digest,main4 trace, digest,main5 trace, digest, this PR trace, digestVK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation ANYPS5_NO_SHADER_CACHE=1 ANYPS5_REQUIRE_VULKAN=1 ctest -R '^agc_' -Vonmaine2a2914aa1and onmain+ this PR (d484281b61, a local merge).main: 815 of 815 pass; with this PR: 815 of 815 pass. Neither log reports the layer missing. VUIDs onmain:VUID-VkImageViewCreateInfo-usage-02275×6,VUID-VkPipelineColorBlendStateCreateInfo-pAttachments-00605×18,VUID-vkCmdCopyBuffer-srcBuffer-00118×2,VUID-vkCmdDraw-multisampledRenderToSingleSampled-07285×2,VUID-vkCmdWaitEvents-srcStageMask-01158×2,VUID-vkSetEvent-event-09543×2; with this PR:VUID-VkImageViewCreateInfo-usage-02275×6,VUID-VkPipelineColorBlendStateCreateInfo-pAttachments-00605×18,VUID-vkCmdCopyBuffer-srcBuffer-00118×2,VUID-vkCmdDraw-multisampledRenderToSingleSampled-07285×2,VUID-vkCmdWaitEvents-srcStageMask-01158×2,VUID-vkSetEvent-event-09543×2. None is new with this PR.main1d4164552+ #7516 + #7494:shader_ir_use_rewritewith the tightened bounds and the 41-test set pass 3 of 3, MinGW accepts thesizeof(IrValue) == 128assert; identical shader-cache entries and replay hashes against twomainruns (Showcase PPSA99005, RetroArch PPSA99169, vkQuake PPSA99010), static frames bit-identical, and draw-path recompile time withinmain's spread.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 github-actions[bot] on 2026-10-18T11:35:05Z.
Some comparisons could not be completed because a pull request conflicts with the base branch. Review these shared files manually before concluding that there is no overlap.
core/shader/recompiler/ShaderDiskCache.cppcore/shader/recompiler/ShaderDiskCache.cppcore/shader/recompiler/ShaderDiskCache.cppcore/shader/recompiler/ShaderDiskCache.cppcore/shader/recompiler/ShaderDiskCache.cppcore/shader/recompiler/ShaderDiskCache.cppcore/shader/recompiler/ShaderDiskCache.cppcore/shader/recompiler/ShaderDiskCache.cppcore/shader/recompiler/ShaderDiskCache.cppcore/shader/recompiler/ShaderDiskCache.cppcore/shader/recompiler/ShaderDiskCache.cppcore/shader/recompiler/ShaderDiskCache.cppcore/shader/recompiler/ShaderDiskCache.cppcore/shader/recompiler/ShaderDiskCache.cppcore/shader/recompiler/ShaderDiskCache.cppcore/shader/recompiler/ShaderDiskCache.cppcore/shader/recompiler/ShaderDiskCache.cppcore/shader/recompiler/ShaderDiskCache.cppcore/shader/recompiler/ShaderDiskCache.cppImported from boykopovar/AnyPS5. Originally posted by milescoviello on 2026-10-18T12:09:30Z.
A note on how this composes with my other open shader preparation PRs: #7431, #7491, #7494, #7503, #7504, #7516, #7518 and #7522. I merged all nine onto
main0d078f75d3locally, in that order, and none of them conflicted.ctest -R "^agc_|^shader_ir": 710/710 pass (3 skipped: host import sparse, sparse dma-buf import, stencil ref export).agc_shader_replay --spvover the 466 dumped requests: 15 of the 462.spvfiles differ frommain. All 15 are shaders whose f16 min/max lowering #7516 changes; I checked that #7516 alone changes them. The other 447 are byte-identical.Release replay CPU time (
task-clock), interleaved:main0d078f75d30x1e401c0000, 10 runs each0x10f9f30000, 10 runs eachEach PR still stands on its own, and its description has its own measurement against
main.AI-assisted: yes (Claude Code)
Imported from boykopovar/AnyPS5. Originally posted by DavidAmsellem on 2026-10-19T16:27:58Z.
First AMD-on-Linux (RADV) run of this PR. Merged onto
main0fbfd91e5(clean merge), built in full and compared with the samemain, on the same machine.AMD Radeon RX 7900 XT (RADV NAVI31, gfx1100), Mesa 25.2.8, Linux Mint 22.3 (Ubuntu 24.04 base), kernel 7.0.0-38, GCC 13.3, Release.
Full suite (
ANYPS5_REQUIRE_VULKAN=1 ctest -j8): 1637/1639 with this PR, against 1637/1639 onmain. The failures are the same two on both, so this PR adds none:agc_driver_image_table_vouched_lookups(#7766, fixed by #7916) andagc_driver_guest_memory_lifetime(#7784). The PR adds no new ctest entry, and no test is skipped or removed compared withmain. This checks correctness only: I did not measure the speed-up.GPUs tested: AMD Radeon RX 7900 XT (RADV NAVI31, gfx1100), Mesa 25.2.8, Linux Mint 22.3 / kernel 7.0.0-38
AI-assisted: yes
Imported from boykopovar/AnyPS5. Originally posted by milescoviello on 2026-10-19T16:30:55Z.
@DavidAmsellem thank you for the RADV full-suite run. It matches
mainon gfx1100 with the same two failures, so this change adds none. It's in the description now.AI-assisted: yes (Claude Code)
Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-19T16:31:06Z.
This pull request is labeled
perf, so it needs the summary line of a capture onmainand one with the change, from the same title, scene and machine, as the Performance report form asks. Please paste bothaps5-summary.jsonllines, attach the two traces and add the output oftools/trace_digest.py.Imported from boykopovar/AnyPS5. Originally posted by milescoviello on 2026-10-19T21:35:23Z.
The title capture the
perflabel asks for is now in the description, with both summary lines, the traces and the digests: MXGP26 from boot, a cold shader cache,maindbac9fa15bagainstmain+ this PR (c5944449ddce). The fivemaincaptures span 17.7% in total shader prepare time, so the capture can't resolve this change, and the replay measurement in the description is still its measurement. This PR's capture falls inside the range of the fivemaincaptures.AI-assisted: yes (Claude Code)
Imported from boykopovar/AnyPS5. Originally posted by screamm on 2026-10-20T15:01:48Z.
Windows run of #7520 and #7522 together. Host: Windows 11 Pro 26220, NVIDIA GeForce RTX 4070 driver 596.21, Ryzen 7 5700X, WinLibs MinGW-w64 GCC 15.2 r7, Release. I stacked #7520 at
d484281b6and #7522 atcabf6ad53onmain1d4164552+ #7516 (f099292f8) + #7494 (0aab6c1f3). None of the four shares a file, and all merges were clean. Targeted build: the shader and AGC test targets pluslibs. MinGW accepts the newstatic_assert(sizeof(IrValue) == 128).shader_ir_use_rewritewith the tightened allocation bounds: 3 of 3.agc_masked_selectwith the new "no dead code left" check: 3 of 3. The rest of the 41-test set also passed 3 of 3, none skipped: the^shader_|^agc_shader_set,agc_ir_reclaim,agc_constant_phiand the f16/f32 min/max GPU tests.mainruns and this stack. I matched keys with the source-version word masked and compared payloads with host pointers masked: 5 Showcase entries hold a host address and differ between twomainruns too. Showcase's two dumped compute requests replay (agc_shader_replay --spv) to identical SHA-256 onmainand the stack.main/#7516+#7494 A B A B. Settings:ANYPS5_NO_SHADER_CACHE=1, a freshly relinked run directory,APS5_PROFILE_DRAW=1 APS5_PROFILE_GPU=1 APS5_TRACE_SUMMARY_EVERY=10. No stop line. These titles compile on the draw path; they have noShader.PrepareArtifactslices. Draw-path recompile time over the run, summed from the[draw] driver phaseslines:main2594.5 / 2223.2 ms.main46.8 / 35.0 ms.main, p50 8.34 ms in all.mainrange. This is one run, so it shows no regression, not a speed-up.main. The animated frames differ between twomainruns too, so they can't be compared.mainagainst the new UseRewrite bound, and MaskedSelect without the inner DCE).tools/check_conventions.py --base origin/mainandgit diff --check: pass.Not checked: a full build, the
agc_driver_GPU suite (the RADV full-suite run above covers that on AMD), replay CPU time, an A B A B of these two on their own, AMD/Intel on Windows.Written with Claude Code assistance (validation by a subagent, reviewed in this session).
Imported from boykopovar/AnyPS5. Originally posted by milescoviello on 2026-10-20T15:03:45Z.
Thanks @screamm, the first Windows title runs of this one, and the identical cache entries and replay hashes are the right check for a bit-exact change. Added to the description.
AI-assisted: yes (Claude Code)
Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-20T15:27:56Z.
Checked that every
Uses()caller only iterates it or usesall_of(SsaPass::TryRemoveTrivialPhicopies it), thatHasUses,UseCountandInvalidatereadoperandUses, that the use order is unchanged, and that the updatedshader_ir_use_rewritebounds match the single remaining list. The AMD run by @DavidAmsellem covers the full suite on RADV; merged.Imported from boykopovar/AnyPS5. Originally posted by milescoviello on 2026-10-20T15:46:48Z.
Thanks @adriadam10 for checking the callers and for the merge.
AI-assisted: yes (Claude Code)