perf(agc-driver): join the imports of adjacent large ranges when a buffer is bound across them #4515

Merged
AlxcBTW merged 1 commit from upstream-pr/4515 into main 2026-10-13 16:55:28 +00:00
AlxcBTW commented 2026-10-12 14:09:21 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by AlxcBTW on 2026-10-12T14:09:21Z.


Sword of the Sea (PPSA25344) main menu: a 368 MiB buffer bound across two registered ranges is imported once instead of being copied whole on every use. In the default env the menu goes from 289 to 134 ms per frame (3.5 to 7.5 FPS).

What

#3425 imports adjacent registered ranges as one only where they fill a whole aligned 8 MiB window. A range larger than the window always crosses a window edge, so it is imported alone. When a buffer is bound across two such ranges, no import covers it, and the whole region is copied on every use. Sword of the Sea binds a 368 MiB buffer across a 320.75 MiB range and the 47.3 MiB range right after it. Both are registered early and never grow, but they stay two imports. On main the buffer is copied on almost every use: 549 times (202 GB) in a 180 s menu run.

With this change, UploadFinish looks at every pending region that no import covers. If the region crosses adjacent registered ranges and each of them is larger than the window, the run of those ranges is imported once and their own imports are retired.

  • When. Before any region of the build confirms an import pointer. The retire moves the epoch, so the pointers taken by UploadPrepare or by the cached address space are resolved again instead of trusted.
  • Which ranges. Only ranges larger than the window. A heap that grows 64 KiB at a time never takes part, so the re-import churn #3425 fixed for #3158 cannot come back. A range that large is registered whole, so the joined import is never extended as a heap grows.
  • How long it lasts. refreshImports keeps the joined import while exactly its ranges stand (HostImport::joined; sameRange checks the joined run). A lookup on a stale registry no longer splits it.
  • Growth. A later join that reaches an existing joined import takes it in. A buffer across ranges 2/3 after one across 1/2 gives one import of 1-3; two joins that share a range never retire each other in turn.
  • Failure. A joined run that cannot be imported (alignment, an unaliasable view on Windows, a driver refusal) is remembered in failedJoins (an ImportFailure like failed, dropped once the joined run changes), so its ranges still import alone and the buffer is copied as before.
  • Unit shadows. A unit inside the joined import still counts as registered when the joined import retires (joinedCovers).
  • HostImportFor returns a joined import for a range that no single run covers, once it has reconciled.

The TechnicalDebt entry for host imports says which ranges are joined now, and that a heap grown in pieces larger than 8 MiB would re-import its joined run once for each piece a bound buffer crosses into.

Tested

Windows 11 (26200), WinLibs GCC 15.2.0 r7, Release, on #4385 and #4367 (this branch is on top of both, on main ffa198407).

  • importJoinTests (new, in agc_driver_graphics). On main with only this test it fails with a buffer bound across two adjacent ranges larger than the import window was not imported as one. It sets a 128 KiB window and checks:

    • two adjacent 192 KiB ranges, each imported alone;
    • an address-based build with a view across them gets one 384 KiB import and binds the view in place;
    • a later descriptor-only bind uses that import with nothing copied;
    • two 64 KiB heap pieces across a window edge are never joined;
    • a registry change elsewhere keeps the joined import;
    • a third range registered next to them joins only when a view crosses into it, and a later bind across the first two keeps the grown import;
    • removing the middle range retires the join, and the first range is imported alone again.
  • Non-GPU suite (Lenovo node, ctest -E "^agc_driver|^video_out|^guest_video"): 600 of 601 pass, including guest_memory and agc_host_import_retire; log_macros fails as on main on that machine.

  • agc_driver_graphics: passes on an RTX 5070 Laptop (616.92), with importJoinTests.

  • Sword of the Sea menu, 180 s rows, A B A B on our lab branch (main a11c8f7f7 plus local patches for this title), one build with the join switched off (A) and on (B). These rows were measured with this join on a lab build whose cross-section alias was our own earlier alias change rather than #4367. The mechanism is the same, and #4367 replaces it. A rerun on #4367 follows when the lab has a slot.

    Upstream default env (APS5_RESOURCE_CACHE_ENTRIES=8192, APS5_PROFILE_DRAW=1):

    A1 / A2 (join off) B1 / B2 (join on)
    Menu FPS 3.45-3.51 / 3.37-3.50 7.26-7.57 / 7.38-7.73
    Frame / GPU / worker ms 289 / 92 / 287, 301 / 92 / 298 134 / 81 / 132, 139 / 81 / 136
    Copied on the CPU outside imports 421,677 / 386,006 MiB 18,057 MiB
    368 MiB and 96 MiB copies 549 / 504 × 368 MiB, 2196 / 2015 × 96 MiB none
    Buffer pool large evictions 3328 / 2462 0
    vkQueueSubmit mean 522 / 525 µs 38 / 41 µs

    B's trace has host import of 0x30b1880000+0x17010000 ok (...), joined at bind; its t170 capture shows the normal menu.

    With host imports kept write-watched and unit shadows off (APS5_WRITE_WATCH_IMPORTS=watch, a local switch of ours that is not on main, plus APS5_NO_UNIT_SHADOW=1), the same A/B goes from 437 / 419 to 202 / 208 ms per frame, CPU copies from 269,671 to 10,381 MiB, and buffer pool evictions from 1762 to 0.

  • Conventions: tools/check_conventions.py --base origin/main passes.

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: #4385, #4367
  • AI-assisted: yes (Claude Code)

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4830609262 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4515). Originally posted by [AlxcBTW](https://github.com/AlxcBTW) on 2026-10-12T14:09:21Z. --- Sword of the Sea (PPSA25344) main menu: a 368 MiB buffer bound across two registered ranges is imported once instead of being copied whole on every use. In the default env the menu goes from 289 to 134 ms per frame (3.5 to 7.5 FPS). ### What #3425 imports adjacent registered ranges as one only where they fill a whole aligned 8 MiB window. A range larger than the window always crosses a window edge, so it is imported alone. When a buffer is bound across two such ranges, no import covers it, and the whole region is copied on every use. Sword of the Sea binds a 368 MiB buffer across a 320.75 MiB range and the 47.3 MiB range right after it. Both are registered early and never grow, but they stay two imports. On `main` the buffer is copied on almost every use: 549 times (202 GB) in a 180 s menu run. With this change, `UploadFinish` looks at every pending region that no import covers. If the region crosses adjacent registered ranges and each of them is larger than the window, the run of those ranges is imported once and their own imports are retired. - **When.** Before any region of the build confirms an import pointer. The retire moves the epoch, so the pointers taken by `UploadPrepare` or by the cached address space are resolved again instead of trusted. - **Which ranges.** Only ranges larger than the window. A heap that grows 64 KiB at a time never takes part, so the re-import churn #3425 fixed for #3158 cannot come back. A range that large is registered whole, so the joined import is never extended as a heap grows. - **How long it lasts.** `refreshImports` keeps the joined import while exactly its ranges stand (`HostImport::joined`; `sameRange` checks the joined run). A lookup on a stale registry no longer splits it. - **Growth.** A later join that reaches an existing joined import takes it in. A buffer across ranges 2/3 after one across 1/2 gives one import of 1-3; two joins that share a range never retire each other in turn. - **Failure.** A joined run that cannot be imported (alignment, an unaliasable view on Windows, a driver refusal) is remembered in `failedJoins` (an `ImportFailure` like `failed`, dropped once the joined run changes), so its ranges still import alone and the buffer is copied as before. - **Unit shadows.** A unit inside the joined import still counts as registered when the joined import retires (`joinedCovers`). - `HostImportFor` returns a joined import for a range that no single run covers, once it has reconciled. The TechnicalDebt entry for host imports says which ranges are joined now, and that a heap grown in pieces larger than 8 MiB would re-import its joined run once for each piece a bound buffer crosses into. ### Tested Windows 11 (26200), WinLibs GCC 15.2.0 r7, Release, on #4385 and #4367 (this branch is on top of both, on `main` ffa198407). - **`importJoinTests` (new, in `agc_driver_graphics`).** On `main` with only this test it fails with `a buffer bound across two adjacent ranges larger than the import window was not imported as one`. It sets a 128 KiB window and checks: - two adjacent 192 KiB ranges, each imported alone; - an address-based build with a view across them gets one 384 KiB import and binds the view in place; - a later descriptor-only bind uses that import with nothing copied; - two 64 KiB heap pieces across a window edge are never joined; - a registry change elsewhere keeps the joined import; - a third range registered next to them joins only when a view crosses into it, and a later bind across the first two keeps the grown import; - removing the middle range retires the join, and the first range is imported alone again. - **Non-GPU suite (Lenovo node, `ctest -E "^agc_driver|^video_out|^guest_video"`):** 600 of 601 pass, including `guest_memory` and `agc_host_import_retire`; `log_macros` fails as on `main` on that machine. - **`agc_driver_graphics`:** passes on an RTX 5070 Laptop (616.92), with `importJoinTests`. - **Sword of the Sea menu, 180 s rows, A B A B** on our lab branch (`main` a11c8f7f7 plus local patches for this title), one build with the join switched off (A) and on (B). These rows were measured with this join on a lab build whose cross-section alias was our own earlier alias change rather than #4367. The mechanism is the same, and #4367 replaces it. A rerun on #4367 follows when the lab has a slot. Upstream default env (`APS5_RESOURCE_CACHE_ENTRIES=8192`, `APS5_PROFILE_DRAW=1`): | | A1 / A2 (join off) | B1 / B2 (join on) | |---|---|---| | Menu FPS | 3.45-3.51 / 3.37-3.50 | 7.26-7.57 / 7.38-7.73 | | Frame / GPU / worker ms | 289 / 92 / 287, 301 / 92 / 298 | 134 / 81 / 132, 139 / 81 / 136 | | Copied on the CPU outside imports | 421,677 / 386,006 MiB | 18,057 MiB | | 368 MiB and 96 MiB copies | 549 / 504 × 368 MiB, 2196 / 2015 × 96 MiB | none | | Buffer pool large evictions | 3328 / 2462 | 0 | | `vkQueueSubmit` mean | 522 / 525 µs | 38 / 41 µs | B's trace has `host import of 0x30b1880000+0x17010000 ok (...), joined at bind`; its t170 capture shows the normal menu. With host imports kept write-watched and unit shadows off (`APS5_WRITE_WATCH_IMPORTS=watch`, a local switch of ours that is not on `main`, plus `APS5_NO_UNIT_SHADOW=1`), the same A/B goes from 437 / 419 to 202 / 208 ms per frame, CPU copies from 269,671 to 10,381 MiB, and buffer pool evictions from 1762 to 0. - **Conventions:** `tools/check_conventions.py --base origin/main` passes. ### Checklist <!-- Rules behind each item: https://github.com/boykopovar/AnyPS5/blob/main/CONTRIBUTING.md --> - [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: #4385, #4367 - [x] AI-assisted: yes (Claude Code) 🤖 Generated with [Claude Code](https://claude.com/claude-code)
blawesom commented 2026-10-12 14:53:43 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by blawesom on 2026-10-12T14:53:43Z.


Pre-review found a deterministic test failure on Linux/RADV — same as flagged on #4517, which contains this PR's commits plus a generalization:

  • agc_host_import_retire fails 3/3 runs with AGC graphics: missing Vulkan function: vkCmdCopyBuffer (core 1.0 function, present on this device) — the new code resolves it through context.Resolved(&DeviceFunctions::cmdCopyBuffer, ...) on a route where the test context has neither the table entry nor a deviceProc fallback.
  • Everything else passes (949/950).

Since #4517 supersedes this PR's scope ("any size" vs "large ranges"), fixing it there and marking this one Depends on: #4517 (or closing it) may be cleaner than fixing both.

Reproduce: ctest -R agc_host_import_retire --output-on-failure

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6126732375 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4515#issuecomment-6126732375). Originally posted by [blawesom](https://github.com/blawesom) on 2026-10-12T14:53:43Z. --- **Pre-review found a deterministic test failure on Linux/RADV** — same as flagged on #4517, which contains this PR's commits plus a generalization: - `agc_host_import_retire` fails 3/3 runs with `AGC graphics: missing Vulkan function: vkCmdCopyBuffer` (core 1.0 function, present on this device) — the new code resolves it through `context.Resolved(&DeviceFunctions::cmdCopyBuffer, ...)` on a route where the test context has neither the table entry nor a `deviceProc` fallback. - Everything else passes (949/950). Since #4517 supersedes this PR's scope ("any size" vs "large ranges"), fixing it there and marking this one **Depends on: #4517** (or closing it) may be cleaner than fixing both. Reproduce: `ctest -R agc_host_import_retire --output-on-failure`
AlxcBTW commented 2026-10-12 15:47:42 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by AlxcBTW on 2026-10-12T15:47:42Z.


@blawesom thanks for running it on RADV. The failing call wasn't the retire path: on Linux with uffd write watching, the first import runs the write-protection probe (ProbeImportWriteProtection), which records a vkCmdCopyBuffer through the test's mock device, and that mock has none. The test now keeps its imports out of the probe on non-Windows (APS5_WRITE_WATCH_IMPORTS=unwatch, as on Windows), in #4385's 63ddeb09f, merged into this branch.

Checked on a fork runner (Ubuntu 24.04, kernel 6.17, lavapipe): with the previous test file it fails with exactly missing Vulkan function: vkCmdCopyBuffer; with the fix agc_host_import_retire passes 3/3. Windows non-GPU suite 600/601 (log_macros as on main), and the test passes on NVIDIA.

On the dependency: #4517's body lists Depends on: #4515; #4515 lists #4385 and #4367.

AI-assisted: yes (Claude Code)

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6127677045 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4515#issuecomment-6127677045). Originally posted by [AlxcBTW](https://github.com/AlxcBTW) on 2026-10-12T15:47:42Z. --- @blawesom thanks for running it on RADV. The failing call wasn't the retire path: on Linux with uffd write watching, the first import runs the write-protection probe (`ProbeImportWriteProtection`), which records a `vkCmdCopyBuffer` through the test's mock device, and that mock has none. The test now keeps its imports out of the probe on non-Windows (`APS5_WRITE_WATCH_IMPORTS=unwatch`, as on Windows), in #4385's 63ddeb09f, merged into this branch. Checked on a fork runner (Ubuntu 24.04, kernel 6.17, lavapipe): with the previous test file it fails with exactly `missing Vulkan function: vkCmdCopyBuffer`; with the fix `agc_host_import_retire` passes 3/3. Windows non-GPU suite 600/601 (`log_macros` as on `main`), and the test passes on NVIDIA. On the dependency: #4517's body lists `Depends on: #4515`; #4515 lists #4385 and #4367. AI-assisted: yes (Claude Code)
oneandonlydean commented 2026-10-13 10:32:04 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-13T10:32:04Z.


Please rebase onto main as this PR's own commit, without the merge commits: the branch has three, still carries #4367's old commit 87af6e09d (on main now as 2d1cc82bc), and conflicts in docs/dev/debt/libSceAgcDriver.md.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6143599890 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4515#issuecomment-6143599890). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-10-13T10:32:04Z. --- Please rebase onto main as this PR's own commit, without the merge commits: the branch has three, still carries #4367's old commit 87af6e09d (on main now as 2d1cc82bc), and conflicts in `docs/dev/debt/libSceAgcDriver.md`.
AlxcBTW commented 2026-10-13 11:46:32 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by AlxcBTW on 2026-10-13T11:46:32Z.


Rebased onto current main as the single PR commit, without merge commits. Dropped the old #4367 commit, which is on main now. The conflict in docs/dev/debt/libSceAgcDriver.md is resolved by keeping main's Windows alias sentence and re-applying our join sentences. Checked with check_conventions and a full Linux and Windows run on a fork branch before the push.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6144826108 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4515#issuecomment-6144826108). Originally posted by [AlxcBTW](https://github.com/AlxcBTW) on 2026-10-13T11:46:32Z. --- Rebased onto current main as the single PR commit, without merge commits. Dropped the old #4367 commit, which is on main now. The conflict in docs/dev/debt/libSceAgcDriver.md is resolved by keeping main's Windows alias sentence and re-applying our join sentences. Checked with check_conventions and a full Linux and Windows run on a fork branch before the push.
github-actions[bot] commented 2026-10-13 16:34:04 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-13T16:34:04Z.


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.

Pull request Conflicting files (lines on main) Same exports Same new files
#290 core/libs/prx/libSceAgcDriver/Graphics/src/GuestBufferMemory.cpp L655-656, L662-664, L666-667, L669-680, L809, L2015-2017
core/libs/prx/libSceAgcDriver/tests/BdaResources.cpp L245, L1242
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6150330994 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4515#issuecomment-6150330994). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-10-13T16:34:04Z. --- <!-- pr-overlap --> 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**. | Pull request | Conflicting files (lines on `main`) | Same exports | Same new files | |---|---|---|---| | #290 | `core/libs/prx/libSceAgcDriver/Graphics/src/GuestBufferMemory.cpp` [L655-656](https://github.com/boykopovar/AnyPS5/blob/9baec8793ff4e16f3ae28aa771bbfe478754e5b6/core/libs/prx/libSceAgcDriver/Graphics/src/GuestBufferMemory.cpp#L655-L656), [L662-664](https://github.com/boykopovar/AnyPS5/blob/9baec8793ff4e16f3ae28aa771bbfe478754e5b6/core/libs/prx/libSceAgcDriver/Graphics/src/GuestBufferMemory.cpp#L662-L664), [L666-667](https://github.com/boykopovar/AnyPS5/blob/9baec8793ff4e16f3ae28aa771bbfe478754e5b6/core/libs/prx/libSceAgcDriver/Graphics/src/GuestBufferMemory.cpp#L666-L667), [L669-680](https://github.com/boykopovar/AnyPS5/blob/9baec8793ff4e16f3ae28aa771bbfe478754e5b6/core/libs/prx/libSceAgcDriver/Graphics/src/GuestBufferMemory.cpp#L669-L680), [L809](https://github.com/boykopovar/AnyPS5/blob/9baec8793ff4e16f3ae28aa771bbfe478754e5b6/core/libs/prx/libSceAgcDriver/Graphics/src/GuestBufferMemory.cpp#L809), [L2015-2017](https://github.com/boykopovar/AnyPS5/blob/9baec8793ff4e16f3ae28aa771bbfe478754e5b6/core/libs/prx/libSceAgcDriver/Graphics/src/GuestBufferMemory.cpp#L2015-L2017)<br>`core/libs/prx/libSceAgcDriver/tests/BdaResources.cpp` [L245](https://github.com/boykopovar/AnyPS5/blob/9baec8793ff4e16f3ae28aa771bbfe478754e5b6/core/libs/prx/libSceAgcDriver/tests/BdaResources.cpp#L245), [L1242](https://github.com/boykopovar/AnyPS5/blob/9baec8793ff4e16f3ae28aa771bbfe478754e5b6/core/libs/prx/libSceAgcDriver/tests/BdaResources.cpp#L1242) | | |
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!4515
No description provided.