-
Notifications
You must be signed in to change notification settings - Fork 15.2k
[LV] Choose best reduction for VPlan #166138
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
base: main
Are you sure you want to change the base?
Changes from all commits
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,8 @@ | |
| using namespace llvm; | ||
| using namespace VPlanPatternMatch; | ||
|
|
||
| #define DEBUG_TYPE "loop-vectorize" | ||
|
|
||
| static cl::opt<bool> EnableWideActiveLaneMask( | ||
| "enable-wide-lane-mask", cl::init(false), cl::Hidden, | ||
| cl::desc("Enable use of wide get active lane mask instructions")); | ||
|
|
@@ -3761,7 +3763,7 @@ tryToMatchAndCreateMulAccumulateReduction(VPReductionRecipe *Red, | |
|
|
||
| /// This function tries to create abstract recipes from the reduction recipe for | ||
| /// following optimizations and cost estimation. | ||
| static void tryToCreateAbstractReductionRecipe(VPReductionRecipe *Red, | ||
| static bool tryToCreateAbstractReductionRecipe(VPReductionRecipe *Red, | ||
| VPCostContext &Ctx, | ||
| VFRange &Range) { | ||
| VPExpressionRecipe *AbstractR = nullptr; | ||
|
|
@@ -3773,19 +3775,76 @@ static void tryToCreateAbstractReductionRecipe(VPReductionRecipe *Red, | |
| AbstractR = ExtRed; | ||
| // Cannot create abstract inloop reduction recipes. | ||
| if (!AbstractR) | ||
| return; | ||
| return false; | ||
|
|
||
| AbstractR->insertBefore(*VPBB, IP); | ||
| Red->replaceAllUsesWith(AbstractR); | ||
| return true; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Need to add |
||
| } | ||
|
|
||
| /// Lower a partial reduction back to a regular reduction, by | ||
| /// changing the in-loop partial reduction to a binop and removing | ||
| /// the scale factor from the PHI node. | ||
| static void lowerPartialReduction(VPlan &Plan, VPPartialReductionRecipe *Red, | ||
| VPCostContext &Ctx) { | ||
| VPRecipeBase *Acc = Red->getChainOp()->getDefiningRecipe(); | ||
| if (auto *PhiR = dyn_cast<VPReductionPHIRecipe>(Acc)) { | ||
| PhiR->setVFScaleFactor(1); | ||
|
|
||
| // We also need to update the scale factor of the reduction-start-vector | ||
| // operand. | ||
| VPValue *StartV, *IdentityV; | ||
| if (!match(PhiR->getOperand(0), | ||
| m_VPInstruction<VPInstruction::ReductionStartVector>( | ||
| m_VPValue(StartV), m_VPValue(IdentityV), m_VPValue()))) | ||
| llvm_unreachable("Unexpected operand for a partial reduction"); | ||
| Type *I32Ty = IntegerType::getInt32Ty(Plan.getContext()); | ||
| auto *ScaleFactorVPV = Plan.getOrAddLiveIn(ConstantInt::get(I32Ty, 1)); | ||
| cast<VPInstruction>(PhiR->getOperand(0))->setOperand(2, ScaleFactorVPV); | ||
| } | ||
|
|
||
| if (auto *R = dyn_cast<VPPartialReductionRecipe>(Acc)) | ||
| if (R->getVFScaleFactor() != 1) | ||
| lowerPartialReduction(Plan, R, Ctx); | ||
|
|
||
| LLVM_DEBUG( | ||
| dbgs() << "LV: Lowering " << *Red | ||
| << " back to regular reduction, because it is not profitable\n"); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we want an |
||
|
|
||
| // Lower the partial reduction to a regular binop. | ||
| VPBuilder Builder(Red); | ||
| VPInstruction *Add = Builder.createNaryOp( | ||
| RecurrenceDescriptor::getOpcode(Red->getRecurrenceKind()), | ||
| {Red->getChainOp(), Red->getVecOp()}); | ||
| if (Red->isConditional()) | ||
| Add = Builder.createSelect(Red->getCondOp(), Add, Red->getChainOp()); | ||
|
|
||
| Red->replaceAllUsesWith(Add); | ||
| Red->eraseFromParent(); | ||
| } | ||
|
|
||
| void VPlanTransforms::convertToAbstractRecipes(VPlan &Plan, VPCostContext &Ctx, | ||
| VFRange &Range) { | ||
| for (VPBasicBlock *VPBB : VPBlockUtils::blocksOnly<VPBasicBlock>( | ||
| vp_depth_first_deep(Plan.getVectorLoopRegion()))) { | ||
| for (VPRecipeBase &R : make_early_inc_range(*VPBB)) { | ||
| if (auto *Red = dyn_cast<VPReductionRecipe>(&R)) | ||
| tryToCreateAbstractReductionRecipe(Red, Ctx, Range); | ||
| auto *Red = dyn_cast<VPReductionRecipe>(&R); | ||
| if (!Red) | ||
| continue; | ||
|
|
||
| if (!tryToCreateAbstractReductionRecipe(Red, Ctx, Range) && | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If 'Red' is converted to AbstractRecipe, Red should be null. Can you check for |
||
| isa<VPPartialReductionRecipe>(Red)) { | ||
| // If there isn't a profitable VPExpression for a partial reduction, | ||
| // then that suggests using a partial reduction is not profitable | ||
| // for this VPlan. It seems better to resort to a regular (middle-block) | ||
| // reduction, so that the this plan is still profitable to consider. | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Extra |
||
| // Otherwise, the plan might be discarded in favour of a smaller VF. | ||
| // | ||
| // FIXME: There's a lot to unpick when it comes to partial | ||
| // reductions, but this should provide a temporary stop-gap until we | ||
| // reimplement the logic for creating partial reductions. | ||
| lowerPartialReduction(Plan, cast<VPPartialReductionRecipe>(Red), Ctx); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we should only call this if the reduction actually is partial, otherwise we'll waste some time essentially doing nothing in the lower function.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. it would be great if we can lower this here, basically after this |
||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
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.
rather than checking for ratio=1, you should either replace
OR