Repository navigation
agx: interleave provably disjoint shared-memory stores before workgroup barriers - #1560
IronsideXXVI wants to merge 6 commits into
Conversation
…up barriers In tiled compute loops (such as quantized GEMM) that stage an A tile and a B tile into disjoint regions of threadgroup shared memory before a workgroup barrier, the B tile global loads and store_shared ops sit at the end of a long basic block behind the A tile unpack chain. Add agx_nir_interleave_disjoint_shared_stores(), gated to AGX3B compute shaders with shared_size > 0, to: - Scan loop basic blocks up to a workgroup barrier and bail if any side-effecting, shared-read/atomic, global-store/atomic, or volatile op appears before the barrier. - Bound each store_shared byte range via conservative unsigned [lo, hi] interval analysis (u32_interval) and identify a suffix of B-stores whose byte ranges are provably disjoint from all prefix A-stores. - Move each suffix store and its bounded, movable read-only global-load closure (up to 24 instructions) earlier after prefix store anchors, stopping immediately on any unmovable suffix store so later suffix stores never overtake an earlier suffix store. Add unit test SharedByteOffsets.InterleaveDisjointSharedStores covering disjoint interleaving, overlapping shared ranges, and the non-overtaking guard. Signed-off-by: Dylan Ironside <dylan@dylanironside.io>
Three small fixes to agx_nir_interleave_disjoint_shared_stores(): - store_shared_byte_range() ignored ACCESS_VOLATILE, so a volatile store could move past others. Refuse it, as for every other volatile access. - A negative base wraps byte_lo above byte_hi when the index may be 0, and the overlapping range then passes the disjointness test. Refuse it. - The pass overwrote instr->index with block-local numbers and then reported no progress with all metadata preserved, so a valid instr_index became wrong (nir_validate asserts). Require the metadata instead and invalidate it when instructions move. Add tests for the three cases. Signed-off-by: DjDeveloperr <hey@djdev.me>
DjDeveloperr
left a comment
There was a problem hiding this comment.
HOLD at dd59e17ad0d2: no hardware row or standalone A/B at the head. No blocking finding left in the code.
I pushed dd59e17ad0d2 on top of your 847e6c66305a (one added commit, nothing rewritten) with three small fixes and tests:
- a volatile
store_sharedwas still moved (store_shared_byte_range()never looked atACCESS_VOLATILE); - a negative
basewrapsbyte_loabovebyte_hi, and the overlapping range then counts as disjoint; - the pass wrote block-local numbers into
instr->indexand then reported no progress with all metadata kept, sonir_validateaborts withinstr->index > indexwheninstr_indexwas valid. It now requires that metadata and drops it when something moves.
The new tests fail on 847e6c66305a and pass now.
Checked and fine
- Disjointness is tested for every A/B pair at each split, suffix stores keep their order, and a suffix store that cannot move stops the rest. The intervals fall back to the full range on every possible wrap.
- Reordering two disjoint plain stores of one invocation before the workgroup barrier is allowed; shared loads, shared atomics, memory-only barriers, global stores and textures before the barrier all make the block bail. The moved loads are non-writeable, non-volatile, stay in their block and cross no barrier.
- The GEMM matcher and the shared vec2/phase passes that run later do not depend on the order of these stores.
Question (not blocking unless it can happen). The range uses base + index * size without a wrap. agx_nir_opt_shared_vec2.c proves its addresses stay inside shared_size before relying on that; here a B store with an unbounded index and a non-zero base still counts as "above" A. Refusing ranges with byte_hi >= shared_size would close it, if it does not stop the q6_K case from firing.
Not blocking
- By this point
agx_nir_lower_addresshas turned global loads intoload_agx/load_constant_agx.load_constant_agxis movable but does not setsaw_global_load, so tiles loaded from constant buffers never qualify. The tests useload_global_constant, which the pass will not see in a real compile; aload_agxcase withACCESS_NON_WRITEABLEwould cover the real path. - AGX3B here is M3, the M4 family and A18 Pro, not only M4. Please widen the Risk line, and say why AGX3C is left out.
- The limits (24 stores, 5 A stores,
nb < na, 24 instructions) need one line each on why. The closure walk could stop at 24 instead of collecting everything first. u32_interval()andu32_upper_bound()in #1559 are the same walk twice; one shared helper would do. The two PRs conflict at the end oftest-shared-byte-offsets.cpp.
Performance claim. The tok/s figures are from a combined build, and instruction distance is not speed. This pass exists only for performance and the backend scheduler may reorder around it, so it needs its own base/head A/B on one boot (policy/mesa-performance-claims.md).
Local check. dd59e17ad0d2 merged into the current tip e4346bdb3a55 (tree 73c5a9cfa401), assert-enabled x86-64: asahi, poly and vulkan suites 464 ok, 0 failed, 1 skipped; agx_tests 1973 passed, 1 skipped. (Your head on 3146ce4726a1 gave 463 ok and one borealis-sync-runner failure; that is a host timing flake, three reruns alone passed.)
Rows owed at dd59e17ad0d2
- J773g (M4, G16G B0) or your J604: llama-bench Q4_K_M and Q6_K
pp512/tg32, base vs head ABBA, 0 faults, plus gpubenchcompute.*with CRC match anddEQP-VK.compute.*shared*,dEQP-VK.memory_model.*workgroup*, 0 regressions. Expect a measurablepp512gain; at parity there is no reason to carry the pass. - J700 (A18 Pro, G17P, also AGX3B): the same CTS subset and gpubench
compute.*, no regression. Required as the second generation. - M3 (J516s is down): same as J700 when available. Not required.
There was a problem hiding this comment.
Verdict: MERGE at dd59e17
Checked: The exact-head pass remains limited to AGX3B compute. It moves only within the prefix of the first workgroup barrier. Every moved store's byte range is disjoint from the crossed stores, and a rejected suffix store stops later moves. I checked the unsigned interval multiplication and shift against NIR's opcode semantics. The follow-up commit rejects volatile stores and negative bases and preserves valid instruction-index metadata when there is no move. The maintainer reports the new focused cases pass at this head.
Follow-ups: If this grows further, please split the interval analysis from the instruction scheduling so each can be reviewed independently.
…ound closure walk Follow-up polish on agx_nir_interleave_disjoint_shared_stores(): - Reject store_shared byte ranges with byte_hi >= shader->info.shared_size so an unbounded index with a non-zero base cannot wrap and falsely pass the disjointness test. - Recognize nir_intrinsic_load_constant_agx as a global/constant tile load for saw_global_load after agx_nir_lower_address. - Bail early from collect_shared_store_closure() once the 24-instruction cap is reached instead of traversing an unbounded dependency DAG first. - Document the rationale for each heuristic cap (24 stores, split >= 5, nb < na, 24 closure instructions). - Add SharedByteOffsets.InterleaveSharedStoresLoadAgxAndBounds covering read-only load_agx / load_constant_agx, writable load_agx rejection, and byte_hi >= shared_size rejection. Signed-off-by: Dylan Ironside <dylan@dylanironside.io>
|
Thanks for catching and fixing the volatile store, negative-base wrap, and Pushed forward commit
|
J700 (MacBook Neo, A18 Pro, G17P B1): INCOMPLETE. Tested head
|
DjDeveloperr
left a comment
There was a problem hiding this comment.
Verdict: MERGE at 5517fb46fea2 once the rows below pass. No blocking finding in the new commit.
Checked (what changed since dd59e17ad0d2)
byte_hi >= shared_size:shared_sizeis in bytes and so is the range (base + index * element size). Zero-init, the mesh lowerings and the index-sort pass change it earlier, and they only grow it. The only pass that shrinks it is the GEMM matcher, which runs after this one. Variable workgroup memory lies pastshared_size, so it is refused, and with a variable workgroup size the invocation ids have no bound, so those blocks bail. The check can only refuse more than before.- Early bail at 24: same limit as before (the 25th instruction refuses). Nothing is moved until the whole closure is collected, so no half-moved closure is possible. Stores moved by earlier rounds stay valid and progress is still reported.
load_constant_agx/load_agx:load_constant_agxwas already movable (reorderable in NIR) and now also counts as the tile load.load_agxstill needs non-writeable and non-volatile. Loads only move up inside one block before the barrier, and any global store, atomic, shared load or other barrier there makes the block bail, so no aliasing write is crossed.- Tests: the new test fails with the bound removed and with the
load_constant_agxline removed. The cap changes no result, so it has no test.
Local check: merge with aurora-wip e4346bdb3a55 (tree 7e3543b88540), asserts on, x86-64: asahi, poly and vulkan suites 463 ok, 0 failed, 1 skipped, 1 timeout (borealis-sync-runner on a loaded host; 3 of 3 alone passed). agx_tests 1974 passed, 1 skipped; *Interleave* 17/17, with *SharedByteOffsets* 58/58. It also merges cleanly with 4f2853ac8250.
Generated code, dd59e17ad0d2 against 5517fb46fea2: identical, every packed compute part, offline on the drm-shim (nothing executes there).
| Set | G16G | G17P | G15S |
|---|---|---|---|
| llama.cpp MUL_MAT q4_K/q6_K, soft faults on | 70 | 70 | 35 |
| same, soft faults off | 70 | 70 | |
same, off and norobust |
68 | ||
| llama-bench, TinyLlama Q4_K_M | 40 | 40 | |
| GTA V compute pipelines | 481 | 481 | 242 |
The exact head also gives the same MUL_MAT code as the J700 kit's own dd59e17ad0d2 dump, in all three modes. This is not an empty result: with the pass removed, 1 MUL_MAT part differs on G16G (both modes) and G17P (soft faults on, and norobust), and 2 llama-bench parts differ on both chips. GTA V and G15S MUL_MAT never use the pass.
So the J700 MUL_MAT result at dd59e17ad0d2, including the norobust runs of the changed shader, carries over to this head. CTS and gpubench shaders were not in this compare.
Rows left
- J773g (or your J604) at
5517fb46fea2: llama-bench Q4_K_M and Q6_Kpp512/tg32, base/head ABBA, 0 faults, and a measurable gain; gpubenchcompute.*with CRC match;dEQP-VK.compute.*shared*anddEQP-VK.memory_model.*workgroup*. - J700: only the code-hash step at
5517fb46fea2over the same CTS and gpubench lists. If the hashes equal the recorded ones, the 9,311 CTS cases and the gpubench runs atdd59e17ad0d2count. If not, rerun those lists. - M3: optional.
Nit: the > 24 test after the walk can no longer be true.
J700 (MacBook Neo, A18 Pro, G17P B1): code-hash probe at
|
| Set | dd59e17ad0d2 |
5517fb46fea2 |
Differences |
|---|---|---|---|
| CTS list of the earlier row (9,311 cases; 4,844 compile at least one shader) | 18,930 parts, 1,624 distinct | 18,930 parts, 1,624 distinct | 0 |
| GPUBench, 20 configurations | 58 parts | 58 parts | 0 |
0 GPU faults or recoveries, 0 kernel WARNINGs, SLC on, stock handback verified.
So the earlier J700 CTS (9,311 cases, 0 status changes) and GPUBench results apply to 5517fb46fea2, on the same configuration and case lists. That matches the review's check on the shaders it looked at.
Not covered: the run-time internal shaders the driver builds itself aren't in that dump, so they aren't compared. On this stock kernel the pass isn't exercised by default (see the earlier comment), so this is a no-regression result and not proof of the changed path.
|
J773g (Apple M4, Verdict: correctness PASS, standalone perf gate NOT MET (no measurable gain).
Note: no standalone Q6_K GGUF was staged on J773g, so Q6_K was measured directly via |
|
Where this stands at To land it, please post a standalone J604 ABBA at this exact head (Q6_K |
…-shared-stores Resolve the adjacent end-of-file test addition in src/asahi/compiler/test/test-shared-byte-offsets.cpp after PR #1559 merged into aurora-wip, keeping both test suites intact. Signed-off-by: Dylan Ironside <dylan@dylanironside.io>
|
Forward-merged Verified offline at
Standalone J604 A/B numbers on top of current |
DjDeveloperr
left a comment
There was a problem hiding this comment.
Reviewed 33be469f2dae. Its parents are the previously approved 5517fb46fea2 and 4238a66de5585. The same four files have identical added and removed lines against their respective bases. The base merge preserves the shared-store pass and tests.
The measured performance gate is still open. The standalone J773g run showed no gain. A J604 base/head ABBA showing a gain outside run-to-run variance, or evidence that the pass pays in a combined change, remains the next step.
|
Merge readiness at
Areas: Updated by pr-readiness on pushes, edits, reviews and comments, and on a schedule. Rules: merge policy · labels. |
J516s (MacBook Pro M3 Pro, G15S) CTS row at
|
…-shared-stores Resolve the adjacent end-of-file test addition in src/asahi/compiler/test/test-shared-byte-offsets.cpp after PR #1626 merged into aurora-wip, keeping both test suites intact. The compiler diff against aurora-wip is unchanged. Signed-off-by: Dylan Ironside <dylan@dylanironside.io>
|
Forward-merged The 4-file diff against Verified offline at exact head
|
…-shared-stores Resolve the adjacent end-of-file test addition in src/asahi/compiler/test/test-shared-byte-offsets.cpp after PR #1629 merged into aurora-wip, keeping both test suites intact. The compiler diff against aurora-wip is unchanged. Signed-off-by: Dylan Ironside <dylan@dylanironside.io>
|
Forward-merged The 4-file diff against Verified offline at exact head
|
DjDeveloperr
left a comment
There was a problem hiding this comment.
The forward merge retains both sets of unit tests. The added and removed lines in all four files match the previously reviewed change. The pass keeps the relative order of potentially overlapping stores and stops at the barrier. On aurora-wip 0b69db0 plus this head, agx_tests passed 2,023 cases with one skip. The standalone J773g comparison showed no gain. Please post the J604 ABBA result against current aurora-wip, or a combined comparison that isolates this pass's contribution.
What: Add
agx_nir_interleave_disjoint_shared_stores(), gated to AGX3B compute shaders withshared_size > 0, to interleave provably disjoint suffixstore_sharedoperations (and their bounded read-only global/constant-load closures) earlier ahead of workgroup barriers inside loop blocks. Uses conservative unsigned[lo, hi]interval analysis (u32_interval()) to bound each non-volatile, non-negative-basestore_sharedbyte range strictly within[0, shared_size), bails if any side-effecting, shared-read/atomic, global-store/atomic, or volatile op precedes the workgroup barrier, stops immediately if any suffix store cannot be moved so later suffix stores never overtake an earlier suffix store, and maintainsnir_metadata_instr_indexvalidity cleanly.Tested:
origin/aurora-wip(cb983a61cb1, head5517fb46fea,gcc 16.1.1,aarch64,-Dbuildtype=release -Db_ndebug=false):libvulkan_asahi.sobuilt with0warnings in touched files;meson test -t 4 --suite asahi398 Ok / 0 Fail / 1 Skipped;agx_tests1972 passed / 0 failed / 1 skipped (17/17*Interleave*, includingSharedByteOffsets.InterleaveDisjointSharedStores,InterleaveSharedStoresVolatileAndNegativeBase, andInterleaveSharedStoresLoadAgxAndBounds).llama-benchpipelines showed[3] q6_KB0/B1tile loads/stores interleaved101-134instructions earlier before the workgroup barrier (0:0spills across all 16 pipelines). (Earlier J604llama-benchnumbers came from a combined build and are not standalone proof for this pass.)Risk: two-way door · AGX3B compute compiler only (M3, M4 family, and A18 Pro) — strictly gated to
is_agx3b && mesa_shader_stage_is_compute && shared_size > 0. AGX3C (M5) is intentionally left out because it uses a separate backend/scheduling profile and has not been benchmarked with this pass. Only moves non-volatilestore_sharedops with non-negative base whose[byte_lo, byte_hi]intervals stay insideshared_sizeand are provably disjoint from all prefixstore_sharedops, only moves read-only non-volatile load closures inside the same basic block before a workgroup barrier, bails on any side-effecting op before the barrier, and preserves relative suffix store ordering.Not yet:
aurora-wip(5517fb46fea) hardware A/B on J604 / M4 (llama-benchQ4_K_M and Q6_Kpp512/tg32base vs head ABBA on one boot,0faults,gpubench compute.*CRC match,dEQP-VK.compute.*shared*,dEQP-VK.memory_model.*workgroup*).gpubench compute.*and the same CTS subset).Offline & unit test details
origin/aurora-wip(cb983a61cb1, standalone, zero stacking dependencies)5517fb46fea29f9ba544b53bf5836705fe5afd45(includesdd59e17ad0d2)src/asahi/compiler/agx_nir.h,src/asahi/compiler/agx_compile.c,src/asahi/compiler/agx_nir_lower_shared_bitsize.c,src/asahi/compiler/test/test-shared-byte-offsets.cppSharedByteOffsets.InterleaveDisjointSharedStores: disjoint B-store interleaving ahead of the last A-store, rejection on overlapping shared ranges, non-overtaking guard, and metadata preservation on no-progress.SharedByteOffsets.InterleaveSharedStoresVolatileAndNegativeBase: rejects volatilestore_sharedand negative base offsets.SharedByteOffsets.InterleaveSharedStoresLoadAgxAndBounds: covers real post-agx_nir_lower_addressread-onlyload_agx(ACCESS_NON_WRITEABLE) andload_constant_agx, rejects writableload_agx, and rejects unbounded indices whosebyte_hi >= shared_size.