perf(shader): read adjacent BDA dword loads of one block through one span read #4931

Merged
poonasor merged 1 commit from upstream-pr/4931 into main 2026-10-14 11:58:39 +00:00
poonasor commented 2026-10-13 05:06:53 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by poonasor on 2026-10-13T05:06:53Z.


What

A commercial title reaches its title screen on main plus 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 in LinkPipelineFromLibraries, 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 inside amdvlk64 until I stopped it.

The pixel shader of that pipeline is 526,006 SPIR-V words and calls read_bda_span_stop 136 times. The reads are scalar loads of consecutive dwords: an s_load_dwordx8 becomes eight LoadAddressU32, 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 (DontInline is 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:

  • AdjacentBdaLoads collects the following LoadAddress* 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.
  • Address operands are compared by content through GetAddressResource and SelectU32, because every split load carries its own copies of them; anything else must be the same value.
  • Only one-lane emission (laneCount == 1) and 4-aligned offsets are grouped.
  • EmitBdaDwordReads takes 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 vkCreateGraphicsPipelines tool, 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": five buffer_load_dword of one V# at offsets 4, 8, 12, 16 and 20 must call the span read twice. On main it 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_atomics and agc_driver_pixel_interlock (#2009) fail on this GPU and fail the same way with core/shader taken from the commit below this one.
  • Built and tested 86 commits behind the current main; rebased onto it without conflicts and not rebuilt after the rebase.
  • The title as described above.
  • python tools/check_conventions.py --base origin/main passes.

Not tested: NVIDIA, Intel, Linux, macOS; whether other drivers gain or lose compile time.

Checklist

  • Based on current main; no other open PR implements the same functions
  • One topic per PR; follow-ups go in a new PR
  • No comments in code except technical debt
  • Unimplemented paths throw (NotImplemented_nid_no_patch); silent stubs are listed in TechnicalDebt
  • No notes, investigation .md files or images added to the repository (attach them to this PR)
  • New third-party code is a submodule built from source
  • Generic behaviour, not specific to one title
  • Depends on: none
  • AI-assisted: yes

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4838644353 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4931). Originally posted by [poonasor](https://github.com/poonasor) on 2026-10-13T05:06:53Z. --- ### What A commercial title reaches its title screen on `main` plus the fixes in #4617 and #4563 (and others on my fork, [`dev-next`](https://github.com/poonasor/AnyPS5/tree/dev-next)). After START it stays on one frame while AMD's Windows driver compiles the main-menu pipeline: with the driver timed in `LinkPipelineFromLibraries`, 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 inside `amdvlk64` until I stopped it. The pixel shader of that pipeline is 526,006 SPIR-V words and calls `read_bda_span_stop` 136 times. The reads are scalar loads of consecutive dwords: an `s_load_dwordx8` becomes eight `LoadAddressU32`, 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 (`DontInline` is 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: - `AdjacentBdaLoads` collects the following `LoadAddress*` 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. - Address operands are compared by content through `GetAddressResource` and `SelectU32`, because every split load carries its own copies of them; anything else must be the same value. - Only one-lane emission (`laneCount == 1`) and 4-aligned offsets are grouped. - `EmitBdaDwordReads` takes 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 `vkCreateGraphicsPipelines` tool, 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": five `buffer_load_dword` of one V# at offsets 4, 8, 12, 16 and 20 must call the span read twice. On `main` it 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_atomics` and `agc_driver_pixel_interlock` (#2009) fail on this GPU and fail the same way with `core/shader` taken from the commit below this one. - Built and tested 86 commits behind the current `main`; rebased onto it without conflicts and not rebuilt after the rebase. - The title as described above. - `python tools/check_conventions.py --base origin/main` passes. Not tested: NVIDIA, Intel, Linux, macOS; whether other drivers gain or lose compile time. ### Checklist - [x] Based on current `main`; no other open PR implements the same functions - [x] One topic per PR; follow-ups go in a new PR - [x] No comments in code except [technical debt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/CONVENTIONS.md) - [x] Unimplemented paths throw (`NotImplemented_nid_no_patch`); silent stubs are listed in [TechnicalDebt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/TechnicalDebt.md#silent-stubs) - [x] No notes, investigation `.md` files or images added to the repository (attach them to this PR) - [x] New third-party code is a submodule built from source - [x] Generic behaviour, not specific to one title - [x] Depends on: none - [x] AI-assisted: yes 🤖 Generated with [Claude Code](https://claude.com/claude-code)
poonasor commented 2026-10-13 06:48:30 +00:00 (Migrated from github.com)

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* and read_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 of f1b2e605e) 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.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6139974263 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4931#issuecomment-6139974263). Originally posted by [poonasor](https://github.com/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*` and `read_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 of f1b2e605e) 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.
xXPBSXx commented 2026-10-13 09:41:58 +00:00 (Migrated from github.com)

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 2a2a3dc65 together 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 (target agc_shader_memory_tests), run as agc_shader_memory, agc_shader_bindless, agc_shader_srt_read_patching, agc_shader_frontend_pair and agc_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).

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6142757030 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4931#issuecomment-6142757030). Originally posted by [xXPBSXx](https://github.com/xXPBSXx) on 2026-10-13T09:41:58Z. --- Windows test (MinGW GCC 15.2, Release, Windows 10, RTX 4080 SUPER). Built on main `2a2a3dc65` together 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` (target `agc_shader_memory_tests`), run as `agc_shader_memory`, `agc_shader_bindless`, `agc_shader_srt_read_patching`, `agc_shader_frontend_pair` and `agc_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).
kr4y4 commented 2026-10-13 14:58:52 +00:00 (Migrated from github.com)

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 main 0f2683cb7, which includes #5022 and passes 1069 of 1069 on both GPUs.

  • 25 tests, --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_0 to 13, and the six flat_loads and global_loads tests. All 25 pass on both GPUs.
  • The same 25 pass with #4976 merged as well. The two merge with each other without conflicts, ShaderMemory.cpp included.
  • Full suite: 1069 of 1069 on both GPUs.
  • With the four production files from main, agc_shader_memory fails on both GPUs with BDA 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.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6148480655 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4931#issuecomment-6148480655). Originally posted by [kr4y4](https://github.com/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 `main` 0f2683cb7, which includes #5022 and passes 1069 of 1069 on both GPUs. - 25 tests, `--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_0` to `13`, and the six `flat_loads` and `global_loads` tests. All 25 pass on both GPUs. - The same 25 pass with #4976 merged as well. The two merge with each other without conflicts, `ShaderMemory.cpp` included. - Full suite: 1069 of 1069 on both GPUs. - With the four production files from `main`, `agc_shader_memory` fails on both GPUs with `BDA 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.
adriadam10 commented 2026-10-14 11:02:53 +00:00 (Migrated from github.com)

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 or Barrier ends the scan, and the emitter state is per module so bdaGroupedLoads can't go stale. Merges cleanly with #4976.

  • agc_shader_memory passes on the PR merged onto main, and fails with the four production files reverted ("five adjacent dwords of one address do not share two span reads").
  • agc_recorder_kept_bytes and agc_driver_tessellation fail on this Linux host both with and without the change.
  • Windows runs from @kr4y4 (NVIDIA + Intel) and @xXPBSXx pass the BDA read tests.

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.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6166874939 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4931#issuecomment-6166874939). Originally posted by [adriadam10](https://github.com/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 or `Barrier` ends the scan, and the emitter state is per module so `bdaGroupedLoads` can't go stale. Merges cleanly with #4976. - `agc_shader_memory` passes on the PR merged onto `main`, and fails with the four production files reverted ("five adjacent dwords of one address do not share two span reads"). - `agc_recorder_kept_bytes` and `agc_driver_tessellation` fail on this Linux host both with and without the change. - Windows runs from @kr4y4 (NVIDIA + Intel) and @xXPBSXx pass the BDA read tests. 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.
gjonn commented 2026-10-14 12:48:19 +00:00 (Migrated from github.com)

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

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6168703099 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4931#issuecomment-6168703099). Originally posted by [gjonn](https://github.com/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
Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
boykopovar/AnyPS5!4931
No description provided.