Skip to content

Commit 8687fac

Browse files
committed
[SLP] Use named structs in vectorizeStores() (NFC)
This is a mostly straightforward replacement of the previous std::pair<int, std::set<std::pair<...>>> data structure with slightly more readable alternatives. One can use a more standard std::map for mapping pointer distances to instruction indices.
1 parent 29ca03f commit 8687fac

File tree

1 file changed

+71
-49
lines changed

1 file changed

+71
-49
lines changed

llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp

Lines changed: 71 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -19945,6 +19945,41 @@ static bool checkTreeSizes(ArrayRef<std::pair<unsigned, unsigned>> Sizes,
1994519945
return Dev * 96 / (Mean * Mean) == 0;
1994619946
}
1994719947

19948+
namespace {
19949+
19950+
/// A group of instructions that we'll try to bundle together using vector ops.
19951+
/// They are ordered using the signed distance of their address operand to the
19952+
/// address of this group's BaseInstr.
19953+
struct RelatedStoreInsts {
19954+
RelatedStoreInsts(unsigned BaseInstrIdx) { reset(BaseInstrIdx); }
19955+
void reset(unsigned NewBaseInstr) {
19956+
BaseInstrIdx = NewBaseInstr;
19957+
Instrs.clear();
19958+
insert(NewBaseInstr, 0);
19959+
}
19960+
19961+
void insert(unsigned InstrIdx, int PtrDist) {
19962+
Instrs.emplace(PtrDist, InstrIdx);
19963+
}
19964+
19965+
/// Return the instruction with a pointer distance of \p PtrDist, or nullopt.
19966+
std::optional<unsigned> getInstIdx(int PtrDist) const {
19967+
auto It = Instrs.find(PtrDist);
19968+
if (It != Instrs.end())
19969+
return It->second;
19970+
return std::nullopt;
19971+
}
19972+
19973+
/// The index of the Base instruction, i.e. the one with a 0 pointer distance.
19974+
unsigned BaseInstrIdx;
19975+
19976+
/// Maps a pointer distance from \p BaseInstrIdx to an instruction index.
19977+
using DistToInstMap = std::map<int, unsigned>;
19978+
DistToInstMap Instrs;
19979+
};
19980+
19981+
} // end anonymous namespace
19982+
1994819983
bool SLPVectorizerPass::vectorizeStores(
1994919984
ArrayRef<StoreInst *> Stores, BoUpSLP &R,
1995019985
DenseSet<std::tuple<Value *, Value *, Value *, Value *, unsigned>>
@@ -19954,31 +19989,22 @@ bool SLPVectorizerPass::vectorizeStores(
1995419989
BoUpSLP::ValueSet VectorizedStores;
1995519990
bool Changed = false;
1995619991

19957-
struct StoreDistCompare {
19958-
bool operator()(const std::pair<unsigned, int> &Op1,
19959-
const std::pair<unsigned, int> &Op2) const {
19960-
return Op1.second < Op2.second;
19961-
}
19962-
};
19963-
// A set of pairs (index of store in Stores array ref, Distance of the store
19964-
// address relative to base store address in units).
19965-
using StoreIndexToDistSet =
19966-
std::set<std::pair<unsigned, int>, StoreDistCompare>;
19967-
auto TryToVectorize = [&](const StoreIndexToDistSet &Set) {
19992+
auto TryToVectorize = [&](const RelatedStoreInsts::DistToInstMap &StoreSeq) {
1996819993
int PrevDist = -1;
1996919994
BoUpSLP::ValueList Operands;
1997019995
// Collect the chain into a list.
19971-
for (auto [Idx, Data] : enumerate(Set)) {
19972-
if (Operands.empty() || Data.second - PrevDist == 1) {
19973-
Operands.push_back(Stores[Data.first]);
19974-
PrevDist = Data.second;
19975-
if (Idx != Set.size() - 1)
19996+
for (auto [Idx, Data] : enumerate(StoreSeq)) {
19997+
auto &[Dist, InstIdx] = Data;
19998+
if (Operands.empty() || Dist - PrevDist == 1) {
19999+
Operands.push_back(Stores[InstIdx]);
20000+
PrevDist = Dist;
20001+
if (Idx != StoreSeq.size() - 1)
1997620002
continue;
1997720003
}
19978-
auto E = make_scope_exit([&, &DataVar = Data]() {
20004+
auto E = make_scope_exit([&, &Dist = Dist, &InstIdx = InstIdx]() {
1997920005
Operands.clear();
19980-
Operands.push_back(Stores[DataVar.first]);
19981-
PrevDist = DataVar.second;
20006+
Operands.push_back(Stores[InstIdx]);
20007+
PrevDist = Dist;
1998220008
});
1998320009

1998420010
if (Operands.size() <= 1 ||
@@ -20245,7 +20271,8 @@ bool SLPVectorizerPass::vectorizeStores(
2024520271
// Need to store the index of the very first store separately, since the set
2024620272
// may be reordered after the insertion and the first store may be moved. This
2024720273
// container allows to reduce number of calls of getPointersDiff() function.
20248-
SmallVector<std::pair<unsigned, StoreIndexToDistSet>> SortedStores;
20274+
SmallVector<RelatedStoreInsts> SortedStores;
20275+
2024920276
// Inserts the specified store SI with the given index Idx to the set of the
2025020277
// stores. If the store with the same distance is found already - stop
2025120278
// insertion, try to vectorize already found stores. If some stores from this
@@ -20279,56 +20306,51 @@ bool SLPVectorizerPass::vectorizeStores(
2027920306
// dependencies and no need to waste compile time to try to vectorize them.
2028020307
// - Try to vectorize the sequence {1, {1, 0}, {3, 2}}.
2028120308
auto FillStoresSet = [&](unsigned Idx, StoreInst *SI) {
20282-
for (std::pair<unsigned, StoreIndexToDistSet> &Set : SortedStores) {
20309+
for (RelatedStoreInsts &StoreSeq : SortedStores) {
2028320310
std::optional<int> Diff = getPointersDiff(
20284-
Stores[Set.first]->getValueOperand()->getType(),
20285-
Stores[Set.first]->getPointerOperand(),
20311+
Stores[StoreSeq.BaseInstrIdx]->getValueOperand()->getType(),
20312+
Stores[StoreSeq.BaseInstrIdx]->getPointerOperand(),
2028620313
SI->getValueOperand()->getType(), SI->getPointerOperand(), *DL, *SE,
2028720314
/*StrictCheck=*/true);
2028820315
if (!Diff)
2028920316
continue;
20290-
auto It = Set.second.find(std::make_pair(Idx, *Diff));
20291-
if (It == Set.second.end()) {
20292-
Set.second.emplace(Idx, *Diff);
20317+
std::optional<unsigned> PrevInst = StoreSeq.getInstIdx(/*PtrDist=*/*Diff);
20318+
if (!PrevInst) {
20319+
StoreSeq.insert(Idx, *Diff);
2029320320
return;
2029420321
}
2029520322
// Try to vectorize the first found set to avoid duplicate analysis.
20296-
TryToVectorize(Set.second);
20297-
unsigned ItIdx = It->first;
20298-
int ItDist = It->second;
20299-
StoreIndexToDistSet PrevSet;
20300-
copy_if(Set.second, std::inserter(PrevSet, PrevSet.end()),
20301-
[&](const std::pair<unsigned, int> &Pair) {
20302-
return Pair.first > ItIdx;
20323+
TryToVectorize(StoreSeq.Instrs);
20324+
RelatedStoreInsts::DistToInstMap PrevSet;
20325+
copy_if(StoreSeq.Instrs, std::inserter(PrevSet, PrevSet.end()),
20326+
[&](const std::pair<int, unsigned> &DistAndIdx) {
20327+
return DistAndIdx.second > *PrevInst;
2030320328
});
20304-
Set.second.clear();
20305-
Set.first = Idx;
20306-
Set.second.emplace(Idx, 0);
20329+
StoreSeq.reset(Idx);
2030720330
// Insert stores that followed previous match to try to vectorize them
2030820331
// with this store.
20309-
unsigned StartIdx = ItIdx + 1;
20332+
unsigned StartIdx = *PrevInst + 1;
2031020333
SmallBitVector UsedStores(Idx - StartIdx);
2031120334
// Distances to previously found dup store (or this store, since they
2031220335
// store to the same addresses).
2031320336
SmallVector<int> Dists(Idx - StartIdx, 0);
20314-
for (const std::pair<unsigned, int> &Pair : reverse(PrevSet)) {
20337+
for (auto [PtrDist, InstIdx] : reverse(PrevSet)) {
2031520338
// Do not try to vectorize sequences, we already tried.
20316-
if (VectorizedStores.contains(Stores[Pair.first]))
20339+
if (VectorizedStores.contains(Stores[InstIdx]))
2031720340
break;
20318-
unsigned BI = Pair.first - StartIdx;
20341+
unsigned BI = InstIdx - StartIdx;
2031920342
UsedStores.set(BI);
20320-
Dists[BI] = Pair.second - ItDist;
20343+
Dists[BI] = PtrDist - *Diff;
2032120344
}
2032220345
for (unsigned I = StartIdx; I < Idx; ++I) {
2032320346
unsigned BI = I - StartIdx;
2032420347
if (UsedStores.test(BI))
20325-
Set.second.emplace(I, Dists[BI]);
20348+
StoreSeq.insert(I, Dists[BI]);
2032620349
}
2032720350
return;
2032820351
}
20329-
auto &Res = SortedStores.emplace_back();
20330-
Res.first = Idx;
20331-
Res.second.emplace(Idx, 0);
20352+
// We did not find a comparable store, start a new sequence.
20353+
SortedStores.emplace_back(Idx);
2033220354
};
2033320355
Type *PrevValTy = nullptr;
2033420356
for (auto [I, SI] : enumerate(Stores)) {
@@ -20338,17 +20360,17 @@ bool SLPVectorizerPass::vectorizeStores(
2033820360
PrevValTy = SI->getValueOperand()->getType();
2033920361
// Check that we do not try to vectorize stores of different types.
2034020362
if (PrevValTy != SI->getValueOperand()->getType()) {
20341-
for (auto &Set : SortedStores)
20342-
TryToVectorize(Set.second);
20363+
for (RelatedStoreInsts &StoreSeq : SortedStores)
20364+
TryToVectorize(StoreSeq.Instrs);
2034320365
SortedStores.clear();
2034420366
PrevValTy = SI->getValueOperand()->getType();
2034520367
}
2034620368
FillStoresSet(I, SI);
2034720369
}
2034820370

2034920371
// Final vectorization attempt.
20350-
for (auto &Set : SortedStores)
20351-
TryToVectorize(Set.second);
20372+
for (RelatedStoreInsts &StoreSeq : SortedStores)
20373+
TryToVectorize(StoreSeq.Instrs);
2035220374

2035320375
return Changed;
2035420376
}

0 commit comments

Comments
 (0)