[llvm] [NFC] [AMDGPU] Added 2 options to rewrite-agpr-copy-mfma for debugging. (PR #189437)

Dhruva Chakrabarti via llvm-commits llvm-commits at lists.llvm.org
Thu Apr 9 21:53:32 PDT 2026


https://github.com/dhruvachak updated https://github.com/llvm/llvm-project/pull/189437

>From c0c509d374b1e86f15206d22a175a2c7023356e7 Mon Sep 17 00:00:00 2001
From: Dhruva Chakrabarti <Dhruva.Chakrabarti at amd.com>
Date: Mon, 30 Mar 2026 12:31:05 -0500
Subject: [PATCH 1/4] [NFC] [AMDGPU] Added 2 options to rewrite-agpr-copy-mfma
 for debugging.

amdgpu-disable-rewrite-agpr-copy-mfma and amdgpu-rewrite-agpr-copy-mfma-limit
were added.
---
 .../AMDGPU/AMDGPURewriteAGPRCopyMFMA.cpp      | 25 +++++++++++++++++--
 1 file changed, 23 insertions(+), 2 deletions(-)

diff --git a/llvm/lib/Target/AMDGPU/AMDGPURewriteAGPRCopyMFMA.cpp b/llvm/lib/Target/AMDGPU/AMDGPURewriteAGPRCopyMFMA.cpp
index 7a5db42c7a89a..c330763b4614c 100644
--- a/llvm/lib/Target/AMDGPU/AMDGPURewriteAGPRCopyMFMA.cpp
+++ b/llvm/lib/Target/AMDGPU/AMDGPURewriteAGPRCopyMFMA.cpp
@@ -35,11 +35,22 @@
 #include "llvm/CodeGen/SlotIndexes.h"
 #include "llvm/CodeGen/VirtRegMap.h"
 #include "llvm/InitializePasses.h"
+#include "llvm/Support/CommandLine.h"
 
 using namespace llvm;
 
 #define DEBUG_TYPE "amdgpu-rewrite-agpr-copy-mfma"
 
+static cl::opt<bool> DisableRewriteAGPRCopyMFMA(
+    "amdgpu-disable-rewrite-agpr-copy-mfma", cl::Hidden,
+    cl::desc("Disable the post-RA AGPR copy MFMA rewriting pass."),
+    cl::init(false));
+
+static cl::opt<unsigned> RewriteAGPRCopyMFMALimit(
+    "amdgpu-rewrite-agpr-copy-mfma-limit", cl::Hidden,
+    cl::desc("Maximum number of MFMA instructions to rewrite to AGPR form."),
+    cl::init(~0U));
+
 namespace {
 
 STATISTIC(NumMFMAsRewrittenToAGPR,
@@ -59,6 +70,7 @@ class AMDGPURewriteAGPRCopyMFMAImpl {
   LiveIntervals &LIS;
   LiveStacks &LSS;
   const RegisterClassInfo &RegClassInfo;
+  mutable unsigned NumRewritesPerformed = 0;
 
   bool attemptReassignmentsToAGPR(SmallSetVector<Register, 4> &InterferingRegs,
                                   MCPhysReg PrefPhysReg) const;
@@ -267,7 +279,9 @@ bool AMDGPURewriteAGPRCopyMFMAImpl::tryReassigningMFMAChain(
     LRM.unassign(LI);
   }
 
-  if (!attemptReassignmentsToAGPR(RewriteRegs, PhysRegHint)) {
+  if (NumRewritesPerformed + RewriteCandidates.size() >
+          RewriteAGPRCopyMFMALimit ||
+      !attemptReassignmentsToAGPR(RewriteRegs, PhysRegHint)) {
     // Roll back the register assignments to the original state.
     for (auto [LI, OldAssign] : TentativeReassignments) {
       if (VRM.hasPhys(LI->reg()))
@@ -291,6 +305,7 @@ bool AMDGPURewriteAGPRCopyMFMAImpl::tryReassigningMFMAChain(
         AMDGPU::getMFMASrcCVDstAGPROp(RewriteCandidate->getOpcode());
     RewriteCandidate->setDesc(TII.get(NewMFMAOp));
     ++NumMFMAsRewrittenToAGPR;
+    ++NumRewritesPerformed;
   }
 
   return true;
@@ -556,6 +571,9 @@ void AMDGPURewriteAGPRCopyMFMAImpl::eliminateSpillsOfReassignedVGPRs() const {
 }
 
 bool AMDGPURewriteAGPRCopyMFMAImpl::run(MachineFunction &MF) const {
+  if (DisableRewriteAGPRCopyMFMA)
+    return false;
+
   // This only applies on subtargets that have a configurable AGPR vs. VGPR
   // allocation.
   if (!ST.hasGFX90AInsts())
@@ -584,8 +602,11 @@ bool AMDGPURewriteAGPRCopyMFMAImpl::run(MachineFunction &MF) const {
   // If we've successfully rewritten some MFMAs, we've alleviated some VGPR
   // pressure. See if we can eliminate some spills now that those registers are
   // more available.
-  if (MadeChange)
+  if (MadeChange) {
+    LLVM_DEBUG(dbgs() << "Number of MFMA instructions rewritten to AGPR form: "
+                      << NumRewritesPerformed << '\n');
     eliminateSpillsOfReassignedVGPRs();
+  }
 
   return MadeChange;
 }

>From 03021401caa59651e5d3bb13c7a6b393301c3175 Mon Sep 17 00:00:00 2001
From: Dhruva Chakrabarti <Dhruva.Chakrabarti at amd.com>
Date: Wed, 8 Apr 2026 22:34:52 -0500
Subject: [PATCH 2/4] Removed unnecessary counter, instead used the existing
 statistic.

---
 llvm/lib/Target/AMDGPU/AMDGPURewriteAGPRCopyMFMA.cpp | 9 ++-------
 1 file changed, 2 insertions(+), 7 deletions(-)

diff --git a/llvm/lib/Target/AMDGPU/AMDGPURewriteAGPRCopyMFMA.cpp b/llvm/lib/Target/AMDGPU/AMDGPURewriteAGPRCopyMFMA.cpp
index c330763b4614c..ae1a950810f51 100644
--- a/llvm/lib/Target/AMDGPU/AMDGPURewriteAGPRCopyMFMA.cpp
+++ b/llvm/lib/Target/AMDGPU/AMDGPURewriteAGPRCopyMFMA.cpp
@@ -70,7 +70,6 @@ class AMDGPURewriteAGPRCopyMFMAImpl {
   LiveIntervals &LIS;
   LiveStacks &LSS;
   const RegisterClassInfo &RegClassInfo;
-  mutable unsigned NumRewritesPerformed = 0;
 
   bool attemptReassignmentsToAGPR(SmallSetVector<Register, 4> &InterferingRegs,
                                   MCPhysReg PrefPhysReg) const;
@@ -279,7 +278,7 @@ bool AMDGPURewriteAGPRCopyMFMAImpl::tryReassigningMFMAChain(
     LRM.unassign(LI);
   }
 
-  if (NumRewritesPerformed + RewriteCandidates.size() >
+  if (NumMFMAsRewrittenToAGPR + RewriteCandidates.size() >
           RewriteAGPRCopyMFMALimit ||
       !attemptReassignmentsToAGPR(RewriteRegs, PhysRegHint)) {
     // Roll back the register assignments to the original state.
@@ -305,7 +304,6 @@ bool AMDGPURewriteAGPRCopyMFMAImpl::tryReassigningMFMAChain(
         AMDGPU::getMFMASrcCVDstAGPROp(RewriteCandidate->getOpcode());
     RewriteCandidate->setDesc(TII.get(NewMFMAOp));
     ++NumMFMAsRewrittenToAGPR;
-    ++NumRewritesPerformed;
   }
 
   return true;
@@ -602,11 +600,8 @@ bool AMDGPURewriteAGPRCopyMFMAImpl::run(MachineFunction &MF) const {
   // If we've successfully rewritten some MFMAs, we've alleviated some VGPR
   // pressure. See if we can eliminate some spills now that those registers are
   // more available.
-  if (MadeChange) {
-    LLVM_DEBUG(dbgs() << "Number of MFMA instructions rewritten to AGPR form: "
-                      << NumRewritesPerformed << '\n');
+  if (MadeChange)
     eliminateSpillsOfReassignedVGPRs();
-  }
 
   return MadeChange;
 }

>From a822aa02e7b8218736b46efd4e020874de345bf4 Mon Sep 17 00:00:00 2001
From: Dhruva Chakrabarti <Dhruva.Chakrabarti at amd.com>
Date: Thu, 9 Apr 2026 13:35:44 -0500
Subject: [PATCH 3/4] Removed the disable option, other options can be used for
 that.

---
 llvm/lib/Target/AMDGPU/AMDGPURewriteAGPRCopyMFMA.cpp | 8 --------
 1 file changed, 8 deletions(-)

diff --git a/llvm/lib/Target/AMDGPU/AMDGPURewriteAGPRCopyMFMA.cpp b/llvm/lib/Target/AMDGPU/AMDGPURewriteAGPRCopyMFMA.cpp
index ae1a950810f51..c66a3c12800a1 100644
--- a/llvm/lib/Target/AMDGPU/AMDGPURewriteAGPRCopyMFMA.cpp
+++ b/llvm/lib/Target/AMDGPU/AMDGPURewriteAGPRCopyMFMA.cpp
@@ -41,11 +41,6 @@ using namespace llvm;
 
 #define DEBUG_TYPE "amdgpu-rewrite-agpr-copy-mfma"
 
-static cl::opt<bool> DisableRewriteAGPRCopyMFMA(
-    "amdgpu-disable-rewrite-agpr-copy-mfma", cl::Hidden,
-    cl::desc("Disable the post-RA AGPR copy MFMA rewriting pass."),
-    cl::init(false));
-
 static cl::opt<unsigned> RewriteAGPRCopyMFMALimit(
     "amdgpu-rewrite-agpr-copy-mfma-limit", cl::Hidden,
     cl::desc("Maximum number of MFMA instructions to rewrite to AGPR form."),
@@ -569,9 +564,6 @@ void AMDGPURewriteAGPRCopyMFMAImpl::eliminateSpillsOfReassignedVGPRs() const {
 }
 
 bool AMDGPURewriteAGPRCopyMFMAImpl::run(MachineFunction &MF) const {
-  if (DisableRewriteAGPRCopyMFMA)
-    return false;
-
   // This only applies on subtargets that have a configurable AGPR vs. VGPR
   // allocation.
   if (!ST.hasGFX90AInsts())

>From 4bb06e21b0f9d589a9d82f18479770bdbfc8fd91 Mon Sep 17 00:00:00 2001
From: Dhruva Chakrabarti <Dhruva.Chakrabarti at amd.com>
Date: Thu, 9 Apr 2026 23:52:05 -0500
Subject: [PATCH 4/4] Changed to a debug counter.

---
 llvm/lib/Target/AMDGPU/AMDGPURewriteAGPRCopyMFMA.cpp | 11 ++++-------
 1 file changed, 4 insertions(+), 7 deletions(-)

diff --git a/llvm/lib/Target/AMDGPU/AMDGPURewriteAGPRCopyMFMA.cpp b/llvm/lib/Target/AMDGPU/AMDGPURewriteAGPRCopyMFMA.cpp
index c66a3c12800a1..b7cafa5d8f230 100644
--- a/llvm/lib/Target/AMDGPU/AMDGPURewriteAGPRCopyMFMA.cpp
+++ b/llvm/lib/Target/AMDGPU/AMDGPURewriteAGPRCopyMFMA.cpp
@@ -35,16 +35,14 @@
 #include "llvm/CodeGen/SlotIndexes.h"
 #include "llvm/CodeGen/VirtRegMap.h"
 #include "llvm/InitializePasses.h"
-#include "llvm/Support/CommandLine.h"
+#include "llvm/Support/DebugCounter.h"
 
 using namespace llvm;
 
 #define DEBUG_TYPE "amdgpu-rewrite-agpr-copy-mfma"
 
-static cl::opt<unsigned> RewriteAGPRCopyMFMALimit(
-    "amdgpu-rewrite-agpr-copy-mfma-limit", cl::Hidden,
-    cl::desc("Maximum number of MFMA instructions to rewrite to AGPR form."),
-    cl::init(~0U));
+DEBUG_COUNTER(RewriteAGPRCopyMFMACounter, DEBUG_TYPE,
+              "Controls which MFMA chains are rewritten to AGPR form");
 
 namespace {
 
@@ -273,8 +271,7 @@ bool AMDGPURewriteAGPRCopyMFMAImpl::tryReassigningMFMAChain(
     LRM.unassign(LI);
   }
 
-  if (NumMFMAsRewrittenToAGPR + RewriteCandidates.size() >
-          RewriteAGPRCopyMFMALimit ||
+  if (!DebugCounter::shouldExecute(RewriteAGPRCopyMFMACounter) ||
       !attemptReassignmentsToAGPR(RewriteRegs, PhysRegHint)) {
     // Roll back the register assignments to the original state.
     for (auto [LI, OldAssign] : TentativeReassignments) {



More information about the llvm-commits mailing list