16 DGNode *TopN = Nodes.front();
25 DGNode *BotN = Nodes.front();
34 for (
auto *
N : Nodes) {
35 auto *
I =
N->getInstruction();
36 if (
I->getIterator() == Where)
38 I->moveBefore(*Where.getNodeParent(), Where);
57 while (!ListCopy.empty()) {
58 OS << *ListCopy.top() <<
"\n";
70 OS <<
"Before begin of BB " << BB->getName();
72 OS <<
"At end of BB " << BB->getName();
83void Scheduler::scheduleAndUpdateReadyList(
SchedBundle &Bndl) {
85 assert(ScheduleTopItOpt &&
"Should have been set by now!");
87 ? ScheduleTopItOpt->getIterator()
88 : ScheduleTopItOpt->getNext().getIterator();
100 for (
auto *DepN :
N->preds(DAG)) {
101 DepN->decrUnscheduledSuccs();
102 if (DepN->readyBottomUp() && !DepN->scheduled())
108 for (
auto *DepN :
N->succs(DAG)) {
109 DepN->decrUnscheduledPreds();
110 if (DepN->readyTopDown() && !DepN->scheduled())
122 auto *
N = DAG.getNode(
I);
129 bool IsScheduled = ScheduleTopItOpt &&
130 ScheduleTopItOpt->getIterator() !=
I->getParent()->end() &&
132 (*ScheduleTopItOpt.value()).comesBefore(
I)) ||
134 I->comesBefore(&*ScheduleTopItOpt.value())));
142 for (
auto *PredN :
N->preds(DAG)) {
143 ReadyList.remove(PredN);
144 PredN->incrUnscheduledSuccs();
147 for (
auto *SuccN :
N->succs(DAG)) {
148 ReadyList.remove(SuccN);
149 SuccN->incrUnscheduledPreds();
158 for (
auto *
I : Instrs)
159 Nodes.push_back(DAG.getNode(
I));
160 auto BndlPtr = std::make_unique<SchedBundle>(std::move(Nodes));
161 auto *Bndl = BndlPtr.get();
162 Bndls[Bndl] = std::move(BndlPtr);
166void Scheduler::eraseBundle(
SchedBundle *SB) { Bndls.erase(SB); }
171 auto *InstrsSB = createBundle(Instrs);
176 bool KeepScheduling =
true;
177 while (KeepScheduling) {
178 enum class TryScheduleRes {
187 auto TryScheduleBndl = [
this, InstrsSB](
DGNode *ReadyN) -> TryScheduleRes {
188 auto *SB = ReadyN->getSchedBundle();
192 auto *SingletonSB = createBundle({ReadyN->getInstruction()});
193 scheduleAndUpdateReadyList(*SingletonSB);
194 return TryScheduleRes::Success;
196 if (SB->ready(Dir)) {
200 for (
auto *
N : *SB) {
205 scheduleAndUpdateReadyList(*SB);
208 return TryScheduleRes::Finished;
209 return TryScheduleRes::Success;
211 return TryScheduleRes::Failure;
213 while (!ReadyList.empty()) {
214 auto *ReadyN = ReadyList.pop();
215 auto Res = TryScheduleBndl(ReadyN);
217 case TryScheduleRes::Success:
220 case TryScheduleRes::Failure:
223 Retry.push_back(ReadyN);
225 case TryScheduleRes::Finished:
232 KeepScheduling =
false;
234 auto Res = TryScheduleBndl(
N);
235 if (Res == TryScheduleRes::Success) {
236 Retry.erase(
find(Retry,
N));
237 KeepScheduling =
true;
242 eraseBundle(InstrsSB);
246Scheduler::BndlSchedState
248 assert(!Instrs.empty() &&
"Expected non-empty bundle");
249 auto *N0 = DAG.getNode(Instrs[0]);
250 auto *SB0 = N0 !=
nullptr ? N0->getSchedBundle() :
nullptr;
251 bool AllUnscheduled = SB0 ==
nullptr;
252 bool FullyScheduled = SB0 !=
nullptr && !SB0->isSingleton();
254 auto *
N = DAG.getNode(
I);
255 auto *SB =
N !=
nullptr ?
N->getSchedBundle() :
nullptr;
258 AllUnscheduled =
false;
259 if (SB->isSingleton()) {
262 FullyScheduled =
false;
269 FullyScheduled =
false;
271 if ((SB !=
nullptr && !SB->isSingleton()) ||
272 (SB0 !=
nullptr && !SB0->isSingleton()))
273 return BndlSchedState::AlreadyScheduled;
276 return AllUnscheduled ? BndlSchedState::NoneScheduled
277 : FullyScheduled ? BndlSchedState::FullyScheduled
278 : BndlSchedState::TemporarilyScheduled;
298 ? &*ScheduleTopItOpt.value()
302 : &*ScheduleTopItOpt.value();
308 for (
auto &
I : ResetIntvl) {
309 auto *
N = DAG.getNode(&
I);
312 auto *SB =
N->getSchedBundle();
313 if (SB->isSingleton())
315 N->resetScheduleState();
320 auto *
N = DAG.getNode(&
I);
324 for (
auto *PredN :
N->preds(DAG))
325 PredN->incrUnscheduledSuccs();
330 for (
auto *SuccN :
N->succs(DAG))
331 SuccN->incrUnscheduledPreds();
344 auto *
N = DAG.getNode(&
I);
354 return I->getParent() == (*Instrs.
begin())->getParent();
356 "Instrs not in the same BB, should have been rejected by Legality!");
358 if (!DAG.getInterval().empty()) {
359 auto *BB = DAG.getInterval().top()->getParent();
360 if (
any_of(Instrs, [BB](
auto *
I) {
return I->getParent() != BB; }))
363 if (ScheduledBB ==
nullptr)
364 ScheduledBB = Instrs[0]->getParent();
367 [
this](
Instruction *
I) {
return I->getParent() != ScheduledBB; }))
382 auto SchedState = getBndlSchedState(Instrs);
383 switch (SchedState) {
384 case BndlSchedState::FullyScheduled:
387 case BndlSchedState::AlreadyScheduled:
392 case BndlSchedState::TemporarilyScheduled:
397 trimSchedule(Instrs);
398 ScheduleTopItOpt = GetSchedPoint(Dir, Instrs);
399 return tryScheduleUntil(Instrs);
400 case BndlSchedState::NoneScheduled: {
402 if (!ScheduleTopItOpt)
404 ScheduleTopItOpt = GetSchedPoint(Dir, Instrs);
411 for (
auto &
I : ScanForReady) {
412 auto *
N = DAG.getNode(&
I);
417 if (IsReady && !ReadyList.contains(
N))
421 return tryScheduleUntil(Instrs);
429 OS <<
"ReadyList:\n";
434 if (ScheduleTopItOpt)
435 OS << **ScheduleTopItOpt;
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
std::pair< uint64_t, uint64_t > Interval
Represent a constant reference to an array (0 or more elements consecutively in memory),...
InstListType::iterator iterator
Instruction iterators...
void reserve(size_type N)
This class implements an extremely fast bulk output stream that can only output to a stream.
A DependencyGraph Node that points to an Instruction and contains memory dependency edges.
Instruction * getInstruction() const
A sandboxir::User with operands, opcode and linked with previous/next instructions in an instruction ...
LLVM_ABI BBIterator getIterator() const
\Returns a BasicBlock::iterator for this Instruction.
bool comesBefore(const Instruction *Other) const
Given an instruction Other in the same basic block as this instruction, return true if this instructi...
Interval getUnionInterval(const Interval &Other)
\Returns a single interval that spans across both this and Other.
LLVM_DUMP_METHOD void dump() const
The nodes that need to be scheduled back-to-back in a single scheduling cycle form a SchedBundle.
LLVM_ABI DGNode * getBot() const
\Returns the bundle node that comes after the others in program order.
LLVM_ABI DGNode * getTop() const
\Returns the bundle node that comes before the others in program order.
SmallVector< DGNode *, 4 > ContainerTy
LLVM_DUMP_METHOD void dump() const
LLVM_ABI void cluster(BasicBlock::iterator Where)
Move all bundle instructions to Where back-to-back.
LLVM_DUMP_METHOD void dump() const
LLVM_ABI bool trySchedule(ArrayRef< Instruction * > Instrs)
Tries to build a schedule that includes all of Instrs scheduled at the same scheduling cycle.
The scheduling point in the context of the Scheduler points to the top-of-schedule (i....
SchedulingPoint getNext() const
Returns the SchedulingPoint pointing after this.
BasicBlock * atEndOrNull() const
If the SchedulingPoint points after the last instruction in the BB then this returns the correspondin...
Instruction * atInstrOrNull() const
Returns the instruction pointed to by this SchedulingPoint or null if we are before/after BB.
LLVM_DUMP_METHOD void dump() const
SchedulingPoint getPrev() const
Returns the SchedulingPoint pointing before this.
BasicBlock * atBeforeBeginOrNull() const
If the SchedulingPoint points to before the beginning of a BB, then this returns that BB,...
void print(raw_ostream &OS) const
static Instruction * getLowest(ArrayRef< Instruction * > Instrs)
\Returns the instruction in Instrs that is lowest in the BB.
static Instruction * getHighest(ArrayRef< Instruction * > Instrs)
\Returns the instruction in Instrs that is highest in the BB.
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
StringLiteral schedDirectionToStr(SchedDirection Dir)
BasicBlock(llvm::BasicBlock *BB, Context &SBCtx)
template class LLVM_TEMPLATE_ABI Interval< Instruction >
friend class Instruction
Iterator for Instructions in a `BasicBlock.
auto drop_begin(T &&RangeOrContainer, size_t N=1)
Return a range covering RangeOrContainer with the first N elements excluded.
auto find(R &&Range, const T &Val)
Provide wrappers to std::find which take ranges instead of having to pass begin/end explicitly.
bool all_of(R &&range, UnaryPredicate P)
Provide wrappers to std::all_of which take ranges instead of having to pass begin/end explicitly.
iterator_range< early_inc_iterator_impl< detail::IterOfRange< RangeT > > > make_early_inc_range(RangeT &&Range)
Make a range that does early increment to allow mutation of the underlying range without disrupting i...
bool any_of(R &&range, UnaryPredicate P)
Provide wrappers to std::any_of which take ranges instead of having to pass begin/end explicitly.
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
class LLVM_GSL_OWNER SmallVector
Forward declaration of SmallVector so that calculateSmallVectorDefaultInlinedElements can reference s...
@ Success
The lock was released successfully.
ArrayRef(const T &OneElt) -> ArrayRef< T >