Skip to content

D3D12.X: remember committed source regions instead of asking Windows per submit - #3575

Draft
raylitebull wants to merge 1 commit into
mainfrom
perf/source-regions
Draft

raylitebull wants to merge 1 commit into
mainfrom
perf/source-regions

Conversation

@raylitebull

@raylitebull raylitebull commented Dec 1, 2026 •

Copy link
Copy Markdown
Member

Every submit asked Windows about each CPU-view tile it touched while holding the sparse lock: first K32QueryWorkingSetEx, then VirtualQuery for the pages outside the working set. shader::readable and read_committed_into also walked VirtualQuery for title memory outside the coherent window. This PR keeps the spans found committed and accessible. Whatever takes pages away clears their span both before Windows does it and after. On amd-dev (3a3a8e9c, RX 9070 XT, 5+ minutes in a retail title's world), asking Windows which pages are committed went from 1.513 ms avg / 10.233 ms max to 0.002 / 0.009 ms, and the sparse lock per submit from 1.637 / 10.414 ms to 0.149 / 0.767 ms.

What is in it

  • sparse.rs: SourceRegions (sorted, coalesced spans plus a log of the latest 256 invalidations, so a query that raced one replays it or drops its result). holes_now goes through source_holes_cached, and known_readable / cache_readable_region serve shader::readable. Sparse's own decommit and release paths invalidate. XCL_SOURCE_HOLES_CACHE=0 turns the cache off. sparse::free clears after its decommit too. Each unit test has a cache of its own (tests::SOURCE_REGIONS), on only where a test turns it on (tests::REGION_CACHE), so tests that free memory with plain VirtualFree leave nothing behind for others.
  • kernelx.rs: VirtualFree and VirtualAlloc become wrappers (virtual_free / virtual_alloc hold the old bodies). VirtualFree clears the span before and after every path (placeholders, sparse windows, GPU views, the general one). A free of size 0 clears the whole reservation (whole_len): its length comes from XCL's own records (sparse and view windows, graphics reservations) or from VirtualQuery. A block of a committed window clears nothing, and the whole cache is cleared only when the length is unknown. VirtualAlloc clears it around a commit that leaves pages unreadable. The same before-and-after clearing covers VirtualFreeEx of the process's own memory, non-writable VirtualProtect(Ex), release_at and give_back_homes (physical pages given up), the home block FreeTitlePhysicalPages releases, and GPU-view reservations (mirrors::reserved). The clear after a call happens only when the call succeeded, so the title's GetLastError is untouched.
  • gpu/shader.rs: readable answers from the cache first, and records the regions it finds in UMA windows and graphics reservations.
  • title_textures.rs: read_committed_into copies at once when the range is known readable (from 4d6d4a77).
  • Not ported from amd-dev: the alias hook (main has no kernelx::alias) and the XCL_LOOK_STATS=2 timing lines.
  • Tests: source_holes_cached_reuses_regions_and_invalidates_only_freed_ranges, and the_title_s_frees_and_protections_leave_the_region_cache, which goes through kernelx's VirtualFree (a decommit, then the release of a reservation with holes in it, while another allocation stays cached), VirtualProtect and VirtualAlloc.

Tested

  • No Windows machine or GPU here: a Linux x86_64 cloud machine (Ubuntu 24.04), rustc 1.99.0, debug build.
  • cargo check --workspace --all-targets --target x86_64-pc-windows-gnu: no errors, no new warnings.
  • The lib unit tests of the touched modules, built for x86_64-pc-windows-gnu and run under Wine 9.0 (no GPU) with --test-threads=4, next to the same filters on main b2b1f109:
    • hle::d3d12x::sparse: 92 passed, 50 failed, 1 ignored (main: 90 / 50 / 1)
    • hle::kernelx: 66 passed, 0 failed, 1 ignored (main: 66 / 0 / 1)
    • gpu::shader: 4 passed, 2 failed (main: 4 / 2)
    • hle::d3d12x::title_textures: 64 passed, 1 failed, 1 ignored (main: 64 / 1 / 1)
      The failures are the same tests on both, ones that need a D3D12 device or Windows' working-set queries, which Wine lacks.
  • CI's Build and test (Windows, no GPU) passed on the earlier heads eab0ffba and 15ae499c.

Regression run
Not run yet: there was no Windows machine or title here. scripts/smoke.ps1 still has to run on a title, built from main and from this head, so PR checks fails until those two blocks are added.

Not tested

  • The Windows (MSVC) build and the whole suite (cargo test --workspace -- --test-threads=4) on Windows, with or without a GPU; CI's no-GPU job is the first Windows run.
  • Any title on main, and any performance on main: the numbers above are from amd-dev.
  • The cache against a real title's frees and protection changes on main, whose kernelx differs from amd-dev's (no alias module). The physical-page paths (release_at, FreeTitlePhysicalPages) are not covered by a unit test.
  • The cost of whole_len's VirtualQuery walk on a title's frees of size 0 of plain memory (not UMA blocks or graphics reservations, which need no query).

AI use

  • Capy with Claude Opus 5.5 ported raylitebull's amd-dev commit 3a3a8e9c to main, split it into this PR and five others, and wrote the test, the commit message and this description. The amd-dev measurements are raylitebull's own runs.

Open in Capy

@github-actions github-actions Bot added area:d3d12 D3D12.X runtime, command lists, resources, sparse memory area:hle kernelx, XGameRuntime, XAudio2, VFS, PE loader, TLS, EH area:memory Virtual/physical memory, arenas, VRAM budget, UMA area:shaders Shader cache, PSO compilation, pipeline libraries, precompilation gpu:amd Code is specific to AMD / RDNA / wave64 GPUs (scope tag; see needs:amd-test) labels Dec 1, 2026
@Foniass

Foniass commented Dec 1, 2026

Copy link
Copy Markdown
Contributor

Review (head eab0ffb, diff read, not a merge-check; draft):

  • Freed memory can stay "committed" in the cache. The cache's own doc comment (sparse.rs ~399) says to clear it around any decommit or release. Several paths clear only before Windows decommits:

    • sparse::free (~5148-5157: decommitted, then VirtualFree);
    • kernelx's VirtualFree for sparse windows and GPU views (kernelx.rs ~1216, ~1244);
    • placeholders::free.

    If shader::readable queries in the gap, it caches the span (shader.rs ~237) as committed after the decommit, and a later readable or read_committed_into reads freed memory, which can crash. source_decommit / source_release clear before and after, and these paths need the same.

  • Unmapped physical pages can stay cached. release_at (kernelx.rs ~2307) decommits through source_going, which clears only for paired CPU views or with page mappings on. A page unmapped or moved out of a graphics reservation stays cached, since readable caches those reservations (unverified that a title hits it).

  • Re-committing committed pages with VirtualAlloc(MEM_COMMIT, PAGE_NOACCESS / GUARD) changes protection without clearing (unverified whether a title does it).

  • No test covers the hooks. The cache is off in tests unless turned on, and the one new test clears it by hand, so nothing would catch a missing hook. A test that frees through each path, then asks readable, would.

  • It's on by default (XCL_SOURCE_HOLES_CACHE=0 turns it off), and the gain isn't measured on main. The numbers are from amd-dev, whose kernelx differs, and include the read_committed_into change from 4d6d4a7.

Fine: the lock is never held across a Windows call or around other locks, the replay of the 64 latest clears is right (a query that can't be replayed is dropped), holes are never cached (so commits and lazy commits need no hook), paired CPU views are covered, and memory is bounded. #2772, #1191 and #2544 edit the same kernelx hooks.

AI review: Claude Code (Claude Opus 5.5), posted by Foniass without a human check of each point.

@raylitebull raylitebull added the gpu:both Code affects both AMD and NVIDIA GPUs (scope tag; see needs:*-test for remaining checks) label Dec 1, 2026
@github-actions github-actions Bot removed the gpu:both Code affects both AMD and NVIDIA GPUs (scope tag; see needs:*-test for remaining checks) label Dec 1, 2026
@capy-ai

capy-ai Bot commented Dec 1, 2026

Copy link
Copy Markdown
Contributor

@Foniass thanks, the gaps were real, and most of them are on amd-dev too. Fixed in 15ae499:

  • Before and after: kernelx VirtualFree is now a wrapper that clears the span before the call and after a successful one, whichever path the free takes (placeholders, sparse windows, GPU views, the general one). VirtualFreeEx of the process's own memory, sparse::free, release_at (which also covers pages moved away from home) and the block FreeTitlePhysicalPages releases now clear before and after too.
  • Whole-reservation releases: a release of size 0 now clears the whole cache. Before, it dropped only the span holding the address, so a reservation with holes left its other spans cached.
  • Commits that leave pages unreadable: VirtualAlloc with MEM_COMMIT and a protection that isn't read-write clears the span around the call, as VirtualProtect does.
  • A test through the hooks: the_title_s_frees_and_protections_leave_the_region_cache decommits, protects, recommits PAGE_NOACCESS and releases through kernelx, and checks the cache after each step. Each unit test now has its own cache, so a parallel test's release can't clear another test's.
  • Still open: the physical-page paths have no unit test (they need UMA and physical pages), there's no measurement on main yet, and the smoke runs are still to come. The size-0 clear may cost hit rate on a title that releases whole reservations often, and the smoke runs will show it.

AI use: written by Capy (Claude Opus 5.5) and posted on raylitebull's behalf, with their go-ahead.

@Foniass

Foniass commented Dec 1, 2026

Copy link
Copy Markdown
Contributor

Re-review (head 15ae499; the diff from eab0ffb read). Most of my review is addressed:

  • Freed memory staying "committed": fixed for the paths I named. kernelx::VirtualFree now clears before and after the real free (kernelx.rs ~1214-1223), which covers placeholders::free, sparse windows, GPU views, graphics reservations and plain memory. sparse::free clears again after its decommit (sparse.rs ~5159-5162), and VirtualFreeEx on the process itself and FreeTitlePhysicalPages do the same (~1350-1353, ~2466 / ~2476).
  • Unmapped physical pages: fixed for release_at (~2335-2337).
  • Re-commit with another protection: fixed. A fixed-address MEM_COMMIT whose protection isn't read-write clears around the call (~912-924); PAGE_GUARD is caught because STD_PROTECT_MASK keeps the guard bit.

Still open:

  • give_back_homes clears only before (~2128-2132). It's the same pattern as release_at, for pages moved away from home. A home in a sparse window is cleared once by decommitted before the decommit, and source_going clears afterwards only for a paired CPU view or while pages are shown in GPU views. Homes are given back here, not through release_at (unverified that a reader queries a home in that gap).
  • A size-0 release now empties the whole cache (sparse.rs ~350-357, called from kernelx.rs ~1217 / ~1220). VirtualFree(p, 0, MEM_RELEASE) is how a title frees any allocation, UMA blocks included (which in_committed_window answers before the cache is asked), so every such call wipes the cache and drops every query in flight. Each branch already knows the real length (sparse_reservation, view_reservation, block_len, GRAPHICS_RESERVATIONS, the placeholder region), so it can be narrowed. Performance only, but it may eat most of the gain.
  • A sparse decommit now records three clears (decommitted plus the wrapper twice), and the replay list keeps 64, so a burst of frees drops more in-flight queries. Correct, but it costs hit rate.
  • The new test (~12403-12427) goes through kernelx's own calls. Only its PAGE_NOACCESS re-commit assert fails on the old head, and the cache is per thread in tests, so no test can show the cross-thread race the before / after clears are for.
  • No measurement on main and no smoke pair yet (as you say).

Fine: SOURCE_REGIONS is still a leaf lock, and the replay of recent clears is right with clearing before and after (a query that read the revision before or between the two clears is cut back or rejected). The other VirtualFree / VirtualProtect calls in the crate free XCL's own memory, not memory the cache stores.

AI review: Claude Code (Claude Opus 5.5), posted by Foniass without a human check of each point.

@capy-ai

capy-ai Bot commented Dec 1, 2026

Copy link
Copy Markdown
Contributor

@Foniass thanks for the re-review. In 9fd0f30:

  • give_back_homes now clears before and after, like release_at.
  • A free of size 0 no longer empties the cache. The VirtualFree wrapper takes the reservation's length from whole_len: sparse_reservation / view_reservation, then GRAPHICS_RESERVATIONS, then a VirtualQuery walk over the regions of the allocation at that address. A block of a committed window clears nothing. Only an unknown length still clears everything. VirtualFreeEx uses the same.
  • The replay log keeps the latest 256 clears instead of 64, so a burst of frees drops fewer queries in flight.
  • The hooks test now also checks that releasing one reservation, holes included, leaves another allocation cached.
    Under Wine, the same filtered modules as before fail exactly the tests that fail on main, and both new tests pass.

AI use: written by Capy (Claude Opus 5.5) and posted on raylitebull's behalf, with their go-ahead.

@github-actions github-actions Bot added the needs-pr-check Failing the PR description check (CONTRIBUTING.md) label Dec 1, 2026
@Foniass

Foniass commented Dec 1, 2026

Copy link
Copy Markdown
Contributor

Follow-up (head 9fd0f30; the diff from 15ae499 read). The open points from my re-review are addressed:

  • give_back_homes now clears before and after its decommit (kernelx.rs ~2165-2167).
  • A size-0 release no longer empties the cache. whole_len works out the real span: the sparse or view reservation, a graphics reservation, or the regions of the allocation at that base, read before the free. A block of a committed window is skipped, since the cache never stores those. Only an unknown length still clears everything. The new test checks that another allocation stays cached across a release, and that a release drops the whole span, past its holes too.
  • The replay list holds 256 recent clears instead of 64, which leaves room for the extra clears.

One detail: whole_len reads the allocation's regions with VirtualQuery before the free, so a size-0 release costs a short walk of that allocation's regions. That's cheap next to the free itself.

Still open, as before: no measurement on main and no smoke pair. The test run already queued for this PR will cover this head.

AI review: Claude Code (Claude Opus 5.5), posted by Foniass without a human check of each point.

@Foniass

Foniass commented Dec 1, 2026

Copy link
Copy Markdown
Contributor

Test report (not a merge-check: draft, PR description red)
base: b2b1f10
head: 9fd0f30
merge: clean
build: cargo build --workspace -> ok
tests: cargo test --workspace --no-fail-fast -- --test-threads=4 -> 2904 passed, 0 failed, 51 ignored; SKIPPED: 28 (20 no wave64, 2 no d3d12SDKLayers.dll, 1 no D3D12 debug layer, 1 no heap windows, 1 cannot disable the driver cache, 3 others; the same set as on main); failing: none
review: not repeated here; my follow-up on this head is at #3575 (comment) (with the earlier re-review linked from the thread). This run adds what reading couldn't: whether the change merges, builds and passes the whole suite on current main.
performance: no smoke run. The cache is on by default, so the default path changes; a smoke pair on this head is the evidence still missing.
assessment: merges, builds and passes the whole suite on current main at the newest head (9fd0f30), including the new release / re-commit cache tests; still open: a measurement of the gain on main and a smoke pair, since the cache is on by default
hardware / tool and model: RTX 5070 Ti (driver 621.14), Ryzen 7 7800X3D, Windows 11 26300 / Claude Code with Claude Opus 5.5

AI-written and posted by Foniass without a human check of each point.

…per submit

holes_now asked Windows about every CPU-view tile a submit touched
(K32QueryWorkingSetEx, then VirtualQuery for pages outside the working
set) while holding the sparse lock, and shader::readable and
read_committed_into walked VirtualQuery for title memory outside the
coherent window.

SourceRegions keeps the spans found committed and accessible. Only
accessible spans are kept, so a commit needs no invalidation. Whatever
takes pages away clears their span before Windows does it (no reader
trusts it meanwhile) and after (nothing a query found in between stays):
kernelx's VirtualFree, VirtualFreeEx, a VirtualProtect or a commit that
leaves pages unreadable, physical pages given up (release_at,
give_back_homes) and blocks FreeTitlePhysicalPages releases, GPU-view
reservations, and sparse's own decommit, release and free paths. A free
of size 0 clears the whole reservation, as long as XCL knows it (sparse
and view windows, graphics reservations) or VirtualQuery finds it;
nothing for a block of a committed window; and the whole cache only when
the length is unknown. A query that raced a clear replays the latest
256 clears before inserting, or drops its result when it cannot.
XCL_SOURCE_HOLES_CACHE=0 turns the cache off. Each unit test has a cache
of its own, on only in the tests that turn it on, so tests that free
memory without XCL's hooks leave nothing behind for others.

Measured on amd-dev (3a3a8e9, RX 9070 XT, 5+ minutes in a retail
title's world): asking Windows which pages are committed went from
1.513 ms avg / 10.233 ms max to 0.002 / 0.009 ms, the sparse lock per
submit from 1.637 / 10.414 ms to 0.149 / 0.767 ms. Not measured on main.

Ported from amd-dev 3a3a8e9, with the read_committed_into fast path
from 4d6d4a7.

Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com>
@github-actions github-actions Bot removed the needs-pr-check Failing the PR description check (CONTRIBUTING.md) label Dec 1, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:d3d12 D3D12.X runtime, command lists, resources, sparse memory area:hle kernelx, XGameRuntime, XAudio2, VFS, PE loader, TLS, EH area:memory Virtual/physical memory, arenas, VRAM budget, UMA area:shaders Shader cache, PSO compilation, pipeline libraries, precompilation gpu:amd Code is specific to AMD / RDNA / wave64 GPUs (scope tag; see needs:amd-test)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants