Skip to content

Commit 01a9be6

Browse files
committed
Call xform earlier
1 parent 023fbc9 commit 01a9be6

File tree

3 files changed

+41
-21
lines changed

3 files changed

+41
-21
lines changed

llvm/lib/Transforms/Vectorize/LoopVectorize.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8558,6 +8558,11 @@ VPlanPtr LoopVectorizationPlanner::tryToBuildVPlanWithVPRecipes(
85588558
*Plan))
85598559
return nullptr;
85608560

8561+
// Create whole-vector selects for find-last recurrences.
8562+
if (!VPlanTransforms::runPass(VPlanTransforms::handleFindLastReductions,
8563+
*Plan, RecipeBuilder))
8564+
return nullptr;
8565+
85618566
// Transform recipes to abstract recipes if it is legal and beneficial and
85628567
// clamp the range for better cost estimation.
85638568
// TODO: Enable following transform when the EVL-version of extended-reduction
@@ -8596,10 +8601,6 @@ VPlanPtr LoopVectorizationPlanner::tryToBuildVPlanWithVPRecipes(
85968601
*Plan, Builder))
85978602
return nullptr;
85988603

8599-
// Create whole-vector selects for find-last recurrences.
8600-
VPlanTransforms::runPass(VPlanTransforms::convertFindLastRecurrences, *Plan,
8601-
RecipeBuilder);
8602-
86038604
if (useActiveLaneMask(Style)) {
86048605
// TODO: Move checks to VPlanTransforms::addActiveLaneMask once
86058606
// TailFoldingStyle is visible there.

llvm/lib/Transforms/Vectorize/VPlanConstruction.cpp

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
//===----------------------------------------------------------------------===//
1313

1414
#include "LoopVectorizationPlanner.h"
15+
#include "VPRecipeBuilder.h"
1516
#include "VPlan.h"
1617
#include "VPlanCFG.h"
1718
#include "VPlanDominatorTree.h"
@@ -1002,10 +1003,10 @@ bool VPlanTransforms::handleMaxMinNumReductions(VPlan &Plan) {
10021003
return true;
10031004
}
10041005

1005-
void VPlanTransforms::convertFindLastRecurrences(
1006-
VPlan &Plan, VPRecipeBuilder &RecipeBuilder) {
1006+
bool VPlanTransforms::handleFindLastReductions(VPlan &Plan,
1007+
VPRecipeBuilder &RecipeBuilder) {
10071008
if (Plan.hasScalarVFOnly())
1008-
return;
1009+
return false;
10091010

10101011
// We want to create the following nodes:
10111012
// vec.body:
@@ -1029,8 +1030,20 @@ void VPlanTransforms::convertFindLastRecurrences(
10291030
continue;
10301031

10311032
// Find the condition for the select
1032-
auto *SR = cast<VPWidenSelectRecipe>(&PhiR->getBackedgeRecipe());
1033-
VPValue *Cond = SR->getCond();
1033+
auto *SelectR = cast<VPSingleDefRecipe>(&PhiR->getBackedgeRecipe());
1034+
VPValue *Cond = nullptr;
1035+
if (auto *WidenR = dyn_cast<VPWidenSelectRecipe>(SelectR))
1036+
Cond = WidenR->getCond();
1037+
else if (auto *RepR = dyn_cast<VPReplicateRecipe>(SelectR)) {
1038+
auto *SI = dyn_cast<SelectInst>(RepR->getUnderlyingInstr());
1039+
if (!SI)
1040+
return false;
1041+
auto *CmpI = dyn_cast<Instruction>(SI->getCondition());
1042+
if (!CmpI)
1043+
return false;
1044+
Cond = RecipeBuilder.getRecipe(CmpI)->getVPSingleValue();
1045+
} else
1046+
return false;
10341047

10351048
// Add mask phi
10361049
VPBuilder Builder = VPBuilder::getToInsertAfter(PhiR);
@@ -1040,21 +1053,23 @@ void VPlanTransforms::convertFindLastRecurrences(
10401053
Builder.insert(MaskPHI);
10411054

10421055
// Add select for mask
1043-
Builder.setInsertPoint(SR);
1056+
Builder.setInsertPoint(SelectR);
10441057
VPValue *AnyOf = Builder.createNaryOp(VPInstruction::AnyOf, {Cond});
10451058
VPValue *MaskSelect = Builder.createSelect(AnyOf, Cond, MaskPHI);
10461059
MaskPHI->addOperand(MaskSelect);
10471060

10481061
// Replace select for data
1049-
VPValue *DataSelect = Builder.createSelect(
1050-
AnyOf, SR->getOperand(1), SR->getOperand(2), SR->getDebugLoc());
1051-
SR->replaceAllUsesWith(DataSelect);
1052-
SR->eraseFromParent();
1062+
VPValue *DataSelect =
1063+
Builder.createSelect(AnyOf, SelectR->getOperand(1),
1064+
SelectR->getOperand(2), SelectR->getDebugLoc());
1065+
SelectR->replaceAllUsesWith(DataSelect);
1066+
SelectR->eraseFromParent();
10531067

10541068
// Find final reduction computation and replace it with an
10551069
// extract.last.active intrinsic.
10561070
VPInstruction *RdxResult = findComputeReductionResult(PhiR);
1057-
assert(RdxResult && "Unable to find Reduction Result Recipe");
1071+
if (!RdxResult)
1072+
return false;
10581073
Builder.setInsertPoint(RdxResult);
10591074
auto *ExtractLastActive =
10601075
Builder.createNaryOp(VPInstruction::ExtractLastActive,
@@ -1063,4 +1078,6 @@ void VPlanTransforms::convertFindLastRecurrences(
10631078
RdxResult->replaceAllUsesWith(ExtractLastActive);
10641079
RdxResult->eraseFromParent();
10651080
}
1081+
1082+
return true;
10661083
}

llvm/lib/Transforms/Vectorize/VPlanTransforms.h

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,14 @@ struct VPlanTransforms {
161161
/// this attempt was unsuccessful.
162162
static bool handleMaxMinNumReductions(VPlan &Plan);
163163

164+
/// Check if \p Plan contains any FindLast reductions. If it does, try to
165+
/// update the vector loop to save the appropriate state using selects
166+
/// for entire vectors for both the latest mask containing at least one active
167+
/// element and the corresponding data vector. Return false if this attempt
168+
/// was unsuccessful.
169+
static bool handleFindLastReductions(VPlan &Plan,
170+
VPRecipeBuilder &RecipeBuilder);
171+
164172
/// Clear NSW/NUW flags from reduction instructions if necessary.
165173
static void clearReductionWrapFlags(VPlan &Plan);
166174

@@ -390,12 +398,6 @@ struct VPlanTransforms {
390398
/// users in the original exit block using the VPIRInstruction wrapping to the
391399
/// LCSSA phi.
392400
static void addExitUsersForFirstOrderRecurrences(VPlan &Plan, VFRange &Range);
393-
394-
/// Change FindLast reductions to save the appropriate state using selects
395-
/// for entire vectors for both the latest mask containing at least one active
396-
/// element and the corresponding data vector.
397-
static void convertFindLastRecurrences(VPlan &Plan,
398-
VPRecipeBuilder &RecipeBuilder);
399401
};
400402

401403
} // namespace llvm

0 commit comments

Comments
 (0)