25 #define DEBUG_TYPE "machine-scheduler" 29 class GCNMinRegScheduler {
34 Candidate(
const SUnit *SU_,
int Priority_ = 0)
35 : SU(SU_), Priority(Priority_) {}
42 std::vector<unsigned> NumPreds;
44 bool isScheduled(
const SUnit *SU)
const {
49 void setIsScheduled(
const SUnit *SU) {
54 unsigned getNumPreds(
const SUnit *SU)
const {
60 unsigned decNumPreds(
const SUnit *SU) {
68 int getReadySuccessors(
const SUnit *SU)
const;
69 int getNotReadySuccessors(
const SUnit *SU)
const;
71 template <
typename Calc>
72 unsigned findMax(
unsigned Num, Calc
C);
74 Candidate* pickCandidate();
76 void bumpPredsPriority(
const SUnit *SchedSU,
int Priority);
77 void releaseSuccessors(
const SUnit* SU,
int Priority);
87 NumPreds.resize(SUnits.size());
88 for (
unsigned I = 0;
I < SUnits.size(); ++
I)
89 NumPreds[
I] = SUnits[
I].NumPredsLeft;
92 int GCNMinRegScheduler::getReadySuccessors(
const SUnit *SU)
const {
93 unsigned NumSchedSuccs = 0;
95 bool wouldBeScheduled =
true;
97 auto PSU = PDep.getSUnit();
98 assert(!PSU->isBoundaryNode());
99 if (PSU != SU && !isScheduled(PSU)) {
100 wouldBeScheduled =
false;
104 NumSchedSuccs += wouldBeScheduled ? 1 : 0;
106 return NumSchedSuccs;
109 int GCNMinRegScheduler::getNotReadySuccessors(
const SUnit *SU)
const {
110 return SU->
Succs.size() - getReadySuccessors(SU);
113 template <
typename Calc>
114 unsigned GCNMinRegScheduler::findMax(
unsigned Num, Calc
C) {
115 assert(!RQ.empty() && Num <= RQ.size());
117 using T = decltype(
C(*RQ.begin())) ;
119 T Max = std::numeric_limits<T>::min();
121 for (
auto I = RQ.begin(); Num; --Num) {
139 GCNMinRegScheduler::Candidate* GCNMinRegScheduler::pickCandidate() {
141 unsigned Num = RQ.size();
144 LLVM_DEBUG(
dbgs() <<
"\nSelecting max priority candidates among " << Num
146 Num = findMax(Num, [=](
const Candidate &
C) {
return C.Priority; });
149 LLVM_DEBUG(
dbgs() <<
"\nSelecting min non-ready producing candidate among " 151 Num = findMax(Num, [=](
const Candidate &C) {
153 int Res = getNotReadySuccessors(SU);
155 << Res <<
" successors, metric = " << -Res <<
'\n');
160 LLVM_DEBUG(
dbgs() <<
"\nSelecting most producing candidate among " << Num
162 Num = findMax(Num, [=](
const Candidate &C) {
164 auto Res = getReadySuccessors(SU);
166 <<
" successors, metric = " << Res <<
'\n');
171 Num = Num ? Num : RQ.size();
174 <<
"\nCan't find best candidate, selecting in program order among " 176 Num = findMax(Num, [=](
const Candidate &C) {
return -(int64_t)C.SU->NodeNum; });
183 void GCNMinRegScheduler::bumpPredsPriority(
const SUnit *SchedSU,
int Priority) {
185 for (
const auto &S : SchedSU->
Succs) {
186 if (S.getSUnit()->isBoundaryNode() || isScheduled(S.getSUnit()) ||
189 for (
const auto &
P : S.getSUnit()->Preds) {
190 auto PSU =
P.getSUnit();
191 assert(!PSU->isBoundaryNode());
192 if (PSU != SchedSU && !isScheduled(PSU)) {
198 while (!Worklist.empty()) {
199 auto SU = Worklist.pop_back_val();
201 for (
const auto &
P : SU->
Preds) {
202 if (!
P.getSUnit()->isBoundaryNode() && !isScheduled(
P.getSUnit()) &&
204 Worklist.push_back(
P.getSUnit());
208 <<
")'s non-ready successors of " << Priority
209 <<
" priority in ready queue: ");
210 const auto SetEnd = Set.
end();
212 if (Set.
find(
C.SU) != SetEnd) {
213 C.Priority = Priority;
220 void GCNMinRegScheduler::releaseSuccessors(
const SUnit* SU,
int Priority) {
221 for (
const auto &S : SU->
Succs) {
222 auto SuccSU = S.getSUnit();
225 assert(SuccSU->isBoundaryNode() || getNumPreds(SuccSU) > 0);
226 if (!SuccSU->isBoundaryNode() && decNumPreds(SuccSU) == 0)
227 RQ.push_front(*new (Alloc.
Allocate()) Candidate(SuccSU, Priority));
231 std::vector<const SUnit*>
234 const auto &SUnits = DAG.
SUnits;
235 std::vector<const SUnit*> Schedule;
236 Schedule.reserve(SUnits.size());
238 initNumPreds(SUnits);
242 for (
auto SU : TopRoots) {
243 RQ.push_back(*
new (Alloc.
Allocate()) Candidate(SU, StepNo));
245 releaseSuccessors(&DAG.
EntrySU, StepNo);
247 while (!RQ.empty()) {
253 <<
' ' <<
C.SU->NodeNum <<
"(P" <<
C.Priority <<
')';
256 auto C = pickCandidate();
262 releaseSuccessors(SU, StepNo);
263 Schedule.push_back(SU);
266 if (getReadySuccessors(SU) == 0)
267 bumpPredsPriority(SU, StepNo);
271 assert(SUnits.size() == Schedule.size());
280 GCNMinRegScheduler S;
281 return S.schedule(TopRoots, DAG);
This class represents lattice values for constants.
virtual void dumpNode(const SUnit &SU) const =0
SmallVector< SDep, 4 > Preds
All sunit predecessors.
This file defines the MallocAllocator and BumpPtrAllocator interfaces.
iterator find(ConstPtrType Ptr) const
Regular data dependence (aka true-dependence).
A simple intrusive list implementation.
std::vector< const SUnit * > makeMinRegSchedule(ArrayRef< const SUnit *> TopRoots, const ScheduleDAG &DAG)
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory)...
bool isBoundaryNode() const
Boundary nodes are placeholders for the boundary of the scheduling region.
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements...
Align max(MaybeAlign Lhs, Align Rhs)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small...
A BumpPtrAllocator that allows only elements of a specific type to be allocated.
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
LLVM_ATTRIBUTE_RETURNS_NONNULL LLVM_ATTRIBUTE_RETURNS_NOALIAS void * Allocate(size_t Size, Align Alignment)
Allocate space at the specified alignment.
SUnit EntrySU
Special node for the region entry.
unsigned NodeNum
Entry # of node in the node vector.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
SmallVector< SDep, 4 > Succs
All sunit successors.
std::vector< SUnit > SUnits
The scheduling units.
Scheduling unit. This is a node in the scheduling DAG.