Skip to content

Commit 19555ec

Browse files
committed
Avoid modifying DAG and MFI minOccupancy before a region is scheduled.
1 parent c2419e7 commit 19555ec

File tree

3 files changed

+35
-21
lines changed

3 files changed

+35
-21
lines changed

llvm/lib/Target/AMDGPU/GCNSchedStrategy.cpp

Lines changed: 32 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1051,19 +1051,20 @@ bool UnclusteredHighRPStage::initGCNSchedStage() {
10511051
createIGroupLPDAGMutation(AMDGPU::SchedulingPhase::PreRAReentry));
10521052

10531053
InitialOccupancy = DAG.MinOccupancy;
1054-
IsAnyRegionScheduled = false;
1055-
// Aggressivly try to reduce register pressure in the unclustered high RP
1054+
// Aggressively try to reduce register pressure in the unclustered high RP
10561055
// stage. Temporarily increase occupancy target in the region.
1056+
TempTargetOccupancy = MFI.getMaxWavesPerEU() > DAG.MinOccupancy
1057+
? InitialOccupancy + 1
1058+
: InitialOccupancy;
1059+
IsAnyRegionScheduled = false;
10571060
S.SGPRLimitBias = S.HighRPSGPRBias;
10581061
S.VGPRLimitBias = S.HighRPVGPRBias;
1059-
if (MFI.getMaxWavesPerEU() > DAG.MinOccupancy)
1060-
MFI.increaseOccupancy(MF, ++DAG.MinOccupancy);
10611062

10621063
LLVM_DEBUG(
10631064
dbgs()
10641065
<< "Retrying function scheduling without clustering. "
1065-
"Aggressivly try to reduce register pressure to achieve occupancy "
1066-
<< DAG.MinOccupancy << ".\n");
1066+
"Aggressively try to reduce register pressure to achieve occupancy "
1067+
<< TempTargetOccupancy << ".\n");
10671068

10681069
return true;
10691070
}
@@ -1144,16 +1145,16 @@ void UnclusteredHighRPStage::finalizeGCNSchedStage() {
11441145
SavedMutations.swap(DAG.Mutations);
11451146
S.SGPRLimitBias = S.VGPRLimitBias = 0;
11461147
if (DAG.MinOccupancy > InitialOccupancy) {
1147-
if (IsAnyRegionScheduled) {
1148-
LLVM_DEBUG(dbgs() << StageID
1149-
<< " stage successfully increased occupancy to "
1150-
<< DAG.MinOccupancy << '\n');
1151-
} else {
1152-
DAG.MinOccupancy = InitialOccupancy;
1153-
LLVM_DEBUG(dbgs() << StageID
1154-
<< ": No regions scheduled, resetting min occupancy to "
1155-
<< InitialOccupancy << "\n");
1156-
}
1148+
assert(IsAnyRegionScheduled);
1149+
LLVM_DEBUG(dbgs() << StageID
1150+
<< " stage successfully increased occupancy to "
1151+
<< DAG.MinOccupancy << '\n');
1152+
} else if (!IsAnyRegionScheduled) {
1153+
assert(DAG.MinOccupancy == InitialOccupancy);
1154+
LLVM_DEBUG(dbgs() << StageID
1155+
<< ": No regions scheduled, min occupancy stays at "
1156+
<< DAG.MinOccupancy << ", MFI occupancy stays at "
1157+
<< MFI.getOccupancy() << ".\n");
11571158
}
11581159

11591160
GCNSchedStage::finalizeGCNSchedStage();
@@ -1227,16 +1228,27 @@ bool UnclusteredHighRPStage::initGCNRegion() {
12271228
// rescheduling of previous regions did not make occupancy drop back down to
12281229
// the initial minimum).
12291230
unsigned DynamicVGPRBlockSize = DAG.MFI.getDynamicVGPRBlockSize();
1231+
// If no region has been scheduled yet, the DAG has not yet been updated with
1232+
// the occupancy target. So retrieve it from the temporary.
1233+
unsigned CurrentTargetOccupancy =
1234+
IsAnyRegionScheduled ? DAG.MinOccupancy : TempTargetOccupancy;
12301235
if (!DAG.RegionsWithExcessRP[RegionIdx] &&
1231-
(DAG.MinOccupancy <= InitialOccupancy ||
1236+
(CurrentTargetOccupancy <= InitialOccupancy ||
12321237
DAG.Pressure[RegionIdx].getOccupancy(ST, DynamicVGPRBlockSize) !=
12331238
InitialOccupancy))
12341239
return false;
12351240

1236-
bool IsRegionScheduled = GCNSchedStage::initGCNRegion();
1237-
if (!IsAnyRegionScheduled && IsRegionScheduled)
1241+
bool IsSchedulingThisRegion = GCNSchedStage::initGCNRegion();
1242+
// If this is the first region scheduled during this stage, make the target
1243+
// occupancy changes in the DAG and MFI.
1244+
if (!IsAnyRegionScheduled && IsSchedulingThisRegion) {
12381245
IsAnyRegionScheduled = true;
1239-
return IsRegionScheduled;
1246+
if (MFI.getMaxWavesPerEU() > DAG.MinOccupancy) {
1247+
DAG.MinOccupancy = TempTargetOccupancy;
1248+
MFI.increaseOccupancy(MF, TempTargetOccupancy);
1249+
}
1250+
}
1251+
return IsSchedulingThisRegion;
12401252
}
12411253

12421254
bool ClusteredLowOccStage::initGCNRegion() {

llvm/lib/Target/AMDGPU/GCNSchedStrategy.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -402,6 +402,8 @@ class UnclusteredHighRPStage : public GCNSchedStage {
402402
private:
403403
// Save the initial occupancy before starting this stage.
404404
unsigned InitialOccupancy;
405+
// Save the temporary target occupancy before starting this stage.
406+
unsigned TempTargetOccupancy;
405407
// Track whether any region was scheduled by this stage.
406408
bool IsAnyRegionScheduled;
407409

llvm/test/CodeGen/AMDGPU/schedule-regpressure-no-unclustered-regions.mir

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
# flexibility for RA.
1414

1515
# If Unclustered High RP Reschedule gets run, the following CHECK will have to be removed.
16-
# CHECK: Unclustered High Register Pressure Reschedule: No regions scheduled, resetting min occupancy
16+
# CHECK: Unclustered High Register Pressure Reschedule: No regions scheduled, min occupancy stays at 4, MFI occupancy stays at 4.
1717

1818
---
1919
name: no_sched_metric_due_to_spills

0 commit comments

Comments
 (0)