-
Notifications
You must be signed in to change notification settings - Fork 15k
[LV] Create in-loop sub reductions #147026
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
Changes from 18 commits
6171fd6
93dd762
52a8138
2f7a77f
b38b1cb
75baa8e
0669c78
e855872
35b6b9d
c659696
2b8e239
bdf2604
83f029c
6978e95
f53accf
0033606
3ae3e67
0f5cb6d
bd5c474
6a6bb86
c32b0e1
340003b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -40,6 +40,7 @@ bool RecurrenceDescriptor::isIntegerRecurrenceKind(RecurKind Kind) { | |
| switch (Kind) { | ||
| default: | ||
| break; | ||
| case RecurKind::Sub: | ||
| case RecurKind::Add: | ||
| case RecurKind::Mul: | ||
| case RecurKind::Or: | ||
|
|
@@ -897,8 +898,23 @@ RecurrenceDescriptor::InstDesc RecurrenceDescriptor::isRecurrenceInstr( | |
| case Instruction::PHI: | ||
| return InstDesc(I, Prev.getRecKind(), Prev.getExactFPMathInst()); | ||
| case Instruction::Sub: | ||
| if (Prev.getRecKind() == RecurKind::Add && Kind == RecurKind::Add) | ||
| return InstDesc(I, Prev.getRecKind()); | ||
|
||
| else if (Kind == RecurKind::Sub) | ||
| return InstDesc(I, Kind); | ||
| else | ||
| return InstDesc(false, I); | ||
| case Instruction::Add: | ||
| return InstDesc(Kind == RecurKind::Add, I); | ||
| // Loops with a sub reduction followed by an add reduction will have the sub | ||
| // input negated. It needs to be recorded as RecurKind::Add for that to | ||
| // happen since the loop vectorizer considers the last found RecurKind for | ||
| // the reduction phi's kind. | ||
| if (Prev.getRecKind() == RecurKind::Sub && Kind == RecurKind::Sub) | ||
| return InstDesc(I, RecurKind::Add); | ||
| else if (Kind == RecurKind::Add) | ||
| return InstDesc(I, Kind); | ||
| else | ||
| return InstDesc(false, I); | ||
| case Instruction::Mul: | ||
| return InstDesc(Kind == RecurKind::Mul, I); | ||
| case Instruction::And: | ||
|
|
@@ -917,7 +933,8 @@ RecurrenceDescriptor::InstDesc RecurrenceDescriptor::isRecurrenceInstr( | |
| I->hasAllowReassoc() ? nullptr : I); | ||
| case Instruction::Select: | ||
| if (Kind == RecurKind::FAdd || Kind == RecurKind::FMul || | ||
| Kind == RecurKind::Add || Kind == RecurKind::Mul) | ||
| Kind == RecurKind::Add || Kind == RecurKind::Mul || | ||
| Kind == RecurKind::Sub) | ||
| return isConditionalRdxPattern(I); | ||
| if (isFindIVRecurrenceKind(Kind) && SE) | ||
| return isFindIVPattern(Kind, L, OrigPhi, I, *SE); | ||
|
|
@@ -1003,6 +1020,11 @@ bool RecurrenceDescriptor::isReductionPHI(PHINode *Phi, Loop *TheLoop, | |
| LLVM_DEBUG(dbgs() << "Found an ADD reduction PHI." << *Phi << "\n"); | ||
| return true; | ||
| } | ||
| if (AddReductionVar(Phi, RecurKind::Sub, TheLoop, FMF, RedDes, DB, AC, DT, | ||
| SE)) { | ||
| LLVM_DEBUG(dbgs() << "Found a SUB reduction PHI." << *Phi << "\n"); | ||
| return true; | ||
| } | ||
| if (AddReductionVar(Phi, RecurKind::Mul, TheLoop, FMF, RedDes, DB, AC, DT, | ||
| SE)) { | ||
| LLVM_DEBUG(dbgs() << "Found a MUL reduction PHI." << *Phi << "\n"); | ||
|
|
@@ -1201,6 +1223,8 @@ bool RecurrenceDescriptor::isFixedOrderRecurrence(PHINode *Phi, Loop *TheLoop, | |
|
|
||
| unsigned RecurrenceDescriptor::getOpcode(RecurKind Kind) { | ||
| switch (Kind) { | ||
| case RecurKind::Sub: | ||
sdesmalen-arm marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| return Instruction::Sub; | ||
| case RecurKind::Add: | ||
| return Instruction::Add; | ||
| case RecurKind::Mul: | ||
|
|
@@ -1288,6 +1312,9 @@ RecurrenceDescriptor::getReductionOpChain(PHINode *Phi, Loop *L) const { | |
| if (isFMulAddIntrinsic(Cur)) | ||
| return true; | ||
|
|
||
| if (Cur->getOpcode() == Instruction::Sub && getOpcode() == Instruction::Add) | ||
sdesmalen-arm marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| return true; | ||
|
|
||
| return Cur->getOpcode() == getOpcode(); | ||
| }; | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -9118,6 +9118,16 @@ void LoopVectorizationPlanner::adjustRecipesForReductions( | |
| CurrentLinkI->getFastMathFlags()); | ||
| LinkVPBB->insert(FMulRecipe, CurrentLink->getIterator()); | ||
| VecOp = FMulRecipe; | ||
| } else if (PhiR->isInLoop() && Kind == RecurKind::Add && | ||
|
||
| CurrentLinkI->getOpcode() == Instruction::Sub) { | ||
| Type *PhiTy = PhiR->getUnderlyingValue()->getType(); | ||
| auto *Zero = Plan->getOrAddLiveIn(ConstantInt::get(PhiTy, 0)); | ||
| VPWidenRecipe *Sub = new VPWidenRecipe( | ||
| Instruction::Sub, {Zero, CurrentLink->getOperand(1)}, {}, | ||
| VPIRMetadata(), CurrentLinkI->getDebugLoc()); | ||
| Sub->setUnderlyingValue(CurrentLinkI); | ||
| LinkVPBB->insert(Sub, CurrentLink->getIterator()); | ||
| VecOp = Sub; | ||
| } else { | ||
| if (RecurrenceDescriptor::isMinMaxRecurrenceKind(Kind)) { | ||
| if (isa<VPWidenRecipe>(CurrentLink)) { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -22344,6 +22344,7 @@ class HorizontalReduction { | |
| return Builder.CreateBinOp((Instruction::BinaryOps)RdxOpcode, LHS, RHS, | ||
| Name); | ||
| } | ||
| case RecurKind::Sub: | ||
|
||
| case RecurKind::Add: | ||
| case RecurKind::Mul: | ||
| case RecurKind::Xor: | ||
|
|
@@ -23505,6 +23506,7 @@ class HorizontalReduction { | |
| // vector with full register use). | ||
| bool DoesRequireReductionOp = !AllConsts && VectorValuesAndScales.empty(); | ||
| switch (RdxKind) { | ||
| case RecurKind::Sub: | ||
| case RecurKind::Add: | ||
| case RecurKind::Mul: | ||
| case RecurKind::Or: | ||
|
|
@@ -23641,6 +23643,7 @@ class HorizontalReduction { | |
| if (Cnt > 1) { | ||
| ElementCount EC = cast<VectorType>(Vec->getType())->getElementCount(); | ||
| switch (RdxKind) { | ||
| case RecurKind::Sub: | ||
| case RecurKind::Add: { | ||
| if (ScalarTy == Builder.getInt1Ty() && ScalarTy != DestTy) { | ||
| unsigned VF = getNumElements(Vec->getType()); | ||
|
|
@@ -23661,8 +23664,9 @@ class HorizontalReduction { | |
| IsSigned); | ||
| Value *Scale = ConstantVector::getSplat( | ||
| EC, ConstantInt::get(DestTy->getScalarType(), Cnt)); | ||
| LLVM_DEBUG(dbgs() << "SLP: Add (to-mul) " << Cnt << "of " << Vec | ||
| << ". (HorRdx)\n"); | ||
| LLVM_DEBUG(dbgs() | ||
| << "SLP: " << (RdxKind == RecurKind::Add ? "Add" : "Sub") | ||
| << " (to-mul) " << Cnt << "of " << Vec << ". (HorRdx)\n"); | ||
| ++NumVectorInstructions; | ||
| Vec = Builder.CreateMul(Vec, Scale); | ||
| break; | ||
|
|
@@ -23802,11 +23806,14 @@ class HorizontalReduction { | |
| if (Cnt == 1) | ||
| return VectorizedValue; | ||
| switch (RdxKind) { | ||
| case RecurKind::Sub: | ||
| case RecurKind::Add: { | ||
| // res = mul vv, n | ||
| Value *Scale = ConstantInt::get(VectorizedValue->getType(), Cnt); | ||
| LLVM_DEBUG(dbgs() << "SLP: Add (to-mul) " << Cnt << "of " | ||
| << VectorizedValue << ". (HorRdx)\n"); | ||
| LLVM_DEBUG(dbgs() << "SLP: " | ||
| << (RdxKind == RecurKind::Add ? "Add" : "Sub") | ||
| << " (to-mul) " << Cnt << "of " << VectorizedValue | ||
| << ". (HorRdx)\n"); | ||
| return Builder.CreateMul(VectorizedValue, Scale); | ||
| } | ||
| case RecurKind::Xor: { | ||
|
|
@@ -23872,6 +23879,7 @@ class HorizontalReduction { | |
| R.isSignedMinBitwidthRootNode()); | ||
| } | ||
| switch (RdxKind) { | ||
| case RecurKind::Sub: | ||
| case RecurKind::Add: { | ||
| // root = mul prev_root, <1, 1, n, 1> | ||
| SmallVector<Constant *> Vals; | ||
|
|
@@ -23880,8 +23888,10 @@ class HorizontalReduction { | |
| Vals.push_back(ConstantInt::get(V->getType(), Cnt, /*IsSigned=*/false)); | ||
| } | ||
| auto *Scale = ConstantVector::get(Vals); | ||
| LLVM_DEBUG(dbgs() << "SLP: Add (to-mul) " << Scale << "of " | ||
| << VectorizedValue << ". (HorRdx)\n"); | ||
| LLVM_DEBUG(dbgs() << "SLP: " | ||
| << (RdxKind == RecurKind::Add ? "Add" : "Sub") | ||
| << " (to-mul) " << Scale << "of " << VectorizedValue | ||
| << ". (HorRdx)\n"); | ||
| return Builder.CreateMul(VectorizedValue, Scale); | ||
| } | ||
| case RecurKind::And: | ||
|
|
@@ -24331,6 +24341,7 @@ bool SLPVectorizerPass::tryToVectorize(Instruction *I, BoUpSLP &R) { | |
| TTI.getInstructionCost(Inst, CostKind); | ||
| InstructionCost RedCost; | ||
| switch (::getRdxKind(Inst)) { | ||
| case RecurKind::Sub: | ||
| case RecurKind::Add: | ||
| case RecurKind::Mul: | ||
| case RecurKind::Or: | ||
|
|
||
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.
You will need to update a number of places with switches over recurrence kind, in SLPVectorizer and possibly others
Probably also should have a SLP test to make sure its handled correctly.
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, I've added
RecurKind::Subcases to the SLP vectorizer and added a test. I don't think adding proper sub reduction support to SLP is in scope of this PR so haven't added full support.