10b57cec5SDimitry Andric //===-- GCNSchedStrategy.cpp - GCN Scheduler Strategy ---------------------===// 20b57cec5SDimitry Andric // 30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 60b57cec5SDimitry Andric // 70b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 80b57cec5SDimitry Andric // 90b57cec5SDimitry Andric /// \file 100b57cec5SDimitry Andric /// This contains a MachineSchedStrategy implementation for maximizing wave 110b57cec5SDimitry Andric /// occupancy on GCN hardware. 120b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 130b57cec5SDimitry Andric 140b57cec5SDimitry Andric #include "GCNSchedStrategy.h" 150b57cec5SDimitry Andric #include "AMDGPUSubtarget.h" 160b57cec5SDimitry Andric #include "SIInstrInfo.h" 170b57cec5SDimitry Andric #include "SIMachineFunctionInfo.h" 180b57cec5SDimitry Andric #include "SIRegisterInfo.h" 19*480093f4SDimitry Andric #include "Utils/AMDGPUBaseInfo.h" 200b57cec5SDimitry Andric #include "llvm/CodeGen/RegisterClassInfo.h" 210b57cec5SDimitry Andric #include "llvm/Support/MathExtras.h" 220b57cec5SDimitry Andric 230b57cec5SDimitry Andric #define DEBUG_TYPE "machine-scheduler" 240b57cec5SDimitry Andric 250b57cec5SDimitry Andric using namespace llvm; 260b57cec5SDimitry Andric 270b57cec5SDimitry Andric GCNMaxOccupancySchedStrategy::GCNMaxOccupancySchedStrategy( 280b57cec5SDimitry Andric const MachineSchedContext *C) : 290b57cec5SDimitry Andric GenericScheduler(C), TargetOccupancy(0), MF(nullptr) { } 300b57cec5SDimitry Andric 310b57cec5SDimitry Andric void GCNMaxOccupancySchedStrategy::initialize(ScheduleDAGMI *DAG) { 320b57cec5SDimitry Andric GenericScheduler::initialize(DAG); 330b57cec5SDimitry Andric 340b57cec5SDimitry Andric const SIRegisterInfo *SRI = static_cast<const SIRegisterInfo*>(TRI); 350b57cec5SDimitry Andric 360b57cec5SDimitry Andric MF = &DAG->MF; 370b57cec5SDimitry Andric 380b57cec5SDimitry Andric const GCNSubtarget &ST = MF->getSubtarget<GCNSubtarget>(); 390b57cec5SDimitry Andric 400b57cec5SDimitry Andric // FIXME: This is also necessary, because some passes that run after 410b57cec5SDimitry Andric // scheduling and before regalloc increase register pressure. 420b57cec5SDimitry Andric const int ErrorMargin = 3; 430b57cec5SDimitry Andric 440b57cec5SDimitry Andric SGPRExcessLimit = Context->RegClassInfo 450b57cec5SDimitry Andric ->getNumAllocatableRegs(&AMDGPU::SGPR_32RegClass) - ErrorMargin; 460b57cec5SDimitry Andric VGPRExcessLimit = Context->RegClassInfo 470b57cec5SDimitry Andric ->getNumAllocatableRegs(&AMDGPU::VGPR_32RegClass) - ErrorMargin; 480b57cec5SDimitry Andric if (TargetOccupancy) { 490b57cec5SDimitry Andric SGPRCriticalLimit = ST.getMaxNumSGPRs(TargetOccupancy, true); 500b57cec5SDimitry Andric VGPRCriticalLimit = ST.getMaxNumVGPRs(TargetOccupancy); 510b57cec5SDimitry Andric } else { 520b57cec5SDimitry Andric SGPRCriticalLimit = SRI->getRegPressureSetLimit(DAG->MF, 530b57cec5SDimitry Andric SRI->getSGPRPressureSet()); 540b57cec5SDimitry Andric VGPRCriticalLimit = SRI->getRegPressureSetLimit(DAG->MF, 550b57cec5SDimitry Andric SRI->getVGPRPressureSet()); 560b57cec5SDimitry Andric } 570b57cec5SDimitry Andric 580b57cec5SDimitry Andric SGPRCriticalLimit -= ErrorMargin; 590b57cec5SDimitry Andric VGPRCriticalLimit -= ErrorMargin; 600b57cec5SDimitry Andric } 610b57cec5SDimitry Andric 620b57cec5SDimitry Andric void GCNMaxOccupancySchedStrategy::initCandidate(SchedCandidate &Cand, SUnit *SU, 630b57cec5SDimitry Andric bool AtTop, const RegPressureTracker &RPTracker, 640b57cec5SDimitry Andric const SIRegisterInfo *SRI, 650b57cec5SDimitry Andric unsigned SGPRPressure, 660b57cec5SDimitry Andric unsigned VGPRPressure) { 670b57cec5SDimitry Andric 680b57cec5SDimitry Andric Cand.SU = SU; 690b57cec5SDimitry Andric Cand.AtTop = AtTop; 700b57cec5SDimitry Andric 710b57cec5SDimitry Andric // getDownwardPressure() and getUpwardPressure() make temporary changes to 720b57cec5SDimitry Andric // the tracker, so we need to pass those function a non-const copy. 730b57cec5SDimitry Andric RegPressureTracker &TempTracker = const_cast<RegPressureTracker&>(RPTracker); 740b57cec5SDimitry Andric 758bcb0991SDimitry Andric Pressure.clear(); 768bcb0991SDimitry Andric MaxPressure.clear(); 770b57cec5SDimitry Andric 780b57cec5SDimitry Andric if (AtTop) 790b57cec5SDimitry Andric TempTracker.getDownwardPressure(SU->getInstr(), Pressure, MaxPressure); 800b57cec5SDimitry Andric else { 810b57cec5SDimitry Andric // FIXME: I think for bottom up scheduling, the register pressure is cached 820b57cec5SDimitry Andric // and can be retrieved by DAG->getPressureDif(SU). 830b57cec5SDimitry Andric TempTracker.getUpwardPressure(SU->getInstr(), Pressure, MaxPressure); 840b57cec5SDimitry Andric } 850b57cec5SDimitry Andric 860b57cec5SDimitry Andric unsigned NewSGPRPressure = Pressure[SRI->getSGPRPressureSet()]; 870b57cec5SDimitry Andric unsigned NewVGPRPressure = Pressure[SRI->getVGPRPressureSet()]; 880b57cec5SDimitry Andric 890b57cec5SDimitry Andric // If two instructions increase the pressure of different register sets 900b57cec5SDimitry Andric // by the same amount, the generic scheduler will prefer to schedule the 910b57cec5SDimitry Andric // instruction that increases the set with the least amount of registers, 920b57cec5SDimitry Andric // which in our case would be SGPRs. This is rarely what we want, so 930b57cec5SDimitry Andric // when we report excess/critical register pressure, we do it either 940b57cec5SDimitry Andric // only for VGPRs or only for SGPRs. 950b57cec5SDimitry Andric 960b57cec5SDimitry Andric // FIXME: Better heuristics to determine whether to prefer SGPRs or VGPRs. 970b57cec5SDimitry Andric const unsigned MaxVGPRPressureInc = 16; 980b57cec5SDimitry Andric bool ShouldTrackVGPRs = VGPRPressure + MaxVGPRPressureInc >= VGPRExcessLimit; 990b57cec5SDimitry Andric bool ShouldTrackSGPRs = !ShouldTrackVGPRs && SGPRPressure >= SGPRExcessLimit; 1000b57cec5SDimitry Andric 1010b57cec5SDimitry Andric 1020b57cec5SDimitry Andric // FIXME: We have to enter REG-EXCESS before we reach the actual threshold 1030b57cec5SDimitry Andric // to increase the likelihood we don't go over the limits. We should improve 1040b57cec5SDimitry Andric // the analysis to look through dependencies to find the path with the least 1050b57cec5SDimitry Andric // register pressure. 1060b57cec5SDimitry Andric 1078bcb0991SDimitry Andric // We only need to update the RPDelta for instructions that increase register 1088bcb0991SDimitry Andric // pressure. Instructions that decrease or keep reg pressure the same will be 1098bcb0991SDimitry Andric // marked as RegExcess in tryCandidate() when they are compared with 1108bcb0991SDimitry Andric // instructions that increase the register pressure. 1110b57cec5SDimitry Andric if (ShouldTrackVGPRs && NewVGPRPressure >= VGPRExcessLimit) { 1120b57cec5SDimitry Andric Cand.RPDelta.Excess = PressureChange(SRI->getVGPRPressureSet()); 1130b57cec5SDimitry Andric Cand.RPDelta.Excess.setUnitInc(NewVGPRPressure - VGPRExcessLimit); 1140b57cec5SDimitry Andric } 1150b57cec5SDimitry Andric 1160b57cec5SDimitry Andric if (ShouldTrackSGPRs && NewSGPRPressure >= SGPRExcessLimit) { 1170b57cec5SDimitry Andric Cand.RPDelta.Excess = PressureChange(SRI->getSGPRPressureSet()); 1180b57cec5SDimitry Andric Cand.RPDelta.Excess.setUnitInc(NewSGPRPressure - SGPRExcessLimit); 1190b57cec5SDimitry Andric } 1200b57cec5SDimitry Andric 1210b57cec5SDimitry Andric // Register pressure is considered 'CRITICAL' if it is approaching a value 1220b57cec5SDimitry Andric // that would reduce the wave occupancy for the execution unit. When 1230b57cec5SDimitry Andric // register pressure is 'CRITICAL', increading SGPR and VGPR pressure both 1240b57cec5SDimitry Andric // has the same cost, so we don't need to prefer one over the other. 1250b57cec5SDimitry Andric 1260b57cec5SDimitry Andric int SGPRDelta = NewSGPRPressure - SGPRCriticalLimit; 1270b57cec5SDimitry Andric int VGPRDelta = NewVGPRPressure - VGPRCriticalLimit; 1280b57cec5SDimitry Andric 1290b57cec5SDimitry Andric if (SGPRDelta >= 0 || VGPRDelta >= 0) { 1300b57cec5SDimitry Andric if (SGPRDelta > VGPRDelta) { 1310b57cec5SDimitry Andric Cand.RPDelta.CriticalMax = PressureChange(SRI->getSGPRPressureSet()); 1320b57cec5SDimitry Andric Cand.RPDelta.CriticalMax.setUnitInc(SGPRDelta); 1330b57cec5SDimitry Andric } else { 1340b57cec5SDimitry Andric Cand.RPDelta.CriticalMax = PressureChange(SRI->getVGPRPressureSet()); 1350b57cec5SDimitry Andric Cand.RPDelta.CriticalMax.setUnitInc(VGPRDelta); 1360b57cec5SDimitry Andric } 1370b57cec5SDimitry Andric } 1380b57cec5SDimitry Andric } 1390b57cec5SDimitry Andric 1400b57cec5SDimitry Andric // This function is mostly cut and pasted from 1410b57cec5SDimitry Andric // GenericScheduler::pickNodeFromQueue() 1420b57cec5SDimitry Andric void GCNMaxOccupancySchedStrategy::pickNodeFromQueue(SchedBoundary &Zone, 1430b57cec5SDimitry Andric const CandPolicy &ZonePolicy, 1440b57cec5SDimitry Andric const RegPressureTracker &RPTracker, 1450b57cec5SDimitry Andric SchedCandidate &Cand) { 1460b57cec5SDimitry Andric const SIRegisterInfo *SRI = static_cast<const SIRegisterInfo*>(TRI); 1470b57cec5SDimitry Andric ArrayRef<unsigned> Pressure = RPTracker.getRegSetPressureAtPos(); 1480b57cec5SDimitry Andric unsigned SGPRPressure = Pressure[SRI->getSGPRPressureSet()]; 1490b57cec5SDimitry Andric unsigned VGPRPressure = Pressure[SRI->getVGPRPressureSet()]; 1500b57cec5SDimitry Andric ReadyQueue &Q = Zone.Available; 1510b57cec5SDimitry Andric for (SUnit *SU : Q) { 1520b57cec5SDimitry Andric 1530b57cec5SDimitry Andric SchedCandidate TryCand(ZonePolicy); 1540b57cec5SDimitry Andric initCandidate(TryCand, SU, Zone.isTop(), RPTracker, SRI, 1550b57cec5SDimitry Andric SGPRPressure, VGPRPressure); 1560b57cec5SDimitry Andric // Pass SchedBoundary only when comparing nodes from the same boundary. 1570b57cec5SDimitry Andric SchedBoundary *ZoneArg = Cand.AtTop == TryCand.AtTop ? &Zone : nullptr; 1580b57cec5SDimitry Andric GenericScheduler::tryCandidate(Cand, TryCand, ZoneArg); 1590b57cec5SDimitry Andric if (TryCand.Reason != NoCand) { 1600b57cec5SDimitry Andric // Initialize resource delta if needed in case future heuristics query it. 1610b57cec5SDimitry Andric if (TryCand.ResDelta == SchedResourceDelta()) 1620b57cec5SDimitry Andric TryCand.initResourceDelta(Zone.DAG, SchedModel); 1630b57cec5SDimitry Andric Cand.setBest(TryCand); 1648bcb0991SDimitry Andric LLVM_DEBUG(traceCandidate(Cand)); 1650b57cec5SDimitry Andric } 1660b57cec5SDimitry Andric } 1670b57cec5SDimitry Andric } 1680b57cec5SDimitry Andric 1690b57cec5SDimitry Andric // This function is mostly cut and pasted from 1700b57cec5SDimitry Andric // GenericScheduler::pickNodeBidirectional() 1710b57cec5SDimitry Andric SUnit *GCNMaxOccupancySchedStrategy::pickNodeBidirectional(bool &IsTopNode) { 1720b57cec5SDimitry Andric // Schedule as far as possible in the direction of no choice. This is most 1730b57cec5SDimitry Andric // efficient, but also provides the best heuristics for CriticalPSets. 1740b57cec5SDimitry Andric if (SUnit *SU = Bot.pickOnlyChoice()) { 1750b57cec5SDimitry Andric IsTopNode = false; 1760b57cec5SDimitry Andric return SU; 1770b57cec5SDimitry Andric } 1780b57cec5SDimitry Andric if (SUnit *SU = Top.pickOnlyChoice()) { 1790b57cec5SDimitry Andric IsTopNode = true; 1800b57cec5SDimitry Andric return SU; 1810b57cec5SDimitry Andric } 1820b57cec5SDimitry Andric // Set the bottom-up policy based on the state of the current bottom zone and 1830b57cec5SDimitry Andric // the instructions outside the zone, including the top zone. 1840b57cec5SDimitry Andric CandPolicy BotPolicy; 1850b57cec5SDimitry Andric setPolicy(BotPolicy, /*IsPostRA=*/false, Bot, &Top); 1860b57cec5SDimitry Andric // Set the top-down policy based on the state of the current top zone and 1870b57cec5SDimitry Andric // the instructions outside the zone, including the bottom zone. 1880b57cec5SDimitry Andric CandPolicy TopPolicy; 1890b57cec5SDimitry Andric setPolicy(TopPolicy, /*IsPostRA=*/false, Top, &Bot); 1900b57cec5SDimitry Andric 1910b57cec5SDimitry Andric // See if BotCand is still valid (because we previously scheduled from Top). 1920b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "Picking from Bot:\n"); 1930b57cec5SDimitry Andric if (!BotCand.isValid() || BotCand.SU->isScheduled || 1940b57cec5SDimitry Andric BotCand.Policy != BotPolicy) { 1950b57cec5SDimitry Andric BotCand.reset(CandPolicy()); 1960b57cec5SDimitry Andric pickNodeFromQueue(Bot, BotPolicy, DAG->getBotRPTracker(), BotCand); 1970b57cec5SDimitry Andric assert(BotCand.Reason != NoCand && "failed to find the first candidate"); 1980b57cec5SDimitry Andric } else { 1990b57cec5SDimitry Andric LLVM_DEBUG(traceCandidate(BotCand)); 2008bcb0991SDimitry Andric #ifndef NDEBUG 2018bcb0991SDimitry Andric if (VerifyScheduling) { 2028bcb0991SDimitry Andric SchedCandidate TCand; 2038bcb0991SDimitry Andric TCand.reset(CandPolicy()); 2048bcb0991SDimitry Andric pickNodeFromQueue(Bot, BotPolicy, DAG->getBotRPTracker(), TCand); 2058bcb0991SDimitry Andric assert(TCand.SU == BotCand.SU && 2068bcb0991SDimitry Andric "Last pick result should correspond to re-picking right now"); 2078bcb0991SDimitry Andric } 2088bcb0991SDimitry Andric #endif 2090b57cec5SDimitry Andric } 2100b57cec5SDimitry Andric 2110b57cec5SDimitry Andric // Check if the top Q has a better candidate. 2120b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "Picking from Top:\n"); 2130b57cec5SDimitry Andric if (!TopCand.isValid() || TopCand.SU->isScheduled || 2140b57cec5SDimitry Andric TopCand.Policy != TopPolicy) { 2150b57cec5SDimitry Andric TopCand.reset(CandPolicy()); 2160b57cec5SDimitry Andric pickNodeFromQueue(Top, TopPolicy, DAG->getTopRPTracker(), TopCand); 2170b57cec5SDimitry Andric assert(TopCand.Reason != NoCand && "failed to find the first candidate"); 2180b57cec5SDimitry Andric } else { 2190b57cec5SDimitry Andric LLVM_DEBUG(traceCandidate(TopCand)); 2208bcb0991SDimitry Andric #ifndef NDEBUG 2218bcb0991SDimitry Andric if (VerifyScheduling) { 2228bcb0991SDimitry Andric SchedCandidate TCand; 2238bcb0991SDimitry Andric TCand.reset(CandPolicy()); 2248bcb0991SDimitry Andric pickNodeFromQueue(Top, TopPolicy, DAG->getTopRPTracker(), TCand); 2258bcb0991SDimitry Andric assert(TCand.SU == TopCand.SU && 2268bcb0991SDimitry Andric "Last pick result should correspond to re-picking right now"); 2278bcb0991SDimitry Andric } 2288bcb0991SDimitry Andric #endif 2290b57cec5SDimitry Andric } 2300b57cec5SDimitry Andric 2310b57cec5SDimitry Andric // Pick best from BotCand and TopCand. 2320b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "Top Cand: "; traceCandidate(TopCand); 2330b57cec5SDimitry Andric dbgs() << "Bot Cand: "; traceCandidate(BotCand);); 2340b57cec5SDimitry Andric SchedCandidate Cand; 2350b57cec5SDimitry Andric if (TopCand.Reason == BotCand.Reason) { 2360b57cec5SDimitry Andric Cand = BotCand; 2370b57cec5SDimitry Andric GenericSchedulerBase::CandReason TopReason = TopCand.Reason; 2380b57cec5SDimitry Andric TopCand.Reason = NoCand; 2390b57cec5SDimitry Andric GenericScheduler::tryCandidate(Cand, TopCand, nullptr); 2400b57cec5SDimitry Andric if (TopCand.Reason != NoCand) { 2410b57cec5SDimitry Andric Cand.setBest(TopCand); 2420b57cec5SDimitry Andric } else { 2430b57cec5SDimitry Andric TopCand.Reason = TopReason; 2440b57cec5SDimitry Andric } 2450b57cec5SDimitry Andric } else { 2460b57cec5SDimitry Andric if (TopCand.Reason == RegExcess && TopCand.RPDelta.Excess.getUnitInc() <= 0) { 2470b57cec5SDimitry Andric Cand = TopCand; 2480b57cec5SDimitry Andric } else if (BotCand.Reason == RegExcess && BotCand.RPDelta.Excess.getUnitInc() <= 0) { 2490b57cec5SDimitry Andric Cand = BotCand; 2500b57cec5SDimitry Andric } else if (TopCand.Reason == RegCritical && TopCand.RPDelta.CriticalMax.getUnitInc() <= 0) { 2510b57cec5SDimitry Andric Cand = TopCand; 2520b57cec5SDimitry Andric } else if (BotCand.Reason == RegCritical && BotCand.RPDelta.CriticalMax.getUnitInc() <= 0) { 2530b57cec5SDimitry Andric Cand = BotCand; 2540b57cec5SDimitry Andric } else { 2550b57cec5SDimitry Andric if (BotCand.Reason > TopCand.Reason) { 2560b57cec5SDimitry Andric Cand = TopCand; 2570b57cec5SDimitry Andric } else { 2580b57cec5SDimitry Andric Cand = BotCand; 2590b57cec5SDimitry Andric } 2600b57cec5SDimitry Andric } 2610b57cec5SDimitry Andric } 2620b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "Picking: "; traceCandidate(Cand);); 2630b57cec5SDimitry Andric 2640b57cec5SDimitry Andric IsTopNode = Cand.AtTop; 2650b57cec5SDimitry Andric return Cand.SU; 2660b57cec5SDimitry Andric } 2670b57cec5SDimitry Andric 2680b57cec5SDimitry Andric // This function is mostly cut and pasted from 2690b57cec5SDimitry Andric // GenericScheduler::pickNode() 2700b57cec5SDimitry Andric SUnit *GCNMaxOccupancySchedStrategy::pickNode(bool &IsTopNode) { 2710b57cec5SDimitry Andric if (DAG->top() == DAG->bottom()) { 2720b57cec5SDimitry Andric assert(Top.Available.empty() && Top.Pending.empty() && 2730b57cec5SDimitry Andric Bot.Available.empty() && Bot.Pending.empty() && "ReadyQ garbage"); 2740b57cec5SDimitry Andric return nullptr; 2750b57cec5SDimitry Andric } 2760b57cec5SDimitry Andric SUnit *SU; 2770b57cec5SDimitry Andric do { 2780b57cec5SDimitry Andric if (RegionPolicy.OnlyTopDown) { 2790b57cec5SDimitry Andric SU = Top.pickOnlyChoice(); 2800b57cec5SDimitry Andric if (!SU) { 2810b57cec5SDimitry Andric CandPolicy NoPolicy; 2820b57cec5SDimitry Andric TopCand.reset(NoPolicy); 2830b57cec5SDimitry Andric pickNodeFromQueue(Top, NoPolicy, DAG->getTopRPTracker(), TopCand); 2840b57cec5SDimitry Andric assert(TopCand.Reason != NoCand && "failed to find a candidate"); 2850b57cec5SDimitry Andric SU = TopCand.SU; 2860b57cec5SDimitry Andric } 2870b57cec5SDimitry Andric IsTopNode = true; 2880b57cec5SDimitry Andric } else if (RegionPolicy.OnlyBottomUp) { 2890b57cec5SDimitry Andric SU = Bot.pickOnlyChoice(); 2900b57cec5SDimitry Andric if (!SU) { 2910b57cec5SDimitry Andric CandPolicy NoPolicy; 2920b57cec5SDimitry Andric BotCand.reset(NoPolicy); 2930b57cec5SDimitry Andric pickNodeFromQueue(Bot, NoPolicy, DAG->getBotRPTracker(), BotCand); 2940b57cec5SDimitry Andric assert(BotCand.Reason != NoCand && "failed to find a candidate"); 2950b57cec5SDimitry Andric SU = BotCand.SU; 2960b57cec5SDimitry Andric } 2970b57cec5SDimitry Andric IsTopNode = false; 2980b57cec5SDimitry Andric } else { 2990b57cec5SDimitry Andric SU = pickNodeBidirectional(IsTopNode); 3000b57cec5SDimitry Andric } 3010b57cec5SDimitry Andric } while (SU->isScheduled); 3020b57cec5SDimitry Andric 3030b57cec5SDimitry Andric if (SU->isTopReady()) 3040b57cec5SDimitry Andric Top.removeReady(SU); 3050b57cec5SDimitry Andric if (SU->isBottomReady()) 3060b57cec5SDimitry Andric Bot.removeReady(SU); 3070b57cec5SDimitry Andric 3080b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "Scheduling SU(" << SU->NodeNum << ") " 3090b57cec5SDimitry Andric << *SU->getInstr()); 3100b57cec5SDimitry Andric return SU; 3110b57cec5SDimitry Andric } 3120b57cec5SDimitry Andric 3130b57cec5SDimitry Andric GCNScheduleDAGMILive::GCNScheduleDAGMILive(MachineSchedContext *C, 3140b57cec5SDimitry Andric std::unique_ptr<MachineSchedStrategy> S) : 3150b57cec5SDimitry Andric ScheduleDAGMILive(C, std::move(S)), 3160b57cec5SDimitry Andric ST(MF.getSubtarget<GCNSubtarget>()), 3170b57cec5SDimitry Andric MFI(*MF.getInfo<SIMachineFunctionInfo>()), 3180b57cec5SDimitry Andric StartingOccupancy(MFI.getOccupancy()), 3190b57cec5SDimitry Andric MinOccupancy(StartingOccupancy), Stage(0), RegionIdx(0) { 3200b57cec5SDimitry Andric 3210b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "Starting occupancy is " << StartingOccupancy << ".\n"); 3220b57cec5SDimitry Andric } 3230b57cec5SDimitry Andric 3240b57cec5SDimitry Andric void GCNScheduleDAGMILive::schedule() { 3250b57cec5SDimitry Andric if (Stage == 0) { 3260b57cec5SDimitry Andric // Just record regions at the first pass. 3270b57cec5SDimitry Andric Regions.push_back(std::make_pair(RegionBegin, RegionEnd)); 3280b57cec5SDimitry Andric return; 3290b57cec5SDimitry Andric } 3300b57cec5SDimitry Andric 3310b57cec5SDimitry Andric std::vector<MachineInstr*> Unsched; 3320b57cec5SDimitry Andric Unsched.reserve(NumRegionInstrs); 3330b57cec5SDimitry Andric for (auto &I : *this) { 3340b57cec5SDimitry Andric Unsched.push_back(&I); 3350b57cec5SDimitry Andric } 3360b57cec5SDimitry Andric 3370b57cec5SDimitry Andric GCNRegPressure PressureBefore; 3380b57cec5SDimitry Andric if (LIS) { 3390b57cec5SDimitry Andric PressureBefore = Pressure[RegionIdx]; 3400b57cec5SDimitry Andric 3410b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "Pressure before scheduling:\nRegion live-ins:"; 3420b57cec5SDimitry Andric GCNRPTracker::printLiveRegs(dbgs(), LiveIns[RegionIdx], MRI); 3430b57cec5SDimitry Andric dbgs() << "Region live-in pressure: "; 3440b57cec5SDimitry Andric llvm::getRegPressure(MRI, LiveIns[RegionIdx]).print(dbgs()); 3450b57cec5SDimitry Andric dbgs() << "Region register pressure: "; 3460b57cec5SDimitry Andric PressureBefore.print(dbgs())); 3470b57cec5SDimitry Andric } 3480b57cec5SDimitry Andric 3490b57cec5SDimitry Andric ScheduleDAGMILive::schedule(); 3500b57cec5SDimitry Andric Regions[RegionIdx] = std::make_pair(RegionBegin, RegionEnd); 3510b57cec5SDimitry Andric 3520b57cec5SDimitry Andric if (!LIS) 3530b57cec5SDimitry Andric return; 3540b57cec5SDimitry Andric 3550b57cec5SDimitry Andric // Check the results of scheduling. 3560b57cec5SDimitry Andric GCNMaxOccupancySchedStrategy &S = (GCNMaxOccupancySchedStrategy&)*SchedImpl; 3570b57cec5SDimitry Andric auto PressureAfter = getRealRegPressure(); 3580b57cec5SDimitry Andric 3590b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "Pressure after scheduling: "; 3600b57cec5SDimitry Andric PressureAfter.print(dbgs())); 3610b57cec5SDimitry Andric 3620b57cec5SDimitry Andric if (PressureAfter.getSGPRNum() <= S.SGPRCriticalLimit && 3630b57cec5SDimitry Andric PressureAfter.getVGPRNum() <= S.VGPRCriticalLimit) { 3640b57cec5SDimitry Andric Pressure[RegionIdx] = PressureAfter; 3650b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "Pressure in desired limits, done.\n"); 3660b57cec5SDimitry Andric return; 3670b57cec5SDimitry Andric } 3680b57cec5SDimitry Andric unsigned Occ = MFI.getOccupancy(); 3690b57cec5SDimitry Andric unsigned WavesAfter = std::min(Occ, PressureAfter.getOccupancy(ST)); 3700b57cec5SDimitry Andric unsigned WavesBefore = std::min(Occ, PressureBefore.getOccupancy(ST)); 3710b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "Occupancy before scheduling: " << WavesBefore 3720b57cec5SDimitry Andric << ", after " << WavesAfter << ".\n"); 3730b57cec5SDimitry Andric 3740b57cec5SDimitry Andric // We could not keep current target occupancy because of the just scheduled 3750b57cec5SDimitry Andric // region. Record new occupancy for next scheduling cycle. 3760b57cec5SDimitry Andric unsigned NewOccupancy = std::max(WavesAfter, WavesBefore); 3770b57cec5SDimitry Andric // Allow memory bound functions to drop to 4 waves if not limited by an 3780b57cec5SDimitry Andric // attribute. 3790b57cec5SDimitry Andric if (WavesAfter < WavesBefore && WavesAfter < MinOccupancy && 3800b57cec5SDimitry Andric WavesAfter >= MFI.getMinAllowedOccupancy()) { 3810b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "Function is memory bound, allow occupancy drop up to " 3820b57cec5SDimitry Andric << MFI.getMinAllowedOccupancy() << " waves\n"); 3830b57cec5SDimitry Andric NewOccupancy = WavesAfter; 3840b57cec5SDimitry Andric } 3850b57cec5SDimitry Andric if (NewOccupancy < MinOccupancy) { 3860b57cec5SDimitry Andric MinOccupancy = NewOccupancy; 3870b57cec5SDimitry Andric MFI.limitOccupancy(MinOccupancy); 3880b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "Occupancy lowered for the function to " 3890b57cec5SDimitry Andric << MinOccupancy << ".\n"); 3900b57cec5SDimitry Andric } 3910b57cec5SDimitry Andric 3920b57cec5SDimitry Andric if (WavesAfter >= MinOccupancy) { 393*480093f4SDimitry Andric unsigned TotalVGPRs = AMDGPU::IsaInfo::getAddressableNumVGPRs(&ST); 394*480093f4SDimitry Andric unsigned TotalSGPRs = AMDGPU::IsaInfo::getAddressableNumSGPRs(&ST); 395*480093f4SDimitry Andric if (WavesAfter > MFI.getMinWavesPerEU() || 396*480093f4SDimitry Andric PressureAfter.less(ST, PressureBefore) || 397*480093f4SDimitry Andric (TotalVGPRs >= PressureAfter.getVGPRNum() && 398*480093f4SDimitry Andric TotalSGPRs >= PressureAfter.getSGPRNum())) { 3990b57cec5SDimitry Andric Pressure[RegionIdx] = PressureAfter; 4000b57cec5SDimitry Andric return; 4010b57cec5SDimitry Andric } 402*480093f4SDimitry Andric LLVM_DEBUG(dbgs() << "New pressure will result in more spilling.\n"); 403*480093f4SDimitry Andric } 4040b57cec5SDimitry Andric 4050b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "Attempting to revert scheduling.\n"); 4060b57cec5SDimitry Andric RegionEnd = RegionBegin; 4070b57cec5SDimitry Andric for (MachineInstr *MI : Unsched) { 4080b57cec5SDimitry Andric if (MI->isDebugInstr()) 4090b57cec5SDimitry Andric continue; 4100b57cec5SDimitry Andric 4110b57cec5SDimitry Andric if (MI->getIterator() != RegionEnd) { 4120b57cec5SDimitry Andric BB->remove(MI); 4130b57cec5SDimitry Andric BB->insert(RegionEnd, MI); 4140b57cec5SDimitry Andric if (!MI->isDebugInstr()) 4150b57cec5SDimitry Andric LIS->handleMove(*MI, true); 4160b57cec5SDimitry Andric } 4170b57cec5SDimitry Andric // Reset read-undef flags and update them later. 4180b57cec5SDimitry Andric for (auto &Op : MI->operands()) 4190b57cec5SDimitry Andric if (Op.isReg() && Op.isDef()) 4200b57cec5SDimitry Andric Op.setIsUndef(false); 4210b57cec5SDimitry Andric RegisterOperands RegOpers; 4220b57cec5SDimitry Andric RegOpers.collect(*MI, *TRI, MRI, ShouldTrackLaneMasks, false); 4230b57cec5SDimitry Andric if (!MI->isDebugInstr()) { 4240b57cec5SDimitry Andric if (ShouldTrackLaneMasks) { 4250b57cec5SDimitry Andric // Adjust liveness and add missing dead+read-undef flags. 4260b57cec5SDimitry Andric SlotIndex SlotIdx = LIS->getInstructionIndex(*MI).getRegSlot(); 4270b57cec5SDimitry Andric RegOpers.adjustLaneLiveness(*LIS, MRI, SlotIdx, MI); 4280b57cec5SDimitry Andric } else { 4290b57cec5SDimitry Andric // Adjust for missing dead-def flags. 4300b57cec5SDimitry Andric RegOpers.detectDeadDefs(*MI, *LIS); 4310b57cec5SDimitry Andric } 4320b57cec5SDimitry Andric } 4330b57cec5SDimitry Andric RegionEnd = MI->getIterator(); 4340b57cec5SDimitry Andric ++RegionEnd; 4350b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "Scheduling " << *MI); 4360b57cec5SDimitry Andric } 4370b57cec5SDimitry Andric RegionBegin = Unsched.front()->getIterator(); 4380b57cec5SDimitry Andric Regions[RegionIdx] = std::make_pair(RegionBegin, RegionEnd); 4390b57cec5SDimitry Andric 4400b57cec5SDimitry Andric placeDebugValues(); 4410b57cec5SDimitry Andric } 4420b57cec5SDimitry Andric 4430b57cec5SDimitry Andric GCNRegPressure GCNScheduleDAGMILive::getRealRegPressure() const { 4440b57cec5SDimitry Andric GCNDownwardRPTracker RPTracker(*LIS); 4450b57cec5SDimitry Andric RPTracker.advance(begin(), end(), &LiveIns[RegionIdx]); 4460b57cec5SDimitry Andric return RPTracker.moveMaxPressure(); 4470b57cec5SDimitry Andric } 4480b57cec5SDimitry Andric 4490b57cec5SDimitry Andric void GCNScheduleDAGMILive::computeBlockPressure(const MachineBasicBlock *MBB) { 4500b57cec5SDimitry Andric GCNDownwardRPTracker RPTracker(*LIS); 4510b57cec5SDimitry Andric 4520b57cec5SDimitry Andric // If the block has the only successor then live-ins of that successor are 4530b57cec5SDimitry Andric // live-outs of the current block. We can reuse calculated live set if the 4540b57cec5SDimitry Andric // successor will be sent to scheduling past current block. 4550b57cec5SDimitry Andric const MachineBasicBlock *OnlySucc = nullptr; 4560b57cec5SDimitry Andric if (MBB->succ_size() == 1 && !(*MBB->succ_begin())->empty()) { 4570b57cec5SDimitry Andric SlotIndexes *Ind = LIS->getSlotIndexes(); 4580b57cec5SDimitry Andric if (Ind->getMBBStartIdx(MBB) < Ind->getMBBStartIdx(*MBB->succ_begin())) 4590b57cec5SDimitry Andric OnlySucc = *MBB->succ_begin(); 4600b57cec5SDimitry Andric } 4610b57cec5SDimitry Andric 4620b57cec5SDimitry Andric // Scheduler sends regions from the end of the block upwards. 4630b57cec5SDimitry Andric size_t CurRegion = RegionIdx; 4640b57cec5SDimitry Andric for (size_t E = Regions.size(); CurRegion != E; ++CurRegion) 4650b57cec5SDimitry Andric if (Regions[CurRegion].first->getParent() != MBB) 4660b57cec5SDimitry Andric break; 4670b57cec5SDimitry Andric --CurRegion; 4680b57cec5SDimitry Andric 4690b57cec5SDimitry Andric auto I = MBB->begin(); 4700b57cec5SDimitry Andric auto LiveInIt = MBBLiveIns.find(MBB); 4710b57cec5SDimitry Andric if (LiveInIt != MBBLiveIns.end()) { 4720b57cec5SDimitry Andric auto LiveIn = std::move(LiveInIt->second); 4730b57cec5SDimitry Andric RPTracker.reset(*MBB->begin(), &LiveIn); 4740b57cec5SDimitry Andric MBBLiveIns.erase(LiveInIt); 4750b57cec5SDimitry Andric } else { 4760b57cec5SDimitry Andric auto &Rgn = Regions[CurRegion]; 4770b57cec5SDimitry Andric I = Rgn.first; 4780b57cec5SDimitry Andric auto *NonDbgMI = &*skipDebugInstructionsForward(Rgn.first, Rgn.second); 4790b57cec5SDimitry Andric auto LRS = BBLiveInMap.lookup(NonDbgMI); 4800b57cec5SDimitry Andric assert(isEqual(getLiveRegsBefore(*NonDbgMI, *LIS), LRS)); 4810b57cec5SDimitry Andric RPTracker.reset(*I, &LRS); 4820b57cec5SDimitry Andric } 4830b57cec5SDimitry Andric 4840b57cec5SDimitry Andric for ( ; ; ) { 4850b57cec5SDimitry Andric I = RPTracker.getNext(); 4860b57cec5SDimitry Andric 4870b57cec5SDimitry Andric if (Regions[CurRegion].first == I) { 4880b57cec5SDimitry Andric LiveIns[CurRegion] = RPTracker.getLiveRegs(); 4890b57cec5SDimitry Andric RPTracker.clearMaxPressure(); 4900b57cec5SDimitry Andric } 4910b57cec5SDimitry Andric 4920b57cec5SDimitry Andric if (Regions[CurRegion].second == I) { 4930b57cec5SDimitry Andric Pressure[CurRegion] = RPTracker.moveMaxPressure(); 4940b57cec5SDimitry Andric if (CurRegion-- == RegionIdx) 4950b57cec5SDimitry Andric break; 4960b57cec5SDimitry Andric } 4970b57cec5SDimitry Andric RPTracker.advanceToNext(); 4980b57cec5SDimitry Andric RPTracker.advanceBeforeNext(); 4990b57cec5SDimitry Andric } 5000b57cec5SDimitry Andric 5010b57cec5SDimitry Andric if (OnlySucc) { 5020b57cec5SDimitry Andric if (I != MBB->end()) { 5030b57cec5SDimitry Andric RPTracker.advanceToNext(); 5040b57cec5SDimitry Andric RPTracker.advance(MBB->end()); 5050b57cec5SDimitry Andric } 5060b57cec5SDimitry Andric RPTracker.reset(*OnlySucc->begin(), &RPTracker.getLiveRegs()); 5070b57cec5SDimitry Andric RPTracker.advanceBeforeNext(); 5080b57cec5SDimitry Andric MBBLiveIns[OnlySucc] = RPTracker.moveLiveRegs(); 5090b57cec5SDimitry Andric } 5100b57cec5SDimitry Andric } 5110b57cec5SDimitry Andric 5120b57cec5SDimitry Andric DenseMap<MachineInstr *, GCNRPTracker::LiveRegSet> 5130b57cec5SDimitry Andric GCNScheduleDAGMILive::getBBLiveInMap() const { 5140b57cec5SDimitry Andric assert(!Regions.empty()); 5150b57cec5SDimitry Andric std::vector<MachineInstr *> BBStarters; 5160b57cec5SDimitry Andric BBStarters.reserve(Regions.size()); 5170b57cec5SDimitry Andric auto I = Regions.rbegin(), E = Regions.rend(); 5180b57cec5SDimitry Andric auto *BB = I->first->getParent(); 5190b57cec5SDimitry Andric do { 5200b57cec5SDimitry Andric auto *MI = &*skipDebugInstructionsForward(I->first, I->second); 5210b57cec5SDimitry Andric BBStarters.push_back(MI); 5220b57cec5SDimitry Andric do { 5230b57cec5SDimitry Andric ++I; 5240b57cec5SDimitry Andric } while (I != E && I->first->getParent() == BB); 5250b57cec5SDimitry Andric } while (I != E); 5260b57cec5SDimitry Andric return getLiveRegMap(BBStarters, false /*After*/, *LIS); 5270b57cec5SDimitry Andric } 5280b57cec5SDimitry Andric 5290b57cec5SDimitry Andric void GCNScheduleDAGMILive::finalizeSchedule() { 5300b57cec5SDimitry Andric GCNMaxOccupancySchedStrategy &S = (GCNMaxOccupancySchedStrategy&)*SchedImpl; 5310b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "All regions recorded, starting actual scheduling.\n"); 5320b57cec5SDimitry Andric 5330b57cec5SDimitry Andric LiveIns.resize(Regions.size()); 5340b57cec5SDimitry Andric Pressure.resize(Regions.size()); 5350b57cec5SDimitry Andric 5360b57cec5SDimitry Andric if (!Regions.empty()) 5370b57cec5SDimitry Andric BBLiveInMap = getBBLiveInMap(); 5380b57cec5SDimitry Andric 5390b57cec5SDimitry Andric do { 5400b57cec5SDimitry Andric Stage++; 5410b57cec5SDimitry Andric RegionIdx = 0; 5420b57cec5SDimitry Andric MachineBasicBlock *MBB = nullptr; 5430b57cec5SDimitry Andric 5440b57cec5SDimitry Andric if (Stage > 1) { 5450b57cec5SDimitry Andric // Retry function scheduling if we found resulting occupancy and it is 5460b57cec5SDimitry Andric // lower than used for first pass scheduling. This will give more freedom 5470b57cec5SDimitry Andric // to schedule low register pressure blocks. 5480b57cec5SDimitry Andric // Code is partially copied from MachineSchedulerBase::scheduleRegions(). 5490b57cec5SDimitry Andric 5500b57cec5SDimitry Andric if (!LIS || StartingOccupancy <= MinOccupancy) 5510b57cec5SDimitry Andric break; 5520b57cec5SDimitry Andric 5530b57cec5SDimitry Andric LLVM_DEBUG( 5540b57cec5SDimitry Andric dbgs() 5550b57cec5SDimitry Andric << "Retrying function scheduling with lowest recorded occupancy " 5560b57cec5SDimitry Andric << MinOccupancy << ".\n"); 5570b57cec5SDimitry Andric 5580b57cec5SDimitry Andric S.setTargetOccupancy(MinOccupancy); 5590b57cec5SDimitry Andric } 5600b57cec5SDimitry Andric 5610b57cec5SDimitry Andric for (auto Region : Regions) { 5620b57cec5SDimitry Andric RegionBegin = Region.first; 5630b57cec5SDimitry Andric RegionEnd = Region.second; 5640b57cec5SDimitry Andric 5650b57cec5SDimitry Andric if (RegionBegin->getParent() != MBB) { 5660b57cec5SDimitry Andric if (MBB) finishBlock(); 5670b57cec5SDimitry Andric MBB = RegionBegin->getParent(); 5680b57cec5SDimitry Andric startBlock(MBB); 5690b57cec5SDimitry Andric if (Stage == 1) 5700b57cec5SDimitry Andric computeBlockPressure(MBB); 5710b57cec5SDimitry Andric } 5720b57cec5SDimitry Andric 5730b57cec5SDimitry Andric unsigned NumRegionInstrs = std::distance(begin(), end()); 5740b57cec5SDimitry Andric enterRegion(MBB, begin(), end(), NumRegionInstrs); 5750b57cec5SDimitry Andric 5760b57cec5SDimitry Andric // Skip empty scheduling regions (0 or 1 schedulable instructions). 5770b57cec5SDimitry Andric if (begin() == end() || begin() == std::prev(end())) { 5780b57cec5SDimitry Andric exitRegion(); 5790b57cec5SDimitry Andric continue; 5800b57cec5SDimitry Andric } 5810b57cec5SDimitry Andric 5820b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "********** MI Scheduling **********\n"); 5830b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << MF.getName() << ":" << printMBBReference(*MBB) << " " 5840b57cec5SDimitry Andric << MBB->getName() << "\n From: " << *begin() 5850b57cec5SDimitry Andric << " To: "; 5860b57cec5SDimitry Andric if (RegionEnd != MBB->end()) dbgs() << *RegionEnd; 5870b57cec5SDimitry Andric else dbgs() << "End"; 5880b57cec5SDimitry Andric dbgs() << " RegionInstrs: " << NumRegionInstrs << '\n'); 5890b57cec5SDimitry Andric 5900b57cec5SDimitry Andric schedule(); 5910b57cec5SDimitry Andric 5920b57cec5SDimitry Andric exitRegion(); 5930b57cec5SDimitry Andric ++RegionIdx; 5940b57cec5SDimitry Andric } 5950b57cec5SDimitry Andric finishBlock(); 5960b57cec5SDimitry Andric 5970b57cec5SDimitry Andric } while (Stage < 2); 5980b57cec5SDimitry Andric } 599