25#define DEBUG_TYPE "amdgpu-mc-resource-usage"
31 auto GOCS = [FuncName, &OutContext](
StringRef Suffix) {
38 return GOCS(
".num_vgpr");
40 return GOCS(
".num_agpr");
42 return GOCS(
".numbered_sgpr");
44 return GOCS(
".num_named_barrier");
46 return GOCS(
".private_seg_size");
48 return GOCS(
".uses_vcc");
50 return GOCS(
".uses_flat_scratch");
52 return GOCS(
".has_dyn_sized_stack");
54 return GOCS(
".has_recursion");
56 return GOCS(
".has_indirect_call");
67void MCResourceInfo::assignMaxRegs(
MCContext &OutContext) {
74 auto assignMaxRegSym = [&OutContext](
MCSymbol *Sym, int32_t RegCount) {
79 assignMaxRegSym(MaxVGPRSym, MaxVGPR);
80 assignMaxRegSym(MaxAGPRSym, MaxAGPR);
81 assignMaxRegSym(MaxSGPRSym, MaxSGPR);
82 assignMaxRegSym(MaxNamedBarrierSym, MaxNamedBarrier);
88 assert(!Finalized &&
"Cannot finalize ResourceInfo again.");
90 assignMaxRegs(OutContext);
112const MCExpr *MCResourceInfo::flattenedCycleExpr(
113 MCSymbol *Sym,
const MCExpr *Expr, int64_t LocalValue, ResourceInfoKind RIK,
116 "expected an idempotent resource expression");
121 int64_t ConstantValue = 0;
123 while (!WorkList.
empty()) {
125 if (!Seen.
insert(CurExpr).second)
130 ? std::max(ConstantValue,
Constant->getValue())
141 if (
Ref.isVariable())
143 else if (UnresolvedSymbols.
insert(&
Ref).second)
144 Args.push_back(CurExpr);
149 if (TargetExpr->getKind() == Kind) {
160 <<
": Unexpected cyclic expression; using fallback\n");
189 if (
Args.size() == 1)
194void MCResourceInfo::assignResourceInfoExpr(
198 const TargetMachine &TM = MF.
getTarget();
200 const MCConstantExpr *LocalConstExpr =
202 const MCExpr *SymVal = LocalConstExpr;
205 << LocalValue <<
" as function local usage\n");
206 if (!Callees.
empty()) {
208 SmallPtrSet<const Function *, 8> Seen;
211 for (
const Function *Callee : Callees) {
212 if (!Seen.
insert(Callee).second)
219 << CalleeValSym->
getName() <<
" as callee\n");
222 if (ArgExprs.
size() > 1) {
228 <<
": Normalizing cyclic resource expression\n");
230 flattenedCycleExpr(Sym, SymVal, LocalValue, RIK, Kind, OutContext);
249 bool IsDynamicVGPREnabled =
253 if (!IsDynamicVGPREnabled || !IsChainCC)
263 LLVM_DEBUG(
dbgs() <<
"MCResUse: Gathering resource information for "
274 LLVM_DEBUG(
dbgs() <<
"MCResUse: object linking enabled, no call-graph "
275 "propagation; emitting local resource values only\n");
291 dbgs() <<
"MCResUse: Callees:\n";
294 dbgs() <<
"MCResUse: " << CalleeFnSym->
getName() <<
'\n';
299 auto SetMaxReg = [&](
MCSymbol *MaxSym, int32_t numRegs,
311 <<
": Indirect callee within, using module maximum\n");
344 bool HasPrivateSegmentCycle =
false;
353 <<
" for indirect/recursive callees within\n");
361 if (!Seen.
insert(Callee).second)
365 if (!Callee->isDeclaration() &&
377 << CalleeValSym->
getName() <<
" as callee\n");
384 HasPrivateSegmentCycle =
true;
386 <<
": Private-segment call cycle found; using "
387 "dynamic-stack fallback\n");
391 const MCExpr *localConstExpr =
395 <<
" as function local usage\n");
396 if (!ArgExprs.
empty()) {
414 assignResourceInfoExpr(FRI.
HasRecursion || HasPrivateSegmentCycle,
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
MC infrastructure to propagate the function level resource usage info.
static constexpr Value * getValue(Ty &ValueOrUse)
AMDGPU target specific MCExpr operations.
static const AMDGPUMCExpr * createMax(ArrayRef< const MCExpr * > Args, MCContext &Ctx)
static const AMDGPUMCExpr * createTotalNumVGPR(const MCExpr *NumAGPR, const MCExpr *NumVGPR, MCContext &Ctx)
static const AMDGPUMCExpr * create(VariantKind Kind, ArrayRef< const MCExpr * > Args, MCContext &Ctx)
static const AMDGPUMCExpr * createExtraSGPRs(const MCExpr *VCCUsed, const MCExpr *FlatScrUsed, bool XNACKUsed, MCContext &Ctx)
Allow delayed MCExpr resolve of ExtraSGPRs (in case VCCUsed or FlatScrUsed are unresolvable but neede...
static bool isSymbolUsedInExpression(const MCSymbol *Sym, const MCExpr *E)
static bool EnableObjectLinking
CallingConv::ID getCallingConv() const
getCallingConv()/setCallingConv(CC) - These method get and set the calling convention of this functio...
StringRef getInternalSymbolPrefix() const
static const MCBinaryExpr * createAdd(const MCExpr *LHS, const MCExpr *RHS, MCContext &Ctx, SMLoc Loc=SMLoc())
static LLVM_ABI const MCConstantExpr * create(int64_t Value, MCContext &Ctx, bool PrintInHex=false, unsigned SizeInBytes=0)
Context object for machine code objects.
LLVM_ABI MCSymbol * getOrCreateSymbol(const Twine &Name)
Lookup the symbol inside with the specified Name.
const MCAsmInfo & getAsmInfo() const
Base class for the full range of assembler expressions which are needed for parsing.
MCSymbol * getMaxNamedBarrierSymbol(MCContext &OutContext)
void addMaxSGPRCandidate(int32_t candidate)
const MCExpr * getSymRefExpr(StringRef FuncName, ResourceInfoKind RIK, MCContext &Ctx)
MCSymbol * getMaxSGPRSymbol(MCContext &OutContext)
void addMaxNamedBarrierCandidate(int32_t candidate)
MCSymbol * getMaxAGPRSymbol(MCContext &OutContext)
const MCExpr * createTotalNumVGPRs(const MachineFunction &MF, MCContext &Ctx)
void finalize(MCContext &OutContext)
void addMaxAGPRCandidate(int32_t candidate)
MCSymbol * getMaxVGPRSymbol(MCContext &OutContext)
const MCExpr * createTotalNumSGPRs(const MachineFunction &MF, bool hasXnack, MCContext &Ctx)
MCSymbol * getSymbol(StringRef FuncName, ResourceInfoKind RIK, MCContext &OutContext)
void addMaxVGPRCandidate(int32_t candidate)
void gatherResourceInfo(const MachineFunction &MF, const AMDGPUResourceUsageAnalysisWrapperPass::FunctionResourceInfo &FRI, MCContext &OutContext)
AMDGPUResourceUsageAnalysis gathers resource usage on a per-function granularity.
static const MCSymbolRefExpr * create(const MCSymbol *Symbol, MCContext &Ctx, SMLoc Loc=SMLoc())
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
StringRef getName() const
getName - Get the symbol name.
bool isVariable() const
isVariable - Check if this is a variable symbol.
LLVM_ABI void setVariableValue(const MCExpr *Value)
const MCExpr * getVariableValue() const
Get the expression of the variable symbol.
Function & getFunction()
Return the LLVM function that this machine code represents.
Ty * getInfo()
getInfo - Keep track of various per-function pieces of information for backends that would like to do...
const TargetMachine & getTarget() const
getTarget - Return the target machine this machine code is compiled with
This class keeps track of the SPI_SP_INPUT_ADDR config register, which tells the hardware which inter...
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements.
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
void push_back(const T &Elt)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Represent a constant reference to a string, i.e.
Primary interface to the complete machine description for the target machine.
MCSymbol * getSymbol(const GlobalValue *GV) const
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
LLVM Value Representation.
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
constexpr char Args[]
Key for Kernel::Metadata::mArgs.
LLVM_READNONE constexpr bool isEntryFunctionCC(CallingConv::ID CC)
LLVM_READNONE constexpr bool isChainCC(CallingConv::ID CC)
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
@ AMDGPU_CS
Used for Mesa/AMDPAL compute shaders.
This is an optimization pass for GlobalISel generic memory operations.
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
void append_range(Container &C, Range &&R)
Wrapper function to append range R to container C.
auto reverse(ContainerTy &&C)
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
class LLVM_GSL_OWNER SmallVector
Forward declaration of SmallVector so that calculateSmallVectorDefaultInlinedElements can reference s...
@ Ref
The access may reference the value stored in memory.
bool HasNonChainIndirectCall
bool HasDynamicallySizedStack
uint64_t PrivateSegmentSize
SmallVector< const Function *, 16 > Callees
uint64_t CalleeSegmentSize
AMDGPUResourceUsageAnalysisImpl::SIFunctionResourceInfo FunctionResourceInfo