1 //=-- SampleProf.cpp - Sample profiling format support --------------------===//
3 // 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
7 //===----------------------------------------------------------------------===//
9 // This file contains common definitions used in the reading and writing of
10 // sample profile data.
12 //===----------------------------------------------------------------------===//
14 #include "llvm/ProfileData/SampleProf.h"
15 #include "llvm/Config/llvm-config.h"
16 #include "llvm/IR/DebugInfoMetadata.h"
17 #include "llvm/Support/Compiler.h"
18 #include "llvm/Support/Debug.h"
19 #include "llvm/Support/ErrorHandling.h"
20 #include "llvm/Support/ManagedStatic.h"
21 #include "llvm/Support/raw_ostream.h"
23 #include <system_error>
26 using namespace sampleprof
;
29 namespace sampleprof
{
30 SampleProfileFormat
FunctionSamples::Format
;
31 DenseMap
<uint64_t, StringRef
> FunctionSamples::GUIDToFuncNameMap
;
32 Module
*FunctionSamples::CurrentModule
;
33 } // namespace sampleprof
38 // 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.
41 class SampleProfErrorCategoryType
: public std::error_category
{
42 const char *name() const noexcept override
{ return "llvm.sampleprof"; }
44 std::string
message(int IE
) const override
{
45 sampleprof_error E
= static_cast<sampleprof_error
>(IE
);
47 case sampleprof_error::success
:
49 case sampleprof_error::bad_magic
:
50 return "Invalid sample profile data (bad magic)";
51 case sampleprof_error::unsupported_version
:
52 return "Unsupported sample profile format version";
53 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
:
58 return "Malformed sample profile data";
59 case sampleprof_error::unrecognized_format
:
60 return "Unrecognized sample profile encoding format";
61 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";
65 case sampleprof_error::not_implemented
:
66 return "Unimplemented feature";
67 case sampleprof_error::counter_overflow
:
68 return "Counter overflow";
69 case sampleprof_error::ostream_seek_unsupported
:
70 return "Ostream does not support seek";
72 llvm_unreachable("A value of sampleprof_error has no message.");
76 } // end anonymous namespace
78 static ManagedStatic
<SampleProfErrorCategoryType
> ErrorCategory
;
80 const std::error_category
&llvm::sampleprof_category() {
81 return *ErrorCategory
;
84 void LineLocation::print(raw_ostream
&OS
) const {
86 if (Discriminator
> 0)
87 OS
<< "." << Discriminator
;
90 raw_ostream
&llvm::sampleprof::operator<<(raw_ostream
&OS
,
91 const LineLocation
&Loc
) {
96 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
97 LLVM_DUMP_METHOD
void LineLocation::dump() const { print(dbgs()); }
100 /// Print the sample record to the stream \p OS indented by \p Indent.
101 void SampleRecord::print(raw_ostream
&OS
, unsigned Indent
) const {
105 for (const auto &I
: getCallTargets())
106 OS
<< " " << I
.first() << ":" << I
.second
;
111 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
112 LLVM_DUMP_METHOD
void SampleRecord::dump() const { print(dbgs(), 0); }
115 raw_ostream
&llvm::sampleprof::operator<<(raw_ostream
&OS
,
116 const SampleRecord
&Sample
) {
121 /// Print the samples collected for a function on stream \p OS.
122 void FunctionSamples::print(raw_ostream
&OS
, unsigned Indent
) const {
123 OS
<< TotalSamples
<< ", " << TotalHeadSamples
<< ", " << BodySamples
.size()
124 << " sampled lines\n";
127 if (!BodySamples
.empty()) {
128 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
;
137 OS
<< "No samples collected in the function's body\n";
141 if (!CallsiteSamples
.empty()) {
142 OS
<< "Samples collected in inlined callsites {\n";
143 SampleSorter
<LineLocation
, FunctionSamplesMap
> SortedCallsiteSamples(
145 for (const auto &CS
: SortedCallsiteSamples
.get()) {
146 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);
154 OS
<< "No inlined callsites in this function\n";
158 raw_ostream
&llvm::sampleprof::operator<<(raw_ostream
&OS
,
159 const FunctionSamples
&FS
) {
164 unsigned FunctionSamples::getOffset(const DILocation
*DIL
) {
165 return (DIL
->getLine() - DIL
->getScope()->getSubprogram()->getLine()) &
169 const FunctionSamples
*
170 FunctionSamples::findFunctionSamples(const DILocation
*DIL
) const {
172 SmallVector
<std::pair
<LineLocation
, StringRef
>, 10> S
;
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()));
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
);
190 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
191 LLVM_DUMP_METHOD
void FunctionSamples::dump() const { print(dbgs(), 0); }