Files
metal2vulkan/docs
Anees Iqbal 24dc3f20bb Keep an ICB range's chunks from their second execution, and forget a written chunk in one store
A range's first execution no longer pays for keeping. Before, every execution of CPU-encoded
commands recorded each 256-command chunk apart, in a KeptContext of its own (a command pool, a
descriptor arena and uploads), so it could run that recording again later, even when no later
execution came. Now a chunk is recorded to keep only from its second execution with no write
reaching it since. The first execution since a write records inline, as before 922caf3199, and
keeps nothing. A consumer that re-encodes every command each frame now pays nothing for keeping.

- kept_commands.rs: KeptChunks notes which chunks an execution that may keep its commands ran
  since a write last reached them. look_up answers each chunk of a range as Kept::Recording (a
  kept recording), Kept::Again (run before, nothing kept) or Kept::First, and notes each as run.
  IndirectCommandBuffer::kept_chunks answers None, so the execution records inline, when every
  chunk is First. When some chunks are kept and others are not, a First chunk records apart from
  its command buffer's own pool and arenas, with no KeptContext, and is not kept. An Again chunk
  records apart in a KeptContext and is kept.
- KeptChunks is dense: one ChunkState per chunk (kept recording, stepped, seen) up to the
  furthest chunk looked up, plus a flag for whether any chunk holds a state. KeptChunks::wrote
  costs one store per chunk the written range covers, whatever the number of kept chunks, and
  returns at once while no chunk holds a state. Before, it ran BTreeMap/BTreeSet::retain over
  every kept and stepped chunk on each setter call.
- WorkCounts::kept_contexts_made counts the KeptContexts made (api_signatures.tsv).
- Docs: kept_commands.rs, render/executions/kept.rs, indirect_execute/kept.rs and
  docs/FAST-PATHS.md (the kept-recordings row).
- examples/icb_setters.rs --kept now executes three times before each repetition: the first
  keeps nothing, the second records every chunk to keep, and the third runs all of them from
  those recordings.

Tests:
- runtime_lane::kept_commands asserts kept_contexts_made beside the records read and the chunks
  and commands kept.
  - A once-executed range makes no context: [N, 0, 0, 0].
  - Its second execution keeps every chunk: [N, 0, 0, 3].
  - Its third runs them: [0, 3, N, 0].
  - After a write, the written chunk records inline again and the next execution keeps it.
- kept_commands::tests::a_write_costs_the_chunks_it_covers asserts the count of chunk states
  wrote() looks at.

Verified on the M3 Max:
- Debug device lanes (--cfg m2v_gpu, nextest), with the live compute flags fix from stack22
  applied locally (not part of this commit): every runtime_lane module that executes an indirect
  command buffer (39 modules, 471 tests) pass.
- Negative control for the ruling (each chunk keeps from its first execution), debug:
  3 of the 5 kept_commands tests fail, a first execution making 3 contexts where 0 are expected
  and a written chunk making 2.
- Negative control for wrote() (it walks every chunk state): a_write_costs_the_chunks_it_covers
  fails, looking at 1000 states where 1 is expected.
- Unit tests: kept_commands::tests 2/2 and metal2vulkan tests::fast_paths 19/19.
- cargo fmt --check and clippy -D warnings (runtime and validation, all targets and features,
  --cfg m2v_gpu) are clean.
- icb_setters compute --kept (release, instructions per command, median of 30, two runs each):

  | | before (2c542021bf) | after |
  |---|---|---|
  | compute command, nothing kept | 2528.7 / 2529.1 | 2428.1 / 2427.7 |
  | compute command, 64 chunks kept | 5947.8 / 5945.5 | 2586.5 / 2583.9 |
  | set_kernel_buffer, 64 chunks kept | 1129.6 / 1129.6 | 456.9 / 456.3 |

  With three executions, the third runs all 64 chunks from kept recordings (the
  bench prints [64]), at 2583.9 and 456.3 instructions.
- conformance cost --alone indirect-render-commands-inherited-state reads arena_pools_made 1,
  argument_objects_retained 2 and argument_tables_retained 1, the recorded bounds. With a first
  execution's KeptContext these read 3, 6 and 3 (the cost run that held its record back).
- execution_costs (release) on a loaded host: wall times are dominated by host load. An earlier
  pair of runs read the first execution as slower (render n=1024 all re-encoded commit 2.88 to
  6.55 ms), but the parent then read 6.55 ms itself under the same load. Time Profiler, test
  thread CPU, same case and count, this change on 2b55ab415e against its parent:
  - render n=1024 one re-encoded, 300 executions: 611 to 610 ms;
  - compute n=1024 all re-encoded, 80 executions: 336 to 333 ms;
  - render n=1024 all re-encoded, 120 executions: 782 to 859 ms. This is the inline path's
    whole-range read: RenderExecution::commands collects the range into a second
    Vec<Option<Command>> (memmove 0 to 25 ms in RenderExecution::record), and render_records
    reallocates as it grows. The same profile has IndirectCommandBuffer::edit, whose code is
    the same in both builds, up 31 to 40 ms, so part of the difference is load.
  A later run of this commit's tree read render n=1024 all re-encoded at 2.37 ms commit, one re-encoded
  at 0.61 ms, and compute n=1024 all re-encoded at 3.49 ms.

Needs a RUNTIME_VERSION bump. No SEMANTICS_VERSION: no translated module changes.

(cherry picked from commit a54756c6a4f7fbe9ae82d732e8de46a19f4f86e5)
(cherry picked from commit 71e798c081648ea3818abbffc7538a3061456c75)
2026-11-03 13:34:02 -08:00
..