shader: cache recompiler plans and SPIR-V on disk #988
No reviewers
Labels
No labels
bug
documentation
duplicate
enhancement
GPU
GUI
help wanted
question
Shaders
Video
wontfix
No milestone
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
github/KytyPS5--KytyPS5!988
Loading…
Reference in a new issue
No description provided.
Delete branch "gh-pr/988/head"
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?
@wuboo on GitHub · 2026-10-02 21:12 UTC · original
wuboo:pr/shader-disk-cache→main· +1081 −13 · 7 files · 1 commits · closed without mergingProblem
The shader recompiler runs again on every launch, even when the guest code and the specialization have not changed. On a warm intro of Astro Bot (PPSA21564) on macOS/MoltenVK, with pipelines already compiled, that was 8.6 seconds of recompiler time per launch (219 shaders) and 21.2 seconds of stalls longer than one second. The same launch reading a disk cache of the recompiler result dropped the recompiler time to 0.56 seconds (68 vertex shaders that my macOS integration tree re-emits for a per-vertex prototype) and the long stalls to 13.8 seconds. Startup stalls on the splash and boot screens shrank with it.
Current main has no on-disk recompiler cache. This tree also has no per-vertex prototype, so vertex shaders are cached here too.
Change
After a shader is translated, two files are written under
_ShaderCache/<title>/:rc_plan_<key>.bin— the resource plan (ResourcePlan): the value graph, memory and shader info, descriptor sources and the control-flow predicates.MaterializeResourcesstill runs at runtime.rc_perm_<key>.bin— one specialized permutation: SPIR-V plusCompiledShaderInfo(stage, bindings, export mask).The key is a 128-bit hash of the format version, a SHA-256 of the recompiler sources (
KYTY_RECOMPILER_SIGNATURE, computed at build time fromsrc/graphics/shader/**and the GPU format headers), the stage, the guest code and its back-code, the static stage state, wave size, user-data base and count, the host subgroup size, and the specialization (buffer stride/format/swizzle/zero-stride, image class/dimension/mips/swizzle/indirect/cube/fmask). The permutation key adds the push-data cursor.User data, guest addresses and flattened SRT bytes are not in the key and not in the file. A change to the recompiler sources changes the signature and every old file misses. A commit that does not touch those sources does not.
Each file has a magic, a format version, a payload size and an XXH3 of the payload. A short, long, or hash-mismatched file is ignored and the shader is compiled again. Deserialize checks opcode numbers and operand counts and returns false on a truncated or over-long buffer.
The cache is on by default.
KYTY_OPT_OFF=shader_disk_cacheturns it off. A build whose signature isnonedoes not write it. IR dumps (dump_ir) also skip it, so a logged compile is a fresh one.Tests
Release, macOS:
shader_disk_cache_tests: exit 0. Round-trip of a plan (phi loop, every immediate width, indirect image sources, integer-border sampler, control-flow blocks) reproduces the same bytes. Truncation, a trailing byte and a wrong format version are rejected. Flipping every payload byte does not crash. A permutation round-trips the SPIR-V words andhas_address_writes.shader_cfg_testswithKYTY_VS_LANE_FROM_INDEX=0: exit 0.scalar_provenance_tests: exit 0.resource_tracking_tests: exit 0.Release
kyty_emulatorfrom this branch links. The generated signature is a SHA-256, notnone.Limits
The measured 8.6 s to 0.56 s is from my macOS integration tree (the bring-up stack from #715 and related), where vertex shaders under the per-vertex prototype are not cached. This branch caches them, so a warm launch here should spend even less time in the recompiler. That has not been re-measured on this branch.
A loop or option that changes SPIR-V without changing the hashed sources, and that is not in the specialization key, would reuse a stale permutation. The source hash covers the recompiler.
KYTY_OPT_OFFis not mixed into the key: on this tree the recompiler does not read it, except for this cache's own switch.The file-header hash is checked when a file is read. The unit test corrupts the payload bytes, not a file on disk.
Environment switches that change the emitted SPIR-V must be part of the key. Today the recompiler reads none besides this cache's own switch. If #986 (
KYTY_SHADER_LOOP_LIMIT) lands, its value has to be added to the key; I'll rebase whichever of the two lands second.@coderabbitai[bot] on GitHub · 2026-10-02 21:22 UTC · original
Navigate logical layers of code changes, visualize relationships, and explore their blast radius.
No actionable comments were generated in the recent review. 🎉
ℹ️ Recent review info
⚙️ Run configuration
Configuration used: Repository: KytyPS5/KytyPS5/.coderabbit.yaml
Review profile: CHILL
Plan: Advanced
Run ID:
9a83248b-b993-43a9-b7eb-262a45f8bcb4📥 Commits
Reviewing files that changed from the base of the PR and between
e317465d8aanda8c1050bff.📒 Files selected for processing (7)
CMakeLists.txtsrc/generate_version.cmakesrc/graphics/host_gpu/renderer/pipeline/pipelineCache.cppsrc/graphics/shader/recompiler/ShaderDiskCache.cppsrc/graphics/shader/recompiler/ShaderDiskCache.hsrc/kytyGitVersion.h.intests/ShaderDiskCacheTests.cppIncluded review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 1 remain after this review.
📝 Walkthrough
Walkthrough
Adds an on-disk shader cache for resource plans and compiled permutations. The cache uses a signature derived from recompiler sources and keys that include shader and specialization data. Cache files are validated before use. When a cache entry is unavailable or invalid, the pipeline continues through translation and compilation. Adds serialization round-trip and malformed-input tests, and registers the test executable with CTest.
Suggested reviewers:
nmzikPriority: ➖ Normal
Change: Feature
Merge Risk: ⚪ Minimal · up to
a8c10The shader disk cache validates its files and falls back to compiling when an entry is missing or invalid. Cache keys cover the recompiler sources and all specialization inputs. No concrete defect remains that should block merging.
Security Architecture Review
Security architecture risk: 🟡 Moderate · up to
a8c10Persisted shader data can now reach unchecked memory access and bypass enabled shader validation. Exploitation requires access to an eligible cache file; remote access, privilege escalation, and code execution are not established.
Retained concerns
Security review details
Security Blast Radius
Security Findings and Attack Paths
Trust Boundaries and Controls
Resilience and Maintainability Implications
Hardening Proposals
🚥 Pre-merge checks | ✅ 4 | ❌ 1
❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
Full details: Docstring Coverage
Explanation
Docstring coverage is 9.09% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 66 functions across 4 files. (3 skipped: 3 unsupported.)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.
❤️ Share
Comment
@coderabbitai helpto get the list of available commands.@Nmzik on GitHub · 2026-10-28 21:51 UTC · original
No longer needed
Pull request closed