Skip to content

startup: totals for native shader compiles and refusals - #3494

Merged
R0Y00 merged 2 commits into
R0Y00:mainfrom
Leyukaka:runtime-native-compile-totals
Nov 30, 2026
Merged

R0Y00 merged 2 commits into
R0Y00:mainfrom
Leyukaka:runtime-native-compile-totals

Conversation

@Leyukaka

Copy link
Copy Markdown
Collaborator

Refs #3477 (item 6).

Why

In one long local run of the application with native shaders enabled, 84 compute programs were translated natively and about 270 were refused. 16 of the refusals were logged (the cap in refuse_native), the rest were counted nowhere by reason, and neither native path recorded a duration. See also #3451 (watchdog).

What changes

Counting and timing only. Nothing is translated or refused differently, and no locking changes: both native paths hold draw_lock as before, and the presenter reads the counters with atomic loads.

One new line, printed with the periodic render lines (every 300 frames or when the 5 s report is due) in render mode when X2W_NATIVE_SHADERS is set:

x2w-render native shader totals: compute compiles=N total_ms=F max_ms=F draw_lock_ms=F; graphics compiles=N total_ms=F max_ms=F draw_lock_ms=F; compute refusal_overflow=N "reason"=N "reason"=N ...
  • compiles, total_ms, max_ms: calls to the DXIL compiler, cache lookups and failures included. Graphics counts one per stage.
  • draw_lock_ms: time spent in the native translation function, which runs under draw_lock.
  • Refusals: compute refusals per reason, all of them, not only the 16 logged. The native compute +0x...: prefix is stripped so one reason groups across PCs. The table holds 12 reasons; a 13th distinct reason, or a reason of 72 bytes or more, adds to refusal_overflow.

d3d12_draw.inl: the early return in try_native_graphics is split in two so the timer starts after the environment check; the returns are the same. The stage compile goes through compile_native_shader, which forwards the same arguments.

Tests

Three new tests reuse startup_compute_dispatch_native_check.py:

  • startup_native_shader_totals_refusals_warp: 21 pipelines refused for two reasons. 16 detail lines, and the totals line carries all 21:
    compute compiles=0 ... compute refusal_overflow=0 "unaligned descriptor table entry"=11 "compute state block A is not 28 bytes"=10
  • startup_native_shader_totals_compile_{warp,hardware}: one native compile, output equal to the CPU reference, compute compiles=1 with 0 < total_ms == max_ms <= draw_lock_ms.
ctest --preset dev --no-tests=error -j 1 --output-on-failure -R '^(startup_native_shader_totals_.*|startup_compute_dispatch_native_.*|startup_native_graphics_draw_(warp|hardware))$'

Base 26df1826, head 9fcfe0de:

  • Passed (5): startup_native_graphics_draw_warp, startup_native_graphics_draw_hardware, startup_compute_dispatch_native_hardware, startup_native_shader_totals_compile_hardware, startup_native_shader_totals_refusals_warp.
  • Skipped (2, code 77, no 32-lane waves on WARP): startup_compute_dispatch_native_warp, startup_native_shader_totals_compile_warp.

Mutations:

  • Counting only the first 16 refusals: refusals_warp fails (counts 8 and 8).
  • Dropping the compiles increment: compile_hardware fails (compute compiles=0).

Not covered

  • The 5 s heartbeat path itself: the tests reach the line through the 300-frame condition.
  • Graphics counts: 0 in every fixture.
  • refusal_overflow > 0.

Merge order

Merges cleanly with #3480, #3482, #3481 and main. Conflicts with #3491 at the 5 s block in d3d12_xs_device.cpp, where #3491 makes last_report atomic. Whichever lands second takes now - last_report.load(std::memory_order_relaxed) >= 5000 in the totals condition.

@Not-Saii Not-Saii 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.

Approve. The fix holds up in an isolated sandbox build: the mutation that reverts it is caught, and the new tests pass five times in a row. No MUST. The four SHOULDs below are follow-ups that can land here or in a later PR.

Summary

Counting and timing only: compute and graphics native compiles (count, total, max), time in the native
translation functions, and compute refusals per reason, printed as one x2w-render native shader totals:
line with the 300-frame / 5 s render reports when X2W_NATIVE_SHADERS is set. Nothing is translated or
refused differently.

Verified by reading:

  • Every compute refusal is counted. translate_compute_natively refuses in two places, the pack
    unsupported return and the catch. Both go through refuse_native, which now calls
    count_native_compute_refusal before the 16-line log cap (d3d12_compute_dispatch.inl:487). A
    cache_path failure returns before the lambda; it was never a refusal.
  • No double counting per key. load_compute_translation sets tr.state = 2 first
    (d3d12_compute_dispatch.inl:279), so the tr->state || !translate_compute_natively(*tr) guard at :814
    tries native translation once per key. Manifest retries don't re-enter it. The totals count keys, not
    Dispatches.
  • Thread safety holds today. Every writer runs under exclusive draw_lock. That covers
    record_translated_dispatch (:798, then :814) and translation_for_targets, which is reached from
    d3d12_draw.inl:3126 after the exclusive acquire at :3124. So the non-atomic max_ticks load and store
    (d3d12_xs_device.cpp:448-449) and the slot claim in count_native_compute_refusal (:479-483) each
    have one writer at a time. The presenter reads with acquire on compiles and on count, and the reason
    bytes are written before the release store. A reader sees either nothing or a complete reason.
  • The line fits the buffer. Worst case with 12 reasons of 71 bytes and 20-digit counts is about 1,480
    characters, under both the 1,534-byte local buffer and the 1,533 characters log keeps.

Findings

SHOULD

  1. The totals miss three DXIL compiles that run under the same switch. The line calls itself
    native shader totals, and the body says compiles counts calls to the DXIL compiler, yet three
    compile sites skip compile_native_shader:

    • the rectangle geometry stage at src/startup_stubs/d3d12_draw.inl:1438-1440 (gs_6_6, under
      X2W_NATIVE_SHADERS);
    • the mesh and pixel stages at src/startup_stubs/native_mesh57.inl:410-412. Their tests set
      X2W_NATIVE_SHADERS; see CMakeLists.txt:1957.

    A run that uses these paths would report graphics compiles=0 even though DXC ran. Either route the
    three calls through compile_native_shader (the GS stage into native_graphics_totals, mesh into
    graphics or a third group), or narrow the line and the "Not covered" list to the two paths it measures.

  2. Merge order with #3467. That open PR moves translate_compute_natively onto a worker thread without
    draw_lock (one worker). Afterwards compute still has one writer, so the counters stay correct. But the
    comment at d3d12_xs_device.cpp:432 ("Both native translation paths already hold draw_lock") and the
    draw_lock_ms field name would be wrong for compute. The merge-order section names #3491 only. Please
    add #3467 and say what the second PR to land should rename, or describe the invariant as one writer per
    totals group rather than draw_lock.

  3. Document the new line. Its neighbours are documented in docs/experiments.md:17267-17274 (upload
    ring, compute dispatches) and docs/startup-runtime.md:1133-1135 (the 16 refusal lines). One sentence
    there with the field meanings would cover it: compiles counts cache hits and failures, graphics counts
    one per stage, draw_lock_ms contains total_ms, and what overflow means. AGENTS.md asks for the docs
    to change when a reproduction or diagnostic changes.

  4. Evidence fields (AGENTS.md "Evidence"). The _hardware passes in the body don't name the adapter,
    driver, Windows build or debug-layer status. The results are for 9fcfe0de; the head is now the merge
    commit 8989155b. Please add the environment and say whether the run was repeated on the merge head.

NIT

  1. A long or 13th reason is counted but never named. A reason of 72 bytes or more falls into
    refusal_overflow (d3d12_xs_device.cpp:479). Storing the first 71 bytes would keep the reason
    recognisable in the census. Refusal reasons that end with a view-kind number or a PC are the ones most
    likely to run long.
  2. used += is not clamped (d3d12_xs_device.cpp:498-521). The line fits today only because of the
    12 × 72 sizing. A (std::min)(used, sizeof(line) - 1) after each snprintf, or a static_assert on the
    sizes, would turn the comment at :469 into a check. size_t underflow is the failure mode if the
    table grows.
  3. The test never groups one reason across different PCs. The
    unaligned descriptor table entry refusals carry the native compute +0x…: prefix
    (src/gfx1013_compute_native.cpp:39, :1063), so the expected string proves the prefix is stripped.
    All 11 come from the same PC, though. "Grouped across PCs" is untested; a fixture with two programs
    refusing at different PCs would cover it. Not blocking, and in line with the PR's "Not covered" list.

What each PR test proves

  • startup_native_shader_totals_refusals_warp: runs 21 keys that each fail native translation, 11 with
    a frontend reason (prefixed, then stripped) and 10 with a request-shape reason (not prefixed). It checks
    that 16 detail lines appear while the totals count all 21, by reason, with zero compiles and
    draw_lock_ms > 0. The output buffer is untouched. It runs on WARP, and both refusals happen before the
    wave-capability check, so it cannot skip 77.
  • startup_native_shader_totals_compile_hardware: one native compile, output equal to the CPU reference,
    compute compiles=1, 0 < total_ms == max_ms <= draw_lock_ms, no refusal entries.
  • startup_native_shader_totals_compile_warp: with the in-box WARP (no 32-lane waves) it is refused and
    exits 77 after checking that output is untouched and compiles=0, which proves only the skip path. With
    WARP 1.0.22 it fails instead; see the sandbox results below.
  • Not proved: graphics counts (0 in every fixture), the 5 s path, refusal_overflow > 0. The PR lists all
    three as not covered.

CI

Run 46306948144 on 8989155b passed. The Windows job runs only the pr ctest preset: 82 smoke tests, no
native_* tests, none of this PR's three tests and none of the regression tests it names
(startup_compute_dispatch_native_*, startup_native_graphics_draw_*). The pr build preset compiled
d3d12_xs_device.cpp into d3d12_xs_shim with X2W_NATIVE_SHADERS, but it did not build
startup_stream_consumer, so the consumer changes were not compiled in CI. The Linux job runs no test
this PR touches.

Sandbox run (isolated, network off)

  • Commit: head 8989155b53d6677c694a6fb5365a32dc26f04c58, base ff9dcef0.
  • Environment: Windows 11 Enterprise 10.0.26100 (Windows Sandbox), MSVC 14.44, CMake 3.31.6, Python 3.14.
    Hardware adapter as the fixtures print it: NVIDIA GeForce RTX 4090, driver 32.0.16.1088 (sandbox vGPU).
    WARP: Microsoft Basic Render Driver 1.0.22.0 (OFFSCREEN_WARP_DIR).
  • Debug layer: available. D3D12GetDebugInterface returned 0x00000000 with d3d12SDKLayers.dll
    10.0.26100.9818. The fixtures that print it report it enabled:
    startup_native_graphics_draw_* print "debug layer: 0x00000000 enabled", and
    startup_compute_dispatch_native_hardware prints "debug layer: enabled, no error message". The three new
    tests print no debug-layer line, so their status isn't reported.
  • Commands:
    cmake --preset dev -DX2W_WITH_OPUS=OFF, then
    cmake --build --preset dev --target startup_stream_consumer compile_dxil, then
    ctest --preset dev --no-tests=error --output-on-failure -V -R '^(startup_native_shader_totals_.*|startup_compute_dispatch_native_.*|startup_native_graphics_draw_(warp|hardware))$'

Head (7 tests):

  • Passed (5): startup_native_shader_totals_refusals_warp, startup_native_shader_totals_compile_hardware,
    startup_compute_dispatch_native_hardware, startup_native_graphics_draw_warp,
    startup_native_graphics_draw_hardware.
  • Failed (2), both on WARP 1.0.22 and not caused by this PR:
    • startup_compute_dispatch_native_warp fails on base ff9dcef0 too (word 0 c5c5c5c5, expected
      00000400; 288 words differ). This WARP reports 32-lane waves, so the program is translated natively,
      but the output stays at the sentinel.
    • startup_native_shader_totals_compile_warp is new, so it has no base result. It fails the same way
      ("native Dispatch output differed from the CPU reference"). It reuses the same Dispatch, so it inherits
      the WARP problem above. On the in-box WARP it skips 77, as your body says.
  • Skipped (77): none.
  • The new totals lines on head:
    • compile_hardware: compute compiles=1 total_ms=7.159 max_ms=7.159 draw_lock_ms=77.512; graphics compiles=0 ...; compute refusal_overflow=0
    • refusals_warp: compute compiles=0 total_ms=0.000 max_ms=0.000 draw_lock_ms=10.690; ... refusal_overflow=0 "unaligned descriptor table entry"=11 "compute state block A is not 28 bytes"=10

Repeat (--repeat until-fail:5): the five passing tests passed 5/5, including refusals_warp and
compile_hardware. The two WARP failures failed again.

Mutation: removed count_native_compute_refusal(reason); from refuse_native
(d3d12_compute_dispatch.inl:487). The rebuild recompiled d3d12_xs_device.cpp and relinked d3d12_xs.dll.
startup_native_shader_totals_refusals_warp then failed at the reasons assertion: the totals line
ended at compute refusal_overflow=0 with no reason entries. The other results were unchanged. After
restoring the file and rebuilding, it passed again.

CI coverage, restated: none of the three new tests and none of the four regression tests above run in
the PR job. This sandbox run is the only execution of them in this review.

@R0Y00
R0Y00 merged commit 88136c9 into R0Y00:main Nov 30, 2026
5 checks passed
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.

3 participants