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

Closed
lukaslw-lab wants to merge 0 commits from upstream-pr/3467 into main
lukaslw-lab commented 2026-11-29 15:37:31 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by lukaslw-lab on 2026-11-29T15:37:31Z.


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

<!-- greyfinch-sync:R0Y00/Xbox2Windows:issue:5552267338 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/3467). Originally posted by [lukaslw-lab](https://github.com/lukaslw-lab) on 2026-11-29T15:37:31Z. --- 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](https://claude.com/claude-code)
Leyukaka commented 2026-11-29 16:07:51 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by Leyukaka on 2026-11-29T16:07:51Z.

Review: CHANGES_REQUESTED


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

<!-- greyfinch-sync:R0Y00/Xbox2Windows:review:6103675141 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/3467#pullrequestreview-6103675141). Originally posted by [Leyukaka](https://github.com/Leyukaka) on 2026-11-29T16:07:51Z. **Review:** CHANGES_REQUESTED --- 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 -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 commented 2026-11-29 18:09:13 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by Leyukaka on 2026-11-29T18:09:13Z.


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:

  • 5607594f is kept unchanged; current main is merged on top.
  • Review point 1: while the native translation of a key is pending, the Dispatch binds an existing published translation instead of being dropped.
  • Review point 2: a test that fails when the async path breaks.
  • load_compute_translation also moves off draw_lock if that can be done safely: earlier runs of ours without X2W_NATIVE_SHADERS ended at the same site (#3451, comment).
  • Tests run in an isolated environment.
<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7260254306 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/3467#issuecomment-7260254306). Originally posted by [Leyukaka](https://github.com/Leyukaka) on 2026-11-29T18:09:13Z. --- 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: - 5607594f is kept unchanged; current main is merged on top. - Review point 1: while the native translation of a key is pending, the Dispatch binds an existing published translation instead of being dropped. - Review point 2: a test that fails when the async path breaks. - `load_compute_translation` also moves off `draw_lock` if that can be done safely: earlier runs of ours without `X2W_NATIVE_SHADERS` ended at the same site (#3451, [comment](https://github.com/R0Y00/Xbox2Windows/issues/3451#issuecomment-7260047823)). - Tests run in an isolated environment.
Leyukaka commented 2026-11-29 18:51:12 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by Leyukaka on 2026-11-29T18:51:12Z.


Continued in #3506.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7260810323 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/3467#issuecomment-7260810323). Originally posted by [Leyukaka](https://github.com/Leyukaka) on 2026-11-29T18:51:12Z. --- Continued in #3506.
lukaslw-lab commented 2026-11-30 00:29:54 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by lukaslw-lab on 2026-11-30T00:29:54Z.


Continued in #3506, which keeps 5607594f and fixes both review points. Closing in favour of it.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7265321063 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/3467#issuecomment-7265321063). Originally posted by [lukaslw-lab](https://github.com/lukaslw-lab) on 2026-11-30T00:29:54Z. --- Continued in #3506, which keeps 5607594f and fixes both review points. Closing in favour of it.

Pull request closed

Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
R0Y00/Xbox2Windows!3467
No description provided.