shader: accept scalar loads that precede a dispatch's own buffer writes #1253

Closed
github-bot wants to merge 1 commit from gh-pr/1253/head into gh-pr/1253/base
Owner

@⁠TheCruZ on GitHub · 2026-10-08 20:04 UTC · original
pr/scalar-read-before-own-write → main · +331 −8 · 8 files · 1 commits · closed without merging

Summary

Fixes the emulator exiting with scalar resource reads overlap a shader buffer write when a Fidelity mode (ray tracing) save of Grand Theft Auto V (PPSA04263) is loaded. The failing dispatch is the BVH build compute shader cs 9df701ab41f8ed18.

Problem

The shader S_LOADs the flags dword at offset 0x18 of a PSR_BVHL header, then sets a bit in that same dword with BUFFER_ATOMIC_OR_X2 through a V# over the header, and traps when bit 4 of the loaded value is set. Since branch reachability is refreshed from scalar reads, that load is a specialization read captured at dispatch time, and CommitBindings exits whenever a specialization read overlaps a buffer that any bound shader writes.

Here the overlap is harmless: each wave issues the load before its own writes to the buffer, so the dispatch-time snapshot is a value the load observes on hardware.

Change

  • Each captured specialization read is tagged with the flat SRT slot whose scalar load made it, or with the memory-info index of a scalar or uniform buffer load the shader issues itself (RawReadSlot), in ResourceSnapshot::specialization_read_slots (SrtRuntime::read_slot is set by the walker around each load).
  • At translation, SrtReadsAfterBufferWrites records for each slot and each such load the written buffers whose stores or atomics may precede it in program order (loops included; a load the analysis did not see counts as following every write); they are kept in CompiledShaderInfo.
  • CommitBindings accepts an overlap with the reading shader's own buffer only when SpecializationReadFollowsBufferWrite says no write to that buffer can precede the read. Loads that follow a write to the buffer, overlaps with other stages' buffers and overlaps with images still exit as before.

Upstream ec3e48cd (written compute descriptors read without an alias proof) does not cover this case: here the read is a specialization read of branch reachability, not a descriptor.

Testing

  • Grand Theft Auto V (PPSA04263), Fidelity mode: the save loads and the BVH build runs; played in my fork with this change.
  • resource_tracking_tests: new TestScalarReadBeforeOwnBufferWrite builds the BVH header pattern with the read before the write (accepted), the write before the read (rejected) and the write in a loop back to the read (rejected), and checks that an earlier store to another buffer is still reported and that a load the analysis did not see is treated as following the writes. Passes.
  • resource_materialization_tests, memory_tracker_tests, page_manager_tests, shader_cfg_tests, virtual_memory_allocation_tests and the default run of shader_recompiler_compute_tests pass.
  • Tested on Windows 11 with AMD Ryzen 5900x and RTX 3090.

AI usage

AI (Claude Opus 5.5) was used to investigate the issue and to implement the change. I reviewed the code, understand it, tested it as described above, and wrote this description myself, in general fully tested the game while doing few missions and playing in-game with my GTA V fork, i will be bringing PRs here after test them enough.

**@⁠TheCruZ** on GitHub · 2026-10-08 20:04 UTC · [original](https://github.com/KytyPS5/KytyPS5/pull/1253) `pr/scalar-read-before-own-write` → `main` · +331 −8 · 8 files · 1 commits · closed without merging ## Summary Fixes the emulator exiting with `scalar resource reads overlap a shader buffer write` when a Fidelity mode (ray tracing) save of Grand Theft Auto V (PPSA04263) is loaded. The failing dispatch is the BVH build compute shader `cs 9df701ab41f8ed18`. ## Problem The shader `S_LOAD`s the flags dword at offset `0x18` of a `PSR_BVHL` header, then sets a bit in that same dword with `BUFFER_ATOMIC_OR_X2` through a V# over the header, and traps when bit 4 of the loaded value is set. Since branch reachability is refreshed from scalar reads, that load is a specialization read captured at dispatch time, and `CommitBindings` exits whenever a specialization read overlaps a buffer that any bound shader writes. Here the overlap is harmless: each wave issues the load before its own writes to the buffer, so the dispatch-time snapshot is a value the load observes on hardware. ## Change - Each captured specialization read is tagged with the flat SRT slot whose scalar load made it, or with the memory-info index of a scalar or uniform buffer load the shader issues itself (`RawReadSlot`), in `ResourceSnapshot::specialization_read_slots` (`SrtRuntime::read_slot` is set by the walker around each load). - At translation, `SrtReadsAfterBufferWrites` records for each slot and each such load the written buffers whose stores or atomics may precede it in program order (loops included; a load the analysis did not see counts as following every write); they are kept in `CompiledShaderInfo`. - `CommitBindings` accepts an overlap with the reading shader's own buffer only when `SpecializationReadFollowsBufferWrite` says no write to that buffer can precede the read. Loads that follow a write to the buffer, overlaps with other stages' buffers and overlaps with images still exit as before. Upstream ec3e48cd (written compute descriptors read without an alias proof) does not cover this case: here the read is a specialization read of branch reachability, not a descriptor. ## Testing - Grand Theft Auto V (PPSA04263), Fidelity mode: the save loads and the BVH build runs; played in my fork with this change. - `resource_tracking_tests`: new `TestScalarReadBeforeOwnBufferWrite` builds the BVH header pattern with the read before the write (accepted), the write before the read (rejected) and the write in a loop back to the read (rejected), and checks that an earlier store to another buffer is still reported and that a load the analysis did not see is treated as following the writes. Passes. - `resource_materialization_tests`, `memory_tracker_tests`, `page_manager_tests`, `shader_cfg_tests`, `virtual_memory_allocation_tests` and the default run of `shader_recompiler_compute_tests` pass. - Tested on Windows 11 with AMD Ryzen 5900x and RTX 3090. ## AI usage AI (Claude Opus 5.5) was used to investigate the issue and to implement the change. I reviewed the code, understand it, tested it as described above, and wrote this description myself, in general fully tested the game while doing few missions and playing in-game with my [GTA V fork](https://github.com/TheCruZ/KytyPS5-GTA), i will be bringing PRs here after test them enough.
GTA V's Fidelity BVH build CS 9df701ab41f8ed18 S_LOADs the flags dword at
offset 0x18 of a "PSR_BVHL" header, then sets a bit in it with
BUFFER_ATOMIC_OR_X2 through a V# over the header, and traps when bit 4 of
the loaded value is set. Since branch reachability is refreshed from scalar
reads, that load is a specialization read, and loading a Fidelity save exits
with "scalar resource reads overlap a shader buffer write".

Each wave issues that load before its own writes to the buffer, so the
dispatch-time snapshot is a value the load observes on hardware. Tag each
captured read with the flat SRT slot whose load made it, or with the
memory index of a scalar or uniform buffer load the shader issues itself
(native scalar data is no longer flattened), record for each slot and
each such load the written buffers whose stores or atomics may precede it
in program order (loops included; a load the analysis did not see follows
them all), and accept an overlap with the reading shader's own buffer
only when that buffer is not among them. Loads that follow a write to the
buffer and overlaps with other stages or with images still exit.

Tests: TestScalarReadBeforeOwnBufferWrite (read first, write first, loop,
unanalyzed load).
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 09:50:10 +00:00
github-bot closed this pull request 2026-11-26 09:50:10 +00:00
Author
Owner

@⁠coderabbitai[bot] on GitHub · 2026-10-08 20:09 UTC · original · edited 2026-10-08 21:12 UTC

Review in Change Stack →

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: e894c118-8574-47c8-ab26-686330f5d402
📥 Commits

Reviewing files that changed from the base of the PR and between 19a1258a4213a382aaacc3639e03f0cbea8a2095 and 0e3c6c87f6.

📒 Files selected for processing (2)
  • src/graphics/shader/recompiler/ir/Program.cpp
  • tests/ResourceTrackingTests.cpp

Included review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 0 remain after this review.


📝 Walkthrough

Walkthrough

The change records the slot for each specialization read, analyzes possible buffer writes before SRT and raw reads, and stores the results in compiled shader metadata. Renderer binding checks use this ordering information for same-stage buffer overlaps. Added tests cover read-first, write-first, and loop execution orders.

Suggested reviewers: nmzik

Priority: ➖ Normal

Change: Bug fix

Merge Risk: ⚪ Minimal · up to 0e3c6

The previously reported raw-read overlap risk is fixed at the reviewed head. No actionable merge-blocking issue remains from the supplied evidence.

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 10.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 20 functions across 8 files. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
Title check ✅ Passed The title clearly summarizes the main change: allowing scalar loads before the same dispatch writes to the buffer.
Description check ✅ Passed The description directly explains the failure, implementation, preserved safety checks, and test coverage for the changeset.
  • Autopilot · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

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 help to get the list of available commands.

**@⁠coderabbitai[bot]** on GitHub · 2026-10-08 20:09 UTC · [original](https://github.com/KytyPS5/KytyPS5/pull/1253#issuecomment-6068144450) · edited 2026-10-08 21:12 UTC <!-- This is an auto-generated comment: summarize by coderabbit.ai --> <!-- review_stack_entry_start --> <a href="https://app.coderabbit.ai/change-stack/KytyPS5/KytyPS5/pull/1253?scope=ghh_OusAJGA4HG8ljoOV0wJvVx8NmnZIjc2DdjmxOu0xZWA&amp;cs_source=review_comment"><img src="https://storage.googleapis.com/coderabbit_public_assets/review-stack-in-coderabbit-ui-dark.svg?v=2" alt="Review in Change Stack →" width="220" height="32"></a> <!-- review_stack_entry_end --> <!-- recent_review_start --> No actionable comments were generated in the recent review. 🎉 <details> <summary>ℹ️ Recent review info</summary> <details> <summary>⚙️ Run configuration</summary> - **Configuration used**: Repository: KytyPS5/KytyPS5/.coderabbit.yaml - **Review profile**: CHILL - **Plan**: Advanced - **Run ID**: `e894c118-8574-47c8-ab26-686330f5d402` </details> <details> <summary>📥 Commits</summary> Reviewing files that changed from the base of the PR and between 19a1258a4213a382aaacc3639e03f0cbea8a2095 and 0e3c6c87f62a50a67d7cafcbf56bba7008905be3. </details> <details> <summary>📒 Files selected for processing (2)</summary> * `src/graphics/shader/recompiler/ir/Program.cpp` * `tests/ResourceTrackingTests.cpp` </details> **Included review availability:** This review used your included allowance. Your plan provides up to 8 included reviews per hour; 0 remain after this review. </details> --- <!-- recent_review_end --> <!-- walkthrough_start --> <details> <summary>📝 Walkthrough</summary> ## Walkthrough The change records the slot for each specialization read, analyzes possible buffer writes before SRT and raw reads, and stores the results in compiled shader metadata. Renderer binding checks use this ordering information for same-stage buffer overlaps. Added tests cover read-first, write-first, and loop execution orders. **Suggested reviewers:** `nmzik` </details> <!-- walkthrough_end --> <!-- change_assessment_start --> **Priority:** ➖ Normal <!-- change_assessment_commit:"0e3c6c87f62a50a67d7cafcbf56bba7008905be3" --> **Change:** Bug fix <!-- change_assessment_end --> <!-- final_review_risk_start --> **Merge Risk:** **⚪ Minimal** · up to `0e3c6` <!-- final_review_risk_coverage:{"sourceCommitId":"0e3c6c87f62a50a67d7cafcbf56bba7008905be3","coveredCommitId":"0e3c6c87f62a50a67d7cafcbf56bba7008905be3","kind":"reviewed"} --> The previously reported raw-read overlap risk is fixed at the reviewed head. No actionable merge-blocking issue remains from the supplied evidence. <!-- final_review_risk_end --> <!-- pre_merge_checks_walkthrough_start --> <details> <summary>🚥 Pre-merge checks | ✅ 4 | ❌ 1</summary> ### ❌ Failed checks (1 warning) | Check name | Status | Explanation | Resolution | | :---------------- | :--------- | :------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | :--------------------------------------------------------------------------------- | | Docstring Coverage | ⚠️ Warning | Docstring coverage is 10.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 20 functions across 8 files. | Write docstrings for the functions missing them to satisfy the coverage threshold. | <details> <summary>✅ Passed checks (4 passed)</summary> | Check name | Status | Explanation | | :------------------------ | :------- | :--------------------------------------------------------------------------------------------------------------------------- | | Linked Issues check | ✅ Passed | Check skipped because no linked issues were found for this pull request. | | Out of Scope Changes check | ✅ Passed | Check skipped because no linked issues were found for this pull request. | | Title check | ✅ Passed | The title clearly summarizes the main change: allowing scalar loads before the same dispatch writes to the buffer. | | Description check | ✅ Passed | The description directly explains the failure, implementation, preserved safety checks, and test coverage for the changeset. | </details> </details> <!-- pre_merge_checks_walkthrough_end --> <!-- autopilot:start --> - [ ] <!-- {"checkboxId":"2708ad07-9f24-4260-9c11-7dc76a49f2e3"} --> <strong title="Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts">Autopilot</strong> · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts <!-- autopilot:end --> <!-- tips_start --> --- Thanks for using [CodeRabbit](https://coderabbit.ai?utm_source=oss&utm_medium=github&utm_campaign=KytyPS5/KytyPS5&utm_content=1253)! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. <details> <summary>❤️ Share</summary> - [X](https://twitter.com/intent/tweet?text=I%20just%20used%20%40coderabbitai%20for%20my%20code%20review%2C%20and%20it%27s%20fantastic%21%20It%27s%20free%20for%20OSS%20and%20offers%20a%20free%20trial%20for%20the%20proprietary%20code.%20Check%20it%20out%3A&url=https%3A//coderabbit.ai) - [Mastodon](https://mastodon.social/share?text=I%20just%20used%20%40coderabbitai%20for%20my%20code%20review%2C%20and%20it%27s%20fantastic%21%20It%27s%20free%20for%20OSS%20and%20offers%20a%20free%20trial%20for%20the%20proprietary%20code.%20Check%20it%20out%3A%20https%3A%2F%2Fcoderabbit.ai) - [Reddit](https://www.reddit.com/submit?title=Great%20tool%20for%20code%20review%20-%20CodeRabbit&text=I%20just%20used%20CodeRabbit%20for%20my%20code%20review%2C%20and%20it%27s%20fantastic%21%20It%27s%20free%20for%20OSS%20and%20offers%20a%20free%20trial%20for%20proprietary%20code.%20Check%20it%20out%3A%20https%3A//coderabbit.ai) - [LinkedIn](https://www.linkedin.com/sharing/share-offsite/?url=https%3A%2F%2Fcoderabbit.ai&mini=true&title=Great%20tool%20for%20code%20review%20-%20CodeRabbit&summary=I%20just%20used%20CodeRabbit%20for%20my%20code%20review%2C%20and%20it%27s%20fantastic%21%20It%27s%20free%20for%20OSS%20and%20offers%20a%20free%20trial%20for%20proprietary%20code) </details> <sub>Comment `@coderabbitai help` to get the list of available commands.</sub> <!-- tips_end -->
Author
Owner

@⁠coderabbitai[bot] on GitHub · 2026-10-08 20:09 UTC · original · edited 2026-10-08 21:14 UTC
On src/graphics/shader/recompiler/ir/Program.cpp line 315 (outdated):

@@ -218,6 +223,143 @@ bool HasShaderMemoryWrites(const Program& program) {
 	return false;
 }
 
+std::vector<std::vector<uint32_t>> SrtReadsAfterBufferWrites(
+    const Program& program, std::vector<std::vector<uint32_t>>* raw) {
+	const auto buffers = program.info.buffers.size();
+	uint32_t   slots   = static_cast<uint32_t>(program.srt_reads.size());
+	const auto slot_of = [](const Inst& inst) {
+		const auto slot = inst.Arg(1).Resolve();
+		return slot.IsImmediate() && slot.GetType() == Type::U32 ? slot.U32() : NoSrtSlot;
+	};
+	for (const auto* block: program.blocks) {
+		for (const auto& inst: *block) {
+			if (inst.GetOpcode() == ValueOpcode::ReadConst && slot_of(inst) != NoSrtSlot) {
+				slots = std::max(slots, slot_of(inst) + 1u);
+			}
+		}
+	}
+	std::vector<std::vector<uint32_t>> result(slots);
+	if (raw != nullptr) {
+		raw->assign(program.memory_info.size(), {});
+	}
+	if (buffers == 0 || (slots == 0 && raw == nullptr)) {
+		return result;
+	}
+	// Marks the buffers a store or atomic updates; one with an unknown target updates them all.
+	const auto apply_write = [&](const Inst& inst, std::vector<uint8_t>& written) {
+		const auto access = BufferAccessOf(inst.GetOpcode());
+		if (access != BufferAccess::Write && access != BufferAccess::Atomic) {
+			return;
+		}
+		const auto index    = inst.Flags<MemoryFlags>().index;
+		const auto kind     = index < program.memory_info.size() ? program.memory_info[index].kind
+		                                                         : ResourceKind::None;
+		const auto resource = kind != ResourceKind::None ? program.memory_info[index].resource : 0u;
+		if (kind == ResourceKind::Lds || kind == ResourceKind::Gds) {
+			return;
+		}
+		if (kind == ResourceKind::Buffer && resource < buffers) {
+			written[resource] = 1u;
+		} else {
+			std::fill(written.begin(), written.end(), uint8_t {1});
+		}
+	};
+	std::unordered_map<const Block*, size_t> indices;
+	for (size_t index = 0; index < program.blocks.size(); index++) {
+		indices.emplace(program.blocks[index], index);
+	}
+	// Buffers that may have been written on some path into each block, loops included.
+	std::vector<std::vector<uint8_t>> written_in(program.blocks.size(),
+	                                             std::vector<uint8_t>(buffers));
+	std::vector<size_t> pending(program.blocks.size());
+	for (size_t index = 0; index < pending.size(); index++) {
+		pending[index] = pending.size() - 1u - index;
+	}
+	std::vector<uint8_t> state;
+	while (!pending.empty()) {
+		const auto index = pending.back();
+		pending.pop_back();
+		state = written_in[index];
+		for (const auto& inst: *program.blocks[index]) {
+			apply_write(inst, state);
+		}
+		for (const auto* successor: program.blocks[index]->ImmSuccessors()) {
+			const auto found = indices.find(successor);
+			if (found == indices.end()) {
+				continue;
+			}
+			auto& target  = written_in[found->second];
+			bool  changed = false;
+			for (size_t buffer = 0; buffer < buffers; buffer++) {
+				if (state[buffer] != 0u && target[buffer] == 0u) {
+					target[buffer] = 1u;
+					changed        = true;
+				}
+			}
+			if (changed) {
+				pending.push_back(found->second);
+			}
+		}
+	}
+	std::vector<std::vector<uint8_t>> after(slots, std::vector<uint8_t>(buffers));
+	std::vector<std::vector<uint8_t>> raw_after(raw != nullptr ? program.memory_info.size() : 0u);
+	for (size_t index = 0; index < program.blocks.size(); index++) {
+		state = written_in[index];
+		for (const auto& inst: *program.blocks[index]) {
+			if (inst.GetOpcode() == ValueOpcode::ReadConst) {
+				const auto slot = slot_of(inst);
+				for (size_t buffer = 0; slot != NoSrtSlot && buffer < buffers; buffer++) {
+					after[slot][buffer] |= state[buffer];
+				}
+			} else if (raw != nullptr && (inst.GetOpcode() == ValueOpcode::LoadAddressU32 ||
+			                              inst.GetOpcode() == ValueOpcode::ReadConstBuffer)) {

🗄️ Data Integrity & Integration | 🟠 Major | ⚡ Quick win

Track LoadBufferU32 raw reads. Treat a raw-read slot with no analysis entry as following a write.

SrtWalker::EvaluateRawRead (SrtWalker.cpp Lines 530-532) assigns RawReadSlot(index) to every unflattened raw read. That includes LoadBufferU32 reads, and the capture records those reads through CaptureStrictRead.

This loop fills raw_after only for LoadAddressU32 and ReadConstBuffer. For a LoadBufferU32 memory index, raw_reads_after_buffer_writes[index] keeps the empty vector from raw->assign(...).

SpecializationReadFollowsBufferWrite passes the size check for that index. It then searches an empty list and returns false, which means "no write precedes this read".

Consequence: in CommitBindings (descriptors.cpp Lines 1041-1043), the check now skips the exit when a same-stage LoadBufferU32 specialization read overlaps a buffer the shader writes earlier. Before this change, every such overlap exited. Now the snapshot can be stale with no diagnostic.

The same failure applies to any raw-read index that the final IR no longer contains. The query returns "safe" when it has no data, so the default is not conservative.

Fix:

  • Include ValueOpcode::LoadBufferU32 in the raw-read branch.
  • Distinguish "analyzed" from "no data". For example, assign all raw entries as std::optional, or keep a separate analyzed flag. Return true from the query when an entry was not analyzed.
Proposed fix for the raw-read branch
--- "a/src/graphics/shader/recompiler/ir/Program.cpp"
+++ "b/src/graphics/shader/recompiler/ir/Program.cpp"
@@ -311,8 +311,9 @@
 				for (size_t buffer = 0; slot != NoSrtSlot && buffer < buffers; buffer++) {
 					after[slot][buffer] |= state[buffer];
 				}
-			} else if (raw != nullptr && (inst.GetOpcode() == ValueOpcode::LoadAddressU32 ||
-			                              inst.GetOpcode() == ValueOpcode::ReadConstBuffer)) {
+			} else if (raw != nullptr && (inst.GetOpcode() == ValueOpcode::LoadAddressU32 ||
+			                              inst.GetOpcode() == ValueOpcode::ReadConstBuffer ||
+			                              inst.GetOpcode() == ValueOpcode::LoadBufferU32)) {
 				const auto memory = inst.Flags<MemoryFlags>().index;
 				if (memory < raw_after.size()) {
 					auto& seen = raw_after[memory];

Also mark entries that were never analyzed as conservative. For example, fill all buffer indices into raw entries whose raw_after[memory] stayed empty because no instruction with that index was visited.

📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

			} else if (raw != nullptr && (inst.GetOpcode() == ValueOpcode::LoadAddressU32 ||
			                              inst.GetOpcode() == ValueOpcode::ReadConstBuffer ||
			                              inst.GetOpcode() == ValueOpcode::LoadBufferU32)) {

✅ Confirmed as addressed by @⁠TheCruZ

**@⁠coderabbitai[bot]** on GitHub · 2026-10-08 20:09 UTC · [original](https://github.com/KytyPS5/KytyPS5/pull/1253#discussion_r4223639546) · edited 2026-10-08 21:14 UTC On `src/graphics/shader/recompiler/ir/Program.cpp` line 315 (outdated): ```diff @@ -218,6 +223,143 @@ bool HasShaderMemoryWrites(const Program& program) { return false; } +std::vector<std::vector<uint32_t>> SrtReadsAfterBufferWrites( + const Program& program, std::vector<std::vector<uint32_t>>* raw) { + const auto buffers = program.info.buffers.size(); + uint32_t slots = static_cast<uint32_t>(program.srt_reads.size()); + const auto slot_of = [](const Inst& inst) { + const auto slot = inst.Arg(1).Resolve(); + return slot.IsImmediate() && slot.GetType() == Type::U32 ? slot.U32() : NoSrtSlot; + }; + for (const auto* block: program.blocks) { + for (const auto& inst: *block) { + if (inst.GetOpcode() == ValueOpcode::ReadConst && slot_of(inst) != NoSrtSlot) { + slots = std::max(slots, slot_of(inst) + 1u); + } + } + } + std::vector<std::vector<uint32_t>> result(slots); + if (raw != nullptr) { + raw->assign(program.memory_info.size(), {}); + } + if (buffers == 0 || (slots == 0 && raw == nullptr)) { + return result; + } + // Marks the buffers a store or atomic updates; one with an unknown target updates them all. + const auto apply_write = [&](const Inst& inst, std::vector<uint8_t>& written) { + const auto access = BufferAccessOf(inst.GetOpcode()); + if (access != BufferAccess::Write && access != BufferAccess::Atomic) { + return; + } + const auto index = inst.Flags<MemoryFlags>().index; + const auto kind = index < program.memory_info.size() ? program.memory_info[index].kind + : ResourceKind::None; + const auto resource = kind != ResourceKind::None ? program.memory_info[index].resource : 0u; + if (kind == ResourceKind::Lds || kind == ResourceKind::Gds) { + return; + } + if (kind == ResourceKind::Buffer && resource < buffers) { + written[resource] = 1u; + } else { + std::fill(written.begin(), written.end(), uint8_t {1}); + } + }; + std::unordered_map<const Block*, size_t> indices; + for (size_t index = 0; index < program.blocks.size(); index++) { + indices.emplace(program.blocks[index], index); + } + // Buffers that may have been written on some path into each block, loops included. + std::vector<std::vector<uint8_t>> written_in(program.blocks.size(), + std::vector<uint8_t>(buffers)); + std::vector<size_t> pending(program.blocks.size()); + for (size_t index = 0; index < pending.size(); index++) { + pending[index] = pending.size() - 1u - index; + } + std::vector<uint8_t> state; + while (!pending.empty()) { + const auto index = pending.back(); + pending.pop_back(); + state = written_in[index]; + for (const auto& inst: *program.blocks[index]) { + apply_write(inst, state); + } + for (const auto* successor: program.blocks[index]->ImmSuccessors()) { + const auto found = indices.find(successor); + if (found == indices.end()) { + continue; + } + auto& target = written_in[found->second]; + bool changed = false; + for (size_t buffer = 0; buffer < buffers; buffer++) { + if (state[buffer] != 0u && target[buffer] == 0u) { + target[buffer] = 1u; + changed = true; + } + } + if (changed) { + pending.push_back(found->second); + } + } + } + std::vector<std::vector<uint8_t>> after(slots, std::vector<uint8_t>(buffers)); + std::vector<std::vector<uint8_t>> raw_after(raw != nullptr ? program.memory_info.size() : 0u); + for (size_t index = 0; index < program.blocks.size(); index++) { + state = written_in[index]; + for (const auto& inst: *program.blocks[index]) { + if (inst.GetOpcode() == ValueOpcode::ReadConst) { + const auto slot = slot_of(inst); + for (size_t buffer = 0; slot != NoSrtSlot && buffer < buffers; buffer++) { + after[slot][buffer] |= state[buffer]; + } + } else if (raw != nullptr && (inst.GetOpcode() == ValueOpcode::LoadAddressU32 || + inst.GetOpcode() == ValueOpcode::ReadConstBuffer)) { ``` **🗄️ Data Integrity & Integration** | **🟠 Major** | **⚡ Quick win** **Track `LoadBufferU32` raw reads. Treat a raw-read slot with no analysis entry as following a write.** `SrtWalker::EvaluateRawRead` (SrtWalker.cpp Lines 530-532) assigns `RawReadSlot(index)` to every unflattened raw read. That includes `LoadBufferU32` reads, and the capture records those reads through `CaptureStrictRead`. This loop fills `raw_after` only for `LoadAddressU32` and `ReadConstBuffer`. For a `LoadBufferU32` memory index, `raw_reads_after_buffer_writes[index]` keeps the empty vector from `raw->assign(...)`. `SpecializationReadFollowsBufferWrite` passes the size check for that index. It then searches an empty list and returns `false`, which means "no write precedes this read". Consequence: in `CommitBindings` (descriptors.cpp Lines 1041-1043), the check now skips the exit when a same-stage `LoadBufferU32` specialization read overlaps a buffer the shader writes earlier. Before this change, every such overlap exited. Now the snapshot can be stale with no diagnostic. The same failure applies to any raw-read index that the final IR no longer contains. The query returns "safe" when it has no data, so the default is not conservative. Fix: - Include `ValueOpcode::LoadBufferU32` in the raw-read branch. - Distinguish "analyzed" from "no data". For example, assign all raw entries as `std::optional`, or keep a separate analyzed flag. Return `true` from the query when an entry was not analyzed. <details> <summary>Proposed fix for the raw-read branch</summary> ```diff --- "a/src/graphics/shader/recompiler/ir/Program.cpp" +++ "b/src/graphics/shader/recompiler/ir/Program.cpp" @@ -311,8 +311,9 @@ for (size_t buffer = 0; slot != NoSrtSlot && buffer < buffers; buffer++) { after[slot][buffer] |= state[buffer]; } - } else if (raw != nullptr && (inst.GetOpcode() == ValueOpcode::LoadAddressU32 || - inst.GetOpcode() == ValueOpcode::ReadConstBuffer)) { + } else if (raw != nullptr && (inst.GetOpcode() == ValueOpcode::LoadAddressU32 || + inst.GetOpcode() == ValueOpcode::ReadConstBuffer || + inst.GetOpcode() == ValueOpcode::LoadBufferU32)) { const auto memory = inst.Flags<MemoryFlags>().index; if (memory < raw_after.size()) { auto& seen = raw_after[memory]; ``` </details> Also mark entries that were never analyzed as conservative. For example, fill all buffer indices into raw entries whose `raw_after[memory]` stayed empty because no instruction with that index was visited. <!-- suggestion_start --> <details> <summary>📝 Committable suggestion</summary> > ‼️ **IMPORTANT** > Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements. ```suggestion } else if (raw != nullptr && (inst.GetOpcode() == ValueOpcode::LoadAddressU32 || inst.GetOpcode() == ValueOpcode::ReadConstBuffer || inst.GetOpcode() == ValueOpcode::LoadBufferU32)) { ``` </details> <!-- suggestion_end --> <!-- fingerprinting:phantom:medusa:pangolin --> <!-- cr-indicator-types:potential_issue --> <!-- cr-comment:v1:32139d8ba5ee1e3c9904e354 --> <!-- This is an auto-generated reply by CodeRabbit --> ✅ Confirmed as addressed by @⁠TheCruZ <!-- This is an auto-generated comment by CodeRabbit -->
Author
Owner

@⁠coderabbitai[bot] on GitHub · 2026-10-08 20:09 UTC · original · reviewed

Actionable comments posted: 1


ℹ️ Review info
⚙️ Run configuration
  • Configuration used: Repository: KytyPS5/KytyPS5/.coderabbit.yaml
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: c34ea8aa-3b24-479e-8f0c-76c458271d04
📥 Commits

Reviewing files that changed from the base of the PR and between 2f4b8a335d and 19a1258a4213a382aaacc3639e03f0cbea8a2095.

📒 Files selected for processing (8)
  • src/graphics/host_gpu/renderer/pipeline/descriptors.cpp
  • src/graphics/shader/recompiler/ir/Program.cpp
  • src/graphics/shader/recompiler/ir/ResourceSnapshot.h
  • src/graphics/shader/recompiler/ir/ShaderIR.h
  • src/graphics/shader/recompiler/ir/passes/ResourceMaterialization.cpp
  • src/graphics/shader/recompiler/ir/passes/SrtWalker.cpp
  • src/graphics/shader/recompiler/ir/passes/SrtWalker.h
  • tests/ResourceTrackingTests.cpp

Included review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 4 remain after this review.

**@⁠coderabbitai[bot]** on GitHub · 2026-10-08 20:09 UTC · [original](https://github.com/KytyPS5/KytyPS5/pull/1253#pullrequestreview-5462271632) · **reviewed** **Actionable comments posted: 1** --- <details> <summary>ℹ️ Review info</summary> <details> <summary>⚙️ Run configuration</summary> - **Configuration used**: Repository: KytyPS5/KytyPS5/.coderabbit.yaml - **Review profile**: CHILL - **Plan**: Advanced - **Run ID**: `c34ea8aa-3b24-479e-8f0c-76c458271d04` </details> <details> <summary>📥 Commits</summary> Reviewing files that changed from the base of the PR and between 2f4b8a335d5e27a5153bdea670bd877a061e37b1 and 19a1258a4213a382aaacc3639e03f0cbea8a2095. </details> <details> <summary>📒 Files selected for processing (8)</summary> * `src/graphics/host_gpu/renderer/pipeline/descriptors.cpp` * `src/graphics/shader/recompiler/ir/Program.cpp` * `src/graphics/shader/recompiler/ir/ResourceSnapshot.h` * `src/graphics/shader/recompiler/ir/ShaderIR.h` * `src/graphics/shader/recompiler/ir/passes/ResourceMaterialization.cpp` * `src/graphics/shader/recompiler/ir/passes/SrtWalker.cpp` * `src/graphics/shader/recompiler/ir/passes/SrtWalker.h` * `tests/ResourceTrackingTests.cpp` </details> **Included review availability:** This review used your included allowance. Your plan provides up to 8 included reviews per hour; 4 remain after this review. </details> <!-- This is an auto-generated comment by CodeRabbit for review status --> <!-- coderabbit-review-publication v1 publication=d9badb41-3e9c-410e-bd0b-7d8455a1ccb4 attempt=5fa66989-88bf-4515-98e9-a97b86e3a7be batch=1/1 -->
Author
Owner

@⁠TheCruZ on GitHub · 2026-10-08 21:08 UTC · original
On src/graphics/shader/recompiler/ir/Program.cpp line 315 (outdated):

@@ -218,6 +223,143 @@ bool HasShaderMemoryWrites(const Program& program) {
 	return false;
 }
 
+std::vector<std::vector<uint32_t>> SrtReadsAfterBufferWrites(
+    const Program& program, std::vector<std::vector<uint32_t>>* raw) {
+	const auto buffers = program.info.buffers.size();
+	uint32_t   slots   = static_cast<uint32_t>(program.srt_reads.size());
+	const auto slot_of = [](const Inst& inst) {
+		const auto slot = inst.Arg(1).Resolve();
+		return slot.IsImmediate() && slot.GetType() == Type::U32 ? slot.U32() : NoSrtSlot;
+	};
+	for (const auto* block: program.blocks) {
+		for (const auto& inst: *block) {
+			if (inst.GetOpcode() == ValueOpcode::ReadConst && slot_of(inst) != NoSrtSlot) {
+				slots = std::max(slots, slot_of(inst) + 1u);
+			}
+		}
+	}
+	std::vector<std::vector<uint32_t>> result(slots);
+	if (raw != nullptr) {
+		raw->assign(program.memory_info.size(), {});
+	}
+	if (buffers == 0 || (slots == 0 && raw == nullptr)) {
+		return result;
+	}
+	// Marks the buffers a store or atomic updates; one with an unknown target updates them all.
+	const auto apply_write = [&](const Inst& inst, std::vector<uint8_t>& written) {
+		const auto access = BufferAccessOf(inst.GetOpcode());
+		if (access != BufferAccess::Write && access != BufferAccess::Atomic) {
+			return;
+		}
+		const auto index    = inst.Flags<MemoryFlags>().index;
+		const auto kind     = index < program.memory_info.size() ? program.memory_info[index].kind
+		                                                         : ResourceKind::None;
+		const auto resource = kind != ResourceKind::None ? program.memory_info[index].resource : 0u;
+		if (kind == ResourceKind::Lds || kind == ResourceKind::Gds) {
+			return;
+		}
+		if (kind == ResourceKind::Buffer && resource < buffers) {
+			written[resource] = 1u;
+		} else {
+			std::fill(written.begin(), written.end(), uint8_t {1});
+		}
+	};
+	std::unordered_map<const Block*, size_t> indices;
+	for (size_t index = 0; index < program.blocks.size(); index++) {
+		indices.emplace(program.blocks[index], index);
+	}
+	// Buffers that may have been written on some path into each block, loops included.
+	std::vector<std::vector<uint8_t>> written_in(program.blocks.size(),
+	                                             std::vector<uint8_t>(buffers));
+	std::vector<size_t> pending(program.blocks.size());
+	for (size_t index = 0; index < pending.size(); index++) {
+		pending[index] = pending.size() - 1u - index;
+	}
+	std::vector<uint8_t> state;
+	while (!pending.empty()) {
+		const auto index = pending.back();
+		pending.pop_back();
+		state = written_in[index];
+		for (const auto& inst: *program.blocks[index]) {
+			apply_write(inst, state);
+		}
+		for (const auto* successor: program.blocks[index]->ImmSuccessors()) {
+			const auto found = indices.find(successor);
+			if (found == indices.end()) {
+				continue;
+			}
+			auto& target  = written_in[found->second];
+			bool  changed = false;
+			for (size_t buffer = 0; buffer < buffers; buffer++) {
+				if (state[buffer] != 0u && target[buffer] == 0u) {
+					target[buffer] = 1u;
+					changed        = true;
+				}
+			}
+			if (changed) {
+				pending.push_back(found->second);
+			}
+		}
+	}
+	std::vector<std::vector<uint8_t>> after(slots, std::vector<uint8_t>(buffers));
+	std::vector<std::vector<uint8_t>> raw_after(raw != nullptr ? program.memory_info.size() : 0u);
+	for (size_t index = 0; index < program.blocks.size(); index++) {
+		state = written_in[index];
+		for (const auto& inst: *program.blocks[index]) {
+			if (inst.GetOpcode() == ValueOpcode::ReadConst) {
+				const auto slot = slot_of(inst);
+				for (size_t buffer = 0; slot != NoSrtSlot && buffer < buffers; buffer++) {
+					after[slot][buffer] |= state[buffer];
+				}
+			} else if (raw != nullptr && (inst.GetOpcode() == ValueOpcode::LoadAddressU32 ||
+			                              inst.GetOpcode() == ValueOpcode::ReadConstBuffer)) {

Fixed in 0e3c6c8: LoadBufferU32 is recorded like the other raw reads, and a raw-read index the analysis never saw now lists every written buffer, so SpecializationReadFollowsBufferWrite is conservative when it has no data. TestScalarReadBeforeOwnBufferWrite checks the unanalyzed case.

**@⁠TheCruZ** on GitHub · 2026-10-08 21:08 UTC · [original](https://github.com/KytyPS5/KytyPS5/pull/1253#discussion_r4224148149) On `src/graphics/shader/recompiler/ir/Program.cpp` line 315 (outdated): ```diff @@ -218,6 +223,143 @@ bool HasShaderMemoryWrites(const Program& program) { return false; } +std::vector<std::vector<uint32_t>> SrtReadsAfterBufferWrites( + const Program& program, std::vector<std::vector<uint32_t>>* raw) { + const auto buffers = program.info.buffers.size(); + uint32_t slots = static_cast<uint32_t>(program.srt_reads.size()); + const auto slot_of = [](const Inst& inst) { + const auto slot = inst.Arg(1).Resolve(); + return slot.IsImmediate() && slot.GetType() == Type::U32 ? slot.U32() : NoSrtSlot; + }; + for (const auto* block: program.blocks) { + for (const auto& inst: *block) { + if (inst.GetOpcode() == ValueOpcode::ReadConst && slot_of(inst) != NoSrtSlot) { + slots = std::max(slots, slot_of(inst) + 1u); + } + } + } + std::vector<std::vector<uint32_t>> result(slots); + if (raw != nullptr) { + raw->assign(program.memory_info.size(), {}); + } + if (buffers == 0 || (slots == 0 && raw == nullptr)) { + return result; + } + // Marks the buffers a store or atomic updates; one with an unknown target updates them all. + const auto apply_write = [&](const Inst& inst, std::vector<uint8_t>& written) { + const auto access = BufferAccessOf(inst.GetOpcode()); + if (access != BufferAccess::Write && access != BufferAccess::Atomic) { + return; + } + const auto index = inst.Flags<MemoryFlags>().index; + const auto kind = index < program.memory_info.size() ? program.memory_info[index].kind + : ResourceKind::None; + const auto resource = kind != ResourceKind::None ? program.memory_info[index].resource : 0u; + if (kind == ResourceKind::Lds || kind == ResourceKind::Gds) { + return; + } + if (kind == ResourceKind::Buffer && resource < buffers) { + written[resource] = 1u; + } else { + std::fill(written.begin(), written.end(), uint8_t {1}); + } + }; + std::unordered_map<const Block*, size_t> indices; + for (size_t index = 0; index < program.blocks.size(); index++) { + indices.emplace(program.blocks[index], index); + } + // Buffers that may have been written on some path into each block, loops included. + std::vector<std::vector<uint8_t>> written_in(program.blocks.size(), + std::vector<uint8_t>(buffers)); + std::vector<size_t> pending(program.blocks.size()); + for (size_t index = 0; index < pending.size(); index++) { + pending[index] = pending.size() - 1u - index; + } + std::vector<uint8_t> state; + while (!pending.empty()) { + const auto index = pending.back(); + pending.pop_back(); + state = written_in[index]; + for (const auto& inst: *program.blocks[index]) { + apply_write(inst, state); + } + for (const auto* successor: program.blocks[index]->ImmSuccessors()) { + const auto found = indices.find(successor); + if (found == indices.end()) { + continue; + } + auto& target = written_in[found->second]; + bool changed = false; + for (size_t buffer = 0; buffer < buffers; buffer++) { + if (state[buffer] != 0u && target[buffer] == 0u) { + target[buffer] = 1u; + changed = true; + } + } + if (changed) { + pending.push_back(found->second); + } + } + } + std::vector<std::vector<uint8_t>> after(slots, std::vector<uint8_t>(buffers)); + std::vector<std::vector<uint8_t>> raw_after(raw != nullptr ? program.memory_info.size() : 0u); + for (size_t index = 0; index < program.blocks.size(); index++) { + state = written_in[index]; + for (const auto& inst: *program.blocks[index]) { + if (inst.GetOpcode() == ValueOpcode::ReadConst) { + const auto slot = slot_of(inst); + for (size_t buffer = 0; slot != NoSrtSlot && buffer < buffers; buffer++) { + after[slot][buffer] |= state[buffer]; + } + } else if (raw != nullptr && (inst.GetOpcode() == ValueOpcode::LoadAddressU32 || + inst.GetOpcode() == ValueOpcode::ReadConstBuffer)) { ``` Fixed in 0e3c6c8: `LoadBufferU32` is recorded like the other raw reads, and a raw-read index the analysis never saw now lists every written buffer, so `SpecializationReadFollowsBufferWrite` is conservative when it has no data. `TestScalarReadBeforeOwnBufferWrite` checks the unanalyzed case.
Author
Owner

@⁠coderabbitai[bot] on GitHub · 2026-10-08 21:14 UTC · original
On src/graphics/shader/recompiler/ir/Program.cpp line 315 (outdated):

@@ -218,6 +223,143 @@ bool HasShaderMemoryWrites(const Program& program) {
 	return false;
 }
 
+std::vector<std::vector<uint32_t>> SrtReadsAfterBufferWrites(
+    const Program& program, std::vector<std::vector<uint32_t>>* raw) {
+	const auto buffers = program.info.buffers.size();
+	uint32_t   slots   = static_cast<uint32_t>(program.srt_reads.size());
+	const auto slot_of = [](const Inst& inst) {
+		const auto slot = inst.Arg(1).Resolve();
+		return slot.IsImmediate() && slot.GetType() == Type::U32 ? slot.U32() : NoSrtSlot;
+	};
+	for (const auto* block: program.blocks) {
+		for (const auto& inst: *block) {
+			if (inst.GetOpcode() == ValueOpcode::ReadConst && slot_of(inst) != NoSrtSlot) {
+				slots = std::max(slots, slot_of(inst) + 1u);
+			}
+		}
+	}
+	std::vector<std::vector<uint32_t>> result(slots);
+	if (raw != nullptr) {
+		raw->assign(program.memory_info.size(), {});
+	}
+	if (buffers == 0 || (slots == 0 && raw == nullptr)) {
+		return result;
+	}
+	// Marks the buffers a store or atomic updates; one with an unknown target updates them all.
+	const auto apply_write = [&](const Inst& inst, std::vector<uint8_t>& written) {
+		const auto access = BufferAccessOf(inst.GetOpcode());
+		if (access != BufferAccess::Write && access != BufferAccess::Atomic) {
+			return;
+		}
+		const auto index    = inst.Flags<MemoryFlags>().index;
+		const auto kind     = index < program.memory_info.size() ? program.memory_info[index].kind
+		                                                         : ResourceKind::None;
+		const auto resource = kind != ResourceKind::None ? program.memory_info[index].resource : 0u;
+		if (kind == ResourceKind::Lds || kind == ResourceKind::Gds) {
+			return;
+		}
+		if (kind == ResourceKind::Buffer && resource < buffers) {
+			written[resource] = 1u;
+		} else {
+			std::fill(written.begin(), written.end(), uint8_t {1});
+		}
+	};
+	std::unordered_map<const Block*, size_t> indices;
+	for (size_t index = 0; index < program.blocks.size(); index++) {
+		indices.emplace(program.blocks[index], index);
+	}
+	// Buffers that may have been written on some path into each block, loops included.
+	std::vector<std::vector<uint8_t>> written_in(program.blocks.size(),
+	                                             std::vector<uint8_t>(buffers));
+	std::vector<size_t> pending(program.blocks.size());
+	for (size_t index = 0; index < pending.size(); index++) {
+		pending[index] = pending.size() - 1u - index;
+	}
+	std::vector<uint8_t> state;
+	while (!pending.empty()) {
+		const auto index = pending.back();
+		pending.pop_back();
+		state = written_in[index];
+		for (const auto& inst: *program.blocks[index]) {
+			apply_write(inst, state);
+		}
+		for (const auto* successor: program.blocks[index]->ImmSuccessors()) {
+			const auto found = indices.find(successor);
+			if (found == indices.end()) {
+				continue;
+			}
+			auto& target  = written_in[found->second];
+			bool  changed = false;
+			for (size_t buffer = 0; buffer < buffers; buffer++) {
+				if (state[buffer] != 0u && target[buffer] == 0u) {
+					target[buffer] = 1u;
+					changed        = true;
+				}
+			}
+			if (changed) {
+				pending.push_back(found->second);
+			}
+		}
+	}
+	std::vector<std::vector<uint8_t>> after(slots, std::vector<uint8_t>(buffers));
+	std::vector<std::vector<uint8_t>> raw_after(raw != nullptr ? program.memory_info.size() : 0u);
+	for (size_t index = 0; index < program.blocks.size(); index++) {
+		state = written_in[index];
+		for (const auto& inst: *program.blocks[index]) {
+			if (inst.GetOpcode() == ValueOpcode::ReadConst) {
+				const auto slot = slot_of(inst);
+				for (size_t buffer = 0; slot != NoSrtSlot && buffer < buffers; buffer++) {
+					after[slot][buffer] |= state[buffer];
+				}
+			} else if (raw != nullptr && (inst.GetOpcode() == ValueOpcode::LoadAddressU32 ||
+			                              inst.GetOpcode() == ValueOpcode::ReadConstBuffer)) {

@TheCruZ, thanks for the fix. The provided code records LoadBufferU32 raw reads and fills unanalyzed entries with every buffer index. This makes SpecializationReadFollowsBufferWrite conservative when analysis data is missing and addresses both parts of the finding.


If you found this review helpful, would you consider giving us a shout-out on X?

Thank you for using CodeRabbit!

✅ Review thread resolved.

You are interacting with an AI system.

**@⁠coderabbitai[bot]** on GitHub · 2026-10-08 21:14 UTC · [original](https://github.com/KytyPS5/KytyPS5/pull/1253#discussion_r4224201951) On `src/graphics/shader/recompiler/ir/Program.cpp` line 315 (outdated): ```diff @@ -218,6 +223,143 @@ bool HasShaderMemoryWrites(const Program& program) { return false; } +std::vector<std::vector<uint32_t>> SrtReadsAfterBufferWrites( + const Program& program, std::vector<std::vector<uint32_t>>* raw) { + const auto buffers = program.info.buffers.size(); + uint32_t slots = static_cast<uint32_t>(program.srt_reads.size()); + const auto slot_of = [](const Inst& inst) { + const auto slot = inst.Arg(1).Resolve(); + return slot.IsImmediate() && slot.GetType() == Type::U32 ? slot.U32() : NoSrtSlot; + }; + for (const auto* block: program.blocks) { + for (const auto& inst: *block) { + if (inst.GetOpcode() == ValueOpcode::ReadConst && slot_of(inst) != NoSrtSlot) { + slots = std::max(slots, slot_of(inst) + 1u); + } + } + } + std::vector<std::vector<uint32_t>> result(slots); + if (raw != nullptr) { + raw->assign(program.memory_info.size(), {}); + } + if (buffers == 0 || (slots == 0 && raw == nullptr)) { + return result; + } + // Marks the buffers a store or atomic updates; one with an unknown target updates them all. + const auto apply_write = [&](const Inst& inst, std::vector<uint8_t>& written) { + const auto access = BufferAccessOf(inst.GetOpcode()); + if (access != BufferAccess::Write && access != BufferAccess::Atomic) { + return; + } + const auto index = inst.Flags<MemoryFlags>().index; + const auto kind = index < program.memory_info.size() ? program.memory_info[index].kind + : ResourceKind::None; + const auto resource = kind != ResourceKind::None ? program.memory_info[index].resource : 0u; + if (kind == ResourceKind::Lds || kind == ResourceKind::Gds) { + return; + } + if (kind == ResourceKind::Buffer && resource < buffers) { + written[resource] = 1u; + } else { + std::fill(written.begin(), written.end(), uint8_t {1}); + } + }; + std::unordered_map<const Block*, size_t> indices; + for (size_t index = 0; index < program.blocks.size(); index++) { + indices.emplace(program.blocks[index], index); + } + // Buffers that may have been written on some path into each block, loops included. + std::vector<std::vector<uint8_t>> written_in(program.blocks.size(), + std::vector<uint8_t>(buffers)); + std::vector<size_t> pending(program.blocks.size()); + for (size_t index = 0; index < pending.size(); index++) { + pending[index] = pending.size() - 1u - index; + } + std::vector<uint8_t> state; + while (!pending.empty()) { + const auto index = pending.back(); + pending.pop_back(); + state = written_in[index]; + for (const auto& inst: *program.blocks[index]) { + apply_write(inst, state); + } + for (const auto* successor: program.blocks[index]->ImmSuccessors()) { + const auto found = indices.find(successor); + if (found == indices.end()) { + continue; + } + auto& target = written_in[found->second]; + bool changed = false; + for (size_t buffer = 0; buffer < buffers; buffer++) { + if (state[buffer] != 0u && target[buffer] == 0u) { + target[buffer] = 1u; + changed = true; + } + } + if (changed) { + pending.push_back(found->second); + } + } + } + std::vector<std::vector<uint8_t>> after(slots, std::vector<uint8_t>(buffers)); + std::vector<std::vector<uint8_t>> raw_after(raw != nullptr ? program.memory_info.size() : 0u); + for (size_t index = 0; index < program.blocks.size(); index++) { + state = written_in[index]; + for (const auto& inst: *program.blocks[index]) { + if (inst.GetOpcode() == ValueOpcode::ReadConst) { + const auto slot = slot_of(inst); + for (size_t buffer = 0; slot != NoSrtSlot && buffer < buffers; buffer++) { + after[slot][buffer] |= state[buffer]; + } + } else if (raw != nullptr && (inst.GetOpcode() == ValueOpcode::LoadAddressU32 || + inst.GetOpcode() == ValueOpcode::ReadConstBuffer)) { ``` `@TheCruZ`, thanks for the fix. The provided code records `LoadBufferU32` raw reads and fills unanalyzed entries with every buffer index. This makes `SpecializationReadFollowsBufferWrite` conservative when analysis data is missing and addresses both parts of the finding. --- <!-- This is an auto-generated comment: tweet message by coderabbit.ai --> If you found this review helpful, would you consider giving us a shout-out on [X](https://twitter.com/intent/tweet?related=CodeRabbitAI&text=Track%20%60LoadBufferU32%60%20raw%20reads%20and%20treat%20unanalyzed%20indices%20conservatively%20to%20prevent%20stale%20snapshots%20when%20shaders%20read%20buffers%20they%20wrote.%20Try%20%40coderabbitai%20for%20actionable%20code%20reviews.%20%F0%9F%9B%A1%EF%B8%8F&url=https%3A%2F%2Fgithub.com%2FKytyPS5%2FKytyPS5%2Fpull%2F1253%23discussion_r4223639546)? Thank you for using CodeRabbit! <!-- end of auto-generated comment: tweet message by coderabbit.ai --> ✅ Review thread resolved. _You are interacting with an AI system._ <!-- This is an auto-generated reply by CodeRabbit -->
Author
Owner

@⁠Crispy81 on GitHub · 2026-10-08 22:27 UTC · original · edited 2026-10-08 22:28 UTC

Out of curiosity, would this PR cover:

scalar resource reads overlap a shader buffer write
in D:\a\KytyPS5\KytyPS5\src\graphics\host_gpu\renderer\pipeline\descriptors.cpp:1038

or is it a separate issue?? I've seen this error on a few games.

**@⁠Crispy81** on GitHub · 2026-10-08 22:27 UTC · [original](https://github.com/KytyPS5/KytyPS5/pull/1253#issuecomment-6070305580) · edited 2026-10-08 22:28 UTC Out of curiosity, would this PR cover: scalar resource reads overlap a shader buffer write in D:\a\KytyPS5\KytyPS5\src\graphics\host_gpu\renderer\pipeline\descriptors.cpp:1038 or is it a separate issue?? I've seen this error on a few games.
Author
Owner

@⁠tv7 on GitHub · 2026-10-10 13:32 UTC · original

Another title that stops on this error: SILENT HILL: Townfall (PPSA33286 v02.003.000), Windows 11, RTX 3070 (driver 617.14).

  • Release 307fdcf: exits about 5 s after start with scalar resource reads overlap a shader buffer write (descriptors.cpp:978), black window, 1 VS / 30 CS compiled.
  • This PR's Windows CI build (d53546a): that error is gone. The title runs about 40 s and compiles 7 VS / 11 PS / 170 CS / 1 GS, then stops later on buffer descriptor is not a valid runtime value (ResourceTracking.cpp:506, compute 0xb9da5e64f4c5b10a), which is unrelated to this PR.

Full logs and details: #1373

Tested and written with Claude (AI assistant) on my machine.

**@⁠tv7** on GitHub · 2026-10-10 13:32 UTC · [original](https://github.com/KytyPS5/KytyPS5/pull/1253#issuecomment-6098027378) Another title that stops on this error: SILENT HILL: Townfall (PPSA33286 v02.003.000), Windows 11, RTX 3070 (driver 617.14). - **Release 307fdcf:** exits about 5 s after start with `scalar resource reads overlap a shader buffer write` (descriptors.cpp:978), black window, 1 VS / 30 CS compiled. - **This PR's Windows CI build (d53546a):** that error is gone. The title runs about 40 s and compiles 7 VS / 11 PS / 170 CS / 1 GS, then stops later on `buffer descriptor is not a valid runtime value` (ResourceTracking.cpp:506, compute `0xb9da5e64f4c5b10a`), which is unrelated to this PR. Full logs and details: https://git.gtahub.st/github/KytyPS5--KytyPS5/issues/1373 *Tested and written with Claude (AI assistant) on my machine.*

Pull request closed

This discussion has been locked. Commenting is limited to contributors.
No description provided.