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