20 #define DEBUG_TYPE "machine-scheduler"
38 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
46 auto BB = Begin->getParent();
47 OS << BB->getParent()->getName() <<
":BB#" << BB->getNumber()
48 <<
' ' << BB->getName() <<
":\n";
50 MaxInstNum =
std::max(MaxInstNum, 1u);
51 for (;
I != End && MaxInstNum; ++
I, --MaxInstNum) {
52 if (!
I->isDebugValue() && LIS)
59 if (!
I->isDebugValue() && LIS)
63 if (End != BB->end()) {
75 const auto BB = Begin->getParent();
76 const auto &
MRI = BB->getParent()->getRegInfo();
82 const auto BottomMI = End == BB->end() ? std::prev(End) :
End;
92 OS <<
"Region to schedule ";
96 R->MaxPressure.print(OS, &
ST);
104 OS <<
"\nAfter scheduling ";
130 auto BB = R.
Begin->getParent();
131 Sch.BaseClass::startBlock(
BB);
134 Sch.buildSchedGraph(Sch.AA,
nullptr,
nullptr,
nullptr,
136 Sch.Topo.InitDAGTopologicalSorting();
139 Sch.findRootsAndBiasEdges(TopRoots, BotRoots);
142 Sch.BaseClass::exitRegion();
143 Sch.BaseClass::finishBlock();
153 std::unique_ptr<MachineSchedStrategy> SaveSchedImpl;
161 , SaveSchedImpl(std::move(_Sch.
SchedImpl))
162 , SaveMaxRP(R.MaxPressure) {
163 Sch.SchedImpl.reset(&OverrideStrategy);
164 auto BB = R.
Begin->getParent();
165 Sch.BaseClass::startBlock(
BB);
169 Sch.BaseClass::exitRegion();
170 Sch.BaseClass::finishBlock();
171 Sch.SchedImpl.release();
172 Sch.SchedImpl = std::move(SaveSchedImpl);
175 assert(Sch.RegionBegin == Rgn.Begin && Sch.RegionEnd == Rgn.End);
178 Sch.BaseClass::schedule();
181 Sch.RegionEnd = Rgn.End;
183 Rgn.Begin = Sch.RegionBegin;
184 Rgn.MaxPressure.clear();
187 assert(Sch.RegionBegin == Rgn.Begin && Sch.RegionEnd == Rgn.End);
190 Sch.scheduleRegion(Rgn, Sch.SUnits, SaveMaxRP);
198 bool shouldTrackPressure()
const override {
return false; }
199 bool shouldTrackLaneMasks()
const override {
return false; }
201 SUnit *pickNode(
bool &IsTopNode)
override {
return nullptr; }
202 void schedNode(
SUnit *SU,
bool IsTopNode)
override {}
203 void releaseTopNode(
SUnit *SU)
override {}
204 void releaseBottomNode(
SUnit *SU)
override {}
224 auto const BBEnd = Begin->getParent()->end();
225 auto const BottomMI = End == BBEnd ? std::prev(End) :
End;
229 auto AfterBottomMI = std::next(BottomMI);
230 if (AfterBottomMI == BBEnd ||
237 for (
auto I = BottomMI;
I != Begin; --
I)
243 (
dbgs() <<
"Tracked region ",
251 Range &&Schedule)
const {
252 auto const BBEnd = R.
Begin->getParent()->end();
254 if (R.
End != BBEnd) {
261 RPTracker.
reset(*std::prev(BBEnd));
263 for (
auto I = Schedule.end(),
B = Schedule.begin();
I !=
B;) {
272 unsigned NumRegionInstrs) {
274 if (NumRegionInstrs > 2) {
276 new (
Alloc.Allocate())
287 dbgs() <<
"Max RP: ";
306 std::vector<MachineInstr*>
308 std::vector<MachineInstr*> Res;
309 Res.reserve(Schedule.
size() * 2);
315 for (
auto SU : Schedule) {
316 Res.push_back(SU->getInstr());
317 const auto &
D =
std::find_if(DbgB, DbgE, [SU](decltype(*DbgB) &
P) {
318 return P.second == SU->getInstr();
321 Res.push_back(
D->first);
341 template <
typename Range>
349 auto BB = R.
Begin->getParent();
351 for (
const auto &
I : Schedule) {
356 if (!
MI->isDebugValue())
359 if (!
MI->isDebugValue()) {
361 for (
auto &
Op :
MI->operands())
362 if (
Op.isReg() &&
Op.isDef())
363 Op.setIsUndef(
false);
372 Top = std::next(
MI->getIterator());
378 if (!std::is_same<decltype(*Schedule.begin()),
MachineInstr*>::value) {
392 assert((SchedMaxRP == RegionMaxRP && (MaxRP.
empty() || SchedMaxRP == MaxRP))
393 || (
dbgs() <<
"Max RP mismatch!!!\n"
394 "RP for schedule (calculated): ",
395 SchedMaxRP.print(
dbgs(), &
ST),
396 dbgs() <<
"RP for schedule (reported): ",
398 dbgs() <<
"RP after scheduling: ",
399 RegionMaxRP.print(
dbgs(), &
ST),
408 return R2->MaxPressure.less(
ST, R1->MaxPressure, TargetOcc);
424 const auto Occ =
Regions.front()->MaxPressure.getOccupancy(
ST);
425 DEBUG(
dbgs() <<
"Trying to to improve occupancy, target = " << TargetOcc
426 <<
", current = " << Occ <<
'\n');
428 auto NewOcc = TargetOcc;
430 if (R->MaxPressure.getOccupancy(
ST) >= NewOcc)
439 DEBUG(
dbgs() <<
"Occupancy improvement attempt:\n";
442 NewOcc =
std::min(NewOcc, MaxRP.getOccupancy(
ST));
448 DEBUG(
dbgs() <<
"New occupancy = " << NewOcc
449 <<
", prev occupancy = " << Occ <<
'\n');
454 bool TryMaximizeOccupancy) {
456 auto TgtOcc =
ST.getOccupancyWithLocalMemSize(
MF);
459 auto Occ =
Regions.front()->MaxPressure.getOccupancy(
ST);
461 if (TryMaximizeOccupancy && Occ < TgtOcc)
466 const int NumPasses = Occ < TgtOcc ? 2 : 1;
469 DEBUG(
dbgs() <<
"Scheduling using default scheduler, "
470 "target occupancy = " << TgtOcc <<
'\n');
473 for (
int I = 0;
I < NumPasses; ++
I) {
484 if (
RP.getOccupancy(
ST) < TgtOcc) {
485 DEBUG(
dbgs() <<
"Didn't fit into target occupancy O" << TgtOcc);
486 if (R->BestSchedule.get() &&
487 R->BestSchedule->MaxPressure.getOccupancy(
ST) >= TgtOcc) {
488 DEBUG(
dbgs() <<
", scheduling minimal register\n");
493 assert(R->MaxPressure.getOccupancy(
ST) >= TgtOcc);
505 const auto TgtOcc =
ST.getOccupancyWithLocalMemSize(
MF);
508 auto MaxPressure =
Regions.front()->MaxPressure;
510 if (!force && R->MaxPressure.less(
ST, MaxPressure, TgtOcc))
517 DEBUG(
if (R->MaxPressure.less(
ST, RP, TgtOcc)) {
518 dbgs() <<
"\nWarning: Pressure becomes worse after minreg!";
522 if (!force && MaxPressure.less(
ST, RP, TgtOcc))
unsigned tryMaximizeOccupancy(unsigned TargetOcc=std::numeric_limits< unsigned >::max())
void enterRegion(MachineBasicBlock *BB, MachineBasicBlock::iterator Begin, MachineBasicBlock::iterator End, unsigned RegionInstrs) override
Implement the ScheduleDAGInstrs interface for handling the next scheduling region.
GCNRegPressure max(const GCNRegPressure &P1, const GCNRegPressure &P2)
#define LLVM_DUMP_METHOD
Mark debug helper function definitions like dump() that should not be stripped from debug builds...
This is a minimal scheduler strategy.
void sortRegionsByPressure(unsigned TargetOcc)
decltype(MaxPressure) moveMaxPressure()
SpecificBumpPtrAllocator< Region > Alloc
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
MachineInstr * getInstr() const
Returns the representative MachineInstr for this SUnit.
std::vector< MachineInstr * > detachSchedule(ScheduleRef Schedule) const
void printRegions(raw_ostream &OS) const
GCNRegPressure MaxPressure
GCNRegPressure getRegionPressure(MachineBasicBlock::iterator Begin, MachineBasicBlock::iterator End) const
std::vector< Region * > Regions
void setTargetOccupancy(unsigned Occ)
SlotIndex getInstructionIndex(const MachineInstr &Instr) const
Returns the base index of the given instruction.
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
ScheduleDAGMILive is an implementation of ScheduleDAGInstrs that schedules machine instructions while...
MachineFunction & MF
Machine function.
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.
MachineBasicBlock::iterator Begin
void finalizeSchedule() override
Allow targets to perform final scheduling actions at the level of the whole MachineFunction.
MachineSchedContext * Context
DbgValueVector DbgValues
Remember instruction that precedes DBG_VALUE.
void printSchedResult(raw_ostream &OS, const Region *R, const GCNRegPressure &RP) const
void scheduleLegacyMaxOccupancy(bool TryMaximizeOccupancy=true)
void scheduleBest(Region &R)
MachineBasicBlock::iterator RegionBegin
The beginning of the range to be scheduled.
static LLVM_DUMP_METHOD void printLivenessInfo(raw_ostream &OS, MachineBasicBlock::iterator Begin, MachineBasicBlock::iterator End, const LiveIntervals *LIS)
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory)...
GCNRPTracker::LiveRegSet getLiveRegsBefore(const MachineInstr &MI, const LiveIntervals &LIS)
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...
static MachineInstr * getMachineInstr(MachineInstr *MI)
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
size_t size() const
size - Get the array size.
void reset(const MachineInstr &MI, const LiveRegSet *LiveRegs=nullptr)
void setBestSchedule(Region &R, ScheduleRef Schedule, const GCNRegPressure &MaxRP=GCNRegPressure())
unsigned const MachineRegisterInfo * MRI
std::enable_if<!std::is_array< T >::value, std::unique_ptr< T > >::type make_unique(Args &&...args)
Constructs a new T() with the given args and returns a unique_ptr<T> which owns the object...
GCNRegPressure getSchedulePressure(const Region &R, Range &&Schedule) const
~OverrideLegacyStrategy()
const unsigned NumRegionInstrs
const MachineInstr * getLastTrackedMI() const
std::vector< const SUnit * > makeMinRegSchedule(ArrayRef< const SUnit * > TopRoots, const ScheduleDAG &DAG)
static const unsigned End
GCNRPTracker::LiveRegSet getLiveRegsAfter(const MachineInstr &MI, const LiveIntervals &LIS)
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...
ArrayRef< const SUnit * > getTopRoots() const
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small...
OverrideLegacyStrategy(Region &R, MachineSchedStrategy &OverrideStrategy, GCNIterativeScheduler &_Sch)
void enterRegion(MachineBasicBlock *bb, MachineBasicBlock::iterator begin, MachineBasicBlock::iterator end, unsigned regioninstrs) override
Implement the ScheduleDAGInstrs interface for handling the next scheduling region.
void schedule() override
Implement ScheduleDAGInstrs interface for scheduling a sequence of reorderable instructions.
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.
GCNUpwardRPTracker UPTracker
static void initialize(TargetLibraryInfoImpl &TLI, const Triple &T, ArrayRef< StringRef > StandardNames)
initialize - Initialize the set of available library functions based on the specified target triple...
const MachineBasicBlock::iterator End
static LLVM_DUMP_METHOD void printRegion(raw_ostream &OS, MachineBasicBlock::iterator Begin, MachineBasicBlock::iterator End, const LiveIntervals *LIS, unsigned MaxInstNum=std::numeric_limits< unsigned >::max())
GCNRegPressure getRegPressure(const MachineRegisterInfo &MRI, Range &&LiveRegs)
Representation of each machine instruction.
BuildDAG(const Region &R, GCNIterativeScheduler &_Sch)
const TargetRegisterInfo * TRI
Target processor register info.
const StrategyKind Strategy
GCNIterativeScheduler(MachineSchedContext *C, StrategyKind S)
MachineSchedContext provides enough context from the MachineScheduler pass for the target to instanti...
MachineSchedStrategy - Interface to the scheduling algorithm used by ScheduleDAGMI.
void placeDebugValues()
Reinsert debug_values recorded in ScheduleDAGInstrs::DbgValues.
void scheduleRegion(Region &R, Range &&Schedule, const GCNRegPressure &MaxRP=GCNRegPressure())
SlotIndex getRegSlot(bool EC=false) const
Returns the register use/def slot in the current instruction for a normal or early-clobber def...
void printSchedRP(raw_ostream &OS, const GCNRegPressure &Before, const GCNRegPressure &After) const
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
void scheduleMinReg(bool force=false)
This class implements an extremely fast bulk output stream that can only output to a stream...
MachineInstr * FirstDbgValue
MachineBasicBlock * BB
The block in which to insert instructions.
void sort(Policy policy, RandomAccessIterator Start, RandomAccessIterator End, const Comparator &Comp=Comparator())
MachineRegisterInfo & MRI
Virtual/real register map.
auto find_if(R &&Range, UnaryPredicate P) -> decltype(std::begin(Range))
Provide wrappers to std::find_if which take ranges instead of having to pass begin/end explicitly...
void recede(const MachineInstr &MI)
RegPressureTracker RPTracker
std::unique_ptr< TentativeSchedule > BestSchedule
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.