-
Notifications
You must be signed in to change notification settings - Fork 14.6k
[VPlan] Run narrowInterleaveGroups during general VPlan optimizations. #149706
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
Open
fhahn
wants to merge
1
commit into
llvm:main
Choose a base branch
from
fhahn:vplan-move-narrow-interleave-early
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+184
−71
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3146,19 +3146,20 @@ static bool isAlreadyNarrow(VPValue *VPV) { | |
return RepR && RepR->isSingleScalar(); | ||
} | ||
|
||
void VPlanTransforms::narrowInterleaveGroups(VPlan &Plan, ElementCount VF, | ||
unsigned VectorRegWidth) { | ||
std::unique_ptr<VPlan> | ||
VPlanTransforms::narrowInterleaveGroups(VPlan &Plan, unsigned VectorRegWidth, | ||
VFRange &Range) { | ||
using namespace llvm::VPlanPatternMatch; | ||
VPRegionBlock *VectorLoop = Plan.getVectorLoopRegion(); | ||
if (VF.isScalable() || !VectorLoop) | ||
return; | ||
if (Plan.hasScalableVF() || !VectorLoop) | ||
return nullptr; | ||
|
||
VPCanonicalIVPHIRecipe *CanonicalIV = Plan.getCanonicalIV(); | ||
Type *CanonicalIVType = CanonicalIV->getScalarType(); | ||
VPTypeAnalysis TypeInfo(CanonicalIVType); | ||
|
||
unsigned FixedVF = VF.getFixedValue(); | ||
SmallVector<VPInterleaveRecipe *> StoreGroups; | ||
std::optional<unsigned> VFToOptimize; | ||
for (auto &R : *VectorLoop->getEntryBasicBlock()) { | ||
if (isa<VPCanonicalIVPHIRecipe>(&R) || | ||
match(&R, m_BranchOnCount(m_VPValue(), m_VPValue()))) | ||
|
@@ -3173,30 +3174,47 @@ void VPlanTransforms::narrowInterleaveGroups(VPlan &Plan, ElementCount VF, | |
// * recipes writing to memory except interleave groups | ||
// Only support plans with a canonical induction phi. | ||
if (R.isPhi()) | ||
return; | ||
return nullptr; | ||
|
||
auto *InterleaveR = dyn_cast<VPInterleaveRecipe>(&R); | ||
if (R.mayWriteToMemory() && !InterleaveR) | ||
return; | ||
return nullptr; | ||
|
||
// Do not narrow interleave groups if there are VectorPointer recipes and | ||
// the plan was unrolled. The recipe implicitly uses VF from | ||
// VPTransformState. | ||
// TODO: Remove restriction once the VF for the VectorPointer offset is | ||
// modeled explicitly as operand. | ||
if (isa<VPVectorPointerRecipe>(&R) && Plan.getUF() > 1) | ||
return; | ||
return nullptr; | ||
|
||
// All other ops are allowed, but we reject uses that cannot be converted | ||
// when checking all allowed consumers (store interleave groups) below. | ||
if (!InterleaveR) | ||
continue; | ||
|
||
// Bail out on non-consecutive interleave groups. | ||
if (!isConsecutiveInterleaveGroup(InterleaveR, FixedVF, TypeInfo, | ||
VectorRegWidth)) | ||
return; | ||
|
||
// Try to find a single VF, where all interleave groups are consecutive and | ||
// saturate the full vector width. If we already have a candidate VF, check | ||
// if it is applicable for the current InterleaveR, otherwise look for a | ||
// suitable VF across the Plans VFs. | ||
// | ||
if (VFToOptimize) { | ||
if (!isConsecutiveInterleaveGroup(InterleaveR, *VFToOptimize, TypeInfo, | ||
VectorRegWidth)) | ||
return nullptr; | ||
} else { | ||
for (ElementCount VF : Plan.vectorFactors()) { | ||
if (!VF.isFixed()) | ||
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. Why are we bailing out for scalable vectors here? Is there a good reason why this cannot work for scalable vectors? |
||
continue; | ||
if (isConsecutiveInterleaveGroup(InterleaveR, VF.getFixedValue(), | ||
TypeInfo, VectorRegWidth)) { | ||
VFToOptimize = VF.getFixedValue(); | ||
break; | ||
} | ||
} | ||
if (!VFToOptimize) | ||
return nullptr; | ||
} | ||
// Skip read interleave groups. | ||
if (InterleaveR->getStoredValues().empty()) | ||
continue; | ||
|
@@ -3232,24 +3250,44 @@ void VPlanTransforms::narrowInterleaveGroups(VPlan &Plan, ElementCount VF, | |
auto *WideMember0 = dyn_cast_or_null<VPWidenRecipe>( | ||
InterleaveR->getStoredValues()[0]->getDefiningRecipe()); | ||
if (!WideMember0) | ||
return; | ||
return nullptr; | ||
for (const auto &[I, V] : enumerate(InterleaveR->getStoredValues())) { | ||
auto *R = dyn_cast_or_null<VPWidenRecipe>(V->getDefiningRecipe()); | ||
if (!R || R->getOpcode() != WideMember0->getOpcode() || | ||
R->getNumOperands() > 2) | ||
return; | ||
return nullptr; | ||
if (any_of(enumerate(R->operands()), | ||
[WideMember0, Idx = I](const auto &P) { | ||
const auto &[OpIdx, OpV] = P; | ||
return !canNarrowLoad(WideMember0, OpIdx, OpV, Idx); | ||
})) | ||
return; | ||
return nullptr; | ||
} | ||
StoreGroups.push_back(InterleaveR); | ||
} | ||
|
||
if (StoreGroups.empty()) | ||
return; | ||
return nullptr; | ||
|
||
// All interleave groups in Plan can be narrowed for VFToOptimize. Split the | ||
// original Plan into 2: a) a new clone which contains all VFs of Plan, except | ||
// VFToOptimize, and b) the original Plan with VFToOptimize as single VF. | ||
std::unique_ptr<VPlan> NewPlan; | ||
if (size(Plan.vectorFactors()) != 1) { | ||
NewPlan = std::unique_ptr<VPlan>(Plan.duplicate()); | ||
Plan.setVF(ElementCount::getFixed(*VFToOptimize)); | ||
bool First = true; | ||
for (ElementCount VF : NewPlan->vectorFactors()) { | ||
if (VF.isFixed() && VF.getFixedValue() == *VFToOptimize) | ||
continue; | ||
if (First) { | ||
NewPlan->setVF(VF); | ||
First = false; | ||
continue; | ||
} | ||
NewPlan->addVF(VF); | ||
} | ||
} | ||
|
||
// Convert InterleaveGroup \p R to a single VPWidenLoadRecipe. | ||
auto NarrowOp = [](VPValue *V) -> VPValue * { | ||
|
@@ -3314,11 +3352,11 @@ void VPlanTransforms::narrowInterleaveGroups(VPlan &Plan, ElementCount VF, | |
// original iteration. | ||
auto *CanIV = Plan.getCanonicalIV(); | ||
auto *Inc = cast<VPInstruction>(CanIV->getBackedgeValue()); | ||
Inc->setOperand(1, Plan.getOrAddLiveIn(ConstantInt::get( | ||
CanIV->getScalarType(), 1 * Plan.getUF()))); | ||
Inc->setOperand(1, &Plan.getSymbolicUF()); | ||
Plan.getVF().replaceAllUsesWith( | ||
Plan.getOrAddLiveIn(ConstantInt::get(CanIV->getScalarType(), 1))); | ||
removeDeadRecipes(Plan); | ||
return NewPlan; | ||
} | ||
|
||
/// Add branch weight metadata, if the \p Plan's middle block is terminated by a | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
const