Repository navigation
borealis: reduce ordered integer tiles with subgroup shuffles - #1374
Conversation
Match compute shaders that reduce a 16x16 tile of integer channels through shared memory in a fixed order, and rewrite the reduction to relative subgroup shuffles on full 32-lane subgroups. The pass runs on AGX3C, where the relative shuffles encode natively, and only on shaders whose effects outside the tree are per-invocation reads and the guarded image stores. Signed-off-by: DjDeveloperr <hey@djdev.me>
There was a problem hiding this comment.
Verdict: MERGE at 09c4d9e1
Blockers:
- None.
Checked:
- All 5 changed files (
src/asahi/vulkan/hk_nir_opt_ordered_tile_reduction.{c,h},src/asahi/vulkan/hk_shader.c,src/asahi/vulkan/meson.build,src/asahi/vulkan/tests/test-hk-ordered-tile-reduction.c). - Verified the pass is strictly gated in
hk_preprocess_nir_internal()onAGX_ISA_ENCODING_AGX3C(M5 family only; G15/G16/G17P are AGX3B and G18G is AGX3D), fixed 32-lane subgroups, compute stage, andVK_SUBGROUP_FEATURE_SHUFFLE_RELATIVE_BIT, running before AGX subgroup lowering andnir_lower_compute_system_values(). - Verified the matcher rejects loops, jumps, calls, atomics, SSBOs, non-
txf/sparse textures, derivatives, pre-existing subgroup ops, volatile/coherent/explicit-layout/zero-init shared memory, pointer escapes, partial initialization, reversed operands, or mismatched guards/barriers. - Verified the
32x8x1launch permutation (residue = (group << 2) | (lane & 3),logical = ((lane >> 2) << 5) | residue) maps all 256 invocations bijectively to16x16x1, preserves exact lower-lane-first reduction order across stages128, 64, 32(intra-subgroupshuffle_xor16/8/4), singleACQ_RELbarrier onuvec3[32]partials, and stages16, 8, 4, 2, 1in subgroup 0, and preservesvkCmdDispatchBaseviaload_base_workgroup_id(b, 32). - Verified
test-hk-ordered-tile-reduction.ccovers 1 positive and 16 negative variants plus a 256-lane lockstep NIR interpreter comparing stock vs lowered output, and verified J714s M5 Pro (G17S) hardware evidence (Lavapipe byte-for-byte match at47x33and3024x1964, dispatch271.5 -> 125.1 µs, GTA V Enhanced+0.95 FPS, 0 GPU faults).
Evidence
- Stage permutation checked mathematically:
lane >> 2(bits 2..4 oflane) maps directly to bits 5..7 oflogical, soshuffle_xor16/8/4 flips bits 7/6/5 (+128/+64/+32for the active lower half), leaving lanes0..3of each subgroup holding the exact stage-2 results forlogical == residue(0..31), which subgroup 0 then reduces viashuffle_xor16/8/4/2/1 to logical lane 0. nir_opt_dce,nir_opt_dead_cf,nir_remove_dead_variables(nir, nir_var_mem_shared, NULL), andnir_shader_gather_info()cleanly drop the originalpairs/sumsarrays and update shader metadata before backend lowering.
|
@DjDeveloperr, code-owner heads-up: App approval at |
eval_local() walked an ALU instruction's sources one scalar at a time before it looked at the opcode. A stage guard written as a vector all() lowers to ball_iequal2, whose sources are two-component vectors, so nir_scalar_chase_alu_src() asserted in builds with assertions enabled. Reject opcodes the evaluator does not handle before walking any source. Every opcode it handles works per component. Shaders it accepted before are evaluated exactly as before. Add a host test case with the span-8 guard written as all(uvec2(index >> 3, 0) == uvec2(0, 0)); the pass must leave that shader unchanged. Signed-off-by: DjDeveloperr <hey@djdev.me>
There was a problem hiding this comment.
Verdict: HOLD at 02474d6f for the current-head hardware evidence.
Checked: The follow-up rejects unsupported opcodes before scalar source traversal. The vector-all stage-guard test exercises that decision. The previous per-component integer operations remain eligible, and the owner reports all 18 debug host cases passed. Keep the J714s regression and the affected family coverage or maintainer disposition. The earlier GTA and reference readback used 09c4d9e1.
|
Row at
|
|
MacBook Neo (J700, A18 Pro, G17P B1), stock Fedora 44 on
All 14,261 statuses matched. Under Meson Zero GPU faults, kernel WARNs or AVCs. Same stock boot, with SLC enabled on four planes. |
|
Verdict: MERGE at Blockers:
Checked:
A base M5 (G17G) row is still owed after the merge. Evidence
|
What: Add a NIR pass that rewrites an ordered 16x16 integer tile reduction (min, max and sum through shared memory in eight barrier-separated stages) into relative subgroup shuffles on full 32-lane subgroups, in the same reduction order. GTA V Enhanced runs this reduction every frame; on the M5 Pro its dispatch drops from 271.5 µs to 125.1 µs.
Tested: M5 Pro (J714s, G17S), GTA V Enhanced built-in benchmark at 3024x1964: dropping only this pass from the measured stage stack costs 0.95 FPS (70.78 → 69.83, 1.4 %), 0 GPU faults, 0 s at the 338 MHz clamp. J714s at
09c4d9e10d8a: GTA V's original compute module matches the Lavapipe reference byte for byte at 47x33 and 3024x1964 on two inputs, with the pass active (125 instructions, 124.7 µs per dispatch). On the measured stack the same module also matched with the pass off (187 instructions, 271.5 µs). Includes the host test test-hk-ordered-tile-reduction. At02474d6fb0d2(opcode check before walking sources): the host test passes all 18 cases in an x86 debugoptimized build with assertions on; its new vector all() stage-guard case hits thenir_scalar_chase_alu_srcassertion at09c4d9e10d8a. Rows at02474d6fb0d2: on J714s, GTA V's module matches Lavapipe in 8 of 8 readbacks with the pass active, and the GTA V benchmark runs at 67.81 FPS with 0 GPU faults. On J700 (G17P), all 14,261 subgroup-shuffle and compute CTS statuses match the base, the readback matches Lavapipe, and the binaries are identical (the pass stays off there).Risk: two-way · M5 family only (AGX3C: G17G, G17S, G17C); other Macs are untouched.
Not yet: a base M5 (G17G) row.
Details
The pass runs before AGX subgroup lowering, only on AGX3C compute shaders with a fixed 32-wide subgroup and relative shuffle support. It matches a fixed logical 16x16x1 workgroup with two plain shared arrays (an interleaved pair and a scalar), eight exactly guarded stages, nine workgroup barriers and output from logical lane zero. Anything else outside the tree (atomics, other subgroup operations, derivatives, calls, pointer escapes) makes it decline.
On a match the shader launches as 32x8x1 with the shader-visible IDs translated back to 16x16x1. The first three stages become XOR shuffles inside each subgroup, one value per lane goes through the first barrier, and subgroup zero finishes the last five stages. Every step keeps the original lower-lane-first operand order, so the result is the same integer tree.
Evidence
GTA V benchmark on J714s, kernel 7.3.0-m5-pr67, SLC on, Borealis built natively on the M5, base
aurora-wip152e449, same settings hash for every run. The stage stack is #1205, a forced-headless WSI change used by the benchmark harness, this pass, and two smaller compute passes.Full-stack pair ratios 1.056–1.064. Every run: 0 new GPU faults, 0 s at 338 MHz, TCMb 68–70 °C at launch.
Dispatch time of the original module at 3024x1964, 6 bursts of 96 dispatches per run, ABBA: pass off 271.6 / 271.3 µs, pass on 125.1 / 125.1 µs.