Skip to content

Commit ef65d73

Browse files
committed
[SLP] More OOP to simplify vectorizeStores() (NFC)
This moves more code into the RelatedStoreInsts helper class. The FillStoresSet lambda is now only a couple of lines and is easier to read.
1 parent 842bc07 commit ef65d73

File tree

1 file changed

+80
-57
lines changed

1 file changed

+80
-57
lines changed

llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp

Lines changed: 80 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -20856,9 +20856,15 @@ namespace {
2085620856
/// A group of stores that we'll try to bundle together using vector ops.
2085720857
/// They are ordered using the signed distance of their address operand to the
2085820858
/// address of this group's BaseInstr.
20859-
struct RelatedStoreInsts {
20860-
RelatedStoreInsts(unsigned BaseInstrIdx) { reset(BaseInstrIdx); }
20859+
class RelatedStoreInsts {
20860+
public:
20861+
RelatedStoreInsts(unsigned BaseInstrIdx, ArrayRef<StoreInst *> AllStores)
20862+
: AllStores(AllStores) {
20863+
reset(BaseInstrIdx);
20864+
}
20865+
2086120866
void reset(unsigned NewBaseInstr) {
20867+
assert(NewBaseInstr < AllStores.size());
2086220868
BaseInstrIdx = NewBaseInstr;
2086320869
Instrs.clear();
2086420870
insertOrLookup(NewBaseInstr, 0);
@@ -20873,12 +20879,54 @@ struct RelatedStoreInsts {
2087320879
return Inserted ? std::nullopt : std::optional<unsigned>(It->second);
2087420880
}
2087520881

20882+
StoreInst &getBaseStore() const { return *AllStores[BaseInstrIdx]; }
20883+
using DistToInstMap = std::map<int, unsigned>;
20884+
const DistToInstMap &getStores() const { return Instrs; }
20885+
20886+
/// Recompute the pointer distances to be based on \p NewBaseInstIdx.
20887+
/// Stores whose index is less than \p MinSafeIdx will be dropped.
20888+
void rebase(unsigned MinSafeIdx, unsigned NewBaseInstIdx,
20889+
int DistFromCurBase) {
20890+
DistToInstMap PrevSet = std::move(Instrs);
20891+
reset(NewBaseInstIdx);
20892+
20893+
// Re-insert stores that come after MinSafeIdx to try and vectorize them
20894+
// again. Their distance will be "rebased" to use NewBaseInstIdx as
20895+
// reference.
20896+
for (auto [Dist, InstIdx] : PrevSet) {
20897+
if (InstIdx >= MinSafeIdx) {
20898+
insertOrLookup(InstIdx, Dist - DistFromCurBase);
20899+
}
20900+
}
20901+
}
20902+
20903+
/// Remove all stores that have been vectorized from this group.
20904+
void clearVectorizedStores(const BoUpSLP::ValueSet &VectorizedStores) {
20905+
const auto Begin = Instrs.begin();
20906+
auto NonVectorizedStore = Instrs.end();
20907+
20908+
while (NonVectorizedStore != Begin) {
20909+
const auto Prev = std::prev(NonVectorizedStore);
20910+
unsigned InstrIdx = Prev->second;
20911+
if (VectorizedStores.contains(AllStores[InstrIdx])) {
20912+
// NonVectorizedStore is the last scalar instruction.
20913+
// Erase all stores before it so we don't try to vectorize them again.
20914+
Instrs.erase(Begin, NonVectorizedStore);
20915+
return;
20916+
}
20917+
NonVectorizedStore = Prev;
20918+
}
20919+
}
20920+
20921+
private:
2087620922
/// The index of the Base instruction, i.e. the one with a 0 pointer distance.
2087720923
unsigned BaseInstrIdx;
2087820924

2087920925
/// Maps a pointer distance from \p BaseInstrIdx to an instruction index.
20880-
using DistToInstMap = std::map<int, unsigned>;
2088120926
DistToInstMap Instrs;
20927+
20928+
/// Reference to all the stores in the BB being analyzed.
20929+
ArrayRef<StoreInst *> AllStores;
2088220930
};
2088320931

2088420932
} // end anonymous namespace
@@ -21166,14 +21214,7 @@ bool SLPVectorizerPass::vectorizeStores(
2116621214
}
2116721215
};
2116821216

21169-
// Stores pair (first: index of the store into Stores array ref, address of
21170-
// which taken as base, second: sorted set of pairs {index, dist}, which are
21171-
// indices of stores in the set and their store location distances relative to
21172-
// the base address).
21173-
21174-
// Need to store the index of the very first store separately, since the set
21175-
// may be reordered after the insertion and the first store may be moved. This
21176-
// container allows to reduce number of calls of getPointersDiff() function.
21217+
/// Groups of stores to vectorize
2117721218
SmallVector<RelatedStoreInsts> SortedStores;
2117821219

2117921220
// Inserts the specified store SI with the given index Idx to the set of the
@@ -21209,52 +21250,34 @@ bool SLPVectorizerPass::vectorizeStores(
2120921250
// dependencies and no need to waste compile time to try to vectorize them.
2121021251
// - Try to vectorize the sequence {1, {1, 0}, {3, 2}}.
2121121252
auto FillStoresSet = [&](unsigned Idx, StoreInst *SI) {
21212-
for (RelatedStoreInsts &StoreSeq : SortedStores) {
21213-
std::optional<int> Diff = getPointersDiff(
21214-
Stores[StoreSeq.BaseInstrIdx]->getValueOperand()->getType(),
21215-
Stores[StoreSeq.BaseInstrIdx]->getPointerOperand(),
21216-
SI->getValueOperand()->getType(), SI->getPointerOperand(), *DL, *SE,
21217-
/*StrictCheck=*/true);
21218-
if (!Diff)
21219-
continue;
21220-
std::optional<unsigned> PrevInst =
21221-
StoreSeq.insertOrLookup(/*InstrIdx=*/Idx, /*PtrDist=*/*Diff);
21222-
if (!PrevInst) {
21223-
// No store was associated to that distance. Keep collecting.
21224-
return;
21225-
}
21226-
// Try to vectorize the first found set to avoid duplicate analysis.
21227-
TryToVectorize(StoreSeq.Instrs);
21228-
RelatedStoreInsts::DistToInstMap PrevSet;
21229-
copy_if(StoreSeq.Instrs, std::inserter(PrevSet, PrevSet.end()),
21230-
[&](const std::pair<int, unsigned> &DistAndIdx) {
21231-
return DistAndIdx.second > *PrevInst;
21232-
});
21233-
StoreSeq.reset(Idx);
21234-
// Insert stores that followed previous match to try to vectorize them
21235-
// with this store.
21236-
unsigned StartIdx = *PrevInst + 1;
21237-
SmallBitVector UsedStores(Idx - StartIdx);
21238-
// Distances to previously found dup store (or this store, since they
21239-
// store to the same addresses).
21240-
SmallVector<int> Dists(Idx - StartIdx, 0);
21241-
for (auto [PtrDist, InstIdx] : reverse(PrevSet)) {
21242-
// Do not try to vectorize sequences, we already tried.
21243-
if (VectorizedStores.contains(Stores[InstIdx]))
21244-
break;
21245-
unsigned BI = InstIdx - StartIdx;
21246-
UsedStores.set(BI);
21247-
Dists[BI] = PtrDist - *Diff;
21248-
}
21249-
for (unsigned I = StartIdx; I < Idx; ++I) {
21250-
unsigned BI = I - StartIdx;
21251-
if (UsedStores.test(BI))
21252-
StoreSeq.insertOrLookup(I, Dists[BI]);
21253-
}
21253+
std::optional<int> Diff;
21254+
auto *RelatedStores =
21255+
find_if(SortedStores, [&](const RelatedStoreInsts &StoreSeq) {
21256+
StoreInst &BaseStore = StoreSeq.getBaseStore();
21257+
Diff = getPointersDiff(BaseStore.getValueOperand()->getType(),
21258+
BaseStore.getPointerOperand(),
21259+
SI->getValueOperand()->getType(),
21260+
SI->getPointerOperand(), *DL, *SE,
21261+
/*StrictCheck=*/true);
21262+
return Diff.has_value();
21263+
});
21264+
21265+
// We did not find a comparable store, start a new group.
21266+
if (RelatedStores == SortedStores.end()) {
21267+
SortedStores.emplace_back(Idx, Stores);
2125421268
return;
2125521269
}
21256-
// We did not find a comparable store, start a new sequence.
21257-
SortedStores.emplace_back(Idx);
21270+
21271+
// If there is already a store in the group with the same PtrDiff, try to
21272+
// vectorize the existing instructions before adding the current store.
21273+
if (std::optional<unsigned> PrevInst =
21274+
RelatedStores->insertOrLookup(Idx, *Diff)) {
21275+
TryToVectorize(RelatedStores->getStores());
21276+
RelatedStores->clearVectorizedStores(VectorizedStores);
21277+
RelatedStores->rebase(/*MinSafeIdx=*/*PrevInst + 1,
21278+
/*NewBaseInstIdx=*/Idx,
21279+
/*DistFromCurBase=*/*Diff);
21280+
}
2125821281
};
2125921282
Type *PrevValTy = nullptr;
2126021283
for (auto [I, SI] : enumerate(Stores)) {
@@ -21265,7 +21288,7 @@ bool SLPVectorizerPass::vectorizeStores(
2126521288
// Check that we do not try to vectorize stores of different types.
2126621289
if (PrevValTy != SI->getValueOperand()->getType()) {
2126721290
for (RelatedStoreInsts &StoreSeq : SortedStores)
21268-
TryToVectorize(StoreSeq.Instrs);
21291+
TryToVectorize(StoreSeq.getStores());
2126921292
SortedStores.clear();
2127021293
PrevValTy = SI->getValueOperand()->getType();
2127121294
}
@@ -21274,7 +21297,7 @@ bool SLPVectorizerPass::vectorizeStores(
2127421297

2127521298
// Final vectorization attempt.
2127621299
for (RelatedStoreInsts &StoreSeq : SortedStores)
21277-
TryToVectorize(StoreSeq.Instrs);
21300+
TryToVectorize(StoreSeq.getStores());
2127821301

2127921302
return Changed;
2128021303
}

0 commit comments

Comments
 (0)