Commit Graph
7 Commits
Author SHA1 Message Date
Anees Iqbal 45d69aff80 Dispatches keep their uses' storage and list no group counts outside predication
host_ab percall's gate holds a dispatch to 8.2 heap allocations a call (8.13 measured on the
M3 at the 11-03 re-bless). At a1ca5c673f a scratch allocation-site probe of the gate's
dispatch group (256 dispatch_threadgroups of tiny_kernel, nothing changed between them) counts
10 a call in steady state on the M3 Max (MoltenVK) and on gem10's RX 7900 XTX (RADV) alike, so
the gate fails on its own venue: the same ten sites in compute::record on both.

Two of them rebuilt per call what nothing needed rebuilt:

- binding::command_uses made each dispatch's uses in a CommandUses of its own (its uses, the
  sources added on 11-03 for patching a draw's buffer uses, and the plan's other uses: 3
  allocations), where a draw makes them over the last draw's storage, keeping those of what the
  encoder made resident (command_uses_into, LiveBind::uses). An encoder's own dispatch now
  does the same over LiveDispatch::uses; an indirect command's state still makes them whole.
- record listed every region's group count for predication.gates, which answers none outside
  a unit that gates dispatches: the counts are listed only where
  Predication::gates_dispatches says the unit gates them.

The uses a dispatch records, its gates and its commands are unchanged: command_uses_into is
the draws' path, whose builds with debug assertions check the kept uses against those made whole
at every command.

(cherry picked from commit 3ed927ecda1e395550b54db0c3b237827e45d3eb)
2026-11-08 14:31:46 -08:00
Anees Iqbal 6549792160 Count a moved buffer offset into its encoder's tally, not its thread's
refresh_offset counted bound_offsets_moved through the buffer's device,
its shared state and the calling thread's shard: a chain of loads and a
thread-local lookup at every moved slot of every draw rebound (about 34
of refresh_changed's 183 instructions a draw on lavapipe; a call into
the thread-local lookup on macOS).

refresh_changed now takes the caller's counts, which each of its three
callers holds already, and the slot counts there. bring_in takes its
encoder's counts once for both of its counts. WorkCounts sums encoder
tallies and thread shards alike, so the count reads the same.

(cherry picked from commit 880c98ae814cecace72c88ac61ef398db15fccbc)
2026-11-07 21:43:28 -08:00
Anees Iqbal ec9706e6e3 Survive the loss of the VkDevice: the device is a line of generations
Metal's MTLDevice survives a GPU recovery; the runtime's device refused every
call with DeviceLost from the first loss on, and the consumer ends its VM at
the first refusal. Now a device the runtime made is a line of generations
(device/line.rs), each a DeviceShared over its own VkDevice, and the first call
that needs the device after a loss makes a new VkDevice of the same physical
device, options, queue families and extensions.

- Two kinds of handle. The program's device (and its clones) is a root handle:
  it holds the line, pins no generation, and every public Device call goes to
  the generation that serves (each public method forwards to its *_now body).
  Objects hold generation handles, so their Vulkan calls, drops included, reach
  their own VkDevice alone. A generation handle's check is one relaxed load of
  the line's generation (Device::generation_serves).
- A draw pays nothing for it. A command buffer takes its generation once, as
  it is made (CommandBuffer::generation); its encoders' per-draw reaches (work
  counts, fresh-unwritten and alias records, sampler-slot changes, attachment
  read notices) read it there, not through a device handle's kind. A bind's
  device check is one compare of the cells the two handles hold, inlined, its
  refusal (Device::foreign) out of line.
- The state machine (Live, Lost, Failed): the claiming call recovers, every
  other call waits (a notice past 10 s, waiting on); threads command buffers'
  completion depends on never wait. Recovery: the lost generation's command
  buffers in flight fail through its completion path, its queues drain under
  their locks, create_on makes the next VkDevice, the queues are retired.
- Command-buffer answers, as a real loss is one more hang: the oldest
  incomplete batch's command buffer fails GpuHang (exact on one VkQueue; on two
  or more the queue whose oldest incomplete batch was submitted first, said so
  in the notice), counted against its queue so the second revokes it
  (AccessRevoked); the others GpuRecoveryVictim; one committed on the hang's
  queue before the recovery runs nothing and completes with no error; one made
  before and committed after fails GpuRecoveryVictim. One loss names one hang:
  a batch that went to the driver just before the loss and reached the lost
  device's completion after the loss took the pending batches fails with it,
  as a victim (its own wait need not fail: the test hook's VkDevice runs it),
  and no later failure of the lost device names a second hang.
- Command queues survive: a queue is a root handle and a cell of its place on
  one generation (VkQueue, slots, pools, hangs), which a recovery retires and
  the next command buffer re-makes; label and GPU-error count go on.
- Objects made before a recovery are refused where they meet the new VkDevice
  or need their lost one: RuntimeGap::MadeBeforeRecovery, at the 38 lost-device
  checks (Device::lost_refusal) and at every foreign-object check of one line
  (Device::foreign). DeviceLost stays for a device the consumer made.
- Carried across generations: the address space, resource IDs and the passing
  holds a making waits out before it takes one, unique identifiers, counters
  and the announced notice kinds.
- A device handle is an enum of the two kinds (24 bytes, was 16): the shadow
  merge's copy-back dispatch, which holds handles, is boxed (clippy's
  large_enum_variant).
- Device::recover_for_test (public, documented) runs the whole path with no
  GPU fault; RuntimeGap::AdoptedDeviceRecovery on an adopted device.
- Degradation::DeviceRecovered (device-recovered), announced at each recovery
  with the culprit and its time. WorkCounts::vk_devices_live, vk_devices_made,
  device_recoveries: two VkDevices while objects of the lost one live, one
  after.

Docs: docs/API.md catalogue entry, the runtime README's device-loss section,
FAST-PATHS known gap (a make whose own submission first meets the loss is
refused DeviceLost), docs/REFUSALS.md regenerated (refusal_surface.py names a
forwarded body's sites by its public method), api_signatures blessed.

Tests: runtime_lane::device_recovery (hang and victims, command buffers on
their way to the driver at a recovery, MadeBeforeRecovery and the VkDevice
bound, revocation at the second hang, a command buffer made before fails as
victim, all under the validation layer; a recovery that finds nothing of the
queue scheduled names no hang, its command buffers victims),
tests::device_recovery (the hold of the hang's queue between a loss and the
recovery), detach_lost and the MoltenVK lost-device lanes updated to the
recovery's answers.

Verified:
- lavapipe: runtime suite and touched validation tests 1850/1850, surface
  19/19 (m3-max-5695, gem10, before the rebase onto 7e039f062a); on
  7e039f062a, before the one-hang fix, 1853/1853, surface 19/19, clippy clean
  (m3-max-6150, ms01). This tree: clippy clean (m3-max-6278, msi).
- RADV RX 7900 XTX: 81/81 touched device tests (m3-max-5709). Iris Xe (ANV):
  80/81 (m3-max-5766), the 81st kept_images, which fails so at development
  too and the last of these three commits fixes; the recovery tests alone 3
  rounds of 3 (m3-max-5852). RTX 5080 Laptop GPU: 80/81 (b5080 entry
  devloss-devloss-tip1-nine-163410), the 81st the test's own 20 s bound on
  the recovery, which took 39.5 s beside another entry; the test now prints
  the recovery beside the device's first making and bounds neither.
- MoltenVK on the M3 Max: the device_recovery tests 8 rounds of 8
  (m3-max-6135); with the one-hang fix 12 of 12 (m3-max-6242), where without
  it 3 rounds of about 20 failed with a command buffer Completed after the
  hang (m3-max-6219, m3-max-6233). The RTX 5080 failed the second-hang test
  so in 2 runs of 3 (devloss-devloss-rec-alone-170457).
- Recovery time, alone, under the validation layer: llvmpipe 29.9 ms, RX 7900
  XTX 20.4 ms, Iris Xe 15 to 21 ms (its first making 30 to 41 ms), M3 Max
  (MoltenVK) 35 to 40 ms (42 to 50 ms), RTX 5080 530 to 559 ms (602 to 635
  ms). VK_EXT_device_fault listed: llvmpipe 0, XTX 1, Iris 0, 5080 1.
- Per draw (draw_costs, release, --heap, M3 Max, base 7e039f062a and the tip
  alternating, 4 rounds, m3-max-6121): instructions a draw ws +0.03%, many
  -0.05%, 10k -0.31%; draw_primitives +10, +8 and -26 of 17,172, 11,245 and
  7,736 a call; each set_* call +3 to +4 (of 425 to 860); a command buffer's
  creation +170 to +370, an encoder's end +120 to +390.

(cherry picked from commit 3d7758c074fc9895d5d6c9766604c6c4eae9166d)
2026-11-07 18:38:26 -08:00
Anees Iqbal 630deb9385 Count each command into a tally its encoder owns while it is open
What: the 58 counts a draw, dispatch, tile dispatch or bind adds as its
encoder records it (the 23 that were sharded by thread, draws_rebound,
stage_tables_read, stage_tables_read_in_part, draw_states_recorded,
set_zero_writes_pushed and resident_uses_* among them, and 35 that were
one shared AtomicU64 each, set_zero_sets_reused/found, no_copy_lies_read,
draw_shapes_*, graphics_pipelines_looked_up, draws_settled and the rest)
move into one struct, CommandCounters, kept in shards (shards.rs):

- by encoder: each EncoderCore takes one of 64 tallies as it opens
  (Tally::take; a process-wide bitmask, lowest free bit) and gives it
  back as it drops. Where the count is made with the core at hand
  (EncoderCore::counts, Counters::of), it adds into that tally by a
  plain load and store: no thread-local lookup and no atomic
  read-modify-write. An encoder is recorded by one thread at a time
  (EncoderCore is not Sync; under the Worker policy its lowering sits
  behind a mutex, render/deferral.rs), so the tally has one writer.
  Encoders past the 64 open at once share one more by an atomic add.
- by thread: counts made with no core at hand (a no-copy buffer's lie
  read, a pipeline lookup, a bound offset moved, the ycbcr binds, ...)
  go into the calling thread's shard (Counters::thread), as the draws'
  sharded counts did. The lowering thread takes its own shard as it starts.

The fold point is the read: Device::work_counts() sums every tally and
every thread shard, open encoders' included, so a count is whole whenever
it is read. Lanes and tests read the counts while encoders are open
(dispatch_work, dynamic_multi_variable, neutral_zeros_shared,
uniform_bind_offsets among others), so a fold at an encoder's end would
leave those reads short. A tally given back keeps its counts; its next
owner adds on top (release on give-back, acquire on take).

Why: the consumer's profile of game_draws-like frames (about 12k draws a
frame on several walk threads) spent 0.094 samples a frame in the work
counts, 0.077 of them in draws_rebound: each count took two calls on
macOS for the thread-local shard, and the shared ones were atomic adds
every walk thread contended for. Nobody read the counts in that run.
No public item changes: WorkCounts keeps its fields.

Verified, release builds of the runtime_lane game_draws lane (two object
passes of 6,001 draws each, 12,002 a frame; a measurement branch carries
the lane), ns of the encoding thread's CPU a draw, median of 5 rounds,
before (e1b342d1b9) -> after -> a floor build with every per-draw count
left out:

- RTX 5080 Laptop host (Core Ultra 9 275HX, encoding pinned to P cores
  by taskset, a sitting of its own): bytes 437 -> 423 (floor 425),
  8605 -> 8536 instructions a draw (floor 8446); chunks 532 -> 523
  (floor 517), 10900 -> 10867 (floor 10738); four threads encoding at
  once 669 -> 538, their chunks 763 -> 661; four beside 16 idle threads
  that drew once 903 -> 589 (the floor's 660, 728 and 687: the floor
  build's layout is no bound there).
- M3 Max (macOS 27.0.1, unpinned, other jobs on the host; 3 rounds
  single-threaded): bytes 416 -> 414 (floor 400), chunks 565 -> 566
  (floor 550), within the runs' spread there (another round read 462 ->
  473, floor 467); four threads 760 -> 684, their chunks 957 -> 779,
  beside 16 idle threads 1143 -> 703.
- frame_costs_plain (402 draws a frame): 5080 2.12-2.14 -> 2.11-2.23 ms
  of CPU a frame, M3 2.42-2.44 -> 2.38-2.44.
- Every count a frame of game_draws bytes, chunks and threads is equal
  before and after on the M3.

Tests, with the next two commits' changes on top: the runtime's
device tests that read a moved count (draw_again, dynamic_multi_variable,
neutral_zeros_shared, no_copy_lies, resident_uses, set_zero_at_offsets,
uniform_bind_offsets, deferred_calls, recorded_at_the_call and the rest,
with the shard tests and the new command_counts) pass on the M3 under
--cfg m2v_gpu (297 of 297) and on the 5080 (296 of 296), and the lane
modules that read one (dispatch_work, draw_work, frame_costs,
replayed_frames, provoking, line_widths and others) on the M3 (251 of
251); on lavapipe, 295 of 296 before command_counts' first precondition
was loosened. The three new tally unit tests pass on all three; under
a control where every encoder shares tally 0 all three fail (lost
additions), and under one where a read sums only given-back tallies (a
fold at the encoder's end) the two that read fail. clippy --all-targets
--all-features -D warnings on metal2vulkan-runtime is clean in the plain,
m2v_gpu and m2v_cpu_icd cfgs (Linux, the final tree; plain and m2v_gpu on
macOS too), and so is rustdoc -D warnings.

Resolved onto stack 35 (3e3eb3981f): resident_keys_settled, which stack
33 added to the draws' thread shards, joins CommandCounters and counts
into the encoder's tally at both of its draw paths (render.rs,
draw_again/rebound.rs); resident_uses_geometry_checked keeps stack 33's
walked count, now through the tally. host_alias_lookups stays the alias
table's own atomic: it counts only a lookup under that table's mutex
(host_aliases.rs augment_into), which a command whose keys are quiet
skips, so the add rides with the lock it counts. Two module docs that
still said a draw's counts went into its thread's shard now say its
encoder's tally (draw_again.rs, rebound.rs).

(cherry picked from commit 834354cb55a139b7ce01fb83ff30d7ced45742b6)
(cherry picked from commit 777714ca010d0bb476a8457596c4fc54030cd6b5)
2026-11-05 18:59:19 -08:00
Anees Iqbal 62b0650c1a Set a kept compute state's token-order and resident-apart flags as its whole state has them
LiveDispatch::refresh brings a compute dispatch's bind state in place, but left
BindState::used_in_token_order and resident_apart at false, where
DispatchTables::bind_state (d6f17f77ab, a38aeff464) sets both. The debug check
check_whole then failed on every kept compute dispatch (616 of 1837 lane cases
and 11 runtime device tests), and release builds bound kept compute states
without the search and the trimmed root tables. Neither flag is part of the
bind shape, the residency lists its used resources in token order, and a
dispatch's memo is filled only after refresh, so nothing kept predates them.

Verified: the indirect_dispatches, predication, first_use and stage_in_strides
lane cases, 37 of them, pass under --cfg m2v_gpu on the M3 Max (debug).

(cherry picked from commit 2d8616c74700cce4a28bcad52229a49e82aa9567)
2026-11-03 12:36:59 -08:00
Anees Iqbal 81681968e4 Compute encoders look at their resident textures once a change, and usage shadows read without a lock
Each compute dispatch walked every resident texture for its shadows (shadow::shadow_uses), taking
each texture's usage-shadow mutex, and each dispatch reading texture handles walked them again
for one a carrier holds (pvrtc::degrade_unreadable); each encoder open walked every resident
resource for shadowed textures (Residency::shadows).

- shadow.rs: ResidentShadows keeps the resident textures' shadow copies, listed again only where
  the residency's generation or the device's count of usage shadows made
  (DeviceShared::usage_shadows_made) moved; shadow_uses_of takes the bound textures fresh, then
  the kept list, in the order shadow_uses took them.
- pvrtc.rs: ResidentUnreadable keeps the first resident texture a carrier holds, found again only
  where the generation moved (a carrier and a decoded twin are fixed at a texture's making); the
  announcement is made at the same dispatches as before.
- compute/live.rs: the encoder's kept state holds both lists, emptied when it goes to the spares.
- texture/usage_shadow.rs: the usage shadow is a OnceLock read without a lock, made under its own
  mutex, and counted on the device once readable.
- residency.rs: Residency::shadows walks the textures alone, in the order they were made resident.
- WorkCounts::resident_textures_looked_at counts the looks.

Bound textures are still looked at per command, now without a lock each. The same copies,
barriers and announcements at the same commands. Render's draw and tile dispatch keep calling
shadow_uses (render code; they can take ResidentShadows the same way).

Check that fails without it: runtime_lane::dispatch_work::dispatches_list_what_their_encoder_made_resident_once_a_change
now also makes 16 textures resident and counts the looks: (1, 0 per offset move, 1 after a buffer
made resident); with the list made at every dispatch, each offset move looks again (1 for 0).

Release, M3 Max, MoltenVK 1.4.2, host_ab encode, before (1b3fe1c315) and after interleaved twice,
us per dispatch (medians of 3 rounds):

| workload | before | after |
|---|---:|---:|
| dispatch_resident_textures_4096 | 35.4-35.7 | 0.76-0.78 |
| dispatch_resident_textures_256 | 2.63-2.69 | 0.64-0.65 |
| dispatch_resident_256 | 0.633-0.640 | 0.640-0.653 |
| dispatch_resident_moving_256 | 1.569 | 1.575-1.581 |
| dispatch_static | 0.655-0.669 | 0.673-0.676 |

Verified on MoltenVK: the runtime GPU suite 1505/1505; the validation runtime lanes 533/535 (the
two failing the same at the base); the shadow, usage-shadow and carrier lanes 52/52.

(cherry picked from commit b01779101c7b6e05d435274ddd929da7441816df)
(cherry picked from commit 0c35f13441cf9d4c8d1b788632b5edf65afaa78f)
(cherry picked from commit 686656d2b2899865da6e5425125cdaf973e154c4)
(cherry picked from commit a984fef4d783b985883318703f93b0f72c55e90d)
2026-11-03 01:40:27 -08:00
Anees Iqbal a4546da224 Compute dispatches keep their bind state between dispatches, and pipelines keep four plans
A compute encoder's dispatch that did not take what the last one settled rebuilt its whole bind
state, listing every resource the encoder made resident, and each pipeline state kept one plan, so
per-dispatch cost grew with the resident count and two encoders of one pipeline with different
resident sets planned again at every dispatch.

- compute/live.rs: the encoder keeps its bind state (LiveDispatch), brought in place at each
  dispatch as a render encoder's draw does (live_bind.rs): the kernel table is read again only
  where its stamp, interface, sampler maps or no-copy moves changed, and only the slots calls
  changed where the table keeps them; the used resources are listed again only where the
  residency's generation, a no-copy move or an indirect command buffer's slot changed; the shape
  is kept while no change may have changed it. Debug builds check the whole state against one
  built from scratch at every dispatch. The state goes to the device's spares at the encoder's
  end (four kept), with its memo let go first, so a kept state holds nothing alive.
- DispatchMemo stays the "nothing changed" shortcut, now owned by the live state.
- pipeline/mod.rs: a pipeline state keeps four plans, least recently used first out, found by
  identity and then compared in place (BindState::position_of_shape), with the same key.
- The ICB and host-step dispatch paths keep the fresh path.
- WorkCounts: dispatch_shapes_made, dispatch_shapes_found, dispatch_used_resources_listed.
- residency.rs: the Generations doc says what is listed once per change, and where.

The same Vulkan commands, barriers, notices and refusals at the same calls; retains unchanged.

Checks that fail without it:
- runtime_lane::dispatch_work::dispatches_list_what_their_encoder_made_resident_once_a_change:
  with the residency reuse forced off, every offset change lists again ((1,0,1) for (0,0,1)).
- dispatches_take_what_their_state_planned_for_the_same_shape gains a third encoder of the first
  shape: with one kept plan it plans again ("an encoder of the first shape", 0 for 1).

Release, M3 Max, MoltenVK 1.4.2, host_ab encode (3 rounds, median, us per dispatch), base 096b427da3:

| workload | before | after |
|---|---:|---:|
| dispatch_resident_moving_0 | 2.090 | 1.757 |
| dispatch_resident_moving_16 | 2.570 | 1.754 |
| dispatch_resident_moving_256 | 11.472 | 1.764 |
| dispatch_resident_moving_4096 | 209.803 | 1.799 |
| dispatch_offsets | 2.186 | 1.929 |
| dispatch_churn | 2.164 | 1.928 |
| dispatch_static (memo hit) | 0.846 | 0.848 |
| dispatch_resident_4096 (memo hit) | 0.961 | 0.807 |

host_ab calls: many_dispatch 6397 -> 4095 ns and 74 -> 49 allocations; many_dispatch_resident
5331 -> 3496 ns, 78 -> 50; wide_dispatch 2825 -> 2293 ns, 40.9 -> 36.6. percall allocation
counts unchanged.

Verified on MoltenVK (with the tracker change that follows also applied): the runtime GPU suite
1504/1504; the validation runtime lanes, but for two failures that fail the same at the base
(function_tables::tables_and_handles_answer_as_metal_s_do,
predication::every_dispatch_form_runs_in_each_pass_past_the_copies).

(cherry picked from commit 8f350dddbe5c1cb3f5a486413b19ff23aa4f16c6)
(cherry picked from commit 5b3671810c7d658fd4223d5a6c97c1e33443e8a2)
(cherry picked from commit 03fe129bd31904ddf5013631ce104bb47438eaa9)
(cherry picked from commit 6457fa412ca00092e5b0a8603e05cbd99cd130b4)
2026-11-03 01:40:26 -08:00