blob: 182ce1e4f631301d66f124a97a65075f9e3965da [file] [log] [blame]
Eugene Zelenkoc8fbf6f2017-08-10 00:46:15 +00001//===- GCNIterativeScheduler.cpp ------------------------------------------===//
Valery Pykhtinfd4c4102017-03-21 13:15:46 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
Valery Pykhtinfd4c4102017-03-21 13:15:46 +00009
10#include "GCNIterativeScheduler.h"
Eugene Zelenkoc8fbf6f2017-08-10 00:46:15 +000011#include "AMDGPUSubtarget.h"
12#include "GCNRegPressure.h"
Valery Pykhtinfd4c4102017-03-21 13:15:46 +000013#include "GCNSchedStrategy.h"
Eugene Zelenkoc8fbf6f2017-08-10 00:46:15 +000014#include "llvm/ADT/ArrayRef.h"
15#include "llvm/ADT/STLExtras.h"
16#include "llvm/ADT/SmallVector.h"
Matthias Braunf8422972017-12-13 02:51:04 +000017#include "llvm/CodeGen/LiveIntervals.h"
Eugene Zelenkoc8fbf6f2017-08-10 00:46:15 +000018#include "llvm/CodeGen/MachineBasicBlock.h"
19#include "llvm/CodeGen/MachineFunction.h"
20#include "llvm/CodeGen/RegisterPressure.h"
21#include "llvm/CodeGen/ScheduleDAG.h"
Nico Weber432a3882018-04-30 14:59:11 +000022#include "llvm/Config/llvm-config.h"
Eugene Zelenkoc8fbf6f2017-08-10 00:46:15 +000023#include "llvm/Support/Compiler.h"
24#include "llvm/Support/Debug.h"
25#include "llvm/Support/raw_ostream.h"
26#include <algorithm>
27#include <cassert>
28#include <iterator>
29#include <limits>
30#include <memory>
31#include <type_traits>
32#include <vector>
Valery Pykhtinfd4c4102017-03-21 13:15:46 +000033
34using namespace llvm;
35
Evandro Menezes0cd23f562017-07-11 22:08:28 +000036#define DEBUG_TYPE "machine-scheduler"
Valery Pykhtinfd4c4102017-03-21 13:15:46 +000037
38namespace llvm {
Eugene Zelenkoc8fbf6f2017-08-10 00:46:15 +000039
40std::vector<const SUnit *> makeMinRegSchedule(ArrayRef<const SUnit *> TopRoots,
41 const ScheduleDAG &DAG);
42
Valery Pykhtinf2fe9722017-11-20 14:35:53 +000043 std::vector<const SUnit*> makeGCNILPScheduler(ArrayRef<const SUnit*> BotRoots,
44 const ScheduleDAG &DAG);
45}
Valery Pykhtinfd4c4102017-03-21 13:15:46 +000046
47// shim accessors for different order containers
48static inline MachineInstr *getMachineInstr(MachineInstr *MI) {
49 return MI;
50}
51static inline MachineInstr *getMachineInstr(const SUnit *SU) {
52 return SU->getInstr();
53}
54static inline MachineInstr *getMachineInstr(const SUnit &SU) {
55 return SU.getInstr();
56}
57
Aaron Ballman615eb472017-10-15 14:32:27 +000058#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
Valery Pykhtinfd4c4102017-03-21 13:15:46 +000059LLVM_DUMP_METHOD
60static void printRegion(raw_ostream &OS,
61 MachineBasicBlock::iterator Begin,
62 MachineBasicBlock::iterator End,
63 const LiveIntervals *LIS,
64 unsigned MaxInstNum =
65 std::numeric_limits<unsigned>::max()) {
66 auto BB = Begin->getParent();
Francis Visoiu Mistrih25528d62017-12-04 17:18:51 +000067 OS << BB->getParent()->getName() << ":" << printMBBReference(*BB) << ' '
68 << BB->getName() << ":\n";
Valery Pykhtinfd4c4102017-03-21 13:15:46 +000069 auto I = Begin;
70 MaxInstNum = std::max(MaxInstNum, 1u);
71 for (; I != End && MaxInstNum; ++I, --MaxInstNum) {
Shiva Chen801bf7e2018-05-09 02:42:00 +000072 if (!I->isDebugInstr() && LIS)
Valery Pykhtinfd4c4102017-03-21 13:15:46 +000073 OS << LIS->getInstructionIndex(*I);
74 OS << '\t' << *I;
75 }
76 if (I != End) {
77 OS << "\t...\n";
78 I = std::prev(End);
Shiva Chen801bf7e2018-05-09 02:42:00 +000079 if (!I->isDebugInstr() && LIS)
Valery Pykhtinfd4c4102017-03-21 13:15:46 +000080 OS << LIS->getInstructionIndex(*I);
81 OS << '\t' << *I;
82 }
83 if (End != BB->end()) { // print boundary inst if present
84 OS << "----\n";
85 if (LIS) OS << LIS->getInstructionIndex(*End) << '\t';
86 OS << *End;
87 }
88}
89
90LLVM_DUMP_METHOD
91static void printLivenessInfo(raw_ostream &OS,
92 MachineBasicBlock::iterator Begin,
93 MachineBasicBlock::iterator End,
94 const LiveIntervals *LIS) {
95 const auto BB = Begin->getParent();
96 const auto &MRI = BB->getParent()->getRegInfo();
97
98 const auto LiveIns = getLiveRegsBefore(*Begin, *LIS);
99 OS << "LIn RP: ";
100 getRegPressure(MRI, LiveIns).print(OS);
101
102 const auto BottomMI = End == BB->end() ? std::prev(End) : End;
103 const auto LiveOuts = getLiveRegsAfter(*BottomMI, *LIS);
104 OS << "LOt RP: ";
105 getRegPressure(MRI, LiveOuts).print(OS);
106}
107
108LLVM_DUMP_METHOD
109void GCNIterativeScheduler::printRegions(raw_ostream &OS) const {
110 const auto &ST = MF.getSubtarget<SISubtarget>();
111 for (const auto R : Regions) {
112 OS << "Region to schedule ";
113 printRegion(OS, R->Begin, R->End, LIS, 1);
114 printLivenessInfo(OS, R->Begin, R->End, LIS);
115 OS << "Max RP: ";
116 R->MaxPressure.print(OS, &ST);
117 }
118}
119
120LLVM_DUMP_METHOD
121void GCNIterativeScheduler::printSchedResult(raw_ostream &OS,
122 const Region *R,
123 const GCNRegPressure &RP) const {
124 OS << "\nAfter scheduling ";
125 printRegion(OS, R->Begin, R->End, LIS);
126 printSchedRP(OS, R->MaxPressure, RP);
127 OS << '\n';
128}
129
130LLVM_DUMP_METHOD
131void GCNIterativeScheduler::printSchedRP(raw_ostream &OS,
132 const GCNRegPressure &Before,
133 const GCNRegPressure &After) const {
134 const auto &ST = MF.getSubtarget<SISubtarget>();
135 OS << "RP before: ";
136 Before.print(OS, &ST);
137 OS << "RP after: ";
138 After.print(OS, &ST);
139}
Valery Pykhtinfd4c4102017-03-21 13:15:46 +0000140#endif
141
142// DAG builder helper
143class GCNIterativeScheduler::BuildDAG {
144 GCNIterativeScheduler &Sch;
Eugene Zelenkoc8fbf6f2017-08-10 00:46:15 +0000145 SmallVector<SUnit *, 8> TopRoots;
146
Valery Pykhtinf2fe9722017-11-20 14:35:53 +0000147 SmallVector<SUnit*, 8> BotRoots;
Valery Pykhtinfd4c4102017-03-21 13:15:46 +0000148public:
149 BuildDAG(const Region &R, GCNIterativeScheduler &_Sch)
150 : Sch(_Sch) {
151 auto BB = R.Begin->getParent();
152 Sch.BaseClass::startBlock(BB);
153 Sch.BaseClass::enterRegion(BB, R.Begin, R.End, R.NumRegionInstrs);
154
155 Sch.buildSchedGraph(Sch.AA, nullptr, nullptr, nullptr,
156 /*TrackLaneMask*/true);
157 Sch.Topo.InitDAGTopologicalSorting();
Valery Pykhtinfd4c4102017-03-21 13:15:46 +0000158 Sch.findRootsAndBiasEdges(TopRoots, BotRoots);
159 }
Eugene Zelenkoc8fbf6f2017-08-10 00:46:15 +0000160
Valery Pykhtinfd4c4102017-03-21 13:15:46 +0000161 ~BuildDAG() {
162 Sch.BaseClass::exitRegion();
163 Sch.BaseClass::finishBlock();
164 }
Eugene Zelenkoc8fbf6f2017-08-10 00:46:15 +0000165
166 ArrayRef<const SUnit *> getTopRoots() const {
Valery Pykhtinfd4c4102017-03-21 13:15:46 +0000167 return TopRoots;
168 }
Valery Pykhtinf2fe9722017-11-20 14:35:53 +0000169 ArrayRef<SUnit*> getBottomRoots() const {
170 return BotRoots;
171 }
Valery Pykhtinfd4c4102017-03-21 13:15:46 +0000172};
173
174class GCNIterativeScheduler::OverrideLegacyStrategy {
175 GCNIterativeScheduler &Sch;
176 Region &Rgn;
177 std::unique_ptr<MachineSchedStrategy> SaveSchedImpl;
178 GCNRegPressure SaveMaxRP;
Eugene Zelenkoc8fbf6f2017-08-10 00:46:15 +0000179
Valery Pykhtinfd4c4102017-03-21 13:15:46 +0000180public:
181 OverrideLegacyStrategy(Region &R,
182 MachineSchedStrategy &OverrideStrategy,
183 GCNIterativeScheduler &_Sch)
184 : Sch(_Sch)
185 , Rgn(R)
186 , SaveSchedImpl(std::move(_Sch.SchedImpl))
187 , SaveMaxRP(R.MaxPressure) {
188 Sch.SchedImpl.reset(&OverrideStrategy);
189 auto BB = R.Begin->getParent();
190 Sch.BaseClass::startBlock(BB);
191 Sch.BaseClass::enterRegion(BB, R.Begin, R.End, R.NumRegionInstrs);
192 }
Eugene Zelenkoc8fbf6f2017-08-10 00:46:15 +0000193
Valery Pykhtinfd4c4102017-03-21 13:15:46 +0000194 ~OverrideLegacyStrategy() {
195 Sch.BaseClass::exitRegion();
196 Sch.BaseClass::finishBlock();
197 Sch.SchedImpl.release();
198 Sch.SchedImpl = std::move(SaveSchedImpl);
199 }
Eugene Zelenkoc8fbf6f2017-08-10 00:46:15 +0000200
Valery Pykhtinfd4c4102017-03-21 13:15:46 +0000201 void schedule() {
202 assert(Sch.RegionBegin == Rgn.Begin && Sch.RegionEnd == Rgn.End);
203 DEBUG(dbgs() << "\nScheduling ";
204 printRegion(dbgs(), Rgn.Begin, Rgn.End, Sch.LIS, 2));
205 Sch.BaseClass::schedule();
206
207 // Unfortunatelly placeDebugValues incorrectly modifies RegionEnd, restore
208 Sch.RegionEnd = Rgn.End;
209 //assert(Rgn.End == Sch.RegionEnd);
210 Rgn.Begin = Sch.RegionBegin;
211 Rgn.MaxPressure.clear();
212 }
Eugene Zelenkoc8fbf6f2017-08-10 00:46:15 +0000213
Valery Pykhtinfd4c4102017-03-21 13:15:46 +0000214 void restoreOrder() {
215 assert(Sch.RegionBegin == Rgn.Begin && Sch.RegionEnd == Rgn.End);
216 // DAG SUnits are stored using original region's order
217 // so just use SUnits as the restoring schedule
218 Sch.scheduleRegion(Rgn, Sch.SUnits, SaveMaxRP);
219 }
220};
221
Benjamin Kramerdebb3c32017-05-26 20:09:00 +0000222namespace {
Eugene Zelenkoc8fbf6f2017-08-10 00:46:15 +0000223
Valery Pykhtinfd4c4102017-03-21 13:15:46 +0000224// just a stub to make base class happy
225class SchedStrategyStub : public MachineSchedStrategy {
226public:
227 bool shouldTrackPressure() const override { return false; }
228 bool shouldTrackLaneMasks() const override { return false; }
229 void initialize(ScheduleDAGMI *DAG) override {}
230 SUnit *pickNode(bool &IsTopNode) override { return nullptr; }
231 void schedNode(SUnit *SU, bool IsTopNode) override {}
232 void releaseTopNode(SUnit *SU) override {}
233 void releaseBottomNode(SUnit *SU) override {}
234};
Eugene Zelenkoc8fbf6f2017-08-10 00:46:15 +0000235
236} // end anonymous namespace
Valery Pykhtinfd4c4102017-03-21 13:15:46 +0000237
238GCNIterativeScheduler::GCNIterativeScheduler(MachineSchedContext *C,
239 StrategyKind S)
240 : BaseClass(C, llvm::make_unique<SchedStrategyStub>())
241 , Context(C)
242 , Strategy(S)
243 , UPTracker(*LIS) {
244}
245
246// returns max pressure for a region
247GCNRegPressure
248GCNIterativeScheduler::getRegionPressure(MachineBasicBlock::iterator Begin,
249 MachineBasicBlock::iterator End)
250 const {
251 // For the purpose of pressure tracking bottom inst of the region should
252 // be also processed. End is either BB end, BB terminator inst or sched
253 // boundary inst.
254 auto const BBEnd = Begin->getParent()->end();
255 auto const BottomMI = End == BBEnd ? std::prev(End) : End;
256
257 // scheduleRegions walks bottom to top, so its likely we just get next
258 // instruction to track
259 auto AfterBottomMI = std::next(BottomMI);
260 if (AfterBottomMI == BBEnd ||
261 &*AfterBottomMI != UPTracker.getLastTrackedMI()) {
262 UPTracker.reset(*BottomMI);
263 } else {
264 assert(UPTracker.isValid());
265 }
266
267 for (auto I = BottomMI; I != Begin; --I)
268 UPTracker.recede(*I);
269
270 UPTracker.recede(*Begin);
271
272 assert(UPTracker.isValid() ||
273 (dbgs() << "Tracked region ",
274 printRegion(dbgs(), Begin, End, LIS), false));
275 return UPTracker.moveMaxPressure();
276}
277
278// returns max pressure for a tentative schedule
279template <typename Range> GCNRegPressure
280GCNIterativeScheduler::getSchedulePressure(const Region &R,
281 Range &&Schedule) const {
282 auto const BBEnd = R.Begin->getParent()->end();
283 GCNUpwardRPTracker RPTracker(*LIS);
284 if (R.End != BBEnd) {
285 // R.End points to the boundary instruction but the
286 // schedule doesn't include it
287 RPTracker.reset(*R.End);
288 RPTracker.recede(*R.End);
289 } else {
290 // R.End doesn't point to the boundary instruction
291 RPTracker.reset(*std::prev(BBEnd));
292 }
293 for (auto I = Schedule.end(), B = Schedule.begin(); I != B;) {
294 RPTracker.recede(*getMachineInstr(*--I));
295 }
296 return RPTracker.moveMaxPressure();
297}
298
299void GCNIterativeScheduler::enterRegion(MachineBasicBlock *BB, // overriden
300 MachineBasicBlock::iterator Begin,
301 MachineBasicBlock::iterator End,
302 unsigned NumRegionInstrs) {
303 BaseClass::enterRegion(BB, Begin, End, NumRegionInstrs);
304 if (NumRegionInstrs > 2) {
305 Regions.push_back(
306 new (Alloc.Allocate())
307 Region { Begin, End, NumRegionInstrs,
308 getRegionPressure(Begin, End), nullptr });
309 }
310}
311
312void GCNIterativeScheduler::schedule() { // overriden
313 // do nothing
314 DEBUG(
315 printLivenessInfo(dbgs(), RegionBegin, RegionEnd, LIS);
316 if (!Regions.empty() && Regions.back()->Begin == RegionBegin) {
317 dbgs() << "Max RP: ";
318 Regions.back()->MaxPressure.print(dbgs(), &MF.getSubtarget<SISubtarget>());
319 }
320 dbgs() << '\n';
321 );
322}
323
324void GCNIterativeScheduler::finalizeSchedule() { // overriden
325 if (Regions.empty())
326 return;
327 switch (Strategy) {
328 case SCHEDULE_MINREGONLY: scheduleMinReg(); break;
329 case SCHEDULE_MINREGFORCED: scheduleMinReg(true); break;
330 case SCHEDULE_LEGACYMAXOCCUPANCY: scheduleLegacyMaxOccupancy(); break;
Valery Pykhtinf2fe9722017-11-20 14:35:53 +0000331 case SCHEDULE_ILP: scheduleILP(false); break;
Valery Pykhtinfd4c4102017-03-21 13:15:46 +0000332 }
333}
334
335// Detach schedule from SUnits and interleave it with debug values.
336// Returned schedule becomes independent of DAG state.
337std::vector<MachineInstr*>
338GCNIterativeScheduler::detachSchedule(ScheduleRef Schedule) const {
339 std::vector<MachineInstr*> Res;
340 Res.reserve(Schedule.size() * 2);
341
342 if (FirstDbgValue)
343 Res.push_back(FirstDbgValue);
344
345 const auto DbgB = DbgValues.begin(), DbgE = DbgValues.end();
346 for (auto SU : Schedule) {
347 Res.push_back(SU->getInstr());
348 const auto &D = std::find_if(DbgB, DbgE, [SU](decltype(*DbgB) &P) {
349 return P.second == SU->getInstr();
350 });
351 if (D != DbgE)
352 Res.push_back(D->first);
353 }
354 return Res;
355}
356
357void GCNIterativeScheduler::setBestSchedule(Region &R,
358 ScheduleRef Schedule,
359 const GCNRegPressure &MaxRP) {
360 R.BestSchedule.reset(
361 new TentativeSchedule{ detachSchedule(Schedule), MaxRP });
362}
363
364void GCNIterativeScheduler::scheduleBest(Region &R) {
365 assert(R.BestSchedule.get() && "No schedule specified");
366 scheduleRegion(R, R.BestSchedule->Schedule, R.BestSchedule->MaxPressure);
367 R.BestSchedule.reset();
368}
369
370// minimal required region scheduler, works for ranges of SUnits*,
371// SUnits or MachineIntrs*
372template <typename Range>
373void GCNIterativeScheduler::scheduleRegion(Region &R, Range &&Schedule,
374 const GCNRegPressure &MaxRP) {
375 assert(RegionBegin == R.Begin && RegionEnd == R.End);
376 assert(LIS != nullptr);
377#ifndef NDEBUG
378 const auto SchedMaxRP = getSchedulePressure(R, Schedule);
379#endif
380 auto BB = R.Begin->getParent();
381 auto Top = R.Begin;
382 for (const auto &I : Schedule) {
383 auto MI = getMachineInstr(I);
384 if (MI != &*Top) {
385 BB->remove(MI);
386 BB->insert(Top, MI);
Shiva Chen801bf7e2018-05-09 02:42:00 +0000387 if (!MI->isDebugInstr())
Valery Pykhtinfd4c4102017-03-21 13:15:46 +0000388 LIS->handleMove(*MI, true);
389 }
Shiva Chen801bf7e2018-05-09 02:42:00 +0000390 if (!MI->isDebugInstr()) {
Valery Pykhtinfd4c4102017-03-21 13:15:46 +0000391 // Reset read - undef flags and update them later.
392 for (auto &Op : MI->operands())
393 if (Op.isReg() && Op.isDef())
394 Op.setIsUndef(false);
395
396 RegisterOperands RegOpers;
397 RegOpers.collect(*MI, *TRI, MRI, /*ShouldTrackLaneMasks*/true,
398 /*IgnoreDead*/false);
399 // Adjust liveness and add missing dead+read-undef flags.
400 auto SlotIdx = LIS->getInstructionIndex(*MI).getRegSlot();
401 RegOpers.adjustLaneLiveness(*LIS, MRI, SlotIdx, MI);
402 }
403 Top = std::next(MI->getIterator());
404 }
405 RegionBegin = getMachineInstr(Schedule.front());
406
407 // Schedule consisting of MachineInstr* is considered 'detached'
408 // and already interleaved with debug values
409 if (!std::is_same<decltype(*Schedule.begin()), MachineInstr*>::value) {
410 placeDebugValues();
411 // Unfortunatelly placeDebugValues incorrectly modifies RegionEnd, restore
412 //assert(R.End == RegionEnd);
413 RegionEnd = R.End;
414 }
415
416 R.Begin = RegionBegin;
417 R.MaxPressure = MaxRP;
418
419#ifndef NDEBUG
420 const auto RegionMaxRP = getRegionPressure(R);
421 const auto &ST = MF.getSubtarget<SISubtarget>();
422#endif
423 assert((SchedMaxRP == RegionMaxRP && (MaxRP.empty() || SchedMaxRP == MaxRP))
424 || (dbgs() << "Max RP mismatch!!!\n"
425 "RP for schedule (calculated): ",
426 SchedMaxRP.print(dbgs(), &ST),
427 dbgs() << "RP for schedule (reported): ",
428 MaxRP.print(dbgs(), &ST),
429 dbgs() << "RP after scheduling: ",
430 RegionMaxRP.print(dbgs(), &ST),
431 false));
432}
433
434// Sort recorded regions by pressure - highest at the front
435void GCNIterativeScheduler::sortRegionsByPressure(unsigned TargetOcc) {
436 const auto &ST = MF.getSubtarget<SISubtarget>();
Mandeep Singh Grang860adef2018-03-24 17:15:04 +0000437 llvm::sort(Regions.begin(), Regions.end(),
Valery Pykhtinfd4c4102017-03-21 13:15:46 +0000438 [&ST, TargetOcc](const Region *R1, const Region *R2) {
439 return R2->MaxPressure.less(ST, R1->MaxPressure, TargetOcc);
440 });
441}
442
443///////////////////////////////////////////////////////////////////////////////
444// Legacy MaxOccupancy Strategy
445
446// Tries to increase occupancy applying minreg scheduler for a sequence of
447// most demanding regions. Obtained schedules are saved as BestSchedule for a
448// region.
449// TargetOcc is the best achievable occupancy for a kernel.
450// Returns better occupancy on success or current occupancy on fail.
451// BestSchedules aren't deleted on fail.
452unsigned GCNIterativeScheduler::tryMaximizeOccupancy(unsigned TargetOcc) {
453 // TODO: assert Regions are sorted descending by pressure
454 const auto &ST = MF.getSubtarget<SISubtarget>();
455 const auto Occ = Regions.front()->MaxPressure.getOccupancy(ST);
Hiroshi Inouec8e92452018-01-29 05:17:03 +0000456 DEBUG(dbgs() << "Trying to improve occupancy, target = " << TargetOcc
Valery Pykhtinfd4c4102017-03-21 13:15:46 +0000457 << ", current = " << Occ << '\n');
458
459 auto NewOcc = TargetOcc;
460 for (auto R : Regions) {
461 if (R->MaxPressure.getOccupancy(ST) >= NewOcc)
462 break;
463
464 DEBUG(printRegion(dbgs(), R->Begin, R->End, LIS, 3);
465 printLivenessInfo(dbgs(), R->Begin, R->End, LIS));
466
467 BuildDAG DAG(*R, *this);
468 const auto MinSchedule = makeMinRegSchedule(DAG.getTopRoots(), *this);
469 const auto MaxRP = getSchedulePressure(*R, MinSchedule);
470 DEBUG(dbgs() << "Occupancy improvement attempt:\n";
471 printSchedRP(dbgs(), R->MaxPressure, MaxRP));
472
473 NewOcc = std::min(NewOcc, MaxRP.getOccupancy(ST));
474 if (NewOcc <= Occ)
475 break;
476
477 setBestSchedule(*R, MinSchedule, MaxRP);
478 }
479 DEBUG(dbgs() << "New occupancy = " << NewOcc
480 << ", prev occupancy = " << Occ << '\n');
481 return std::max(NewOcc, Occ);
482}
483
484void GCNIterativeScheduler::scheduleLegacyMaxOccupancy(
485 bool TryMaximizeOccupancy) {
486 const auto &ST = MF.getSubtarget<SISubtarget>();
487 auto TgtOcc = ST.getOccupancyWithLocalMemSize(MF);
488
489 sortRegionsByPressure(TgtOcc);
490 auto Occ = Regions.front()->MaxPressure.getOccupancy(ST);
491
492 if (TryMaximizeOccupancy && Occ < TgtOcc)
493 Occ = tryMaximizeOccupancy(TgtOcc);
494
495 // This is really weird but for some magic scheduling regions twice
496 // gives performance improvement
497 const int NumPasses = Occ < TgtOcc ? 2 : 1;
498
499 TgtOcc = std::min(Occ, TgtOcc);
500 DEBUG(dbgs() << "Scheduling using default scheduler, "
501 "target occupancy = " << TgtOcc << '\n');
502 GCNMaxOccupancySchedStrategy LStrgy(Context);
503
504 for (int I = 0; I < NumPasses; ++I) {
505 // running first pass with TargetOccupancy = 0 mimics previous scheduling
506 // approach and is a performance magic
507 LStrgy.setTargetOccupancy(I == 0 ? 0 : TgtOcc);
508 for (auto R : Regions) {
509 OverrideLegacyStrategy Ovr(*R, LStrgy, *this);
510
511 Ovr.schedule();
512 const auto RP = getRegionPressure(*R);
513 DEBUG(printSchedRP(dbgs(), R->MaxPressure, RP));
514
515 if (RP.getOccupancy(ST) < TgtOcc) {
516 DEBUG(dbgs() << "Didn't fit into target occupancy O" << TgtOcc);
517 if (R->BestSchedule.get() &&
518 R->BestSchedule->MaxPressure.getOccupancy(ST) >= TgtOcc) {
519 DEBUG(dbgs() << ", scheduling minimal register\n");
520 scheduleBest(*R);
521 } else {
522 DEBUG(dbgs() << ", restoring\n");
523 Ovr.restoreOrder();
524 assert(R->MaxPressure.getOccupancy(ST) >= TgtOcc);
525 }
526 }
527 }
528 }
529}
530
531///////////////////////////////////////////////////////////////////////////////
532// Minimal Register Strategy
533
534void GCNIterativeScheduler::scheduleMinReg(bool force) {
535 const auto &ST = MF.getSubtarget<SISubtarget>();
536 const auto TgtOcc = ST.getOccupancyWithLocalMemSize(MF);
537 sortRegionsByPressure(TgtOcc);
538
539 auto MaxPressure = Regions.front()->MaxPressure;
540 for (auto R : Regions) {
541 if (!force && R->MaxPressure.less(ST, MaxPressure, TgtOcc))
542 break;
543
544 BuildDAG DAG(*R, *this);
545 const auto MinSchedule = makeMinRegSchedule(DAG.getTopRoots(), *this);
546
547 const auto RP = getSchedulePressure(*R, MinSchedule);
548 DEBUG(if (R->MaxPressure.less(ST, RP, TgtOcc)) {
549 dbgs() << "\nWarning: Pressure becomes worse after minreg!";
550 printSchedRP(dbgs(), R->MaxPressure, RP);
551 });
552
553 if (!force && MaxPressure.less(ST, RP, TgtOcc))
554 break;
555
556 scheduleRegion(*R, MinSchedule, RP);
557 DEBUG(printSchedResult(dbgs(), R, RP));
558
559 MaxPressure = RP;
560 }
561}
Valery Pykhtinf2fe9722017-11-20 14:35:53 +0000562
563///////////////////////////////////////////////////////////////////////////////
564// ILP scheduler port
565
566void GCNIterativeScheduler::scheduleILP(
567 bool TryMaximizeOccupancy) {
568 const auto &ST = MF.getSubtarget<SISubtarget>();
Stanislav Mekhanoshin7012c242018-05-12 01:41:56 +0000569 const SIMachineFunctionInfo *MFI = MF.getInfo<SIMachineFunctionInfo>();
Valery Pykhtinf2fe9722017-11-20 14:35:53 +0000570 auto TgtOcc = std::min(ST.getOccupancyWithLocalMemSize(MF),
Stanislav Mekhanoshin7012c242018-05-12 01:41:56 +0000571 MFI->getMaxWavesPerEU());
Valery Pykhtinf2fe9722017-11-20 14:35:53 +0000572
573 sortRegionsByPressure(TgtOcc);
574 auto Occ = Regions.front()->MaxPressure.getOccupancy(ST);
575
576 if (TryMaximizeOccupancy && Occ < TgtOcc)
577 Occ = tryMaximizeOccupancy(TgtOcc);
578
579 TgtOcc = std::min(Occ, TgtOcc);
580 DEBUG(dbgs() << "Scheduling using default scheduler, "
581 "target occupancy = " << TgtOcc << '\n');
582
583 for (auto R : Regions) {
584 BuildDAG DAG(*R, *this);
585 const auto ILPSchedule = makeGCNILPScheduler(DAG.getBottomRoots(), *this);
586
587 const auto RP = getSchedulePressure(*R, ILPSchedule);
588 DEBUG(printSchedRP(dbgs(), R->MaxPressure, RP));
589
590 if (RP.getOccupancy(ST) < TgtOcc) {
591 DEBUG(dbgs() << "Didn't fit into target occupancy O" << TgtOcc);
592 if (R->BestSchedule.get() &&
593 R->BestSchedule->MaxPressure.getOccupancy(ST) >= TgtOcc) {
594 DEBUG(dbgs() << ", scheduling minimal register\n");
595 scheduleBest(*R);
596 }
597 } else {
598 scheduleRegion(*R, ILPSchedule, RP);
599 DEBUG(printSchedResult(dbgs(), R, RP));
600 }
601 }
602}