23#define DEBUG_TYPE "memory-profile-info"
27 cl::desc(
"Report total allocation sizes of hinted allocations"));
34 cl::desc(
"Keep all non-cold contexts (increases cloning overheads)"));
38 cl::desc(
"Min percent of cold bytes to hint alloc cold during cloning"));
46 cl::desc(
"Min percent of cold bytes at a callsite to discard non-cold "
53 cl::desc(
"Min percent of max cold bytes for critical cold context"));
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;
103 case AllocationType::NotCold:
106 case AllocationType::Cold:
109 case AllocationType::Hot:
113 assert(
false &&
"Unexpected alloc type");
120 assert(NumAllocTypes != 0);
121 return NumAllocTypes == 1;
126 std::vector<ContextTotalSize> ContextSizeInfo) {
128 CallStackTrieNode *Curr =
nullptr;
129 for (
auto StackId : StackIds) {
134 assert(AllocStackId == StackId);
137 AllocStackId = StackId;
138 Alloc =
new CallStackTrieNode(
AllocType);
144 auto [Next, Inserted] = Curr->Callers.try_emplace(StackId);
151 auto *New =
new CallStackTrieNode(
AllocType);
161 BuiltFromExistingMetadata =
true;
166 for (
const auto &MIBStackIter : StackMD->
operands()) {
167 auto *StackId = mdconst::dyn_extract<ConstantInt>(MIBStackIter);
169 CallStack.push_back(StackId->getZExtValue());
171 std::vector<ContextTotalSize> ContextSizeInfo;
178 mdconst::dyn_extract<ConstantInt>(ContextSizePair->
getOperand(0))
181 mdconst::dyn_extract<ConstantInt>(ContextSizePair->
getOperand(1))
183 ContextSizeInfo.push_back({FullStackId, TotalSize});
193 bool BuiltFromExistingMetadata,
200 if (ContextSizeInfo.
empty()) {
209 for (
const auto &[FullStackId, TotalSize] : ContextSizeInfo) {
210 TotalBytes += TotalSize;
211 bool LargeColdContext =
false;
213 ColdBytes += TotalSize;
219 LargeColdContext =
true;
230 auto *ContextSizeMD =
MDNode::get(Ctx, {FullStackIdMD, TotalSizeMD});
238void CallStackTrie::collectContextSizeInfo(
239 CallStackTrieNode *
Node, std::vector<ContextTotalSize> &ContextSizeInfo) {
241 for (
auto &Caller :
Node->Callers)
242 collectContextSizeInfo(
Caller.second, ContextSizeInfo);
245void CallStackTrie::convertHotToNotCold(CallStackTrieNode *
Node) {
250 for (
auto &Caller :
Node->Callers)
251 convertHotToNotCold(
Caller.second);
257 std::vector<Metadata *> &SavedMIBNodes,
258 unsigned CallerContextLength,
260 bool BuiltFromExistingMetadata) {
261 const bool MostlyCold =
287 mdconst::dyn_extract<ConstantInt>(ContextSizePair->
getOperand(0))
290 mdconst::dyn_extract<ConstantInt>(ContextSizePair->
getOperand(1))
292 errs() <<
"MemProf hinting: Total size for " <<
Tag
293 <<
" non-cold full allocation context hash " << FullStackId
294 << Extra <<
": " << TS <<
"\n";
302 auto NewColdMIBNodes =
304 auto MIBMD = cast<MDNode>(M);
309 const float PercentCold = ColdBytes * 100.0 / TotalBytes;
310 std::string PercentStr;
312 OS <<
format(
" for %5.2f%% cold bytes", PercentCold);
313 EmitMessageForRemovedContexts(MIBMD,
"discarded",
OS.str());
317 for (
auto *M : NewColdMIBNodes)
318 SavedMIBNodes.push_back(M);
349 bool LongerNotColdContextKept =
false;
350 for (
auto *MIB : NewMIBNodes) {
351 auto MIBMD = cast<MDNode>(MIB);
357 LongerNotColdContextKept =
true;
363 bool KeepFirstNewNotCold = !LongerNotColdContextKept;
365 auto MIBMD = cast<MDNode>(M);
375 if (KeepFirstNewNotCold) {
376 KeepFirstNewNotCold =
false;
380 EmitMessageForRemovedContexts(MIBMD,
"pruned",
"");
385 for (
auto *M : NewColdMIBNodes)
386 SavedMIBNodes.push_back(M);
393bool CallStackTrie::buildMIBNodes(CallStackTrieNode *
Node,
LLVMContext &Ctx,
394 std::vector<uint64_t> &MIBCallStack,
395 std::vector<Metadata *> &MIBNodes,
396 bool CalleeHasAmbiguousCallerContext,
401 std::vector<ContextTotalSize> ContextSizeInfo;
402 collectContextSizeInfo(
Node, ContextSizeInfo);
405 MaxColdSize, BuiltFromExistingMetadata, TotalBytes, ColdBytes));
411 if (!
Node->Callers.empty()) {
412 bool NodeHasAmbiguousCallerContext =
Node->Callers.size() > 1;
413 bool AddedMIBNodesForAllCallerContexts =
true;
417 std::vector<Metadata *> NewMIBNodes;
422 for (
auto &Caller :
Node->Callers) {
423 MIBCallStack.push_back(
Caller.first);
424 AddedMIBNodesForAllCallerContexts &= buildMIBNodes(
425 Caller.second, Ctx, MIBCallStack, NewMIBNodes,
426 NodeHasAmbiguousCallerContext, CallerTotalBytes, CallerColdBytes);
428 MIBCallStack.pop_back();
433 CallerTotalBytes, CallerColdBytes,
434 BuiltFromExistingMetadata);
435 TotalBytes += CallerTotalBytes;
436 ColdBytes += CallerColdBytes;
438 if (AddedMIBNodesForAllCallerContexts)
442 assert(!NodeHasAmbiguousCallerContext);
455 if (!CalleeHasAmbiguousCallerContext)
457 std::vector<ContextTotalSize> ContextSizeInfo;
458 collectContextSizeInfo(
Node, ContextSizeInfo);
461 BuiltFromExistingMetadata, TotalBytes, ColdBytes));
471 std::vector<ContextTotalSize> ContextSizeInfo;
472 collectContextSizeInfo(Alloc, ContextSizeInfo);
473 for (
const auto &[FullStackId, TotalSize] : ContextSizeInfo) {
474 errs() <<
"MemProf hinting: Total size for full allocation context hash "
475 << FullStackId <<
" and " << Descriptor <<
" alloc type "
481 <<
ore::NV(
"AllocationCall", CI) <<
" in function "
483 <<
" marked with memprof allocation attribute "
484 <<
ore::NV(
"Attribute", AllocTypeString));
504 convertHotToNotCold(Alloc);
513 std::vector<uint64_t> MIBCallStack;
514 MIBCallStack.push_back(AllocStackId);
515 std::vector<Metadata *> MIBNodes;
518 assert(!Alloc->Callers.empty() &&
"addCallStack has not been called yet");
522 if (buildMIBNodes(Alloc, Ctx, MIBCallStack, MIBNodes,
525 assert(MIBCallStack.size() == 1 &&
526 "Should only be left with Alloc's location in stack");
545 Iter =
End ?
N->op_end() :
N->op_begin();
559 return mdconst::dyn_extract<ConstantInt>(
N->operands().back())
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
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...
LLVM_ABI cl::opt< bool > MemProfKeepAllNotColdContexts("memprof-keep-all-not-cold-contexts", cl::init(false), cl::Hidden, cl::desc("Keep all non-cold contexts (increases cloning overheads)"))
cl::opt< unsigned > MinPercentMaxColdSize("memprof-min-percent-max-cold-size", cl::init(100), cl::Hidden, cl::desc("Min percent of max cold bytes for critical cold context"))
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 > MinCallsiteColdBytePercent("memprof-callsite-cold-threshold", cl::init(100), cl::Hidden, cl::desc("Min percent of cold bytes at a callsite to discard non-cold " "contexts"))
static MDNode * createMIBNode(LLVMContext &Ctx, ArrayRef< uint64_t > MIBCallStack, AllocationType AllocType, ArrayRef< ContextTotalSize > ContextSizeInfo, const uint64_t MaxColdSize, bool BuiltFromExistingMetadata, uint64_t &TotalBytes, uint64_t &ColdBytes)
static void saveFilteredNewMIBNodes(std::vector< Metadata * > &NewMIBNodes, std::vector< Metadata * > &SavedMIBNodes, unsigned CallerContextLength, uint64_t TotalBytes, uint64_t ColdBytes, bool BuiltFromExistingMetadata)
cl::opt< unsigned > MinClonedColdBytePercent("memprof-cloning-cold-threshold", cl::init(100), cl::Hidden, cl::desc("Min percent of cold bytes to hint alloc cold during cloning"))
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 LLVM_ABI 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...
LLVM_ABI const Function * getFunction() const
Return the function this instruction belongs to.
LLVM_ABI 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 LLVM_ABI 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 LLVM_ABI MDNode * getMergedMemProfMetadata(MDNode *A, MDNode *B)
static LLVM_ABI 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 LLVM_ABI IntegerType * getInt64Ty(LLVMContext &C)
LLVM_ABI LLVMContext & getContext() const
All values hold a context through their type.
LLVM_ABI 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.
LLVM_ABI void addSingleAllocTypeAttribute(CallBase *CI, AllocationType AT, StringRef Descriptor)
Add an attribute for the given allocation type to the call instruction.
LLVM_ABI 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...
A raw_ostream that writes to an std::string.
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
initializer< Ty > init(const Ty &Val)
LLVM_ABI MDNode * buildCallstackMetadata(ArrayRef< uint64_t > CallStack, LLVMContext &Ctx)
Build callstack metadata from the provided list of call stack ids.
LLVM_ABI bool recordContextSizeInfoForAnalysis()
Whether we need to record the context size info in the alloc trie used to build metadata.
LLVM_ABI bool metadataIncludesAllContextSizeInfo()
Whether the alloc memeprof metadata will include context size info for all MIBs.
LLVM_ABI AllocationType getMIBAllocType(const MDNode *MIB)
Returns the allocation type from an MIB metadata node.
LLVM_ABI bool metadataMayIncludeContextSizeInfo()
Whether the alloc memprof metadata may include context size info for some MIBs (but possibly not all)...
LLVM_ABI bool hasSingleAllocType(uint8_t AllocTypes)
True if the AllocTypes bitmask contains just a single type.
LLVM_ABI std::string getAllocTypeAttributeString(AllocationType Type)
Returns the string to use in attributes with the given type.
LLVM_ABI 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.
void append_range(Container &C, Range &&R)
Wrapper function to append range R to container C.
iterator_range< filter_iterator< detail::IterOfRange< RangeT >, PredicateT > > make_filter_range(RangeT &&Range, PredicateT Pred)
Convenience function that takes a range of elements and a predicate, and return a new filter_iterator...
format_object< Ts... > format(const char *Fmt, const Ts &... Vals)
These are helper functions used to produce formatted output.
LLVM_ABI 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.
Used in the streaming interface as the general argument type.
CallStackIterator(const NodeT *N, bool End)