[llvm] [AMDGPU] Prefer lower total register usage in regions with spilling (PR #71882)

Jeffrey Byrnes via llvm-commits llvm-commits at lists.llvm.org
Fri Dec 1 15:18:14 PST 2023


================
@@ -102,20 +102,56 @@ bool GCNRegPressure::less(const GCNSubtarget &ST,
     std::min(MaxOccupancy,
              ST.getOccupancyWithNumVGPRs(O.getVGPRNum(ST.hasGFX90AInsts())));
 
+  unsigned MaxVGPRs = ST.getMaxNumVGPRs(MF);
+  unsigned MaxSGPRs = ST.getMaxNumSGPRs(MF);
+
+  bool ExcessVGPR = getVGPRNum(false) > MaxVGPRs || getAGPRNum() > MaxVGPRs;
+  bool ExcessSGPR = getSGPRNum() > MaxSGPRs;
+  bool OtherExcessVGPR =
+      O.getVGPRNum(false) > MaxVGPRs || O.getAGPRNum() > MaxVGPRs;
+  bool OtherExcessSGPR = O.getSGPRNum() > MaxSGPRs;
+
+  bool ExcessRP = ExcessSGPR || ExcessVGPR;
+  bool OtherExcessRP = OtherExcessSGPR || OtherExcessVGPR;
+  // In regions with spilling, we give precedence to the schedule with
+  // lower general RP.
+  if (ExcessRP || OtherExcessRP) {
+    // If the current RP doesn't have excess, then the other must have excess.
+    // Current RP is better.
+    if (!ExcessRP)
+      return true;
+    // If the other didn't have excess, but the current does, then current RP is
+    // worse.
+    else if (!OtherExcessRP)
+      return false;
+    else {
+      bool SGPRImportant = OtherExcessSGPR && ExcessSGPR;
+      unsigned GPRPressure =
+          SGPRImportant ? getSGPRNum() : getVGPRNum(ST.hasGFX90AInsts());
+      unsigned OtherGPRPressure =
+          SGPRImportant ? O.getSGPRNum() : O.getVGPRNum(ST.hasGFX90AInsts());
+      // If the pressures are the same, fall through to the subsequent checks
+      if (GPRPressure != OtherGPRPressure)
+        return GPRPressure < OtherGPRPressure;
+    }
----------------
jrbyrnes wrote:

I implemented the weight cost comparison, with a very limited cost model. With the inclusion of additional types of register excess (e.g. archVGPR / AGPR) it doesn't make sense to try to compare just one. The new implementation should be able to choose the better RP when there are excesses from multiple types.

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


More information about the llvm-commits mailing list