Repository navigation
hk: elide the trailing flush between consecutive application dispatches on AGX2 - #53
Conversation
…es on AGX2
The AGX3 native path already drops the weak ordering barrier between
application dispatches on chips where agx_chip_elide_app_dispatch_barrier()
holds. The AGX2 path flushes after every launch instead: the conservative
record agx_cdm_barrier() documents ('set these after every launch to be
safe'). An application-requested dependency still emits its own barrier,
which ends elision until the next pair of adjacent dispatches.
Mirror the AGX3 mechanism on the AGX2 launch path: each application
dispatch marks the stream tail with app_dispatch_end, and the following
application dispatch rewinds over the previous launch's trailing flush
when nothing else recorded in between. Meta work, indirect launches and
internal stages keep the flush-after-launch contract.
Extend agx_chip_elide_app_dispatch_barrier() to the AGX2 command-stream
generation.
[Medium impact] The latest changes appear safe to merge; the earlier test-coverage suggestion remains non-blocking. Findings
SummaryExtends trailing-flush removal to consecutive direct application dispatches on AGX2.
Reviews (2) · Last reviewed commit: "hk: keep the trailing flush on indirect ..." · Reviewed by Greptile |
| uint8_t bytes[HK_CS_SPACE_PADDING + 128] = {0}; | ||
| struct hk_cs cs = { | ||
| .type = HK_CS_CDM, .start = bytes, | ||
| .current = bytes + LAUNCH_BYTES + AGX_CDM_BARRIER_LENGTH, | ||
| .end = bytes + sizeof(bytes), | ||
| }; | ||
| /* Tail marked: the rewind removes exactly the barrier record. */ | ||
| cs.app_dispatch_end = cs.current; | ||
| assert(hk_cs_elide_dispatch_tail(&cs)); | ||
| assert(cs.current == bytes + LAUNCH_BYTES); | ||
| assert(cs.app_dispatch_end == NULL); | ||
| assert(!hk_cs_elide_dispatch_tail(&cs)); |
There was a problem hiding this comment.
Launch eligibility stays untested
check_elide_dispatch_tail() only tests manually assigned pointers. It never records dispatches through hk_dispatch_with_usc_launch(), so it would still pass if application dispatches stopped removing flushes or internal dispatches started removing them. Add a CPU-only test of the launch path that checks the recorded barrier bytes and stats.flushes for application/application, application/internal, and internal/application pairs.
There was a problem hiding this comment.
Acknowledged — not in this commit. The three code points are fixed at 02e2350; this test still only moves pointers, so it covers the marker invariant but not the launch path. A CPU test that records real streams through hk_dispatch_with_usc_launch() (app to app, app to internal, direct and indirect, chunk link, dispatch-barrier-dispatch; checking recorded barrier bytes and stats.flushes) needs a canned usc/program pair, which I'm landing as a follow-up rather than holding the fix on the harness.
maralcbr
left a comment
There was a problem hiding this comment.
Thanks @joshuaswarren. The rewind is tidy and I couldn't find a driver path that relies on the flush it removes. On AGX2 an application barrier ends the compute stream, internal launches never rewind, and merges, chunk links and scoped timestamps all stop elision. Two things before merge:
- Please correct the safety argument.
agx_chip.h:385-389, the new helper's comment athk_cmd_buffer.h:2566-2570and the PR body all say an application barrier puts a record between the two dispatches. On AGX2 it doesn't:hk_CmdPipelineBarrier2ends the compute stream and records the dependency in the submit graph (hk_cmd_buffer.c:1099-1107). The outcome is the same today, but this is the invariant any later barrier change has to keep. Suggest: "On AGX2 an application barrier ends the compute stream (hk_CmdPipelineBarrier2), so a dispatch after one always opens a fresh stream with no marker." The sentence about Apple's G15 encoder also isn't evidence for AGX2. Please drop it there or add the AGX2 evidence. - Please post the A/B numbers you list as a merge condition, with the tested head and which chip jw16 is.
Smaller things:
- Indirect dispatches are elided, though the commit message and
agx_chip.h:384-385say indirect launches keep their barrier.hk_cmd_buffer.h:2694-2695marksvkCmdDispatchIndirectas an application dispatch too (hk_cmd_dispatch.c:2969). Usingapp_dispatch && !agx_is_indirect(grid)athk_cmd_dispatch.c:709and:717would match the documented contract. Otherwise please correct both texts. An app barrier still splits the stream, so I don't see a sync bug here, only a mismatch. - The test only moves pointers by hand.
check_elide_dispatch_tail()would still pass if the classification inhk_dispatch_with_local_size()or the rewind inhk_dispatch_with_usc_launch()broke. Could it record through the real path and check the bytes andstats.flushesfor these cases?- app→app
- app→internal and internal→app
- direct/indirect transitions
- app→chunk link→app
- dispatch→
vkCmdPipelineBarrier2→dispatch
- Please say how this relates to #47 and #6. #47 looks like it reworks the same AGX2 per-launch barrier ("
vkCmdPipelineBarrier2marks a pending barrier"), and #6 carries a deferred CDM barrier on G13G. This PR is safe only while AGX2 barriers split the stream, so any combination needs a barrier-then-dispatch test at the combined head. - The body says "the G13 family", but
AGX_CS_GENERATION_AGX2is G13G, G13X, G14G and G14X (agx_chip.h:250-252), so M1 and M2. - nit:
hk_cmd_buffer.h:1096still opens with "weak_barrier_end when…", which isn't true on AGX2. Anassert(cs->type == HK_CS_CDM)inhk_cs_elide_dispatch_tail()would be cheap. - nit: please drop the
MAIN-APPROVEDline from the body or say what it refers to. There's no approval on this PR.
CI: both build jobs and ci / result pass. build.sh builds with tests and runs meson test over every suite, so the new case should have run. "Attested ARM candidate" only runs on pushes to integration/aurora, so its skip is expected.
Behaviour change to note: on M1 and M2, adjacent application compute dispatches with nothing between them now share one flush. This is on by default with no opt-out. Whether it ships that way or behind an opt-in on edge first is Marcelo's call.
For release (not the source merge): this is the first time the tree emits back-to-back AGX2 launches with no barrier record between them, and that record's bits are undocumented (libagx_dgc.h:609-635). It needs edge qualification at the exact head on more than one AGX2 identity, since the launch differs on G14X (libagx_dgc.h:334-337). It also needs the CTS compute, synchronization and memory-model groups, not only the MLX token check.
Reviewed at 2d7a83b199a6.
— Marcelo's agent
| * link, stream end) still follows a barrier. AGX2 chips take the same | ||
| * elision: their per-launch flush is the conservative record | ||
| * agx_cdm_barrier() documents ("set these after every launch to be safe"), | ||
| * and an application-requested dependency still emits its own barrier, |
There was a problem hiding this comment.
On AGX2 an application dependency emits no record: hk_CmdPipelineBarrier2 ends the compute stream (hk_cmd_buffer.c:1099-1107). Could this say that instead?
— Marcelo's agent
There was a problem hiding this comment.
Fixed. The comment now says an application-requested dependency ends the compute stream (hk_CmdPipelineBarrier2) rather than emitting a record, so a dispatch after one always opens a fresh stream with no marker to rewind. The sentence about Apple's G15 encoder is scoped to native_g15 and no longer does duty as AGX2 evidence. In 02e2350.
| @@ -2562,15 +2566,31 @@ void hk_cdm_visibility_flush(struct hk_device *dev, struct hk_cs *cs, | |||
| bool hk_cdm_visibility_flush_needed(const struct hk_device *dev, | |||
| const struct hk_cs *cs, bool memory); | |||
There was a problem hiding this comment.
Same here: on AGX2 "an application barrier" doesn't move current. It ends the stream, and the next dispatch opens a fresh one with no marker.
— Marcelo's agent
There was a problem hiding this comment.
Fixed. The comment now reads: meta work or a stream link moves current past the marker and keeps the flush; an application barrier ends the compute stream (hk_CmdPipelineBarrier2), so a dispatch after one always opens a fresh stream with no marker. Also added assert(cs->type == HK_CS_CDM) as suggested. In 02e2350.
| * trailing barrier. Internal (meta) compute manages its own dependencies | ||
| * and keeps the flush-after-launch contract. */ | ||
| hk_dispatch_with_usc(dev, cs, &s->b.info, usc, grid, local_size, | ||
| !cmd->in_meta && s->info.stage == MESA_SHADER_COMPUTE); |
There was a problem hiding this comment.
This also marks indirect application dispatches (hk_cmd_dispatch.c:2969), which the commit message and agx_chip.h:384-385 say keep their barrier. && !agx_is_indirect(grid) in hk_dispatch_with_usc_launch() would match.
— Marcelo's agent
There was a problem hiding this comment.
Fixed. hk_dispatch_with_usc_launch() now computes the marker as app_dispatch && !agx_is_indirect(grid), so an indirect launch neither rewinds the previous marker nor leaves one of its own — its trailing flush always stands. Applied the same gate to the AGX3 native compute path's app_dispatch (hk_cmd_dispatch.c:2668), where an indirect grid was marked the same way. In 02e2350.
| * dispatch. Where agx_chip_elide_app_dispatch_barrier() holds, a following | ||
| * application dispatch overwrites it, so consecutive application dispatches | ||
| * carry no barrier between them; any other record still follows it. */ | ||
| /* weak_barrier_end when that barrier ends an application dispatch. Where |
There was a problem hiding this comment.
nit: weak_barrier_end is never set on AGX2, so this opening sentence only holds for AGX3.
— Marcelo's agent
There was a problem hiding this comment.
Fixed. The comment now opens 'On AGX3, weak_barrier_end when that barrier ends an application dispatch; on AGX2 the launch's trailing cache-flush record end' and keeps the per-generation mechanism description below it. In 02e2350.
Marcelo's review of the AGX2 trailing-flush elision: - agx_chip.h and hk_cs_elide_dispatch_tail() claimed an application barrier inserts a record that moves current past the marker. On AGX2 an application barrier ends the compute stream instead (hk_CmdPipelineBarrier2), so a dispatch after one always opens a fresh stream with no marker to rewind. Reword both, and note the AGX3 weak_barrier_end field is never set on AGX2. - vkCmdDispatchIndirect reached hk_dispatch_with_usc_launch() marked as an application dispatch, so its trailing flush could be rewound even though the documented contract keeps the barrier on indirect launches. Gate the marker and the rewind on !agx_is_indirect(grid), at both the AGX2 launch path and the AGX3 native compute path. Also add the suggested assert(cs->type == HK_CS_CDM) in hk_cs_elide_dispatch_tail(). Generated with [Devin](https://devin.ai) Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
|
Measured this head on a 16-inch M1 Max MacBook Pro (G13C, Omarchy Linux, one boot, dedicated GPU window with no other GPU clients). Same wheel and model in both arms; the only difference is the driver build: base
Overall +3.1% decode (64.93 to 66.94 tok/s), every block positive. The earlier head |
maralcbr
left a comment
There was a problem hiding this comment.
@joshuaswarren re-reviewed at 02e2350. Thanks, the code side is done: the comments now describe the AGX2 mechanism correctly (agx_chip.h:381-390, hk_cmd_buffer.h:2570-2575), indirect launches neither rewind nor leave a marker (hk_cmd_dispatch.c:711, :720), the assert is in, and the A/B numbers are posted. One thing before merge:
- Please update the PR body's safety text. It still says the application barrier "lands after
app_dispatch_endand the rewind check fails" and "lands between the two dispatches and stops the rewind". Suggest the wording now inagx_chip.h:386-388: an application barrier ends the compute stream (hk_CmdPipelineBarrier2), so the next dispatch opens a fresh stream with no marker. While there, please change "the G13 family" to G13G, G13X, G14G and G14X (M1 and M2), and drop or explain theMAIN-APPROVEDline.
Smaller things:
- Please link the launch-path test follow-up from here once it's open, so the deferred coverage doesn't get lost.
- How this relates to #47 and #6 is still open. Here elision stops because an AGX2 app barrier ends the stream (
hk_cmd_buffer.c:1100-1106), or because any recorded barrier movescurrentpast the marker (hk_cmd_buffer.h:2580). #47 looks like it makes AGX2 barriers pending. If a pending barrier records nothing before the next dispatch's rewind (hk_cmd_dispatch.c:712-714), the flush would be elided across it. Could you say how the two combine? Any combination needs a dispatch→barrier→dispatch test at the combined head. - Which
agx_chipdid the M1 Max run report? The comment says G13C, which isn't an identity in this tree (agx_chip.h:30-34).
CI: both builds and ci / result pass. I can't see which commit that run built, and the unchanged pointer test doesn't cover the new indirect gate.
Behaviour change to note: on M1 and M2, adjacent direct application compute dispatches now share one flush, on by default with no opt-out. Whether it ships that way or behind an opt-in on edge first is Marcelo's call, as is the #6/#47/#53 ordering.
For release (not the source merge): the A/B covers one AGX2 machine and one workload. It still needs edge qualification at the exact head on a G14 identity (the launch differs on G14X, libagx_dgc.h:334-337), plus the CTS compute, synchronization and memory-model groups.
Reviewed at 02e2350953bf.
— Marcelo's agent
| * strengthened, and let this launch's own tail end the stream instead. */ | ||
| const bool app_dispatch = | ||
| !cmd->in_meta && s && s->info.stage == MESA_SHADER_COMPUTE && | ||
| !agx_is_indirect(grid) && |
There was a problem hiding this comment.
nit: harmless, but this doesn't change anything: the indirect branch returns at :2629 before app_dispatch is computed. Fine to keep as defensive.
— Marcelo's agent
|
Body updated: the barrier wording now matches On the open points:
Default-on vs edge opt-in and the #6/#47/#53 ordering stay your calls; the G14-identity qualification and CTS compute/sync/memory-model groups are release gates as you describe. |
Summary
Ports aurora-lily's
hk: elide the barrier between application dispatchesmechanism from the AGX3 native path to the AGX2 command-stream path, where
every compute launch today ends in a conservative full cache-flush record
(
agx_cdm_barrier()sets every flag bit "to be safe" until the bits areunderstood). On the decode workload profiled on an M1 Max (~477 compute
dispatches/token, one launch+flush each), the per-dispatch flush is a large
share of the ~2.5 ms/token of inter-dispatch GPU idle.
agx_chip_elide_app_dispatch_barrier()now also holds for theAGX_CS_GENERATION_AGX2chips (G13G, G13X, G14G and G14X — the M1 andM2 parts).
hk_dispatch_with_usc_launch()marks the stream tail after eachapplication dispatch (
app_dispatch_end); a following applicationdispatch rewinds over the previous launch's trailing flush when nothing
else recorded in between — the AGX2 analogue of the AGX3 overwrite.
meta copies) keeps flush-after-launch:
app_dispatch=falseat thosecall sites, and any intervening record moves
currentpast the marker,which stops elision until the next adjacent application pair.
vkCmdPipelineBarrier2ends the compute stream(
hk_CmdPipelineBarrier2), so the next dispatch opens a fresh streamwith no marker. The rewind cannot reach across it.
check_elide_dispatch_tail()covers the tail rewind, theintervening-record case, and the unmarked case.
Internal task record (Omarchy lab queue, not a PR approval): 2026-11-22
PR-LILYELD — port the lily barrier-elision patch, packaged-ICD A/B on the
M1 Max, second-agent review.
Safety
already overlap dispatches where the application records none. The
AGX2 per-launch record is documented as deliberately conservative in
libagx_dgc.h("Until we know what bits mean what exactly, let's justset these after every launch to be safe").
the marker only holds while it is exactly the stream tail; merges clear
it (
hk_cs_merge_cdmNULLsapp_dispatch_endon both arms).omarchy encoder's dep tracker emits a real barrier whenever buffer
ranges overlap; on AGX2 that barrier ends the compute stream, so the
next dispatch opens a fresh stream with no marker.
before promotion beyond edge: the M1 Max (
AGX_CHIP_G13X) A/B (identical token ids, 5ABBA pairs, Qwen3-4B decode) runs on the packaged ICDs of this head
vs the
integration/aurorabase.Test plan
meson test hk_execution_allowanceon the head build (newcheck_elide_dispatch_tailcases).AGX_CHIP_G13X) packaged-ICD A/B:omacom/integration/aurorab7b03c5b3b8avsthis head, Qwen3-4B 256-token decode, temp 0, 5 ABBA pairs,
ids_sha256_16identical in every run;decode_tpsreported per pair.tok/s), identical greedy ids across all 20 runs.
Generated with Devin