fix(agc): synchronize pending writes before command allocation #6747

Merged
krishalivaibhav merged 1 commit from upstream-pr/6747 into main 2026-10-23 14:20:43 +00:00
krishalivaibhav commented 2026-10-16 09:22:53 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by krishalivaibhav on 2026-10-16T09:22:53Z.


What

Before the native command allocator returns a range for CPU packet writes, synchronize tracked GPU writes already pending over that range. Disjoint ranges keep the existing asynchronous path. The check runs after Reserve, so callback-selected storage is covered, and before cursor advancement or packet writes. Packet tests that compile Packet.cpp directly now link its driver dependency.

Balatro PPSA21402 exposed the problem in our combined macOS port: a deferred queue-0 GPU label requested a four-byte store of 1 at 0x2c4432738. The next copied submission used that exact address as the value word of SET_UCONFIG_REG_INDEX:

[write-provenance] phase=gpu-label-request address=00000002c4432738 bytes=4 low=00000001
[index-source] valueAddress=00000002c4432738 pendingValueWriter=1
packet: c0017a00 20000243 00000001
following draw: c0042700 00000006 c4c7767a 00000002 00000006 00000000
terminating due to ... null or misaligned index base

That selects four-byte indices for an address aligned only to two bytes. The native setter normally emits 0x400 | indexSize. Symbolication identifies the matching write request as Driver::recordDeferredLabels. This evidence identifies a concrete overlapping write request; the patch guarantees ordering for tracked writes already pending at allocation. It does not cover external packet patches that bypass Allocate, GPU readers, or newly racing unsynchronized writers.

Tested

  • Apple M2, 16 GB, macOS 27.2, x86-64 through Rosetta, MoltenVK 1.4.2. Runtime validation used our combined local macOS port, with this allocator change integrated; the clean main-based branch has not yet been built or run on Linux/Windows. CI and independent platform testing remain pending.
  • Real-GPU regression through Agc::Command::Emit: queue a GPU store of 1, emit an index-type packet reusing that word for 0x400, finish GPU work, and check 0x400 survives. A disjoint packet allocation must leave the pending batch recording. The local integration fixture fails with synchronization disabled (a pending GPU label overwrote a reused command word) and passes with it enabled. The dedicated CTest entry returns 77 when host imports are unavailable.
  • Eight affected packet programs built and passed locally: register commands, conditional writes, branch patching, predication, DMA data, write-data patching, PRIME_UTCL2, conditional-execution patching.
  • Same diagnostic binary hashes/cache: allocator check disabled crashed at 12.50 seconds; enabled completed a 33.13-second bounded run. A later 90-second run completed normally at 93.88 seconds, and the user played it and reported approximately 45 FPS with no stutter or crash before timed shutdown. That run also included separate, process-local shader experiments; its FPS is integration context, not an isolated performance result for this patch. Long-session stability remains unverified.
  • tools/check_conventions.py --base origin/main and git diff --check pass on this clean branch based on 5d9dcc42d.
  • Initial GitHub conventions/intake checks passed. The repository workflow skips automatic full Linux/Windows builds for PRs; its workflow_dispatch requires collaborator write access. A maintainer can select pr=6747, full=true, linux=true to validate the clean main merge. The passing eligibility job does not mean the skipped platform builds ran.

Checklist

  • Based on current main; no known open PR implements this allocator guard
  • One topic per PR; follow-ups go in a new PR
  • No comments in code except technical debt
  • Unimplemented paths throw; no new silent stubs
  • No notes, investigation files or images added to the repository
  • New third-party code is a submodule built from source (none added)
  • Generic behaviour, not specific to one title
  • Depends on: none (code uses APIs already on main; macOS gameplay used additional local port work)
  • Unblocks: PPSA21402, local combined macOS port; compatibility context in #1
  • GPUs tested: Apple M2 / MoltenVK 1.4.2 / macOS 27.2, local combined port
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4886244914 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/6747). Originally posted by [krishalivaibhav](https://github.com/krishalivaibhav) on 2026-10-16T09:22:53Z. --- ### What Before the native command allocator returns a range for CPU packet writes, synchronize tracked GPU writes already pending over that range. Disjoint ranges keep the existing asynchronous path. The check runs after `Reserve`, so callback-selected storage is covered, and before cursor advancement or packet writes. Packet tests that compile `Packet.cpp` directly now link its driver dependency. Balatro **PPSA21402** exposed the problem in our combined macOS port: a deferred queue-0 GPU label requested a four-byte store of `1` at `0x2c4432738`. The next copied submission used that exact address as the value word of `SET_UCONFIG_REG_INDEX`: ```text [write-provenance] phase=gpu-label-request address=00000002c4432738 bytes=4 low=00000001 [index-source] valueAddress=00000002c4432738 pendingValueWriter=1 packet: c0017a00 20000243 00000001 following draw: c0042700 00000006 c4c7767a 00000002 00000006 00000000 terminating due to ... null or misaligned index base ``` That selects four-byte indices for an address aligned only to two bytes. The native setter normally emits `0x400 | indexSize`. Symbolication identifies the matching write request as `Driver::recordDeferredLabels`. This evidence identifies a concrete overlapping write request; the patch guarantees ordering for tracked writes already pending at allocation. It does not cover external packet patches that bypass `Allocate`, GPU readers, or newly racing unsynchronized writers. ### Tested - Apple M2, 16 GB, macOS 27.2, x86-64 through Rosetta, MoltenVK 1.4.2. Runtime validation used our combined local macOS port, with this allocator change integrated; **the clean main-based branch has not yet been built or run on Linux/Windows**. CI and independent platform testing remain pending. - Real-GPU regression through `Agc::Command::Emit`: queue a GPU store of `1`, emit an index-type packet reusing that word for `0x400`, finish GPU work, and check `0x400` survives. A disjoint packet allocation must leave the pending batch recording. The local integration fixture fails with synchronization disabled (`a pending GPU label overwrote a reused command word`) and passes with it enabled. The dedicated CTest entry returns 77 when host imports are unavailable. - Eight affected packet programs built and passed locally: register commands, conditional writes, branch patching, predication, DMA data, write-data patching, PRIME_UTCL2, conditional-execution patching. - Same diagnostic binary hashes/cache: allocator check disabled crashed at 12.50 seconds; enabled completed a 33.13-second bounded run. A later 90-second run completed normally at 93.88 seconds, and the user played it and reported approximately 45 FPS with no stutter or crash before timed shutdown. That run also included separate, process-local shader experiments; its FPS is integration context, not an isolated performance result for this patch. Long-session stability remains unverified. - `tools/check_conventions.py --base origin/main` and `git diff --check` pass on this clean branch based on `5d9dcc42d`. - Initial GitHub conventions/intake checks passed. The repository workflow skips automatic full Linux/Windows builds for PRs; its `workflow_dispatch` requires collaborator write access. A maintainer can select `pr=6747`, `full=true`, `linux=true` to validate the clean main merge. The passing eligibility job does not mean the skipped platform builds ran. ### Checklist - [x] Based on current `main`; no known open PR implements this allocator guard - [x] One topic per PR; follow-ups go in a new PR - [x] No comments in code except technical debt - [x] Unimplemented paths throw; no new silent stubs - [x] No notes, investigation files or images added to the repository - [x] New third-party code is a submodule built from source (none added) - [x] Generic behaviour, not specific to one title - [x] Depends on: none (code uses APIs already on main; macOS gameplay used additional local port work) - [x] Unblocks: PPSA21402, local combined macOS port; compatibility context in #1 - [x] GPUs tested: Apple M2 / MoltenVK 1.4.2 / macOS 27.2, local combined port - [x] AI-assisted: yes
blawesom commented 2026-10-17 09:52:54 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by blawesom on 2026-10-17T09:52:54Z.


Rebase handoff — rebased onto current main (authorship unchanged). One conflict in the test registration: your agc_command_memory_reuse entry sits alongside main's newer agc_recorder_device_selection entry, both with their Windows environment blocks.

Verified on Linux (CachyOS, kernel 7.2.9, GCC 16.2.1, Ninja, Release, BUILD_TESTING=ON, AMD RX 9070 XT / RADV):

  • builds; tools/check_conventions.py --base origin/main passes
  • full suite: 1335/1336 — the only failure is the known main regression (agc_driver_tessellation segfault, #4727), not this PR

To pick it up:

git fetch https://github.com/blawesom/AnyPS5 rebase/6747
git checkout <your branch>
git merge --ff-only FETCH_HEAD

Head is 2b5e10f8d.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6232501497 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/6747#issuecomment-6232501497). Originally posted by [blawesom](https://github.com/blawesom) on 2026-10-17T09:52:54Z. --- **Rebase handoff** — rebased onto current `main` (authorship unchanged). One conflict in the test registration: your `agc_command_memory_reuse` entry sits alongside `main`'s newer `agc_recorder_device_selection` entry, both with their Windows environment blocks. Verified on Linux (CachyOS, kernel 7.2.9, GCC 16.2.1, Ninja, Release, `BUILD_TESTING=ON`, AMD RX 9070 XT / RADV): - builds; `tools/check_conventions.py --base origin/main` passes - **full suite: 1335/1336** — the only failure is the known `main` regression (`agc_driver_tessellation` segfault, #4727), not this PR **To pick it up:** ``` git fetch https://github.com/blawesom/AnyPS5 rebase/6747 git checkout <your branch> git merge --ff-only FETCH_HEAD ``` Head is `2b5e10f8d`.
krishalivaibhav commented 2026-10-21 20:30:33 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by krishalivaibhav on 2026-10-21T20:30:33Z.


Thanks @blawesom for the rebase handoff. I picked it up and rebased again onto current main (head 0b5ba4470); the test registration resolution is unchanged from yours.

Linux (Ubuntu 24.04 x86-64 under Rosetta, GCC 13.3, Ninja, Release, BUILD_TESTING=ON): agc_command_memory_reuse, agc_recorder_device_selection, agc_recorder_read_list, agc_recorder_deferred_failure_report and agc_recorder_kept_bytes pass. Full suite not run locally.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6325872409 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/6747#issuecomment-6325872409). Originally posted by [krishalivaibhav](https://github.com/krishalivaibhav) on 2026-10-21T20:30:33Z. --- Thanks @blawesom for the rebase handoff. I picked it up and rebased again onto current `main` (head `0b5ba4470`); the test registration resolution is unchanged from yours. Linux (Ubuntu 24.04 x86-64 under Rosetta, GCC 13.3, Ninja, Release, `BUILD_TESTING=ON`): `agc_command_memory_reuse`, `agc_recorder_device_selection`, `agc_recorder_read_list`, `agc_recorder_deferred_failure_report` and `agc_recorder_kept_bytes` pass. Full suite not run locally.
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!6747
No description provided.