[llvm] 894c214 - [AMDGPU][InstCombine] Fold identity wave shuffle to its source (#204121)
via llvm-commits
llvm-commits at lists.llvm.org
Wed Jun 17 06:00:21 PDT 2026
Author: Aleksandar Spasojevic
Date: 2026-06-17T15:00:16+02:00
New Revision: 894c21432024c05583aafee9add41cb4978a6f7b
URL: https://github.com/llvm/llvm-project/commit/894c21432024c05583aafee9add41cb4978a6f7b
DIFF: https://github.com/llvm/llvm-project/commit/894c21432024c05583aafee9add41cb4978a6f7b.diff
LOG: [AMDGPU][InstCombine] Fold identity wave shuffle to its source (#204121)
The constant-shuffle matcher emitted a self-targeting update_dpp
(quad_perm 0xe4) for an identity shuffle instead of eliminating it.
Return the source value when every lane reads itself so the intrinsic
folds away.
Add a separate wave64 test file that drives the matcher with the full
wave64 thread ID (mbcnt.hi(mbcnt.lo)), covering quad_perm and other DPP
forms plus identity folding.
Added:
llvm/test/Transforms/InstCombine/AMDGPU/wave-shuffle-patterns-wave64.ll
Modified:
llvm/lib/Target/AMDGPU/AMDGPUInstCombineIntrinsic.cpp
llvm/test/Transforms/InstCombine/AMDGPU/llvm.amdgcn.wave.shuffle.ll
llvm/test/Transforms/InstCombine/AMDGPU/wave-shuffle-patterns.ll
Removed:
################################################################################
diff --git a/llvm/lib/Target/AMDGPU/AMDGPUInstCombineIntrinsic.cpp b/llvm/lib/Target/AMDGPU/AMDGPUInstCombineIntrinsic.cpp
index 3c22ce96ff1f6..5788932ae5e2e 100644
--- a/llvm/lib/Target/AMDGPU/AMDGPUInstCombineIntrinsic.cpp
+++ b/llvm/lib/Target/AMDGPU/AMDGPUInstCombineIntrinsic.cpp
@@ -976,6 +976,11 @@ static Value *matchShuffleToHWIntrinsic(IRBuilderBase &B, Value *Src,
ArrayRef<uint8_t> Ids,
const GCNSubtarget &ST,
const DataLayout &DL) {
+ // Identity shuffle (every lane reads itself) folds to the source value.
+ if (all_of(enumerate(Ids),
+ [](const auto &E) { return E.value() == E.index(); }))
+ return Src;
+
// Uniform shuffle (all lanes read the same value) is handled by cheaper
// broadcast/readlane intrinsics.
if (all_equal(Ids))
diff --git a/llvm/test/Transforms/InstCombine/AMDGPU/llvm.amdgcn.wave.shuffle.ll b/llvm/test/Transforms/InstCombine/AMDGPU/llvm.amdgcn.wave.shuffle.ll
index 582789501dd41..1ad3d73d7a722 100644
--- a/llvm/test/Transforms/InstCombine/AMDGPU/llvm.amdgcn.wave.shuffle.ll
+++ b/llvm/test/Transforms/InstCombine/AMDGPU/llvm.amdgcn.wave.shuffle.ll
@@ -8,13 +8,11 @@
define i32 @test_wave_shuffle_self_select(i32 %val) {
; CHECK-W32-LABEL: define i32 @test_wave_shuffle_self_select(
; CHECK-W32-SAME: i32 [[VAL:%.*]]) #[[ATTR0:[0-9]+]] {
-; CHECK-W32-NEXT: [[RES:%.*]] = call i32 @llvm.amdgcn.update.dpp.i32(i32 poison, i32 [[VAL]], i32 228, i32 15, i32 15, i1 true)
-; CHECK-W32-NEXT: ret i32 [[RES]]
+; CHECK-W32-NEXT: ret i32 [[VAL]]
;
; CHECK-W64-LABEL: define i32 @test_wave_shuffle_self_select(
; CHECK-W64-SAME: i32 [[VAL:%.*]]) #[[ATTR0:[0-9]+]] {
-; CHECK-W64-NEXT: [[RES:%.*]] = call i32 @llvm.amdgcn.update.dpp.i32(i32 poison, i32 [[VAL]], i32 228, i32 15, i32 15, i1 true)
-; CHECK-W64-NEXT: ret i32 [[RES]]
+; CHECK-W64-NEXT: ret i32 [[VAL]]
;
; CHECK-NO-WAVE-SIZE-LABEL: define i32 @test_wave_shuffle_self_select(
; CHECK-NO-WAVE-SIZE-SAME: i32 [[VAL:%.*]]) #[[ATTR0:[0-9]+]] {
diff --git a/llvm/test/Transforms/InstCombine/AMDGPU/wave-shuffle-patterns-wave64.ll b/llvm/test/Transforms/InstCombine/AMDGPU/wave-shuffle-patterns-wave64.ll
new file mode 100644
index 0000000000000..a225980ec0fa1
--- /dev/null
+++ b/llvm/test/Transforms/InstCombine/AMDGPU/wave-shuffle-patterns-wave64.ll
@@ -0,0 +1,114 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
+; RUN: opt -mtriple=amdgcn-amd-amdhsa -mcpu=gfx1100 -mattr=+wavefrontsize64 -passes=instcombine -S < %s | FileCheck %s --check-prefixes=GFX11-W64
+; RUN: opt -mtriple=amdgcn-amd-amdhsa -mcpu=gfx900 -passes=instcombine -S < %s | FileCheck %s --check-prefixes=GFX9
+
+; Wave64 shuffle patterns. The index must be the full wave64 thread ID,
+; mbcnt.hi(-1, mbcnt.lo(-1, 0)); using mbcnt.lo alone would only yield
+; lane & 0x1F and is not a wave-wide permutation.
+
+declare i32 @llvm.amdgcn.mbcnt.lo(i32, i32)
+declare i32 @llvm.amdgcn.mbcnt.hi(i32, i32)
+declare i32 @llvm.amdgcn.wave.shuffle.i32(i32, i32)
+
+; Identity: every lane reads itself, folded to the source value.
+define i32 @test_identity_w64(i32 %val) {
+; GFX11-W64-LABEL: @test_identity_w64(
+; GFX11-W64-NEXT: ret i32 [[VAL:%.*]]
+;
+; GFX9-LABEL: @test_identity_w64(
+; GFX9-NEXT: ret i32 [[VAL:%.*]]
+;
+ %lo = call i32 @llvm.amdgcn.mbcnt.lo(i32 -1, i32 0)
+ %lane = call i32 @llvm.amdgcn.mbcnt.hi(i32 -1, i32 %lo)
+ %result = call i32 @llvm.amdgcn.wave.shuffle.i32(i32 %val, i32 %lane)
+ ret i32 %result
+}
+
+; Quad perm: lane ^ 1 swaps adjacent pairs.
+define i32 @test_quad_perm_xor1_w64(i32 %val) {
+; GFX11-W64-LABEL: @test_quad_perm_xor1_w64(
+; GFX11-W64-NEXT: [[RESULT:%.*]] = call i32 @llvm.amdgcn.update.dpp.i32(i32 poison, i32 [[VAL:%.*]], i32 177, i32 15, i32 15, i1 true)
+; GFX11-W64-NEXT: ret i32 [[RESULT]]
+;
+; GFX9-LABEL: @test_quad_perm_xor1_w64(
+; GFX9-NEXT: [[RESULT:%.*]] = call i32 @llvm.amdgcn.update.dpp.i32(i32 poison, i32 [[VAL:%.*]], i32 177, i32 15, i32 15, i1 true)
+; GFX9-NEXT: ret i32 [[RESULT]]
+;
+ %lo = call i32 @llvm.amdgcn.mbcnt.lo(i32 -1, i32 0)
+ %lane = call i32 @llvm.amdgcn.mbcnt.hi(i32 -1, i32 %lo)
+ %idx = xor i32 %lane, 1
+ %result = call i32 @llvm.amdgcn.wave.shuffle.i32(i32 %val, i32 %idx)
+ ret i32 %result
+}
+
+; Full-row mirror: reverse within every 16-lane row (lane ^ 15).
+define i32 @test_full_row_mirror_w64(i32 %val) {
+; GFX11-W64-LABEL: @test_full_row_mirror_w64(
+; GFX11-W64-NEXT: [[RESULT:%.*]] = call i32 @llvm.amdgcn.update.dpp.i32(i32 poison, i32 [[VAL:%.*]], i32 320, i32 15, i32 15, i1 true)
+; GFX11-W64-NEXT: ret i32 [[RESULT]]
+;
+; GFX9-LABEL: @test_full_row_mirror_w64(
+; GFX9-NEXT: [[RESULT:%.*]] = call i32 @llvm.amdgcn.update.dpp.i32(i32 poison, i32 [[VAL:%.*]], i32 320, i32 15, i32 15, i1 true)
+; GFX9-NEXT: ret i32 [[RESULT]]
+;
+ %lo = call i32 @llvm.amdgcn.mbcnt.lo(i32 -1, i32 0)
+ %lane = call i32 @llvm.amdgcn.mbcnt.hi(i32 -1, i32 %lo)
+ %idx = xor i32 %lane, 15
+ %result = call i32 @llvm.amdgcn.wave.shuffle.i32(i32 %val, i32 %idx)
+ ret i32 %result
+}
+
+; Row rotate by 3 within each 16-lane row: (lane & ~15) | ((lane + 3) & 15).
+define i32 @test_row_rotate3_w64(i32 %val) {
+; GFX11-W64-LABEL: @test_row_rotate3_w64(
+; GFX11-W64-NEXT: [[RESULT:%.*]] = call i32 @llvm.amdgcn.update.dpp.i32(i32 poison, i32 [[VAL:%.*]], i32 301, i32 15, i32 15, i1 true)
+; GFX11-W64-NEXT: ret i32 [[RESULT]]
+;
+; GFX9-LABEL: @test_row_rotate3_w64(
+; GFX9-NEXT: [[RESULT:%.*]] = call i32 @llvm.amdgcn.update.dpp.i32(i32 poison, i32 [[VAL:%.*]], i32 301, i32 15, i32 15, i1 true)
+; GFX9-NEXT: ret i32 [[RESULT]]
+;
+ %lo = call i32 @llvm.amdgcn.mbcnt.lo(i32 -1, i32 0)
+ %lane = call i32 @llvm.amdgcn.mbcnt.hi(i32 -1, i32 %lo)
+ %row = and i32 %lane, -16
+ %inrow = and i32 %lane, 15
+ %add = add i32 %inrow, 3
+ %mod = and i32 %add, 15
+ %idx = or i32 %row, %mod
+ %result = call i32 @llvm.amdgcn.wave.shuffle.i32(i32 %val, i32 %idx)
+ ret i32 %result
+}
+
+; Row XOR mask 5 (lane ^ 5); row_xmask is GFX10+, ds_swizzle fallback on GFX9.
+define i32 @test_row_xmask5_w64(i32 %val) {
+; GFX11-W64-LABEL: @test_row_xmask5_w64(
+; GFX11-W64-NEXT: [[RESULT:%.*]] = call i32 @llvm.amdgcn.update.dpp.i32(i32 poison, i32 [[VAL:%.*]], i32 357, i32 15, i32 15, i1 true)
+; GFX11-W64-NEXT: ret i32 [[RESULT]]
+;
+; GFX9-LABEL: @test_row_xmask5_w64(
+; GFX9-NEXT: [[RESULT:%.*]] = call i32 @llvm.amdgcn.ds.swizzle(i32 [[VAL:%.*]], i32 5151)
+; GFX9-NEXT: ret i32 [[RESULT]]
+;
+ %lo = call i32 @llvm.amdgcn.mbcnt.lo(i32 -1, i32 0)
+ %lane = call i32 @llvm.amdgcn.mbcnt.hi(i32 -1, i32 %lo)
+ %idx = xor i32 %lane, 5
+ %result = call i32 @llvm.amdgcn.wave.shuffle.i32(i32 %val, i32 %idx)
+ ret i32 %result
+}
+
+; Cross-row XOR (lane ^ 16); permlanex16 is GFX11+, ds_swizzle fallback on GFX9.
+define i32 @test_xor_cross_row_w64(i32 %val) {
+; GFX11-W64-LABEL: @test_xor_cross_row_w64(
+; GFX11-W64-NEXT: [[RESULT:%.*]] = call i32 @llvm.amdgcn.permlanex16.i32(i32 poison, i32 [[VAL:%.*]], i32 1985229328, i32 -19088744, i1 false, i1 false)
+; GFX11-W64-NEXT: ret i32 [[RESULT]]
+;
+; GFX9-LABEL: @test_xor_cross_row_w64(
+; GFX9-NEXT: [[RESULT:%.*]] = call i32 @llvm.amdgcn.ds.swizzle(i32 [[VAL:%.*]], i32 16415)
+; GFX9-NEXT: ret i32 [[RESULT]]
+;
+ %lo = call i32 @llvm.amdgcn.mbcnt.lo(i32 -1, i32 0)
+ %lane = call i32 @llvm.amdgcn.mbcnt.hi(i32 -1, i32 %lo)
+ %idx = xor i32 %lane, 16
+ %result = call i32 @llvm.amdgcn.wave.shuffle.i32(i32 %val, i32 %idx)
+ ret i32 %result
+}
diff --git a/llvm/test/Transforms/InstCombine/AMDGPU/wave-shuffle-patterns.ll b/llvm/test/Transforms/InstCombine/AMDGPU/wave-shuffle-patterns.ll
index 30c18ab5f34e9..3b8217e03c6f6 100644
--- a/llvm/test/Transforms/InstCombine/AMDGPU/wave-shuffle-patterns.ll
+++ b/llvm/test/Transforms/InstCombine/AMDGPU/wave-shuffle-patterns.ll
@@ -609,8 +609,7 @@ define i32 @test_nonconstant_shuffle_w32(i32 %val, i32 %idx) {
; Identity shuffle: lane_id itself.
define i32 @test_identity_shuffle_w32(i32 %val) {
; GFX11-LABEL: @test_identity_shuffle_w32(
-; GFX11-NEXT: [[RESULT:%.*]] = call i32 @llvm.amdgcn.update.dpp.i32(i32 poison, i32 [[VAL:%.*]], i32 228, i32 15, i32 15, i1 true)
-; GFX11-NEXT: ret i32 [[RESULT]]
+; GFX11-NEXT: ret i32 [[RESULT:%.*]]
;
; GFX11-W64-LABEL: @test_identity_shuffle_w32(
; GFX11-W64-NEXT: [[LANE:%.*]] = call range(i32 0, 33) i32 @llvm.amdgcn.mbcnt.lo(i32 -1, i32 0)
@@ -900,13 +899,12 @@ define i32 @test_rotate_add1_full_w64(i32 %val) {
ret i32 %result
}
-; Identity shuffle (N=0 degenerate case for rotate; add %lane,0 folds to %lane).
-; On GFX11 wave32 this still folds to the identity quad-perm via DPP; on wave64
-; targets (GFX11-W64, GFX9) mbcnt.lo alone is not the full lane ID so no fold.
+; Identity shuffle (add %lane,0 folds to %lane). On GFX11 wave32 this folds to
+; the source value; on wave64 targets (GFX11-W64, GFX9) mbcnt.lo alone is not
+; the full lane ID so no fold occurs.
define i32 @test_identity_w32(i32 %val) {
; GFX11-LABEL: @test_identity_w32(
-; GFX11-NEXT: [[RESULT:%.*]] = call i32 @llvm.amdgcn.update.dpp.i32(i32 poison, i32 [[VAL:%.*]], i32 228, i32 15, i32 15, i1 true)
-; GFX11-NEXT: ret i32 [[RESULT]]
+; GFX11-NEXT: ret i32 [[RESULT:%.*]]
;
; GFX11-W64-LABEL: @test_identity_w32(
; GFX11-W64-NEXT: [[LANE:%.*]] = call range(i32 0, 33) i32 @llvm.amdgcn.mbcnt.lo(i32 -1, i32 0)
More information about the llvm-commits
mailing list