LLVM  5.0.0
HexagonMachineScheduler.h
Go to the documentation of this file.
1 //===-- HexagonMachineScheduler.h - Custom Hexagon MI scheduler. ----===//
2 //
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 //===----------------------------------------------------------------------===//
9 //
10 // Custom Hexagon MI scheduler.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #ifndef LLVM_LIB_TARGET_HEXAGON_HEXAGONMACHINESCHEDULER_H
15 #define LLVM_LIB_TARGET_HEXAGON_HEXAGONMACHINESCHEDULER_H
16 
17 #include "llvm/ADT/PriorityQueue.h"
21 #include "llvm/CodeGen/Passes.h"
27 #include "llvm/Support/Debug.h"
31 
32 using namespace llvm;
33 
34 namespace llvm {
35 
37  /// ResourcesModel - Represents VLIW state.
38  /// Not limited to VLIW targets per se, but assumes
39  /// definition of DFA by a target.
40  DFAPacketizer *ResourcesModel;
41 
42  const TargetSchedModel *SchedModel;
43 
44  /// Local packet/bundle model. Purely
45  /// internal to the MI schedulre at the time.
46  std::vector<SUnit*> Packet;
47 
48  /// Total packets created.
49  unsigned TotalPackets;
50 
51 public:
52  /// Save the last formed packet.
53  std::vector<SUnit*> OldPacket;
54 
55 public:
57  : SchedModel(SM), TotalPackets(0) {
58  ResourcesModel = STI.getInstrInfo()->CreateTargetScheduleState(STI);
59 
60  // This hard requirement could be relaxed,
61  // but for now do not let it proceed.
62  assert(ResourcesModel && "Unimplemented CreateTargetScheduleState.");
63 
64  Packet.resize(SchedModel->getIssueWidth());
65  Packet.clear();
66  OldPacket.resize(SchedModel->getIssueWidth());
67  OldPacket.clear();
68  ResourcesModel->clearResources();
69  }
70 
72  delete ResourcesModel;
73  }
74 
76  Packet.clear();
77  }
78 
79  void resetDFA() {
80  ResourcesModel->clearResources();
81  }
82 
83  void reset() {
84  Packet.clear();
85  ResourcesModel->clearResources();
86  }
87 
88  bool isResourceAvailable(SUnit *SU);
89  bool reserveResources(SUnit *SU);
90  void savePacket();
91  unsigned getTotalPackets() const { return TotalPackets; }
92 
93  bool isInPacket(SUnit *SU) const { return is_contained(Packet, SU); }
94 };
95 
96 /// Extend the standard ScheduleDAGMI to provide more context and override the
97 /// top-level schedule() driver.
99 public:
101  std::unique_ptr<MachineSchedStrategy> S)
102  : ScheduleDAGMILive(C, std::move(S)) {}
103 
104  /// Schedule - This is called back from ScheduleDAGInstrs::Run() when it's
105  /// time to do some work.
106  void schedule() override;
107 };
108 
109 //===----------------------------------------------------------------------===//
110 // ConvergingVLIWScheduler - Implementation of the standard
111 // MachineSchedStrategy.
112 //===----------------------------------------------------------------------===//
113 
114 /// ConvergingVLIWScheduler shrinks the unscheduled zone using heuristics
115 /// to balance the schedule.
117 
118  /// Store the state used by ConvergingVLIWScheduler heuristics, required
119  /// for the lifetime of one invocation of pickNode().
120  struct SchedCandidate {
121  // The best SUnit candidate.
122  SUnit *SU;
123 
124  // Register pressure values for the best candidate.
125  RegPressureDelta RPDelta;
126 
127  // Best scheduling cost.
128  int SCost;
129 
130  SchedCandidate(): SU(nullptr), SCost(0) {}
131  };
132  /// Represent the type of SchedCandidate found within a single queue.
133  enum CandResult {
134  NoCand, NodeOrder, SingleExcess, SingleCritical, SingleMax, MultiPressure,
135  BestCost};
136 
137  /// Each Scheduling boundary is associated with ready queues. It tracks the
138  /// current cycle in whichever direction at has moved, and maintains the state
139  /// of "hazards" and other interlocks at the current cycle.
140  struct VLIWSchedBoundary {
142  const TargetSchedModel *SchedModel;
143 
144  ReadyQueue Available;
145  ReadyQueue Pending;
146  bool CheckPending;
147 
148  ScheduleHazardRecognizer *HazardRec;
149  VLIWResourceModel *ResourceModel;
150 
151  unsigned CurrCycle;
152  unsigned IssueCount;
153 
154  /// MinReadyCycle - Cycle of the soonest available instruction.
155  unsigned MinReadyCycle;
156 
157  // Remember the greatest min operand latency.
158  unsigned MaxMinLatency;
159 
160  /// Pending queues extend the ready queues with the same ID and the
161  /// PendingFlag set.
162  VLIWSchedBoundary(unsigned ID, const Twine &Name):
163  DAG(nullptr), SchedModel(nullptr), Available(ID, Name+".A"),
164  Pending(ID << ConvergingVLIWScheduler::LogMaxQID, Name+".P"),
165  CheckPending(false), HazardRec(nullptr), ResourceModel(nullptr),
166  CurrCycle(0), IssueCount(0),
167  MinReadyCycle(UINT_MAX), MaxMinLatency(0) {}
168 
169  ~VLIWSchedBoundary() {
170  delete ResourceModel;
171  delete HazardRec;
172  }
173 
174  void init(VLIWMachineScheduler *dag, const TargetSchedModel *smodel) {
175  DAG = dag;
176  SchedModel = smodel;
177  IssueCount = 0;
178  }
179 
180  bool isTop() const {
181  return Available.getID() == ConvergingVLIWScheduler::TopQID;
182  }
183 
184  bool checkHazard(SUnit *SU);
185 
186  void releaseNode(SUnit *SU, unsigned ReadyCycle);
187 
188  void bumpCycle();
189 
190  void bumpNode(SUnit *SU);
191 
192  void releasePending();
193 
194  void removeReady(SUnit *SU);
195 
196  SUnit *pickOnlyChoice();
197  };
198 
200  const TargetSchedModel *SchedModel;
201 
202  // State of the top and bottom scheduled instruction boundaries.
203  VLIWSchedBoundary Top;
204  VLIWSchedBoundary Bot;
205 
206 public:
207  /// SUnit::NodeQueueId: 0 (none), 1 (top), 2 (bot), 3 (both)
208  enum {
209  TopQID = 1,
210  BotQID = 2,
212  };
213 
215  : DAG(nullptr), SchedModel(nullptr), Top(TopQID, "TopQ"),
216  Bot(BotQID, "BotQ") {}
217 
218  void initialize(ScheduleDAGMI *dag) override;
219 
220  SUnit *pickNode(bool &IsTopNode) override;
221 
222  void schedNode(SUnit *SU, bool IsTopNode) override;
223 
224  void releaseTopNode(SUnit *SU) override;
225 
226  void releaseBottomNode(SUnit *SU) override;
227 
228  unsigned ReportPackets() {
229  return Top.ResourceModel->getTotalPackets() +
230  Bot.ResourceModel->getTotalPackets();
231  }
232 
233 protected:
234  SUnit *pickNodeBidrectional(bool &IsTopNode);
235 
236  int SchedulingCost(ReadyQueue &Q,
237  SUnit *SU, SchedCandidate &Candidate,
238  RegPressureDelta &Delta, bool verbose);
239 
240  CandResult pickNodeFromQueue(ReadyQueue &Q,
241  const RegPressureTracker &RPTracker,
242  SchedCandidate &Candidate);
243 #ifndef NDEBUG
244  void traceCandidate(const char *Label, const ReadyQueue &Q, SUnit *SU,
245  int Cost, PressureChange P = PressureChange());
246 
247  void readyQueueVerboseDump(const RegPressureTracker &RPTracker,
248  SchedCandidate &Candidate, ReadyQueue &Q);
249 #endif
250 };
251 
252 } // namespace
253 
254 
255 #endif
VLIWResourceModel(const TargetSubtargetInfo &STI, const TargetSchedModel *SM)
void schedule() override
Schedule - This is called back from ScheduleDAGInstrs::Run() when it's time to do some work...
Extend the standard ScheduleDAGMI to provide more context and override the top-level schedule() drive...
void readyQueueVerboseDump(const RegPressureTracker &RPTracker, SchedCandidate &Candidate, ReadyQueue &Q)
int SchedulingCost(ReadyQueue &Q, SUnit *SU, SchedCandidate &Candidate, RegPressureDelta &Delta, bool verbose)
Single point to compute overall scheduling cost.
bool isInPacket(SUnit *SU) const
CandResult pickNodeFromQueue(ReadyQueue &Q, const RegPressureTracker &RPTracker, SchedCandidate &Candidate)
Pick the best candidate from the top queue.
void savePacket()
Save the last formed packet.
ScheduleDAGMI is an implementation of ScheduleDAGInstrs that simply schedules machine instructions ac...
ScheduleDAGMILive is an implementation of ScheduleDAGInstrs that schedules machine instructions while...
SUnit * pickNodeBidrectional(bool &IsTopNode)
Pick the best candidate node from either the top or bottom queue.
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:81
Provide an instruction scheduling machine model to CodeGen passes.
SUnit * pickNode(bool &IsTopNode) override
Pick the best node to balance the schedule. Implements MachineSchedStrategy.
VLIWMachineScheduler(MachineSchedContext *C, std::unique_ptr< MachineSchedStrategy > S)
void releaseTopNode(SUnit *SU) override
When all predecessor dependencies have been resolved, free this node for top-down scheduling...
const char * Name
virtual DFAPacketizer * CreateTargetScheduleState(const TargetSubtargetInfo &) const
Create machine specific model for scheduling.
#define P(N)
initializer< Ty > init(const Ty &Val)
Definition: CommandLine.h:401
Helpers for implementing custom MachineSchedStrategy classes.
HazardRecognizer - This determines whether or not an instruction can be issued this cycle...
Track the current register pressure at some position in the instruction stream, and remember the high...
bool isResourceAvailable(SUnit *SU)
Check if scheduling of this SU is possible in the current packet.
void schedNode(SUnit *SU, bool IsTopNode) override
Update the scheduler's state after scheduling a node.
ConvergingVLIWScheduler shrinks the unscheduled zone using heuristics to balance the schedule...
void traceCandidate(const char *Label, const ReadyQueue &Q, SUnit *SU, int Cost, PressureChange P=PressureChange())
TargetSubtargetInfo - Generic base class for all target subtargets.
#define C
Definition: LargeTest.cpp:25
MachineSchedContext provides enough context from the MachineScheduler pass for the target to instanti...
MachineSchedStrategy - Interface to the scheduling algorithm used by ScheduleDAGMI.
Capture a change in pressure for a single pressure set.
bool reserveResources(SUnit *SU)
Keep track of available resources.
void releaseBottomNode(SUnit *SU) override
When all successor dependencies have been resolved, free this node for bottom-up scheduling.
Store the effects of a change in pressure on things that MI scheduler cares about.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
std::vector< SUnit * > OldPacket
Save the last formed packet.
virtual const TargetInstrInfo * getInstrInfo() const
unsigned getIssueWidth() const
Maximum number of micro-ops that may be scheduled per cycle.
Scheduling unit. This is a node in the scheduling DAG.
Definition: ScheduleDAG.h:247
void initialize(ScheduleDAGMI *dag) override
Initialize the strategy after building the DAG for a new region.
bool is_contained(R &&Range, const E &Element)
Wrapper function around std::find to detect if an element exists in a container.
Definition: STLExtras.h:872