renderer: check scalar reads against merged write ranges #1285
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!1285
Loading…
Reference in a new issue
No description provided.
Delete branch "gh-pr/1285/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?
@NeySlim on GitHub · 2026-10-09 07:25 UTC · original
NeySlim:perf/scalar-read-overlap→main· +136 −0 · 3 files · 2 commits · closed without mergingProblem
Before recording a draw or dispatch,
CommitBindingschecks that the guest memory read to specialize its shaders (ResourceSnapshot::specialization_reads) is not written by the same draw. For every read it walks every bound image (a slot lookup each) and every written shader buffer. In God of War Ragnarök (PPSA08332, 05.005) a frame records about 2,100 draws and dispatches with about 255,000 such reads, so about 1.3 million range comparisons per frame. In a profile of the opening cutscene the check takes 6% of the samples of the GPU command thread (ImageRangeOverlapsalone 3.3%), on a branch that already collects the written ranges once per draw instead of once per read.Change
GatherWriteRangescollects the ranges written by the bound images (data, stencil and metadata) and shader buffers of the draw once, the first time a stage has reads to check, then sorts and merges those that overlap or touch.ScalarReadsMayOverlapchecks each read against the merged ranges with a binary search.MergeGuestRangesexits on an empty or wrapping write range, asImageRangeOverlapsdid for every write before, so an invalid write range still stops the emulator instead of being merged with a wrapped end.A read overlaps the union of the writes exactly when it overlaps one of them, so the decision is the same as before.
Test
New host check
ScalarReadOverlapinshader_recompiler_compute_tests(runs with every invocation): range merging, reads that touch a write without overlapping it, reads spanning several writes, invalid reads, and 2,000 random draws compared with the pairwise check. Breaking the merge (merging only overlapping ranges, or comparing ends with<=) makes it fail.ScalarReadOverlapWrappingWrite(non-Windows, it forks like the other fatal checks) expects a wrapping write range to exit; it fails without the range check inMergeGuestRanges.Validation
Linux, Ryzen AI Max+ 395, Radeon 8060S, Mesa RADV 26.2.3.
God of War Ragnarök from a new game, alternating every 5 s between this change and the previous code within one session. Measured on a branch with other pending GPU thread changes, which already collects the written ranges once per draw, per frame:
CommitBindingson the GPU command thread11 (cutscene) and 18 (sled) pairs of 5 s periods. Frame time differences stayed within the noise of these scenes.
resource_tracking_testsandresource_materialization_testspass.shader_recompiler_compute_tests, one case per process: same results asorigin/main(597 of 607 cases pass on both; the same 10 fail on this RADV setup).Not tested on Windows or macOS. The change is platform-independent C++ in the renderer.
Overlap with #1211
#1211 also rewrites the scalar-read loop in
CommitBindings(it collects the image write ranges once per draw). The two branches conflict indescriptors.cpp; whichever is merged second needs a rebase, and this PR replaces that part of #1211.AI use
I used Claude Code (AI agent) to profile the GPU command thread, write the change and the test, and run the measurements and tests above on my machine. I reviewed the diff and the results before opening this PR.
@coderabbitai[bot] on GitHub · 2026-10-09 07:29 UTC · original · edited 2026-10-17 11:05 UTC
📝 Walkthrough
The change adds helpers to merge guest-memory write ranges and check scalar-read overlap.
RenderExecutorgathers writable image and shader-buffer ranges, then uses the overlap check inCommitBindingsbefore detailed per-resource checks. Host tests cover merging, interval boundaries, invalid ranges, and generated overlap cases.Suggested reviewers:
defektuPriority: ➖ Normal
Change: Refactor
Merge Risk: 🟠 High · up to
8e87bValid shader reads can bypass write-conflict checks, and the host test detects the error. Fix the search before merging.
Pre-merge checks:
4 ·
1
Full details: Docstring Coverage
Explanation
Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 7 functions across 2 files. (1 skipped: 1 too large.)
Resolution
Write docstrings for the functions missing them to satisfy the coverage threshold.
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.@ -887,0 +914,4 @@const auto next = std::upper_bound(writes.begin(), writes.end(), address,[](uint64_t value, const GuestRange& range) { return value < range.End(); });if (next != writes.end() && next->address < address + size) {@coderabbitai[bot] on GitHub · 2026-10-17 11:05 UTC · original · edited 2026-10-17 11:28 UTC
🗄️ Data Integrity & Integration | 🟠 Major | ⚡ Quick win
🔎 Supported by static analysis
🏁 Script executed:
Repository: KytyPS5/KytyPS5
Length of output: 9281
🏁 Script executed:
Repository: KytyPS5/KytyPS5
Length of output: 319
Fix the binary-search comparator for reads inside a write range.
For a read inside a single merged write, the current comparator makes
std::upper_boundreturnwrites.end().ScalarReadsMayOverlapreturns false, soCommitBindingsskips the detailed conflict checks.Suggested fix
📝 Committable suggestion
@coderabbitai[bot] on GitHub · 2026-10-17 11:05 UTC · original · reviewed
Actionable comments posted: 1
ℹ️ Review info
⚙️ Run configuration
73f46ca7-b2e2-4aa9-8577-43498a657b30📥 Commits
Reviewing files that changed from the base of the PR and between b57e4252dcf857df173a0a8f37b6c4fe86a45faa and
8e87bdf0e1.📒 Files selected for processing: 3
src/graphics/host_gpu/renderer/pipeline/descriptors.cppsrc/graphics/host_gpu/renderer/render.htests/ShaderRecompilerComputeTests.cppIncluded review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 7 remain after this review.
@ -887,0 +914,4 @@const auto next = std::upper_bound(writes.begin(), writes.end(), address,[](uint64_t value, const GuestRange& range) { return value < range.End(); });if (next != writes.end() && next->address < address + size) {@NeySlim on GitHub · 2026-10-17 11:28 UTC · original
I don't think so:
upper_boundcalls the comparator ascomp(value, element), so it returns the first write whose end is past the read address. For a read inside[0x1000, 0x1180)that's the write itself, notend(), andnext->address < address + sizecatches it. The random draws inCheckScalarReadOverlapcompare against the pairwise check and include reads inside a single write, and they pass.@ -887,0 +914,4 @@const auto next = std::upper_bound(writes.begin(), writes.end(), address,[](uint64_t value, const GuestRange& range) { return value < range.End(); });if (next != writes.end() && next->address < address + size) {@coderabbitai[bot] on GitHub · 2026-10-17 11:28 UTC · original
@NeySlim You’re right. My Python model reversed the
std::upper_boundbranches, so its mismatch did not demonstrate a defect in your code.The comparator finds the first write with
range.End() > address. Because the merged writes are sorted and disjoint, their ends are increasing. For a read inside[0x1000, 0x1180), the search returns that write, and the remaining check detects the overlap.No comparator change is needed. I withdraw this finding. Thanks for the correction.
✏️ Learnings added
✅ Review thread resolved.
You are interacting with an AI system.
Pull request closed