Skip to content

Commit 07f0fae

Browse files
committedJul 1, 2021
[NFC][Scheduler] Refactor tryCandidate to return boolean
This patch changes return type of tryCandidate from void to bool: 1. Methods in some targets already follow this convention. 2. This would help if some target wants to re-use generic code. 3. It looks more intuitive if these try-method returns the same type. We may need to change return type of them from bool to some enum further, to make it less confusing. Reviewed By: foad Differential Revision: https://reviews.llvm.org/D103951
1 parent b9c2425 commit 07f0fae

File tree

4 files changed

+69
-56
lines changed

4 files changed

+69
-56
lines changed
 

‎llvm/include/llvm/CodeGen/MachineScheduler.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -1012,7 +1012,7 @@ class GenericScheduler : public GenericSchedulerBase {
10121012
const RegPressureTracker &RPTracker,
10131013
RegPressureTracker &TempTracker);
10141014

1015-
virtual void tryCandidate(SchedCandidate &Cand, SchedCandidate &TryCand,
1015+
virtual bool tryCandidate(SchedCandidate &Cand, SchedCandidate &TryCand,
10161016
SchedBoundary *Zone) const;
10171017

10181018
SUnit *pickNodeBidirectional(bool &IsTopNode);
@@ -1075,7 +1075,7 @@ class PostGenericScheduler : public GenericSchedulerBase {
10751075
}
10761076

10771077
protected:
1078-
virtual void tryCandidate(SchedCandidate &Cand, SchedCandidate &TryCand);
1078+
virtual bool tryCandidate(SchedCandidate &Cand, SchedCandidate &TryCand);
10791079

10801080
void pickNodeFromQueue(SchedCandidate &Cand);
10811081
};

‎llvm/lib/CodeGen/MachineScheduler.cpp

+36-28
Original file line numberDiff line numberDiff line change
@@ -2818,6 +2818,8 @@ void GenericSchedulerBase::traceCandidate(const SchedCandidate &Cand) {
28182818

28192819
namespace llvm {
28202820
/// Return true if this heuristic determines order.
2821+
/// TODO: Consider refactor return type of these functions as integer or enum,
2822+
/// as we may need to differentiate whether TryCand is better than Cand.
28212823
bool tryLess(int TryVal, int CandVal,
28222824
GenericSchedulerBase::SchedCandidate &TryCand,
28232825
GenericSchedulerBase::SchedCandidate &Cand,
@@ -3176,34 +3178,35 @@ void GenericScheduler::initCandidate(SchedCandidate &Cand, SUnit *SU,
31763178
/// \param Cand provides the policy and current best candidate.
31773179
/// \param TryCand refers to the next SUnit candidate, otherwise uninitialized.
31783180
/// \param Zone describes the scheduled zone that we are extending, or nullptr
3179-
// if Cand is from a different zone than TryCand.
3180-
void GenericScheduler::tryCandidate(SchedCandidate &Cand,
3181+
/// if Cand is from a different zone than TryCand.
3182+
/// \return \c true if TryCand is better than Cand (Reason is NOT NoCand)
3183+
bool GenericScheduler::tryCandidate(SchedCandidate &Cand,
31813184
SchedCandidate &TryCand,
31823185
SchedBoundary *Zone) const {
31833186
// Initialize the candidate if needed.
31843187
if (!Cand.isValid()) {
31853188
TryCand.Reason = NodeOrder;
3186-
return;
3189+
return true;
31873190
}
31883191

31893192
// Bias PhysReg Defs and copies to their uses and defined respectively.
31903193
if (tryGreater(biasPhysReg(TryCand.SU, TryCand.AtTop),
31913194
biasPhysReg(Cand.SU, Cand.AtTop), TryCand, Cand, PhysReg))
3192-
return;
3195+
return TryCand.Reason != NoCand;
31933196

31943197
// Avoid exceeding the target's limit.
31953198
if (DAG->isTrackingPressure() && tryPressure(TryCand.RPDelta.Excess,
31963199
Cand.RPDelta.Excess,
31973200
TryCand, Cand, RegExcess, TRI,
31983201
DAG->MF))
3199-
return;
3202+
return TryCand.Reason != NoCand;
32003203

32013204
// Avoid increasing the max critical pressure in the scheduled region.
32023205
if (DAG->isTrackingPressure() && tryPressure(TryCand.RPDelta.CriticalMax,
32033206
Cand.RPDelta.CriticalMax,
32043207
TryCand, Cand, RegCritical, TRI,
32053208
DAG->MF))
3206-
return;
3209+
return TryCand.Reason != NoCand;
32073210

32083211
// We only compare a subset of features when comparing nodes between
32093212
// Top and Bottom boundary. Some properties are simply incomparable, in many
@@ -3217,12 +3220,12 @@ void GenericScheduler::tryCandidate(SchedCandidate &Cand,
32173220
// heuristics to take precedence.
32183221
if (Rem.IsAcyclicLatencyLimited && !Zone->getCurrMOps() &&
32193222
tryLatency(TryCand, Cand, *Zone))
3220-
return;
3223+
return TryCand.Reason != NoCand;
32213224

32223225
// Prioritize instructions that read unbuffered resources by stall cycles.
32233226
if (tryLess(Zone->getLatencyStallCycles(TryCand.SU),
32243227
Zone->getLatencyStallCycles(Cand.SU), TryCand, Cand, Stall))
3225-
return;
3228+
return TryCand.Reason != NoCand;
32263229
}
32273230

32283231
// Keep clustered nodes together to encourage downstream peephole
@@ -3238,46 +3241,49 @@ void GenericScheduler::tryCandidate(SchedCandidate &Cand,
32383241
if (tryGreater(TryCand.SU == TryCandNextClusterSU,
32393242
Cand.SU == CandNextClusterSU,
32403243
TryCand, Cand, Cluster))
3241-
return;
3244+
return TryCand.Reason != NoCand;
32423245

32433246
if (SameBoundary) {
32443247
// Weak edges are for clustering and other constraints.
32453248
if (tryLess(getWeakLeft(TryCand.SU, TryCand.AtTop),
32463249
getWeakLeft(Cand.SU, Cand.AtTop),
32473250
TryCand, Cand, Weak))
3248-
return;
3251+
return TryCand.Reason != NoCand;
32493252
}
32503253

32513254
// Avoid increasing the max pressure of the entire region.
32523255
if (DAG->isTrackingPressure() && tryPressure(TryCand.RPDelta.CurrentMax,
32533256
Cand.RPDelta.CurrentMax,
32543257
TryCand, Cand, RegMax, TRI,
32553258
DAG->MF))
3256-
return;
3259+
return TryCand.Reason != NoCand;
32573260

32583261
if (SameBoundary) {
32593262
// Avoid critical resource consumption and balance the schedule.
32603263
TryCand.initResourceDelta(DAG, SchedModel);
32613264
if (tryLess(TryCand.ResDelta.CritResources, Cand.ResDelta.CritResources,
32623265
TryCand, Cand, ResourceReduce))
3263-
return;
3266+
return TryCand.Reason != NoCand;
32643267
if (tryGreater(TryCand.ResDelta.DemandedResources,
32653268
Cand.ResDelta.DemandedResources,
32663269
TryCand, Cand, ResourceDemand))
3267-
return;
3270+
return TryCand.Reason != NoCand;
32683271

32693272
// Avoid serializing long latency dependence chains.
32703273
// For acyclic path limited loops, latency was already checked above.
32713274
if (!RegionPolicy.DisableLatencyHeuristic && TryCand.Policy.ReduceLatency &&
32723275
!Rem.IsAcyclicLatencyLimited && tryLatency(TryCand, Cand, *Zone))
3273-
return;
3276+
return TryCand.Reason != NoCand;
32743277

32753278
// Fall through to original instruction order.
32763279
if ((Zone->isTop() && TryCand.SU->NodeNum < Cand.SU->NodeNum)
32773280
|| (!Zone->isTop() && TryCand.SU->NodeNum > Cand.SU->NodeNum)) {
32783281
TryCand.Reason = NodeOrder;
3282+
return true;
32793283
}
32803284
}
3285+
3286+
return false;
32813287
}
32823288

32833289
/// Pick the best candidate from the queue.
@@ -3299,8 +3305,7 @@ void GenericScheduler::pickNodeFromQueue(SchedBoundary &Zone,
32993305
initCandidate(TryCand, SU, Zone.isTop(), RPTracker, TempTracker);
33003306
// Pass SchedBoundary only when comparing nodes from the same boundary.
33013307
SchedBoundary *ZoneArg = Cand.AtTop == TryCand.AtTop ? &Zone : nullptr;
3302-
tryCandidate(Cand, TryCand, ZoneArg);
3303-
if (TryCand.Reason != NoCand) {
3308+
if (tryCandidate(Cand, TryCand, ZoneArg)) {
33043309
// Initialize resource delta if needed in case future heuristics query it.
33053310
if (TryCand.ResDelta == SchedResourceDelta())
33063311
TryCand.initResourceDelta(DAG, SchedModel);
@@ -3378,8 +3383,7 @@ SUnit *GenericScheduler::pickNodeBidirectional(bool &IsTopNode) {
33783383
assert(TopCand.isValid());
33793384
SchedCandidate Cand = BotCand;
33803385
TopCand.Reason = NoCand;
3381-
tryCandidate(Cand, TopCand, nullptr);
3382-
if (TopCand.Reason != NoCand) {
3386+
if (tryCandidate(Cand, TopCand, nullptr)) {
33833387
Cand.setBest(TopCand);
33843388
LLVM_DEBUG(traceCandidate(Cand));
33853389
}
@@ -3543,42 +3547,47 @@ void PostGenericScheduler::registerRoots() {
35433547
///
35443548
/// \param Cand provides the policy and current best candidate.
35453549
/// \param TryCand refers to the next SUnit candidate, otherwise uninitialized.
3546-
void PostGenericScheduler::tryCandidate(SchedCandidate &Cand,
3550+
/// \return \c true if TryCand is better than Cand (Reason is NOT NoCand)
3551+
bool PostGenericScheduler::tryCandidate(SchedCandidate &Cand,
35473552
SchedCandidate &TryCand) {
35483553
// Initialize the candidate if needed.
35493554
if (!Cand.isValid()) {
35503555
TryCand.Reason = NodeOrder;
3551-
return;
3556+
return true;
35523557
}
35533558

35543559
// Prioritize instructions that read unbuffered resources by stall cycles.
35553560
if (tryLess(Top.getLatencyStallCycles(TryCand.SU),
35563561
Top.getLatencyStallCycles(Cand.SU), TryCand, Cand, Stall))
3557-
return;
3562+
return TryCand.Reason != NoCand;
35583563

35593564
// Keep clustered nodes together.
35603565
if (tryGreater(TryCand.SU == DAG->getNextClusterSucc(),
35613566
Cand.SU == DAG->getNextClusterSucc(),
35623567
TryCand, Cand, Cluster))
3563-
return;
3568+
return TryCand.Reason != NoCand;
35643569

35653570
// Avoid critical resource consumption and balance the schedule.
35663571
if (tryLess(TryCand.ResDelta.CritResources, Cand.ResDelta.CritResources,
35673572
TryCand, Cand, ResourceReduce))
3568-
return;
3573+
return TryCand.Reason != NoCand;
35693574
if (tryGreater(TryCand.ResDelta.DemandedResources,
35703575
Cand.ResDelta.DemandedResources,
35713576
TryCand, Cand, ResourceDemand))
3572-
return;
3577+
return TryCand.Reason != NoCand;
35733578

35743579
// Avoid serializing long latency dependence chains.
35753580
if (Cand.Policy.ReduceLatency && tryLatency(TryCand, Cand, Top)) {
3576-
return;
3581+
return TryCand.Reason != NoCand;
35773582
}
35783583

35793584
// Fall through to original instruction order.
3580-
if (TryCand.SU->NodeNum < Cand.SU->NodeNum)
3585+
if (TryCand.SU->NodeNum < Cand.SU->NodeNum) {
35813586
TryCand.Reason = NodeOrder;
3587+
return true;
3588+
}
3589+
3590+
return false;
35823591
}
35833592

35843593
void PostGenericScheduler::pickNodeFromQueue(SchedCandidate &Cand) {
@@ -3588,8 +3597,7 @@ void PostGenericScheduler::pickNodeFromQueue(SchedCandidate &Cand) {
35883597
TryCand.SU = SU;
35893598
TryCand.AtTop = true;
35903599
TryCand.initResourceDelta(DAG, SchedModel);
3591-
tryCandidate(Cand, TryCand);
3592-
if (TryCand.Reason != NoCand) {
3600+
if (tryCandidate(Cand, TryCand)) {
35933601
Cand.setBest(TryCand);
35943602
LLVM_DEBUG(traceCandidate(Cand));
35953603
}

0 commit comments

Comments
 (0)