[PR #1887] [MERGED] d3d12: track live console-heap V# slots in a host-RAM bitset (RT-CONSOLE-HEAP-VSHARP-BITSET) #1900

Closed
opened 2026-11-26 21:19:20 +01:00 by 4ld0 · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/ubiquitarianism/leonida/pull/1887
Author: @IlyassRS3
Created: 11/25/2026
Status: ✅ Merged
Merged: 11/25/2026
Merged by: @aslan2142

Base: main ← Head: rt-console-heap-vsharp-bitset


📝 Commits (1)

  • 3233b98 d3d12: track live console-heap V# slots in a host-RAM bitset

📊 Changes

4 files changed (+392 additions, -9 deletions)

View changed files

📝 runtime/CMakeLists.txt (+6 -0)
📝 runtime/shims/d3d12_vulkan_device.cpp (+12 -9)
➕ runtime/src/console_heap_vsharp_slots.h (+115 -0)
➕ runtime/test/console_heap_vsharp_slots_test.cpp (+259 -0)

📄 Description

Item: RT-CONSOLE-HEAP-VSHARP-BITSET
Claim: https://github.com/ubiquitarianism/leonida/issues/1608#issuecomment-7164694343

Summary

Eliminates uncached write-combined GPU memory reads from heap.copy during console resource-heap invalidation scans in refresh_console_heap() (runtime/shims/d3d12_vulkan_device.cpp).

  • Root cause: Whenever released_backings or title_va_aliases().invalidations() advanced, refresh_console_heap() scanned every 32-byte slot of heap.copy linearly (for (size_t at = 0; at + kDescriptorSize <= heap.bytes; at += kDescriptorSize)) checking !(w[3] >> 30) && (w[2] || w[6]). When heap.copy falls back to arena.mapped (uncached WC GPU memory — e.g. when import_console_heap cannot land within 4 GiB of arena.address or host-memory import is unavailable), reading 4–5.5+ MiB of WC memory triggers ~130k–180k uncached PCIe reads per buffer release (hundreds of ms/submission); even in cached scratch memory it linearly scans all empty and T# slots.
  • Fix:
    • Add portable header-only leonida::ConsoleHeapVsharpSlots (runtime/src/console_heap_vsharp_slots.h), a dense host-RAM uint64_t bitset (~22 KiB for a 5.5 MiB heap, L1-resident) with O(1) empty() fast path, clear_range() for zeroed page runs, std::countr_zero iteration safe against in-place bit clears inside the callback, and is_live_vsharp(w) matching the exact legacy predicate !(w[3] >> 30) && (w[2] || w[6]).
    • Keep heap.live_vsharps synchronized at every resource-heap write to heap.copy: add_console_heap (reset), fix_console_slot (set), fix_console_slots deferred zeroing (clear), and for_each_zeroed (clear_range).
    • Walk heap.live_vsharps.for_each(...) in refresh_console_heap instead of reading heap.copy.

No overlap with #1728 or #1867.

Verification

  • AI-assisted: yes
  • GPUs tested: none (CPU-only macOS arm64 host; d3d12_vulkan_device.cpp is Windows-only and verified by inspection + Windows CI)
  • New portable unit test runtime/test/console_heap_vsharp_slots_test.cpp (CTest console_heap_vsharp_slots), including a 4,000-step differential model against a linear 32-byte slot buffer (fix_console_slot, not_tsharp, deferred zeroing, clear_range, in-place invalidation clears) and 5.5 MiB sparse heap check:
    • clang++ -std=c++20 -Wall -Wextra -Werror -O1 -g -fsanitize=address,undefined: passed
    • clang++ -std=c++20 -Wall -Wextra -Werror -O1 -g -fsanitize=thread: passed
    • clang++ -std=c++20 -Wall -Wextra -Werror -O2 (3 runs): passed
  • Synthetic cached-RAM microbenchmark (5.5 MiB heap = 180,224 slots, 2,048 live V#s, -O2 on Apple Silicon): linear scan 60.30 us vs bitset 0.93 us (64.9x faster even in cached RAM; eliminates 100% of WC reads from heap.copy when heap.copy is in arena.mapped)
  • python3 -I scripts/check_no_game_data.py on all changed files: passed
  • python3 -I tests/test_text_write_line_endings.py: passed (3 tests)

🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/ubiquitarianism/leonida/pull/1887 **Author:** [@IlyassRS3](https://github.com/IlyassRS3) **Created:** 11/25/2026 **Status:** ✅ Merged **Merged:** 11/25/2026 **Merged by:** [@aslan2142](https://github.com/aslan2142) **Base:** `main` ← **Head:** `rt-console-heap-vsharp-bitset` --- ### 📝 Commits (1) - [`3233b98`](https://github.com/ubiquitarianism/leonida/commit/3233b9857fc299eccff5b26820e42cadd258e12b) d3d12: track live console-heap V# slots in a host-RAM bitset ### 📊 Changes **4 files changed** (+392 additions, -9 deletions) <details> <summary>View changed files</summary> 📝 `runtime/CMakeLists.txt` (+6 -0) 📝 `runtime/shims/d3d12_vulkan_device.cpp` (+12 -9) ➕ `runtime/src/console_heap_vsharp_slots.h` (+115 -0) ➕ `runtime/test/console_heap_vsharp_slots_test.cpp` (+259 -0) </details> ### 📄 Description Item: `RT-CONSOLE-HEAP-VSHARP-BITSET` Claim: https://github.com/ubiquitarianism/leonida/issues/1608#issuecomment-7164694343 ### Summary Eliminates uncached write-combined GPU memory reads from `heap.copy` during console resource-heap invalidation scans in `refresh_console_heap()` (`runtime/shims/d3d12_vulkan_device.cpp`). - **Root cause**: Whenever `released_backings` or `title_va_aliases().invalidations()` advanced, `refresh_console_heap()` scanned every 32-byte slot of `heap.copy` linearly (`for (size_t at = 0; at + kDescriptorSize <= heap.bytes; at += kDescriptorSize)`) checking `!(w[3] >> 30) && (w[2] || w[6])`. When `heap.copy` falls back to `arena.mapped` (uncached WC GPU memory — e.g. when `import_console_heap` cannot land within 4 GiB of `arena.address` or host-memory import is unavailable), reading 4–5.5+ MiB of WC memory triggers ~130k–180k uncached PCIe reads per buffer release (hundreds of ms/submission); even in cached scratch memory it linearly scans all empty and T# slots. - **Fix**: - Add portable header-only `leonida::ConsoleHeapVsharpSlots` (`runtime/src/console_heap_vsharp_slots.h`), a dense host-RAM `uint64_t` bitset (~22 KiB for a 5.5 MiB heap, L1-resident) with O(1) `empty()` fast path, `clear_range()` for zeroed page runs, `std::countr_zero` iteration safe against in-place bit clears inside the callback, and `is_live_vsharp(w)` matching the exact legacy predicate `!(w[3] >> 30) && (w[2] || w[6])`. - Keep `heap.live_vsharps` synchronized at every resource-heap write to `heap.copy`: `add_console_heap` (`reset`), `fix_console_slot` (`set`), `fix_console_slots` deferred zeroing (`clear`), and `for_each_zeroed` (`clear_range`). - Walk `heap.live_vsharps.for_each(...)` in `refresh_console_heap` instead of reading `heap.copy`. No overlap with `#1728` or `#1867`. ### Verification - **AI-assisted**: yes - **GPUs tested**: none (CPU-only macOS arm64 host; `d3d12_vulkan_device.cpp` is Windows-only and verified by inspection + Windows CI) - New portable unit test `runtime/test/console_heap_vsharp_slots_test.cpp` (CTest `console_heap_vsharp_slots`), including a 4,000-step differential model against a linear 32-byte slot buffer (`fix_console_slot`, `not_tsharp`, deferred zeroing, `clear_range`, in-place invalidation clears) and 5.5 MiB sparse heap check: - `clang++ -std=c++20 -Wall -Wextra -Werror -O1 -g -fsanitize=address,undefined`: passed - `clang++ -std=c++20 -Wall -Wextra -Werror -O1 -g -fsanitize=thread`: passed - `clang++ -std=c++20 -Wall -Wextra -Werror -O2` (3 runs): passed - Synthetic cached-RAM microbenchmark (5.5 MiB heap = 180,224 slots, 2,048 live V#s, `-O2` on Apple Silicon): linear scan `60.30 us` vs bitset `0.93 us` (**64.9x faster** even in cached RAM; eliminates 100% of WC reads from `heap.copy` when `heap.copy` is in `arena.mapped`) - `python3 -I scripts/check_no_game_data.py` on all changed files: passed - `python3 -I tests/test_text_write_line_endings.py`: passed (3 tests) --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
4ld0 2026-11-26 21:19:20 +01:00
Sign in to join this conversation.
No labels
bug
pull-request
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
starred/leonida#1900
No description provided.