gpu: make GPU-side scalar loads resident before the shader runs #1344
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!1344
Loading…
Reference in a new issue
No description provided.
Delete branch "gh-pr/1344/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-10 00:07 UTC · original
NeySlim:fix/gpu-scalar-load-residency→main· +339 −0 · 8 files · 1 commits · closed without mergingProblem
Since
5b68a26c("shader: keep scalar payload and terminal conditions on the GPU"), anS_LOADwhose result is only used as data stays on the GPU as a BDA load instead of being read into the flattened SRT by the host plan. That keeps the aliased atomic case of that commit correct. BDA, however, only finds pages that already have a buffer in the buffer cache: a load from a page without one reads zero and sets a fault bit, andFaultManager::ProcessFaultBuffercreates the buffer after the submission. The faulting dispatch is not run again.Nothing gives those pages a buffer before the shader runs, so the first dispatch or draw that reads a CPU-written table through such a load sees zeros. In God of War Ragnarok (PPSA08332), a new game hangs the GPU (
ErrorDeviceLost, gfx ring timeout) on the calibration screen. The RADV hang dump points at CS 0x5c046e9ef798f2bc, which builds a loop bound from a user-data table:With zeros the bound is 0xffffffff. The hung pipeline has the BDA page table and fault buffer bound and no flattened SRT; dumps of the same shader from builds before
5b68a26cread this load through the flattened SRT.The fault path suits addresses that only the GPU knows (vector addresses, GPU-selected descriptors). This address is known before the dispatch: it comes from user data and SRT reads that the host plan already evaluates, the same values the flattened SRT path used for these loads. The renderer already does the same for mesh draws, which register the guest index range with
FindBufferbeforePrepareGraphicsBindingsso that the shader's BDA reads find it.Change
ExtractResourcePlanrecords eachLoadAddressU32that stays on the GPU as a scalar BDA load (ScalarAddress, not planning-only) when its base and SGPR offset are runtime values built only from user data, the shader address, immediates and planned SRT reads (GpuScalarRead: address terms, instruction offset, block).MaterializeResourcesevaluates these addresses right afterRefreshFlatBuffer, with the same DWORD-aligned terms as the shader'sGuestAddress, and stores their byte ranges inResourceSnapshot::gpu_scalar_reads(contiguous DWORDs merged). Loads in blocks that the refresh did not visit are skipped. The visited ones only depend on SRT reads that the refresh already evaluated, so no address is read that the refresh did not read, and the loaded value itself is never read on the host: the aliased atomic case of5b68a26cstill has no specialization read. An address the host cannot evaluate keeps the fault path.RenderExecutor::FindBufferspasses each stage's ranges to the newRenderContext::FindBdaBuffers, which callsBufferCache::FindBufferpage by page on their mapped parts, like the fault buffer does, so a page that a buffer already holds is not merged with a neighbouring page that has none.PrepareBdathen uploads them with the other mapped buffers before the dispatch or draw.Programs without such loads have an empty list, so other draws only pay an empty check.
Limits: a load whose address depends on memory the shader reads itself (for example a pointer loaded with a register offset) keeps using the fault path. A load behind a branch that the host plan does not decide (its condition is not kept on the host since
5b68a26c) is made resident even when the branch is not taken; that costs a buffer for a mapped page, not a wrong read.Test
ScalarLoadResidency(shader_recompiler_compute_tests, also--scalar-load-residency-only): the GoW load throughCommandProcessor::DispatchDirectwith the real pipeline and buffer caches. The table sits in a page that no descriptor of the dispatch covers. The test checks that the load is a BDA read (no flattened SRT) from a page without a buffer, then that the first dispatch stores 3 and 2 from the CPU table. Without theFindBdaBufferscall it stores 0 and 0.BdaReadBuffers: a range that crosses from a held buffer into an empty page, and one that leaves the mapping. The held buffer must stay as it is, the empty page gets its own buffer, and nothing is created outside the mapped range.shader_cfg_tests:TestGpuScalarReadSkipsUnreachedBlock(a load in a block the host plan skips gets no range, a taken one does, and the host reads no memory),TestGpuScalarReadNeedsHostAddress(a load offset by another GPU-side load gets no range and is not evaluated on the host), andTestNativeScalarAtomicPayloadStaysOnGpunow checks the reported range while the mutable flag is still not read on the host.CheckResourcePlanHandoffchecks the range of its numeric payload load.Validation
Linux, AMD Radeon 8060S (RADV, Mesa 26.2.3):
shader_recompiler_compute_tests, one case per process: 614 cases, the same 10 failures as main59183dbfon this GPU; the two new cases pass.FindBdaBufferscallScalarLoadResidencyreads 0/0; without the plan entriesResourcePlanHandoffandshader_cfg_testsfail too; without the visited-block check, the dependency check or the page-by-page loop, the matching new test fails.ctest: 45 of 53 pass; the 8 others (GpuTilerCpuParity, and image creation with a D24S8 format this GPU lacks) fail the same way on main59183dbf.Not tested on Windows. The change has no platform-specific code, and the tests use the same fixtures as
NativeIndirectDispatchandBdaPageTableUploads.AI use
I used Claude Code (AI agent) to investigate the hang (RADV hang dump, shader disassembly, a harness reproduction), write the change, the tests and this description, and run the tests above on my machine. A second agent reviewed the diff independently, and its findings are addressed here (non-fatal address evaluation, page-by-page lookups, the negative tests). I have not run the game with this change yet.
@coderabbitai[bot] on GitHub · 2026-10-10 00:12 UTC · original
No actionable comments were generated in the recent review. 🎉
ℹ️ Recent review info
⚙️ Run configuration
51165fc3-1ae8-4f75-990b-37ed740edbe5📥 Commits
Reviewing files that changed from the base of the PR and between
59183dbfa8and637f278b0a.📒 Files selected for processing (8)
src/graphics/host_gpu/renderer/pipeline/descriptors.cppsrc/graphics/host_gpu/renderer/renderContext.cppsrc/graphics/host_gpu/renderer/renderContext.hsrc/graphics/shader/recompiler/ir/ResourceSnapshot.hsrc/graphics/shader/recompiler/ir/ShaderIR.hsrc/graphics/shader/recompiler/ir/passes/ResourceMaterialization.cpptests/ShaderRecompilerComputeTests.cpptests/shaderCfgTests.cppIncluded review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 7 remain after this review.
📝 Walkthrough
Walkthrough
The change records eligible GPU-side scalar reads during resource planning, refreshes their address ranges during materialization, and exposes those ranges to the renderer. The renderer finds buffers for mapped portions of the ranges in caching-page-sized chunks. Tests cover address evaluation, control flow, buffer ownership, and scalar-load residency.
Priority: ⬆️ High
Estimated code review effort:
Suggested reviewers:
nmzikChange: Bug fix
Merge Risk: ⚪ Minimal · up to
637f2No confirmed issue remains that should hold this change from normal merge checks.
Pre-merge checks |
4 |
1
❌ Failed checks (1 inconclusive)
✅ Passed checks (4 passed)
Full details: Docstring Coverage
Explanation
Docstring coverage is 14.29% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 14 functions across 6 files. (2 skipped: 2 too large.)
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.@NeySlim on GitHub · 2026-10-10 01:55 UTC · original
Upstream reverted
5b68a26cin1c301c9a, so the hang this PR fixes no longer happens on main: scalar loads from user data are read on the CPU again. I am leaving this PR open in case the GPU-side scalar loads come back; it would then be needed as is (the test covers the non-resident page case). Feel free to close it if that change is not planned to return.@theantipopau on GitHub · 2026-10-10 02:39 UTC · original
Evidence from a Windows fork build (AMD RX-series host, clang-cl):
637f278bapplies on top of current fork main (ccfb55d9+ our SPIRV-IR mergef3836bb8) as pick58b2295c.tests/shaderCfgTests.cpp; we took the upstream test body (TestNativeScalarAtomicPayloadStaysOnGpu, the SAROS b62b494c program) verbatim on top of the fork's existing cover-art pieces of that file — all other 7 files merged clean.Goal for us: with #1346 (barycentric optional) plus resident scalar loads, our RX-class GPU should finally reach the Vulkan harness rasterization features path that has been failing locally.
@NeySlim on GitHub · 2026-10-10 02:43 UTC · original
Thanks for testing it on Windows and AMD, that is useful.
One note: main reverted
5b68a26cin1c301c9a, so on current main scalar loads from user data are read on the CPU again and this change has nothing to act on there. It only matters on a tree that keeps the GPU-side scalar loads, as yours seems to. If your tree keeps them, this fix (or reverting5b68a26cas main did) is what avoids the zero reads and the loop hang.Pull request closed