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(
"Enable use of hot hints (only supported for "
48 "unambigously hot allocations)"));
52 cl::desc(
"Report total allocation sizes of hinted allocations"));
59 if (((
float)TotalLifetimeAccessDensity) / AllocCount / 100 <
62 && ((
float)TotalLifetime) / AllocCount >=
64 return AllocationType::Cold;
69 ((
float)TotalLifetimeAccessDensity) / AllocCount / 100 >
71 return AllocationType::Hot;
73 return AllocationType::NotCold;
99 auto *MDS = dyn_cast<MDString>(MIB->
getOperand(1));
101 if (MDS->getString() ==
"cold") {
102 return AllocationType::Cold;
103 }
else if (MDS->getString() ==
"hot") {
104 return AllocationType::Hot;
106 return AllocationType::NotCold;
111 case AllocationType::NotCold:
114 case AllocationType::Cold:
117 case AllocationType::Hot:
121 assert(
false &&
"Unexpected alloc type");
135 assert(NumAllocTypes != 0);
136 return NumAllocTypes == 1;
141 std::vector<ContextTotalSize> ContextSizeInfo) {
143 CallStackTrieNode *Curr =
nullptr;
144 for (
auto StackId : StackIds) {
149 assert(AllocStackId == StackId);
152 AllocStackId = StackId;
153 Alloc =
new CallStackTrieNode(
AllocType);
159 auto Next = Curr->Callers.find(StackId);
160 if (Next != Curr->Callers.end()) {
166 auto *New =
new CallStackTrieNode(
AllocType);
167 Curr->Callers[StackId] = New;
171 Curr->ContextSizeInfo.insert(Curr->ContextSizeInfo.end(),
172 ContextSizeInfo.begin(), ContextSizeInfo.end());
180 for (
const auto &MIBStackIter : StackMD->
operands()) {
181 auto *StackId = mdconst::dyn_extract<ConstantInt>(MIBStackIter);
183 CallStack.push_back(StackId->getZExtValue());
185 std::vector<ContextTotalSize> ContextSizeInfo;
192 mdconst::dyn_extract<ConstantInt>(ContextSizePair->
getOperand(0))
195 mdconst::dyn_extract<ConstantInt>(ContextSizePair->
getOperand(1))
197 ContextSizeInfo.push_back({FullStackId, TotalSize});
210 if (!ContextSizeInfo.
empty()) {
211 for (
const auto &[FullStackId, TotalSize] : ContextSizeInfo) {
216 auto *ContextSizeMD =
MDNode::get(Ctx, {FullStackIdMD, TotalSizeMD});
223void CallStackTrie::collectContextSizeInfo(
224 CallStackTrieNode *
Node, std::vector<ContextTotalSize> &ContextSizeInfo) {
225 ContextSizeInfo.insert(ContextSizeInfo.end(),
Node->ContextSizeInfo.begin(),
226 Node->ContextSizeInfo.end());
227 for (
auto &Caller :
Node->Callers)
228 collectContextSizeInfo(
Caller.second, ContextSizeInfo);
231void CallStackTrie::convertHotToNotCold(CallStackTrieNode *
Node) {
236 for (
auto &Caller :
Node->Callers)
237 convertHotToNotCold(
Caller.second);
243bool CallStackTrie::buildMIBNodes(CallStackTrieNode *
Node,
LLVMContext &Ctx,
244 std::vector<uint64_t> &MIBCallStack,
245 std::vector<Metadata *> &MIBNodes,
246 bool CalleeHasAmbiguousCallerContext) {
250 std::vector<ContextTotalSize> ContextSizeInfo;
251 collectContextSizeInfo(
Node, ContextSizeInfo);
259 if (!
Node->Callers.empty()) {
260 bool NodeHasAmbiguousCallerContext =
Node->Callers.size() > 1;
261 bool AddedMIBNodesForAllCallerContexts =
true;
262 for (
auto &Caller :
Node->Callers) {
263 MIBCallStack.push_back(
Caller.first);
264 AddedMIBNodesForAllCallerContexts &=
265 buildMIBNodes(
Caller.second, Ctx, MIBCallStack, MIBNodes,
266 NodeHasAmbiguousCallerContext);
268 MIBCallStack.pop_back();
270 if (AddedMIBNodesForAllCallerContexts)
274 assert(!NodeHasAmbiguousCallerContext);
287 if (!CalleeHasAmbiguousCallerContext)
289 std::vector<ContextTotalSize> ContextSizeInfo;
290 collectContextSizeInfo(
Node, ContextSizeInfo);
300 std::vector<ContextTotalSize> ContextSizeInfo;
301 collectContextSizeInfo(Alloc, ContextSizeInfo);
302 for (
const auto &[FullStackId, TotalSize] : ContextSizeInfo) {
303 errs() <<
"MemProf hinting: Total size for full allocation context hash "
304 << FullStackId <<
" and " << Descriptor <<
" alloc type "
327 convertHotToNotCold(Alloc);
336 std::vector<uint64_t> MIBCallStack;
337 MIBCallStack.push_back(AllocStackId);
338 std::vector<Metadata *> MIBNodes;
339 assert(!Alloc->Callers.empty() &&
"addCallStack has not been called yet");
343 if (buildMIBNodes(Alloc, Ctx, MIBCallStack, MIBNodes,
false)) {
344 assert(MIBCallStack.size() == 1 &&
345 "Should only be left with Alloc's location in stack");
364 Iter =
End ?
N->op_end() :
N->op_begin();
378 return mdconst::dyn_extract<ConstantInt>(
N->operands().back())
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
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 > MemProfUseHotHints("memprof-use-hot-hints", cl::init(false), cl::Hidden, cl::desc("Enable use of hot hints (only supported for " "unambigously hot allocations)"))
cl::opt< bool > MemProfReportHintedSizes("memprof-report-hinted-sizes", cl::init(false), cl::Hidden, cl::desc("Report total allocation sizes of hinted allocations"))
static MDNode * createMIBNode(LLVMContext &Ctx, ArrayRef< uint64_t > MIBCallStack, AllocationType AllocType, ArrayRef< ContextTotalSize > ContextSizeInfo)
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 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),...
bool empty() const
empty - Check if the array is empty.
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.
static MDNode * getMergedCallsiteMetadata(MDNode *A, MDNode *B)
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 MDNode * getMergedMemProfMetadata(MDNode *A, MDNode *B)
static MDString * get(LLVMContext &Context, StringRef Str)
void push_back(Metadata *MD)
Append an element to the tuple. This will resize the node.
void reserve(size_type N)
void push_back(const T &Elt)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
StringRef - Represent a constant reference to a string, i.e.
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, std::vector< ContextTotalSize > ContextSizeInfo={})
Add a call stack context with the given allocation type to the Trie.
void addSingleAllocTypeAttribute(CallBase *CI, AllocationType AT, StringRef Descriptor)
Add an attribute for the given allocation type to the call instruction.
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.
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)