Skip to content

borealis/agx2: reduce and defer CDM cache flushes on G13G - #1219

Merged
RyanTheTide merged 3 commits into
aurora-wipfrom
w8B/agx2-g13g-cdm-defer
Nov 27, 2026
Merged

RyanTheTide merged 3 commits into
aurora-wipfrom
w8B/agx2-g13g-cdm-defer

Conversation

@joshuaswarren

Copy link
Copy Markdown

Summary

Two compute-barrier improvements for Apple M1 (G13G) on AGX2 command streams, enabled only on the measured G13G chip row with no new runtime switches:

  1. Enable the reduced 0x1ff / 0x160 CDM barrier word on G13G (agx2_cdm_barrier_default_word()): extends hk: narrow AGX2 CDM barrier word to 0x160 for pure compute SSBO dependencies #1069's measured default 0x1ff barrier word and 0x160 pure compute-to-compute storage-buffer narrowing to AGX_CHIP_G13G (which previously kept the full 0xfffff word on every dispatch). Sparse resource creation and fullcdmbarrier still upgrade all recorded sites to 0xfffff.
  2. Defer post-launch CDM cache flushes on G13G (AGX_CAP_CDM_DEFER_FLUSH): omits the trailing 0x1ff cache flush after independent application compute launches inside an open AGX2 compute stream. Instead, the driver emits 0x160 when a pure compute-to-compute SSBO pipeline barrier precedes the next launch, 0x1ff on broad pipeline barriers, image-touching boundaries, or before non-deferred / meta launches, and 0x1ff at compute stream close (hk_cdm_end_stream_flush()). fullcdmbarrier disables deferral and emits 0xfffff after every launch.

Measurement (Apple M1 8-core, G13G, Linux)

Matched 5-pair interleaved ABBA (A->B, B->A, ...) on Qwen3-4B-Instruct-2507-4bit (gen=64, 3 inner trials per leg, token deferral disabled, isolated per-arm shader caches, bit-identical greedy token IDs across every trial and arm, 0 GPU timeouts). Measured against aurora-wip@3c2139a4bb0 (rebased onto aurora-wip@b200715b4841 with zero functional diff in the touched files):

Comparison Workload Decode (tok/s) Delta Pair wins Prefill (tok/s) Greedy IDs
Base (0xfffff) vs Commit 1 (0x1ff/0x160) Qwen3-4B ctx=512 23.71 -> 23.88 +0.72% (+0.92% paired med) 4/5 135.6 -> 135.5 (-0.04%) identical (5dff52a5eb914256)
Base (0xfffff) vs Commit 1 (0x1ff/0x160) Qwen3-4B ctx=2048 21.09 -> 21.15 +0.25% (+1.05% paired med) 4/5 144.6 -> 144.6 (-0.04%) identical (277cc0082ed08638)
Commit 1 (0x1ff/0x160) vs Commit 2 (+DEFER_FLUSH) Qwen3-4B ctx=512 23.72 -> 23.99 +1.16% 5/5 135.2 -> 135.8 (+0.44%, 5/5) identical (5dff52a5eb914256)
Commit 1 (0x1ff/0x160) vs Commit 2 (+DEFER_FLUSH) Qwen3-4B ctx=2048 21.12 -> 21.38 +1.25% 4/5 144.6 -> 144.6 (-0.02%) identical (277cc0082ed08638)
Base (0xfffff) vs Combined (Commits 1 + 2) Qwen3-4B ctx=512 23.46 -> 23.96 +2.16% 5/5 135.6 -> 135.6 (-0.02%) identical (5dff52a5eb914256)
Base (0xfffff) vs Combined (Commits 1 + 2) Qwen3-4B ctx=2048 20.73 -> 21.42 +3.33% 5/5 144.6 -> 144.6 (+0.03%) identical (277cc0082ed08638)

Verification

  • Unit tests (-Dbuildtype=release -Db_ndebug=false, clang 18.1.3): full meson test --suite asahi 214/214 PASS on rebased head d8ab776cfa8c, including new G13G coverage in test-agx-cdm-barrier-word.c, test-chip-caps.c (test_cdm_defer_flush), and test-hk-cdm-barrier-stream.c (test_defer_flush_g13g covering independent launches, pure SSBO 0x160, broad 0x1ff, submit-time sparse patching to 0xfffff, in_meta flush fencing, and fullcdmbarrier).
  • Live Vulkan CTS on Apple M1 (G13G): 16,421 cases (dEQP-VK.synchronization.*, dEQP-VK.synchronization2.*, dEQP-VK.memory_model.*) comparing base vs Commit 1 vs Combined: 5,175 PASS / 0 FAIL / 11,246 NotSupported on all three arms (0 regressions, 0 status diffs).
  • Other AGX2 parts (G13X, G14G, G14X) and AGX3: unchanged; AGX_CAP_CDM_DEFER_FLUSH is enabled on G13G only until other AGX2 dies are measured.

Extend agx2_cdm_barrier_default_word() to AGX_CHIP_G13G so G13G uses the
0x1ff default CDM barrier word and narrows pure compute-to-compute SSBO
barriers in place to 0x160 (still upgrading to 0xfffff on sparse use or
fullcdmbarrier).

Signed-off-by: Joshua Warren <816217+joshuaswarren@users.noreply.github.com>
Add AGX_CAP_CDM_DEFER_FLUSH (enabled on G13G) to omit the trailing 0x1ff
CDM cache flush after independent application compute launches, emitting
0x160 on pure SSBO barriers and 0x1ff on broad barriers, meta launches,
and stream close.

Signed-off-by: Joshua Warren <816217+joshuaswarren@users.noreply.github.com>
@joshuaswarren
joshuaswarren requested review from a team and DjDeveloperr as code owners November 27, 2026 08:47
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 27, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ❌ Failed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ❌ Failed 2026-11-27T08:47:42.437370Z
We couldn’t complete this review. Comment @codex review to try again.
d8ab776 PR opened
🔒 Security Review ❌ Failed 2026-11-27T08:47:50.458859Z
We couldn’t complete this review. Comment @codex security review to try again.
d8ab776 PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector

Copy link
Copy Markdown

You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard.
To continue using code reviews, add credits to your account and enable them for code reviews in your settings.

@chatgpt-codex-connector

Copy link
Copy Markdown

The account paying for this security review has reached its Codex usage limits. The payer can check the Codex usage dashboard. For personal accounts, using credits requires enabling “Use credits for security reviews” in Code review settings. If you do not manage the paying account, contact this repository's admins.

@RyanTheTide RyanTheTide left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verdict: FINISH, then merge at d8ab776cfa8ceaecb1625102da164b2cc4452429

Blockers:

  1. src/asahi/vulkan/hk_cmd_dispatch.c:939: defer is true for every launch on current_cs.cs outside in_meta. That includes the driver's own libagx precomp launches (hk_dispatch_precomp, AGX2 path, hk_cmd_buffer.c:5439), which also set neither in_meta nor cdm_barrier_pending. Ordering between those internal launches came only from the 0x1ff that ended every launch, and on G13G that 0x1ff is now gone.

    Input that breaks: on G13G, record vkCmdResetQueryPool(pool, 0, N) on any non-timestamp pool (occlusion, pipeline statistics), then vkCmdCopyQueryPoolResults(pool, 0, N, buf, ...) in the same command buffer with no barrier. Vulkan 17.2 says the copy sees the reset without synchronization. Here is what gets recorded:

    • hk_CmdResetQueryPool records the reset kernel (hk_query_pool.c:632) and makes the open stream the cut (hk_sync_after_stream).
    • hk_query_command_cs() keeps the same stream for the copy, because the cut is not later than it (hk_sync.c:1439 and cs_reusable() at hk_sync.c:1326 both use a strict >).
    • The copy kernel (hk_query_pool.c:2014) is then launched with defer true and cdm_barrier_pending false, so neither the barrier branch nor the !defer && cdm_flush_pending branch at :956 runs.

    The stream is therefore reset launch, then copy launch, with no CDM barrier between them, and the copy can read the results and availability from before the reset. Before this PR the reset launch ended in 0x1ff.

    The same happens with reset, then a dispatch inside an active pipeline-statistics query: libagx_increment_cs_invocations (hk_cmd_dispatch.c:5029) adds to the counter the reset kernel is still zeroing.

    Fix: defer only application dispatches. For example, decide defer in the API dispatch path and pass it down, or have the AGX2 precomp branch drain a pending flush before its launch and keep its own trailing flush, as in_meta launches do. Add a stream-test case for precomp then precomp, and run dEQP-VK.query_pool.* on G13G. The CTS run in the body (synchronization, synchronization2, memory_model) doesn't use queries.

Checked:

  • Read the whole diff, the body and the comments.

  • The codex-connector comments are usage-limit failures and contain no findings, so nothing there is left unanswered.

  • The application-dispatch paths handle the pending flush correctly:

    • a pure-SSBO barrier appends 0x160 through hk_cdm_narrow_trailing_barrier;
    • a broad barrier, an image-touching boundary or an in_meta launch emits 0x1ff first;
    • every hk_cdm_flush_impl call clears cdm_flush_pending;
    • every stream close goes through hk_cdm_end_stream_flush. The one early return there (deferred occlusion availability) requires stats.cmds == 0, so nothing can be pending.
  • hk_queue_write and the query copy on native AGX3 flush explicitly before their launch, so they are fine.

  • I also checked the other internal producer/consumer chains:

    • the conditional-rendering grid patch is native_agx3 only (hk_physical_device.c:283);
    • the gen1 acceleration-structure rebase and restore, and trace_rays, are G16S/G17P/M5 only.

    None of them is reachable on G13G.

  • NO-GATES: AGX_CAP_CDM_DEFER_FLUSH is a row in the chip-caps table, which is the tree's capability mechanism. The G13G addition to agx2_cdm_barrier_default_word extends an existing measured-chip list. Neither is a blocker.

  • Rows: the G13G (M1) row was measured on 3c2139a4bb0, rebased onto b200715b. The body says the touched files have no functional diff, but the row is not recorded at this exact head, and there is no regression row from another AGX2 generation (G13S/G14G) for the shared hk_cmd_dispatch.c path.

Evidence
  • Local x86 drm-shim gate, this head merged into aurora-wip a97571c6: 338/338 OK, the same as the tip. The unit tests call hk_dispatch_with_usc_launch directly and never go through hk_dispatch_precomp/query recording, so they can't see the case above.
  • Trace at this head: hk_CmdResetQueryPool → hk_query_command_cs → emit_zero_queries → libagx_reset_query_struct → hk_dispatch_precomp (AGX2 branch) → hk_dispatch_with_usc_launch (defer, cdm_flush_pending = true, :968). Then hk_CmdCopyQueryPoolResults → hk_query_command_cs (same stream) → libagx_copy_query_struct → hk_dispatch_with_usc_launch (defer, no barrier emitted).

Pass allow_defer into hk_dispatch_with_usc_launch() so only application
compute dispatches defer the trailing CDM flush on G13G. Internal
libagx precomp launches (such as query reset, query copy, and compute
invocation stat updates) and non-compute helper launches pass
allow_defer = false, draining any pending deferred flush before launch
and keeping their own trailing flush.

Add stream tests covering precomp -> precomp, deferred app -> precomp,
and deferred app -> pure-SSBO barrier -> precomp.

Signed-off-by: Joshua Warren <816217+joshuaswarren@users.noreply.github.com>
@joshuaswarren

Copy link
Copy Markdown
Author

Good catch on hk_dispatch_precomp -- pushed the fix in 53c0d9c0e802 on top of d8ab776cfa8c.

Blocker 1 (src/asahi/vulkan/hk_cmd_dispatch.c:939 deferring internal precomp launches):

  • Added bool allow_defer to hk_dispatch_with_usc_launch() and gated defer on it.
  • hk_dispatch_precomp() now passes allow_defer = false (and hk_dispatch_with_usc() passes info->stage == MESA_SHADER_COMPUTE), so only application compute dispatches defer the trailing CDM flush on G13G.
  • Every internal precomp launch (query reset, query copy, compute invocation stat updates) and non-compute helper launch now drains any pending deferred flush before launching and keeps its own trailing 0x1ff flush, matching in_meta launches.
  • Added three cases in src/asahi/vulkan/tests/test-hk-cdm-barrier-stream.c: precomp -> precomp, deferred app -> precomp, and deferred app -> pure-SSBO barrier -> precomp.

Verification at 53c0d9c0e802 vs b200715b4841:

  • Unit tests (mesa:asahi, -Db_ndebug=false): 214/214 pass, including hk_cdm_barrier_stream and asahi-agx-chip-caps.
  • Live Apple M1 (G13G) dEQP-VK.query_pool.* (19,276 cases): 19,244 pass / 0 fail / 32 not supported, 0 regressions vs b200715b4841.
  • Live Apple M1 (G13G) dEQP-VK.synchronization.* + synchronization2.* + memory_model.* (16,421 cases): 5,175 pass / 0 fail / 11,246 not supported, 0 regressions vs b200715b4841.

Also queuing a matched 5-pair exact-head Apple M1 (G13G) LLM decode ABBA at 53c0d9c0e802 vs b200715b4841 (the fix leaves the application compute dispatch path unchanged, and I will drop the exact-head numbers here once it completes).

@RyanTheTide RyanTheTide left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verdict: MERGE at 53c0d9c0e802

Blockers: none. The blocker from the d8ab776 review is fixed.

Checked:

  • hk_dispatch_precomp() now passes allow_defer = false, and hk_dispatch_with_usc() passes it only for MESA_SHADER_COMPUTE. Internal kernels (query reset/copy, stat updates) therefore drain any deferred flush before they launch and keep their own trailing 0x1ff. The query reset -> copy race on G13G is closed.
  • Head merged into aurora-wip 088f2038: local drm-shim gate 338 Ok / 0 Fail.
  • Negative check: with the allow_defer && term removed from defer, hk_cdm_barrier_stream aborts (337/1), so the new cases cover the mechanism. Non-blocking: they call hk_dispatch_with_usc_launch(..., false) directly, so flipping the argument inside hk_dispatch_precomp() itself still passes (338/0). A case that goes through hk_dispatch_precomp() would pin that line.
  • Rows: the G13G (M1) CTS at this head covers query_pool.* 19,244/0 and sync/sync2/memory_model 5,175/0, with 0 regressions vs b200715b. My earlier ask for a G13S/G14G regression row is withdrawn. Every behaviour change in this PR is gated to G13G: AGX_CAP_CDM_DEFER_FLUSH is G13G-only, and the agx_chip.h change only adds G13G. On other chips cdm_flush_pending is never set, so their command streams are unchanged by construction, and a row there would exercise none of this code. The decode ABBA you queued is welcome as a perf record, but it doesn't gate the merge.

@RyanTheTide
RyanTheTide merged commit 422af02 into aurora-wip Nov 27, 2026
@RyanTheTide
RyanTheTide deleted the w8B/agx2-g13g-cdm-defer branch November 27, 2026 13:39
joshuaswarren added a commit that referenced this pull request Nov 27, 2026
…ait slots on G14X

Add two AGX2 compute-shader scheduling passes gated via agx_chip_caps on G14X (Apple M2 Pro/Max/Ultra), with no new runtime switches. Other AGX2 chips (G13G, G13X, G14G) keep their prior behavior until measured on aurora-wip:

1. AGX_CAP_COMPUTE_HOIST_LOAD_ADDRS (1u << 28; agx_hoist_loads in agx_pressure_schedule.c, run after agx_ilp_schedule on AGX2 compute): hoists non-coherent device_loads together with up to 3 pure SSA address-producing predecessors (pure reorderable/eliminable opcodes, SSA dests only, SSA/uniform/immediate sources only) up to 64 instructions earlier across independent ALU, threadgroup barriers, workgroup memory accesses, non-conflicting uniform stores, and other non-coherent device loads. Maintains a strictly-earlier (best_target, best_chain_len) dominance invariant so no producer ever moves downward past other consumers.

2. AGX_CAP_COMPUTE_SMART_WAITS (1u << 27; agx_insert_waits_local in agx_insert_waits.c): for compute shaders on AGX2, partitions the two hardware scoreboard slots by first-use distance and prefers the non-full slot before overflowing AGX_MAX_PENDING (8), eliminating mid-burst slot-0 overflow stalls when early-use loads mix with late-use scale/bias loads.

Rebased onto aurora-wip a15657a and renumbered caps to 1u << 27 and 1u << 28 (after merged #1219 took 1u << 25 and pending agx/g14x-preamble-bases-low uses 1u << 26).

Unit tests:
- test-chip-caps: all checks pass (G14X only).
- HoistLoads (10 cases) + InsertWaits (1 case): 11/11 pass on this head (fail on base 3edbd1c).

Author-Model: google/gemini-4-neon
RyanTheTide pushed a commit that referenced this pull request Nov 28, 2026
Document and assert in hk_dispatch_with_usc_launch() that draw-feeding
GS/tess compute helpers on current_cs.pre_gfx never defer their trailing
CDM cache flush, and update the stale AGX2 flush comment on the G17P
compute launch path after AGX_CAP_CDM_DEFER_FLUSH landed in #1219/#1233.

Signed-off-by: Joshua Warren <816217+joshuaswarren@users.noreply.github.com>
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