Tom Stellard | 0d23ebe | 2016-08-29 19:42:52 +0000 | [diff] [blame] | 1 | //===-- GCNSchedStrategy.cpp - GCN Scheduler Strategy ---------------------===// |
| 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 | /// \file |
| 11 | /// This contains a MachineSchedStrategy implementation for maximizing wave |
| 12 | /// occupancy on GCN hardware. |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #include "GCNSchedStrategy.h" |
| 16 | #include "AMDGPUSubtarget.h" |
| 17 | #include "SIInstrInfo.h" |
| 18 | #include "SIMachineFunctionInfo.h" |
| 19 | #include "SIRegisterInfo.h" |
| 20 | #include "llvm/CodeGen/RegisterClassInfo.h" |
Stanislav Mekhanoshin | 282e8e4 | 2017-02-28 17:22:39 +0000 | [diff] [blame] | 21 | #include "llvm/Support/MathExtras.h" |
Tom Stellard | 0d23ebe | 2016-08-29 19:42:52 +0000 | [diff] [blame] | 22 | |
Evandro Menezes | 0cd23f56 | 2017-07-11 22:08:28 +0000 | [diff] [blame] | 23 | #define DEBUG_TYPE "machine-scheduler" |
Tom Stellard | 0d23ebe | 2016-08-29 19:42:52 +0000 | [diff] [blame] | 24 | |
| 25 | using namespace llvm; |
| 26 | |
| 27 | GCNMaxOccupancySchedStrategy::GCNMaxOccupancySchedStrategy( |
| 28 | const MachineSchedContext *C) : |
Stanislav Mekhanoshin | 357d3db | 2017-02-28 19:20:33 +0000 | [diff] [blame] | 29 | GenericScheduler(C), TargetOccupancy(0), MF(nullptr) { } |
Tom Stellard | 0d23ebe | 2016-08-29 19:42:52 +0000 | [diff] [blame] | 30 | |
| 31 | static unsigned getMaxWaves(unsigned SGPRs, unsigned VGPRs, |
| 32 | const MachineFunction &MF) { |
| 33 | |
| 34 | const SISubtarget &ST = MF.getSubtarget<SISubtarget>(); |
| 35 | const SIMachineFunctionInfo *MFI = MF.getInfo<SIMachineFunctionInfo>(); |
| 36 | unsigned MinRegOccupancy = std::min(ST.getOccupancyWithNumSGPRs(SGPRs), |
| 37 | ST.getOccupancyWithNumVGPRs(VGPRs)); |
| 38 | return std::min(MinRegOccupancy, |
Stanislav Mekhanoshin | 2b913b1 | 2017-02-01 22:59:50 +0000 | [diff] [blame] | 39 | ST.getOccupancyWithLocalMemSize(MFI->getLDSSize(), |
Matthias Braun | f1caa28 | 2017-12-15 22:22:58 +0000 | [diff] [blame] | 40 | MF.getFunction())); |
Tom Stellard | 0d23ebe | 2016-08-29 19:42:52 +0000 | [diff] [blame] | 41 | } |
| 42 | |
Stanislav Mekhanoshin | 582a523 | 2017-02-15 17:19:50 +0000 | [diff] [blame] | 43 | void GCNMaxOccupancySchedStrategy::initialize(ScheduleDAGMI *DAG) { |
| 44 | GenericScheduler::initialize(DAG); |
| 45 | |
| 46 | const SIRegisterInfo *SRI = static_cast<const SIRegisterInfo*>(TRI); |
| 47 | |
Stanislav Mekhanoshin | 357d3db | 2017-02-28 19:20:33 +0000 | [diff] [blame] | 48 | MF = &DAG->MF; |
| 49 | |
| 50 | const SISubtarget &ST = MF->getSubtarget<SISubtarget>(); |
| 51 | |
Stanislav Mekhanoshin | 582a523 | 2017-02-15 17:19:50 +0000 | [diff] [blame] | 52 | // FIXME: This is also necessary, because some passes that run after |
| 53 | // scheduling and before regalloc increase register pressure. |
| 54 | const int ErrorMargin = 3; |
| 55 | |
| 56 | SGPRExcessLimit = Context->RegClassInfo |
| 57 | ->getNumAllocatableRegs(&AMDGPU::SGPR_32RegClass) - ErrorMargin; |
| 58 | VGPRExcessLimit = Context->RegClassInfo |
| 59 | ->getNumAllocatableRegs(&AMDGPU::VGPR_32RegClass) - ErrorMargin; |
Stanislav Mekhanoshin | 357d3db | 2017-02-28 19:20:33 +0000 | [diff] [blame] | 60 | if (TargetOccupancy) { |
| 61 | SGPRCriticalLimit = ST.getMaxNumSGPRs(TargetOccupancy, true); |
| 62 | VGPRCriticalLimit = ST.getMaxNumVGPRs(TargetOccupancy); |
| 63 | } else { |
| 64 | SGPRCriticalLimit = SRI->getRegPressureSetLimit(DAG->MF, |
| 65 | SRI->getSGPRPressureSet()); |
| 66 | VGPRCriticalLimit = SRI->getRegPressureSetLimit(DAG->MF, |
| 67 | SRI->getVGPRPressureSet()); |
| 68 | } |
| 69 | |
| 70 | SGPRCriticalLimit -= ErrorMargin; |
| 71 | VGPRCriticalLimit -= ErrorMargin; |
Stanislav Mekhanoshin | 582a523 | 2017-02-15 17:19:50 +0000 | [diff] [blame] | 72 | } |
| 73 | |
Tom Stellard | 0d23ebe | 2016-08-29 19:42:52 +0000 | [diff] [blame] | 74 | void GCNMaxOccupancySchedStrategy::initCandidate(SchedCandidate &Cand, SUnit *SU, |
| 75 | bool AtTop, const RegPressureTracker &RPTracker, |
| 76 | const SIRegisterInfo *SRI, |
Stanislav Mekhanoshin | 582a523 | 2017-02-15 17:19:50 +0000 | [diff] [blame] | 77 | unsigned SGPRPressure, |
| 78 | unsigned VGPRPressure) { |
Tom Stellard | 0d23ebe | 2016-08-29 19:42:52 +0000 | [diff] [blame] | 79 | |
| 80 | Cand.SU = SU; |
| 81 | Cand.AtTop = AtTop; |
| 82 | |
| 83 | // getDownwardPressure() and getUpwardPressure() make temporary changes to |
Hiroshi Inoue | 290adb3 | 2018-01-22 05:54:46 +0000 | [diff] [blame] | 84 | // the tracker, so we need to pass those function a non-const copy. |
Tom Stellard | 0d23ebe | 2016-08-29 19:42:52 +0000 | [diff] [blame] | 85 | RegPressureTracker &TempTracker = const_cast<RegPressureTracker&>(RPTracker); |
| 86 | |
| 87 | std::vector<unsigned> Pressure; |
| 88 | std::vector<unsigned> MaxPressure; |
| 89 | |
| 90 | if (AtTop) |
| 91 | TempTracker.getDownwardPressure(SU->getInstr(), Pressure, MaxPressure); |
| 92 | else { |
| 93 | // FIXME: I think for bottom up scheduling, the register pressure is cached |
| 94 | // and can be retrieved by DAG->getPressureDif(SU). |
| 95 | TempTracker.getUpwardPressure(SU->getInstr(), Pressure, MaxPressure); |
| 96 | } |
Matt Arsenault | f3dd863 | 2016-11-01 00:55:14 +0000 | [diff] [blame] | 97 | |
Stanislav Mekhanoshin | 582a523 | 2017-02-15 17:19:50 +0000 | [diff] [blame] | 98 | unsigned NewSGPRPressure = Pressure[SRI->getSGPRPressureSet()]; |
| 99 | unsigned NewVGPRPressure = Pressure[SRI->getVGPRPressureSet()]; |
Tom Stellard | 0d23ebe | 2016-08-29 19:42:52 +0000 | [diff] [blame] | 100 | |
| 101 | // If two instructions increase the pressure of different register sets |
| 102 | // by the same amount, the generic scheduler will prefer to schedule the |
| 103 | // instruction that increases the set with the least amount of registers, |
| 104 | // which in our case would be SGPRs. This is rarely what we want, so |
| 105 | // when we report excess/critical register pressure, we do it either |
| 106 | // only for VGPRs or only for SGPRs. |
| 107 | |
| 108 | // FIXME: Better heuristics to determine whether to prefer SGPRs or VGPRs. |
Stanislav Mekhanoshin | 582a523 | 2017-02-15 17:19:50 +0000 | [diff] [blame] | 109 | const unsigned MaxVGPRPressureInc = 16; |
Tom Stellard | 0d23ebe | 2016-08-29 19:42:52 +0000 | [diff] [blame] | 110 | bool ShouldTrackVGPRs = VGPRPressure + MaxVGPRPressureInc >= VGPRExcessLimit; |
| 111 | bool ShouldTrackSGPRs = !ShouldTrackVGPRs && SGPRPressure >= SGPRExcessLimit; |
| 112 | |
| 113 | |
| 114 | // FIXME: We have to enter REG-EXCESS before we reach the actual threshold |
| 115 | // to increase the likelihood we don't go over the limits. We should improve |
| 116 | // the analysis to look through dependencies to find the path with the least |
| 117 | // register pressure. |
Tom Stellard | 0d23ebe | 2016-08-29 19:42:52 +0000 | [diff] [blame] | 118 | |
| 119 | // We only need to update the RPDelata for instructions that increase |
| 120 | // register pressure. Instructions that decrease or keep reg pressure |
| 121 | // the same will be marked as RegExcess in tryCandidate() when they |
| 122 | // are compared with instructions that increase the register pressure. |
| 123 | if (ShouldTrackVGPRs && NewVGPRPressure >= VGPRExcessLimit) { |
| 124 | Cand.RPDelta.Excess = PressureChange(SRI->getVGPRPressureSet()); |
| 125 | Cand.RPDelta.Excess.setUnitInc(NewVGPRPressure - VGPRExcessLimit); |
| 126 | } |
| 127 | |
| 128 | if (ShouldTrackSGPRs && NewSGPRPressure >= SGPRExcessLimit) { |
| 129 | Cand.RPDelta.Excess = PressureChange(SRI->getSGPRPressureSet()); |
Valery Pykhtin | 75d1de9 | 2017-01-26 10:51:47 +0000 | [diff] [blame] | 130 | Cand.RPDelta.Excess.setUnitInc(NewSGPRPressure - SGPRExcessLimit); |
Tom Stellard | 0d23ebe | 2016-08-29 19:42:52 +0000 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | // Register pressure is considered 'CRITICAL' if it is approaching a value |
| 134 | // that would reduce the wave occupancy for the execution unit. When |
| 135 | // register pressure is 'CRITICAL', increading SGPR and VGPR pressure both |
| 136 | // has the same cost, so we don't need to prefer one over the other. |
| 137 | |
Tom Stellard | 0d23ebe | 2016-08-29 19:42:52 +0000 | [diff] [blame] | 138 | int SGPRDelta = NewSGPRPressure - SGPRCriticalLimit; |
| 139 | int VGPRDelta = NewVGPRPressure - VGPRCriticalLimit; |
| 140 | |
| 141 | if (SGPRDelta >= 0 || VGPRDelta >= 0) { |
| 142 | if (SGPRDelta > VGPRDelta) { |
| 143 | Cand.RPDelta.CriticalMax = PressureChange(SRI->getSGPRPressureSet()); |
| 144 | Cand.RPDelta.CriticalMax.setUnitInc(SGPRDelta); |
| 145 | } else { |
| 146 | Cand.RPDelta.CriticalMax = PressureChange(SRI->getVGPRPressureSet()); |
| 147 | Cand.RPDelta.CriticalMax.setUnitInc(VGPRDelta); |
| 148 | } |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | // This function is mostly cut and pasted from |
| 153 | // GenericScheduler::pickNodeFromQueue() |
| 154 | void GCNMaxOccupancySchedStrategy::pickNodeFromQueue(SchedBoundary &Zone, |
| 155 | const CandPolicy &ZonePolicy, |
| 156 | const RegPressureTracker &RPTracker, |
| 157 | SchedCandidate &Cand) { |
Tom Stellard | 0d23ebe | 2016-08-29 19:42:52 +0000 | [diff] [blame] | 158 | const SIRegisterInfo *SRI = static_cast<const SIRegisterInfo*>(TRI); |
| 159 | ArrayRef<unsigned> Pressure = RPTracker.getRegSetPressureAtPos(); |
| 160 | unsigned SGPRPressure = Pressure[SRI->getSGPRPressureSet()]; |
| 161 | unsigned VGPRPressure = Pressure[SRI->getVGPRPressureSet()]; |
Tom Stellard | 0d23ebe | 2016-08-29 19:42:52 +0000 | [diff] [blame] | 162 | ReadyQueue &Q = Zone.Available; |
| 163 | for (SUnit *SU : Q) { |
| 164 | |
| 165 | SchedCandidate TryCand(ZonePolicy); |
| 166 | initCandidate(TryCand, SU, Zone.isTop(), RPTracker, SRI, |
Stanislav Mekhanoshin | 582a523 | 2017-02-15 17:19:50 +0000 | [diff] [blame] | 167 | SGPRPressure, VGPRPressure); |
Tom Stellard | 0d23ebe | 2016-08-29 19:42:52 +0000 | [diff] [blame] | 168 | // Pass SchedBoundary only when comparing nodes from the same boundary. |
| 169 | SchedBoundary *ZoneArg = Cand.AtTop == TryCand.AtTop ? &Zone : nullptr; |
| 170 | GenericScheduler::tryCandidate(Cand, TryCand, ZoneArg); |
| 171 | if (TryCand.Reason != NoCand) { |
| 172 | // Initialize resource delta if needed in case future heuristics query it. |
| 173 | if (TryCand.ResDelta == SchedResourceDelta()) |
| 174 | TryCand.initResourceDelta(Zone.DAG, SchedModel); |
| 175 | Cand.setBest(TryCand); |
| 176 | } |
| 177 | } |
| 178 | } |
| 179 | |
Tom Stellard | 0d23ebe | 2016-08-29 19:42:52 +0000 | [diff] [blame] | 180 | // This function is mostly cut and pasted from |
| 181 | // GenericScheduler::pickNodeBidirectional() |
| 182 | SUnit *GCNMaxOccupancySchedStrategy::pickNodeBidirectional(bool &IsTopNode) { |
| 183 | // Schedule as far as possible in the direction of no choice. This is most |
| 184 | // efficient, but also provides the best heuristics for CriticalPSets. |
| 185 | if (SUnit *SU = Bot.pickOnlyChoice()) { |
| 186 | IsTopNode = false; |
| 187 | return SU; |
| 188 | } |
| 189 | if (SUnit *SU = Top.pickOnlyChoice()) { |
| 190 | IsTopNode = true; |
| 191 | return SU; |
| 192 | } |
| 193 | // Set the bottom-up policy based on the state of the current bottom zone and |
| 194 | // the instructions outside the zone, including the top zone. |
| 195 | CandPolicy BotPolicy; |
| 196 | setPolicy(BotPolicy, /*IsPostRA=*/false, Bot, &Top); |
| 197 | // Set the top-down policy based on the state of the current top zone and |
| 198 | // the instructions outside the zone, including the bottom zone. |
| 199 | CandPolicy TopPolicy; |
| 200 | setPolicy(TopPolicy, /*IsPostRA=*/false, Top, &Bot); |
| 201 | |
| 202 | // See if BotCand is still valid (because we previously scheduled from Top). |
| 203 | DEBUG(dbgs() << "Picking from Bot:\n"); |
| 204 | if (!BotCand.isValid() || BotCand.SU->isScheduled || |
| 205 | BotCand.Policy != BotPolicy) { |
| 206 | BotCand.reset(CandPolicy()); |
| 207 | pickNodeFromQueue(Bot, BotPolicy, DAG->getBotRPTracker(), BotCand); |
| 208 | assert(BotCand.Reason != NoCand && "failed to find the first candidate"); |
| 209 | } else { |
| 210 | DEBUG(traceCandidate(BotCand)); |
| 211 | } |
| 212 | |
| 213 | // Check if the top Q has a better candidate. |
| 214 | DEBUG(dbgs() << "Picking from Top:\n"); |
| 215 | if (!TopCand.isValid() || TopCand.SU->isScheduled || |
| 216 | TopCand.Policy != TopPolicy) { |
| 217 | TopCand.reset(CandPolicy()); |
| 218 | pickNodeFromQueue(Top, TopPolicy, DAG->getTopRPTracker(), TopCand); |
| 219 | assert(TopCand.Reason != NoCand && "failed to find the first candidate"); |
| 220 | } else { |
| 221 | DEBUG(traceCandidate(TopCand)); |
| 222 | } |
| 223 | |
| 224 | // Pick best from BotCand and TopCand. |
| 225 | DEBUG( |
| 226 | dbgs() << "Top Cand: "; |
Tom Stellard | 0d23ebe | 2016-08-29 19:42:52 +0000 | [diff] [blame] | 227 | traceCandidate(TopCand); |
Stanislav Mekhanoshin | 99be1af | 2017-02-06 23:16:51 +0000 | [diff] [blame] | 228 | dbgs() << "Bot Cand: "; |
| 229 | traceCandidate(BotCand); |
Tom Stellard | 0d23ebe | 2016-08-29 19:42:52 +0000 | [diff] [blame] | 230 | ); |
| 231 | SchedCandidate Cand; |
| 232 | if (TopCand.Reason == BotCand.Reason) { |
| 233 | Cand = BotCand; |
| 234 | GenericSchedulerBase::CandReason TopReason = TopCand.Reason; |
| 235 | TopCand.Reason = NoCand; |
| 236 | GenericScheduler::tryCandidate(Cand, TopCand, nullptr); |
| 237 | if (TopCand.Reason != NoCand) { |
Matt Arsenault | f3dd863 | 2016-11-01 00:55:14 +0000 | [diff] [blame] | 238 | Cand.setBest(TopCand); |
Tom Stellard | 0d23ebe | 2016-08-29 19:42:52 +0000 | [diff] [blame] | 239 | } else { |
| 240 | TopCand.Reason = TopReason; |
| 241 | } |
| 242 | } else { |
| 243 | if (TopCand.Reason == RegExcess && TopCand.RPDelta.Excess.getUnitInc() <= 0) { |
| 244 | Cand = TopCand; |
| 245 | } else if (BotCand.Reason == RegExcess && BotCand.RPDelta.Excess.getUnitInc() <= 0) { |
| 246 | Cand = BotCand; |
| 247 | } else if (TopCand.Reason == RegCritical && TopCand.RPDelta.CriticalMax.getUnitInc() <= 0) { |
| 248 | Cand = TopCand; |
| 249 | } else if (BotCand.Reason == RegCritical && BotCand.RPDelta.CriticalMax.getUnitInc() <= 0) { |
| 250 | Cand = BotCand; |
| 251 | } else { |
Stanislav Mekhanoshin | 79da2a7 | 2017-03-11 00:29:27 +0000 | [diff] [blame] | 252 | if (BotCand.Reason > TopCand.Reason) { |
Tom Stellard | 0d23ebe | 2016-08-29 19:42:52 +0000 | [diff] [blame] | 253 | Cand = TopCand; |
| 254 | } else { |
| 255 | Cand = BotCand; |
| 256 | } |
| 257 | } |
| 258 | } |
| 259 | DEBUG( |
| 260 | dbgs() << "Picking: "; |
| 261 | traceCandidate(Cand); |
| 262 | ); |
| 263 | |
| 264 | IsTopNode = Cand.AtTop; |
| 265 | return Cand.SU; |
| 266 | } |
| 267 | |
| 268 | // This function is mostly cut and pasted from |
| 269 | // GenericScheduler::pickNode() |
| 270 | SUnit *GCNMaxOccupancySchedStrategy::pickNode(bool &IsTopNode) { |
| 271 | if (DAG->top() == DAG->bottom()) { |
| 272 | assert(Top.Available.empty() && Top.Pending.empty() && |
| 273 | Bot.Available.empty() && Bot.Pending.empty() && "ReadyQ garbage"); |
| 274 | return nullptr; |
| 275 | } |
| 276 | SUnit *SU; |
| 277 | do { |
| 278 | if (RegionPolicy.OnlyTopDown) { |
| 279 | SU = Top.pickOnlyChoice(); |
| 280 | if (!SU) { |
| 281 | CandPolicy NoPolicy; |
| 282 | TopCand.reset(NoPolicy); |
| 283 | pickNodeFromQueue(Top, NoPolicy, DAG->getTopRPTracker(), TopCand); |
| 284 | assert(TopCand.Reason != NoCand && "failed to find a candidate"); |
| 285 | SU = TopCand.SU; |
| 286 | } |
| 287 | IsTopNode = true; |
| 288 | } else if (RegionPolicy.OnlyBottomUp) { |
| 289 | SU = Bot.pickOnlyChoice(); |
| 290 | if (!SU) { |
| 291 | CandPolicy NoPolicy; |
| 292 | BotCand.reset(NoPolicy); |
| 293 | pickNodeFromQueue(Bot, NoPolicy, DAG->getBotRPTracker(), BotCand); |
| 294 | assert(BotCand.Reason != NoCand && "failed to find a candidate"); |
| 295 | SU = BotCand.SU; |
| 296 | } |
| 297 | IsTopNode = false; |
| 298 | } else { |
| 299 | SU = pickNodeBidirectional(IsTopNode); |
| 300 | } |
| 301 | } while (SU->isScheduled); |
| 302 | |
| 303 | if (SU->isTopReady()) |
| 304 | Top.removeReady(SU); |
| 305 | if (SU->isBottomReady()) |
| 306 | Bot.removeReady(SU); |
| 307 | |
| 308 | DEBUG(dbgs() << "Scheduling SU(" << SU->NodeNum << ") " << *SU->getInstr()); |
| 309 | return SU; |
| 310 | } |
Stanislav Mekhanoshin | 582a523 | 2017-02-15 17:19:50 +0000 | [diff] [blame] | 311 | |
Stanislav Mekhanoshin | 357d3db | 2017-02-28 19:20:33 +0000 | [diff] [blame] | 312 | GCNScheduleDAGMILive::GCNScheduleDAGMILive(MachineSchedContext *C, |
| 313 | std::unique_ptr<MachineSchedStrategy> S) : |
| 314 | ScheduleDAGMILive(C, std::move(S)), |
| 315 | ST(MF.getSubtarget<SISubtarget>()), |
| 316 | MFI(*MF.getInfo<SIMachineFunctionInfo>()), |
Stanislav Mekhanoshin | 7012c24 | 2018-05-12 01:41:56 +0000 | [diff] [blame^] | 317 | StartingOccupancy(std::min(ST.getOccupancyWithLocalMemSize(MFI.getLDSSize(), |
| 318 | MF.getFunction()), |
| 319 | MFI.getMaxWavesPerEU())), |
Stanislav Mekhanoshin | b108607 | 2017-05-16 16:11:26 +0000 | [diff] [blame] | 320 | MinOccupancy(StartingOccupancy), Stage(0), RegionIdx(0) { |
Stanislav Mekhanoshin | 357d3db | 2017-02-28 19:20:33 +0000 | [diff] [blame] | 321 | |
| 322 | DEBUG(dbgs() << "Starting occupancy is " << StartingOccupancy << ".\n"); |
| 323 | } |
| 324 | |
Stanislav Mekhanoshin | 582a523 | 2017-02-15 17:19:50 +0000 | [diff] [blame] | 325 | void GCNScheduleDAGMILive::schedule() { |
Stanislav Mekhanoshin | b108607 | 2017-05-16 16:11:26 +0000 | [diff] [blame] | 326 | if (Stage == 0) { |
| 327 | // Just record regions at the first pass. |
| 328 | Regions.push_back(std::make_pair(RegionBegin, RegionEnd)); |
| 329 | return; |
| 330 | } |
| 331 | |
Stanislav Mekhanoshin | 582a523 | 2017-02-15 17:19:50 +0000 | [diff] [blame] | 332 | std::vector<MachineInstr*> Unsched; |
| 333 | Unsched.reserve(NumRegionInstrs); |
Matt Arsenault | 9a60c3e | 2017-12-05 03:09:23 +0000 | [diff] [blame] | 334 | for (auto &I : *this) { |
Stanislav Mekhanoshin | 582a523 | 2017-02-15 17:19:50 +0000 | [diff] [blame] | 335 | Unsched.push_back(&I); |
Matt Arsenault | 9a60c3e | 2017-12-05 03:09:23 +0000 | [diff] [blame] | 336 | } |
Stanislav Mekhanoshin | 582a523 | 2017-02-15 17:19:50 +0000 | [diff] [blame] | 337 | |
Stanislav Mekhanoshin | 464cecf | 2017-05-16 15:43:52 +0000 | [diff] [blame] | 338 | GCNRegPressure PressureBefore; |
Stanislav Mekhanoshin | 282e8e4 | 2017-02-28 17:22:39 +0000 | [diff] [blame] | 339 | if (LIS) { |
Stanislav Mekhanoshin | b108607 | 2017-05-16 16:11:26 +0000 | [diff] [blame] | 340 | PressureBefore = Pressure[RegionIdx]; |
Stanislav Mekhanoshin | 464cecf | 2017-05-16 15:43:52 +0000 | [diff] [blame] | 341 | |
Stanislav Mekhanoshin | acca0f5 | 2017-05-16 16:31:45 +0000 | [diff] [blame] | 342 | DEBUG(dbgs() << "Pressure before scheduling:\nRegion live-ins:"; |
| 343 | GCNRPTracker::printLiveRegs(dbgs(), LiveIns[RegionIdx], MRI); |
| 344 | dbgs() << "Region live-in pressure: "; |
| 345 | llvm::getRegPressure(MRI, LiveIns[RegionIdx]).print(dbgs()); |
| 346 | dbgs() << "Region register pressure: "; |
| 347 | PressureBefore.print(dbgs())); |
Stanislav Mekhanoshin | 282e8e4 | 2017-02-28 17:22:39 +0000 | [diff] [blame] | 348 | } |
| 349 | |
Stanislav Mekhanoshin | 582a523 | 2017-02-15 17:19:50 +0000 | [diff] [blame] | 350 | ScheduleDAGMILive::schedule(); |
Stanislav Mekhanoshin | b108607 | 2017-05-16 16:11:26 +0000 | [diff] [blame] | 351 | Regions[RegionIdx] = std::make_pair(RegionBegin, RegionEnd); |
Stanislav Mekhanoshin | b933c3f | 2017-03-28 21:48:54 +0000 | [diff] [blame] | 352 | |
Stanislav Mekhanoshin | 282e8e4 | 2017-02-28 17:22:39 +0000 | [diff] [blame] | 353 | if (!LIS) |
| 354 | return; |
Stanislav Mekhanoshin | 582a523 | 2017-02-15 17:19:50 +0000 | [diff] [blame] | 355 | |
| 356 | // Check the results of scheduling. |
| 357 | GCNMaxOccupancySchedStrategy &S = (GCNMaxOccupancySchedStrategy&)*SchedImpl; |
Stanislav Mekhanoshin | 282e8e4 | 2017-02-28 17:22:39 +0000 | [diff] [blame] | 358 | auto PressureAfter = getRealRegPressure(); |
Stanislav Mekhanoshin | 464cecf | 2017-05-16 15:43:52 +0000 | [diff] [blame] | 359 | |
Stanislav Mekhanoshin | acca0f5 | 2017-05-16 16:31:45 +0000 | [diff] [blame] | 360 | DEBUG(dbgs() << "Pressure after scheduling: "; PressureAfter.print(dbgs())); |
Stanislav Mekhanoshin | 464cecf | 2017-05-16 15:43:52 +0000 | [diff] [blame] | 361 | |
Stanislav Mekhanoshin | 464cecf | 2017-05-16 15:43:52 +0000 | [diff] [blame] | 362 | if (PressureAfter.getSGPRNum() <= S.SGPRCriticalLimit && |
| 363 | PressureAfter.getVGPRNum() <= S.VGPRCriticalLimit) { |
Stanislav Mekhanoshin | b108607 | 2017-05-16 16:11:26 +0000 | [diff] [blame] | 364 | Pressure[RegionIdx] = PressureAfter; |
Stanislav Mekhanoshin | 582a523 | 2017-02-15 17:19:50 +0000 | [diff] [blame] | 365 | DEBUG(dbgs() << "Pressure in desired limits, done.\n"); |
| 366 | return; |
| 367 | } |
Stanislav Mekhanoshin | 464cecf | 2017-05-16 15:43:52 +0000 | [diff] [blame] | 368 | unsigned WavesAfter = getMaxWaves(PressureAfter.getSGPRNum(), |
| 369 | PressureAfter.getVGPRNum(), MF); |
| 370 | unsigned WavesBefore = getMaxWaves(PressureBefore.getSGPRNum(), |
| 371 | PressureBefore.getVGPRNum(), MF); |
Stanislav Mekhanoshin | 7012c24 | 2018-05-12 01:41:56 +0000 | [diff] [blame^] | 372 | WavesAfter = std::min(WavesAfter, MFI.getMaxWavesPerEU()); |
| 373 | WavesBefore = std::min(WavesBefore, MFI.getMaxWavesPerEU()); |
Stanislav Mekhanoshin | 282e8e4 | 2017-02-28 17:22:39 +0000 | [diff] [blame] | 374 | DEBUG(dbgs() << "Occupancy before scheduling: " << WavesBefore << |
| 375 | ", after " << WavesAfter << ".\n"); |
| 376 | |
Stanislav Mekhanoshin | 357d3db | 2017-02-28 19:20:33 +0000 | [diff] [blame] | 377 | // We could not keep current target occupancy because of the just scheduled |
| 378 | // region. Record new occupancy for next scheduling cycle. |
| 379 | unsigned NewOccupancy = std::max(WavesAfter, WavesBefore); |
| 380 | if (NewOccupancy < MinOccupancy) { |
| 381 | MinOccupancy = NewOccupancy; |
| 382 | DEBUG(dbgs() << "Occupancy lowered for the function to " |
| 383 | << MinOccupancy << ".\n"); |
| 384 | } |
| 385 | |
Stanislav Mekhanoshin | b108607 | 2017-05-16 16:11:26 +0000 | [diff] [blame] | 386 | if (WavesAfter >= WavesBefore) { |
| 387 | Pressure[RegionIdx] = PressureAfter; |
Stanislav Mekhanoshin | 582a523 | 2017-02-15 17:19:50 +0000 | [diff] [blame] | 388 | return; |
Stanislav Mekhanoshin | b108607 | 2017-05-16 16:11:26 +0000 | [diff] [blame] | 389 | } |
Stanislav Mekhanoshin | 582a523 | 2017-02-15 17:19:50 +0000 | [diff] [blame] | 390 | |
| 391 | DEBUG(dbgs() << "Attempting to revert scheduling.\n"); |
| 392 | RegionEnd = RegionBegin; |
| 393 | for (MachineInstr *MI : Unsched) { |
Shiva Chen | 801bf7e | 2018-05-09 02:42:00 +0000 | [diff] [blame] | 394 | if (MI->isDebugInstr()) |
Matt Arsenault | 9a60c3e | 2017-12-05 03:09:23 +0000 | [diff] [blame] | 395 | continue; |
| 396 | |
Stanislav Mekhanoshin | 582a523 | 2017-02-15 17:19:50 +0000 | [diff] [blame] | 397 | if (MI->getIterator() != RegionEnd) { |
| 398 | BB->remove(MI); |
| 399 | BB->insert(RegionEnd, MI); |
Shiva Chen | 801bf7e | 2018-05-09 02:42:00 +0000 | [diff] [blame] | 400 | if (!MI->isDebugInstr()) |
Yaxun Liu | c41e2f6 | 2017-12-15 03:56:57 +0000 | [diff] [blame] | 401 | LIS->handleMove(*MI, true); |
Stanislav Mekhanoshin | 080889c | 2017-02-28 16:26:27 +0000 | [diff] [blame] | 402 | } |
| 403 | // Reset read-undef flags and update them later. |
| 404 | for (auto &Op : MI->operands()) |
| 405 | if (Op.isReg() && Op.isDef()) |
| 406 | Op.setIsUndef(false); |
| 407 | RegisterOperands RegOpers; |
| 408 | RegOpers.collect(*MI, *TRI, MRI, ShouldTrackLaneMasks, false); |
Shiva Chen | 801bf7e | 2018-05-09 02:42:00 +0000 | [diff] [blame] | 409 | if (!MI->isDebugInstr()) { |
Yaxun Liu | c41e2f6 | 2017-12-15 03:56:57 +0000 | [diff] [blame] | 410 | if (ShouldTrackLaneMasks) { |
| 411 | // Adjust liveness and add missing dead+read-undef flags. |
| 412 | SlotIndex SlotIdx = LIS->getInstructionIndex(*MI).getRegSlot(); |
| 413 | RegOpers.adjustLaneLiveness(*LIS, MRI, SlotIdx, MI); |
| 414 | } else { |
| 415 | // Adjust for missing dead-def flags. |
| 416 | RegOpers.detectDeadDefs(*MI, *LIS); |
| 417 | } |
Stanislav Mekhanoshin | 582a523 | 2017-02-15 17:19:50 +0000 | [diff] [blame] | 418 | } |
| 419 | RegionEnd = MI->getIterator(); |
| 420 | ++RegionEnd; |
| 421 | DEBUG(dbgs() << "Scheduling " << *MI); |
| 422 | } |
| 423 | RegionBegin = Unsched.front()->getIterator(); |
Stanislav Mekhanoshin | b108607 | 2017-05-16 16:11:26 +0000 | [diff] [blame] | 424 | Regions[RegionIdx] = std::make_pair(RegionBegin, RegionEnd); |
Stanislav Mekhanoshin | 582a523 | 2017-02-15 17:19:50 +0000 | [diff] [blame] | 425 | |
| 426 | placeDebugValues(); |
| 427 | } |
Stanislav Mekhanoshin | 282e8e4 | 2017-02-28 17:22:39 +0000 | [diff] [blame] | 428 | |
Stanislav Mekhanoshin | 464cecf | 2017-05-16 15:43:52 +0000 | [diff] [blame] | 429 | GCNRegPressure GCNScheduleDAGMILive::getRealRegPressure() const { |
| 430 | GCNDownwardRPTracker RPTracker(*LIS); |
Stanislav Mekhanoshin | b108607 | 2017-05-16 16:11:26 +0000 | [diff] [blame] | 431 | RPTracker.advance(begin(), end(), &LiveIns[RegionIdx]); |
Stanislav Mekhanoshin | 464cecf | 2017-05-16 15:43:52 +0000 | [diff] [blame] | 432 | return RPTracker.moveMaxPressure(); |
Stanislav Mekhanoshin | 282e8e4 | 2017-02-28 17:22:39 +0000 | [diff] [blame] | 433 | } |
| 434 | |
Stanislav Mekhanoshin | b108607 | 2017-05-16 16:11:26 +0000 | [diff] [blame] | 435 | void GCNScheduleDAGMILive::computeBlockPressure(const MachineBasicBlock *MBB) { |
| 436 | GCNDownwardRPTracker RPTracker(*LIS); |
Stanislav Mekhanoshin | 357d3db | 2017-02-28 19:20:33 +0000 | [diff] [blame] | 437 | |
Stanislav Mekhanoshin | b108607 | 2017-05-16 16:11:26 +0000 | [diff] [blame] | 438 | // If the block has the only successor then live-ins of that successor are |
| 439 | // live-outs of the current block. We can reuse calculated live set if the |
| 440 | // successor will be sent to scheduling past current block. |
| 441 | const MachineBasicBlock *OnlySucc = nullptr; |
| 442 | if (MBB->succ_size() == 1 && !(*MBB->succ_begin())->empty()) { |
| 443 | SlotIndexes *Ind = LIS->getSlotIndexes(); |
| 444 | if (Ind->getMBBStartIdx(MBB) < Ind->getMBBStartIdx(*MBB->succ_begin())) |
| 445 | OnlySucc = *MBB->succ_begin(); |
Stanislav Mekhanoshin | 357d3db | 2017-02-28 19:20:33 +0000 | [diff] [blame] | 446 | } |
Stanislav Mekhanoshin | b108607 | 2017-05-16 16:11:26 +0000 | [diff] [blame] | 447 | |
| 448 | // Scheduler sends regions from the end of the block upwards. |
| 449 | size_t CurRegion = RegionIdx; |
| 450 | for (size_t E = Regions.size(); CurRegion != E; ++CurRegion) |
| 451 | if (Regions[CurRegion].first->getParent() != MBB) |
| 452 | break; |
| 453 | --CurRegion; |
| 454 | |
| 455 | auto I = MBB->begin(); |
| 456 | auto LiveInIt = MBBLiveIns.find(MBB); |
| 457 | if (LiveInIt != MBBLiveIns.end()) { |
| 458 | auto LiveIn = std::move(LiveInIt->second); |
| 459 | RPTracker.reset(*MBB->begin(), &LiveIn); |
| 460 | MBBLiveIns.erase(LiveInIt); |
| 461 | } else { |
| 462 | I = Regions[CurRegion].first; |
| 463 | RPTracker.reset(*I); |
| 464 | } |
| 465 | |
| 466 | for ( ; ; ) { |
| 467 | I = RPTracker.getNext(); |
| 468 | |
| 469 | if (Regions[CurRegion].first == I) { |
| 470 | LiveIns[CurRegion] = RPTracker.getLiveRegs(); |
| 471 | RPTracker.clearMaxPressure(); |
| 472 | } |
| 473 | |
| 474 | if (Regions[CurRegion].second == I) { |
| 475 | Pressure[CurRegion] = RPTracker.moveMaxPressure(); |
| 476 | if (CurRegion-- == RegionIdx) |
| 477 | break; |
| 478 | } |
| 479 | RPTracker.advanceToNext(); |
| 480 | RPTracker.advanceBeforeNext(); |
| 481 | } |
| 482 | |
| 483 | if (OnlySucc) { |
| 484 | if (I != MBB->end()) { |
| 485 | RPTracker.advanceToNext(); |
| 486 | RPTracker.advance(MBB->end()); |
| 487 | } |
| 488 | RPTracker.reset(*OnlySucc->begin(), &RPTracker.getLiveRegs()); |
| 489 | RPTracker.advanceBeforeNext(); |
| 490 | MBBLiveIns[OnlySucc] = RPTracker.moveLiveRegs(); |
| 491 | } |
| 492 | } |
| 493 | |
| 494 | void GCNScheduleDAGMILive::finalizeSchedule() { |
| 495 | GCNMaxOccupancySchedStrategy &S = (GCNMaxOccupancySchedStrategy&)*SchedImpl; |
| 496 | DEBUG(dbgs() << "All regions recorded, starting actual scheduling.\n"); |
| 497 | |
| 498 | LiveIns.resize(Regions.size()); |
| 499 | Pressure.resize(Regions.size()); |
| 500 | |
| 501 | do { |
| 502 | Stage++; |
| 503 | RegionIdx = 0; |
| 504 | MachineBasicBlock *MBB = nullptr; |
| 505 | |
| 506 | if (Stage > 1) { |
| 507 | // Retry function scheduling if we found resulting occupancy and it is |
| 508 | // lower than used for first pass scheduling. This will give more freedom |
| 509 | // to schedule low register pressure blocks. |
| 510 | // Code is partially copied from MachineSchedulerBase::scheduleRegions(). |
| 511 | |
| 512 | if (!LIS || StartingOccupancy <= MinOccupancy) |
| 513 | break; |
| 514 | |
| 515 | DEBUG(dbgs() |
| 516 | << "Retrying function scheduling with lowest recorded occupancy " |
| 517 | << MinOccupancy << ".\n"); |
| 518 | |
| 519 | S.setTargetOccupancy(MinOccupancy); |
| 520 | } |
| 521 | |
| 522 | for (auto Region : Regions) { |
| 523 | RegionBegin = Region.first; |
| 524 | RegionEnd = Region.second; |
| 525 | |
| 526 | if (RegionBegin->getParent() != MBB) { |
| 527 | if (MBB) finishBlock(); |
| 528 | MBB = RegionBegin->getParent(); |
| 529 | startBlock(MBB); |
| 530 | if (Stage == 1) |
| 531 | computeBlockPressure(MBB); |
| 532 | } |
| 533 | |
| 534 | unsigned NumRegionInstrs = std::distance(begin(), end()); |
| 535 | enterRegion(MBB, begin(), end(), NumRegionInstrs); |
| 536 | |
| 537 | // Skip empty scheduling regions (0 or 1 schedulable instructions). |
| 538 | if (begin() == end() || begin() == std::prev(end())) { |
| 539 | exitRegion(); |
| 540 | continue; |
| 541 | } |
| 542 | |
| 543 | DEBUG(dbgs() << "********** MI Scheduling **********\n"); |
Francis Visoiu Mistrih | 25528d6 | 2017-12-04 17:18:51 +0000 | [diff] [blame] | 544 | DEBUG(dbgs() << MF.getName() << ":" << printMBBReference(*MBB) << " " |
| 545 | << MBB->getName() << "\n From: " << *begin() << " To: "; |
Stanislav Mekhanoshin | b108607 | 2017-05-16 16:11:26 +0000 | [diff] [blame] | 546 | if (RegionEnd != MBB->end()) dbgs() << *RegionEnd; |
| 547 | else dbgs() << "End"; |
| 548 | dbgs() << " RegionInstrs: " << NumRegionInstrs << '\n'); |
| 549 | |
| 550 | schedule(); |
| 551 | |
| 552 | exitRegion(); |
| 553 | ++RegionIdx; |
| 554 | } |
| 555 | finishBlock(); |
| 556 | |
| 557 | } while (Stage < 2); |
Stanislav Mekhanoshin | 282e8e4 | 2017-02-28 17:22:39 +0000 | [diff] [blame] | 558 | } |