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
58 "memprof-ambiguous-attributes", cl::init(true), cl::Hidden,
59 cl::desc("Apply ambiguous memprof attribute to ambiguous allocations"));
60
61} // end namespace llvm
62
66
70
75
77 LLVMContext &Ctx) {
79 StackVals.reserve(CallStack.size());
80 for (auto Id : CallStack) {
81 auto *StackValMD =
82 ValueAsMetadata::get(ConstantInt::get(Type::getInt64Ty(Ctx), Id));
83 StackVals.push_back(StackValMD);
84 }
85 return MDNode::get(Ctx, StackVals);
86}
87
89 assert(MIB->getNumOperands() >= 2);
90 // The stack metadata is the first operand of each memprof MIB metadata.
91 return cast<MDNode>(MIB->getOperand(0));
92}
93
95 assert(MIB->getNumOperands() >= 2);
96 // The allocation type is currently the second operand of each memprof
97 // MIB metadata. This will need to change as we add additional allocation
98 // types that can be applied based on the allocation profile data.
99 auto *MDS = dyn_cast<MDString>(MIB->getOperand(1));
100 assert(MDS);
101 if (MDS->getString() == "cold") {
103 } else if (MDS->getString() == "hot") {
104 return AllocationType::Hot;
105 }
107}
108
110 switch (Type) {
112 return "notcold";
113 break;
115 return "cold";
116 break;
118 return "hot";
119 break;
120 default:
121 assert(false && "Unexpected alloc type");
122 }
123 llvm_unreachable("invalid alloc type");
124}
125
127 const unsigned NumAllocTypes = llvm::popcount(AllocTypes);
128 assert(NumAllocTypes != 0);
129 return NumAllocTypes == 1;
130}
131
133 if (!CB->hasFnAttr("memprof"))
134 return;
135 assert(CB->getFnAttr("memprof").getValueAsString() == "ambiguous");
136 CB->removeFnAttr("memprof");
137}
138
141 return;
142 // We may have an existing ambiguous attribute if we are reanalyzing
143 // after inlining.
144 if (CB->hasFnAttr("memprof")) {
145 assert(CB->getFnAttr("memprof").getValueAsString() == "ambiguous");
146 } else {
147 auto A = llvm::Attribute::get(CB->getContext(), "memprof", "ambiguous");
148 CB->addFnAttr(A);
149 }
150}
151
153 AllocationType AllocType, ArrayRef<uint64_t> StackIds,
154 std::vector<ContextTotalSize> ContextSizeInfo) {
155 bool First = true;
156 CallStackTrieNode *Curr = nullptr;
157 for (auto StackId : StackIds) {
158 // If this is the first stack frame, add or update alloc node.
159 if (First) {
160 First = false;
161 if (Alloc) {
162 assert(AllocStackId == StackId);
163 Alloc->addAllocType(AllocType);
164 } else {
165 AllocStackId = StackId;
166 Alloc = new CallStackTrieNode(AllocType);
167 }
168 Curr = Alloc;
169 continue;
170 }
171 // Update existing caller node if it exists.
172 auto [Next, Inserted] = Curr->Callers.try_emplace(StackId);
173 if (!Inserted) {
174 Curr = Next->second;
175 Curr->addAllocType(AllocType);
176 continue;
177 }
178 // Otherwise add a new caller node.
179 auto *New = new CallStackTrieNode(AllocType);
180 Next->second = New;
181 Curr = New;
182 }
183 assert(Curr);
184 llvm::append_range(Curr->ContextSizeInfo, ContextSizeInfo);
185}
186
188 // Note that we are building this from existing MD_memprof metadata.
189 BuiltFromExistingMetadata = true;
190 MDNode *StackMD = getMIBStackNode(MIB);
191 assert(StackMD);
192 std::vector<uint64_t> CallStack;
193 CallStack.reserve(StackMD->getNumOperands());
194 for (const auto &MIBStackIter : StackMD->operands()) {
195 auto *StackId = mdconst::dyn_extract<ConstantInt>(MIBStackIter);
196 assert(StackId);
197 CallStack.push_back(StackId->getZExtValue());
198 }
199 std::vector<ContextTotalSize> ContextSizeInfo;
200 // Collect the context size information if it exists.
201 if (MIB->getNumOperands() > 2) {
202 for (unsigned I = 2; I < MIB->getNumOperands(); I++) {
203 MDNode *ContextSizePair = dyn_cast<MDNode>(MIB->getOperand(I));
204 assert(ContextSizePair->getNumOperands() == 2);
205 uint64_t FullStackId =
207 ->getZExtValue();
208 uint64_t TotalSize =
210 ->getZExtValue();
211 ContextSizeInfo.push_back({FullStackId, TotalSize});
212 }
213 }
214 addCallStack(getMIBAllocType(MIB), CallStack, std::move(ContextSizeInfo));
215}
216
219 ArrayRef<ContextTotalSize> ContextSizeInfo,
220 const uint64_t MaxColdSize,
221 bool BuiltFromExistingMetadata,
222 uint64_t &TotalBytes, uint64_t &ColdBytes) {
223 SmallVector<Metadata *> MIBPayload(
224 {buildCallstackMetadata(MIBCallStack, Ctx)});
225 MIBPayload.push_back(
227
228 if (ContextSizeInfo.empty()) {
229 // The profile matcher should have provided context size info if there was a
230 // MinCallsiteColdBytePercent < 100. Here we check >=100 to gracefully
231 // handle a user-provided percent larger than 100. However, we may not have
232 // this information if we built the Trie from existing MD_memprof metadata.
233 assert(BuiltFromExistingMetadata || MinCallsiteColdBytePercent >= 100);
234 return MDNode::get(Ctx, MIBPayload);
235 }
236
237 for (const auto &[FullStackId, TotalSize] : ContextSizeInfo) {
238 TotalBytes += TotalSize;
239 bool LargeColdContext = false;
241 ColdBytes += TotalSize;
242 // If we have the max cold context size from summary information and have
243 // requested identification of contexts above a percentage of the max, see
244 // if this context qualifies.
245 if (MaxColdSize > 0 && MinPercentMaxColdSize < 100 &&
246 TotalSize * 100 >= MaxColdSize * MinPercentMaxColdSize)
247 LargeColdContext = true;
248 }
249 // Only add the context size info as metadata if we need it in the thin
250 // link (currently if reporting of hinted sizes is enabled, we have
251 // specified a threshold for marking allocations cold after cloning, or we
252 // have identified this as a large cold context of interest above).
253 if (metadataIncludesAllContextSizeInfo() || LargeColdContext) {
254 auto *FullStackIdMD = ValueAsMetadata::get(
255 ConstantInt::get(Type::getInt64Ty(Ctx), FullStackId));
256 auto *TotalSizeMD = ValueAsMetadata::get(
257 ConstantInt::get(Type::getInt64Ty(Ctx), TotalSize));
258 auto *ContextSizeMD = MDNode::get(Ctx, {FullStackIdMD, TotalSizeMD});
259 MIBPayload.push_back(ContextSizeMD);
260 }
261 }
262 assert(TotalBytes > 0);
263 return MDNode::get(Ctx, MIBPayload);
264}
265
266void CallStackTrie::collectContextSizeInfo(
267 CallStackTrieNode *Node, std::vector<ContextTotalSize> &ContextSizeInfo) {
268 llvm::append_range(ContextSizeInfo, Node->ContextSizeInfo);
269 for (auto &Caller : Node->Callers)
270 collectContextSizeInfo(Caller.second, ContextSizeInfo);
271}
272
273void CallStackTrie::convertHotToNotCold(CallStackTrieNode *Node) {
274 if (Node->hasAllocType(AllocationType::Hot)) {
275 Node->removeAllocType(AllocationType::Hot);
276 Node->addAllocType(AllocationType::NotCold);
277 }
278 for (auto &Caller : Node->Callers)
279 convertHotToNotCold(Caller.second);
280}
281
282// Copy over some or all of NewMIBNodes to the SavedMIBNodes vector, depending
283// on options that enable filtering out some NotCold contexts.
284static void saveFilteredNewMIBNodes(std::vector<Metadata *> &NewMIBNodes,
285 std::vector<Metadata *> &SavedMIBNodes,
286 unsigned CallerContextLength,
287 uint64_t TotalBytes, uint64_t ColdBytes,
288 bool BuiltFromExistingMetadata) {
289 const bool MostlyCold =
290 // If we have built the Trie from existing MD_memprof metadata, we may or
291 // may not have context size information (in which case ColdBytes and
292 // TotalBytes are 0, which is not also guarded against below). Even if we
293 // do have some context size information from the the metadata, we have
294 // already gone through a round of discarding of small non-cold contexts
295 // during matching, and it would be overly aggressive to do it again, and
296 // we also want to maintain the same behavior with and without reporting
297 // of hinted bytes enabled.
298 !BuiltFromExistingMetadata && MinCallsiteColdBytePercent < 100 &&
299 ColdBytes > 0 &&
300 ColdBytes * 100 >= MinCallsiteColdBytePercent * TotalBytes;
301
302 // In the simplest case, with pruning disabled, keep all the new MIB nodes.
303 if (MemProfKeepAllNotColdContexts && !MostlyCold) {
304 append_range(SavedMIBNodes, NewMIBNodes);
305 return;
306 }
307
308 auto EmitMessageForRemovedContexts = [](const MDNode *MIBMD, StringRef Tag,
309 StringRef Extra) {
310 assert(MIBMD->getNumOperands() > 2);
311 for (unsigned I = 2; I < MIBMD->getNumOperands(); I++) {
312 MDNode *ContextSizePair = dyn_cast<MDNode>(MIBMD->getOperand(I));
313 assert(ContextSizePair->getNumOperands() == 2);
314 uint64_t FullStackId =
316 ->getZExtValue();
317 uint64_t TS =
319 ->getZExtValue();
320 errs() << "MemProf hinting: Total size for " << Tag
321 << " non-cold full allocation context hash " << FullStackId
322 << Extra << ": " << TS << "\n";
323 }
324 };
325
326 // If the cold bytes at the current callsite exceed the given threshold, we
327 // discard all non-cold contexts so do not need any of the later pruning
328 // handling. We can simply copy over all the cold contexts and return early.
329 if (MostlyCold) {
330 auto NewColdMIBNodes =
331 make_filter_range(NewMIBNodes, [&](const Metadata *M) {
332 auto MIBMD = cast<MDNode>(M);
333 // Only append cold contexts.
335 return true;
337 const float PercentCold = ColdBytes * 100.0 / TotalBytes;
338 std::string PercentStr;
339 llvm::raw_string_ostream OS(PercentStr);
340 OS << format(" for %5.2f%% cold bytes", PercentCold);
341 EmitMessageForRemovedContexts(MIBMD, "discarded", OS.str());
342 }
343 return false;
344 });
345 for (auto *M : NewColdMIBNodes)
346 SavedMIBNodes.push_back(M);
347 return;
348 }
349
350 // Prune unneeded NotCold contexts, taking advantage of the fact
351 // that we later will only clone Cold contexts, as NotCold is the allocation
352 // default. We only need to keep as metadata the NotCold contexts that
353 // overlap the longest with Cold allocations, so that we know how deeply we
354 // need to clone. For example, assume we add the following contexts to the
355 // trie:
356 // 1 3 (notcold)
357 // 1 2 4 (cold)
358 // 1 2 5 (notcold)
359 // 1 2 6 (notcold)
360 // the trie looks like:
361 // 1
362 // / \
363 // 2 3
364 // /|\
365 // 4 5 6
366 //
367 // It is sufficient to prune all but one not-cold contexts (either 1,2,5 or
368 // 1,2,6, we arbitrarily keep the first one we encounter which will be
369 // 1,2,5).
370 //
371 // To do this pruning, we first check if there were any not-cold
372 // contexts kept for a deeper caller, which will have a context length larger
373 // than the CallerContextLength being handled here (i.e. kept by a deeper
374 // recursion step). If so, none of the not-cold MIB nodes added for the
375 // immediate callers need to be kept. If not, we keep the first (created
376 // for the immediate caller) not-cold MIB node.
377 bool LongerNotColdContextKept = false;
378 for (auto *MIB : NewMIBNodes) {
379 auto MIBMD = cast<MDNode>(MIB);
381 continue;
382 MDNode *StackMD = getMIBStackNode(MIBMD);
383 assert(StackMD);
384 if (StackMD->getNumOperands() > CallerContextLength) {
385 LongerNotColdContextKept = true;
386 break;
387 }
388 }
389 // Don't need to emit any for the immediate caller if we already have
390 // longer overlapping contexts;
391 bool KeepFirstNewNotCold = !LongerNotColdContextKept;
392 auto NewColdMIBNodes = make_filter_range(NewMIBNodes, [&](const Metadata *M) {
393 auto MIBMD = cast<MDNode>(M);
394 // Only keep cold contexts and first (longest non-cold context).
396 MDNode *StackMD = getMIBStackNode(MIBMD);
397 assert(StackMD);
398 // Keep any already kept for longer contexts.
399 if (StackMD->getNumOperands() > CallerContextLength)
400 return true;
401 // Otherwise keep the first one added by the immediate caller if there
402 // were no longer contexts.
403 if (KeepFirstNewNotCold) {
404 KeepFirstNewNotCold = false;
405 return true;
406 }
408 EmitMessageForRemovedContexts(MIBMD, "pruned", "");
409 return false;
410 }
411 return true;
412 });
413 for (auto *M : NewColdMIBNodes)
414 SavedMIBNodes.push_back(M);
415}
416
417// Recursive helper to trim contexts and create metadata nodes.
418// Caller should have pushed Node's loc to MIBCallStack. Doing this in the
419// caller makes it simpler to handle the many early returns in this method.
420// Updates the total and cold profiled bytes in the subtrie rooted at this node.
421bool CallStackTrie::buildMIBNodes(CallStackTrieNode *Node, LLVMContext &Ctx,
422 std::vector<uint64_t> &MIBCallStack,
423 std::vector<Metadata *> &MIBNodes,
424 bool CalleeHasAmbiguousCallerContext,
425 uint64_t &TotalBytes, uint64_t &ColdBytes) {
426 // Trim context below the first node in a prefix with a single alloc type.
427 // Add an MIB record for the current call stack prefix.
428 if (hasSingleAllocType(Node->AllocTypes)) {
429 std::vector<ContextTotalSize> ContextSizeInfo;
430 collectContextSizeInfo(Node, ContextSizeInfo);
431 MIBNodes.push_back(createMIBNode(
432 Ctx, MIBCallStack, (AllocationType)Node->AllocTypes, ContextSizeInfo,
433 MaxColdSize, BuiltFromExistingMetadata, TotalBytes, ColdBytes));
434 return true;
435 }
436
437 // We don't have a single allocation for all the contexts sharing this prefix,
438 // so recursively descend into callers in trie.
439 if (!Node->Callers.empty()) {
440 bool NodeHasAmbiguousCallerContext = Node->Callers.size() > 1;
441 bool AddedMIBNodesForAllCallerContexts = true;
442 // Accumulate all new MIB nodes by the recursive calls below into a vector
443 // that will later be filtered before adding to the caller's MIBNodes
444 // vector.
445 std::vector<Metadata *> NewMIBNodes;
446 // Determine the total and cold byte counts for all callers, then add to the
447 // caller's counts further below.
448 uint64_t CallerTotalBytes = 0;
449 uint64_t CallerColdBytes = 0;
450 for (auto &Caller : Node->Callers) {
451 MIBCallStack.push_back(Caller.first);
452 AddedMIBNodesForAllCallerContexts &= buildMIBNodes(
453 Caller.second, Ctx, MIBCallStack, NewMIBNodes,
454 NodeHasAmbiguousCallerContext, CallerTotalBytes, CallerColdBytes);
455 // Remove Caller.
456 MIBCallStack.pop_back();
457 }
458 // Pass in the stack length of the MIB nodes added for the immediate caller,
459 // which is the current stack length plus 1.
460 saveFilteredNewMIBNodes(NewMIBNodes, MIBNodes, MIBCallStack.size() + 1,
461 CallerTotalBytes, CallerColdBytes,
462 BuiltFromExistingMetadata);
463 TotalBytes += CallerTotalBytes;
464 ColdBytes += CallerColdBytes;
465
466 if (AddedMIBNodesForAllCallerContexts)
467 return true;
468 // We expect that the callers should be forced to add MIBs to disambiguate
469 // the context in this case (see below).
470 assert(!NodeHasAmbiguousCallerContext);
471 }
472
473 // If we reached here, then this node does not have a single allocation type,
474 // and we didn't add metadata for a longer call stack prefix including any of
475 // Node's callers. That means we never hit a single allocation type along all
476 // call stacks with this prefix. This can happen due to recursion collapsing
477 // or the stack being deeper than tracked by the profiler runtime, leading to
478 // contexts with different allocation types being merged. In that case, we
479 // trim the context just below the deepest context split, which is this
480 // node if the callee has an ambiguous caller context (multiple callers),
481 // since the recursive calls above returned false. Conservatively give it
482 // non-cold allocation type.
483 if (!CalleeHasAmbiguousCallerContext)
484 return false;
485 std::vector<ContextTotalSize> ContextSizeInfo;
486 collectContextSizeInfo(Node, ContextSizeInfo);
487 MIBNodes.push_back(createMIBNode(
488 Ctx, MIBCallStack, AllocationType::NotCold, ContextSizeInfo, MaxColdSize,
489 BuiltFromExistingMetadata, TotalBytes, ColdBytes));
490 return true;
491}
492
494 StringRef Descriptor) {
495 auto AllocTypeString = getAllocTypeAttributeString(AT);
496 auto A = llvm::Attribute::get(CI->getContext(), "memprof", AllocTypeString);
497 // After inlining we may be able to convert an existing ambiguous allocation
498 // to an unambiguous one.
500 CI->addFnAttr(A);
502 std::vector<ContextTotalSize> ContextSizeInfo;
503 collectContextSizeInfo(Alloc, ContextSizeInfo);
504 for (const auto &[FullStackId, TotalSize] : ContextSizeInfo) {
505 errs() << "MemProf hinting: Total size for full allocation context hash "
506 << FullStackId << " and " << Descriptor << " alloc type "
507 << getAllocTypeAttributeString(AT) << ": " << TotalSize << "\n";
508 }
509 }
510 if (ORE)
511 ORE->emit(OptimizationRemark(DEBUG_TYPE, "MemprofAttribute", CI)
512 << ore::NV("AllocationCall", CI) << " in function "
513 << ore::NV("Caller", CI->getFunction())
514 << " marked with memprof allocation attribute "
515 << ore::NV("Attribute", AllocTypeString));
516}
517
518// Build and attach the minimal necessary MIB metadata. If the alloc has a
519// single allocation type, add a function attribute instead. Returns true if
520// memprof metadata attached, false if not (attribute added).
522 if (hasSingleAllocType(Alloc->AllocTypes)) {
523 addSingleAllocTypeAttribute(CI, (AllocationType)Alloc->AllocTypes,
524 "single");
525 return false;
526 }
527 // If there were any hot allocation contexts, the Alloc trie node would have
528 // the Hot type set. If so, because we don't currently support cloning for hot
529 // contexts, they should be converted to NotCold. This happens in the cloning
530 // support anyway, however, doing this now enables more aggressive context
531 // trimming when building the MIB metadata (and possibly may make the
532 // allocation have a single NotCold allocation type), greatly reducing
533 // overheads in bitcode, cloning memory and cloning time.
534 if (Alloc->hasAllocType(AllocationType::Hot)) {
535 convertHotToNotCold(Alloc);
536 // Check whether we now have a single alloc type.
537 if (hasSingleAllocType(Alloc->AllocTypes)) {
538 addSingleAllocTypeAttribute(CI, (AllocationType)Alloc->AllocTypes,
539 "single");
540 return false;
541 }
542 }
543 auto &Ctx = CI->getContext();
544 std::vector<uint64_t> MIBCallStack;
545 MIBCallStack.push_back(AllocStackId);
546 std::vector<Metadata *> MIBNodes;
547 uint64_t TotalBytes = 0;
548 uint64_t ColdBytes = 0;
549 assert(!Alloc->Callers.empty() && "addCallStack has not been called yet");
550 // The CalleeHasAmbiguousCallerContext flag is meant to say whether the
551 // callee of the given node has more than one caller. Here the node being
552 // passed in is the alloc and it has no callees. So it's false.
553 if (buildMIBNodes(Alloc, Ctx, MIBCallStack, MIBNodes,
554 /*CalleeHasAmbiguousCallerContext=*/false, TotalBytes,
555 ColdBytes)) {
556 assert(MIBCallStack.size() == 1 &&
557 "Should only be left with Alloc's location in stack");
558 CI->setMetadata(LLVMContext::MD_memprof, MDNode::get(Ctx, MIBNodes));
560 return true;
561 }
562 // If there exists corner case that CallStackTrie has one chain to leaf
563 // and all node in the chain have multi alloc type, conservatively give
564 // it non-cold allocation type.
565 // FIXME: Avoid this case before memory profile created. Alternatively, select
566 // hint based on fraction cold.
568 return false;
569}
570
571template <>
573 const MDNode *N, bool End)
574 : N(N) {
575 if (!N)
576 return;
577 Iter = End ? N->op_end() : N->op_begin();
578}
579
580template <>
583 assert(Iter != N->op_end());
585 assert(StackIdCInt);
586 return StackIdCInt->getZExtValue();
587}
588
590 assert(N);
591 return mdconst::dyn_extract<ConstantInt>(N->operands().back())
592 ->getZExtValue();
593}
594
596 // TODO: Support more sophisticated merging, such as selecting the one with
597 // more bytes allocated, or implement support for carrying multiple allocation
598 // leaf contexts. For now, keep the first one.
599 if (A)
600 return A;
601 return B;
602}
603
605 // TODO: Support more sophisticated merging, which will require support for
606 // carrying multiple contexts. For now, keep the first one.
607 if (A)
608 return A;
609 return B;
610}
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)"))
LLVM_ABI cl::opt< bool > MemProfUseAmbiguousAttributes("memprof-ambiguous-attributes", cl::init(true), cl::Hidden, cl::desc("Apply ambiguous memprof attribute to ambiguous 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"))
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