perf(shader): look up the BDA range of a user-data pointer once at program entry #5859
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/5859"
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?
Imported from boykopovar/AnyPS5. Originally posted by gjonn on 2026-10-14T13:54:42Z.
What
Address-based reads whose 64-bit base is a pair of user-data registers (
GetAddressResource(GetUserData, GetUserData)) search the BDA table at every read, although the base is one value for the whole draw. In the 4K fragment passes of PPSA01317's main map most global/scalar reads are of this kind, and the per-read searches (and the registers they hold across the program) dominate the pass.With this change, when a program has two or more such read sites of one base (counted after adjacent loads are grouped; at most four bases, those with the most read sites):
CollectBdaAnchorspicks the bases;bda_anchor_range, a variant of the lookup that returns the range(begin, bound, device)instead of one device address and records no faults, finds the range holding each base once at the program's entry (before the pixel interlock).boundis the range's end, or the next table entry's begin when that comes first: a lookup selects the last range beginning at or below an address, so on tables whose ranges overlap the anchor only covers the addresses for which a lookup would select the same range. When the program already requires subgroup ballots, the subgroup's elected lane searches and broadcasts the result; otherwise each invocation searches once.LoadBda, grouped adjacent loads,LoadBdaWide) takes one branch: when the anchor's range holds all of its bytes (offset(base - begin) + zext(low) + immediatechecked againstsize - bytes), and for dword reads the device address is 4-aligned, it loads them directly (device-scope atomic loads for coherent dwords, byte loads for 8/16-bit reads, as the lookup path does). Otherwise it runs the lookup path unchanged, so data, fault records, invocation stops and wraparound are exactly as before.0xffffffff00000000(sobase + zext(low)cannot wrap and the lookup path's overflow checks cannot fire for a read the range holds), when the range is longer than 2^62 bytes (so the offset arithmetic cannot wrap with a positive immediate), or when its device addresses wrap.GuestBufferMemory::AddressRangesmerges a build's regions read over the address space's ranges).BdaAbi::RangesOrderedstates that contract, andBdaResourcesnow requires it when it builds a table (once per new table, not on cache hits).BdaAbi::RangesOrderedis added toshaderCacheReviewedNames.No switch: anchors are how such reads are generated.
Tested
Windows 11, MinGW-w64 GCC 15.2, NVIDIA GeForce RTX 5090 Laptop GPU (driver 616.92). Built on
mainatcee9841b6; the branch merges cleanly into the currentmain.agc_driver_bda_device(newRunBdaAnchorTests): GPU exactness. Programs with 36 read sites (8, 16 and 32-bit reads, 1/3/4-dword reads with all and some dwords used, immediates 0, 4, -1, -8, 0x7ffffff0 and INT32_MIN) are compiled twice, throughEmitAnchoredBdaReadand through the lookups alone, and run against a table with byte-granular ranges, a range mapped to an odd device address, a range overlapping the one before it, a range ending at the anchor limit and one above it. For 15 bases (in each range, at range ends, unmapped, near 2^64) and 59 lane offsets per dispatch, values and fault state must match; with invocation stops, single-lane runs must match the whole fault record. Also coherent reads, elected search with 64 lanes, a range without read permission, an invalid table and a range longer than 2^62 bytes. A program whose lookup path returns a sentinel shows which reads load directly: real data inside the anchor's range, the sentinel at its end, across ranges, past the next range's begin, at an odd device address, above the limit, without read permission and with an invalid table. Mutations checked locally: removing the next-begin bound, the address limit, the alignment check or the size check, an off-by-one in the range check, ignoring the first used dword and never taking the direct path each make it fail. Removing the 2^62 span limit does not, because with real device addresses (above 2^32) such a range is already refused for wrapping device addresses.agc_driver_bda_anchor_reads(new): a recompiled wave32/wave64 compute program with three adjacentglobal_load_dword(grouped into one span read), an unaligned one and twos_load_dwordthrough the user-data bases[0:1]runs on the GPU and must search an anchor and store the expected data. The grouped and unaligned reads take the anchor (shifting their offset fails the test); the scalar reads are evaluated from captured memory at recompile time, so they do not reach the anchor path.agc_shader_memory(newverifyBdaAnchors): codegen. Recompiled compute programs with 0, 1, 2 and 4global_load_dwordthroughs[4:5](separate and adjacent offsets) plus one VGPR-addressed load: anchors exactly when two or more read sites share the base (adjacent loads grouped into one span read count once), one search inmainbefore any read, every read keeps itsread_bda_spanlookup path, anchored reads load directly. With anchors disabled it fails.BdaContracts:RangesOrderedaccepts tables ascending by begin, including empty and overlapping ranges, and refuses unsorted ones.ctest -R "^agc_|^shader": 531 of 531 pass (3 skipped by design).spirv-val --target-env vulkan1.3on the SPIR-V of both new tests (codegen programs and GPU test programs, plain and anchored): valid.python tools/check_conventions.py --base origin/mainpasses.agc_driver_bda_devicetakes 6-14 s here instead of 5 s.RunBdaExecutionTests(and so the new GPU tests) is skipped on CPU Vulkan devices, so lavapipe CI does not run them.Performance. These numbers come from my local branch, not from stock
main: that branch carries an earlier version of this change on top of local BDA lookup changesmaindoes not have (a lookup hint and cache), so the baseline differs frommainand the gain onmainis not measured. I could not replay the same requests onmainbecause its request format has moved on from the dumps.Not tested: AMD, Intel, Linux, macOS; real pixel shaders compiled from this branch (only the test programs above).
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)Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-14T14:06:56Z.
Other open pull requests touch the same code. Merging one will break the other, or both implement the same thing. If this one builds on them, list them in Depends on.
main)core/shader/recompiler/SpirvBackend/src/SpirvModuleEmitter.cppL1189core/libs/prx/libSceAgcDriver/tests/BdaContracts.cppL11, L183core/shader/recompiler/SpirvBackend/src/SpirvModuleEmitter.cppL1189Some comparisons could not be completed because a pull request conflicts with the base branch. Review these shared files manually before concluding that there is no overlap.
core/libs/prx/libSceAgcDriver/tests/ShaderMemory.cppcore/shader/recompiler/CMakeLists.txtcore/shader/recompiler/SpirvBackend/include/SpirvBackend/SpirvEmitterState.hppcore/shader/recompiler/SpirvBackend/src/SpirvModuleEmitter.cppcore/shader/recompiler/SpirvBackend/include/SpirvBackend/SpirvEmitterState.hppcore/shader/recompiler/SpirvBackend/src/SpirvMemory/SpirvMemoryInstructions.cppcore/libs/prx/libSceAgcDriver/tests/ShaderMemory.cppcore/libs/prx/libSceAgcDriver/tests/ShaderMemory.cppcore/shader/recompiler/SpirvBackend/src/SpirvModuleEmitter.cppcore/shader/recompiler/CMakeLists.txtcore/libs/prx/libSceAgcDriver/tests/ShaderMemory.cppcore/libs/prx/libSceAgcDriver/tests/ShaderMemory.cppcore/shader/recompiler/SpirvBackend/include/SpirvBackend/SpirvEmitterState.hppcore/libs/prx/libSceAgcDriver/tests/ShaderMemory.cppcore/shader/recompiler/SpirvBackend/include/SpirvBackend/SpirvEmitterState.hppcore/shader/recompiler/SpirvBackend/src/SpirvModuleEmitter.cppcore/libs/prx/libSceAgcDriver/tests/ShaderMemory.cppcore/libs/prx/libSceAgcDriver/tests/ShaderMemory.cppcore/shader/recompiler/SpirvBackend/src/SpirvModuleEmitter.cppcore/libs/prx/libSceAgcDriver/Graphics/src/BdaResources.cppcore/libs/prx/libSceAgcDriver/Graphics/src/BdaResources.cppcore/libs/prx/libSceAgcDriver/tests/BdaExecution.cppcore/libs/prx/libSceAgcDriver/tests/BdaShader.cppcore/libs/prx/libSceAgcDriver/tests/BdaShader.hppcore/shader/recompiler/BdaAbi.hppcore/libs/prx/libSceAgcDriver/tests/ShaderMemory.cppcore/shader/recompiler/SpirvBackend/include/SpirvBackend/SpirvBda.hppcore/shader/recompiler/SpirvBackend/include/SpirvBackend/SpirvEmitterState.hppcore/libs/prx/libSceAgcDriver/tests/ShaderMemory.cppcore/libs/prx/libSceAgcDriver/tests/ShaderMemory.cppcore/shader/recompiler/SpirvBackend/include/SpirvBackend/SpirvEmitterState.hppcore/libs/prx/libSceAgcDriver/Graphics/src/BdaResources.cppcore/libs/prx/libSceAgcDriver/tests/ShaderMemory.cppcore/shader/recompiler/SpirvBackend/include/SpirvBackend/SpirvEmitterState.hppcore/shader/recompiler/CMakeLists.txtcore/libs/prx/libSceAgcDriver/tests/ShaderMemory.cppcore/shader/recompiler/CMakeLists.txtcore/libs/prx/libSceAgcDriver/Graphics/src/BdaResources.cppcore/libs/prx/libSceAgcDriver/tests/ShaderMemory.cppcore/shader/recompiler/BdaAbi.hppcore/shader/recompiler/CMakeLists.txtcore/shader/recompiler/SpirvBackend/include/SpirvBackend/SpirvBda.hppcore/shader/recompiler/SpirvBackend/include/SpirvBackend/SpirvEmitterState.hppcore/shader/recompiler/SpirvBackend/src/SpirvBdaLookup.cppcore/shader/recompiler/SpirvBackend/src/SpirvModuleEmitter.cppcore/shader/recompiler/CMakeLists.txtcore/shader/recompiler/SpirvBackend/include/SpirvBackend/SpirvEmitterState.hppcore/libs/prx/libSceAgcDriver/tests/ShaderMemory.cppcore/libs/prx/libSceAgcDriver/tests/ShaderMemory.cppcore/libs/prx/libSceAgcDriver/tests/ShaderMemory.cppcore/libs/prx/libSceAgcDriver/tests/ShaderMemory.cppcore/libs/prx/libSceAgcDriver/Graphics/src/BdaResources.cppcore/shader/recompiler/CMakeLists.txtcore/shader/recompiler/SpirvBackend/include/SpirvBackend/SpirvEmitterState.hppcore/shader/recompiler/SpirvBackend/src/SpirvModuleEmitter.cppcore/shader/recompiler/SpirvBackend/src/SpirvModuleEmitter.cppImported from boykopovar/AnyPS5. Originally posted by CnRJay on 2026-10-14T16:56:11Z.
Review: COMMENTED
Resolving the range of a user-data base once per invocation is a good idea for these 4K passes. The exactness test (anchored against lookup-only compiles, faults included) is thorough. I'd like to see two costs measured before this becomes the only way such reads are generated:
1. Pipeline compile time and module size. Every anchored read now carries both paths: the direct load, plus the full lookup path kept as the fallback branch. Programs with many read sites therefore grow, and so does the driver compile, which shows up as hitches on first use rather than in steady-state FPS. A table of SPIR-V word counts and
vkCreateGraphicsPipelines/vkCreateComputePipelinestime for the eight PPSA01317 programs, with and without anchors, would show whether that is small. A warm-cache run won't capture it, so a cold-cache start of the main map with the shader disk cache disabled would be the place to look.2. Gains measured on
main. The 49–61 % offline and +8 % in-game figures come from a branch with other BDA lookup changes (a hint and a cache) thatmaindoesn't have. Those reduce the cost of the lookup path, so they change how much an anchor saves. Theaps5-summary.jsonllines ofmainand ofmain+ this PR from the same scene, with noAPS5_PROFILE_*switch, are what the Performance report form asks for, and you offered to capture them.Also: everything was measured on NVIDIA. RADV/ACO and the AMD Windows driver handle the extra branch and the coherent atomic loads differently, so one AMD run would be worth having, since the change applies to every title. With no switch, the anchors can't be turned off to compare in the field.
APS5_NO_BDA_ANCHORSwould let testers isolate a regression.Imported from boykopovar/AnyPS5. Originally posted by EagleBearerYS on 2026-10-15T00:35:50Z.
A note before the full Build runs on this. With
ANYPS5_ENABLE_SPIRV_TOOLS=ON, as CI configures it, programs without 64-bit integers fail SPIR-V validation on this head (3265cb6d7) merged onto currentmain(ab9a47684):EmitBdaAnchorsruns for every program (SpirvModuleEmitter.cpp:1167), and its first line declares the 64-bit type (TypeScalarU64,SpirvBdaRead.cpp:544) even when the program has no anchors.agc_shader_memoryandagc_formatted_buffer_oncefail that way.if (state.bdaAnchors.empty()) return;at the top ofEmitBdaAnchors, both pass, and so doesagc_shader_bindless.Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-17T00:41:50Z.
@EagleBearerYS's finding is the blocker: with SPIRV-Tools on, as CI configures it, programs without 64-bit integers fail validation on this head because
EmitBdaAnchorsdeclares the u64 type for every program. @gjonn, please declare it only when the program has an anchor. @CnRJay's two measurements (compile time and module size with both paths in every anchored read) are still needed before this becomes the only way such reads are generated.Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-18T03:22:57Z.
@gjonn this one is approved to merge and CI is green on
88642b874f, but it's still marked as a draft, so it can't be merged yet. Could you mark it ready for review when you get a chance? Please don't push new commits, or we'll need to re-run checks.Imported from boykopovar/AnyPS5. Originally posted by gjonn on 2026-10-18T13:10:44Z.
@EagleBearerYS thanks for catching that. Fixed in
88642b874:EmitBdaAnchorsnow returns beforeTypeScalarU64when the program has no anchors (if (state.bdaAnchors.empty()) return;at the top, as you suggested), so programs without anchors no longer declare a 64-bit integer type.Thanks for marking it approved, @oneandonlydean. It's out of draft now, and I won't push anything else to it.
AI-assisted: yes
Imported from boykopovar/AnyPS5. Originally posted by milescoviello on 2026-10-18T14:53:31Z.
A heads-up for NVIDIA: since this PR,
agc_driver_bda_devicehangs on an RTX 4070 Ti (driver 595.99.02, Linux). It fails 3 of 3 withvkWaitForFences graphics: Vulkan result 2, and the kernel logsXid 109 ... CTX SWITCH TIMEOUT.385ed71f2c, the commit before the merge, passes, andmainpasses on Intel ANV.The dispatch that hangs is in
RunBdaAnchorTests, but in the plain program, throughget_bda_pointerandprobe_bda_pointer. The new test inputs reach a case the existing lookups had: a subgroup whose invocations leave the lookup through three different returns. #7571 has the reduction and makes the lookups return once, keepingDontInline. With it, the test passes 3 of 3 on both GPUs.AI-assisted: yes (Claude Code)
Imported from boykopovar/AnyPS5. Originally posted by screamm on 2026-10-18T15:54:47Z.
@gjonn
Found by cross-validation on an RTX 4070:
agc_driver_bda_devicenow times out invkWaitForFences(VK_TIMEOUT) on main. Bisected tod422c760cfrom this PR; the parent385ed71f2passes 3/3. Details in #7584.Written with Claude Code assistance.