Skip to content

Commit 816068e

Browse files
authored
[NFC][SLP] Remove useless code of the schedule (#104697)
Currently, the SLP schedule has two containers of `ScheduleData`: `ExtraScheduleDataMap` and `ScheduleDataMap`. However, the `ScheduleData` in `ExtraScheduleDataMap` is only used to indicate whether the instruction is processed or not and does not participate in the schedule, which is useless. `ScheduleDataMap` is sufficient for this purpose. The `OpValue` member is used only in `ExtraScheduleDataMap`, which is also useless.
1 parent f2fcd9c commit 816068e

File tree

1 file changed

+38
-117
lines changed

1 file changed

+38
-117
lines changed

llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp

Lines changed: 38 additions & 117 deletions
Original file line numberDiff line numberDiff line change
@@ -795,16 +795,6 @@ struct InstructionsState {
795795

796796
} // end anonymous namespace
797797

798-
/// Chooses the correct key for scheduling data. If \p Op has the same (or
799-
/// alternate) opcode as \p OpValue, the key is \p Op. Otherwise the key is \p
800-
/// OpValue.
801-
static Value *isOneOf(const InstructionsState &S, Value *Op) {
802-
auto *I = dyn_cast<Instruction>(Op);
803-
if (I && S.isOpcodeOrAlt(I))
804-
return Op;
805-
return S.OpValue;
806-
}
807-
808798
/// \returns true if \p Opcode is allowed as part of the main/alternate
809799
/// instruction for SLP vectorization.
810800
///
@@ -3583,14 +3573,14 @@ class BoUpSLP {
35833573

35843574
ScheduleData() = default;
35853575

3586-
void init(int BlockSchedulingRegionID, Value *OpVal) {
3576+
void init(int BlockSchedulingRegionID, Instruction *I) {
35873577
FirstInBundle = this;
35883578
NextInBundle = nullptr;
35893579
NextLoadStore = nullptr;
35903580
IsScheduled = false;
35913581
SchedulingRegionID = BlockSchedulingRegionID;
35923582
clearDependencies();
3593-
OpValue = OpVal;
3583+
Inst = I;
35943584
TE = nullptr;
35953585
}
35963586

@@ -3696,9 +3686,6 @@ class BoUpSLP {
36963686

36973687
Instruction *Inst = nullptr;
36983688

3699-
/// Opcode of the current instruction in the schedule data.
3700-
Value *OpValue = nullptr;
3701-
37023689
/// The TreeEntry that this instruction corresponds to.
37033690
TreeEntry *TE = nullptr;
37043691

@@ -3815,18 +3802,6 @@ class BoUpSLP {
38153802
return nullptr;
38163803
}
38173804

3818-
ScheduleData *getScheduleData(Value *V, Value *Key) {
3819-
if (V == Key)
3820-
return getScheduleData(V);
3821-
auto I = ExtraScheduleDataMap.find(V);
3822-
if (I != ExtraScheduleDataMap.end()) {
3823-
ScheduleData *SD = I->second.lookup(Key);
3824-
if (SD && isInSchedulingRegion(SD))
3825-
return SD;
3826-
}
3827-
return nullptr;
3828-
}
3829-
38303805
bool isInSchedulingRegion(ScheduleData *SD) const {
38313806
return SD->SchedulingRegionID == SchedulingRegionID;
38323807
}
@@ -3840,27 +3815,24 @@ class BoUpSLP {
38403815

38413816
for (ScheduleData *BundleMember = SD; BundleMember;
38423817
BundleMember = BundleMember->NextInBundle) {
3843-
if (BundleMember->Inst != BundleMember->OpValue)
3844-
continue;
38453818

38463819
// Handle the def-use chain dependencies.
38473820

38483821
// Decrement the unscheduled counter and insert to ready list if ready.
38493822
auto &&DecrUnsched = [this, &ReadyList](Instruction *I) {
3850-
doForAllOpcodes(I, [&ReadyList](ScheduleData *OpDef) {
3851-
if (OpDef && OpDef->hasValidDependencies() &&
3852-
OpDef->incrementUnscheduledDeps(-1) == 0) {
3853-
// There are no more unscheduled dependencies after
3854-
// decrementing, so we can put the dependent instruction
3855-
// into the ready list.
3856-
ScheduleData *DepBundle = OpDef->FirstInBundle;
3857-
assert(!DepBundle->IsScheduled &&
3858-
"already scheduled bundle gets ready");
3859-
ReadyList.insert(DepBundle);
3860-
LLVM_DEBUG(dbgs()
3861-
<< "SLP: gets ready (def): " << *DepBundle << "\n");
3862-
}
3863-
});
3823+
ScheduleData *OpDef = getScheduleData(I);
3824+
if (OpDef && OpDef->hasValidDependencies() &&
3825+
OpDef->incrementUnscheduledDeps(-1) == 0) {
3826+
// There are no more unscheduled dependencies after
3827+
// decrementing, so we can put the dependent instruction
3828+
// into the ready list.
3829+
ScheduleData *DepBundle = OpDef->FirstInBundle;
3830+
assert(!DepBundle->IsScheduled &&
3831+
"already scheduled bundle gets ready");
3832+
ReadyList.insert(DepBundle);
3833+
LLVM_DEBUG(dbgs()
3834+
<< "SLP: gets ready (def): " << *DepBundle << "\n");
3835+
}
38643836
};
38653837

38663838
// If BundleMember is a vector bundle, its operands may have been
@@ -3944,8 +3916,7 @@ class BoUpSLP {
39443916
"primary schedule data not in window?");
39453917
assert(isInSchedulingRegion(SD->FirstInBundle) &&
39463918
"entire bundle in window!");
3947-
(void)SD;
3948-
doForAllOpcodes(I, [](ScheduleData *SD) { SD->verify(); });
3919+
SD->verify();
39493920
}
39503921

39513922
for (auto *SD : ReadyInsts) {
@@ -3955,29 +3926,17 @@ class BoUpSLP {
39553926
}
39563927
}
39573928

3958-
void doForAllOpcodes(Value *V,
3959-
function_ref<void(ScheduleData *SD)> Action) {
3960-
if (ScheduleData *SD = getScheduleData(V))
3961-
Action(SD);
3962-
auto I = ExtraScheduleDataMap.find(V);
3963-
if (I != ExtraScheduleDataMap.end())
3964-
for (auto &P : I->second)
3965-
if (isInSchedulingRegion(P.second))
3966-
Action(P.second);
3967-
}
3968-
39693929
/// Put all instructions into the ReadyList which are ready for scheduling.
39703930
template <typename ReadyListType>
39713931
void initialFillReadyList(ReadyListType &ReadyList) {
39723932
for (auto *I = ScheduleStart; I != ScheduleEnd; I = I->getNextNode()) {
3973-
doForAllOpcodes(I, [&](ScheduleData *SD) {
3974-
if (SD->isSchedulingEntity() && SD->hasValidDependencies() &&
3975-
SD->isReady()) {
3976-
ReadyList.insert(SD);
3977-
LLVM_DEBUG(dbgs()
3978-
<< "SLP: initially in ready list: " << *SD << "\n");
3979-
}
3980-
});
3933+
ScheduleData *SD = getScheduleData(I);
3934+
if (SD && SD->isSchedulingEntity() && SD->hasValidDependencies() &&
3935+
SD->isReady()) {
3936+
ReadyList.insert(SD);
3937+
LLVM_DEBUG(dbgs()
3938+
<< "SLP: initially in ready list: " << *SD << "\n");
3939+
}
39813940
}
39823941
}
39833942

@@ -4035,10 +3994,6 @@ class BoUpSLP {
40353994
/// ScheduleData structures are recycled.
40363995
DenseMap<Instruction *, ScheduleData *> ScheduleDataMap;
40373996

4038-
/// Attaches ScheduleData to Instruction with the leading key.
4039-
DenseMap<Value *, SmallDenseMap<Value *, ScheduleData *>>
4040-
ExtraScheduleDataMap;
4041-
40423997
/// The ready-list for scheduling (only used for the dry-run).
40433998
SetVector<ScheduleData *> ReadyInsts;
40443999

@@ -11898,8 +11853,7 @@ Instruction &BoUpSLP::getLastInstructionInBundle(const TreeEntry *E) {
1189811853
auto *Bundle = BlocksSchedules[BB]->getScheduleData(V);
1189911854
if (Bundle && Bundle->isPartOfBundle())
1190011855
for (; Bundle; Bundle = Bundle->NextInBundle)
11901-
if (Bundle->OpValue == Bundle->Inst)
11902-
Res.second = Bundle->Inst;
11856+
Res.second = Bundle->Inst;
1190311857
}
1190411858

1190511859
// LastInst can still be null at this point if there's either not an entry
@@ -14966,7 +14920,8 @@ BoUpSLP::BlockScheduling::tryScheduleBundle(ArrayRef<Value *> VL, BoUpSLP *SLP,
1496614920
// initial bundle to the region.
1496714921
if (ScheduleEnd != OldScheduleEnd) {
1496814922
for (auto *I = ScheduleStart; I != ScheduleEnd; I = I->getNextNode())
14969-
doForAllOpcodes(I, [](ScheduleData *SD) { SD->clearDependencies(); });
14923+
if (ScheduleData *SD = getScheduleData(I))
14924+
SD->clearDependencies();
1497014925
ReSchedule = true;
1497114926
}
1497214927
if (Bundle) {
@@ -15085,37 +15040,21 @@ BoUpSLP::ScheduleData *BoUpSLP::BlockScheduling::allocateScheduleDataChunks() {
1508515040
return &(ScheduleDataChunks.back()[ChunkPos++]);
1508615041
}
1508715042

15088-
bool BoUpSLP::BlockScheduling::extendSchedulingRegion(Value *V,
15089-
const InstructionsState &S) {
15090-
if (getScheduleData(V, isOneOf(S, V)))
15091-
return true;
15043+
bool BoUpSLP::BlockScheduling::extendSchedulingRegion(
15044+
Value *V, const InstructionsState &S) {
1509215045
Instruction *I = dyn_cast<Instruction>(V);
1509315046
assert(I && "bundle member must be an instruction");
1509415047
assert(!isa<PHINode>(I) && !isVectorLikeInstWithConstOps(I) &&
1509515048
!doesNotNeedToBeScheduled(I) &&
1509615049
"phi nodes/insertelements/extractelements/extractvalues don't need to "
1509715050
"be scheduled");
15098-
auto &&CheckScheduleForI = [this, &S](Instruction *I) -> bool {
15099-
ScheduleData *ISD = getScheduleData(I);
15100-
if (!ISD)
15101-
return false;
15102-
assert(isInSchedulingRegion(ISD) &&
15103-
"ScheduleData not in scheduling region");
15104-
ScheduleData *SD = allocateScheduleDataChunks();
15105-
SD->Inst = I;
15106-
SD->init(SchedulingRegionID, S.OpValue);
15107-
ExtraScheduleDataMap[I][S.OpValue] = SD;
15108-
return true;
15109-
};
15110-
if (CheckScheduleForI(I))
15051+
if (getScheduleData(I))
1511115052
return true;
1511215053
if (!ScheduleStart) {
1511315054
// It's the first instruction in the new region.
1511415055
initScheduleData(I, I->getNextNode(), nullptr, nullptr);
1511515056
ScheduleStart = I;
1511615057
ScheduleEnd = I->getNextNode();
15117-
if (isOneOf(S, I) != I)
15118-
CheckScheduleForI(I);
1511915058
assert(ScheduleEnd && "tried to vectorize a terminator?");
1512015059
LLVM_DEBUG(dbgs() << "SLP: initialize schedule region to " << *I << "\n");
1512115060
return true;
@@ -15154,8 +15093,6 @@ bool BoUpSLP::BlockScheduling::extendSchedulingRegion(Value *V,
1515415093
"Instruction is in wrong basic block.");
1515515094
initScheduleData(I, ScheduleStart, nullptr, FirstLoadStoreInRegion);
1515615095
ScheduleStart = I;
15157-
if (isOneOf(S, I) != I)
15158-
CheckScheduleForI(I);
1515915096
LLVM_DEBUG(dbgs() << "SLP: extend schedule region start to " << *I
1516015097
<< "\n");
1516115098
return true;
@@ -15168,8 +15105,6 @@ bool BoUpSLP::BlockScheduling::extendSchedulingRegion(Value *V,
1516815105
initScheduleData(ScheduleEnd, I->getNextNode(), LastLoadStoreInRegion,
1516915106
nullptr);
1517015107
ScheduleEnd = I->getNextNode();
15171-
if (isOneOf(S, I) != I)
15172-
CheckScheduleForI(I);
1517315108
assert(ScheduleEnd && "tried to vectorize a terminator?");
1517415109
LLVM_DEBUG(dbgs() << "SLP: extend schedule region end to " << *I << "\n");
1517515110
return true;
@@ -15188,7 +15123,6 @@ void BoUpSLP::BlockScheduling::initScheduleData(Instruction *FromI,
1518815123
if (!SD) {
1518915124
SD = allocateScheduleDataChunks();
1519015125
ScheduleDataMap[I] = SD;
15191-
SD->Inst = I;
1519215126
}
1519315127
assert(!isInSchedulingRegion(SD) &&
1519415128
"new ScheduleData already in scheduling region");
@@ -15242,26 +15176,15 @@ void BoUpSLP::BlockScheduling::calculateDependencies(ScheduleData *SD,
1524215176
BundleMember->resetUnscheduledDeps();
1524315177

1524415178
// Handle def-use chain dependencies.
15245-
if (BundleMember->OpValue != BundleMember->Inst) {
15246-
if (ScheduleData *UseSD = getScheduleData(BundleMember->Inst)) {
15179+
for (User *U : BundleMember->Inst->users()) {
15180+
if (ScheduleData *UseSD = getScheduleData(cast<Instruction>(U))) {
1524715181
BundleMember->Dependencies++;
1524815182
ScheduleData *DestBundle = UseSD->FirstInBundle;
1524915183
if (!DestBundle->IsScheduled)
1525015184
BundleMember->incrementUnscheduledDeps(1);
1525115185
if (!DestBundle->hasValidDependencies())
1525215186
WorkList.push_back(DestBundle);
1525315187
}
15254-
} else {
15255-
for (User *U : BundleMember->Inst->users()) {
15256-
if (ScheduleData *UseSD = getScheduleData(cast<Instruction>(U))) {
15257-
BundleMember->Dependencies++;
15258-
ScheduleData *DestBundle = UseSD->FirstInBundle;
15259-
if (!DestBundle->IsScheduled)
15260-
BundleMember->incrementUnscheduledDeps(1);
15261-
if (!DestBundle->hasValidDependencies())
15262-
WorkList.push_back(DestBundle);
15263-
}
15264-
}
1526515188
}
1526615189

1526715190
auto MakeControlDependent = [&](Instruction *I) {
@@ -15409,12 +15332,12 @@ void BoUpSLP::BlockScheduling::resetSchedule() {
1540915332
assert(ScheduleStart &&
1541015333
"tried to reset schedule on block which has not been scheduled");
1541115334
for (Instruction *I = ScheduleStart; I != ScheduleEnd; I = I->getNextNode()) {
15412-
doForAllOpcodes(I, [&](ScheduleData *SD) {
15335+
if (ScheduleData *SD = getScheduleData(I)) {
1541315336
assert(isInSchedulingRegion(SD) &&
1541415337
"ScheduleData not in scheduling region");
1541515338
SD->IsScheduled = false;
1541615339
SD->resetUnscheduledDeps();
15417-
});
15340+
}
1541815341
}
1541915342
ReadyInsts.clear();
1542015343
}
@@ -15449,7 +15372,7 @@ void BoUpSLP::scheduleBlock(BlockScheduling *BS) {
1544915372
int Idx = 0;
1545015373
for (auto *I = BS->ScheduleStart; I != BS->ScheduleEnd;
1545115374
I = I->getNextNode()) {
15452-
BS->doForAllOpcodes(I, [this, &Idx, BS](ScheduleData *SD) {
15375+
if (ScheduleData *SD = BS->getScheduleData(I)) {
1545315376
TreeEntry *SDTE = getTreeEntry(SD->Inst);
1545415377
(void)SDTE;
1545515378
assert((isVectorLikeInstWithConstOps(SD->Inst) ||
@@ -15460,7 +15383,7 @@ void BoUpSLP::scheduleBlock(BlockScheduling *BS) {
1546015383

1546115384
if (SD->isSchedulingEntity() && SD->isPartOfBundle())
1546215385
BS->calculateDependencies(SD, false, this);
15463-
});
15386+
}
1546415387
}
1546515388
BS->initialFillReadyList(ReadyInsts);
1546615389

@@ -15492,11 +15415,9 @@ void BoUpSLP::scheduleBlock(BlockScheduling *BS) {
1549215415
#if !defined(NDEBUG) || defined(EXPENSIVE_CHECKS)
1549315416
// Check that all schedulable entities got scheduled
1549415417
for (auto *I = BS->ScheduleStart; I != BS->ScheduleEnd; I = I->getNextNode()) {
15495-
BS->doForAllOpcodes(I, [&](ScheduleData *SD) {
15496-
if (SD->isSchedulingEntity() && SD->hasValidDependencies()) {
15497-
assert(SD->IsScheduled && "must be scheduled at this point");
15498-
}
15499-
});
15418+
ScheduleData *SD = BS->getScheduleData(I);
15419+
if (SD && SD->isSchedulingEntity() && SD->hasValidDependencies())
15420+
assert(SD->IsScheduled && "must be scheduled at this point");
1550015421
}
1550115422
#endif
1550215423

0 commit comments

Comments
 (0)