d3d12: lock-free view publication and shared resource reads on list threads (D3D12-GPU-LIST-LOCK-CONTENTION) #2764
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/2764"
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 ubiquitarianism/leonida. Originally posted by zmasi on 2026-11-30T16:41:36Z.
Summary
Addresses the four dominant
leonida-list-0..5recording-thread lock hotspots identified in the whole-process WPR CPU profile in #2707 (D3D12-GPU-LIST-LOCK-CONTENTION, ~34.3% of list-thread samples insideleonida_sm.exeacrossdescriptor::view_at16.7%,RwLock::read_contended4.2%,resource::hosted4.2%,descriptor::host::placed_texture+resource::want_level3.5% + 1.6%, anddescriptor::host::table_r9g9b9e5_views2.7%):Lock-free descriptor view & shape publication (
d3d12/crates/leonida-gpu/src/descriptor.rs):PUBLISHED: a paged lock-free atomic store (1,02464-byte-alignedSlot([AtomicU64; 16])records per page) covering all2^22immutablePLACEDshapes and the first2^20reusableViews::listentries, kept updated underViewsWrite(shape,entry,forget) regardless of switch state so A/B toggles remain seamless.view_atreads bothPLACEDshapes andkind == 0entries without touchingVIEWS.read(), thread-localRefCells, or the 8,192-slot direct-mappedVIEW_CACHEDtable (which suffered associative conflict misses across >8,192 active entries). Each reusable slot carries(turn, generation, kind, MARKED, R9)in word0withAcquire/Releaseseqlock validation (> 1.44e17combined period, zero non-atomic data races, bounded retry fallback toviews_read()).Zero-lock fast skip & R9 head filter in
table_r9g9b9e5_views(d3d12/crates/leonida-gpu/src/descriptor/host.rs):any_r9_views()atomically as views/shapes are registered sotable_r9g9b9e5_viewsreturns immediately when zero placedR9G9B9E5shader views exist in the session, without scanning0..ndescriptor slots or takingVIEWS.read().R9G9B9E5placed views do exist,r9_view_atchecks the precomputedSlot::R9bit in word0before decoding payload words, avoidingVIEWS.read()across bothPLACEDandkind == 0slots.Lock-free
want_level/host_waits/ absenthostedchecks & shared-readhosted(d3d12/crates/leonida-gpu/src/resource.rs):Hosted::madefromMutex<Option<Made>>toRwLock<Option<Made>>paired withmade_base: AtomicU32(NO_COPYwhen absent,HostTexture::basewhen present), maintained across all mutation sites (Hosted::put/Hosted::take/Drop).want_levelreturns without locking whenmade_baseshows an existing copy already covers the requested mip level;host_waitsandhosted(..., false)/refused/no_roomchecks answer without locking; steady-statehostedhits take a shared read lock (updatingusedonly whenused < nowviafetch_maxto avoid redundant cacheline invalidations) while preservinglet_go_idle's exclusive-lockArc::strong_count(host) == 1invariant andwait_for/make_waitingwakeup ordering.Per-thread 2-way
PLACED_NEARlookup table intexture_at(d3d12/crates/leonida-gpu/src/resource.rs):PLACED_NEAR, storing resource addresses only soArc<HostTexture>refcounts are unaffected) in front ofPLACED_FOUNDwhenplaced_shards() && list_lock_fast(), eliminating shared shardRwLock::readatomic RMWs on repeated per-frame placed texture SRV lookups while preservingLEONIDA_PLACED_SHARDS=0diagnostics behavior.Negative-Control Switch
LEONIDA_LIST_LOCK_FAST(crate::toggled!("LEONIDA_LIST_LOCK_FAST"), on by default;LEONIDA_LIST_LOCK_FAST=0restores the baseline locked read paths, and can also be toggled by turns under A/B becausePUBLISHEDandmade_basestay populated on writes).Benchmark (
6Concurrent Recording Threads,--release)Measured on Windows 11 (
10.0.26200) + NVIDIA GeForce RTX 3090 (driver610.88) usingdescriptor::host::list_contention_tests::list_threads_descriptor_and_resource_contention_bench(6threads,32,768distinct table entries withi ^ 8192associative stride, inlinePLACEDviews, 32-slottable_r9g9b9e5_viewsscans inno-r9andmixedphases,placed_texture+want_level, andresource::host_copy, each configuration run in a fresh process):no-r9(ns/draw)mixed(ns/draw)combined(ns/draw)draws/s, 6 threads)LEONIDA_LIST_LOCK_FAST=0,LEONIDA_VIEW_CACHE=0)299.55312.75306.153,266,3761.00xLEONIDA_LIST_LOCK_FAST=0,LEONIDA_VIEW_CACHE=0)299.99303.66301.833,313,1511.00xLEONIDA_VIEW_CACHE=1(LEONIDA_LIST_LOCK_FAST=0)239.72219.01229.364,359,9421.32xLEONIDA_LIST_LOCK_FAST=1)32.4057.2044.8022,321,9699.25x(no-r9) /6.79x(combined)LEONIDA_LIST_LOCK_FAST=1)31.2658.9245.0922,176,5659.60x(no-r9) /6.69x(combined)Verification
Tested on Windows 11 (
10.0.26200), NVIDIA GeForce RTX 3090 (driver610.88) withLEONIDA_D3D12_DEBUG=1:25new non-ignored tests acrossdescriptor::tests,descriptor::host::list_contention_tests, andresource::tests, exercising bothLEONIDA_LIST_LOCK_FAST=0and=1plus per-call alternation): all passed (0failed).cargo clippy -p leonida-gpu --all-targetsand--features diagnostics --all-targets:0warnings in touched code;host.rsuntouched (0diff lines).python -I tests/test_d3d12_switches.py(19/19passed) andpython -I scripts/check_no_game_data.py(rc=0).AI-assisted: authored with GPT-6.2 Astra and Claude / Gemini 4 Neon, benchmarked and verified with Gemini 4 Carbon (High), and independently reviewed with Gemini 4 Neon (XHigh).
Imported from ubiquitarianism/leonida. Originally posted by Kal0s-design on 2026-11-30T17:15:07Z.
Independent Windows check of
2bf6a3e60on currentmain1d4949d58(no conflict, merge871b3ce2d). Native Windows 11, MSVC x64, Rust 1.98.1, unit tests only (no title run).cargo test -p leonida-gpu --lib -- --test-threads=32, five runs in debug and one in--release: 597 passed, 0 failed, 53 ignored every time.mainlists 624 tests, the merge 650, so it adds 26.descriptor::tests::a_,published_shapes,old_wordsanda_table_tells, includinga_read_that_a_reuse_overtakes_is_read_againanda_slot_that_changes_under_every_read_is_left_to_the_lock. 0 failures in 200.GPUs tested: n/a for the run itself (unit tests; some make heaps on the local D3D12 device). AI-assisted: yes.
Imported from ubiquitarianism/leonida. Originally posted by KelboLive on 2026-12-01T05:17:49Z.
Review: COMMENTED
This is the Rust runtime (
descriptor.rs,descriptor/host.rs,resource.rs), not the C++ shim: 596/0 lib tests, the threesix_readers…stress tests pass (6 readers vs a writer freeing and reusing one entry, 50k rounds), and the shipped benchmark in release gives ~40 ns/draw combined withLEONIDA_LIST_LOCK_FAST=1against ~181 withLEONIDA_LIST_LOCK_FAST=0 LEONIDA_VIEW_CACHE=0— 4.5x here versus the 6.8-9.3x in the body, which is hardware-dependent; the direction is right.The seqlock is sound as written: writers (under
ViewsWrite) store the empty head, then the payload, then the full head, allRelease(descriptor.rs:1406,:1383,:1458); readers take head, payload and the head again withAcquireand compare the whole head, retrying up to three times before falling back toVIEWS.read()(:1484,:1395-1474,:1637-1652). The payloadAcquireis what makes the argument work — a reader that observes a word of a later view has thereby ordered the empty-head store before its second look, so it cannot validate the stale head.made_baseis only a hint and theRwLockread is the real edge (resource.rs:1933);PLACED_NEARholds addresses of resources kept for the run. Pages are leakedBoxes, so no slot moves or is freed under a reader.Mutation: dropping the second head check makes all three stress tests fail with mixed words of two views — so the structure is guarded. But relaxing the payload
Acquire/Releasestill passes everything on x86, because TSO supplies the order and every access stays atomic; the suite cannot catch an ordering regression here. That needs an ARM64 run (or a loom-style model ofSlot::view/hold), and the PR should say so.Two things before merge:
LEONIDA_LIST_LOCK_FASTis missing fromd3d12/docs/switches.md—tools/d3d12_switches.py --checkexits 1 withmissing from the page: LEONIDA_LIST_LOCK_FAST(the page also misses names that are already missing on main, but this one is new).PUBLISHED's pages are never freed: at the table limits that is up to 640 MiB, so state the expected high-water mark where the leak is intentional.Imported from ubiquitarianism/leonida. Originally posted by Neutron-0 on 2026-12-01T10:20:28Z.
A heads-up for this pull request: the page of D3D12 switches (
d3d12/docs/switches.md) is written bytools/d3d12_switches.pyfrom the sources ofd3d12/, and 1 name this pull request reads is not on the page yet:LEONIDA_LIST_LOCK_FAST.Nothing checks that today, so the page has drifted before (it was a name behind when this was measured). A test that checks it is waiting to be opened (DOCS-SWITCHES-PAGE-CHECK), and it would make this pull request's CI red until the page is written again. One command does that:
It writes the page with the names in it and nothing else; the diff is the new rows.
Imported from ubiquitarianism/leonida. Originally posted by zmasi on 2026-12-01T10:58:13Z.
Thanks for the heads-up @Neutron-0 — regenerated
d3d12/docs/switches.md(LEONIDA_LIST_LOCK_FAST,--checkrc 0) in96d3ad208.AI-assisted: Gemini 4 Carbon.
Imported from ubiquitarianism/leonida. Originally posted by aslan2142 on 2026-12-01T11:13:32Z.
Review: APPROVED
Thoroughly verified the lock-free publication and shared-read changes across
descriptor.rs,descriptor/host.rs,resource.rs, andswitches.md:Slot([AtomicU64; 16])seqlock ordering (Releaseon clear/payload/full-head underViewsWrite,Acquireon head/payload/head re-check with 27-bit turn + 30-bit generation) is sound and safely falls back toVIEWS.read()after 3 overtaken reads or pastENTRIES_PUBLISHED.Hosted::made(RwLock+made_baseatomic) keepslet_go_ofandwant_levelexclusive-writer-serialized and preservesusedmonotonicity viafetch_max.PLACED_NEARis properly gated behindplaced_shards() && list_lock_fast(), andd3d12/docs/switches.mdhas been updated forLEONIDA_LIST_LOCK_FAST.d3d12 (windows-latest)are green.(Non-blocking note echoing KelboLive's review: documenting the intentional leaked-page high-water mark for
PUBLISHEDand eventually testing non-TSO/ARM64 ordering or a loom model are good follow-ups.) Merging.(AI-assisted review.)