Skip to content

Commit

Permalink
[AMDGPU] Fix debug values in scheduler not placed correctly when reve…
Browse files Browse the repository at this point in the history
…rting

Debug position data is cleared after ScheduleDAGMILive::schedule() due to it also calling placeDebugValues(). Make it so the data is not cleared after initial call to placeDebugValues since we will call it again after reverting a schedule.

Secondly, since we skip debug instructions when reverting the schedule on AMDGPU, all debug instructions are now moved to the end of the scheduling region. RegionEnd points to the beginning of this chunk of debug instructions since it was not incremented when a debug instruction was skipped. RegionBegin may also point to the same debug instruction if Unsched.front() is a debug instruction thus shrinking the region to 1. Fix RegionBegin and RegionEnd so that they point to the current beginning and ending before calling placeDebugValues() since both vars will be used as reference points to move debug instructions back.

Reviewed By: rampitec

Differential Revision: https://reviews.llvm.org/D119022
  • Loading branch information
vangthao95 committed Feb 7, 2022
1 parent 5c2ae5f commit 5704711
Show file tree
Hide file tree
Showing 3 changed files with 329 additions and 6 deletions.
6 changes: 2 additions & 4 deletions llvm/lib/CodeGen/MachineScheduler.cpp
Expand Up @@ -920,12 +920,10 @@ void ScheduleDAGMI::placeDebugValues() {
MachineBasicBlock::iterator OrigPrevMI = P.second;
if (&*RegionBegin == DbgValue)
++RegionBegin;
BB->splice(++OrigPrevMI, BB, DbgValue);
if (OrigPrevMI == std::prev(RegionEnd))
BB->splice(std::next(OrigPrevMI), BB, DbgValue);
if (RegionEnd != BB->end() && OrigPrevMI == &*RegionEnd)
RegionEnd = DbgValue;
}
DbgValues.clear();
FirstDbgValue = nullptr;
}

#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
Expand Down
28 changes: 26 additions & 2 deletions llvm/lib/Target/AMDGPU/GCNSchedStrategy.cpp
Expand Up @@ -429,9 +429,12 @@ void GCNScheduleDAGMILive::schedule() {
RescheduleRegions[RegionIdx] = RegionsWithClusters[RegionIdx] ||
(Stage + 1) != UnclusteredReschedule;
RegionEnd = RegionBegin;
int SkippedDebugInstr = 0;
for (MachineInstr *MI : Unsched) {
if (MI->isDebugInstr())
if (MI->isDebugInstr()) {
++SkippedDebugInstr;
continue;
}

if (MI->getIterator() != RegionEnd) {
BB->remove(MI);
Expand Down Expand Up @@ -459,10 +462,31 @@ void GCNScheduleDAGMILive::schedule() {
++RegionEnd;
LLVM_DEBUG(dbgs() << "Scheduling " << *MI);
}

// After reverting schedule, debug instrs will now be at the end of the block
// and RegionEnd will point to the first debug instr. Increment RegionEnd
// pass debug instrs to the actual end of the scheduling region.
while (SkippedDebugInstr-- > 0)
++RegionEnd;

// If Unsched.front() instruction is a debug instruction, this will actually
// shrink the region since we moved all debug instructions to the end of the
// block. Find the first instruction that is not a debug instruction.
RegionBegin = Unsched.front()->getIterator();
Regions[RegionIdx] = std::make_pair(RegionBegin, RegionEnd);
if (RegionBegin->isDebugInstr()) {
for (MachineInstr *MI : Unsched) {
if (MI->isDebugInstr())
continue;
RegionBegin = MI->getIterator();
break;
}
}

// Then move the debug instructions back into their correct place and set
// RegionBegin and RegionEnd if needed.
placeDebugValues();

Regions[RegionIdx] = std::make_pair(RegionBegin, RegionEnd);
}

GCNRegPressure GCNScheduleDAGMILive::getRealRegPressure() const {
Expand Down

0 comments on commit 5704711

Please sign in to comment.