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