Skip to content

Commit 31ceb2a

Browse files
committed
[AMDGPU] InstCombine llvm.amdgcn.ds.bpermute with uniform arguments
Reland llvm#129895 with a fix to avoid trying to combine bpermute of bitcast.
1 parent 78281fd commit 31ceb2a

File tree

3 files changed

+74
-4
lines changed

3 files changed

+74
-4
lines changed

llvm/lib/Target/AMDGPU/AMDGPUInstCombineIntrinsic.cpp

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1118,9 +1118,11 @@ GCNTTIImpl::instCombineIntrinsic(InstCombiner &IC, IntrinsicInst &II) const {
11181118
}
11191119
case Intrinsic::amdgcn_permlane64:
11201120
case Intrinsic::amdgcn_readfirstlane:
1121-
case Intrinsic::amdgcn_readlane: {
1122-
// If the first argument is uniform these intrinsics return it unchanged.
1123-
const Use &Src = II.getArgOperandUse(0);
1121+
case Intrinsic::amdgcn_readlane:
1122+
case Intrinsic::amdgcn_ds_bpermute: {
1123+
// If the data argument is uniform these intrinsics return it unchanged.
1124+
unsigned SrcIdx = IID == Intrinsic::amdgcn_ds_bpermute ? 1 : 0;
1125+
const Use &Src = II.getArgOperandUse(SrcIdx);
11241126
if (isTriviallyUniform(Src))
11251127
return IC.replaceInstUsesWith(II, Src.get());
11261128

@@ -1129,7 +1131,8 @@ GCNTTIImpl::instCombineIntrinsic(InstCombiner &IC, IntrinsicInst &II) const {
11291131
return ⅈ
11301132

11311133
// readfirstlane.ty0 (bitcast ty1 x to ty0) -> bitcast (readfirstlane.ty1)
1132-
if (auto *BC = dyn_cast<BitCastInst>(Src); BC && BC->hasOneUse()) {
1134+
if (auto *BC = dyn_cast<BitCastInst>(Src); BC && BC->hasOneUse() &&
1135+
IID != Intrinsic::amdgcn_ds_bpermute) {
11331136
Value *BCSrc = BC->getOperand(0);
11341137

11351138
// TODO: Handle this for update_dpp, mov_ddp8, and all permlane variants.
@@ -1152,6 +1155,22 @@ GCNTTIImpl::instCombineIntrinsic(InstCombiner &IC, IntrinsicInst &II) const {
11521155
}
11531156
}
11541157

1158+
// If the lane argument of bpermute is uniform, change it to readlane. This
1159+
// generates better code and can enable further optimizations because
1160+
// readlane is AlwaysUniform.
1161+
if (IID == Intrinsic::amdgcn_ds_bpermute) {
1162+
const Use &Lane = II.getArgOperandUse(0);
1163+
if (isTriviallyUniform(Lane)) {
1164+
Value *NewLane = IC.Builder.CreateLShr(Lane, 2);
1165+
Function *NewDecl = Intrinsic::getOrInsertDeclaration(
1166+
II.getModule(), Intrinsic::amdgcn_readlane, II.getType());
1167+
II.setCalledFunction(NewDecl);
1168+
II.setOperand(0, Src);
1169+
II.setOperand(1, NewLane);
1170+
return &II;
1171+
}
1172+
}
1173+
11551174
return std::nullopt;
11561175
}
11571176
case Intrinsic::amdgcn_writelane: {

llvm/test/Transforms/InstCombine/AMDGPU/amdgcn-intrinsics.ll

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6583,3 +6583,42 @@ define i32 @prng_poison_i32() {
65836583
%prng = call i32 @llvm.amdgcn.prng.b32(i32 poison)
65846584
ret i32 %prng
65856585
}
6586+
6587+
; --------------------------------------------------------------------
6588+
; llvm.amdgcn.ds.bpermute
6589+
; --------------------------------------------------------------------
6590+
6591+
define amdgpu_kernel void @ds_bpermute_uniform_src(ptr addrspace(1) %out, i32 %lane) {
6592+
; CHECK-LABEL: @ds_bpermute_uniform_src(
6593+
; CHECK-NEXT: store i32 7, ptr addrspace(1) [[OUT:%.*]], align 4
6594+
; CHECK-NEXT: ret void
6595+
;
6596+
%v = call i32 @llvm.amdgcn.ds.bpermute(i32 %lane, i32 7)
6597+
store i32 %v, ptr addrspace(1) %out
6598+
ret void
6599+
}
6600+
6601+
define amdgpu_kernel void @ds_bpermute_constant_lane(ptr addrspace(1) %out, i32 %src) {
6602+
; CHECK-LABEL: @ds_bpermute_constant_lane(
6603+
; CHECK-NEXT: [[V:%.*]] = call i32 @llvm.amdgcn.readlane.i32(i32 [[SRC:%.*]], i32 7)
6604+
; CHECK-NEXT: store i32 [[V]], ptr addrspace(1) [[OUT:%.*]], align 4
6605+
; CHECK-NEXT: ret void
6606+
;
6607+
%v = call i32 @llvm.amdgcn.ds.bpermute(i32 28, i32 %src)
6608+
store i32 %v, ptr addrspace(1) %out
6609+
ret void
6610+
}
6611+
6612+
define amdgpu_kernel void @ds_bpermute_uniform_lane(ptr addrspace(1) %out, i32 %lanearg, i32 %src) {
6613+
; CHECK-LABEL: @ds_bpermute_uniform_lane(
6614+
; CHECK-NEXT: [[LANE:%.*]] = call i32 @llvm.amdgcn.readfirstlane.i32(i32 [[LANEARG:%.*]])
6615+
; CHECK-NEXT: [[TMP1:%.*]] = lshr i32 [[LANE]], 2
6616+
; CHECK-NEXT: [[V:%.*]] = call i32 @llvm.amdgcn.readlane.i32(i32 [[SRC:%.*]], i32 [[TMP1]])
6617+
; CHECK-NEXT: store i32 [[V]], ptr addrspace(1) [[OUT:%.*]], align 4
6618+
; CHECK-NEXT: ret void
6619+
;
6620+
%lane = call i32 @llvm.amdgcn.readfirstlane(i32 %lanearg)
6621+
%v = call i32 @llvm.amdgcn.ds.bpermute(i32 %lane, i32 %src)
6622+
store i32 %v, ptr addrspace(1) %out
6623+
ret void
6624+
}

llvm/test/Transforms/InstCombine/AMDGPU/bitcast-fold-lane-ops.ll

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -311,3 +311,15 @@ define i32 @test_bitcast_f32_to_i32_readlane_convergencetoken(float %val, i32 in
311311
%result = call i32 @llvm.amdgcn.readlane.i32(i32 %bitcast, i32 %lane.index) [ "convergencectrl"(token %t) ]
312312
ret i32 %result
313313
}
314+
315+
define i32 @test_bitcast_f32_to_i32_ds_bpermute(float %val, i32 %addr) {
316+
; CHECK-LABEL: define i32 @test_bitcast_f32_to_i32_ds_bpermute(
317+
; CHECK-SAME: float [[VAL:%.*]], i32 [[ADDR:%.*]]) #[[ATTR0]] {
318+
; CHECK-NEXT: [[BITCAST:%.*]] = bitcast float [[VAL]] to i32
319+
; CHECK-NEXT: [[RESULT:%.*]] = call i32 @llvm.amdgcn.ds.bpermute(i32 [[ADDR]], i32 [[BITCAST]])
320+
; CHECK-NEXT: ret i32 [[RESULT]]
321+
;
322+
%bitcast = bitcast float %val to i32
323+
%result = call i32 @llvm.amdgcn.ds.bpermute(i32 %addr, i32 %bitcast)
324+
ret i32 %result
325+
}

0 commit comments

Comments
 (0)