[llvm] [AMDGPU][RA] Avoid pending scheduler candidates near unified VGPR occupancy limits (PR #220975)
Alan Li via llvm-commits
llvm-commits at lists.llvm.org
Thu Sep 3 09:11:39 PDT 2026
https://github.com/lialan updated https://github.com/llvm/llvm-project/pull/220975
>From bd491a85b92c80ae7c4009d663e703d8e23a7287 Mon Sep 17 00:00:00 2001
From: Alan Li <me at alanli.org>
Date: Wed, 2 Sep 2026 14:46:48 -0700
Subject: [PATCH] fix(amdgpu): preserve unified VGPR occupancy
Pending nodes can extend live ranges across unified VGPR occupancy cliffs.
Refs #219377
---
llvm/lib/Target/AMDGPU/GCNSchedStrategy.cpp | 49 +++++++++---
llvm/lib/Target/AMDGPU/GCNSchedStrategy.h | 4 +
.../AMDGPU/schedule-unified-vgpr-occupancy.ll | 76 +++++++++++++++++++
3 files changed, 120 insertions(+), 9 deletions(-)
create mode 100644 llvm/test/CodeGen/AMDGPU/schedule-unified-vgpr-occupancy.ll
diff --git a/llvm/lib/Target/AMDGPU/GCNSchedStrategy.cpp b/llvm/lib/Target/AMDGPU/GCNSchedStrategy.cpp
index 3dd468772e9b1..ee603d4be7f80 100644
--- a/llvm/lib/Target/AMDGPU/GCNSchedStrategy.cpp
+++ b/llvm/lib/Target/AMDGPU/GCNSchedStrategy.cpp
@@ -84,6 +84,9 @@ static cl::opt<unsigned> PendingQueueLimit(
"Max (Available+Pending) size to inspect pending queue (0 disables)"),
cl::init(256));
+// Heuristic maximum VGPR pressure increase used near register limits.
+static constexpr unsigned MaxVGPRPressureInc = 16;
+
#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
#define DUMP_MAX_REG_PRESSURE
static cl::opt<bool> PrintMaxRPRegUsageBeforeScheduler(
@@ -386,7 +389,6 @@ void GCNSchedStrategy::initCandidate(SchedCandidate &Cand, SUnit *SU,
// only for VGPRs or only for SGPRs.
// FIXME: Better heuristics to determine whether to prefer SGPRs or VGPRs.
- const unsigned MaxVGPRPressureInc = 16;
bool ShouldTrackVGPRs = VGPRPressure + MaxVGPRPressureInc >= VGPRExcessLimit;
bool ShouldTrackSGPRs = !ShouldTrackVGPRs && SGPRPressure >= SGPRExcessLimit;
@@ -442,10 +444,13 @@ static bool shouldCheckPending(SchedBoundary &Zone,
}
static SUnit *pickOnlyChoice(SchedBoundary &Zone,
- const TargetSchedModel *SchedModel) {
- // pickOnlyChoice() releases pending instructions and checks for new hazards.
+ const TargetSchedModel *SchedModel,
+ bool AllowPendingCandidates) {
+ // Keep queue maintenance and hazard checks active when nodes still in
+ // Pending are excluded from candidate selection.
SUnit *OnlyChoice = Zone.pickOnlyChoice();
- if (!shouldCheckPending(Zone, SchedModel) || Zone.Pending.empty())
+ if (!AllowPendingCandidates || !shouldCheckPending(Zone, SchedModel) ||
+ Zone.Pending.empty())
return OnlyChoice;
return nullptr;
@@ -512,7 +517,7 @@ void GCNSchedStrategy::pickNodeFromQueue(SchedBoundary &Zone,
}
}
- if (!shouldCheckPending(Zone, SchedModel))
+ if (!AllowPendingCandidates || !shouldCheckPending(Zone, SchedModel))
return;
LLVM_DEBUG(dbgs() << "Pending Q:\n");
@@ -544,11 +549,11 @@ SUnit *GCNSchedStrategy::pickNodeBidirectional(bool &IsTopNode,
bool &PickedPending) {
// Schedule as far as possible in the direction of no choice. This is most
// efficient, but also provides the best heuristics for CriticalPSets.
- if (SUnit *SU = pickOnlyChoice(Bot, SchedModel)) {
+ if (SUnit *SU = pickOnlyChoice(Bot, SchedModel, AllowPendingCandidates)) {
IsTopNode = false;
return SU;
}
- if (SUnit *SU = pickOnlyChoice(Top, SchedModel)) {
+ if (SUnit *SU = pickOnlyChoice(Top, SchedModel, AllowPendingCandidates)) {
IsTopNode = true;
return SU;
}
@@ -648,7 +653,7 @@ SUnit *GCNSchedStrategy::pickNode(bool &IsTopNode) {
do {
PickedPending = false;
if (RegionPolicy.OnlyTopDown) {
- SU = pickOnlyChoice(Top, SchedModel);
+ SU = pickOnlyChoice(Top, SchedModel, AllowPendingCandidates);
if (!SU) {
CandPolicy NoPolicy;
TopCand.reset(NoPolicy);
@@ -660,7 +665,7 @@ SUnit *GCNSchedStrategy::pickNode(bool &IsTopNode) {
}
IsTopNode = true;
} else if (RegionPolicy.OnlyBottomUp) {
- SU = pickOnlyChoice(Bot, SchedModel);
+ SU = pickOnlyChoice(Bot, SchedModel, AllowPendingCandidates);
if (!SU) {
CandPolicy NoPolicy;
BotCand.reset(NoPolicy);
@@ -1832,6 +1837,32 @@ bool GCNSchedStage::initGCNRegion() {
PressureBefore = DAG.Pressure[RegionIdx];
+ S.AllowPendingCandidates = true;
+
+ // Only adjust the initial max-occupancy schedule, and do not override the
+ // explicit scheduling policy of an IGLP region.
+ if (StageID == GCNSchedStageID::OccInitialSchedule && ST.hasGFX90AInsts() &&
+ !DAG.RegionsWithIGLPInstrs[RegionIdx]) {
+ unsigned DynamicVGPRBlockSize = MFI.getDynamicVGPRBlockSize();
+ unsigned RegionOccupancy =
+ std::min(DAG.MinOccupancy,
+ PressureBefore.getOccupancy(ST, DynamicVGPRBlockSize));
+
+ // Pending candidates may extend live ranges. Round the estimate to the
+ // hardware allocation granule and keep the final block for the current
+ // occupancy in reserve. At one wave there is no lower occupancy to protect.
+ if (RegionOccupancy > 1) {
+ unsigned UnifiedVGPRPressure =
+ PressureBefore.getVGPRNum(/*UnifiedVGPRFile=*/true);
+ unsigned EstimatedUnifiedVGPRPressure =
+ alignTo(UnifiedVGPRPressure + MaxVGPRPressureInc + S.ErrorMargin,
+ ST.getVGPRAllocGranule(DynamicVGPRBlockSize));
+ unsigned MaxVGPRs =
+ ST.getMaxNumVGPRs(RegionOccupancy, DynamicVGPRBlockSize);
+ S.AllowPendingCandidates = EstimatedUnifiedVGPRPressure < MaxVGPRs;
+ }
+ }
+
LLVM_DEBUG(
dbgs() << "Pressure before scheduling:\nRegion live-ins:"
<< print(DAG.LiveIns[RegionIdx], DAG.MRI)
diff --git a/llvm/lib/Target/AMDGPU/GCNSchedStrategy.h b/llvm/lib/Target/AMDGPU/GCNSchedStrategy.h
index 2059f4e6479ff..6de8e7725fa23 100644
--- a/llvm/lib/Target/AMDGPU/GCNSchedStrategy.h
+++ b/llvm/lib/Target/AMDGPU/GCNSchedStrategy.h
@@ -121,6 +121,10 @@ class GCNSchedStrategy : public GenericScheduler {
// increasing ILP and preserving VGPRs.
bool KnownExcessRP = false;
+ // Whether nodes still in Pending may be selected in the current region.
+ // Queue maintenance remains enabled.
+ bool AllowPendingCandidates = true;
+
// An error margin is necessary because of poor performance of the generic RP
// tracker and can be adjusted up for tuning heuristics to try and more
// aggressively reduce register pressure.
diff --git a/llvm/test/CodeGen/AMDGPU/schedule-unified-vgpr-occupancy.ll b/llvm/test/CodeGen/AMDGPU/schedule-unified-vgpr-occupancy.ll
new file mode 100644
index 0000000000000..4b8628e78083a
--- /dev/null
+++ b/llvm/test/CodeGen/AMDGPU/schedule-unified-vgpr-occupancy.ll
@@ -0,0 +1,76 @@
+; NOTE: Do not autogenerate. This test intentionally checks only resource usage.
+; RUN: llc -mtriple=amdgcn-amd-amdhsa -mcpu=gfx950 -verify-machineinstrs < %s | FileCheck %s
+
+; Regression test for https://github.com/llvm/llvm-project/issues/219377#issuecomment-5510810499.
+; The loop's original pressure is 45 VGPRs. With an 8-register allocation
+; granule, alignTo(45 + 16 + 3, 8) equals the 64-register occupancy boundary.
+; Reserving that final granule stops pending nodes from extending live ranges.
+
+; CHECK-LABEL: avoid_unified_vgpr_occupancy_cliff:
+; CHECK: ; NumVgprs: 64{{$}}
+; CHECK-NEXT: ; NumAgprs: 0{{$}}
+; CHECK-NEXT: ; TotalNumVgprs: 64{{$}}
+; CHECK-NEXT: ; ScratchSize: 0{{$}}
+; CHECK: ; Occupancy: 8{{$}}
+
+define amdgpu_kernel void @avoid_unified_vgpr_occupancy_cliff(<8 x bfloat> %matrix0, <8 x bfloat> %matrix1, <8 x bfloat> %matrix2) {
+entry:
+ br label %loop
+
+loop: ; preds = %loop, %entry
+ %acc0 = phi float [ 0.000000e+00, %entry ], [ %next.acc0, %loop ]
+ %acc1 = phi float [ 0.000000e+00, %entry ], [ %next.acc1, %loop ]
+ %acc2 = phi float [ 0.000000e+00, %entry ], [ %next.acc2, %loop ]
+ %acc3 = phi float [ 0.000000e+00, %entry ], [ %next.acc3, %loop ]
+ %acc0.insert0 = insertelement <4 x float> zeroinitializer, float %acc0, i64 0
+ %acc0.insert1 = insertelement <4 x float> %acc0.insert0, float %acc0, i64 1
+ %acc0.seed = insertelement <4 x float> %acc0.insert1, float 0.000000e+00, i64 0
+ %chain0.0 = tail call <4 x float> @llvm.amdgcn.mfma.f32.16x16x32.bf16(<8 x bfloat> zeroinitializer, <8 x bfloat> zeroinitializer, <4 x float> %acc0.seed, i32 0, i32 0, i32 0)
+ %acc1.insert0 = insertelement <4 x float> zeroinitializer, float %acc1, i64 0
+ %acc1.insert1 = insertelement <4 x float> %acc1.insert0, float %acc1, i64 1
+ %acc1.seed = insertelement <4 x float> %acc1.insert1, float 0.000000e+00, i64 0
+ %chain1.0 = tail call <4 x float> @llvm.amdgcn.mfma.f32.16x16x32.bf16(<8 x bfloat> %matrix0, <8 x bfloat> zeroinitializer, <4 x float> %acc1.seed, i32 0, i32 0, i32 0)
+ %acc2.insert0 = insertelement <4 x float> zeroinitializer, float %acc2, i64 0
+ %acc2.seed = insertelement <4 x float> %acc2.insert0, float %acc2, i64 0
+ %acc3.insert0 = insertelement <4 x float> zeroinitializer, float %acc3, i64 0
+ %acc3.seed = insertelement <4 x float> %acc3.insert0, float %acc3, i64 0
+ %chain3.0 = tail call <4 x float> @llvm.amdgcn.mfma.f32.16x16x32.bf16(<8 x bfloat> %matrix1, <8 x bfloat> zeroinitializer, <4 x float> %acc3.seed, i32 0, i32 0, i32 0)
+ %chain0.1 = tail call <4 x float> @llvm.amdgcn.mfma.f32.16x16x32.bf16(<8 x bfloat> zeroinitializer, <8 x bfloat> zeroinitializer, <4 x float> %chain0.0, i32 0, i32 0, i32 0)
+ %chain1.1 = tail call <4 x float> @llvm.amdgcn.mfma.f32.16x16x32.bf16(<8 x bfloat> %matrix2, <8 x bfloat> zeroinitializer, <4 x float> %chain1.0, i32 0, i32 0, i32 0)
+ %chain2.0 = tail call <4 x float> @llvm.amdgcn.mfma.f32.16x16x32.bf16(<8 x bfloat> <bfloat 0.000000e+00, bfloat 0.000000e+00, bfloat 0.000000e+00, bfloat 0.000000e+00, bfloat 1.000000e+00, bfloat 1.000000e+00, bfloat 1.000000e+00, bfloat 1.000000e+00>, <8 x bfloat> zeroinitializer, <4 x float> %acc2.seed, i32 0, i32 0, i32 0)
+ %chain2.1 = tail call <4 x float> @llvm.amdgcn.mfma.f32.16x16x32.bf16(<8 x bfloat> splat (bfloat 1.000000e+00), <8 x bfloat> zeroinitializer, <4 x float> %chain2.0, i32 0, i32 0, i32 0)
+ ; Preserve the scheduling-region boundary and reserve low VGPRs so the
+ ; allocation difference straddles the 64-register occupancy boundary.
+ tail call void asm sideeffect "", "~{v[0:15]},~{v[16:23]}"()
+ %chain0.2 = tail call <4 x float> @llvm.amdgcn.mfma.f32.16x16x32.bf16(<8 x bfloat> zeroinitializer, <8 x bfloat> zeroinitializer, <4 x float> %chain0.1, i32 0, i32 0, i32 0)
+ %chain0.3 = tail call <4 x float> @llvm.amdgcn.mfma.f32.16x16x32.bf16(<8 x bfloat> zeroinitializer, <8 x bfloat> zeroinitializer, <4 x float> %chain0.2, i32 0, i32 0, i32 0)
+ %chain0.4 = tail call <4 x float> @llvm.amdgcn.mfma.f32.16x16x32.bf16(<8 x bfloat> zeroinitializer, <8 x bfloat> zeroinitializer, <4 x float> %chain0.3, i32 0, i32 0, i32 0)
+ %chain0.5 = tail call <4 x float> @llvm.amdgcn.mfma.f32.16x16x32.bf16(<8 x bfloat> zeroinitializer, <8 x bfloat> zeroinitializer, <4 x float> %chain0.4, i32 0, i32 0, i32 0)
+ %chain0.6 = tail call <4 x float> @llvm.amdgcn.mfma.f32.16x16x32.bf16(<8 x bfloat> zeroinitializer, <8 x bfloat> zeroinitializer, <4 x float> %chain0.5, i32 0, i32 0, i32 0)
+ %chain0.7 = tail call <4 x float> @llvm.amdgcn.mfma.f32.16x16x32.bf16(<8 x bfloat> zeroinitializer, <8 x bfloat> zeroinitializer, <4 x float> %chain0.6, i32 0, i32 0, i32 0)
+ %next.acc0 = extractelement <4 x float> %chain0.7, i64 0
+ %chain1.2 = tail call <4 x float> @llvm.amdgcn.mfma.f32.16x16x32.bf16(<8 x bfloat> zeroinitializer, <8 x bfloat> zeroinitializer, <4 x float> %chain1.1, i32 0, i32 0, i32 0)
+ %chain1.3 = tail call <4 x float> @llvm.amdgcn.mfma.f32.16x16x32.bf16(<8 x bfloat> zeroinitializer, <8 x bfloat> zeroinitializer, <4 x float> %chain1.2, i32 0, i32 0, i32 0)
+ %chain1.4 = tail call <4 x float> @llvm.amdgcn.mfma.f32.16x16x32.bf16(<8 x bfloat> zeroinitializer, <8 x bfloat> zeroinitializer, <4 x float> %chain1.3, i32 0, i32 0, i32 0)
+ %chain1.5 = tail call <4 x float> @llvm.amdgcn.mfma.f32.16x16x32.bf16(<8 x bfloat> zeroinitializer, <8 x bfloat> zeroinitializer, <4 x float> %chain1.4, i32 0, i32 0, i32 0)
+ %chain1.6 = tail call <4 x float> @llvm.amdgcn.mfma.f32.16x16x32.bf16(<8 x bfloat> zeroinitializer, <8 x bfloat> zeroinitializer, <4 x float> %chain1.5, i32 0, i32 0, i32 0)
+ %chain1.7 = tail call <4 x float> @llvm.amdgcn.mfma.f32.16x16x32.bf16(<8 x bfloat> zeroinitializer, <8 x bfloat> zeroinitializer, <4 x float> %chain1.6, i32 0, i32 0, i32 0)
+ %next.acc1 = extractelement <4 x float> %chain1.7, i64 0
+ %chain2.2 = tail call <4 x float> @llvm.amdgcn.mfma.f32.16x16x32.bf16(<8 x bfloat> zeroinitializer, <8 x bfloat> zeroinitializer, <4 x float> %chain2.1, i32 0, i32 0, i32 0)
+ %chain2.3 = tail call <4 x float> @llvm.amdgcn.mfma.f32.16x16x32.bf16(<8 x bfloat> zeroinitializer, <8 x bfloat> zeroinitializer, <4 x float> %chain2.2, i32 0, i32 0, i32 0)
+ %chain2.4 = tail call <4 x float> @llvm.amdgcn.mfma.f32.16x16x32.bf16(<8 x bfloat> zeroinitializer, <8 x bfloat> zeroinitializer, <4 x float> %chain2.3, i32 0, i32 0, i32 0)
+ %chain2.5 = tail call <4 x float> @llvm.amdgcn.mfma.f32.16x16x32.bf16(<8 x bfloat> zeroinitializer, <8 x bfloat> zeroinitializer, <4 x float> %chain2.4, i32 0, i32 0, i32 0)
+ %chain2.6 = tail call <4 x float> @llvm.amdgcn.mfma.f32.16x16x32.bf16(<8 x bfloat> zeroinitializer, <8 x bfloat> zeroinitializer, <4 x float> %chain2.5, i32 0, i32 0, i32 0)
+ %chain2.7 = tail call <4 x float> @llvm.amdgcn.mfma.f32.16x16x32.bf16(<8 x bfloat> zeroinitializer, <8 x bfloat> zeroinitializer, <4 x float> %chain2.6, i32 0, i32 0, i32 0)
+ %next.acc2 = extractelement <4 x float> %chain2.7, i64 0
+ %chain3.1 = tail call <4 x float> @llvm.amdgcn.mfma.f32.16x16x32.bf16(<8 x bfloat> zeroinitializer, <8 x bfloat> zeroinitializer, <4 x float> %chain3.0, i32 0, i32 0, i32 0)
+ %chain3.2 = tail call <4 x float> @llvm.amdgcn.mfma.f32.16x16x32.bf16(<8 x bfloat> zeroinitializer, <8 x bfloat> zeroinitializer, <4 x float> %chain3.1, i32 0, i32 0, i32 0)
+ %chain3.3 = tail call <4 x float> @llvm.amdgcn.mfma.f32.16x16x32.bf16(<8 x bfloat> zeroinitializer, <8 x bfloat> zeroinitializer, <4 x float> %chain3.2, i32 0, i32 0, i32 0)
+ %chain3.4 = tail call <4 x float> @llvm.amdgcn.mfma.f32.16x16x32.bf16(<8 x bfloat> zeroinitializer, <8 x bfloat> zeroinitializer, <4 x float> %chain3.3, i32 0, i32 0, i32 0)
+ %chain3.5 = tail call <4 x float> @llvm.amdgcn.mfma.f32.16x16x32.bf16(<8 x bfloat> zeroinitializer, <8 x bfloat> zeroinitializer, <4 x float> %chain3.4, i32 0, i32 0, i32 0)
+ %chain3.6 = tail call <4 x float> @llvm.amdgcn.mfma.f32.16x16x32.bf16(<8 x bfloat> zeroinitializer, <8 x bfloat> zeroinitializer, <4 x float> %chain3.5, i32 0, i32 0, i32 0)
+ %next.acc3 = extractelement <4 x float> %chain3.6, i64 0
+ br label %loop
+}
+
+declare <4 x float> @llvm.amdgcn.mfma.f32.16x16x32.bf16(<8 x bfloat>, <8 x bfloat>, <4 x float>, i32 immarg, i32 immarg, i32 immarg)
More information about the llvm-commits
mailing list