startup: use cached native compute frontend results before translating live #3522

Open
Leyukaka wants to merge 1 commit from upstream-pr/3522 into main
Leyukaka commented 2026-11-29 22:11:15 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by Leyukaka on 2026-11-29T22:11:15Z.


Refs #2935

What changes

At the first Dispatch of a slot-67 request, translate_compute_natively (src/startup_stubs/d3d12_compute_dispatch.inl) now looks for a cached frontend result in <cache>\native before it calls translate_compute. The lookup runs after a pack miss or when no pack is loaded. A pack hit or a pack refusal behaves as before.

  • New lookup_compute_frontend (src/direct_compute_cache.cpp) builds the key with slot67_digest, the function the pack and prepare_direct_compute already use: frontend revision, entry offset, SHA-256 of the six request parts. It reads <key>.csir only. It never publishes.
  • The file checks are the existing ones, moved out of lookup into lookup_frontend_payload: size bounds, stored identity equal to the key, SHA-256 of the payload. lookup is now a wrapper around it, and cached_compute_ir goes through the same reader (lookup_compute_ir), which also runs decode.
  • The result is hit, missing, identity_mismatch or invalid. Anything but hit goes to the same translate_compute(*frozen, entry, true) call as before.
  • <cache>\native is the directory the route already passed to the DXC bytecode compile. compile_dxil fills it when it gets that directory as CACHE_DIRECTORY (documented in docs/experiments/native-startup-request-cache.md).
  • The first-dispatch log line ends with (frontend cached), or with (frontend live) plus the reason. The heartbeat gains a compute frontend cache: hits, misses, identity mismatches, invalid line.

With no .csir present, the route does one extra file open per key at first Dispatch, then translates live as before.

Why

On ff9dcef0 the route translated live at first Dispatch unless X2W_NATIVE_COMPUTE_PACK held the key. The .csir files that compile_dxil writes were never read there, so an offline compile_dxil batch over saved requests did not shorten a later run unless a pack was also built.

Evidence

Local build of compile_dxil, native_compute_pack_check, startup_stream_consumer and d3d12_xs_shim, then
ctest -R '^(native_compute_csir_hardware|native_compute_pack(_hardware)?|native_compile_dxil_watch|startup_compute_dispatch_native_(hardware|warp))$':

Test Result
native_compute_csir_hardware (new) passed
native_compute_pack passed
native_compute_pack_hardware passed
native_compile_dxil_watch passed
startup_compute_dispatch_native_hardware passed
startup_compute_dispatch_native_warp skipped

The same filter gives the same results in a clean Windows Sandbox build (dev preset, MSVC, network off).

The WARP test skips because the adapter has no 32-lane waves. Before it exits 77 it still checks the refusal and the published-translation fallback (320 words).

native_compute_csir_hardware does the following on a hardware adapter, comparing all 320 words with the CPU reference and with the live run each time:

  1. Live dispatch with a cold cache: log (frontend live) (frontend cache miss), and no .csir is left behind.
  2. compile_dxil builds the same request offline into <cache>\native. The bytecode .bin files are deleted. A fresh runtime process logs (frontend cached).
  3. One byte of the stored identity changed: (frontend live) (frontend cache identity mismatch).
  4. One byte of the stored checksum changed: (frontend live) (invalid frontend cache entry).
  5. A correctly checksummed payload with an instruction count over the decoder's limit: (frontend live) (invalid frontend cache entry).

In 3 and 4 the damaged file is byte-identical after the run.

Mutations, each built and run alone, each fails native_compute_csir_hardware:

  • Runtime lookup forced to missing: fails on the (frontend cached) check.
  • Identity comparison removed from lookup_frontend_payload: fails on the identity-mismatch check.
  • Payload checksum ignored (return Result::hit): fails on the damaged-entry check.

Not covered

  • Pack format and its size limit (#3473, #3484).
  • --jobs (#3472).
  • No full-run measurement of the application yet. The time saved is not quantified.
  • Editing direct_compute_cache.cpp changes the frontend revision, so .csir files and packs built before this commit miss and need regenerating.
<!-- greyfinch-sync:R0Y00/Xbox2Windows:issue:5555681173 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/3522). Originally posted by [Leyukaka](https://github.com/Leyukaka) on 2026-11-29T22:11:15Z. --- Refs #2935 ## What changes At the first Dispatch of a slot-67 request, `translate_compute_natively` (`src/startup_stubs/d3d12_compute_dispatch.inl`) now looks for a cached frontend result in `<cache>\native` before it calls `translate_compute`. The lookup runs after a pack miss or when no pack is loaded. A pack hit or a pack refusal behaves as before. - New `lookup_compute_frontend` (`src/direct_compute_cache.cpp`) builds the key with `slot67_digest`, the function the pack and `prepare_direct_compute` already use: frontend revision, entry offset, SHA-256 of the six request parts. It reads `<key>.csir` only. It never publishes. - The file checks are the existing ones, moved out of `lookup` into `lookup_frontend_payload`: size bounds, stored identity equal to the key, SHA-256 of the payload. `lookup` is now a wrapper around it, and `cached_compute_ir` goes through the same reader (`lookup_compute_ir`), which also runs `decode`. - The result is `hit`, `missing`, `identity_mismatch` or `invalid`. Anything but `hit` goes to the same `translate_compute(*frozen, entry, true)` call as before. - `<cache>\native` is the directory the route already passed to the DXC bytecode compile. `compile_dxil` fills it when it gets that directory as `CACHE_DIRECTORY` (documented in `docs/experiments/native-startup-request-cache.md`). - The first-dispatch log line ends with `(frontend cached)`, or with `(frontend live)` plus the reason. The heartbeat gains a `compute frontend cache: hits, misses, identity mismatches, invalid` line. With no `.csir` present, the route does one extra file open per key at first Dispatch, then translates live as before. ## Why On `ff9dcef0` the route translated live at first Dispatch unless `X2W_NATIVE_COMPUTE_PACK` held the key. The `.csir` files that `compile_dxil` writes were never read there, so an offline `compile_dxil` batch over saved requests did not shorten a later run unless a pack was also built. ## Evidence Local build of `compile_dxil`, `native_compute_pack_check`, `startup_stream_consumer` and `d3d12_xs_shim`, then `ctest -R '^(native_compute_csir_hardware|native_compute_pack(_hardware)?|native_compile_dxil_watch|startup_compute_dispatch_native_(hardware|warp))$'`: | Test | Result | | --- | --- | | `native_compute_csir_hardware` (new) | passed | | `native_compute_pack` | passed | | `native_compute_pack_hardware` | passed | | `native_compile_dxil_watch` | passed | | `startup_compute_dispatch_native_hardware` | passed | | `startup_compute_dispatch_native_warp` | skipped | The same filter gives the same results in a clean Windows Sandbox build (dev preset, MSVC, network off). The WARP test skips because the adapter has no 32-lane waves. Before it exits 77 it still checks the refusal and the published-translation fallback (320 words). `native_compute_csir_hardware` does the following on a hardware adapter, comparing all 320 words with the CPU reference and with the live run each time: 1. Live dispatch with a cold cache: log `(frontend live) (frontend cache miss)`, and no `.csir` is left behind. 2. `compile_dxil` builds the same request offline into `<cache>\native`. The bytecode `.bin` files are deleted. A fresh runtime process logs `(frontend cached)`. 3. One byte of the stored identity changed: `(frontend live) (frontend cache identity mismatch)`. 4. One byte of the stored checksum changed: `(frontend live) (invalid frontend cache entry)`. 5. A correctly checksummed payload with an instruction count over the decoder's limit: `(frontend live) (invalid frontend cache entry)`. In 3 and 4 the damaged file is byte-identical after the run. Mutations, each built and run alone, each fails `native_compute_csir_hardware`: - Runtime lookup forced to `missing`: fails on the `(frontend cached)` check. - Identity comparison removed from `lookup_frontend_payload`: fails on the identity-mismatch check. - Payload checksum ignored (`return Result::hit`): fails on the damaged-entry check. ## Not covered - Pack format and its size limit (#3473, #3484). - `--jobs` (#3472). - No full-run measurement of the application yet. The time saved is not quantified. - Editing `direct_compute_cache.cpp` changes the frontend revision, so `.csir` files and packs built before this commit miss and need regenerating.
lukaslw-lab commented 2026-11-30 07:44:44 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by lukaslw-lab on 2026-11-30T07:44:44Z.

Review: APPROVED


Reviewed a5a2eb57 (draft) by reading the diff; not built or run.

Verdict: approve. The runtime reads .csir with the same key, file checks and decoder that compile_dxil and the pack already use, and every result except hit falls back to the same translate_compute call. No MUST.

  • SHOULD Rebase: src/startup_stubs/d3d12_compute_dispatch.inl conflicts with main on the translated natively ... (frontend %s) log line (9fcfe0de, native shader totals). It is a small merge, but the new log suffix your test checks lives there.
  • NIT The runtime never publishes a .csir after a live translation; the test asserts this (tests/native_compute_csir_check.py:42). So a warm run still needs an offline compile_dxil batch. If the runtime stays read-only on purpose (keeping cache writes off draw_lock, a single writer), say so in docs/experiments/native-startup-request-cache.md, so nobody "fixes" it later.
  • Checked, no finding:
    • The key is slot67_digest, the same one the pack and prepare_direct_compute use, and the frontend revision comes from the shared x2w_shader_compiler sources.
    • A heap view contract cannot reach a non-container .csir (prepare_direct_compute throws).
    • Refusals are never cached, so refused keys behave as before.
    • Any open error other than not-found counts as invalid and goes live.
    • lookup keeps its old result for cached_compute_ir and the pack builder.
    • It merges cleanly with #3472.

Tests: native_compute_csir_hardware proves the hit (with the bytecode deleted, so the frontend really came from .csir), the identity, checksum and decode fallbacks, and that every route gives the same 320 words. The three mutations show each check is reached. CI did not run it: it is not in the Windows pr filter, and it needs wave32 hardware. The only results are the author's local run and the sandbox run.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:review:6108435470 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/3522#pullrequestreview-6108435470). Originally posted by [lukaslw-lab](https://github.com/lukaslw-lab) on 2026-11-30T07:44:44Z. **Review:** APPROVED --- Reviewed `a5a2eb57` (draft) by reading the diff; not built or run. **Verdict: approve.** The runtime reads `.csir` with the same key, file checks and decoder that `compile_dxil` and the pack already use, and every result except `hit` falls back to the same `translate_compute` call. No MUST. - **SHOULD** Rebase: `src/startup_stubs/d3d12_compute_dispatch.inl` conflicts with main on the `translated natively ... (frontend %s)` log line (9fcfe0de, native shader totals). It is a small merge, but the new log suffix your test checks lives there. - **NIT** The runtime never publishes a `.csir` after a live translation; the test asserts this (`tests/native_compute_csir_check.py:42`). So a warm run still needs an offline `compile_dxil` batch. If the runtime stays read-only on purpose (keeping cache writes off `draw_lock`, a single writer), say so in `docs/experiments/native-startup-request-cache.md`, so nobody "fixes" it later. - Checked, no finding: - The key is `slot67_digest`, the same one the pack and `prepare_direct_compute` use, and the frontend revision comes from the shared `x2w_shader_compiler` sources. - A heap view contract cannot reach a non-container `.csir` (`prepare_direct_compute` throws). - Refusals are never cached, so refused keys behave as before. - Any open error other than not-found counts as `invalid` and goes live. - `lookup` keeps its old result for `cached_compute_ir` and the pack builder. - It merges cleanly with #3472. Tests: `native_compute_csir_hardware` proves the hit (with the bytecode deleted, so the frontend really came from `.csir`), the identity, checksum and decode fallbacks, and that every route gives the same 320 words. The three mutations show each check is reached. CI did not run it: it is not in the Windows `pr` filter, and it needs wave32 hardware. The only results are the author's local run and the sandbox run.
This pull request cannot be merged automatically due to conflicts.
Merge manually to resolve the conflicts.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin 'upstream-pr/3522:upstream-pr/3522'
git switch 'upstream-pr/3522'

Merge

Merge the changes and update on Forgejo.

Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.

git switch 'main'
git merge --no-ff 'upstream-pr/3522'
git switch 'upstream-pr/3522'
git rebase 'main'
git switch 'main'
git merge --ff-only 'upstream-pr/3522'
git switch 'upstream-pr/3522'
git rebase 'main'
git switch 'main'
git merge --no-ff 'upstream-pr/3522'
git switch 'main'
git merge --squash 'upstream-pr/3522'
git switch 'main'
git merge --ff-only 'upstream-pr/3522'
git switch 'main'
git merge 'upstream-pr/3522'
git push origin 'main'
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!3522
No description provided.