shader: cheaper SRT walks for flattened reads and block conditions #1322
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!1322
Loading…
Reference in a new issue
No description provided.
Delete branch "gh-pr/1322/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 18:09 UTC · original
NeySlim:perf/srt-flat-reads→main· +1310 −196 · 6 files · 5 commits · closed without mergingStacked on #1287: it changes the compiled-node SRT walker added there. The branch holds the commits of #1287, then the two new commits.
Problem
Every draw and dispatch refreshes the flattened SRT of its shaders through the SRT walker. In God of War Ragnarök (PPSA08332, 05.005) that is 3,600 to 4,700 stage walks per frame depending on the scene, and a walk reads about 74 SRT DWORDs. In the six large plans I dumped from the game, every flattened SRT slot is such a read: a
LoadAddressU32at a constant offset below aGetAddressResourcewhose address DWORDs are user data; the constant-buffer reads (ReadConstBuffer) have the same shape with four handle DWORDs.Each of these reads goes through
EvaluateRef,EvaluateNodeRef, the kind dispatch ofEvaluateNode, thenEvaluateRawRead, which fetches the handle DWORDs through a capturing lambda. On a synthetic plan shaped like the game's (96 SRT reads, a dozen descriptors), a walk executes about 41,700 instructions.Block conditions go through
Evaluate(Value), which resolves the value, looks up and checks its node and refreshes the walker's pointers on every walk; the walker refreshes its pointers again after each block. Large plans in this game have up to 264 blocks.Change
shader: read flattened SRT DWORDs without the walker dispatch: when a plan's SRT reads are compiled, a scalar-address or constant-buffer DWORD read with a constant offset operand records its handle DWORDs and its aligned offset (WalkerRead, one per SRT read root).RefreshFlatBufferevaluates such a root directly with the stepsEvaluateNodeRefandEvaluateRawReadtake for that node shape: the same memo entry and in-progress mark, the handle DWORDs evaluated in the same order, the same address and bounds checks (a helper now shared withEvaluateRawRead), the same reader. A root of another shape, a walker with an active EXEC mask, or a clean read reaching a walker that delegates it keeps the generic path.shader: compile SRT block conditions once per plan: block conditions become walker roots like the SRT reads and the descriptor DWORDs (walker_conditions): compiled on first use, compared with the plan's condition on each walk so that a replaced condition is followed, and dropped with the other compiled nodes. Compiling a root refreshes the walker's pointers, so the refresh after each block is no longer needed.The walker's results, reads and failures do not change. On the synthetic plan above, a walk now executes about 32,000 instructions (24,300 instead of 35,600 when the plan does not capture its reads).
Test
TestFlatReadsKeepReadSemantics(resource_materialization_tests): scalar-address reads (unaligned table pointer, unaligned offset operand, negative immediate, a handle whose address DWORDs are themselves SRT reads) and a constant-buffer read (unaligned base and offset operand, records and fourth DWORD read from memory through the handle only). A recording reader checks the flattened values, the addresses read and their order, that the reads were prepared for the direct path, a later walk with changed memory, failed reads (an SRT DWORD, the buffer's fourth DWORD) that stop the walk, the bounds of a buffer without a stride (4 and 11 records rejected, 12 accepted), and an address below zero rejected before reading.TestCleanFlatReadsUseTheStrictReader: in a plan that captures its reads (it branches on a value read from memory), the flat reads feeding a written buffer are clean. They go through the strict reader once each (the branch reuses their memo), the other flat read through the ordinary reader, and all three are captured.TestNegativeBufferImmediateIsNotRead: a constant-buffer read below its base is not prepared and is not read, even with bounds that would accept it.TestRecompiledWalkerFollowsBlockConditions: after a replaced SRT read recompiles the plan's nodes, the block condition is compiled again and follows the memory.TestReplacedBlockConditionIsFollowed(resource_tracking_tests): a replaced block condition, then the original one, are followed by the next walks.Without their checks on
walker_reads, these cases also pass on #1287 alone. Mutation check: each of these changes fails a case: an unaligned base (scalar or buffer), a dropped offset operand (scalar or buffer), either half of the buffer bounds check, an ignored stride, a buffer read taken as a scalar one, a read that bypasses the reader, the memo entry left unset, the records and fourth DWORD evaluated in the other order or the fourth DWORD skipped, the reads not prepared, a negative buffer immediate prepared, a condition root reused without comparing it with the plan's condition or kept when the nodes are recompiled. A direct path that skips the 48-bit address check of buffer reads failsTestBufferReadPastAddressSpaceFailsfrom #1287.Validation
Linux, Ryzen AI Max+ 395, Radeon 8060S, Mesa RADV 26.2.3.
In God of War Ragnarök from a new game, on main with my open PRs (#1287 among them):
On this branch:
resource_materialization_tests,resource_tracking_tests,scalar_provenance_testsandshader_cfg_testspass.shader_recompiler_compute_tests, one case per process: same results as main (602 of 612 cases pass; the same 10 fail on both with this RADV setup).Not tested on Windows or macOS. The change is platform-independent C++. The two
[[gnu::noinline]]hints, already used elsewhere in the tree, keep the slow paths out of line; the small result struct comes back in registers on System V x86-64 and through memory on Windows x64.AI use
I used Claude Code (AI agent) to measure the walker, write the change and the tests, 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 18:14 UTC · original · edited 2026-10-09 18:37 UTC
No actionable comments were generated in the recent review. 🎉
ℹ️ Recent review info
⚙️ Run configuration
a89766ab-c90e-47b7-ab8e-f1d5deff2fb8📥 Commits
Reviewing files that changed from the base of the PR and between 3bc5536248c0012a5852ced618a42464ece1cc36 and
ae744a5c15.📒 Files selected for processing (2)
src/graphics/shader/recompiler/ir/passes/SrtWalker.cpptests/ResourceMaterializationTests.cppIncluded review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 2 remain after this review.
📝 Walkthrough
Walkthrough
Resource plans now store compiled walker data, and
SrtWalkerevaluates roots through compiled references with memoization. The walker refreshes compiled state when SRT roots change and validates raw-read addresses and buffer bounds. New tests cover walker updates, lazy evaluation, control-flow conditions, and memory-read behavior.Suggested reviewers:
nmzikPriority: ➖ Normal
Change: Refactor
Merge Risk: ⚪ Minimal · up to
ae744The previously identified out-of-range read is guarded, and no remaining issue identified here prevents merging after normal checks.
🚥 Pre-merge checks | ✅ 4 | ❌ 1
❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
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.@coderabbitai[bot] on GitHub · 2026-10-09 18:14 UTC · original · edited 2026-10-09 18:37 UTC
On
src/graphics/shader/recompiler/ir/passes/SrtWalker.cppline 117 (outdated):🩺 Stability & Availability | 🟡 Minor | ⚡ Quick win
Reject buffer DWORD addresses that leave the 48-bit address space.
For scalar reads,
AddSignedAddressrejects results aboveAddressMask. For buffer reads, the code addsbyte_offsettoReadBase(low, high)without the same check.BufferDwordInBoundsallowsbyte_offsetup to about0x3fff * 0xffffffff, which is about 2^46. A guest descriptor with a base near 2^48 therefore produces an address aboveAddressMask.ReadWordthen passes that address toread_memory. Ifread_memoryis null, it copies directly from the unchecked host address withmemcpy.ReadScalarTableinResourceMaterialization.cppalready rejectsoffset > AddressMask - base. BothEvaluateRawReadandComputeFastReadcallRawReadAddress, so one check fixes both paths.🛡️ Proposed fix
📝 Committable suggestion
✅ Confirmed as addressed by @NeySlim
@coderabbitai[bot] on GitHub · 2026-10-09 18:14 UTC · original · reviewed
Actionable comments posted: 1
ℹ️ Review info
⚙️ Run configuration
f37752a7-61e7-481f-8de4-2fbc1cd23526📥 Commits
Reviewing files that changed from the base of the PR and between
c1926f915dand 3bc5536248c0012a5852ced618a42464ece1cc36.📒 Files selected for processing (6)
src/graphics/shader/recompiler/ir/ShaderIR.hsrc/graphics/shader/recompiler/ir/passes/ResourceMaterialization.cppsrc/graphics/shader/recompiler/ir/passes/SrtWalker.cppsrc/graphics/shader/recompiler/ir/passes/SrtWalker.htests/ResourceMaterializationTests.cpptests/ResourceTrackingTests.cppIncluded review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 6 remain after this review.
@NeySlim on GitHub · 2026-10-09 18:33 UTC · original
On
src/graphics/shader/recompiler/ir/passes/SrtWalker.cppline 117 (outdated):Confirmed, thanks. The same unchecked addition was already in
EvaluateRawReadof #1287 (and in the interpreter on main), so the fix is its own commit in #1287,1d5801e7: a buffer DWORD whose address leaves the 48-bit address space now fails before reading, like a scalar read.TestBufferReadPastAddressSpaceFailscovers it and fails without the check. This branch is rebased on it:RawReadAddressnow ends withAddSignedAddressfor both kinds of reads (43ff904d), and a direct path that skips the check also fails that test.@coderabbitai[bot] on GitHub · 2026-10-09 18:37 UTC · original
On
src/graphics/shader/recompiler/ir/passes/SrtWalker.cppline 117 (outdated):@NeySlimThanks for the fix and the regression-test details. The suppliedRawReadAddressnow checks buffer bounds, then callsAddSignedAddressfor both read kinds. This addresses the unchecked addition in bothEvaluateRawReadandComputeFastRead.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.
@NeySlim on GitHub · 2026-10-12 15:17 UTC · original
Closing as well, it sits on top of #1287. Covered by #1456.
Pull request closed