Recompiler: title-thread lazy compute builds bypass BIG_COMPILES/GIANT_COMPILES caps, dxc::wait_for_memory TOCTOU race, and pso.rs compact/validation cache gaps #2463

Open
opened 2026-11-26 05:31:21 +00:00 by github-bot · 5 comments
Owner

@⁠microwave805 on GitHub · 2026-11-25 19:30 UTC · original

Static audit on main @ c7e1799 found three compile-gating / cache-safety bugs on the shader pipeline compilation path. Part of #1269, Part of #1566. (Note: open PR #2424 separately covers inline split helper parent = None in pso_async::build_pieces, and #1409 already covers XCL_SAFE_BARRIER=0.)

1. Title-thread lazy compute builds completely bypass BIG_COMPILES / GIANT_COMPILES OOM caps

  • Where: crates/xcl/src/hle/d3d12x/real.rs:3976-3986, 4047 (driver_compile_identified), 5909 (finish_two_pass); crates/xcl/src/hle/d3d12x/pso_async.rs:1032-1056 (resolve_wait_for)
  • Root cause: driver_compile_identified (real.rs:4047) and finish_two_pass (real.rs:5909) only gate threads whose name starts with "xcl-pso":
    let huge = gate && bytes >= huge_pso_bytes() && std::thread::current().name().is_some_and(|n| n.starts_with("xcl-pso"));
    
    Under default settings (XCL_LAZY_PSO=1, XCL_PSO_WAIT_MS unset → wait_budget() == None), resolve_wait_for (pso_async.rs:1043-1046) runs job() synchronously on the calling title thread (not named "xcl-pso*"). Thus huge == false and concurrent title-thread lazy builds (or a title-thread giant build alongside a worker's giant build) completely bypass both BIG_COMPILES (wanted_most = 2) and GIANT_COMPILES (wanted_most = 1), allowing unbounded concurrent giant driver compiles → OOM.
  • Fix direction: Remove the starts_with("xcl-pso") exemption at real.rs:4047, 5909 and pass wanted = !pso_async::building_here() || pso_async::build_is_wanted() to big_compile_turn (so title threads bypass background commit waits while still respecting wanted_most).

2. dxc::wait_for_memory TOCTOU race admits bursts of compiles past WANTED_AT_ONCE and commit-headroom caps

  • Where: crates/xcl/src/gpu/recomp/dxc.rs:399, 444-462 (wait_for_memory)
  • Root cause: Lines 451-456 load WORKING.load(Ordering::Acquire) to check plenty and WORKING < WANTED_AT_ONCE, then unconditionally execute WORKING.fetch_add(1, Ordering::AcqRel) outside any lock or CAS loop (unlike line 457's compare_exchange(0, 1, ...)). When a burst of worker threads wakes up simultaneously under tight commit headroom, every thread sees WORKING == 0 and enters fetch_add(1) at once, spiking commit past OOM.
  • Fix direction: Guard the headroom check + WORKING increment with a Mutex or CAS loop.

3. pso.rs translated / refusal cache (#1474): direct_output() omits compact_nest_tag(), and production cached_rerouted / store_in skip container validation

  • Where: crates/xcl/src/gpu/recomp/pso.rs:263-266, 344-363 (store_in), 456-458 (cached_in), 491-506 (direct_output), 535-554 (cached_rerouted)
  • Root cause:
    1. identity() (pso.rs:263-266) includes compact_nest_tag() (XCL_DIRECT_COMPACT_NEST) only when Backend::current() != Backend::Hlsl, while direct_output() (pso.rs:491-506) omits compact_nest_tag() completely. Under default HLSL mode, enormous (> 8 MiB HLSL) compute shaders auto-rerouted to direct_compute (pso.rs:602-617) collide on the same rerouted .bin and .err paths across different XCL_DIRECT_COMPACT_NEST settings.
    2. Production cached_rerouted (pso.rs:540-547) omits the valid_dxil_container(&c.dxil) guard enforced in cached_in (pso.rs:456-458), and store_in (pso.rs:345-363) validates only &c.dxil and not chain.pieces (whereas load_in at pso.rs:334 rejects invalid pieces).
  • Fix direction: Include compact_nest_tag() in direct_output(), and enforce valid_dxil_container on c.dxil and all dispatch_chain.pieces in both cached_rerouted and store_in.

AI use

  • Antigravity (Gemini 4 Neon): static code audit on main @ c7e1799 and issue drafting.
**@⁠microwave805** on GitHub · 2026-11-25 19:30 UTC · [original](https://github.com/xcl-dev/xcl/issues/2463) Static audit on `main @ c7e1799` found three compile-gating / cache-safety bugs on the shader pipeline compilation path. Part of #1269, Part of #1566. *(Note: open PR #2424 separately covers inline split helper `parent = None` in `pso_async::build_pieces`, and #1409 already covers `XCL_SAFE_BARRIER=0`.)* ### 1. Title-thread lazy compute builds completely bypass `BIG_COMPILES` / `GIANT_COMPILES` OOM caps - **Where:** `crates/xcl/src/hle/d3d12x/real.rs:3976-3986, 4047` (`driver_compile_identified`), `5909` (`finish_two_pass`); `crates/xcl/src/hle/d3d12x/pso_async.rs:1032-1056` (`resolve_wait_for`) - **Root cause:** `driver_compile_identified` (`real.rs:4047`) and `finish_two_pass` (`real.rs:5909`) only gate threads whose name starts with `"xcl-pso"`: ```rust let huge = gate && bytes >= huge_pso_bytes() && std::thread::current().name().is_some_and(|n| n.starts_with("xcl-pso")); ``` Under default settings (`XCL_LAZY_PSO=1`, `XCL_PSO_WAIT_MS` unset → `wait_budget() == None`), `resolve_wait_for` (`pso_async.rs:1043-1046`) runs `job()` synchronously on the calling title thread (not named `"xcl-pso*"`). Thus `huge == false` and concurrent title-thread lazy builds (or a title-thread giant build alongside a worker's giant build) completely bypass both `BIG_COMPILES` (`wanted_most = 2`) and `GIANT_COMPILES` (`wanted_most = 1`), allowing unbounded concurrent giant driver compiles → OOM. - **Fix direction:** Remove the `starts_with("xcl-pso")` exemption at `real.rs:4047, 5909` and pass `wanted = !pso_async::building_here() || pso_async::build_is_wanted()` to `big_compile_turn` (so title threads bypass background commit waits while still respecting `wanted_most`). ### 2. `dxc::wait_for_memory` TOCTOU race admits bursts of compiles past `WANTED_AT_ONCE` and commit-headroom caps - **Where:** `crates/xcl/src/gpu/recomp/dxc.rs:399, 444-462` (`wait_for_memory`) - **Root cause:** Lines `451-456` load `WORKING.load(Ordering::Acquire)` to check `plenty` and `WORKING < WANTED_AT_ONCE`, then unconditionally execute `WORKING.fetch_add(1, Ordering::AcqRel)` outside any lock or CAS loop (unlike line 457's `compare_exchange(0, 1, ...)`). When a burst of worker threads wakes up simultaneously under tight commit headroom, every thread sees `WORKING == 0` and enters `fetch_add(1)` at once, spiking commit past OOM. - **Fix direction:** Guard the headroom check + `WORKING` increment with a `Mutex` or CAS loop. ### 3. `pso.rs` translated / refusal cache (#1474): `direct_output()` omits `compact_nest_tag()`, and production `cached_rerouted` / `store_in` skip container validation - **Where:** `crates/xcl/src/gpu/recomp/pso.rs:263-266, 344-363` (`store_in`), `456-458` (`cached_in`), `491-506` (`direct_output`), `535-554` (`cached_rerouted`) - **Root cause:** 1. `identity()` (`pso.rs:263-266`) includes `compact_nest_tag()` (`XCL_DIRECT_COMPACT_NEST`) only when `Backend::current() != Backend::Hlsl`, while `direct_output()` (`pso.rs:491-506`) omits `compact_nest_tag()` completely. Under default HLSL mode, `enormous` (> 8 MiB HLSL) compute shaders auto-rerouted to `direct_compute` (`pso.rs:602-617`) collide on the same `rerouted` `.bin` and `.err` paths across different `XCL_DIRECT_COMPACT_NEST` settings. 2. Production `cached_rerouted` (`pso.rs:540-547`) omits the `valid_dxil_container(&c.dxil)` guard enforced in `cached_in` (`pso.rs:456-458`), and `store_in` (`pso.rs:345-363`) validates only `&c.dxil` and not `chain.pieces` (whereas `load_in` at `pso.rs:334` rejects invalid pieces). - **Fix direction:** Include `compact_nest_tag()` in `direct_output()`, and enforce `valid_dxil_container` on `c.dxil` and all `dispatch_chain.pieces` in both `cached_rerouted` and `store_in`. ### AI use - Antigravity (Gemini 4 Neon): static code audit on `main @ c7e1799` and issue drafting.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 05:31:21 +00:00
Author
Owner

@⁠itsSio on GitHub · 2026-11-25 22:26 UTC · original · edited 2026-11-25 22:51 UTC

Completed item 1 of #2463 on my fork (itsSio/xcl#4, branch d3d12x/title-thread-compile-gate, commit 5a60764a), ready as a fork draft PR pending paired retail scripts/smoke.ps1 runs before upstream submission.

Implemented (5a60764a)

  • crates/xcl/src/hle/d3d12x/pso_async.rs (+7/-0 runtime, +22/-0 tests): added pub(super) fn wanted_compile() -> bool { !building_here() || build_is_wanted() } and unit test title_threads_always_report_wanted_compile_while_workers_follow_demand.
  • crates/xcl/src/hle/d3d12x/real.rs (+6/-6 runtime, +102/-14 tests): removed the starts_with("xcl-pso") thread-name exemption in driver_compile_identified and finish_two_pass, wired wanted_compile into big_compile_turn, updated gate_note, and replaced small_and_title_thread_compiles_bypass_the_gate with unit tests verifying small/cached bypasses, title-thread gating/caps across BIG_COMPILES / GIANT_COMPILES, and commit-wait bypass.

Tested (debug build, clean commit 5a60764a7526e14aa7d49ba82539b25248c87e03)

  • Main PC — NVIDIA GeForce RTX 5080 (Win11 26200, Ryzen 7 9800X3D, driver 32.0.16.1088, rustc 1.97.0, DXC 1.9.2609.5):
    • cargo test --workspace -- --test-threads=4: 1426 passed, 0 failed, 46 ignored (26 SKIPPED: 22 wave64, 3 #1426 f16/cvt per-lane refused, 1 no heap windows).
    • big_compile_tests & pso_async: 13 passed, 0 failed, 0 ignored and 15 passed, 0 failed, 0 ignored (0 SKIPPED).
  • Main PC — AMD Radeon Graphics (RDNA2 iGPU, native wave64, XCL_ADAPTER=radeon, driver 32.0.21043.5001):
    • big_compile_tests (13/0/0), pso_async (15/0/0), gpu_pso_e2e (7/0/0), gpu_compute_e2e (6/0/0, 1 SKIPPED: no heap windows).
  • Build PC — NVIDIA GeForce RTX 2080 SUPER (Win11 26200, Ryzen 5 5600X, 15.91 GB RAM, driver 610.88, rustc 1.99.1, SDK DXC 1.8.2502.11):
    • cargo test --workspace -- --test-threads=4: 1426 passed, 0 failed, 46 ignored (29 SKIPPED: 22 wave64, 3 #1426 per-lane refused, 1 no heap windows, 2 no debug layer DLL, 1 no developer mode for driver-cache disable).
    • big_compile_tests (13/0/0), pso_async (15/0/0), gpu_pso_e2e (7/0/0).

Not tested

  • Retail title scripts/smoke.ps1 regression runs (not yet available), AMD RDNA4 / discrete AMD GPUs, Intel GPUs.

AI use
Ran via Samaritan:

  • Antigravity (Gemini 4 Neon (High)): session coordination, task scoping, and review/release oversight.
  • Antigravity (Gemini 4 Hydrogen (High)): #1833/#2463 overlap scouting and design, implementation and unit tests, independent code review, dual-PC build/test execution, and report drafting.
**@⁠itsSio** on GitHub · 2026-11-25 22:26 UTC · [original](https://github.com/xcl-dev/xcl/issues/2463#issuecomment-7176355168) · edited 2026-11-25 22:51 UTC Completed item 1 of `#2463` on my fork ([itsSio/xcl#4](https://github.com/itsSio/xcl/pull/4), branch `d3d12x/title-thread-compile-gate`, commit `5a60764a`), ready as a fork draft PR pending paired retail `scripts/smoke.ps1` runs before upstream submission. **Implemented (`5a60764a`)** - `crates/xcl/src/hle/d3d12x/pso_async.rs` (+7/-0 runtime, +22/-0 tests): added `pub(super) fn wanted_compile() -> bool { !building_here() || build_is_wanted() }` and unit test `title_threads_always_report_wanted_compile_while_workers_follow_demand`. - `crates/xcl/src/hle/d3d12x/real.rs` (+6/-6 runtime, +102/-14 tests): removed the `starts_with("xcl-pso")` thread-name exemption in `driver_compile_identified` and `finish_two_pass`, wired `wanted_compile` into `big_compile_turn`, updated `gate_note`, and replaced `small_and_title_thread_compiles_bypass_the_gate` with unit tests verifying small/cached bypasses, title-thread gating/caps across `BIG_COMPILES` / `GIANT_COMPILES`, and commit-wait bypass. **Tested (debug build, clean commit `5a60764a7526e14aa7d49ba82539b25248c87e03`)** - **Main PC — NVIDIA GeForce RTX 5080** (Win11 26200, Ryzen 7 9800X3D, driver `32.0.16.1088`, rustc 1.97.0, DXC 1.9.2609.5): - `cargo test --workspace -- --test-threads=4`: **1426 passed, 0 failed, 46 ignored** (26 `SKIPPED`: 22 wave64, 3 `#1426` f16/cvt per-lane refused, 1 no heap windows). - `big_compile_tests` & `pso_async`: **13 passed, 0 failed, 0 ignored** and **15 passed, 0 failed, 0 ignored** (0 `SKIPPED`). - **Main PC — AMD Radeon Graphics (RDNA2 iGPU, native wave64, `XCL_ADAPTER=radeon`, driver `32.0.21043.5001`):** - `big_compile_tests` (**13/0/0**), `pso_async` (**15/0/0**), `gpu_pso_e2e` (**7/0/0**), `gpu_compute_e2e` (**6/0/0**, 1 `SKIPPED`: no heap windows). - **Build PC — NVIDIA GeForce RTX 2080 SUPER** (Win11 26200, Ryzen 5 5600X, 15.91 GB RAM, driver 610.88, rustc 1.99.1, SDK DXC 1.8.2502.11): - `cargo test --workspace -- --test-threads=4`: **1426 passed, 0 failed, 46 ignored** (29 `SKIPPED`: 22 wave64, 3 `#1426` per-lane refused, 1 no heap windows, 2 no debug layer DLL, 1 no developer mode for driver-cache disable). - `big_compile_tests` (**13/0/0**), `pso_async` (**15/0/0**), `gpu_pso_e2e` (**7/0/0**). **Not tested** - Retail title `scripts/smoke.ps1` regression runs (not yet available), AMD RDNA4 / discrete AMD GPUs, Intel GPUs. **AI use** Ran via Samaritan: - Antigravity (Gemini 4 Neon (High)): session coordination, task scoping, and review/release oversight. - Antigravity (Gemini 4 Hydrogen (High)): #1833/#2463 overlap scouting and design, implementation and unit tests, independent code review, dual-PC build/test execution, and report drafting.
Author
Owner

@⁠KOLESNiii on GitHub · 2026-11-26 00:52 UTC · original

Related admission issue on amd-dev at 6a0bb5b4f294b0729771ed457af1f96c01f5c06b. The new 16-per-frame deferred-dispatch limit checks has_slot under the mutex, drops the lock for split, and records the allocation afterward under a second lock. At a count of 15, two recording threads can both pass the check before either records its allocation. If both splits succeed, the count reaches 17. The existing unit test checks the serial path. Admission and allocation, current test.

A standalone test using the branch's FrameDeferCap and a barrier between the check and increment reached 17 with two threads. The narrow fix would reserve under the first lock and give back the reservation on failed split for that same present, leaving driver calls outside the lock. This demonstrates a cap overshoot. Windows allocator behavior and the effect on memory or presents remain unmeasured.

The same AMD head has a separate offline admission problem. Pipelines::make_one increments the shared running count before taking the 900 KiB cold-compile mutex. With four driver slots and four cold giant jobs, one can be in the driver while three hold the remaining shared slots waiting for the giant mutex. A fifth, small job then cannot reach the driver. This scheduling consequence follows from the lock order. Whether it explains any reported offline stall is unmeasured. For #2179 or the branch equivalent, hold shared slots only for jobs ready to issue the native call. Slot acquisition, giant lock and release.

AI use — OpenAI Codex (GPT-6, exact serving variant unavailable) traced the code, ran the standalone probe and drafted this comment.

**@⁠KOLESNiii** on GitHub · 2026-11-26 00:52 UTC · [original](https://github.com/xcl-dev/xcl/issues/2463#issuecomment-7179208228) Related admission issue on `amd-dev` at `6a0bb5b4f294b0729771ed457af1f96c01f5c06b`. The new 16-per-frame deferred-dispatch limit checks `has_slot` under the mutex, drops the lock for `split`, and records the allocation afterward under a second lock. At a count of 15, two recording threads can both pass the check before either records its allocation. If both splits succeed, the count reaches 17. The existing unit test checks the serial path. [Admission and allocation](https://github.com/xcl-dev/xcl/blob/6a0bb5b/crates/xcl/src/hle/d3d12x/list_x.rs#L3111-L3165), [current test](https://github.com/xcl-dev/xcl/blob/6a0bb5b/crates/xcl/src/hle/d3d12x/list_x.rs#L3168-L3187). A standalone test using the branch's `FrameDeferCap` and a barrier between the check and increment reached 17 with two threads. The narrow fix would reserve under the first lock and give back the reservation on failed `split` for that same present, leaving driver calls outside the lock. This demonstrates a cap overshoot. Windows allocator behavior and the effect on memory or presents remain unmeasured. The same AMD head has a separate offline admission problem. `Pipelines::make_one` increments the shared `running` count before taking the 900 KiB cold-compile mutex. With four driver slots and four cold giant jobs, one can be in the driver while three hold the remaining shared slots waiting for the giant mutex. A fifth, small job then cannot reach the driver. This scheduling consequence follows from the lock order. Whether it explains any reported offline stall is unmeasured. For #2179 or the branch equivalent, hold shared slots only for jobs ready to issue the native call. [Slot acquisition](https://github.com/xcl-dev/xcl/blob/6a0bb5b/crates/xcl/src/shaders.rs#L1600-L1606), [giant lock and release](https://github.com/xcl-dev/xcl/blob/6a0bb5b/crates/xcl/src/shaders.rs#L1628-L1665). AI use — OpenAI Codex (GPT-6, exact serving variant unavailable) traced the code, ran the standalone probe and drafted this comment.
Author
Owner

@⁠KOLESNiii on GitHub · 2026-11-26 01:34 UTC · original

Follow-up on main b49be18. There is also a shared commit-budget gap even when both native calls run on a gated worker. The 256 KiB-class and 4 MiB-class builds use separate counters and mutexes. Each checks the current free-commit snapshot before taking its own slot. Both can enter while that snapshot still looks good, before either driver call has grown its commit. The counters enforce their class limits. They carry no combined reservation. An extracted-code test with a fake commit snapshot admitted both classes when each estimate fit individually and their sum exceeded the snapshot. This establishes the admission behavior, with no inference about an actual compiler's peak. Classes · Admission and estimate

#1924 already proposes counting background builds in flight. The useful regression for that direction is concurrent cross-class admission with the same budget snapshot. Check and charge the shared reservation together, while preserving the per-class count limits. A separate test should cover the reserved foreground allowance across both classes. Related to #2508 and #2608.

Static review and standalone Linux probe. Windows behavior remains unmeasured. AI use was OpenAI Codex (GPT-6, exact serving variant unavailable) for review, probe and drafting.

**@⁠KOLESNiii** on GitHub · 2026-11-26 01:34 UTC · [original](https://github.com/xcl-dev/xcl/issues/2463#issuecomment-7179910439) Follow-up on `main` `b49be18`. There is also a shared commit-budget gap even when both native calls run on a gated worker. The 256 KiB-class and 4 MiB-class builds use separate counters and mutexes. Each checks the current free-commit snapshot before taking its own slot. Both can enter while that snapshot still looks good, before either driver call has grown its commit. The counters enforce their class limits. They carry no combined reservation. An extracted-code test with a fake commit snapshot admitted both classes when each estimate fit individually and their sum exceeded the snapshot. This establishes the admission behavior, with no inference about an actual compiler's peak. [Classes](https://github.com/xcl-dev/xcl/blob/b49be18/crates/xcl/src/hle/d3d12x/real.rs#L3994-L4028) · [Admission and estimate](https://github.com/xcl-dev/xcl/blob/b49be18/crates/xcl/src/hle/d3d12x/real.rs#L4084-L4119) #1924 already proposes counting background builds in flight. The useful regression for that direction is concurrent cross-class admission with the same budget snapshot. Check and charge the shared reservation together, while preserving the per-class count limits. A separate test should cover the reserved foreground allowance across both classes. Related to #2508 and #2608. Static review and standalone Linux probe. Windows behavior remains unmeasured. AI use was OpenAI Codex (GPT-6, exact serving variant unavailable) for review, probe and drafting.
Author
Owner

@⁠oni-swr on GitHub · 2026-11-26 08:21 UTC · original

Claiming an independent amd-win test report on PR #2652 at 88d7b0a1, covering the synthetic DXC admission tests and an RX 7800 XT workspace run against the exact parent.
AI use: Codex (gpt-6.2-sol) coordinated this claim. Claude Code (Claude Sonnet 5.5) is planned for verification.

**@⁠oni-swr** on GitHub · 2026-11-26 08:21 UTC · [original](https://github.com/xcl-dev/xcl/issues/2463#issuecomment-7186489493) Claiming an independent amd-win test report on PR #2652 at `88d7b0a1`, covering the synthetic DXC admission tests and an RX 7800 XT workspace run against the exact parent. AI use: Codex (gpt-6.2-sol) coordinated this claim. Claude Code (Claude Sonnet 5.5) is planned for verification.
Author
Owner

@⁠oni-swr on GitHub · 2026-11-26 08:31 UTC · original

Report posted for the claim above: #2652 (exact head 88d7b0a1 vs parent b49be183 on RX 7800 XT: 1438 passed / 1 failed / 47 ignored vs 1434 / 1 / 47; the one failure, fuzz seed 722, is the same on both). The RX 7800 XT slot is released. Claude Code (Claude Sonnet 5.5).

**@⁠oni-swr** on GitHub · 2026-11-26 08:31 UTC · [original](https://github.com/xcl-dev/xcl/issues/2463#issuecomment-7186707563) Report posted for the claim above: https://git.gtahub.st/github/xcl-dev--xcl/pulls/2652 (exact head `88d7b0a1` vs parent `b49be183` on RX 7800 XT: 1438 passed / 1 failed / 47 ignored vs 1434 / 1 / 47; the one failure, fuzz seed 722, is the same on both). The RX 7800 XT slot is released. Claude Code (Claude Sonnet 5.5).
This discussion has been locked. Commenting is limited to contributors.
No description provided.