Diego Novillo | c572e92 | 2014-10-30 18:00:06 +0000 | [diff] [blame] | 1 | //=-- SampleProf.cpp - Sample profiling format support --------------------===// |
| 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame^] | 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 |
Diego Novillo | c572e92 | 2014-10-30 18:00:06 +0000 | [diff] [blame] | 6 | // |
| 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 Weber | 432a388 | 2018-04-30 14:59:11 +0000 | [diff] [blame] | 15 | #include "llvm/Config/llvm-config.h" |
| 16 | #include "llvm/IR/DebugInfoMetadata.h" |
Eugene Zelenko | e78d131 | 2017-03-03 01:07:34 +0000 | [diff] [blame] | 17 | #include "llvm/Support/Compiler.h" |
| 18 | #include "llvm/Support/Debug.h" |
Diego Novillo | c572e92 | 2014-10-30 18:00:06 +0000 | [diff] [blame] | 19 | #include "llvm/Support/ErrorHandling.h" |
| 20 | #include "llvm/Support/ManagedStatic.h" |
Eugene Zelenko | e78d131 | 2017-03-03 01:07:34 +0000 | [diff] [blame] | 21 | #include "llvm/Support/raw_ostream.h" |
| 22 | #include <string> |
| 23 | #include <system_error> |
Diego Novillo | c572e92 | 2014-10-30 18:00:06 +0000 | [diff] [blame] | 24 | |
| 25 | using namespace llvm; |
Eugene Zelenko | e78d131 | 2017-03-03 01:07:34 +0000 | [diff] [blame] | 26 | using namespace sampleprof; |
Diego Novillo | c572e92 | 2014-10-30 18:00:06 +0000 | [diff] [blame] | 27 | |
Wei Mi | 94d44c9 | 2018-09-06 22:03:37 +0000 | [diff] [blame] | 28 | namespace llvm { |
| 29 | namespace sampleprof { |
| 30 | SampleProfileFormat FunctionSamples::Format; |
| 31 | DenseMap<uint64_t, StringRef> FunctionSamples::GUIDToFuncNameMap; |
| 32 | Module *FunctionSamples::CurrentModule; |
| 33 | } // namespace sampleprof |
| 34 | } // namespace llvm |
| 35 | |
Diego Novillo | c572e92 | 2014-10-30 18:00:06 +0000 | [diff] [blame] | 36 | namespace { |
Eugene Zelenko | e78d131 | 2017-03-03 01:07:34 +0000 | [diff] [blame] | 37 | |
Peter Collingbourne | 4718f8b | 2016-05-24 20:13:46 +0000 | [diff] [blame] | 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. |
Diego Novillo | c572e92 | 2014-10-30 18:00:06 +0000 | [diff] [blame] | 41 | class SampleProfErrorCategoryType : public std::error_category { |
Reid Kleckner | 990504e | 2016-10-19 23:52:38 +0000 | [diff] [blame] | 42 | const char *name() const noexcept override { return "llvm.sampleprof"; } |
Eugene Zelenko | e78d131 | 2017-03-03 01:07:34 +0000 | [diff] [blame] | 43 | |
Diego Novillo | c572e92 | 2014-10-30 18:00:06 +0000 | [diff] [blame] | 44 | 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 Slingerland | 4f82366 | 2015-11-13 03:47:58 +0000 | [diff] [blame] | 50 | return "Invalid sample profile data (bad magic)"; |
Diego Novillo | c572e92 | 2014-10-30 18:00:06 +0000 | [diff] [blame] | 51 | case sampleprof_error::unsupported_version: |
Nathan Slingerland | 4f82366 | 2015-11-13 03:47:58 +0000 | [diff] [blame] | 52 | return "Unsupported sample profile format version"; |
Diego Novillo | c572e92 | 2014-10-30 18:00:06 +0000 | [diff] [blame] | 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: |
Nathan Slingerland | 4f82366 | 2015-11-13 03:47:58 +0000 | [diff] [blame] | 58 | return "Malformed sample profile data"; |
Diego Novillo | d5336ae | 2014-11-01 00:56:55 +0000 | [diff] [blame] | 59 | case sampleprof_error::unrecognized_format: |
Nathan Slingerland | 4f82366 | 2015-11-13 03:47:58 +0000 | [diff] [blame] | 60 | return "Unrecognized sample profile encoding format"; |
Diego Novillo | 760c5a8 | 2015-10-13 22:48:46 +0000 | [diff] [blame] | 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"; |
Diego Novillo | 3376a78 | 2015-09-17 00:17:24 +0000 | [diff] [blame] | 65 | case sampleprof_error::not_implemented: |
| 66 | return "Unimplemented feature"; |
Nathan Slingerland | 48dd080 | 2015-12-16 21:45:43 +0000 | [diff] [blame] | 67 | case sampleprof_error::counter_overflow: |
| 68 | return "Counter overflow"; |
Wei Mi | 6a14325 | 2018-09-14 20:52:59 +0000 | [diff] [blame] | 69 | case sampleprof_error::ostream_seek_unsupported: |
| 70 | return "Ostream does not support seek"; |
Diego Novillo | c572e92 | 2014-10-30 18:00:06 +0000 | [diff] [blame] | 71 | } |
| 72 | llvm_unreachable("A value of sampleprof_error has no message."); |
| 73 | } |
| 74 | }; |
Eugene Zelenko | e78d131 | 2017-03-03 01:07:34 +0000 | [diff] [blame] | 75 | |
| 76 | } // end anonymous namespace |
Diego Novillo | c572e92 | 2014-10-30 18:00:06 +0000 | [diff] [blame] | 77 | |
| 78 | static ManagedStatic<SampleProfErrorCategoryType> ErrorCategory; |
| 79 | |
| 80 | const std::error_category &llvm::sampleprof_category() { |
| 81 | return *ErrorCategory; |
| 82 | } |
Diego Novillo | 4b6bdb5 | 2015-11-12 17:58:14 +0000 | [diff] [blame] | 83 | |
Diego Novillo | ba920be | 2015-11-17 19:04:46 +0000 | [diff] [blame] | 84 | void LineLocation::print(raw_ostream &OS) const { |
| 85 | OS << LineOffset; |
| 86 | if (Discriminator > 0) |
| 87 | OS << "." << Discriminator; |
| 88 | } |
| 89 | |
| 90 | raw_ostream &llvm::sampleprof::operator<<(raw_ostream &OS, |
| 91 | const LineLocation &Loc) { |
| 92 | Loc.print(OS); |
| 93 | return OS; |
| 94 | } |
| 95 | |
Aaron Ballman | 615eb47 | 2017-10-15 14:32:27 +0000 | [diff] [blame] | 96 | #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) |
Yaron Keren | eb2a254 | 2016-01-29 20:50:44 +0000 | [diff] [blame] | 97 | LLVM_DUMP_METHOD void LineLocation::dump() const { print(dbgs()); } |
Matthias Braun | 8c209aa | 2017-01-28 02:02:38 +0000 | [diff] [blame] | 98 | #endif |
Diego Novillo | ba920be | 2015-11-17 19:04:46 +0000 | [diff] [blame] | 99 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 100 | /// Print the sample record to the stream \p OS indented by \p Indent. |
Diego Novillo | 8e415a8 | 2015-11-13 20:24:28 +0000 | [diff] [blame] | 101 | void 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 Ballman | 615eb47 | 2017-10-15 14:32:27 +0000 | [diff] [blame] | 111 | #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) |
Yaron Keren | eb2a254 | 2016-01-29 20:50:44 +0000 | [diff] [blame] | 112 | LLVM_DUMP_METHOD void SampleRecord::dump() const { print(dbgs(), 0); } |
Matthias Braun | 8c209aa | 2017-01-28 02:02:38 +0000 | [diff] [blame] | 113 | #endif |
Diego Novillo | ba920be | 2015-11-17 19:04:46 +0000 | [diff] [blame] | 114 | |
| 115 | raw_ostream &llvm::sampleprof::operator<<(raw_ostream &OS, |
| 116 | const SampleRecord &Sample) { |
| 117 | Sample.print(OS, 0); |
| 118 | return OS; |
| 119 | } |
| 120 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 121 | /// Print the samples collected for a function on stream \p OS. |
Diego Novillo | 4b6bdb5 | 2015-11-12 17:58:14 +0000 | [diff] [blame] | 122 | void FunctionSamples::print(raw_ostream &OS, unsigned Indent) const { |
| 123 | OS << TotalSamples << ", " << TotalHeadSamples << ", " << BodySamples.size() |
| 124 | << " sampled lines\n"; |
Diego Novillo | 8e415a8 | 2015-11-13 20:24:28 +0000 | [diff] [blame] | 125 | |
Diego Novillo | 379cc5e | 2015-11-19 22:18:30 +0000 | [diff] [blame] | 126 | OS.indent(Indent); |
Eugene Zelenko | e78d131 | 2017-03-03 01:07:34 +0000 | [diff] [blame] | 127 | if (!BodySamples.empty()) { |
Diego Novillo | 379cc5e | 2015-11-19 22:18:30 +0000 | [diff] [blame] | 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; |
| 133 | } |
Diego Novillo | 4b6bdb5 | 2015-11-12 17:58:14 +0000 | [diff] [blame] | 134 | OS.indent(Indent); |
Diego Novillo | 379cc5e | 2015-11-19 22:18:30 +0000 | [diff] [blame] | 135 | OS << "}\n"; |
| 136 | } else { |
| 137 | OS << "No samples collected in the function's body\n"; |
Diego Novillo | 4b6bdb5 | 2015-11-12 17:58:14 +0000 | [diff] [blame] | 138 | } |
Diego Novillo | 8e415a8 | 2015-11-13 20:24:28 +0000 | [diff] [blame] | 139 | |
Diego Novillo | 379cc5e | 2015-11-19 22:18:30 +0000 | [diff] [blame] | 140 | OS.indent(Indent); |
Eugene Zelenko | e78d131 | 2017-03-03 01:07:34 +0000 | [diff] [blame] | 141 | if (!CallsiteSamples.empty()) { |
Diego Novillo | 379cc5e | 2015-11-19 22:18:30 +0000 | [diff] [blame] | 142 | OS << "Samples collected in inlined callsites {\n"; |
Dehao Chen | 2c7ca9b | 2017-04-13 19:52:10 +0000 | [diff] [blame] | 143 | SampleSorter<LineLocation, FunctionSamplesMap> SortedCallsiteSamples( |
Diego Novillo | 379cc5e | 2015-11-19 22:18:30 +0000 | [diff] [blame] | 144 | CallsiteSamples); |
| 145 | for (const auto &CS : SortedCallsiteSamples.get()) { |
Dehao Chen | 2c7ca9b | 2017-04-13 19:52:10 +0000 | [diff] [blame] | 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); |
| 150 | } |
Diego Novillo | 379cc5e | 2015-11-19 22:18:30 +0000 | [diff] [blame] | 151 | } |
| 152 | OS << "}\n"; |
| 153 | } else { |
| 154 | OS << "No inlined callsites in this function\n"; |
Diego Novillo | 4b6bdb5 | 2015-11-12 17:58:14 +0000 | [diff] [blame] | 155 | } |
| 156 | } |
Diego Novillo | ba920be | 2015-11-17 19:04:46 +0000 | [diff] [blame] | 157 | |
| 158 | raw_ostream &llvm::sampleprof::operator<<(raw_ostream &OS, |
| 159 | const FunctionSamples &FS) { |
| 160 | FS.print(OS); |
| 161 | return OS; |
| 162 | } |
| 163 | |
Mircea Trofin | 5695097 | 2018-02-22 06:42:57 +0000 | [diff] [blame] | 164 | unsigned FunctionSamples::getOffset(const DILocation *DIL) { |
| 165 | return (DIL->getLine() - DIL->getScope()->getSubprogram()->getLine()) & |
| 166 | 0xffff; |
| 167 | } |
| 168 | |
| 169 | const FunctionSamples * |
| 170 | FunctionSamples::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 Ballman | 615eb47 | 2017-10-15 14:32:27 +0000 | [diff] [blame] | 190 | #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) |
Eugene Zelenko | e78d131 | 2017-03-03 01:07:34 +0000 | [diff] [blame] | 191 | LLVM_DUMP_METHOD void FunctionSamples::dump() const { print(dbgs(), 0); } |
Matthias Braun | 8c209aa | 2017-01-28 02:02:38 +0000 | [diff] [blame] | 192 | #endif |