Skip to content

Commit 717adeb

Browse files
committed
[LV] Address review
1 parent 09a62b5 commit 717adeb

File tree

5 files changed

+57
-51
lines changed

5 files changed

+57
-51
lines changed

llvm/include/llvm/Analysis/IVDescriptors.h

Lines changed: 28 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,12 @@ enum class RecurKind {
5454
FMulAdd, ///< Sum of float products with llvm.fmuladd(a * b + sum).
5555
AnyOf, ///< AnyOf reduction with select(cmp(),x,y) where one of (x,y) is
5656
///< loop invariant, and both x and y are integer type.
57-
FindLastIV, ///< FindLast reduction with select(cmp(),x,y) where one of
58-
///< (x,y) is increasing loop induction, and both x and y are
59-
///< integer type.
57+
FindLastIVSMin, ///< FindLast reduction with select(cmp(),x,y) where one of
58+
///< (x,y) is increasing loop induction, and both x and y
59+
///< are integer type, producing a SMin reduction.
60+
FindLastIVUMin, ///< FindLast reduction with select(cmp(),x,y) where one of
61+
///< (x,y) is increasing loop induction, and both x and y
62+
///< are integer type, producting a UMin reduction.
6063
// clang-format on
6164
// TODO: Any_of and FindLast reduction need not be restricted to integer type
6265
// only.
@@ -80,13 +83,12 @@ class RecurrenceDescriptor {
8083

8184
RecurrenceDescriptor(Value *Start, Instruction *Exit, StoreInst *Store,
8285
RecurKind K, FastMathFlags FMF, Instruction *ExactFP,
83-
Type *RT, bool IsResultSigned, bool IsReduxSigned,
84-
bool Ordered, SmallPtrSetImpl<Instruction *> &CI,
86+
Type *RT, bool Signed, bool Ordered,
87+
SmallPtrSetImpl<Instruction *> &CI,
8588
unsigned MinWidthCastToRecurTy)
8689
: IntermediateStore(Store), StartValue(Start), LoopExitInstr(Exit),
8790
Kind(K), FMF(FMF), ExactFPMathInst(ExactFP), RecurrenceType(RT),
88-
IsResultSigned(IsResultSigned), IsReduxSigned(IsReduxSigned),
89-
IsOrdered(Ordered),
91+
IsSigned(Signed), IsOrdered(Ordered),
9092
MinWidthCastToRecurrenceType(MinWidthCastToRecurTy) {
9193
CastInsts.insert_range(CI);
9294
}
@@ -98,14 +100,12 @@ class RecurrenceDescriptor {
98100
: IsRecurrence(IsRecur), PatternLastInst(I),
99101
RecKind(RecurKind::None), ExactFPMathInst(ExactFP) {}
100102

101-
InstDesc(Instruction *I, RecurKind K, bool IsSigned = false)
102-
: IsRecurrence(true), IsSigned(IsSigned), PatternLastInst(I),
103-
RecKind(K) {}
103+
InstDesc(Instruction *I, RecurKind K, Instruction *ExactFP = nullptr)
104+
: IsRecurrence(true), PatternLastInst(I), RecKind(K),
105+
ExactFPMathInst(ExactFP) {}
104106

105107
bool isRecurrence() const { return IsRecurrence; }
106108

107-
bool isSigned() const { return IsSigned; }
108-
109109
bool needsExactFPMath() const { return ExactFPMathInst != nullptr; }
110110

111111
Instruction *getExactFPMathInst() const { return ExactFPMathInst; }
@@ -117,15 +117,13 @@ class RecurrenceDescriptor {
117117
private:
118118
// Is this instruction a recurrence candidate.
119119
bool IsRecurrence;
120-
// Is this recurrence a signed variant.
121-
bool IsSigned = false;
122120
// The last instruction in a min/max pattern (select of the select(icmp())
123121
// pattern), or the current recurrence instruction otherwise.
124122
Instruction *PatternLastInst;
125123
// If this is a min/max pattern.
126124
RecurKind RecKind;
127125
// Recurrence does not allow floating-point reassociation.
128-
Instruction *ExactFPMathInst = nullptr;
126+
Instruction *ExactFPMathInst;
129127
};
130128

131129
/// Returns a struct describing if the instruction 'I' can be a recurrence
@@ -265,7 +263,14 @@ class RecurrenceDescriptor {
265263
/// Returns true if the recurrence kind is of the form
266264
/// select(cmp(),x,y) where one of (x,y) is increasing loop induction.
267265
static bool isFindLastIVRecurrenceKind(RecurKind Kind) {
268-
return Kind == RecurKind::FindLastIV;
266+
return Kind == RecurKind::FindLastIVSMin ||
267+
Kind == RecurKind::FindLastIVUMin;
268+
}
269+
270+
/// Returns true if recurrece kind is a signed redux kind.
271+
static bool isSignedRecurrenceKind(RecurKind Kind) {
272+
return Kind == RecurKind::SMax || Kind == RecurKind::SMin ||
273+
Kind == RecurKind::FindLastIVSMin;
269274
}
270275

271276
/// Returns the type of the recurrence. This type can be narrower than the
@@ -278,8 +283,9 @@ class RecurrenceDescriptor {
278283
assert(isFindLastIVRecurrenceKind(Kind) && "Unexpected recurrence kind");
279284
Type *Ty = StartValue->getType();
280285
unsigned BW = Ty->getIntegerBitWidth();
281-
return ConstantInt::get(Ty, isReduxSigned() ? APInt::getSignedMinValue(BW)
282-
: APInt::getMinValue(BW));
286+
return ConstantInt::get(Ty, isSignedRecurrenceKind(Kind)
287+
? APInt::getSignedMinValue(BW)
288+
: APInt::getMinValue(BW));
283289
}
284290

285291
/// Returns a reference to the instructions used for type-promoting the
@@ -291,11 +297,8 @@ class RecurrenceDescriptor {
291297
return MinWidthCastToRecurrenceType;
292298
}
293299

294-
/// Returns true if the reduction result is signed.
295-
bool isResultSigned() const { return IsResultSigned; }
296-
297-
/// Returns true if the reduction redux is signed.
298-
bool isReduxSigned() const { return IsReduxSigned; }
300+
/// Returns true if all source operands of the recurrence are SExtInsts.
301+
bool isSigned() const { return IsSigned; }
299302

300303
/// Expose an ordered FP reduction to the instance users.
301304
bool isOrdered() const { return IsOrdered; }
@@ -331,10 +334,8 @@ class RecurrenceDescriptor {
331334
Instruction *ExactFPMathInst = nullptr;
332335
// The type of the recurrence.
333336
Type *RecurrenceType = nullptr;
334-
// True if reduction result is signed.
335-
bool IsResultSigned = false;
336-
// True if reduction redux is signed.
337-
bool IsReduxSigned = false;
337+
// True if all source operands of the recurrence are SExtInsts.
338+
bool IsSigned = false;
338339
// True if this recurrence can be treated as an in-order reduction.
339340
// Currently only a non-reassociative FAdd can be considered in-order,
340341
// if it is also the only FAdd in the PHI's use chain.

llvm/lib/Analysis/IVDescriptors.cpp

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,8 @@ bool RecurrenceDescriptor::isIntegerRecurrenceKind(RecurKind Kind) {
5050
case RecurKind::UMax:
5151
case RecurKind::UMin:
5252
case RecurKind::AnyOf:
53-
case RecurKind::FindLastIV:
53+
case RecurKind::FindLastIVSMin:
54+
case RecurKind::FindLastIVUMin:
5455
return true;
5556
}
5657
return false;
@@ -88,7 +89,7 @@ static Instruction *lookThroughAnd(PHINode *Phi, Type *&RT,
8889
}
8990

9091
/// Compute the minimal bit width needed to represent a reduction whose exit
91-
/// instruction is given by Exit, along with its signedness.
92+
/// instruction is given by Exit.
9293
static std::pair<Type *, bool> computeRecurrenceType(Instruction *Exit,
9394
DemandedBits *DB,
9495
AssumptionCache *AC,
@@ -255,7 +256,7 @@ bool RecurrenceDescriptor::AddReductionVar(
255256
SmallPtrSet<Instruction *, 4> CastInsts;
256257
unsigned MinWidthCastToRecurrenceType;
257258
Instruction *Start = Phi;
258-
bool IsResultSigned = false, IsReduxSigned = false;
259+
bool IsSigned = false;
259260

260261
SmallPtrSet<Instruction *, 8> VisitedInsts;
261262
SmallVector<Instruction *, 8> Worklist;
@@ -396,7 +397,6 @@ bool RecurrenceDescriptor::AddReductionVar(
396397
// state accurate while processing the worklist?
397398
if (ReduxDesc.getRecKind() != RecurKind::None)
398399
Kind = ReduxDesc.getRecKind();
399-
IsReduxSigned = ReduxDesc.isSigned();
400400
}
401401

402402
bool IsASelect = isa<SelectInst>(Cur);
@@ -566,7 +566,7 @@ bool RecurrenceDescriptor::AddReductionVar(
566566
// smaller type. We should just generate a correctly typed expression
567567
// to begin with.
568568
Type *ComputedType;
569-
std::tie(ComputedType, IsResultSigned) =
569+
std::tie(ComputedType, IsSigned) =
570570
computeRecurrenceType(ExitInstruction, DB, AC, DT);
571571
if (ComputedType != RecurrenceType)
572572
return false;
@@ -596,9 +596,8 @@ bool RecurrenceDescriptor::AddReductionVar(
596596

597597
// Save the description of this reduction variable.
598598
RecurrenceDescriptor RD(RdxStart, ExitInstruction, IntermediateStore, Kind,
599-
FMF, ExactFPMathInst, RecurrenceType, IsResultSigned,
600-
IsReduxSigned, IsOrdered, CastInsts,
601-
MinWidthCastToRecurrenceType);
599+
FMF, ExactFPMathInst, RecurrenceType, IsSigned,
600+
IsOrdered, CastInsts, MinWidthCastToRecurrenceType);
602601
RedDes = RD;
603602

604603
return true;
@@ -704,7 +703,7 @@ RecurrenceDescriptor::isFindLastIVPattern(Loop *TheLoop, PHINode *OrigPhi,
704703

705704
// Returns a non-nullopt boolean indicating the signedness of the recurrence
706705
// when a valid FindLastIV pattern is found.
707-
auto GetInductionSignedness = [&](Value *V) -> std::optional<bool> {
706+
auto GetRecurKind = [&](Value *V) -> std::optional<RecurKind> {
708707
Type *Ty = V->getType();
709708
if (!SE.isSCEVable(Ty))
710709
return std::nullopt;
@@ -741,18 +740,18 @@ RecurrenceDescriptor::isFindLastIVPattern(Loop *TheLoop, PHINode *OrigPhi,
741740
return ValidRange.contains(IVRange);
742741
};
743742
if (CheckRange(true))
744-
return true;
743+
return RecurKind::FindLastIVSMin;
745744
if (CheckRange(false))
746-
return false;
745+
return RecurKind::FindLastIVUMin;
747746
return std::nullopt;
748747
};
749748

750749
// We are looking for selects of the form:
751750
// select(cmp(), phi, increasing_loop_induction) or
752751
// select(cmp(), increasing_loop_induction, phi)
753752
// TODO: Support for monotonically decreasing induction variable
754-
if (auto IsSigned = GetInductionSignedness(NonRdxPhi))
755-
return InstDesc(I, RecurKind::FindLastIV, *IsSigned);
753+
if (auto RK = GetRecurKind(NonRdxPhi))
754+
return InstDesc(I, *RK);
756755

757756
return InstDesc(false, I);
758757
}
@@ -999,8 +998,8 @@ bool RecurrenceDescriptor::isReductionPHI(PHINode *Phi, Loop *TheLoop,
999998
<< "\n");
1000999
return true;
10011000
}
1002-
if (AddReductionVar(Phi, RecurKind::FindLastIV, TheLoop, FMF, RedDes, DB, AC,
1003-
DT, SE)) {
1001+
if (AddReductionVar(Phi, RecurKind::FindLastIVSMin, TheLoop, FMF, RedDes, DB,
1002+
AC, DT, SE)) {
10041003
LLVM_DEBUG(dbgs() << "Found a FindLastIV reduction PHI." << *Phi << "\n");
10051004
return true;
10061005
}
@@ -1151,7 +1150,8 @@ unsigned RecurrenceDescriptor::getOpcode(RecurKind Kind) {
11511150
case RecurKind::Mul:
11521151
return Instruction::Mul;
11531152
case RecurKind::AnyOf:
1154-
case RecurKind::FindLastIV:
1153+
case RecurKind::FindLastIVSMin:
1154+
case RecurKind::FindLastIVUMin:
11551155
case RecurKind::Or:
11561156
return Instruction::Or;
11571157
case RecurKind::And:

llvm/lib/Transforms/Vectorize/LoopVectorize.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9215,7 +9215,7 @@ void LoopVectorizationPlanner::adjustRecipesForReductions(
92159215
auto *Trunc =
92169216
new VPWidenCastRecipe(Instruction::Trunc, NewExitingVPV, RdxTy);
92179217
auto *Extnd =
9218-
RdxDesc.isResultSigned()
9218+
RdxDesc.isSigned()
92199219
? new VPWidenCastRecipe(Instruction::SExt, Trunc, PhiTy)
92209220
: new VPWidenCastRecipe(Instruction::ZExt, Trunc, PhiTy);
92219221

llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23105,7 +23105,8 @@ class HorizontalReduction {
2310523105
case RecurKind::FMul:
2310623106
case RecurKind::FMulAdd:
2310723107
case RecurKind::AnyOf:
23108-
case RecurKind::FindLastIV:
23108+
case RecurKind::FindLastIVSMin:
23109+
case RecurKind::FindLastIVUMin:
2310923110
case RecurKind::FMaximumNum:
2311023111
case RecurKind::FMinimumNum:
2311123112
case RecurKind::None:
@@ -23239,7 +23240,8 @@ class HorizontalReduction {
2323923240
case RecurKind::FMul:
2324023241
case RecurKind::FMulAdd:
2324123242
case RecurKind::AnyOf:
23242-
case RecurKind::FindLastIV:
23243+
case RecurKind::FindLastIVSMin:
23244+
case RecurKind::FindLastIVUMin:
2324323245
case RecurKind::FMaximumNum:
2324423246
case RecurKind::FMinimumNum:
2324523247
case RecurKind::None:
@@ -23338,7 +23340,8 @@ class HorizontalReduction {
2333823340
case RecurKind::FMul:
2333923341
case RecurKind::FMulAdd:
2334023342
case RecurKind::AnyOf:
23341-
case RecurKind::FindLastIV:
23343+
case RecurKind::FindLastIVSMin:
23344+
case RecurKind::FindLastIVUMin:
2334223345
case RecurKind::FMaximumNum:
2334323346
case RecurKind::FMinimumNum:
2334423347
case RecurKind::None:

llvm/lib/Transforms/Vectorize/VPlanRecipes.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -634,10 +634,12 @@ Value *VPInstruction::generate(VPTransformState &State) {
634634
// each part of the reduction.
635635
unsigned UF = getNumOperands() - 2;
636636
Value *ReducedPartRdx = State.get(getOperand(2));
637+
auto MinMaxOp = RecurrenceDescriptor::isSignedRecurrenceKind(RK)
638+
? RecurKind::SMax
639+
: RecurKind::UMax;
637640
for (unsigned Part = 1; Part < UF; ++Part) {
638-
ReducedPartRdx = createMinMaxOp(
639-
Builder, RdxDesc.isReduxSigned() ? RecurKind::SMax : RecurKind::UMax,
640-
ReducedPartRdx, State.get(getOperand(2 + Part)));
641+
ReducedPartRdx = createMinMaxOp(Builder, MinMaxOp, ReducedPartRdx,
642+
State.get(getOperand(2 + Part)));
641643
}
642644

643645
return createFindLastIVReduction(Builder, ReducedPartRdx,
@@ -705,7 +707,7 @@ Value *VPInstruction::generate(VPTransformState &State) {
705707
// If the reduction can be performed in a smaller type, we need to extend
706708
// the reduction to the wider type before we branch to the original loop.
707709
if (PhiTy != RdxDesc.getRecurrenceType())
708-
ReducedPartRdx = RdxDesc.isResultSigned()
710+
ReducedPartRdx = RdxDesc.isSigned()
709711
? Builder.CreateSExt(ReducedPartRdx, PhiTy)
710712
: Builder.CreateZExt(ReducedPartRdx, PhiTy);
711713
}

0 commit comments

Comments
 (0)