Skip to content

RT-STREAM-POOL-KEEP-VA-ALIASES: preserve live mappings when mirroring stream pools - #1920

Merged
aslan2142 merged 2 commits into
ProjectLeonida:mainfrom
lixonik:fix/stream-pool-keep-va-aliases
Nov 27, 2026
Merged

aslan2142 merged 2 commits into
ProjectLeonida:mainfrom
lixonik:fix/stream-pool-keep-va-aliases

Conversation

@lixonik

@lixonik lixonik commented Nov 25, 2026

Copy link
Copy Markdown
Contributor

Claimed in the coordination issue as: RT-STREAM-POOL-KEEP-VA-ALIASES

What and why

A stream-pool extent was checked for an existing mapping at its first byte. A live mapping could start later on either side of the pair. Publishing the entire extent then evicted that mapping, even if the new request only needed the free area before it. The runtime now limits new extents to the intersection of the CPU and GPU gaps. Registration checks both sides under one lock and publishes the pair together. Reusing the other view's mapping also stops at the next live range.

If a competing mapping appears while the copy is allocated, that mapping takes precedence. The unused backing follows the device's existing allocation lifetime.

This is on the stream-pool path reported during world start. The effect on a retail boot has not been measured.

Tested

  • MSVC on Windows: title_va_alias_test, title_window_plan_test and title_window_plan_bounds_test passed. The new overlap assertion failed against the original alias table.
  • GCC with UBSan on Linux/WSL: title_va_alias_test and title_window_plan_bounds_test passed.
  • A synthetic harness compiled the actual pool resolver body with mocked allocation and memory queries. The original path allocated 1 MiB and displaced a live neighboring alias. The fixed path used the free 64 KiB and preserved it. A mapping arriving during allocation survived without a partial pair being published.
  • python -I -B scripts/run_semantic_tests.py tests/semantic: 6,261 tests across 489 files, zero failures.
  • Full Windows/Vulkan shim build and retail boot were not run.

GPUs tested: n/a (synthetic CPU tests)

AI-assisted.

Checklist

  • Based on current main and merges cleanly.
  • One topic.
  • No game data, keys or decryption.

@GGandCo

GGandCo commented Nov 25, 2026

Copy link
Copy Markdown
Contributor

Native Windows/Intel verification of #1920 (7cb6962) together with #1926, on main b0f3d39 plus local integration fixes:

  • Intel UHD Graphics, driver 32.0.101.6314, MSVC 14.51, Vulkan SDK 1.4.363.1.
  • title_va_alias, title_window_plan, title_window_plan_bounds and vulkan_title_image_pool: 4/4 passed.
  • xmem, d3d12_xmem_placed (production batch route, no injected mapping hook), d3d12_host_prefix and d3d12_texture_memory: 4/4 passed with LEONIDA_VK_VALIDATION=1.
  • Focused staged plane/BC-array, aliased-guard and mapped single-byte WB/WC readback cases also passed. Host image copies were disabled; on-demand pipelines enabled.

Combined integration, not an isolated A/B of this PR. No successful retail load or performance improvement claimed. The full existing_heap fixture still has a pre-existing first-render-target device-loss failure on this Intel setup, reproduced before these changes; the focused cases above were run separately. No game data included. AI-assisted with OpenAI Codex.

@DadaCap

DadaCap commented Nov 27, 2026 •

Copy link
Copy Markdown
Collaborator

Retail data for this path, from the RT-TITLE-WINDOW-RETIRE work (RTX 5080, NVIDIA 621.24, Windows 11, main d1dff79 plus a local diagnostic that counts windows; AI-assisted, Claude Code). It supports the bug this PR fixes, and it shows one more thing the bounding needs.

The bug, measured. 15-minute run with main's stream-pool path: 4,534 window-backed pool extents, about 2.9 GB in total but only about 1.5 GB of distinct pages; 2,887 overlapped an earlier extent and 2,501 (about 1.6 GB) were orphaned: no alias range answered from them, yet each was still refreshed every submission and charged to the window budget. Only 1 pool request in the run was served by an existing mapping.

What bounding alone does. With extents bounded by the free span in both views (the same rule as this PR), overlaps and orphans went to 0, but a request whose bytes run past the free span gets a mapping shorter than itself: resolve_pool_memory never checks that the extent covers [va, va + bytes) (bytes is only logged). A V# then becomes a zero-size view (vsharp_address refuses room < bytes), and a root view (resolved with kRootViewBytes, 64 KiB) reads and writes past its copy into whatever the arena holds next. Our first build with bounding died at about 160 s with title read faults (it also had a second bug of ours, in retirement, so the two causes were not separated).

What held. Giving such a request one mapping over the request plus every window it reaches (each taken whole, in both views, the replaced windows retired after the next submission), and having TitleVaAliases::resolve / vsharp_address go back to the resolver when the range holding a view's first byte ends before the view does: two 20-minute runs into the world, no fault; 0 overlapping, 0 orphaned windows; about 4,100 windows retired, about 3 GB returned.

That work is on DadaCap/leonida:window-orphans (not opened as a PR, to avoid overlapping this one). It also retires windows that other paths orphan (a slot-133 buffer placed over a window) and keeps a retired window's GPU writes going back to the title. @lixonik, would you rather take the cover part into this PR, or have me open mine on top of yours with Depends on: #1920? I'll go with whichever you prefer. This PR currently shows a conflict with main.

DadaCap identified the short-mapping case in the review of PR ProjectLeonida#1920. Reuse and allocation now require full coverage, preserving both live aliases when a request spans a boundary.
@lixonik
lixonik force-pushed the fix/stream-pool-keep-va-aliases branch from c088fd5 to d78b0e9 Compare November 27, 2026 08:36
@lixonik

lixonik commented Nov 27, 2026

Copy link
Copy Markdown
Contributor Author

@DadaCap Thanks for measuring the overlaps and catching the short mapping. I rebased #1920 onto d1dff7949 and added a length check to both the pool reuse and allocation paths in d78b0e90a. A request that cannot be backed for its full length now fails through the existing unbacked-view path and leaves both live aliases intact.

Please open window-orphans as a follow-up with Depends on: #1920. Your seam-covering path needs the old windows to stay alive through their in-flight submissions and pending GPU write-back. Keeping the covering, retirement and write-back changes together in your PR also keeps your end-to-end regression with the code it exercises.

On Windows 11 / MSVC with an RTX 5070 (610.88), all eight focused CTests passed, including the new synthetic seam cases and window write-back. The two portable tests also passed with GCC/UBSan. Vulkan validation layers were unavailable on this machine.

AI-assisted.

@DadaCap

DadaCap commented Nov 27, 2026

Copy link
Copy Markdown
Collaborator

@lixonik thanks, will do. I'm porting the covering, retirement and write-back changes onto d78b0e90a now (your bounding and length checks kept; the cover replaces the unbacked fallback only for a request that runs into existing windows, which it takes whole and retires after their in-flight submissions and write-back). It will open as a follow-up with Depends on: #1920 after the full ctest and a retail run on top of your branch. AI-assisted (Claude Code).

@DadaCap

DadaCap commented Nov 27, 2026

Copy link
Copy Markdown
Collaborator

@lixonik the follow-up is open: #2465 (Depends on: #1920, built on your d78b0e9 unchanged). Your bounding and both length checks stay; the cover replaces the unbacked fallback only where a request runs into windows, which it takes whole and retires after their in-flight submissions and write-back. All your tests pass unchanged. AI-assisted (Claude Code).

@MoonEyes

Copy link
Copy Markdown
Collaborator

Short A/B of this PR against main on one machine: same recipe, same pipeline cache folder, only the runtime executable changes (my recipe-diff tool reports exactly one difference). n=1 per side. One run per side, so this is not conclusive.

RUN w78 | 2026-11-27 | MoonEyes | AI-assisted: Claude Code (Claude Opus 5.5)
BASE:        main ec91d695c + PR #1920 (head d78b0e90a), merged locally | shaders: main's own translation, retranslated locally
COMPARED TO: RUN w77 = main ec91d695c alone, same short recipe, same cache folder (control)
MACHINE:     RTX 4060 8 GB, desktop, only GPU, display attached | NVIDIA 610.88 | Windows 11 Pro 26300 | Ryzen 5 3600 | 32 GB
RECIPE:      docs/running.md Windows NVIDIA recipe + Proton NVIDIA list, short form: 330 s limit, scripted button presses every 10 s from 60 s | stack dumps at fixed times (LEONIDA_STACKS)
DEVIATIONS:  Windows GPU timeout at the default (2 s); ASYNC_MIN_BYTES and COMPILE_GATE at main's defaults
CACHES:      pipeline cache warm, same folder for both | RUN 1 of 1 for this build
MEASURED (w78 | w77 main):
  in-title state (session=1)     120 s | 145 s
  world entered                  165 s | 195 s   (-30 s)
  last window >= 1 img/s         234 s | 293 s
  PresentX ok / failed           2670 / 0 | 3330 / 0
  private RAM max                17.0 GB | 16.0 GB
  free RAM min                   4307 MB | 5323 MB
END:         w78 stopped by my local stall guard (no submit-phases line for 62 s; last at 234 s) | w77 ran to its 337 s time limit | device loss: no, either run
VERDICT:     inconclusive; the display stop is still there (last window 69 s after world entry on the PR run, 98 s on the control)

Reading (marked):

  • MEASURED: the world is entered 30 s earlier with the PR (165 s against 195 s).
  • MEASURED: the display still stops on the PR build. The control reached its time limit at 337 s with its last window at 293 s; the PR run was stopped by the guard, so the two end times are not comparable.
  • NOT SHOWN: that the 30 s is caused by the PR. Four runs of main on this machine (two recipes, cold and warm cache) enter the world at 185-200 s and have their last window >= 1 img/s at 267-293 s; the only same-recipe repeat differs by 5 s and 7 s. One run per side, so this is not conclusive.

Limits: one machine, one run per side, short recipe.

AI-assisted (Claude Code, Claude Opus 5.5)

@MoonEyes

Copy link
Copy Markdown
Collaborator

Follow-up to my RUN above, with more runs on the same machine (RTX 4060 8 GB, NVIDIA 610.88, Windows 11, main ec91d69, same short recipe and same warm cache folder):

run build world entered last window >= 1 img/s
w77 (17:31) main 195 s 293 s
w78 (17:37) main + #1920 165 s 234 s
w79 (17:48) main, same recipe as w77 200 s 286 s
rdc-court (18:33) main + 4 unrelated commits from another contributor 165 s 246 s
budget-court (18:56) main, one compile setting changed (LEONIDA_SERIAL_BIG_COMPILES unset) 165 s 243 s (*)

(*) read from that run's log at 19:05 and also recorded by my stop guard; the log was later removed by a disk cleanup on my side.

Two later runs without #1920 also entered the world at 165 s. So the 30 s earlier world entry I reported for #1920 is within what this machine shows across runs, and I do not attribute it to the PR. The display stop is the same with and without #1920. n=1 per row; I have not repeated #1920 itself.

AI-assisted (Claude Code, Claude Opus 5.5)

aslan2142 pushed a commit that referenced this pull request Nov 27, 2026
…t still goes back (RT-TITLE-WINDOW-RETIRE)

On top of #1920, which keeps stream-pool extents inside the free span in
both views and makes TitleVaAliases::add_twin refuse an overlap. Other
ranges still replace a window's ranges whole by design: a slot-133 buffer
the title places over the window's pages, a BVH input's exact copy, an
upload alias. The window was then left in title_windows and pool_spans,
refreshed at every submission and charged to LEONIDA_TITLE_WINDOWS_MB, with
nothing left to reach it. A retail run showed most of the windows' memory
growth to be such orphans; the title released none of the windows' pages,
so retiring a window with its pages never fired.

A window no range answers from any more is retired after the next
submission's refresh: the alias table's listener records removed ranges'
copies, and the touched windows TitleVaAliases::answers_into finds
unreached leave pool_spans and the budget at once.

Its memory is freed only once nothing can still reach it. A command list
resolves its root views when they are recorded (GraphicsCommandList::
address), so a list recorded before the retirement and executed, or
executed again, after it still names the window; so does the copy of the
lists a held batch (a Wait before its Signal) or the async worker keeps.
Each backend CommandList, and each copy, now carries the epoch its
recording began in (src/command_list_epochs.h); a retired window takes its
ticket only once no list older than its retirement is alive and the
replays in progress then have recorded their submissions, and is freed
when that ticket has completed.

Until then the copy stays a target of its pages' tracker, still
refreshed, and what the GPU wrote in it goes back to the title's pages:
with its submissions' write-back, and once more when it is freed. The
second is needed because a list's ranges are noted when it is recorded,
so a held batch's can go back, tagged with a later submission's ticket,
before the batch has run. A retired copy goes back only where no live copy
changed the bytes, and every copy is compared, 256 bytes at a time, with
the title's bytes as they were before any copy went back: a copy that
still held them used to overwrite another's result when it came later in
the tracker.

Tests: d3d12_title_window_orphans. A window a slot-133 buffer replaced is
retired, its bytes returned and its memory freed. A writing batch held on
a fence on another queue, recorded before the next submission's refresh
retires its window, keeps the window alive past a completed submission,
and once it has run and its list is reset, its 64 words reach the title's
pages and the window is freed. Without the retirement the window stays
(3 -> 3 windows); without the list epochs it is freed while the held batch
still names it; without the last copy back 0 of 64 words arrive.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Gt9hwNPurxyJ9LAfUbNiYr
@aslan2142
aslan2142 merged commit d78b0e9 into ProjectLeonida:main Nov 27, 2026
7 of 12 checks passed
aslan2142 pushed a commit that referenced this pull request Nov 27, 2026
…over them, not a zero-size view (RT-TITLE-WINDOW-RETIRE)

#1920 stops a new pool extent at the next mapping in either view and, in
d78b0e9, refuses a mapping shorter than the request: such a request
gets a zero-size view (loads 0, stores dropped). That keeps the views from
reaching past their copies, which the first version of this fix did not,
but a refusal is a regression for requests main served: a V# or a root
view (64 KiB, kRootViewBytes) that crosses from one extent into the next,
or starts inside one and ends past it. Stream-pool extents sit next to
each other, so these are common. Retail runs (local, RTX 5080): our
first version, which gave such requests a short mapping, crashed in the
title in one run (m176); with one mapping over the request instead, runs
m177-m179 ran without one. The crash's cause was not established.

At #1920's two refusals of a request longer than what would serve it
(the extent clipped by clip_title_pool_extent, or the mapping holding its
first byte), resolve_pool_memory now tries, after #1920's reuse,
cover_pool_request: one mapping over the request and every window it
reaches, each whole, in both views (title_pool_cover). It is entered with
the new TitleVaAliases::replace_twin, since add_twin refuses an overlap;
the windows it replaces are retired at the next submission like any
window nothing answers from (previous commit), and their bytes are not
charged to the budget twice meanwhile. With a range in the way that is not
a window (a slot-133 buffer, a geometry copy), or pages that are not
readable, #1920's refusal stands. #1920's own refusal when the window
cannot be made or entered is unchanged.

TitleVaAliases::resolve and resolve_title_memory already ask again for a
range that ends before the view (#1920); vsharp_address did not, so a
V# whose base lies in a short range was refused without asking: it now
asks resolve_title_memory first.

Tests: #1920's pass unchanged (their seam cases cross a range that is
not a window, which still gets a zero-size view). title_window_plan: the
cover takes a window whole in both views, refuses a buffer in the way and
past the cap, and replace_twin enters it where add_twin refuses.
d3d12_title_window_orphans: a 64 KiB CBV across two extents' seam gets
one 448 KiB mapping, a valid V# and the right words on the GPU, and the two
extents are retired at the next submission; without the cover its V# base
is 0.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Gt9hwNPurxyJ9LAfUbNiYr
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants