Skip to content

borealis: reduce ordered integer tiles with subgroup shuffles - #1374

Merged
DjDeveloperr merged 2 commits into
aurora-wipfrom
perf/borealis-ordered-tile-reduction
Nov 30, 2026
Merged

DjDeveloperr merged 2 commits into
aurora-wipfrom
perf/borealis-ordered-tile-reduction

Conversation

@DjDeveloperr

@DjDeveloperr DjDeveloperr commented Nov 30, 2026 •

Copy link
Copy Markdown

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. At 02474d6fb0d2 (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 the nir_scalar_chase_alu_src assertion at 09c4d9e10d8a. Rows at 02474d6fb0d2: 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-wip 152e449, 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.

Arm Runs Five-pass mean FPS
aurora-wip 5 (ABBA, BAAB, +1) 66.88 (66.73–67.06)
full stage stack 4 70.78 (70.50–71.02)
stack without this pass 1 scored + 1 warm 69.83 (warm 69.90)

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.

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>
@DjDeveloperr
DjDeveloperr requested review from a team as code owners November 30, 2026 09:52

@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 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() on AGX_ISA_ENCODING_AGX3C (M5 family only; G15/G16/G17P are AGX3B and G18G is AGX3D), fixed 32-lane subgroups, compute stage, and VK_SUBGROUP_FEATURE_SHUFFLE_RELATIVE_BIT, running before AGX subgroup lowering and nir_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 32x8x1 launch permutation (residue = (group << 2) | (lane & 3), logical = ((lane >> 2) << 5) | residue) maps all 256 invocations bijectively to 16x16x1, preserves exact lower-lane-first reduction order across stages 128, 64, 32 (intra-subgroup shuffle_xor 16/8/4), single ACQ_REL barrier on uvec3[32] partials, and stages 16, 8, 4, 2, 1 in subgroup 0, and preserves vkCmdDispatchBase via load_base_workgroup_id(b, 32).
  • Verified test-hk-ordered-tile-reduction.c covers 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 at 47x33 and 3024x1964, dispatch 271.5 -> 125.1 µs, GTA V Enhanced +0.95 FPS, 0 GPU faults).
Evidence
  • Stage permutation checked mathematically: lane >> 2 (bits 2..4 of lane) maps directly to bits 5..7 of logical, so shuffle_xor 16/8/4 flips bits 7/6/5 (+128/+64/+32 for the active lower half), leaving lanes 0..3 of each subgroup holding the exact stage-2 results for logical == residue (0..31), which subgroup 0 then reduces via shuffle_xor 16/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), and nir_shader_gather_info() cleanly drop the original pairs/sums arrays and update shader metadata before backend lowering.

@RyanTheTide

Copy link
Copy Markdown

@DjDeveloperr, code-owner heads-up: App approval at 09c4d9e10d8a from a different lane. This PR goes into the merge train's next A/B (aurora-wip e1db0fd1 + #1374) on M1 Pro G13S + M4 G16G. If that row is clean, I plan to land it via --admin under the 6-point rule no earlier than one hour from now, unless you post a hold.

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>

@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: 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.

@DjDeveloperr

Copy link
Copy Markdown
Author

Row at 02474d6fb0d2 on J714s (M5 Pro, G17S), kernel 7.3.0-m5-pr67, SLC on, Borealis built natively on the M5:

  • GTA V's original ordered-tile compute module against the Lavapipe reference: 8/8 byte-identical readbacks (47x33 and 3024x1964, two input patterns, each run twice). The pass is active: 125 instructions, 124.7 to 125.5 µs per dispatch.
  • GTA V Enhanced built-in benchmark, 3024x1964, stock settings: 67.81 FPS (69.92 / 63.73 / 65.23 / 73.67 / 66.52), 0 new GPU faults, 0 s at the 338 MHz clamp, clean exit. One run, not an A/B.

@DjDeveloperr

Copy link
Copy Markdown
Author

MacBook Neo (J700, A18 Pro, G17P B1), stock Fedora 44 on 7.3.0-41.fc44.aurora.aarch64. Base 089aeb0e6289, head 02474d6fb0d2. Both arms loaded their checksum-verified Borealis ICDs.

Vulkan CTS 1.4.7.1 selection Base pass / fail / NotSupported Head pass / fail / NotSupported
dEQP-VK.subgroups.shuffle* 7,042 / 0 / 2,520 7,042 / 0 / 2,520
Compute controls 3,713 / 0 / 986 3,713 / 0 / 986
Total 10,755 / 0 / 3,506 10,755 / 0 / 3,506

All 14,261 statuses matched. Under dEQP-VK.compute., the selections were pipeline.basic.*, shader_object_binary.basic.*, shader_object_spirv.basic.*, pipeline.workgroup_memory_explicit_layout.*, pipeline.zero_initialize_workgroup_memory.* and pipeline.device_group.*. These cover shared storage, barriers, workgroup layout and dispatch base.

Meson hk_ordered_tile_reduction passed. At 47×33, both output images from the ordered-tile reduction matched the Lavapipe reference byte for byte. AGX_MESA_DEBUG=binaries recorded identical G17P compute main and preamble binaries, 3,450 and 410 bytes respectively, consistent with the AGX3C gate.

Zero GPU faults, kernel WARNs or AVCs. Same stock boot, with SLC enabled on four planes.

@DjDeveloperr

Copy link
Copy Markdown
Author

Verdict: MERGE at 02474d6fb0d2

Blockers:

  • None.

Checked:

  • All five changed files. eval_local() now handles constants, local IDs, the workgroup size and vecN components first. Any other ALU opcode has to be on the supported list before its sources are walked, so nir_scalar_chase_alu_src() only ever sees per-component opcodes or vecN. That is the only place the pass chases sources. A stage guard written as all(uvec2(index >> 3, 0) == uvec2(0, 0)) (ball_iequal2) now leaves the shader unchanged.
  • Guards and shared-array indices can only be built from constants, the local IDs and the fixed 16x16 size, and the pass checks every guard against all 256 invocations. A match can't depend on data or on a non-uniform value. Every input store must write its own invocation's slot on every path, and those stores become SSA values. Shared loads, loops, jumps, atomics, other subgroup operations and image loads before the first barrier make the pass decline.
  • All the new shuffles run with full subgroups. The first three are at the top level, before the kept barrier. The last five are inside subgroup_id == 0, which is uniform per subgroup. The 32x8 launch always has eight full subgroups. Each of the 32 partial slots has one writer before that barrier, and subgroup 0 reads them after it. Physical subgroup 0, lane 0 is logical invocation 0, so the output keeps its writer and coordinates. Compute shaders have no helper invocations.
  • Every system value that depends on the workgroup size is either remapped (local ID, local index, global ID, workgroup size) or makes the pass decline. The rebuilt global ID, (workgroup_id + base_workgroup_id) * 16 + local_id, matches this tree: nir_lower_system_values has already added the base to SPIR-V's WorkgroupId, and hk_shader.c lowers the base global invocation ID to zero. vkCmdDispatchBase stays correct.
  • umin, umax and wrapping iadd are exact in any order, and the pass keeps the original operand order anyway.
  • Rows at this head: J714s readbacks match Lavapipe 8 of 8 with the pass active, GTA V runs at 67.81 FPS and there are 0 faults. J700 (G17P): 14,261 CTS statuses are identical to the base, the readback matches Lavapipe, and the binaries are identical, so the pass stays off on G17P.
  • The PR applied cleanly onto the current aurora-wip (e30a6b098e5a) and builds without new warnings. With the asahi CI configuration, the asahi and poly suites pass 361 of 361 on that merge result, including hk_ordered_tile_reduction. The host test passes 18 of 18 with assertions on. With the 09c4d9e10d8a version of the pass, the vector all() case aborts on the nir_scalar_chase_alu_src assertion, as described.

A base M5 (G17G) row is still owed after the merge.

Evidence
  • Merge result built on x86_64 Linux. GCC debugoptimized with assertions on: libvulkan_asahi.so and hk_ordered_tile_reduction_test, with no warnings in the changed files.
  • Clang debugoptimized (assertions on), configured like the asahi CI job (-Dgallium-drivers=asahi -Dvulkan-drivers=asahi -Dplatforms=wayland,x11 -Dbuild-tests=true -Dtools=drm-shim): meson test --suite asahi --suite poly gives 361 OK and 0 fail.
  • Negative control: only hk_nir_opt_ordered_tile_reduction.c was swapped for its 09c4d9e10d8a version, with today's test. Result: nir_scalar_chase_alu_src: Assertion 'nir_op_infos[alu->op].input_sizes[alu_src_idx] == 1' failed, exit 134. The file was then restored, and the run was 18 of 18 again.
  • aurora-wip changed hk_shader.c after this PR's base, but only in the M5 task-shader query code. That doesn't overlap this PR's hunk in hk_preprocess_nir_internal().

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