LLVM  16.0.0git
MemProf.cpp
Go to the documentation of this file.
2 #include "llvm/ADT/SmallVector.h"
3 #include "llvm/IR/Function.h"
5 #include "llvm/Support/Endian.h"
7 
8 namespace llvm {
9 namespace memprof {
10 
12  raw_ostream &OS) {
13  using namespace support;
14 
16 
17  LE.write<uint64_t>(AllocSites.size());
18  for (const IndexedAllocationInfo &N : AllocSites) {
19  LE.write<uint64_t>(N.CallStack.size());
20  for (const FrameId &Id : N.CallStack)
21  LE.write<FrameId>(Id);
22  N.Info.serialize(Schema, OS);
23  }
24 
25  // Related contexts.
26  LE.write<uint64_t>(CallSites.size());
27  for (const auto &Frames : CallSites) {
28  LE.write<uint64_t>(Frames.size());
29  for (const FrameId &Id : Frames)
30  LE.write<FrameId>(Id);
31  }
32 }
33 
36  const unsigned char *Ptr) {
37  using namespace support;
38 
40 
41  // Read the meminfo nodes.
42  const uint64_t NumNodes = endian::readNext<uint64_t, little, unaligned>(Ptr);
43  for (uint64_t I = 0; I < NumNodes; I++) {
45  const uint64_t NumFrames =
46  endian::readNext<uint64_t, little, unaligned>(Ptr);
47  for (uint64_t J = 0; J < NumFrames; J++) {
48  const FrameId Id = endian::readNext<FrameId, little, unaligned>(Ptr);
49  Node.CallStack.push_back(Id);
50  }
51  Node.Info.deserialize(Schema, Ptr);
53  Record.AllocSites.push_back(Node);
54  }
55 
56  // Read the callsite information.
57  const uint64_t NumCtxs = endian::readNext<uint64_t, little, unaligned>(Ptr);
58  for (uint64_t J = 0; J < NumCtxs; J++) {
59  const uint64_t NumFrames =
60  endian::readNext<uint64_t, little, unaligned>(Ptr);
62  Frames.reserve(NumFrames);
63  for (uint64_t K = 0; K < NumFrames; K++) {
64  const FrameId Id = endian::readNext<FrameId, little, unaligned>(Ptr);
65  Frames.push_back(Id);
66  }
67  Record.CallSites.push_back(Frames);
68  }
69 
70  return Record;
71 }
72 
74  const auto Pos = FunctionName.find(".llvm.");
75 
76  // We use the function guid which we expect to be a uint64_t. At
77  // this time, it is the lower 64 bits of the md5 of the function
78  // name. Any suffix with .llvm. is trimmed since these are added by
79  // thinLTO global promotion. At the time the profile is consumed,
80  // these suffixes will not be present.
81  return Function::getGUID(FunctionName.take_front(Pos));
82 }
83 
84 Expected<MemProfSchema> readMemProfSchema(const unsigned char *&Buffer) {
85  using namespace support;
86 
87  const unsigned char *Ptr = Buffer;
88  const uint64_t NumSchemaIds =
89  endian::readNext<uint64_t, little, unaligned>(Ptr);
90  if (NumSchemaIds > static_cast<uint64_t>(Meta::Size)) {
91  return make_error<InstrProfError>(instrprof_error::malformed,
92  "memprof schema invalid");
93  }
94 
95  MemProfSchema Result;
96  for (size_t I = 0; I < NumSchemaIds; I++) {
97  const uint64_t Tag = endian::readNext<uint64_t, little, unaligned>(Ptr);
98  if (Tag >= static_cast<uint64_t>(Meta::Size)) {
99  return make_error<InstrProfError>(instrprof_error::malformed,
100  "memprof schema invalid");
101  }
102  Result.push_back(static_cast<Meta>(Tag));
103  }
104  // Advace the buffer to one past the schema if we succeeded.
105  Buffer = Ptr;
106  return Result;
107 }
108 
109 } // namespace memprof
110 } // namespace llvm
llvm::StringRef::take_front
StringRef take_front(size_t N=1) const
Return a StringRef equal to 'this' but with only the first N elements remaining.
Definition: StringRef.h:572
llvm
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
llvm::memprof::IndexedMemProfRecord
Definition: MemProf.h:323
llvm::memprof::PortableMemInfoBlock::serializedSize
static constexpr size_t serializedSize()
Definition: MemProf.h:122
llvm::memprof::IndexedMemProfRecord::CallSites
llvm::SmallVector< llvm::SmallVector< FrameId > > CallSites
Definition: MemProf.h:332
llvm::SmallVector< Meta, static_cast< int >(Meta::Size)>
llvm::Expected
Tagged union holding either a T or a Error.
Definition: APFloat.h:41
llvm::dwarf::Tag
Tag
Definition: Dwarf.h:105
llvm::support::endian::Writer
Adapter to write values to a stream in a particular byte order.
Definition: EndianStream.h:52
llvm::support::little
@ little
Definition: Endian.h:27
InstrProf.h
llvm::raw_ostream
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:53
llvm::AArch64CC::LE
@ LE
Definition: AArch64BaseInfo.h:268
llvm::memprof::readMemProfSchema
Expected< MemProfSchema > readMemProfSchema(const unsigned char *&Buffer)
Definition: MemProf.cpp:84
llvm::memprof::Meta
Meta
Definition: MemProf.h:18
uint64_t
I
#define I(x, y, z)
Definition: MD5.cpp:58
Ptr
@ Ptr
Definition: TargetLibraryInfo.cpp:60
llvm::Record
Definition: Record.h:1573
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
Node
Definition: ItaniumDemangle.h:156
MemProf.h
EndianStream.h
llvm::memprof::IndexedAllocationInfo
Definition: MemProf.h:253
llvm::GlobalValue::getGUID
GUID getGUID() const
Return a 64-bit global unique ID constructed from global value name (i.e.
Definition: GlobalValue.h:589
Function.h
llvm::memprof::IndexedMemProfRecord::getGUID
static GlobalValue::GUID getGUID(const StringRef FunctionName)
Definition: MemProf.cpp:73
SmallVector.h
llvm::memprof::IndexedMemProfRecord::AllocSites
llvm::SmallVector< IndexedAllocationInfo > AllocSites
Definition: MemProf.h:326
llvm::memprof::IndexedMemProfRecord::serialize
void serialize(const MemProfSchema &Schema, raw_ostream &OS)
Definition: MemProf.cpp:11
N
#define N
support
Reimplement select in terms of SEL *We would really like to support but we need to prove that the add doesn t need to overflow between the two bit chunks *Implement pre post increment support(e.g. PR935) *Implement smarter const ant generation for binops with large immediates. A few ARMv6T2 ops should be pattern matched
Definition: README.txt:10
llvm::instrprof_error::malformed
@ malformed
llvm::StringRef::find
size_t find(char C, size_t From=0) const
Search for the first character C in the string.
Definition: StringRef.h:294
llvm::memprof::Meta::Size
@ Size
llvm::AMDGPU::VGPRIndexMode::Id
Id
Definition: SIDefines.h:241
llvm::memprof::IndexedMemProfRecord::deserialize
static IndexedMemProfRecord deserialize(const MemProfSchema &Schema, const unsigned char *Buffer)
Definition: MemProf.cpp:35
llvm::SmallVectorImpl::reserve
void reserve(size_type N)
Definition: SmallVector.h:667
Endian.h