Repository navigation
startup: async native compute with published fallback (Refs #3451) - #3506
Conversation
…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
left a comment
There was a problem hiding this comment.
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_dispatchtakesdraw_lockatd3d12_compute_dispatch.inl:996.ensure_published_computereturns 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.
SleepConditionVariableSRWwaits ondraw_lock, and the predicate is rechecked in a loop. Every exit from the loading section clearspublished_in_progressand wakes all waiters, including the catch-all at :775. - Native result wins. The worker installs its result under
draw_lockand wakes the waiters. The loader checkstr.state != 1after it reacquires the lock (:783). So a published candidate never replaces a native result. Native translation runs once per key, so onesuperseded_*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 byinstall_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 bycompile_lock.
- Lifetimes. Each work item holds a module reference (released with
FreeLibraryWhenCallbackReturns) and a device reference. Map entries are never erased.~ComputeTranslationalso detaches the superseded pair during process termination.
SHOULD
- 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.
- 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
- Existing fixtures now use the wait switch. They keep passing because the shared
startup_compute_request_check.prepare()setsX2W_TEST_NATIVE_COMPUTE_WAIT=1(tests/startup_compute_request_check.py:78-80), and everyprepare_casecaller 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_*andstartup_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.
- This affects
- Missing skip code.
startup_compute_native_worker_hardwarehas noSKIP_RETURN_CODE 77(tests/cmake/startup_compute_native_worker.cmake:9), unlike the other_hardwaretests. The 4 s timing margin itself looked comfortable: the consumer finished in 66–431 ms. - Silent submit failure. If
submit_native_computefails (pool, work item or module reference), nothing is logged, and the key is never retried becausenative_attemptedis already set (:1012-1013). Please log the first failure. - Pool leak. If
SetThreadpoolThreadMinimumfails (:722), the pool leaks. CallingCloseThreadpoolthere would fix it. - Contradictory comments. The comment on
translate_compute_nativelysays DXC "has its own lock" (:509-510). The comment at :626 says the shared instance does not. Please make them agree. - Test hooks in the production path.
open_compute_test_eventreads the environment and callsOpenEventWon every published load and every worker job. Reading each variable once, asdelay_msdoes, 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 takesdraw_lockwhile 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 publishedtranslated: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 withgit archive. Base was not run, because no head test failed. - Commands:
cmake --preset dev -DX2W_WITH_OPUS=OFFcmake --build --preset dev --target startup_stream_consumer compile_dxil offscreen_check native_store_replay native_frozen_request_check native_compute_pack_checkctest --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_warpnative_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_warpstartup_compute_native_worker_transition_warp,startup_compute_published_load_native_wins_warp
- Passed: all
_hardwarevariants and every other test in the filter.
- Skipped (77), all on WARP, which has no 32-lane waves:
- 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
_warpfixtures 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).
- 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
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
left a comment
There was a problem hiding this comment.
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.
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
draw_lock. The manifest and sidecar I/O, the parsing, and the root signature and PSO creation no longer run underdraw_lock.draw_lock, so other keys and draws are not blocked.draw_lockstill covers the per-key lookup, claiming and publishing a result, binding and recording, and the census._warpand_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 acquiredraw_lockwhile 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 arestartup_compute_native_worker_transition_warpandstartup_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.inlwas applied alone and the affected tests rerun:native_worker_transitionandpublished_load_native_wins, WARP and hardwaredraw_lockduring the published loadpublished_load_unlockandpublished_load_concurrent, WARP and hardwarepublished_load_native_wins_hardwareonly; the WARP variant skipspublished_load_concurrent, WARP and hardwareNot covered