-
Notifications
You must be signed in to change notification settings - Fork 14.4k
[VPlan] Fix first-order splices without header mask not using EVL #146672
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 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 |
---|---|---|
|
@@ -2114,21 +2114,19 @@ void VPlanTransforms::addActiveLaneMask( | |
HeaderMask->replaceAllUsesWith(LaneMask); | ||
} | ||
|
||
/// Try to convert \p CurRecipe to a corresponding EVL-based recipe. Returns | ||
/// nullptr if no EVL-based recipe could be created. | ||
/// Try to optimize a \p CurRecipe masked by \p HeaderMask to a corresponding | ||
/// EVL-based recipe without the header mask. Returns nullptr if no EVL-based | ||
/// recipe could be created. | ||
/// \p HeaderMask Header Mask. | ||
/// \p CurRecipe Recipe to be transform. | ||
/// \p TypeInfo VPlan-based type analysis. | ||
/// \p AllOneMask The vector mask parameter of vector-predication intrinsics. | ||
/// \p EVL The explicit vector length parameter of vector-predication | ||
/// intrinsics. | ||
/// \p PrevEVL The explicit vector length of the previous iteration. Only | ||
/// required if \p CurRecipe is a VPInstruction::FirstOrderRecurrenceSplice. | ||
static VPRecipeBase *createEVLRecipe(VPValue *HeaderMask, | ||
VPRecipeBase &CurRecipe, | ||
VPTypeAnalysis &TypeInfo, | ||
VPValue &AllOneMask, VPValue &EVL, | ||
VPValue *PrevEVL) { | ||
static VPRecipeBase *optimizeMaskToEVL(VPValue *HeaderMask, | ||
VPRecipeBase &CurRecipe, | ||
VPTypeAnalysis &TypeInfo, | ||
VPValue &AllOneMask, VPValue &EVL) { | ||
using namespace llvm::VPlanPatternMatch; | ||
auto GetNewMask = [&](VPValue *OrigMask) -> VPValue * { | ||
assert(OrigMask && "Unmasked recipe when folding tail"); | ||
|
@@ -2153,18 +2151,6 @@ static VPRecipeBase *createEVLRecipe(VPValue *HeaderMask, | |
return new VPReductionEVLRecipe(*Red, EVL, NewMask); | ||
}) | ||
.Case<VPInstruction>([&](VPInstruction *VPI) -> VPRecipeBase * { | ||
if (VPI->getOpcode() == VPInstruction::FirstOrderRecurrenceSplice) { | ||
assert(PrevEVL && "Fixed-order recurrences require previous EVL"); | ||
VPValue *MinusOneVPV = VPI->getParent()->getPlan()->getOrAddLiveIn( | ||
ConstantInt::getSigned(Type::getInt32Ty(TypeInfo.getContext()), | ||
-1)); | ||
SmallVector<VPValue *> Ops(VPI->operands()); | ||
Ops.append({MinusOneVPV, &AllOneMask, PrevEVL, &EVL}); | ||
return new VPWidenIntrinsicRecipe(Intrinsic::experimental_vp_splice, | ||
Ops, TypeInfo.inferScalarType(VPI), | ||
VPI->getDebugLoc()); | ||
} | ||
|
||
VPValue *LHS, *RHS; | ||
// Transform select with a header mask condition | ||
// select(header_mask, LHS, RHS) | ||
|
@@ -2197,9 +2183,12 @@ static void transformRecipestoEVLRecipes(VPlan &Plan, VPValue &EVL) { | |
"User of VF that we can't transform to EVL."); | ||
Plan.getVF().replaceAllUsesWith(&EVL); | ||
|
||
// Defer erasing recipes till the end so that we don't invalidate the | ||
// VPTypeAnalysis cache. | ||
SmallVector<VPRecipeBase *> ToErase; | ||
|
||
// Create a scalar phi to track the previous EVL if fixed-order recurrence is | ||
// contained. | ||
VPInstruction *PrevEVL = nullptr; | ||
bool ContainsFORs = | ||
any_of(Header->phis(), IsaPred<VPFirstOrderRecurrencePHIRecipe>); | ||
if (ContainsFORs) { | ||
|
@@ -2212,16 +2201,37 @@ static void transformRecipestoEVLRecipes(VPlan &Plan, VPValue &EVL) { | |
DebugLoc()); | ||
|
||
Builder.setInsertPoint(Header, Header->getFirstNonPhi()); | ||
PrevEVL = Builder.createScalarPhi({MaxEVL, &EVL}, DebugLoc(), "prev.evl"); | ||
VPValue *PrevEVL = | ||
Builder.createScalarPhi({MaxEVL, &EVL}, DebugLoc(), "prev.evl"); | ||
|
||
for (VPBasicBlock *VPBB : VPBlockUtils::blocksOnly<VPBasicBlock>( | ||
vp_depth_first_deep(Plan.getVectorLoopRegion()->getEntry()))) { | ||
for (VPRecipeBase &R : *VPBB) { | ||
using namespace VPlanPatternMatch; | ||
VPValue *V1, *V2; | ||
if (!match(&R, | ||
m_VPInstruction<VPInstruction::FirstOrderRecurrenceSplice>( | ||
m_VPValue(V1), m_VPValue(V2)))) | ||
continue; | ||
VPValue *Imm = Plan.getOrAddLiveIn( | ||
ConstantInt::getSigned(Type::getInt32Ty(Ctx), -1)); | ||
VPWidenIntrinsicRecipe *VPSplice = new VPWidenIntrinsicRecipe( | ||
Intrinsic::experimental_vp_splice, | ||
{V1, V2, Imm, AllOneMask, PrevEVL, &EVL}, | ||
TypeInfo.inferScalarType(R.getVPSingleValue()), R.getDebugLoc()); | ||
VPSplice->insertBefore(&R); | ||
R.getVPSingleValue()->replaceAllUsesWith(VPSplice); | ||
ToErase.push_back(&R); | ||
} | ||
} | ||
Comment on lines
+2204
to
+2226
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. Could we extract the code into a static function? 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. Which part needs extracted? The loop over each recipe? 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. Extract
if possible. 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. Oh yeah, I had the same thoughts too. There's a lot of places that do this in VPlanTransforms. We can look at doing this in a separate NFC patch? 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. Btw I've gone ahead and merged this to fix the EVL buildbot, hope this wasn't too hasty. Happy to address any more review comments if you have them! |
||
} | ||
|
||
SmallVector<VPRecipeBase *> ToErase; | ||
|
||
// Try to optimize header mask recipes away to their EVL variants. | ||
for (VPValue *HeaderMask : collectAllHeaderMasks(Plan)) { | ||
for (VPUser *U : collectUsersRecursively(HeaderMask)) { | ||
auto *CurRecipe = cast<VPRecipeBase>(U); | ||
VPRecipeBase *EVLRecipe = createEVLRecipe( | ||
HeaderMask, *CurRecipe, TypeInfo, *AllOneMask, EVL, PrevEVL); | ||
VPRecipeBase *EVLRecipe = | ||
optimizeMaskToEVL(HeaderMask, *CurRecipe, TypeInfo, *AllOneMask, EVL); | ||
if (!EVLRecipe) | ||
continue; | ||
|
||
|
@@ -2237,8 +2247,6 @@ static void transformRecipestoEVLRecipes(VPlan &Plan, VPValue &EVL) { | |
VPValue *CurVPV = CurRecipe->getVPSingleValue(); | ||
CurVPV->replaceAllUsesWith(EVLRecipe->getVPSingleValue()); | ||
} | ||
// Defer erasing recipes till the end so that we don't invalidate the | ||
// VPTypeAnalysis cache. | ||
ToErase.push_back(CurRecipe); | ||
} | ||
} | ||
|
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.
Instead of first checking if there are any first-order recurrence phis and then iterating over all recipes in the loop region, can we instead just iterate over all header phis , and simply iterate over the users to replace the splices?
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 think so, all splices need to have a first-order recurrence phi as an operand right? Will take a look and submit a follow up PR