1d88c1a5aSDimitry Andric //===-- GCNSchedStrategy.cpp - GCN Scheduler Strategy ---------------------===//
2d88c1a5aSDimitry Andric //
3d88c1a5aSDimitry Andric // The LLVM Compiler Infrastructure
4d88c1a5aSDimitry Andric //
5d88c1a5aSDimitry Andric // This file is distributed under the University of Illinois Open Source
6d88c1a5aSDimitry Andric // License. See LICENSE.TXT for details.
7d88c1a5aSDimitry Andric //
8d88c1a5aSDimitry Andric //===----------------------------------------------------------------------===//
9d88c1a5aSDimitry Andric //
10d88c1a5aSDimitry Andric /// \file
11d88c1a5aSDimitry Andric /// This contains a MachineSchedStrategy implementation for maximizing wave
12d88c1a5aSDimitry Andric /// occupancy on GCN hardware.
13d88c1a5aSDimitry Andric //===----------------------------------------------------------------------===//
14d88c1a5aSDimitry Andric
15d88c1a5aSDimitry Andric #include "GCNSchedStrategy.h"
16d88c1a5aSDimitry Andric #include "AMDGPUSubtarget.h"
17d88c1a5aSDimitry Andric #include "SIInstrInfo.h"
18d88c1a5aSDimitry Andric #include "SIMachineFunctionInfo.h"
19d88c1a5aSDimitry Andric #include "SIRegisterInfo.h"
20d88c1a5aSDimitry Andric #include "llvm/CodeGen/RegisterClassInfo.h"
217a7e6055SDimitry Andric #include "llvm/Support/MathExtras.h"
22d88c1a5aSDimitry Andric
23c4394386SDimitry Andric #define DEBUG_TYPE "machine-scheduler"
24d88c1a5aSDimitry Andric
25d88c1a5aSDimitry Andric using namespace llvm;
26d88c1a5aSDimitry Andric
GCNMaxOccupancySchedStrategy(const MachineSchedContext * C)27d88c1a5aSDimitry Andric GCNMaxOccupancySchedStrategy::GCNMaxOccupancySchedStrategy(
28d88c1a5aSDimitry Andric const MachineSchedContext *C) :
297a7e6055SDimitry Andric GenericScheduler(C), TargetOccupancy(0), MF(nullptr) { }
30d88c1a5aSDimitry Andric
initialize(ScheduleDAGMI * DAG)317a7e6055SDimitry Andric void GCNMaxOccupancySchedStrategy::initialize(ScheduleDAGMI *DAG) {
327a7e6055SDimitry Andric GenericScheduler::initialize(DAG);
337a7e6055SDimitry Andric
347a7e6055SDimitry Andric const SIRegisterInfo *SRI = static_cast<const SIRegisterInfo*>(TRI);
357a7e6055SDimitry Andric
367a7e6055SDimitry Andric MF = &DAG->MF;
377a7e6055SDimitry Andric
38*4ba319b5SDimitry Andric const GCNSubtarget &ST = MF->getSubtarget<GCNSubtarget>();
397a7e6055SDimitry Andric
407a7e6055SDimitry Andric // FIXME: This is also necessary, because some passes that run after
417a7e6055SDimitry Andric // scheduling and before regalloc increase register pressure.
427a7e6055SDimitry Andric const int ErrorMargin = 3;
437a7e6055SDimitry Andric
447a7e6055SDimitry Andric SGPRExcessLimit = Context->RegClassInfo
457a7e6055SDimitry Andric ->getNumAllocatableRegs(&AMDGPU::SGPR_32RegClass) - ErrorMargin;
467a7e6055SDimitry Andric VGPRExcessLimit = Context->RegClassInfo
477a7e6055SDimitry Andric ->getNumAllocatableRegs(&AMDGPU::VGPR_32RegClass) - ErrorMargin;
487a7e6055SDimitry Andric if (TargetOccupancy) {
497a7e6055SDimitry Andric SGPRCriticalLimit = ST.getMaxNumSGPRs(TargetOccupancy, true);
507a7e6055SDimitry Andric VGPRCriticalLimit = ST.getMaxNumVGPRs(TargetOccupancy);
517a7e6055SDimitry Andric } else {
527a7e6055SDimitry Andric SGPRCriticalLimit = SRI->getRegPressureSetLimit(DAG->MF,
537a7e6055SDimitry Andric SRI->getSGPRPressureSet());
547a7e6055SDimitry Andric VGPRCriticalLimit = SRI->getRegPressureSetLimit(DAG->MF,
557a7e6055SDimitry Andric SRI->getVGPRPressureSet());
567a7e6055SDimitry Andric }
577a7e6055SDimitry Andric
587a7e6055SDimitry Andric SGPRCriticalLimit -= ErrorMargin;
597a7e6055SDimitry Andric VGPRCriticalLimit -= ErrorMargin;
60d88c1a5aSDimitry Andric }
61d88c1a5aSDimitry Andric
initCandidate(SchedCandidate & Cand,SUnit * SU,bool AtTop,const RegPressureTracker & RPTracker,const SIRegisterInfo * SRI,unsigned SGPRPressure,unsigned VGPRPressure)62d88c1a5aSDimitry Andric void GCNMaxOccupancySchedStrategy::initCandidate(SchedCandidate &Cand, SUnit *SU,
63d88c1a5aSDimitry Andric bool AtTop, const RegPressureTracker &RPTracker,
64d88c1a5aSDimitry Andric const SIRegisterInfo *SRI,
657a7e6055SDimitry Andric unsigned SGPRPressure,
667a7e6055SDimitry Andric unsigned VGPRPressure) {
67d88c1a5aSDimitry Andric
68d88c1a5aSDimitry Andric Cand.SU = SU;
69d88c1a5aSDimitry Andric Cand.AtTop = AtTop;
70d88c1a5aSDimitry Andric
71d88c1a5aSDimitry Andric // getDownwardPressure() and getUpwardPressure() make temporary changes to
72*4ba319b5SDimitry Andric // the tracker, so we need to pass those function a non-const copy.
73d88c1a5aSDimitry Andric RegPressureTracker &TempTracker = const_cast<RegPressureTracker&>(RPTracker);
74d88c1a5aSDimitry Andric
75d88c1a5aSDimitry Andric std::vector<unsigned> Pressure;
76d88c1a5aSDimitry Andric std::vector<unsigned> MaxPressure;
77d88c1a5aSDimitry Andric
78d88c1a5aSDimitry Andric if (AtTop)
79d88c1a5aSDimitry Andric TempTracker.getDownwardPressure(SU->getInstr(), Pressure, MaxPressure);
80d88c1a5aSDimitry Andric else {
81d88c1a5aSDimitry Andric // FIXME: I think for bottom up scheduling, the register pressure is cached
82d88c1a5aSDimitry Andric // and can be retrieved by DAG->getPressureDif(SU).
83d88c1a5aSDimitry Andric TempTracker.getUpwardPressure(SU->getInstr(), Pressure, MaxPressure);
84d88c1a5aSDimitry Andric }
85d88c1a5aSDimitry Andric
867a7e6055SDimitry Andric unsigned NewSGPRPressure = Pressure[SRI->getSGPRPressureSet()];
877a7e6055SDimitry Andric unsigned NewVGPRPressure = Pressure[SRI->getVGPRPressureSet()];
88d88c1a5aSDimitry Andric
89d88c1a5aSDimitry Andric // If two instructions increase the pressure of different register sets
90d88c1a5aSDimitry Andric // by the same amount, the generic scheduler will prefer to schedule the
91d88c1a5aSDimitry Andric // instruction that increases the set with the least amount of registers,
92d88c1a5aSDimitry Andric // which in our case would be SGPRs. This is rarely what we want, so
93d88c1a5aSDimitry Andric // when we report excess/critical register pressure, we do it either
94d88c1a5aSDimitry Andric // only for VGPRs or only for SGPRs.
95d88c1a5aSDimitry Andric
96d88c1a5aSDimitry Andric // FIXME: Better heuristics to determine whether to prefer SGPRs or VGPRs.
977a7e6055SDimitry Andric const unsigned MaxVGPRPressureInc = 16;
98d88c1a5aSDimitry Andric bool ShouldTrackVGPRs = VGPRPressure + MaxVGPRPressureInc >= VGPRExcessLimit;
99d88c1a5aSDimitry Andric bool ShouldTrackSGPRs = !ShouldTrackVGPRs && SGPRPressure >= SGPRExcessLimit;
100d88c1a5aSDimitry Andric
101d88c1a5aSDimitry Andric
102d88c1a5aSDimitry Andric // FIXME: We have to enter REG-EXCESS before we reach the actual threshold
103d88c1a5aSDimitry Andric // to increase the likelihood we don't go over the limits. We should improve
104d88c1a5aSDimitry Andric // the analysis to look through dependencies to find the path with the least
105d88c1a5aSDimitry Andric // register pressure.
106d88c1a5aSDimitry Andric
107d88c1a5aSDimitry Andric // We only need to update the RPDelata for instructions that increase
108d88c1a5aSDimitry Andric // register pressure. Instructions that decrease or keep reg pressure
109d88c1a5aSDimitry Andric // the same will be marked as RegExcess in tryCandidate() when they
110d88c1a5aSDimitry Andric // are compared with instructions that increase the register pressure.
111d88c1a5aSDimitry Andric if (ShouldTrackVGPRs && NewVGPRPressure >= VGPRExcessLimit) {
112d88c1a5aSDimitry Andric Cand.RPDelta.Excess = PressureChange(SRI->getVGPRPressureSet());
113d88c1a5aSDimitry Andric Cand.RPDelta.Excess.setUnitInc(NewVGPRPressure - VGPRExcessLimit);
114d88c1a5aSDimitry Andric }
115d88c1a5aSDimitry Andric
116d88c1a5aSDimitry Andric if (ShouldTrackSGPRs && NewSGPRPressure >= SGPRExcessLimit) {
117d88c1a5aSDimitry Andric Cand.RPDelta.Excess = PressureChange(SRI->getSGPRPressureSet());
1187a7e6055SDimitry Andric Cand.RPDelta.Excess.setUnitInc(NewSGPRPressure - SGPRExcessLimit);
119d88c1a5aSDimitry Andric }
120d88c1a5aSDimitry Andric
121d88c1a5aSDimitry Andric // Register pressure is considered 'CRITICAL' if it is approaching a value
122d88c1a5aSDimitry Andric // that would reduce the wave occupancy for the execution unit. When
123d88c1a5aSDimitry Andric // register pressure is 'CRITICAL', increading SGPR and VGPR pressure both
124d88c1a5aSDimitry Andric // has the same cost, so we don't need to prefer one over the other.
125d88c1a5aSDimitry Andric
126d88c1a5aSDimitry Andric int SGPRDelta = NewSGPRPressure - SGPRCriticalLimit;
127d88c1a5aSDimitry Andric int VGPRDelta = NewVGPRPressure - VGPRCriticalLimit;
128d88c1a5aSDimitry Andric
129d88c1a5aSDimitry Andric if (SGPRDelta >= 0 || VGPRDelta >= 0) {
130d88c1a5aSDimitry Andric if (SGPRDelta > VGPRDelta) {
131d88c1a5aSDimitry Andric Cand.RPDelta.CriticalMax = PressureChange(SRI->getSGPRPressureSet());
132d88c1a5aSDimitry Andric Cand.RPDelta.CriticalMax.setUnitInc(SGPRDelta);
133d88c1a5aSDimitry Andric } else {
134d88c1a5aSDimitry Andric Cand.RPDelta.CriticalMax = PressureChange(SRI->getVGPRPressureSet());
135d88c1a5aSDimitry Andric Cand.RPDelta.CriticalMax.setUnitInc(VGPRDelta);
136d88c1a5aSDimitry Andric }
137d88c1a5aSDimitry Andric }
138d88c1a5aSDimitry Andric }
139d88c1a5aSDimitry Andric
140d88c1a5aSDimitry Andric // This function is mostly cut and pasted from
141d88c1a5aSDimitry Andric // GenericScheduler::pickNodeFromQueue()
pickNodeFromQueue(SchedBoundary & Zone,const CandPolicy & ZonePolicy,const RegPressureTracker & RPTracker,SchedCandidate & Cand)142d88c1a5aSDimitry Andric void GCNMaxOccupancySchedStrategy::pickNodeFromQueue(SchedBoundary &Zone,
143d88c1a5aSDimitry Andric const CandPolicy &ZonePolicy,
144d88c1a5aSDimitry Andric const RegPressureTracker &RPTracker,
145d88c1a5aSDimitry Andric SchedCandidate &Cand) {
146d88c1a5aSDimitry Andric const SIRegisterInfo *SRI = static_cast<const SIRegisterInfo*>(TRI);
147d88c1a5aSDimitry Andric ArrayRef<unsigned> Pressure = RPTracker.getRegSetPressureAtPos();
148d88c1a5aSDimitry Andric unsigned SGPRPressure = Pressure[SRI->getSGPRPressureSet()];
149d88c1a5aSDimitry Andric unsigned VGPRPressure = Pressure[SRI->getVGPRPressureSet()];
150d88c1a5aSDimitry Andric ReadyQueue &Q = Zone.Available;
151d88c1a5aSDimitry Andric for (SUnit *SU : Q) {
152d88c1a5aSDimitry Andric
153d88c1a5aSDimitry Andric SchedCandidate TryCand(ZonePolicy);
154d88c1a5aSDimitry Andric initCandidate(TryCand, SU, Zone.isTop(), RPTracker, SRI,
1557a7e6055SDimitry Andric SGPRPressure, VGPRPressure);
156d88c1a5aSDimitry Andric // Pass SchedBoundary only when comparing nodes from the same boundary.
157d88c1a5aSDimitry Andric SchedBoundary *ZoneArg = Cand.AtTop == TryCand.AtTop ? &Zone : nullptr;
158d88c1a5aSDimitry Andric GenericScheduler::tryCandidate(Cand, TryCand, ZoneArg);
159d88c1a5aSDimitry Andric if (TryCand.Reason != NoCand) {
160d88c1a5aSDimitry Andric // Initialize resource delta if needed in case future heuristics query it.
161d88c1a5aSDimitry Andric if (TryCand.ResDelta == SchedResourceDelta())
162d88c1a5aSDimitry Andric TryCand.initResourceDelta(Zone.DAG, SchedModel);
163d88c1a5aSDimitry Andric Cand.setBest(TryCand);
164d88c1a5aSDimitry Andric }
165d88c1a5aSDimitry Andric }
166d88c1a5aSDimitry Andric }
167d88c1a5aSDimitry Andric
168d88c1a5aSDimitry Andric // This function is mostly cut and pasted from
169d88c1a5aSDimitry Andric // GenericScheduler::pickNodeBidirectional()
pickNodeBidirectional(bool & IsTopNode)170d88c1a5aSDimitry Andric SUnit *GCNMaxOccupancySchedStrategy::pickNodeBidirectional(bool &IsTopNode) {
171d88c1a5aSDimitry Andric // Schedule as far as possible in the direction of no choice. This is most
172d88c1a5aSDimitry Andric // efficient, but also provides the best heuristics for CriticalPSets.
173d88c1a5aSDimitry Andric if (SUnit *SU = Bot.pickOnlyChoice()) {
174d88c1a5aSDimitry Andric IsTopNode = false;
175d88c1a5aSDimitry Andric return SU;
176d88c1a5aSDimitry Andric }
177d88c1a5aSDimitry Andric if (SUnit *SU = Top.pickOnlyChoice()) {
178d88c1a5aSDimitry Andric IsTopNode = true;
179d88c1a5aSDimitry Andric return SU;
180d88c1a5aSDimitry Andric }
181d88c1a5aSDimitry Andric // Set the bottom-up policy based on the state of the current bottom zone and
182d88c1a5aSDimitry Andric // the instructions outside the zone, including the top zone.
183d88c1a5aSDimitry Andric CandPolicy BotPolicy;
184d88c1a5aSDimitry Andric setPolicy(BotPolicy, /*IsPostRA=*/false, Bot, &Top);
185d88c1a5aSDimitry Andric // Set the top-down policy based on the state of the current top zone and
186d88c1a5aSDimitry Andric // the instructions outside the zone, including the bottom zone.
187d88c1a5aSDimitry Andric CandPolicy TopPolicy;
188d88c1a5aSDimitry Andric setPolicy(TopPolicy, /*IsPostRA=*/false, Top, &Bot);
189d88c1a5aSDimitry Andric
190d88c1a5aSDimitry Andric // See if BotCand is still valid (because we previously scheduled from Top).
191*4ba319b5SDimitry Andric LLVM_DEBUG(dbgs() << "Picking from Bot:\n");
192d88c1a5aSDimitry Andric if (!BotCand.isValid() || BotCand.SU->isScheduled ||
193d88c1a5aSDimitry Andric BotCand.Policy != BotPolicy) {
194d88c1a5aSDimitry Andric BotCand.reset(CandPolicy());
195d88c1a5aSDimitry Andric pickNodeFromQueue(Bot, BotPolicy, DAG->getBotRPTracker(), BotCand);
196d88c1a5aSDimitry Andric assert(BotCand.Reason != NoCand && "failed to find the first candidate");
197d88c1a5aSDimitry Andric } else {
198*4ba319b5SDimitry Andric LLVM_DEBUG(traceCandidate(BotCand));
199d88c1a5aSDimitry Andric }
200d88c1a5aSDimitry Andric
201d88c1a5aSDimitry Andric // Check if the top Q has a better candidate.
202*4ba319b5SDimitry Andric LLVM_DEBUG(dbgs() << "Picking from Top:\n");
203d88c1a5aSDimitry Andric if (!TopCand.isValid() || TopCand.SU->isScheduled ||
204d88c1a5aSDimitry Andric TopCand.Policy != TopPolicy) {
205d88c1a5aSDimitry Andric TopCand.reset(CandPolicy());
206d88c1a5aSDimitry Andric pickNodeFromQueue(Top, TopPolicy, DAG->getTopRPTracker(), TopCand);
207d88c1a5aSDimitry Andric assert(TopCand.Reason != NoCand && "failed to find the first candidate");
208d88c1a5aSDimitry Andric } else {
209*4ba319b5SDimitry Andric LLVM_DEBUG(traceCandidate(TopCand));
210d88c1a5aSDimitry Andric }
211d88c1a5aSDimitry Andric
212d88c1a5aSDimitry Andric // Pick best from BotCand and TopCand.
213*4ba319b5SDimitry Andric LLVM_DEBUG(dbgs() << "Top Cand: "; traceCandidate(TopCand);
214*4ba319b5SDimitry Andric dbgs() << "Bot Cand: "; traceCandidate(BotCand););
215d88c1a5aSDimitry Andric SchedCandidate Cand;
216d88c1a5aSDimitry Andric if (TopCand.Reason == BotCand.Reason) {
217d88c1a5aSDimitry Andric Cand = BotCand;
218d88c1a5aSDimitry Andric GenericSchedulerBase::CandReason TopReason = TopCand.Reason;
219d88c1a5aSDimitry Andric TopCand.Reason = NoCand;
220d88c1a5aSDimitry Andric GenericScheduler::tryCandidate(Cand, TopCand, nullptr);
221d88c1a5aSDimitry Andric if (TopCand.Reason != NoCand) {
222d88c1a5aSDimitry Andric Cand.setBest(TopCand);
223d88c1a5aSDimitry Andric } else {
224d88c1a5aSDimitry Andric TopCand.Reason = TopReason;
225d88c1a5aSDimitry Andric }
226d88c1a5aSDimitry Andric } else {
227d88c1a5aSDimitry Andric if (TopCand.Reason == RegExcess && TopCand.RPDelta.Excess.getUnitInc() <= 0) {
228d88c1a5aSDimitry Andric Cand = TopCand;
229d88c1a5aSDimitry Andric } else if (BotCand.Reason == RegExcess && BotCand.RPDelta.Excess.getUnitInc() <= 0) {
230d88c1a5aSDimitry Andric Cand = BotCand;
231d88c1a5aSDimitry Andric } else if (TopCand.Reason == RegCritical && TopCand.RPDelta.CriticalMax.getUnitInc() <= 0) {
232d88c1a5aSDimitry Andric Cand = TopCand;
233d88c1a5aSDimitry Andric } else if (BotCand.Reason == RegCritical && BotCand.RPDelta.CriticalMax.getUnitInc() <= 0) {
234d88c1a5aSDimitry Andric Cand = BotCand;
235d88c1a5aSDimitry Andric } else {
2367a7e6055SDimitry Andric if (BotCand.Reason > TopCand.Reason) {
237d88c1a5aSDimitry Andric Cand = TopCand;
238d88c1a5aSDimitry Andric } else {
239d88c1a5aSDimitry Andric Cand = BotCand;
240d88c1a5aSDimitry Andric }
241d88c1a5aSDimitry Andric }
242d88c1a5aSDimitry Andric }
243*4ba319b5SDimitry Andric LLVM_DEBUG(dbgs() << "Picking: "; traceCandidate(Cand););
244d88c1a5aSDimitry Andric
245d88c1a5aSDimitry Andric IsTopNode = Cand.AtTop;
246d88c1a5aSDimitry Andric return Cand.SU;
247d88c1a5aSDimitry Andric }
248d88c1a5aSDimitry Andric
249d88c1a5aSDimitry Andric // This function is mostly cut and pasted from
250d88c1a5aSDimitry Andric // GenericScheduler::pickNode()
pickNode(bool & IsTopNode)251d88c1a5aSDimitry Andric SUnit *GCNMaxOccupancySchedStrategy::pickNode(bool &IsTopNode) {
252d88c1a5aSDimitry Andric if (DAG->top() == DAG->bottom()) {
253d88c1a5aSDimitry Andric assert(Top.Available.empty() && Top.Pending.empty() &&
254d88c1a5aSDimitry Andric Bot.Available.empty() && Bot.Pending.empty() && "ReadyQ garbage");
255d88c1a5aSDimitry Andric return nullptr;
256d88c1a5aSDimitry Andric }
257d88c1a5aSDimitry Andric SUnit *SU;
258d88c1a5aSDimitry Andric do {
259d88c1a5aSDimitry Andric if (RegionPolicy.OnlyTopDown) {
260d88c1a5aSDimitry Andric SU = Top.pickOnlyChoice();
261d88c1a5aSDimitry Andric if (!SU) {
262d88c1a5aSDimitry Andric CandPolicy NoPolicy;
263d88c1a5aSDimitry Andric TopCand.reset(NoPolicy);
264d88c1a5aSDimitry Andric pickNodeFromQueue(Top, NoPolicy, DAG->getTopRPTracker(), TopCand);
265d88c1a5aSDimitry Andric assert(TopCand.Reason != NoCand && "failed to find a candidate");
266d88c1a5aSDimitry Andric SU = TopCand.SU;
267d88c1a5aSDimitry Andric }
268d88c1a5aSDimitry Andric IsTopNode = true;
269d88c1a5aSDimitry Andric } else if (RegionPolicy.OnlyBottomUp) {
270d88c1a5aSDimitry Andric SU = Bot.pickOnlyChoice();
271d88c1a5aSDimitry Andric if (!SU) {
272d88c1a5aSDimitry Andric CandPolicy NoPolicy;
273d88c1a5aSDimitry Andric BotCand.reset(NoPolicy);
274d88c1a5aSDimitry Andric pickNodeFromQueue(Bot, NoPolicy, DAG->getBotRPTracker(), BotCand);
275d88c1a5aSDimitry Andric assert(BotCand.Reason != NoCand && "failed to find a candidate");
276d88c1a5aSDimitry Andric SU = BotCand.SU;
277d88c1a5aSDimitry Andric }
278d88c1a5aSDimitry Andric IsTopNode = false;
279d88c1a5aSDimitry Andric } else {
280d88c1a5aSDimitry Andric SU = pickNodeBidirectional(IsTopNode);
281d88c1a5aSDimitry Andric }
282d88c1a5aSDimitry Andric } while (SU->isScheduled);
283d88c1a5aSDimitry Andric
284d88c1a5aSDimitry Andric if (SU->isTopReady())
285d88c1a5aSDimitry Andric Top.removeReady(SU);
286d88c1a5aSDimitry Andric if (SU->isBottomReady())
287d88c1a5aSDimitry Andric Bot.removeReady(SU);
288d88c1a5aSDimitry Andric
289*4ba319b5SDimitry Andric LLVM_DEBUG(dbgs() << "Scheduling SU(" << SU->NodeNum << ") "
290*4ba319b5SDimitry Andric << *SU->getInstr());
291d88c1a5aSDimitry Andric return SU;
292d88c1a5aSDimitry Andric }
2937a7e6055SDimitry Andric
GCNScheduleDAGMILive(MachineSchedContext * C,std::unique_ptr<MachineSchedStrategy> S)2947a7e6055SDimitry Andric GCNScheduleDAGMILive::GCNScheduleDAGMILive(MachineSchedContext *C,
2957a7e6055SDimitry Andric std::unique_ptr<MachineSchedStrategy> S) :
2967a7e6055SDimitry Andric ScheduleDAGMILive(C, std::move(S)),
297*4ba319b5SDimitry Andric ST(MF.getSubtarget<GCNSubtarget>()),
2987a7e6055SDimitry Andric MFI(*MF.getInfo<SIMachineFunctionInfo>()),
299*4ba319b5SDimitry Andric StartingOccupancy(MFI.getOccupancy()),
3005517e702SDimitry Andric MinOccupancy(StartingOccupancy), Stage(0), RegionIdx(0) {
3017a7e6055SDimitry Andric
302*4ba319b5SDimitry Andric LLVM_DEBUG(dbgs() << "Starting occupancy is " << StartingOccupancy << ".\n");
3037a7e6055SDimitry Andric }
3047a7e6055SDimitry Andric
schedule()3057a7e6055SDimitry Andric void GCNScheduleDAGMILive::schedule() {
3065517e702SDimitry Andric if (Stage == 0) {
3075517e702SDimitry Andric // Just record regions at the first pass.
3085517e702SDimitry Andric Regions.push_back(std::make_pair(RegionBegin, RegionEnd));
3095517e702SDimitry Andric return;
3105517e702SDimitry Andric }
3115517e702SDimitry Andric
3127a7e6055SDimitry Andric std::vector<MachineInstr*> Unsched;
3137a7e6055SDimitry Andric Unsched.reserve(NumRegionInstrs);
3142cab237bSDimitry Andric for (auto &I : *this) {
3157a7e6055SDimitry Andric Unsched.push_back(&I);
3162cab237bSDimitry Andric }
3177a7e6055SDimitry Andric
3185517e702SDimitry Andric GCNRegPressure PressureBefore;
3197a7e6055SDimitry Andric if (LIS) {
3205517e702SDimitry Andric PressureBefore = Pressure[RegionIdx];
3215517e702SDimitry Andric
322*4ba319b5SDimitry Andric LLVM_DEBUG(dbgs() << "Pressure before scheduling:\nRegion live-ins:";
3235517e702SDimitry Andric GCNRPTracker::printLiveRegs(dbgs(), LiveIns[RegionIdx], MRI);
3245517e702SDimitry Andric dbgs() << "Region live-in pressure: ";
3255517e702SDimitry Andric llvm::getRegPressure(MRI, LiveIns[RegionIdx]).print(dbgs());
3265517e702SDimitry Andric dbgs() << "Region register pressure: ";
3275517e702SDimitry Andric PressureBefore.print(dbgs()));
3287a7e6055SDimitry Andric }
3297a7e6055SDimitry Andric
3307a7e6055SDimitry Andric ScheduleDAGMILive::schedule();
3315517e702SDimitry Andric Regions[RegionIdx] = std::make_pair(RegionBegin, RegionEnd);
3327a7e6055SDimitry Andric
3337a7e6055SDimitry Andric if (!LIS)
3347a7e6055SDimitry Andric return;
3357a7e6055SDimitry Andric
3367a7e6055SDimitry Andric // Check the results of scheduling.
3377a7e6055SDimitry Andric GCNMaxOccupancySchedStrategy &S = (GCNMaxOccupancySchedStrategy&)*SchedImpl;
3387a7e6055SDimitry Andric auto PressureAfter = getRealRegPressure();
3397a7e6055SDimitry Andric
340*4ba319b5SDimitry Andric LLVM_DEBUG(dbgs() << "Pressure after scheduling: ";
341*4ba319b5SDimitry Andric PressureAfter.print(dbgs()));
3425517e702SDimitry Andric
3435517e702SDimitry Andric if (PressureAfter.getSGPRNum() <= S.SGPRCriticalLimit &&
3445517e702SDimitry Andric PressureAfter.getVGPRNum() <= S.VGPRCriticalLimit) {
3455517e702SDimitry Andric Pressure[RegionIdx] = PressureAfter;
346*4ba319b5SDimitry Andric LLVM_DEBUG(dbgs() << "Pressure in desired limits, done.\n");
3477a7e6055SDimitry Andric return;
3487a7e6055SDimitry Andric }
349*4ba319b5SDimitry Andric unsigned Occ = MFI.getOccupancy();
350*4ba319b5SDimitry Andric unsigned WavesAfter = std::min(Occ, PressureAfter.getOccupancy(ST));
351*4ba319b5SDimitry Andric unsigned WavesBefore = std::min(Occ, PressureBefore.getOccupancy(ST));
352*4ba319b5SDimitry Andric LLVM_DEBUG(dbgs() << "Occupancy before scheduling: " << WavesBefore
353*4ba319b5SDimitry Andric << ", after " << WavesAfter << ".\n");
3547a7e6055SDimitry Andric
3557a7e6055SDimitry Andric // We could not keep current target occupancy because of the just scheduled
3567a7e6055SDimitry Andric // region. Record new occupancy for next scheduling cycle.
3577a7e6055SDimitry Andric unsigned NewOccupancy = std::max(WavesAfter, WavesBefore);
358*4ba319b5SDimitry Andric // Allow memory bound functions to drop to 4 waves if not limited by an
359*4ba319b5SDimitry Andric // attribute.
360*4ba319b5SDimitry Andric if (WavesAfter < WavesBefore && WavesAfter < MinOccupancy &&
361*4ba319b5SDimitry Andric WavesAfter >= MFI.getMinAllowedOccupancy()) {
362*4ba319b5SDimitry Andric LLVM_DEBUG(dbgs() << "Function is memory bound, allow occupancy drop up to "
363*4ba319b5SDimitry Andric << MFI.getMinAllowedOccupancy() << " waves\n");
364*4ba319b5SDimitry Andric NewOccupancy = WavesAfter;
365*4ba319b5SDimitry Andric }
3667a7e6055SDimitry Andric if (NewOccupancy < MinOccupancy) {
3677a7e6055SDimitry Andric MinOccupancy = NewOccupancy;
368*4ba319b5SDimitry Andric MFI.limitOccupancy(MinOccupancy);
369*4ba319b5SDimitry Andric LLVM_DEBUG(dbgs() << "Occupancy lowered for the function to "
3707a7e6055SDimitry Andric << MinOccupancy << ".\n");
3717a7e6055SDimitry Andric }
3727a7e6055SDimitry Andric
373*4ba319b5SDimitry Andric if (WavesAfter >= MinOccupancy) {
3745517e702SDimitry Andric Pressure[RegionIdx] = PressureAfter;
3757a7e6055SDimitry Andric return;
3765517e702SDimitry Andric }
3777a7e6055SDimitry Andric
378*4ba319b5SDimitry Andric LLVM_DEBUG(dbgs() << "Attempting to revert scheduling.\n");
3797a7e6055SDimitry Andric RegionEnd = RegionBegin;
3807a7e6055SDimitry Andric for (MachineInstr *MI : Unsched) {
381*4ba319b5SDimitry Andric if (MI->isDebugInstr())
3822cab237bSDimitry Andric continue;
3832cab237bSDimitry Andric
3847a7e6055SDimitry Andric if (MI->getIterator() != RegionEnd) {
3857a7e6055SDimitry Andric BB->remove(MI);
3867a7e6055SDimitry Andric BB->insert(RegionEnd, MI);
387*4ba319b5SDimitry Andric if (!MI->isDebugInstr())
3887a7e6055SDimitry Andric LIS->handleMove(*MI, true);
3897a7e6055SDimitry Andric }
3907a7e6055SDimitry Andric // Reset read-undef flags and update them later.
3917a7e6055SDimitry Andric for (auto &Op : MI->operands())
3927a7e6055SDimitry Andric if (Op.isReg() && Op.isDef())
3937a7e6055SDimitry Andric Op.setIsUndef(false);
3947a7e6055SDimitry Andric RegisterOperands RegOpers;
3957a7e6055SDimitry Andric RegOpers.collect(*MI, *TRI, MRI, ShouldTrackLaneMasks, false);
396*4ba319b5SDimitry Andric if (!MI->isDebugInstr()) {
3977a7e6055SDimitry Andric if (ShouldTrackLaneMasks) {
3987a7e6055SDimitry Andric // Adjust liveness and add missing dead+read-undef flags.
3997a7e6055SDimitry Andric SlotIndex SlotIdx = LIS->getInstructionIndex(*MI).getRegSlot();
4007a7e6055SDimitry Andric RegOpers.adjustLaneLiveness(*LIS, MRI, SlotIdx, MI);
4017a7e6055SDimitry Andric } else {
4027a7e6055SDimitry Andric // Adjust for missing dead-def flags.
4037a7e6055SDimitry Andric RegOpers.detectDeadDefs(*MI, *LIS);
4047a7e6055SDimitry Andric }
4052cab237bSDimitry Andric }
4067a7e6055SDimitry Andric RegionEnd = MI->getIterator();
4077a7e6055SDimitry Andric ++RegionEnd;
408*4ba319b5SDimitry Andric LLVM_DEBUG(dbgs() << "Scheduling " << *MI);
4097a7e6055SDimitry Andric }
4107a7e6055SDimitry Andric RegionBegin = Unsched.front()->getIterator();
4115517e702SDimitry Andric Regions[RegionIdx] = std::make_pair(RegionBegin, RegionEnd);
4127a7e6055SDimitry Andric
4137a7e6055SDimitry Andric placeDebugValues();
4147a7e6055SDimitry Andric }
4157a7e6055SDimitry Andric
getRealRegPressure() const4165517e702SDimitry Andric GCNRegPressure GCNScheduleDAGMILive::getRealRegPressure() const {
4175517e702SDimitry Andric GCNDownwardRPTracker RPTracker(*LIS);
4185517e702SDimitry Andric RPTracker.advance(begin(), end(), &LiveIns[RegionIdx]);
4195517e702SDimitry Andric return RPTracker.moveMaxPressure();
4207a7e6055SDimitry Andric }
4217a7e6055SDimitry Andric
computeBlockPressure(const MachineBasicBlock * MBB)4225517e702SDimitry Andric void GCNScheduleDAGMILive::computeBlockPressure(const MachineBasicBlock *MBB) {
4235517e702SDimitry Andric GCNDownwardRPTracker RPTracker(*LIS);
4247a7e6055SDimitry Andric
4255517e702SDimitry Andric // If the block has the only successor then live-ins of that successor are
4265517e702SDimitry Andric // live-outs of the current block. We can reuse calculated live set if the
4275517e702SDimitry Andric // successor will be sent to scheduling past current block.
4285517e702SDimitry Andric const MachineBasicBlock *OnlySucc = nullptr;
4295517e702SDimitry Andric if (MBB->succ_size() == 1 && !(*MBB->succ_begin())->empty()) {
4305517e702SDimitry Andric SlotIndexes *Ind = LIS->getSlotIndexes();
4315517e702SDimitry Andric if (Ind->getMBBStartIdx(MBB) < Ind->getMBBStartIdx(*MBB->succ_begin()))
4325517e702SDimitry Andric OnlySucc = *MBB->succ_begin();
4337a7e6055SDimitry Andric }
4347a7e6055SDimitry Andric
4355517e702SDimitry Andric // Scheduler sends regions from the end of the block upwards.
4365517e702SDimitry Andric size_t CurRegion = RegionIdx;
4375517e702SDimitry Andric for (size_t E = Regions.size(); CurRegion != E; ++CurRegion)
4385517e702SDimitry Andric if (Regions[CurRegion].first->getParent() != MBB)
4395517e702SDimitry Andric break;
4405517e702SDimitry Andric --CurRegion;
4417a7e6055SDimitry Andric
4425517e702SDimitry Andric auto I = MBB->begin();
4435517e702SDimitry Andric auto LiveInIt = MBBLiveIns.find(MBB);
4445517e702SDimitry Andric if (LiveInIt != MBBLiveIns.end()) {
4455517e702SDimitry Andric auto LiveIn = std::move(LiveInIt->second);
4465517e702SDimitry Andric RPTracker.reset(*MBB->begin(), &LiveIn);
4475517e702SDimitry Andric MBBLiveIns.erase(LiveInIt);
4485517e702SDimitry Andric } else {
4495517e702SDimitry Andric I = Regions[CurRegion].first;
4505517e702SDimitry Andric RPTracker.reset(*I);
4517a7e6055SDimitry Andric }
4527a7e6055SDimitry Andric
4535517e702SDimitry Andric for ( ; ; ) {
4545517e702SDimitry Andric I = RPTracker.getNext();
4557a7e6055SDimitry Andric
4565517e702SDimitry Andric if (Regions[CurRegion].first == I) {
4575517e702SDimitry Andric LiveIns[CurRegion] = RPTracker.getLiveRegs();
4585517e702SDimitry Andric RPTracker.clearMaxPressure();
4597a7e6055SDimitry Andric }
4607a7e6055SDimitry Andric
4615517e702SDimitry Andric if (Regions[CurRegion].second == I) {
4625517e702SDimitry Andric Pressure[CurRegion] = RPTracker.moveMaxPressure();
4635517e702SDimitry Andric if (CurRegion-- == RegionIdx)
4645517e702SDimitry Andric break;
4657a7e6055SDimitry Andric }
4665517e702SDimitry Andric RPTracker.advanceToNext();
4675517e702SDimitry Andric RPTracker.advanceBeforeNext();
4687a7e6055SDimitry Andric }
4697a7e6055SDimitry Andric
4705517e702SDimitry Andric if (OnlySucc) {
4715517e702SDimitry Andric if (I != MBB->end()) {
4725517e702SDimitry Andric RPTracker.advanceToNext();
4735517e702SDimitry Andric RPTracker.advance(MBB->end());
4747a7e6055SDimitry Andric }
4755517e702SDimitry Andric RPTracker.reset(*OnlySucc->begin(), &RPTracker.getLiveRegs());
4765517e702SDimitry Andric RPTracker.advanceBeforeNext();
4775517e702SDimitry Andric MBBLiveIns[OnlySucc] = RPTracker.moveLiveRegs();
4787a7e6055SDimitry Andric }
4797a7e6055SDimitry Andric }
4807a7e6055SDimitry Andric
finalizeSchedule()4817a7e6055SDimitry Andric void GCNScheduleDAGMILive::finalizeSchedule() {
4825517e702SDimitry Andric GCNMaxOccupancySchedStrategy &S = (GCNMaxOccupancySchedStrategy&)*SchedImpl;
483*4ba319b5SDimitry Andric LLVM_DEBUG(dbgs() << "All regions recorded, starting actual scheduling.\n");
4845517e702SDimitry Andric
4855517e702SDimitry Andric LiveIns.resize(Regions.size());
4865517e702SDimitry Andric Pressure.resize(Regions.size());
4875517e702SDimitry Andric
4885517e702SDimitry Andric do {
4895517e702SDimitry Andric Stage++;
4905517e702SDimitry Andric RegionIdx = 0;
4915517e702SDimitry Andric MachineBasicBlock *MBB = nullptr;
4925517e702SDimitry Andric
4935517e702SDimitry Andric if (Stage > 1) {
4947a7e6055SDimitry Andric // Retry function scheduling if we found resulting occupancy and it is
4957a7e6055SDimitry Andric // lower than used for first pass scheduling. This will give more freedom
4967a7e6055SDimitry Andric // to schedule low register pressure blocks.
4977a7e6055SDimitry Andric // Code is partially copied from MachineSchedulerBase::scheduleRegions().
4987a7e6055SDimitry Andric
4997a7e6055SDimitry Andric if (!LIS || StartingOccupancy <= MinOccupancy)
5005517e702SDimitry Andric break;
5017a7e6055SDimitry Andric
502*4ba319b5SDimitry Andric LLVM_DEBUG(
503*4ba319b5SDimitry Andric dbgs()
5045517e702SDimitry Andric << "Retrying function scheduling with lowest recorded occupancy "
5057a7e6055SDimitry Andric << MinOccupancy << ".\n");
5067a7e6055SDimitry Andric
5077a7e6055SDimitry Andric S.setTargetOccupancy(MinOccupancy);
5085517e702SDimitry Andric }
5097a7e6055SDimitry Andric
5107a7e6055SDimitry Andric for (auto Region : Regions) {
5117a7e6055SDimitry Andric RegionBegin = Region.first;
5127a7e6055SDimitry Andric RegionEnd = Region.second;
5137a7e6055SDimitry Andric
5147a7e6055SDimitry Andric if (RegionBegin->getParent() != MBB) {
5157a7e6055SDimitry Andric if (MBB) finishBlock();
5167a7e6055SDimitry Andric MBB = RegionBegin->getParent();
5177a7e6055SDimitry Andric startBlock(MBB);
5185517e702SDimitry Andric if (Stage == 1)
5195517e702SDimitry Andric computeBlockPressure(MBB);
5207a7e6055SDimitry Andric }
5217a7e6055SDimitry Andric
5227a7e6055SDimitry Andric unsigned NumRegionInstrs = std::distance(begin(), end());
5237a7e6055SDimitry Andric enterRegion(MBB, begin(), end(), NumRegionInstrs);
5247a7e6055SDimitry Andric
5257a7e6055SDimitry Andric // Skip empty scheduling regions (0 or 1 schedulable instructions).
5267a7e6055SDimitry Andric if (begin() == end() || begin() == std::prev(end())) {
5277a7e6055SDimitry Andric exitRegion();
5287a7e6055SDimitry Andric continue;
5297a7e6055SDimitry Andric }
5305517e702SDimitry Andric
531*4ba319b5SDimitry Andric LLVM_DEBUG(dbgs() << "********** MI Scheduling **********\n");
532*4ba319b5SDimitry Andric LLVM_DEBUG(dbgs() << MF.getName() << ":" << printMBBReference(*MBB) << " "
533*4ba319b5SDimitry Andric << MBB->getName() << "\n From: " << *begin()
534*4ba319b5SDimitry Andric << " To: ";
5357a7e6055SDimitry Andric if (RegionEnd != MBB->end()) dbgs() << *RegionEnd;
5367a7e6055SDimitry Andric else dbgs() << "End";
5377a7e6055SDimitry Andric dbgs() << " RegionInstrs: " << NumRegionInstrs << '\n');
5387a7e6055SDimitry Andric
5397a7e6055SDimitry Andric schedule();
5407a7e6055SDimitry Andric
5417a7e6055SDimitry Andric exitRegion();
5425517e702SDimitry Andric ++RegionIdx;
5437a7e6055SDimitry Andric }
5447a7e6055SDimitry Andric finishBlock();
5455517e702SDimitry Andric
5465517e702SDimitry Andric } while (Stage < 2);
5477a7e6055SDimitry Andric }
548