Repository navigation
borealis/agx2: reduce and defer CDM cache flushes on G13G - #1219
Conversation
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>
Codex Review Summary: ❌ FailedThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
|
You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard. |
|
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
left a comment
There was a problem hiding this comment.
Verdict: FINISH, then merge at d8ab776cfa8ceaecb1625102da164b2cc4452429
Blockers:
-
src/asahi/vulkan/hk_cmd_dispatch.c:939:deferis true for every launch oncurrent_cs.csoutsidein_meta. That includes the driver's own libagx precomp launches (hk_dispatch_precomp, AGX2 path,hk_cmd_buffer.c:5439), which also set neitherin_metanorcdm_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), thenvkCmdCopyQueryPoolResults(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_CmdResetQueryPoolrecords 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:1439andcs_reusable()athk_sync.c:1326both use a strict>).- The copy kernel (
hk_query_pool.c:2014) is then launched withdefertrue andcdm_barrier_pendingfalse, so neither the barrier branch nor the!defer && cdm_flush_pendingbranch at:956runs.
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
deferin 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, asin_metalaunches do. Add a stream-test case for precomp then precomp, and rundEQP-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_metalaunch emits 0x1ff first; - every
hk_cdm_flush_implcall clearscdm_flush_pending; - every stream close goes through
hk_cdm_end_stream_flush. The one early return there (deferred occlusion availability) requiresstats.cmds == 0, so nothing can be pending.
- a pure-SSBO barrier appends 0x160 through
-
hk_queue_writeand 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_agx3only (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.
- the conditional-rendering grid patch is
-
NO-GATES:
AGX_CAP_CDM_DEFER_FLUSHis a row in the chip-caps table, which is the tree's capability mechanism. The G13G addition toagx2_cdm_barrier_default_wordextends an existing measured-chip list. Neither is a blocker. -
Rows: the G13G (M1) row was measured on
3c2139a4bb0, rebased ontob200715b. 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 sharedhk_cmd_dispatch.cpath.
Evidence
- Local x86 drm-shim gate, this head merged into aurora-wip
a97571c6: 338/338 OK, the same as the tip. The unit tests callhk_dispatch_with_usc_launchdirectly and never go throughhk_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). Thenhk_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>
|
Good catch on Blocker 1 (
Verification at
Also queuing a matched 5-pair exact-head Apple M1 (G13G) LLM decode ABBA at |
RyanTheTide
left a comment
There was a problem hiding this comment.
Verdict: MERGE at 53c0d9c0e802
Blockers: none. The blocker from the d8ab776 review is fixed.
Checked:
hk_dispatch_precomp()now passesallow_defer = false, andhk_dispatch_with_usc()passes it only forMESA_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 fromdefer,hk_cdm_barrier_streamaborts (337/1), so the new cases cover the mechanism. Non-blocking: they callhk_dispatch_with_usc_launch(..., false)directly, so flipping the argument insidehk_dispatch_precomp()itself still passes (338/0). A case that goes throughhk_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 vsb200715b. 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_FLUSHis G13G-only, and theagx_chip.hchange only adds G13G. On other chipscdm_flush_pendingis 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.
…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
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>
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:
0x1ff/0x160CDM 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 default0x1ffbarrier word and0x160pure compute-to-compute storage-buffer narrowing toAGX_CHIP_G13G(which previously kept the full0xfffffword on every dispatch). Sparse resource creation andfullcdmbarrierstill upgrade all recorded sites to0xfffff.AGX_CAP_CDM_DEFER_FLUSH): omits the trailing0x1ffcache flush after independent application compute launches inside an open AGX2 compute stream. Instead, the driver emits0x160when a pure compute-to-compute SSBO pipeline barrier precedes the next launch,0x1ffon broad pipeline barriers, image-touching boundaries, or before non-deferred / meta launches, and0x1ffat compute stream close (hk_cdm_end_stream_flush()).fullcdmbarrierdisables deferral and emits0xfffffafter every launch.Measurement (Apple M1 8-core, G13G, Linux)
Matched 5-pair interleaved ABBA (
A->B,B->A, ...) onQwen3-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 againstaurora-wip@3c2139a4bb0(rebased ontoaurora-wip@b200715b4841with zero functional diff in the touched files):0xfffff) vs Commit 1 (0x1ff/0x160)ctx=51223.71 -> 23.88+0.72%(+0.92%paired med)4/5135.6 -> 135.5(-0.04%)5dff52a5eb914256)0xfffff) vs Commit 1 (0x1ff/0x160)ctx=204821.09 -> 21.15+0.25%(+1.05%paired med)4/5144.6 -> 144.6(-0.04%)277cc0082ed08638)0x1ff/0x160) vs Commit 2 (+DEFER_FLUSH)ctx=51223.72 -> 23.99+1.16%5/5135.2 -> 135.8(+0.44%,5/5)5dff52a5eb914256)0x1ff/0x160) vs Commit 2 (+DEFER_FLUSH)ctx=204821.12 -> 21.38+1.25%4/5144.6 -> 144.6(-0.02%)277cc0082ed08638)0xfffff) vs Combined (Commits 1 + 2)ctx=51223.46 -> 23.96+2.16%5/5135.6 -> 135.6(-0.02%)5dff52a5eb914256)0xfffff) vs Combined (Commits 1 + 2)ctx=204820.73 -> 21.42+3.33%5/5144.6 -> 144.6(+0.03%)277cc0082ed08638)Verification
-Dbuildtype=release -Db_ndebug=false, clang 18.1.3): fullmeson test --suite asahi214/214 PASS on rebased headd8ab776cfa8c, including newG13Gcoverage intest-agx-cdm-barrier-word.c,test-chip-caps.c(test_cdm_defer_flush), andtest-hk-cdm-barrier-stream.c(test_defer_flush_g13gcovering independent launches, pure SSBO0x160, broad0x1ff, submit-time sparse patching to0xfffff,in_metaflush fencing, andfullcdmbarrier).16,421cases (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).G13X,G14G,G14X) and AGX3: unchanged;AGX_CAP_CDM_DEFER_FLUSHis enabled onG13Gonly until other AGX2 dies are measured.