LLVM 22.0.0git
MemoryProfileInfo.cpp
Go to the documentation of this file.
1//===-- MemoryProfileInfo.cpp - memory profile info ------------------------==//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This file contains utilities to analyze memory profile information.
10//
11//===----------------------------------------------------------------------===//
12
15#include "llvm/IR/Constants.h"
18#include "llvm/Support/Format.h"
19
20using namespace llvm;
21using namespace llvm::memprof;
22
23#define DEBUG_TYPE "memory-profile-info"
24
25namespace llvm {
26
28 "memprof-report-hinted-sizes", cl::init(false), cl::Hidden,
29 cl::desc("Report total allocation sizes of hinted allocations"));
30
31// This is useful if we have enabled reporting of hinted sizes, and want to get
32// information from the indexing step for all contexts (especially for testing),
33// or have specified a value less than 100% for -memprof-cloning-cold-threshold.
35 "memprof-keep-all-not-cold-contexts", cl::init(false), cl::Hidden,
36 cl::desc("Keep all non-cold contexts (increases cloning overheads)"));
37
39 "memprof-cloning-cold-threshold", cl::init(100), cl::Hidden,
40 cl::desc("Min percent of cold bytes to hint alloc cold during cloning"));
41
42// Discard non-cold contexts if they overlap with much larger cold contexts,
43// specifically, if all contexts reaching a given callsite are at least this
44// percent cold byte allocations. This reduces the amount of cloning required
45// to expose the cold contexts when they greatly dominate non-cold contexts.
47 "memprof-callsite-cold-threshold", cl::init(100), cl::Hidden,
48 cl::desc("Min percent of cold bytes at a callsite to discard non-cold "
49 "contexts"));
50
51// Enable saving context size information for largest cold contexts, which can
52// be used to flag contexts for more aggressive cloning and reporting.
54 "memprof-min-percent-max-cold-size", cl::init(100), cl::Hidden,
55 cl::desc("Min percent of max cold bytes for critical cold context"));
56
57} // end namespace llvm
58
62
66
71
73 LLVMContext &Ctx) {
75 StackVals.reserve(CallStack.size());
76 for (auto Id : CallStack) {
77 auto *StackValMD =
78 ValueAsMetadata::get(ConstantInt::get(Type::getInt64Ty(Ctx), Id));
79 StackVals.push_back(StackValMD);
80 }
81 return MDNode::get(Ctx, StackVals);
82}
83
85 assert(MIB->getNumOperands() >= 2);
86 // The stack metadata is the first operand of each memprof MIB metadata.
87 return cast<MDNode>(MIB->getOperand(0));
88}
89
91 assert(MIB->getNumOperands() >= 2);
92 // The allocation type is currently the second operand of each memprof
93 // MIB metadata. This will need to change as we add additional allocation
94 // types that can be applied based on the allocation profile data.
95 auto *MDS = dyn_cast<MDString>(MIB->getOperand(1));
96 assert(MDS);
97 if (MDS->getString() == "cold") {
99 } else if (MDS->getString() == "hot") {
100 return AllocationType::Hot;
101 }
103}
104
106 switch (Type) {
108 return "notcold";
109 break;
111 return "cold";
112 break;
114 return "hot";
115 break;
116 default:
117 assert(false && "Unexpected alloc type");
118 }
119 llvm_unreachable("invalid alloc type");
120}
121
123 const unsigned NumAllocTypes = llvm::popcount(AllocTypes);
124 assert(NumAllocTypes != 0);
125 return NumAllocTypes == 1;
126}
127
129 if (!CB->hasFnAttr("memprof"))
130 return;
131 assert(CB->getFnAttr("memprof").getValueAsString() == "ambiguous");
132 CB->removeFnAttr("memprof");
133}
134
136 // We may have an existing ambiguous attribute if we are reanalyzing
137 // after inlining.
138 if (CB->hasFnAttr("memprof")) {
139 assert(CB->getFnAttr("memprof").getValueAsString() == "ambiguous");
140 } else {
141 auto A = llvm::Attribute::get(CB->getContext(), "memprof", "ambiguous");
142 CB->addFnAttr(A);
143 }
144}
145
147 AllocationType AllocType, ArrayRef<uint64_t> StackIds,
148 std::vector<ContextTotalSize> ContextSizeInfo) {
149 bool First = true;
150 CallStackTrieNode *Curr = nullptr;
151 for (auto StackId : StackIds) {
152 // If this is the first stack frame, add or update alloc node.
153 if (First) {
154 First = false;
155 if (Alloc) {
156 assert(AllocStackId == StackId);
157 Alloc->addAllocType(AllocType);
158 } else {
159 AllocStackId = StackId;
160 Alloc = new CallStackTrieNode(AllocType);
161 }
162 Curr = Alloc;
163 continue;
164 }
165 // Update existing caller node if it exists.
166 auto [Next, Inserted] = Curr->Callers.try_emplace(StackId);
167 if (!Inserted) {
168 Curr = Next->second;
169 Curr->addAllocType(AllocType);
170 continue;
171 }
172 // Otherwise add a new caller node.
173 auto *New = new CallStackTrieNode(AllocType);
174 Next->second = New;
175 Curr = New;
176 }
177 assert(Curr);
178 llvm::append_range(Curr->ContextSizeInfo, ContextSizeInfo);
179}
180
182 // Note that we are building this from existing MD_memprof metadata.
183 BuiltFromExistingMetadata = true;
184 MDNode *StackMD = getMIBStackNode(MIB);
185 assert(StackMD);
186 std::vector<uint64_t> CallStack;
187 CallStack.reserve(StackMD->getNumOperands());
188 for (const auto &MIBStackIter : StackMD->operands()) {
189 auto *StackId = mdconst::dyn_extract<ConstantInt>(MIBStackIter);
190 assert(StackId);
191 CallStack.push_back(StackId->getZExtValue());
192 }
193 std::vector<ContextTotalSize> ContextSizeInfo;
194 // Collect the context size information if it exists.
195 if (MIB->getNumOperands() > 2) {
196 for (unsigned I = 2; I < MIB->getNumOperands(); I++) {
197 MDNode *ContextSizePair = dyn_cast<MDNode>(MIB->getOperand(I));
198 assert(ContextSizePair->getNumOperands() == 2);
199 uint64_t FullStackId =
201 ->getZExtValue();
202 uint64_t TotalSize =
204 ->getZExtValue();
205 ContextSizeInfo.push_back({FullStackId, TotalSize});
206 }
207 }
208 addCallStack(getMIBAllocType(MIB), CallStack, std::move(ContextSizeInfo));
209}
210
213 ArrayRef<ContextTotalSize> ContextSizeInfo,
214 const uint64_t MaxColdSize,
215 bool BuiltFromExistingMetadata,
216 uint64_t &TotalBytes, uint64_t &ColdBytes) {
217 SmallVector<Metadata *> MIBPayload(
218 {buildCallstackMetadata(MIBCallStack, Ctx)});
219 MIBPayload.push_back(
221
222 if (ContextSizeInfo.empty()) {
223 // The profile matcher should have provided context size info if there was a
224 // MinCallsiteColdBytePercent < 100. Here we check >=100 to gracefully
225 // handle a user-provided percent larger than 100. However, we may not have
226 // this information if we built the Trie from existing MD_memprof metadata.
227 assert(BuiltFromExistingMetadata || MinCallsiteColdBytePercent >= 100);
228 return MDNode::get(Ctx, MIBPayload);
229 }
230
231 for (const auto &[FullStackId, TotalSize] : ContextSizeInfo) {
232 TotalBytes += TotalSize;
233 bool LargeColdContext = false;
235 ColdBytes += TotalSize;
236 // If we have the max cold context size from summary information and have
237 // requested identification of contexts above a percentage of the max, see
238 // if this context qualifies.
239 if (MaxColdSize > 0 && MinPercentMaxColdSize < 100 &&
240 TotalSize * 100 >= MaxColdSize * MinPercentMaxColdSize)
241 LargeColdContext = true;
242 }
243 // Only add the context size info as metadata if we need it in the thin
244 // link (currently if reporting of hinted sizes is enabled, we have
245 // specified a threshold for marking allocations cold after cloning, or we
246 // have identified this as a large cold context of interest above).
247 if (metadataIncludesAllContextSizeInfo() || LargeColdContext) {
248 auto *FullStackIdMD = ValueAsMetadata::get(
249 ConstantInt::get(Type::getInt64Ty(Ctx), FullStackId));
250 auto *TotalSizeMD = ValueAsMetadata::get(
251 ConstantInt::get(Type::getInt64Ty(Ctx), TotalSize));
252 auto *ContextSizeMD = MDNode::get(Ctx, {FullStackIdMD, TotalSizeMD});
253 MIBPayload.push_back(ContextSizeMD);
254 }
255 }
256 assert(TotalBytes > 0);
257 return MDNode::get(Ctx, MIBPayload);
258}
259
260void CallStackTrie::collectContextSizeInfo(
261 CallStackTrieNode *Node, std::vector<ContextTotalSize> &ContextSizeInfo) {
262 llvm::append_range(ContextSizeInfo, Node->ContextSizeInfo);
263 for (auto &Caller : Node->Callers)
264 collectContextSizeInfo(Caller.second, ContextSizeInfo);
265}
266
267void CallStackTrie::convertHotToNotCold(CallStackTrieNode *Node) {
268 if (Node->hasAllocType(AllocationType::Hot)) {
269 Node->removeAllocType(AllocationType::Hot);
270 Node->addAllocType(AllocationType::NotCold);
271 }
272 for (auto &Caller : Node->Callers)
273 convertHotToNotCold(Caller.second);
274}
275
276// Copy over some or all of NewMIBNodes to the SavedMIBNodes vector, depending
277// on options that enable filtering out some NotCold contexts.
278static void saveFilteredNewMIBNodes(std::vector<Metadata *> &NewMIBNodes,
279 std::vector<Metadata *> &SavedMIBNodes,
280 unsigned CallerContextLength,
281 uint64_t TotalBytes, uint64_t ColdBytes,
282 bool BuiltFromExistingMetadata) {
283 const bool MostlyCold =
284 // If we have built the Trie from existing MD_memprof metadata, we may or
285 // may not have context size information (in which case ColdBytes and
286 // TotalBytes are 0, which is not also guarded against below). Even if we
287 // do have some context size information from the the metadata, we have
288 // already gone through a round of discarding of small non-cold contexts
289 // during matching, and it would be overly aggressive to do it again, and
290 // we also want to maintain the same behavior with and without reporting
291 // of hinted bytes enabled.
292 !BuiltFromExistingMetadata && MinCallsiteColdBytePercent < 100 &&
293 ColdBytes > 0 &&
294 ColdBytes * 100 >= MinCallsiteColdBytePercent * TotalBytes;
295
296 // In the simplest case, with pruning disabled, keep all the new MIB nodes.
297 if (MemProfKeepAllNotColdContexts && !MostlyCold) {
298 append_range(SavedMIBNodes, NewMIBNodes);
299 return;
300 }
301
302 auto EmitMessageForRemovedContexts = [](const MDNode *MIBMD, StringRef Tag,
303 StringRef Extra) {
304 assert(MIBMD->getNumOperands() > 2);
305 for (unsigned I = 2; I < MIBMD->getNumOperands(); I++) {
306 MDNode *ContextSizePair = dyn_cast<MDNode>(MIBMD->getOperand(I));
307 assert(ContextSizePair->getNumOperands() == 2);
308 uint64_t FullStackId =
310 ->getZExtValue();
311 uint64_t TS =
313 ->getZExtValue();
314 errs() << "MemProf hinting: Total size for " << Tag
315 << " non-cold full allocation context hash " << FullStackId
316 << Extra << ": " << TS << "\n";
317 }
318 };
319
320 // If the cold bytes at the current callsite exceed the given threshold, we
321 // discard all non-cold contexts so do not need any of the later pruning
322 // handling. We can simply copy over all the cold contexts and return early.
323 if (MostlyCold) {
324 auto NewColdMIBNodes =
325 make_filter_range(NewMIBNodes, [&](const Metadata *M) {
326 auto MIBMD = cast<MDNode>(M);
327 // Only append cold contexts.
329 return true;
331 const float PercentCold = ColdBytes * 100.0 / TotalBytes;
332 std::string PercentStr;
333 llvm::raw_string_ostream OS(PercentStr);
334 OS << format(" for %5.2f%% cold bytes", PercentCold);
335 EmitMessageForRemovedContexts(MIBMD, "discarded", OS.str());
336 }
337 return false;
338 });
339 for (auto *M : NewColdMIBNodes)
340 SavedMIBNodes.push_back(M);
341 return;
342 }
343
344 // Prune unneeded NotCold contexts, taking advantage of the fact
345 // that we later will only clone Cold contexts, as NotCold is the allocation
346 // default. We only need to keep as metadata the NotCold contexts that
347 // overlap the longest with Cold allocations, so that we know how deeply we
348 // need to clone. For example, assume we add the following contexts to the
349 // trie:
350 // 1 3 (notcold)
351 // 1 2 4 (cold)
352 // 1 2 5 (notcold)
353 // 1 2 6 (notcold)
354 // the trie looks like:
355 // 1
356 // / \
357 // 2 3
358 // /|\
359 // 4 5 6
360 //
361 // It is sufficient to prune all but one not-cold contexts (either 1,2,5 or
362 // 1,2,6, we arbitrarily keep the first one we encounter which will be
363 // 1,2,5).
364 //
365 // To do this pruning, we first check if there were any not-cold
366 // contexts kept for a deeper caller, which will have a context length larger
367 // than the CallerContextLength being handled here (i.e. kept by a deeper
368 // recursion step). If so, none of the not-cold MIB nodes added for the
369 // immediate callers need to be kept. If not, we keep the first (created
370 // for the immediate caller) not-cold MIB node.
371 bool LongerNotColdContextKept = false;
372 for (auto *MIB : NewMIBNodes) {
373 auto MIBMD = cast<MDNode>(MIB);
375 continue;
376 MDNode *StackMD = getMIBStackNode(MIBMD);
377 assert(StackMD);
378 if (StackMD->getNumOperands() > CallerContextLength) {
379 LongerNotColdContextKept = true;
380 break;
381 }
382 }
383 // Don't need to emit any for the immediate caller if we already have
384 // longer overlapping contexts;
385 bool KeepFirstNewNotCold = !LongerNotColdContextKept;
386 auto NewColdMIBNodes = make_filter_range(NewMIBNodes, [&](const Metadata *M) {
387 auto MIBMD = cast<MDNode>(M);
388 // Only keep cold contexts and first (longest non-cold context).
390 MDNode *StackMD = getMIBStackNode(MIBMD);
391 assert(StackMD);
392 // Keep any already kept for longer contexts.
393 if (StackMD->getNumOperands() > CallerContextLength)
394 return true;
395 // Otherwise keep the first one added by the immediate caller if there
396 // were no longer contexts.
397 if (KeepFirstNewNotCold) {
398 KeepFirstNewNotCold = false;
399 return true;
400 }
402 EmitMessageForRemovedContexts(MIBMD, "pruned", "");
403 return false;
404 }
405 return true;
406 });
407 for (auto *M : NewColdMIBNodes)
408 SavedMIBNodes.push_back(M);
409}
410
411// Recursive helper to trim contexts and create metadata nodes.
412// Caller should have pushed Node's loc to MIBCallStack. Doing this in the
413// caller makes it simpler to handle the many early returns in this method.
414// Updates the total and cold profiled bytes in the subtrie rooted at this node.
415bool CallStackTrie::buildMIBNodes(CallStackTrieNode *Node, LLVMContext &Ctx,
416 std::vector<uint64_t> &MIBCallStack,
417 std::vector<Metadata *> &MIBNodes,
418 bool CalleeHasAmbiguousCallerContext,
419 uint64_t &TotalBytes, uint64_t &ColdBytes) {
420 // Trim context below the first node in a prefix with a single alloc type.
421 // Add an MIB record for the current call stack prefix.
422 if (hasSingleAllocType(Node->AllocTypes)) {
423 std::vector<ContextTotalSize> ContextSizeInfo;
424 collectContextSizeInfo(Node, ContextSizeInfo);
425 MIBNodes.push_back(createMIBNode(
426 Ctx, MIBCallStack, (AllocationType)Node->AllocTypes, ContextSizeInfo,
427 MaxColdSize, BuiltFromExistingMetadata, TotalBytes, ColdBytes));
428 return true;
429 }
430
431 // We don't have a single allocation for all the contexts sharing this prefix,
432 // so recursively descend into callers in trie.
433 if (!Node->Callers.empty()) {
434 bool NodeHasAmbiguousCallerContext = Node->Callers.size() > 1;
435 bool AddedMIBNodesForAllCallerContexts = true;
436 // Accumulate all new MIB nodes by the recursive calls below into a vector
437 // that will later be filtered before adding to the caller's MIBNodes
438 // vector.
439 std::vector<Metadata *> NewMIBNodes;
440 // Determine the total and cold byte counts for all callers, then add to the
441 // caller's counts further below.
442 uint64_t CallerTotalBytes = 0;
443 uint64_t CallerColdBytes = 0;
444 for (auto &Caller : Node->Callers) {
445 MIBCallStack.push_back(Caller.first);
446 AddedMIBNodesForAllCallerContexts &= buildMIBNodes(
447 Caller.second, Ctx, MIBCallStack, NewMIBNodes,
448 NodeHasAmbiguousCallerContext, CallerTotalBytes, CallerColdBytes);
449 // Remove Caller.
450 MIBCallStack.pop_back();
451 }
452 // Pass in the stack length of the MIB nodes added for the immediate caller,
453 // which is the current stack length plus 1.
454 saveFilteredNewMIBNodes(NewMIBNodes, MIBNodes, MIBCallStack.size() + 1,
455 CallerTotalBytes, CallerColdBytes,
456 BuiltFromExistingMetadata);
457 TotalBytes += CallerTotalBytes;
458 ColdBytes += CallerColdBytes;
459
460 if (AddedMIBNodesForAllCallerContexts)
461 return true;
462 // We expect that the callers should be forced to add MIBs to disambiguate
463 // the context in this case (see below).
464 assert(!NodeHasAmbiguousCallerContext);
465 }
466
467 // If we reached here, then this node does not have a single allocation type,
468 // and we didn't add metadata for a longer call stack prefix including any of
469 // Node's callers. That means we never hit a single allocation type along all
470 // call stacks with this prefix. This can happen due to recursion collapsing
471 // or the stack being deeper than tracked by the profiler runtime, leading to
472 // contexts with different allocation types being merged. In that case, we
473 // trim the context just below the deepest context split, which is this
474 // node if the callee has an ambiguous caller context (multiple callers),
475 // since the recursive calls above returned false. Conservatively give it
476 // non-cold allocation type.
477 if (!CalleeHasAmbiguousCallerContext)
478 return false;
479 std::vector<ContextTotalSize> ContextSizeInfo;
480 collectContextSizeInfo(Node, ContextSizeInfo);
481 MIBNodes.push_back(createMIBNode(
482 Ctx, MIBCallStack, AllocationType::NotCold, ContextSizeInfo, MaxColdSize,
483 BuiltFromExistingMetadata, TotalBytes, ColdBytes));
484 return true;
485}
486
488 StringRef Descriptor) {
489 auto AllocTypeString = getAllocTypeAttributeString(AT);
490 auto A = llvm::Attribute::get(CI->getContext(), "memprof", AllocTypeString);
491 // After inlining we may be able to convert an existing ambiguous allocation
492 // to an unambiguous one.
494 CI->addFnAttr(A);
496 std::vector<ContextTotalSize> ContextSizeInfo;
497 collectContextSizeInfo(Alloc, ContextSizeInfo);
498 for (const auto &[FullStackId, TotalSize] : ContextSizeInfo) {
499 errs() << "MemProf hinting: Total size for full allocation context hash "
500 << FullStackId << " and " << Descriptor << " alloc type "
501 << getAllocTypeAttributeString(AT) << ": " << TotalSize << "\n";
502 }
503 }
504 if (ORE)
505 ORE->emit(OptimizationRemark(DEBUG_TYPE, "MemprofAttribute", CI)
506 << ore::NV("AllocationCall", CI) << " in function "
507 << ore::NV("Caller", CI->getFunction())
508 << " marked with memprof allocation attribute "
509 << ore::NV("Attribute", AllocTypeString));
510}
511
512// Build and attach the minimal necessary MIB metadata. If the alloc has a
513// single allocation type, add a function attribute instead. Returns true if
514// memprof metadata attached, false if not (attribute added).
516 if (hasSingleAllocType(Alloc->AllocTypes)) {
517 addSingleAllocTypeAttribute(CI, (AllocationType)Alloc->AllocTypes,
518 "single");
519 return false;
520 }
521 // If there were any hot allocation contexts, the Alloc trie node would have
522 // the Hot type set. If so, because we don't currently support cloning for hot
523 // contexts, they should be converted to NotCold. This happens in the cloning
524 // support anyway, however, doing this now enables more aggressive context
525 // trimming when building the MIB metadata (and possibly may make the
526 // allocation have a single NotCold allocation type), greatly reducing
527 // overheads in bitcode, cloning memory and cloning time.
528 if (Alloc->hasAllocType(AllocationType::Hot)) {
529 convertHotToNotCold(Alloc);
530 // Check whether we now have a single alloc type.
531 if (hasSingleAllocType(Alloc->AllocTypes)) {
532 addSingleAllocTypeAttribute(CI, (AllocationType)Alloc->AllocTypes,
533 "single");
534 return false;
535 }
536 }
537 auto &Ctx = CI->getContext();
538 std::vector<uint64_t> MIBCallStack;
539 MIBCallStack.push_back(AllocStackId);
540 std::vector<Metadata *> MIBNodes;
541 uint64_t TotalBytes = 0;
542 uint64_t ColdBytes = 0;
543 assert(!Alloc->Callers.empty() && "addCallStack has not been called yet");
544 // The CalleeHasAmbiguousCallerContext flag is meant to say whether the
545 // callee of the given node has more than one caller. Here the node being
546 // passed in is the alloc and it has no callees. So it's false.
547 if (buildMIBNodes(Alloc, Ctx, MIBCallStack, MIBNodes,
548 /*CalleeHasAmbiguousCallerContext=*/false, TotalBytes,
549 ColdBytes)) {
550 assert(MIBCallStack.size() == 1 &&
551 "Should only be left with Alloc's location in stack");
552 CI->setMetadata(LLVMContext::MD_memprof, MDNode::get(Ctx, MIBNodes));
554 return true;
555 }
556 // If there exists corner case that CallStackTrie has one chain to leaf
557 // and all node in the chain have multi alloc type, conservatively give
558 // it non-cold allocation type.
559 // FIXME: Avoid this case before memory profile created. Alternatively, select
560 // hint based on fraction cold.
562 return false;
563}
564
565template <>
567 const MDNode *N, bool End)
568 : N(N) {
569 if (!N)
570 return;
571 Iter = End ? N->op_end() : N->op_begin();
572}
573
574template <>
577 assert(Iter != N->op_end());
579 assert(StackIdCInt);
580 return StackIdCInt->getZExtValue();
581}
582
584 assert(N);
585 return mdconst::dyn_extract<ConstantInt>(N->operands().back())
586 ->getZExtValue();
587}
588
590 // TODO: Support more sophisticated merging, such as selecting the one with
591 // more bytes allocated, or implement support for carrying multiple allocation
592 // leaf contexts. For now, keep the first one.
593 if (A)
594 return A;
595 return B;
596}
597
599 // TODO: Support more sophisticated merging, which will require support for
600 // carrying multiple contexts. For now, keep the first one.
601 if (A)
602 return A;
603 return B;
604}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
#define LLVM_ABI
Definition Compiler.h:213
This file contains the declarations for the subclasses of Constant, which represent the different fla...
#define DEBUG_TYPE
#define I(x, y, z)
Definition MD5.cpp:58
AllocType
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)
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:41
bool empty() const
empty - Check if the array is empty.
Definition ArrayRef.h:142
static LLVM_ABI Attribute get(LLVMContext &Context, AttrKind Kind, uint64_t Val=0)
Return a uniquified Attribute object.
LLVM_ABI StringRef getValueAsString() const
Return the attribute's value as a string.
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.
bool hasFnAttr(Attribute::AttrKind Kind) const
Determine whether this call has the given attribute.
Attribute getFnAttr(StringRef Kind) const
Get the attribute of a given kind for the function.
void removeFnAttr(Attribute::AttrKind Kind)
Removes the attribute from the function.
This is the shared class of boolean and integer constants.
Definition Constants.h:87
uint64_t getZExtValue() const
Return the constant as a 64-bit unsigned integer value after it has been zero extended as appropriate...
Definition Constants.h:163
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.
Definition LLVMContext.h:68
Metadata node.
Definition Metadata.h:1078
static LLVM_ABI MDNode * getMergedCallsiteMetadata(MDNode *A, MDNode *B)
const MDOperand & getOperand(unsigned I) const
Definition Metadata.h:1442
ArrayRef< MDOperand > operands() const
Definition Metadata.h:1440
static MDTuple * get(LLVMContext &Context, ArrayRef< Metadata * > MDs)
Definition Metadata.h:1569
unsigned getNumOperands() const
Return number of MDNode operands.
Definition Metadata.h:1448
LLVM_ABI MDNode(LLVMContext &Context, unsigned ID, StorageType Storage, ArrayRef< Metadata * > Ops1, ArrayRef< Metadata * > Ops2={})
Definition Metadata.cpp:652
static LLVM_ABI MDNode * getMergedMemProfMetadata(MDNode *A, MDNode *B)
static LLVM_ABI MDString * get(LLVMContext &Context, StringRef Str)
Definition Metadata.cpp:608
void push_back(Metadata *MD)
Append an element to the tuple. This will resize the node.
Definition Metadata.h:1555
Root of the metadata hierarchy.
Definition Metadata.h:64
Diagnostic information for applied optimization remarks.
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.
Definition StringRef.h:55
The instances of the Type class are immutable: once they are created, they are never changed.
Definition Type.h:45
static LLVM_ABI IntegerType * getInt64Ty(LLVMContext &C)
Definition Type.cpp:298
static LLVM_ABI ValueAsMetadata * get(Value *V)
Definition Metadata.cpp:503
LLVM_ABI LLVMContext & getContext() const
All values hold a context through their type.
Definition Value.cpp:1099
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.
std::string & str()
Returns the string's reference.
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
initializer< Ty > init(const Ty &Val)
std::enable_if_t< detail::IsValidPointer< X, Y >::value, X * > dyn_extract(Y &&MD)
Extract a Value from Metadata, if any.
Definition Metadata.h:695
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.
LLVM_ABI void removeAnyExistingAmbiguousAttribute(CallBase *CB)
Removes any existing "ambiguous" memprof attribute.
LLVM_ABI void addAmbiguousAttribute(CallBase *CB)
Adds an "ambiguous" memprof attribute to call with a matched allocation profile but that we haven't y...
DiagnosticInfoOptimizationBase::Argument NV
NodeAddr< NodeBase * > Node
Definition RDFGraph.h:381
This is an optimization pass for GlobalISel generic memory operations.
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"))
cl::opt< bool > MemProfReportHintedSizes("memprof-report-hinted-sizes", cl::init(false), cl::Hidden, cl::desc("Report total allocation sizes of hinted allocations"))
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:644
void append_range(Container &C, Range &&R)
Wrapper function to append range R to container C.
Definition STLExtras.h:2116
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 > 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"))
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...
Definition STLExtras.h:552
format_object< Ts... > format(const char *Fmt, const Ts &... Vals)
These are helper functions used to produce formatted output.
Definition Format.h:118
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.
Definition ModRef.h:71
FunctionAddr VTableAddr Next
Definition InstrProf.h:141
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:560
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"))
int popcount(T Value) noexcept
Count the number of set bits in a value.
Definition bit.h:154
#define N