Skip to content

fix(agc): flush pending writes before copying root command buffers - #27775

Draft
malekaifa wants to merge 4 commits into
boykopovar:mainfrom
malekaifa:prime-c/fix-root-command-buffer-flush-20261127
Draft

malekaifa wants to merge 4 commits into
boykopovar:mainfrom
malekaifa:prime-c/fix-root-command-buffer-flush-20261127

Conversation

@malekaifa

@malekaifa malekaifa commented Nov 27, 2026 •

Copy link
Copy Markdown
Contributor

What

Rebased onto current main at 1b8f79a.

copyCommands snapshots the root PM4 command buffer directly from guest memory. That path bypasses the host-read synchronization performed by GuestMemory::Read, so GPU writes that precede submission (including label writes) could still be pending when the host copied the root buffer.

The root-copy path now:

  • checks and charges the root dword budget exactly once before touching guest memory;
  • checks the complete non-empty guest range before flushing;
  • calls GuestMemory::FlushGpuWrites for the root range before the direct host copy;
  • invokes copySegment with root budget charging disabled because the root was already charged;
  • continues charging chained and nested indirect segments, preserving the copy limit and current-main chain/rewind handling.

copyRewindTail routes its tail through copyCommands, so the same validation and flush ordering also applies to rewind-tail root copies. The duplicate range check in Submit was removed; the common copy entry point owns the check.

The tests cover visibility of a GPU-written root command buffer, and verify that over-budget and inaccessible root ranges fail before the flush hook is called.

Tested

  • Windows 11 Education build 26200, WinLibs GCC 15.2.0: agc_driver_tests built successfully.
  • CTest agc_driver: passed on NVIDIA GeForce RTX 5080 (driver 32.0.16.2140) and AMD Radeon(TM) Graphics (driver 32.0.21042.62).
  • VK_LAYER_KHRONOS_validation enabled on both adapters: zero VUIDs, validation errors, or validation warnings.
  • Negative control: temporarily removing the root FlushGpuWrites call made agc_driver fail at the new root-buffer test with unsupported PM4 packet type: header 0x00000001; restoring the call and rebuilding returned the test to passing.
  • python tools/check_conventions.py --base upstream/main and git diff --check pass.
  • The reported Madden capture has not been supplied for a title-level reproduction; this change is a generic synchronization fix and does not claim that the title-specific issue is closed.

Manual overlap audit of the bot's shared-file list:

Checklist

  • Based on current main; no other open PR implements the same root-buffer synchronization fix
  • One topic per PR; follow-ups go in a new PR
  • No comments in code except technical debt and the licence notice of code adapted from upstream
  • Unimplemented paths throw (NotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file
  • No notes, investigation .md files or images added to the repository
  • New third-party code is a submodule built from source
  • Generic behaviour, not specific to one title
  • Depends on: none
  • Fixes: none (no issue is closed until the title-specific reproduction is verified)
  • Unblocks: none
  • GPUs tested: NVIDIA GeForce RTX 5080, driver 32.0.16.2140; AMD Radeon(TM) Graphics, driver 32.0.21042.62; Windows 11 Education build 26200
  • AI-assisted: yes

@coderabbitai

coderabbitai Bot commented Nov 27, 2026

Copy link
Copy Markdown

Important

Draft PR not reviewed

Draft PRs are not automatically reviewed by default.

  • Trigger a manual review

To automatically review draft PRs, update your CodeRabbit configuration:

reviews:
  auto_review:
    drafts: true
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

@MarcZX10 MarcZX10 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed — looks good

Nothing blocks this change; the reviewed code adds the required flush before snapshotting a non-empty root command buffer and covers the reported ordering gap with a focused test.

What I ran

  • Reviewed head 242105bf9a8fe98964d84f2c93fa1750050d5f68 using the prepared review bundle and repo_read.py.
  • No commands or tests were run by me. The test and git diff --check results in the description are author-reported.

Results

Check Result Note
Diff and targeted source review passed Both changed files read
Automated checks not run Author reports Windows test and diff check passed

Findings

Blocking

None.

Non-blocking

None.

Not verified

  • The Madden #24212 reproduction remains unconfirmed, as stated in the description.
  • No independent test run or git diff --check was performed.
  • Read: core/libs/prx/libSceAgcDriver/Execution/src/Driver/Queues/Submission.cpp, core/libs/prx/libSceAgcDriver/tests/Submit.cpp, and targeted sections of core/libs/prx/libSceAgcDriver/Execution/include/GuestMemory.hpp and core/libs/prx/libSceAgcDriver/Execution/src/GuestMemory.cpp.
  • Excluded: none listed in manifest.json; both changed files were included and read.

To get this to a good state

Nothing is required. Optional: retest the Madden #24212 capture against this branch to confirm the reported hang is fixed.

// The root command buffer is read directly by copySegment. Unlike GuestMemory::Read,
// that copy does not pass through the host-read hook; land any earlier GPU writes
// (notably labels) before snapshotting the words the guest just submitted.
GuestMemory::FlushGpuWrites(reinterpret_cast<std::uintptr_t>(guest), bytes);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could the shared root-copy path check the word budget and guest range once before invoking the flush hook? On this head, a regular submission checks the range in Submit and again in copyCommands. The new flush also runs before copyLink enforces the copy budget. Moving the root checks into copyCommands would give rewind tails the same validation order, avoid a second range check on every normal submission, and let the existing per-segment budget continue to cover jumps. The positive fixture already checks that the flush precedes the actual read.

AI-assisted: yes

@malekaifa malekaifa Nov 30, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rebased onto current main 1b8f79a. The rebase retained main's origin-aware copySegment/copyLink signatures, chain-loop detection, and copyRewindTail path. copyCommands checks and charges the root dword budget first, then checks the complete non-empty guest range, then calls FlushGpuWrites before the direct copy. The root segment is passed with chargeBudget=false because it was already charged; chained and nested indirect segments remain charged normally. copyRewindTail routes through copyCommands, so it gets the same ordering. The duplicate Submit range check is gone.

The latest-main build succeeded. CTest agc_driver passed on NVIDIA RTX 5080 (driver 32.0.16.2140) and AMD Radeon(TM) Graphics (driver 32.0.21042.62), with VK_LAYER_KHRONOS_validation enabled and zero VUID/error/warning diagnostics on both adapters. A negative control with only the root FlushGpuWrites call removed fails at the new root-buffer test with unsupported PM4 header 0x00000001; restoring the call and rebuilding passes again. Convention checks pass with the required PR template headings and no non-debt code comments.

Manual overlap audit: #15136 changes awaited-label/wait tracking and rewind-tail execution bookkeeping. #22434 is a direct signature-level overlap: it threads GuestRanges source tracking through copyCommands/copySegment/copyLink and caches rewind-tail copies. It does not implement root range validation, FlushGpuWrites, or root budget charging; these are orthogonal, but any rebase must carry GuestRanges independently from this PR's origin and chargeBudget parameters. #27635 adds GCN packet lowering at the packet-copy point in copyLink, also distinct from root-copy ordering. The other listed Driver.hpp changes are unrelated declarations/state. The PR remains draft because the reported Madden title capture has not been provided for a title-level reproduction.

@MarcZX10 MarcZX10 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed — looks good

Nothing blocks this change; the reported game capture still needs validation before the issue can be considered confirmed fixed.

What I ran

Reviewed head 65fa6b5f07f456ef7dc1d1518bfe2465d892733c. I read description.md, commits.md, manifest.json, diff.patch, and all three changed files listed as reviewed. I inspected targeted GuestMemory range-check and flush-hook implementations. No commands or tests were run; the environment provided the pinned reader repo_read.py only.

Results

Check Result Note
Diff and changed code passed All three reviewed files read
Automated tests not run Author reports Windows test passed
Madden capture not run Description says retest remains needed

Findings

Blocking

None.

Non-blocking

  • The regression test demonstrates the flush hook mutating the root buffer before parsing, and the range check occurs before the flush hook. The reported Madden capture remains unconfirmed, as the description states.

Sources

  • Submission.cpp:206-209 validates and flushes the submitted root range before copySegment reads it. The existing direct read is at Submission.cpp:240-241.
  • GuestMemory.cpp:1239-1250 shows CheckRange rejecting invalid or inaccessible ranges; GuestMemory.cpp:4006-4008 shows the flush hook invocation.
  • Submit.cpp:120-160 contains the new regression checks.

Not verified

I did not run tests or independently verify the author-reported Windows run or negative control. I did not reproduce the Madden capture or validate behavior on hardware. files.json was not available as a review-folder document; the listing showed all three changed files marked included, and I read each one.

To get this to a good state

Nothing is required. Retesting the Madden capture is optional validation noted in the PR description.

@github-actions github-actions Bot added the agc libSceAgc* label Nov 27, 2026
@JJenkx

JJenkx commented Nov 27, 2026

Copy link
Copy Markdown
Contributor

Linux / AMD run of head c7a203bedfd2, next to the Windows / RTX 5080 result in the description.

Setup: Linux 7.3.5, AMD Radeon RX 6650 XT (gfx1032, RADV), Mesa 26.3.1, GCC 16.2.1, Release. Fresh configure of the PR head. agc_driver_tests (default run, which includes testSubmissionFlushesRootCommandBuffer and testSubmissionChecksRootCopyBeforeFlush), with ANYPS5_REQUIRE_VULKAN=1 and the Khronos validation layer:

build result
PR head exit 0, 0 VUIDs
PR head with the GuestMemory::FlushGpuWrites(...) line in Driver::copyCommands removed exit 1: unsupported PM4 packet type: header 0x00000001 at DWORD 0 of 1
line restored exit 0, 0 VUIDs

So the regression test reproduces the described failure on Linux / RADV too, and the flush fixes it. I don't have the Madden #24212 capture, so the hang itself isn't retested here.

AI-assisted: yes

@github-actions github-actions Bot added the needs rebase Conflicts with main, rebase needed label Nov 27, 2026
@github-actions

github-actions Bot commented Nov 28, 2026 •

Copy link
Copy Markdown

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
PR 27635 core/libs/prx/libSceAgcDriver/Execution/src/Driver/Queues/Submission.cpp L235-237
PR 29094 core/libs/prx/libSceAgcDriver/Execution/src/Driver/Queues/Submission.cpp L264-266

Some comparisons could not be completed because a pull request conflicts with the base branch. Review these shared files manually before concluding that there is no overlap.

Pull request Shared files not compared
PR 5609 core/libs/prx/libSceAgcDriver/Execution/include/Driver/Driver.hpp
PR 5925 core/libs/prx/libSceAgcDriver/Execution/include/Driver/Driver.hpp
PR 6188 core/libs/prx/libSceAgcDriver/Execution/include/Driver/Driver.hpp
PR 7275 core/libs/prx/libSceAgcDriver/Execution/include/Driver/Driver.hpp
PR 7642 core/libs/prx/libSceAgcDriver/Execution/include/Driver/Driver.hpp
PR 9508 core/libs/prx/libSceAgcDriver/Execution/include/Driver/Driver.hpp
PR 10193 core/libs/prx/libSceAgcDriver/Execution/include/Driver/Driver.hpp
PR 10359 core/libs/prx/libSceAgcDriver/Execution/include/Driver/Driver.hpp
PR 11435 core/libs/prx/libSceAgcDriver/Execution/include/Driver/Driver.hpp
PR 13928 core/libs/prx/libSceAgcDriver/Execution/include/Driver/Driver.hpp
PR 13929 core/libs/prx/libSceAgcDriver/Execution/include/Driver/Driver.hpp
PR 15136 core/libs/prx/libSceAgcDriver/Execution/include/Driver/Driver.hpp
core/libs/prx/libSceAgcDriver/Execution/src/Driver/Queues/Submission.cpp
PR 19557 core/libs/prx/libSceAgcDriver/Execution/include/Driver/Driver.hpp
PR 20172 core/libs/prx/libSceAgcDriver/tests/Submit.cpp
PR 21923 core/libs/prx/libSceAgcDriver/Execution/include/Driver/Driver.hpp
PR 22029 core/libs/prx/libSceAgcDriver/Execution/include/Driver/Driver.hpp
core/libs/prx/libSceAgcDriver/Execution/src/Driver/Queues/Submission.cpp
PR 22434 core/libs/prx/libSceAgcDriver/Execution/include/Driver/Driver.hpp
core/libs/prx/libSceAgcDriver/Execution/src/Driver/Queues/Submission.cpp
PR 22647 core/libs/prx/libSceAgcDriver/Execution/include/Driver/Driver.hpp
core/libs/prx/libSceAgcDriver/Execution/src/Driver/Queues/Submission.cpp
PR 22841 core/libs/prx/libSceAgcDriver/Execution/include/Driver/Driver.hpp
PR 24661 core/libs/prx/libSceAgcDriver/Execution/include/Driver/Driver.hpp
PR 27133 core/libs/prx/libSceAgcDriver/Execution/include/Driver/Driver.hpp
PR 28237 core/libs/prx/libSceAgcDriver/Execution/include/Driver/Driver.hpp
PR 28808 core/libs/prx/libSceAgcDriver/Execution/include/Driver/Driver.hpp

@malekaifa
malekaifa force-pushed the prime-c/fix-root-command-buffer-flush-20261127 branch from c7a203b to d4df94f Compare November 30, 2026 08:20

@MarcZX10 MarcZX10 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed — looks good

Nothing blocks this change; the prepared diff and source context support the root-range validation, flush ordering, and single root-budget charge described in the PR.

What I ran

  • Head: 9b32c39e18cc1e8efb7f59eed1b578152040be51.
  • Read the prepared description, commit list, manifest, and diff through repo_read.py; inspected all three changed files at the pinned head.
  • No build or tests were run in this review; reported test results are author supplied.

Results

Check Result Note
Diff and source review passed All three changed files read
Build and tests not run Results in PR description are author supplied
Negative control not run Author reports a successful negative control

Findings

Blocking

None.

Non-blocking

None.

Not verified

  • Runtime behavior, build, tests, and the reported hardware results were not independently verified.
  • files.json was not available through the review reader, so exclusions beyond the manifest could not be confirmed.

To get this to a good state

Nothing is required.

@malekaifa

Copy link
Copy Markdown
Contributor Author

Exact-head recheck (2026-11-30): the current head is 9b32c39e18cc, replacing the older c7a203bedf noted in the handoff. It merges cleanly against current main dd5412d54ef5; the exact-head checks are complete with 7 successes and no failures (7 skipped, 4 cancelled). Keeping this PR in draft because the title-specific capture/reproduction is still unavailable; the generic synchronization fix is not being represented as closing that title report.

@xXPBSXx

xXPBSXx commented Dec 1, 2026

Copy link
Copy Markdown
Contributor

Title evidence for PPSA15246 (Windows 10 Pro, one machine, 600 s per run, 2026-12-01). The title was run with and without this pull request (head 9b32c39e18cc) on the same main 76d0a4a7dcdf, three runs per arm, with libraries rebuilt for each arm. A temporary diagnostic that is not part of this pull request was added to both arms: when the driver rejects a submission with unsupported PM4 packet type, it compares the guest command buffer with the copy the driver took, once at the rejection and once 50 ms later, and prints [pm4-recheck] lines (changed dwords, first and last index, zero dwords in the copy and in the guest now).

Result in short: this pull request does not remove the rejection for this title. In all 6 runs the submission of 6387 dwords is rejected with header 0x00000000 at DWORD 15 of 6387, which is the same as on main.

Arm Run Dwords changed in the guest buffer after the copy (at the rejection / 50 ms later) Zero dwords in the copy Rejection
without 1 1 / 4 (first 191, last 3194) 2189 yes
without 2 0 / 0 2592 yes
without 3 1 / 1 (index 3194) 2593 yes
with 1 0 / 0 2592 yes
with 2 0 / 0 2596 yes
with 3 0 / 0 2592 yes

In every run the 15 dwords before the rejected header are equal in the copy and in the guest buffer, and the changed dwords lie far behind dword 15. The [pm4-recheck] lines of each run are quoted below.

What this shows: with this pull request the guest buffer did not change after the copy in 3 of 3 runs, without it it changed in 2 of 3 runs (one to four dwords). What it does not show: who writes those dwords, whether this pull request is the reason they did not change (three runs per arm, no random assignment), why dword 15 is zero, or what a PS5 does with a zero header (not measured). No change is requested; the numbers are given as a title measurement.

without, run 1: [pm4-recheck] lines
[pm4-recheck] at the refusal: guest 00000002035c5620, dwords before 15 equal 1, from dword 15 of 6387: changed 1 (first 191, last 191), zero dwords in the copy 2189, zero dwords in the guest now 2188
[pm4-recheck] 50 ms later: guest 00000002035c5620, dwords before 15 equal 1, from dword 15 of 6387: changed 4 (first 191, last 3194), zero dwords in the copy 2189, zero dwords in the guest now 2185
without, run 2: [pm4-recheck] lines
[pm4-recheck] at the refusal: guest 0000000255235620, dwords before 15 equal 1, from dword 15 of 6387: changed 0 (first 0, last 0), zero dwords in the copy 2592, zero dwords in the guest now 2592
[pm4-recheck] 50 ms later: guest 0000000255235620, dwords before 15 equal 1, from dword 15 of 6387: changed 0 (first 0, last 0), zero dwords in the copy 2592, zero dwords in the guest now 2592
without, run 3: [pm4-recheck] lines
[pm4-recheck] at the refusal: guest 0000000255225620, dwords before 15 equal 1, from dword 15 of 6387: changed 1 (first 3194, last 3194), zero dwords in the copy 2593, zero dwords in the guest now 2592
[pm4-recheck] 50 ms later: guest 0000000255225620, dwords before 15 equal 1, from dword 15 of 6387: changed 1 (first 3194, last 3194), zero dwords in the copy 2593, zero dwords in the guest now 2592
with, run 1: [pm4-recheck] lines
[pm4-recheck] at the refusal: guest 0000000297235620, dwords before 15 equal 1, from dword 15 of 6387: changed 0 (first 0, last 0), zero dwords in the copy 2592, zero dwords in the guest now 2592
[pm4-recheck] 50 ms later: guest 0000000297235620, dwords before 15 equal 1, from dword 15 of 6387: changed 0 (first 0, last 0), zero dwords in the copy 2592, zero dwords in the guest now 2592
with, run 2: [pm4-recheck] lines
[pm4-recheck] at the refusal: guest 0000000315225620, dwords before 15 equal 1, from dword 15 of 6387: changed 0 (first 0, last 0), zero dwords in the copy 2596, zero dwords in the guest now 2596
[pm4-recheck] 50 ms later: guest 0000000315225620, dwords before 15 equal 1, from dword 15 of 6387: changed 0 (first 0, last 0), zero dwords in the copy 2596, zero dwords in the guest now 2596
with, run 3: [pm4-recheck] lines
[pm4-recheck] at the refusal: guest 0000000259215620, dwords before 15 equal 1, from dword 15 of 6387: changed 0 (first 0, last 0), zero dwords in the copy 2592, zero dwords in the guest now 2592
[pm4-recheck] 50 ms later: guest 0000000259215620, dwords before 15 equal 1, from dword 15 of 6387: changed 0 (first 0, last 0), zero dwords in the copy 2592, zero dwords in the guest now 2592

AI-assisted (Claude Code).

@ligmaSec ligmaSec left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The new hook test verifies validation and flush order for submission. The same change now invokes the production hook while noteReleasedTails scans a released rewind tail and again when executeRewindTail copies it. Could you cover that worker path with a pending producer write into the released tail, including the case where the tail scan runs before the producer finishes?

Source review at 9b32c39e18. The head trial-merges onto 6ef4f2f73b. Focused Linux checks are queued.

AI-assisted: yes.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

agc libSceAgc*

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants