LLVM 24.0.0git
R600MachineScheduler.cpp
Go to the documentation of this file.
1//===-- R600MachineScheduler.cpp - R600 Scheduler Interface -*- C++ -*-----===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9/// \file
10/// R600 Machine Scheduler interface
11//
12//===----------------------------------------------------------------------===//
13
16#include "R600Subtarget.h"
17
18using namespace llvm;
19
20#define DEBUG_TYPE "machine-scheduler"
21
23 assert(dag->hasVRegLiveness() && "R600SchedStrategy needs vreg liveness");
24 DAG = static_cast<ScheduleDAGMILive*>(dag);
25 const R600Subtarget &ST = DAG->MF.getSubtarget<R600Subtarget>();
26 TII = static_cast<const R600InstrInfo *>(DAG->TII);
27 VLIW5 = !ST.hasCaymanISA();
28 MRI = &DAG->MRI;
29 CurInstKind = IDOther;
30 CurEmitted = 0;
31 OccupiedSlotsMask = 31;
32 InstKindLimit[IDAlu] = TII->getMaxAlusPerClause();
33 InstKindLimit[IDOther] = 32;
34 InstKindLimit[IDFetch] = ST.getTexVTXClauseSize();
35 AluInstCount = 0;
36 FetchInstCount = 0;
37}
38
39void R600SchedStrategy::MoveUnits(std::vector<SUnit *> &QSrc,
40 std::vector<SUnit *> &QDst)
41{
42 llvm::append_range(QDst, QSrc);
43 QSrc.clear();
44}
45
46static unsigned getWFCountLimitedByGPR(unsigned GPRCount) {
47 assert (GPRCount && "GPRCount cannot be 0");
48 return 248 / GPRCount;
49}
50
52 SUnit *SU = nullptr;
53 NextInstKind = IDOther;
54
55 IsTopNode = false;
56
57 // check if we might want to switch current clause type
58 bool AllowSwitchToAlu = (CurEmitted >= InstKindLimit[CurInstKind]) ||
59 (Available[CurInstKind].empty());
60 bool AllowSwitchFromAlu = (CurEmitted >= InstKindLimit[CurInstKind]) &&
61 (!Available[IDFetch].empty() || !Available[IDOther].empty());
62
63 if (CurInstKind == IDAlu && !Available[IDFetch].empty()) {
64 // We use the heuristic provided by AMD Accelerated Parallel Processing
65 // OpenCL Programming Guide :
66 // The approx. number of WF that allows TEX inst to hide ALU inst is :
67 // 500 (cycles for TEX) / (AluFetchRatio * 8 (cycles for ALU))
68 float ALUFetchRationEstimate =
69 (AluInstCount + AvailablesAluCount() + Pending[IDAlu].size()) /
70 (FetchInstCount + Available[IDFetch].size());
71 if (ALUFetchRationEstimate == 0) {
72 AllowSwitchFromAlu = true;
73 } else {
74 unsigned NeededWF = 62.5f / ALUFetchRationEstimate;
75 LLVM_DEBUG(dbgs() << NeededWF << " approx. Wavefronts Required\n");
76 // We assume the local GPR requirements to be "dominated" by the requirement
77 // of the TEX clause (which consumes 128 bits regs) ; ALU inst before and
78 // after TEX are indeed likely to consume or generate values from/for the
79 // TEX clause.
80 // Available[IDFetch].size() * 2 : GPRs required in the Fetch clause
81 // We assume that fetch instructions are either TnXYZW = TEX TnXYZW (need
82 // one GPR) or TmXYZW = TnXYZW (need 2 GPR).
83 // (TODO : use RegisterPressure)
84 // If we are going too use too many GPR, we flush Fetch instruction to lower
85 // register pressure on 128 bits regs.
86 unsigned NearRegisterRequirement = 2 * Available[IDFetch].size();
87 if (NeededWF > getWFCountLimitedByGPR(NearRegisterRequirement))
88 AllowSwitchFromAlu = true;
89 }
90 }
91
92 if (!SU && ((AllowSwitchToAlu && CurInstKind != IDAlu) ||
93 (!AllowSwitchFromAlu && CurInstKind == IDAlu))) {
94 // try to pick ALU
95 SU = pickAlu();
96 if (!SU && !PhysicalRegCopy.empty()) {
97 SU = PhysicalRegCopy.front();
98 PhysicalRegCopy.erase(PhysicalRegCopy.begin());
99 }
100 if (SU) {
101 if (CurEmitted >= InstKindLimit[IDAlu])
102 CurEmitted = 0;
103 NextInstKind = IDAlu;
104 }
105 }
106
107 if (!SU) {
108 // try to pick FETCH
109 SU = pickOther(IDFetch);
110 if (SU)
111 NextInstKind = IDFetch;
112 }
113
114 // try to pick other
115 if (!SU) {
116 SU = pickOther(IDOther);
117 if (SU)
118 NextInstKind = IDOther;
119 }
120
121 LLVM_DEBUG(if (SU) {
122 dbgs() << " ** Pick node **\n";
123 DAG->dumpNode(*SU);
124 } else {
125 dbgs() << "NO NODE \n";
126 for (const SUnit &S : DAG->SUnits)
127 if (!S.isScheduled)
128 DAG->dumpNode(S);
129 });
130
131 return SU;
132}
133
134void R600SchedStrategy::schedNode(SUnit *SU, bool IsTopNode) {
135 if (NextInstKind != CurInstKind) {
136 LLVM_DEBUG(dbgs() << "Instruction Type Switch\n");
137 if (NextInstKind != IDAlu)
138 OccupiedSlotsMask |= 31;
139 CurEmitted = 0;
140 CurInstKind = NextInstKind;
141 }
142
143 if (CurInstKind == IDAlu) {
144 AluInstCount ++;
145 switch (getAluKind(SU)) {
146 case AluT_XYZW:
147 CurEmitted += 4;
148 break;
149 case AluDiscarded:
150 break;
151 default: {
152 ++CurEmitted;
154 E = SU->getInstr()->operands_end(); It != E; ++It) {
155 MachineOperand &MO = *It;
156 if (MO.isReg() && MO.getReg() == R600::ALU_LITERAL_X)
157 ++CurEmitted;
158 }
159 }
160 }
161 } else {
162 ++CurEmitted;
163 }
164
165 LLVM_DEBUG(dbgs() << CurEmitted << " Instructions Emitted in this clause\n");
166
167 if (CurInstKind != IDFetch) {
168 MoveUnits(Pending[IDFetch], Available[IDFetch]);
169 } else
170 FetchInstCount++;
171}
172
173static bool
175 if (MI->getOpcode() != R600::COPY)
176 return false;
177
178 return !MI->getOperand(1).getReg().isVirtual();
179}
180
182 LLVM_DEBUG(dbgs() << "Top Releasing "; DAG->dumpNode(*SU));
183}
184
186 LLVM_DEBUG(dbgs() << "Bottom Releasing "; DAG->dumpNode(*SU));
187 if (isPhysicalRegCopy(SU->getInstr())) {
188 PhysicalRegCopy.push_back(SU);
189 return;
190 }
191
192 int IK = getInstKind(SU);
193
194 // There is no export clause, we can schedule one as soon as its ready
195 if (IK == IDOther)
196 Available[IDOther].push_back(SU);
197 else
198 Pending[IK].push_back(SU);
199
200}
201
202bool R600SchedStrategy::regBelongsToClass(Register Reg,
203 const TargetRegisterClass *RC) const {
204 if (!Reg.isVirtual())
205 return RC->contains(Reg);
206 return MRI->getRegClass(Reg) == RC;
207}
208
209R600SchedStrategy::AluKind R600SchedStrategy::getAluKind(SUnit *SU) const {
210 MachineInstr *MI = SU->getInstr();
211
212 if (TII->isTransOnly(*MI))
213 return AluTrans;
214
215 switch (MI->getOpcode()) {
216 case R600::PRED_X:
217 return AluPredX;
218 case R600::INTERP_PAIR_XY:
219 case R600::INTERP_PAIR_ZW:
220 case R600::INTERP_VEC_LOAD:
221 case R600::DOT_4:
222 return AluT_XYZW;
223 case R600::COPY:
224 if (MI->getOperand(1).isUndef()) {
225 // MI will become a KILL, don't considers it in scheduling
226 return AluDiscarded;
227 }
228 break;
229 default:
230 break;
231 }
232
233 // Does the instruction take a whole IG ?
234 // XXX: Is it possible to add a helper function in R600InstrInfo that can
235 // be used here and in R600PacketizerList::isSoloInstruction() ?
236 if(TII->isVector(*MI) ||
237 TII->isCubeOp(MI->getOpcode()) ||
238 TII->isReductionOp(MI->getOpcode()) ||
239 MI->getOpcode() == R600::GROUP_BARRIER) {
240 return AluT_XYZW;
241 }
242
243 if (TII->isLDSInstr(MI->getOpcode())) {
244 return AluT_X;
245 }
246
247 // Is the result already assigned to a channel ?
248 unsigned DestSubReg = MI->getOperand(0).getSubReg();
249 switch (DestSubReg) {
250 case R600::sub0:
251 return AluT_X;
252 case R600::sub1:
253 return AluT_Y;
254 case R600::sub2:
255 return AluT_Z;
256 case R600::sub3:
257 return AluT_W;
258 default:
259 break;
260 }
261
262 // Is the result already member of a X/Y/Z/W class ?
263 Register DestReg = MI->getOperand(0).getReg();
264 if (regBelongsToClass(DestReg, &R600::R600_TReg32_XRegClass) ||
265 regBelongsToClass(DestReg, &R600::R600_AddrRegClass))
266 return AluT_X;
267 if (regBelongsToClass(DestReg, &R600::R600_TReg32_YRegClass))
268 return AluT_Y;
269 if (regBelongsToClass(DestReg, &R600::R600_TReg32_ZRegClass))
270 return AluT_Z;
271 if (regBelongsToClass(DestReg, &R600::R600_TReg32_WRegClass))
272 return AluT_W;
273 if (regBelongsToClass(DestReg, &R600::R600_Reg128RegClass))
274 return AluT_XYZW;
275
276 // LDS src registers cannot be used in the Trans slot.
277 if (TII->readsLDSSrcReg(*MI))
278 return AluT_XYZW;
279
280 return AluAny;
281}
282
283int R600SchedStrategy::getInstKind(SUnit* SU) {
284 int Opcode = SU->getInstr()->getOpcode();
285
286 if (TII->usesTextureCache(Opcode) || TII->usesVertexCache(Opcode))
287 return IDFetch;
288
289 if (TII->isALUInstr(Opcode)) {
290 return IDAlu;
291 }
292
293 switch (Opcode) {
294 case R600::PRED_X:
295 case R600::COPY:
296 case R600::CONST_COPY:
297 case R600::INTERP_PAIR_XY:
298 case R600::INTERP_PAIR_ZW:
299 case R600::INTERP_VEC_LOAD:
300 case R600::DOT_4:
301 return IDAlu;
302 default:
303 return IDOther;
304 }
305}
306
307SUnit *R600SchedStrategy::PopInst(std::vector<SUnit *> &Q, bool AnyALU) {
308 if (Q.empty())
309 return nullptr;
310 for (std::vector<SUnit *>::reverse_iterator It = Q.rbegin(), E = Q.rend();
311 It != E; ++It) {
312 SUnit *SU = *It;
313 InstructionsGroupCandidate.push_back(SU->getInstr());
314 if (TII->fitsConstReadLimitations(InstructionsGroupCandidate) &&
315 (!AnyALU || !TII->isVectorOnly(*SU->getInstr()))) {
316 InstructionsGroupCandidate.pop_back();
317 Q.erase((It + 1).base());
318 return SU;
319 }
320 InstructionsGroupCandidate.pop_back();
321 }
322 return nullptr;
323}
324
325void R600SchedStrategy::LoadAlu() {
326 std::vector<SUnit *> &QSrc = Pending[IDAlu];
327 for (SUnit *SU : QSrc) {
328 AluKind AK = getAluKind(SU);
329 AvailableAlus[AK].push_back(SU);
330 }
331 QSrc.clear();
332}
333
334void R600SchedStrategy::PrepareNextSlot() {
335 LLVM_DEBUG(dbgs() << "New Slot\n");
336 assert(OccupiedSlotsMask && "Slot wasn't filled");
337 OccupiedSlotsMask = 0;
338 // if (HwGen == AMDGPUSubtarget::NORTHERN_ISLANDS)
339 // OccupiedSlotsMask |= 16;
340 InstructionsGroupCandidate.clear();
341 LoadAlu();
342}
343
344void R600SchedStrategy::AssignSlot(MachineInstr* MI, unsigned Slot) {
345 int DstIndex = TII->getOperandIdx(MI->getOpcode(), R600::OpName::dst);
346 if (DstIndex == -1) {
347 return;
348 }
349 Register DestReg = MI->getOperand(DstIndex).getReg();
350 // PressureRegister crashes if an operand is def and used in the same inst
351 // and we try to constraint its regclass
352 for (const MachineOperand &MO : MI->all_uses())
353 if (MO.getReg() == DestReg)
354 return;
355 // Constrains the regclass of DestReg to assign it to Slot
356 switch (Slot) {
357 case 0:
358 MRI->constrainRegClass(DestReg, &R600::R600_TReg32_XRegClass);
359 break;
360 case 1:
361 MRI->constrainRegClass(DestReg, &R600::R600_TReg32_YRegClass);
362 break;
363 case 2:
364 MRI->constrainRegClass(DestReg, &R600::R600_TReg32_ZRegClass);
365 break;
366 case 3:
367 MRI->constrainRegClass(DestReg, &R600::R600_TReg32_WRegClass);
368 break;
369 }
370}
371
372SUnit *R600SchedStrategy::AttemptFillSlot(unsigned Slot, bool AnyAlu) {
373 static const AluKind IndexToID[] = {AluT_X, AluT_Y, AluT_Z, AluT_W};
374 SUnit *SlotedSU = PopInst(AvailableAlus[IndexToID[Slot]], AnyAlu);
375 if (SlotedSU)
376 return SlotedSU;
377 SUnit *UnslotedSU = PopInst(AvailableAlus[AluAny], AnyAlu);
378 if (UnslotedSU)
379 AssignSlot(UnslotedSU->getInstr(), Slot);
380 return UnslotedSU;
381}
382
383unsigned R600SchedStrategy::AvailablesAluCount() const {
384 return AvailableAlus[AluAny].size() + AvailableAlus[AluT_XYZW].size() +
385 AvailableAlus[AluT_X].size() + AvailableAlus[AluT_Y].size() +
386 AvailableAlus[AluT_Z].size() + AvailableAlus[AluT_W].size() +
387 AvailableAlus[AluTrans].size() + AvailableAlus[AluDiscarded].size() +
388 AvailableAlus[AluPredX].size();
389}
390
391SUnit* R600SchedStrategy::pickAlu() {
392 while (AvailablesAluCount() || !Pending[IDAlu].empty()) {
393 if (!OccupiedSlotsMask) {
394 // Bottom up scheduling : predX must comes first
395 if (!AvailableAlus[AluPredX].empty()) {
396 OccupiedSlotsMask |= 31;
397 return PopInst(AvailableAlus[AluPredX], false);
398 }
399 // Flush physical reg copies (RA will discard them)
400 if (!AvailableAlus[AluDiscarded].empty()) {
401 OccupiedSlotsMask |= 31;
402 return PopInst(AvailableAlus[AluDiscarded], false);
403 }
404 // If there is a T_XYZW alu available, use it
405 if (!AvailableAlus[AluT_XYZW].empty()) {
406 OccupiedSlotsMask |= 15;
407 return PopInst(AvailableAlus[AluT_XYZW], false);
408 }
409 }
410 bool TransSlotOccupied = OccupiedSlotsMask & 16;
411 if (!TransSlotOccupied && VLIW5) {
412 if (!AvailableAlus[AluTrans].empty()) {
413 OccupiedSlotsMask |= 16;
414 return PopInst(AvailableAlus[AluTrans], false);
415 }
416 SUnit *SU = AttemptFillSlot(3, true);
417 if (SU) {
418 OccupiedSlotsMask |= 16;
419 return SU;
420 }
421 }
422 for (int Chan = 3; Chan > -1; --Chan) {
423 bool isOccupied = OccupiedSlotsMask & (1 << Chan);
424 if (!isOccupied) {
425 SUnit *SU = AttemptFillSlot(Chan, false);
426 if (SU) {
427 OccupiedSlotsMask |= (1 << Chan);
428 InstructionsGroupCandidate.push_back(SU->getInstr());
429 return SU;
430 }
431 }
432 }
433 PrepareNextSlot();
434 }
435 return nullptr;
436}
437
438SUnit* R600SchedStrategy::pickOther(int QID) {
439 SUnit *SU = nullptr;
440 std::vector<SUnit *> &AQ = Available[QID];
441
442 if (AQ.empty()) {
443 MoveUnits(Pending[QID], AQ);
444 }
445 if (!AQ.empty()) {
446 SU = AQ.back();
447 AQ.pop_back();
448 }
449 return SU;
450}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
const HexagonInstrInfo * TII
IRTranslator LLVM IR MI
Promote Memory to Register
Definition Mem2Reg.cpp:110
Provides R600 specific target descriptions.
static unsigned getWFCountLimitedByGPR(unsigned GPRCount)
static bool isPhysicalRegCopy(MachineInstr *MI)
R600 Machine Scheduler interface.
AMDGPU R600 specific subclass of TargetSubtarget.
MachineRegisterInfo & MRI
#define LLVM_DEBUG(...)
Definition Debug.h:119
bool contains(MCRegister Reg) const
contains - Return true if the specified register is included in this register class.
Representation of each machine instruction.
mop_iterator operands_begin()
unsigned getOpcode() const
Returns the opcode of this MachineInstr.
mop_iterator operands_end()
MachineOperand * mop_iterator
iterator/begin/end - Iterate over all operands of a machine instruction.
MachineOperand class - Representation of each machine instruction operand.
bool isReg() const
isReg - Tests if this is a MO_Register operand.
Register getReg() const
getReg - Returns the register number.
void initialize(ScheduleDAGMI *dag) override
Initialize the strategy after building the DAG for a new region.
SUnit * pickNode(bool &IsTopNode) override
Pick the next node to schedule, or return NULL.
void releaseBottomNode(SUnit *SU) override
When all successor dependencies have been resolved, free this node for bottom-up scheduling.
void releaseTopNode(SUnit *SU) override
When all predecessor dependencies have been resolved, free this node for top-down scheduling.
void schedNode(SUnit *SU, bool IsTopNode) override
Notify MachineSchedStrategy that ScheduleDAGMI has scheduled an instruction and updated scheduled/rem...
Wrapper class representing virtual and physical registers.
Definition Register.h:20
Scheduling unit. This is a node in the scheduling DAG.
bool isScheduled
True once scheduled.
MachineInstr * getInstr() const
Returns the representative MachineInstr for this SUnit.
ScheduleDAGMILive is an implementation of ScheduleDAGInstrs that schedules machine instructions while...
ScheduleDAGMI is an implementation of ScheduleDAGInstrs that simply schedules machine instructions ac...
virtual bool hasVRegLiveness() const
Return true if this DAG supports VReg liveness and RegPressure.
bool empty() const
Definition BasicBlock.h:101
This is an optimization pass for GlobalISel generic memory operations.
auto size(R &&Range, std::enable_if_t< std::is_base_of< std::random_access_iterator_tag, typename std::iterator_traits< decltype(Range.begin())>::iterator_category >::value, void > *=nullptr)
Get the size of a range.
Definition STLExtras.h:1685
void append_range(Container &C, Range &&R)
Wrapper function to append range R to container C.
Definition STLExtras.h:2229
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition Debug.cpp:209
MCRegisterClass TargetRegisterClass
Definition FastISel.h:58