BDA write tracking (note_bda_write) costs ~48% of a write-heavy dispatch; proposal: per-range written bitmap #22372

Open
opened 2026-11-17 13:14:03 +00:00 by Ketz7 · 0 comments
Ketz7 commented 2026-11-17 13:14:03 +00:00 (Migrated from github.com)
Contributor

Imported from boykopovar/AnyPS5. Originally posted by Ketz7 on 2026-11-17T13:14:03Z.


Problem

Every BDA store and atomic calls note_bda_write(address) after the store. It looks the page's 128 KiB chunk up in a 2048-slot hash table in the fault buffer: up to 8 probes, each an atomic load and CAS, then an AtomicIAdd on the list count and an atomic OR on the page mask. A dispatch that writes more than 2048 chunks hits the host Require ("more than 2048 chunks … not implemented"), and writable ranges must end below WrittenAddressLimit.

Measurement

vkQuake's BVH builder (compute shader 0x20c612000), replayed from a capture on an RX 7900 XT, 2 rounds each:

Variant Time (s)
main (note_bda_write) 119.0 / 126.6
one atomic OR per write 63.3 / 63.1
one atomic OR + one store of a flag 63.6 / 65.6
no write tracking at all (stub) 61.6 / 65.1

So write tracking is about half of this dispatch's time, and a single OR per write recovers almost all of it.

Proposal

  • Fault buffer layout.
    • FaultBufferBytes and SrtGuardWord stay where they are.
    • The overflow word becomes a single dirty flag.
    • The hash slots and list region (6,145 words) become a pool of per-range bitmaps.
  • Table build. When the host builds the table, it gives each writable Range a block in that pool, encoded in Range.reserved (renamed written): the block's word offset plus a granule shift.
    • The shift is 12 (one bit per 4 KiB page) by default.
    • It goes coarser only for the largest ranges, and only when the pool would otherwise overflow.
    • The assignment depends only on the ranges, so cached and shared tables stay valid.
  • Shader side. The write lookups (get_bda_write_pointer, probe_bda_write_pointer, get_bda_atomic_pointer) already find the range and know [address, address + bytes). After their checks pass, they:
    • set the bit for the first and the last granule, which is one AtomicOr, or two when the bits are in different words;
    • store 1 to the dirty flag.
    • note_bda_write and all its call sites go away. Stores that cross a page are also covered: today only the first page is noted, except on the wide-store path.
  • Host side. markWrittenPages runs when the dirty flag is set:
    • it walks the writable ranges' blocks and calls GuestMemory::MarkPossiblyWritten for each run of set bits;
    • it clears what it read.
    • faultSlotClean keeps checking only the head words, since a set bit implies a set flag.
  • ABI and limits.
    • The 2048-chunk limit and WrittenAddressLimit are removed.
    • The new limit is about 6,000 writable ranges per table, and a coarser granule over large ranges (it only marks more pages as possibly written, never fewer).
    • BdaAbi::Version goes to 2.

Questions before I open the PR

  • Is it OK to give Range.reserved a meaning? Bumping the ABI version invalidates cached shaders.
  • Is marking before the store acceptable? The marking happens in the lookup, and the probe lookup can mark a page that the dword fallback then writes anyway. Possibly-written stays conservative.
  • Do you know of titles with more than a few thousand writable ranges in one table? If so, I'd add a shared fallback block rather than a Require.

AI-assisted: yes

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:6445365615 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/issues/22372). Originally posted by [Ketz7](https://github.com/Ketz7) on 2026-11-17T13:14:03Z. --- ### Problem Every BDA store and atomic calls `note_bda_write(address)` after the store. It looks the page's 128 KiB chunk up in a 2048-slot hash table in the fault buffer: up to 8 probes, each an atomic load and CAS, then an `AtomicIAdd` on the list count and an atomic OR on the page mask. A dispatch that writes more than 2048 chunks hits the host `Require` ("more than 2048 chunks … not implemented"), and writable ranges must end below `WrittenAddressLimit`. ### Measurement vkQuake's BVH builder (compute shader 0x20c612000), replayed from a capture on an RX 7900 XT, 2 rounds each: | Variant | Time (s) | |---|---| | main (`note_bda_write`) | 119.0 / 126.6 | | one atomic OR per write | 63.3 / 63.1 | | one atomic OR + one store of a flag | 63.6 / 65.6 | | no write tracking at all (stub) | 61.6 / 65.1 | So write tracking is about half of this dispatch's time, and a single OR per write recovers almost all of it. ### Proposal - **Fault buffer layout.** - `FaultBufferBytes` and `SrtGuardWord` stay where they are. - The overflow word becomes a single dirty flag. - The hash slots and list region (6,145 words) become a pool of per-range bitmaps. - **Table build.** When the host builds the table, it gives each writable `Range` a block in that pool, encoded in `Range.reserved` (renamed `written`): the block's word offset plus a granule shift. - The shift is 12 (one bit per 4 KiB page) by default. - It goes coarser only for the largest ranges, and only when the pool would otherwise overflow. - The assignment depends only on the ranges, so cached and shared tables stay valid. - **Shader side.** The write lookups (`get_bda_write_pointer`, `probe_bda_write_pointer`, `get_bda_atomic_pointer`) already find the range and know `[address, address + bytes)`. After their checks pass, they: - set the bit for the first and the last granule, which is one `AtomicOr`, or two when the bits are in different words; - store 1 to the dirty flag. - `note_bda_write` and all its call sites go away. Stores that cross a page are also covered: today only the first page is noted, except on the wide-store path. - **Host side.** `markWrittenPages` runs when the dirty flag is set: - it walks the writable ranges' blocks and calls `GuestMemory::MarkPossiblyWritten` for each run of set bits; - it clears what it read. - `faultSlotClean` keeps checking only the head words, since a set bit implies a set flag. - **ABI and limits.** - The 2048-chunk limit and `WrittenAddressLimit` are removed. - The new limit is about 6,000 writable ranges per table, and a coarser granule over large ranges (it only marks more pages as possibly written, never fewer). - `BdaAbi::Version` goes to 2. ### Questions before I open the PR - Is it OK to give `Range.reserved` a meaning? Bumping the ABI version invalidates cached shaders. - Is marking before the store acceptable? The marking happens in the lookup, and the probe lookup can mark a page that the dword fallback then writes anyway. Possibly-written stays conservative. - Do you know of titles with more than a few thousand writable ranges in one table? If so, I'd add a shared fallback block rather than a `Require`. AI-assisted: yes
Sign in to join this conversation.
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#22372
No description provided.