Skip to content

Commit 36e1032

Browse files
committed
!fixup, formatting and address comments.
1 parent 84f8a46 commit 36e1032

File tree

5 files changed

+56
-36
lines changed

5 files changed

+56
-36
lines changed

llvm/lib/Transforms/Vectorize/LoopVectorize.cpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9419,6 +9419,10 @@ LoopVectorizationPlanner::tryToBuildVPlanWithVPRecipes(VFRange &Range) {
94199419
"entry block must be set to a VPRegionBlock having a non-empty entry "
94209420
"VPBasicBlock");
94219421

9422+
for (ElementCount VF : Range)
9423+
Plan->addVF(VF);
9424+
Plan->setName("Initial VPlan");
9425+
94229426
// Update wide induction increments to use the same step as the corresponding
94239427
// wide induction. This enables detecting induction increments directly in
94249428
// VPlan and removes redundant splats.
@@ -9465,9 +9469,13 @@ LoopVectorizationPlanner::tryToBuildVPlanWithVPRecipes(VFRange &Range) {
94659469
CostCtx, Range);
94669470
}
94679471

9472+
// Update VF after convertToAbstractRecipes. Cannot set the VF here since
9473+
// `handleUncountableEarlyExit` will check the VF of the plan, need to set
9474+
// before it and update.
9475+
// TODO: Use a better method that only set the VF for plan once.
9476+
Plan->clearVF();
94689477
for (ElementCount VF : Range)
94699478
Plan->addVF(VF);
9470-
Plan->setName("Initial VPlan");
94719479

94729480
// Interleave memory: for each Interleave Group we marked earlier as relevant
94739481
// for this VPlan, replace the Recipes widening its memory instructions with a

llvm/lib/Transforms/Vectorize/VPlan.h

Lines changed: 29 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2291,7 +2291,7 @@ class VPReductionRecipe : public VPRecipeWithIRFlags {
22912291
}
22922292

22932293
/// For VPExtendedReductionRecipe.
2294-
/// Note that IsNonNeg flag and the debug location are for extend instruction.
2294+
/// Note that IsNonNeg flag and the debug location are from the extend.
22952295
VPReductionRecipe(const unsigned char SC, const RecurKind RdxKind,
22962296
ArrayRef<VPValue *> Operands, VPValue *CondOp,
22972297
bool IsOrdered, NonNegFlagsTy NonNeg, DebugLoc DL)
@@ -2302,7 +2302,7 @@ class VPReductionRecipe : public VPRecipeWithIRFlags {
23022302
}
23032303

23042304
/// For VPMulAccumulateReductionRecipe.
2305-
/// Note that the NUW/NSW and DL are for mul instruction.
2305+
/// Note that the NUW/NSW and DL are from the Mul.
23062306
VPReductionRecipe(const unsigned char SC, const RecurKind RdxKind,
23072307
ArrayRef<VPValue *> Operands, VPValue *CondOp,
23082308
bool IsOrdered, WrapFlagsTy WrapFlags, DebugLoc DL)
@@ -2320,9 +2320,9 @@ class VPReductionRecipe : public VPRecipeWithIRFlags {
23202320
ArrayRef<VPValue *>({ChainOp, VecOp}), CondOp,
23212321
IsOrdered, DL) {}
23222322

2323-
VPReductionRecipe(const RecurKind RdxKind, FastMathFlags FMFs, VPValue *ChainOp,
2324-
VPValue *VecOp, VPValue *CondOp, bool IsOrdered,
2325-
DebugLoc DL = {})
2323+
VPReductionRecipe(const RecurKind RdxKind, FastMathFlags FMFs,
2324+
VPValue *ChainOp, VPValue *VecOp, VPValue *CondOp,
2325+
bool IsOrdered, DebugLoc DL = {})
23262326
: VPReductionRecipe(VPDef::VPReductionSC, RdxKind, FMFs, nullptr,
23272327
ArrayRef<VPValue *>({ChainOp, VecOp}), CondOp,
23282328
IsOrdered, DL) {}
@@ -2434,16 +2434,17 @@ class VPExtendedReductionRecipe : public VPReductionRecipe {
24342434
VPExtendedReductionRecipe(VPExtendedReductionRecipe *ExtRed)
24352435
: VPReductionRecipe(
24362436
VPDef::VPExtendedReductionSC, ExtRed->getRecurrenceKind(),
2437-
{ExtRed->getChainOp(), ExtRed->getVecOp()},
2438-
ExtRed->getCondOp(), ExtRed->isOrdered(), NonNegFlagsTy(ExtRed->isNonNeg()), ExtRed->getDebugLoc()),
2437+
{ExtRed->getChainOp(), ExtRed->getVecOp()}, ExtRed->getCondOp(),
2438+
ExtRed->isOrdered(), NonNegFlagsTy(ExtRed->isNonNeg()),
2439+
ExtRed->getDebugLoc()),
24392440
ExtOp(ExtRed->getExtOpcode()), ResultTy(ExtRed->getResultType()) {}
24402441

24412442
public:
24422443
VPExtendedReductionRecipe(VPReductionRecipe *R, VPWidenCastRecipe *Ext)
2443-
: VPReductionRecipe(
2444-
VPDef::VPExtendedReductionSC, R->getRecurrenceKind(),
2445-
{R->getChainOp(), Ext->getOperand(0)}, R->getCondOp(),
2446-
R->isOrdered(), NonNegFlagsTy(Ext->isNonNeg()), Ext->getDebugLoc()),
2444+
: VPReductionRecipe(VPDef::VPExtendedReductionSC, R->getRecurrenceKind(),
2445+
{R->getChainOp(), Ext->getOperand(0)}, R->getCondOp(),
2446+
R->isOrdered(), NonNegFlagsTy(Ext->isNonNeg()),
2447+
Ext->getDebugLoc()),
24472448
ExtOp(Ext->getOpcode()), ResultTy(Ext->getResultType()) {}
24482449

24492450
~VPExtendedReductionRecipe() override = default;
@@ -2472,9 +2473,7 @@ class VPExtendedReductionRecipe : public VPReductionRecipe {
24722473
#endif
24732474

24742475
/// The scalar type after extended.
2475-
Type *getResultType() const {
2476-
return ResultTy;
2477-
}
2476+
Type *getResultType() const { return ResultTy; }
24782477

24792478
/// Is the extend ZExt?
24802479
bool isZExt() const { return getExtOpcode() == Instruction::ZExt; }
@@ -2500,11 +2499,13 @@ class VPMulAccumulateReductionRecipe : public VPReductionRecipe {
25002499
/// For cloning VPMulAccumulateReductionRecipe.
25012500
VPMulAccumulateReductionRecipe(VPMulAccumulateReductionRecipe *MulAcc)
25022501
: VPReductionRecipe(
2503-
VPDef::VPMulAccumulateReductionSC,
2504-
MulAcc->getRecurrenceKind(),
2502+
VPDef::VPMulAccumulateReductionSC, MulAcc->getRecurrenceKind(),
25052503
{MulAcc->getChainOp(), MulAcc->getVecOp0(), MulAcc->getVecOp1()},
2506-
MulAcc->getCondOp(), MulAcc->isOrdered(), WrapFlagsTy(MulAcc->hasNoUnsignedWrap(), MulAcc->hasNoSignedWrap()), MulAcc->getDebugLoc()),
2507-
ExtOp(MulAcc->getExtOpcode()), IsNonNeg(MulAcc->isNonNeg()), ResultTy(MulAcc->getResultType()) {}
2504+
MulAcc->getCondOp(), MulAcc->isOrdered(),
2505+
WrapFlagsTy(MulAcc->hasNoUnsignedWrap(), MulAcc->hasNoSignedWrap()),
2506+
MulAcc->getDebugLoc()),
2507+
ExtOp(MulAcc->getExtOpcode()), IsNonNeg(MulAcc->isNonNeg()),
2508+
ResultTy(MulAcc->getResultType()) {}
25082509

25092510
public:
25102511
VPMulAccumulateReductionRecipe(VPReductionRecipe *R, VPWidenRecipe *Mul,
@@ -2516,8 +2517,10 @@ class VPMulAccumulateReductionRecipe : public VPReductionRecipe {
25162517
R->getCondOp(), R->isOrdered(),
25172518
WrapFlagsTy(Mul->hasNoUnsignedWrap(), Mul->hasNoSignedWrap()),
25182519
R->getDebugLoc()),
2519-
ExtOp(Ext0->getOpcode()), IsNonNeg(Ext0->isNonNeg()), ResultTy(ResultTy) {
2520-
assert(RecurrenceDescriptor::getOpcode(getRecurrenceKind())== Instruction::Add &&
2520+
ExtOp(Ext0->getOpcode()), IsNonNeg(Ext0->isNonNeg()),
2521+
ResultTy(ResultTy) {
2522+
assert(RecurrenceDescriptor::getOpcode(getRecurrenceKind()) ==
2523+
Instruction::Add &&
25212524
"The reduction instruction in MulAccumulateteReductionRecipe must "
25222525
"be Add");
25232526
}
@@ -2530,7 +2533,8 @@ class VPMulAccumulateReductionRecipe : public VPReductionRecipe {
25302533
WrapFlagsTy(Mul->hasNoUnsignedWrap(), Mul->hasNoSignedWrap()),
25312534
R->getDebugLoc()),
25322535
ExtOp(Instruction::CastOps::CastOpsEnd) {
2533-
assert(RecurrenceDescriptor::getOpcode(getRecurrenceKind()) == Instruction::Add &&
2536+
assert(RecurrenceDescriptor::getOpcode(getRecurrenceKind()) ==
2537+
Instruction::Add &&
25342538
"The reduction instruction in MulAccumulateReductionRecipe must be "
25352539
"Add");
25362540
}
@@ -2562,7 +2566,8 @@ class VPMulAccumulateReductionRecipe : public VPReductionRecipe {
25622566
#endif
25632567

25642568
Type *getResultType() const {
2565-
assert(isExtended() && "Only support getResultType when this recipe contains implicit extend.");
2569+
assert(isExtended() && "Only support getResultType when this recipe "
2570+
"contains implicit extend.");
25662571
return ResultTy;
25672572
}
25682573

@@ -3792,6 +3797,8 @@ class VPlan {
37923797
VFs.insert(VF);
37933798
}
37943799

3800+
void clearVF() { VFs.clear(); }
3801+
37953802
bool hasVF(ElementCount VF) const { return VFs.count(VF); }
37963803
bool hasScalableVF() const {
37973804
return any_of(VFs, [](ElementCount VF) { return VF.isScalable(); });

llvm/lib/Transforms/Vectorize/VPlanRecipes.cpp

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2396,6 +2396,8 @@ InstructionCost VPReductionRecipe::computeCost(ElementCount VF,
23962396
auto *VectorTy = cast<VectorType>(toVectorTy(ElementTy, VF));
23972397
unsigned Opcode = RecurrenceDescriptor::getOpcode(RdxKind);
23982398
FastMathFlags FMFs = getFastMathFlags();
2399+
std::optional<FastMathFlags> OptionalFMF =
2400+
ElementTy->isFloatingPointTy() ? std::make_optional(FMFs) : std::nullopt;
23992401

24002402
// TODO: Support any-of reductions.
24012403
assert(
@@ -2410,12 +2412,7 @@ InstructionCost VPReductionRecipe::computeCost(ElementCount VF,
24102412
return Ctx.TTI.getMinMaxReductionCost(Id, VectorTy, FMFs, Ctx.CostKind);
24112413
}
24122414

2413-
if (ElementTy->isFloatingPointTy())
2414-
return Ctx.TTI.getArithmeticReductionCost(Opcode, VectorTy, FMFs,
2415-
Ctx.CostKind);
2416-
// Cannot get correct cost when quering TTI with FMFs not contains `reassoc`
2417-
// for non-FP reductions.
2418-
return Ctx.TTI.getArithmeticReductionCost(Opcode, VectorTy, std::nullopt,
2415+
return Ctx.TTI.getArithmeticReductionCost(Opcode, VectorTy, OptionalFMF,
24192416
Ctx.CostKind);
24202417
}
24212418

@@ -2426,7 +2423,6 @@ VPExtendedReductionRecipe::computeCost(ElementCount VF,
24262423
Type *RedTy = Ctx.Types.inferScalarType(this);
24272424
auto *SrcVecTy =
24282425
cast<VectorType>(toVectorTy(Ctx.Types.inferScalarType(getVecOp()), VF));
2429-
24302426
assert(RedTy->isIntegerTy() &&
24312427
"ExtendedReduction only support integer type currently.");
24322428
return Ctx.TTI.getExtendedReductionCost(Opcode, isZExt(), RedTy, SrcVecTy,
@@ -2439,7 +2435,6 @@ VPMulAccumulateReductionRecipe::computeCost(ElementCount VF,
24392435
Type *RedTy = Ctx.Types.inferScalarType(this);
24402436
auto *SrcVecTy =
24412437
cast<VectorType>(toVectorTy(Ctx.Types.inferScalarType(getVecOp0()), VF));
2442-
24432438
return Ctx.TTI.getMulAccReductionCost(isZExt(), RedTy, SrcVecTy,
24442439
Ctx.CostKind);
24452440
}
@@ -2494,7 +2489,10 @@ void VPExtendedReductionRecipe::print(raw_ostream &O, const Twine &Indent,
24942489
O << " = ";
24952490
getChainOp()->printAsOperand(O, SlotTracker);
24962491
O << " +";
2497-
O << " reduce." << Instruction::getOpcodeName(RecurrenceDescriptor::getOpcode(getRecurrenceKind())) << " (";
2492+
O << " reduce."
2493+
<< Instruction::getOpcodeName(
2494+
RecurrenceDescriptor::getOpcode(getRecurrenceKind()))
2495+
<< " (";
24982496
getVecOp()->printAsOperand(O, SlotTracker);
24992497
O << " extended to " << *getResultType();
25002498
if (isConditional()) {
@@ -2511,7 +2509,10 @@ void VPMulAccumulateReductionRecipe::print(raw_ostream &O, const Twine &Indent,
25112509
O << " = ";
25122510
getChainOp()->printAsOperand(O, SlotTracker);
25132511
O << " + ";
2514-
O << "reduce." << Instruction::getOpcodeName(RecurrenceDescriptor::getOpcode(getRecurrenceKind())) << " (";
2512+
O << "reduce."
2513+
<< Instruction::getOpcodeName(
2514+
RecurrenceDescriptor::getOpcode(getRecurrenceKind()))
2515+
<< " (";
25152516
O << "mul";
25162517
printFlags(O);
25172518
if (isExtended())

llvm/lib/Transforms/Vectorize/VPlanTransforms.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include "VPlanAnalysis.h"
1818
#include "VPlanCFG.h"
1919
#include "VPlanDominatorTree.h"
20+
#include "VPlanHelpers.h"
2021
#include "VPlanPatternMatch.h"
2122
#include "VPlanUtils.h"
2223
#include "VPlanVerifier.h"
@@ -2384,7 +2385,8 @@ tryToMatchAndCreateMulAccumulateReduction(VPReductionRecipe *Red,
23842385
IsMulAccValidAndClampRange(RecipeA->getOpcode() ==
23852386
Instruction::CastOps::ZExt,
23862387
Mul, RecipeA, RecipeB, nullptr))
2387-
return new VPMulAccumulateReductionRecipe(Red, Mul, RecipeA, RecipeB, RecipeA->getResultType());
2388+
return new VPMulAccumulateReductionRecipe(Red, Mul, RecipeA, RecipeB,
2389+
RecipeA->getResultType());
23882390
// Matched reduce.add(mul)
23892391
if (IsMulAccValidAndClampRange(true, Mul, nullptr, nullptr, nullptr))
23902392
return new VPMulAccumulateReductionRecipe(Red, Mul);
@@ -2405,7 +2407,8 @@ tryToMatchAndCreateMulAccumulateReduction(VPReductionRecipe *Red,
24052407
IsMulAccValidAndClampRange(Ext0->getOpcode() ==
24062408
Instruction::CastOps::ZExt,
24072409
Mul, Ext0, Ext1, Ext))
2408-
return new VPMulAccumulateReductionRecipe(Red, Mul, Ext0, Ext1, Ext->getResultType());
2410+
return new VPMulAccumulateReductionRecipe(Red, Mul, Ext0, Ext1,
2411+
Ext->getResultType());
24092412
}
24102413
return nullptr;
24112414
}

llvm/lib/Transforms/Vectorize/VPlanTransforms.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
#define LLVM_TRANSFORMS_VECTORIZE_VPLANTRANSFORMS_H
1515

1616
#include "VPlan.h"
17-
#include "VPlanHelpers.h"
1817
#include "VPlanVerifier.h"
1918
#include "llvm/ADT/STLFunctionalExtras.h"
2019
#include "llvm/Support/CommandLine.h"
@@ -29,6 +28,8 @@ class PredicatedScalarEvolution;
2928
class TargetLibraryInfo;
3029
class VPBuilder;
3130
class VPRecipeBuilder;
31+
class VPCostContext;
32+
class VFRange;
3233

3334
extern cl::opt<bool> VerifyEachVPlan;
3435

0 commit comments

Comments
 (0)