blob: e17865cd15a474daa81018cc1d39c30e2cf8b3b7 [file] [log] [blame]
Diego Novilloc572e922014-10-30 18:00:06 +00001//=-- SampleProf.cpp - Sample profiling format support --------------------===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Diego Novilloc572e922014-10-30 18:00:06 +00006//
7//===----------------------------------------------------------------------===//
8//
9// This file contains common definitions used in the reading and writing of
10// sample profile data.
11//
12//===----------------------------------------------------------------------===//
13
14#include "llvm/ProfileData/SampleProf.h"
Nico Weber432a3882018-04-30 14:59:11 +000015#include "llvm/Config/llvm-config.h"
16#include "llvm/IR/DebugInfoMetadata.h"
Eugene Zelenkoe78d1312017-03-03 01:07:34 +000017#include "llvm/Support/Compiler.h"
18#include "llvm/Support/Debug.h"
Diego Novilloc572e922014-10-30 18:00:06 +000019#include "llvm/Support/ErrorHandling.h"
20#include "llvm/Support/ManagedStatic.h"
Eugene Zelenkoe78d1312017-03-03 01:07:34 +000021#include "llvm/Support/raw_ostream.h"
22#include <string>
23#include <system_error>
Diego Novilloc572e922014-10-30 18:00:06 +000024
25using namespace llvm;
Eugene Zelenkoe78d1312017-03-03 01:07:34 +000026using namespace sampleprof;
Diego Novilloc572e922014-10-30 18:00:06 +000027
Wei Mi94d44c92018-09-06 22:03:37 +000028namespace llvm {
29namespace sampleprof {
30SampleProfileFormat FunctionSamples::Format;
31DenseMap<uint64_t, StringRef> FunctionSamples::GUIDToFuncNameMap;
32Module *FunctionSamples::CurrentModule;
33} // namespace sampleprof
34} // namespace llvm
35
Diego Novilloc572e922014-10-30 18:00:06 +000036namespace {
Eugene Zelenkoe78d1312017-03-03 01:07:34 +000037
Peter Collingbourne4718f8b2016-05-24 20:13:46 +000038// FIXME: This class is only here to support the transition to llvm::Error. It
39// will be removed once this transition is complete. Clients should prefer to
40// deal with the Error value directly, rather than converting to error_code.
Diego Novilloc572e922014-10-30 18:00:06 +000041class SampleProfErrorCategoryType : public std::error_category {
Reid Kleckner990504e2016-10-19 23:52:38 +000042 const char *name() const noexcept override { return "llvm.sampleprof"; }
Eugene Zelenkoe78d1312017-03-03 01:07:34 +000043
Diego Novilloc572e922014-10-30 18:00:06 +000044 std::string message(int IE) const override {
45 sampleprof_error E = static_cast<sampleprof_error>(IE);
46 switch (E) {
47 case sampleprof_error::success:
48 return "Success";
49 case sampleprof_error::bad_magic:
Nathan Slingerland4f823662015-11-13 03:47:58 +000050 return "Invalid sample profile data (bad magic)";
Diego Novilloc572e922014-10-30 18:00:06 +000051 case sampleprof_error::unsupported_version:
Nathan Slingerland4f823662015-11-13 03:47:58 +000052 return "Unsupported sample profile format version";
Diego Novilloc572e922014-10-30 18:00:06 +000053 case sampleprof_error::too_large:
54 return "Too much profile data";
55 case sampleprof_error::truncated:
56 return "Truncated profile data";
57 case sampleprof_error::malformed:
Nathan Slingerland4f823662015-11-13 03:47:58 +000058 return "Malformed sample profile data";
Diego Novillod5336ae2014-11-01 00:56:55 +000059 case sampleprof_error::unrecognized_format:
Nathan Slingerland4f823662015-11-13 03:47:58 +000060 return "Unrecognized sample profile encoding format";
Diego Novillo760c5a82015-10-13 22:48:46 +000061 case sampleprof_error::unsupported_writing_format:
62 return "Profile encoding format unsupported for writing operations";
63 case sampleprof_error::truncated_name_table:
64 return "Truncated function name table";
Diego Novillo3376a782015-09-17 00:17:24 +000065 case sampleprof_error::not_implemented:
66 return "Unimplemented feature";
Nathan Slingerland48dd0802015-12-16 21:45:43 +000067 case sampleprof_error::counter_overflow:
68 return "Counter overflow";
Wei Mi6a143252018-09-14 20:52:59 +000069 case sampleprof_error::ostream_seek_unsupported:
70 return "Ostream does not support seek";
Diego Novilloc572e922014-10-30 18:00:06 +000071 }
72 llvm_unreachable("A value of sampleprof_error has no message.");
73 }
74};
Eugene Zelenkoe78d1312017-03-03 01:07:34 +000075
76} // end anonymous namespace
Diego Novilloc572e922014-10-30 18:00:06 +000077
78static ManagedStatic<SampleProfErrorCategoryType> ErrorCategory;
79
80const std::error_category &llvm::sampleprof_category() {
81 return *ErrorCategory;
82}
Diego Novillo4b6bdb52015-11-12 17:58:14 +000083
Diego Novilloba920be2015-11-17 19:04:46 +000084void LineLocation::print(raw_ostream &OS) const {
85 OS << LineOffset;
86 if (Discriminator > 0)
87 OS << "." << Discriminator;
88}
89
90raw_ostream &llvm::sampleprof::operator<<(raw_ostream &OS,
91 const LineLocation &Loc) {
92 Loc.print(OS);
93 return OS;
94}
95
Aaron Ballman615eb472017-10-15 14:32:27 +000096#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
Yaron Kereneb2a2542016-01-29 20:50:44 +000097LLVM_DUMP_METHOD void LineLocation::dump() const { print(dbgs()); }
Matthias Braun8c209aa2017-01-28 02:02:38 +000098#endif
Diego Novilloba920be2015-11-17 19:04:46 +000099
Adrian Prantl5f8f34e42018-05-01 15:54:18 +0000100/// Print the sample record to the stream \p OS indented by \p Indent.
Diego Novillo8e415a82015-11-13 20:24:28 +0000101void SampleRecord::print(raw_ostream &OS, unsigned Indent) const {
102 OS << NumSamples;
103 if (hasCalls()) {
104 OS << ", calls:";
105 for (const auto &I : getCallTargets())
106 OS << " " << I.first() << ":" << I.second;
107 }
108 OS << "\n";
109}
110
Aaron Ballman615eb472017-10-15 14:32:27 +0000111#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
Yaron Kereneb2a2542016-01-29 20:50:44 +0000112LLVM_DUMP_METHOD void SampleRecord::dump() const { print(dbgs(), 0); }
Matthias Braun8c209aa2017-01-28 02:02:38 +0000113#endif
Diego Novilloba920be2015-11-17 19:04:46 +0000114
115raw_ostream &llvm::sampleprof::operator<<(raw_ostream &OS,
116 const SampleRecord &Sample) {
117 Sample.print(OS, 0);
118 return OS;
119}
120
Adrian Prantl5f8f34e42018-05-01 15:54:18 +0000121/// Print the samples collected for a function on stream \p OS.
Diego Novillo4b6bdb52015-11-12 17:58:14 +0000122void FunctionSamples::print(raw_ostream &OS, unsigned Indent) const {
123 OS << TotalSamples << ", " << TotalHeadSamples << ", " << BodySamples.size()
124 << " sampled lines\n";
Diego Novillo8e415a82015-11-13 20:24:28 +0000125
Diego Novillo379cc5e2015-11-19 22:18:30 +0000126 OS.indent(Indent);
Eugene Zelenkoe78d1312017-03-03 01:07:34 +0000127 if (!BodySamples.empty()) {
Diego Novillo379cc5e2015-11-19 22:18:30 +0000128 OS << "Samples collected in the function's body {\n";
129 SampleSorter<LineLocation, SampleRecord> SortedBodySamples(BodySamples);
130 for (const auto &SI : SortedBodySamples.get()) {
131 OS.indent(Indent + 2);
132 OS << SI->first << ": " << SI->second;
133 }
Diego Novillo4b6bdb52015-11-12 17:58:14 +0000134 OS.indent(Indent);
Diego Novillo379cc5e2015-11-19 22:18:30 +0000135 OS << "}\n";
136 } else {
137 OS << "No samples collected in the function's body\n";
Diego Novillo4b6bdb52015-11-12 17:58:14 +0000138 }
Diego Novillo8e415a82015-11-13 20:24:28 +0000139
Diego Novillo379cc5e2015-11-19 22:18:30 +0000140 OS.indent(Indent);
Eugene Zelenkoe78d1312017-03-03 01:07:34 +0000141 if (!CallsiteSamples.empty()) {
Diego Novillo379cc5e2015-11-19 22:18:30 +0000142 OS << "Samples collected in inlined callsites {\n";
Dehao Chen2c7ca9b2017-04-13 19:52:10 +0000143 SampleSorter<LineLocation, FunctionSamplesMap> SortedCallsiteSamples(
Diego Novillo379cc5e2015-11-19 22:18:30 +0000144 CallsiteSamples);
145 for (const auto &CS : SortedCallsiteSamples.get()) {
Dehao Chen2c7ca9b2017-04-13 19:52:10 +0000146 for (const auto &FS : CS->second) {
147 OS.indent(Indent + 2);
148 OS << CS->first << ": inlined callee: " << FS.second.getName() << ": ";
149 FS.second.print(OS, Indent + 4);
150 }
Diego Novillo379cc5e2015-11-19 22:18:30 +0000151 }
152 OS << "}\n";
153 } else {
154 OS << "No inlined callsites in this function\n";
Diego Novillo4b6bdb52015-11-12 17:58:14 +0000155 }
156}
Diego Novilloba920be2015-11-17 19:04:46 +0000157
158raw_ostream &llvm::sampleprof::operator<<(raw_ostream &OS,
159 const FunctionSamples &FS) {
160 FS.print(OS);
161 return OS;
162}
163
Mircea Trofin56950972018-02-22 06:42:57 +0000164unsigned FunctionSamples::getOffset(const DILocation *DIL) {
165 return (DIL->getLine() - DIL->getScope()->getSubprogram()->getLine()) &
166 0xffff;
167}
168
169const FunctionSamples *
170FunctionSamples::findFunctionSamples(const DILocation *DIL) const {
171 assert(DIL);
172 SmallVector<std::pair<LineLocation, StringRef>, 10> S;
173
174 const DILocation *PrevDIL = DIL;
175 for (DIL = DIL->getInlinedAt(); DIL; DIL = DIL->getInlinedAt()) {
176 S.push_back(std::make_pair(
177 LineLocation(getOffset(DIL), DIL->getBaseDiscriminator()),
178 PrevDIL->getScope()->getSubprogram()->getLinkageName()));
179 PrevDIL = DIL;
180 }
181 if (S.size() == 0)
182 return this;
183 const FunctionSamples *FS = this;
184 for (int i = S.size() - 1; i >= 0 && FS != nullptr; i--) {
185 FS = FS->findFunctionSamplesAt(S[i].first, S[i].second);
186 }
187 return FS;
188}
189
Aaron Ballman615eb472017-10-15 14:32:27 +0000190#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
Eugene Zelenkoe78d1312017-03-03 01:07:34 +0000191LLVM_DUMP_METHOD void FunctionSamples::dump() const { print(dbgs(), 0); }
Matthias Braun8c209aa2017-01-28 02:02:38 +0000192#endif