perf(shader): compile resource plan walks instead of interpreting them #6095
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/6095"
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 meh7an on 2026-10-14T21:46:55Z.
What
Every capture walks its resource plan's runtime sources (
SrtWalker::EvaluateRuntimeSources): the descriptor dwords, the flat SRT slots and the control-flow conditions. Each of these is an IR expression that theEvaluatorinterprets value by value. On the S2 page of Sonic Origins Plus's main menu, the walk cost the queue worker 3.58 samples per frame (APS5 sampler, 15 s), about 8% of its time. That was before #1193, which made the interpretation itself cheaper: values are memoized in a per-thread array instead of a hash table, and the walk reads a compact copy of the plan.A plan's walk is now compiled once into a flat node array, one node per IR value and context, and run on every capture. The node array (
SrtTape) is kept with the plan. The tape keeps the Evaluator's results and reads: the clean and read-first-lane contexts, lazy evaluation, the read trace, image activity, and a constant read past its V# reading 0. It also records the user-data use that the Evaluator records for draw relocation (#4943):asValue: a user-data word read as a value;asBase: a word that a raw read takes directly as its base address, which does not count as a value;reads: each raw read's address, together with that word.A walk the tape hands back to the Evaluator leaves no record behind.
The tape hands a walk to the Evaluator whenever it cannot finish it exactly as the Evaluator would. That covers a read through a null user-data root, an inaccessible word, or a failed operation.
APS5_NO_SRT_TAPE=1evaluates with the Evaluator only.APS5_VERIFY_SRT_TAPE=1runs both and reports any difference. The Evaluator gets its own user-data use there, which is compared with the tape's.IrResourcePlanguards move to 24 fields and, on Linux with libstdc++, 728 bytes.agc_shader_memory(verifySrtTape) walks three cases through both the tape and the Evaluator: the guarded-image program in four (T#, branch) states, a null SRT pointer, and a constant read past its V#. It requires every output to match, the user-data use included, and requires the guarded program's walk to report a user-data use. A tape that leaves out image activity, or does not mark a word it reads as a value, fails it.APS5_VERIFY_SRT_TAPE=1compared 1,000,000 walks of the S2 page with the Evaluator and found 0 differences. That run predates the user-data use, which main added with #4943 after this PR's previous rebase.Before #1193,
EvaluateRuntimeSourcesfell from 3.58 to 1.68 samples per frame, and the Evaluator's own time from 2.95 to 0. Against today's compact walk, the tape cuts the walk's time per capture from 4.70 to 2.84 us (see Tested). The title FPS does not move with it (10.52 against 10.39), so the gain is the walk's own time, about 1.3 ms per frame.Overlapping PRs:
ResourcePlan.hppand theIrResourcePlansize assert inShaderDiskCache.cpp.Tested
Windows 11 (10.0.26300), WinLibs GCC 15.2.0 (UCRT), Release, NVIDIA GeForce RTX 2080 Ti, driver 617.14.
db7176f8a, which records the user-data use (#4943). The tape runs first, and a walk it hands back goes to main's walk: the compact one, or the IR one when the plan has no compact copy. #1193 made the Evaluator a template over the node view without changing what it computes, so the tape still mirrors it.agc_shader_memory(withverifySrtTape),agc_shader_disk_cache,agc_shader_bindlessandagc_shader_srt_read_patching.agc_driver_gpu_indirect_arguments_misaligned_writer_aheadis not run: main itself does not compile it with MinGW.select_testsfails in its setup on Windows without the symlink privilege (WinError 1314fromsymlink_to), as on main.verifySrtTapefails when the tape'sasValueupdate is removed.APS5_VERIFY_SRT_TAPE=1,agc_shader_memory's walks report 0 differences, the user-data use included.APS5_TRACE_SECONDS=200:15on the same machine in one session, caches warm. Traces attached.The FPS shown in the title went from 7.53 to 7.96 (median of six readings).
These numbers predate #1193: their baseline interprets with the hash-table Evaluator. Against today's walk I measured the walk itself:
8856f180cwith this change. Baseline:APS5_NO_SRT_TAPE=1(main's compact walk). Change: the tape.APS5_PROFILE_DRAW=1, its[capture]line (printed every 10 s).The menu makes about 7,300 captures per second, so that is about 1.3 ms per frame at its 10.5 FPS. The title FPS does not show a change of that size: 10.52 with the compact walk, 10.39 with the tape (median of 12 readings, one run each).
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)Traces and summary lines (baseline f7a41addbcbb, this change b593e36eccba):
tape-traces.zip
Imported from boykopovar/AnyPS5. Originally posted by mrfelek on 2026-10-15T01:21:40Z.
Castlevania and Diablo II on this PR: about +11% and +13% presents per second on Linux
System: Linux, Ryzen 9 5950X, RTX 3080 Ti (NVIDIA 615.71.09)
Builds:
maineea428ca8, andmaineea428ca8plus this PR (cf6e64084), merged locally (see the conflict below)Results
Steady-state windows of 10 s, about 2 minutes in game.
main, one runmain+ this PR, one runmain, two runsmain+ this PR, one runAPS5_VERIFY_SRT_TAPE=1, so this is a speed check only.Merge conflict with
main(eea428ca8)One file conflicts:
IrMetadata/ResourcePlan.hpp.mainaddedIrValueArenawhere this PR addsSrtTapeSlot. They are independent, and keeping both resolves it. After the merge,ShaderDiskCache.cppfailsstatic_assert(sizeof(IrResourcePlan) == 584). The size is 600 with both changes, so that assertion needs updating in the rebase. The build and the runs were fine after that.AI-assisted; the runs are from my machine.
Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-15T12:14:40Z.
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/libs/prx/libSceAgcDriver/tests/ShaderMemory.cppcore/shader/recompiler/CMakeLists.txtcore/shader/recompiler/IntermediateRepresentation/include/IntermediateRepresentation/IrMetadata/ResourcePlan.hppcore/shader/recompiler/Optimization/src/SrtWalker/SrtDescriptorEvaluation.cppcore/shader/recompiler/ShaderDiskCache.cppcore/libs/prx/libSceAgcDriver/tests/ShaderMemory.cppcore/libs/prx/libSceAgcDriver/tests/ShaderMemory.cppcore/shader/recompiler/ShaderDiskCache.cppcore/shader/recompiler/Optimization/src/SrtWalker/SrtDescriptorEvaluation.cppcore/shader/recompiler/ShaderDiskCache.cppcore/shader/recompiler/CMakeLists.txtcore/shader/recompiler/ShaderDiskCache.cppcore/shader/recompiler/ShaderDiskCache.cppcore/shader/recompiler/ShaderDiskCache.cppcore/libs/prx/libSceAgcDriver/tests/ShaderMemory.cppcore/libs/prx/libSceAgcDriver/tests/ShaderMemory.cppcore/libs/prx/libSceAgcDriver/tests/ShaderMemory.cppcore/libs/prx/libSceAgcDriver/tests/ShaderMemory.cppcore/shader/recompiler/ShaderDiskCache.cppcore/libs/prx/libSceAgcDriver/tests/ShaderMemory.cppcore/shader/recompiler/ShaderDiskCache.cppcore/libs/prx/libSceAgcDriver/tests/ShaderMemory.cppcore/shader/recompiler/IntermediateRepresentation/include/IntermediateRepresentation/IrMetadata/ResourcePlan.hppcore/shader/recompiler/ShaderDiskCache.cppcore/libs/prx/libSceAgcDriver/tests/ShaderMemory.cppcore/shader/recompiler/CMakeLists.txtcore/shader/recompiler/IntermediateRepresentation/include/IntermediateRepresentation/IrMetadata/ResourcePlan.hppcore/shader/recompiler/ShaderDiskCache.cppcore/libs/prx/libSceAgcDriver/tests/ShaderMemory.cppcore/shader/recompiler/IntermediateRepresentation/include/IntermediateRepresentation/IrMetadata/ResourcePlan.hppcore/shader/recompiler/ShaderDiskCache.cppcore/libs/prx/libSceAgcDriver/tests/ShaderMemory.cppcore/shader/recompiler/Optimization/src/SrtWalker/SrtDescriptorEvaluation.cppcore/shader/recompiler/ShaderDiskCache.cppcore/libs/prx/libSceAgcDriver/tests/ShaderMemory.cppcore/libs/prx/libSceAgcDriver/tests/ShaderMemory.cppcore/shader/recompiler/CMakeLists.txtcore/shader/recompiler/CMakeLists.txtcore/shader/recompiler/ShaderDiskCache.cppcore/shader/recompiler/ShaderDiskCache.cppcore/libs/prx/libSceAgcDriver/tests/ShaderMemory.cppcore/shader/recompiler/CMakeLists.txtcore/shader/recompiler/ShaderDiskCache.cppcore/libs/prx/libSceAgcDriver/tests/ShaderMemory.cppcore/shader/recompiler/CMakeLists.txtcore/shader/recompiler/IntermediateRepresentation/include/IntermediateRepresentation/IrMetadata/ResourcePlan.hppcore/shader/recompiler/ShaderDiskCache.cppcore/libs/prx/libSceAgcDriver/tests/ShaderMemory.cppcore/shader/recompiler/CMakeLists.txtcore/shader/recompiler/IntermediateRepresentation/include/IntermediateRepresentation/IrMetadata/ResourcePlan.hppcore/shader/recompiler/ShaderDiskCache.cppcore/shader/recompiler/CMakeLists.txtcore/shader/recompiler/ShaderDiskCache.cppcore/libs/prx/libSceAgcDriver/tests/ShaderMemory.cppcore/libs/prx/libSceAgcDriver/tests/ShaderMemory.cppcore/libs/prx/libSceAgcDriver/tests/ShaderMemory.cppcore/libs/prx/libSceAgcDriver/tests/ShaderMemory.cppcore/shader/recompiler/CMakeLists.txtcore/shader/recompiler/IntermediateRepresentation/include/IntermediateRepresentation/IrMetadata/ResourcePlan.hppcore/shader/recompiler/ShaderDiskCache.cppcore/libs/prx/libSceAgcDriver/tests/ShaderMemory.cppcore/shader/recompiler/IntermediateRepresentation/include/IntermediateRepresentation/IrMetadata/ResourcePlan.hppcore/shader/recompiler/ShaderDiskCache.cppImported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-16T22:50:59Z.
Review: DISMISSED
Thanks for the tape, the ops match
SrtEvaluator.cppandagc_shader_memorypasses on the merge with main (Linux). One thing blocks it:SrtTape::Runnever fillsruntime.userDataUse.materializeCapturesets it (Recompiler.cpp:1347) andResourceMaterializer.cpp:803passes the plan's ownmaterializationSources/cleanFlatSlots, so every capture takes the tape path. The Evaluator recordsasValue(SrtEvaluator.cpp:422),asBase(:205) andreads(:243); the tape records none, socapture.userDataUsestays empty and reachestagVariantRuns(DrawCapture.cpp:164).verifyTapehides it: it copies the runtime with the sameuserDataUsepointer, so the Evaluator fills it during verification andAPS5_VERIFY_SRT_TAPE=1reports 0 differences.verifySrtTapedoes not setuserDataUseeither.To fix: record the same
userDataUsein the tape (UserData nodes, and the base/read origin for raw reads), giveverifyTapeits ownSrtUserDataUseand compare it (asValue, asBase, reads) with the one the tape produced, and makeverifySrtTaperun withuserDataUseset and require equality. Mutating the new check (dropping theasValueupdate) should make it fail.Smaller notes: the standard "revert the non-test files" negative can't build because
CMakeLists.txtlistsSrtTape.cpp; I mutated the tape's branch selection by hand andagc_shader_memory_testsfails, so that part is covered. The title FPS did not move in your own measurement (10.52 vs 10.39), so the gain is the ~1.3 ms/frame walk time; please say so in the description. #6946 also editsResourcePlan.hppand theIrResourcePlansize assert inShaderDiskCache.cpp, so whichever lands second needs a rebase.Imported from boykopovar/AnyPS5. Originally posted by meh7an on 2026-10-17T00:06:11Z.
Thanks, that was a real gap. The user-data use came with #4943 after this PR's last rebase, and the tape never recorded it. Fixed in
acee01a4e:UserDatanodes setasValue.asBase, notasValue.(address, origin)at the point where the Evaluator does.verifyTapegives the Evaluator its ownSrtUserDataUse, seeded with the state before the walk.asValue,asBaseand the reads, sorted and deduplicated astagVariantRunsuses them.verifySrtTaperuns both walks withuserDataUseset. It requires them to be equal and requires the guarded program to report a use. Removing the tape'sasValueupdate makes it fail.Rebased onto
db7176f8a. The full suite passes 1347 of 1349: the misaligned test is not built with MinGW, andselect_testsneeds the symlink privilege.