Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[AMDGPU] Debug dump for AMDGPU resource usage #122952

Merged
merged 3 commits into from
Mar 4, 2025

Conversation

JanekvO
Copy link
Contributor

@JanekvO JanekvO commented Jan 14, 2025

No description provided.

@llvmbot
Copy link
Member

llvmbot commented Jan 14, 2025

@llvm/pr-subscribers-backend-amdgpu

Author: Janek van Oirschot (JanekvO)

Changes

Full diff: https://github.com/llvm/llvm-project/pull/122952.diff

1 Files Affected:

  • (modified) llvm/lib/Target/AMDGPU/AMDGPUMCResourceInfo.cpp (+39-1)
diff --git a/llvm/lib/Target/AMDGPU/AMDGPUMCResourceInfo.cpp b/llvm/lib/Target/AMDGPU/AMDGPUMCResourceInfo.cpp
index 9511b6bb7de062..67cf9044ee37ce 100644
--- a/llvm/lib/Target/AMDGPU/AMDGPUMCResourceInfo.cpp
+++ b/llvm/lib/Target/AMDGPU/AMDGPUMCResourceInfo.cpp
@@ -19,6 +19,8 @@
 #include "llvm/MC/MCSymbol.h"
 #include "llvm/Target/TargetMachine.h"
 
+#define DEBUG_TYPE "amdgpu-mc-resource-usage"
+
 using namespace llvm;
 
 MCSymbol *MCResourceInfo::getSymbol(StringRef FuncName, ResourceInfoKind RIK,
@@ -101,6 +103,8 @@ void MCResourceInfo::assignResourceInfoExpr(
       MCConstantExpr::create(LocalValue, OutContext);
   const MCExpr *SymVal = LocalConstExpr;
   MCSymbol *Sym = getSymbol(FnSym->getName(), RIK, OutContext);
+  LLVM_DEBUG(dbgs() << "MCResUse:   " << Sym->getName() << ": Adding "
+                    << LocalValue << " as function local usage\n");
   if (!Callees.empty()) {
     SmallVector<const MCExpr *, 8> ArgExprs;
     SmallPtrSet<const Function *, 8> Seen;
@@ -119,8 +123,13 @@ void MCResourceInfo::assignResourceInfoExpr(
       if (!CalleeValSym->isVariable() ||
           !CalleeValSym->getVariableValue(/*isUsed=*/false)
                ->isSymbolUsedInExpression(Sym)) {
+        LLVM_DEBUG(dbgs() << "MCResUse:   " << Sym->getName() << ": Adding "
+                          << CalleeValSym->getName() << " as callee\n");
         ArgExprs.push_back(MCSymbolRefExpr::create(CalleeValSym, OutContext));
       } else {
+        LLVM_DEBUG(
+            dbgs() << "MCResUse:   " << Sym->getName()
+                   << ": Recursion found, falling back to module maximum\n");
         // In case of recursion: make sure to use conservative register counts
         // (i.e., specifically for VGPR/SGPR/AGPR).
         switch (RIK) {
@@ -165,6 +174,20 @@ void MCResourceInfo::gatherResourceInfo(
   const TargetMachine &TM = MF.getTarget();
   MCSymbol *FnSym = TM.getSymbol(&MF.getFunction());
 
+  LLVM_DEBUG(dbgs() << "MCResUse: Gathering resource information for "
+                    << FnSym->getName() << "\n");
+  // clang-format off
+  LLVM_DEBUG(
+    if (!FRI.Callees.empty()) {
+      dbgs() << "MCResUse: Callees:\n";
+      for (const Function *Callee : FRI.Callees) {
+        MCSymbol *CalleeFnSym = TM.getSymbol(&Callee->getFunction());
+        dbgs() << "MCResUse:   " << CalleeFnSym->getName() << "\n";
+      }
+    }
+  );
+  // clang-format on
+
   auto SetMaxReg = [&](MCSymbol *MaxSym, int32_t numRegs,
                        ResourceInfoKind RIK) {
     if (!FRI.HasIndirectCall) {
@@ -176,9 +199,12 @@ void MCResourceInfo::gatherResourceInfo(
       const MCExpr *MaxWithLocal = AMDGPUMCExpr::createMax(
           {MCConstantExpr::create(numRegs, OutContext), SymRef}, OutContext);
       LocalNumSym->setVariableValue(MaxWithLocal);
+      LLVM_DEBUG(dbgs() << "MCResUse:   " << LocalNumSym->getName()
+                        << ": Indirect callee within, using module maximum\n");
     }
   };
 
+  LLVM_DEBUG(dbgs() << "MCResUse: " << FnSym->getName() << "\n");
   SetMaxReg(MaxVGPRSym, FRI.NumVGPR, RIK_NumVGPR);
   SetMaxReg(MaxAGPRSym, FRI.NumAGPR, RIK_NumAGPR);
   SetMaxReg(MaxSGPRSym, FRI.NumExplicitSGPR, RIK_NumSGPR);
@@ -188,9 +214,13 @@ void MCResourceInfo::gatherResourceInfo(
     // + max(FRI.Callees, FRI.CalleeSegmentSize)
     SmallVector<const MCExpr *, 8> ArgExprs;
     MCSymbol *Sym = getSymbol(FnSym->getName(), RIK_PrivateSegSize, OutContext);
-    if (FRI.CalleeSegmentSize)
+    if (FRI.CalleeSegmentSize) {
+      LLVM_DEBUG(dbgs() << "MCResUse:   " << Sym->getName() << ": Adding "
+                        << FRI.CalleeSegmentSize
+                        << " for indirect/recursive callees within\n");
       ArgExprs.push_back(
           MCConstantExpr::create(FRI.CalleeSegmentSize, OutContext));
+    }
 
     SmallPtrSet<const Function *, 8> Seen;
     Seen.insert(&MF.getFunction());
@@ -207,12 +237,17 @@ void MCResourceInfo::gatherResourceInfo(
         if (!CalleeValSym->isVariable() ||
             !CalleeValSym->getVariableValue(/*isUsed=*/false)
                  ->isSymbolUsedInExpression(Sym)) {
+          LLVM_DEBUG(dbgs() << "MCResUse:   " << Sym->getName() << ": Adding "
+                            << CalleeValSym->getName() << " as callee\n");
           ArgExprs.push_back(MCSymbolRefExpr::create(CalleeValSym, OutContext));
         }
       }
     }
     const MCExpr *localConstExpr =
         MCConstantExpr::create(FRI.PrivateSegmentSize, OutContext);
+    LLVM_DEBUG(dbgs() << "MCResUse:   " << Sym->getName() << ": Adding "
+                      << FRI.PrivateSegmentSize
+                      << " as function local usage\n");
     if (!ArgExprs.empty()) {
       const AMDGPUMCExpr *transitiveExpr =
           AMDGPUMCExpr::createMax(ArgExprs, OutContext);
@@ -224,6 +259,9 @@ void MCResourceInfo::gatherResourceInfo(
 
   auto SetToLocal = [&](int64_t LocalValue, ResourceInfoKind RIK) {
     MCSymbol *Sym = getSymbol(FnSym->getName(), RIK, OutContext);
+    LLVM_DEBUG(
+        dbgs() << "MCResUse:   " << Sym->getName() << ": Adding " << LocalValue
+               << ", no further propagation as indirect callee found within\n");
     Sym->setVariableValue(MCConstantExpr::create(LocalValue, OutContext));
   };
 

ArgExprs.push_back(MCSymbolRefExpr::create(CalleeValSym, OutContext));
} else {
LLVM_DEBUG(
dbgs() << "MCResUse: " << Sym->getName()
<< ": Recursion found, falling back to module maximum\n");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe this one could be promoted to an opt remark

@arsenm
Copy link
Contributor

arsenm commented Mar 4, 2025

Needs rebase

@JanekvO JanekvO force-pushed the resource-usage-debug-dump branch from e385c22 to 55d4094 Compare March 4, 2025 15:09
@JanekvO JanekvO merged commit 0a93bc7 into llvm:main Mar 4, 2025
11 checks passed
jph-13 pushed a commit to jph-13/llvm-project that referenced this pull request Mar 21, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants