shader: keep branches on written buffers unknown #1630
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!1630
Loading…
Reference in a new issue
No description provided.
Delete branch "gh-pr/1630/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?
@Nawid3333 on GitHub · 2026-10-18 04:11 UTC · original
Nawid3333:shader/written-buffer-control→main· +101 −1 · 2 files · 3 commits · closed without mergingFixes #1660 (Demon's Souls, the same error on
099de8d; the earlier reports #1591 and #1637 are closed).What is wrong
Since
c2173801("shader: skip resources in proven empty unsigned loops"), Demon's Souls (PPSA01341) stops a few seconds after starting withscalar resource reads overlap a shader buffer write(descriptors.cpp:955 on763d3be3). Five people reported it in #1591, the latest onfe3857a, and #1660 shows it on099de8d; builds5dec523andd58aefdrun the game without this error.The shader is compute shader
0x02632cb15eb8128dfrom kavg13's log. Every lane loads an item count from a buffer (BUFFER_LOAD_DWORD, offset 0), branches ongid < count, and later updates the same buffer withBUFFER_ATOMIC_ADD.c2173801letsULessThan32into the projected control predicates, so at draw time the walker now reads the count to try to prove the branch empty. That read becomes a specialization read, and because the same dispatch writes the buffer, CommitBindings rejects it. The check is right: the shader may change the value the host read. Forgid < countthe read can only prove the branch empty when the count is 0, and a value the shader itself writes is not a draw-time constant anyway.What changes
ExtractResourcePlanleaves a predicate unknown (both sides reachable) when it depends on a vector load (LoadBufferU32) of a buffer the shader writes (info.buffers[...].written, which includes atomics and is the flag CommitBindings uses). Only predicates that depend on such a load change; c2173801's empty-loop proof for counts read from scalar memory works as before. ScalarReadConstBufferloads are not affected: the Writable case ofTestConservativeBufferReachabilityexpects branches on values loaded withReadConstBufferto stay projected. The change also covers comparisons such ascount != 0, which were projected beforec2173801too: that case of the new test already fails on56d03f16.How I checked it without the game
The log has the shader's RDNA2 listing but not its binary, so I encoded its 58 instructions (78 dwords) from the listing; Kyty's decoder prints all 58 back exactly as the log shows them. Then I ran the recompiler's own planning path on the CPU (TranslateProgram, ExtractResourcePlan, MaterializeResources, CompileProgram) and applied CommitBindings' overlap rule:
56d03f16(parent ofc2173801): no specialization reads, no EXIT;c2173801and763d3be3: the count is read inside the written buffer, the same EXIT;The log does not contain the game's buffer descriptor, so the reconstruction assumes OOB_SELECT 0, which Kyty requires for indexed buffers and which all six buffer descriptor dumps I found in logs of other games have. A run of the game with this PR's build would confirm it. Saros (#1626) stops with the same error on the same builds; its log doesn't include the shader, so I can't tell whether it is the same case.
Tests
TestBranchOnWrittenBufferNotCaptured(resource_materialization_tests): a branchlane < countorcount != 0on a count loaded withLoadBufferU32, for count 0 and 3. If the shader writes that buffer, no specialization read may land in it (fails on763d3be3, passes with this change). If the buffer is read-only, the read must still be captured, so the projection stays on.kernel_file_system, which also fails on763d3be3.763d3be3.5ee50326(this PR's head).@kavg13 @Kassadd, if you have time, could you try the Windows build of this PR (the
KytyPS5-Windows-x64artifact of its CI run) with Demon's Souls?If you'd like this done differently, tell me and I'll change it.
Written with Claude Code (AI): it traced the crash to
c2173801from the log, wrote the change and the test, and ran the tests on my PC (Windows 11, RX 9070 XT and Ryzen 7600X iGPU, AMD driver 26.8.1).🤖 Generated with Claude Code
@coderabbitai[bot] on GitHub · 2026-10-18 04:16 UTC · original · edited 2026-10-30 01:00 UTC
Walkthrough
Resource-plan extraction now scans predicate instruction graphs for
LoadBufferU32reads from buffers marked written. Predicates with those reads do not use the direct integer runtime-value path and instead use existing predicate-projection handling. Tests cover unsigned-bound and nonzero predicates with written and read-only buffers, including descriptor retention and specialization-read capture.Suggested reviewers:
nmzikPriority:
Normal
Change: Bug fix · Severity of issue fixed: Medium
Merge Risk:
Minimal · up to
9e7bbThe change preserves unknown written-buffer predicates. A test improvement is recommended, but no merge-blocking risk is established.
Pre-merge checks:
4 ·
1
#1660requires Demon's Souls to pass the startup failure and reach gameplay without the emulator buffer-write overlap error.ExtractResourcePlannow leaves predicates that depend on `Lo…#1660. The source change limits resource planning for predicates that read shader-written buffers. The added test directly verifies the reported overlap case and the requ…Full details: Linked Issues check
Explanation
Active issue
#1660requires Demon's Souls to pass the startup failure and reach gameplay without the emulator buffer-write overlap error.ExtractResourcePlannow leaves predicates that depend onLoadBufferU32from a written buffer unknown.TestBranchOnWrittenBufferNotCapturedcoverslane < countandcount != 0, with zero and nonzero values, and preserves capture for read-only buffers. The reported follow-up runs reached character creation or gameplay with this change. Closed issues#1591and#1637provide context only.Full details: Out of Scope Changes check
Explanation
The changes stay within issue
#1660. The source change limits resource planning for predicates that read shader-written buffers. The added test directly verifies the reported overlap case and the required read-only behavior. No unrelated production behavior or unrelated test scope is shown.Full details: Docstring Coverage
Explanation
Docstring coverage is 33.33% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff with cyclomatic complexity of at least 2. Documentation-only edits and deletions within surviving functions are included; unchanged diff context and fully deleted functions are excluded. Analyzed 3 functions across 2 files.
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.@ -1492,0 +1501,4 @@const auto& memory = program.memory_info.at(inst->Flags<MemoryFlags>().index);if (memory.kind == ResourceKind::Buffer &&memory.resource < program.info.buffers.size() &&program.info.buffers[memory.resource].written)@coderabbitai[bot] on GitHub · 2026-10-18 04:16 UTC · original · edited 2026-10-18 04:36 UTC
🩺 Stability & Availability | 🟠 Major | ⚡ Quick win
🔎 Supported by static analysis
🏁 Script executed:
Repository: KytyPS5/KytyPS5
Length of output: 41530
🏁 Script executed:
Repository: KytyPS5/KytyPS5
Length of output: 28392
🏁 Script executed:
Repository: KytyPS5/KytyPS5
Length of output: 22888
🏁 Script executed:
Repository: KytyPS5/KytyPS5
Length of output: 6254
🏁 Script executed:
Repository: KytyPS5/KytyPS5
Length of output: 14875
Account for writable buffer aliases across resource indices.
reads_written_bufferchecks only the loaded resource’swrittenflag. Distinct buffer resources can receive separate descriptors that resolve to overlapping ranges. In that case, the direct predicate path can capture the buffer read, and the renderer can abort when it detects overlap with a written buffer range. Treat buffer-derived predicates as unknown whenever a writable buffer exists unless disjointness is established. Add a test with two resource indices whose descriptors overlap.🐛 Suggested fix
@coderabbitai[bot] on GitHub · 2026-10-18 04:16 UTC · original · reviewed
Actionable comments posted: 2
ℹ️ Review info
⚙️ Run configuration
3931fd86-4043-48bc-a3b6-e9b23eceb1ac📥 Commits
Reviewing files that changed from the base of the PR and between
37df7f3278and5ee503269a.📒 Files selected for processing: 2
src/graphics/shader/recompiler/ir/passes/ResourceMaterialization.cpptests/ResourceMaterializationTests.cppIncluded review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 7 remain after this review.
@ -1492,0 +1505,4 @@return true;}for (size_t index = 0; index < inst->NumArgs(); ++index)pending.push_back(inst->Arg(index));@coderabbitai[bot] on GitHub · 2026-10-18 04:16 UTC · original · edited 2026-10-18 04:36 UTC
🩺 Stability & Availability | 🟠 Major | 🏗️ Heavy lift
🔎 Supported by static analysis
🏁 Script executed:
Repository: KytyPS5/KytyPS5
Length of output: 33971
🏁 Script executed:
Repository: KytyPS5/KytyPS5
Length of output: 38100
🏁 Script executed:
Repository: KytyPS5/KytyPS5
Length of output: 18859
Follow flattened SRT aliases when scanning predicate dependencies.
reads_written_bufferfollows direct instruction arguments but does not resolveReadConstthroughprogram.srt_reads. If the aliased value depends on aLoadBufferU32from a written buffer, the scan misses the dependency.Clonethen expands the same alias, so the predicate can still take the integer-runtime-value path and capture the read.Resolve
ReadConstslots during this scan, with cycle protection consistent with the existing traversal. Add a regression test for this flattened-alias path.@ -1492,0 +1501,4 @@const auto& memory = program.memory_info.at(inst->Flags<MemoryFlags>().index);if (memory.kind == ResourceKind::Buffer &&memory.resource < program.info.buffers.size() &&program.info.buffers[memory.resource].written)@Nawid3333 on GitHub · 2026-10-18 04:36 UTC · original
Overlapping descriptors are a separate case, and they still fail loudly. If another buffer resource overlaps the one the count is read from, the read is still captured, and CommitBindings compares every captured read with the address range of every written buffer binding in all stages (descriptors.cpp, the check that prints this error), so it stops with the same error as on main today rather than using a stale value. Which descriptors overlap is only known at draw time, not when
ExtractResourcePlanbuilds the plan. Making every branch on a buffer load unknown as soon as the shader writes any buffer would also give up those branches in shaders whose buffers can't overlap. This PR covers the case the game hits: the shader reads and writes the same buffer resource.Written with Claude Code (AI): it checked the code paths; I read this reply before posting it.
@ -1492,0 +1505,4 @@return true;}for (size_t index = 0; index < inst->NumArgs(); ++index)pending.push_back(inst->Arg(index));@Nawid3333 on GitHub · 2026-10-18 04:36 UTC · original
Following the
ReadConstalias here wouldn't change the result. When the SRT is materialized, every slot of a reachable block is read (SrtWalker::RefreshFlatBuffer), whether or not a branch uses it, so if the slot's address depends on a load from a buffer the shader writes, that read is captured anyway and CommitBindings stops on it, as on main. I checked this with a version of the new test where the branch doesn't use the slot at all: the read inside the written buffer is still captured. Making the scan follow the alias (asClonedoes) gave the same result, so I left it out. That case is outside this PR.Written with Claude Code (AI): it wrote and ran the check; I read this reply before posting it.
@ -1492,0 +1505,4 @@return true;}for (size_t index = 0; index < inst->NumArgs(); ++index)pending.push_back(inst->Arg(index));@coderabbitai[bot] on GitHub · 2026-10-18 04:36 UTC · original
@Nawid3333 Thanks for the clarification. I withdraw this finding.
ExtractResourcePlanalready clones everyprogram.srt_readsvalue. As you explained,SrtWalker::RefreshFlatBufferevaluates every slot in a reachable block, even when the branch does not use that slot. Following theReadConstalias in this scan therefore would not prevent the captured read or the laterCommitBindingsrejection. My comment incorrectly treated that existing limitation as a defect introduced by this PR.Would you like a follow-up issue for the existing flattened-SRT limitation?
✏️ Learnings added
✅ Review thread resolved.
You are interacting with an AI system.
@ -1492,0 +1501,4 @@const auto& memory = program.memory_info.at(inst->Flags<MemoryFlags>().index);if (memory.kind == ResourceKind::Buffer &&memory.resource < program.info.buffers.size() &&program.info.buffers[memory.resource].written)@coderabbitai[bot] on GitHub · 2026-10-18 04:36 UTC · original
🧩 Analysis chain
🏁 Script executed:
Length of output: 5816
@Nawid3333Thanks for clarifying. I checkedCommitBindingsindescriptors.cpp. It compares captured specialization reads against written buffer ranges across all prepared stages and aborts on overlap.My finding treated that existing rejection as a regression introduced by this PR. That was incorrect. The broader guard I suggested would also disable predicate projection for disjoint buffers.
I withdraw the finding. Cross-resource aliases do not require a change in this PR, which targets reads and writes through the same buffer resource.
✏️ Learnings added
✅ Review thread resolved.
You are interacting with an AI system.
@Nawid3333 on GitHub · 2026-10-18 21:02 UTC · original
Tested with the game on AMD hardware: volan3 ran Demon's Souls (PPSA01341, 01.007.000) on an RX 6700 XT (Ryzen 7 5700X, Windows) with two builds from my fork's CI. Both are
099de8dplus #1152, which removes an unrelated AMD driver crash on RDNA2 (#821):099de8d+ #1152 (a59509e): stops withscalar resource reads overlap a shader buffer write(descriptors.cpp:955) right afterShaders: VS 2 | PS 5 | CS 111, the same point as the099de8dlog in #1660 (RTX 4070 SUPER). LogVS 51 | PS 98 | CS 234) before a separate crash in the game's own code (a null pointer in a CPU function, not in the renderer; log).Both builds pass the CI build and tests on Windows, Linux and macOS.
Written with Claude Code (AI): it prepared the two test builds and compared the logs; I read this before posting.
@Nawid3333 on GitHub · 2026-10-21 21:33 UTC · original
Retested in the game on today's main (
28648188, which includes68d0ba5a): Demon's Souls (PPSA01341), RX 9070 XT, Windows, the same input script in every run.scalar resource reads overlap a shader buffer write(descriptors.cpp:950), right after compute shader 0x02632cb15eb8128d, atShaders: VS 2 | PS 5 | CS 111(the point reported in #1660).VS 25 | PS 55 | CS 152at the end).VS 23 | PS 47 | CS 160).So on current main, either PR gets Demon's Souls past this stop.
@Nawid3333 on GitHub · 2026-10-23 18:38 UTC · original
Tested in the game on NVIDIA too: alxtorrentazos ran Demon's Souls on an RTX 3080 (Ryzen 5900X, Windows) in #1746.
9dcff95: stops atVS 2 | PS 5 | CS 111withscalar resource reads overlap a shader buffer write(#1746).9dcff95plus this PR (fork build3f05536): gets past that point and through the character creation, up toCS 233(#1746). The crash there, in the fog after the intro, looks like the one volan3 had in #821 at the same point, which went away with "Windows SysV red zone crash protection" checked.@coderabbitai[bot] on GitHub · 2026-10-30 00:59 UTC · original · reviewed
3a9cc4e3-3277-4ecc-a6ac-6d6e0fd59c70Reviewing files that changed from the base of the PR and between
5ee503269aand9e7bb9d71f.src/graphics/shader/recompiler/ir/passes/ResourceMaterialization.cpptests/ResourceMaterializationTests.cppIncluded review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 9 remain after this review.
@Nawid3333 on GitHub · 2026-10-30 19:38 UTC · original
Closing this: since
eb3cb472("renderer: drop the per-draw scalar-read overlap guard") main no longer stops at "scalar resource reads overlap a shader buffer write" inCommitBindings, which is the stop this PR got Demon's Souls and Code Violet past. Demon's Souls now reaches gameplay on4919fdb(#1660).Branches on buffers the shader writes can still be specialized on main. If a game shows wrong rendering from that, I can reopen this with the case.
Pull request closed