D3D12.X: a native host's last Release only queues the resource #2445
No reviewers
Labels
No labels
accessibility
bug
documentation
duplicate
enhancement
goal:world-load
good first issue
help wanted
invalid
keep-open
off-goal
question
stale
test-only
wontfix
No milestone
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
github/xcl-dev--xcl!2445
Loading…
Reference in a new issue
No description provided.
Delete branch "github-bot/gh-2445"
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?
@Ahmet-Dedeler on GitHub · 2026-11-25 19:08 UTC · original
Ahmet-Dedeler:d3d12x/release-native-host→main· +429 −26 · 2 files · 3 commits · openStacked on #1789; only the top two commits belong here (
D3D12.X: a resource's last Release forgets its address in one functionandD3D12.X: a native host's last Release only queues the resource). The bottom one is #1789's commit, @vakaobr's, rebased onto currentmain(#1789's base predates a change tores_releasethat the second commit builds on). Part of #258.Apple's D3DMetal calls
Releaseon threads of its own (its queue thread at submit) and from inside calls XCL makes while XCL holds a lock of its own. When that is the title's lastReleaseand late release keeps the object,res_releasetook the maps' locks, the sparse state's and the texture table's, andlate_release::holdsignalled the queues and waited. Whatever the host's thread waits for there, its queue waits for too. On a native host, such aReleasenow only writes the resource into a ring of atomics and returns. The late-release thread forgets what XCL keeps by the address and then lets the object go. This is the ring frommac7'sfd1e593, which @vakaobr asked to see opened onmainin #258 so his host-thread part can go on top. That part (no hostReleaseat all on a host thread) is not in this PR.The gate needs no other open PR.
class_foralready callsmodule_ofon the table's first method. When that is 0 (the host's D3D12 is in no PE module), it setsNATIVE_HOSTonce and starts the late-release thread there, on the adopting thread. D3D12 on Windows and vkd3d-proton are PE modules. There the flag is never set, the ring stays empty (drain_knownreads two atomics) and aReleasedoes what it did.What is in it
crates/xcl/src/hle/d3d12x/real.rs: the body ofres_releasethat forgets a released resource moves intoforget_released, unchanged (first commit, no behaviour change).crates/xcl/src/hle/d3d12x/real.rs:NATIVE_HOST, set inclass_forfrommodule_of(...) == 0. With it set, a kept lastReleasecallslate_release::hand_overand returns 0. Testsonly_a_table_in_no_pe_module_makes_a_native_hostanda_kept_last_release_on_a_native_host_waits_for_no_lock.crates/xcl/src/hle/d3d12x/late_release.rs:Ring(16,384 slots; the indices only grow; a writer claims an index with one compare-exchange, only while it is less than N past the tail, so a claimed slot is always one the reader has emptied; one reader at a time, which empties a slot before it moves the tail).hand_over(atomics only; with the ring full the reference is kept for good and counted, never the locking path).drain_knowntakes the ring first: it forgets each resource, then holds it like any other.start_threadis split out ofhold.Tested
x86_64-pc-windows-gnu(mingw-w64 GCC 16.2.0), debug, DXC 1.9.2609.CIworkflow (Windows runner, no GPU) on this head6df8f64, run on my fork because it waits for approval here: Build and test passed.cargo build --workspace --target x86_64-pc-windows-gnuat each of the two commits: builds, no new warnings (the same 3 asmain).<xcl lib test exe> --exact <test> --test-threads=1): passed:late_releasetests: the 4 new ring tests (on6df8f64), plusa_held_resource_goes_when_the_queue_is_past_its_batchandwithout_queues_a_batch_goes_at_once;real::native_host_tests;title_textures::tests::gpu_mirror_upload_follows_cpu_writes_and_later_mapping_changes;sparse::tests::a_buffer_released_while_the_state_is_held_does_not_wait_for_it.a_kept_last_release_on_a_native_host_waits_for_no_lockwith thehand_overbranch taken out ofres_releasefails: "the last Release waited for a lock another thread held: Timeout".d3d12.dll, so the gate test checked the PE-host side. The native path ran through the test's own switch (native_host_tests::FORCED, a thread-local read only after the flag).Regression run
From this Mac,
scripts/smoke.ps1unchanged under PowerShell, with a wrapper as-Xclthat startsxcl.exeunder Wine, both from the same copy of the caches. On this hostmainshows no frame (the title stops about 3 s in, before it creates its device), so neither run reaches a title'sRelease: this pair shows that the build starts and ends asmaindoes.main:
PR:
(
peak commitis 0 because Wine reports no commit charge.)Not tested
main, and the gate on a real D3DMetal table in a test. (In this bottle XCL's own device goes throughd3d12.dll, a PE module, so the gate test saw that side. In earlier retail runs on this Mac the tables of resources, queues, lists and fences were at addresses in no module,PC vtable 0x21a6…in the log, which is what the gate keys on.) Without #1904,maindoes not get far on D3DMetal. Onmac7, the loading runs I have predate this change.AI use
@SlopTart on GitHub · 2026-11-25 20:02 UTC · original
gpu_suite (cold cache): 160 passed / 4 failed, no crashes. The four are the known baseline on this lane (seed-1003 fuzz, mad/mac contraction, depth-buffer state refusal, andgpu_promoted_uav_clear_values_pass_create_committed_resource(report); none caused by this PR.@DavidM1911 on GitHub · 2026-11-25 20:16 UTC · original
Verification on RTX 3060 Ti (Windows 11), branch merged with current
main:cargo test -p xcl --lib: 1038 passed, 1 failed in one parallel run; the failing test passes on a single-threaded rerun (1039/0). Intermittent single-test flake / 0xc0000005 abort under parallel scheduling — tracked in #1978; clean on single-threaded rerun.Thanks!
@Ahmet-Dedeler on GitHub · 2026-11-25 21:17 UTC · original
@bmjask you're right, thanks. The check and the
fetch_addwere two steps, and nothing bounded how many writers could pass the check at once.Fixed in
6df8f64(squashed into the ring commit):Ring::pushnow claims an index with onecompare_exchange_weakonhead, and only whilehead - tail < N, so writers arriving at once can't claim past the tail together. The tail is still stored only after the reader has emptied the slot, so a claimed slot is always an empty one.RING_WRITERSand its margin are gone, and the full ring is exactly N slots now.New test
writers_at_once_past_the_free_room_overwrite_nothing: a 512-slot ring with 255 entries, then 300 writers released by one barrier. It checks that exactly 257 are accepted, that the 255 already in the ring come out unchanged and in order, and that every accepted resource comes out once. With the oldpushthat's more writers than the 256 reserve. The other three ring tests pass on the new head too (under Wine,--exact, one process each), and the description has the smoke block for6df8f64.@fromthemoonx on GitHub · 2026-11-25 20:19 UTC · original · edited 2026-11-25 22:14 UTC
Rerun on the new head
6df8f64: passes on NVIDIA, and the ring tests are stable. The newwriters_at_once_past_the_free_room_overwrite_nothingcatches the old check-then-fetch_addpush, but only rarely here: 1 of 340 runs.cargo test --workspace --no-fail-fast -- --test-threads=4,6df8f64merged ontomaine1d84b7(clean)SKIPPEDlines6df8f64a_bridged_mask_write_where_one_half_is_elsewhere_meets_no_barrier, from the base3ceb550(fixed onmainbyc828d8c), not this PRwriters_at_once_past_the_free_room_overwrite_nothing,writers_and_readers_at_once_…,a_full_ring_…, alone, 40 runs eachlate_release::+real::native_host_tests::(8 tests),--test-threads=8, 40 runspushput back as a check followed byfetch_add(scratch)exactly the free slots are claimed, left258, right257So the new test can catch the race, but on an 8-core Ryzen it almost never does: the 300 writers rarely reach the check in the same window. A test that holds writers between the check and the claim (or loops the barrier round many times in one run) would fail reliably. The compare-exchange itself looks right to me by reading.
First run, on
6979e9f: Windows run: passed on NVIDIA with the whole suite, the ring and native-host tests were stable under repeats, and on Windows the gate stays off (the e2e title's log has no native-host line, and its debug-layer messages are the same asmain's). This covers the "Windows and the whole suite" item under Not tested.Tested:
6979e9f, and the same commit merged ontomaine0d93c8(clean;real.rsauto-merged). RTX 3060 12 GB (driver 610.88), Ryzen 7 5700X, Windows 11 Pro 26300, Rust 1.100.0 MSVC (x86_64-pc-windows-msvc), debug, Graphics Tools (D3D12 debug layer) installed.cargo test --workspace --no-fail-fast -- --test-threads=4, mergedSKIPPEDlines (no wave64, refused per-lane forms)6979e9fa_bridged_mask_write_where_one_half_is_elsewhere_meets_no_barrier, because the base3ceb550predates its fix onmain(c828d8c); not this PRlate_releasering tests and the 2native_host_tests, alone, 40 runs eachhle::d3d12x::late_release::+hle::d3d12x::real::(66 tests),--test-threads=8, 30 runse2etest_title_draws_with_a_root_signature_under_the_debug_layer, merged vsmaine0d93c8the host's D3D12 is in no PE moduleappears in neither logNot tested: a title on Windows with this branch, a release build, AMD / Intel, Wine / D3DMetal (nothing here).
AI use: Claude Code (Claude Opus 5.5) ran the tests and the debug-layer comparison, and wrote this report.
@DavidM1911 on GitHub · 2026-11-26 01:50 UTC · original
Re-verification on RTX 3060 Ti (Windows 11), new head merged with current
main:cargo test -p xcl --lib: all passed, 0 failed, 27 ignored. The earlier merge conflict is resolved.Thanks!
— Devin
@bmjask on GitHub · 2026-11-25 20:18 UTC · original · reviewed
P1: Ring::push has a concurrent reservation race. late_release.rs:172-179 checks head-tail and then separately fetch_adds tail; RING_WRITERS=256 reserves space only under a bounded simultaneous-writer assumption, but no such bound is enforced. For example with N=512 and used=255, 258 writers can all observe occupancy below N-256 before incrementing; the last claim reaches tail+N and writes slots[tail % N] before the reader consumes it, overwriting a pending resource. That loses its pointer/reference and leaves its XCL address state unforgotten. The new 4-writer stress test cannot cover this. Please use CAS reservation/a bounded MPMC queue or enforce the writer bound, with a barrier test exceeding the reserve.
@luckyteagirl on GitHub · 2026-11-26 07:28 UTC · original · reviewed
late_release.rs:171–176: the capacity check can treat an empty ring as full because it samplesheadbeforetail. Start withhead = tail = 0: producer A readsi = 0and pauses; B claims slot 0; the reader drains it and publishestail = 1(take, line 204). A then reads that tail, so0usize.wrapping_sub(1) == usize::MAXand returnsfalseeven thoughhead = tail = 1.hand_over_tocounts the rejected resource as kept for good (lines 224–229), so an ordinary two-producer/one-reader interleaving leaks a resource and retains its address bookkeeping without the ring ever being full. The newer CAS reservation fixes the writer-overflow race, but not this stale-head/fresh-tail snapshot.Could the check read
tailbefore the head sample, or refreshheadbefore treating an apparent wrap as full, with a deterministic barrier test after A's head load? This is source-derived at exact head6df8f647253b3e2cb071543afce1248be3a4f08f; a CPU arithmetic probe confirmed theusize::MAXresult, but I have not run the XCL ring test or a native-host title. The behavior is inherited by stacked #2498. AI-assisted review with Codex.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.