1 //===-- R600MachineScheduler.cpp - R600 Scheduler Interface -*- C++ -*-----===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
11 /// \brief R600 Machine Scheduler interface
13 //===----------------------------------------------------------------------===//
15 #include "R600MachineScheduler.h"
16 #include "AMDGPUSubtarget.h"
17 #include "llvm/CodeGen/MachineRegisterInfo.h"
18 #include "llvm/Pass.h"
19 #include "llvm/IR/LegacyPassManager.h"
20 #include "llvm/Support/raw_ostream.h"
24 #define DEBUG_TYPE "misched"
26 void R600SchedStrategy::initialize(ScheduleDAGMI *dag) {
27 assert(dag->hasVRegLiveness() && "R600SchedStrategy needs vreg liveness");
28 DAG = static_cast<ScheduleDAGMILive*>(dag);
29 const AMDGPUSubtarget &ST = DAG->MF.getSubtarget<AMDGPUSubtarget>();
30 TII = static_cast<const R600InstrInfo*>(DAG->TII);
31 TRI = static_cast<const R600RegisterInfo*>(DAG->TRI);
32 VLIW5 = !ST.hasCaymanISA();
34 CurInstKind = IDOther;
36 OccupedSlotsMask = 31;
37 InstKindLimit[IDAlu] = TII->getMaxAlusPerClause();
38 InstKindLimit[IDOther] = 32;
39 InstKindLimit[IDFetch] = ST.getTexVTXClauseSize();
44 void R600SchedStrategy::MoveUnits(std::vector<SUnit *> &QSrc,
45 std::vector<SUnit *> &QDst)
47 QDst.insert(QDst.end(), QSrc.begin(), QSrc.end());
52 unsigned getWFCountLimitedByGPR(unsigned GPRCount) {
53 assert (GPRCount && "GPRCount cannot be 0");
54 return 248 / GPRCount;
57 SUnit* R600SchedStrategy::pickNode(bool &IsTopNode) {
59 NextInstKind = IDOther;
63 // check if we might want to switch current clause type
64 bool AllowSwitchToAlu = (CurEmitted >= InstKindLimit[CurInstKind]) ||
65 (Available[CurInstKind].empty());
66 bool AllowSwitchFromAlu = (CurEmitted >= InstKindLimit[CurInstKind]) &&
67 (!Available[IDFetch].empty() || !Available[IDOther].empty());
69 if (CurInstKind == IDAlu && !Available[IDFetch].empty()) {
70 // We use the heuristic provided by AMD Accelerated Parallel Processing
71 // OpenCL Programming Guide :
72 // The approx. number of WF that allows TEX inst to hide ALU inst is :
73 // 500 (cycles for TEX) / (AluFetchRatio * 8 (cycles for ALU))
74 float ALUFetchRationEstimate =
75 (AluInstCount + AvailablesAluCount() + Pending[IDAlu].size()) /
76 (FetchInstCount + Available[IDFetch].size());
77 if (ALUFetchRationEstimate == 0) {
78 AllowSwitchFromAlu = true;
80 unsigned NeededWF = 62.5f / ALUFetchRationEstimate;
81 DEBUG( dbgs() << NeededWF << " approx. Wavefronts Required\n" );
82 // We assume the local GPR requirements to be "dominated" by the requirement
83 // of the TEX clause (which consumes 128 bits regs) ; ALU inst before and
84 // after TEX are indeed likely to consume or generate values from/for the
86 // Available[IDFetch].size() * 2 : GPRs required in the Fetch clause
87 // We assume that fetch instructions are either TnXYZW = TEX TnXYZW (need
88 // one GPR) or TmXYZW = TnXYZW (need 2 GPR).
89 // (TODO : use RegisterPressure)
90 // If we are going too use too many GPR, we flush Fetch instruction to lower
91 // register pressure on 128 bits regs.
92 unsigned NearRegisterRequirement = 2 * Available[IDFetch].size();
93 if (NeededWF > getWFCountLimitedByGPR(NearRegisterRequirement))
94 AllowSwitchFromAlu = true;
98 if (!SU && ((AllowSwitchToAlu && CurInstKind != IDAlu) ||
99 (!AllowSwitchFromAlu && CurInstKind == IDAlu))) {
102 if (!SU && !PhysicalRegCopy.empty()) {
103 SU = PhysicalRegCopy.front();
104 PhysicalRegCopy.erase(PhysicalRegCopy.begin());
107 if (CurEmitted >= InstKindLimit[IDAlu])
109 NextInstKind = IDAlu;
115 SU = pickOther(IDFetch);
117 NextInstKind = IDFetch;
122 SU = pickOther(IDOther);
124 NextInstKind = IDOther;
129 dbgs() << " ** Pick node **\n";
132 dbgs() << "NO NODE \n";
133 for (unsigned i = 0; i < DAG->SUnits.size(); i++) {
134 const SUnit &S = DAG->SUnits[i];
144 void R600SchedStrategy::schedNode(SUnit *SU, bool IsTopNode) {
145 if (NextInstKind != CurInstKind) {
146 DEBUG(dbgs() << "Instruction Type Switch\n");
147 if (NextInstKind != IDAlu)
148 OccupedSlotsMask |= 31;
150 CurInstKind = NextInstKind;
153 if (CurInstKind == IDAlu) {
155 switch (getAluKind(SU)) {
163 for (MachineInstr::mop_iterator It = SU->getInstr()->operands_begin(),
164 E = SU->getInstr()->operands_end(); It != E; ++It) {
165 MachineOperand &MO = *It;
166 if (MO.isReg() && MO.getReg() == AMDGPU::ALU_LITERAL_X)
176 DEBUG(dbgs() << CurEmitted << " Instructions Emitted in this clause\n");
178 if (CurInstKind != IDFetch) {
179 MoveUnits(Pending[IDFetch], Available[IDFetch]);
185 isPhysicalRegCopy(MachineInstr *MI) {
186 if (MI->getOpcode() != AMDGPU::COPY)
189 return !TargetRegisterInfo::isVirtualRegister(MI->getOperand(1).getReg());
192 void R600SchedStrategy::releaseTopNode(SUnit *SU) {
193 DEBUG(dbgs() << "Top Releasing ";SU->dump(DAG););
196 void R600SchedStrategy::releaseBottomNode(SUnit *SU) {
197 DEBUG(dbgs() << "Bottom Releasing ";SU->dump(DAG););
198 if (isPhysicalRegCopy(SU->getInstr())) {
199 PhysicalRegCopy.push_back(SU);
203 int IK = getInstKind(SU);
205 // There is no export clause, we can schedule one as soon as its ready
207 Available[IDOther].push_back(SU);
209 Pending[IK].push_back(SU);
213 bool R600SchedStrategy::regBelongsToClass(unsigned Reg,
214 const TargetRegisterClass *RC) const {
215 if (!TargetRegisterInfo::isVirtualRegister(Reg)) {
216 return RC->contains(Reg);
218 return MRI->getRegClass(Reg) == RC;
222 R600SchedStrategy::AluKind R600SchedStrategy::getAluKind(SUnit *SU) const {
223 MachineInstr *MI = SU->getInstr();
225 if (TII->isTransOnly(MI))
228 switch (MI->getOpcode()) {
231 case AMDGPU::INTERP_PAIR_XY:
232 case AMDGPU::INTERP_PAIR_ZW:
233 case AMDGPU::INTERP_VEC_LOAD:
237 if (MI->getOperand(1).isUndef()) {
238 // MI will become a KILL, don't considers it in scheduling
245 // Does the instruction take a whole IG ?
246 // XXX: Is it possible to add a helper function in R600InstrInfo that can
247 // be used here and in R600PacketizerList::isSoloInstruction() ?
248 if(TII->isVector(*MI) ||
249 TII->isCubeOp(MI->getOpcode()) ||
250 TII->isReductionOp(MI->getOpcode()) ||
251 MI->getOpcode() == AMDGPU::GROUP_BARRIER) {
255 if (TII->isLDSInstr(MI->getOpcode())) {
259 // Is the result already assigned to a channel ?
260 unsigned DestSubReg = MI->getOperand(0).getSubReg();
261 switch (DestSubReg) {
274 // Is the result already member of a X/Y/Z/W class ?
275 unsigned DestReg = MI->getOperand(0).getReg();
276 if (regBelongsToClass(DestReg, &AMDGPU::R600_TReg32_XRegClass) ||
277 regBelongsToClass(DestReg, &AMDGPU::R600_AddrRegClass))
279 if (regBelongsToClass(DestReg, &AMDGPU::R600_TReg32_YRegClass))
281 if (regBelongsToClass(DestReg, &AMDGPU::R600_TReg32_ZRegClass))
283 if (regBelongsToClass(DestReg, &AMDGPU::R600_TReg32_WRegClass))
285 if (regBelongsToClass(DestReg, &AMDGPU::R600_Reg128RegClass))
288 // LDS src registers cannot be used in the Trans slot.
289 if (TII->readsLDSSrcReg(MI))
296 int R600SchedStrategy::getInstKind(SUnit* SU) {
297 int Opcode = SU->getInstr()->getOpcode();
299 if (TII->usesTextureCache(Opcode) || TII->usesVertexCache(Opcode))
302 if (TII->isALUInstr(Opcode)) {
309 case AMDGPU::CONST_COPY:
310 case AMDGPU::INTERP_PAIR_XY:
311 case AMDGPU::INTERP_PAIR_ZW:
312 case AMDGPU::INTERP_VEC_LOAD:
320 SUnit *R600SchedStrategy::PopInst(std::vector<SUnit *> &Q, bool AnyALU) {
323 for (std::vector<SUnit *>::reverse_iterator It = Q.rbegin(), E = Q.rend();
326 InstructionsGroupCandidate.push_back(SU->getInstr());
327 if (TII->fitsConstReadLimitations(InstructionsGroupCandidate)
328 && (!AnyALU || !TII->isVectorOnly(SU->getInstr()))
330 InstructionsGroupCandidate.pop_back();
331 Q.erase((It + 1).base());
334 InstructionsGroupCandidate.pop_back();
340 void R600SchedStrategy::LoadAlu() {
341 std::vector<SUnit *> &QSrc = Pending[IDAlu];
342 for (unsigned i = 0, e = QSrc.size(); i < e; ++i) {
343 AluKind AK = getAluKind(QSrc[i]);
344 AvailableAlus[AK].push_back(QSrc[i]);
349 void R600SchedStrategy::PrepareNextSlot() {
350 DEBUG(dbgs() << "New Slot\n");
351 assert (OccupedSlotsMask && "Slot wasn't filled");
352 OccupedSlotsMask = 0;
353 // if (HwGen == AMDGPUSubtarget::NORTHERN_ISLANDS)
354 // OccupedSlotsMask |= 16;
355 InstructionsGroupCandidate.clear();
359 void R600SchedStrategy::AssignSlot(MachineInstr* MI, unsigned Slot) {
360 int DstIndex = TII->getOperandIdx(MI->getOpcode(), AMDGPU::OpName::dst);
361 if (DstIndex == -1) {
364 unsigned DestReg = MI->getOperand(DstIndex).getReg();
365 // PressureRegister crashes if an operand is def and used in the same inst
366 // and we try to constraint its regclass
367 for (MachineInstr::mop_iterator It = MI->operands_begin(),
368 E = MI->operands_end(); It != E; ++It) {
369 MachineOperand &MO = *It;
370 if (MO.isReg() && !MO.isDef() &&
371 MO.getReg() == DestReg)
374 // Constrains the regclass of DestReg to assign it to Slot
377 MRI->constrainRegClass(DestReg, &AMDGPU::R600_TReg32_XRegClass);
380 MRI->constrainRegClass(DestReg, &AMDGPU::R600_TReg32_YRegClass);
383 MRI->constrainRegClass(DestReg, &AMDGPU::R600_TReg32_ZRegClass);
386 MRI->constrainRegClass(DestReg, &AMDGPU::R600_TReg32_WRegClass);
391 SUnit *R600SchedStrategy::AttemptFillSlot(unsigned Slot, bool AnyAlu) {
392 static const AluKind IndexToID[] = {AluT_X, AluT_Y, AluT_Z, AluT_W};
393 SUnit *SlotedSU = PopInst(AvailableAlus[IndexToID[Slot]], AnyAlu);
396 SUnit *UnslotedSU = PopInst(AvailableAlus[AluAny], AnyAlu);
398 AssignSlot(UnslotedSU->getInstr(), Slot);
402 unsigned R600SchedStrategy::AvailablesAluCount() const {
403 return AvailableAlus[AluAny].size() + AvailableAlus[AluT_XYZW].size() +
404 AvailableAlus[AluT_X].size() + AvailableAlus[AluT_Y].size() +
405 AvailableAlus[AluT_Z].size() + AvailableAlus[AluT_W].size() +
406 AvailableAlus[AluTrans].size() + AvailableAlus[AluDiscarded].size() +
407 AvailableAlus[AluPredX].size();
410 SUnit* R600SchedStrategy::pickAlu() {
411 while (AvailablesAluCount() || !Pending[IDAlu].empty()) {
412 if (!OccupedSlotsMask) {
413 // Bottom up scheduling : predX must comes first
414 if (!AvailableAlus[AluPredX].empty()) {
415 OccupedSlotsMask |= 31;
416 return PopInst(AvailableAlus[AluPredX], false);
418 // Flush physical reg copies (RA will discard them)
419 if (!AvailableAlus[AluDiscarded].empty()) {
420 OccupedSlotsMask |= 31;
421 return PopInst(AvailableAlus[AluDiscarded], false);
423 // If there is a T_XYZW alu available, use it
424 if (!AvailableAlus[AluT_XYZW].empty()) {
425 OccupedSlotsMask |= 15;
426 return PopInst(AvailableAlus[AluT_XYZW], false);
429 bool TransSlotOccuped = OccupedSlotsMask & 16;
430 if (!TransSlotOccuped && VLIW5) {
431 if (!AvailableAlus[AluTrans].empty()) {
432 OccupedSlotsMask |= 16;
433 return PopInst(AvailableAlus[AluTrans], false);
435 SUnit *SU = AttemptFillSlot(3, true);
437 OccupedSlotsMask |= 16;
441 for (int Chan = 3; Chan > -1; --Chan) {
442 bool isOccupied = OccupedSlotsMask & (1 << Chan);
444 SUnit *SU = AttemptFillSlot(Chan, false);
446 OccupedSlotsMask |= (1 << Chan);
447 InstructionsGroupCandidate.push_back(SU->getInstr());
457 SUnit* R600SchedStrategy::pickOther(int QID) {
459 std::vector<SUnit *> &AQ = Available[QID];
462 MoveUnits(Pending[QID], AQ);
466 AQ.resize(AQ.size() - 1);