28 DGNode *TopN = Nodes.front();
37 DGNode *BotN = Nodes.front();
46 for (
auto *
N : Nodes) {
47 auto *
I =
N->getInstruction();
48 if (
I->getIterator() == Where)
50 I->moveBefore(*Where.getNodeParent(), Where);
69 while (!ListCopy.empty()) {
70 OS << *ListCopy.top() <<
"\n";
82 OS <<
"Before begin of BB " << BB->getName();
84 OS <<
"At end of BB " << BB->getName();
95void Scheduler::scheduleAndUpdateReadyList(
SchedBundle &Bndl) {
97 assert(ScheduleTopItOpt &&
"Should have been set by now!");
99 ? ScheduleTopItOpt->getIterator()
100 : ScheduleTopItOpt->getNext().getIterator();
112 for (
auto *DepN :
N->preds(DAG)) {
113 DepN->decrUnscheduledSuccs();
114 if (DepN->readyBottomUp() && !DepN->scheduled())
120 for (
auto *DepN :
N->succs(DAG)) {
121 DepN->decrUnscheduledPreds();
122 if (DepN->readyTopDown() && !DepN->scheduled())
134 auto *
N = DAG.getNode(
I);
141 bool IsScheduled = ScheduleTopItOpt &&
142 ScheduleTopItOpt->getIterator() !=
I->getParent()->end() &&
144 (*ScheduleTopItOpt.value()).comesBefore(
I)) ||
146 I->comesBefore(&*ScheduleTopItOpt.value())));
154 for (
auto *PredN :
N->preds(DAG)) {
155 ReadyList.remove(PredN);
156 PredN->incrUnscheduledSuccs();
159 for (
auto *SuccN :
N->succs(DAG)) {
160 ReadyList.remove(SuccN);
161 SuccN->incrUnscheduledPreds();
170 for (
auto *
I : Instrs)
171 Nodes.push_back(DAG.getNode(
I));
172 auto BndlPtr = std::make_unique<SchedBundle>(std::move(Nodes));
173 auto *Bndl = BndlPtr.get();
174 Bndls[Bndl] = std::move(BndlPtr);
178void Scheduler::eraseBundle(
SchedBundle *SB) { Bndls.erase(SB); }
183 auto *InstrsSB = createBundle(Instrs);
188 bool KeepScheduling =
true;
189 while (KeepScheduling) {
190 enum class TryScheduleRes {
199 auto TryScheduleBndl = [
this, InstrsSB](
DGNode *ReadyN) -> TryScheduleRes {
200 auto *SB = ReadyN->getSchedBundle();
204 auto *SingletonSB = createBundle({ReadyN->getInstruction()});
205 scheduleAndUpdateReadyList(*SingletonSB);
206 return TryScheduleRes::Success;
208 if (SB->ready(Dir)) {
212 for (
auto *
N : *SB) {
217 scheduleAndUpdateReadyList(*SB);
220 return TryScheduleRes::Finished;
221 return TryScheduleRes::Success;
223 return TryScheduleRes::Failure;
225 while (!ReadyList.empty()) {
226 auto *ReadyN = ReadyList.pop();
227 auto Res = TryScheduleBndl(ReadyN);
229 case TryScheduleRes::Success:
232 case TryScheduleRes::Failure:
235 Retry.push_back(ReadyN);
237 case TryScheduleRes::Finished:
244 KeepScheduling =
false;
246 auto Res = TryScheduleBndl(
N);
247 if (Res == TryScheduleRes::Success) {
248 Retry.erase(
find(Retry,
N));
249 KeepScheduling =
true;
254 eraseBundle(InstrsSB);
258Scheduler::BndlSchedState
260 assert(!Instrs.empty() &&
"Expected non-empty bundle");
261 auto *N0 = DAG.getNode(Instrs[0]);
262 auto *SB0 = N0 !=
nullptr ? N0->getSchedBundle() :
nullptr;
263 bool AllUnscheduled = SB0 ==
nullptr;
264 bool FullyScheduled = SB0 !=
nullptr && !SB0->isSingleton();
266 auto *
N = DAG.getNode(
I);
267 auto *SB =
N !=
nullptr ?
N->getSchedBundle() :
nullptr;
270 AllUnscheduled =
false;
271 if (SB->isSingleton()) {
274 FullyScheduled =
false;
281 FullyScheduled =
false;
283 if ((SB !=
nullptr && !SB->isSingleton()) ||
284 (SB0 !=
nullptr && !SB0->isSingleton()))
285 return BndlSchedState::AlreadyScheduled;
288 return AllUnscheduled ? BndlSchedState::NoneScheduled
289 : FullyScheduled ? BndlSchedState::FullyScheduled
290 : BndlSchedState::TemporarilyScheduled;
310 ? &*ScheduleTopItOpt.value()
314 : &*ScheduleTopItOpt.value();
320 for (
auto &
I : ResetIntvl) {
321 auto *
N = DAG.getNode(&
I);
324 auto *SB =
N->getSchedBundle();
325 if (SB->isSingleton())
327 N->resetScheduleState();
332 auto *
N = DAG.getNode(&
I);
336 for (
auto *PredN :
N->preds(DAG))
337 PredN->incrUnscheduledSuccs();
342 for (
auto *SuccN :
N->succs(DAG))
343 SuccN->incrUnscheduledPreds();
356 auto *
N = DAG.getNode(&
I);
366 return I->getParent() == (*Instrs.
begin())->getParent();
368 "Instrs not in the same BB, should have been rejected by Legality!");
370 if (!DAG.getInterval().empty()) {
371 auto *BB = DAG.getInterval().top()->getParent();
372 if (
any_of(Instrs, [BB](
auto *
I) {
return I->getParent() != BB; }))
375 if (ScheduledBB ==
nullptr)
376 ScheduledBB = Instrs[0]->getParent();
379 [
this](
Instruction *
I) {
return I->getParent() != ScheduledBB; }))
394 auto SchedState = getBndlSchedState(Instrs);
395 switch (SchedState) {
396 case BndlSchedState::FullyScheduled:
399 case BndlSchedState::AlreadyScheduled:
404 case BndlSchedState::TemporarilyScheduled:
409 trimSchedule(Instrs);
410 ScheduleTopItOpt = GetSchedPoint(Dir, Instrs);
411 return tryScheduleUntil(Instrs);
412 case BndlSchedState::NoneScheduled: {
414 if (!ScheduleTopItOpt)
416 ScheduleTopItOpt = GetSchedPoint(Dir, Instrs);
420 for (
auto &
I : Extension) {
421 auto *
N = DAG.getNode(&
I);
427 return tryScheduleUntil(Instrs);
435 OS <<
"ReadyList:\n";
440 if (ScheduleTopItOpt)
441 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)
A wrapper around a string literal that serves as a proxy for constructing global tables of StringRefs...
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
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 >