Skip to content

hk: pack runs of plain compute command buffers into one command - #488

Merged
DjDeveloperr merged 11 commits into
aurora-wipfrom
djdev/g16-submit-packing
Nov 20, 2026
Merged

DjDeveloperr merged 11 commits into
aurora-wipfrom
djdev/g16-submit-packing

Conversation

@DjDeveloperr

@DjDeveloperr DjDeveloperr commented Nov 19, 2026 •

Copy link
Copy Markdown

Geekbench's Vulkan compute workloads (and ML graphs and image filters generally) record one dispatch per command buffer and submit hundreds at once. Each command buffer became one firmware command, and each command costs a fixed firmware and host turnaround: ~0.3 ms on the M4 (G16G) before the matching kernel change (aurora-silicon/linux#2419), ~11 µs of GPU time plus host time on the M5. Metal records one command buffer with many encoders: ~1 kick per iteration.

This PR packs each run of consecutive command buffers that hold one plain compute stream into one submission-owned stream, submitted as one command. It is the M5 driver-overhead lane's design, ported to aurora-wip.

Where it is on

Packing is chosen by how a member's end publishes its writes to the next member (hk_pack_boundary()), not by which GPUs were measured:

  • M5, wherever the kernel reports DRM_ASAHI_FEATURE_COMPUTE_VISIBLE_COMPLETION. Every stream then ends with the strong memory-scoped flush the kernel takes as the command's completion, and a boundary keeps it. The kernel advertises the feature only with the M5 command format and its completion inputs, so every GPU that reports it has the same completion semantics.
    • On the current integration kernel only J714 (M5 Pro, G17S) reports it.
    • M5 (G17G), M5 Max and M5 Ultra (G17C) boards don't report it yet. They pack once their kernel does, with no Mesa change.
  • HAL200 streams whose strong flush has no memory-scoped form (G16). The kernel publishes a command's writes when it completes, and a boundary becomes the in-stream strong flush (0x60001560 on G16G), the record an in-buffer compute/transfer barrier already gets.
  • No packing elsewhere: G17P (its strong flush also has a memory-scoped form, which aliased memory needs and no such boundary asks for), G15 (wide-visibility barrier) and AGX2.

What a packet keeps

  • Recorded barriers and each stream's end-of-buffer flush are kept. Every wait a member had is kept: the planner only groups command buffers, and a member that would add a render wait or a synchronization kind starts a new packet. The planner reads the same fields the resolver does.
  • Streams with links, calls, immediate writes, queries, timestamps, events, indirect or internal launches stay separate commands.
  • At most 256 launches, the thread and static-work bounds, at most one stream with a looping program, and no more attachments than one kernel command takes, per packet.
  • Global atomics: packed on G16G. Its kernel runs every compute command of a VM in order on one firmware compute queue, and never beside another VM's (linux#2419 keeps several commands of one VM in flight on that queue). No uAPI feature reports that yet, so this one switch stays keyed to the chip. Opt-in elsewhere.
  • Switches, kept for A/B rows: HK_PACK_CDM_SUBMIT=0/1 (the old ASAHI_M5_PACK_CDM_SUBMIT still works), HK_PACK_ATOMICS=0/1, and HK_PACK_CDM_TRACE=1 for a per-submit stderr line.

Tests

  • hk_pack_test (suite asahi, 135 checks) runs the real hk_sync recorder and resolver with hk_pack.c:
    • the selector, every member rejection and the planner;
    • packets formed by the planner, resolved in legacy and exact-prior mode;
    • a randomized packed-vs-unpacked harness (3180 packets, 0 failures);
    • a queue-history harness (4872 packets, 0 failures).
  • aurora-packtest (hardware) compares 21 cases word for word with a CPU model. It reports packed command buffers per case and fails a must-pack case that packs nothing with HK_PACK_CDM_SUBMIT=1, or any packing with =0.

Hardware verification

  • G17S (M5 Pro): hardware verification pending (M5 lane s8c). It will be the first GPU reporting the feature to run this head; results are posted here when they land.

  • G16G (M4, J773g Mac mini), with the kernel PR, at this PR's first head:

    • packtest 21/21 PASS packed (chain242 25.4 → 0.84 ms, atom-chain 20.6 → 0.98 ms, indep-cbs 13.6 → 0.61 ms);
    • Geekbench 6.7.1 GPU Vulkan, ms per iteration (kernel PR only → kernel PR + this; macOS Metal): Background Blur 34.7 → 23.2 (9.1), Face Detection 50.9 → 38.8 (6.2), Feature Matching 40.4 → 37.5 (15.0), Particle Physics 67.0 → 64.2 (31.2); Stereo, Gaussian, Edge and Horizon unchanged (≤ 0.3 ms);
    • Vulkan CTS 1.4.7.1, 1/16 sample (198,732 cases): 84,219 Pass, 0 Fail, 0 Crash.

    The on/off row on the reviewed head (aurora-packtest must-pack and pack-off checks, Geekbench HK_PACK_CDM_SUBMIT=0/1) runs in the next mini slot and will be posted here.

  • What M5 packs with this PR alone. At the visible-completion boundary a member must end with the memory-scoped strong flush, as hk_EndCommandBuffer() makes a trailing dispatch's weak tail. An application barrier at the end of a command buffer leaves a strong flush without memory scope (no aliasing feature enabled), and that does not qualify.

  • G17C and G17G: not packing yet. They pack once their kernel reports DRM_ASAHI_FEATURE_COMPUTE_VISIBLE_COMPLETION.

    • If you have this hardware, please run aurora-packtest after merge, only on a kernel that reports the feature. Without the feature, packing stays off by design, so the must-pack checks under HK_PACK_CDM_SUBMIT=1 FAIL.
    • On such a kernel, HK_PACK_CDM_SUBMIT=1 aurora-packtest 3 should show every case PASS with packed_cbs > 0 on the must-pack cases, and HK_PACK_CDM_SUBMIT=0 aurora-packtest 3 should show packed_cbs=0.

meson test --suite asahi: 139/139 (host aarch64, clang, release).

🤖 Generated with Claude Code

@RyanTheTide

Copy link
Copy Markdown

Thanks for porting the packing work. The packtest and Geekbench numbers on the M4 mini are a big win, and the bounds on each packet are carefully reasoned.

One request about how packing is selected. In this project we choose behaviour by a hardware or kernel property, not by the list of machines it has been measured on. In src/asahi/vulkan/hk_queue.c:

  • 2385-2389: on M5 the check is already the kernel feature DRM_ASAHI_FEATURE_COMPUTE_VISIBLE_COMPLETION, which is the right selector.
  • 2399-2400: /* On for G17S (M5 Pro) and G16G (M4); other M5 parts opt in. */ and return dev->dev.chip == AGX_CHIP_G17S || dev->dev.chip == AGX_CHIP_G16G;

G17C and G17G use the same stream encoding and expose the same feature, so they would only be left out because they haven't been measured. Could the M5 default be "on wherever the kernel reports visible completion"? Keep a chip check only where there is a real hardware difference, with a comment saying what it is. The G16G reason (one command at a time on HAL200) is a good example.

Once the default is set that way, HK_PACK_CDM_SUBMIT and the ASAHI_M5_* aliases could become debug-only or be dropped. We are happy to run rows on our M1 Pro, M2, M4 Pro and A18 Pro machines if you want to see how far the same approach carries.

Submit packing copies the compute streams of several command buffers into
one command. To decide which streams it may copy and how much work one
command may then hold, it needs facts the stream does not record yet:

- whether a dispatch runs a program with global atomics (a new compiler
  output, set where global atomics are emitted);
- how many launches are direct application dispatches, their threads,
  and threads times static native instructions;
- whether the stream calls another stream through a link with return
  (ray tracing's single kick), whose bytes must stay where they were
  recorded;
- whether the command buffer sets, resets or waits on an event, which
  the host or another queue can observe before its submission completes.

Stream merges carry the new fields, and hk_cs_merge_cdm_until() merges a
stream up to a given point, before its terminate, into a stream of
another command buffer. No behaviour changes.

Signed-off-by: Diljit Singh <hey@djdev.me>
Every firmware compute command costs a fixed setup and completion on top
of its dispatches: on the M5 Pro (G17S) a chain of 242 one-dispatch
command buffers took 3.0 ms as separate commands and 0.31 ms as one, and
the M4 (G16G) firmware runs the device's compute commands one at a
time, with 0.1 to 0.3 ms of firmware and host turnaround each. Applications that record a
dispatch or a few per command buffer and submit many at once (ML graphs,
image filters, Geekbench's compute workloads) pay that per command
buffer.

At submit, copy each run of consecutive command buffers that hold one
plain compute stream back to back into one submission-owned stream, and
submit the run as one command. Each member boundary publishes the
member's writes to the members after it:

- M5 streams end with the strong, memory-scoped flush that visible
  completion asks for; the copy keeps it.
- G16G streams end with the last launch's ordering barrier or a strong
  flush, and the end of the command publishes the writes. At a boundary
  the record becomes the strong flush (0x60001560), the record a compute
  or transfer dependency inside one stream gets on G16G. HAL200 has no
  memory-scoped or wide form, and hk treats no G16G memory as reachable
  through a second address (hk_cdm_alias_scope() is false there), as
  for those in-stream dependencies. The last member's tail
  stays as recorded, followed by the packet's own end of command. The
  packet carries no compute flags, which the G16 kernel requires.

Work in the packet waits for what any member waited for, and later work
waits for the whole packet: packing only adds ordering the firmware
imposes on separate commands anyway. Streams with links, calls,
immediate writes, queries, timestamps, events, indirect or internal
launches, or global atomics stay separate commands. A packet covers at
most 256 launches, 2^28 threads and 2^35 threads times static
instructions, so one watchdog allowance (5 s per command on G16) still
covers it, and no more compute attachments than the kernel takes for one
command, so none is dropped that a separate command would carry; a
longer run starts a new packet.

The packet's stream lives in a driver-owned buffer. On M5 the existing
command-memory pins keep it until the submission retires; elsewhere the
same mechanism now pins that buffer alone, since the BO cache would
otherwise hand it out again while the GPU still reads it.

Default on for G17S and G16G. HK_PACK_CDM_SUBMIT=0 turns it off (the
older ASAHI_M5_PACK_CDM_SUBMIT is still read), =1 turns it on for the
other M5 parts; HK_PACK_CDM_TRACE=1 prints what each submit packed.

Signed-off-by: Diljit Singh <hey@djdev.me>
Each packed M5 command buffer ends with the strong, memory-scoped flush
that publishes its writes at the end of its own command. Inside a packet
the next member reads through the same GPU addresses, and the host and
other queues observe nothing of the packet before its completion, so
between members the plain strong flush is enough unless memory can also
be reached through a second address (hk_cdm_alias_scope()). The packet's
last flush keeps the memory scope. G16G has no memory-scoped flush and is
unchanged.

Signed-off-by: Diljit Singh <hey@djdev.me>
Streams with global atomics stayed separate commands because, on M5, two
kernel queues running packed atomic chains on the same memory lost
contributions (root cause unknown). That concern needs another kernel
queue running beside the packet.

On G16 every queue feeds one firmware compute queue, which runs one
command at a time (queued commands take exactly their GPU time each,
measured on the J773g Mac mini), so nothing runs beside a packet there: G16G packs such
streams by default (HK_PACK_ATOMICS=0 keeps them separate). On M5 they
are packed only with ASAHI_M5_PACK_ATOMICS=1 (or HK_PACK_ATOMICS=1) and
while the device has a single kernel queue, which runs its commands in
order; a second kernel queue created while a packet runs is not covered,
and unpacked atomic work has the same exposure.

Signed-off-by: Diljit Singh <hey@djdev.me>
With HK_PACK_CDM_TRACE=1, a submission that packs nothing now says so too,
and every line counts the command buffers that qualified as packet
members, so a run can tell a workload that never qualifies from one whose
qualifying command buffers are not adjacent.

Signed-off-by: Diljit Singh <hey@djdev.me>
@DjDeveloperr
DjDeveloperr force-pushed the djdev/g16-submit-packing branch from 0eec131 to 2c46924 Compare November 19, 2026 22:02
@DjDeveloperr

Copy link
Copy Markdown
Author

Rebased onto aurora-wip af7ec5f (#551 merged). Old head 0eec131 (on 57df4db), new head 2c46924.

  • One conflict, in hk_queue.c: upstream added the resolved argument to hk_sibling_eligible(), and this PR adds the
    !packed_bo guard to the same condition. Both are kept: a packed submission still never goes to a sibling queue.
  • The HK_PACK_* switches (and their ASAHI_M5_PACK_* aliases, which this PR adds) stay for now, so the A/B
    fallback survives the hardware verification. One follow-up drops all of them after this merges and is verified.
  • Nothing asahi, hk: drop the remaining driver env switches and diagnostic dumps #551 removed comes back: the only env reads in the diff are this PR's own packing switches.

Local checks (Actions doesn't run on this repo), host aarch64, clang 23.1.1, -Dbuildtype=release -Db_ndebug=true -Dvulkan-drivers=asahi -Dbuild-tests=true, -j2:

  • libvulkan_asahi.so builds (119 s).
  • meson test --suite asahi: 138 Ok, 0 Fail (137 test programs).
  • No compiler warning in the lines this PR adds. The one hk_queue.c warning is an existing upstream line.

M4 hardware A/B (packtest packing off/on, Geekbench 6 Vulkan with HK_PACK_CDM_SUBMIT=0/1) is queued for the next
J773g mini slot. #506 is stacked on this branch and needs a rebase onto the new head.

@DjDeveloperr

Copy link
Copy Markdown
Author

Independent review (separate agent, not the author's lane)

Reviewed head 2c4692477bb6 against base aurora-wip af7ec5f4. I read the diff together with the code it relies on: queue_submit_recordings(), hk_sync_resolve_submit(), hk_cs_merge_cdm_until() / merge_control_streams(), hk_cdm_flush(), the pending-pin code in hk_cmd_pool.c, and how events, timestamps and queries are recorded.

I found no wrong-result bug in the dependency translation, the member-boundary flushes or the packed BO's lifetime. There are two design problems and a test gap worth fixing before this ships on by default on G16G and G17S.

Findings (most severe first)

1. The packet bounds don't limit how long a packet runs, so a packet can exceed the per-command watchdog that each member would pass.
hk_queue.c:2388-2392, 2626-2662; hk_cmd_dispatch.c:2968

The work bound is threads × static instruction count. The code comment says this doesn't bound loops, but the commit message relies on it: "so one watchdog allowance (5 s per command on G16) still covers it".

Scenario: an ML-style submit of 256 one-dispatch CBs, each with 2^20 threads, 64 static instructions and a 2048-iteration K loop.

  • Static work is 2^26 per member, so all 256 fit one packet: 2^34 ≤ 2^35, and threads total exactly 2^28.
  • Dynamic work is about 1.4e11 thread-instructions per dispatch, roughly 70 ms each on the M4 10-core at peak issue rate.
  • Unpacked, each command takes about 70 ms. Packed, the single command runs about 18 s, past the 5 s allowance: the GPU times out and the device is lost.
  • CTS won't catch this. It also affects apps that split long compute across command buffers specifically to stay under timeouts.

Fix: have the compiler report whether a program contains loops, the same way this PR adds uses_global_atomics. Keep such dispatches out of multi-member packets, or cap packets that contain them to a few members. If the G16 and M5 timeouts turn out to be no-progress detectors rather than total-duration limits, say so in the comment and drop the claim instead.

2. Hoisting a later member's render (VDM) wait onto earlier members serializes compute that used to overlap rendering.
hk_queue.c:2766-2779; claim at 2358-2361

cs->sync.dep[sq].prior |= item->sync.dep[sq].prior (and the folded req->prior) also applies to sq == HK_SYNC_VDM. The claim that "packing only adds ordering that the firmware imposes on separate commands anyway" holds for CDM priors, because compute commands run in order. It doesn't hold for VDM priors: in-order execution makes later work wait for earlier work, not earlier work wait for a later command's render dependency.

Scenario: one submit of [R: long render] [C1: compute independent of R] [C2: barrier COLOR_ATTACHMENT_OUTPUT→COMPUTE, reads R's output].

  • Unpacked, C1 runs alongside R.
  • Packed, {C1, C2} waits for all of R.
  • On M5 the driver refuses exactly this inside a single CB: cdm_raise_delays_work() / hk_sync_merge_delays() at hk_sync.c:941-958, used by merge_control_streams() at hk_cmd_buffer.c:788. Packing reintroduces it across CBs.
  • Results stay correct; no wait cycle can form, because members are consecutive single-CDM-stream CBs. It's a throughput regression for render-plus-compute submits on M5 and G16G.

Fix: close the open run before a member whose VDM requirement adds kinds the packet doesn't already wait for. That requirement is its dep[HK_SYNC_VDM].prior, plus the VDM priors folded from earlier members' req. In practice, a member that adds a render wait may only start a packet. Then reword the comment.

3. No test covers the packing paths.

  • The PR adds or changes no tests. hk_pack_runs() and hk_pack_run_member() are static and never tested.
  • test-hk-execution-allowance.c still covers hk_cs_merge_cdm(), but none of the merged fields this PR adds (uses_global_atomics, calls_stream, pack_*).
  • The packtest cited in the description isn't in the tree.
  • "CTS 0 Fail" says little about this feature, because most CTS cases submit one command buffer per vkQueueSubmit, so they never form a run. Counting HK_PACK runs=N lines with N > 0 under HK_PACK_CDM_TRACE=1 over the CTS sample would show how much was actually exercised.

Suggestion: move the packet's dependency folding (the loop at 2766-2781 plus the last[] / req setup) into a helper. Test it through hk_sync_resolve_submit(), the way test-hk-sync-prior.c already does, covering at least:

  • (a) a member's tail cut makes a later CB wait for the packet;
  • (b) a member's req.prior naming an earlier member's kind sets cut to the packet;
  • (c) a VDM prior from any member reaches the packet's barrier;
  • (d) a render CB after a packet resolves to the packet's index;
  • (e) every rejection in hk_pack_run_member(): events, timestamps including a pending start stamp, immediate writes, indirect, internal launches and linked streams.

Also check packtest in next to the other aurora_* tests.

Lower severity / nits

  • hk_queue.c:2736 is guarded only by an assert. The assert is all that keeps hk_cs_merge_cdm_until() on its inline path. In a release build, a run sized wrong would take the link path, which points the packet's current into the source CB's stream. The terminate memcpy, the padding memset and the M5 FLUSH_MEMORY clear would then write into the application's command buffer. The size arithmetic holds today (remaining − requested = terminate + padding + link), so this is hardening: check it at runtime and fall back to the unpacked submit.
  • hk_queue.c:2280-2292 can turn host OOM into device loss on G16G. G16G now creates a pending entry (a vk_zalloc plus one syncobj per ioctl) for every packed submit. If that allocation fails, the submit becomes VK_ERROR_DEVICE_LOST after the ordinals were already resolved; before this PR, G16G never took that path. Allocating the pin before hk_sync_resolve_submit() would avoid it.
  • G17S defaults on, but this port hasn't run on M5. That includes commit 3's memory-scope trim. Make sure the hardware runs for hk: pack M5 command buffers that end in a barrier without memory scope #506 / hk: give M5 barriers memory scope only while a host import may alias #552 compare HK_PACK_CDM_SUBMIT=0 with =1 before relying on the default.

Checked and found correct

  • Dependency translation: member dep / req / cut folding against hk_sync_resolve_submit(), including carry, last[], exact-prior ordinals and chunked timelines. The packet's tail req.cut and last make later CBs wait for the whole packet.
  • Member boundaries:
    • G16G is native AGX3, so barriers inside a CB are already in-stream flushes, and merge_control_streams() already concatenates streams the same way. Upgrading the weak tail to the strong flush is at least that strong.
    • The M5 memory-scope trim matches what a compute→compute barrier inside one CB emits when hk_cdm_alias_scope() is false. The last member keeps the memory scope, and the packet sets VISIBLE_COMPLETION.
  • Exclusions:
    • Events: secondaries replay through vk_cmd_queue onto the primary, so they set uses_events too. On G16G uses_events is the only event guard, because the event write there is a vk_meta dispatch that counts as an application dispatch. Keep it.
    • Timestamps: always land in cs->timestamp.*, including pending top-of-pipe stamps.
    • Queries: resets and copies set query_access.
    • Also excluded: immediate writes, indirect dispatches (conditional dispatches become indirect), the RT single kick, and linked streams. The only stream-link emitters are hk_ensure_cs_has_space() and the trace-rays call.
  • Sparse: hk_pack_runs() bails on any bind.
  • Simultaneous use: source streams are only read, and are copied once per occurrence.
  • Packed BO lifetime: pinned through hk_cmd_pending_begin(extra_bo) and released only after its own syncobjs report success; failed submissions keep it until device teardown. hk_query_note_submit() and the M5 recording pins use the original submit.
  • Attachments: the kernel's MAX_ATTACHMENTS is 16, below the set's capacity of 20, so the union is never truncated.
  • Rebased sibling condition, hk_queue.c:1849: !packed_bo && hk_sibling_eligible(..., resolved, ...) is right. Sibling queues exist only on G17P, where packing is off, so the guard can't fire today. Keep it anyway: the merge doesn't carry sib_threads / sib_indirect, so a packet would otherwise pass the thread gate as internal-only work.

No commits pushed; not approving or merging.

🤖 Generated with Claude Code

Review of #488 found two ways a packet could behave worse than the
command buffers it replaces:

- The static work bound (threads x static instructions) does not bound a
  program with loops. 256 looping one-dispatch command buffers fit one
  packet and could run far past the per-command timeout that each of them
  alone stays under (on G16 the kernel's 5 s deadline renews only when a
  command's stamp moves, so it limits a command's total run time). The
  compiler now reports whether a program has a loop (has_loops, like
  uses_global_atomics), streams record it (pack_loops), and a packet holds
  at most one looping stream: it runs about as long as that stream alone
  plus statically bounded work.
- A packet waits for whatever any member waits for. For the compute
  subqueue that adds no ordering, but a later member's wait on a render
  held the earlier members back, so [render][C1][C2 reads the render]
  serialized C1 behind the render. A member now starts a new packet when
  it, or the tail of a member before it, would add a render wait or a
  synchronization kind (whose carried requirements the packet would take
  on); the last member's tail is a requirement of later command buffers
  only and no longer holds the packet itself back.

The member check, the planner and the dependency fold move to hk_pack.c,
so they can be tested without a device. Also:
- The inline-merge size condition is checked at run time; a packet that
  would not fit falls back to separate commands instead of letting the
  merge link into the source command buffer's stream.
- The command-memory pin is allocated before anything changes queue or
  synchronization state, and a failed allocation fails the submission
  with VK_ERROR_OUT_OF_HOST_MEMORY instead of losing the device.

Signed-off-by: Diljit Singh <hey@djdev.me>
hk_pack_test runs the member check, the planner and the dependency fold
against the real hk_sync recorder and resolver (suite asahi, no device):
every rejection of hk_pack_run_member() (events, timestamps including a
pending start stamp, immediate writes, indirect and internal launches,
linked streams and calls, queries, atomics, cuts); the bounds, the
one-looping-member cap and the render-wait and kind splits of the
planner; and through hk_sync_resolve_submit() that a member's tail cut or
tail prior makes later command buffers wait for the packet, that a
member's render wait reaches the packet's barrier, that a render after a
packet waits for it, and that the last member's tail holds back later
command buffers only.

aurora-packtest is the hardware test the packing was qualified with:
dependent chains, barriers, transfers and work that stays out of packets,
compared word for word with a CPU model. Its plain dispatches run a
program without loops, its heavy ones a looping program; run it with
HK_PACK_CDM_SUBMIT=0 and =1.

Signed-off-by: Diljit Singh <hey@djdev.me>
@DjDeveloperr

Copy link
Copy Markdown
Author

Re: independent review

Thanks for the review. Fixed on top of 2c46924, new head 6816224 (two commits, no history rewritten):

1. Packet run time and loops (668b3ba884b). The G16 kernel's 5 s deadline renews only when a command's stamp moves (Firmware::progress()), so it is a limit on one command's total run time, not a no-progress detector; the comment's claim was wrong, and capping is the fix. The compiler now sets agx_shader_info::has_loops when it emits any loop (main or preamble, emit_loop()), dispatch_grid() records it per stream (hk_cs::pack_loops, merged like uses_global_atomics), and a packet holds at most one looping stream (hk_pack_plan()). A packet then runs about as long as its looping member alone plus statically bounded work (the 2^35 thread-instruction bound, ~17 ms on the M4). Looping streams still pack with loop-free neighbours; two looping streams never share a packet. The cost on ML workloads (most kernels loop) will show in the M4 HK_PACK on/off row.

2. Render waits pulled forward (same commit). A member now starts a new packet when it would add a render wait or a synchronization kind: its dep[VDM].prior plus the VDM tails of the members before it must stay within what the first member waits for, and its kinds within the first member's (a new kind would pull in that kind's carried requirements). The fold no longer applies the last member's tail to the packet itself: it only holds later command buffers, as it would for the separate command. The comment now says packing adds no ordering on the compute subqueue and splits instead of adding a render wait.

3. Tests (681622448cc). The member check, planner and dependency fold moved to hk_pack.c; hk_pack_test (suite asahi, 63 checks) runs them against the real hk_sync recorder and resolver:

  • every hk_pack_run_member() rejection: events, a pending start stamp (now rejected explicitly), start and end stamps, immediate writes, indirect and internal launches, uncounted launches, linked streams and calls, queries, query resets, timestamp query commands, native ray query, AS builds, render streams, a missing tail barrier, cuts, a wrong terminate, two streams, atomics with and without permission;
  • planner: bounds, the one-looping-member cap, the render-wait split (own wait and earlier member's tail), the kind split;
  • through hk_sync_resolve_submit(): (a) a member's tail cut and (b) a tail prior naming an earlier member make a later CB wait for the packet, (c) a member's render wait reaches the packet's render barrier, (d) a render after the packet waits for the packet's index, and the last member's tail holds later CBs only.

packtest is checked in as aurora-packtest (embedded SPIR-V): plain dispatches use a loop-free program so packets still form, heavy ones a looping program; a new loops case chains them.

Lower severity:

Local checks at 6816224 (host aarch64, clang 23.1.1, release, -j2, 2 GiB cap): libvulkan_asahi.so and aurora-packtest build; meson test --suite asahi 139 Ok, 0 Fail (138 before plus hk_pack); no compiler warning in changed lines. The M4 hardware A/B (aurora-packtest packing off/on, Geekbench 6 Vulkan HK_PACK_CDM_SUBMIT=0/1) runs on this head in the next J773g mini slot.

@RyanTheTide

Copy link
Copy Markdown

Thanks for the rebase and for the fixes after the independent review. Allowing one looping member per packet, the render-wait split and hk_pack_test cover the problems it raised. Keeping HK_PACK_* as the A/B fallback until the hardware rows are in, then dropping them all in one follow-up, works for us.

The default at hk_queue.c:2428-2429 has not changed yet. G17C and G17G report DRM_ASAHI_FEATURE_COMPUTE_VISIBLE_COMPLETION like G17S, and 2414-2417 already checks for it. Could the M5 default follow that feature, with a comment wherever a part really differs? If useful, we can run aurora-packtest with HK_PACK_CDM_SUBMIT=0/1 on our M4 Pro and A18 Pro.

@DjDeveloperr

Copy link
Copy Markdown
Author

Independent re-review (separate agent, not the author's lane)

Re-reviewed head 681622448cc5 (fix commits 668b3ba884b and 681622448cc) against the first review and the author's reply.

How I checked it:

  • I read both commits and the code they rely on: hk_sync.c, hk_CmdPipelineBarrier2(), hk_EndCommandBuffer(), the compiler, and the pending-pin code.
  • I checked the kernel claim in linux-m4gpupipe-20261119.
  • I built hk_pack.c, hk_sync.c and tests/test-hk-pack.c from a clean checkout of the head, using the meson release flags. All 63 checks pass.
  • I wrote a randomized harness that compares packed and unpacked resolution, using the real hk_sync resolver and the real hk_pack.c. It is described under "Checked and correct" and is not committed.

Status of the first review's findings

# Finding Status
1 Packet can outrun the per-command timeout (loops) Resolved for loops. The kernel claim and the compiler flag both check out (details below). A smaller residual remains for memory-bound loop-free work: finding 3.
2 Render waits pulled forward onto earlier members Resolved. No wait is dropped, and no packet waits for more render work than its first member. But the new split fires on barriers that have no render wait at all: finding 1.
3 No tests Partly resolved. Gaps remain: finding 2.
— Assert-only size guard Resolved. It is now a runtime check with an unpacked fallback.
— Pin OOM reported as device lost Resolved.

Findings (most severe first)

1. Real bug, blocking: the render-wait split treats compute-only barriers as render waits. A command buffer recorded as {dispatch, barrier} never packs, and that includes this PR's own 242-CB chain.
hk_pack.c:88, hk_pack.c:90, hk_pack.c:142; root cause at hk_sync.c:385

Why it happens:

  • hk_sync_src_dep() records the source kinds as a prior on both subqueues. So a COMPUTE→COMPUTE barrier recorded after the dispatch sets req[COMPUTE][VDM].prior = COMPUTE.
  • hk_pack_describe() ORs that bit into render_tail.
  • The member's own render_waits stays 0. hk_CmdPipelineBarrier2() emits its flush through cmd->current_cs.cs before hk_sync_barrier() (hk_cmd_buffer.c:1157), and nothing in vkEndCommandBuffer re-validates the stream.
  • hk_pack_plan() therefore refuses the next member.
  • The resolver only turns a prior of kind k into a wait if the render subqueue has run work of kind k: resolve_prior(), hk_sync.c:1589-1599, checks ss->last / queue->sync.ever. A compute-only application never runs COMPUTE-kind work there, so the split guards against a wait that never resolves.

Measured: I ran the real hk_sync_barrier() and hk_pack_plan() on 8 one-dispatch CBs, G16G, with no render work anywhere:

8 x {dispatch}                        -> 1 packet [0..7]
8 x {barrier, dispatch}               -> 1 packet [0..7]
8 x {dispatch, barrier}               -> 0 packets   (render_waits=0, render_tail=COMPUTE)
8 x {barrier, dispatch, barrier}      -> 1 packet [0..7]
{dispatch} + 7 x {barrier, dispatch}  -> 1 packet [1..7]; the first CB is left out

Impact:

  • aurora_packtest's chain242, chain300, loops and chain-all cases all record one dispatch then one barrier per CB (tests/aurora_packtest.c:463-495). At this head they form no packets.
  • chain242 is the PR's headline measurement (242 one-dispatch CBs, 3.0 ms → 0.31 ms).
  • The planned M4 HK_PACK_CDM_SUBMIT=0/1 A/B on this head would compare two unpacked runs for those cases.
  • packtest would still pass, because it only compares results.
  • Before 668b3ba884b, all of these packed.

Fix: mask render_waits / render_tail with the kinds the render subqueue actually holds before the run, the same test resolve_prior() applies.

  • Pass queue->sync.ever[HK_SYNC_VDM] OR the kinds of VDM streams in command_buffers[0..run.first) into describe/plan.
  • More precise alternative: no render stream lies between members, so let a member join when the latest render position its kinds resolve to is no later than the first member's.
  • Either change is safe for correctness. With the render check removed entirely, my harness reports over-waits only, never a dropped wait (negative control below).
  • Add a planner test along these lines:
    struct hk_cmd_buffer *c[8];
    for (unsigned i = 0; i < 8; ++i) {
       c[i] = plain(1);
       hk_sync_barrier(c[i], HK_SYNC_COMPUTE, HK_SYNC_COMPUTE);
    }
    plan(c, 8, runs, &count);
    T("dispatch-then-barrier chain packs", runs_are(runs, count, 1, (unsigned[]){0}, (unsigned[]){8}));
  • Add the same test with a GFX render earlier in the submission; it should still form one packet.

2. Test gap, medium: the dependency tests cover packet shapes the planner cannot form, and nothing checks that packing actually happens.

  • test-hk-pack.c:297-325, cases (a) and (b), pack [plain + COMPUTE→COMPUTE tail, plain] by calling pack() directly. hk_pack_plan() refuses that pair (finding 1). The only planner test that records a barrier after a dispatch uses a GFX source and expects a split. None uses a compute-only source, which is why finding 1 got through.
  • Every resolver check runs with exact_prior = false. The G16 kernel advertises DRM_ASAHI_FEATURE_EXACT_PRIOR_BARRIERS (g16/params.rs:229), so on G16G the shipping path is resolve_absolute(), carry_absolute and the persisted queue->sync.tail. My harness covered that path, including a follow-up submission, and found no failures. hk_pack_test should cover it too.
  • aurora-packtest passes whether or not anything was packed. Have the unit test assert that chain242's recording pattern forms packets, or have the hardware run check HK_PACK runs>0 in the trace.
  • No test runs hk_pack_runs() itself: the byte copy, the boundary flush upgrade, resource dedup and the runtime size fallback. Nor does any test cover the untouched-OOM path. Both are acceptable without a device stub, but worth saying in the PR.

3. Residual risk, low-medium, non-blocking: the static work bound assumes the ALU rate.
hk_pack.h:22-29, hk_queue.c:2383-2386

  • 2^35 thread-instructions is about 18 ms on the M4 at issue rate. A loop-free member whose instructions are mostly cache-missing loads runs one to two orders of magnitude slower per instruction.
  • At 16 B per load, 2^35 instructions can move about 550 GB. That is about 4.6 s at about 120 GB/s, before counting the one looping member a packet may hold. Random gathers that pull a full line per 4 B access are worse.
  • So a packet of one looping member plus a few multi-second memory-bound gathers can pass the 5 s deadline even though each member stays under it.
  • It needs very large dispatches, so it is rare. Possible fixes:
    • count memory instructions separately (the compiler can report them next to has_loops); or
    • lower HK_PACK_MAX_STATIC_WORK to a value the trace shows the measured graphs still fit under; or
    • reword "runs about as long as its longest member could alone" (hk_queue.c:2385, hk_pack.h:24), which holds only at ALU rate.

Nits

  • hk_queue.c:2751-2754: after an untouched OOM failure, the else if (has_compute) branch still calls hk_compute_kick_note(). It's harmless: G17C only, and it notes the unchanged timeline point. Writing else if (result == VK_SUCCESS && has_compute) would say what's meant.
  • FYI, two folds in hk_pack_fold_member() are redundant under the planner's constraints: OR-ing members' own dep priors, and the non-last tail fold. Removing either one changes nothing in hk_pack_test or in my harness. They're fine as defence in depth, but no test pins them.

Checked and correct

Kernel deadline claim. It holds.

  • g16/poll.rs:821-844: Firmware::progress() renews the deadline only when the oldest flight's firmware stamps move.
  • engine.rs:1349-1350: publication renews it unless the command is behind others.
  • poll.rs:753: completing a prefix renews it.
  • asahi_core.rs:260-267: both deadlines default to 5000 ms.
  • So a single command gets about 5 s in total from when it starts.

Loop flag. Set for every loop form, with no stale cache entries.

  • emit_loop() (agx_compile.c:9400) is the only place the backend creates a back edge. agx_lower_pseudo and agx3_pack_binary only consume existing loop_header blocks.
  • The preamble and main share out (ctx->out = out).
  • Both recompile paths re-run emit_loop() after restoring out: the RA budget cut (*out = initial_out) and the AGX3C float-constant retry.
  • General functions are UNREACHABLE, and every hk shader goes through agx_compile_shader_nir().
  • Fully unrolled loops become straight-line code that stats.instrs counts. agx_calc_stats() always runs, not only for shader-db.
  • Partially unrolled loops, and loops created by lowering (non-uniform access, subgroups, software ray query), are still nir_loops and get flagged.
  • agx_shader_info is cached as a raw struct, but the disk cache and shaderBinaryUUID key on the build sha, so no entry written before this change is read.

No dropped waits: randomized harness.

  • 80,000 random submissions: 40,000 seeds, each through both the legacy and the EXACT_PRIOR resolver.
  • Each submission has 2 to 7 command buffers. Each is a render or a compute CB; compute kinds are COMPUTE, XFER or both; barriers before and after each stream are drawn from GFX, GFX_FRAG, COMPUTE, XFER and ALL.
  • The real hk_pack_plan() forms the runs. Packets are built the way hk_pack_runs() builds them. Both versions resolve through hk_sync_resolve_submit(), and in exact mode a follow-up submission covers the persisted tail.
  • Result: 23,662 packets and 661,238 checks. No stream waits for less than it did unpacked (a wait on a member counts as a wait on its packet). No packet waits for more render work than its first member.
  • Negative controls confirm the harness catches breakage:
    • disabling the render split → 5,486 over-waits flagged;
    • dropping the packet cut for member tails → 11,475 dropped compute waits flagged.

OOM path. Queue and synchronization state stay consistent.

  • The pin (hk_queue.c:1636-1648, its allocation and error return) now comes before hk_queue_ensure_drm(), the timeline advance and resolution.
  • hk_cmd_pending_begin() reaps only retired entries before it allocates.
  • Every later error path calls hk_cmd_pending_end(), which recycles a pin that no ioctl used.
  • Query notes, frame anatomy and resolution all come after the pin.
  • In immediate mode, vk_queue_submit_final() returns a non-lost error with the semaphores untouched.

Runtime size guard (hk_queue.c:2639-2647).

  • It mirrors the inline_b condition of hk_cs_merge_cdm_until(). The packet is never linked, and members never are.
  • On failure the fallback frees the partly built packets. Sources are only read.

Pending start stamp. Now rejected explicitly (hk_pack.c:21).

Verdict

Not ready to merge. Blocking: finding 1. As it stands, packing is effectively off for the {dispatch, barrier} pattern this PR was measured on, so the planned M4 A/B would qualify a nearly disabled feature. Fix it before that hardware run, and add the planner test from finding 2. Findings 2 (rest), 3 and the nits are non-blocking.

No commits pushed; not approving or merging.

🤖 Generated with Claude Code

hk_sync_src_dep() records a barrier's source kinds against both
subqueues, so a COMPUTE -> COMPUTE barrier after a dispatch also names
COMPUTE work on the render subqueue. The resolver turns that into a wait
only once the render subqueue ran COMPUTE-kind work (resolve_prior()), but
the planner took it for an added render wait and split: command buffers
recorded as {dispatch, barrier}, the pattern packing was measured on
(242 such command buffers, 3.0 -> 0.31 ms), formed no packets.

The planner now masks a member's render waits and tail with the kinds the
render subqueue ran before it: the queue's earlier submissions
(hk_sync_queue::ever) and the render streams of the command buffers before
it in the submission. The kind rule applies only once render work ran:
before that no carried requirement can resolve to a render wait.

The planner also keeps the tail per kind, as hk_pack_fold_member() folds
it: a packet waits for a member's tail only for the kinds the packet has,
so a GFX -> GFX barrier after a dispatch no longer splits. Neither change
alters which waits are kept, only how much is packed.

Also: no compute-kick note after a submission that failed untouched, and
the comments say the static work bound limits a packet at ALU rate.

Signed-off-by: Diljit Singh <hey@djdev.me>
- The planner packs a {dispatch, COMPUTE -> COMPUTE barrier} chain into
  one packet on G16G and on G17S (M5), also after a render or on a queue
  that rendered; COMPUTE work on the render subqueue makes that barrier a
  render wait again. A render tail splits a packet only for kinds the
  packet has.
- The dependency checks build their packets through the planner, and run
  with both the legacy and the exact-prior resolver (the G16 kernel takes
  exact priors), including a later submission that waits through the
  queue's persisted tail.
- The two folds the planner's rules make redundant are pinned on packets
  built directly.
- 2000 random submissions per resolver mode are resolved packed and
  unpacked: no wait may be dropped, and no packet may wait for more render
  work than its first member (from the second review of #488).

aurora-packtest reads back the driver's packing trace and reports the
command buffers packed per case. With HK_PACK_CDM_SUBMIT=1 a case that
must pack fails when nothing was packed, and with =0 any packing fails.

Signed-off-by: Diljit Singh <hey@djdev.me>
@DjDeveloperr

Copy link
Copy Markdown
Author

Thanks for the re-review and the harness. Fixed in two new commits on top of 6816224, new head 532fbf4 (history before it unchanged):

1. Compute-only barriers split packets (blocker) (962495aa671). Fixed with the first option you suggested, so the planner applies the same test as resolve_prior(). hk_pack_runs() passes queue->sync.ever[HK_SYNC_VDM] plus the kinds of the render streams of the command buffers before each member (hk_pack_render_kinds()) to hk_pack_describe(). That function masks the member's render_waits and render_tail with those kinds. The "no new kind" rule now applies only once render work ran (hk_pack_member::render_ran): before that, a carried requirement cannot resolve to a render wait.

The M5 lane's re-review added one more change, now included. The planner keeps the render tail per kind, as hk_pack_fold_member() folds it: a packet waits for a member's tail only for the kinds the packet has. Before this, a GFX -> GFX or GFX -> XFER barrier after a compute dispatch also split.

Neither change affects which waits are kept, only how much is packed. The random harness (below) runs on this head.

2. Tests (532fbf4db53, hk_pack_test now has 111 checks):

  • Planner regression: 8× and 3× {dispatch, COMPUTE -> COMPUTE barrier} form one packet on G16G and on G17S. The member check is built per chip: the ordering barrier on G16G, the strong memory-scoped flush with visible completion on M5. They also form one packet after a render in the same submission and on a queue that ran GFX. COMPUTE-kind work on the render subqueue (ever = COMPUTE) makes the barrier a real render wait again and splits.
  • Per-kind tail: a GFX -> GFX tail doesn't split compute members. A GFX -> XFER tail splits transfer members but not compute members.
  • Dependency checks (a)–(e) now build every packet through the planner (pack_planned() asserts one run covering all members). They run in both legacy and exact-prior mode. In exact mode a follow-up submission waits through the queue's persisted tail (carry_absolute/queue->sync.tail), which is the G16 path.
  • Redundant folds: both folds are pinned by test_folds() on directly built packets (own dep prior; non-last tail). They stay as defence in depth.
  • Random harness: your harness, trimmed, is checked in as test_random(). It runs 2000 seeds × {legacy, exact}, with every seed resolved packed and unpacked. It asserts no dropped wait, no packet waiting for more render work than its first member, and >500 packets formed. At this head: 3174 packets, 29940 checks, 0 failures.
  • aurora-packtest turns on the driver's packing trace (HK_PACK_CDM_TRACE=1), reads the driver's HK_PACK runs/members lines back and prints PACKED <case> packed_cbs=N per case. With HK_PACK_CDM_SUBMIT=1, a must-pack case (chain242, chain300, loops, chain-all, multi, multi-tail, disjoint) that packs nothing FAILs. With =0, any packing FAILs. The M4 on/off row reports these counts.
  • Not covered by a unit test: hk_pack_runs() itself (byte copy, boundary flush upgrade, resource dedup, runtime size fallback) and the untouched-OOM path. Both need a device stub; the hardware row covers the first through packtest's result checks.

3. Static work bound vs memory-bound code. I took the "explain" option rather than a cap. hk_pack.h says the 2^35 bound is ~18 ms at the M4's ALU issue rate and limits memory-bound programs only roughly. The hk_queue.c comment says "about as long as its longest member could alone" holds at ALU rate. The case you describe needs a looping member plus several multi-second memory-bound loop-free gathers in one submission, which is rare, and it is not fixed here. If the M4 trace shows the measured graphs fit well under it, I'll lower HK_PACK_MAX_STATIC_WORK, or count memory instructions next to has_loops, in a follow-up.

Nits: hk_compute_kick_note() now runs only on VK_SUCCESS. The folds are kept and tested (above).

Local checks at 532fbf4 (host aarch64, clang 23.1.1, release, -j2, 2 GiB cap):

  • libvulkan_asahi.so and aurora-packtest build.
  • meson test --suite asahi: 139 Ok, 0 Fail.
  • hk_pack_test: 111 checks, 0 failures.
  • No compiler warning in the changed files.

The M4 hardware A/B runs on this head in the next J773g slot: aurora-packtest packing off/on with packed counts per case, and Geekbench 6 Vulkan with HK_PACK_CDM_SUBMIT=0/1.

@DjDeveloperr

Copy link
Copy Markdown
Author

Independent re-review (separate agent, not the author's lane)

I re-reviewed the two new commits at head 532fbf4db534: 962495aa671 (the planner fix) and 532fbf4db53 (the tests). I checked them against the re-review, the M5 lane's rebase notes and the author's reply.

Verdict: ready to merge.

  • The blocker is fixed: {dispatch, COMPUTE -> COMPUTE barrier} chains now pack.
  • The new tests catch the old planner and every non-equivalent under-cautious mutant I tried.
  • The findings below are all perf-only or test-robustness issues. None of them drops a wait.

How I checked it:

  • Build and run. I built hk_pack_test from a disposable worktree at the head, with the meson release flags (clang, -j2, 2 GiB MemoryMax scope).
    • hk_pack_test: 111 checks, 0 failures. Random harness: 3174 packets, 29940 checks, 0 failures.
    • Same results under ASan+UBSan.
    • hk_queue.c and hk_pack.c (libhk flags) and aurora_packtest.c (-Wextra) compile with no warnings.
  • Mutants. I applied 21 mutants to hk_pack.c and hk_queue.c. Each one was rebuilt, run and reverted. The table is under "3." below.
  • History harness (local only). I extended the random harness to plan against queue history:
    • 4000 seeds × {legacy, exact}.
    • Each seed runs 1–2 earlier submissions with GFX-, XFER- and COMPUTE-kind renders.
    • It then plans a submission with ever[VDM], packs it, and compares it with the same submission unpacked.
    • At this head: 5100 packets, 36056 checks, 0 failures.
    • It is not weak: with plan_on() changed to ignore ever, it reports 1633 failures.
  • Not done: nothing was run on hardware.

1. Is the planner's test the same as the resolver's?

Yes, with one exception (finding 1).

  • Legacy mode. resolve_prior() (hk_sync.c:1589) waits for kind k in two cases:

    • an earlier command buffer of this submission has a render of kind k (ss->last[k][VDM] > 0); or
    • ever[VDM] has k.

    The planner masks with ever[VDM] plus the kinds of the render streams in the command buffers before the member (hk_queue.c:2562-2568). This is the same set as long as cmd->sync.last[k][VDM] != NULL exactly when one of the command buffer's render streams has kind k. note_last() keeps that true.

  • Exact mode. resolve_absolute() (hk_sync.c:1612) reads last_absolute[k][VDM] instead of ever. The same hk_sync_resolve_submit() sets both, from the same streams. So ever[VDM] ⊆ {k : last_absolute[k][VDM] ≠ 0}, and the two are equal except in finding 1.

  • Queue-saved tails (queue->sync.tail, exact mode). A non-zero tail[k][VDM] needs a render cut or a last_absolute hit, so it implies ever[VDM] ≠ 0.

    • So render_ran == false really means that no carried requirement can become a render wait, in both modes.
    • Letting a member add kinds before any render ran is therefore sound.
  • Same mask across a run. Members are command buffers with a single compute stream, so the render kinds don't change within a run.

    • Masking each member at its own position is the same as masking at the packet's position.
    • The packet's position is where the fold's union of priors gets resolved.
  • No wait is dropped. The planner only decides the grouping. The fold still takes the union of every member's own priors and the tails of the members before it. test_folds() and mutants M3a–d cover this.

  • No packet waits for more than its first member. Both the history harness and the in-tree random harness check this.

2. Does the per-kind tail match the fold?

Exactly.

  • hk_pack_fold_member() (hk_pack.c:248) adds a non-last member's req[k] for each kind bit k of the packet's final kind.
  • At each join, the planner checks tails[k] for the kinds kinds | m->kinds.
  • Take any earlier member i and any packet kind k. The planner checks member i's tail for k when the later of these two events happens: kind k joins, or member i+1 joins.
  • Both sides use kind bits, not full_kinds().

Two pieces of the planner never change the result. Their mutants (M2e, M2i) are equivalent:

  • the | m->kinds in the tail check;
  • the kinds |= m->kinds update.

Both matter only while render_ran is false, and then every tail is already masked to 0.

3. Do the tests catch the old and the under-cautious versions?

Each mutant was rebuilt, run and reverted. "fail" counts failing checks; "random" counts failures in the random harness.

Mutant Result
M1: the 681622448cc5 planner (no mask, render_ran always true, tail ORed over all kinds) 17 fail, including the 8 chain checks on G16G and G17S and the per-kind tail checks
M1a: no mask / M1b: tail over all kinds / M1c: tail unmasked / M1d: own waits unmasked 15 / 2 / 11 / 3 fail
M2a: render kinds forced to 0 7 fail, random 502
M2b: hk_pack_render_kinds() returns 0 caught only by the random harness (502)
M2c: drop the new-kind rule 2 fail, random 32
M2d: drop the tail check 5 fail, random 130
M2g: drop the member's own waits 2 fail, random 122
M2h: tails not accumulated 2 fail, random 32
M2j: tail checked only for m->kinds random 12
M2f: count compute streams as render kinds 12 fail, random 114
M3a–d: fold mutants (no tail fold, no own prior, no packet cut, no packet tail) 2–10 fail each, plus random
M2e: kinds | m->kinds → kinds; M2i: drop kinds |= m->kinds survive: equivalent (see 2.)
M4a: render_kinds in hk_queue.c starts at 0; M4b: no per-command-buffer accumulation survive: 111/0 (finding 2)

Findings (most severe first)

1. LOW, perf only. The planner and the resolver disagree after hk_sync_forget_stream() moves last[k][VDM] to a stream of another kind.
Code: hk_pack.c:100-109, hk_queue.c:2562. Cause: hk_sync.c:1119, called from hk_cmd_buffer.c:4670.

  • Cause: forgetting a render points cmd->sync.last[k][VDM] at the previous render, whatever that render's kind. This happens, for example, when a clear-only pass is replaced by compute clears.
    • The resolver then counts kind k as having run: ss.last[k][VDM] > 0, and in exact mode last_absolute in later submissions.
    • The planner looks at stream kinds and does not.
  • Reproduced with the real recorder, resolver and planner (test_forget_divergence() in my local extension):
    • CB0 = {an XFER-kind render, then a GFX render that is forgotten}; CB1 = a plain dispatch; CB2 = {a GFX → COMPUTE barrier, a dispatch}.
    • The planner packs CB1 and CB2.
    • The packet waits for the XFER render (legacy barrier 1, exact ordinal 1). CB1 alone would wait for no render.
    • No wait is dropped.
  • How likely: rare. It needs, in one command buffer, a transfer render followed by a clear-only pass that gets replaced, and then a member run.
  • Fix: build the set from the same field the resolver reads.
    uint8_t
    hk_pack_render_kinds(const struct hk_cmd_buffer *cmd)
    {
       uint8_t kinds = 0;
       for (unsigned k = 0; k < HK_SYNC_NUM_KINDS; ++k) {
          if (cmd->sync.last[k][HK_SYNC_VDM])
             kinds |= BITFIELD_BIT(k);
       }
       return kinds;
    }
    • Start from ever[VDM] on legacy queues, and from {k : last_absolute[k][VDM] ≠ 0} on exact_prior queues.
    • Better still, put one helper in hk_sync.c next to resolve_prior(), so the two can't drift apart.

2. LOW, test gap. The test re-implements hk_pack_runs()'s describe loop instead of calling it, so the production loop is untested.
Code: hk_queue.c:2561-2570 against tests/test-hk-pack.c:214-223.

  • Scenario: M4a (start from 0 instead of ever[VDM]) and M4b (drop the per-command-buffer accumulation) both pass 111/111.
  • Both make the planner under-cautious, so a member could add a render wait and hold back the earlier members.
  • aurora-packtest would not catch either one on hardware: they pack more, and the must-pack check only asks for at least one packed command buffer.
  • Fix: move the loop into hk_pack.c and call it from both hk_pack_runs() and plan_on(). For example: unsigned hk_pack_describe_submit(members, cmds, n, ever_vdm, dev, terminate, atomics), returning eligible.

3. LOW, test gap. The random harness never plans against queue history, and its renders are all GFX-kind.
Code: tests/test-hk-pack.c:645 (plan_on(P, n, 0, …)), build() at :593.

  • Scenario: with plan_on() changed to ignore ever, the in-tree random harness still reports 0 failures. The directed tests do catch it (6 fail).

  • Its "next submission" is resolved but never planned or packed, so the cross-submit half of the rule is not randomized.

  • wait_of() maps a legacy barrier = 0 ("earlier ioctls") to 0, the same value as "no wait". That is fine while the queue is fresh, but it would hide waits once the queue has history.

  • Fix: add the history variant described above:

    • 1–2 unpacked history submissions with render kinds {GFX, XFER, COMPUTE};
    • plan with q->sync.ever[VDM];
    • compare legacy waits as b + 1.

    I can paste it if that helps.

4. LOW, aurora-packtest. The must-pack verdict for chain-all depends on case order.
Code: tests/aurora_packtest.c:851.

  • Scenario: HK_PACK_CDM_SUBMIT=1 aurora-packtest N split-render chain-all.
    • split-render leaves a render on the queue. On native-EOT parts, a clear-only pass stays a render.
    • Each chain-all command buffer ends with an ALL → ALL barrier. Its tail names GFX on the render subqueue, which is a real render wait for every member after the first.
    • The planner therefore forms 0 packets, which is correct, and the case FAILs as "must pack".
  • The default list runs chain-all before the split cases, so the planned M4 row is not affected. This is from reading the code; I did not run it on hardware.
  • Fix: remove chain-all from must_pack(), or check it only while no render has run on the queue.

5. NIT, aurora-packtest. When a case dies, the trace capture loses the driver's stderr.
Code: tests/aurora_packtest.c:798-845, die() at :41-50.

  • fd 2 points at an unlinked tmpfile, which is echoed only after each case.
    • A CHECK() failure (for example VK_ERROR_DEVICE_LOST) calls die(), which calls exit(2).
    • The driver's messages for exactly the failing case are then lost.
  • Every must-pack check also passes silently in either of these cases:
    • tmpfile() or dup() fails;
    • the caller exported HK_PACK_CDM_TRACE=0, since setenv(…, 0) keeps an existing value.
  • Fix:
    • flush the rest of the trace to real_stderr from an atexit() handler;
    • print WARN trace unavailable when there is no trace, or FAIL if HK_PACK_CDM_SUBMIT=1;
    • use setenv(…, 1).

6. NIT, comment. This was in the M5 lane's notes and is still open.

  • hk_queue.c:2365 says the copy keeps M5's memory-scoped flush between members.
  • But hk_queue.c:2662-2669 drops the memory scope between members when no alias exists.

Optional follow-up (not blocking)

Once a queue has rendered, {dispatch, ALL -> ALL} chains on it never pack again (see finding 4).

  • With exact priors, those render waits name ordinals at or below the queue's completed point, so they cost nothing at run time.
  • If the planner ignored render waits at or below completed, it could pack these chains.

Out of scope

These commits don't touch RyanTheTide's question about the M5 default (now hk_queue.c:2430).

The disposable worktree has been removed. Nothing was pushed, approved or merged.

🤖 Generated with Claude Code

Packing was on by chip: G17S and G16G, other M5 parts opting in.
hk_pack_boundary() now decides it from how a member's end publishes its
writes to the members after it:

- M5 streams, where the kernel takes their own completion flush
  (DRM_ASAHI_FEATURE_COMPUTE_VISIBLE_COMPLETION): every stream then ends
  with the strong memory-scoped flush a boundary keeps. G17G and G17C
  pack as G17S does.
- HAL200 streams whose strong flush has no memory-scoped form (G16): the
  kernel publishes a command's writes when it completes, and a boundary
  becomes the strong flush an in-stream compute dependency gets. G17P
  stays out: its strong flush also has a memory-scoped form, which
  memory reached through a second address needs and which no such
  boundary asks for (hk_cdm_memory_scope_form(), now shared with
  hk_cdm_flush_has_memory_scope()). G15 and AGX2 streams have no
  boundary.

Packing is on wherever a boundary is defined; HK_PACK_CDM_SUBMIT stays
for A/B rows. The member check and the copy take the boundary from the
same function. Global atomics stay packed only on G16G: the G16
kernel's whole-device ordering is not reported by any uAPI feature yet.

The planner's render kinds now come from the fields the resolver reads,
through hk_sync_queue_kinds() (hk_sync_queue::ever, or last_absolute on
exact-prior queues) and hk_sync_cmd_kinds() (cmd->sync.last). Reading
stream kinds disagreed after hk_sync_forget_stream() left a latest render
of a kind on a render of another kind. hk_pack_describe_submit() is the
one describe loop for hk_pack_runs() and the tests.

The planner and the fold now apply a member's tail to each destination
class of the packet's stream (hk_sync_full_kinds()), as the resolver
applies carried requirements: a fragment-class tail of a member of
every kind was neither checked nor folded.

Signed-off-by: Diljit Singh <hey@djdev.me>
- hk_pack_boundary(): G17G, G17S and G17C pack with visible completion
  and not without; G16G and G16S take the HAL200 boundary; G17P, G15S
  and G14G have none. A G17S stream is a member only with the feature.
- The tests plan through hk_pack_describe_submit(), the loop
  hk_pack_runs() runs.
- A render forgotten after a transfer render leaves a graphics render
  wait that the planner now sees, as the resolver does (legacy and
  exact).
- A fragment-class tail of a member of every kind splits the plan and
  is folded.
- The random harness also draws members of every kind (HK_SYNC_ALL), and
  a history harness (from the third review of #488) runs 1-2 earlier
  submissions with graphics, transfer and compute renders before
  planning a submission against the queue's state, in legacy and exact
  mode, comparing it packed and unpacked.

aurora-packtest passes the driver's stderr through as it is written,
through a pipe and a thread that counts packed command buffers, so a
case that dies keeps its driver messages. It forces HK_PACK_CDM_TRACE=1,
fails a must-pack check when no trace is available, and checks chain-all
(an ALL -> ALL barrier in each command buffer) as must-pack only before
the queue ran a render.

Signed-off-by: Diljit Singh <hey@djdev.me>
@DjDeveloperr DjDeveloperr changed the title hk: pack runs of plain compute command buffers into one command (on by default on G16G) hk: pack runs of plain compute command buffers into one command Nov 19, 2026
@DjDeveloperr

Copy link
Copy Markdown
Author

New head ee3de2b: two commits on top of 532fbf4 (a3904592907 driver, ee3de2b5ebe tests); history before it unchanged.

@RyanTheTide: the default now follows a property, not a chip list. hk_pack_boundary() (hk_pack.c) decides from how a member's end publishes its writes to the next member:

  • M5: packs wherever the kernel reports DRM_ASAHI_FEATURE_COMPUTE_VISIBLE_COMPLETION. G17G and G17C are on by default, like G17S. The kernel advertises the feature only with the M5 command format and its completion inputs, so the semantics are the same on every GPU that reports it.
  • HAL200 (G16): packs where the strong flush has no memory-scoped form. The kernel publishes a command's writes at its end, and a boundary gets the strong flush an in-stream compute dependency gets. That covers G16G and would cover G16S/G16C on a kernel that runs them.
  • Chip checks with a real hardware difference, both commented:
    • G17P stays out: its strong flush also has a memory-scoped form, which aliased memory needs and this boundary doesn't ask for. hk_cdm_memory_scope_form() is now shared with the flush code.
    • G15 publishes in-stream dependencies with the wide barrier, so it stays out too.
  • One switch is still keyed to the chip: packing global atomics on G16G. That rests on the G16 kernel running one command of the device at a time, a kernel property no uAPI feature reports yet. The comment says so; once the kernel reports it, the switch can follow the feature.
  • The member check and the copy now take the boundary from the same function.
  • HK_PACK_* stay as A/B switches until the rows are in; dropping them in one follow-up still works for us.
  • Your offer to run rows on M4 Pro and A18 Pro would help. A18 Pro (G17P) has no boundary, so the M4 Pro row would say more. The PR body asks G17C/G17G owners to run aurora-packtest after merge.

Third review:

  1. Planner vs resolver after hk_sync_forget_stream(). Fixed. The render kinds now come from the fields the resolver reads:

    • hk_sync_queue_kinds(): ever, or last_absolute on exact-prior queues;
    • hk_sync_cmd_kinds(): cmd->sync.last.

    Both sit next to resolve_prior() in hk_sync.c. test_forget() (legacy and exact) checks your case: the planner splits, and the resolver agrees.

  2. Describe loop untested. It moved to hk_pack_describe_submit(), called by both hk_pack_runs() and the tests (plan_queue()), so M4a/M4b now hit the real loop.

  3. History. Your history harness is in as test_history(): 4000 seeds × {legacy, exact}, history renders of GFX, XFER and COMPUTE kind, planned against the queue itself, and legacy waits compared as b + 1. Result: 4872 packets, 36056 checks, 0 failures.

  4. chain-all order. It is must-pack only before the queue ran a render.

Nits:

  • aurora-packtest now passes driver stderr through live (a pipe plus a thread that counts packed command buffers), so a case that dies keeps its messages, and an atexit sync drains it.
  • HK_PACK_CDM_TRACE=1 is forced.
  • With no trace, the run prints WARN, or FAILs when HK_PACK_CDM_SUBMIT=1.
  • The comment at hk_queue.c:2365 now says what each boundary does, including M5 dropping the memory scope between members when nothing is aliased.

From the M5 lane's re-review: the planner's tail check (hk_pack_tail()) and the fold now apply a member's tail to each destination class of the packet's stream (hk_sync_full_kinds(), moved to hk_sync.h), as the resolver applies carried requirements. Before this, a fragment-class tail on a member of every kind was neither checked nor folded, so my earlier claim that no wait changed was wrong for that case. test_full_classes() covers it, and the random harness now draws HK_SYNC_ALL members.

Local checks at ee3de2b (host aarch64, clang 23.1.1, release, -j2, 2 GiB cap):

  • libvulkan_asahi.so and aurora-packtest build.
  • meson test --suite asahi: 139 Ok, 0 Fail.
  • hk_pack_test: 135 checks, 0 failures; random harness 3180 packets, 30204 checks, 0 failures.
  • No new warnings.

The PR body now records hardware status: G17S by the M5 lane's s8c row, and G16G at the first head. The M4 on/off row on this head comes next.

@DjDeveloperr

Copy link
Copy Markdown
Author

Independent review of a3904592907 and ee3de2b5ebe (separate agent, not the author's lane)

Scope: the two commits on top of the last reviewed head 532fbf4db534, at head ee3de2b5ebe5. I read them against the author's reply, @RyanTheTide's request, and the kernel on integration/aurora 26294e82e9c8. For G16 I also read linux#2419 (djdev/g16-in-flight eb9d5fe625ea), which the PR body names as the matching kernel change.

Verdict: ready to merge.

  • No path packs on a GPU the selector rejects.
  • The full_kinds() change only adds waits.
  • The new tests catch the old behaviour.
  • The findings below are wording, test coverage and one hardware premise to check later. None drops a wait or packs where packing is off.

How I checked

  • Build. Disposable worktree at ee3de2b5ebe, meson release, clang 23.1.1, -j2, 2 GiB MemoryMax scope.
    • hk_pack_test, aurora-packtest and libvulkan_asahi.so build.
    • No warnings in any file this PR touches. The library build's 8 warnings are all in hk_cmd_meta.c and hk_physical_device.c.
  • Unit tests. hk_pack_test: 135 checks, 0 failures. Random harness: 3180 packets, 30204 checks, 0 failures. History harness: 4872 packets, 36056 checks, 0 failures. The same under ASan+UBSan.
  • aurora-packtest. Run only without a GPU (no Vulkan driver). The new pipe and thread pass the fatal message through and exit at once, with no hang in the atexit sync.
  • Mutants. Mutated copies of hk_pack.c and hk_sync.c, built outside the tree and linked against the test. Table under (d).
  • Not done: no GPU hardware runs.

(a) Hardware claims behind each hk_pack_boundary() branch

  • M5, VISIBLE_COMPLETION: correct.
    • In-stream dependency vs boundary:
      • An in-stream COMPUTE/XFER dependency on M5 records the strong flush with memory scope only when hk_cdm_alias_scope() (hk_cmd_buffer.c:1138-1155).
      • The boundary drops the scope under exactly that condition (hk_queue.c:2643-2659).
      • The packet's end keeps the full memory-scoped flush that DRM_ASAHI_COMPUTE_VISIBLE_COMPLETION promises.
      • hk_EndCommandBuffer() adds that flush under the same predicate as the selector (hk_cmd_buffer.c:929-936).
    • Kernel gate: g17/mod.rs:2538 = m5_commands && board.completion_inputs().
      • Only J714 (M5 Pro, G17S) sets completion_inputs (g17/fw/m5/initdata.rs:2439).
      • J716C (M5 Max), J775D (M5 Ultra) and J813 (M5, G17G) have false (lines 2470, 2511, 2550).
      • So the semantics are the same wherever the feature is reported, but no G17G or G17C board reports it today (finding 3).
  • G16, STRONG_FLUSH: right for memory reached through one address.
    • The boundary is the record an in-stream dependency gets on G16G: flags 5, not wide, no memory bit (hk_cmd_dispatch.c:612-622).
    • That is Apple's HAL200 in-stream form (agx_chip.h:518-521).
    • The G16 kernel refuses any compute flags (g16/command.rs:623), and the packet sets none.
    • The memory caveat is finding 1.
  • G17P excluded: correct.
    • G17P caches are tagged by virtual address (hk_device.h:400-405), and hk_cdm_alias_scope() can be true there.
    • A STRONG_FLUSH boundary passes memory = false, so it would lose writes that the end of the command used to publish to a second mapping.
    • To include G17P later, the boundary would need hk_cdm_alias_scope() read at submit, as on M5.
  • G15 excluded: safe, but the stated reason is not a hardware one (finding 6).
    • hk_cdm_flush() already emits the wide word on G15 (hk_cmd_dispatch.c:620).
    • A STRONG_FLUSH-style boundary there would therefore be the in-stream record too.

(b) Every chip test now goes through the selector

  • Every place that packs takes the selector:
    • hk_pack_runs_enabled() (hk_queue.c:2414-2416) returns false on NONE before reading HK_PACK_CDM_SUBMIT, so the variable cannot turn packing on there.
    • hk_pack_run_member() returns NULL on NONE (hk_pack.c:53-55) and picks the end check by boundary (line 89).
    • hk_pack_runs() takes visible_completion from it (hk_queue.c:2539-2540, 2605, 2643, 2670).
  • Remaining chip tests on the path:
    • hk_pack_atomics_allowed() for G16G: intended.
    • pin_recordings = agx_chip_g17_m5(): decides pins, not packing; the packed BO is pinned on every chip.
    • hk_pack_attachment_limit(): unchanged.
  • Mutants S1–S5 below show the selector tests catch each wrong branch.
  • hk_queue.c is not in the test binary, so its gate is checked by reading only.

(c) full_kinds() in the tail check and the fold

  • Correct. The resolver applies carried requirements for full_kinds(cs->sync.kind) (hk_sync.c:1948-1949).
  • Fold. The fold now adds a non-last member's req[k] for every class in full_kinds() of the packet's final kind, a superset of before.
    • The packet's kind is set from all members before folding (hk_queue.c:2609-2613), so a later member's classes count too.
  • Planner. It checks tails[k] for the same classes, so it only splits more.
  • New-kind rule. It stays on raw kinds, which is equivalent: full(m) ⊄ full(kinds) exactly when m ⊄ kinds.
  • No wait can be dropped. For each member j:
    • the packet resolves full(K) ⊇ full(kind_j);
    • the fold unions every member's own dependencies;
    • earlier members' tails are folded for full(K);
    • a member's own CDM tail cut is met by the boundary;
    • any other cut rejects the member.

(d) Do the tests fail on the old behaviour? (mutate, rebuild, run, revert)

Mutant Result
S1: old chip list (G17S with the feature, G16G) 3 fail: G17G/G17C with the feature, G16S
S2: M5 ignores the feature 4 fail
S3: HAL200 branch takes G17P / S4: G15 too 1 fail each
S5: hk_pack_run_member() skips the NONE check 1 fail
F1: planner tail on raw kinds 3 fail, random 2, history 56
F2: fold on raw kinds 1 fail (test_full_classes)
F3: both on raw kinds (old) 3 fail, history 5
R1: render kinds from stream kinds (old) 2 fail (test_forget legacy and exact)
R3: render kinds start at 0 / R4: no per-command-buffer accumulation / R5: hk_sync_cmd_kinds() returns 0 8 fail, history 1656 / 4 fail, random 540, history 274 / same as R4
R6: exact hk_sync_queue_kinds() returns 0 1 fail, history 665
All old behaviour together 8 fail
R2: exact hk_sync_queue_kinds() returns ever survives (finding 5)

The previous review's surviving M4a/M4b (render kinds starting at 0, no accumulation) are now caught (R3, R4).

Findings (most severe first)

1. LOW, older than these commits, needs a hardware check: G16 boundaries assume no G16G memory has a second GPU address.

  • Code: hk_queue.c:2660-2663, and the selector's HAL200 branch at hk_pack.c:42-44.
  • Premise: hk treats no G16G memory as reachable through a second address, because hk_cdm_alias_scope() is false there.
  • But a G16G device can have such memory:
    • KHR_external_memory_fd, EXT_external_memory_dma_buf and bufferDeviceAddressCaptureReplay are exposed (hk_physical_device.c:152, 256, 500);
    • hk_device_may_alias_memory() counts each of them (hk_device.c:928-954).
  • In-stream dependencies already carry this exposure. Packing adds a new case: it replaces the end of a command, which writes caches back, with a flags-5 flush.
  • The wide word, which has the memory write-back bit, is grounded on G16G (agx3_cmdstream.h:623-631, 637-648). So "no memory-scoped form" is not quite true for G16G either.
  • Scenario, if G16G caches are tagged by virtual address like G17P/M5 (unverified either way): two imports of one dma-buf, or a capture/replay buffer over the same memory.
    • CB1 writes through one address, CB2 reads through the other.
    • As separate commands, CB2 reads the new data. Packed, it can read stale data.
  • Fix, either of:
    • an M4 A/B on exactly that case;
    • until then, on STRONG_FLUSH devices with dev->may_alias_memory, either skip packing or end non-last members with the grounded G16G wide word.
  • Geekbench-style compute processes enable none of these features, so the fallback would cost little.

2. LOW (rationale): the G16G atomics exception names a property linux#2419 removes.

  • The text says "The G16 kernel runs one command of the whole device at a time, so nothing ever runs beside a packet". It appears in:
    • hk_queue.c:2443-2447, the comment this commit added to;
    • the PR body ("Global atomics: packed on G16G, whose kernel runs one command of the device at a time");
    • and, in the same terms, hk_pack.c:189-190.
  • The PR body pairs this PR with linux#2419, which changes that: "Compute commands queue behind the same VM's in-flight compute on the one firmware compute queue" (g16/engine.rs can_queue_behind(), 926-949). Two kernel queues of one VkDevice share that VM.
  • Packing atomics there is still sound if the one firmware compute queue runs its commands in order. #2419 already relies on that: "same-queue compute dependencies follow the published frontier", with CTS at 0 Fail, and packtest atom-chain/atom-multi pass packed.
  • But that is the property a future uAPI feature would have to report, not "one command at a time".
  • Fix: restate both comments and the PR body as "G16 runs every compute command of a VM in order on one firmware compute queue, and never beside another VM's". Ideally the M4 row confirms atom-multi on two VkQueues with #2419.

3. LOW (accuracy, affects the requested rows): G17G and G17C are not on by default with any current kernel.

  • Only J714 reports DRM_ASAHI_FEATURE_COMPUTE_VISIBLE_COMPLETION (see (a)). So "G17G and G17C pack as G17S does" (commit message, reply) and "packing is on by default" for G17C/G17G (PR body) describe a kernel that does not exist yet.
  • The default is unchanged on every M5 board today. That is fine under a by-property rule.
  • The PR body asks G17C/G17G owners to run HK_PACK_CDM_SUBMIT=1 aurora-packtest 3 and expect packed_cbs > 0.
    • On those machines hk_pack_runs_enabled() returns false before reading the variable, so nothing packs.
    • Every must-pack case then prints FAIL ... (must pack), which reads as a packing bug.
  • Fix:
    • PR body: say "wherever the kernel reports it (today J714 only)".
    • aurora-packtest: report UNAVAILABLE instead of FAIL when a case of two or more command buffers produced no HK_PACK trace line at all. The driver prints one (runs=0 ...) whenever packing is enabled.

4. LOW (latent): the HAL200 branch selects "HAL200 minus G17P", which also gives G17A (and G16P/G16S/G16C) the STRONG_FLUSH boundary.

  • Code: hk_pack.c:42-44, table at agx_chip.h:394-411.
  • G17A sits between G17P and M5, which both have tagged caches and the memory-scope form. It gets STRONG_FLUSH only because hk_cdm_memory_scope_form() (hk_device.h:450-457) does not list it.
  • Unreachable today: agx_chip_driver_supported() admits only G16G of these (agx_chip.c:397-447).
  • But admitting G17A would turn packing on without anyone deciding it.
  • Fix: limit the branch to the G16 parts the comment names, and add a G17A row to test_boundary().

5. LOW (test gap, performance only): nothing distinguishes the exact-mode hk_sync_queue_kinds() from ever.

  • Mutant R2 (hk_pack.c:148 using sync->ever[HK_SYNC_VDM]) passes every test.
  • The two differ only when a render was forgotten in an earlier submission on an exact queue. test_forget() forgets within the planned submission.
  • Effect of the bug: a packet that waits for more render work than its first member. No wait is dropped.
  • This test passes at head and catches R2:
static void
test_forget_history(void)
{
   struct hk_cmd_buffer *r = new_cmd();
   r->sync.kind_override = HK_SYNC_XFER;
   add(r, false);
   r->sync.kind_override = 0;
   struct hk_cs *gone = add(r, false);
   hk_sync_forget_stream(r, gone);
   list_del(&gone->node);
   struct hk_queue *q = new_queue(true);
   T("exact: earlier forget: history resolves", resolve(q, &r, 1));
   struct hk_cmd_buffer *c[2] = {plain(1), after_render()};
   struct hk_pack_run runs[4];
   unsigned count;
   plan_queue(q, c, 2, false, runs, &count);
   T("exact: a render forgotten in an earlier submission splits", count == 0);
   T("exact: earlier forget: resolves", resolve(q, c, 2));
   T("exact: earlier forget: the resolver agrees",
     wait_of(first_cs(c[0]), HK_SYNC_VDM, 0, true) == 0 &&
        wait_of(first_cs(c[1]), HK_SYNC_VDM, 0, true) != 0);
}

6. NIT (wording): the G15 reason at hk_pack.c:27-29 and the test name "G15S: wide visibility, no boundary" (test-hk-pack.c:483).

  • As shown in (a), the wide word is what the boundary would emit on G15.
  • The real reason is that G15 packing (kernel, watchdog and attachment limits) has not been qualified.
  • Since this is the "chip check with a stated hardware reason" the request asked for, say that instead.

7. NIT: aurora_packtest.c:970 return bad + on; fails every case when no trace is available with HK_PACK_CDM_SUBMIT=1, not only must-pack cases as the commit message says.

  • Fix: bad + (on && must), and keep WARN for the rest.

8. NIT: on G16G, an unparsable HK_PACK_ATOMICS now means off (was on) (hk_queue.c:2459).

  • This matches the M5 path and is more conservative. Mention it in the commit message, or keep the old default.

🤖 Generated with Claude Code

@DjDeveloperr

Copy link
Copy Markdown
Author

M4 hardware row on ee3de2b: packing off vs on

Setup. J773g Mac mini (M4, G16G B0) running kernel 7.3.0-ARCH-m4drv8 with GPU firmware Apple GFX 25G83. The aurora.ko is p5d (sha 2f97353a), the linux#2419 module the first-head numbers used. The Mesa driver is built from this head (libvulkan_asahi.so sha 4ea2f10a) and installed as a private ICD. Packing is switched with HK_PACK_CDM_SUBMIT=0/1; the run was 2026-11-19 19:03 EST.

aurora-packtest at this head

  • Each case runs as its own process for 3 iterations.
  • Every result is compared word for word, guard words included, with a CPU model.
  • packed CBs is the driver trace's count of command buffers packed over the 3 iterations.

Result: 22/22 PASS with packing off and 22/22 PASS with packing on.

  • Packing on: all seven must-pack cases packed, and no line FAILed.
  • Packing off: every case reports packed_cbs=0.
  • Atomic streams kept separate (HK_PACK_ATOMICS=0): atom-chain and atom-multi PASS.
case CBs off best µs on best µs packed CBs (on) off / on
chain242 242 23577 930 726 PASS/PASS
chain300 300 28869 1899 900 PASS/PASS
loops 24 7119 6920 72 PASS/PASS
chain-all 64 6468 479 192 PASS/PASS
multi 40 5039 1358 120 PASS/PASS
multi-tail 48 4932 663 144 PASS/PASS
disjoint 64 6546 775 192 PASS/PASS
xfer 48 4993 710 144 PASS/PASS
split-ts 41 4898 772 120 PASS/PASS
split-render 41 4276 500 120 PASS/PASS
split-event 41 4794 476 120 PASS/PASS
split-indirect 41 5122 456 120 PASS/PASS
twobatch 120 12224 716 360 PASS/PASS
depth 40 93405 9148 2880 PASS/PASS
indep 1 614 539 0 PASS/PASS
indep-order 6 7072 7136 0 PASS/PASS
indep-cbs 128 12654 624 384 PASS/PASS
indep-cbs-order 33 6713 3722 99 PASS/PASS
atom-chain 196 18737 844 588 PASS/PASS
atom-multi 41 4109 911 123 PASS/PASS
sem 120 11660 1255 360 PASS/PASS
dup 5 617 469 15 PASS/PASS

Must-pack cases: chain242, chain300, loops, chain-all, multi, multi-tail, disjoint. indep and indep-order record nothing packable, so 0 is expected there. loops packs at most one looping stream per packet, so its time barely changes.

Geekbench 6.7.1 GPU Vulkan

Median ms per iteration (iterations 1–19); every workload ran its 20 iterations in both arms.

workload off on on/off
Background Blur 32.23 24.18 0.75
Face Detection 48.20 38.92 0.81
Horizon Detection 18.01 18.07 1.00
Edge Detection 13.36 13.25 0.99
Gaussian Blur 22.43 22.43 1.00
Feature Matching 38.01 38.10 1.00
Stereo Matching 16.93 16.88 1.00
Particle Physics 65.88 65.90 1.00
  • No workload is slower with packing on.
  • Background Blur and Face Detection keep their gain from the first head (34.7 → 23.2 and 50.9 → 38.8 there).
  • Feature Matching and Particle Physics no longer gain (first head: 40.4 → 37.5 and 67.0 → 64.2).
    • The likely cause is the one-looping-stream-per-packet cap added for review finding 1. Most of their kernels loop, and that cap is what bounds a packet's run time under the 5 s command deadline.
    • This run did not trace those two workloads, so the cause is not confirmed yet. I'll confirm it with HK_PACK_CDM_TRACE in a follow-up row.

Review finding 1 on hardware: the same memory at two GPU addresses

The new aurora-packtest cases alias-chain and alias-instream (on the local follow-up branch) set this up:

  • a capture/replay buffer bound to the slot memory, which the driver maps at its own address (0x5fffbf8000 next to 0x5bfdaf0000);
  • dispatches that alternate between the two addresses with a COMPUTE → COMPUTE barrier between them;
  • 50 iterations, packing off and on.
case packing off packing on
alias-chain (64 CBs, alternate CBs use the other address) FAIL (0 packed) FAIL (3200 packed)
alias-instream (the same in one command buffer) FAIL FAIL

What the failures show:

  • A read through one address returns the memory as it was before the write through the other address: exactly mix(0xa5a5a5a5, salt), where 0xa5a5a5a5 is the host's initial fill. This happens across separate, unpacked commands too.
  • So G16G does keep stale data per address, as the reviewer suspected. But the exposure predates this PR: neither the in-stream flags-5 flush nor the end of a command makes a write through one address visible through the other.
  • Packing neither causes nor fixes it, so it doesn't block this PR. It is an existing hk bug for G16G memory reached through two addresses (capture/replay and sparse buffers): hk_cdm_alias_scope() is false on G16G.
  • I'll open it as its own issue/PR, with the grounded G16G wide flush (memory write-back bit) as the first candidate fix and these two cases as the test. If the fix is needed at the end of a command too, the kernel side is in scope.

@DjDeveloperr

Copy link
Copy Markdown
Author

Follow-up to the M4 row above (same session, head unchanged at ee3de2b).

Feature Matching / Particle Physics: confirmed, the one-looping-stream cap. Geekbench 6 ran with HK_PACK_CDM_TRACE=1, on this head plus local, trace-only counters of which planner condition splits each pair.

workload CBs packed split reason
Particle Physics 3838 0 submissions of 200 eligible CBs: 199/199 pair splits are loops
Feature Matching 4466 422 129-CB submissions: 128/128 loops
Gaussian Blur 2345 0 loops

No other condition (kinds, render waits, dispatch/thread/static-work bounds, size) fired. So the review's loop cap costs exactly the ~3 ms these two gained at the first head. Getting it back needs a run-time bound on looping members (for example a per-pipeline measured GPU time), not a different rule. I'm leaving that to a follow-up, and #488 keeps the cap.

Two GPU addresses: a fix candidate passes on hardware. The fix makes G16G use its wide barrier word, which writes cached data back to memory, wherever hk_cdm_alias_scope() asks for memory scope. That covers app barriers and stream ends, and only while an app has enabled an aliasing feature.

driver alias-chain off / on alias-instream off / on
this head FAIL / FAIL FAIL / FAIL
fix candidate PASS / PASS (3200 packed) PASS / PASS
  • 50 iterations each, on p5d.
  • The fix candidate also passes all 22 default packtest cases with packing on.

It goes up as its own PR after #488, together with the alias-* packtest cases.

@DjDeveloperr
DjDeveloperr merged commit 11ee623 into aurora-wip Nov 20, 2026
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