-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[IVDesc] Unify RecurKinds [I|F]AnyOf #118393
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
@llvm/pr-subscribers-llvm-transforms @llvm/pr-subscribers-llvm-analysis Author: Ramkumar Ramachandra (artagnon) ChangesChecking RecurKind::FAnyOf in isRecurrenceDescriptor() is wasted work when it already checks RecurKind::IAnyOf. Affect a minor adjustment to the code to facilitate skipping the RecurKind::FAnyOf check, and strip the check. The patch has the side-effect of rewriting some flaky code, which would match an ICmp having the reduction phi as an operand with IAnyOf, and due to NumCmpSelectPatternInst != 1 (the select redux is also matched), it would have to rely on failing to match an FCmp with FAnyOf, setting NumCmpSelectPatternInst = 1, and successfully vectorizing an IAnyOf pattern with the incorrect debug output. There is a test for this already in select-cmp.ll: select_i32_from_icmp_same_inputs. Full diff: https://github.com/llvm/llvm-project/pull/118393.diff 1 Files Affected:
diff --git a/llvm/lib/Analysis/IVDescriptors.cpp b/llvm/lib/Analysis/IVDescriptors.cpp
index 23e11bdbeab4c5..9678fcdcbd7c0e 100644
--- a/llvm/lib/Analysis/IVDescriptors.cpp
+++ b/llvm/lib/Analysis/IVDescriptors.cpp
@@ -415,12 +415,9 @@ bool RecurrenceDescriptor::AddReductionVar(
if (IsAPhi && Cur != Phi && !areAllUsesIn(Cur, VisitedInsts))
return false;
- if ((isIntMinMaxRecurrenceKind(Kind) || Kind == RecurKind::IAnyOf) &&
- (isa<ICmpInst>(Cur) || isa<SelectInst>(Cur)))
- ++NumCmpSelectPatternInst;
- if ((isFPMinMaxRecurrenceKind(Kind) || Kind == RecurKind::FAnyOf) &&
- (isa<FCmpInst>(Cur) || isa<SelectInst>(Cur)))
- ++NumCmpSelectPatternInst;
+ if (isIntMinMaxRecurrenceKind(Kind) || isAnyOfRecurrenceKind(Kind))
+ if (match(Cur, m_Select(m_Cmp(), m_Value(), m_Value())))
+ ++NumCmpSelectPatternInst;
// Check whether we found a reduction operator.
FoundReduxOp |= !IsAPhi && Cur != Start;
@@ -500,7 +497,7 @@ bool RecurrenceDescriptor::AddReductionVar(
// This means we have seen one but not the other instruction of the
// pattern or more than just a select and cmp. Zero implies that we saw a
// llvm.min/max intrinsic, which is always OK.
- if (isMinMaxRecurrenceKind(Kind) && NumCmpSelectPatternInst != 2 &&
+ if (isMinMaxRecurrenceKind(Kind) && NumCmpSelectPatternInst != 1 &&
NumCmpSelectPatternInst != 0)
return false;
@@ -889,8 +886,8 @@ bool RecurrenceDescriptor::isReductionPHI(PHINode *Phi, Loop *TheLoop,
}
if (AddReductionVar(Phi, RecurKind::IAnyOf, TheLoop, FMF, RedDes, DB, AC, DT,
SE)) {
- LLVM_DEBUG(dbgs() << "Found an integer conditional select reduction PHI."
- << *Phi << "\n");
+ LLVM_DEBUG(dbgs() << "Found an conditional select reduction PHI." << *Phi
+ << "\n");
return true;
}
if (AddReductionVar(Phi, RecurKind::FMul, TheLoop, FMF, RedDes, DB, AC, DT,
@@ -913,12 +910,6 @@ bool RecurrenceDescriptor::isReductionPHI(PHINode *Phi, Loop *TheLoop,
LLVM_DEBUG(dbgs() << "Found a float MIN reduction PHI." << *Phi << "\n");
return true;
}
- if (AddReductionVar(Phi, RecurKind::FAnyOf, TheLoop, FMF, RedDes, DB, AC, DT,
- SE)) {
- LLVM_DEBUG(dbgs() << "Found a float conditional select reduction PHI."
- << " PHI." << *Phi << "\n");
- return true;
- }
if (AddReductionVar(Phi, RecurKind::FMulAdd, TheLoop, FMF, RedDes, DB, AC, DT,
SE)) {
LLVM_DEBUG(dbgs() << "Found an FMulAdd reduction PHI." << *Phi << "\n");
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This effectively makes FAnyOf dead, right? In that case it should be removed and IAnyOf
-> AnyOf
?
There is still FAnyOf matching with FCmp, and IAnyOf matching with ICmp, so I don't think it can be removed. See getReductionOpChain's call to getOpcode for example. See also the return statement in isAnyOfPattern:
|
I agree with @fhahn. Looking at the code after this patch lands there is no logical reason for distinguishing between IAnyOf and FAnyOf. You should be able to condense the two RecurKind types into a single AnyOf I think. When creating |
Also, I think if this patch is fixing a bug (which you implied from the commit message) then it probably shouldn't have NFC in the title or commit message? That sounds like a functional change. |
8ce79f5
to
b70d6e3
Compare
Thanks, both. FAnyOf has now been stripped altogether, and IAnyOf has been renamed to AnyOf. |
b70d6e3
to
05ee143
Compare
Rebased. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks your contribution. I've done a rough review of this patch for now. On Thursday, I’ll conduct a more detailed review since I won’t be in the office this Wednesday.
I'm not sure if this is the issue you discovered. (The main patch of this pre-commit might have been lost.)
https://reviews.llvm.org/D157375
We had an internal discussion later and found that this case should be vectorizable in the form of an AnyOf reduction.
Yes, it's the same issue, and there's a test for it in the tree, as the commit message mentions. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I support unification AnyOf.
But I don't think non-arithmetical reduction (min/max, AnyOf, FindLast...etc.) needs getOpcode
. I think a better approach is to remove the dependence of non-arithmetical reduction on getOpcode
and then start to unify AnyOf.
Here is my first step #118777.
71d0bc8
to
2c387ce
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is not NFC since getOpcode() returned different result from before.
@artagnon The purpose of this PR is really good, and I think it should continue, but my concern is that the approach and results are incorrect. I believe the real reason we cannot unify IAnyOf and FAnyOf is due to Earlier, I identified the locations where
Because I've been busy with EVL tail folding, the cleanup may proceed slowly. Welcome your contribution If you’re willing and interested. Then we can continue this patch after we finish the clean up. :) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Adding some thoughts raised by this unification effort.
This patch changes the preferInLoopReduction function to take a RecurKind instead of an unsigned Opcode. This makes it possible to distinguish non-arithmetic reductions such as min/max, AnyOf, and FindLastIV, and also helps unify IAnyOf with FAnyOf and IFindLastIV with FFindLastIV. Related patch #118393 #131830
…ctionResult, nfc (#140245) When reducing unrolled parts, explicitly check for min/max reductions using the function RecurrenceDescriptor::isMinMaxRecurrenceKind. Only if the reduction is not min/max reduction, call RecurrenceDescriptor::getOpcode() to handle other cases via CreateBinOp. Based on #140242 Related to #118393
…ComputeReductionResult, nfc (#140245) When reducing unrolled parts, explicitly check for min/max reductions using the function RecurrenceDescriptor::isMinMaxRecurrenceKind. Only if the reduction is not min/max reduction, call RecurrenceDescriptor::getOpcode() to handle other cases via CreateBinOp. Based on llvm/llvm-project#140242 Related to llvm/llvm-project#118393
@artagnon I think you can re-open and continue this work. |
af86167
to
c9b44e9
Compare
Thanks @Mel-Chen for the prerequisite work. This patch should be ready to review now. |
c740737
to
89e1f95
Compare
Checking RecurKind::FAnyOf in isRecurrenceDescriptor() is wasted work when it already checks RecurKind::IAnyOf. Affect a minor adjustment to the code to facilitate skipping the RecurKind::FAnyOf check, and strip the check. The patch has the side-effect of rewriting some flaky code, which would match an ICmp having the reduction phi as an operand with IAnyOf, and due to NumCmpSelectPatternInst != 1 (the select redux is also matched), it would have to rely on failing to match an FCmp with FAnyOf, setting NumCmpSelectPatternInst = 1, and successfully vectorizing an IAnyOf pattern with the incorrect debug output. There is a test for this already in select-cmp.ll: select_i32_from_icmp_same_inputs. Co-authored-by: Mel Chen <[email protected]>
89e1f95
to
7ddb7dd
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
Maybe it's worth updating the patch description?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, thanks
Co-authored-by: Mel Chen <[email protected]>
…ctionResult, nfc (llvm#140245) When reducing unrolled parts, explicitly check for min/max reductions using the function RecurrenceDescriptor::isMinMaxRecurrenceKind. Only if the reduction is not min/max reduction, call RecurrenceDescriptor::getOpcode() to handle other cases via CreateBinOp. Based on llvm#140242 Related to llvm#118393
No description provided.