Skip to content

agx/compiler: fix ILP scheduler fwd_cap PHI/net-growth accounting and collect in-flight tracking (+8.6% pp512) - #1287

Merged
RyanTheTide merged 2 commits into
aurora-wipfrom
satya/ilp-fwd-cap-phi-net-grow-collect
Nov 28, 2026
Merged

RyanTheTide merged 2 commits into
aurora-wipfrom
satya/ilp-fwd-cap-phi-net-grow-collect

Conversation

@satchlj

@satchlj satchlj commented Nov 28, 2026

Copy link
Copy Markdown
Member

What

Fix three scheduling accuracy bugs in ilp_schedule_block() (src/asahi/compiler/agx_pressure_schedule.c) that severely hurt cooperative-matrix compute loops such as llama.cpp mul_mm Q4_0 and Flash Attention:

  1. Exclude loop-carried PHI halves from consuming fwd_cap:
    Loop-header PHIs (e.g. 64 halves of matmad16 accumulators plus loop induction variables) are already live on block entry and cannot be reduced by intra-block scheduling. Charging them against AGX_ILP_FS_SAMPLE_HALVES (88 halves) immediately pushed live > fwd_cap before the first real instruction, forcing the scheduler into smallest-destination-first fallback and starving wide vec4 device_loads behind scalar ALU and local_stores.
  2. Use net register growth (dest halves minus dying SSA source halves) for fwd_cap fits and !fits ordering:
    Counting only destination width made accumulator-updating instructions like matmad16_agx3 (+16 dest halves, -16 to -32 dying input halves, net ≤ 0) look like the worst pressure offenders compared to local_load (+2/+4), starving all 8 matmad16 operations behind every local_load in the block.
  3. Treat AGX_OPCODE_COLLECT like AGX_OPCODE_SPLIT in in_flight load-consumption tracking:
    A collect that regroups split channels from hoisted loads does not consume those loads; advancing consumed_loads on collect caused all 32 local_loads in a cooperative-matrix K-tile loop to hoist ahead of all 8 matmad16 instructions and hold both K-halves live across the end-of-tile barrier.

Tested

Risk

  • Low. The three fixes strictly improve the accuracy of the existing forward register-pressure and in-flight load trackers in ilp_schedule_block() while preserving the exact final ilp_max_pressure() budget check and all 29 existing M5CopySchedule tests.

Not yet

  • Does not change the in-flight load window sizes (flat_windows) on M5 compute.

… collect in-flight tracking

Fix three scheduling accuracy bugs in ilp_schedule_block() that
severely hurt cooperative-matrix compute loops (such as llama.cpp
mul_mm Q4_0 and Flash Attention):

1. Exclude loop-carried PHI halves from consuming fwd_cap:
   Loop-header PHIs (e.g. 64 halves of matmad16 accumulators plus loop
   IVs) are already live on block entry and cannot be reduced by
   intra-block scheduling. Charging them against AGX_ILP_FS_SAMPLE_HALVES
   (88 halves) immediately pushed live > fwd_cap before the first real
   instruction, forcing the scheduler into smallest-destination-first
   mode and starving wide vec4 device_loads behind scalar ALU and
   local_stores.

2. Use net register growth (dest halves minus dying SSA source halves)
   for the fwd_cap fits check and !fits tie-breaker:
   Counting only destination size made accumulator-updating instructions
   like matmad16_agx3 (+16 dest, -16 to -32 dying inputs, net <= 0) look
   like the worst pressure offenders compared to local_loads (+2/+4),
   starving all matmad16 ops behind every local_load in the block.

3. Treat AGX_OPCODE_COLLECT like AGX_OPCODE_SPLIT for in-flight load
   consumption:
   A collect that regroups split channels from hoisted loads does not
   consume those loads; advancing consumed_loads on collect caused all
   32 local_loads in a cooperative-matrix K-tile loop to hoist ahead of
   all 8 matmad16 instructions and hold both K-halves live across the
   end-of-tile barrier.

On Apple M5 Max (J775c, G17C B1) with llama.cpp Vulkan (Llama-2-7B Q4_0,
fa=1, ngl=99, r=3), interleaved ABBA stacked on #1280+#1283+#1284+#1286:
- pp512:         2719.53 -> 2951.92 tok/s (+232.39 tok/s / +8.55%)
- pp2048:        2578.13 -> 2787.53 tok/s (+209.40 tok/s / +8.12%)
- tg128:         109.20  -> 109.20  tok/s (neutral)
- tg128 @ d2048: 76.12   -> 76.25   tok/s (+0.17%)
- mul_mm Q4_0 drops from 207 GPRs (512 threads) to 168 GPRs (640 threads),
  overlapping B-matrix device_loads with A dequant and interleaving all
  8 matmad16 ops with local_loads; Flash Attention drops 191 -> 175 GPRs
  (512 -> 576 threads).

Signed-off-by: Satya Benson <satya@benson.earth>
@satchlj
satchlj requested review from a team and DjDeveloperr as code owners November 28, 2026 03:32
@AxxzyWasTaken

Copy link
Copy Markdown

Code review notes at d60108813ab9 (specifically checking agx_pressure_schedule.c across AGX3B / G17P as well as AGX3C):

  1. COLLECT -> SPLIT (and nested COLLECT) leaks in_flight hoist-window slots (src/asahi/compiler/agx_pressure_schedule.c:1370-1391, 1538-1556):

    • agx_ilp_schedule() runs at agx_compile.c:15992, before agx_opt_collect_split() (agx_compile.c:16025), so load -> (split ->) collect -> split2 -> consumer chains are still present during scheduling.
    • collect sets collect_def[col] = collect while leaving load_node[col] = -1 (:1377-1388), and split2 only inspects load_node[src[0]] (:1370-1376), so neither load_node nor collect_def propagates to split2's destinations.
    • Both collect and split2 are skipped for consumed_loads (:1538-1539), so when consumer reads split2->dest[k], both load_node[v] == -1 and collect_def[v] == NULL (:1542-1546) and the underlying hoisted load is never marked consumed, permanently stealing a slot from consumed_loads + hoist_samples. The same happens for nested COLLECT -> COLLECT (:1380, :1548 only inspect load_node[] one level deep).
    • Propagating load_node[dest[d]] = load_node[collect_def[src]->src[d]] in the AGX_OPCODE_SPLIT case (and resolving collect_def transitively on COLLECT inputs) avoids freezing the hoist window.
  2. eff_fwd_cap accounting (src/asahi/compiler/agx_pressure_schedule.c:1333-1338):

    • Dead PHIs (uses_left[v] == 0 && dies[v]) still add their full width to eff_fwd_cap even though they contribute 0 net live halves (:1523 + :1531-1532).
    • :1335 matches every AGX_OPCODE_PHI regardless of whether the block is a loop header, and eff_fwd_cap = 88 + L_phi is unclamped. On AGX3B / G17P (192-half register file), lifting fwd_cap on non-loop if/else merge PHIs removes the brake near AGX3B's 176/192-half compute spill cliff (:2326-2355, where occ_cap is false for windows <= 4) and non-loop fragment +16-half slack floor (:2444-2486). Restricting to live loop-header PHIs (or clamping eff_fwd_cap against the block's register headroom) and checking AGX3B shaderdb spill/GPR counts would close that exposure.
  3. Test coverage: test-m5-copy-schedule.cpp covers fixes agx: introduce agx3b support & HAL200 #1 and nir/range_analysis: allow the 32-bit shift count of a narrow ishl #3, but does not yet include a unit test for fix agx: fix the -Werror build of the AGX drivers #2 (net register growth dest - dying_srcs / !fits tie-breaking).

…cap to live loop PHIs

Address review feedback on ilp_schedule_block():

1. Propagate load_node and collect_def across COLLECT->SPLIT (per-channel
   when widths/counts match, whole-collect fallback otherwise) and nested
   COLLECT->COLLECT chains, and resolve collect_def recursively in
   ilp_mark_consumed_ssa() so in-flight hoist-window slots cannot leak
   when scheduling runs before agx_opt_collect_split().

2. Compute eff_fwd_cap after uses_left[] and dies[] (including trailing
   control flow) are complete, and only add live loop-header PHIs
   (block->loop_header && (uses_left[v] > 0 || !dies[v])). Dead PHIs and
   non-loop if/else merge PHIs keep the original fwd_cap brake intact.

3. Add unit tests in test-m5-copy-schedule.cpp for net-growth priority
   under fwd_cap, COLLECT->SPLIT / nested COLLECT load consumption, and
   non-loop merge PHI fwd_cap preservation.

Signed-off-by: Satya Benson <satya@benson.earth>
@satchlj

satchlj commented Nov 28, 2026

Copy link
Copy Markdown
Member Author

@AxxzyWasTaken Thanks for the thorough AGX3B/AGX3C review! All three points are addressed in follow-up commit bbfd2814c74a:

  1. Transitive COLLECT -> SPLIT and nested COLLECT load consumption: added ilp_mark_consumed_ssa() to walk collect_def recursively, propagated load_node / collect_def per-channel across width-matching COLLECT -> SPLIT (with whole-collect fallback for bit-reinterpreting splits), and propagated collect_def across nested COLLECT -> COLLECT.
  2. eff_fwd_cap scoped strictly to live loop-header PHIs: now computed after uses_left[] and dies[] (including trailing control flow) are complete, gated on block->loop_header && (uses_left[v] > 0 || !dies[v]). Dead PHIs and non-loop if/else merge PHIs keep the original fwd_cap brake untouched.
  3. Unit tests: added NetGrowthPrefersRegisterReleasingOpOverGrowingLoad, CollectSplitAndNestedCollectConsumeHoistedLoads (collect-to-split and nested-collect), and NonLoopMergePhisKeepOriginalFwdCap in test-m5-copy-schedule.cpp.

Exact-head bbfd2814c74a verification (plain aurora-wip, unstacked, Apple M5 Max J775c / G17C B1):

  • meson test -C build --suite asahi: 337/337 PASS (34/34 M5CopySchedule.*).
  • libvulkan_asahi.so SHA-256: 905a6bbc33038e6410ba9bb4fd7dcb0c6a2e3630159f282f64e70023776e85dc.
  • dEQP-VK.compute.pipeline.basic.*: 80/80 supported Passed (1 Not supported, 0 Failed).
  • Standalone llama-bench (Llama-2-7B Q4_0, fa=1, ngl=99, r=3, DRIRC_CONFIGDIR=/usr/share/drirc.d): pp512 = 2515.79 tok/s (+9.7% vs 2292.66), pp2048 = 2391.63 tok/s (+9.2% vs 2190.33), tg128 = 108.42 tok/s, tg128 @ d2048 = 75.68 tok/s.
  • dmesg: 0 GPU/MMU/asahi faults.

@aurora-silicon-review aurora-silicon-review Bot 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 bbfd2814

Blockers: none.

Checked: no Apple material, no AI attribution, no chip/board/firmware gates, human Signed-off-by, full diff of src/asahi/compiler/agx_pressure_schedule.c and test-m5-copy-schedule.cpp, loop-header PHI scoping (block->loop_header), net register growth with repeated-source deduplication vs best_dest_grow live tracking, acyclic COLLECT/SPLIT consumption tracking (ilp_mark_consumed_ssa), 337/337 asahi unit tests, CTS compute.pipeline.basic.* 80/80 PASS, J775c ABBA llama-bench (+8.55% pp512, +8.12% pp2048).

Evidence
  • Inspected all changes in src/asahi/compiler/agx_pressure_schedule.c:
    • eff_fwd_cap adds only live loop-header PHIs (block->loop_header && (uses_left[v] > 0 || !dies[v])), keeping non-loop merge PHIs under the original fwd_cap brake (verified by NonLoopMergePhisKeepOriginalFwdCap).
    • Net register growth (grow = dest_grow - dying_unique_srcs) deduplicates repeated SSA sources (seen_before, uses_left[v] == occ) for fits and tie-breaking while live still advances by best_dest_grow before per-source decrement.
    • collect_def and ilp_mark_consumed_ssa() walk intra-block SSA DAG COLLECT/SPLIT chains accurately (including exact-channel COLLECT -> SPLIT and nested COLLECT -> COLLECT) without prematurely advancing consumed_loads.
  • Verified on J775c M5 Max (G17C B1): 337/337 asahi tests, CTS compute.pipeline.basic.* 80/80 PASS, ABBA pp512 +8.55% / pp2048 +8.12% (standalone +9.8% pp512).

@RyanTheTide

Copy link
Copy Markdown

@DjDeveloperr, code-owner heads-up: this PR has an aurora-silicon-review App approval at bbfd2814c74a from a different lane plus the author's exact-head hardware row. I plan to land it no earlier than 06:29Z, after a combined A/B regression row on two lab GPU families (M1 Pro G13S + M4 G16G). The App approval doesn't count toward the ruleset, so that merge uses --admin under the 6-point rule. Please post a review here if you want it held.

@RyanTheTide

Copy link
Copy Markdown

Merge-train landing at bbfd2814c74a.

  • Review: aurora-silicon-review App APPROVE at this head by NEON-PR-REVIEW (Gemini lane, different model family from the author), with the author's exact-head J775c (M5 Max, G17C) row (non-author).
  • Feature row: the author's exact-head hardware row on this PR.
  • Regression: combined A/B overlay of aurora-wip plus this PR and the other queued PRs, user space, default kernel command line. M1 Pro (G13S, fleet89): 0 status changes. M4 Pro (G16S, fleet89): 0 regressions. The only flips there are 5 synchronization multi_queue timeouts under 4-job load; all 5 pass 3/3 alone on both arms, and the base arm shows the same flake. Each Mac ran 26,595 dEQP-VK + 8,153 dEQP-GLES cases, with 0 GPU firmware crashes, Enforcing, 0 failed units. Evidence traces/gpu/20261128-mergetrain-mesa.
  • --admin under the 6-point rule: the App approval does not count toward the ruleset. Dj was pinged here more than 1 h ago, and no blocking review is open.

Revert: git revert -m 1 <merge commit> on aurora-wip. A regression gets reverted first and fixed after.

@RyanTheTide
RyanTheTide merged commit 7d6efec into aurora-wip Nov 28, 2026
@RyanTheTide
RyanTheTide deleted the satya/ilp-fwd-cap-phi-net-grow-collect branch November 28, 2026 06:33
jgoyvaerts pushed a commit to jgoyvaerts/mesa that referenced this pull request Nov 28, 2026
… with occupancy headroom

Extend the wide_flat ({8, 6, 4, 2, 1}) in-flight load window selection
in agx_ilp_schedule() to M5 (AGX3C, non-G18G) compute loop bodies when
device_loads > 4 and the block has register headroom inside its current
occupancy step (device_load_cap > peak).

Because windows > 4 remain strictly capped to device_load_cap - peak
(occ_cap), this never crosses an occupancy boundary and falls back
cleanly to {6, 4, 2, 1} when headroom is tight. Straight-line compute
blocks and G18G are unchanged.

In memory-latency-bound quantized GEMV loops (such as llama.cpp
mul_mat_vec Q4_0 / Q6_K with 10-14 device_loads per loop iteration),
widening the in-flight window from 4 to 6/8 overlaps trailing activation
and weight loads with dequantization ALU instead of stalling shortly
before consumption.

On Apple M5 Max (J775c, G17C B1) with llama.cpp Vulkan (Llama-2-7B Q4_0,
fa=1, ngl=99, r=3), interleaved ABBA stacked on aurora-silicon#1280+aurora-silicon#1283+aurora-silicon#1284+aurora-silicon#1286+aurora-silicon#1287:
- tg128:         109.17 -> 111.34 tok/s (+2.17 tok/s / +1.99%)
- tg128 @ d2048: 76.30  -> 77.13  tok/s (+0.83 tok/s / +1.09%)
- pp512:         2953.41 -> 2953.88 tok/s (neutral)
- pp2048:        2785.65 -> 2787.48 tok/s (+0.07%)

Signed-off-by: Satya Benson <satya@benson.earth>
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.

3 participants