perf(shader): read adjacent BDA dword loads of one block through one span read #4931
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/4931"
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 poonasor on 2026-10-13T05:06:53Z.
What
A commercial title reaches its title screen on
mainplus the fixes in #4617 and #4563 (and others on my fork,dev-next). After START it stays on one frame while AMD's Windows driver compiles the main-menu pipeline: with the driver timed inLinkPipelineFromLibraries, that one link had not returned after 12 to 30 minutes in several runs, and in others the process grew past 70 GB of committed memory insideamdvlk64until I stopped it.The pixel shader of that pipeline is 526,006 SPIR-V words and calls
read_bda_span_stop136 times. The reads are scalar loads of consecutive dwords: ans_load_dwordx8becomes eightLoadAddressU32, each with its own span read (a page-table probe plus the byte fallback). AMD's compiler inlines the read helpers at every call site (DontInlineis not honoured), so the helper bodies are compiled 136 times.Loads in one block whose address operands are equal and whose offsets lie within 16 bytes of the first now share that first load's span read, with the used-dword mask of the group:
AdjacentBdaLoadscollects the followingLoadAddress*of the same opcode in the block. An instruction that writes memory (buffer, shared, address or image write or atomic,Barrier,StoreVectorWindow) ends the scan; register writes do not.GetAddressResourceandSelectU32, because every split load carries its own copies of them; anything else must be the same value.laneCount == 1) and 4-aligned offsets are grouped.EmitBdaDwordReadstakes the used-dword mask of the group.Effect on the shader above: 136 span reads become 34 and the module shrinks to 510,478 words. The same pair of stages compiled alone on the GPU (a small stand-alone
vkCreateGraphicsPipelinestool, one descriptor set layout per set): did not finish in 10 minutes and reached 13.9 GB before; 106 s and 2.8 GB after. In the title the link of that pipeline took 143 s.A fault in a grouped load is recorded with the first load's instruction address, and all loads of a group are read where the first one stood. Between them there is no memory write, so the values are the same; what can differ is which fault is recorded first when another read between them faults.
Tested
Windows 11, MinGW-w64 GCC 15.2, AMD RX 6700 XT (Adrenalin 25.10, amdvlk64).
agc_shader_memory, "BDA read functions": fivebuffer_load_dwordof one V# at offsets 4, 8, 12, 16 and 20 must call the span read twice. Onmainit calls it five times and the test fails with "five adjacent dwords of one address do not share two span reads"; with the change it passes. The existing five-site assertions of that test now use offsets 32 bytes apart so that they keep measuring one site each.ctest -R "^agc_|^shader": 450 of 452 pass.agc_driver_lds_mixed_width_atomicsandagc_driver_pixel_interlock(#2009) fail on this GPU and fail the same way withcore/shadertaken from the commit below this one.main; rebased onto it without conflicts and not rebuilt after the rebase.python tools/check_conventions.py --base origin/mainpasses.Not tested: NVIDIA, Intel, Linux, macOS; whether other drivers gain or lose compile time.
Checklist
main; no other open PR implements the same functionsNotImplemented_nid_no_patch); silent stubs are listed in TechnicalDebt.mdfiles or images added to the repository (attach them to this PR)🤖 Generated with Claude Code
Imported from boykopovar/AnyPS5. Originally posted by poonasor on 2026-10-13T06:48:30Z.
A correction to the numbers in the description, after re-running on current
main(7e181fcc3) tonight.The 106 s / 2.8 GB stand-alone compile and the 143 s in-title link were measured on my fork branch, which besides this change also carried a second change to the BDA read helpers: the byte fallback of
read_bda_span*andread_bda_dword_bytes*written as loops instead of four unrolled copies. I had not separated the two.On
main+ this PR alone, the title sat on the first main-menu frame for 14.5 minutes inside the driver's compile before I stopped it. With the loop change ported on top (onto the split in-place/fallback span read off1b2e605e) the same link takes 140 s ([gpu] pipeline link took 140101 ms).So this PR does what the description says to the module (136 span reads become 34, test included), but it is not sufficient by itself for that title on AMD; the loop change is the other half and I will open it as a separate PR. If you would rather review the two together, say so and I will stack them.
AI-assisted: yes.
Imported from boykopovar/AnyPS5. Originally posted by xXPBSXx on 2026-10-13T09:41:58Z.
Windows test (MinGW GCC 15.2, Release, Windows 10, RTX 4080 SUPER). Built on main
2a2a3dc65together with nine other open GPU PRs (their own commits applied, no merge commits; one further PR, #5024, conflicted with them and was left out), so only the tests that belong to this PR say something about it:tests/ShaderMemory.cpp(targetagc_shader_memory_tests), run asagc_shader_memory,agc_shader_bindless,agc_shader_srt_read_patching,agc_shader_frontend_pairandagc_shader_frontend_pair_off: 3 of 3 runs passed for each.Not tested: no game run. The batch was built once, not per PR; nothing failed, so no bisect was needed.
AI-assisted (Claude Code).
Imported from boykopovar/AnyPS5. Originally posted by kr4y4 on 2026-10-13T14:58:52Z.
First NVIDIA and Intel run. The BDA read tests pass on both GPUs, including NVIDIA 595.97, where the read shape from #4759 lost the device until #5022.
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). The head 45776333e merged cleanly into
main0f2683cb7, which includes #5022 and passes 1069 of 1069 on both GPUs.--repeat until-fail:3:agc_shader_memory,agc_driver_bda_span_reads,agc_driver_pixel_interlock,agc_driver_bda_device,agc_driver_scalar_wide_loads,agc_driver_flat_apertures_0to13, and the sixflat_loadsandglobal_loadstests. All 25 pass on both GPUs.ShaderMemory.cppincluded.main,agc_shader_memoryfails on both GPUs withBDA read functions: five adjacent dwords of one address do not share two span reads, and the other 24 pass.I didn't measure compile time or run time.
Written with AI assistance.
Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-14T11:02:53Z.
Checked the grouping against the diff: the active argument must be the same value, address operands are compared only through
GetAddressResource/SelectU32, any memory write orBarrierends the scan, and the emitter state is per module sobdaGroupedLoadscan't go stale. Merges cleanly with #4976.agc_shader_memorypasses on the PR merged ontomain, and fails with the four production files reverted ("five adjacent dwords of one address do not share two span reads").agc_recorder_kept_bytesandagc_driver_tessellationfail on this Linux host both with and without the change.Note for the record: the compile times in the description (106 s, 143 s) were measured together with #4976, as the later comment says; this PR alone does not unblock that title on AMD. Merging.
Imported from boykopovar/AnyPS5. Originally posted by gjonn on 2026-10-14T12:48:19Z.
A data point from PPSA01317, in case it helps review. It isn't a verdict on this PR alone.
I ported #4931, #3655 and #4976 together (#4976 folded into #3655, since they overlap almost entirely) onto a local branch. That branch isn't stock main: it has its own BDA lookup hint and inline-hit fast paths. I then measured the title's main map, which has heavy 3840x2160 passes made of address-based draws.
Effect: the map went from 17.07 to 14.15 FPS with the ports, about −17%. These were matched sessions with profiling off, the same navigation, a warm shader cache and the same build otherwise.
Shader-level change: in the hottest fragment shader, the lookup calls in the module dropped from 151 to 65, and the read sites per loop iteration from 16 to 8. Replays show the main cost moving to the looped page-table walk and the looped byte fallback, which replace the unrolled code.
GPU: RTX 5090 Laptop, NVIDIA driver 616.92, Windows. My guess is that NVIDIA's compiler handles the loops worse than the unrolled form in these large fragment shaders (128 registers, with spills). I haven't isolated which of the three PRs causes the drop.
I haven't tried other GPUs, or these PRs on stock main.
AI-assisted: yes