LLVM 18.0.0git
MemProf.cpp
Go to the documentation of this file.
3#include "llvm/IR/Function.h"
8
9namespace llvm {
10namespace memprof {
11
13 raw_ostream &OS) {
14 using namespace support;
15
16 endian::Writer LE(OS, little);
17
18 LE.write<uint64_t>(AllocSites.size());
19 for (const IndexedAllocationInfo &N : AllocSites) {
20 LE.write<uint64_t>(N.CallStack.size());
21 for (const FrameId &Id : N.CallStack)
22 LE.write<FrameId>(Id);
23 N.Info.serialize(Schema, OS);
24 }
25
26 // Related contexts.
27 LE.write<uint64_t>(CallSites.size());
28 for (const auto &Frames : CallSites) {
29 LE.write<uint64_t>(Frames.size());
30 for (const FrameId &Id : Frames)
31 LE.write<FrameId>(Id);
32 }
33}
34
37 const unsigned char *Ptr) {
38 using namespace support;
39
41
42 // Read the meminfo nodes.
43 const uint64_t NumNodes = endian::readNext<uint64_t, little, unaligned>(Ptr);
44 for (uint64_t I = 0; I < NumNodes; I++) {
46 const uint64_t NumFrames =
47 endian::readNext<uint64_t, little, unaligned>(Ptr);
48 for (uint64_t J = 0; J < NumFrames; J++) {
49 const FrameId Id = endian::readNext<FrameId, little, unaligned>(Ptr);
50 Node.CallStack.push_back(Id);
51 }
52 Node.Info.deserialize(Schema, Ptr);
54 Record.AllocSites.push_back(Node);
55 }
56
57 // Read the callsite information.
58 const uint64_t NumCtxs = endian::readNext<uint64_t, little, unaligned>(Ptr);
59 for (uint64_t J = 0; J < NumCtxs; J++) {
60 const uint64_t NumFrames =
61 endian::readNext<uint64_t, little, unaligned>(Ptr);
63 Frames.reserve(NumFrames);
64 for (uint64_t K = 0; K < NumFrames; K++) {
65 const FrameId Id = endian::readNext<FrameId, little, unaligned>(Ptr);
66 Frames.push_back(Id);
67 }
68 Record.CallSites.push_back(Frames);
69 }
70
71 return Record;
72}
73
75 // Canonicalize the function name to drop suffixes such as ".llvm.", ".uniq."
76 // etc. We can then match functions in the profile use phase prior to the
77 // addition of these suffixes. Note that this applies to both instrumented and
78 // sampled function names.
79 StringRef CanonicalName =
81
82 // We use the function guid which we expect to be a uint64_t. At
83 // this time, it is the lower 64 bits of the md5 of the canonical
84 // function name.
85 return Function::getGUID(CanonicalName);
86}
87
88Expected<MemProfSchema> readMemProfSchema(const unsigned char *&Buffer) {
89 using namespace support;
90
91 const unsigned char *Ptr = Buffer;
92 const uint64_t NumSchemaIds =
93 endian::readNext<uint64_t, little, unaligned>(Ptr);
94 if (NumSchemaIds > static_cast<uint64_t>(Meta::Size)) {
95 return make_error<InstrProfError>(instrprof_error::malformed,
96 "memprof schema invalid");
97 }
98
99 MemProfSchema Result;
100 for (size_t I = 0; I < NumSchemaIds; I++) {
101 const uint64_t Tag = endian::readNext<uint64_t, little, unaligned>(Ptr);
102 if (Tag >= static_cast<uint64_t>(Meta::Size)) {
103 return make_error<InstrProfError>(instrprof_error::malformed,
104 "memprof schema invalid");
105 }
106 Result.push_back(static_cast<Meta>(Tag));
107 }
108 // Advace the buffer to one past the schema if we succeeded.
109 Buffer = Ptr;
110 return Result;
111}
112
113} // namespace memprof
114} // namespace llvm
#define I(x, y, z)
Definition: MD5.cpp:58
raw_pwrite_stream & OS
This file defines the SmallVector class.
Tagged union holding either a T or a Error.
Definition: Error.h:474
GUID getGUID() const
Return a 64-bit global unique ID constructed from global value name (i.e.
Definition: GlobalValue.h:591
void reserve(size_type N)
Definition: SmallVector.h:667
void push_back(const T &Elt)
Definition: SmallVector.h:416
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:52
static StringRef getCanonicalFnName(const Function &F)
Return the canonical name for a function, taking into account suffix elision policy attributes.
Definition: SampleProf.h:1087
Expected< MemProfSchema > readMemProfSchema(const unsigned char *&Buffer)
Definition: MemProf.cpp:88
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
#define N
llvm::SmallVector< IndexedAllocationInfo > AllocSites
Definition: MemProf.h:327
void serialize(const MemProfSchema &Schema, raw_ostream &OS)
Definition: MemProf.cpp:12
llvm::SmallVector< llvm::SmallVector< FrameId > > CallSites
Definition: MemProf.h:333
static GlobalValue::GUID getGUID(const StringRef FunctionName)
Definition: MemProf.cpp:74
static IndexedMemProfRecord deserialize(const MemProfSchema &Schema, const unsigned char *Buffer)
Definition: MemProf.cpp:36
static constexpr size_t serializedSize()
Definition: MemProf.h:123
Adapter to write values to a stream in a particular byte order.
Definition: EndianStream.h:68