[PATCH] D149170: [SLP] Account for phi cost in both scalar and vectors
Luke Lau via Phabricator via llvm-commits
llvm-commits at lists.llvm.org
Tue Apr 25 10:25:09 PDT 2023
luke created this revision.
luke added reviewers: ABataev, RKSimon, craig.topper, reames.
Herald added subscribers: asb, vporpo, pmatos, frasercrmck, luismarques, apazos, sameer.abuasal, s.egerton, Jim, jocewei, PkmX, the_o, brucehoult, MartinMosbeck, rogfer01, edward-jones, zzheng, jrtc27, niosHD, sabuasal, simoncook, johnrusso, rbar, hiraditya.
Herald added a project: All.
luke requested review of this revision.
Herald added subscribers: llvm-commits, pcwang-thead, MaskRay.
Herald added a project: LLVM.
Use TTI::getPHICost to work out the cost of a phi instruction for
scalars and vectors, because they may not be free. Specifically, if the
phi has constant operands, there may be a cost to materializing them.
Fixes https://github.com/llvm/llvm-project/issues/62327
Repository:
rG LLVM Github Monorepo
https://reviews.llvm.org/D149170
Files:
llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp
llvm/test/Transforms/SLPVectorizer/RISCV/phi-const.ll
Index: llvm/test/Transforms/SLPVectorizer/RISCV/phi-const.ll
===================================================================
--- llvm/test/Transforms/SLPVectorizer/RISCV/phi-const.ll
+++ llvm/test/Transforms/SLPVectorizer/RISCV/phi-const.ll
@@ -11,14 +11,17 @@
; CHECK-LABEL: define void @f
; CHECK-SAME: (ptr [[P:%.*]], i1 [[C:%.*]]) #[[ATTR0:[0-9]+]] {
; CHECK-NEXT: [[P_0:%.*]] = getelementptr i8, ptr [[P]]
+; CHECK-NEXT: [[P_1:%.*]] = getelementptr i8, ptr [[P]], i32 1
; CHECK-NEXT: br i1 [[C]], label [[A:%.*]], label [[B:%.*]]
; CHECK: a:
; CHECK-NEXT: br label [[D:%.*]]
; CHECK: b:
; CHECK-NEXT: br label [[D]]
; CHECK: d:
-; CHECK-NEXT: [[TMP1:%.*]] = phi <2 x i8> [ <i8 1, i8 -1>, [[A]] ], [ <i8 -1, i8 1>, [[B]] ]
-; CHECK-NEXT: store <2 x i8> [[TMP1]], ptr [[P_0]], align 1
+; CHECK-NEXT: [[X:%.*]] = phi i8 [ 1, [[A]] ], [ -1, [[B]] ]
+; CHECK-NEXT: [[Y:%.*]] = phi i8 [ -1, [[A]] ], [ 1, [[B]] ]
+; CHECK-NEXT: store i8 [[X]], ptr [[P_0]], align 1
+; CHECK-NEXT: store i8 [[Y]], ptr [[P_1]], align 1
; CHECK-NEXT: ret void
;
%p.0 = getelementptr i8, ptr %p
@@ -40,15 +43,19 @@
; CHECK-LABEL: define void @g
; CHECK-SAME: (ptr [[P:%.*]], i1 [[C:%.*]]) #[[ATTR0]] {
; CHECK-NEXT: [[P_0:%.*]] = getelementptr i8, ptr [[P]]
+; CHECK-NEXT: [[P_1:%.*]] = getelementptr i8, ptr [[P]], i32 1
; CHECK-NEXT: br i1 [[C]], label [[A:%.*]], label [[B:%.*]]
; CHECK: a:
; CHECK-NEXT: br label [[D:%.*]]
; CHECK: b:
; CHECK-NEXT: br label [[D]]
; CHECK: d:
-; CHECK-NEXT: [[TMP1:%.*]] = phi <2 x i8> [ <i8 1, i8 -1>, [[A]] ], [ <i8 -1, i8 1>, [[B]] ]
-; CHECK-NEXT: [[TMP2:%.*]] = add <2 x i8> [[TMP1]], <i8 1, i8 1>
-; CHECK-NEXT: store <2 x i8> [[TMP2]], ptr [[P_0]], align 1
+; CHECK-NEXT: [[X:%.*]] = phi i8 [ 1, [[A]] ], [ -1, [[B]] ]
+; CHECK-NEXT: [[Y:%.*]] = phi i8 [ -1, [[A]] ], [ 1, [[B]] ]
+; CHECK-NEXT: [[X_ADD:%.*]] = add i8 [[X]], 1
+; CHECK-NEXT: [[Y_ADD:%.*]] = add i8 [[Y]], 1
+; CHECK-NEXT: store i8 [[X_ADD]], ptr [[P_0]], align 1
+; CHECK-NEXT: store i8 [[Y_ADD]], ptr [[P_1]], align 1
; CHECK-NEXT: ret void
;
%p.0 = getelementptr i8, ptr %p
Index: llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp
===================================================================
--- llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp
+++ llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp
@@ -7331,23 +7331,24 @@
InstructionCost ScalarCost = 0;
SmallPtrSet<const TreeEntry *, 4> CountedOps;
for (Value *V : VL) {
- auto *PHI = dyn_cast<PHINode>(V);
- if (!PHI)
- continue;
+ auto *PHI = cast<PHINode>(V);
- ValueList Operands(PHI->getNumIncomingValues(), nullptr);
- for (unsigned I = 0, N = PHI->getNumIncomingValues(); I < N; ++I) {
- Value *Op = PHI->getIncomingValue(I);
- Operands[I] = Op;
- }
+ ValueList Operands(PHI->incoming_values());
if (const TreeEntry *OpTE = getTreeEntry(Operands.front()))
if (OpTE->isSame(Operands) && CountedOps.insert(OpTE).second)
if (!OpTE->ReuseShuffleIndices.empty())
- ScalarCost += TTI::TCC_Basic * (OpTE->ReuseShuffleIndices.size() -
- OpTE->Scalars.size());
+ ScalarCost +=
+ TTI->getPHICost(PHI->getType(), TTI::TCK_RecipThroughput) *
+ (OpTE->ReuseShuffleIndices.size() - OpTE->Scalars.size());
}
- return CommonCost - ScalarCost;
+ SmallVector<TTI::OperandValueInfo, 4> OpInfos;
+ for (unsigned I = 0; I < E->getMainOp()->getNumOperands(); I++)
+ OpInfos.push_back(getOperandInfo(VL, I));
+ InstructionCost VecCost =
+ TTI->getPHICost(VecTy, TTI::TCK_RecipThroughput, OpInfos);
+
+ return VecCost + CommonCost - ScalarCost;
}
case Instruction::ExtractValue:
case Instruction::ExtractElement: {
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D149170.516841.patch
Type: text/x-patch
Size: 3931 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20230425/cd062209/attachment.bin>
More information about the llvm-commits
mailing list