Skip to content

Commit fdd52f5

Browse files
authored
[VPlan] Handle WidenGEP in narrowToSingleScalars (#166740)
This allows us to strip a special case in VPWidenGEP::execute.
1 parent c3c4a88 commit fdd52f5

File tree

5 files changed

+41
-65
lines changed

5 files changed

+41
-65
lines changed

llvm/lib/Transforms/Vectorize/VPlan.h

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1787,12 +1787,6 @@ class LLVM_ABI_FOR_TEST VPWidenGEPRecipe : public VPRecipeWithIRFlags {
17871787
return getOperand(I + 1)->isDefinedOutsideLoopRegions();
17881788
}
17891789

1790-
bool areAllOperandsInvariant() const {
1791-
return all_of(operands(), [](VPValue *Op) {
1792-
return Op->isDefinedOutsideLoopRegions();
1793-
});
1794-
}
1795-
17961790
public:
17971791
VPWidenGEPRecipe(GetElementPtrInst *GEP, ArrayRef<VPValue *> Operands)
17981792
: VPRecipeWithIRFlags(VPDef::VPWidenGEPSC, Operands, *GEP),

llvm/lib/Transforms/Vectorize/VPlanRecipes.cpp

Lines changed: 26 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -2523,51 +2523,32 @@ void VPWidenGEPRecipe::execute(VPTransformState &State) {
25232523
// is vector-typed. Thus, to keep the representation compact, we only use
25242524
// vector-typed operands for loop-varying values.
25252525

2526-
if (areAllOperandsInvariant()) {
2527-
// If we are vectorizing, but the GEP has only loop-invariant operands,
2528-
// the GEP we build (by only using vector-typed operands for
2529-
// loop-varying values) would be a scalar pointer. Thus, to ensure we
2530-
// produce a vector of pointers, we need to either arbitrarily pick an
2531-
// operand to broadcast, or broadcast a clone of the original GEP.
2532-
// Here, we broadcast a clone of the original.
2533-
//
2534-
// TODO: If at some point we decide to scalarize instructions having
2535-
// loop-invariant operands, this special case will no longer be
2536-
// required. We would add the scalarization decision to
2537-
// collectLoopScalars() and teach getVectorValue() to broadcast
2538-
// the lane-zero scalar value.
2539-
SmallVector<Value *> Ops;
2540-
for (unsigned I = 0, E = getNumOperands(); I != E; I++)
2541-
Ops.push_back(State.get(getOperand(I), VPLane(0)));
2542-
2543-
auto *NewGEP =
2544-
State.Builder.CreateGEP(getSourceElementType(), Ops[0], drop_begin(Ops),
2545-
"", getGEPNoWrapFlags());
2546-
Value *Splat = State.Builder.CreateVectorSplat(State.VF, NewGEP);
2547-
State.set(this, Splat);
2548-
} else {
2549-
// If the GEP has at least one loop-varying operand, we are sure to
2550-
// produce a vector of pointers unless VF is scalar.
2551-
// The pointer operand of the new GEP. If it's loop-invariant, we
2552-
// won't broadcast it.
2553-
auto *Ptr = State.get(getOperand(0), isPointerLoopInvariant());
2554-
2555-
// Collect all the indices for the new GEP. If any index is
2556-
// loop-invariant, we won't broadcast it.
2557-
SmallVector<Value *, 4> Indices;
2558-
for (unsigned I = 1, E = getNumOperands(); I < E; I++) {
2559-
VPValue *Operand = getOperand(I);
2560-
Indices.push_back(State.get(Operand, isIndexLoopInvariant(I - 1)));
2561-
}
2562-
2563-
// Create the new GEP. Note that this GEP may be a scalar if VF == 1,
2564-
// but it should be a vector, otherwise.
2565-
auto *NewGEP = State.Builder.CreateGEP(getSourceElementType(), Ptr, Indices,
2566-
"", getGEPNoWrapFlags());
2567-
assert((State.VF.isScalar() || NewGEP->getType()->isVectorTy()) &&
2568-
"NewGEP is not a pointer vector");
2569-
State.set(this, NewGEP);
2570-
}
2526+
assert(
2527+
any_of(operands(),
2528+
[](VPValue *Op) { return !Op->isDefinedOutsideLoopRegions(); }) &&
2529+
"Expected at least one loop-variant operand");
2530+
2531+
// If the GEP has at least one loop-varying operand, we are sure to
2532+
// produce a vector of pointers unless VF is scalar.
2533+
// The pointer operand of the new GEP. If it's loop-invariant, we
2534+
// won't broadcast it.
2535+
auto *Ptr = State.get(getOperand(0), isPointerLoopInvariant());
2536+
2537+
// Collect all the indices for the new GEP. If any index is
2538+
// loop-invariant, we won't broadcast it.
2539+
SmallVector<Value *, 4> Indices;
2540+
for (unsigned I = 1, E = getNumOperands(); I < E; I++) {
2541+
VPValue *Operand = getOperand(I);
2542+
Indices.push_back(State.get(Operand, isIndexLoopInvariant(I - 1)));
2543+
}
2544+
2545+
// Create the new GEP. Note that this GEP may be a scalar if VF == 1,
2546+
// but it should be a vector, otherwise.
2547+
auto *NewGEP = State.Builder.CreateGEP(getSourceElementType(), Ptr, Indices,
2548+
"", getGEPNoWrapFlags());
2549+
assert((State.VF.isScalar() || NewGEP->getType()->isVectorTy()) &&
2550+
"NewGEP is not a pointer vector");
2551+
State.set(this, NewGEP);
25712552
}
25722553

25732554
#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)

llvm/lib/Transforms/Vectorize/VPlanTransforms.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1391,7 +1391,8 @@ static void narrowToSingleScalarRecipes(VPlan &Plan) {
13911391
for (VPBasicBlock *VPBB : VPBlockUtils::blocksOnly<VPBasicBlock>(
13921392
vp_depth_first_shallow(Plan.getVectorLoopRegion()->getEntry()))) {
13931393
for (VPRecipeBase &R : make_early_inc_range(reverse(*VPBB))) {
1394-
if (!isa<VPWidenRecipe, VPWidenSelectRecipe, VPReplicateRecipe>(&R))
1394+
if (!isa<VPWidenRecipe, VPWidenSelectRecipe, VPWidenGEPRecipe,
1395+
VPReplicateRecipe>(&R))
13951396
continue;
13961397
auto *RepR = dyn_cast<VPReplicateRecipe>(&R);
13971398
if (RepR && (RepR->isSingleScalar() || RepR->isPredicated()))

llvm/test/Transforms/LoopVectorize/RISCV/gather-scatter-cost.ll

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ define void @predicated_uniform_load(ptr %src, i32 %n, ptr %dst, i1 %cond) {
6363
; CHECK-NEXT: store i32 [[STORE]], ptr [[NBRBOXES]], align 4
6464
; CHECK-NEXT: [[IV_NEXT]] = add i32 [[IV]], 1
6565
; CHECK-NEXT: [[EXITCOND:%.*]] = icmp sgt i32 [[IV]], [[IBOX]]
66-
; CHECK-NEXT: br i1 [[EXITCOND]], label [[EXIT]], label [[LOOP]], !llvm.loop [[LOOP9:![0-9]+]]
66+
; CHECK-NEXT: br i1 [[EXITCOND]], label [[EXIT]], label [[LOOP]], !llvm.loop [[LOOP8:![0-9]+]]
6767
; CHECK: exit:
6868
; CHECK-NEXT: ret void
6969
;
@@ -114,7 +114,7 @@ define void @predicated_strided_store(ptr %start) {
114114
; RVA23-NEXT: [[AVL_NEXT]] = sub nuw i64 [[AVL]], [[TMP3]]
115115
; RVA23-NEXT: [[VEC_IND_NEXT]] = add <vscale x 8 x i64> [[VEC_IND]], [[BROADCAST_SPLAT]]
116116
; RVA23-NEXT: [[TMP7:%.*]] = icmp eq i64 [[AVL_NEXT]], 0
117-
; RVA23-NEXT: br i1 [[TMP7]], label [[MIDDLE_BLOCK:%.*]], label [[VECTOR_BODY]], !llvm.loop [[LOOP10:![0-9]+]]
117+
; RVA23-NEXT: br i1 [[TMP7]], label [[MIDDLE_BLOCK:%.*]], label [[VECTOR_BODY]], !llvm.loop [[LOOP9:![0-9]+]]
118118
; RVA23: middle.block:
119119
; RVA23-NEXT: br label [[LOOP:%.*]]
120120
; RVA23: exit:
@@ -141,7 +141,7 @@ define void @predicated_strided_store(ptr %start) {
141141
; RVA23ZVL1024B-NEXT: [[AVL_NEXT]] = sub nuw i64 [[AVL]], [[TMP3]]
142142
; RVA23ZVL1024B-NEXT: [[VEC_IND_NEXT]] = add <vscale x 2 x i64> [[VEC_IND]], [[BROADCAST_SPLAT]]
143143
; RVA23ZVL1024B-NEXT: [[TMP7:%.*]] = icmp eq i64 [[AVL_NEXT]], 0
144-
; RVA23ZVL1024B-NEXT: br i1 [[TMP7]], label [[MIDDLE_BLOCK:%.*]], label [[VECTOR_BODY]], !llvm.loop [[LOOP10:![0-9]+]]
144+
; RVA23ZVL1024B-NEXT: br i1 [[TMP7]], label [[MIDDLE_BLOCK:%.*]], label [[VECTOR_BODY]], !llvm.loop [[LOOP9:![0-9]+]]
145145
; RVA23ZVL1024B: middle.block:
146146
; RVA23ZVL1024B-NEXT: br label [[LOOP:%.*]]
147147
; RVA23ZVL1024B: exit:
@@ -185,16 +185,16 @@ define void @store_to_addr_generated_from_invariant_addr(ptr noalias %p0, ptr no
185185
; CHECK-NEXT: [[TMP5:%.*]] = getelementptr i32, ptr [[P1:%.*]], <vscale x 2 x i64> [[VEC_IND]]
186186
; CHECK-NEXT: call void @llvm.vp.scatter.nxv2p0.nxv2p0(<vscale x 2 x ptr> [[BROADCAST_SPLAT1]], <vscale x 2 x ptr> align 8 [[TMP5]], <vscale x 2 x i1> splat (i1 true), i32 [[TMP3]])
187187
; CHECK-NEXT: [[TMP6:%.*]] = load i64, ptr [[P2:%.*]], align 4
188-
; CHECK-NEXT: [[BROADCAST_SPLATINSERT1:%.*]] = insertelement <vscale x 2 x i64> poison, i64 [[TMP6]], i64 0
189-
; CHECK-NEXT: [[BROADCAST_SPLAT2:%.*]] = shufflevector <vscale x 2 x i64> [[BROADCAST_SPLATINSERT1]], <vscale x 2 x i64> poison, <vscale x 2 x i32> zeroinitializer
190-
; CHECK-NEXT: [[TMP7:%.*]] = getelementptr i8, ptr [[P3:%.*]], <vscale x 2 x i64> [[BROADCAST_SPLAT2]]
188+
; CHECK-NEXT: [[TMP8:%.*]] = getelementptr i8, ptr [[P3:%.*]], i64 [[TMP6]]
189+
; CHECK-NEXT: [[BROADCAST_SPLATINSERT3:%.*]] = insertelement <vscale x 2 x ptr> poison, ptr [[TMP8]], i64 0
190+
; CHECK-NEXT: [[TMP7:%.*]] = shufflevector <vscale x 2 x ptr> [[BROADCAST_SPLATINSERT3]], <vscale x 2 x ptr> poison, <vscale x 2 x i32> zeroinitializer
191191
; CHECK-NEXT: call void @llvm.vp.scatter.nxv2i32.nxv2p0(<vscale x 2 x i32> zeroinitializer, <vscale x 2 x ptr> align 4 [[TMP7]], <vscale x 2 x i1> splat (i1 true), i32 [[TMP3]])
192192
; CHECK-NEXT: call void @llvm.vp.scatter.nxv2i32.nxv2p0(<vscale x 2 x i32> zeroinitializer, <vscale x 2 x ptr> align 4 [[TMP7]], <vscale x 2 x i1> splat (i1 true), i32 [[TMP3]])
193193
; CHECK-NEXT: call void @llvm.vp.scatter.nxv2i8.nxv2p0(<vscale x 2 x i8> zeroinitializer, <vscale x 2 x ptr> align 1 [[TMP7]], <vscale x 2 x i1> splat (i1 true), i32 [[TMP3]])
194194
; CHECK-NEXT: [[AVL_NEXT]] = sub nuw i64 [[AVL]], [[TMP4]]
195195
; CHECK-NEXT: [[VEC_IND_NEXT]] = add <vscale x 2 x i64> [[VEC_IND]], [[BROADCAST_SPLAT]]
196196
; CHECK-NEXT: [[TMP9:%.*]] = icmp eq i64 [[AVL_NEXT]], 0
197-
; CHECK-NEXT: br i1 [[TMP9]], label [[MIDDLE_BLOCK:%.*]], label [[VECTOR_BODY]], !llvm.loop [[LOOP11:![0-9]+]]
197+
; CHECK-NEXT: br i1 [[TMP9]], label [[MIDDLE_BLOCK:%.*]], label [[VECTOR_BODY]], !llvm.loop [[LOOP10:![0-9]+]]
198198
; CHECK: middle.block:
199199
; CHECK-NEXT: br label [[LOOP:%.*]]
200200
; CHECK: exit:

llvm/test/Transforms/LoopVectorize/widen-gep-all-indices-invariant.ll

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,14 @@ define void @pr63340(ptr %A, ptr %B) {
88
; CHECK-NEXT: br label [[VECTOR_PH:%.*]]
99
; CHECK: vector.ph:
1010
; CHECK-NEXT: [[TMP0:%.*]] = getelementptr i8, ptr [[A]], i64 1
11-
; CHECK-NEXT: [[DOTSPLATINSERT:%.*]] = insertelement <4 x ptr> poison, ptr [[TMP0]], i64 0
12-
; CHECK-NEXT: [[DOTSPLAT:%.*]] = shufflevector <4 x ptr> [[DOTSPLATINSERT]], <4 x ptr> poison, <4 x i32> zeroinitializer
11+
; CHECK-NEXT: [[BROADCAST_SPLATINSERT:%.*]] = insertelement <4 x ptr> poison, ptr [[TMP0]], i64 0
12+
; CHECK-NEXT: [[BROADCAST_SPLAT:%.*]] = shufflevector <4 x ptr> [[BROADCAST_SPLATINSERT]], <4 x ptr> poison, <4 x i32> zeroinitializer
1313
; CHECK-NEXT: br label [[VECTOR_BODY:%.*]]
1414
; CHECK: vector.body:
1515
; CHECK-NEXT: [[INDEX:%.*]] = phi i32 [ 0, [[VECTOR_PH]] ], [ [[INDEX_NEXT:%.*]], [[VECTOR_BODY]] ]
1616
; CHECK-NEXT: [[OFFSET_IDX:%.*]] = trunc i32 [[INDEX]] to i8
1717
; CHECK-NEXT: [[TMP1:%.*]] = getelementptr inbounds ptr, ptr [[B]], i8 [[OFFSET_IDX]]
18-
; CHECK-NEXT: store <4 x ptr> [[DOTSPLAT]], ptr [[TMP1]], align 8
18+
; CHECK-NEXT: store <4 x ptr> [[BROADCAST_SPLAT]], ptr [[TMP1]], align 8
1919
; CHECK-NEXT: [[INDEX_NEXT]] = add nuw i32 [[INDEX]], 4
2020
; CHECK-NEXT: [[TMP2:%.*]] = icmp eq i32 [[INDEX_NEXT]], 128
2121
; CHECK-NEXT: br i1 [[TMP2]], label [[MIDDLE_BLOCK:%.*]], label [[VECTOR_BODY]], !llvm.loop [[LOOP0:![0-9]+]]
@@ -55,11 +55,11 @@ define void @wide_gep_index_invariant(ptr noalias %dst, ptr noalias %src, i64 %n
5555
; CHECK: vector.body:
5656
; CHECK-NEXT: [[INDEX:%.*]] = phi i64 [ 0, [[VECTOR_PH]] ], [ [[INDEX_NEXT:%.*]], [[VECTOR_BODY]] ]
5757
; CHECK-NEXT: [[TMP0:%.*]] = load ptr, ptr [[SRC]], align 8
58-
; CHECK-NEXT: [[BROADCAST_SPLATINSERT:%.*]] = insertelement <4 x ptr> poison, ptr [[TMP0]], i64 0
58+
; CHECK-NEXT: [[TMP1:%.*]] = getelementptr float, ptr [[TMP0]], i64 [[N]]
59+
; CHECK-NEXT: [[BROADCAST_SPLATINSERT:%.*]] = insertelement <4 x ptr> poison, ptr [[TMP1]], i64 0
5960
; CHECK-NEXT: [[BROADCAST_SPLAT:%.*]] = shufflevector <4 x ptr> [[BROADCAST_SPLATINSERT]], <4 x ptr> poison, <4 x i32> zeroinitializer
60-
; CHECK-NEXT: [[TMP1:%.*]] = getelementptr float, <4 x ptr> [[BROADCAST_SPLAT]], i64 [[N]]
6161
; CHECK-NEXT: [[TMP2:%.*]] = getelementptr ptr, ptr [[DST]], i64 [[INDEX]]
62-
; CHECK-NEXT: store <4 x ptr> [[TMP1]], ptr [[TMP2]], align 8
62+
; CHECK-NEXT: store <4 x ptr> [[BROADCAST_SPLAT]], ptr [[TMP2]], align 8
6363
; CHECK-NEXT: [[INDEX_NEXT]] = add nuw i64 [[INDEX]], 4
6464
; CHECK-NEXT: [[TMP3:%.*]] = icmp eq i64 [[INDEX_NEXT]], 100
6565
; CHECK-NEXT: br i1 [[TMP3]], label [[MIDDLE_BLOCK:%.*]], label [[VECTOR_BODY]], !llvm.loop [[LOOP3:![0-9]+]]

0 commit comments

Comments
 (0)