Skip to content

startup: async native compute with published fallback (Refs #3451) - #3506

Merged
R0Y00 merged 7 commits into
R0Y00:mainfrom
Leyukaka:takeover-3467-async-compute
Nov 30, 2026
Merged

R0Y00 merged 7 commits into
R0Y00:mainfrom
Leyukaka:takeover-3467-async-compute

Conversation

@Leyukaka

Copy link
Copy Markdown
Collaborator

Refs #3451. Replaces #3467 (takeover announced there).

The worker-thread native compute translation is by @lukaslw-lab; commit 5607594 is kept unchanged, and the commits after the merge of main are the follow-ups.

What changes

  1. Published fallback while native is pending. In 5607594 a Dispatch of a key stayed untranslated until the worker had stored its result. Now, while the key's native translation is pending, the Dispatch binds an existing published translation. If there is none, it goes through the existing untranslated counter. When the worker finishes, the native result replaces the published one, once per key.
  2. Published load outside draw_lock. The manifest and sidecar I/O, the parsing, and the root signature and PSO creation no longer run under draw_lock.
    • One load runs per key. Other Dispatches of that key wait on a condition variable that releases draw_lock, so other keys and draws are not blocked.
    • The loaded result is discarded if the native result was published in the meantime.
    • The superseded published PSO and root signature are kept until teardown, because recordings may still reference them.
    • draw_lock still covers the per-key lookup, claiming and publishing a result, binding and recording, and the census.
    • Any exception from the loader clears the per-key loading mark and wakes the waiters.
  3. Tests, each as _warp and _hardware:
    • startup_compute_native_worker_transition: with the worker held, the first Dispatch must write the published program's marker; after the worker completes, the second must write the native output. With an empty cache, the first Dispatch must be counted as skipped.
    • startup_compute_published_load_unlock: the census must acquire draw_lock while a published load is paused.
    • startup_compute_published_load_native_wins: the worker publishes during a paused published load; the native output is required, with no published translation line.
    • startup_compute_published_load_concurrent: a second thread dispatches the same key during a paused load; no second load may start, and both readbacks must hold the published marker.

Verification

Built with the dev preset and run in an isolated environment with the network off, on one desktop GPU and on WARP.

ctest -R '^startup_compute_(native_worker|published_load|request)' at 2d3c247: 13 passed, 2 skipped, 0 failed. The two skips are startup_compute_native_worker_transition_warp and startup_compute_published_load_native_wins_warp: WARP refuses the native route, so they report a capability skip.

Each mutation of src/startup_stubs/d3d12_compute_dispatch.inl was applied alone and the affected tests rerun:

Mutation Tests that fail
Skip the published route while native is pending native_worker_transition and published_load_native_wins, WARP and hardware
Keep draw_lock during the published load published_load_unlock and published_load_concurrent, WARP and hardware
Install the loaded result even if native already published published_load_native_wins_hardware only; the WARP variant skips
Remove the same-key wait published_load_concurrent, WARP and hardware

Not covered

lukaslw-lab and others added 7 commits November 29, 2026 16:35
…3451)

The first Dispatch of a compute key translated it in the dispatching thread with draw_lock held,
about 100 ms per key, so every other draw and dispatch waited and the title's watchdog ended
startup runs. One worker thread of a private pool now translates without draw_lock; Dispatch
calls of the key stay untranslated until it has stored the native result (or, refused, the
published route). Each queued key holds a module reference until its callback returns, so a
FreeLibrary meanwhile unloads the stand-in only after the last one.

X2W_TEST_NATIVE_COMPUTE_WAIT=1 lets fixtures that record one Dispatch wait for the result;
X2W_TEST_NATIVE_COMPUTE_DELAY_MS slows the worker for startup_compute_native_worker, whose
wait control must fail its timing check.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
ensure_published_compute releases draw_lock inside record_translated_dispatch. Say why the caller's pre-checks stay valid and what is read again after the lock is reacquired. Catch any exception from the loader, so a key is never left marked as loading with its next Dispatch waiting forever. Document the fixture event switches and the kept superseded PSO and root signature.
…dispatches

startup_compute_published_load_native_wins_* lets the native worker publish while the published load is paused and requires the native program's output with no published translation line, so it fails if the loaded candidate is installed anyway. startup_compute_published_load_concurrent_* dispatches the same key from a second thread during the paused load and requires that no second load starts, both Dispatch calls are counted before the load is released, and both readbacks hold the published marker, so it fails if the second Dispatch does not wait.

@Not-Saii Not-Saii left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed at head 2d3c2477ce317a1da928dfede2bedf4e5c8bc865 (merges cleanly into main ff9dcef0). I read the diff statically, then built it and ran the tests in an isolated Windows Sandbox with networking disabled. The PR tree was mapped read-only, and the only writable folder was one output folder.

Verdict: approve. I found no blocking issues. The locking, the condition-variable wait, the order in which native and published results take effect, and the lifetimes all hold up on reading, and the sandbox run and the mutation check agree with that.

Static review

  • Lock balance. record_translated_dispatch takes draw_lock at d3d12_compute_dispatch.inl:996. ensure_published_compute returns with the lock held, and the lock is released at :1491 (refusal) or :1576 (recorded). Neither caller (d3d12_xs_device.cpp:3923, :4653) holds another lock across the call. The pre-checks that run before the lock is released only read state that belongs to the recording list.
  • Wait and wake. SleepConditionVariableSRW waits on draw_lock, and the predicate is rechecked in a loop. Every exit from the loading section clears published_in_progress and wakes all waiters, including the catch-all at :775.
  • Native result wins. The worker installs its result under draw_lock and wakes the waiters. The loader checks tr.state != 1 after it reacquires the lock (:783). So a published candidate never replaces a native result. Native translation runs once per key, so one superseded_* pair is enough.
  • Install is complete. Every field the two translators write to the candidate (uds, srvs, uavs, heap_srvs, samplers, root, pipeline, state) is moved by install_compute_translation.
  • Code that now runs off the lock. None of it needs draw_lock:
    • cache_path, the frozen request store (which has its own mutex) and the const pack lookup.
    • The DXIL cache publish, which writes to a temporary file named by process ID and an atomic serial.
    • Graphics and mesh each create their own ShaderDxilCompiler. The shared compute compiler instance is serialized by compile_lock.
  • Lifetimes. Each work item holds a module reference (released with FreeLibraryWhenCallbackReturns) and a device reference. Map entries are never erased. ~ComputeTranslation also detaches the superseded pair during process termination.

SHOULD

  1. Missing evidence fields. The hardware result in the PR body does not name the adapter, driver, Windows build or debug-layer status, which AGENTS.md asks for. Please add them.
  2. Dispatches dropped during translation. The launchers default to X2W_NATIVE_SHADERS_ONLY=1, so no published translation exists. Every Dispatch of a key that arrives before its worker finishes is skipped. That window is about 100 ms or more per key, and keys queue behind a single worker.
    • A key that is dispatched only inside that window, such as a one-shot initialization pass, never runs.
    • The docs describe the untranslated count but not this consequence.
    • Please add it to "Not covered" or to docs/startup-runtime.md. Alternatively, report from a package run how many keys had every Dispatch skipped (the census lists keys with 0 translated).

NIT

  1. Existing fixtures now use the wait switch. They keep passing because the shared startup_compute_request_check.prepare() sets X2W_TEST_NATIVE_COMPUTE_WAIT=1 (tests/startup_compute_request_check.py:78-80), and every prepare_case caller inherits it.
    • This affects startup_compute_dispatch_native_*, native_compute_pack_hardware, native_descriptor_scc_*, startup_compute_indexed_atomic_uav_*, native_image_store_exact_width_* and startup_compute_dispatch_texture_native_*.
    • So the default path, with no wait, is covered only by the new fixtures.
    • I ran those fixtures (results below) and they pass. Saying this in the PR body would help the next reader.
  2. Missing skip code. startup_compute_native_worker_hardware has no SKIP_RETURN_CODE 77 (tests/cmake/startup_compute_native_worker.cmake:9), unlike the other _hardware tests. The 4 s timing margin itself looked comfortable: the consumer finished in 66–431 ms.
  3. Silent submit failure. If submit_native_compute fails (pool, work item or module reference), nothing is logged, and the key is never retried because native_attempted is already set (:1012-1013). Please log the first failure.
  4. Pool leak. If SetThreadpoolThreadMinimum fails (:722), the pool leaks. Calling CloseThreadpool there would fix it.
  5. Contradictory comments. The comment on translate_compute_natively says DXC "has its own lock" (:509-510). The comment at :626 says the shared instance does not. Please make them agree.
  6. Test hooks in the production path. open_compute_test_event reads the environment and calls OpenEventW on every published load and every worker job. Reading each variable once, as delay_ms does, would keep the test hooks out of the production path.

What each test proves (all observed in the sandbox run)

  • startup_compute_native_worker_{warp,hardware}. With a 4 s worker delay and nothing published, the single-Dispatch consumer exits in 66–431 ms. Its Dispatch is refused with "no translation" and the output is untouched, so the Dispatch does not wait for native translation.
  • startup_compute_native_worker_wait_control_*. This is the negative control. With the wait switch on, the consumer takes 4095–4548 ms and check (a) fails as expected.
  • startup_compute_native_worker_transition_hardware.
    • While native translation is pending, the first Dispatch records the published program (the marker word), or is counted as skipped when nothing is published.
    • After the worker completes, the second Dispatch records native output.
    • The WARP variant checks the pending legs, then exits 77.
  • startup_compute_published_load_unlock_*. The census takes draw_lock while a published load is paused, so the load runs outside the lock.
  • startup_compute_published_load_native_wins_hardware. A native result published during a paused load is kept, the loaded candidate is discarded, and no published translated: line appears. The WARP variant exits 77.
  • startup_compute_published_load_concurrent_*. A second Dispatch of the same key during a paused load waits instead of starting a second load, and both readbacks hold the published marker.
  • Not covered by any test. No test checks that the superseded published PSO and root stay alive for a list recorded before the switch and executed after it, because each fixture fences every Dispatch before the next one. The code keeps them alive, but no test exercises that.

Sandbox run

  • Commit: 2d3c2477ce317a1da928dfede2bedf4e5c8bc865, exported with git archive. Base was not run, because no head test failed.
  • Commands:
    • cmake --preset dev -DX2W_WITH_OPUS=OFF
    • cmake --build --preset dev --target startup_stream_consumer compile_dxil offscreen_check native_store_replay native_frozen_request_check native_compute_pack_check
    • ctest --preset dev --no-tests=error --output-on-failure -V -R '^(startup_compute_(native_worker|published_load|request|dispatch_native|indexed_atomic_uav|dispatch_texture_native|descriptor_scc)|native_(compute_pack|frozen_request|descriptor_scc|image_store_exact_width|startup_request_cache))'
  • Results: 46 tests. 34 passed, 0 failed, 12 skipped (77).
    • Skipped (77), all on WARP, which has no 32-lane waves:
      • native_descriptor_scc_warp, startup_compute_descriptor_scc_warp
      • native_image_store_exact_width_warp_{r,rg,rgb,array,volume,rgba}
      • startup_compute_dispatch_texture_native_warp, startup_compute_dispatch_native_warp, startup_compute_indexed_atomic_uav_warp
      • startup_compute_native_worker_transition_warp, startup_compute_published_load_native_wins_warp
    • Passed: all _hardware variants and every other test in the filter.
  • Repeat run: ctest ... -R '^startup_compute_(native_worker|published_load)' --repeat until-fail:5. The 10 non-skipping PR tests each passed 5 of 5 runs (50 runs, no failure). The two WARP skips ended their repeat at the first 77.
  • Environment:
    • Adapter: NVIDIA GeForce RTX 4090, through the sandbox vGPU (GPU paravirtualization). The fixtures' own log reports driver 32.0.16.1088; WMI inside the sandbox shows 10.0.26100.1150. The _warp fixtures used Microsoft Basic Render Driver.
    • Windows: the sandbox runs Windows 11 Enterprise 10.0.26100 (build 26100); the host is 10.0.26300.
    • Debug layer: unavailable. The sandbox image has no d3d12SDKLayers.dll, so the fixtures logged "debug layer: unavailable" and the sev 0/1 assertions had nothing to catch.
    • Toolchain: MSVC 14.44, CMake 3.31.6. The build is Debug (dev preset).

Mutation check

I deleted the ReleaseSRWLockExclusive(&draw_lock); before the loader call and the AcquireSRWLockExclusive(&draw_lock); after it in ensure_published_compute (d3d12_compute_dispatch.inl:772 and :782), which keeps draw_lock held during the published load. This was done in a scratch copy, not on the PR branch.

  • The tree built, so the failures below are not build errors.
  • startup_compute_published_load_unlock_{warp,hardware} failed with an assertion: the census was blocked until the loader's fixture gate timed out, and the key's first refusal became the gate timeout.
  • startup_compute_published_load_concurrent_{warp,hardware} failed with "the key was not loaded exactly once".
  • After restoring the file, all 4 passed again.

CI

The PR check passed, but its Windows job ran the pr preset (82 tests), which includes none of this PR's tests and none of the existing native-compute fixtures. The Linux jobs run Python checks and gfx1013 assembly only. CI did not run this PR's tests; the results above come from the sandbox.

@lukaslw-lab lukaslw-lab left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed 2d3c2477 by reading the diff, plus A/B runs of the application. Reads correct, and I agree with Not-Saii's review without repeating it. Thanks for picking this up and keeping my commit.

Run data for "Not covered" and Not-Saii's SHOULD 2: Windows 10 + Agility, RTX 3080 Ti, one local build (this head + #3464 + #3097), published cache (X2W_NATIVE_SHADERS_ONLY=0), the same switches in every run.

run native frames published keys: Dispatches untranslated fault / removal
from a save, 360 s on / off 7,478 / 8,068 0 / 0 none
same + compute pack on / off 7,285 / 7,597 0 / 0 none
fresh start, 350 s on / off 6,670 / 6,875 0 / 0 none
  • Published keys bound the published translation first and switched to native with no gap. 5607594 lost 140 of 5,501 there.
  • SHOULD 2, keys with no published translation (from the periodic census, so approximate): 0, 20 and 3 keys had every Dispatch before their native result. Most native-only keys (67, 93, 73) were refused later at binding anyway. The wait shows on single keys: in the fresh run one key had 1,524 of 3,317 Dispatches before its result, queued behind about 140 startup compiles. Priority for keys without a published fallback, or a second worker, would be a follow-up.

I'll close #3467 in favour of this one.

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

Labels

priority: P0 Highest project priority

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants