Skip to content

D3D12.X: mapped ranges asked again at the same generation need no lock - #3476

Open
Foniass wants to merge 1 commit into
xcl-dev:mainfrom
Foniass:d3d12x-mapped-ranges-same-gen
Open

Foniass wants to merge 1 commit into
xcl-dev:mainfrom
Foniass:d3d12x-mapped-ranges-same-gen

Conversation

@Foniass

@Foniass Foniass commented Nov 29, 2026 •

Copy link
Copy Markdown
Contributor

Port of raylitebull's amd-dev commit 707123ad. sparse::mapped_ranges took sparse::STATE every time the watched ranges were rebuilt. In a retail title's world on amd-dev (RX 9070 XT), a submit waited 0.08–0.29 ms for that lock there. The ranges only change under that lock, and every change moves MAPPED_GEN. So the last answer is now kept with the generation it was made at, and given again without the lock while the generation stands. init moves the generation when it puts in a new state.

What is in it

  • crates/xcl/src/hle/d3d12x/sparse.rs: LAST_MAPPED_RANGES (amd-dev's RANGES, renamed: main has a const RANGES), init moving the generation under main's STATE guard, the XCL_LOOK_STATS=2 count of answers made with and without the lock (main's split line has no CPU-reads part, so only this count is added), and the test mapped_ranges_made_at_this_generation_need_no_lock

Review of the port:

  • Every change of State::runs / src_runs on main is followed by a MAPPED_GEN bump under STATE: shadow_window, ensure_listed, commit_range, decommit, unmirror, link_mirror, unlink_kept.
  • The cache is only written with STATE held, so its generation always matches its ranges.
  • A reader racing a writer that changed the runs but has not bumped the generation gets the answer from before that write. That is the same as taking the lock just before the writer.
  • One difference: the lock-free answer skips forget_released, so buffers waiting in FORGOTTEN are handled by the next lock holder. That only delays a release.

Tested

  • NVIDIA GeForce RTX 5070 Ti (driver 32.0.16.2114), AMD Ryzen 7 7800X3D, Windows 11 build 26300, Rust 1.100.0 (stable-x86_64-pc-windows-msvc), debug tests, release smoke
  • cargo test -p xcl --lib hle::d3d12x::sparse: 141 passed, 0 failed, 1 ignored. The new test fails with the lock-free path taken out.
  • cargo test --workspace --no-fail-fast -- --test-threads=4 on b47d0eb, rebased on main 86e86fc (5070 Ti): 2890 passed, 0 failed, 51 ignored. SKIPPED: 28 notices, almost all no wave64 (the others: no D3D12 debug layer, heap windows off, driver cache needs Developer Mode). The narrower runs listed here are from before the rebase.
  • Retail title: 60 s smoke run below.

Regression run

main:

xcl-smoke v1
commit: 86e86fcb33f94fa5d0c809d9b50af7b84bf49cce
gpu: NVIDIA GeForce RTX 5070 Ti
title: 3cb1d8e0
seconds: 60
exit: 0x0
frames shown: 1608 (PresentX 1608)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 15766 MB

this PR:

xcl-smoke v1
commit: b47d0eb8178579d635d54187438ce853d79b7f1a
gpu: NVIDIA GeForce RTX 5070 Ti
title: 3cb1d8e0
seconds: 60
exit: 0x0
frames shown: 1611 (PresentX 1611)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 16702 MB

Not tested

  • The lock wait it saves was measured on amd-dev (RX 9070 XT), not here; a 60 s run does not reach the title's world
  • AMD GPUs

AI use

  • Claude Code with Claude Opus 5.5: ported the commit to main (rename, init, the split line), reviewed every change of the ranges for the generation bump, ran the tests and the smoke runs, wrote this description.
  • Original code: raylitebull's amd-dev 707123ad.

🤖 Generated with Claude Code

@github-actions github-actions Bot added area:d3d12 D3D12.X runtime, command lists, resources, sparse memory area:memory Virtual/physical memory, arenas, VRAM budget, UMA gpu:amd Code is specific to AMD / RDNA / wave64 GPUs (scope tag; see needs:amd-test) labels Nov 29, 2026
`title_textures::cached_watch_ranges` makes the watched ranges again
whenever any part of its key moved (the textures' 16 MiB blocks, the
graphics reservations, the shown pages, the mapped tiles), and each time
it asked `sparse::mapped_ranges`, which takes `sparse::STATE`. In a
retail title's world (RX 9070 XT, XCL_LOOK_STATS=2) a submit's "ranges"
stage was 0.11-0.31 ms, 0.08-0.29 ms of it waiting for that lock ("the
submit's other waits for it: ... ranges").

The tiles' runs (`State::runs`, `src_runs`) change only under the lock,
and every change moves `MAPPED_GEN` (which is in the key already). So the
last answer is kept with the generation it was made at (`RANGES`) and
given again without the lock while the generation stands; `init` moves it
when it puts a new state in. This is the "already up to date" check made
lock-free, as VIctory-Neo does for its refreshes (55eb862, its write
journal's epochs); here there is one generation for all the ranges.

XCL_LOOK_STATS=2: the sparse lock split line counts the answers made under
the lock and those that were not.

Test: mapped_ranges_made_at_this_generation_need_no_lock (answered while
another thread holds the lock; once the generation moved, it waits for
the lock and makes them again).

Ported from amd-dev: the static is `LAST_MAPPED_RANGES` (main has a
`const RANGES` look field); main's lock split line has no CPU reads part,
so only the mapped ranges count is added to it; `init` moves the
generation under main's `STATE` guard, which it keeps for the early
commits.

(cherry picked from commit 0383b3fe8a6a2595031fb89d702d0ce3e3f898ad)
(cherry picked from commit 707123a)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@Foniass
Foniass force-pushed the d3d12x-mapped-ranges-same-gen branch from 1dc5083 to b47d0eb Compare November 30, 2026 23:07
@fromthemoonx

fromthemoonx commented Dec 1, 2026 •

Copy link
Copy Markdown

merge-check
base: 6bfb5c5
head: b47d0eb
merge: clean
build: cargo build --workspace -> ok
tests: cargo test --workspace --no-fail-fast -- --test-threads=4 -> 2905 passed, 0 failed, 51 ignored (base: 2904 passed, 0 failed, 51 ignored); SKIPPED: 25 (22 no wave64, 2 no heap windows, 1 no GPU_UPLOAD heaps), the same set as base; failing: none from this PR. The counts are the lib binary's clean run (2757 passed, 0 failed, 31 ignored) plus the other binaries (148 passed, 0 failed, 20 ignored). How the runs went:

  • The whole-suite run ended the lib binary with 0xc0000005 after 7 of the 40 dumps of eh::tests::thread_dumps_while_threads_run_and_exit. That is Tests: the lib test binary sometimes ends with 0xc0000005 during eh::tests::thread_dumps_while_threads_run_and_exit #3586, which happens on main too.
  • The first lib rerun failed two known flakes:
  • The second lib rerun was clean.
    review: read the whole diff (sparse.rs, +71 -2). No correctness problem found.
  • I checked the port's main claim. watched_ranges reads only runs and src_runs. Every change to them is followed by a MAPPED_GEN bump under STATE:
    • early_add / early_remove at sparse.rs 3390, 3780, 3864/3873 (bumped when new || unparked), 5790, 5873, 6519, 6545.
    • init (new in this PR).
    • Nothing sets STATE back to None.
  • The cache is written only with STATE held, with the generation read under it. So a lock-free answer is at worst the one from just before a writer's bump.
  • mapped_ranges_made_at_this_generation_need_no_lock: 30 of 30 alone, 30 of 30 with hle::d3d12x::sparse::tests:: at --test-threads=8.
  • Mutation: lock-free path off (if false && *gen == ...) fails the new test with "answered with the state's lock held elsewhere: Timeout". Good.
  • Minor: with the new MAPPED_GEN bump in init removed, all 1212 hle::d3d12x:: tests still pass. No test makes a second state and asks for its ranges, so that line, which keeps an old device's ranges from being answered, has no test.
  • No retail title on this machine: the submit-path lock waits were not measured here.
    performance: not measured here. The lock-free answer clones the cached Vec, as the locked path's result already did.
    verdict: safe to merge
    hardware / tool and model: RTX 3060 12 GB (610.88), Ryzen 7 5700X, Windows 11 Pro 26300 / Claude Code (Claude Opus 5.5)

@github-actions github-actions Bot added needs-1st-check Passes gates; needs first independent merge-check (#3041) needs:amd-test Needs a test report or merge-check on an AMD GPU labels Dec 1, 2026
@github-actions github-actions Bot added needs-2nd-check Has 1 safe merge-check; needs second independent check (#3041) and removed needs-1st-check Passes gates; needs first independent merge-check (#3041) labels Dec 1, 2026
@vasanex

vasanex commented Dec 2, 2026

Copy link
Copy Markdown

merge-check claim
head: b47d0eb

@vasanex

vasanex commented Dec 2, 2026

Copy link
Copy Markdown

merge-check
base: 1c8b551
head: b47d0eb
merge: clean (merged tree 42485b91, git diff --check clean)
build: cargo build --workspace -> ok
tests: cargo test --workspace --no-fail-fast -- --test-threads=4 -> 2910 passed, 0 failed, 51 ignored; SKIPPED: 25 (22 no wave64, 2 no heap windows, 1 cannot disable the driver cache), the same set as on the base; failing: none. Base alone, same machine: 2909 passed, 0 failed, 51 ignored. The new test mapped_ranges_made_at_this_generation_need_no_lock passes.
review: whole diff read (one file, sparse.rs, +71/-2), in the merge result. No finding. The claim the change rests on holds on current main: watched_ranges is made of runs and src_runs only, and every place that changes either (the tile mapping and unmapping paths, the shadow window, the mirror sources) moves MAPPED_GEN while still holding STATE; init now moves it too when it puts in a new state. The answer is stored with the generation read under the lock, so a stored answer is never newer than its generation. A caller that asks while another thread is inside the lock between a change and its fetch_add gets the answer from before that change, which is what it would have got by asking a moment earlier. LAST_MAPPED_RANGES is taken alone or inside STATE, never around it. The extra text on the XCL_LOOK_STATS=2 line only appears with that switch.
performance: 60 s smoke of the same title, release, default settings, same machine, one run each. Base 1c8b551: 1608 frames shown, presents per 2 s (last 3) 60 60 61, 0 device removals, 0 crashes, peak commit 19911 MB. Merge result (local merge commit): 1624 frames shown, 60 60 60, 0 device removals, 0 crashes, peak commit 20004 MB. No difference outside the spread; the start and menu phase of this title does not have the lock contention the change is for, so the gain in a world is not shown here. Cost by default, reasoned: one short mutex and a clone of the ranges per call, where main took STATE and built the same vector.
verdict: safe to merge
hardware / tool and model: NVIDIA GeForce RTX 3080 10 GB (driver 621.40), AMD Ryzen 9 7950X, Windows 11 Pro 26200, rustc 1.100.0, debug suite and release smoke / Claude Code with Claude Fable 5.2. Run and posted without a human check of each point.

@github-actions github-actions Bot added ready-to-merge Has 2 independent safe merge-checks on current head (#3041) and removed needs-2nd-check Has 1 safe merge-check; needs second independent check (#3041) needs:amd-test Needs a test report or merge-check on an AMD GPU labels Dec 2, 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:memory Virtual/physical memory, arenas, VRAM budget, UMA gpu:amd Code is specific to AMD / RDNA / wave64 GPUs (scope tag; see needs:amd-test) ready-to-merge Has 2 independent safe merge-checks on current head (#3041)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants