[llvm] 4efa097 - [LV] Allow tryToCreateWidenRecipe to return a VPValue, use for blends.
Florian Hahn via llvm-commits
llvm-commits at lists.llvm.org
Tue Feb 23 08:54:33 PST 2021
Author: Florian Hahn
Date: 2021-02-23T16:52:03Z
New Revision: 4efa097eb4c87d7ffe09a95a5b4ff372bdddda85
URL: https://github.com/llvm/llvm-project/commit/4efa097eb4c87d7ffe09a95a5b4ff372bdddda85
DIFF: https://github.com/llvm/llvm-project/commit/4efa097eb4c87d7ffe09a95a5b4ff372bdddda85.diff
LOG: [LV] Allow tryToCreateWidenRecipe to return a VPValue, use for blends.
Generalize the return value of tryToCreateWidenRecipe to return either a
newly create recipe or an existing VPValue. Use this to avoid creating
unnecessary VPBlendRecipes.
Fixes PR44800.
Added:
Modified:
llvm/lib/Transforms/Vectorize/LoopVectorize.cpp
llvm/lib/Transforms/Vectorize/VPRecipeBuilder.h
llvm/test/Transforms/LoopVectorize/single-value-blend-phis.ll
Removed:
################################################################################
diff --git a/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp b/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp
index cebb8ece2827..8cf043cfa727 100644
--- a/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp
+++ b/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp
@@ -8249,15 +8249,24 @@ VPRecipeBuilder::tryToOptimizeInductionTruncate(TruncInst *I, VFRange &Range,
return nullptr;
}
-VPBlendRecipe *VPRecipeBuilder::tryToBlend(PHINode *Phi, VPlanPtr &Plan) {
+VPRecipeOrVPValueTy VPRecipeBuilder::tryToBlend(PHINode *Phi, VPlanPtr &Plan) {
+ // If all incoming values are equal, the incoming VPValue can be used directly
+ // instead of creating a new VPBlendRecipe.
+ Value *FirstIncoming = Phi->getIncomingValue(0);
+ if (all_of(Phi->incoming_values(), [FirstIncoming](const Value *Inc) {
+ return FirstIncoming == Inc;
+ })) {
+ return Plan->getOrAddVPValue(Phi->getIncomingValue(0));
+ }
+
// We know that all PHIs in non-header blocks are converted into selects, so
// we don't have to worry about the insertion order and we can just use the
// builder. At this point we generate the predication tree. There may be
// duplications since this is a simple recursive scan, but future
// optimizations will clean it up.
-
SmallVector<VPValue *, 2> Operands;
unsigned NumIncoming = Phi->getNumIncomingValues();
+
for (unsigned In = 0; In < NumIncoming; In++) {
VPValue *EdgeMask =
createEdgeMask(Phi->getIncomingBlock(In), Phi->getParent(), Plan);
@@ -8449,9 +8458,9 @@ VPRegionBlock *VPRecipeBuilder::createReplicateRegion(Instruction *Instr,
return Region;
}
-VPRecipeBase *VPRecipeBuilder::tryToCreateWidenRecipe(Instruction *Instr,
- VFRange &Range,
- VPlanPtr &Plan) {
+VPRecipeOrVPValueTy VPRecipeBuilder::tryToCreateWidenRecipe(Instruction *Instr,
+ VFRange &Range,
+ VPlanPtr &Plan) {
// First, check for specific widening recipes that deal with calls, memory
// operations, inductions and Phi nodes.
if (auto *CI = dyn_cast<CallInst>(Instr))
@@ -8623,17 +8632,15 @@ VPlanPtr LoopVectorizationPlanner::buildVPlanWithVPRecipes(
if (isa<BranchInst>(Instr) || DeadInstructions.count(Instr))
continue;
- if (auto Recipe =
+ if (auto RecipeOrValue =
RecipeBuilder.tryToCreateWidenRecipe(Instr, Range, Plan)) {
-
- // VPBlendRecipes with a single incoming (value, mask) pair are no-ops.
- // Use the incoming value directly.
- if (isa<VPBlendRecipe>(Recipe) && Recipe->getNumOperands() <= 2) {
- Plan->removeVPValueFor(Instr);
- Plan->addVPValue(Instr, Recipe->getOperand(0));
- delete Recipe;
+ // If Instr can be simplified to an existing VPValue, use it.
+ if (RecipeOrValue.is<VPValue *>()) {
+ Plan->addVPValue(Instr, RecipeOrValue.get<VPValue *>());
continue;
}
+ // Otherwise, add the new recipe.
+ VPRecipeBase *Recipe = RecipeOrValue.get<VPRecipeBase *>();
for (auto *Def : Recipe->definedValues()) {
auto *UV = Def->getUnderlyingValue();
Plan->addVPValue(UV, Def);
diff --git a/llvm/lib/Transforms/Vectorize/VPRecipeBuilder.h b/llvm/lib/Transforms/Vectorize/VPRecipeBuilder.h
index 873701676067..956ced719b52 100644
--- a/llvm/lib/Transforms/Vectorize/VPRecipeBuilder.h
+++ b/llvm/lib/Transforms/Vectorize/VPRecipeBuilder.h
@@ -12,6 +12,7 @@
#include "LoopVectorizationPlanner.h"
#include "VPlan.h"
#include "llvm/ADT/DenseMap.h"
+#include "llvm/ADT/PointerUnion.h"
#include "llvm/IR/IRBuilder.h"
namespace llvm {
@@ -20,6 +21,8 @@ class LoopVectorizationLegality;
class LoopVectorizationCostModel;
class TargetLibraryInfo;
+using VPRecipeOrVPValueTy = PointerUnion<VPRecipeBase *, VPValue *>;
+
/// Helper class to create VPRecipies from IR instructions.
class VPRecipeBuilder {
/// The loop that we evaluate.
@@ -75,10 +78,11 @@ class VPRecipeBuilder {
tryToOptimizeInductionTruncate(TruncInst *I, VFRange &Range,
VPlan &Plan) const;
- /// Handle non-loop phi nodes. Currently all such phi nodes are turned into
- /// a sequence of select instructions as the vectorizer currently performs
- /// full if-conversion.
- VPBlendRecipe *tryToBlend(PHINode *Phi, VPlanPtr &Plan);
+ /// Handle non-loop phi nodes. Return a VPValue, if all incoming values match
+ /// or a new VPBlendRecipe otherwise. Currently all such phi nodes are turned
+ /// into a sequence of select instructions as the vectorizer currently
+ /// performs full if-conversion.
+ VPRecipeOrVPValueTy tryToBlend(PHINode *Phi, VPlanPtr &Plan);
/// Handle call instructions. If \p CI can be widened for \p Range.Start,
/// return a new VPWidenCallRecipe. Range.End may be decreased to ensure same
@@ -99,10 +103,12 @@ class VPRecipeBuilder {
: OrigLoop(OrigLoop), TLI(TLI), Legal(Legal), CM(CM), PSE(PSE),
Builder(Builder) {}
- /// Check if a recipe can be create for \p I withing the given VF \p Range.
- /// If a recipe can be created, return it. Otherwise return nullptr.
- VPRecipeBase *tryToCreateWidenRecipe(Instruction *Instr, VFRange &Range,
- VPlanPtr &Plan);
+ /// Check if an existing VPValue can be used for \p Instr or a recipe can be
+ /// create for \p I withing the given VF \p Range. If an existing VPValue can
+ /// be used or if a recipe can be created, return it. Otherwise return a
+ /// VPRecipeOrVPValueTy with nullptr.
+ VPRecipeOrVPValueTy tryToCreateWidenRecipe(Instruction *Instr, VFRange &Range,
+ VPlanPtr &Plan);
/// Set the recipe created for given ingredient. This operation is a no-op for
/// ingredients that were not marked using a nullptr entry in the map.
diff --git a/llvm/test/Transforms/LoopVectorize/single-value-blend-phis.ll b/llvm/test/Transforms/LoopVectorize/single-value-blend-phis.ll
index b98a3ab7336d..ebcb9f0bfe45 100644
--- a/llvm/test/Transforms/LoopVectorize/single-value-blend-phis.ll
+++ b/llvm/test/Transforms/LoopVectorize/single-value-blend-phis.ll
@@ -382,3 +382,65 @@ loop.latch:
exit:
ret void
}
+
+; Test case for PR44800.
+define void @duplicated_incoming_blocks_blend(i32 %x, i32* %ptr) {
+; CHECK-LABEL: @duplicated_incoming_blocks_blend(
+; CHECK-NEXT: entry:
+; CHECK-NEXT: br i1 false, label [[SCALAR_PH:%.*]], label [[VECTOR_PH:%.*]]
+; CHECK: vector.ph:
+; CHECK-NEXT: [[BROADCAST_SPLATINSERT:%.*]] = insertelement <2 x i32> poison, i32 [[X:%.*]], i32 0
+; CHECK-NEXT: [[BROADCAST_SPLAT:%.*]] = shufflevector <2 x i32> [[BROADCAST_SPLATINSERT]], <2 x i32> poison, <2 x i32> zeroinitializer
+; CHECK-NEXT: br label [[VECTOR_BODY:%.*]]
+; CHECK: vector.body:
+; CHECK-NEXT: [[INDEX:%.*]] = phi i32 [ 0, [[VECTOR_PH]] ], [ [[INDEX_NEXT:%.*]], [[VECTOR_BODY]] ]
+; CHECK-NEXT: [[VEC_IND:%.*]] = phi <2 x i32> [ <i32 0, i32 1>, [[VECTOR_PH]] ], [ [[VEC_IND_NEXT:%.*]], [[VECTOR_BODY]] ]
+; CHECK-NEXT: [[TMP0:%.*]] = icmp ugt <2 x i32> [[VEC_IND]], [[BROADCAST_SPLAT]]
+; CHECK-NEXT: [[TMP1:%.*]] = extractelement <2 x i32> [[VEC_IND]], i32 0
+; CHECK-NEXT: [[TMP2:%.*]] = getelementptr i32, i32* [[PTR:%.*]], i32 [[TMP1]]
+; CHECK-NEXT: [[TMP3:%.*]] = getelementptr i32, i32* [[TMP2]], i32 0
+; CHECK-NEXT: [[TMP4:%.*]] = bitcast i32* [[TMP3]] to <2 x i32>*
+; CHECK-NEXT: store <2 x i32> [[VEC_IND]], <2 x i32>* [[TMP4]], align 4
+; CHECK-NEXT: [[INDEX_NEXT]] = add i32 [[INDEX]], 2
+; CHECK-NEXT: [[VEC_IND_NEXT]] = add <2 x i32> [[VEC_IND]], <i32 2, i32 2>
+; CHECK-NEXT: [[TMP5:%.*]] = icmp eq i32 [[INDEX_NEXT]], 1000
+; CHECK-NEXT: br i1 [[TMP5]], label [[MIDDLE_BLOCK:%.*]], label [[VECTOR_BODY]], [[LOOP10:!llvm.loop !.*]]
+; CHECK: middle.block:
+; CHECK-NEXT: [[CMP_N:%.*]] = icmp eq i32 1000, 1000
+; CHECK-NEXT: br i1 [[CMP_N]], label [[EXIT:%.*]], label [[SCALAR_PH]]
+; CHECK: scalar.ph:
+; CHECK-NEXT: [[BC_RESUME_VAL:%.*]] = phi i32 [ 1000, [[MIDDLE_BLOCK]] ], [ 0, [[ENTRY:%.*]] ]
+; CHECK-NEXT: br label [[LOOP_HEADER:%.*]]
+; CHECK: loop.header:
+; CHECK-NEXT: [[IV:%.*]] = phi i32 [ [[BC_RESUME_VAL]], [[SCALAR_PH]] ], [ [[ADD_I:%.*]], [[LOOP_LATCH:%.*]] ]
+; CHECK-NEXT: [[C_0:%.*]] = icmp ugt i32 [[IV]], [[X]]
+; CHECK-NEXT: br i1 [[C_0]], label [[LOOP_LATCH]], label [[LOOP_LATCH]]
+; CHECK: loop.latch:
+; CHECK-NEXT: [[P:%.*]] = phi i32 [ [[IV]], [[LOOP_HEADER]] ], [ [[IV]], [[LOOP_HEADER]] ]
+; CHECK-NEXT: [[GEP_PTR:%.*]] = getelementptr i32, i32* [[PTR]], i32 [[P]]
+; CHECK-NEXT: store i32 [[P]], i32* [[GEP_PTR]], align 4
+; CHECK-NEXT: [[ADD_I]] = add nsw i32 [[P]], 1
+; CHECK-NEXT: [[CMP:%.*]] = icmp slt i32 [[ADD_I]], 1000
+; CHECK-NEXT: br i1 [[CMP]], label [[LOOP_HEADER]], label [[EXIT]], [[LOOP11:!llvm.loop !.*]]
+; CHECK: exit:
+; CHECK-NEXT: ret void
+;
+entry:
+ br label %loop.header
+
+loop.header:
+ %iv = phi i32 [ 0 , %entry ], [ %add.i, %loop.latch ]
+ %c.0 = icmp ugt i32 %iv, %x
+ br i1 %c.0, label %loop.latch, label %loop.latch
+
+loop.latch:
+ %p = phi i32 [ %iv, %loop.header ], [ %iv, %loop.header ]
+ %gep.ptr = getelementptr i32, i32* %ptr, i32 %p
+ store i32 %p, i32* %gep.ptr
+ %add.i = add nsw i32 %p, 1
+ %cmp = icmp slt i32 %add.i, 1000
+ br i1 %cmp, label %loop.header, label %exit
+
+exit:
+ ret void
+}
More information about the llvm-commits
mailing list