Skip to content

Commit b36818f

Browse files
committed
fixup! [SLP] Use named structs in vectorizeStores() (NFC)
1 parent d8a2f21 commit b36818f

File tree

1 file changed

+44
-38
lines changed

1 file changed

+44
-38
lines changed

llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp

Lines changed: 44 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -884,6 +884,42 @@ class InstructionsState {
884884
static InstructionsState invalid() { return {nullptr, nullptr}; }
885885
};
886886

887+
/// A reference to a ld/st instruction and the distance of its address to a base
888+
/// pointer.
889+
struct MemInstAndPtrDist {
890+
MemInstAndPtrDist(unsigned InstrIdx, int DistToBasePtr)
891+
: InstrIdx(InstrIdx), DistToBasePtr(DistToBasePtr) {}
892+
unsigned InstrIdx;
893+
int DistToBasePtr;
894+
};
895+
struct PtrDistCompare {
896+
bool operator()(const MemInstAndPtrDist &Op1,
897+
const MemInstAndPtrDist &Op2) const {
898+
return Op1.DistToBasePtr < Op2.DistToBasePtr;
899+
}
900+
};
901+
902+
/// A group of memory instructions that we'll try to bundle together using
903+
/// vector ops. They are ordered using their signed distance to the address of
904+
/// this group's BaseInstr.
905+
struct RelatedMemInsts {
906+
RelatedMemInsts(unsigned BaseInstrIdx) { reset(BaseInstrIdx); }
907+
void reset(unsigned NewBaseInstr) {
908+
BaseInstrIdx = NewBaseInstr;
909+
Instrs.clear();
910+
Instrs.emplace(NewBaseInstr, 0);
911+
}
912+
913+
// TODO: This should probably just be an std::map
914+
using InstSet = std::set<MemInstAndPtrDist, PtrDistCompare>;
915+
916+
/// The index of the Base instruction, i.e. the one with a 0 pointer distance.
917+
unsigned BaseInstrIdx;
918+
919+
/// Maps an instruction index to a pointer distance from \p BaseInstrIdx.
920+
InstSet Instrs;
921+
};
922+
887923
} // end anonymous namespace
888924

889925
/// \returns true if \p Opcode is allowed as part of the main/alternate
@@ -19923,37 +19959,7 @@ bool SLPVectorizerPass::vectorizeStores(
1992319959
BoUpSLP::ValueSet VectorizedStores;
1992419960
bool Changed = false;
1992519961

19926-
/// A store instruction and the distance of its address to a base pointer.
19927-
struct CandidateInstr {
19928-
CandidateInstr(unsigned InstrIdx, int DistToBasePtr)
19929-
: InstrIdx(InstrIdx), DistToBasePtr(DistToBasePtr) {}
19930-
unsigned InstrIdx;
19931-
int DistToBasePtr;
19932-
};
19933-
struct StoreDistCompare {
19934-
bool operator()(const CandidateInstr &Op1,
19935-
const CandidateInstr &Op2) const {
19936-
return Op1.DistToBasePtr < Op2.DistToBasePtr;
19937-
}
19938-
};
19939-
19940-
/// A group of store instructions that we'll try to bundle together.
19941-
/// They are ordered using their signed distance to the address of this
19942-
/// set's BaseInstr.
19943-
struct CandidateBundle {
19944-
CandidateBundle(unsigned BaseInstrIdx) { reset(BaseInstrIdx); }
19945-
void reset(unsigned NewBaseInstr) {
19946-
BaseInstrIdx = NewBaseInstr;
19947-
Instrs.clear();
19948-
Instrs.emplace(NewBaseInstr, 0);
19949-
}
19950-
// TODO: This should probably just be an std::map
19951-
using CandidateSet = std::set<CandidateInstr, StoreDistCompare>;
19952-
unsigned BaseInstrIdx;
19953-
CandidateSet Instrs;
19954-
};
19955-
19956-
auto TryToVectorize = [&](const CandidateBundle::CandidateSet &StoreSeq) {
19962+
auto TryToVectorize = [&](const RelatedMemInsts::InstSet &StoreSeq) {
1995719963
int PrevDist = -1;
1995819964
BoUpSLP::ValueList Operands;
1995919965
// Collect the chain into a list.
@@ -20234,7 +20240,7 @@ bool SLPVectorizerPass::vectorizeStores(
2023420240
// Need to store the index of the very first store separately, since the set
2023520241
// may be reordered after the insertion and the first store may be moved. This
2023620242
// container allows to reduce number of calls of getPointersDiff() function.
20237-
SmallVector<CandidateBundle> SortedStores;
20243+
SmallVector<RelatedMemInsts> SortedStores;
2023820244

2023920245
// Inserts the specified store SI with the given index Idx to the set of the
2024020246
// stores. If the store with the same distance is found already - stop
@@ -20269,7 +20275,7 @@ bool SLPVectorizerPass::vectorizeStores(
2026920275
// dependencies and no need to waste compile time to try to vectorize them.
2027020276
// - Try to vectorize the sequence {1, {1, 0}, {3, 2}}.
2027120277
auto FillStoresSet = [&](unsigned Idx, StoreInst *SI) {
20272-
for (CandidateBundle &StoreSeq : SortedStores) {
20278+
for (RelatedMemInsts &StoreSeq : SortedStores) {
2027320279
std::optional<int> Diff = getPointersDiff(
2027420280
Stores[StoreSeq.BaseInstrIdx]->getValueOperand()->getType(),
2027520281
Stores[StoreSeq.BaseInstrIdx]->getPointerOperand(),
@@ -20286,9 +20292,9 @@ bool SLPVectorizerPass::vectorizeStores(
2028620292
TryToVectorize(StoreSeq.Instrs);
2028720293
unsigned ItIdx = It->InstrIdx;
2028820294
int ItDist = It->DistToBasePtr;
20289-
CandidateBundle::CandidateSet PrevSet;
20295+
RelatedMemInsts::InstSet PrevSet;
2029020296
copy_if(StoreSeq.Instrs, std::inserter(PrevSet, PrevSet.end()),
20291-
[&](const CandidateInstr &I) { return I.InstrIdx > ItIdx; });
20297+
[&](const MemInstAndPtrDist &I) { return I.InstrIdx > ItIdx; });
2029220298
StoreSeq.reset(Idx);
2029320299
// Insert stores that followed previous match to try to vectorize them
2029420300
// with this store.
@@ -20297,7 +20303,7 @@ bool SLPVectorizerPass::vectorizeStores(
2029720303
// Distances to previously found dup store (or this store, since they
2029820304
// store to the same addresses).
2029920305
SmallVector<int> Dists(Idx - StartIdx, 0);
20300-
for (const CandidateInstr &Store : reverse(PrevSet)) {
20306+
for (const MemInstAndPtrDist &Store : reverse(PrevSet)) {
2030120307
// Do not try to vectorize sequences, we already tried.
2030220308
if (VectorizedStores.contains(Stores[Store.InstrIdx]))
2030320309
break;
@@ -20323,7 +20329,7 @@ bool SLPVectorizerPass::vectorizeStores(
2032320329
PrevValTy = SI->getValueOperand()->getType();
2032420330
// Check that we do not try to vectorize stores of different types.
2032520331
if (PrevValTy != SI->getValueOperand()->getType()) {
20326-
for (CandidateBundle &StoreSeq : SortedStores)
20332+
for (RelatedMemInsts &StoreSeq : SortedStores)
2032720333
TryToVectorize(StoreSeq.Instrs);
2032820334
SortedStores.clear();
2032920335
PrevValTy = SI->getValueOperand()->getType();
@@ -20332,7 +20338,7 @@ bool SLPVectorizerPass::vectorizeStores(
2033220338
}
2033320339

2033420340
// Final vectorization attempt.
20335-
for (CandidateBundle &StoreSeq : SortedStores)
20341+
for (RelatedMemInsts &StoreSeq : SortedStores)
2033620342
TryToVectorize(StoreSeq.Instrs);
2033720343

2033820344
return Changed;

0 commit comments

Comments
 (0)