startup: translate native compute programs on a worker thread (#3451) #3469
No reviewers
Labels
No labels
accessibility
awaiting author
bug
documentation
duplicate
enhancement
good first issue
help wanted
invalid
priority: P0
question
wontfix
No milestone
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
github/R0Y00--Xbox2Windows!3469
Loading…
Reference in a new issue
No description provided.
Delete branch "github-bot/gh-3467"
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?
@lukaslw-lab on GitHub · 2026-11-29 15:37 UTC · original
lukaslw-lab:runtime/native-compute-worker→main· +228 −6 · 5 files · 1 commits · closed without mergingRefs #3453 (the compute stall; the fixed-graphics key mismatch is #3412)
With native shaders on, the first Dispatch of a compute key translated it in the dispatching thread while holding
draw_lock. That takes about 100 ms per key, often more when refused, and every other draw and dispatch waited.In a startup run from a save on Windows 10, the title's own watchdog ended the process at 115–138 s, after 30 s
without its heartbeat event (#3453). Only releasing
draw_lockaround the translation was not enough: the title'sthreads still waited for their own translations, and the run faulted at 129 s.
Now one worker thread of a private thread pool does the translation without
draw_lock. The Dispatch that queues akey, and every Dispatch of that key until the worker has stored its result, stays untranslated, as a refused one
does. The worker then stores the native result, or for a refused key loads the published route.
draw_lockFreeLibrarymeanwhileunloads it only after the last item (no wait in
DllMain).X2W_TEST_NATIVE_COMPUTE_WAIT=1makes the queuing Dispatch wait for the result (set instartup_compute_request_check.prepare, for the fixtures that record one Dispatch);X2W_TEST_NATIVE_COMPUTE_DELAY_MS=<n>slows the worker down.docs/startup-runtime.md.Tests:
startup_compute_native_worker_{warp,hardware}delays the translation by 4 s; the consumer, which recordsone Dispatch and exits, must finish sooner (114 ms WARP, 525 ms hardware) with that Dispatch untranslated and its
output untouched.
startup_compute_native_worker_wait_control_*adds the wait switch and must fail the timing check(4266 / 4670 ms). Locally on a dev build with the Agility SDK (RTX 3080 Ti, driver 32.0.16.1047, Windows 10 19045):
ctest -R "^(startup_compute|native_|startup_native|startup_mrt|startup_indirect_dispatch)"passes 590 of 614; thesame 24 fail on
mainin the same tree (WARP and shader model 6.6 cases on this host, none new).🤖 Generated with Claude Code
@Leyukaka on GitHub · 2026-11-29 16:07 UTC · original · requested changes
Diff reading only at
5607594f; nothing was built or run. Lines are on the PR head.1. Dispatches are dropped until the worker reaches the key (blocking until measured). This changes what existing code does with the default settings:
run.ps1 -RendersetsX2W_NATIVE_SHADERSunless-Translateis given (tools/startup/run.ps1:84-103).<key>.txtin that same call (withX2W_NATIVE_SHADERS_ONLY=0).skipis set for the queuing Dispatch and for every later one whilenative_in_progressis set (src/startup_stubs/d3d12_compute_dispatch.inl:920-925), so they all end asno translation(:940-943). That includes a key the compiler refuses whose published translation is already on disk: it is loaded only by the worker (:650-652), after the key's turn in the queue.:681-682), so keys wait behind each other. The description gives a longest wait of 36 s / 20 s. A program dispatched once, or only during that wait, never runs.The description counts translations (362 / 968) and shows the run no longer faults, but gives no count of translated against refused Dispatch calls. Could you add
compute_dispatches_translated/untranslatedat a fixed time for this branch against native off (-Translate), and againstmainup to the fault? If the loss is large, one direction is to bind an existing published translation in the Dispatch while the native one is pending.2. The asynchronous path has no test that fails when it breaks (blocking).
startup_compute_request_check.preparenow setsX2W_TEST_NATIVE_COMPUTE_WAIT=1(tests/startup_compute_request_check.py:79-80), and every fixture built on it waits in the first Dispatch. The only run without the switch isstartup_compute_native_worker_<adapter>, which records one Dispatch, checks that it was dropped and exits before the worker runs (tests/startup_compute_native_worker_check.py:50-53). By reading:if (!tr->state && tr->native_in_progress) skip = true;branch (:921-922) passes everything: the waiting fixtures leave their first Dispatch withstateset, and the worker test has one Dispatch. In a real run each Dispatch of a pending key would queue it again;submit_native_computethat queues nothing and returns true passes too. Only the waiting fixtures would notice, by hanging.Nothing checks that a later Dispatch of the key is translated once the worker has stored the result, or that a key is queued once. Suggested: a consumer mode with several Dispatches of one key and no wait switch, expecting
translated nativelyexactly once and the last Dispatch translated.3. Device use off-lock (not blocking). The worker calls
device_of(the_device)with no reference held (:243,:251,:264,:271);the_deviceis a raw global (src/startup_stubs/d3d12_xs_device.cpp:384). Before, this ran inside a Dispatch, with the list's device alive. A queued item can now run after the application has released the device. I did not check whether that happens in practice. An AddRef on the real device at submit, released in the callback, would close it.4. Silent disable (not blocking). If
CreateThreadpoolorSetThreadpoolThreadMinimumfails (:679-682),readystays false,submit_native_computereturns false for every key and the Dispatch goes to the published route (:935-937). Native compute translation is then off for the whole process with no log line. The pool also leaks at:682.5. Skip code (not blocking).
tests/cmake/startup_compute_native_worker.cmakesets noSKIP_RETURN_CODE 77, and the check callsdispatch()withoutprobe=True(tests/startup_compute_native_worker_check.py:47). The consumer exits 77 when no hardware adapter is found (tests/startup_stream_setup.cpp:103,:108);dispatch()then raises (tests/startup_compute_dispatch_check.py:54-55), sostartup_compute_native_worker_hardwareand its control fail there instead of skipping.Checked and found consistent: the worker translates into a local
ComputeTranslationand touches the map entry only underdraw_lock(:637-655); thetrpointer survives the unlock in the wait loop (std::unordered_map, no erase); the module reference taken at:690is returned at:656; with the switch unset the published route runs as before.@Leyukaka on GitHub · 2026-11-29 18:09 UTC · original
Taking this over since there has been no activity for 2 h; will hand back if you resume.
Plan, in a new PR that credits you:
5607594fis kept unchanged; current main is merged on top.load_compute_translationalso moves offdraw_lockif that can be done safely: earlier runs of ours withoutX2W_NATIVE_SHADERSended at the same site (#3453, comment).@Leyukaka on GitHub · 2026-11-29 18:51 UTC · original
Continued in #3508.
@lukaslw-lab on GitHub · 2026-11-30 00:29 UTC · original
Continued in #3508, which keeps
5607594fand fixes both review points. Closing in favour of it.Pull request closed