[llvm] [AMDGPU] Add heuristic to set CarriedLatencies (PR #224057)
Dmitry Sidorov via llvm-commits
llvm-commits at lists.llvm.org
Fri Oct 2 03:47:10 PDT 2026
https://github.com/MrSidims approved this pull request.
The patch is LGTM, but there is one defect currently. The mode inferred for one region stays in effect for every later region of the function, so a region without the WMMA to DS structure inherits the fence carried latency of an earlier one.
<details>
<summary>I do have a patch for this on top of this PR, which looks like this:</summary>
```
diff --git a/llvm/lib/Target/AMDGPU/AMDGPUCoExecSchedStrategy.cpp b/llvm/lib/Target/AMDGPU/AMDGPUCoExecSchedStrategy.cpp
index 8e7f57393fa1..c795ec2020cf 100644
--- a/llvm/lib/Target/AMDGPU/AMDGPUCoExecSchedStrategy.cpp
+++ b/llvm/lib/Target/AMDGPU/AMDGPUCoExecSchedStrategy.cpp
@@ -878,60 +878,47 @@ unsigned CandidateHeuristics::getCarriedLatency(SUnit *SU) {
return CarriedLatency;
}
+bool CandidateHeuristics::mustScheduleDSAfterWMMA() const {
+ bool HasWMMA = false;
+ for (auto &SU : DAG->SUnits) {
+ MachineInstr *MI = SU.getInstr();
+ if (!MI || classifyFlavor(*MI, *SII) != InstructionFlavor::WMMA)
+ continue;
+ HasWMMA = true;
+ bool HasDSSucc = false;
+ for (auto &Succ : SU.Succs) {
+ MachineInstr *SuccMI = Succ.getSUnit()->getInstr();
+ if (SuccMI && classifyFlavor(*SuccMI, *SII) == InstructionFlavor::DS) {
+ HasDSSucc = true;
+ break;
+ }
+ }
+ if (!HasDSSucc)
+ return false;
+ }
+ return HasWMMA;
+}
+
void CandidateHeuristics::collectRegionSummary() {
CarriedLatencies.clear();
+ RegionCarriedLatency = CarriedLatency::Off;
if (!SchedModel || !SchedModel->hasInstrSchedModel())
return;
+ // In some cases, we may end up with loads at the end of a predecssor block.
+ // In these cases, it is preferable to defer scheduling a fence which
+ // corresponds with a waitcnt for those loads until the latency of the load
+ // has cleared. Unfortunately, there is no reliable way to determine whether
+ // or not a predecessor block will end up scheduling loads at the end. Here,
+ // we inspect the dependency structure to define a rough heuristic: if we
+ // must schedule ds_loads after wmma, then we carry the latency of ds_loads
+ // to successor block fences.
+ // TODO: 1. extend to different memory instructions, 2. teach carried
+ // latencies about fence legalization.
if (BlockCarriedLatency.getNumOccurrences())
RegionCarriedLatency = BlockCarriedLatency;
-
- if (!BlockCarriedLatency.getNumOccurrences()) {
- SmallVector<SUnit *, 16> RegionWMMAs;
-
- for (auto &SU : DAG->SUnits) {
- if (!SU.getInstr())
- continue;
- MachineInstr *MI = SU.getInstr();
- const InstructionFlavor Flavor = classifyFlavor(*MI, *SII);
- if (Flavor == InstructionFlavor::WMMA)
- RegionWMMAs.push_back(&SU);
- }
-
- bool MustHaveDSAfter = RegionWMMAs.size();
-
- // In some cases, we may end up with loads at the end of a predecssor block.
- // In these cases, it is preferable to defer scheduling a fence which
- // corresponds with a waitcnt for those loads until the latency of the load
- // has cleared. Unfortunately, there is no reliable way to determine whether
- // or not a predecessor block will end up scheduling loads at the end. Here,
- // we inspect the dependency structure to define a rough heuristic: if we
- // must schedule ds_loads after wmma, then we carry the latency of ds_loads
- // to successor block fences.
- // TODO: 1. extend to different memory instructions, 2. teach carried
- // latencies about fence legalization.
- for (SUnit *SU : RegionWMMAs) {
- bool HasDSSucc = false;
- for (auto &Succ : SU->Succs) {
- if (!Succ.getSUnit()->getInstr())
- continue;
- if (classifyFlavor(*Succ.getSUnit()->getInstr(), *SII) ==
- InstructionFlavor::DS) {
- HasDSSucc = true;
- break;
- }
- }
-
- if (!HasDSSucc) {
- MustHaveDSAfter = false;
- break;
- }
- }
-
- if (MustHaveDSAfter) {
- RegionCarriedLatency = CarriedLatency::Fence;
- }
- }
+ else if (mustScheduleDSAfterWMMA())
+ RegionCarriedLatency = CarriedLatency::Fence;
for (auto &SU : DAG->SUnits) {
MachineInstr *MI = SU.getInstr();
diff --git a/llvm/lib/Target/AMDGPU/AMDGPUCoExecSchedStrategy.h b/llvm/lib/Target/AMDGPU/AMDGPUCoExecSchedStrategy.h
index ecfcc83f1809..66294f91dea1 100644
--- a/llvm/lib/Target/AMDGPU/AMDGPUCoExecSchedStrategy.h
+++ b/llvm/lib/Target/AMDGPU/AMDGPUCoExecSchedStrategy.h
@@ -268,6 +268,10 @@ protected:
/// heuristics.
void collectRegionSummary();
+ /// \returns true when the region has WMMA instructions and every one of
+ /// them has a DS successor.
+ bool mustScheduleDSAfterWMMA() const;
+
/// \returns the maximum blocking cycles according to the SchedModel for a
/// given MCSchedClassDesc \p SC.
unsigned getMaxBlockingCycles(const MCSchedClassDesc *SC,
```
</details>
Feel free to reuse it or I can create a follow-up PR if you find it requires a bit more detailed discussion.
https://github.com/llvm/llvm-project/pull/224057
More information about the llvm-commits
mailing list