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
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 37 additions & 1 deletion llvm/lib/Target/AMDGPU/AMDGPUMCResourceInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,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,
Expand Down Expand Up @@ -106,6 +108,8 @@ void MCResourceInfo::assignResourceInfoExpr(
MCConstantExpr::create(LocalValue, OutContext);
const MCExpr *SymVal = LocalConstExpr;
MCSymbol *Sym = getSymbol(FnSym->getName(), RIK, OutContext, IsLocal);
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;
Expand All @@ -125,8 +129,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");
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

// In case of recursion: make sure to use conservative register counts
// (i.e., specifically for VGPR/SGPR/AGPR).
switch (RIK) {
Expand Down Expand Up @@ -172,6 +181,18 @@ 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');
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';
}
}
});

auto SetMaxReg = [&](MCSymbol *MaxSym, int32_t numRegs,
ResourceInfoKind RIK) {
if (!FRI.HasIndirectCall) {
Expand All @@ -184,9 +205,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);
Expand All @@ -197,9 +221,13 @@ void MCResourceInfo::gatherResourceInfo(
SmallVector<const MCExpr *, 8> ArgExprs;
MCSymbol *Sym =
getSymbol(FnSym->getName(), RIK_PrivateSegSize, OutContext, IsLocal);
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());
Expand All @@ -218,12 +246,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);
Expand All @@ -235,6 +268,9 @@ void MCResourceInfo::gatherResourceInfo(

auto SetToLocal = [&](int64_t LocalValue, ResourceInfoKind RIK) {
MCSymbol *Sym = getSymbol(FnSym->getName(), RIK, OutContext, IsLocal);
LLVM_DEBUG(
dbgs() << "MCResUse: " << Sym->getName() << ": Adding " << LocalValue
<< ", no further propagation as indirect callee found within\n");
Sym->setVariableValue(MCConstantExpr::create(LocalValue, OutContext));
};

Expand Down