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"));
46 cl::desc(
"Report total allocation sizes of hinted allocations"));
53 if (((
float)TotalLifetimeAccessDensity) / AllocCount / 100 <
56 && ((
float)TotalLifetime) / AllocCount >=
58 return AllocationType::Cold;
62 if (((
float)TotalLifetimeAccessDensity) / AllocCount / 100 >
64 return AllocationType::Hot;
66 return AllocationType::NotCold;
71 std::vector<Metadata *> StackVals;
75 StackVals.push_back(StackValMD);
91 auto *MDS = dyn_cast<MDString>(MIB->
getOperand(1));
93 if (MDS->getString() ==
"cold") {
94 return AllocationType::Cold;
95 }
else if (MDS->getString() ==
"hot") {
96 return AllocationType::Hot;
98 return AllocationType::NotCold;
104 return mdconst::dyn_extract<ConstantInt>(MIB->
getOperand(2))->getZExtValue();
109 case AllocationType::NotCold:
112 case AllocationType::Cold:
115 case AllocationType::Hot:
119 assert(
false &&
"Unexpected alloc type");
133 assert(NumAllocTypes != 0);
134 return NumAllocTypes == 1;
141 CallStackTrieNode *Curr =
nullptr;
142 for (
auto StackId : StackIds) {
147 assert(AllocStackId == StackId);
148 Alloc->AllocTypes |=
static_cast<uint8_t
>(
AllocType);
149 Alloc->TotalSize += TotalSize;
151 AllocStackId = StackId;
152 Alloc =
new CallStackTrieNode(
AllocType, TotalSize);
158 auto Next = Curr->Callers.find(StackId);
159 if (Next != Curr->Callers.end()) {
161 Curr->AllocTypes |=
static_cast<uint8_t
>(
AllocType);
162 Curr->TotalSize += TotalSize;
166 auto *New =
new CallStackTrieNode(
AllocType, TotalSize);
167 Curr->Callers[StackId] = New;
178 for (
const auto &MIBStackIter : StackMD->
operands()) {
179 auto *StackId = mdconst::dyn_extract<ConstantInt>(MIBStackIter);
181 CallStack.push_back(StackId->getZExtValue());
187 std::vector<uint64_t> &MIBCallStack,
189 std::vector<Metadata *> MIBPayload(
191 MIBPayload.push_back(
202bool CallStackTrie::buildMIBNodes(CallStackTrieNode *
Node,
LLVMContext &Ctx,
203 std::vector<uint64_t> &MIBCallStack,
204 std::vector<Metadata *> &MIBNodes,
205 bool CalleeHasAmbiguousCallerContext) {
216 if (!
Node->Callers.empty()) {
217 bool NodeHasAmbiguousCallerContext =
Node->Callers.size() > 1;
218 bool AddedMIBNodesForAllCallerContexts =
true;
219 for (
auto &Caller :
Node->Callers) {
220 MIBCallStack.push_back(
Caller.first);
221 AddedMIBNodesForAllCallerContexts &=
222 buildMIBNodes(
Caller.second, Ctx, MIBCallStack, MIBNodes,
223 NodeHasAmbiguousCallerContext);
225 MIBCallStack.pop_back();
227 if (AddedMIBNodesForAllCallerContexts)
231 assert(!NodeHasAmbiguousCallerContext);
244 if (!CalleeHasAmbiguousCallerContext)
260 errs() <<
"Total size for allocation with location hash " << AllocStackId
261 <<
" and single alloc type "
263 <<
": " << Alloc->TotalSize <<
"\n";
267 std::vector<uint64_t> MIBCallStack;
268 MIBCallStack.push_back(AllocStackId);
269 std::vector<Metadata *> MIBNodes;
270 assert(!Alloc->Callers.empty() &&
"addCallStack has not been called yet");
274 if (buildMIBNodes(Alloc, Ctx, MIBCallStack, MIBNodes,
false)) {
275 assert(MIBCallStack.size() == 1 &&
276 "Should only be left with Alloc's location in stack");
294 Iter =
End ?
N->op_end() :
N->op_begin();
308 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"))
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)