Repository navigation
Broadcast subgroupShuffle with a uniform index instead of looping over every offset (linux/0006) - #10
Merged
Conversation
…nux/0006) ir3 lowers shuffle(x, i) to rotate(x, i - gl_SubgroupInvocationID) in a loop with one iteration per distinct relative offset, so a broadcast costs about one iteration per invocation; with the half-wave subgroups patch the index is never uniform and every such shuffle runs ~32 iterations. linux/0006 lowers a uniform index to read_invocation_cond_ir3 and, for a divergent one, serves the invocations with the same absolute source and the ones with the same relative offset in the same iteration. In 007 First Light on Adreno 840 one compute shader went from 94.6 to 8.4 ms per frame.
maxjivi05
force-pushed
the
ir3-shuffle-broadcast
branch
from
October 31, 2026 13:23
7b51086 to
02a2d6e
Compare
# Conflicts: # patches/series.json
# Conflicts: # patches/series.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
See #9.
ir3_nir_lower_shuffle()turnsshuffle(x, i)intorotate(x, i - gl_SubgroupInvocationID)and, because that offset is divergent, into
make_shuffle_uniform()'s loop with one iteration per distinctoffset. A shuffle where all invocations read the same source (or a few sources) therefore costs about one
iteration per invocation. With
linux/0002the index is(id & ~31) | (i & 31), which is never uniform, soevery such shuffle runs ~32 iterations.
Change
patches/linux/0006-ir3-shuffle-broadcast.patch, only inlower_shuffle()fornir_intrinsic_shufflewith a 32-bit value:
read_invocation_cond_ir3(x, id == i), no loop. This is the samesequence ir3 already uses for
subgroupBroadcast(lower_read_invocation_to_cond).make_shuffle_mixed()loop. Each iteration reads both the absoluteindex and the relative offset of a not-yet-served invocation, then serves every invocation that wants the
same absolute source (broadcast via
read_invocation_cond_ir3) and every invocation that wants the samerelative offset (
rotate, as before). With half-wave subgroups a broadcast takes 2 iterations (one perhalf) instead of ~32. Every iteration finishes a whole group of invocations with the same offset, so it
never needs more iterations than the current loop; for patterns that already were cheap (rotations, xor)
the count is the same, but each iteration has a few more instructions.
Other bit sizes and
shuffle_up/down/xorkeep the existing lowering. Nothing is A8XX- or KGSL-specific;the patch does not depend on
linux/0002and applies to plain Mesa main too.Correctness notes
read_invocation_cond_ir3/vote_all, as in the existing loop.If the source invocation is inactive or out of range the result is undefined (as the spec allows), and
the loop still ends: the invocation that supplied the index is always marked done.
make_shuffle_uniform(), the loop reads the next index withread_invocation_cond_ir3under acondition that is true in several invocations, so it relies on the same behaviour of that macro. It does
not need the absolute index and the offset to come from the same invocation.
gl_SubgroupInvocationIDhere is the hardware invocation id (0–63), and theindex already contains the half base, so absolute indices stay inside the caller's half.
random, few-sources and out-of-range indices) gives the expected value for every invocation whose source
is active and in range, and never more iterations than the current loop.
Testing
scripts/apply_patches.py linuxon Mesa main 087e1fe4e6a5 (2026-10-27): the whole series including0006applies.0006also applies on its own to plain Mesa main and to Mesa e2ab6687d5ad.0006builds
libvulkan_freedreno.sowith no warnings inir3_lower_subgroups.c; plain Mesa main +0006alone builds
libfreedreno_ir3.a, also without warnings. Same meson options asbuild_linux.sh, except-Dplatforms=x11(the Debian wayland-protocols is older than main now requires) and--wrap-mode=nodownload. This build was only compiled, not run; the run-time testing below is on thev0.3.4 base.
(Mesa e2ab6687d5ad + this series) with this change plus two local changes that are not part of this PR
(the descriptor pool fix, and a pipeline cache key change I only used to force recompilation of shaders
with
GroupNonUniformShuffle). In 007 First Light the shader from the issue went from 94.6 to 8.4 ms perframe; menu 23 → 56 FPS, gameplay 6–7 → 12–15 FPS. The final NIR of that shader has 21
read_invocation_cond_ir3+ 7rotate+ 7vote_allinstead of 7 + 7 + 7, the same 11 loops, and2 iterations per shuffle instead of ~32. I did not compare the image before and after.
Not tested: other games, other Adreno generations (A6XX/A7XX), MSM/DRM, the Android target, Vulkan CTS
(
dEQP-VK.subgroups.shuffle.*), 16-bit shuffles (they keep the old path). Shaders that usesubgroupShufflewith a divergent index that is effectively a rotation or xor pattern get the same numberof iterations with a slightly longer body — not measured.
The patch is enabled for the
linuxtarget only, since that is what I tested; the uniform-index casealso exists without
linux/0002, so the Android target could benefit as well.Fixes #9