Skip to content

[AMDGPU] NFC: Add BBLiveOutMap & LiveOut Cache #93089

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 55 additions & 1 deletion llvm/lib/Target/AMDGPU/GCNSchedStrategy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,11 @@ static cl::opt<bool>
"Wave Limited (amdgpu-limit-wave-threshold)."),
cl::init(false));

static cl::opt<bool> GCNTrackers(
"amdgpu-use-amdgpu-trackers", cl::Hidden,
cl::desc("Use the AMDGPU specific RPTrackers during scheduling"),
cl::init(false));

const unsigned ScheduleMetrics::ScaleFactor = 100;

GCNSchedStrategy::GCNSchedStrategy(const MachineSchedContext *C)
Expand Down Expand Up @@ -526,6 +531,19 @@ GCNScheduleDAGMILive::getRealRegPressure(unsigned RegionIdx) const {
return RPTracker.moveMaxPressure();
}

static MachineInstr *getLastMIForRegion(MachineBasicBlock::iterator RegionBegin,
MachineBasicBlock::iterator RegionEnd) {
MachineInstr *LastMI;
auto *BB = RegionBegin->getParent();
if (RegionEnd != BB->end() && !RegionEnd->isDebugInstr())
LastMI = &*RegionEnd;
else if (RegionEnd == BB->end())
LastMI = &*prev_nodbg(RegionEnd, RegionBegin);
else
LastMI = &*skipDebugInstructionsBackward(RegionEnd, RegionBegin);
return LastMI;
}

void GCNScheduleDAGMILive::computeBlockPressure(unsigned RegionIdx,
const MachineBasicBlock *MBB) {
GCNDownwardRPTracker RPTracker(*LIS);
Expand Down Expand Up @@ -597,6 +615,16 @@ void GCNScheduleDAGMILive::computeBlockPressure(unsigned RegionIdx,
RPTracker.advanceBeforeNext();
MBBLiveIns[OnlySucc] = RPTracker.moveLiveRegs();
}

if (GCNTrackers) {
assert(LiveOuts.size() == Regions.size());
for (unsigned RegionIdx = 0; RegionIdx < Regions.size(); RegionIdx++) {
auto RegionBegin = Regions[RegionIdx].first;
auto RegionEnd = Regions[RegionIdx].second;
MachineInstr *LastMI = getLastMIForRegion(RegionBegin, RegionEnd);
LiveOuts[RegionIdx] = BBLiveOutMap.lookup(LastMI);
}
}
}

DenseMap<MachineInstr *, GCNRPTracker::LiveRegSet>
Expand All @@ -616,11 +644,24 @@ GCNScheduleDAGMILive::getBBLiveInMap() const {
return getLiveRegMap(BBStarters, false /*After*/, *LIS);
}

DenseMap<MachineInstr *, GCNRPTracker::LiveRegSet>
GCNScheduleDAGMILive::getBBLiveOutMap() const {
assert(!Regions.empty());
std::vector<MachineInstr *> BBEnders;
BBEnders.reserve(Regions.size());
auto I = Regions.rbegin(), E = Regions.rend();
for (; I != E; I++)
BBEnders.push_back(getLastMIForRegion(I->first, I->second));

return getLiveRegMap(BBEnders, true /*After*/, *LIS);
}

void GCNScheduleDAGMILive::finalizeSchedule() {
// Start actual scheduling here. This function is called by the base
// MachineScheduler after all regions have been recorded by
// GCNScheduleDAGMILive::schedule().
LiveIns.resize(Regions.size());
LiveOuts.resize(Regions.size());
Pressure.resize(Regions.size());
RescheduleRegions.resize(Regions.size());
RegionsWithHighRP.resize(Regions.size());
Expand All @@ -639,8 +680,12 @@ void GCNScheduleDAGMILive::finalizeSchedule() {
void GCNScheduleDAGMILive::runSchedStages() {
LLVM_DEBUG(dbgs() << "All regions recorded, starting actual scheduling.\n");

if (!Regions.empty())
if (!Regions.empty()) {
BBLiveInMap = getBBLiveInMap();
if (GCNTrackers) {
BBLiveOutMap = getBBLiveOutMap();
}
}

GCNSchedStrategy &S = static_cast<GCNSchedStrategy &>(*SchedImpl);
while (S.advanceStage()) {
Expand Down Expand Up @@ -1499,6 +1544,15 @@ bool PreRARematStage::sinkTriviallyRematInsts(const GCNSubtarget &ST,
DAG.Regions = NewRegions;
DAG.RescheduleRegions = NewRescheduleRegions;

if (GCNTrackers) {
DAG.BBLiveOutMap = DAG.getBBLiveOutMap();
auto I = DAG.Regions.begin(), E = DAG.Regions.end();
for (; I != E; I++) {
MachineInstr *LastMI = getLastMIForRegion(I->first, I->second);
DAG.LiveOuts.push_back(DAG.BBLiveOutMap.lookup(LastMI));
}
}

SIMachineFunctionInfo &MFI = *MF.getInfo<SIMachineFunctionInfo>();
MFI.increaseOccupancy(MF, ++DAG.MinOccupancy);

Expand Down
7 changes: 7 additions & 0 deletions llvm/lib/Target/AMDGPU/GCNSchedStrategy.h
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,9 @@ class GCNScheduleDAGMILive final : public ScheduleDAGMILive {
// Region live-in cache.
SmallVector<GCNRPTracker::LiveRegSet, 32> LiveIns;

// Region live-out cache.
SmallVector<GCNRPTracker::LiveRegSet, 32> LiveOuts;

// Region pressure cache.
SmallVector<GCNRegPressure, 32> Pressure;

Expand All @@ -215,6 +218,10 @@ class GCNScheduleDAGMILive final : public ScheduleDAGMILive {

DenseMap<MachineInstr *, GCNRPTracker::LiveRegSet> getBBLiveInMap() const;

DenseMap<MachineInstr *, GCNRPTracker::LiveRegSet> BBLiveOutMap;

DenseMap<MachineInstr *, GCNRPTracker::LiveRegSet> getBBLiveOutMap() const;

// Return current region pressure.
GCNRegPressure getRealRegPressure(unsigned RegionIdx) const;

Expand Down
Loading