Skip to content

D3D12.X: charge the compute-blob budget once per cache entry - #826

Draft
mikebuckets171 wants to merge 1 commit into
xcl-dev:mainfrom
mikebuckets171:pr/blob-cache-accounting
Draft

mikebuckets171 wants to merge 1 commit into
xcl-dev:mainfrom
mikebuckets171:pr/blob-cache-accounting

Conversation

@mikebuckets171

@mikebuckets171 mikebuckets171 commented Nov 24, 2026 •

Copy link
Copy Markdown

What

keep_compute_blob charges a driver blob's size to the COMPUTE_BLOBS_MAX total and then inserts it.
Two calls for the same key can pass the compute_blob(key).is_some() guard above before either has
stored its result (the guard and the insert are not one step): both charge, only one entry ends up in
the map, and the discarded charge is never taken back — forget_compute_blob subtracts only what the
map holds. The budget then drifts upward with bytes the map never kept, and later blobs of real
programs are refused while memory is free.

Insert through the entry API and charge a vacant key only. insert_compute_blob is split out so the
accounting is testable without a GPU; the new test is CPU-only.

Size: +35 / −3, one file (crates/xcl/src/hle/d3d12x/real.rs), one logical change.

Note on ordering: #774 changes the call site around keep_compute_blob (a refused driver blob going to
the big-compile queue). It does not touch the charge or the insert, so the two are independent, but
whichever lands first I will rebase onto.

Tested

  • Builds on macOS (Apple silicon) for the Wine target x86_64-pc-windows-gnullvm (LLVM-MinGW 20261117 as
    the linker): main at e50836b and this head — both green in 0.1–20 s incremental.
  • The unit tests, run under Wine (CrossOver 26 bottle, D3DMetal), lib unit binary of this head, filtered
    to the module this patch is about: compute_blob_tests → 2 passed, 0 failed (the new
    two_results_for_one_key_charge_the_budget_once, which asserts that the second result for one key
    charges nothing and that the first blob is the one kept, plus the existing key/forget test).
  • The same binary unfiltered on this host: 188 tests started, 14 failed, 0 skipped before a 300 s cap —
    all in the GPU families this host is known for (the v_*_f64 programs and several graphics tests), the
    same ones the other macOS reports in macOS (Apple Silicon, CrossOver + D3DMetal): the newer retail game reaches its first-run menus at 30 fps #258 name. None of them reach the code this patch changes; the
    section that runs on the CPU passes.
  • A 60 s xcl-smoke pair from this bench (same title, same bottle, same sandbox, same DXC 1.9.2609, each
    on a fresh data root), one on a commit of main, one on this head:
xcl-smoke v1
commit: e50836bdf6f5c026f281d545ed86fb2f07ba6e6a
gpu: AMD Compatibility Mode
title: retail (Xbox Series X|S package, private drive)
seconds: 60
exit: 0x0
frames shown: 0 (PresentX 0)
presents per 2 s (last 3): 0 0 0
device removals: 0
crashes: 0
peak commit: 0 MB
xcl-smoke v1
commit: 991b4da1a0dc6b299410f3d6981d3bd84ad1224c
gpu: AMD Compatibility Mode
title: retail (Xbox Series X|S package, private drive)
seconds: 60
exit: 0x0
frames shown: 0 (PresentX 0)
presents per 2 s (last 3): 0 0 0
device removals: 0
crashes: 0
peak commit: 0 MB

Read these as what they are: on this host they show that the head adds no device removals, no crashes and
no commit growth — not frame parity. main cross-built for the Wine target does not reach a presented
frame at all (it stops ~1.8 s in with 0 presents and no PSO work; main has no Mac host path yet), so
both sides of a main-based Mac pair are 0 frames. The host where this title does run is @Ahmet-Dedeler's
Mac port; COMPUTE_BLOBS does not exist on that port's base, so there is no pair to take there for this
patch. The package is on a private drive, so its name is not put here.

  • Cross-built on Windows with the whole suite, by other contributors, with the counts CONTRIBUTING item 2
    asks for (quoted from their comments below, not run by this bench):
    • @dcastell1 — Windows 11, RTX 5090, MSVC, applied on main@beaa245: cargo test --workspace -- --test-threads=4 → 738 passed, 0 failed, 41 ignored (one more passing test than main on the
      same box).
    • @SlopTart — Linux / RX 7900 XT, cross x86_64-pc-windows-gnu, head on main@a1672ef: 705 passed,
      11 failed, 41 ignored
      , the 11 being that host's known Wine-lane set, "nothing here fails because of
      your change".
  • A review by @iggyvilla found a clause in the new doc comment that was not true (a blob forgotten and
    built again is vacant again and charged once, because forget_compute_blob removes and subtracts under
    one lock). It is corrected in 991b4da, which is why this head was re-pushed and its block re-run.

Not tested

AI use

  • GPT-6 Luna, for the patch and its test, from a diagnosis made on a macOS bench.

@dcastell1

Copy link
Copy Markdown

Verified on Windows 11 10.0.26300, NVIDIA GeForce RTX 5090 (driver 621.24), Rust 1.100.0 MSVC 14.44.35207, debug build, applied on main@beaa245:

patch -p1 < this PR            # clean, no fuzz
cargo test --workspace -- --test-threads=4
test result: 738 passed; 0 failed; 41 ignored   (20 blocks, exit 0)

That is one more passing test than main on the same box (737), so the compute-blob budget charged once per cache entry runs green here and nothing else in the workspace changes verdict. No game data, hardware adapter, no Wine.

AI-assisted (opencode): applied the patch to a built tree, ran the suite, restored the two files.

@SlopTart

Copy link
Copy Markdown

Linux / AMD RDNA3 datapoint.

Merged state: head 5aa4c42 onto main a1672ef, clean merge.

Full suite, cargo test --workspace --release --no-fail-fast --target x86_64-pc-windows-gnu -- --test-threads=4: 705 passed, 11 failed, 41 ignored. Failures are the known Wine-lane set from my #42 rows plus #718 trio members; nothing here fails because of your change. Compute-blob budget accounting tests green; no new failures.

Not tested: retail titles (none here), Windows, RDNA4.
Counts only, no game data.

Machine: RX 7900 XT (RADV NAVI31), Mesa 26.2.5, kernel 7.2 (Arch), Proton Experimental (wine-11.0), patched vkd3d-proton v3.1, Rust cross x86_64-pc-windows-gnu, XCL_NETWORK=0, release profile, --test-threads=4. Linux rows: #42 / #718.

AI use: Hermes agent (local Qwen-class) ran the suite and drafted this; I checked it before posting.

@iggyvilla

Copy link
Copy Markdown
Contributor

Read of 5aa4c42 (not built or run by me): the fix is right. keep_compute_blob runs on the xcl-pso workers, the compute_blob(key).is_some() check and the insert are two separate lock acquisitions, and charging only the Entry::Vacant case keeps total equal to what the map holds.

One sentence in the doc comment of insert_compute_blob is not true: "a program built again after its blob was forgotten reaches this call a second time: inserting in both cases charges the budget for bytes the map does not hold". forget_compute_blob removes the entry and subtracts its length from total in the same lock, so a later insert for that key is vacant and is charged once, correctly; only two workers inserting the same key at once ever double-charged. I would drop that clause so the comment names the one case the change fixes.

AI use: Claude Code, Claude Sonnet 5.5, read the diff and checked it against forget_compute_blob; the comment is mine.

keep_compute_blob checks compute_blob(key) and then inserts, two separate lock acquisitions: two
xcl-pso workers can both pass the check for the same pipeline, both charge the COMPUTE_BLOBS_MAX
total, and only one entry ends up in the map. The discarded charge is never taken back -
forget_compute_blob subtracts only what the map holds - so the budget drifts upward with bytes the
map never kept and later blobs of real programs are refused while memory is free.

Insert through the entry API and charge a vacant key only, which keeps the total equal to what the
map holds. insert_compute_blob is split out so the accounting is testable without a GPU; the new
test is CPU-only.
@mikebuckets171
mikebuckets171 force-pushed the pr/blob-cache-accounting branch from 5aa4c42 to 991b4da Compare November 24, 2026 07:42
@mikebuckets171

Copy link
Copy Markdown
Author

@iggyvilla you are right and it is fixed: forget_compute_blob removes the entry and subtracts its length under the same lock, so a blob built again after being forgotten is vacant and is charged once, correctly. The double charge only ever happens between two workers that both pass the compute_blob(key).is_some() check. The doc comment now names that one case and says explicitly that the forgotten-then-rebuilt case is correct (991b4da). The head's xcl-smoke block was re-run on the new commit; the suite counts from @dcastell1 and @SlopTart are quoted in the description so the gate sees them where the PR is read. Thanks for reading the diff closely.

@mikebuckets171

Copy link
Copy Markdown
Author

Premise re-checked against today's main, because real.rs moved under this PR after it was filed - the change still describes main accurately.

Since this was filed, real.rs absorbed at least four merges (5c3ff31, 11920b5, e7da8a1, 285560f). Re-read at 6efdabc:

  • keep_compute_blob is at real.rs:3640. It still takes the lock, checks only capacity - if bytes.is_empty() || *total + bytes.len() > COMPUTE_BLOBS_MAX { return; } (3654) - then charges *total += bytes.len() (3657) and inserts m.insert(key, std::sync::Arc::new(bytes)) (3658), with no check of whether key is already charged.
  • The vacancy question is still asked earlier and elsewhere: the caller does let mut cached = compute_blob(key) at real.rs:3420 and calls keep_compute_blob(key, *ppv, dxil.len()) at 3449. Two workers can still both find the key vacant and both charge for it, which is what this PR closes.
  • New since filing: line 3450 re-reads the map after keeping (if let (Some(p), Some(b)) = (disk.as_deref(), compute_blob(key))) to write the blob to the disk cache. That interacts with this change but does not fight it: this patch keeps the first blob stored for a key, so the disk write takes the same blob the map holds. No behavioural fork between the two paths.

git merge-tree of 6efdabc against pr/blob-cache-accounting reports no conflict, so the branch needs no rebase for the Windows pair @Kalmarv offered - head 991b4da onto 6efdabc is a clean merge. PR checks is green here; CI has never started on any of my heads (only the PR checks description job appears, which is main's own pull_request_target workflow), so a maintainer's workflow approval is still what it needs to leave draft.

Host: macOS 27.2.1 (Apple silicon, M5 Pro), CrossOver 26, D3DMetal - the port-side tests behind this PR's Tested section were run here. AI use: GPT-6 Luna re-read the code at 6efdabc, ran the merge check and drafted this comment; no new runs were needed for it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants