Repository navigation
fix(shader): read GPU-selected V#s of s_buffer_load as SMEM does - #5161
Closed
nikowolf123 wants to merge 1 commit into
Closed
nikowolf123 wants to merge 1 commit into
nikowolf123 wants to merge 1 commit into
Conversation
An s_buffer_load_dword* whose V# the recompiler reads at run time (a GPU-selected V#, for example one built with v_readfirstlane) went through the vector buffer descriptor checks. A nonzero TYPE in word 3 made it return 0 and record an invalid-descriptor fault, and the base was used with its two low bits, so a base off a dword boundary read shifted bytes. SMEM uses only the base, stride and num_records of the V#, and the two low bits of the base and of the final address are ignored (AMD RDNA2 ISA guide, 30 November 2020, section 7.1 Table 27 SBASE and section 7.2.1 "Reads using Buffer Constant"). The capture-time evaluator already follows both rules, and the bound path already drops the two low bits. For scalar buffer loads the runtime path now masks the base and skips the TYPE check; vector, formatted and atomic accesses keep it. agc_driver_scalar_buffer_gpu_descriptor runs s_buffer_load_dword and s_buffer_load_dwordx2 through such a V# with the base 0 to 3 bytes off a dword and TYPE 0 to 3, in wave32 and wave64. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Contributor
|
First Windows build and hardware run. It passes on both GPUs, and without the fix it fails with the same message as on lavapipe. Setup: Windows 11 (26200), WinLibs GCC 15.2.0 r7, Release, CI configure flags, an RTX 4070 Laptop GPU (NVIDIA 595.97) and an Intel Raptor Lake-S iGPU (32.0.101.6790). Both have 32-lane subgroups. The head 86fdb52 merged into
Written with AI assistance. |
Contributor
Author
|
Thank you for the NVIDIA and Intel runs; I added them to the description. |
RINNECODER
added a commit
to RINNECODER/AnyPS5
that referenced
this pull request
Oct 13, 2026
* fix(shader): keep each BDA page-table walk to one inlined copy per read RADV inlines every SPIR-V function, DontInline included, so every get_bda_pointer call site becomes its own copy of the page-table search, and NIR's copy propagation of variables grows with each copy. The lookup now keeps its search bounds in phis and records its header and range faults from one call site each, picking the first failing check; a byte read and the byte fallback of a span read loop over one lookup instead of unrolling one per byte or dword. (cherry picked from commit 2d3a970) Ported from upstream PR boykopovar#3655 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * fix(shader): decode a GPU-selected formatted load from a format table A formatted load whose V# is selected on the GPU emitted all 77 format cases in its shared function, and drivers that inline every function, like RADV, copied all of them into each call site. The load now reads the format's component count, widths, byte and bit offsets and type from a constant table and runs one path for its four components, with the existing conversion of each (type, width) pair selected at run time. Stores and loads through a prepared V# keep the format switch. (cherry picked from commit 369928d) Ported from upstream PR boykopovar#3656 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * test(shader): pin GPU-selected formatted loads for every buffer format agc_driver_gpu_buffer_format loads the V#'s records and word3 from memory and moves them into SGPRs with v_readfirstlane, so the recompiler sees a GPU-selected descriptor and the load goes through the format table. It checks buffer_load_format x/xy/xyz/xyzw and d16_xyzw on two lanes for formats 1-77, selectors 0/1/4-7 on 1-, 2- and 4-component formats, loads with a component past NUM_RECORDS, and that formats 0 and 78 stop the invocation before its stores. The expected values were captured on the format switch (2d3a970) and are identical with the table. (cherry picked from commit 6aaae03) Ported from upstream PR boykopovar#3656 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * perf(shader): try the last resolved range first in BDA lookups Every access by guest address calls a lookup that binary-searches the BDA range table: about 11 dependent steps over ~1200 ranges, 34 table loads per access. Since boykopovar#639 every buffer access goes through it. The lookups (read, probe, write and atomic) now keep the last range a search of the invocation resolved in four private variables: guest begin and end, device base and permissions. An access inside that range with the wanted permissions returns base + (address - begin) at once. Anything else (another range, a missing permission, an unmapped, zero-byte or wrapping access) takes the full search with every check and fault it records today, and a successful search refills the cache. The variables only hold what the table says, and no shader writes the table, so the result is the search's own answer. On one title's busiest scene (Windows, discrete GPU), GPU time of the draws 59.5 -> 35 ms per frame. APS5_NO_BDA_LOOKUP_CACHE=1 emits the lookups without the cache. The switch is part of the shader disk cache key. (cherry picked from commit 443583a) Ported from upstream PR boykopovar#2837 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * perf(shader): read adjacent BDA dword loads of one block through one span read A scalar load of several dwords (s_load_dwordx8) becomes one LoadAddressU32 per dword, and each took its own span read: a probe of the page table and a fallback that looks every byte up. Loads in one block with equal address operands and offsets within 16 bytes of the first now share its span read. Instructions that write memory end a group; the address operands are compared by content, since each load carries its own GetAddressResource and SelectU32 values. A title's main-menu pixel shader goes from 136 span reads to 34. AMD's Windows compiler inlines the read helpers at every call, and did not finish that shader before. (cherry picked from commit 4577633) Ported from upstream PR boykopovar#4931 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * fix(shader): read GPU-selected V#s of s_buffer_load as SMEM does An s_buffer_load_dword* whose V# the recompiler reads at run time (a GPU-selected V#, for example one built with v_readfirstlane) went through the vector buffer descriptor checks. A nonzero TYPE in word 3 made it return 0 and record an invalid-descriptor fault, and the base was used with its two low bits, so a base off a dword boundary read shifted bytes. SMEM uses only the base, stride and num_records of the V#, and the two low bits of the base and of the final address are ignored (AMD RDNA2 ISA guide, 30 November 2020, section 7.1 Table 27 SBASE and section 7.2.1 "Reads using Buffer Constant"). The capture-time evaluator already follows both rules, and the bound path already drops the two low bits. For scalar buffer loads the runtime path now masks the base and skips the TYPE check; vector, formatted and atomic accesses keep it. agc_driver_scalar_buffer_gpu_descriptor runs s_buffer_load_dword and s_buffer_load_dwordx2 through such a V# with the base 0 to 3 bytes off a dword and TYPE 0 to 3, in wave32 and wave64. (cherry picked from commit 86fdb52) Ported from upstream PR boykopovar#5161 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * perf(recompiler): read the buffer stride and presence at run time in compute modules (cherry picked from commit 573ba3b) Ported from upstream PR boykopovar#5353 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> --------- Co-authored-by: EdouardSence <103503538+EdouardSence@users.noreply.github.com> Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com> Co-authored-by: DotDebian <quentin.waguet@hotmail.fr> Co-authored-by: poonasor <rickypoon89@icloud.com> Co-authored-by: Niko <nikobilger2000@gmail.com> Co-authored-by: Alex Collini <pipodi93@gmail.com>
Contributor
Author
|
Closing this following the request on #4926 to hold off on pull requests that do not name a title needing the change while the queue is this long. The branch stays; if a title stops on this on current |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
s_buffer_load_dword*through a V# that the recompiler reads at run time handled that V# the way vector buffer accesses do. This is thegpuDescriptorpath: a V# dword comes from a value the SRT walker cannot evaluate when it captures resources, for examplev_readfirstlane_b32of a loaded VGPR. It went wrong in two ways, both indescriptor()inSpirvRuntimeBuffer.cpp(line numbers onmain0f2683c):InvalidDescriptorBDA fault, which also stops the invocation when BDA faults stop invocations, and line 88 makes the load return 0. After the dispatch the driver reportsBDA access failed: ... reason=8.Sources:
m_base,m_strideandm_num_recordsonly and never reads word 3.main, both on RDNA2 (gfx1035):s_buffer_loadreads(base & ~3) + (offset & ~3).SrtEvaluator.cpp:132-149) already follows both rules: it reads word 3 but never checks TYPE, and it reads at((base & ~3) + byteOffset) & ~3. The bound path drops the two low bits (baseMisalignmentreturns 0 for a scalar buffer,SpirvRuntimeBuffer.cpp:145), but it still rejects a nonzero TYPE when the resource is materialized (ResourceMaterializer.cpp:590).smem_buffer_load_callbackinsrc/amd/compiler/instruction_selection/aco_select_nir_intrinsics.cpp:487-534passes the V# tos_buffer_load_*unchanged and does not state either rule. I found no LLVM or Mesa text that states them, so the ISA guide is the source.What changed:
descriptor(): for aResourceKind::ScalarBufferaccess, the base takes dword 0 with bits 1:0 cleared.invalidisConstantBool(state, false), as in the formatted path of the same file.validis stillbase != 0. A base of 1 to 3 (with zero high bits) is now the same V# as base 0 and reads 0 like a null base. Before, it read from guest address 1 to 3 plus the offset.addr = (m_base + offset) & ~0x3taken alone would differ when the low bits of the base and the offset carry. This change keeps(base & ~3) + (offset & ~3), which matches the section's text and the gfx1035 measurement.docs/dev/debt/shader.md: a new entry follows "V# bases off a DWORD boundary". It says the TYPE part is not measured, and that vector accesses through such a V# still read 0 for a nonzero TYPE.#5049 applies the same word 3 rule on the bound path (
ResourceMaterializer.cpp). This PR changes a different file, andgit merge-treemerges the two cleanly. #5049 reports that the captured PPSA26358 scene shader0x12c2298400stopped on the bound path'sbuffer descriptor uses an unsupported type. I have no capture that reaches this runtime path, so no title is claimed here.Tested
Built and run on
mainc827e11; the latermaincommits up to 0f2683c change only CI workflows,README.mdandtools/progress*.py. Linux (WSL2 Ubuntu), GCC, Release, configured like CI (-DBUILD_TESTING=ON -DANYPS5_ENABLE_SPIRV_TOOLS=ON, ccache). Tests ran on lavapipe (llvmpipe, LLVM 21.1.8, 256 bits) withVK_ICD_FILENAMES=<lvp_icd>.json ANYPS5_REQUIRE_VULKAN=1 GALLIUM_OVERRIDE_CPU_CAPS=avx.New test
ScalarBufferGpuDescriptor.cpp(agc_driver_scalar_buffer_gpu_descriptor):buffer_load_dwordx4and moves it to SGPRs with fourv_readfirstlane_b32, so the load takes thegpuDescriptorpath. It then runss_buffer_load_dwordat offset 0 ands_buffer_load_dwordx2at offset 4. It runs in wave32 and wave64 with 64 threads.GuestAllocationsblock, so a stale V# would also fail. Every thread's three dwords must equal the dwords at the region's aligned start.MESA_SHADER_CACHE_DISABLE=true) the test takes 3.3 to 3.7 s.Results for the new test with each version of
SpirvRuntimeBuffer.cpp(onmainc827e11):main's file: fails with "wave32, base +1 (word 3 0x31016fac): thread 0 s_buffer_load_dword is 0xefc69d74, expected 0xc69d744b".Other runs:
ctest -j4 -R 'agc_shader|agc_driver|shader|recompiler': 384 tests, 348 passed, 36 skipped, 0 failed.ctest -j4: 1053 tests, 1010 passed, 43 skipped, 0 failed.mainc827e11 in the same tree: 383 tests, 347 passed, 36 skipped in the filtered run; 1052 tests, 1009 passed, 43 skipped in the full run. Per-test statuses are identical apart from the new test.git merge-treeagainst every open PR that touchesSpirvRuntimeBuffer.cppordocs/dev/debt/shader.md(29 PRs, perf(recompiler): select guest memory loads instead of branching around them #4048 among them): this branch adds no conflict that the PR does not already have withmain.tools/check_conventions.py --base origin/main(main's checker) andgit diff --check: clean.This was not measured on AMD hardware. A gfx103x reviewer can check both rules with the hardware oracle, from the repository root:
How the kernel works:
s[4:5] + 0x200.Per the ISA, all 20 lines end in
04030201 08070605 0c0b0a09 100f0e0d 14131211. If base bits 1:0 were used, the offset 1 to 3 lines would be shifted by that many bytes. If SMEM used TYPE, the lines with a nonzero TYPE (71016fac,91b00f2e,c0000000,ffffffff) would differ from the31016faclines.Native Windows 11, WinLibs GCC 15.2.0, NVIDIA GeForce RTX 5080 (driver 616.92), on this branch rebased onto
main0f2683c (86fdb52):agc_driver_scalar_buffer_gpu_descriptorpasses 3 of 3 runs, andctest -R 'agc_shader|agc_driver|shader|recompiler'passes 383 of 383.Run by @kr4y4 on Windows 11 on an RTX 4070 Laptop GPU (NVIDIA 595.97) and an Intel Raptor Lake-S iGPU (32.0.101.6790), head 86fdb52 merged into
main3b94f92 (comment):agc_driver_scalar_buffer_gpu_descriptorpasses 3 of 3 runs on both GPUs and the full suite 1087 of 1087; withSpirvRuntimeBuffer.cppput back tomainthe test fails on both with the same message as on lavapipe.Checklist
main; no other open PR implements the same functionsNotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file.mdfiles or images added to the repository (attach them to this PR)🤖 Generated with Claude Code