blob: 1a124415f17979a25eef70c6b4ccba7c4433a824 [file] [log] [blame]
Diego Novilloe75c2b32014-10-30 18:00:06 +00001//=-- 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
15#include "llvm/ProfileData/SampleProf.h"
Nico Weber0f38c602018-04-30 14:59:11 +000016#include "llvm/Config/llvm-config.h"
17#include "llvm/IR/DebugInfoMetadata.h"
Eugene Zelenkof4f67a02017-03-03 01:07:34 +000018#include "llvm/Support/Compiler.h"
19#include "llvm/Support/Debug.h"
Diego Novilloe75c2b32014-10-30 18:00:06 +000020#include "llvm/Support/ErrorHandling.h"
21#include "llvm/Support/ManagedStatic.h"
Eugene Zelenkof4f67a02017-03-03 01:07:34 +000022#include "llvm/Support/raw_ostream.h"
23#include <string>
24#include <system_error>
Diego Novilloe75c2b32014-10-30 18:00:06 +000025
26using namespace llvm;
Eugene Zelenkof4f67a02017-03-03 01:07:34 +000027using namespace sampleprof;
Diego Novilloe75c2b32014-10-30 18:00:06 +000028
Wei Mie465d882018-09-06 22:03:37 +000029namespace llvm {
30namespace sampleprof {
31SampleProfileFormat FunctionSamples::Format;
32DenseMap<uint64_t, StringRef> FunctionSamples::GUIDToFuncNameMap;
33Module *FunctionSamples::CurrentModule;
34} // namespace sampleprof
35} // namespace llvm
36
Diego Novilloe75c2b32014-10-30 18:00:06 +000037namespace {
Eugene Zelenkof4f67a02017-03-03 01:07:34 +000038
Peter Collingbourne84e27c22016-05-24 20:13:46 +000039// 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.
Diego Novilloe75c2b32014-10-30 18:00:06 +000042class SampleProfErrorCategoryType : public std::error_category {
Reid Kleckner5b1c9f32016-10-19 23:52:38 +000043 const char *name() const noexcept override { return "llvm.sampleprof"; }
Eugene Zelenkof4f67a02017-03-03 01:07:34 +000044
Diego Novilloe75c2b32014-10-30 18:00:06 +000045 std::string message(int IE) const override {
46 sampleprof_error E = static_cast<sampleprof_error>(IE);
47 switch (E) {
48 case sampleprof_error::success:
49 return "Success";
50 case sampleprof_error::bad_magic:
Nathan Slingerland572e6332015-11-13 03:47:58 +000051 return "Invalid sample profile data (bad magic)";
Diego Novilloe75c2b32014-10-30 18:00:06 +000052 case sampleprof_error::unsupported_version:
Nathan Slingerland572e6332015-11-13 03:47:58 +000053 return "Unsupported sample profile format version";
Diego Novilloe75c2b32014-10-30 18:00:06 +000054 case sampleprof_error::too_large:
55 return "Too much profile data";
56 case sampleprof_error::truncated:
57 return "Truncated profile data";
58 case sampleprof_error::malformed:
Nathan Slingerland572e6332015-11-13 03:47:58 +000059 return "Malformed sample profile data";
Diego Novillo9657de5f2014-11-01 00:56:55 +000060 case sampleprof_error::unrecognized_format:
Nathan Slingerland572e6332015-11-13 03:47:58 +000061 return "Unrecognized sample profile encoding format";
Diego Novillodb271652015-10-13 22:48:46 +000062 case sampleprof_error::unsupported_writing_format:
63 return "Profile encoding format unsupported for writing operations";
64 case sampleprof_error::truncated_name_table:
65 return "Truncated function name table";
Diego Novillod139c5d2015-09-17 00:17:24 +000066 case sampleprof_error::not_implemented:
67 return "Unimplemented feature";
Nathan Slingerlandfd568242015-12-16 21:45:43 +000068 case sampleprof_error::counter_overflow:
69 return "Counter overflow";
Wei Miec13dea2018-09-14 20:52:59 +000070 case sampleprof_error::ostream_seek_unsupported:
71 return "Ostream does not support seek";
Diego Novilloe75c2b32014-10-30 18:00:06 +000072 }
73 llvm_unreachable("A value of sampleprof_error has no message.");
74 }
75};
Eugene Zelenkof4f67a02017-03-03 01:07:34 +000076
77} // end anonymous namespace
Diego Novilloe75c2b32014-10-30 18:00:06 +000078
79static ManagedStatic<SampleProfErrorCategoryType> ErrorCategory;
80
81const std::error_category &llvm::sampleprof_category() {
82 return *ErrorCategory;
83}
Diego Novillo7e027532015-11-12 17:58:14 +000084
Diego Novillo64725c32015-11-17 19:04:46 +000085void LineLocation::print(raw_ostream &OS) const {
86 OS << LineOffset;
87 if (Discriminator > 0)
88 OS << "." << Discriminator;
89}
90
91raw_ostream &llvm::sampleprof::operator<<(raw_ostream &OS,
92 const LineLocation &Loc) {
93 Loc.print(OS);
94 return OS;
95}
96
Aaron Ballman1d03d382017-10-15 14:32:27 +000097#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
Yaron Keren55307982016-01-29 20:50:44 +000098LLVM_DUMP_METHOD void LineLocation::dump() const { print(dbgs()); }
Matthias Braun88d20752017-01-28 02:02:38 +000099#endif
Diego Novillo64725c32015-11-17 19:04:46 +0000100
Adrian Prantl26b584c2018-05-01 15:54:18 +0000101/// Print the sample record to the stream \p OS indented by \p Indent.
Diego Novillodbe26eb2015-11-13 20:24:28 +0000102void 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
Aaron Ballman1d03d382017-10-15 14:32:27 +0000112#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
Yaron Keren55307982016-01-29 20:50:44 +0000113LLVM_DUMP_METHOD void SampleRecord::dump() const { print(dbgs(), 0); }
Matthias Braun88d20752017-01-28 02:02:38 +0000114#endif
Diego Novillo64725c32015-11-17 19:04:46 +0000115
116raw_ostream &llvm::sampleprof::operator<<(raw_ostream &OS,
117 const SampleRecord &Sample) {
118 Sample.print(OS, 0);
119 return OS;
120}
121
Adrian Prantl26b584c2018-05-01 15:54:18 +0000122/// Print the samples collected for a function on stream \p OS.
Diego Novillo7e027532015-11-12 17:58:14 +0000123void FunctionSamples::print(raw_ostream &OS, unsigned Indent) const {
124 OS << TotalSamples << ", " << TotalHeadSamples << ", " << BodySamples.size()
125 << " sampled lines\n";
Diego Novillodbe26eb2015-11-13 20:24:28 +0000126
Diego Novillo8003b382015-11-19 22:18:30 +0000127 OS.indent(Indent);
Eugene Zelenkof4f67a02017-03-03 01:07:34 +0000128 if (!BodySamples.empty()) {
Diego Novillo8003b382015-11-19 22:18:30 +0000129 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 }
Diego Novillo7e027532015-11-12 17:58:14 +0000135 OS.indent(Indent);
Diego Novillo8003b382015-11-19 22:18:30 +0000136 OS << "}\n";
137 } else {
138 OS << "No samples collected in the function's body\n";
Diego Novillo7e027532015-11-12 17:58:14 +0000139 }
Diego Novillodbe26eb2015-11-13 20:24:28 +0000140
Diego Novillo8003b382015-11-19 22:18:30 +0000141 OS.indent(Indent);
Eugene Zelenkof4f67a02017-03-03 01:07:34 +0000142 if (!CallsiteSamples.empty()) {
Diego Novillo8003b382015-11-19 22:18:30 +0000143 OS << "Samples collected in inlined callsites {\n";
Dehao Chen14eaa3c2017-04-13 19:52:10 +0000144 SampleSorter<LineLocation, FunctionSamplesMap> SortedCallsiteSamples(
Diego Novillo8003b382015-11-19 22:18:30 +0000145 CallsiteSamples);
146 for (const auto &CS : SortedCallsiteSamples.get()) {
Dehao Chen14eaa3c2017-04-13 19:52:10 +0000147 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 }
Diego Novillo8003b382015-11-19 22:18:30 +0000152 }
153 OS << "}\n";
154 } else {
155 OS << "No inlined callsites in this function\n";
Diego Novillo7e027532015-11-12 17:58:14 +0000156 }
157}
Diego Novillo64725c32015-11-17 19:04:46 +0000158
159raw_ostream &llvm::sampleprof::operator<<(raw_ostream &OS,
160 const FunctionSamples &FS) {
161 FS.print(OS);
162 return OS;
163}
164
Mircea Trofin77fc8fc2018-02-22 06:42:57 +0000165unsigned FunctionSamples::getOffset(const DILocation *DIL) {
166 return (DIL->getLine() - DIL->getScope()->getSubprogram()->getLine()) &
167 0xffff;
168}
169
170const FunctionSamples *
171FunctionSamples::findFunctionSamples(const DILocation *DIL) const {
172 assert(DIL);
173 SmallVector<std::pair<LineLocation, StringRef>, 10> S;
174
175 const DILocation *PrevDIL = DIL;
176 for (DIL = DIL->getInlinedAt(); DIL; DIL = DIL->getInlinedAt()) {
177 S.push_back(std::make_pair(
178 LineLocation(getOffset(DIL), DIL->getBaseDiscriminator()),
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
Aaron Ballman1d03d382017-10-15 14:32:27 +0000191#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
Eugene Zelenkof4f67a02017-03-03 01:07:34 +0000192LLVM_DUMP_METHOD void FunctionSamples::dump() const { print(dbgs(), 0); }
Matthias Braun88d20752017-01-28 02:02:38 +0000193#endif