WIP: hwrt: empty the emptiest BLAS heap into the others and give it back (DARNER_HW_RT_HEAP_EVACUATE, off by default) #453

Draft
Layne wants to merge 2 commits from Layne/Darner:pr/hwrt-heap-evacuate into Main
First-time contributor

Not run in the game on this branch. It is a port, onto main, of a change that ran for one hour on a fork with other local changes. The port differs from that version in ways that matter (see "Differences from the fork's version"). The switch is off by default, and nothing changes while it is unset.

Depends on #452 (DARNER_HW_RT_COMPACT). It reuses that PR's per-structure address list (HostAs::keys), its build generation counter and its per-frame share (compact_pick). The two switches work independently at run time, but the code is stacked. Keep this PR in draft until that one is merged.

Contributor role (optional)

  • Fluxer ID: Layne#3304

What it changes

  • The problem on main: read from the code, and measured on a fork. The shared BLAS heaps (64 MB each) are kept for the whole run. The units that BLASes give back (evicted, rebuilt elsewhere, or compacted) end up scattered over every heap, and a few structures that stay keep each heap alive. On an RTX 4070 Ti (12 GB, a fork, 18 minutes), 24-26 heaps (1,536-1,664 MB) held 960-1,130 MB of units in use, 60-71 % full.

With DARNER_HW_RT_HEAP_EVACUATE=1 (and the BLAS heaps on, as by default), once a frame in blas_work, after the compaction:

  • The heap with the fewest units in use is chosen to be emptied, but only if the other heaps that hold something have room for all of it plus 128 MB to spare. It is marked as draining: new BLASes take units from it only if no other heap has room, before a new heap would be made.
  • Its BLASes that were built without update and have been left alone (not built into, refitted or read by a refit) for 30 frames are cloned (COPY_MODE_CLONE) into buffers of the same size in other heaps that already hold something. No new heap and no committed buffer is made for them. At most 16 BLASes (8 MB) move per frame, largest first. A BLAS waiting for compaction is left to the compaction.
  • A BLAS moves only under the same conditions as a compaction, checked under the lookup table's write lock: the table still points at it, top levels are already given that very buffer (shown, so the list that built or copied it has been submitted and runs before the copy on the one queue), and nothing but the table holds it. The holder count is read before the generation counter, behind an Acquire fence, so a build recorded into it since the look is caught.
  • Every console address that pointed at the old buffer then points at the new one. Top levels get the new one from the copying list's submission, and the old buffer is released past the fences with its last holder.
  • Once nothing is placed in the heap any more, it is removed from the heap list under that list's lock (where units are taken, so none can be taken from it meanwhile) and released past the fences. A heap whose units in use stop going down for 600 frames is given up and not chosen again for 3,600 frames.
  • One line every five seconds: hardware RT BLAS heaps emptied (...), giving heaps chosen, emptied, let go and given up, BLASes moved (count and MB), BLASes with no room elsewhere, BLASes built into since the look, and the CPU time per frame.
  • create_blas_buffer now also returns the heap the buffer was placed in, and take_units gets a grow flag (false for a moved structure: only non-empty, non-draining heaps).

Why updatable BLASes are not moved

On main a refit reads its source from the lookup table at record time, and lists are recorded on several threads. After a move, the table points at the new buffer before the copying list is submitted, so a refit in a list submitted earlier would read a buffer that has not been written yet. The fork's version moved updatable BLASes too, but it was safe only because of a local "refit only from a submitted source" guard. #446 (DARNER_HW_RT_REFIT_BUILT) is that guard upstream. If #446 lands, updatable BLASes could be moved as well: the new HostAs would get built = false, set when the copying list is submitted. With #445 (DARNER_HW_RT_REFIT_COMMITTED, on by default) updatable BLASes are in committed buffers and never in the heaps, so this restriction costs nothing there.

Relation to other pull requests

  • #318 (BLAS heap size classes, fullest-fit, release of empty heaps beyond one spare): overlaps. Both give heaps back and both change take_units/place_blas_buffer. #318 releases heaps that empty by themselves. This PR empties one on purpose and releases it. If #318 is merged first, this PR should be rebased onto it: choose the victim within a size class, use #318's release, and keep only the moving and the draining mark. Not meant to go in before #318 is decided.
  • #445: compatible in behaviour (see above). Textual conflict in create_blas_buffer (both change its signature): it would take both the refittable argument and the returned heap.
  • #446: compatible. No moved structure is a refit source. Textual conflict on the HostAs fields and constructors: the moved HostAs gets built: true (its source was already submitted, as shown proves).
  • #268: not needed (the generation counter is used instead of lists holding Arc<HostAs>).

Differences from the fork's version

  • Only BLASes built without update are moved (see above).
  • The release of the emptied heap is in this PR (evacuate_victim). The fork relied on a separate local heap-packing change that is not upstream.
  • No timing on the hot path of take_units (the fork timed every placement: 115-750 us/frame of measuring overhead in its log).
  • No slab support (the fork's slabs are not upstream).

How it was tested

  • Tested revision: df1b41b98 (on 7d95441b6, #452, on main 47d21cd3b).

  • Commands and results:

    • cargo check --release -p darner-gpu: finished, no warnings
    • cargo clippy -p darner-gpu --all-targets --locked -- -D warnings: clean
    • cargo test --release -p darner-gpu --lib hwrt: 36 passed, 0 failed (5 new)
    • New unit tests: the room the other heaps offer (never an empty heap), the fit with slack, the choice of the emptiest heap that fits (barred heaps skipped, newest of equals), giving a heap up once it stops going down, and the 30-frame wait.
  • Changes a default: no (off unless DARNER_HW_RT_HEAP_EVACUATE=1).

  • How the switch was checked: the fork's version ran for one hour on an RTX 4070 Ti 12 GB (Series X profile, 1440p, DLSS Quality, frame generation x2, hardware RT, 33.3 ms frame interval, with DARNER_HW_RT_COMPACT=1 and other local memory changes). At the end of the hour: heaps chosen 668, emptied 644, given up 23; BLASes moved 41,234 (7,458 MB); no room elsewhere 586; 645 heaps given back, but 677 made, ending at 32 heaps (2,048 MB) for 1,358 MB of units in use (66 %) and 1,054 MB live. The card ledger showed accel 2,068 MB. There is no device-removed line in that log. That run did not show a smaller steady-state heap count than the 18-minute run without evacuation (24-26 heaps), and heaps were made and released about every 5.5 s. An A/B on the same route is needed before any claim of saving.

    A (off) B (on)
    heaps held / MB in use after N minutes not measured on this branch not measured on this branch
  • Gates run on that revision: none of the three full gates. Only the narrower checks above.

  • Hardware dependence: may depend on VRAM size and driver (heap creation cost, release timing).

  • Coverage gaps: the GPU path (evacuate_work, heap release) has no unit test. Not run on this branch, and not with the D3D12 debug layer.

Risks

  • Churn: heaps can be emptied and then made again soon after (one every ~5.5 s in the fork's hour). Each made heap is a 64 MB CreateHeap.
  • Up to 8 MB of BLAS copies a frame on the card, on top of the compaction's.
  • CPU: once a frame, a look at the BLASes of one heap (its index, not the whole table), one write-locked pass over up to 16 of them, and one pass over shown when the copying list is submitted. The fork measured roughly 0.1-1 ms per frame for the evacuation work in total.
  • A list that records copies but is never submitted leaves the lookup table pointing at an unwritten buffer. Top levels never use it (they read shown), and it is never refitted. The next full build replaces it.

How to test

  1. Build, then run with DARNER_HW_RT=1 DARNER_HW_RT_HEAP_EVACUATE=1, with and without DARNER_HW_RT_COMPACT=1.
  2. In the log, watch hardware RT BLAS heaps emptied (let go, BLASes moved) and hardware RT BLAS buffers (heaps, MB in use).
  3. A/B on the same route: switch off, then on. Compare the heap count and MB held after 10, 30 and 60 minutes, the card ledger kinds accel value, and the frame times. Watch for device loss, TDRs, and missing or flickering geometry in ray-traced shadows and reflections.

Checklist

  • I ran this change in the game, or say below why it can't be (docs, tests only). A fork's version ran; this port has not.
  • This PR contains one change based on main. Stacked on #452.
  • The diff follows docs/asset-policy.md.
  • I recorded this PR's dependencies below.
    • Depends on #452 (DARNER_HW_RT_COMPACT): shares HostAs::keys, generation and compact_pick.

Checked for overlap

  • I searched the open pull requests for the same issue and for the files this touches.
  • Related PRs: #318 (empty heaps released, size classes), #445 (refittable BLASes in committed buffers), #446 (refits only from submitted sources), #268
  • Replaces or overlaps: overlaps #318 (heap release). This one moves structures out of a chosen heap; #318 only releases heaps that empty by themselves.

🤖 Generated with Claude Code

**Not run in the game on this branch.** It is a port, onto `main`, of a change that ran for one hour on a fork with other local changes. The port differs from that version in ways that matter (see "Differences from the fork's version"). The switch is off by default, and nothing changes while it is unset. **Depends on #452 (`DARNER_HW_RT_COMPACT`).** It reuses that PR's per-structure address list (`HostAs::keys`), its build `generation` counter and its per-frame share (`compact_pick`). The two switches work independently at run time, but the code is stacked. Keep this PR in draft until that one is merged. ## Contributor role (optional) - Fluxer ID: Layne#3304 ## What it changes - The problem on main: read from the code, and measured on a fork. The shared BLAS heaps (64 MB each) are kept for the whole run. The units that BLASes give back (evicted, rebuilt elsewhere, or compacted) end up scattered over every heap, and a few structures that stay keep each heap alive. On an RTX 4070 Ti (12 GB, a fork, 18 minutes), 24-26 heaps (1,536-1,664 MB) held 960-1,130 MB of units in use, 60-71 % full. With `DARNER_HW_RT_HEAP_EVACUATE=1` (and the BLAS heaps on, as by default), once a frame in `blas_work`, after the compaction: - The heap with the fewest units in use is chosen to be emptied, but only if the other heaps that hold something have room for all of it plus 128 MB to spare. It is marked as draining: new BLASes take units from it only if no other heap has room, before a new heap would be made. - Its BLASes that were **built without update** and have been left alone (not built into, refitted or read by a refit) for 30 frames are cloned (`COPY_MODE_CLONE`) into buffers of the same size in other heaps that already hold something. No new heap and no committed buffer is made for them. At most 16 BLASes (8 MB) move per frame, largest first. A BLAS waiting for compaction is left to the compaction. - A BLAS moves only under the same conditions as a compaction, checked under the lookup table's write lock: the table still points at it, top levels are already given that very buffer (`shown`, so the list that built or copied it has been submitted and runs before the copy on the one queue), and nothing but the table holds it. The holder count is read before the `generation` counter, behind an Acquire fence, so a build recorded into it since the look is caught. - Every console address that pointed at the old buffer then points at the new one. Top levels get the new one from the copying list's submission, and the old buffer is released past the fences with its last holder. - Once nothing is placed in the heap any more, it is removed from the heap list under that list's lock (where units are taken, so none can be taken from it meanwhile) and released past the fences. A heap whose units in use stop going down for 600 frames is given up and not chosen again for 3,600 frames. - One line every five seconds: `hardware RT BLAS heaps emptied (...)`, giving heaps chosen, emptied, let go and given up, BLASes moved (count and MB), BLASes with no room elsewhere, BLASes built into since the look, and the CPU time per frame. - `create_blas_buffer` now also returns the heap the buffer was placed in, and `take_units` gets a `grow` flag (false for a moved structure: only non-empty, non-draining heaps). ### Why updatable BLASes are not moved On `main` a refit reads its source from the lookup table at record time, and lists are recorded on several threads. After a move, the table points at the new buffer before the copying list is submitted, so a refit in a list submitted earlier would read a buffer that has not been written yet. The fork's version moved updatable BLASes too, but it was safe only because of a local "refit only from a submitted source" guard. **#446** (`DARNER_HW_RT_REFIT_BUILT`) is that guard upstream. If #446 lands, updatable BLASes could be moved as well: the new `HostAs` would get `built = false`, set when the copying list is submitted. With **#445** (`DARNER_HW_RT_REFIT_COMMITTED`, on by default) updatable BLASes are in committed buffers and never in the heaps, so this restriction costs nothing there. ### Relation to other pull requests - **#318** (BLAS heap size classes, fullest-fit, release of empty heaps beyond one spare): overlaps. Both give heaps back and both change `take_units`/`place_blas_buffer`. #318 releases heaps that empty by themselves. This PR empties one on purpose and releases it. If #318 is merged first, this PR should be rebased onto it: choose the victim within a size class, use #318's release, and keep only the moving and the draining mark. Not meant to go in before #318 is decided. - **#445**: compatible in behaviour (see above). Textual conflict in `create_blas_buffer` (both change its signature): it would take both the `refittable` argument and the returned heap. - **#446**: compatible. No moved structure is a refit source. Textual conflict on the `HostAs` fields and constructors: the moved `HostAs` gets `built: true` (its source was already submitted, as `shown` proves). - **#268**: not needed (the `generation` counter is used instead of lists holding `Arc<HostAs>`). ### Differences from the fork's version - Only BLASes built without update are moved (see above). - The release of the emptied heap is in this PR (`evacuate_victim`). The fork relied on a separate local heap-packing change that is not upstream. - No timing on the hot path of `take_units` (the fork timed every placement: 115-750 us/frame of measuring overhead in its log). - No slab support (the fork's slabs are not upstream). ## How it was tested - Tested revision: df1b41b98 (on 7d95441b6, #452, on `main` 47d21cd3b). - Commands and results: - `cargo check --release -p darner-gpu`: finished, no warnings - `cargo clippy -p darner-gpu --all-targets --locked -- -D warnings`: clean - `cargo test --release -p darner-gpu --lib hwrt`: 36 passed, 0 failed (5 new) - New unit tests: the room the other heaps offer (never an empty heap), the fit with slack, the choice of the emptiest heap that fits (barred heaps skipped, newest of equals), giving a heap up once it stops going down, and the 30-frame wait. - Changes a default: no (off unless `DARNER_HW_RT_HEAP_EVACUATE=1`). - How the switch was checked: the fork's version ran for one hour on an RTX 4070 Ti 12 GB (Series X profile, 1440p, DLSS Quality, frame generation x2, hardware RT, 33.3 ms frame interval, with `DARNER_HW_RT_COMPACT=1` and other local memory changes). At the end of the hour: heaps chosen 668, emptied 644, given up 23; BLASes moved 41,234 (7,458 MB); no room elsewhere 586; 645 heaps given back, but 677 made, ending at 32 heaps (2,048 MB) for 1,358 MB of units in use (66 %) and 1,054 MB live. The card ledger showed `accel` 2,068 MB. There is no device-removed line in that log. That run did **not** show a smaller steady-state heap count than the 18-minute run without evacuation (24-26 heaps), and heaps were made and released about every 5.5 s. An A/B on the same route is needed before any claim of saving. | | A (off) | B (on) | |---|---|---| | heaps held / MB in use after N minutes | not measured on this branch | not measured on this branch | - Gates run on that revision: none of the three full gates. Only the narrower checks above. - Hardware dependence: may depend on VRAM size and driver (heap creation cost, release timing). - Coverage gaps: the GPU path (`evacuate_work`, heap release) has no unit test. Not run on this branch, and not with the D3D12 debug layer. ### Risks - Churn: heaps can be emptied and then made again soon after (one every ~5.5 s in the fork's hour). Each made heap is a 64 MB `CreateHeap`. - Up to 8 MB of BLAS copies a frame on the card, on top of the compaction's. - CPU: once a frame, a look at the BLASes of one heap (its index, not the whole table), one write-locked pass over up to 16 of them, and one pass over `shown` when the copying list is submitted. The fork measured roughly 0.1-1 ms per frame for the evacuation work in total. - A list that records copies but is never submitted leaves the lookup table pointing at an unwritten buffer. Top levels never use it (they read `shown`), and it is never refitted. The next full build replaces it. ## How to test 1. Build, then run with `DARNER_HW_RT=1 DARNER_HW_RT_HEAP_EVACUATE=1`, with and without `DARNER_HW_RT_COMPACT=1`. 2. In the log, watch `hardware RT BLAS heaps emptied` (`let go`, `BLASes moved`) and `hardware RT BLAS buffers` (heaps, MB in use). 3. A/B on the same route: switch off, then on. Compare the heap count and MB held after 10, 30 and 60 minutes, the `card ledger kinds` `accel` value, and the frame times. Watch for device loss, TDRs, and missing or flickering geometry in ray-traced shadows and reflections. ## Checklist - [ ] I ran this change in the game, or say below why it can't be (docs, tests only). *A fork's version ran; this port has not.* - [x] This PR contains one change based on `main`. *Stacked on #452.* - [x] The diff follows `docs/asset-policy.md`. - [x] I recorded this PR's dependencies below. - Depends on #452 (`DARNER_HW_RT_COMPACT`): shares `HostAs::keys`, `generation` and `compact_pick`. ## Checked for overlap - [ ] I searched the open pull requests for the same issue and for the files this touches. - Related PRs: #318 (empty heaps released, size classes), #445 (refittable BLASes in committed buffers), #446 (refits only from submitted sources), #268 - Replaces or overlaps: overlaps #318 (heap release). This one moves structures out of a chosen heap; #318 only releases heaps that empty by themselves. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
A BLAS keeps the buffer of its largest possible size; the title's own
compaction only aliases a second console address to the same host buffer.
With DARNER_HW_RT_COMPACT=1, BLASes built with neither ALLOW_UPDATE nor
PERFORM_UPDATE get ALLOW_COMPACTION, their compacted size is read back once
the building list is reset, and after 120 unchanged frames those that save a
whole 64 KiB unit are copied (COPY_MODE_COMPACT) into a buffer of that size,
at most 16 (8 MB) a frame. Every console address naming the old buffer names
the new one (per-structure address list, no table walk); top levels get it
from the copying list's submission (shown); the old buffer retires past the
fences with its last holder. A build in place is caught by a per-structure
generation counter, read after the holder count behind an Acquire fence.
Never refitted structures only, so no refit reads an unsubmitted copy.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
The shared BLAS heaps are kept for the whole run, and units freed by
evicted, rebuilt or compacted BLASes lie scattered over every heap. With
DARNER_HW_RT_HEAP_EVACUATE=1, once a frame the heap with the fewest units
in use, if the other non-empty heaps can take all of it plus 128 MB, is
marked draining (taken from only before a new heap would be made). Its
BLASes built without update and left alone for 30 frames are cloned
(COPY_MODE_CLONE) into the other heaps, at most 16 / 8 MB a frame, under
the same conditions as the compaction (lookup table still names it, shown
already, nothing else holds it, generation unchanged, count read before the
generation behind an Acquire fence). Top levels get the new buffer from the
copying list's submission. Once the heap is empty it is removed from the
heap list under its lock and released past the fences; a heap that stops
emptying for 600 frames is given up for 3600.

Updatable BLASes are not moved: a refit recorded on another thread reads
its source from the lookup table, which names the new buffer before the
copy is submitted.

Builds on the compaction change (shares HostAs::keys, the generation
counter and the per-frame share). create_blas_buffer now also returns the
heap it placed the buffer in.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
TrustNo1 changed title from WIP: [pr-feature][memory][RAYTRACING] hwrt: empty the emptiest BLAS heap into the others and give it back (DARNER_HW_RT_HEAP_EVACUATE, off by default) to WIP: hwrt: empty the emptiest BLAS heap into the others and give it back (DARNER_HW_RT_HEAP_EVACUATE, off by default) 2026-12-06 03:28:50 +00:00
This pull request has changes conflicting with the target branch.
  • crates/darner-gpu/src/hwrt.rs
View command line instructions

Manual merge helper

Use this merge commit message when completing the merge manually.

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u 'pr/hwrt-heap-evacuate:Layne-pr/hwrt-heap-evacuate'
git switch 'Layne-pr/hwrt-heap-evacuate'

Merge

Merge the changes and update on Forgejo.

Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.

git switch 'Main'
git merge --no-ff 'Layne-pr/hwrt-heap-evacuate'
git switch 'Layne-pr/hwrt-heap-evacuate'
git rebase 'Main'
git switch 'Main'
git merge --ff-only 'Layne-pr/hwrt-heap-evacuate'
git switch 'Layne-pr/hwrt-heap-evacuate'
git rebase 'Main'
git switch 'Main'
git merge --no-ff 'Layne-pr/hwrt-heap-evacuate'
git switch 'Main'
git merge --squash 'Layne-pr/hwrt-heap-evacuate'
git switch 'Main'
git merge --ff-only 'Layne-pr/hwrt-heap-evacuate'
git switch 'Main'
git merge 'Layne-pr/hwrt-heap-evacuate'
git push origin 'Main'
Sign in to join this conversation.
No reviewers
No milestone
No project
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.

Dependencies

No dependencies set

Reference
TrustNo1/Darner!453
No description provided.