1*0b57cec5SDimitry Andric //=-- SampleProf.cpp - Sample profiling format support --------------------===//
2*0b57cec5SDimitry Andric //
3*0b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*0b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5*0b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*0b57cec5SDimitry Andric //
7*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
8*0b57cec5SDimitry Andric //
9*0b57cec5SDimitry Andric // This file contains common definitions used in the reading and writing of
10*0b57cec5SDimitry Andric // sample profile data.
11*0b57cec5SDimitry Andric //
12*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
13*0b57cec5SDimitry Andric 
14*0b57cec5SDimitry Andric #include "llvm/ProfileData/SampleProf.h"
15*0b57cec5SDimitry Andric #include "llvm/Config/llvm-config.h"
16*0b57cec5SDimitry Andric #include "llvm/IR/DebugInfoMetadata.h"
17*0b57cec5SDimitry Andric #include "llvm/Support/Compiler.h"
18*0b57cec5SDimitry Andric #include "llvm/Support/Debug.h"
19*0b57cec5SDimitry Andric #include "llvm/Support/ErrorHandling.h"
20*0b57cec5SDimitry Andric #include "llvm/Support/ManagedStatic.h"
21*0b57cec5SDimitry Andric #include "llvm/Support/raw_ostream.h"
22*0b57cec5SDimitry Andric #include <string>
23*0b57cec5SDimitry Andric #include <system_error>
24*0b57cec5SDimitry Andric 
25*0b57cec5SDimitry Andric using namespace llvm;
26*0b57cec5SDimitry Andric using namespace sampleprof;
27*0b57cec5SDimitry Andric 
28*0b57cec5SDimitry Andric namespace llvm {
29*0b57cec5SDimitry Andric namespace sampleprof {
30*0b57cec5SDimitry Andric SampleProfileFormat FunctionSamples::Format;
31*0b57cec5SDimitry Andric DenseMap<uint64_t, StringRef> FunctionSamples::GUIDToFuncNameMap;
32*0b57cec5SDimitry Andric Module *FunctionSamples::CurrentModule;
33*0b57cec5SDimitry Andric } // namespace sampleprof
34*0b57cec5SDimitry Andric } // namespace llvm
35*0b57cec5SDimitry Andric 
36*0b57cec5SDimitry Andric namespace {
37*0b57cec5SDimitry Andric 
38*0b57cec5SDimitry Andric // FIXME: This class is only here to support the transition to llvm::Error. It
39*0b57cec5SDimitry Andric // will be removed once this transition is complete. Clients should prefer to
40*0b57cec5SDimitry Andric // deal with the Error value directly, rather than converting to error_code.
41*0b57cec5SDimitry Andric class SampleProfErrorCategoryType : public std::error_category {
42*0b57cec5SDimitry Andric   const char *name() const noexcept override { return "llvm.sampleprof"; }
43*0b57cec5SDimitry Andric 
44*0b57cec5SDimitry Andric   std::string message(int IE) const override {
45*0b57cec5SDimitry Andric     sampleprof_error E = static_cast<sampleprof_error>(IE);
46*0b57cec5SDimitry Andric     switch (E) {
47*0b57cec5SDimitry Andric     case sampleprof_error::success:
48*0b57cec5SDimitry Andric       return "Success";
49*0b57cec5SDimitry Andric     case sampleprof_error::bad_magic:
50*0b57cec5SDimitry Andric       return "Invalid sample profile data (bad magic)";
51*0b57cec5SDimitry Andric     case sampleprof_error::unsupported_version:
52*0b57cec5SDimitry Andric       return "Unsupported sample profile format version";
53*0b57cec5SDimitry Andric     case sampleprof_error::too_large:
54*0b57cec5SDimitry Andric       return "Too much profile data";
55*0b57cec5SDimitry Andric     case sampleprof_error::truncated:
56*0b57cec5SDimitry Andric       return "Truncated profile data";
57*0b57cec5SDimitry Andric     case sampleprof_error::malformed:
58*0b57cec5SDimitry Andric       return "Malformed sample profile data";
59*0b57cec5SDimitry Andric     case sampleprof_error::unrecognized_format:
60*0b57cec5SDimitry Andric       return "Unrecognized sample profile encoding format";
61*0b57cec5SDimitry Andric     case sampleprof_error::unsupported_writing_format:
62*0b57cec5SDimitry Andric       return "Profile encoding format unsupported for writing operations";
63*0b57cec5SDimitry Andric     case sampleprof_error::truncated_name_table:
64*0b57cec5SDimitry Andric       return "Truncated function name table";
65*0b57cec5SDimitry Andric     case sampleprof_error::not_implemented:
66*0b57cec5SDimitry Andric       return "Unimplemented feature";
67*0b57cec5SDimitry Andric     case sampleprof_error::counter_overflow:
68*0b57cec5SDimitry Andric       return "Counter overflow";
69*0b57cec5SDimitry Andric     case sampleprof_error::ostream_seek_unsupported:
70*0b57cec5SDimitry Andric       return "Ostream does not support seek";
71*0b57cec5SDimitry Andric     }
72*0b57cec5SDimitry Andric     llvm_unreachable("A value of sampleprof_error has no message.");
73*0b57cec5SDimitry Andric   }
74*0b57cec5SDimitry Andric };
75*0b57cec5SDimitry Andric 
76*0b57cec5SDimitry Andric } // end anonymous namespace
77*0b57cec5SDimitry Andric 
78*0b57cec5SDimitry Andric static ManagedStatic<SampleProfErrorCategoryType> ErrorCategory;
79*0b57cec5SDimitry Andric 
80*0b57cec5SDimitry Andric const std::error_category &llvm::sampleprof_category() {
81*0b57cec5SDimitry Andric   return *ErrorCategory;
82*0b57cec5SDimitry Andric }
83*0b57cec5SDimitry Andric 
84*0b57cec5SDimitry Andric void LineLocation::print(raw_ostream &OS) const {
85*0b57cec5SDimitry Andric   OS << LineOffset;
86*0b57cec5SDimitry Andric   if (Discriminator > 0)
87*0b57cec5SDimitry Andric     OS << "." << Discriminator;
88*0b57cec5SDimitry Andric }
89*0b57cec5SDimitry Andric 
90*0b57cec5SDimitry Andric raw_ostream &llvm::sampleprof::operator<<(raw_ostream &OS,
91*0b57cec5SDimitry Andric                                           const LineLocation &Loc) {
92*0b57cec5SDimitry Andric   Loc.print(OS);
93*0b57cec5SDimitry Andric   return OS;
94*0b57cec5SDimitry Andric }
95*0b57cec5SDimitry Andric 
96*0b57cec5SDimitry Andric #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
97*0b57cec5SDimitry Andric LLVM_DUMP_METHOD void LineLocation::dump() const { print(dbgs()); }
98*0b57cec5SDimitry Andric #endif
99*0b57cec5SDimitry Andric 
100*0b57cec5SDimitry Andric /// Print the sample record to the stream \p OS indented by \p Indent.
101*0b57cec5SDimitry Andric void SampleRecord::print(raw_ostream &OS, unsigned Indent) const {
102*0b57cec5SDimitry Andric   OS << NumSamples;
103*0b57cec5SDimitry Andric   if (hasCalls()) {
104*0b57cec5SDimitry Andric     OS << ", calls:";
105*0b57cec5SDimitry Andric     for (const auto &I : getCallTargets())
106*0b57cec5SDimitry Andric       OS << " " << I.first() << ":" << I.second;
107*0b57cec5SDimitry Andric   }
108*0b57cec5SDimitry Andric   OS << "\n";
109*0b57cec5SDimitry Andric }
110*0b57cec5SDimitry Andric 
111*0b57cec5SDimitry Andric #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
112*0b57cec5SDimitry Andric LLVM_DUMP_METHOD void SampleRecord::dump() const { print(dbgs(), 0); }
113*0b57cec5SDimitry Andric #endif
114*0b57cec5SDimitry Andric 
115*0b57cec5SDimitry Andric raw_ostream &llvm::sampleprof::operator<<(raw_ostream &OS,
116*0b57cec5SDimitry Andric                                           const SampleRecord &Sample) {
117*0b57cec5SDimitry Andric   Sample.print(OS, 0);
118*0b57cec5SDimitry Andric   return OS;
119*0b57cec5SDimitry Andric }
120*0b57cec5SDimitry Andric 
121*0b57cec5SDimitry Andric /// Print the samples collected for a function on stream \p OS.
122*0b57cec5SDimitry Andric void FunctionSamples::print(raw_ostream &OS, unsigned Indent) const {
123*0b57cec5SDimitry Andric   OS << TotalSamples << ", " << TotalHeadSamples << ", " << BodySamples.size()
124*0b57cec5SDimitry Andric      << " sampled lines\n";
125*0b57cec5SDimitry Andric 
126*0b57cec5SDimitry Andric   OS.indent(Indent);
127*0b57cec5SDimitry Andric   if (!BodySamples.empty()) {
128*0b57cec5SDimitry Andric     OS << "Samples collected in the function's body {\n";
129*0b57cec5SDimitry Andric     SampleSorter<LineLocation, SampleRecord> SortedBodySamples(BodySamples);
130*0b57cec5SDimitry Andric     for (const auto &SI : SortedBodySamples.get()) {
131*0b57cec5SDimitry Andric       OS.indent(Indent + 2);
132*0b57cec5SDimitry Andric       OS << SI->first << ": " << SI->second;
133*0b57cec5SDimitry Andric     }
134*0b57cec5SDimitry Andric     OS.indent(Indent);
135*0b57cec5SDimitry Andric     OS << "}\n";
136*0b57cec5SDimitry Andric   } else {
137*0b57cec5SDimitry Andric     OS << "No samples collected in the function's body\n";
138*0b57cec5SDimitry Andric   }
139*0b57cec5SDimitry Andric 
140*0b57cec5SDimitry Andric   OS.indent(Indent);
141*0b57cec5SDimitry Andric   if (!CallsiteSamples.empty()) {
142*0b57cec5SDimitry Andric     OS << "Samples collected in inlined callsites {\n";
143*0b57cec5SDimitry Andric     SampleSorter<LineLocation, FunctionSamplesMap> SortedCallsiteSamples(
144*0b57cec5SDimitry Andric         CallsiteSamples);
145*0b57cec5SDimitry Andric     for (const auto &CS : SortedCallsiteSamples.get()) {
146*0b57cec5SDimitry Andric       for (const auto &FS : CS->second) {
147*0b57cec5SDimitry Andric         OS.indent(Indent + 2);
148*0b57cec5SDimitry Andric         OS << CS->first << ": inlined callee: " << FS.second.getName() << ": ";
149*0b57cec5SDimitry Andric         FS.second.print(OS, Indent + 4);
150*0b57cec5SDimitry Andric       }
151*0b57cec5SDimitry Andric     }
152*0b57cec5SDimitry Andric     OS << "}\n";
153*0b57cec5SDimitry Andric   } else {
154*0b57cec5SDimitry Andric     OS << "No inlined callsites in this function\n";
155*0b57cec5SDimitry Andric   }
156*0b57cec5SDimitry Andric }
157*0b57cec5SDimitry Andric 
158*0b57cec5SDimitry Andric raw_ostream &llvm::sampleprof::operator<<(raw_ostream &OS,
159*0b57cec5SDimitry Andric                                           const FunctionSamples &FS) {
160*0b57cec5SDimitry Andric   FS.print(OS);
161*0b57cec5SDimitry Andric   return OS;
162*0b57cec5SDimitry Andric }
163*0b57cec5SDimitry Andric 
164*0b57cec5SDimitry Andric unsigned FunctionSamples::getOffset(const DILocation *DIL) {
165*0b57cec5SDimitry Andric   return (DIL->getLine() - DIL->getScope()->getSubprogram()->getLine()) &
166*0b57cec5SDimitry Andric       0xffff;
167*0b57cec5SDimitry Andric }
168*0b57cec5SDimitry Andric 
169*0b57cec5SDimitry Andric const FunctionSamples *
170*0b57cec5SDimitry Andric FunctionSamples::findFunctionSamples(const DILocation *DIL) const {
171*0b57cec5SDimitry Andric   assert(DIL);
172*0b57cec5SDimitry Andric   SmallVector<std::pair<LineLocation, StringRef>, 10> S;
173*0b57cec5SDimitry Andric 
174*0b57cec5SDimitry Andric   const DILocation *PrevDIL = DIL;
175*0b57cec5SDimitry Andric   for (DIL = DIL->getInlinedAt(); DIL; DIL = DIL->getInlinedAt()) {
176*0b57cec5SDimitry Andric     S.push_back(std::make_pair(
177*0b57cec5SDimitry Andric         LineLocation(getOffset(DIL), DIL->getBaseDiscriminator()),
178*0b57cec5SDimitry Andric         PrevDIL->getScope()->getSubprogram()->getLinkageName()));
179*0b57cec5SDimitry Andric     PrevDIL = DIL;
180*0b57cec5SDimitry Andric   }
181*0b57cec5SDimitry Andric   if (S.size() == 0)
182*0b57cec5SDimitry Andric     return this;
183*0b57cec5SDimitry Andric   const FunctionSamples *FS = this;
184*0b57cec5SDimitry Andric   for (int i = S.size() - 1; i >= 0 && FS != nullptr; i--) {
185*0b57cec5SDimitry Andric     FS = FS->findFunctionSamplesAt(S[i].first, S[i].second);
186*0b57cec5SDimitry Andric   }
187*0b57cec5SDimitry Andric   return FS;
188*0b57cec5SDimitry Andric }
189*0b57cec5SDimitry Andric 
190*0b57cec5SDimitry Andric #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
191*0b57cec5SDimitry Andric LLVM_DUMP_METHOD void FunctionSamples::dump() const { print(dbgs(), 0); }
192*0b57cec5SDimitry Andric #endif
193