LLVM  7.0.0
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 {
30 
31 // FIXME: This class is only here to support the transition to llvm::Error. It
32 // will be removed once this transition is complete. Clients should prefer to
33 // deal with the Error value directly, rather than converting to error_code.
34 class SampleProfErrorCategoryType : public std::error_category {
35  const char *name() const noexcept override { return "llvm.sampleprof"; }
36 
37  std::string message(int IE) const override {
38  sampleprof_error E = static_cast<sampleprof_error>(IE);
39  switch (E) {
41  return "Success";
43  return "Invalid sample profile data (bad magic)";
45  return "Unsupported sample profile format version";
47  return "Too much profile data";
49  return "Truncated profile data";
51  return "Malformed sample profile data";
53  return "Unrecognized sample profile encoding format";
55  return "Profile encoding format unsupported for writing operations";
57  return "Truncated function name table";
59  return "Unimplemented feature";
61  return "Counter overflow";
62  }
63  llvm_unreachable("A value of sampleprof_error has no message.");
64  }
65 };
66 
67 } // end anonymous namespace
68 
70 
72  return *ErrorCategory;
73 }
74 
76  OS << LineOffset;
77  if (Discriminator > 0)
78  OS << "." << Discriminator;
79 }
80 
82  const LineLocation &Loc) {
83  Loc.print(OS);
84  return OS;
85 }
86 
87 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
89 #endif
90 
91 /// Print the sample record to the stream \p OS indented by \p Indent.
92 void SampleRecord::print(raw_ostream &OS, unsigned Indent) const {
93  OS << NumSamples;
94  if (hasCalls()) {
95  OS << ", calls:";
96  for (const auto &I : getCallTargets())
97  OS << " " << I.first() << ":" << I.second;
98  }
99  OS << "\n";
100 }
101 
102 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
104 #endif
105 
107  const SampleRecord &Sample) {
108  Sample.print(OS, 0);
109  return OS;
110 }
111 
112 /// Print the samples collected for a function on stream \p OS.
113 void FunctionSamples::print(raw_ostream &OS, unsigned Indent) const {
114  OS << TotalSamples << ", " << TotalHeadSamples << ", " << BodySamples.size()
115  << " sampled lines\n";
116 
117  OS.indent(Indent);
118  if (!BodySamples.empty()) {
119  OS << "Samples collected in the function's body {\n";
120  SampleSorter<LineLocation, SampleRecord> SortedBodySamples(BodySamples);
121  for (const auto &SI : SortedBodySamples.get()) {
122  OS.indent(Indent + 2);
123  OS << SI->first << ": " << SI->second;
124  }
125  OS.indent(Indent);
126  OS << "}\n";
127  } else {
128  OS << "No samples collected in the function's body\n";
129  }
130 
131  OS.indent(Indent);
132  if (!CallsiteSamples.empty()) {
133  OS << "Samples collected in inlined callsites {\n";
135  CallsiteSamples);
136  for (const auto &CS : SortedCallsiteSamples.get()) {
137  for (const auto &FS : CS->second) {
138  OS.indent(Indent + 2);
139  OS << CS->first << ": inlined callee: " << FS.second.getName() << ": ";
140  FS.second.print(OS, Indent + 4);
141  }
142  }
143  OS << "}\n";
144  } else {
145  OS << "No inlined callsites in this function\n";
146  }
147 }
148 
150  const FunctionSamples &FS) {
151  FS.print(OS);
152  return OS;
153 }
154 
156  return (DIL->getLine() - DIL->getScope()->getSubprogram()->getLine()) &
157  0xffff;
158 }
159 
160 const FunctionSamples *
162  assert(DIL);
164 
165  const DILocation *PrevDIL = DIL;
166  for (DIL = DIL->getInlinedAt(); DIL; DIL = DIL->getInlinedAt()) {
167  S.push_back(std::make_pair(
169  PrevDIL->getScope()->getSubprogram()->getLinkageName()));
170  PrevDIL = DIL;
171  }
172  if (S.size() == 0)
173  return this;
174  const FunctionSamples *FS = this;
175  for (int i = S.size() - 1; i >= 0 && FS != nullptr; i--) {
176  FS = FS->findFunctionSamplesAt(S[i].first, S[i].second);
177  }
178  return FS;
179 }
180 
181 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
183 #endif
raw_ostream & operator<<(raw_ostream &OS, const LineLocation &Loc)
Definition: SampleProf.cpp:81
Compute iterated dominance frontiers using a linear time algorithm.
Definition: AllocatorList.h:24
raw_ostream & indent(unsigned NumSpaces)
indent - Insert &#39;NumSpaces&#39; spaces.
unsigned second
#define LLVM_DUMP_METHOD
Mark debug helper function definitions like dump() that should not be stripped from debug builds...
Definition: Compiler.h:452
Representation of the samples collected for a function.
Definition: SampleProf.h:216
void print(raw_ostream &OS=dbgs(), unsigned Indent=0) const
Print the samples collected for a function on stream OS.
Definition: SampleProf.cpp:113
void print(raw_ostream &OS, unsigned Indent) const
Print the sample record to the stream OS indented by Indent.
Definition: SampleProf.cpp:92
Representation of a single sample record.
Definition: SampleProf.h:144
static Error getOffset(const SymbolRef &Sym, SectionRef Sec, uint64_t &Result)
const SamplesWithLocList & get() const
Definition: SampleProf.h:481
Debug location.
Sort a LocationT->SampleT map by LocationT.
Definition: SampleProf.h:467
const FunctionSamples * findFunctionSamples(const DILocation *DIL) const
Get the FunctionSamples of the inline instance where DIL originates from.
Definition: SampleProf.cpp:161
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:74
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:294
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:155
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:71
void print(raw_ostream &OS) const
Definition: SampleProf.cpp:75
static ManagedStatic< SampleProfErrorCategoryType > ErrorCategory
Definition: SampleProf.cpp:69
unsigned getBaseDiscriminator() const
Returns the base discriminator stored in the discriminator.
Represents the relative location of an instruction.
Definition: SampleProf.h:117
#define I(x, y, z)
Definition: MD5.cpp:58
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
Definition: JSON.cpp:592
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