23 #define DEBUG_TYPE "machine-scheduler"
54 const int ErrorMargin = 3;
60 if (TargetOccupancy) {
70 SGPRCriticalLimit -= ErrorMargin;
71 VGPRCriticalLimit -= ErrorMargin;
74 void GCNMaxOccupancySchedStrategy::initCandidate(SchedCandidate &Cand,
SUnit *SU,
77 unsigned SGPRPressure,
78 unsigned VGPRPressure) {
87 std::vector<unsigned> Pressure;
88 std::vector<unsigned> MaxPressure;
109 const unsigned MaxVGPRPressureInc = 16;
110 bool ShouldTrackVGPRs = VGPRPressure + MaxVGPRPressureInc >= VGPRExcessLimit;
111 bool ShouldTrackSGPRs = !ShouldTrackVGPRs && SGPRPressure >= SGPRExcessLimit;
123 if (ShouldTrackVGPRs && NewVGPRPressure >= VGPRExcessLimit) {
125 Cand.RPDelta.Excess.
setUnitInc(NewVGPRPressure - VGPRExcessLimit);
128 if (ShouldTrackSGPRs && NewSGPRPressure >= SGPRExcessLimit) {
130 Cand.RPDelta.Excess.
setUnitInc(NewSGPRPressure - SGPRExcessLimit);
138 int SGPRDelta = NewSGPRPressure - SGPRCriticalLimit;
139 int VGPRDelta = NewVGPRPressure - VGPRCriticalLimit;
141 if (SGPRDelta >= 0 || VGPRDelta >= 0) {
142 if (SGPRDelta > VGPRDelta) {
144 Cand.RPDelta.CriticalMax.
setUnitInc(SGPRDelta);
147 Cand.RPDelta.CriticalMax.
setUnitInc(VGPRDelta);
154 void GCNMaxOccupancySchedStrategy::pickNodeFromQueue(
SchedBoundary &Zone,
155 const CandPolicy &ZonePolicy,
157 SchedCandidate &Cand) {
163 for (
SUnit *SU : Q) {
165 SchedCandidate TryCand(ZonePolicy);
166 initCandidate(TryCand, SU, Zone.
isTop(), RPTracker, SRI,
167 SGPRPressure, VGPRPressure);
169 SchedBoundary *ZoneArg = Cand.AtTop == TryCand.AtTop ? &Zone :
nullptr;
171 if (TryCand.Reason !=
NoCand) {
173 if (TryCand.ResDelta == SchedResourceDelta())
175 Cand.setBest(TryCand);
182 SUnit *GCNMaxOccupancySchedStrategy::pickNodeBidirectional(
bool &IsTopNode) {
195 CandPolicy BotPolicy;
199 CandPolicy TopPolicy;
226 dbgs() <<
"Top Cand: ";
228 dbgs() <<
"Bot Cand: ";
260 dbgs() <<
"Picking: ";
264 IsTopNode = Cand.AtTop;
299 SU = pickNodeBidirectional(IsTopNode);
313 std::unique_ptr<MachineSchedStrategy> S) :
317 StartingOccupancy(
ST.getOccupancyWithLocalMemSize(MFI.getLDSSize(),
319 MinOccupancy(StartingOccupancy), Stage(0), RegionIdx(0) {
321 DEBUG(
dbgs() <<
"Starting occupancy is " << StartingOccupancy <<
".\n");
331 std::vector<MachineInstr*> Unsched;
333 for (
auto &
I : *
this)
334 Unsched.push_back(&
I);
338 PressureBefore = Pressure[RegionIdx];
340 DEBUG(
dbgs() <<
"Pressure before scheduling:\nRegion live-ins:";
342 dbgs() <<
"Region live-in pressure: ";
344 dbgs() <<
"Region register pressure: ";
356 auto PressureAfter = getRealRegPressure();
358 DEBUG(
dbgs() <<
"Pressure after scheduling: "; PressureAfter.print(
dbgs()));
360 if (PressureAfter.getSGPRNum() <= S.SGPRCriticalLimit &&
361 PressureAfter.getVGPRNum() <= S.VGPRCriticalLimit) {
362 Pressure[RegionIdx] = PressureAfter;
363 DEBUG(
dbgs() <<
"Pressure in desired limits, done.\n");
366 unsigned WavesAfter =
getMaxWaves(PressureAfter.getSGPRNum(),
367 PressureAfter.getVGPRNum(),
MF);
370 DEBUG(
dbgs() <<
"Occupancy before scheduling: " << WavesBefore <<
371 ", after " << WavesAfter <<
".\n");
375 unsigned NewOccupancy =
std::max(WavesAfter, WavesBefore);
376 if (NewOccupancy < MinOccupancy) {
377 MinOccupancy = NewOccupancy;
378 DEBUG(
dbgs() <<
"Occupancy lowered for the function to "
379 << MinOccupancy <<
".\n");
382 if (WavesAfter >= WavesBefore) {
383 Pressure[RegionIdx] = PressureAfter;
387 DEBUG(
dbgs() <<
"Attempting to revert scheduling.\n");
396 for (
auto &
Op :
MI->operands())
397 if (
Op.isReg() &&
Op.isDef())
398 Op.setIsUndef(
false);
422 return RPTracker.moveMaxPressure();
439 size_t CurRegion = RegionIdx;
440 for (
size_t E = Regions.size(); CurRegion !=
E; ++CurRegion)
441 if (Regions[CurRegion].
first->getParent() != MBB)
446 auto LiveInIt = MBBLiveIns.find(MBB);
447 if (LiveInIt != MBBLiveIns.end()) {
448 auto LiveIn = std::move(LiveInIt->second);
450 MBBLiveIns.erase(LiveInIt);
452 I = Regions[CurRegion].first;
457 I = RPTracker.getNext();
459 if (Regions[CurRegion].
first ==
I) {
460 LiveIns[CurRegion] = RPTracker.getLiveRegs();
461 RPTracker.clearMaxPressure();
464 if (Regions[CurRegion].
second ==
I) {
465 Pressure[CurRegion] = RPTracker.moveMaxPressure();
466 if (CurRegion-- == RegionIdx)
469 RPTracker.advanceToNext();
470 RPTracker.advanceBeforeNext();
474 if (
I != MBB->
end()) {
475 RPTracker.advanceToNext();
478 RPTracker.
reset(*OnlySucc->
begin(), &RPTracker.getLiveRegs());
479 RPTracker.advanceBeforeNext();
480 MBBLiveIns[OnlySucc] = RPTracker.moveLiveRegs();
486 DEBUG(
dbgs() <<
"All regions recorded, starting actual scheduling.\n");
488 LiveIns.resize(Regions.size());
489 Pressure.resize(Regions.size());
502 if (!
LIS || StartingOccupancy <= MinOccupancy)
506 <<
"Retrying function scheduling with lowest recorded occupancy "
507 << MinOccupancy <<
".\n");
512 for (
auto Region : Regions) {
521 computeBlockPressure(MBB);
533 DEBUG(
dbgs() <<
"********** MI Scheduling **********\n");
536 <<
"\n From: " << *
begin() <<
" To: ";
538 else dbgs() <<
"End";
539 dbgs() <<
" RegionInstrs: " << NumRegionInstrs <<
'\n');
unsigned succ_size() const
virtual void finishBlock()
Cleans up after scheduling in the given block.
Interface definition for SIRegisterInfo.
GCNRegPressure max(const GCNRegPressure &P1, const GCNRegPressure &P2)
SUnit * pickNode(bool &IsTopNode) override
Pick the best node to balance the schedule. Implements MachineSchedStrategy.
AMDGPU specific subclass of TargetSubtarget.
void detectDeadDefs(const MachineInstr &MI, const LiveIntervals &LIS)
Use liveness information to find dead defs not marked with a dead flag and move them to the DeadDefs ...
Each Scheduling boundary is associated with ready queues.
static void printLiveRegs(raw_ostream &OS, const LiveRegSet &LiveRegs, const MachineRegisterInfo &MRI)
const MachineSchedContext * Context
This is a minimal scheduler strategy.
int getNumber() const
MachineBasicBlocks are uniquely numbered at the function level, unless they're not in a MachineFuncti...
Maximum length of the test input libFuzzer tries to guess a good value based on the corpus and reports it always prefer smaller inputs during the corpus shuffle When libFuzzer itself reports a bug this exit code will be used If indicates the maximal total time in seconds to run the fuzzer minimizes the provided crash input Use with tries to cleanse the provided crash input to make it contain fewer original bytes Use with exact_artifact_path to specify the output Experimental Use value profile to guide fuzzing Number of jobs to run If min(jobs, NumberOfCpuCores()/2)\" is used.") FUZZER_FLAG_INT(reload
unsigned getMaxNumVGPRs(unsigned WavesPerEU) const
MachineInstr * getInstr() const
Returns the representative MachineInstr for this SUnit.
void traceCandidate(const SchedCandidate &Cand)
MachineBasicBlock::iterator begin() const
Returns an iterator to the top of the current scheduling region.
void setTargetOccupancy(unsigned Occ)
SlotIndex getInstructionIndex(const MachineInstr &Instr) const
Returns the base index of the given instruction.
const Function * getFunction() const
getFunction - Return the LLVM function that this machine code represents
void reset(const CandPolicy &NewPolicy)
unsigned getNumAllocatableRegs(const TargetRegisterClass *RC) const
getNumAllocatableRegs - Returns the number of actually allocatable registers in RC in the current fun...
MachineBasicBlock::iterator top() const
const RegPressureTracker & getTopRPTracker() const
unsigned NumRegionInstrs
Instructions in this region (distance(RegionBegin, RegionEnd)).
ScheduleDAGMI is an implementation of ScheduleDAGInstrs that simply schedules machine instructions ac...
std::unique_ptr< MachineSchedStrategy > SchedImpl
const RegPressureTracker & getBotRPTracker() const
virtual void startBlock(MachineBasicBlock *BB)
Prepares to perform scheduling in the given block.
ScheduleDAGMILive is an implementation of ScheduleDAGInstrs that schedules machine instructions while...
MachineFunction & MF
Machine function.
bool isScheduled
True once scheduled.
unsigned getOccupancyWithNumSGPRs(unsigned SGPRs) const
Return the maximum number of waves per SIMD for kernels using SGPRs SGPRs.
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
MachineBasicBlock::iterator RegionEnd
The end of the range to be scheduled.
bool ShouldTrackLaneMasks
MachineBasicBlock::iterator RegionBegin
The beginning of the range to be scheduled.
MachineSchedPolicy RegionPolicy
void collect(const MachineInstr &MI, const TargetRegisterInfo &TRI, const MachineRegisterInfo &MRI, bool TrackLaneMasks, bool IgnoreDead)
Analyze the given instruction MI and fill in the Uses, Defs and DeadDefs list based on the MachineOpe...
List of registers defined and used by a machine instruction.
instr_iterator insert(instr_iterator I, MachineInstr *M)
Insert MI into the instruction list before I, possibly inside a bundle.
void print(raw_ostream &OS, const SISubtarget *ST=nullptr) const
unsigned getVGPRPressureSet() const
RegisterClassInfo * RegClassInfo
SlotIndexes * getSlotIndexes() const
CandReason
Represent the type of SchedCandidate found within a single queue.
Helpers for implementing custom MachineSchedStrategy classes.
GCNScheduleDAGMILive(MachineSchedContext *C, std::unique_ptr< MachineSchedStrategy > S)
void removeReady(SUnit *SU)
Remove SU from the ready set for this boundary.
static unsigned getMaxWaves(unsigned SGPRs, unsigned VGPRs, const MachineFunction &MF)
unsigned getOccupancyWithNumVGPRs(unsigned VGPRs) const
Return the maximum number of waves per SIMD for kernels using VGPRs VGPRs.
void tryCandidate(SchedCandidate &Cand, SchedCandidate &TryCand, SchedBoundary *Zone)
Apply a set of heursitics to a new candidate.
Track the current register pressure at some position in the instruction stream, and remember the high...
Ty * getInfo()
getInfo - Keep track of various per-function pieces of information for backends that would like to do...
Policy for scheduling the next instruction in the candidate's zone.
const TargetSchedModel * SchedModel
virtual void exitRegion()
Called when the scheduler has finished scheduling the current region.
const TargetRegisterInfo * TRI
unsigned getOccupancyWithLocalMemSize(uint32_t Bytes, const Function &) const
Inverse of getMaxLocalMemWithWaveCount.
succ_iterator succ_begin()
void setPolicy(CandPolicy &Policy, bool IsPostRA, SchedBoundary &CurrZone, SchedBoundary *OtherZone)
Set the CandPolicy given a scheduling zone given the current resources and latencies inside and outsi...
void adjustLaneLiveness(const LiveIntervals &LIS, const MachineRegisterInfo &MRI, SlotIndex Pos, MachineInstr *AddFlagsMI=nullptr)
Use liveness information to find out which uses/defs are partially undefined/dead and adjust the Regi...
void advance()
Advance across the current instruction.
GenericScheduler shrinks the unscheduled zone using heuristics to balance the schedule.
unsigned getRegPressureSetLimit(const MachineFunction &MF, unsigned Idx) const override
void enterRegion(MachineBasicBlock *bb, MachineBasicBlock::iterator begin, MachineBasicBlock::iterator end, unsigned regioninstrs) override
Implement the ScheduleDAGInstrs interface for handling the next scheduling region.
MachineInstr * remove(MachineInstr *I)
Remove the unbundled instruction from the instruction list without deleting it.
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
StringRef getName() const
Return the name of the corresponding LLVM basic block, or an empty string.
PressureChange CriticalMax
MachineBasicBlock::iterator bottom() const
MachineBasicBlock::iterator end() const
Returns an iterator to the bottom of the current scheduling region.
GCNRegPressure getRegPressure(const MachineRegisterInfo &MRI, Range &&LiveRegs)
void initialize(ScheduleDAGMI *dag) override
Initialize the strategy after building the DAG for a new region.
Representation of each machine instruction.
This class keeps track of the SPI_SP_INPUT_ADDR config register, which tells the hardware which inter...
Interface definition for SIInstrInfo.
const TargetRegisterInfo * TRI
Target processor register info.
SUnit * pickOnlyChoice()
Call this before applying any other heuristics to the Available queue.
unsigned getSGPRNum() const
SchedCandidate BotCand
Candidate last picked from Bot boundary.
MachineSchedContext provides enough context from the MachineScheduler pass for the target to instanti...
unsigned getVGPRNum() const
Capture a change in pressure for a single pressure set.
void getUpwardPressure(const MachineInstr *MI, std::vector< unsigned > &PressureResult, std::vector< unsigned > &MaxPressureResult)
Get the pressure of each PSet after traversing this instruction bottom-up.
void placeDebugValues()
Reinsert debug_values recorded in ScheduleDAGInstrs::DbgValues.
void schedule() override
Implement ScheduleDAGInstrs interface for scheduling a sequence of reorderable instructions.
SlotIndex getMBBStartIdx(unsigned Num) const
Returns the first index in the given basic block number.
unsigned getSGPRPressureSet() const
unsigned NodeNum
Entry # of node in the node vector.
const std::vector< unsigned > & getRegSetPressureAtPos() const
Get the register set pressure at the current position, which may be less than the pressure across the...
SlotIndex getRegSlot(bool EC=false) const
Returns the register use/def slot in the current instruction for a normal or early-clobber def...
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
GCNMaxOccupancySchedStrategy(const MachineSchedContext *C)
SchedCandidate TopCand
Candidate last picked from Top boundary.
bool isBottomReady() const
void finalizeSchedule() override
Allow targets to perform final scheduling actions at the level of the whole MachineFunction.
unsigned getMaxNumSGPRs(unsigned WavesPerEU, bool Addressable) const
void getDownwardPressure(const MachineInstr *MI, std::vector< unsigned > &PressureResult, std::vector< unsigned > &MaxPressureResult)
Get the pressure of each PSet after traversing this instruction top-down.
MachineBasicBlock * BB
The block in which to insert instructions.
StringRef getName() const
getName - Return the name of the corresponding LLVM function.
MachineRegisterInfo & MRI
Virtual/real register map.
SlotIndex - An opaque wrapper around machine indexes.
void initialize(ScheduleDAGMI *DAG) override
Initialize the strategy after building the DAG for a new region.
void schedule() override
Implement ScheduleDAGInstrs interface for scheduling a sequence of reorderable instructions.
RegPressureTracker RPTracker
void handleMove(MachineInstr &MI, bool UpdateFlags=false)
Call this method to notify LiveIntervals that instruction MI has been moved within a basic block...
Scheduling unit. This is a node in the scheduling DAG.