LLVM 20.0.0git
MemoryProfileInfo.h
Go to the documentation of this file.
1//===- llvm/Analysis/MemoryProfileInfo.h - memory profile info ---*- C++ -*-==//
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
13#ifndef LLVM_ANALYSIS_MEMORYPROFILEINFO_H
14#define LLVM_ANALYSIS_MEMORYPROFILEINFO_H
15
16#include "llvm/IR/Metadata.h"
18#include <map>
19
20namespace llvm {
21namespace memprof {
22
23/// Return the allocation type for a given set of memory profile values.
24AllocationType getAllocType(uint64_t TotalLifetimeAccessDensity,
25 uint64_t AllocCount, uint64_t TotalLifetime);
26
27/// Build callstack metadata from the provided list of call stack ids. Returns
28/// the resulting metadata node.
30
31/// Build metadata from the provided list of full stack id and profiled size, to
32/// use when reporting of hinted sizes is enabled.
34 LLVMContext &Ctx);
35
36/// Returns the stack node from an MIB metadata node.
37MDNode *getMIBStackNode(const MDNode *MIB);
38
39/// Returns the allocation type from an MIB metadata node.
41
42/// Returns the string to use in attributes with the given type.
44
45/// True if the AllocTypes bitmask contains just a single type.
46bool hasSingleAllocType(uint8_t AllocTypes);
47
48/// Class to build a trie of call stack contexts for a particular profiled
49/// allocation call, along with their associated allocation types.
50/// The allocation will be at the root of the trie, which is then used to
51/// compute the minimum lists of context ids needed to associate a call context
52/// with a single allocation type.
54private:
55 struct CallStackTrieNode {
56 // Allocation types for call context sharing the context prefix at this
57 // node.
58 uint8_t AllocTypes;
59 // If the user has requested reporting of hinted sizes, keep track of the
60 // associated full stack id and profiled sizes. Can have more than one
61 // after trimming (e.g. when building from metadata). This is only placed on
62 // the last (root-most) trie node for each allocation context.
63 std::vector<ContextTotalSize> ContextSizeInfo;
64 // Map of caller stack id to the corresponding child Trie node.
65 std::map<uint64_t, CallStackTrieNode *> Callers;
66 CallStackTrieNode(AllocationType Type)
67 : AllocTypes(static_cast<uint8_t>(Type)) {}
68 };
69
70 // The node for the allocation at the root.
71 CallStackTrieNode *Alloc = nullptr;
72 // The allocation's leaf stack id.
73 uint64_t AllocStackId = 0;
74
75 void deleteTrieNode(CallStackTrieNode *Node) {
76 if (!Node)
77 return;
78 for (auto C : Node->Callers)
79 deleteTrieNode(C.second);
80 delete Node;
81 }
82
83 // Recursively build up a complete list of context size information from the
84 // trie nodes reached form the given Node, for hint size reporting.
85 void collectContextSizeInfo(CallStackTrieNode *Node,
86 std::vector<ContextTotalSize> &ContextSizeInfo);
87
88 // Recursive helper to trim contexts and create metadata nodes.
89 bool buildMIBNodes(CallStackTrieNode *Node, LLVMContext &Ctx,
90 std::vector<uint64_t> &MIBCallStack,
91 std::vector<Metadata *> &MIBNodes,
92 bool CalleeHasAmbiguousCallerContext);
93
94public:
95 CallStackTrie() = default;
96 ~CallStackTrie() { deleteTrieNode(Alloc); }
97
98 bool empty() const { return Alloc == nullptr; }
99
100 /// Add a call stack context with the given allocation type to the Trie.
101 /// The context is represented by the list of stack ids (computed during
102 /// matching via a debug location hash), expected to be in order from the
103 /// allocation call down to the bottom of the call stack (i.e. callee to
104 /// caller order).
106 std::vector<ContextTotalSize> ContextSizeInfo = {});
107
108 /// Add the call stack context along with its allocation type from the MIB
109 /// metadata to the Trie.
110 void addCallStack(MDNode *MIB);
111
112 /// Build and attach the minimal necessary MIB metadata. If the alloc has a
113 /// single allocation type, add a function attribute instead. The reason for
114 /// adding an attribute in this case is that it matches how the behavior for
115 /// allocation calls will be communicated to lib call simplification after
116 /// cloning or another optimization to distinguish the allocation types,
117 /// which is lower overhead and more direct than maintaining this metadata.
118 /// Returns true if memprof metadata attached, false if not (attribute added).
120
121 /// Add an attribute for the given allocation type to the call instruction.
122 /// If hinted by reporting is enabled, a message is emitted with the given
123 /// descriptor used to identify the category of single allocation type.
125 StringRef Descriptor);
126};
127
128/// Helper class to iterate through stack ids in both metadata (memprof MIB and
129/// callsite) and the corresponding ThinLTO summary data structures
130/// (CallsiteInfo and MIBInfo). This simplifies implementation of client code
131/// which doesn't need to worry about whether we are operating with IR (Regular
132/// LTO), or summary (ThinLTO).
133template <class NodeT, class IteratorT> class CallStack {
134public:
135 CallStack(const NodeT *N = nullptr) : N(N) {}
136
137 // Implement minimum required methods for range-based for loop.
138 // The default implementation assumes we are operating on ThinLTO data
139 // structures, which have a vector of StackIdIndices. There are specialized
140 // versions provided to iterate through metadata.
142 const NodeT *N = nullptr;
143 IteratorT Iter;
144 CallStackIterator(const NodeT *N, bool End);
146 bool operator==(const CallStackIterator &rhs) { return Iter == rhs.Iter; }
147 bool operator!=(const CallStackIterator &rhs) { return !(*this == rhs); }
148 void operator++() { ++Iter; }
149 };
150
151 bool empty() const { return N == nullptr; }
152
153 CallStackIterator begin() const;
154 CallStackIterator end() const { return CallStackIterator(N, /*End*/ true); }
155 CallStackIterator beginAfterSharedPrefix(CallStack &Other);
156 uint64_t back() const;
157
158private:
159 const NodeT *N = nullptr;
160};
161
162template <class NodeT, class IteratorT>
164 const NodeT *N, bool End)
165 : N(N) {
166 if (!N) {
167 Iter = nullptr;
168 return;
169 }
170 Iter = End ? N->StackIdIndices.end() : N->StackIdIndices.begin();
171}
172
173template <class NodeT, class IteratorT>
175 assert(Iter != N->StackIdIndices.end());
176 return *Iter;
177}
178
179template <class NodeT, class IteratorT>
181 assert(N);
182 return N->StackIdIndices.back();
183}
184
185template <class NodeT, class IteratorT>
188 return CallStackIterator(N, /*End*/ false);
189}
190
191template <class NodeT, class IteratorT>
194 CallStackIterator Cur = begin();
195 for (CallStackIterator OtherCur = Other.begin();
196 Cur != end() && OtherCur != Other.end(); ++Cur, ++OtherCur)
197 assert(*Cur == *OtherCur);
198 return Cur;
199}
200
201/// Specializations for iterating through IR metadata stack contexts.
202template <>
204 const MDNode *N, bool End);
205template <>
208
209} // end namespace memprof
210} // end namespace llvm
211
212#endif
bool End
Definition: ELF_riscv.cpp:480
AllocType
This file contains the declarations for metadata subclasses.
ModuleSummaryIndex.h This file contains the declarations the classes that hold the module index and s...
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition: ArrayRef.h:41
Base class for all callable instructions (InvokeInst and CallInst) Holds everything related to callin...
Definition: InstrTypes.h:1120
This is an important class for using LLVM in a threaded context.
Definition: LLVMContext.h:67
Metadata node.
Definition: Metadata.h:1069
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:51
The instances of the Type class are immutable: once they are created, they are never changed.
Definition: Type.h:45
Class to build a trie of call stack contexts for a particular profiled allocation call,...
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.
void addSingleAllocTypeAttribute(CallBase *CI, AllocationType AT, StringRef Descriptor)
Add an attribute for the given allocation type to the call instruction.
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...
CallStack(const NodeT *N=nullptr)
CallStackIterator begin() const
CallStackIterator end() const
CallStackIterator beginAfterSharedPrefix(CallStack &Other)
@ C
The default llvm calling convention, compatible with C.
Definition: CallingConv.h:34
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.
MDNode * buildContextSizeMetadata(ArrayRef< ContextTotalSize > ContextSizeInfo, LLVMContext &Ctx)
Build metadata from the provided list of full stack id and profiled size, to use when reporting of hi...
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.
Definition: AddressRanges.h:18
@ Other
Any other memory.
#define N
bool operator!=(const CallStackIterator &rhs)
bool operator==(const CallStackIterator &rhs)