[llvm] [AMDGPU] AMDGPUIGroupLP: Avoid repeating reachability checks in greedy algorithm (PR #182463)
Frederik Harwath via llvm-commits
llvm-commits at lists.llvm.org
Tue Mar 3 01:03:34 PST 2026
https://github.com/frederik-h updated https://github.com/llvm/llvm-project/pull/182463
>From 03ca583cdd87d392262d086026d9ddb6ea7ce312 Mon Sep 17 00:00:00 2001
From: Frederik Harwath <fharwath at amd.com>
Date: Tue, 17 Feb 2026 03:23:53 -0500
Subject: [PATCH 01/11] [AMDGPU] AMDGPUIGroupLP: Avoid repeating reachability
checks in greedy algorithm
In the greedy pipeline solver, the group cost is found using the
addEdges function and the edges must be removed from the DAG after
processing each group. The best group edges are then re-inserted using
the same function. This repeats the costly reachability checks which
become problematic for pipelines with many SchedGroups.
The algorithm is changed to remember the best group edges instead of
recomputing them. Additionally, SchedGroup::tryAddEdge is refactored
to avoid a redundant cycle checks which is already performed by
DAG->addEdge.
---
llvm/lib/Target/AMDGPU/AMDGPUIGroupLP.cpp | 39 ++++++++++++++++-------
1 file changed, 28 insertions(+), 11 deletions(-)
diff --git a/llvm/lib/Target/AMDGPU/AMDGPUIGroupLP.cpp b/llvm/lib/Target/AMDGPU/AMDGPUIGroupLP.cpp
index 10ffbe281beac..15574d6686cba 100644
--- a/llvm/lib/Target/AMDGPU/AMDGPUIGroupLP.cpp
+++ b/llvm/lib/Target/AMDGPU/AMDGPUIGroupLP.cpp
@@ -115,6 +115,13 @@ class InstructionRule {
using SUnitsToCandidateSGsMap = DenseMap<SUnit *, SmallVector<int, 4>>;
+namespace {
+/// Try to add and edge from SU \p A to SU \p B to the \p DAG.
+bool tryAddEdge(ScheduleDAGInstrs *DAG, SUnit *A, SUnit *B) {
+ return A != B && DAG->addEdge(B, SDep(A, SDep::Artificial));
+}
+} // namespace
+
// Classify instructions into groups to enable fine tuned control over the
// scheduler. These groups may be more specific than current SchedModel
// instruction classes.
@@ -702,6 +709,7 @@ void PipelineSolver::greedyFind(
int TempCost;
SchedGroup *BestGroup = nullptr;
int BestGroupID = -1;
+ std::vector<std::pair<SUnit *, SUnit *>> BestEdges;
auto &SyncPipeline = CurrPipeline[CurrSyncGroupIdx];
LLVM_DEBUG(dbgs() << "Fitting SU(" << CurrSU.first->NodeNum
<< ") in Pipeline # " << CurrSyncGroupIdx << "\n");
@@ -711,7 +719,6 @@ void PipelineSolver::greedyFind(
// first. If we fail to do this for the greedy algorithm, the solution will
// likely not be good in more complex cases.
for (; I != E; ++I) {
- std::vector<std::pair<SUnit *, SUnit *>> AddedEdges;
int CandSGID = *I;
SchedGroup *Match = llvm::find_if(SyncPipeline, [CandSGID](SchedGroup &SG) {
return SG.getSGID() == CandSGID;
@@ -729,21 +736,35 @@ void PipelineSolver::greedyFind(
LLVM_DEBUG(dbgs() << "SGID # " << CandSGID << " has conflicting rule\n");
continue;
}
- TempCost = addEdges(SyncPipeline, CurrSU.first, CandSGID, AddedEdges);
+
+ std::vector<std::pair<SUnit *, SUnit *>> TempEdges;
+ TempCost = addEdges(SyncPipeline, CurrSU.first, CandSGID, TempEdges);
LLVM_DEBUG(dbgs() << "Cost of Group " << TempCost << "\n");
+
if (TempCost < BestNodeCost || BestNodeCost == -1) {
+ BestEdges = TempEdges;
BestGroup = Match;
BestNodeCost = TempCost;
BestGroupID = CandSGID;
+
+ if (BestNodeCost == 0)
+ break;
+
+ removeEdges(BestEdges);
}
- removeEdges(AddedEdges);
- if (BestNodeCost == 0)
- break;
+
+ removeEdges(TempEdges);
}
if (BestGroupID != -1) {
BestGroup->add(*CurrSU.first);
- addEdges(SyncPipeline, CurrSU.first, BestGroupID, AddedEdges);
+
+ for (auto &E : BestEdges) {
+ AddedEdges.push_back(E);
+ [[maybe_unused]] bool Added = tryAddEdge(DAG, E.first, E.second);
+ assert(Added && "Edges known to be insertable.");
+ }
+
LLVM_DEBUG(dbgs() << "Best Group has ID: " << BestGroupID << " and Mask"
<< (int)BestGroup->getMask() << "\n");
BestCost += TempCost;
@@ -2381,11 +2402,7 @@ class IGroupLPDAGMutation : public ScheduleDAGMutation {
unsigned SchedGroup::NumSchedGroups = 0;
bool SchedGroup::tryAddEdge(SUnit *A, SUnit *B) {
- if (A != B && DAG->canAddEdge(B, A)) {
- DAG->addEdge(B, SDep(A, SDep::Artificial));
- return true;
- }
- return false;
+ return ::tryAddEdge(DAG, A, B);
}
bool SchedGroup::canAddMI(const MachineInstr &MI) const {
>From 0590c7d65b86e1c0d6e108b936af5dd4b679d036 Mon Sep 17 00:00:00 2001
From: Frederik Harwath <fharwath at amd.com>
Date: Fri, 20 Feb 2026 05:40:52 -0500
Subject: [PATCH 02/11] Review changes
---
llvm/lib/Target/AMDGPU/AMDGPUIGroupLP.cpp | 8 +++-----
1 file changed, 3 insertions(+), 5 deletions(-)
diff --git a/llvm/lib/Target/AMDGPU/AMDGPUIGroupLP.cpp b/llvm/lib/Target/AMDGPU/AMDGPUIGroupLP.cpp
index 15574d6686cba..830642b1d7981 100644
--- a/llvm/lib/Target/AMDGPU/AMDGPUIGroupLP.cpp
+++ b/llvm/lib/Target/AMDGPU/AMDGPUIGroupLP.cpp
@@ -115,12 +115,10 @@ class InstructionRule {
using SUnitsToCandidateSGsMap = DenseMap<SUnit *, SmallVector<int, 4>>;
-namespace {
/// Try to add and edge from SU \p A to SU \p B to the \p DAG.
-bool tryAddEdge(ScheduleDAGInstrs *DAG, SUnit *A, SUnit *B) {
+static bool tryAddEdge(ScheduleDAGInstrs *DAG, SUnit *A, SUnit *B) {
return A != B && DAG->addEdge(B, SDep(A, SDep::Artificial));
}
-} // namespace
// Classify instructions into groups to enable fine tuned control over the
// scheduler. These groups may be more specific than current SchedModel
@@ -761,8 +759,8 @@ void PipelineSolver::greedyFind(
for (auto &E : BestEdges) {
AddedEdges.push_back(E);
- [[maybe_unused]] bool Added = tryAddEdge(DAG, E.first, E.second);
- assert(Added && "Edges known to be insertable.");
+ if (!tryAddEdge(DAG, E.first, E.second))
+ llvm_unreachable("Edges known to be insertable.");
}
LLVM_DEBUG(dbgs() << "Best Group has ID: " << BestGroupID << " and Mask"
>From 37698db15bebcddb9d10c273cbe5643f5855572a Mon Sep 17 00:00:00 2001
From: Frederik Harwath <fharwath at amd.com>
Date: Fri, 20 Feb 2026 07:13:50 -0500
Subject: [PATCH 03/11] Use list instead of vector for AddedEdges
---
llvm/lib/Target/AMDGPU/AMDGPUIGroupLP.cpp | 43 +++++++++++------------
1 file changed, 21 insertions(+), 22 deletions(-)
diff --git a/llvm/lib/Target/AMDGPU/AMDGPUIGroupLP.cpp b/llvm/lib/Target/AMDGPU/AMDGPUIGroupLP.cpp
index 830642b1d7981..cde3b91a0e0d2 100644
--- a/llvm/lib/Target/AMDGPU/AMDGPUIGroupLP.cpp
+++ b/llvm/lib/Target/AMDGPU/AMDGPUIGroupLP.cpp
@@ -171,7 +171,7 @@ class SchedGroup {
// Add DAG dependencies and track which edges are added, and the count of
// missed edges
int link(SUnit &SU, bool MakePred,
- std::vector<std::pair<SUnit *, SUnit *>> &AddedEdges);
+ std::list<std::pair<SUnit *, SUnit *>> &AddedEdges);
// Add DAG dependencies from all SUnits in this SchedGroup and this SU.
// Use the predicate to determine whether SU should be a predecessor (P =
@@ -312,8 +312,7 @@ class PipelineSolver {
// current information. One step in the greedy algorithm. Templated against
// the SchedGroup iterator (either reverse or forward).
template <typename T>
- void greedyFind(std::vector<std::pair<SUnit *, SUnit *>> &AddedEdges, T I,
- T E);
+ void greedyFind(std::list<std::pair<SUnit *, SUnit *>> &AddedEdges, T I, T E);
// Whether or not the current solution is optimal
bool checkOptimal();
// Populate the ready list, prioiritizing fewest missed edges first
@@ -329,15 +328,15 @@ class PipelineSolver {
// Add the edges from the SU to the other SchedGroups in pipeline, and
// return the number of edges missed.
int addEdges(SmallVectorImpl<SchedGroup> &SyncPipeline, SUnit *SU, int SGID,
- std::vector<std::pair<SUnit *, SUnit *>> &AddedEdges);
+ std::list<std::pair<SUnit *, SUnit *>> &AddedEdges);
/// Link the pipeline as if \p SU was in the SchedGroup with ID \p SGID. It
/// returns the cost (in terms of missed pipeline edges), and tracks the edges
/// added in \p AddedEdges
template <typename T>
int linkSUnit(SUnit *SU, int SGID,
- std::vector<std::pair<SUnit *, SUnit *>> &AddedEdges, T I, T E);
+ std::list<std::pair<SUnit *, SUnit *>> &AddedEdges, T I, T E);
/// Remove the edges passed via \p AddedEdges
- void removeEdges(const std::vector<std::pair<SUnit *, SUnit *>> &AddedEdges);
+ void removeEdges(const std::list<std::pair<SUnit *, SUnit *>> &AddedEdges);
// Convert the passed in maps to arrays for bidirectional iterators
void convertSyncMapsToArrays();
@@ -461,7 +460,7 @@ void PipelineSolver::makePipeline() {
template <typename T>
int PipelineSolver::linkSUnit(
- SUnit *SU, int SGID, std::vector<std::pair<SUnit *, SUnit *>> &AddedEdges,
+ SUnit *SU, int SGID, std::list<std::pair<SUnit *, SUnit *>> &AddedEdges,
T I, T E) {
bool MakePred = false;
int AddedCost = 0;
@@ -479,7 +478,7 @@ int PipelineSolver::linkSUnit(
int PipelineSolver::addEdges(
SmallVectorImpl<SchedGroup> &SyncPipeline, SUnit *SU, int SGID,
- std::vector<std::pair<SUnit *, SUnit *>> &AddedEdges) {
+ std::list<std::pair<SUnit *, SUnit *>> &AddedEdges) {
// For IsBottomUp, the first SchedGroup in SyncPipeline contains the
// instructions that are the ultimate successors in the resultant mutation.
@@ -496,7 +495,7 @@ int PipelineSolver::addEdges(
}
void PipelineSolver::removeEdges(
- const std::vector<std::pair<SUnit *, SUnit *>> &EdgesToRemove) {
+ const std::list<std::pair<SUnit *, SUnit *>> &EdgesToRemove) {
// Only remove the edges that we have added when testing
// the fit.
for (auto &PredSuccPair : EdgesToRemove) {
@@ -575,7 +574,7 @@ void PipelineSolver::populateReadyList(
assert(CurrSU.second.size() >= 1);
for (; I != E; ++I) {
- std::vector<std::pair<SUnit *, SUnit *>> AddedEdges;
+ std::list<std::pair<SUnit *, SUnit *>> AddedEdges;
int CandSGID = *I;
SchedGroup *Match = llvm::find_if(SyncPipeline, [CandSGID](SchedGroup &SG) {
return SG.getSGID() == CandSGID;
@@ -634,7 +633,7 @@ bool PipelineSolver::solveExact() {
int CandSGID = I->first;
int AddedCost = 0;
- std::vector<std::pair<SUnit *, SUnit *>> AddedEdges;
+ std::list<std::pair<SUnit *, SUnit *>> AddedEdges;
auto &SyncPipeline = CurrPipeline[CurrSyncGroupIdx];
SchedGroup *Match;
for (auto &SG : SyncPipeline) {
@@ -701,13 +700,13 @@ bool PipelineSolver::solveExact() {
template <typename T>
void PipelineSolver::greedyFind(
- std::vector<std::pair<SUnit *, SUnit *>> &AddedEdges, T I, T E) {
+ std::list<std::pair<SUnit *, SUnit *>> &AddedEdges, T I, T E) {
SUToCandSGsPair CurrSU = PipelineInstrs[CurrSyncGroupIdx][CurrConflInstNo];
int BestNodeCost = -1;
int TempCost;
SchedGroup *BestGroup = nullptr;
int BestGroupID = -1;
- std::vector<std::pair<SUnit *, SUnit *>> BestEdges;
+ std::list<std::pair<SUnit *, SUnit *>> BestEdges;
auto &SyncPipeline = CurrPipeline[CurrSyncGroupIdx];
LLVM_DEBUG(dbgs() << "Fitting SU(" << CurrSU.first->NodeNum
<< ") in Pipeline # " << CurrSyncGroupIdx << "\n");
@@ -735,7 +734,7 @@ void PipelineSolver::greedyFind(
continue;
}
- std::vector<std::pair<SUnit *, SUnit *>> TempEdges;
+ std::list<std::pair<SUnit *, SUnit *>> TempEdges;
TempCost = addEdges(SyncPipeline, CurrSU.first, CandSGID, TempEdges);
LLVM_DEBUG(dbgs() << "Cost of Group " << TempCost << "\n");
@@ -756,12 +755,12 @@ void PipelineSolver::greedyFind(
if (BestGroupID != -1) {
BestGroup->add(*CurrSU.first);
-
- for (auto &E : BestEdges) {
- AddedEdges.push_back(E);
- if (!tryAddEdge(DAG, E.first, E.second))
- llvm_unreachable("Edges known to be insertable.");
- }
+ AddedEdges.splice(AddedEdges.end(), BestEdges);
+ std::for_each(BestEdges.begin(), BestEdges.end(),
+ [this](std::pair<SUnit *, SUnit *> E) {
+ if (!tryAddEdge(DAG, E.first, E.second))
+ llvm_unreachable("Edges known to be insertable.");
+ });
LLVM_DEBUG(dbgs() << "Best Group has ID: " << BestGroupID << " and Mask"
<< (int)BestGroup->getMask() << "\n");
@@ -774,7 +773,7 @@ void PipelineSolver::greedyFind(
bool PipelineSolver::solveGreedy() {
BestCost = 0;
- std::vector<std::pair<SUnit *, SUnit *>> AddedEdges;
+ std::list<std::pair<SUnit *, SUnit *>> AddedEdges;
while (static_cast<size_t>(CurrSyncGroupIdx) < PipelineInstrs.size()) {
SUToCandSGsPair CurrSU = PipelineInstrs[CurrSyncGroupIdx][CurrConflInstNo];
@@ -2520,7 +2519,7 @@ bool SchedGroup::canAddMI(const MachineInstr &MI) const {
}
int SchedGroup::link(SUnit &SU, bool MakePred,
- std::vector<std::pair<SUnit *, SUnit *>> &AddedEdges) {
+ std::list<std::pair<SUnit *, SUnit *>> &AddedEdges) {
int MissedEdges = 0;
for (auto *A : Collection) {
SUnit *B = &SU;
>From bcd7199d0c010946d41b98f47897db4cb68ff97a Mon Sep 17 00:00:00 2001
From: Frederik Harwath <fharwath at amd.com>
Date: Fri, 20 Feb 2026 08:38:25 -0500
Subject: [PATCH 04/11] fixup! Use list instead of vector for AddedEdges
---
llvm/lib/Target/AMDGPU/AMDGPUIGroupLP.cpp | 5 ++++-
1 file changed, 4 insertions(+), 1 deletion(-)
diff --git a/llvm/lib/Target/AMDGPU/AMDGPUIGroupLP.cpp b/llvm/lib/Target/AMDGPU/AMDGPUIGroupLP.cpp
index cde3b91a0e0d2..2596267a70228 100644
--- a/llvm/lib/Target/AMDGPU/AMDGPUIGroupLP.cpp
+++ b/llvm/lib/Target/AMDGPU/AMDGPUIGroupLP.cpp
@@ -755,7 +755,10 @@ void PipelineSolver::greedyFind(
if (BestGroupID != -1) {
BestGroup->add(*CurrSU.first);
- AddedEdges.splice(AddedEdges.end(), BestEdges);
+ if (AddedEdges.empty())
+ AddedEdges = BestEdges;
+ else
+ AddedEdges.splice(std::prev(AddedEdges.cend()), BestEdges);
std::for_each(BestEdges.begin(), BestEdges.end(),
[this](std::pair<SUnit *, SUnit *> E) {
if (!tryAddEdge(DAG, E.first, E.second))
>From d38a18636a7879f837b4baa175b1a71bcaa5a7b4 Mon Sep 17 00:00:00 2001
From: Frederik Harwath <fharwath at amd.com>
Date: Mon, 2 Mar 2026 04:09:17 -0500
Subject: [PATCH 05/11] Review changes
- inline ::tryAddEdge in SchedGroup::tryAddEdge again
- Use single call to removeEdge
---
llvm/lib/Target/AMDGPU/AMDGPUIGroupLP.cpp | 19 ++++++-------------
1 file changed, 6 insertions(+), 13 deletions(-)
diff --git a/llvm/lib/Target/AMDGPU/AMDGPUIGroupLP.cpp b/llvm/lib/Target/AMDGPU/AMDGPUIGroupLP.cpp
index 2596267a70228..de6c838e3e827 100644
--- a/llvm/lib/Target/AMDGPU/AMDGPUIGroupLP.cpp
+++ b/llvm/lib/Target/AMDGPU/AMDGPUIGroupLP.cpp
@@ -115,11 +115,6 @@ class InstructionRule {
using SUnitsToCandidateSGsMap = DenseMap<SUnit *, SmallVector<int, 4>>;
-/// Try to add and edge from SU \p A to SU \p B to the \p DAG.
-static bool tryAddEdge(ScheduleDAGInstrs *DAG, SUnit *A, SUnit *B) {
- return A != B && DAG->addEdge(B, SDep(A, SDep::Artificial));
-}
-
// Classify instructions into groups to enable fine tuned control over the
// scheduler. These groups may be more specific than current SchedModel
// instruction classes.
@@ -146,9 +141,6 @@ class SchedGroup {
// Count of the number of created SchedGroups, used to initialize SGID.
static unsigned NumSchedGroups;
- // Try to add and edge from SU A to SU B.
- bool tryAddEdge(SUnit *A, SUnit *B);
-
// Use SGMask to determine whether we can classify MI as a member of this
// SchedGroup object.
bool canAddMI(const MachineInstr &MI) const;
@@ -160,6 +152,9 @@ class SchedGroup {
ScheduleDAGInstrs *DAG;
const SIInstrInfo *TII;
+ // Try to add and edge from SU A to SU B.
+ bool tryAddEdge(SUnit *A, SUnit *B);
+
// Returns true if SU can be added to this SchedGroup.
bool canAddSU(SUnit &SU) const;
@@ -746,8 +741,6 @@ void PipelineSolver::greedyFind(
if (BestNodeCost == 0)
break;
-
- removeEdges(BestEdges);
}
removeEdges(TempEdges);
@@ -760,8 +753,8 @@ void PipelineSolver::greedyFind(
else
AddedEdges.splice(std::prev(AddedEdges.cend()), BestEdges);
std::for_each(BestEdges.begin(), BestEdges.end(),
- [this](std::pair<SUnit *, SUnit *> E) {
- if (!tryAddEdge(DAG, E.first, E.second))
+ [BestGroup](std::pair<SUnit *, SUnit *> E) {
+ if (!BestGroup->tryAddEdge(E.first, E.second))
llvm_unreachable("Edges known to be insertable.");
});
@@ -2402,7 +2395,7 @@ class IGroupLPDAGMutation : public ScheduleDAGMutation {
unsigned SchedGroup::NumSchedGroups = 0;
bool SchedGroup::tryAddEdge(SUnit *A, SUnit *B) {
- return ::tryAddEdge(DAG, A, B);
+ return A != B && DAG->addEdge(B, SDep(A, SDep::Artificial));
}
bool SchedGroup::canAddMI(const MachineInstr &MI) const {
>From 9ebd9f7659ba1d26eb984c07cbbbf7e9e2b2ad02 Mon Sep 17 00:00:00 2001
From: Frederik Harwath <fharwath at amd.com>
Date: Mon, 2 Mar 2026 05:17:34 -0500
Subject: [PATCH 06/11] [AMDGPU] IGroupLP: Remove useless SchedGroup::IsFull
call (NFC)
---
llvm/lib/Target/AMDGPU/AMDGPUIGroupLP.cpp | 17 +++++++++--------
1 file changed, 9 insertions(+), 8 deletions(-)
diff --git a/llvm/lib/Target/AMDGPU/AMDGPUIGroupLP.cpp b/llvm/lib/Target/AMDGPU/AMDGPUIGroupLP.cpp
index de6c838e3e827..5725642ba52e8 100644
--- a/llvm/lib/Target/AMDGPU/AMDGPUIGroupLP.cpp
+++ b/llvm/lib/Target/AMDGPU/AMDGPUIGroupLP.cpp
@@ -2583,10 +2583,8 @@ bool SchedGroup::canAddSU(SUnit &SU) const {
}
void SchedGroup::initSchedGroup() {
+ assert (!MaxSize && "Only called for SCHED_BARRIERs which don't have a size." );
for (auto &SU : DAG->SUnits) {
- if (isFull())
- break;
-
if (canAddSU(SU))
add(SU);
}
@@ -2594,18 +2592,22 @@ void SchedGroup::initSchedGroup() {
void SchedGroup::initSchedGroup(std::vector<SUnit>::reverse_iterator RIter,
SUnitsToCandidateSGsMap &SyncedInstrs) {
+ // This function is only used for SCHED_GROUP_BARRIER which always
+ // have a size.
+ assert(MaxSize);
+ assert(Collection.empty());
+
SUnit &InitSU = *RIter;
for (auto E = DAG->SUnits.rend(); RIter != E; ++RIter) {
auto &SU = *RIter;
- if (isFull())
- break;
if (canAddSU(SU))
SyncedInstrs[&SU].push_back(SGID);
}
+ // TODO Do not create SchedGroups with MaxSize 0?
+ assert((MaxSize == 0 || !isFull()) && "Nothing added to Collection");
add(InitSU);
- assert(MaxSize);
(*MaxSize)++;
}
@@ -2614,8 +2616,7 @@ void SchedGroup::initSchedGroup(SUnitsToCandidateSGsMap &SyncedInstrs) {
auto E = DAG->SUnits.rend();
for (; I != E; ++I) {
auto &SU = *I;
- if (isFull())
- break;
+
if (canAddSU(SU))
SyncedInstrs[&SU].push_back(SGID);
}
>From b9073f93bed5f1a918f7c91e645ea22e55fb2280 Mon Sep 17 00:00:00 2001
From: Frederik Harwath <fharwath at amd.com>
Date: Mon, 2 Mar 2026 05:42:05 -0500
Subject: [PATCH 07/11] Remove outdated comment
The comment used to refer to a call to resetEdges which has been removed.
---
llvm/lib/Target/AMDGPU/AMDGPUIGroupLP.cpp | 4 ----
1 file changed, 4 deletions(-)
diff --git a/llvm/lib/Target/AMDGPU/AMDGPUIGroupLP.cpp b/llvm/lib/Target/AMDGPU/AMDGPUIGroupLP.cpp
index 5725642ba52e8..41e6ebb7b6848 100644
--- a/llvm/lib/Target/AMDGPU/AMDGPUIGroupLP.cpp
+++ b/llvm/lib/Target/AMDGPU/AMDGPUIGroupLP.cpp
@@ -2665,8 +2665,6 @@ void IGroupLPDAGMutation::apply(ScheduleDAGInstrs *DAGInstrs) {
void IGroupLPDAGMutation::addSchedBarrierEdges(SUnit &SchedBarrier) {
MachineInstr &MI = *SchedBarrier.getInstr();
assert(MI.getOpcode() == AMDGPU::SCHED_BARRIER);
- // Remove all existing edges from the SCHED_BARRIER that were added due to the
- // instruction having side effects.
LLVM_DEBUG(dbgs() << "Building SchedGroup for SchedBarrier with Mask: "
<< MI.getOperand(0).getImm() << "\n");
auto InvertedMask =
@@ -2722,8 +2720,6 @@ IGroupLPDAGMutation::invertSchedBarrierMask(SchedGroupMask Mask) const {
void IGroupLPDAGMutation::initSchedGroupBarrierPipelineStage(
std::vector<SUnit>::reverse_iterator RIter) {
- // Remove all existing edges from the SCHED_GROUP_BARRIER that were added due
- // to the instruction having side effects.
MachineInstr &SGB = *RIter->getInstr();
assert(SGB.getOpcode() == AMDGPU::SCHED_GROUP_BARRIER);
int32_t SGMask = SGB.getOperand(0).getImm();
>From 5393ba1dcf4cb9135e788e7cbe538c66d3a9ed7a Mon Sep 17 00:00:00 2001
From: Frederik Harwath <fharwath at amd.com>
Date: Mon, 2 Mar 2026 06:13:44 -0500
Subject: [PATCH 08/11] [AMDGPU] IGroupLP: Clean up SchedGroup::initSchedGroup
(NFC)
The function has different overloads two of which are only used for
very specific types of SchedGroups, namely SCHED_BARRIER and
SCHED_GROUP_BARRIER. This seems to have a led to some confusion
since the different functions perform checks which are not needed
for their intended restricted use cases. Furthermore, the doc comments
are wrong.
Rename functions, remove useless checks, fix comments.
---
llvm/lib/Target/AMDGPU/AMDGPUIGroupLP.cpp | 47 +++++++++++------------
1 file changed, 23 insertions(+), 24 deletions(-)
diff --git a/llvm/lib/Target/AMDGPU/AMDGPUIGroupLP.cpp b/llvm/lib/Target/AMDGPU/AMDGPUIGroupLP.cpp
index 41e6ebb7b6848..09a4ec6a1d467 100644
--- a/llvm/lib/Target/AMDGPU/AMDGPUIGroupLP.cpp
+++ b/llvm/lib/Target/AMDGPU/AMDGPUIGroupLP.cpp
@@ -209,16 +209,15 @@ class SchedGroup {
// Remove last element in the SchedGroup
void pop() { Collection.pop_back(); }
- // Identify and add all relevant SUs from the DAG to this SchedGroup.
- void initSchedGroup();
+ // Identify and add all relevant SUs from the DAG to this SchedGroup
+ // which represents a SCHED_BARRIER.
+ void initSchedBarrier();
- // Add instructions to the SchedGroup bottom up starting from RIter.
- // PipelineInstrs is a set of instructions that should not be added to the
- // SchedGroup even when the other conditions for adding it are satisfied.
- // RIter will be added to the SchedGroup as well, and dependencies will be
- // added so that RIter will always be scheduled at the end of the group.
- void initSchedGroup(std::vector<SUnit>::reverse_iterator RIter,
- SUnitsToCandidateSGsMap &SyncedInstrs);
+ // Add the SCHED_GROUP_BARRIER instruction to the SchedGroup and for
+ // each SU starting at RIter, add the SchedGroup's SGID to the
+ // collection of potential SchedGroups for SU in SyncedInstrs.
+ void initSchedGroupBarrier(std::vector<SUnit>::reverse_iterator RIter,
+ SUnitsToCandidateSGsMap &SyncedInstrs);
void initSchedGroup(SUnitsToCandidateSGsMap &SyncedInstrs);
@@ -2582,33 +2581,33 @@ bool SchedGroup::canAddSU(SUnit &SU) const {
return std::all_of(B, E, [this](MachineInstr &MI) { return canAddMI(MI); });
}
-void SchedGroup::initSchedGroup() {
- assert (!MaxSize && "Only called for SCHED_BARRIERs which don't have a size." );
+void SchedGroup::initSchedBarrier() {
+ assert(!MaxSize && "SCHED_BARRIERs don't have a size.");
+ assert(Collection.empty());
+
for (auto &SU : DAG->SUnits) {
if (canAddSU(SU))
add(SU);
}
}
-void SchedGroup::initSchedGroup(std::vector<SUnit>::reverse_iterator RIter,
- SUnitsToCandidateSGsMap &SyncedInstrs) {
- // This function is only used for SCHED_GROUP_BARRIER which always
- // have a size.
- assert(MaxSize);
+void SchedGroup::initSchedGroupBarrier(
+ std::vector<SUnit>::reverse_iterator RIter,
+ SUnitsToCandidateSGsMap &SyncedInstrs) {
+ assert(MaxSize && "SCHED_GROUP_BARRIER always has size.");
assert(Collection.empty());
- SUnit &InitSU = *RIter;
+ // TODO Do not create SchedGroups with MaxSize 0?
+ assert((MaxSize == 0 || !isFull()) && "Nothing added to Collection");
+ add(*RIter);
+ (*MaxSize)++;
+
for (auto E = DAG->SUnits.rend(); RIter != E; ++RIter) {
auto &SU = *RIter;
if (canAddSU(SU))
SyncedInstrs[&SU].push_back(SGID);
}
-
- // TODO Do not create SchedGroups with MaxSize 0?
- assert((MaxSize == 0 || !isFull()) && "Nothing added to Collection");
- add(InitSU);
- (*MaxSize)++;
}
void SchedGroup::initSchedGroup(SUnitsToCandidateSGsMap &SyncedInstrs) {
@@ -2670,7 +2669,7 @@ void IGroupLPDAGMutation::addSchedBarrierEdges(SUnit &SchedBarrier) {
auto InvertedMask =
invertSchedBarrierMask((SchedGroupMask)MI.getOperand(0).getImm());
SchedGroup SG(InvertedMask, std::nullopt, DAG, TII);
- SG.initSchedGroup();
+ SG.initSchedBarrier();
// Preserve original instruction ordering relative to the SCHED_BARRIER.
SG.link(
@@ -2729,7 +2728,7 @@ void IGroupLPDAGMutation::initSchedGroupBarrierPipelineStage(
auto &SG = SyncedSchedGroups[SyncID].emplace_back((SchedGroupMask)SGMask,
Size, SyncID, DAG, TII);
- SG.initSchedGroup(RIter, SyncedInstrs[SG.getSyncID()]);
+ SG.initSchedGroupBarrier(RIter, SyncedInstrs[SG.getSyncID()]);
}
bool IGroupLPDAGMutation::initIGLPOpt(SUnit &SU) {
>From 5e59eafa3747bfeee33ad0301d09f463abba3b4a Mon Sep 17 00:00:00 2001
From: Frederik Harwath <fharwath at amd.com>
Date: Mon, 2 Mar 2026 06:50:41 -0500
Subject: [PATCH 09/11] [AMDGPU] IGroupLP: Extract function for assigning
SUnits to SchedGroup
---
llvm/lib/Target/AMDGPU/AMDGPUIGroupLP.cpp | 25 ++++++++++++-----------
1 file changed, 13 insertions(+), 12 deletions(-)
diff --git a/llvm/lib/Target/AMDGPU/AMDGPUIGroupLP.cpp b/llvm/lib/Target/AMDGPU/AMDGPUIGroupLP.cpp
index 09a4ec6a1d467..9b78aafda5e07 100644
--- a/llvm/lib/Target/AMDGPU/AMDGPUIGroupLP.cpp
+++ b/llvm/lib/Target/AMDGPU/AMDGPUIGroupLP.cpp
@@ -209,6 +209,10 @@ class SchedGroup {
// Remove last element in the SchedGroup
void pop() { Collection.pop_back(); }
+ template <class T>
+ void findCandidateSUnits(T Begin, T End,
+ SUnitsToCandidateSGsMap &SyncedInstrs);
+
// Identify and add all relevant SUs from the DAG to this SchedGroup
// which represents a SCHED_BARRIER.
void initSchedBarrier();
@@ -2601,24 +2605,21 @@ void SchedGroup::initSchedGroupBarrier(
assert((MaxSize == 0 || !isFull()) && "Nothing added to Collection");
add(*RIter);
(*MaxSize)++;
-
- for (auto E = DAG->SUnits.rend(); RIter != E; ++RIter) {
- auto &SU = *RIter;
+ findCandidateSUnits(RIter, DAG->SUnits.rend(), SyncedInstrs);
+}
+
+template <class T>
+void SchedGroup::findCandidateSUnits(T Begin, T End,
+ SUnitsToCandidateSGsMap &SyncedInstrs) {
+ std::for_each(Begin, End, [this, &SyncedInstrs](SUnit &SU) {
if (canAddSU(SU))
SyncedInstrs[&SU].push_back(SGID);
- }
+ });
}
void SchedGroup::initSchedGroup(SUnitsToCandidateSGsMap &SyncedInstrs) {
- auto I = DAG->SUnits.rbegin();
- auto E = DAG->SUnits.rend();
- for (; I != E; ++I) {
- auto &SU = *I;
-
- if (canAddSU(SU))
- SyncedInstrs[&SU].push_back(SGID);
- }
+ findCandidateSUnits(DAG->SUnits.rbegin(), DAG->SUnits.rend(), SyncedInstrs);
}
void IGroupLPDAGMutation::apply(ScheduleDAGInstrs *DAGInstrs) {
>From 88ed6ee082a969f1bcb082f9e6f5567d2cf7dc15 Mon Sep 17 00:00:00 2001
From: Frederik Harwath <fharwath at amd.com>
Date: Mon, 2 Mar 2026 08:11:47 -0500
Subject: [PATCH 10/11] [AMDGPU] IGroupLP: Remove/rename initSchedGroup
variants
The variants for SCHED_BARRIER and SCHED_GROUP_BARRIER became so
simple after the last refactoring steps that it seems appropriate to
inline them at their only call site. The remaining initSchedGroup
function does not modify the actual SchedGroup but the SyncedInstrs.
Calling it "findCandidateSUnits" seems better since this is the only
thing that it does.
---
llvm/lib/Target/AMDGPU/AMDGPUIGroupLP.cpp | 139 +++++++++-------------
1 file changed, 56 insertions(+), 83 deletions(-)
diff --git a/llvm/lib/Target/AMDGPU/AMDGPUIGroupLP.cpp b/llvm/lib/Target/AMDGPU/AMDGPUIGroupLP.cpp
index 9b78aafda5e07..22cae1928fe12 100644
--- a/llvm/lib/Target/AMDGPU/AMDGPUIGroupLP.cpp
+++ b/llvm/lib/Target/AMDGPU/AMDGPUIGroupLP.cpp
@@ -213,17 +213,10 @@ class SchedGroup {
void findCandidateSUnits(T Begin, T End,
SUnitsToCandidateSGsMap &SyncedInstrs);
- // Identify and add all relevant SUs from the DAG to this SchedGroup
- // which represents a SCHED_BARRIER.
- void initSchedBarrier();
-
- // Add the SCHED_GROUP_BARRIER instruction to the SchedGroup and for
- // each SU starting at RIter, add the SchedGroup's SGID to the
- // collection of potential SchedGroups for SU in SyncedInstrs.
- void initSchedGroupBarrier(std::vector<SUnit>::reverse_iterator RIter,
- SUnitsToCandidateSGsMap &SyncedInstrs);
-
- void initSchedGroup(SUnitsToCandidateSGsMap &SyncedInstrs);
+ /// Find each SUnit in the DAG that could potentially be added to
+ /// this SchedGroup and add the SGID to the candidate SchedGroups
+ /// for SU in \p SyncedInstrs.
+ void findCandidateSUnits(SUnitsToCandidateSGsMap &SyncedInstrs);
int getSyncID() { return SyncID; }
@@ -893,11 +886,11 @@ bool MFMASmallGemmOpt::applyIGLPStrategy(
for (unsigned I = 0; I < MFMACount * 3; ++I) {
SG = &SyncedSchedGroups[PipelineSyncID].emplace_back(
SchedGroupMask::DS, 2, PipelineSyncID, DAG, TII);
- SG->initSchedGroup(SyncedInstrs[SG->getSyncID()]);
+ SG->findCandidateSUnits(SyncedInstrs[SG->getSyncID()]);
SG = &SyncedSchedGroups[PipelineSyncID].emplace_back(
SchedGroupMask::MFMA, 1, PipelineSyncID, DAG, TII);
- SG->initSchedGroup(SyncedInstrs[SG->getSyncID()]);
+ SG->findCandidateSUnits(SyncedInstrs[SG->getSyncID()]);
}
return true;
@@ -1614,7 +1607,7 @@ bool MFMAExpInterleaveOpt::applyIGLPStrategy(
SG->addRule(
std::make_shared<EnablesNthMFMA>(1, TII, SG->getSGID(), true));
SG->addRule(std::make_shared<IsFMA>(TII, SG->getSGID()));
- SG->initSchedGroup(SyncedInstrs[SG->getSyncID()]);
+ SG->findCandidateSUnits(SyncedInstrs[SG->getSyncID()]);
// Second Round FMA
SG = &SyncedSchedGroups[PipelineSyncID].emplace_back(
@@ -1627,7 +1620,7 @@ bool MFMAExpInterleaveOpt::applyIGLPStrategy(
SG->addRule(std::make_shared<EnablesNthMFMA>(MFMAEnablement + 1, TII,
SG->getSGID(), true));
SG->addRule(std::make_shared<IsFMA>(TII, SG->getSGID()));
- SG->initSchedGroup(SyncedInstrs[SG->getSyncID()]);
+ SG->findCandidateSUnits(SyncedInstrs[SG->getSyncID()]);
}
if (UsesDSRead) {
@@ -1635,7 +1628,7 @@ bool MFMAExpInterleaveOpt::applyIGLPStrategy(
SchedGroupMask::DS_READ, 2, PipelineSyncID, DAG, TII);
SG->addRule(std::make_shared<OccursAtOrAfterNode>(*FirstPipeDSR, TII,
SG->getSGID()));
- SG->initSchedGroup(SyncedInstrs[SG->getSyncID()]);
+ SG->findCandidateSUnits(SyncedInstrs[SG->getSyncID()]);
}
// First Round EXP
@@ -1649,7 +1642,7 @@ bool MFMAExpInterleaveOpt::applyIGLPStrategy(
SG->addRule(std::make_shared<IsPipeExp>(TII, SG->getSGID(), true));
SG->addRule(std::make_shared<LessThanNSuccs>(8, TII, SG->getSGID(),
HasChainBetweenCvt));
- SG->initSchedGroup(SyncedInstrs[SG->getSyncID()]);
+ SG->findCandidateSUnits(SyncedInstrs[SG->getSyncID()]);
incrementTransPosition();
@@ -1666,7 +1659,7 @@ bool MFMAExpInterleaveOpt::applyIGLPStrategy(
else
SG->addRule(std::make_shared<IsSuccOfPrevNthGroup>(
1 + (2 + UsesFMA) * I, TII, SG->getSGID()));
- SG->initSchedGroup(SyncedInstrs[SG->getSyncID()]);
+ SG->findCandidateSUnits(SyncedInstrs[SG->getSyncID()]);
}
// Third Round FMA
@@ -1681,7 +1674,7 @@ bool MFMAExpInterleaveOpt::applyIGLPStrategy(
SG->addRule(std::make_shared<EnablesNthMFMA>(2 * MFMAEnablement + 1,
TII, SG->getSGID(), true));
SG->addRule(std::make_shared<IsFMA>(TII, SG->getSGID()));
- SG->initSchedGroup(SyncedInstrs[SG->getSyncID()]);
+ SG->findCandidateSUnits(SyncedInstrs[SG->getSyncID()]);
}
// Second Round EXP
@@ -1697,7 +1690,7 @@ bool MFMAExpInterleaveOpt::applyIGLPStrategy(
SG->addRule(std::make_shared<IsPipeExp>(TII, SG->getSGID(), true));
SG->addRule(std::make_shared<LessThanNSuccs>(8, TII, SG->getSGID(),
HasChainBetweenCvt));
- SG->initSchedGroup(SyncedInstrs[SG->getSyncID()]);
+ SG->findCandidateSUnits(SyncedInstrs[SG->getSyncID()]);
}
// The "extra" EXP which enables all MFMA
@@ -1707,7 +1700,7 @@ bool MFMAExpInterleaveOpt::applyIGLPStrategy(
SG->addRule(std::make_shared<IsPipeExp>(TII, SG->getSGID(), true));
SG->addRule(std::make_shared<GreaterThanOrEqualToNSuccs>(
8, TII, SG->getSGID(), HasChainBetweenCvt));
- SG->initSchedGroup(SyncedInstrs[SG->getSyncID()]);
+ SG->findCandidateSUnits(SyncedInstrs[SG->getSyncID()]);
// PHASE 2: Main Interleave Loop
@@ -1744,14 +1737,14 @@ bool MFMAExpInterleaveOpt::applyIGLPStrategy(
SG->getSGID(), true));
else
SG->addRule(std::make_shared<OccursAfterExp>(TII, SG->getSGID(), true));
- SG->initSchedGroup(SyncedInstrs[SG->getSyncID()]);
+ SG->findCandidateSUnits(SyncedInstrs[SG->getSyncID()]);
incrementMFMAPosition();
if (UsesVALU) {
SG = &SyncedSchedGroups[PipelineSyncID].emplace_back(
SchedGroupMask::VALU, VALUOps, PipelineSyncID, DAG, TII);
SG->addRule(std::make_shared<IsPipeAdd>(TII, SG->getSGID()));
- SG->initSchedGroup(SyncedInstrs[SG->getSyncID()]);
+ SG->findCandidateSUnits(SyncedInstrs[SG->getSyncID()]);
}
if (UsesDSRead && !(I % 4)) {
@@ -1759,7 +1752,7 @@ bool MFMAExpInterleaveOpt::applyIGLPStrategy(
SchedGroupMask::DS_READ, 2, PipelineSyncID, DAG, TII);
SG->addRule(std::make_shared<OccursAtOrAfterNode>(*FirstPipeDSR, TII,
SG->getSGID()));
- SG->initSchedGroup(SyncedInstrs[SG->getSyncID()]);
+ SG->findCandidateSUnits(SyncedInstrs[SG->getSyncID()]);
}
// CVT, EXP, FMA Interleaving
@@ -1787,7 +1780,7 @@ bool MFMAExpInterleaveOpt::applyIGLPStrategy(
else
SG->addRule(std::make_shared<IsSuccOfPrevNthGroup>(CurrentOffset, TII,
SG->getSGID()));
- SG->initSchedGroup(SyncedInstrs[SG->getSyncID()]);
+ SG->findCandidateSUnits(SyncedInstrs[SG->getSyncID()]);
}
// Round N + 3 FMA
@@ -1804,7 +1797,7 @@ bool MFMAExpInterleaveOpt::applyIGLPStrategy(
(((I * ExpRatio + J) / ExpRequirement) + 3) * MFMAEnablement + 1,
TII, SG->getSGID(), true));
SG->addRule(std::make_shared<IsFMA>(TII, SG->getSGID()));
- SG->initSchedGroup(SyncedInstrs[SG->getSyncID()]);
+ SG->findCandidateSUnits(SyncedInstrs[SG->getSyncID()]);
}
// Round N + 2 Exp
@@ -1821,7 +1814,7 @@ bool MFMAExpInterleaveOpt::applyIGLPStrategy(
SG->addRule(std::make_shared<IsPipeExp>(TII, SG->getSGID(), true));
SG->addRule(std::make_shared<LessThanNSuccs>(8, TII, SG->getSGID(),
HasChainBetweenCvt));
- SG->initSchedGroup(SyncedInstrs[SG->getSyncID()]);
+ SG->findCandidateSUnits(SyncedInstrs[SG->getSyncID()]);
}
}
@@ -1829,7 +1822,7 @@ bool MFMAExpInterleaveOpt::applyIGLPStrategy(
SG = &SyncedSchedGroups[PipelineSyncID].emplace_back(
SchedGroupMask::MFMA, MFMAEnablement * 2, PipelineSyncID, DAG, TII);
SG->addRule(std::make_shared<OccursAfterExp>(TII, SG->getSGID(), true));
- SG->initSchedGroup(SyncedInstrs[SG->getSyncID()]);
+ SG->findCandidateSUnits(SyncedInstrs[SG->getSyncID()]);
return true;
}
@@ -1865,11 +1858,11 @@ bool MFMAExpSimpleInterleaveOpt::applyIGLPStrategy(
for (unsigned I = 0; I < MFMACount * 3; ++I) {
SchedGroup *SG = &SyncedSchedGroups[PipelineSyncID].emplace_back(
SchedGroupMask::TRANS, 1, PipelineSyncID, DAG, TII);
- SG->initSchedGroup(SyncedInstrs[SG->getSyncID()]);
+ SG->findCandidateSUnits(SyncedInstrs[SG->getSyncID()]);
SG = &SyncedSchedGroups[PipelineSyncID].emplace_back(
SchedGroupMask::MFMA, 1, PipelineSyncID, DAG, TII);
- SG->initSchedGroup(SyncedInstrs[SG->getSyncID()]);
+ SG->findCandidateSUnits(SyncedInstrs[SG->getSyncID()]);
}
return true;
@@ -2178,11 +2171,11 @@ bool MFMASmallGemmSingleWaveOpt::applyIGLPStrategy(
for (unsigned I = 0; I < MFMACount; I++) {
SG = &SyncedSchedGroups[PipelineSyncID].emplace_back(
SchedGroupMask::MFMA, 1, PipelineSyncID, DAG, TII);
- SG->initSchedGroup(SyncedInstrs[SG->getSyncID()]);
+ SG->findCandidateSUnits(SyncedInstrs[SG->getSyncID()]);
SG = &SyncedSchedGroups[PipelineSyncID].emplace_back(
SchedGroupMask::VALU, 2, PipelineSyncID, DAG, TII);
- SG->initSchedGroup(SyncedInstrs[SG->getSyncID()]);
+ SG->findCandidateSUnits(SyncedInstrs[SG->getSyncID()]);
}
}
@@ -2195,21 +2188,21 @@ bool MFMASmallGemmSingleWaveOpt::applyIGLPStrategy(
SG = &SyncedSchedGroups[PipelineSyncID].emplace_back(
SchedGroupMask::DS_READ, 4, PipelineSyncID, DAG, TII);
SG->addRule(std::make_shared<EnablesInitialMFMA>(TII, SG->getSGID(), true));
- SG->initSchedGroup(SyncedInstrs[SG->getSyncID()]);
+ SG->findCandidateSUnits(SyncedInstrs[SG->getSyncID()]);
SG = &SyncedSchedGroups[PipelineSyncID].emplace_back(
SchedGroupMask::MFMA, 1, PipelineSyncID, DAG, TII);
- SG->initSchedGroup(SyncedInstrs[SG->getSyncID()]);
+ SG->findCandidateSUnits(SyncedInstrs[SG->getSyncID()]);
// Interleave MFMA with DS_READ prefetch
for (unsigned I = 4; I < DSRCount; ++I) {
SG = &SyncedSchedGroups[PipelineSyncID].emplace_back(
SchedGroupMask::DS_READ, 1, PipelineSyncID, DAG, TII);
- SG->initSchedGroup(SyncedInstrs[SG->getSyncID()]);
+ SG->findCandidateSUnits(SyncedInstrs[SG->getSyncID()]);
SG = &SyncedSchedGroups[PipelineSyncID].emplace_back(
SchedGroupMask::MFMA, 1, PipelineSyncID, DAG, TII);
- SG->initSchedGroup(SyncedInstrs[SG->getSyncID()]);
+ SG->findCandidateSUnits(SyncedInstrs[SG->getSyncID()]);
}
// Phase 2a: Loop carried dependency with V_PERM
@@ -2219,34 +2212,34 @@ bool MFMASmallGemmSingleWaveOpt::applyIGLPStrategy(
SG = &SyncedSchedGroups[PipelineSyncID].emplace_back(
SchedGroupMask::VALU, 4, PipelineSyncID, DAG, TII);
SG->addRule(std::make_shared<IsPermForDSW>(TII, SG->getSGID(), true));
- SG->initSchedGroup(SyncedInstrs[SG->getSyncID()]);
+ SG->findCandidateSUnits(SyncedInstrs[SG->getSyncID()]);
SG = &SyncedSchedGroups[PipelineSyncID].emplace_back(
SchedGroupMask::DS_WRITE, 1, PipelineSyncID, DAG, TII);
SG->addRule(std::make_shared<IsSuccOfPrevGroup>(TII, SG->getSGID()));
- SG->initSchedGroup(SyncedInstrs[SG->getSyncID()]);
+ SG->findCandidateSUnits(SyncedInstrs[SG->getSyncID()]);
SG = &SyncedSchedGroups[PipelineSyncID].emplace_back(
SchedGroupMask::VMEM_READ, 4, PipelineSyncID, DAG, TII);
SG->addRule(std::make_shared<SharesPredWithPrevNthGroup>(
1, TII, SG->getSGID(), true));
SG->addRule(std::make_shared<VMEMSize>(TII, SG->getSGID()));
- SG->initSchedGroup(SyncedInstrs[SG->getSyncID()]);
+ SG->findCandidateSUnits(SyncedInstrs[SG->getSyncID()]);
SG = &SyncedSchedGroups[PipelineSyncID].emplace_back(
SchedGroupMask::MFMA, 1, PipelineSyncID, DAG, TII);
- SG->initSchedGroup(SyncedInstrs[SG->getSyncID()]);
+ SG->findCandidateSUnits(SyncedInstrs[SG->getSyncID()]);
SG = &SyncedSchedGroups[PipelineSyncID].emplace_back(
SchedGroupMask::VMEM_READ, 4, PipelineSyncID, DAG, TII);
SG->addRule(std::make_shared<SharesPredWithPrevNthGroup>(
3, TII, SG->getSGID(), true));
SG->addRule(std::make_shared<VMEMSize>(TII, SG->getSGID()));
- SG->initSchedGroup(SyncedInstrs[SG->getSyncID()]);
+ SG->findCandidateSUnits(SyncedInstrs[SG->getSyncID()]);
SG = &SyncedSchedGroups[PipelineSyncID].emplace_back(
SchedGroupMask::MFMA, 1, PipelineSyncID, DAG, TII);
- SG->initSchedGroup(SyncedInstrs[SG->getSyncID()]);
+ SG->findCandidateSUnits(SyncedInstrs[SG->getSyncID()]);
}
// Phase 2b: Loop carried dependency without V_PERM
@@ -2255,16 +2248,16 @@ bool MFMASmallGemmSingleWaveOpt::applyIGLPStrategy(
for (unsigned I = DSWWithPermCount; I < DSWCount; I++) {
SG = &SyncedSchedGroups[PipelineSyncID].emplace_back(
SchedGroupMask::DS_WRITE, 1, PipelineSyncID, DAG, TII);
- SG->initSchedGroup(SyncedInstrs[SG->getSyncID()]);
+ SG->findCandidateSUnits(SyncedInstrs[SG->getSyncID()]);
SG = &SyncedSchedGroups[PipelineSyncID].emplace_back(
SchedGroupMask::VMEM_READ, 4, PipelineSyncID, DAG, TII);
SG->addRule(std::make_shared<VMEMSize>(TII, SG->getSGID()));
- SG->initSchedGroup(SyncedInstrs[SG->getSyncID()]);
+ SG->findCandidateSUnits(SyncedInstrs[SG->getSyncID()]);
SG = &SyncedSchedGroups[PipelineSyncID].emplace_back(
SchedGroupMask::MFMA, 1, PipelineSyncID, DAG, TII);
- SG->initSchedGroup(SyncedInstrs[SG->getSyncID()]);
+ SG->findCandidateSUnits(SyncedInstrs[SG->getSyncID()]);
}
// Phase 2c: Loop carried dependency with V_PERM, VMEM_READs are
@@ -2276,52 +2269,52 @@ bool MFMASmallGemmSingleWaveOpt::applyIGLPStrategy(
SG = &SyncedSchedGroups[PipelineSyncID].emplace_back(
SchedGroupMask::VALU, 4, PipelineSyncID, DAG, TII);
SG->addRule(std::make_shared<IsPermForDSW>(TII, SG->getSGID(), true));
- SG->initSchedGroup(SyncedInstrs[SG->getSyncID()]);
+ SG->findCandidateSUnits(SyncedInstrs[SG->getSyncID()]);
SG = &SyncedSchedGroups[PipelineSyncID].emplace_back(
SchedGroupMask::DS_WRITE, 1, PipelineSyncID, DAG, TII);
SG->addRule(std::make_shared<IsSuccOfPrevGroup>(TII, SG->getSGID()));
- SG->initSchedGroup(SyncedInstrs[SG->getSyncID()]);
+ SG->findCandidateSUnits(SyncedInstrs[SG->getSyncID()]);
SG = &SyncedSchedGroups[PipelineSyncID].emplace_back(
SchedGroupMask::MFMA, 1, PipelineSyncID, DAG, TII);
- SG->initSchedGroup(SyncedInstrs[SG->getSyncID()]);
+ SG->findCandidateSUnits(SyncedInstrs[SG->getSyncID()]);
SG = &SyncedSchedGroups[PipelineSyncID].emplace_back(
SchedGroupMask::VALU, 4, PipelineSyncID, DAG, TII);
SG->addRule(std::make_shared<IsPermForDSW>(TII, SG->getSGID(), true));
- SG->initSchedGroup(SyncedInstrs[SG->getSyncID()]);
+ SG->findCandidateSUnits(SyncedInstrs[SG->getSyncID()]);
SG = &SyncedSchedGroups[PipelineSyncID].emplace_back(
SchedGroupMask::DS_WRITE, 1, PipelineSyncID, DAG, TII);
SG->addRule(std::make_shared<IsSuccOfPrevGroup>(TII, SG->getSGID()));
- SG->initSchedGroup(SyncedInstrs[SG->getSyncID()]);
+ SG->findCandidateSUnits(SyncedInstrs[SG->getSyncID()]);
SG = &SyncedSchedGroups[PipelineSyncID].emplace_back(
SchedGroupMask::MFMA, 1, PipelineSyncID, DAG, TII);
- SG->initSchedGroup(SyncedInstrs[SG->getSyncID()]);
+ SG->findCandidateSUnits(SyncedInstrs[SG->getSyncID()]);
SG = &SyncedSchedGroups[PipelineSyncID].emplace_back(
SchedGroupMask::VMEM_READ, 4, PipelineSyncID, DAG, TII);
SG->addRule(std::make_shared<SharesPredWithPrevNthGroup>(
2, TII, SG->getSGID(), true));
SG->addRule(std::make_shared<VMEMSize>(TII, SG->getSGID()));
- SG->initSchedGroup(SyncedInstrs[SG->getSyncID()]);
+ SG->findCandidateSUnits(SyncedInstrs[SG->getSyncID()]);
SG = &SyncedSchedGroups[PipelineSyncID].emplace_back(
SchedGroupMask::MFMA, 1, PipelineSyncID, DAG, TII);
- SG->initSchedGroup(SyncedInstrs[SG->getSyncID()]);
+ SG->findCandidateSUnits(SyncedInstrs[SG->getSyncID()]);
SG = &SyncedSchedGroups[PipelineSyncID].emplace_back(
SchedGroupMask::VMEM_READ, 4, PipelineSyncID, DAG, TII);
SG->addRule(std::make_shared<SharesPredWithPrevNthGroup>(
4, TII, SG->getSGID(), true));
SG->addRule(std::make_shared<VMEMSize>(TII, SG->getSGID()));
- SG->initSchedGroup(SyncedInstrs[SG->getSyncID()]);
+ SG->findCandidateSUnits(SyncedInstrs[SG->getSyncID()]);
SG = &SyncedSchedGroups[PipelineSyncID].emplace_back(
SchedGroupMask::MFMA, 1, PipelineSyncID, DAG, TII);
- SG->initSchedGroup(SyncedInstrs[SG->getSyncID()]);
+ SG->findCandidateSUnits(SyncedInstrs[SG->getSyncID()]);
}
return true;
@@ -2585,30 +2578,6 @@ bool SchedGroup::canAddSU(SUnit &SU) const {
return std::all_of(B, E, [this](MachineInstr &MI) { return canAddMI(MI); });
}
-void SchedGroup::initSchedBarrier() {
- assert(!MaxSize && "SCHED_BARRIERs don't have a size.");
- assert(Collection.empty());
-
- for (auto &SU : DAG->SUnits) {
- if (canAddSU(SU))
- add(SU);
- }
-}
-
-void SchedGroup::initSchedGroupBarrier(
- std::vector<SUnit>::reverse_iterator RIter,
- SUnitsToCandidateSGsMap &SyncedInstrs) {
- assert(MaxSize && "SCHED_GROUP_BARRIER always has size.");
- assert(Collection.empty());
-
- // TODO Do not create SchedGroups with MaxSize 0?
- assert((MaxSize == 0 || !isFull()) && "Nothing added to Collection");
- add(*RIter);
- (*MaxSize)++;
-
- findCandidateSUnits(RIter, DAG->SUnits.rend(), SyncedInstrs);
-}
-
template <class T>
void SchedGroup::findCandidateSUnits(T Begin, T End,
SUnitsToCandidateSGsMap &SyncedInstrs) {
@@ -2618,7 +2587,7 @@ void SchedGroup::findCandidateSUnits(T Begin, T End,
});
}
-void SchedGroup::initSchedGroup(SUnitsToCandidateSGsMap &SyncedInstrs) {
+void SchedGroup::findCandidateSUnits(SUnitsToCandidateSGsMap &SyncedInstrs) {
findCandidateSUnits(DAG->SUnits.rbegin(), DAG->SUnits.rend(), SyncedInstrs);
}
@@ -2670,7 +2639,10 @@ void IGroupLPDAGMutation::addSchedBarrierEdges(SUnit &SchedBarrier) {
auto InvertedMask =
invertSchedBarrierMask((SchedGroupMask)MI.getOperand(0).getImm());
SchedGroup SG(InvertedMask, std::nullopt, DAG, TII);
- SG.initSchedBarrier();
+
+ for (SUnit &SU : DAG->SUnits)
+ if (SG.canAddSU(SU))
+ SG.add(SU);
// Preserve original instruction ordering relative to the SCHED_BARRIER.
SG.link(
@@ -2726,10 +2698,11 @@ void IGroupLPDAGMutation::initSchedGroupBarrierPipelineStage(
int32_t Size = SGB.getOperand(1).getImm();
int32_t SyncID = SGB.getOperand(2).getImm();
+ Size++; // Make room for the SCHED_GROUP_BARRIER instruction
auto &SG = SyncedSchedGroups[SyncID].emplace_back((SchedGroupMask)SGMask,
Size, SyncID, DAG, TII);
-
- SG.initSchedGroupBarrier(RIter, SyncedInstrs[SG.getSyncID()]);
+ SG.add(*RIter);
+ SG.findCandidateSUnits(RIter, SG.DAG->SUnits.rend(), SyncedInstrs[SG.getSyncID()]);
}
bool IGroupLPDAGMutation::initIGLPOpt(SUnit &SU) {
>From 57b1c90aa7cc6ee083f285be47deb342e44fa638 Mon Sep 17 00:00:00 2001
From: Frederik Harwath <fharwath at amd.com>
Date: Tue, 3 Mar 2026 04:03:05 -0500
Subject: [PATCH 11/11] Remove std::for_each use
---
llvm/lib/Target/AMDGPU/AMDGPUIGroupLP.cpp | 10 +++++-----
1 file changed, 5 insertions(+), 5 deletions(-)
diff --git a/llvm/lib/Target/AMDGPU/AMDGPUIGroupLP.cpp b/llvm/lib/Target/AMDGPU/AMDGPUIGroupLP.cpp
index 22cae1928fe12..258d59d482c5e 100644
--- a/llvm/lib/Target/AMDGPU/AMDGPUIGroupLP.cpp
+++ b/llvm/lib/Target/AMDGPU/AMDGPUIGroupLP.cpp
@@ -748,11 +748,11 @@ void PipelineSolver::greedyFind(
AddedEdges = BestEdges;
else
AddedEdges.splice(std::prev(AddedEdges.cend()), BestEdges);
- std::for_each(BestEdges.begin(), BestEdges.end(),
- [BestGroup](std::pair<SUnit *, SUnit *> E) {
- if (!BestGroup->tryAddEdge(E.first, E.second))
- llvm_unreachable("Edges known to be insertable.");
- });
+
+ for (const std::pair<SUnit *, SUnit *> &E : BestEdges) {
+ if (!BestGroup->tryAddEdge(E.first, E.second))
+ llvm_unreachable("Edges known to be insertable.");
+ }
LLVM_DEBUG(dbgs() << "Best Group has ID: " << BestGroupID << " and Mask"
<< (int)BestGroup->getMask() << "\n");
More information about the llvm-commits
mailing list