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(ScheduleFrontierOpt &&
"Should have been set by now!");
87 ? ScheduleFrontierOpt->getIterator()
88 : ScheduleFrontierOpt->getNext().getIterator();
100 for (
auto *DepN :
N->preds(DAG)) {
101 DepN->decrUnscheduledDeps();
102 if (DepN->ready() && !DepN->scheduled())
108 for (
auto *DepN :
N->succs(DAG)) {
109 DepN->decrUnscheduledDeps();
110 if (DepN->ready() && !DepN->scheduled())
122 auto *
N = DAG.getNode(
I);
130 ScheduleFrontierOpt &&
131 ScheduleFrontierOpt->getIterator() !=
I->getParent()->end() &&
133 (*ScheduleFrontierOpt.value()).comesBefore(
I)) ||
135 I->comesBefore(&*ScheduleFrontierOpt.value())));
143 for (
auto *PredN :
N->preds(DAG)) {
144 ReadyList.remove(PredN);
145 PredN->incrUnscheduledDeps();
148 for (
auto *SuccN :
N->succs(DAG)) {
149 ReadyList.remove(SuccN);
150 SuccN->incrUnscheduledDeps();
160 auto *
N = DAG.getNode(
I);
166 auto UpdateNodeAndTryAddToReadyList = [
this,
N](
DGNode *DepN) {
167 if (DepN->scheduled())
169 if (!
N->scheduled() && !DepN->ready())
170 DepN->decrUnscheduledDeps();
171 if (DepN->ready() && !ReadyList.contains(DepN))
172 ReadyList.insert(DepN);
175 for (
auto *DepN :
N->preds(DAG))
176 UpdateNodeAndTryAddToReadyList(DepN);
178 for (
auto *DepN :
N->succs(DAG))
179 UpdateNodeAndTryAddToReadyList(DepN);
183void Scheduler::notifyMoveInstr(
Instruction *
I,
const BBIterator &To) {
194void Scheduler::notifySetUse(
const Use &U,
Value *NewSrc) {
199 DGNode *DstN = DAG.getNode(DstI);
210 if (OldSrcN && OldSrcN->ready() && !OldSrcN->scheduled() &&
211 !ReadyList.contains(OldSrcN))
212 ReadyList.insert(OldSrcN);
214 if (NewSrcN && (!DstN || !DstN->scheduled()) && !NewSrcN->ready())
215 ReadyList.remove(NewSrcN);
220 if (DstN && DstN->ready() && !NewSrcN->scheduled() &&
221 !ReadyList.contains(NewSrcN))
222 ReadyList.insert(NewSrcN);
224 if (DstN && !DstN->ready())
225 ReadyList.remove(NewSrcN);
234 for (
auto *
I : Instrs)
235 Nodes.push_back(DAG.getNode(
I));
236 auto BndlPtr = std::make_unique<SchedBundle>(std::move(Nodes));
237 auto *Bndl = BndlPtr.get();
238 Bndls[Bndl] = std::move(BndlPtr);
242void Scheduler::eraseBundle(
SchedBundle *SB) { Bndls.erase(SB); }
247 auto *InstrsSB = createBundle(Instrs);
252 bool KeepScheduling =
true;
253 while (KeepScheduling) {
254 enum class TryScheduleRes {
263 auto TryScheduleBndl = [
this, InstrsSB](
DGNode *ReadyN) -> TryScheduleRes {
264 auto *SB = ReadyN->getSchedBundle();
268 auto *SingletonSB = createBundle({ReadyN->getInstruction()});
269 scheduleAndUpdateReadyList(*SingletonSB);
270 return TryScheduleRes::Success;
272 if (SB->ready(Dir)) {
276 for (
auto *
N : *SB) {
281 scheduleAndUpdateReadyList(*SB);
284 return TryScheduleRes::Finished;
285 return TryScheduleRes::Success;
287 return TryScheduleRes::Failure;
289 while (!ReadyList.empty()) {
290 auto *ReadyN = ReadyList.pop();
291 auto Res = TryScheduleBndl(ReadyN);
293 case TryScheduleRes::Success:
296 case TryScheduleRes::Failure:
299 Retry.push_back(ReadyN);
301 case TryScheduleRes::Finished:
308 KeepScheduling =
false;
310 auto Res = TryScheduleBndl(
N);
311 if (Res == TryScheduleRes::Success) {
312 Retry.erase(
find(Retry,
N));
313 KeepScheduling =
true;
321 for (
auto *RetryN : Retry)
322 ReadyList.insert(RetryN);
324 eraseBundle(InstrsSB);
328Scheduler::BndlSchedState
330 assert(!Instrs.empty() &&
"Expected non-empty bundle");
331 auto *N0 = DAG.getNode(Instrs[0]);
332 auto *SB0 = N0 !=
nullptr ? N0->getSchedBundle() :
nullptr;
333 bool AllUnscheduled = SB0 ==
nullptr;
334 bool FullyScheduled = SB0 !=
nullptr && !SB0->isSingleton();
336 auto *
N = DAG.getNode(
I);
337 auto *SB =
N !=
nullptr ?
N->getSchedBundle() :
nullptr;
340 AllUnscheduled =
false;
341 if (SB->isSingleton()) {
344 FullyScheduled =
false;
351 FullyScheduled =
false;
353 if ((SB !=
nullptr && !SB->isSingleton()) ||
354 (SB0 !=
nullptr && !SB0->isSingleton()))
355 return BndlSchedState::AlreadyScheduled;
358 return AllUnscheduled ? BndlSchedState::NoneScheduled
359 : FullyScheduled ? BndlSchedState::FullyScheduled
360 : BndlSchedState::TemporarilyScheduled;
380 ? &*ScheduleFrontierOpt.value()
384 : &*ScheduleFrontierOpt.value();
390 for (
auto &
I : ResetIntvl) {
391 auto *
N = DAG.getNode(&
I);
394 auto *SB =
N->getSchedBundle();
395 if (SB->isSingleton())
397 N->resetScheduleState();
402 auto *
N = DAG.getNode(&
I);
406 for (
auto *PredN :
N->preds(DAG))
407 PredN->incrUnscheduledDeps();
412 for (
auto *SuccN :
N->succs(DAG))
413 SuccN->incrUnscheduledDeps();
426 auto *
N = DAG.getNode(&
I);
439 return ScheduleFrontierOpt->comesBefore(*
I);
441 "Wrong scheduling direction!");
446 return ScheduleFrontierOpt->comesBefore(*
I);
448 "Wrong scheduling direction!");
457 return I->getParent() == (*Instrs.
begin())->getParent();
459 "Instrs not in the same BB, should have been rejected by Legality!");
461 if (!DAG.getInterval().empty()) {
462 auto *BB = DAG.getInterval().top()->getParent();
463 if (
any_of(Instrs, [BB](
auto *
I) {
return I->getParent() != BB; }))
466 if (ScheduledBB ==
nullptr)
467 ScheduledBB = Instrs[0]->getParent();
470 [
this](
Instruction *
I) {
return I->getParent() != ScheduledBB; }))
485 auto SchedState = getBndlSchedState(Instrs);
486 switch (SchedState) {
487 case BndlSchedState::FullyScheduled:
490 case BndlSchedState::AlreadyScheduled:
495 case BndlSchedState::TemporarilyScheduled:
500 trimSchedule(Instrs);
501 ScheduleFrontierOpt = GetSchedPoint(Dir, Instrs);
502 return tryScheduleUntil(Instrs);
503 case BndlSchedState::NoneScheduled: {
505 if (!ScheduleFrontierOpt) {
507 ScheduleFrontierOpt = GetSchedPoint(Dir, Instrs);
510 assertSameDirection(Instrs);
516 for (
auto &
I : Extension) {
517 auto *
N = DAG.getNode(&
I);
520 if (
N->ready() && !ReadyList.contains(
N))
524 return tryScheduleUntil(Instrs);
532 OS <<
"ReadyList:\n";
537 if (ScheduleFrontierOpt)
538 OS << **ScheduleFrontierOpt;
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...
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
Represents a Def-use/Use-def edge in SandboxIR.
A SandboxIR Value has users. This is the base class.
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...
RelativeUniformCounterPtr ValuesPtrExpr VTableAddr Value
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.
bool none_of(R &&Range, UnaryPredicate P)
Provide wrappers to std::none_of which take ranges instead of having to pass begin/end explicitly.
class LLVM_GSL_OWNER SmallVector
Forward declaration of SmallVector so that calculateSmallVectorDefaultInlinedElements can reference s...
bool isa(const From &Val)
isa<X> - Return true if the parameter to the template is an instance of one of the template type argu...
@ Success
The lock was released successfully.
ArrayRef(const T &OneElt) -> ArrayRef< T >
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.