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

Open
Foniass wants to merge 1 commit from upstream-pr/3476 into main
Foniass commented 2026-11-29 23:34:41 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by Foniass on 2026-11-29T23:34:41Z.


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 b47d0eb8, rebased on main 86e86fcb (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

<!-- greyfinch-sync:ivall/xcl:issue:5556361620 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/3476). Originally posted by [Foniass](https://github.com/Foniass) on 2026-11-29T23:34:41Z. --- 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 b47d0eb8, rebased on main 86e86fcb (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](https://claude.com/claude-code)
fromthemoonx commented 2026-12-01 17:42:35 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by fromthemoonx on 2026-12-01T17:42:35Z.


merge-check
base: 6bfb5c5153
head: b47d0eb817
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 #3586, which happens on main too.
  • The first lib rerun failed two known flakes:
    • title_textures::tests::placed_title_textures_share_vram_and_preserve_pixels, with the same "superseded unaligned extra_pages ... (22609920 -> 22609920)" message I saw today on #1852's merge, which has no sparse.rs change; #2591 is its open fix. Three tests of that module are poisoned after it.
    • xaudio2::tests::xma2_source_voice_decodes_and_manages_lifetime, which #3551 fixes.
  • 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)
<!-- greyfinch-sync:ivall/xcl:comment:7319726811 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/3476#issuecomment-7319726811). Originally posted by [fromthemoonx](https://github.com/fromthemoonx) on 2026-12-01T17:42:35Z. --- merge-check base: 6bfb5c5153ccc23517daf30aa1071ddb0fadb53c head: b47d0eb8178579d635d54187438ce853d79b7f1a 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 #3586, which happens on main too. - The first lib rerun failed two known flakes: - `title_textures::tests::placed_title_textures_share_vram_and_preserve_pixels`, with the same "superseded unaligned extra_pages ... (22609920 -> 22609920)" message I saw today on #1852's merge, which has no `sparse.rs` change; #2591 is its open fix. Three tests of that module are poisoned after it. - `xaudio2::tests::xma2_source_voice_decodes_and_manages_lifetime`, which #3551 fixes. - 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)
vasanex commented 2026-12-02 01:09:25 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by vasanex on 2026-12-02T01:09:25Z.


merge-check claim
head: b47d0eb817

<!-- greyfinch-sync:ivall/xcl:comment:7331378186 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/3476#issuecomment-7331378186). Originally posted by [vasanex](https://github.com/vasanex) on 2026-12-02T01:09:25Z. --- merge-check claim head: b47d0eb8178579d635d54187438ce853d79b7f1a
vasanex commented 2026-12-02 01:22:46 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by vasanex on 2026-12-02T01:22:46Z.


merge-check
base: 1c8b551475
head: b47d0eb817
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 1c8b5514: 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.

<!-- greyfinch-sync:ivall/xcl:comment:7331635471 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/3476#issuecomment-7331635471). Originally posted by [vasanex](https://github.com/vasanex) on 2026-12-02T01:22:46Z. --- merge-check base: 1c8b551475e1e7bfe48f94448492a4c3544a2e0f head: b47d0eb8178579d635d54187438ce853d79b7f1a 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 1c8b5514: 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.
bmjask commented 2026-12-02 14:13:13 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by bmjask on 2026-12-02T14:13:13Z.


@Prime1997 could you take a look at this one for review and merge? My automated review notes are above, if any.

<!-- greyfinch-sync:ivall/xcl:comment:7348183709 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/3476#issuecomment-7348183709). Originally posted by [bmjask](https://github.com/bmjask) on 2026-12-02T14:13:13Z. --- @Prime1997 could you take a look at this one for review and merge? My automated review notes are above, if any.
This pull request cannot be merged automatically due to conflicts.
Merge manually to resolve the conflicts.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin 'upstream-pr/3476:upstream-pr/3476'
git switch 'upstream-pr/3476'

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.

git switch 'main'
git merge --no-ff 'upstream-pr/3476'
git switch 'upstream-pr/3476'
git rebase 'main'
git switch 'main'
git merge --ff-only 'upstream-pr/3476'
git switch 'upstream-pr/3476'
git rebase 'main'
git switch 'main'
git merge --no-ff 'upstream-pr/3476'
git switch 'main'
git merge --squash 'upstream-pr/3476'
git switch 'main'
git merge --ff-only 'upstream-pr/3476'
git switch 'main'
git merge 'upstream-pr/3476'
git push origin 'main'
Sign in to join this conversation.
No reviewers
No labels
No milestone
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.

Reference
xcl-dev/xcl!3476
No description provided.