Skip to content

agx: hoist compute device loads over independent ALU and barriers on G13 - #1236

Closed
joshuaswarren wants to merge 1 commit into
aurora-wipfrom
w8B/agx-compute-hoist-loads
Closed

joshuaswarren wants to merge 1 commit into
aurora-wipfrom
w8B/agx-compute-hoist-loads

Conversation

@joshuaswarren

@joshuaswarren joshuaswarren commented Nov 27, 2026 •

Copy link
Copy Markdown

What

Adds agx_hoist_loads, a post-schedule compiler pass that hoists
compute-device loads over independent ALU work and barriers inside a
64-instruction window, behind a new AGX_CAP_COMPUTE_HOIST_LOADS
capability. The cap is set on the G13G and G13X chip rows (the dies that
share this load-latency profile).

Ported from the mesa-1 hoist-loads line, where it shipped as part of a
family with address-drag and smart-waits; this PR is the standalone
piece with its own capability gate.

Why

Separates a single scheduling lever so its effect can be measured and
reverted on its own, instead of living only inside the combined family.

Tests

  • agx_tests on the PR head tree: 1650 passed, 1 skipped
    (NativeFMA.ExactDyadicOracle, pre-existing skip), including the 10
    new HoistLoads unit tests and the test-chip-capability coverage.
  • Bit-exact token IDs on every ABBA leg (greedy decode, gen64).

Hardware A/B — jw16 (MacBook Pro 16-inch, G13X / t6001)

Same-machine interleaved ABBA, 5 pairs per cell, 3 inner trials per
leg, isolated MESA_SHADER_CACHE_DIR per arm, /proc/self/maps ICD
verification on every leg (libvulkan_asahi_hoist_base_822cf58723a.so
vs libvulkan_asahi_hoist_cand_53926df751c.so), deferral pinned off.
Workload: greedy decode, mlx-community/Qwen3-4B-Instruct-2507-4bit,
gen 64.

cell decode (base → cand) Δ wins prefill (base → cand) Δ bit-exact
ctx512 89.17 → 89.22 tok/s +0.06% 2/5 414.5 → 412.1 tok/s −0.59% yes (5/5 pairs)
ctx2048 66.90 → 66.77 tok/s −0.19% 2/5 493.6 → 487.4 tok/s −1.26% yes (5/5 pairs)

Standalone hoisting is decode-neutral at both context sizes on G13X
(+0.06% / −0.19%, inside run-to-run noise) and slightly slower on
prefill (−0.59% / −1.26%). (In the mesa-1 family measurement the
combined lever line read +3.4% decode on G13C; the standalone lever
is not where that gain lives.) This is the first standalone
measurement of the hoist; posted so the cap and pass can land on
their own with honest numbers. The prefill delta is the honest cost
of the extra hoisted loads in flight; if that is a merge blocker the
cap can be narrowed, but the pass is default-on only where the cap
is set.

Add agx_hoist_loads, a post-schedule compute pass gated by the new
AGX_CAP_COMPUTE_HOIST_LOADS capability on the measured G13 rows (G13G,
G13X). For non-preamble compute shaders on chips carrying the capability,
the pass moves each non-coherent device_load up to 64 instructions earlier,
to just after the last producer of its sources.

It crosses pure ALU (schedule class none, reorderable and eliminable),
threadgroup barriers, workgroup memory loads and stores, other non-coherent
device loads, and immediate uniform_store instructions whose 16-halfword
window does not overlap any uniform source of the load. It stops at device
stores, atomics, memory barriers, coherent loads, image ops, doorbell and
other non-pure ops, phis, preloads, and control flow.

Adds 10 HoistLoads unit tests in test-hoist-loads.cpp and capability-table
coverage in test-chip-caps.c. No new runtime switches or generation gates.

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 15:10
@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-27T15:10:29.972053Z
We couldn’t complete this review. Comment @codex review to try again.
53926df PR opened
🔒 Security Review ❌ Failed 2026-11-27T15:10:37.066201Z
We couldn’t complete this review. Comment @codex security review to try again.
53926df 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 53926df751cbf8c191aafcb2796b0b8172dc9c46

Blockers:

  1. Branch is DIRTY against aurora-wip (tip 7617c835): merge aurora-wip into the branch (merge, not rebase). Three conflicts, all mechanical:
    • src/asahi/lib/agx_chip_caps.h:173: bit 23 is now AGX_CAP_COMPUTE_SKIP_PRESSURE_SCHED, 24/25 are FOLD_SHARED_OFFSETS/CDM_DEFER_FLUSH, and AGX_CAPS_G13 already exists. Use 1u << 26 and add the cap to the existing AGX_CAPS_G13.
    • src/asahi/compiler/agx_compile.c:15409: agx_ilp_schedule moved out of the pressure-schedule block (it now runs under if (!(agx_compiler_debug & AGX_DBG_NOSCHED)) after the skip_sched block). Put the agx_hoist_loads call after that agx_ilp_schedule.
    • src/asahi/lib/tests/test-chip-caps.c: keep both test_g13_compute_codegen_caps()/test_cdm_defer_flush() and test_compute_hoist_loads(), and add the new bit to all_caps.
  2. src/asahi/compiler/agx_pressure_schedule.c:2370: hoist_can_cross() lets any non-coherent device_load cross threadgroup_barrier. On AGX2, barrier() with device/global memory semantics is emitted as memory_barrier followed by threadgroup_barrier (agx_compile.c nir_intrinsic_barrier, AGX2 path: agx_memory_barrier(b) then agx_threadgroup_barrier(b)). The pass stops at memory_barrier but crosses the rendezvous, so store buf[i]; memory_barrier; threadgroup_barrier; x = buf[j] becomes store; memory_barrier; x = buf[j]; threadgroup_barrier. The load now runs before the other invocations have reached the barrier, so it can read a value they have not stored yet. Only loads that nir_opt_access proved read-only (I->memory_can_reorder, now set on AGX2 too since #1232) may cross the rendezvous; #1232 uses exactly that rule to keep loads in flight across the barrier. Fix: in hoist_can_cross, return load->memory_can_reorder for AGX_OPCODE_THREADGROUP_BARRIER (or don't cross it at all), and change AboveAluAndThreadgroupBarrier to build a memory_can_reorder load. Demonstrated with the unit test below: it fails on the head merged into the tip.

Checked: Read the whole diff. Merged the head into aurora-wip 7617c83 with the conflicts resolved as in item 1, then ran the x86 drm-shim gate (meson test --suite asahi --suite poly): 337/338 with the PR's 10 HoistLoads tests passing. The one failure, hk_sync_stamps, fails identically on the tip without this PR. Added one probe test (below), which fails. Legality review: coherent, volatile and atomic loads are excluded via nir_is_coherent (good); device stores, atomics, memory/image barriers, doorbell, stack and control flow stop the walk (good); crossing local_load/local_store and non-overlapping immediate uniform_store is fine; the only unsafe crossing is the workgroup rendezvous in item 2. The cap is a chip-caps table entry, so no NO-GATES issue.

Non-blocking:

  • The A/B base (822cf58723a) predates AGX_CAP_COMPUTE_SKIP_PRESSURE_SCHED (G13 compute now skips the pressure scheduler that moved loads next to their uses) and #1232. The posted numbers (decode +0.06%/-0.19%, prefill -0.59%/-1.26%) are already neutral to negative, so please re-measure on the merged tip before landing. If there is still no win, the pass may not be worth carrying.
  • Rows needed again at the new head after the merge (G13X plus G13G; the cap is G13-only, so no other generation is needed).
Evidence

Gate (head merged into 7617c83, conflicts resolved as in blocker 1, plus one probe test):

Summary of Failures:
131/338 asahi - mesa:hk_sync_stamps   FAIL   (fails identically on the plain tip)
337/338 asahi - mesa:agx_tests        FAIL
[  FAILED  ] HoistLoads.ReviewProbeNotAboveDeviceScopedRendezvous

Probe test:

TEST_F(HoistLoads, ReviewProbeNotAboveDeviceScopedRendezvous)
{
   NEGCASE({
      agx_index off = agx_mov_imm(b, AGX_SIZE_32, 4);
      agx_index x = agx_mov_imm(b, AGX_SIZE_32, 0xcafe);
      agx_device_store(b, x, agx_uniform(0, AGX_SIZE_64), off, AGX_FORMAT_I32,
                       1, 0, false);
      agx_memory_barrier(b);
      agx_threadgroup_barrier(b);
      agx_index v = load(b, off, false);
      agx_unit_test(b, v);
   });
}

AGX2 barrier lowering on the tip (agx_compile.c, nir_intrinsic_barrier): modes & (nir_var_mem_global | nir_var_image) -> agx_memory_barrier(b); then for execution_scope >= SCOPE_WORKGROUP -> agx_threadgroup_barrier(b).

@RyanTheTide

Copy link
Copy Markdown

Merge-train note: #1224 (e0990ec5) has landed the agx_hoist_loads pass itself, plus test-hoist-loads.cpp, on aurora-wip. It's gated by AGX_CAP_COMPUTE_HOIST_LOAD_ADDRS and enabled on G14X only. This PR adds a second copy of the same pass behind its own AGX_CAP_COMPUTE_HOIST_LOADS, so a mechanical forward merge would leave two hoist passes.

The part that's unique here is enabling hoisting on G13G/G13X. Could you re-cut this PR on current aurora-wip as the G13 capability enable for the merged pass (add the G13 rows to AGX_CAP_COMPUTE_HOIST_LOAD_ADDRS, or split the address chain if G13 only wants the load hoist)? Please also attach the G13 decode/CTS rows you posted at that head. The merge train will run the combined A/B row and land it after that.

@joshuaswarren

Copy link
Copy Markdown
Author

Closing: #1224 already landed agx_hoist_loads and test-hoist-loads.cpp behind AGX_CAP_COMPUTE_HOIST_LOAD_ADDRS, #1297 showed enabling hoisting regresses G13G decode, and the standalone G13X rows above are decode-neutral (+0.06% / -0.19%) with a prefill regression (-0.59% / -1.26%). Will open a clean G13X capability PR for the combined AGX_CAP_COMPUTE_SMART_WAITS + AGX_CAP_COMPUTE_HOIST_LOAD_ADDRS pair once measured and CTS-checked on current aurora-wip.

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