Skip to content

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

Draft
Leyukaka wants to merge 1 commit into
R0Y00:mainfrom
Leyukaka:runtime-native-compute-csir-cache
Draft

Leyukaka wants to merge 1 commit into
R0Y00:mainfrom
Leyukaka:runtime-native-compute-csir-cache

Conversation

@Leyukaka

Copy link
Copy Markdown
Collaborator

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

@lukaslw-lab lukaslw-lab left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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 (9fcfe0d, 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.

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