[llvm] [AMDGPU] Add optimization for llvm.amdgcn.wave.shuffle in uniform cases (PR #174795)
via llvm-commits
llvm-commits at lists.llvm.org
Wed Jan 7 08:16:26 PST 2026
https://github.com/saxlungs created https://github.com/llvm/llvm-project/pull/174795
When the llvm.amdgcn.wave.shuffle intrinsic is called with a uniform Index operand, it is effectively the same as the llvm.amdgcn.readlane intrinsic. This change handles this situation and replaces it with the readlane intrinsic
>From fd1b56de6dd1967b50e4b7954b818f25e1cf5470 Mon Sep 17 00:00:00 2001
From: Domenic Nutile <domenic.nutile at gmail.com>
Date: Tue, 6 Jan 2026 16:10:20 -0500
Subject: [PATCH] [AMDGPU] Add optimization for llvm.amdgcn.wave.shuffle in
uniform cases
When the llvm.amdgcn.wave.shuffle intrinsic is called with a uniform Index operand, it is effectively the same as the llvm.amdgcn.readlane intrinsic. This change handles this situation and replaces it with the readlane intrinsic
Signed-off-by: Domenic Nutile <domenic.nutile at gmail.com>
---
.../AMDGPU/AMDGPUUniformIntrinsicCombine.cpp | 25 ++++++++++++++
.../amdgpu-miscellaneous-uniform-intrinsic.ll | 33 +++++++++++++++++++
2 files changed, 58 insertions(+)
diff --git a/llvm/lib/Target/AMDGPU/AMDGPUUniformIntrinsicCombine.cpp b/llvm/lib/Target/AMDGPU/AMDGPUUniformIntrinsicCombine.cpp
index c52eb4e477685..72b1e17322355 100644
--- a/llvm/lib/Target/AMDGPU/AMDGPUUniformIntrinsicCombine.cpp
+++ b/llvm/lib/Target/AMDGPU/AMDGPUUniformIntrinsicCombine.cpp
@@ -107,6 +107,31 @@ static bool optimizeUniformIntrinsic(IntrinsicInst &II,
II.eraseFromParent();
return Changed;
}
+ case Intrinsic::amdgcn_wave_shuffle: {
+ Value *Val = II.getOperand(0);
+ Value *Idx = II.getOperand(1);
+
+ // When Index is uniform, this is just a readlane operation
+ if (isDivergentUseWithNew(II.getOperandUse(1), UI, Tracker))
+ return false;
+
+ // Like with readlane, if Value is also uniform then just propagate it
+ if (!isDivergentUseWithNew(II.getOperandUse(0), UI, Tracker)) {
+ II.replaceAllUsesWith(Val);
+ II.eraseFromParent();
+ return true;
+ }
+
+ // Otherwise, construct a proper call to amdgcn_readlane
+ llvm::IRBuilder<> Builder(&II);
+ CallInst *Readlane =
+ Builder.CreateIntrinsic(Builder.getInt32Ty(),
+ Intrinsic::amdgcn_readlane, {Val, Idx});
+
+ II.replaceAllUsesWith(Readlane);
+ II.eraseFromParent();
+ return true;
+ }
default:
return false;
}
diff --git a/llvm/test/CodeGen/AMDGPU/amdgpu-miscellaneous-uniform-intrinsic.ll b/llvm/test/CodeGen/AMDGPU/amdgpu-miscellaneous-uniform-intrinsic.ll
index 92198f2593f3a..ef89467b3af76 100644
--- a/llvm/test/CodeGen/AMDGPU/amdgpu-miscellaneous-uniform-intrinsic.ll
+++ b/llvm/test/CodeGen/AMDGPU/amdgpu-miscellaneous-uniform-intrinsic.ll
@@ -171,3 +171,36 @@ if:
exit:
ret void
}
+
+define amdgpu_kernel void @wave_shuffle_nonuniform_value_uniform_index(ptr addrspace(1) %out) {
+; CHECK-LABEL: wave_shuffle_nonuniform_value_uniform_index:
+; CHECK: ; %bb.0:
+; CHECK-NEXT: s_load_b64 s[0:1], s[4:5], 0x0
+; CHECK-NEXT: v_and_b32_e32 v0, 0x3ff, v0
+; CHECK-NEXT: s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
+; CHECK-NEXT: v_readlane_b32 s2, v0, 5
+; CHECK-NEXT: v_dual_mov_b32 v0, 0 :: v_dual_mov_b32 v1, s2
+; CHECK-NEXT: s_waitcnt lgkmcnt(0)
+; CHECK-NEXT: global_store_b32 v0, v1, s[0:1]
+; CHECK-NEXT: s_endpgm
+ %tid = call i32 @llvm.amdgcn.workitem.id.x()
+ %res = tail call i32 @llvm.amdgcn.wave.shuffle(i32 %tid, i32 5)
+ store i32 %res, ptr addrspace(1) %out
+ ret void
+}
+
+define amdgpu_kernel void @wave_shuffle_uniform_value_and_index(ptr addrspace(1) %out, i32 %val) {
+; CHECK-LABEL: wave_shuffle_uniform_value_and_index:
+; CHECK: ; %bb.0:
+; CHECK-NEXT: s_mov_b64 s[0:1], 0
+; CHECK-NEXT: s_load_b32 s2, s[0:1], 0x0
+; CHECK-NEXT: s_load_b64 s[0:1], s[4:5], 0x0
+; CHECK-NEXT: s_waitcnt lgkmcnt(0)
+; CHECK-NEXT: v_dual_mov_b32 v0, 0 :: v_dual_mov_b32 v1, s2
+; CHECK-NEXT: global_store_b32 v0, v1, s[0:1]
+; CHECK-NEXT: s_endpgm
+ %uniform_vgpr = load i32, ptr addrspace(1) null, align 4
+ %res = tail call i32 @llvm.amdgcn.wave.shuffle(i32 %uniform_vgpr, i32 5)
+ store i32 %res, ptr addrspace(1) %out
+ ret void
+}
More information about the llvm-commits
mailing list