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/// Returns the stack node from an MIB metadata node.
32MDNode *getMIBStackNode(const MDNode *MIB);
33
34/// Returns the allocation type from an MIB metadata node.
36
37/// Returns the total size from an MIB metadata node, or 0 if it was not
38/// recorded.
40
41/// Returns the string to use in attributes with the given type.
43
44/// True if the AllocTypes bitmask contains just a single type.
45bool hasSingleAllocType(uint8_t AllocTypes);
46
47/// Class to build a trie of call stack contexts for a particular profiled
48/// allocation call, along with their associated allocation types.
49/// The allocation will be at the root of the trie, which is then used to
50/// compute the minimum lists of context ids needed to associate a call context
51/// with a single allocation type.
53private:
54 struct CallStackTrieNode {
55 // Allocation types for call context sharing the context prefix at this
56 // node.
57 uint8_t AllocTypes;
58 uint64_t TotalSize;
59 // Map of caller stack id to the corresponding child Trie node.
60 std::map<uint64_t, CallStackTrieNode *> Callers;
61 CallStackTrieNode(AllocationType Type, uint64_t TotalSize)
62 : AllocTypes(static_cast<uint8_t>(Type)), TotalSize(TotalSize) {}
63 };
64
65 // The node for the allocation at the root.
66 CallStackTrieNode *Alloc = nullptr;
67 // The allocation's leaf stack id.
68 uint64_t AllocStackId = 0;
69
70 void deleteTrieNode(CallStackTrieNode *Node) {
71 if (!Node)
72 return;
73 for (auto C : Node->Callers)
74 deleteTrieNode(C.second);
75 delete Node;
76 }
77
78 // Recursive helper to trim contexts and create metadata nodes.
79 bool buildMIBNodes(CallStackTrieNode *Node, LLVMContext &Ctx,
80 std::vector<uint64_t> &MIBCallStack,
81 std::vector<Metadata *> &MIBNodes,
82 bool CalleeHasAmbiguousCallerContext);
83
84public:
85 CallStackTrie() = default;
86 ~CallStackTrie() { deleteTrieNode(Alloc); }
87
88 bool empty() const { return Alloc == nullptr; }
89
90 /// Add a call stack context with the given allocation type to the Trie.
91 /// The context is represented by the list of stack ids (computed during
92 /// matching via a debug location hash), expected to be in order from the
93 /// allocation call down to the bottom of the call stack (i.e. callee to
94 /// caller order).
96 uint64_t TotalSize = 0);
97
98 /// Add the call stack context along with its allocation type from the MIB
99 /// metadata to the Trie.
100 void addCallStack(MDNode *MIB);
101
102 /// Build and attach the minimal necessary MIB metadata. If the alloc has a
103 /// single allocation type, add a function attribute instead. The reason for
104 /// adding an attribute in this case is that it matches how the behavior for
105 /// allocation calls will be communicated to lib call simplification after
106 /// cloning or another optimization to distinguish the allocation types,
107 /// which is lower overhead and more direct than maintaining this metadata.
108 /// Returns true if memprof metadata attached, false if not (attribute added).
110};
111
112/// Helper class to iterate through stack ids in both metadata (memprof MIB and
113/// callsite) and the corresponding ThinLTO summary data structures
114/// (CallsiteInfo and MIBInfo). This simplifies implementation of client code
115/// which doesn't need to worry about whether we are operating with IR (Regular
116/// LTO), or summary (ThinLTO).
117template <class NodeT, class IteratorT> class CallStack {
118public:
119 CallStack(const NodeT *N = nullptr) : N(N) {}
120
121 // Implement minimum required methods for range-based for loop.
122 // The default implementation assumes we are operating on ThinLTO data
123 // structures, which have a vector of StackIdIndices. There are specialized
124 // versions provided to iterate through metadata.
126 const NodeT *N = nullptr;
127 IteratorT Iter;
128 CallStackIterator(const NodeT *N, bool End);
130 bool operator==(const CallStackIterator &rhs) { return Iter == rhs.Iter; }
131 bool operator!=(const CallStackIterator &rhs) { return !(*this == rhs); }
132 void operator++() { ++Iter; }
133 };
134
135 bool empty() const { return N == nullptr; }
136
137 CallStackIterator begin() const;
138 CallStackIterator end() const { return CallStackIterator(N, /*End*/ true); }
139 CallStackIterator beginAfterSharedPrefix(CallStack &Other);
140 uint64_t back() const;
141
142private:
143 const NodeT *N = nullptr;
144};
145
146template <class NodeT, class IteratorT>
148 const NodeT *N, bool End)
149 : N(N) {
150 if (!N) {
151 Iter = nullptr;
152 return;
153 }
154 Iter = End ? N->StackIdIndices.end() : N->StackIdIndices.begin();
155}
156
157template <class NodeT, class IteratorT>
159 assert(Iter != N->StackIdIndices.end());
160 return *Iter;
161}
162
163template <class NodeT, class IteratorT>
165 assert(N);
166 return N->StackIdIndices.back();
167}
168
169template <class NodeT, class IteratorT>
172 return CallStackIterator(N, /*End*/ false);
173}
174
175template <class NodeT, class IteratorT>
178 CallStackIterator Cur = begin();
179 for (CallStackIterator OtherCur = Other.begin();
180 Cur != end() && OtherCur != Other.end(); ++Cur, ++OtherCur)
181 assert(*Cur == *OtherCur);
182 return Cur;
183}
184
185/// Specializations for iterating through IR metadata stack contexts.
186template <>
188 const MDNode *N, bool End);
189template <>
192
193} // end namespace memprof
194} // end namespace llvm
195
196#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:1236
This is an important class for using LLVM in a threaded context.
Definition: LLVMContext.h:67
Metadata node.
Definition: Metadata.h:1069
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, 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...
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.
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.
Definition: AddressRanges.h:18
@ Other
Any other memory.
#define N
bool operator!=(const CallStackIterator &rhs)
bool operator==(const CallStackIterator &rhs)