LLVM  8.0.1
SampleProf.cpp
Go to the documentation of this file.
1 //=-- SampleProf.cpp - Sample profiling format support --------------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file contains common definitions used in the reading and writing of
11 // sample profile data.
12 //
13 //===----------------------------------------------------------------------===//
14 
16 #include "llvm/Config/llvm-config.h"
18 #include "llvm/Support/Compiler.h"
19 #include "llvm/Support/Debug.h"
23 #include <string>
24 #include <system_error>
25 
26 using namespace llvm;
27 using namespace sampleprof;
28 
29 namespace llvm {
30 namespace sampleprof {
34 } // namespace sampleprof
35 } // namespace llvm
36 
37 namespace {
38 
39 // FIXME: This class is only here to support the transition to llvm::Error. It
40 // will be removed once this transition is complete. Clients should prefer to
41 // deal with the Error value directly, rather than converting to error_code.
42 class SampleProfErrorCategoryType : public std::error_category {
43  const char *name() const noexcept override { return "llvm.sampleprof"; }
44 
45  std::string message(int IE) const override {
46  sampleprof_error E = static_cast<sampleprof_error>(IE);
47  switch (E) {
49  return "Success";
51  return "Invalid sample profile data (bad magic)";
53  return "Unsupported sample profile format version";
55  return "Too much profile data";
57  return "Truncated profile data";
59  return "Malformed sample profile data";
61  return "Unrecognized sample profile encoding format";
63  return "Profile encoding format unsupported for writing operations";
65  return "Truncated function name table";
67  return "Unimplemented feature";
69  return "Counter overflow";
71  return "Ostream does not support seek";
72  }
73  llvm_unreachable("A value of sampleprof_error has no message.");
74  }
75 };
76 
77 } // end anonymous namespace
78 
80 
82  return *ErrorCategory;
83 }
84 
86  OS << LineOffset;
87  if (Discriminator > 0)
88  OS << "." << Discriminator;
89 }
90 
92  const LineLocation &Loc) {
93  Loc.print(OS);
94  return OS;
95 }
96 
97 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
99 #endif
100 
101 /// Print the sample record to the stream \p OS indented by \p Indent.
102 void SampleRecord::print(raw_ostream &OS, unsigned Indent) const {
103  OS << NumSamples;
104  if (hasCalls()) {
105  OS << ", calls:";
106  for (const auto &I : getCallTargets())
107  OS << " " << I.first() << ":" << I.second;
108  }
109  OS << "\n";
110 }
111 
112 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
114 #endif
115 
117  const SampleRecord &Sample) {
118  Sample.print(OS, 0);
119  return OS;
120 }
121 
122 /// Print the samples collected for a function on stream \p OS.
123 void FunctionSamples::print(raw_ostream &OS, unsigned Indent) const {
124  OS << TotalSamples << ", " << TotalHeadSamples << ", " << BodySamples.size()
125  << " sampled lines\n";
126 
127  OS.indent(Indent);
128  if (!BodySamples.empty()) {
129  OS << "Samples collected in the function's body {\n";
130  SampleSorter<LineLocation, SampleRecord> SortedBodySamples(BodySamples);
131  for (const auto &SI : SortedBodySamples.get()) {
132  OS.indent(Indent + 2);
133  OS << SI->first << ": " << SI->second;
134  }
135  OS.indent(Indent);
136  OS << "}\n";
137  } else {
138  OS << "No samples collected in the function's body\n";
139  }
140 
141  OS.indent(Indent);
142  if (!CallsiteSamples.empty()) {
143  OS << "Samples collected in inlined callsites {\n";
145  CallsiteSamples);
146  for (const auto &CS : SortedCallsiteSamples.get()) {
147  for (const auto &FS : CS->second) {
148  OS.indent(Indent + 2);
149  OS << CS->first << ": inlined callee: " << FS.second.getName() << ": ";
150  FS.second.print(OS, Indent + 4);
151  }
152  }
153  OS << "}\n";
154  } else {
155  OS << "No inlined callsites in this function\n";
156  }
157 }
158 
160  const FunctionSamples &FS) {
161  FS.print(OS);
162  return OS;
163 }
164 
166  return (DIL->getLine() - DIL->getScope()->getSubprogram()->getLine()) &
167  0xffff;
168 }
169 
170 const FunctionSamples *
172  assert(DIL);
174 
175  const DILocation *PrevDIL = DIL;
176  for (DIL = DIL->getInlinedAt(); DIL; DIL = DIL->getInlinedAt()) {
177  S.push_back(std::make_pair(
179  PrevDIL->getScope()->getSubprogram()->getLinkageName()));
180  PrevDIL = DIL;
181  }
182  if (S.size() == 0)
183  return this;
184  const FunctionSamples *FS = this;
185  for (int i = S.size() - 1; i >= 0 && FS != nullptr; i--) {
186  FS = FS->findFunctionSamplesAt(S[i].first, S[i].second);
187  }
188  return FS;
189 }
190 
191 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
193 #endif
raw_ostream & operator<<(raw_ostream &OS, const LineLocation &Loc)
Definition: SampleProf.cpp:91
This class represents lattice values for constants.
Definition: AllocatorList.h:24
A Module instance is used to store all the information related to an LLVM module. ...
Definition: Module.h:65
raw_ostream & indent(unsigned NumSpaces)
indent - Insert &#39;NumSpaces&#39; spaces.
unsigned second
static SampleProfileFormat Format
Definition: SampleProf.h:447
Representation of the samples collected for a function.
Definition: SampleProf.h:217
void print(raw_ostream &OS=dbgs(), unsigned Indent=0) const
Print the samples collected for a function on stream OS.
Definition: SampleProf.cpp:123
#define LLVM_DUMP_METHOD
Definition: Compiler.h:74
static DenseMap< uint64_t, StringRef > GUIDToFuncNameMap
GUIDToFuncNameMap saves the mapping from GUID to the symbol name, for all the function symbols define...
Definition: SampleProf.h:450
void print(raw_ostream &OS, unsigned Indent) const
Print the sample record to the stream OS indented by Indent.
Definition: SampleProf.cpp:102
Representation of a single sample record.
Definition: SampleProf.h:145
static Error getOffset(const SymbolRef &Sym, SectionRef Sec, uint64_t &Result)
const SamplesWithLocList & get() const
Definition: SampleProf.h:556
Debug location.
Sort a LocationT->SampleT map by LocationT.
Definition: SampleProf.h:542
const FunctionSamples * findFunctionSamples(const DILocation *DIL) const
Get the FunctionSamples of the inline instance where DIL originates from.
Definition: SampleProf.cpp:171
sampleprof_error
Definition: SampleProf.h:41
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
static ManagedStatic< _object_error_category > error_category
Definition: Error.cpp:75
static void print(raw_ostream &Out, object::Archive::Kind Kind, T Val)
size_t size() const
Definition: SmallVector.h:53
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
unsigned first
const FunctionSamples * findFunctionSamplesAt(const LineLocation &Loc, StringRef CalleeName) const
Returns a pointer to FunctionSamples at the given callsite location Loc with callee CalleeName...
Definition: SampleProf.h:295
This is a &#39;vector&#39; (really, a variable-sized array), optimized for the case when the array is small...
Definition: SmallVector.h:847
static unsigned getOffset(const DILocation *DIL)
Returns the line offset to the start line of the subprogram.
Definition: SampleProf.cpp:165
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:133
const std::error_category & sampleprof_category()
Definition: SampleProf.cpp:81
void print(raw_ostream &OS) const
Definition: SampleProf.cpp:85
static ManagedStatic< SampleProfErrorCategoryType > ErrorCategory
Definition: SampleProf.cpp:79
unsigned getBaseDiscriminator() const
Returns the base discriminator stored in the discriminator.
Represents the relative location of an instruction.
Definition: SampleProf.h:118
#define I(x, y, z)
Definition: MD5.cpp:58
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
Definition: JSON.cpp:598
aarch64 promote const
static const char * name
This class implements an extremely fast bulk output stream that can only output to a stream...
Definition: raw_ostream.h:46
ManagedStatic - This transparently changes the behavior of global statics to be lazily constructed on...
Definition: ManagedStatic.h:61