perf(shader): end dead code elimination when no released value is left dead #7494
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/7494"
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-18T09:34:40Z.
What
DeadCodeEliminator::Eliminaterepeats a pass over every block until a pass removes nothing. So every call that removes anything pays one more full pass that only confirms the result.The pass walks the blocks and their instructions backwards. An instruction it has already visited can only become dead later in the pass when the removal of one of its users drops its last use. For a non-phi user, that argument is usually defined earlier and is still ahead in the walk. The exception is an argument the walk has already passed, such as a phi's input from a later block. Uses only shrink during the pass, and an instruction's side effects don't change.
The pass now keeps the arguments of the instructions it removes. It runs again only when one of them is still in a block, has no uses and has no side effects. If none is, nothing removable is left in the blocks, and the next pass would have removed nothing. When one is, the next pass runs as before. The set of removed instructions, the order of the instructions that remain, and so the SPIR-V are unchanged.
agc_ir_reclaimnow checks that an unused phi in the first block and its input in a later block are both removed. That passes onmain(in a second pass) and with this PR. With the recheck replaced bychanged = false, it fails with "the input of an eliminated phi from a later block was not eliminated".Tested
Ubuntu 24.04 (Linux 6.8), GCC 13.3, NVIDIA RTX 4070 Ti, driver 595.99.02.
ctest -R "^agc_|^shader_ir"oncb1d6e5bd6: 709/709 pass (3 skipped: host import sparse, sparse dma-buf import, stencil ref export). After rebasing onto9caecfd2d5:agc_ir_reclaim,agc_masked_selectandagc_constant_phipass.agc_shader_replay --spv(Release,ANYPS5_ENABLE_SPIRV_TOOLSoff,ANYPS5_NO_SHADER_CACHE=1) on 420 shader requests I had dumped withAPS5_DUMP_SHADERS: 355 from MXGP 26 (PPSA30839, two dumps), 61 from PPSA31162 and 4 from another app. Onmaincb1d6e5bd6and with this PR, all 419.spvfiles that are produced have the same SHA-256, and the replay output is identical, including the one request that fails on both.Pass time, from a temporary timer and pass counter in
Eliminatethat are not part of this PR:mainmainIn the PPSA30839 requests, 334 passes beyond the first per call remain with this PR, for values that the walk had already passed. The largest shader's whole replay takes about 1.2 s of CPU time, so its gain is about 3.5% of that.
Title level: no speed claim. This is background shader preparation. I didn't run a title for it.
This is independent of #7431 (
PlanBuilder::PatchReads) and #7491 (MaskedSelectEliminator), which change other passes of the same shader preparation. #7491 callsEliminateafter rounds that remove a select.tools/check_conventions.py --base upstream/mainpasses.tools/pr_overlap.pyreports no overlap with open pull requests.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 (da0ca697c5e9). 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 is above all fivemaincaptures in total prepare time. With one capture of the change I can't tell that apart from noise, and the replay measurement above points the other way. I can repeat the capture if that would help.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 (0aab6c1f35, a local merge).main: 815 of 815 pass. With this PR, 814 of 815 pass:agc_storage_written_buffer_orderaborted in the dynamic loader before the test began (Inconsistency detected by ld.so: ... elf_machine_rela_relative: Assertion ... failed). A test in another PR's run hit the same assertion that evening, so it isn't this change. Rerun 5 times under the layer on the same merge,agc_storage_written_buffer_orderpasses 5 of 5 with no VUIDs. Neither log reports the layer missing. VUIDs onmainand with this PR:VUID-vkCmdCopyBuffer-srcBuffer-00118×2,VUID-vkCmdDraw-multisampledRenderToSingleSampled-07285×2,VUID-vkCmdWaitEvents-srcStageMask-01158×2,VUID-VkImageViewCreateInfo-usage-02275×6,VUID-VkPipelineColorBlendStateCreateInfo-pAttachments-00605×18,VUID-vkSetEvent-event-09543×2. None is new with this PR.main1d4164552(and with #7520/#7522 in a second arm):agc_ir_reclaim(including the later-block phi case),agc_masked_select,agc_constant_phiand the 41-test shader set pass 3 of 3; 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 milescoviello on 2026-10-19T21:35:32Z.
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 (da0ca697c5e9). 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 is above all fivemaincaptures in total prepare time. With one capture of the change I can't tell that apart from noise, and the replay measurement above points the other way. I can repeat the capture if that would help.AI-assisted: yes (Claude Code)
Imported from boykopovar/AnyPS5. Originally posted by screamm on 2026-10-20T15:01:47Z.
Independent run, Windows. Host: Windows 11 Pro 26220, NVIDIA GeForce RTX 4070 driver 596.21, Ryzen 7 5700X, WinLibs MinGW-w64 GCC 15.2 r7, Release. This PR at
0aab6c1f3was merged with #7516 (f099292f8; they share no file) onmain1d4164552(arm A). A second arm added #7520 (d484281b6) and #7522 (cabf6ad53). All merges were clean. Targeted build: the shader and AGC test targets pluslibs.agc_ir_reclaim, including the new later-block phi case: 3 of 3 on both arms. Also 3x on both arms:agc_masked_select,agc_constant_phi, the^shader_|^agc_shader_set and the f16/f32 min/max GPU tests (41 tests). Arm A passed 40 of 41 and arm B 41 of 41, none skipped. The arm A exception,shader_cache_runtime_sources, hit its 180 s limit once on a loaded host and passed 3 of 3 serially.mainruns, arm A and arm B. 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 on all three builds. That matches your replay result: the SPIR-V doesn't change.main, A,main, A, then one B run, 90 s each. Settings:ANYPS5_NO_SHADER_CACHE=1, a freshly relinked run directory each time,APS5_PROFILE_DRAW=1 APS5_PROFILE_GPU=1 APS5_TRACE_SUMMARY_EVERY=10. No run had a stop line. These titles have noShader.PrepareArtifactslices, because they compile on the draw path. So I summed the draw-path "recompile" row of the[draw] driver phaseslines over each run:main2594.5 / 2223.2 ms (16.7% apart), arm A 2530.6 / 2473.1 ms (+3.9% on the mean), arm B 2245.5 ms.main46.8 / 35.0 ms, arm A 42.2 ms, arm B 39.7 ms. The other arm A run ran during a load spike on the host and is not usable.mainspread, as you expected: a title capture can't resolve this change. These are the first title runs of it on Windows.mainand both arms. The animated frames of all three titles differ between twomainruns too, so they can't be compared.changed = falsecheck; identical output stands in for it.tools/check_conventions.py --base origin/mainandgit diff --check: pass.Not checked: a full build, the
agc_driver_GPU suite, a pass count or DCE timing (that needs your temporary counter), AMD/Intel, Linux.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:39Z.
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 milescoviello on 2026-10-21T03:25:42Z.
A second title for this one: Crimson Desert (PPSA25286), whose start-up is CPU-bound on preparing registered shaders. I dumped 6,065 of its registration requests as they were prepared, then replayed them with
agc_shader_replay --prepare. Release, SPIRV-Tools off,ANYPS5_NO_SHADER_CACHE=1, i7-13700K, task-clock over a fixed subset of 152 requests, 8 interleaved runs per build:main5d10ea9ea0main+ this PR (0aab6c1f35, merged cleanly)That's 2.5 % less CPU for these requests. The prepared SPIR-V of the 152 requests is byte-identical to
main's.I also tried replacing the remaining rescans with a worklist over the removed instructions' operands. All 6,065 prepared modules and 458 recompiled MXGP and SMB3D modules came out identical, but it measured 16.11 s (16.04 to 16.20 s), no better than this PR. So I'm not proposing it: this PR already takes nearly all of the dead-code pass's cost here.
AI-assisted: yes (Claude Code)
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-21T23:46:22Z.
Review: APPROVED
The termination check is sound for the reverse walk: removing an instruction can only make one of its operands newly dead, so the pass repeats only when a released operand still belongs to a block, has no uses and has no side effects. The new later-block phi regression exercises that exception, and the reported control fails when the recheck is removed. The replay reports byte-identical SPIR-V/output hashes across 419 produced modules, and the second title replay shows a small CPU-time reduction with overlapping ranges. The MXGP title capture is noisy and slower than all five main captures; the description appropriately makes no title-speed claim. Conventions, eligibility, labels and report pass; hosted product jobs are skipped. Current
tools/pr_overlap.pyreports no overlap across 1,105 open PRs. I did not run the tests locally. No blocking issue found; approved.Imported from boykopovar/AnyPS5. Originally posted by milescoviello on 2026-10-21T23:47:38Z.
Thanks @MarcZX10 for the review, and for re-running
pr_overlap.pyacross the open PRs.AI-assisted: yes (Claude Code)