D3D12.X: remember committed source regions instead of asking Windows per submit #3575
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/3575"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Imported from ivall/xcl. Originally posted by raylitebull on 2026-12-01T14:44:20Z.
Every submit asked Windows about each CPU-view tile it touched while holding the sparse lock: first
K32QueryWorkingSetEx, thenVirtualQueryfor the pages outside the working set.shader::readableandread_committed_intoalso walkedVirtualQueryfor 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. Onamd-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_nowgoes throughsource_holes_cached, andknown_readable/cache_readable_regionserveshader::readable. Sparse's own decommit and release paths invalidate.XCL_SOURCE_HOLES_CACHE=0turns the cache off.sparse::freeclears 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 plainVirtualFreeleave nothing behind for others.kernelx.rs:VirtualFreeandVirtualAllocbecome wrappers (virtual_free/virtual_allochold the old bodies).VirtualFreeclears 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 fromVirtualQuery. A block of a committed window clears nothing, and the whole cache is cleared only when the length is unknown.VirtualAllocclears it around a commit that leaves pages unreadable. The same before-and-after clearing coversVirtualFreeExof the process's own memory, non-writableVirtualProtect(Ex),release_atandgive_back_homes(physical pages given up), the home blockFreeTitlePhysicalPagesreleases, and GPU-view reservations (mirrors::reserved). The clear after a call happens only when the call succeeded, so the title'sGetLastErroris untouched.gpu/shader.rs:readableanswers from the cache first, and records the regions it finds in UMA windows and graphics reservations.title_textures.rs:read_committed_intocopies at once when the range is known readable (from4d6d4a77).amd-dev: thealiashook (mainhas nokernelx::alias) and theXCL_LOOK_STATS=2timing lines.source_holes_cached_reuses_regions_and_invalidates_only_freed_ranges, andthe_title_s_frees_and_protections_leave_the_region_cache, which goes through kernelx'sVirtualFree(a decommit, then the release of a reservation with holes in it, while another allocation stays cached),VirtualProtectandVirtualAlloc.Tested
cargo check --workspace --all-targets --target x86_64-pc-windows-gnu: no errors, no new warnings.x86_64-pc-windows-gnuand run under Wine 9.0 (no GPU) with--test-threads=4, next to the same filters onmainb2b1f109: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.
Build and test (Windows, no GPU)passed on the earlier headseab0ffbaand15ae499c.Regression run
Not run yet: there was no Windows machine or title here.
scripts/smoke.ps1still has to run on a title, built frommainand from this head, soPR checksfails until those two blocks are added.Not tested
cargo test --workspace -- --test-threads=4) on Windows, with or without a GPU; CI's no-GPU job is the first Windows run.main, and any performance onmain: the numbers above are fromamd-dev.main, whose kernelx differs fromamd-dev's (noaliasmodule). The physical-page paths (release_at,FreeTitlePhysicalPages) are not covered by a unit test.whole_len'sVirtualQuerywalk on a title's frees of size 0 of plain memory (not UMA blocks or graphics reservations, which need no query).AI use
amd-devcommit3a3a8e9ctomain, split it into this PR and five others, and wrote the test, the commit message and this description. Theamd-devmeasurements are raylitebull's own runs.Imported from ivall/xcl. Originally posted by Foniass on 2026-12-01T14:53:09Z.
Review (head
eab0ffba, 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, thenVirtualFree);VirtualFreefor sparse windows and GPU views (kernelx.rs~1216, ~1244);placeholders::free.If
shader::readablequeries in the gap, it caches the span (shader.rs~237) as committed after the decommit, and a laterreadableorread_committed_intoreads freed memory, which can crash.source_decommit/source_releaseclear before and after, and these paths need the same.Unmapped physical pages can stay cached.
release_at(kernelx.rs~2307) decommits throughsource_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, sincereadablecaches 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=0turns it off), and the gain isn't measured on main. The numbers are fromamd-dev, whose kernelx differs, and include theread_committed_intochange from4d6d4a77.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.
Imported from ivall/xcl. Originally posted by capy-ai[bot] on 2026-12-01T15:06:16Z.
@Foniass thanks, the gaps were real, and most of them are on
amd-devtoo. Fixed in15ae499c:VirtualFreeis 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).VirtualFreeExof the process's own memory,sparse::free,release_at(which also covers pages moved away from home) and the blockFreeTitlePhysicalPagesreleases now clear before and after too.VirtualAllocwithMEM_COMMITand a protection that isn't read-write clears the span around the call, asVirtualProtectdoes.the_title_s_frees_and_protections_leave_the_region_cachedecommits, protects, recommitsPAGE_NOACCESSand 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.mainyet, 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.
Imported from ivall/xcl. Originally posted by Foniass on 2026-12-01T15:15:31Z.
Re-review (head
15ae499c; the diff fromeab0ffbaread). Most of my review is addressed:kernelx::VirtualFreenow clears before and after the real free (kernelx.rs~1214-1223), which coversplaceholders::free, sparse windows, GPU views, graphics reservations and plain memory.sparse::freeclears again after its decommit (sparse.rs~5159-5162), andVirtualFreeExon the process itself andFreeTitlePhysicalPagesdo the same (~1350-1353, ~2466 / ~2476).release_at(~2335-2337).MEM_COMMITwhose protection isn't read-write clears around the call (~912-924);PAGE_GUARDis caught becauseSTD_PROTECT_MASKkeeps the guard bit.Still open:
give_back_homesclears only before (~2128-2132). It's the same pattern asrelease_at, for pages moved away from home. A home in a sparse window is cleared once bydecommittedbefore the decommit, andsource_goingclears afterwards only for a paired CPU view or while pages are shown in GPU views. Homes are given back here, not throughrelease_at(unverified that a reader queries a home in that gap).sparse.rs~350-357, called fromkernelx.rs~1217 / ~1220).VirtualFree(p, 0, MEM_RELEASE)is how a title frees any allocation, UMA blocks included (whichin_committed_windowanswers 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.decommittedplus 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.PAGE_NOACCESSre-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.Fine:
SOURCE_REGIONSis 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 otherVirtualFree/VirtualProtectcalls 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.
Imported from ivall/xcl. Originally posted by capy-ai[bot] on 2026-12-01T15:18:49Z.
@Foniass thanks for the re-review. In
9fd0f303:give_back_homesnow clears before and after, likerelease_at.VirtualFreewrapper takes the reservation's length fromwhole_len:sparse_reservation/view_reservation, thenGRAPHICS_RESERVATIONS, then aVirtualQuerywalk over the regions of the allocation at that address. A block of a committed window clears nothing. Only an unknown length still clears everything.VirtualFreeExuses the same.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.
Imported from ivall/xcl. Originally posted by Foniass on 2026-12-01T15:19:31Z.
Follow-up (head
9fd0f303; the diff from15ae499cread). The open points from my re-review are addressed:give_back_homesnow clears before and after its decommit (kernelx.rs~2165-2167).whole_lenworks 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.One detail:
whole_lenreads the allocation's regions withVirtualQuerybefore 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.
Imported from ivall/xcl. Originally posted by Foniass on 2026-12-01T15:26:43Z.
Test report (not a merge-check: draft,
PR descriptionred)base:
b2b1f109d2head:
9fd0f303e9merge: 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 https://github.com/xcl-dev/xcl/pull/3575#issuecomment-7315720707 (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 (
9fd0f303), 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 defaulthardware / 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.
Imported from ivall/xcl. Originally posted by Foniass on 2026-12-02T08:16:57Z.
Test report (not a merge-check: draft)
base:
1c8b551475head:
3e04ddb464merge: clean
build: cargo build --workspace -> ok
tests: cargo test --workspace --no-fail-fast -- --test-threads=4 -> 2911 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: re-check of
3e04ddb4, rebased from9fd0f303(the head of my test report above) onto main1c8b5514, which now has #3608's per-page physical-page release. The only change in the PR's own commit is where its twosparse::invalidate_source_range(home, count * PHYS_PAGE)calls sit: they now run inFreeTitlePhysicalPages' loop over the emptied blocks afterAWAYis dropped (kernelx.rs~2537-2550), once before and once aftersource_goingreleases the home, which keeps the PR's rule (forget a cached committed region before its memory can go, and again after) on main's new free path. The findings in my earlier re-review still stand as they were.performance: 60 s smoke of a retail title, main
1c8b5514vs this merge (3e04ddb4): 1370 -> 1672 frames shown (122 %; last presents per 2 s: 60 60 60), peak commit 17058 -> 18037 MB, 0 device removals, 0 crashes. Main's run on this base was low (1370 frames; main usually gives about 1650-1680 here), so the difference isn't a gain from this PR; against the usual main numbers this run is about even. A 60 s run capped at 60 presents per second can't show a per-submit saving.assessment: on the rebased head, merges cleanly into main
1c8b5514, builds and passes the whole suite (the two e2e failures in an earlier attempt came from another XCL run on this machine at the same time, not from this PR), and the smoke run with the cache on (the default) is clean; still open: a measurement of the gain, and the findings in my earlier re-reviewhardware / 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.
View command line instructions
Checkout
From your project repository, check out a new branch and test the changes.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.