perf(recompiler): keep the front-end resource plan in the shader disk cache #4883
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/4883"
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 nikowolf123 on 2026-10-13T01:47:52Z.
What
With a warm shader cache, every launch still runs the whole recompiler front end once for every shader.
getSourcebuildsResourceProgram(decode, CFG, structurizer, translation, SSA, the fold passes, SrtWalker, ResourceTracker, thenExtractPlan) beforefindOrCompileVariantlooks in the disk cache, because the disk cache keeps compiled variants but not the resource plan. Only SPIR-V emission is skipped. #2292 measured 0.3 to 0.8 s per registration with 0 cache misses and lists this fix as its option 4. #2774 (about 1,950 hits, no misses) found the plan "is built beforeShaderDiskCache::Loadis consulted". #3504 hit its slow pass "with a warm shader cache", and #4404 reports hundreds of queued preparation jobs on a warm run with zero misses.This PR stores the plan in the disk cache and loads it before the front end:
ownPlanValues), so it can be written out.EncodePlanwrites the value graph in storage order as LEB128 integers (opcode, type, id, flags, register, immediate, each argument as the zigzag distance to the value's own index, phi predecessor blocks), the roots as value indices, and the plain data. ShaderInfo uses the variant's encoder, now split out ofencodeInfo.DecodePlanrebuilds the graph the wayownPlanValuesdoes. It rejects an unknown opcode, an overlong integer, and a value, block, descriptor source or image index past the end, including an image table's heap and material sources and the targets of control-flow edges.RecompileCacheKey::Buildand the host subgroup size), every code word and the switch key, under a plan tag. The binding layout is not in it, so all layouts of a shader share one plan. FormatVersion 30. Size asserts and field counts cover the plan structures (below).getSourcetriesLoadPlanfirst. On a hit the program stays null:findOrCompileVariantalready rebuilds it when no stored variant fits a layout. On a miss it builds the plan as before and stores it. A plan the front end refuses is not stored. The disk is not used when the cache is off, under the debug probe, withAPS5_DUMP_IR, or withAPS5_NO_PERF_DISK_PLANS=1, a cache-neutral switch for comparisons.FrontEndBuilds()countsPrepareResourceProgramcalls, for the tests.Field counts: the plan and ShaderInfo encoders write their structures field by field, and until now the size asserts were the only reminder to update them. A field that fits in existing padding does not change the size.
main's #5097 addedShaderInfo::layerExportedandsizeof(ShaderInfo)stayed 232. The three bools #5089 adds toDescriptorSource::IndirectImagekeep it at 28. A size assert catches neither, so a missed encoder update would go unnoticed and a warm cache would hand back entries without the new field. Next to the size asserts, astatic_assertnow pins the field count of every aggregate the plan codec reaches:IrResourcePlan,GuestRegister,MemoryInfo,DescriptorSource,IndirectImage,ResourceBlock,SrtRead,UniformFill,UniformFillPlan, andShaderInfowith its buffer, image, sampler, sampled-pair and stage input and output records. The count is found by brace-initializing the aggregate with more and more values of a type that converts to anything, until that stops compiling. This is plain C++20 and does not depend on the ABI, so unlike the size asserts it is checked on MinGW too.IrValueis a class with private state, so it keeps only its size assert.This adds no new correctness assumption. A warm run on
mainalready pairs a variant compiled by an earlier process with a plan rebuilt by the current one, and both come from the same front end under the same SourceVersion. Specialized modules are still built in each process. Storing them would be a separate change.Bytes per plan entry: 1.4 to 7.1 KB for the 15 in-tree fixtures in the test (4.3 KB on average). Control-flow conditions can pull most of a program into the plan, so the worst case is large. A synthetic compute shader with a branch every 20 dwords has plans of 518 KB, 2.07 MB and 8.25 MB at 2,038, 8,038 and 32,038 dwords. Its variant entries are 935 KB, 3.70 MB and 14.7 MB. Fixed-width fields would have made those plans 2.5 MB at 2,038 dwords, which is why values are LEB128.
Overlap, all textual: #4806 changes
Words()toRead()next to these hunks in Recompiler.cpp and ShaderDiskCache.cpp. #4732 generates the cache version, and #4475 and other open PRs also bump FormatVersion. #4271 and #4757 edit Recompiler.cpp neargetSource. #1341 adds an execution plan toIrResourcePlan, which would be rebuilt afterDecodePlan. #5089 adds three fields toIndirectImage. Whichever of #5089 and this PR lands second must add them to the plan encoder, and the field count turns a missed update into a build error. No open or closed PR stores the plan.Tested
WSL Ubuntu, i9-14900KF, GCC 15, the CI configuration (Release, SPIRV-Tools on, lavapipe), with the series on
maind1fd9372b. Also native Windows 11 (WinLibs GCC 15.2.0, Release with tests) on an RTX 5080 (NVIDIA 616.92), same head: fullctestwithANYPS5_REQUIRE_VULKAN=1, 1021 tests, 0 failed, 7 skipped astools/check_skips.py --job windowsexpects;agc_shader_disk_cache, including its two-process runs, passes there. Not tested with a title.agc_shader_disk_cache(1.2 s) gains these tests:agc_shader_memory, except "descriptor invariant phi", which is its "descriptor phi image" with both branches loading the same descriptor. They cover SRT reads, guarded and chained pointers, control-flow conditions, an indirect image table, a loop phi, an invariant descriptor phi and a fragment shader. The hand-built plan gives every plan-level field, everyMemoryInfoand image table field, the uniform fill and a 64-bit immediate a value other than the default.APS5_NO_PERF_DISK_PLANS=1, a process on the same cache runs the front end for all 15 fixtures, loads and stores no plan, still hits the 15 stored variants, and produces the same output.getSourcehunk reverted, the second process reportsfront end builds 14, plan hits 0, plan misses 0, plan load failures 0, variant hits 14, variant misses 0and the test fails. With the change it passes.EncodePlanwrite the default value instead, which is the same as leaving the field out of both the encoder and the decoder. The test fails for every one of them.43e3a70f8, with local instrumentation that is not part of the PR: everyResourceMaterializer::Materializecall also encoded, decoded and re-encoded its plan, materialized the decoded copy with the same runtime, and aborted on any difference. All 1002 tests passed. Across 266 test executables besides the disk-cache test, more than 3,700 materializations matched (snapshots, read traces and error messages), with no mismatch.Performance: there is no title or Performance report summary line here, only a synthetic harness. It is a compute shader with 16 V#s loaded from an SRT and a buffer load, an ALU chain and a divergent branch every 20 dwords. It calls
PrepareShaderin one process with an empty cache (cold) and then in a new process with the same cache (warm), withAPS5_SPIRV_OPT=none.main43e3a70f8and this series on the same base were built from one tree and run interleaved on the same machine. Since thenmainhas gained one front-end pass and one ShaderInfo field (#4031's ancillary word), which these runs do not include. Load average was 7 to 20 from other jobs. Medians of 7 runs (5 at 32k):PrepareShader395 ms on main, 25 ms with this PR (15.7x).APS5_NO_PERF_DISK_PLANS=1, the branch's warm runs match main: 420 ms, 698 ms and 5.2 s.I can attach the JSON lines of every run, the harness source and the instrumentation patch. A warm-start comparison on a title would show the real gain: start-up time with and without
APS5_NO_PERF_DISK_PLANS=1against the same cache, from the reporters of #2292, #2774 or #4404.main0c3dc9ca2for this pull request: it builds in the CI configuration and the 32 shader, pipeline-library, structurizer and control-flow tests (ctest -R 'agc_shader|agc_driver_prepared|agc_pipeline_library|agc_structurizer|control_flow') pass on lavapipe.Rebased onto
main60fb61870.mainhad added areclaimargument togetSourceandResourceProgram(57b2dc858), which conflicted with the disk-plan path ingetSource. The plan loaded from the disk cache is used as before; when there is none,ResourceProgramis built withmain'sreclaimargument.PrepareShaderstill passesreclaim = false, and on a disk-plan hit its program is rebuilt by the existing fallback infindOrCompileVariantwhen a variant must be compiled. The per-file diff is unchanged. WSL2 Ubuntu 26.04, lavapipe (Mesa 26.0.8), CI configuration: the disk cache, structurizer and control-flow tests 16 of 16, fullctest1067 of 1067.Phi coverage with #5181: the new fixture "descriptor invariant phi" loads one image descriptor from the same SRT offset on both sides of a branch. Its plan keeps eight invariant phis as descriptor dwords, and the CPU walk evaluates them, from the built and from the stored plan, to the image descriptor in the SRT. The only other planned phi is the loop counter of "bindless sample in a loop latch", in a latch condition the walk cannot evaluate. With #5181 16bd6505e merged locally (the plan codec moved to its value arena), the phi check passes with or without the new fixture, because #5181 counts
CompositeExtractU32x2as evaluable on its own and keeps that latch condition. With extracts checked through their source instead, it fails withno fixture plans a phiwithout the fixture and passes with it. On the branch,ctest -R 'disk_cache|shader_cache|agc_shader|recompiler|structurizer': 16 of 16.Rebased onto
main0214069e3.main's #5097 addedlayerExportedto the ShaderInfo encoder, which this PR splits intoencodeShaderInfoanddecodeShaderInfo. The flag is now written last in both, right afterrasterPrimitive, as onmain. A variant entry's payload therefore has the same bytes as onmain, and plans store the flag too. The entry round trip now setslayerExported, as it does the other ShaderInfo flags, so an encoder and decoder that both skip it fail withentry round trip: shader info differs. #5097 also took FormatVersion 29, so this PR moves to 30. Againstmain, the series changes the same lines as before the rebase except the FormatVersion line, plus the field-count asserts. Nothing elsemainchanged after60fb61870touches a structure the codecs write, apart from the newStageInputKind::LayerExported, which the stage-input encoder writes as its integer value. WSL2 Ubuntu 26.04, lavapipe (Mesa 26.0.8), CI configuration:ctest -R 'disk_cache|shader_cache|agc_shader|recompiler|structurizer'16 of 16, fullctest1077 of 1077 (43 skipped themselves: lavapipe features, and the WSLg display has no 120 Hz mode). The field-count asserts compile in that build (GCC 15.2), with Clang 21, and in a native Windows compile ofShaderDiskCache.cppwith MinGW GCC 15.2.0 (WinLibs r7). With #5089's three bools added toIndirectImage, every size assert still holds and the build stops atstatic assertion failed: IndirectImage changed: update the plan encoder(the comparison reduces to '(10 == 7)') with each of those compilers. A bool added in the padding ofShaderInfo,MemoryInfo,IrResourcePlan,StageInputorSamplerResourcealso keeps its size and stops the build at that structure's field count.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 EagleBearerYS on 2026-10-13T15:13:29Z.
A title measurement: Resident Evil Village (PPSA01556, 01.210.000) on Windows 11, RTX 3070 Laptop, WinLibs GCC 15.2.0 r7, Release. It registers its whole PSO cache at boot behind a black screen, about 6,000 shaders, so the boot measures shader preparation directly.
The build is
mainb478fbbb1plus #4949's register change (so it gets past its first frame) and this PR. Merging this PR needed one conflict resolved inRecompiler.cpp: #4271 added areclaimparameter togetSourceandResourceProgram, which I kept. I filled the cache with one cold run, then ran warm runs on the same cache, alternatingAPS5_NO_PERF_DISK_PLANS=1and the default. Shaders back are the[shader-disk-cache]totals:The cold run took about 240 s to reach 2,800 shaders. The front end is the bottleneck of this boot, and with this PR a warm boot skips most of it.
A heads-up on the tests: #5181 leaves branch conditions the CPU walk can never evaluate out of resource plans. I had the same idea locally. With it,
agc_shader_disk_cache's coverage check failed on top of this PR with "no fixture plans a phi": the only fixture phis are the loop counter of the "descriptor phi loop" fixture (not invariant, so never evaluable) and the branch on it. Each PR passed on its own. #5181 is likely to hit the same check. A fixture whose plan keeps an evaluable phi would cover both, for example an invariant phi that feeds a descriptor dword.Imported from boykopovar/AnyPS5. Originally posted by nikowolf123 on 2026-10-13T15:38:52Z.
Thank you, that's the title measurement this PR needed. On a warm cache, Resident Evil Village gets all of its roughly 2,900 shaders back by 32 s with plans from the disk cache, against about 1,400 without them.
The
reclaimconflict you resolved is the one I resolved in today's rebase (3d86335b9), the same way: the disk-plan path stays, andResourceProgramgetsmain'sreclaimargument when there is no stored plan.Thanks also for the #5181 heads-up. I'm adding a fixture whose plan keeps an evaluable phi, so the coverage check holds in whichever order the two PRs land. I'll post here when it's pushed.
Imported from boykopovar/AnyPS5. Originally posted by nikowolf123 on 2026-10-13T17:34:15Z.
@EagleBearerYS the fixture is pushed in
ce8944f85. "descriptor invariant phi" loads one image descriptor from the same SRT offset on both sides of a branch. Its plan keeps eight invariant phis as descriptor dwords, and the CPU walk evaluates them from both the built plan and the stored one.As it turns out, with #5181's current head (16bd6505e) the phi check would pass even without it. #5181's
MayEvaluatecountsCompositeExtractU32x2as evaluable without looking at its source. So the loop-latch condition of "bindless sample in a loop latch" stays in the plan, and so does its counter phi, even though the walk can never evaluate that condition. If extracts were checked through their source, that condition and its phi would be dropped, and the check would fail withno fixture plans a phi. The new fixture keeps phis covered in either case, whichever PR lands first.The same push rebases onto
main(format 30, withmain'slayerExportedcarried into the split ShaderInfo codec) and adds664ad7703, which pins the field count of every structure the plan codec writes, so a field added inside padding (as in #5089) breaks the build until the codec writes it.Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-13T17:45:57Z.
Other open pull requests touch the same code. Merging one will break the other, or both implement the same thing. If this one builds on them, list them in Depends on.
main)core/libs/prx/libSceAgcDriver/tests/ShaderDiskCache.cppL20-21, L1084core/shader/recompiler/ShaderDiskCache.cppL560-561, L671