fix(agc): keep write-tracker generations 64-bit so they cannot wrap #14899

Merged
Sam-mon7 merged 1 commit from upstream-pr/14899 into main 2026-11-03 05:12:04 +00:00
Sam-mon7 commented 2026-11-01 23:37:15 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Sam-mon7 on 2026-11-01T23:37:15Z.


What

WriteTracker (GuestMemory.cpp) kept its generation counter, its stable counter and every stored stamp as 32-bit values: the per-block arrays (blocks, cpuBlocks, coarseBlocks, hintBlocks), the CPU and walk page stamps, the driver pieces, the refused-block map and the unstamped-store ring. Every consumer (UnchangedSince, WrittenSince, StoredAllSince, NewestStamp, ChangedBlocks) compares 64-bit stamps, and noteWrittenRun narrowed the generation it passes on to 32 bits. After 2^32 generation bumps, a block written after the wrap carried a small stamp. It then compared as older than a stamp taken before the wrap, so changed memory read as unchanged and a cached resource could be reused over new data.

The counters, every stored stamp and noteWrittenRun's parameter are now 64-bit, and nothing narrows between the counter, the stored stamps and the 64-bit API values. Block offsets inside a DriverPiece stay 32-bit. Each 4 GiB tracking leaf grows from 772 KiB to 1544 KiB; leaves are allocated only once a block in them is stamped.

agc_write_tracking_generation_wrap moves the generation to just below 2^32 through AdvanceTrackerGenerationForTest. It then checks that driver and CPU stores made after the crossing still read as changed, over coarse runs, single blocks, CPU pages, driver pieces and ChangedBlocks.

Noticed while working on Demon's Souls (PPSA01341) on a local branch; the change is generic.

Tested

Windows 11 Pro 10.0.26200, WinLibs GCC 15.2.0 r7, Release, NVIDIA GeForce RTX 4070 (driver 610.88). Branch based on main 2c28d327e3; head bcda8554da.

  • Focused tests, ctest -R '^(agc_write_tracking|agc_resident_direct_remap|agc_rewatched_direct_shared|agc_import_alias_shared|agc_compared_shared_generations|agc_collect_mapping_history|agc_draw_snapshot_epoch|agc_driver_store_over_untracked|agc_driver_untracked_snapshot_hint|agc_driver_write_data_written_ahead|agc_private_collect|agc_shared_write_partial_collect|agc_unchanged_cpu_stamps|agc_driver_resource_cache_collect_refusals|agc_screenshot)' -V -j2 with ANYPS5_REQUIRE_VULKAN=1, ANYPS5_NO_SHADER_CACHE=1 and VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation: main 2c28d327e3 23/23 pass; head bcda8554da 24/24 pass, including the new test. Neither log contains VUID- or a missing-validation-layer message.
  • Red/green: with main's GuestMemory.cpp (32-bit tracker, plus only the test hook), agc_write_tracking_tests --generation-wrap fails with the tracker generation did not cross 2^32; with this change it passes.
  • python tools/check_conventions.py --base upstream/main passes.

Overlap: tools/pr_overlap.py --branch found no shared lines with any open pull request it could compare. It could not compare #5021, #6187, #6188, #6192, #6209, #6273, #7048, #7604, #7642 and #12840, which conflict with main. They share GuestMemory.cpp/.hpp, and #6273, #7048 and #7604 also share WriteTracking.cpp. Checked by hand: none of #6187, #6192, #7048 and #7604 touches the tracker's stamp declarations, and #12840 is a BDA heap-mirror write-back. If one of them lands first, I'll rebase.

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 under Silent stubs in their library's technical debt file
  • 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: none
  • Fixes: none
  • Unblocks: none
  • GPUs tested: NVIDIA GeForce RTX 4070, driver 610.88, Windows 11 Pro 10.0.26200
  • AI-assisted: yes (Claude Code)

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5112711808 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/14899). Originally posted by [Sam-mon7](https://github.com/Sam-mon7) on 2026-11-01T23:37:15Z. --- ### What `WriteTracker` (GuestMemory.cpp) kept its generation counter, its stable counter and every stored stamp as 32-bit values: the per-block arrays (`blocks`, `cpuBlocks`, `coarseBlocks`, `hintBlocks`), the CPU and walk page stamps, the driver pieces, the refused-block map and the unstamped-store ring. Every consumer (`UnchangedSince`, `WrittenSince`, `StoredAllSince`, `NewestStamp`, `ChangedBlocks`) compares 64-bit stamps, and `noteWrittenRun` narrowed the generation it passes on to 32 bits. After 2^32 generation bumps, a block written after the wrap carried a small stamp. It then compared as older than a stamp taken before the wrap, so changed memory read as unchanged and a cached resource could be reused over new data. The counters, every stored stamp and `noteWrittenRun`'s parameter are now 64-bit, and nothing narrows between the counter, the stored stamps and the 64-bit API values. Block offsets inside a `DriverPiece` stay 32-bit. Each 4 GiB tracking leaf grows from 772 KiB to 1544 KiB; leaves are allocated only once a block in them is stamped. `agc_write_tracking_generation_wrap` moves the generation to just below 2^32 through `AdvanceTrackerGenerationForTest`. It then checks that driver and CPU stores made after the crossing still read as changed, over coarse runs, single blocks, CPU pages, driver pieces and `ChangedBlocks`. Noticed while working on Demon's Souls (PPSA01341) on a local branch; the change is generic. ### Tested <!-- OS, title or homebrew, what was checked. "Not tested" is a valid answer. A performance change: the summary lines of main and of this PR from the same machine and scene, with the traces attached, captured as the Performance report issue form describes. --> Windows 11 Pro 10.0.26200, WinLibs GCC 15.2.0 r7, Release, NVIDIA GeForce RTX 4070 (driver 610.88). Branch based on `main` `2c28d327e3`; head `bcda8554da`. - Focused tests, `ctest -R '^(agc_write_tracking|agc_resident_direct_remap|agc_rewatched_direct_shared|agc_import_alias_shared|agc_compared_shared_generations|agc_collect_mapping_history|agc_draw_snapshot_epoch|agc_driver_store_over_untracked|agc_driver_untracked_snapshot_hint|agc_driver_write_data_written_ahead|agc_private_collect|agc_shared_write_partial_collect|agc_unchanged_cpu_stamps|agc_driver_resource_cache_collect_refusals|agc_screenshot)' -V -j2` with `ANYPS5_REQUIRE_VULKAN=1`, `ANYPS5_NO_SHADER_CACHE=1` and `VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation`: `main` `2c28d327e3` 23/23 pass; head `bcda8554da` 24/24 pass, including the new test. Neither log contains `VUID-` or a missing-validation-layer message. - Red/green: with `main`'s GuestMemory.cpp (32-bit tracker, plus only the test hook), `agc_write_tracking_tests --generation-wrap` fails with `the tracker generation did not cross 2^32`; with this change it passes. - `python tools/check_conventions.py --base upstream/main` passes. Overlap: `tools/pr_overlap.py --branch` found no shared lines with any open pull request it could compare. It could not compare #5021, #6187, #6188, #6192, #6209, #6273, #7048, #7604, #7642 and #12840, which conflict with `main`. They share GuestMemory.cpp/.hpp, and #6273, #7048 and #7604 also share WriteTracking.cpp. Checked by hand: none of #6187, #6192, #7048 and #7604 touches the tracker's stamp declarations, and #12840 is a BDA heap-mirror write-back. If one of them lands first, I'll rebase. ### 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 under Silent stubs in their library's [technical debt](https://github.com/boykopovar/AnyPS5/tree/main/docs/dev/debt) file - [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: none <!-- #PR, or none --> - [x] Fixes: none <!-- issue it closes as Fixes #N, or none --> - [x] Unblocks: none <!-- title ID and Game bring-up discussion, e.g. PPSA01325 #3232, or none --> - [x] GPUs tested: NVIDIA GeForce RTX 4070, driver 610.88, Windows 11 Pro 10.0.26200 <!-- for AGC, shader recompiler or VideoOut changes: vendor, model, driver and OS of each GPU the GPU tests ran on; otherwise n/a --> - [x] AI-assisted: yes (Claude Code) <!-- yes / no --> 🤖 Generated with [Claude Code](https://claude.com/claude-code)
BunnyFl commented 2026-11-02 02:03:05 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by BunnyFl on 2026-11-02T02:03:05Z.

Review: APPROVED


Windows 11, WinLibs GCC 15.2.0 r7, Release, AMD Radeon RX 9070 XT, ANYPS5_REQUIRE_VULKAN=1 ANYPS5_NO_SHADER_CACHE=1 VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation. Head bcda8554da merged into upstream/main 93dbd0f82a:

  • ctest -R '^agc_write_tracking': 5/5 pass, including agc_write_tracking_generation_wrap.
  • Negative control: main's GuestMemory.cpp (32-bit tracker), with only your AdvanceTrackerGenerationForTest added so the test links. agc_write_tracking_generation_wrap fails (the tracker generation did not cross 2^32), and the other four pass. It fails at the crossing check rather than at a stale "unchanged" answer, because the 32-bit counter can't hold the advanced value. That still shows main can't represent a post-wrap generation.

Read against main: noteWrittenRun's parameter and every stored stamp are now 64-bit, so nothing narrows between the counter and the 64-bit API values. The doubled leaf (772 KiB to 1544 KiB per 4 GiB) is only paid for leaves that are actually stamped.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5731392293 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/14899#pullrequestreview-5731392293). Originally posted by [BunnyFl](https://github.com/BunnyFl) on 2026-11-02T02:03:05Z. **Review:** APPROVED --- Windows 11, WinLibs GCC 15.2.0 r7, Release, AMD Radeon RX 9070 XT, `ANYPS5_REQUIRE_VULKAN=1 ANYPS5_NO_SHADER_CACHE=1 VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation`. Head bcda8554da merged into upstream/main 93dbd0f82a: - `ctest -R '^agc_write_tracking'`: 5/5 pass, including `agc_write_tracking_generation_wrap`. - Negative control: main's `GuestMemory.cpp` (32-bit tracker), with only your `AdvanceTrackerGenerationForTest` added so the test links. `agc_write_tracking_generation_wrap` fails (`the tracker generation did not cross 2^32`), and the other four pass. It fails at the crossing check rather than at a stale "unchanged" answer, because the 32-bit counter can't hold the advanced value. That still shows main can't represent a post-wrap generation. Read against main: `noteWrittenRun`'s parameter and every stored stamp are now 64-bit, so nothing narrows between the counter and the 64-bit API values. The doubled leaf (772 KiB to 1544 KiB per 4 GiB) is only paid for leaves that are actually stamped.
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!14899
No description provided.