Skip to content

fix(agc): preserve GPU edge results after unchanged CPU writes - #1918

Closed
nikhilpanchal1 wants to merge 2 commits into
boykopovar:mainfrom
nikhilpanchal1:fix/agc-storage-edge-writeback
Closed

nikhilpanchal1 wants to merge 2 commits into
boykopovar:mainfrom
nikhilpanchal1:fix/agc-storage-edge-writeback

Conversation

@nikhilpanchal1

Copy link
Copy Markdown
Contributor

What

Addresses finding 2 of #865. A same-value CPU write dirties a page even when its bytes still hold the allocator fill. Storage-image writeback previously discarded the GPU results for that CPU-stamped edge, losing glyphs.

Keep immutable baselines for at most the two partial 64 KiB edges. Merge GPU results into bytes that still equal the baseline while preserving actual CPU edits. Newer driver stores retain ownership even when they write the same value; retired allocation identities cannot authorize a merge into replacement memory.

Capture baselines before uploads, clears, and copies return, waiting only for earlier physical writes overlapping an edge. Alias borrowing retains baseline ownership, and ordinary GPU writeback renews baselines from the exact immutable retiled output. Full-block ownership and upstream Windows import coverage policy remain intact.

Related open PR: #1599 targets the same finding with a whole-range equality check. This is an alternative implementation that also preserves GPU bytes beside real CPU edits in the same edge and distinguishes ordered driver stores from CPU page stamps. It has no dependency on that PR.

Tested

Linux Release build, GCC 14, Mesa 26.1.6 llvmpipe; Vulkan execution required.

  • Full build and patched runtime libraries passed on upstream f1451396.
  • Complete CTest suite, with passing tests repeated three times: 468 passed, 1 failed, 26 capability skips, 495 unique tests.
  • The failure is agc_driver_f32_denormal_flush: lane 20 FMA returns 0x00800000 versus expected 0x00ffffff. It reproduces after rebuilding and running that target on an unmodified checkout of upstream f1451396. Shader translation and this floating-point test are unchanged by this patch.
  • New runtime regression repeated three times with Khronos synchronization validation and shader caches disabled: no validation errors. Repeated separately with unit shadows disabled and collect memoization disabled.
  • Regression covers unchanged and mixed CPU/GPU bytes, neighbors, baseline renewal, raw uncollected writes, aliases, allocation replacement, CPU fallback, driver precedence, and deferred uploads, fills, and copies.
  • Deliberately unavailable Vulkan skips only when ANYPS5_REQUIRE_VULKAN is unset; the same setup fails with it set.
  • Native recorder executable, conventions, test-registration audit, and diff checks passed.
  • Three independent Sol 6.1 xhigh reviewers approved the implementation and refreshed their reviews after rebasing.

This host lacks userfaultfd. The new Linux regression uses a test-only page-watch backend; allocation tracking, host imports, detiling, GPU commands, and readback are production code. Native write-watch and subgroup/rasterization capability skips are reported separately from passes. Windows and interactive title testing were not performed.

Checklist

  • Based on current main; competing implementation fix(agc): store GPU results over CPU-stamped bytes the CPU left unchanged #1599 is disclosed above
  • One topic per PR
  • No new code comments
  • No unimplemented paths or silent stubs added
  • No investigation notes, reports, images, or logs added to the repository
  • No new third-party dependencies
  • Generic behavior, not specific to one title
  • Depends on: none
  • AI-assisted: yes

Merge partial storage-image edges against bounded immutable baselines while preserving real CPU changes, newer driver stores, and allocation ownership. Cover deferred uploads, clears, copies, aliases, fallback, and writeback with runtime regressions.

@klosixgaming-arch klosixgaming-arch 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.

I'm not approving this yet, and I want to be specific about why rather than rubber-stamp it. This is a large, intricate change to the write-back / write-tracking core, and I don't think the diff alone gives me enough grounded confidence to sign off.

What I'd want before this is approvable:

  1. The core invariant isn't demonstrable from the diff. The whole point is "preserve GPU results in bytes that still equal the baseline, while keeping real CPU edits." That depends on the interaction of four new pieces — MergeChangedCommitted's per-byte CAS against original, driverStoredOver/driverBlocks, edgeSnapshot's allocation weak_ptr ownership, and finishEdgeMerges' DriverStoredOver gate. The failure mode that matters (a GPU writeback being silently dropped on a CPU-stamped edge, or a CPU edit being overwritten by a stale GPU result) is exactly the class of bug that's easy to miss in review and hard to prove from reading. Can you point me to a concrete before/after capture — the specific glyph/edge that used to be lost and now survives — plus the test that fails on main and passes here? The body references finding 2 of #865 and "glyphs", but I want the repro, not the claim.

  2. Competing implementation. You disclose #1599 targets the same finding with a whole-range equality check, and this is a "different" implementation. That's a maintainer decision, but I'd want it made before review, not after — otherwise two large divergent write-back rewrites both sit in review and one gets merged by accident. Which one is the project going with, and is this the chosen one?

  3. The CAS merge assumes original is a trustworthy per-byte baseline. In MergeChangedCommitted, compare_exchange_strong(expected=original[byte], desired=current[byte]) keeps a byte only if the current destination still equals original. If a CPU wrote to that byte after the baseline was captured but before this merge ran, the CAS would fail and the CPU write survives — good. But if the GPU and CPU both touched the byte, the GPU result wins. Is that the intended precedence, and is it stated in the debt entry? The body says "Newer driver stores retain ownership even when they write the same value" — I want that precedence made explicit and tested, because it's the load-bearing assumption.

  4. Pre-existing failure disclosure. You report agc_driver_f32_denormal_flush failing (lane 20 FMA 0x00800000 vs 0x00ffffff) and claim it reproduces on unmodified f1451396. That's plausible (llvmpipe denormal handling), but it's exactly the kind of "it fails and I blame the host" that needs a clean proof. A short note with the exact command and the unmodified-checkout output would close it.

I'm not asking you to prove it's wrong — I'm asking for the evidence that lets me verify the one thing that matters (GPU results not silently lost, CPU edits not clobbered) without having to fully simulate the ownership model in my head. If you can supply 1, 2 and 3, I'll re-review and this is very likely approvable.

Written with AI assistance.

@github-actions

github-actions Bot commented Oct 9, 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
#1847 core/libs/prx/libSceAgcDriver/Execution/src/GuestMemory.cpp L770
docs/dev/TechnicalDebt.md L248-249
#1994 docs/dev/TechnicalDebt.md L248-249
#1995 docs/dev/TechnicalDebt.md L248
#2096 core/libs/prx/libSceAgcDriver/Graphics/include/Texture.hpp L427
core/libs/prx/libSceAgcDriver/Graphics/src/Texture.cpp L1479, L1553-1556
docs/dev/TechnicalDebt.md L248-249

@nikhilpanchal1

Copy link
Copy Markdown
Contributor Author

I'm not approving this yet, and I want to be specific about why rather than rubber-stamp it. This is a large, intricate change to the write-back / write-tracking core, and I don't think the diff alone gives me enough grounded confidence to sign off.

What I'd want before this is approvable:

  1. The core invariant isn't demonstrable from the diff. The whole point is "preserve GPU results in bytes that still equal the baseline, while keeping real CPU edits." That depends on the interaction of four new pieces — MergeChangedCommitted's per-byte CAS against original, driverStoredOver/driverBlocks, edgeSnapshot's allocation weak_ptr ownership, and finishEdgeMerges' DriverStoredOver gate. The failure mode that matters (a GPU writeback being silently dropped on a CPU-stamped edge, or a CPU edit being overwritten by a stale GPU result) is exactly the class of bug that's easy to miss in review and hard to prove from reading. Can you point me to a concrete before/after capture — the specific glyph/edge that used to be lost and now survives — plus the test that fails on main and passes here? The body references finding 2 of agc-driver: storage image write-back drops GPU results for CPU-stamped 64 KiB edge blocks (boxes in Silksong's menu text) #865 and "glyphs", but I want the repro, not the claim.

  2. Competing implementation. You disclose fix(agc): store GPU results over CPU-stamped bytes the CPU left unchanged #1599 targets the same finding with a whole-range equality check, and this is a "different" implementation. That's a maintainer decision, but I'd want it made before review, not after — otherwise two large divergent write-back rewrites both sit in review and one gets merged by accident. Which one is the project going with, and is this the chosen one?

  3. The CAS merge assumes original is a trustworthy per-byte baseline. In MergeChangedCommitted, compare_exchange_strong(expected=original[byte], desired=current[byte]) keeps a byte only if the current destination still equals original. If a CPU wrote to that byte after the baseline was captured but before this merge ran, the CAS would fail and the CPU write survives — good. But if the GPU and CPU both touched the byte, the GPU result wins. Is that the intended precedence, and is it stated in the debt entry? The body says "Newer driver stores retain ownership even when they write the same value" — I want that precedence made explicit and tested, because it's the load-bearing assumption.

  4. Pre-existing failure disclosure. You report agc_driver_f32_denormal_flush failing (lane 20 FMA 0x00800000 vs 0x00ffffff) and claim it reproduces on unmodified f1451396. That's plausible (llvmpipe denormal handling), but it's exactly the kind of "it fails and I blame the host" that needs a clean proof. A short note with the exact command and the unmodified-checkout output would close it.

I'm not asking you to prove it's wrong — I'm asking for the evidence that lets me verify the one thing that matters (GPU results not silently lost, CPU edits not clobbered) without having to fully simulate the ownership model in my head. If you can supply 1, 2 and 3, I'll re-review and this is very likely approvable.

Written with AI assistance.

@klosixgaming-arch Thanks for the review. Added regression tests and documented the merge precedence in 97655b6: changed CPU bytes survive, same-value/ABA writes permit GPU merging, and newer driver stores block stale merges. The updated edge test passes locally, uncached and with synchronization validation; CI is still running.

Native/title validation and the choice between #1599 and #1918 remain open.

@oneandonlydean

Copy link
Copy Markdown
Collaborator

Closing: #1599 fixes the same finding from #865 with a much smaller change and a title behind it. If it misses a case this one handles, please show it on #1599 with the title and what it loses on main.

@popcoder565

Copy link
Copy Markdown
Contributor

Native Windows run, since the description notes Windows wasn't tested. Setup: Windows 10 (19045), RTX 3060 Laptop (NVIDIA 616.92), MinGW-w64 GCC 15.2.0 (WinLibs r7), Release. Head 97655b67 merged onto main d9484c7.

The new agc_storage_edge_writeback fails in every run (10 of 10, and in both full-suite runs):

Storage edge write-back test failed: AGC graphics: the untracked driver-store fixture is watched
  • This is the last check in driverProvenanceTests (StorageEdgeWriteBack.cpp:418).
  • The fixture is a std::array on the test thread's stack. On Windows that stack is inside the guest arena's range (8 GiB to 0xFC00000000); here it was at 0x49ab5f5350, and Memory::Watched reports it as watched. On Linux the stack is outside that range.
  • So the fixture's assumption looks like what fails on Windows, not the change itself. A buffer outside the arena's range would avoid it, or the check could be Linux-only.
  • The Windows CI job has no Vulkan device, so it skips this test and won't show the failure.

Everything else passes:

No title was run.

🤖 Addressed by Claude Code

@nikhilpanchal1

Copy link
Copy Markdown
Contributor Author

@popcoder565 thanks for the native Windows run, seems you were right about the stack allocation. Fixed in eca4eee: the untracked-range checks now use an address-only range outside the guest arena, and run before Vulkan setup so missing GPU support cannot hide them. All three assertions are preserved.

The updated regression passes 10/10 on Linux, plus three runs with Vulkan synchronization validation. I haven’t rerun it on native Windows; could you check the updated head when you have time?

@popcoder565

Copy link
Copy Markdown
Contributor

@nikhilpanchal1 thanks. eca4eee5 isn't on the PR branch yet; its head is still 97655b67. I'll rerun the Windows check once it's pushed.

Also, since #2096 merged, the branch conflicts with main (51f69aa) in Texture.cpp, Texture.hpp and TechnicalDebt.md.

🤖 Addressed by Claude Code

@nikhilpanchal1

Copy link
Copy Markdown
Contributor Author

@popcoder565 it's cause the PR is closed. You'll have to fetch from fork unfortunately

@popcoder565

Copy link
Copy Markdown
Contributor

@nikhilpanchal1 thanks. I hadn't noticed this was closed in favour of #1599 before my run. The branch also conflicts with main now, so I'll leave eca4eee for now. If the regression test moves to #1599 or a new PR, I'll run it on Windows there.

🤖 Addressed by Claude Code

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

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants