Skip to content

startup: copy compute input snapshots after the output checks - #3481

Merged
R0Y00 merged 4 commits into
R0Y00:mainfrom
Leyukaka:runtime-snapshot-after-outputs
Nov 30, 2026
Merged

R0Y00 merged 4 commits into
R0Y00:mainfrom
Leyukaka:runtime-snapshot-after-outputs

Conversation

@Leyukaka

Copy link
Copy Markdown
Collaborator

Fixes #3479.

What changes

On main (c4b8a41b), indirect_table_buffer copies a compute input that lives only in guest memory into the snapshot pool as soon as the input is resolved, and reserves the 16 zero bytes of a null input at the same point. The dispatch's outputs are checked afterwards, so a dispatch refused on an output has already used pool bytes.

With this change:

  • indirect_table_buffer returns a pending view for these two cases (source 2: guest address and size, no resource; source 3: 16 bytes, no resource). The guest range is still checked for readability there.
  • record_translated_dispatch does the copy and the null reservation after the output, heap SRV and indirect-argument checks have passed. It is the same recording call, still under draw_lock. Readability is checked again just before the copy.
  • For a typed input, FirstElement is set from the actual pool offset once the copy is done. The descriptor size check stays where it was.
  • compute_inputs_snapshot and compute_inputs_snapshot_kib advance after Dispatch/ExecuteIndirect is recorded, for the same inputs as before (raw, source 2). A refused dispatch no longer counts.
  • The once-per-key "input comes from" log line is emitted after these checks, so a refused dispatch no longer consumes it.
  • New export x2w_d3d12_snapshot_pool_used() in the shim (sum of used over the snapshot chunks, under allocator_lock), used by the fixture.

Only the two callers that allow snapshots, both in record_translated_dispatch, can receive a pending view.

The copy happens later in the call than before, so the interval in which another guest thread can write the range before it is copied is slightly longer. Nothing in the recording call writes guest memory in between.

Refusal order

Refusal texts are unchanged. Which refusal is reported first changes for a dispatch that has two refusals at once:

  • An input-side pool refusal (guest range not readable at copy time, copy from guest memory failed, "a null input could not get its zero bytes", pool offset not on a whole element or not 16-byte aligned) together with a refusal on a later input, an output, a heap SRV or the indirect argument: the second one is now reported first.
  • For one typed input, the minimum-size check is now reported before a pool reservation failure.

Tests

New startup_compute_snapshot_order_{warp,hardware}: one synthetic translated dispatch whose input is only in guest memory, sampled with x2w_d3d12_snapshot_pool_used() before and after recording.

  • accepted dispatch: 1,536 bytes (1,280 rounded to the pool's 512-byte alignment);
  • same input with an empty output descriptor, refused with "an output buffer descriptor has zero records": 0 bytes;
  • null input with the same empty output: 0 bytes;
  • snapshot flag off: refused as on main, 0 bytes.

Both pass on WARP and on hardware.

ctest -R '^startup_compute_(snapshot_order|dispatch(_input|_output)?|typed_buffer_srv)_(warp|hardware)$' -j 1

9 passed, 0 failed.

Skipped, reported separately: startup_compute_typed_buffer_srv_warp exits 77 because WARP here lacks SM 6.6 with 32-lane waves. The hardware variant passes.

Mutations, against startup_compute_snapshot_order_warp:

  • eager ring_copy put back in indirect_table_buffer: fails (refused dispatch uses 1,536 bytes);
  • eager null-input reservation put back: fails (refused dispatch uses 512 bytes).

Merges cleanly with #3469, #3466 and #3480.

Not covered

  • Two refusals come after the copy and still use pool bytes: "a formatted input has no word 3" and "an input texture could not be uploaded". The counters stay correct in both cases.
  • No test makes the deferred copy itself fail.

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

I read this against main, and the deferral looks correct:

  • pending views carry no resource and so can't alias an output or the indirect argument;
  • the counters and the "comes from" log line keep their old scope (raw inputs only, with source 1 still logged);
  • the typed path's whole-element check moves to the point where the real pool offset is known.

One gap in the test. In startup_compute_snapshot_order_check.py, the accepted dispatch is the first snapshot in a fresh process, so its pool slice probably starts at offset 0. The new code that matters most is the part that uses the actual slice:

  • view.offset from the deferred ring_copy;
  • FirstElement = view.offset / element for a typed input.

At offset 0, a mutant that kept the pending offset (0) or skipped the FirstElement update would still pass. Two ways to pin it:

  • record a second snapshot dispatch, or a null input first, in the same process, so the checked slice starts at a non-zero offset;
  • add a mutation that leaves FirstElement at 0, and one that binds the pending offset, and show both fail.

A typed-input variant of the accepted case would also cover the described branch of the deferred loop, which the raw-only cases don't reach.

A minor, pre-existing point that this PR makes visible: the pool hands out slices aligned to 512 bytes, so a 3-channel typed input (12-byte elements) is refused with "does not start at a whole element" unless its slice starts at a multiple of 1,536. main has the same behaviour with the eager copy, so it's not a regression. Now that the check runs after the copy, it may be worth a comment.

AI-assisted.

@Leyukaka

Copy link
Copy Markdown
Collaborator Author

New head 9f3068b covers the three points:

  • The accepted case now records a zero-filled seed snapshot first, so the checked slice starts at pool byte 1536 (raw FirstElement 384); pool use is asserted as 1536 -> 3072.
  • A typed-input variant (12-byte elements, FirstElement 128) reaches the described branch of the deferred loop; the input words encode their pool position, so the readback shows where the view began.
  • d3d12_compute_dispatch.inl has a comment on the 512-byte slice alignment and the 1536-byte multiple for 12-byte elements. No behaviour change.

Mutations, each alone, on WARP and hardware: binding the pending offset fails at startup_compute_snapshot_order_check.py:83 (word 0 read 01234567, expected ea74e376); leaving FirstElement at 0 fails at :106 (word 0 read 00000000, expected 00000600).

ctest -R '^(startup_compute_snapshot_order|startup_compute_dispatch_input|startup_compute_d16_sample_srv_)': 7 run, 6 passed, d16_sample_srv_warp skipped for capability.

@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 9f3068b2 by reading the diff; not built or run.

Verdict: approve. The deferral is contained to the one function that can receive a pending view, and the body's list of what still uses pool bytes is exact. No MUST.

  • Callers: only d3d12_compute_dispatch.inl:221 (inside typed_dword_input_view, called only at :1023) and :1034 pass allow_title_snapshot = true. Both are in record_translated_dispatch. The result-word path (:1234), the draw path and native mesh57 keep the default, so they never see a null-resource view. #3506's head adds no snapshot caller either. record_translated_dispatch is reached only from the guest list's Dispatch and ExecuteIndirect entry points (d3d12_xs_device.cpp:4113, :4845), so the later copy stays on the recording thread under draw_lock.
  • After the copy: the only refusals between the deferred loop (:1248) and Dispatch (:1392) are "a formatted input has no word 3" (:1300) and "an input texture could not be uploaded" (:1314). That matches "Not covered", and the counters skip both because they advance after Dispatch.
  • Source 1 views pass through the new loop only to keep their alignment check and "comes from" line. Source 0 continues, so the old log scope is kept.

Tests: startup_compute_snapshot_order_{warp,hardware} reach the deferred copy, the null reservation, both binding paths at a non-zero offset, and three refusals at zero pool use. CI did not run them: they are not in the pr preset's list, and /O2 was skipped on the PR. The author's WARP and hardware runs are the only ones at this head.

@R0Y00
R0Y00 merged commit 92ad10f 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.

startup: compute input snapshots are taken before the dispatch's outputs are validated, so refused dispatches use the snapshot pool

4 participants