Skip to content

Commit ce81eb5

Browse files
artagnonfhahn
authored andcommitted
[LV] Extend FindFirstIV to unsigned case
Extend FindFirstIV vectorization to the unsigned case by introducing and handling FindFirstIVUMin.
1 parent aeb882a commit ce81eb5

File tree

5 files changed

+217
-51
lines changed

5 files changed

+217
-51
lines changed

llvm/include/llvm/Analysis/IVDescriptors.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,9 @@ enum class RecurKind {
5757
FindFirstIVSMin, /// FindFirst reduction with select(icmp(),x,y) where one of
5858
///< (x,y) is a decreasing loop induction, and both x and y
5959
///< are integer type, producing a SMin reduction.
60+
FindFirstIVUMin, /// FindFirst reduction with select(icmp(),x,y) where one of
61+
///< (x,y) is a decreasing loop induction, and both x and y
62+
///< are integer type, producing a UMin reduction.
6063
FindLastIVSMax, ///< FindLast reduction with select(cmp(),x,y) where one of
6164
///< (x,y) is increasing loop induction, and both x and y
6265
///< are integer type, producing a SMax reduction.
@@ -265,7 +268,8 @@ class RecurrenceDescriptor {
265268
/// Returns true if the recurrence kind is of the form
266269
/// select(cmp(),x,y) where one of (x,y) is decreasing loop induction.
267270
static bool isFindFirstIVRecurrenceKind(RecurKind Kind) {
268-
return Kind == RecurKind::FindFirstIVSMin;
271+
return Kind == RecurKind::FindFirstIVSMin ||
272+
Kind == RecurKind::FindFirstIVUMin;
269273
}
270274

271275
/// Returns true if the recurrence kind is of the form

llvm/lib/Analysis/IVDescriptors.cpp

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ bool RecurrenceDescriptor::isIntegerRecurrenceKind(RecurKind Kind) {
5151
case RecurKind::UMin:
5252
case RecurKind::AnyOf:
5353
case RecurKind::FindFirstIVSMin:
54+
case RecurKind::FindFirstIVUMin:
5455
case RecurKind::FindLastIVSMax:
5556
case RecurKind::FindLastIVUMax:
5657
return true;
@@ -741,10 +742,9 @@ RecurrenceDescriptor::isFindIVPattern(RecurKind Kind, Loop *TheLoop,
741742
: APInt::getMinValue(NumBits);
742743
ValidRange = ConstantRange::getNonEmpty(Sentinel + 1, Sentinel);
743744
} else {
744-
assert(IsSigned && "Only FindFirstIV with SMax is supported currently");
745-
ValidRange =
746-
ConstantRange::getNonEmpty(APInt::getSignedMinValue(NumBits),
747-
APInt::getSignedMaxValue(NumBits) - 1);
745+
APInt Sentinel = IsSigned ? APInt::getSignedMaxValue(NumBits)
746+
: APInt::getMaxValue(NumBits);
747+
ValidRange = ConstantRange::getNonEmpty(Sentinel, Sentinel - 1);
748748
}
749749

750750
LLVM_DEBUG(dbgs() << "LV: "
@@ -770,6 +770,8 @@ RecurrenceDescriptor::isFindIVPattern(RecurKind Kind, Loop *TheLoop,
770770

771771
if (CheckRange(true))
772772
return RecurKind::FindFirstIVSMin;
773+
if (CheckRange(false))
774+
return RecurKind::FindFirstIVUMin;
773775
return std::nullopt;
774776
};
775777

@@ -1183,6 +1185,7 @@ unsigned RecurrenceDescriptor::getOpcode(RecurKind Kind) {
11831185
return Instruction::Mul;
11841186
case RecurKind::AnyOf:
11851187
case RecurKind::FindFirstIVSMin:
1188+
case RecurKind::FindFirstIVUMin:
11861189
case RecurKind::FindLastIVSMax:
11871190
case RecurKind::FindLastIVUMax:
11881191
case RecurKind::Or:

llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23181,6 +23181,7 @@ class HorizontalReduction {
2318123181
case RecurKind::FMulAdd:
2318223182
case RecurKind::AnyOf:
2318323183
case RecurKind::FindFirstIVSMin:
23184+
case RecurKind::FindFirstIVUMin:
2318423185
case RecurKind::FindLastIVSMax:
2318523186
case RecurKind::FindLastIVUMax:
2318623187
case RecurKind::FMaximumNum:
@@ -23317,6 +23318,7 @@ class HorizontalReduction {
2331723318
case RecurKind::FMulAdd:
2331823319
case RecurKind::AnyOf:
2331923320
case RecurKind::FindFirstIVSMin:
23321+
case RecurKind::FindFirstIVUMin:
2332023322
case RecurKind::FindLastIVSMax:
2332123323
case RecurKind::FindLastIVUMax:
2332223324
case RecurKind::FMaximumNum:
@@ -23418,6 +23420,7 @@ class HorizontalReduction {
2341823420
case RecurKind::FMulAdd:
2341923421
case RecurKind::AnyOf:
2342023422
case RecurKind::FindFirstIVSMin:
23423+
case RecurKind::FindFirstIVUMin:
2342123424
case RecurKind::FindLastIVSMax:
2342223425
case RecurKind::FindLastIVUMax:
2342323426
case RecurKind::FMaximumNum:

llvm/lib/Transforms/Vectorize/VPlanRecipes.cpp

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -763,14 +763,10 @@ Value *VPInstruction::generate(VPTransformState &State) {
763763
Value *ReducedPartRdx = State.get(getOperand(3));
764764
RecurKind MinMaxKind;
765765
bool IsSigned = RecurrenceDescriptor::isSignedRecurrenceKind(RK);
766-
if (RecurrenceDescriptor::isFindLastIVRecurrenceKind(RK)) {
766+
if (RecurrenceDescriptor::isFindLastIVRecurrenceKind(RK))
767767
MinMaxKind = IsSigned ? RecurKind::SMax : RecurKind::UMax;
768-
} else {
769-
assert(RecurrenceDescriptor::isFindFirstIVRecurrenceKind(RK) &&
770-
"Kind must either be FindLastIV or FindFirstIV");
771-
assert(IsSigned && "Only FindFirstIV with SMax is currently supported");
772-
MinMaxKind = RecurKind::SMin;
773-
}
768+
else
769+
MinMaxKind = IsSigned ? RecurKind::SMin : RecurKind::UMin;
774770
for (unsigned Part = 1; Part < UF; ++Part)
775771
ReducedPartRdx = createMinMaxOp(Builder, MinMaxKind, ReducedPartRdx,
776772
State.get(getOperand(3 + Part)));

0 commit comments

Comments
 (0)