[llvm] [AMDGPU][CoExecSched] Add free-proximity scheduling heuristic (PR #225334)

Romanov Vlad via llvm-commits llvm-commits at lists.llvm.org
Mon Oct 5 03:36:36 PDT 2026


================
@@ -467,29 +467,106 @@ SUnit *HardwareUnitInfo::getNextTargetSU(bool LookDeep) const {
   return TargetSU;
 }
 
-void HardwareUnitInfo::insert(SUnit *SU, unsigned BlockingCycles) {
-  if (!AllSUs.insert(SU))
-    llvm_unreachable("HardwareUnit already contains SU!");
+/// Counts the registers scheduling \p SU frees, i.e. those it reads whose
+/// defining SU has \p SU as its only remaining unscheduled data successor.
+///
+/// TODO: weight by register size and split per bank (ArchVGPR/AGPR/SGPR have
+/// separate budgets), charging SU for its own defs, so the result is a signed
+/// pressure delta rather than a count.
+static unsigned countRegFrees(const SUnit *SU) {
+  SmallDenseMap<Register, unsigned, 8> RegMaxUnsched;
+
+  for (const SDep &Pred : SU->Preds) {
+    if (Pred.getKind() != SDep::Data)
+      continue;
+    Register Reg = Pred.getReg();
+    if (!Reg)
+      continue;
+    const SUnit *PredSU = Pred.getSUnit();
+    unsigned Unscheduled = 0;
+    // Only successors that read Reg keep it live; PredSU may define other
+    // registers whose edges say nothing about when Reg dies.
+    for (const SDep &Succ : PredSU->Succs)
+      if (Succ.getKind() == SDep::Data && Succ.getReg() == Reg &&
+          !Succ.getSUnit()->isScheduled)
+        ++Unscheduled;
+
+    LLVM_DEBUG({
+      dbgs() << "        pred SU(" << PredSU->NodeNum
+             << ") reg=" << printReg(Reg) << " unschedSuccs=" << Unscheduled
+             << " ";
+      if (PredSU->getInstr())
+        PredSU->getInstr()->print(dbgs(), /*IsStandalone=*/true,
+                                  /*SkipOpers=*/false, /*SkipDebugLoc=*/true);
+      else
+        dbgs() << "<no instr>";
+      dbgs() << "\n";
+    });
+    auto &MaxUnsched = RegMaxUnsched[Reg];
+    MaxUnsched = std::max(MaxUnsched, Unscheduled);
+  }
 
-  TotalCycles += BlockingCycles;
+  // SU itself is always counted, so MaxUnsched >= 1 and freed means == 1.
+  unsigned Frees = 0;
+  for (const auto &[Reg, MaxUnsched] : RegMaxUnsched)
----------------
romanovvlad wrote:

There is a TODO about it. https://github.com/llvm/llvm-project/pull/225334/changes/BASE..929361b7ed7e889b5ff4a2f4844f8bc304a32022#diff-d6e870231c94224c985a53d9b71284b82ead983af6ce16d530e94f92f6f61e33R473

I've tried prototyping it, but have seen no improvements vs simple logic. So I decided to leave it for future.

https://github.com/llvm/llvm-project/pull/225334


More information about the llvm-commits mailing list