Skip to content

startup: translate native compute programs on a worker thread (#3451) - #3467

Closed
lukaslw-lab wants to merge 1 commit into
R0Y00:mainfrom
lukaslw-lab:runtime/native-compute-worker
Closed

lukaslw-lab wants to merge 1 commit into
R0Y00:mainfrom
lukaslw-lab:runtime/native-compute-worker

Conversation

@lukaslw-lab

Copy link
Copy Markdown
Contributor

Refs #3451 (the compute stall; the fixed-graphics key mismatch is #3410)

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 (#3451). Only releasing draw_lock around the translation was not enough: the title's
threads 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 a
key, 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.

Startup run from a save, 360 s, Windows 10 19045 before worker
ends watchdog fault at 115–138 s (6 of 6 runs) full 360 s, no device removal (9 of 9 runs)
native translations in the title's threads, under draw_lock 362 (empty cache) / 968 (with a compute pack) on the worker
longest wait in the queue – 36 s / 20 s
  • Each queued key holds a reference on the stand-in DLL until its callback returns, so a FreeLibrary meanwhile
    unloads it only after the last item (no wait in DllMain).
  • Test switches: X2W_TEST_NATIVE_COMPUTE_WAIT=1 makes the queuing Dispatch wait for the result (set in
    startup_compute_request_check.prepare, for the fixtures that record one Dispatch);
    X2W_TEST_NATIVE_COMPUTE_DELAY_MS=<n> slows the worker down.
  • Docs: "Translating compute programs in the stand-in" in docs/startup-runtime.md.

Tests: startup_compute_native_worker_{warp,hardware} delays the translation by 4 s; the consumer, which records
one 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; the
same 24 fail on main in the same tree (WARP and shader model 6.6 cases on this host, none new).

🤖 Generated with Claude Code

…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>

@Leyukaka Leyukaka left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Diff reading only at 5607594; 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 -Render sets X2W_NATIVE_SHADERS unless -Translate is given (tools/startup/run.ps1:84-103).

  • Before, the Dispatch that first saw a key translated it in the same call and was recorded: native first, and after a native refusal the published <key>.txt in that same call (with X2W_NATIVE_SHADERS_ONLY=0).
  • Now skip is set for the queuing Dispatch and for every later one while native_in_progress is set (src/startup_stubs/d3d12_compute_dispatch.inl:920-925), so they all end as no 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.
  • The pool has one thread (: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 / untranslated at a fixed time for this branch against native off (-Translate), and against main up 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.prepare now sets X2W_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 is startup_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:

  • removing the if (!tr->state && tr->native_in_progress) skip = true; branch (:921-922) passes everything: the waiting fixtures leave their first Dispatch with state set, and the worker test has one Dispatch. In a real run each Dispatch of a pending key would queue it again;
  • in the worker test itself, a submit_native_compute that 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 natively exactly 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_device is 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 CreateThreadpool or SetThreadpoolThreadMinimum fails (:679-682), ready stays false, submit_native_compute returns 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.cmake sets no SKIP_RETURN_CODE 77, and the check calls dispatch() without probe=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), so startup_compute_native_worker_hardware and its control fail there instead of skipping.

Checked and found consistent: the worker translates into a local ComputeTranslation and touches the map entry only under draw_lock (:637-655); the tr pointer survives the unlock in the wait loop (std::unordered_map, no erase); the module reference taken at :690 is returned at :656; with the switch unset the published route runs as before.

@Leyukaka

Copy link
Copy Markdown
Collaborator

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:

@Leyukaka

Leyukaka commented Nov 29, 2026 •

Copy link
Copy Markdown
Collaborator

Continued in #3506.

@lukaslw-lab

Copy link
Copy Markdown
Contributor Author

Continued in #3506, which keeps 5607594 and fixes both review points. Closing in favour of 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.

2 participants