perf(recompiler): load the written-page slot before the CAS #1280

Merged
TheAggie merged 4 commits from upstream-pr/1280 into main 2026-10-09 23:18:06 +00:00
TheAggie commented 2026-10-07 21:42:07 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by TheAggie on 2026-10-07T21:42:07Z.


What

Depends on #1265. Until it merges, this PR also shows its three commits; only the last commit is new here.

note_bda_write runs a device-scope OpAtomicCompareExchange on every probe, also when the slot already holds the page. The written-page table lives in the BDA fault buffer, which is host-visible memory, so every BDA store pays an atomic across PCIe. Almost every store hits a page that an earlier store already noted.

  • Each probe now loads the slot with a relaxed OpAtomicLoad and runs the compare-exchange only when the slot does not hold the page.
  • The slots probed and their order, the pages recorded, and the overflow report are unchanged. If the load sees a stale 0 or another page, the compare-exchange runs exactly as before.

Why the CAS is the cost

DOOM homebrew PPSA99666 v1.1.1 (lowbit/ps5-doom, GPL-2.0) presents through a small compute kernel: 1080 workgroups of 64, each reads palette-indexed bytes and stores 1920 pixels through raw pointers, about 2.07 M BDA stores per frame. It waits 500 ms for the frame's fence and otherwise falls back to its CPU scaler.

GPU time per dispatch DOOM
main 174 ms (APS5_PROFILE_GPU) GPU path, 5.5 FPS
#1265 248 ms (APS5_SYNC_DISPATCH, packet time) frame 3 exceeds 500 ms, CPU fallback
#1265 + this 8.1 ms (APS5_PROFILE_GPU, ~590 dispatches per 10 s) GPU path, 60 FPS (display refresh)

Experiments on main before writing this:

  • Allocating the fault buffer DEVICE_LOCAL | HOST_VISIBLE | HOST_COHERENT (ReBAR) instead of system memory gave the same gain: about 170 ms to about 10 ms per frame.
  • Moving the BDA range table to device-local memory as well changed nothing measurable, so the table lookups are not the cost.
  • APS5_SPIRV_OPT=full changed nothing.

This change keeps both buffers where they are and needs no ReBAR.

Tested

Windows 11 Pro (26200), MinGW-w64 GCC 15.2.0 (WinLibs r7), Release, NVIDIA GeForce RTX 5070 Ti, Ryzen 7 7800X3D.

  • All 195 agc_* and shader_* tests pass on this branch.
  • Built with ANYPS5_ENABLE_SPIRV_TOOLS=ON, which validates every module before and after optimization: flat_store, global_store_addtid, global_atomics, global_atomics64, global_atomics_float, global_atomics_lanes, global_atomic_csub, flat_cache_bits, global_vcc_base, flat_loads, d16_memory and the BDA tests pass (13 tests).
  • On main, the same change before #1265 also passed all 396 registered tests.
  • Tests were run from the commands and environments in the generated CTestTestfile.cmake files, because antivirus blocks ctest.exe on this machine.
  • DOOM measured as in the table. The dumped request (APS5_DUMP_SHADERS, shader_202a81800.req, 304 code words) replays with agc_shader_replay; I can attach it.

Not run on AMD, Intel or Linux.

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: #1265
  • AI-assisted: yes

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4778835242 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1280). Originally posted by [TheAggie](https://github.com/TheAggie) on 2026-10-07T21:42:07Z. --- ### What Depends on #1265. Until it merges, this PR also shows its three commits; only the last commit is new here. `note_bda_write` runs a device-scope `OpAtomicCompareExchange` on every probe, also when the slot already holds the page. The written-page table lives in the BDA fault buffer, which is host-visible memory, so every BDA store pays an atomic across PCIe. Almost every store hits a page that an earlier store already noted. - Each probe now loads the slot with a relaxed `OpAtomicLoad` and runs the compare-exchange only when the slot does not hold the page. - The slots probed and their order, the pages recorded, and the overflow report are unchanged. If the load sees a stale 0 or another page, the compare-exchange runs exactly as before. ### Why the CAS is the cost DOOM homebrew PPSA99666 v1.1.1 ([lowbit/ps5-doom](https://github.com/lowbit/ps5-doom), GPL-2.0) presents through a small compute kernel: 1080 workgroups of 64, each reads palette-indexed bytes and stores 1920 pixels through raw pointers, about 2.07 M BDA stores per frame. It waits 500 ms for the frame's fence and otherwise falls back to its CPU scaler. | | GPU time per dispatch | DOOM | |---|---|---| | `main` | 174 ms (`APS5_PROFILE_GPU`) | GPU path, 5.5 FPS | | #1265 | 248 ms (`APS5_SYNC_DISPATCH`, packet time) | frame 3 exceeds 500 ms, CPU fallback | | #1265 + this | 8.1 ms (`APS5_PROFILE_GPU`, ~590 dispatches per 10 s) | GPU path, 60 FPS (display refresh) | Experiments on `main` before writing this: - Allocating the fault buffer `DEVICE_LOCAL | HOST_VISIBLE | HOST_COHERENT` (ReBAR) instead of system memory gave the same gain: about 170 ms to about 10 ms per frame. - Moving the BDA range table to device-local memory as well changed nothing measurable, so the table lookups are not the cost. - `APS5_SPIRV_OPT=full` changed nothing. This change keeps both buffers where they are and needs no ReBAR. ### Tested Windows 11 Pro (26200), MinGW-w64 GCC 15.2.0 (WinLibs r7), Release, NVIDIA GeForce RTX 5070 Ti, Ryzen 7 7800X3D. - All 195 `agc_*` and `shader_*` tests pass on this branch. - Built with `ANYPS5_ENABLE_SPIRV_TOOLS=ON`, which validates every module before and after optimization: `flat_store`, `global_store_addtid`, `global_atomics`, `global_atomics64`, `global_atomics_float`, `global_atomics_lanes`, `global_atomic_csub`, `flat_cache_bits`, `global_vcc_base`, `flat_loads`, `d16_memory` and the BDA tests pass (13 tests). - On `main`, the same change before #1265 also passed all 396 registered tests. - Tests were run from the commands and environments in the generated `CTestTestfile.cmake` files, because antivirus blocks `ctest.exe` on this machine. - DOOM measured as in the table. The dumped request (`APS5_DUMP_SHADERS`, `shader_202a81800.req`, 304 code words) replays with `agc_shader_replay`; I can attach it. Not run on AMD, Intel or Linux. ### 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: #1265 - [x] AI-assisted: yes 🤖 Generated with [Claude Code](https://claude.com/claude-code)
johnmaia commented 2026-10-08 18:59:03 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by johnmaia on 2026-10-08T18:59:03Z.


Measured on AMD and Linux, which the description lists as not run: Ubuntu 24.04.5 (kernel 6.8), GCC 13.3, Release, AMD Radeon RX 7600 (RADV NAVI33, Mesa 25.2.8, resizable BAR on). This PR's head is 1dca7b4f (including #1265), merged onto main 6b56175a.

DOOM homebrew PPSA99666 v1.1.1, built from source, relinked with --exclude-sce-module libc.prx. A test plan of frames 1500, a warm-up run first and then a measured run. GPU time is from APS5_PROFILE_GPU, in two 10 s windows of about 600 presents each:

GPU time per 10 s per present DOOM
main 876 / 865 ms 1.45 ms GPU path, 60 FPS
#1265 863 / 858 ms 1.43 ms GPU path, 60 FPS
#1265 + this 792 / 785 ms 1.31 ms GPU path, 60 FPS
  • On RADV the compare-exchange that this PR avoids is cheap, so main is already about 120× faster per present than the 174 ms measured on the RTX 5070 Ti. The fault buffer is in the same memory as on Windows: Buffer picks host-visible, coherent, cached memory, which is system memory, not the ReBAR heap. The PR still takes 9% off the presenter's GPU time here, and nothing regressed.

  • Code size with APS5_PIPELINE_STATS=1 and cold caches (largest pipeline). As expected, the extra load per probe adds a little:

    • flat_store: 126,612 → 141,368 B.
    • global_vcc_base: 195,996 → 207,616 B.
    • global_store_addtid: 82,636 → 93,356 B.
    • global_atomics: 51,016 → 56,840 B.

    The full table on #1265 is here.

  • Full ctest: 479/479 pass on this branch, the same as on main, with the GPU tests running on the RX 7600.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6066954702 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1280#issuecomment-6066954702). Originally posted by [johnmaia](https://github.com/johnmaia) on 2026-10-08T18:59:03Z. --- Measured on AMD and Linux, which the description lists as not run: Ubuntu 24.04.5 (kernel 6.8), GCC 13.3, Release, AMD Radeon RX 7600 (RADV NAVI33, Mesa 25.2.8, resizable BAR on). This PR's head is `1dca7b4f` (including #1265), merged onto main `6b56175a`. **DOOM homebrew PPSA99666 v1.1.1**, built from source, relinked with `--exclude-sce-module libc.prx`. A test plan of `frames 1500`, a warm-up run first and then a measured run. GPU time is from `APS5_PROFILE_GPU`, in two 10 s windows of about 600 presents each: | | GPU time per 10 s | per present | DOOM | |---|---|---|---| | `main` | 876 / 865 ms | 1.45 ms | GPU path, 60 FPS | | #1265 | 863 / 858 ms | 1.43 ms | GPU path, 60 FPS | | #1265 + this | 792 / 785 ms | 1.31 ms | GPU path, 60 FPS | - On RADV the compare-exchange that this PR avoids is cheap, so `main` is already about 120× faster per present than the 174 ms measured on the RTX 5070 Ti. The fault buffer is in the same memory as on Windows: `Buffer` picks host-visible, coherent, cached memory, which is system memory, not the ReBAR heap. The PR still takes 9% off the presenter's GPU time here, and nothing regressed. - Code size with `APS5_PIPELINE_STATS=1` and cold caches (largest pipeline). As expected, the extra load per probe adds a little: - `flat_store`: 126,612 → 141,368 B. - `global_vcc_base`: 195,996 → 207,616 B. - `global_store_addtid`: 82,636 → 93,356 B. - `global_atomics`: 51,016 → 56,840 B. The full table on #1265 is [here](https://github.com/boykopovar/AnyPS5/pull/1265#issuecomment-6066954325). - Full ctest: 479/479 pass on this branch, the same as on `main`, with the GPU tests running on the RX 7600.
fisherjoey commented 2026-10-09 04:22:31 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by fisherjoey on 2026-10-09T04:22:31Z.


Linux results, since this was tested on Windows only. Everything passes here.

Nobara 44 (Linux 7.2), GCC 16.2.1, Release, BUILD_TESTING=ON. This PR merged onto main 29e533ac.

main 29e533ac main + this PR
RTX 5070 Ti, driver 595.91.07, ctest -R agc_driver -j1 183 / 183 183 / 183
RADV, Ryzen 5 7600X iGPU (gfx1036), Mesa 26.2.1, same 183 / 183 183 / 183
CPU tests (-E agc_driver) 279 / 280 279 / 280

The CPU failure is guest_dynamic_loader, which fails the same way on main.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6074209918 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1280#issuecomment-6074209918). Originally posted by [fisherjoey](https://github.com/fisherjoey) on 2026-10-09T04:22:31Z. --- Linux results, since this was tested on Windows only. Everything passes here. Nobara 44 (Linux 7.2), GCC 16.2.1, Release, `BUILD_TESTING=ON`. This PR merged onto `main` 29e533ac. | | `main` 29e533ac | `main` + this PR | |---|---|---| | RTX 5070 Ti, driver 595.91.07, `ctest -R agc_driver -j1` | 183 / 183 | 183 / 183 | | RADV, Ryzen 5 7600X iGPU (gfx1036), Mesa 26.2.1, same | 183 / 183 | 183 / 183 | | CPU tests (`-E agc_driver`) | 279 / 280 | 279 / 280 | The CPU failure is `guest_dynamic_loader`, which fails the same way on `main`.
adriadam10 commented 2026-10-09 23:18:03 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-09T23:18:03Z.


Checked the diff against the original probe loop: same slots in the same order, same done and overflow behaviour; a stale 0 or another page in the load just falls through to the compare-exchange as before. Built on current main (#1265 is merged, so only this commit is new): ctest 551/551 green on Linux. Also confirmed by @johnmaia (RX 7600, RADV) and @fisherjoey (RTX 5070 Ti and RADV). Merging once CI is green.

Note: no existing test fails if the page is never recorded through this path (I forced the "slot already holds the page" check to true and agc_driver_flat_store, global_atomics, global_store_addtid still passed). Not a regression of this PR, but a test pinning the written-page table would be welcome in a follow-up.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6090898402 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1280#issuecomment-6090898402). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-09T23:18:03Z. --- Checked the diff against the original probe loop: same slots in the same order, same `done` and overflow behaviour; a stale 0 or another page in the load just falls through to the compare-exchange as before. Built on current `main` (#1265 is merged, so only this commit is new): ctest 551/551 green on Linux. Also confirmed by @johnmaia (RX 7600, RADV) and @fisherjoey (RTX 5070 Ti and RADV). Merging once CI is green. Note: no existing test fails if the page is never recorded through this path (I forced the "slot already holds the page" check to true and `agc_driver_flat_store`, `global_atomics`, `global_store_addtid` still passed). Not a regression of this PR, but a test pinning the written-page table would be welcome in a follow-up.
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!1280
No description provided.