Skip to content

Commit 0da1087

Browse files
fhahnmahesh-attarde
authored andcommitted
[LV] Re-compute cost of scalarized load users.
If there are direct memory op users of the newly scalarized load, their cost may have changed because there's no scalarization overhead for the operand. Update it. This ensures assigning consistent costs to scalarized memory instructions that themselves have scalarized memory instructions as operands.
1 parent f1bb4fb commit 0da1087

File tree

2 files changed

+285
-39
lines changed

2 files changed

+285
-39
lines changed

llvm/lib/Transforms/Vectorize/LoopVectorize.cpp

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5699,6 +5699,20 @@ void LoopVectorizationCostModel::setCostBasedWideningDecision(ElementCount VF) {
56995699
Worklist.push_back(InstOp);
57005700
}
57015701

5702+
auto UpdateMemOpUserCost = [this, VF](LoadInst *LI) {
5703+
// If there are direct memory op users of the newly scalarized load,
5704+
// their cost may have changed because there's no scalarization
5705+
// overhead for the operand. Update it.
5706+
for (User *U : LI->users()) {
5707+
if (!isa<LoadInst, StoreInst>(U))
5708+
continue;
5709+
if (getWideningDecision(cast<Instruction>(U), VF) != CM_Scalarize)
5710+
continue;
5711+
setWideningDecision(
5712+
cast<Instruction>(U), VF, CM_Scalarize,
5713+
getMemInstScalarizationCost(cast<Instruction>(U), VF));
5714+
}
5715+
};
57025716
for (auto *I : AddrDefs) {
57035717
if (isa<LoadInst>(I)) {
57045718
// Setting the desired widening decision should ideally be handled in
@@ -5708,21 +5722,24 @@ void LoopVectorizationCostModel::setCostBasedWideningDecision(ElementCount VF) {
57085722
InstWidening Decision = getWideningDecision(I, VF);
57095723
if (Decision == CM_Widen || Decision == CM_Widen_Reverse ||
57105724
(!isPredicatedInst(I) && !Legal->isUniformMemOp(*I, VF) &&
5711-
Decision == CM_Scalarize))
5725+
Decision == CM_Scalarize)) {
57125726
// Scalarize a widened load of address or update the cost of a scalar
57135727
// load of an address.
57145728
setWideningDecision(
57155729
I, VF, CM_Scalarize,
57165730
(VF.getKnownMinValue() *
57175731
getMemoryInstructionCost(I, ElementCount::getFixed(1))));
5718-
else if (const auto *Group = getInterleavedAccessGroup(I)) {
5732+
UpdateMemOpUserCost(cast<LoadInst>(I));
5733+
} else if (const auto *Group = getInterleavedAccessGroup(I)) {
57195734
// Scalarize an interleave group of address loads.
57205735
for (unsigned I = 0; I < Group->getFactor(); ++I) {
5721-
if (Instruction *Member = Group->getMember(I))
5736+
if (Instruction *Member = Group->getMember(I)) {
57225737
setWideningDecision(
57235738
Member, VF, CM_Scalarize,
57245739
(VF.getKnownMinValue() *
57255740
getMemoryInstructionCost(Member, ElementCount::getFixed(1))));
5741+
UpdateMemOpUserCost(cast<LoadInst>(Member));
5742+
}
57265743
}
57275744
}
57285745
} else {

0 commit comments

Comments
 (0)