20#define DEBUG_TYPE "memory-profile-info"
25 "memprof-lifetime-access-density-cold-threshold",
cl::init(0.05),
27 cl::desc(
"The threshold the lifetime access density (accesses per byte per "
28 "lifetime sec) must be under to consider an allocation cold"));
34 cl::desc(
"The average lifetime (s) for an allocation to be considered "
40 "memprof-min-ave-lifetime-access-density-hot-threshold",
cl::init(1000),
42 cl::desc(
"The minimum TotalLifetimeAccessDensity / AllocCount for an "
43 "allocation to be considered hot"));
47 cl::desc(
"Report total allocation sizes of hinted allocations"));
54 if (((
float)TotalLifetimeAccessDensity) / AllocCount / 100 <
57 && ((
float)TotalLifetime) / AllocCount >=
59 return AllocationType::Cold;
63 if (((
float)TotalLifetimeAccessDensity) / AllocCount / 100 >
65 return AllocationType::Hot;
67 return AllocationType::NotCold;
72 std::vector<Metadata *> StackVals;
76 StackVals.push_back(StackValMD);
92 auto *MDS = dyn_cast<MDString>(MIB->
getOperand(1));
94 if (MDS->getString() ==
"cold") {
95 return AllocationType::Cold;
96 }
else if (MDS->getString() ==
"hot") {
97 return AllocationType::Hot;
99 return AllocationType::NotCold;
105 return mdconst::dyn_extract<ConstantInt>(MIB->
getOperand(2))->getZExtValue();
110 case AllocationType::NotCold:
113 case AllocationType::Cold:
116 case AllocationType::Hot:
120 assert(
false &&
"Unexpected alloc type");
134 assert(NumAllocTypes != 0);
135 return NumAllocTypes == 1;
142 CallStackTrieNode *Curr =
nullptr;
143 for (
auto StackId : StackIds) {
148 assert(AllocStackId == StackId);
149 Alloc->AllocTypes |=
static_cast<uint8_t
>(
AllocType);
150 Alloc->TotalSize += TotalSize;
152 AllocStackId = StackId;
153 Alloc =
new CallStackTrieNode(
AllocType, TotalSize);
159 auto Next = Curr->Callers.find(StackId);
160 if (Next != Curr->Callers.end()) {
162 Curr->AllocTypes |=
static_cast<uint8_t
>(
AllocType);
163 Curr->TotalSize += TotalSize;
167 auto *New =
new CallStackTrieNode(
AllocType, TotalSize);
168 Curr->Callers[StackId] = New;
179 for (
const auto &MIBStackIter : StackMD->
operands()) {
180 auto *StackId = mdconst::dyn_extract<ConstantInt>(MIBStackIter);
182 CallStack.push_back(StackId->getZExtValue());
188 std::vector<uint64_t> &MIBCallStack,
190 std::vector<Metadata *> MIBPayload(
192 MIBPayload.push_back(
203bool CallStackTrie::buildMIBNodes(CallStackTrieNode *
Node,
LLVMContext &Ctx,
204 std::vector<uint64_t> &MIBCallStack,
205 std::vector<Metadata *> &MIBNodes,
206 bool CalleeHasAmbiguousCallerContext) {
217 if (!
Node->Callers.empty()) {
218 bool NodeHasAmbiguousCallerContext =
Node->Callers.size() > 1;
219 bool AddedMIBNodesForAllCallerContexts =
true;
220 for (
auto &Caller :
Node->Callers) {
221 MIBCallStack.push_back(
Caller.first);
222 AddedMIBNodesForAllCallerContexts &=
223 buildMIBNodes(
Caller.second, Ctx, MIBCallStack, MIBNodes,
224 NodeHasAmbiguousCallerContext);
226 MIBCallStack.pop_back();
228 if (AddedMIBNodesForAllCallerContexts)
232 assert(!NodeHasAmbiguousCallerContext);
245 if (!CalleeHasAmbiguousCallerContext)
261 errs() <<
"Total size for allocation with location hash " << AllocStackId
262 <<
" and single alloc type "
264 <<
": " << Alloc->TotalSize <<
"\n";
268 std::vector<uint64_t> MIBCallStack;
269 MIBCallStack.push_back(AllocStackId);
270 std::vector<Metadata *> MIBNodes;
271 assert(!Alloc->Callers.empty() &&
"addCallStack has not been called yet");
275 if (buildMIBNodes(Alloc, Ctx, MIBCallStack, MIBNodes,
false)) {
276 assert(MIBCallStack.size() == 1 &&
277 "Should only be left with Alloc's location in stack");
295 Iter =
End ?
N->op_end() :
N->op_begin();
309 return mdconst::dyn_extract<ConstantInt>(
N->operands().back())
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
This file contains the declarations for the subclasses of Constant, which represent the different fla...
cl::opt< float > MemProfLifetimeAccessDensityColdThreshold("memprof-lifetime-access-density-cold-threshold", cl::init(0.05), cl::Hidden, cl::desc("The threshold the lifetime access density (accesses per byte per " "lifetime sec) must be under to consider an allocation cold"))
cl::opt< unsigned > MemProfMinAveLifetimeAccessDensityHotThreshold("memprof-min-ave-lifetime-access-density-hot-threshold", cl::init(1000), cl::Hidden, cl::desc("The minimum TotalLifetimeAccessDensity / AllocCount for an " "allocation to be considered hot"))
cl::opt< bool > MemProfReportHintedSizes("memprof-report-hinted-sizes", cl::init(false), cl::Hidden, cl::desc("Report total allocation sizes of hinted allocations"))
cl::opt< unsigned > MemProfAveLifetimeColdThreshold("memprof-ave-lifetime-cold-threshold", cl::init(200), cl::Hidden, cl::desc("The average lifetime (s) for an allocation to be considered " "cold"))
static MDNode * createMIBNode(LLVMContext &Ctx, std::vector< uint64_t > &MIBCallStack, AllocationType AllocType, uint64_t TotalSize)
static void addAllocTypeAttribute(LLVMContext &Ctx, CallBase *CI, AllocationType AllocType)
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
static Attribute get(LLVMContext &Context, AttrKind Kind, uint64_t Val=0)
Return a uniquified Attribute object.
Base class for all callable instructions (InvokeInst and CallInst) Holds everything related to callin...
void addFnAttr(Attribute::AttrKind Kind)
Adds the attribute to the function.
This is the shared class of boolean and integer constants.
uint64_t getZExtValue() const
Return the constant as a 64-bit unsigned integer value after it has been zero extended as appropriate...
void setMetadata(unsigned KindID, MDNode *Node)
Set the metadata of the specified kind to the specified node.
This is an important class for using LLVM in a threaded context.
const MDOperand & getOperand(unsigned I) const
ArrayRef< MDOperand > operands() const
static MDTuple * get(LLVMContext &Context, ArrayRef< Metadata * > MDs)
unsigned getNumOperands() const
Return number of MDNode operands.
static MDString * get(LLVMContext &Context, StringRef Str)
The instances of the Type class are immutable: once they are created, they are never changed.
static IntegerType * getInt64Ty(LLVMContext &C)
LLVMContext & getContext() const
All values hold a context through their type.
void addCallStack(AllocationType AllocType, ArrayRef< uint64_t > StackIds, uint64_t TotalSize=0)
Add a call stack context with the given allocation type to the Trie.
bool buildAndAttachMIBMetadata(CallBase *CI)
Build and attach the minimal necessary MIB metadata.
Helper class to iterate through stack ids in both metadata (memprof MIB and callsite) and the corresp...
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
initializer< Ty > init(const Ty &Val)
MDNode * buildCallstackMetadata(ArrayRef< uint64_t > CallStack, LLVMContext &Ctx)
Build callstack metadata from the provided list of call stack ids.
AllocationType getAllocType(uint64_t TotalLifetimeAccessDensity, uint64_t AllocCount, uint64_t TotalLifetime)
Return the allocation type for a given set of memory profile values.
AllocationType getMIBAllocType(const MDNode *MIB)
Returns the allocation type from an MIB metadata node.
uint64_t getMIBTotalSize(const MDNode *MIB)
Returns the total size from an MIB metadata node, or 0 if it was not recorded.
bool hasSingleAllocType(uint8_t AllocTypes)
True if the AllocTypes bitmask contains just a single type.
std::string getAllocTypeAttributeString(AllocationType Type)
Returns the string to use in attributes with the given type.
MDNode * getMIBStackNode(const MDNode *MIB)
Returns the stack node from an MIB metadata node.
This is an optimization pass for GlobalISel generic memory operations.
int popcount(T Value) noexcept
Count the number of set bits in a value.
raw_fd_ostream & errs()
This returns a reference to a raw_ostream for standard error.
@ First
Helpers to iterate all locations in the MemoryEffectsBase class.
CallStackIterator(const NodeT *N, bool End)