19#define DEBUG_TYPE "memory-profile-info"
24 "memprof-lifetime-access-density-cold-threshold",
cl::init(0.05),
26 cl::desc(
"The threshold the lifetime access density (accesses per byte per "
27 "lifetime sec) must be under to consider an allocation cold"));
33 cl::desc(
"The average lifetime (s) for an allocation to be considered "
39 "memprof-min-ave-lifetime-access-density-hot-threshold",
cl::init(1000),
41 cl::desc(
"The minimum TotalLifetimeAccessDensity / AllocCount for an "
42 "allocation to be considered hot"));
49 if (((
float)TotalLifetimeAccessDensity) / AllocCount / 100 <
52 && ((
float)TotalLifetime) / AllocCount >=
54 return AllocationType::Cold;
58 if (((
float)TotalLifetimeAccessDensity) / AllocCount / 100 >
60 return AllocationType::Hot;
62 return AllocationType::NotCold;
67 std::vector<Metadata *> StackVals;
71 StackVals.push_back(StackValMD);
87 auto *MDS = dyn_cast<MDString>(MIB->
getOperand(1));
89 if (MDS->getString().equals(
"cold")) {
90 return AllocationType::Cold;
91 }
else if (MDS->getString().equals(
"hot")) {
92 return AllocationType::Hot;
94 return AllocationType::NotCold;
99 case AllocationType::NotCold:
102 case AllocationType::Cold:
105 case AllocationType::Hot:
109 assert(
false &&
"Unexpected alloc type");
123 assert(NumAllocTypes != 0);
124 return NumAllocTypes == 1;
130 CallStackTrieNode *Curr =
nullptr;
131 for (
auto StackId : StackIds) {
136 assert(AllocStackId == StackId);
137 Alloc->AllocTypes |=
static_cast<uint8_t
>(
AllocType);
139 AllocStackId = StackId;
140 Alloc =
new CallStackTrieNode(
AllocType);
146 auto Next = Curr->Callers.find(StackId);
147 if (Next != Curr->Callers.end()) {
149 Curr->AllocTypes |=
static_cast<uint8_t
>(
AllocType);
153 auto *New =
new CallStackTrieNode(
AllocType);
154 Curr->Callers[StackId] = New;
165 for (
const auto &MIBStackIter : StackMD->
operands()) {
166 auto *StackId = mdconst::dyn_extract<ConstantInt>(MIBStackIter);
168 CallStack.push_back(StackId->getZExtValue());
174 std::vector<uint64_t> &MIBCallStack,
176 std::vector<Metadata *> MIBPayload(
178 MIBPayload.push_back(
186bool CallStackTrie::buildMIBNodes(CallStackTrieNode *
Node,
LLVMContext &Ctx,
187 std::vector<uint64_t> &MIBCallStack,
188 std::vector<Metadata *> &MIBNodes,
189 bool CalleeHasAmbiguousCallerContext) {
200 if (!
Node->Callers.empty()) {
201 bool NodeHasAmbiguousCallerContext =
Node->Callers.size() > 1;
202 bool AddedMIBNodesForAllCallerContexts =
true;
203 for (
auto &Caller :
Node->Callers) {
204 MIBCallStack.push_back(
Caller.first);
205 AddedMIBNodesForAllCallerContexts &=
206 buildMIBNodes(
Caller.second, Ctx, MIBCallStack, MIBNodes,
207 NodeHasAmbiguousCallerContext);
209 MIBCallStack.pop_back();
211 if (AddedMIBNodesForAllCallerContexts)
215 assert(!NodeHasAmbiguousCallerContext);
228 if (!CalleeHasAmbiguousCallerContext)
243 std::vector<uint64_t> MIBCallStack;
244 MIBCallStack.push_back(AllocStackId);
245 std::vector<Metadata *> MIBNodes;
246 assert(!Alloc->Callers.empty() &&
"addCallStack has not been called yet");
247 buildMIBNodes(Alloc, Ctx, MIBCallStack, MIBNodes,
249 assert(MIBCallStack.size() == 1 &&
250 "Should only be left with Alloc's location in stack");
261 Iter =
End ?
N->op_end() :
N->op_begin();
275 return mdconst::dyn_extract<ConstantInt>(
N->operands().back())
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
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"))
static MDNode * createMIBNode(LLVMContext &Ctx, std::vector< uint64_t > &MIBCallStack, AllocationType AllocType)
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),...
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.
static Constant * get(Type *Ty, uint64_t V, bool IsSigned=false)
If Ty is a vector type, return a Constant with a splat of the given value.
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)
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.
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.
@ First
Helpers to iterate all locations in the MemoryEffectsBase class.
CallStackIterator(const NodeT *N, bool End)