Diego Novillo | e75c2b3 | 2014-10-30 18:00:06 +0000 | [diff] [blame] | 1 | //=-- 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 Weber | 0f38c60 | 2018-04-30 14:59:11 +0000 | [diff] [blame] | 16 | #include "llvm/Config/llvm-config.h" |
| 17 | #include "llvm/IR/DebugInfoMetadata.h" |
Eugene Zelenko | f4f67a0 | 2017-03-03 01:07:34 +0000 | [diff] [blame] | 18 | #include "llvm/Support/Compiler.h" |
| 19 | #include "llvm/Support/Debug.h" |
Diego Novillo | e75c2b3 | 2014-10-30 18:00:06 +0000 | [diff] [blame] | 20 | #include "llvm/Support/ErrorHandling.h" |
| 21 | #include "llvm/Support/ManagedStatic.h" |
Eugene Zelenko | f4f67a0 | 2017-03-03 01:07:34 +0000 | [diff] [blame] | 22 | #include "llvm/Support/raw_ostream.h" |
| 23 | #include <string> |
| 24 | #include <system_error> |
Diego Novillo | e75c2b3 | 2014-10-30 18:00:06 +0000 | [diff] [blame] | 25 | |
| 26 | using namespace llvm; |
Eugene Zelenko | f4f67a0 | 2017-03-03 01:07:34 +0000 | [diff] [blame] | 27 | using namespace sampleprof; |
Diego Novillo | e75c2b3 | 2014-10-30 18:00:06 +0000 | [diff] [blame] | 28 | |
Wei Mi | e465d88 | 2018-09-06 22:03:37 +0000 | [diff] [blame] | 29 | namespace llvm { |
| 30 | namespace sampleprof { |
| 31 | SampleProfileFormat FunctionSamples::Format; |
| 32 | DenseMap<uint64_t, StringRef> FunctionSamples::GUIDToFuncNameMap; |
| 33 | Module *FunctionSamples::CurrentModule; |
| 34 | } // namespace sampleprof |
| 35 | } // namespace llvm |
| 36 | |
Diego Novillo | e75c2b3 | 2014-10-30 18:00:06 +0000 | [diff] [blame] | 37 | namespace { |
Eugene Zelenko | f4f67a0 | 2017-03-03 01:07:34 +0000 | [diff] [blame] | 38 | |
Peter Collingbourne | 84e27c2 | 2016-05-24 20:13:46 +0000 | [diff] [blame] | 39 | // 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 Novillo | e75c2b3 | 2014-10-30 18:00:06 +0000 | [diff] [blame] | 42 | class SampleProfErrorCategoryType : public std::error_category { |
Reid Kleckner | 5b1c9f3 | 2016-10-19 23:52:38 +0000 | [diff] [blame] | 43 | const char *name() const noexcept override { return "llvm.sampleprof"; } |
Eugene Zelenko | f4f67a0 | 2017-03-03 01:07:34 +0000 | [diff] [blame] | 44 | |
Diego Novillo | e75c2b3 | 2014-10-30 18:00:06 +0000 | [diff] [blame] | 45 | 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 Slingerland | 572e633 | 2015-11-13 03:47:58 +0000 | [diff] [blame] | 51 | return "Invalid sample profile data (bad magic)"; |
Diego Novillo | e75c2b3 | 2014-10-30 18:00:06 +0000 | [diff] [blame] | 52 | case sampleprof_error::unsupported_version: |
Nathan Slingerland | 572e633 | 2015-11-13 03:47:58 +0000 | [diff] [blame] | 53 | return "Unsupported sample profile format version"; |
Diego Novillo | e75c2b3 | 2014-10-30 18:00:06 +0000 | [diff] [blame] | 54 | 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 Slingerland | 572e633 | 2015-11-13 03:47:58 +0000 | [diff] [blame] | 59 | return "Malformed sample profile data"; |
Diego Novillo | 9657de5f | 2014-11-01 00:56:55 +0000 | [diff] [blame] | 60 | case sampleprof_error::unrecognized_format: |
Nathan Slingerland | 572e633 | 2015-11-13 03:47:58 +0000 | [diff] [blame] | 61 | return "Unrecognized sample profile encoding format"; |
Diego Novillo | db27165 | 2015-10-13 22:48:46 +0000 | [diff] [blame] | 62 | 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 Novillo | d139c5d | 2015-09-17 00:17:24 +0000 | [diff] [blame] | 66 | case sampleprof_error::not_implemented: |
| 67 | return "Unimplemented feature"; |
Nathan Slingerland | fd56824 | 2015-12-16 21:45:43 +0000 | [diff] [blame] | 68 | case sampleprof_error::counter_overflow: |
| 69 | return "Counter overflow"; |
Wei Mi | ec13dea | 2018-09-14 20:52:59 +0000 | [diff] [blame] | 70 | case sampleprof_error::ostream_seek_unsupported: |
| 71 | return "Ostream does not support seek"; |
Diego Novillo | e75c2b3 | 2014-10-30 18:00:06 +0000 | [diff] [blame] | 72 | } |
| 73 | llvm_unreachable("A value of sampleprof_error has no message."); |
| 74 | } |
| 75 | }; |
Eugene Zelenko | f4f67a0 | 2017-03-03 01:07:34 +0000 | [diff] [blame] | 76 | |
| 77 | } // end anonymous namespace |
Diego Novillo | e75c2b3 | 2014-10-30 18:00:06 +0000 | [diff] [blame] | 78 | |
| 79 | static ManagedStatic<SampleProfErrorCategoryType> ErrorCategory; |
| 80 | |
| 81 | const std::error_category &llvm::sampleprof_category() { |
| 82 | return *ErrorCategory; |
| 83 | } |
Diego Novillo | 7e02753 | 2015-11-12 17:58:14 +0000 | [diff] [blame] | 84 | |
Diego Novillo | 64725c3 | 2015-11-17 19:04:46 +0000 | [diff] [blame] | 85 | void LineLocation::print(raw_ostream &OS) const { |
| 86 | OS << LineOffset; |
| 87 | if (Discriminator > 0) |
| 88 | OS << "." << Discriminator; |
| 89 | } |
| 90 | |
| 91 | raw_ostream &llvm::sampleprof::operator<<(raw_ostream &OS, |
| 92 | const LineLocation &Loc) { |
| 93 | Loc.print(OS); |
| 94 | return OS; |
| 95 | } |
| 96 | |
Aaron Ballman | 1d03d38 | 2017-10-15 14:32:27 +0000 | [diff] [blame] | 97 | #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) |
Yaron Keren | 5530798 | 2016-01-29 20:50:44 +0000 | [diff] [blame] | 98 | LLVM_DUMP_METHOD void LineLocation::dump() const { print(dbgs()); } |
Matthias Braun | 88d2075 | 2017-01-28 02:02:38 +0000 | [diff] [blame] | 99 | #endif |
Diego Novillo | 64725c3 | 2015-11-17 19:04:46 +0000 | [diff] [blame] | 100 | |
Adrian Prantl | 26b584c | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 101 | /// Print the sample record to the stream \p OS indented by \p Indent. |
Diego Novillo | dbe26eb | 2015-11-13 20:24:28 +0000 | [diff] [blame] | 102 | void 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 Ballman | 1d03d38 | 2017-10-15 14:32:27 +0000 | [diff] [blame] | 112 | #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) |
Yaron Keren | 5530798 | 2016-01-29 20:50:44 +0000 | [diff] [blame] | 113 | LLVM_DUMP_METHOD void SampleRecord::dump() const { print(dbgs(), 0); } |
Matthias Braun | 88d2075 | 2017-01-28 02:02:38 +0000 | [diff] [blame] | 114 | #endif |
Diego Novillo | 64725c3 | 2015-11-17 19:04:46 +0000 | [diff] [blame] | 115 | |
| 116 | raw_ostream &llvm::sampleprof::operator<<(raw_ostream &OS, |
| 117 | const SampleRecord &Sample) { |
| 118 | Sample.print(OS, 0); |
| 119 | return OS; |
| 120 | } |
| 121 | |
Adrian Prantl | 26b584c | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 122 | /// Print the samples collected for a function on stream \p OS. |
Diego Novillo | 7e02753 | 2015-11-12 17:58:14 +0000 | [diff] [blame] | 123 | void FunctionSamples::print(raw_ostream &OS, unsigned Indent) const { |
| 124 | OS << TotalSamples << ", " << TotalHeadSamples << ", " << BodySamples.size() |
| 125 | << " sampled lines\n"; |
Diego Novillo | dbe26eb | 2015-11-13 20:24:28 +0000 | [diff] [blame] | 126 | |
Diego Novillo | 8003b38 | 2015-11-19 22:18:30 +0000 | [diff] [blame] | 127 | OS.indent(Indent); |
Eugene Zelenko | f4f67a0 | 2017-03-03 01:07:34 +0000 | [diff] [blame] | 128 | if (!BodySamples.empty()) { |
Diego Novillo | 8003b38 | 2015-11-19 22:18:30 +0000 | [diff] [blame] | 129 | 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 Novillo | 7e02753 | 2015-11-12 17:58:14 +0000 | [diff] [blame] | 135 | OS.indent(Indent); |
Diego Novillo | 8003b38 | 2015-11-19 22:18:30 +0000 | [diff] [blame] | 136 | OS << "}\n"; |
| 137 | } else { |
| 138 | OS << "No samples collected in the function's body\n"; |
Diego Novillo | 7e02753 | 2015-11-12 17:58:14 +0000 | [diff] [blame] | 139 | } |
Diego Novillo | dbe26eb | 2015-11-13 20:24:28 +0000 | [diff] [blame] | 140 | |
Diego Novillo | 8003b38 | 2015-11-19 22:18:30 +0000 | [diff] [blame] | 141 | OS.indent(Indent); |
Eugene Zelenko | f4f67a0 | 2017-03-03 01:07:34 +0000 | [diff] [blame] | 142 | if (!CallsiteSamples.empty()) { |
Diego Novillo | 8003b38 | 2015-11-19 22:18:30 +0000 | [diff] [blame] | 143 | OS << "Samples collected in inlined callsites {\n"; |
Dehao Chen | 14eaa3c | 2017-04-13 19:52:10 +0000 | [diff] [blame] | 144 | SampleSorter<LineLocation, FunctionSamplesMap> SortedCallsiteSamples( |
Diego Novillo | 8003b38 | 2015-11-19 22:18:30 +0000 | [diff] [blame] | 145 | CallsiteSamples); |
| 146 | for (const auto &CS : SortedCallsiteSamples.get()) { |
Dehao Chen | 14eaa3c | 2017-04-13 19:52:10 +0000 | [diff] [blame] | 147 | 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 Novillo | 8003b38 | 2015-11-19 22:18:30 +0000 | [diff] [blame] | 152 | } |
| 153 | OS << "}\n"; |
| 154 | } else { |
| 155 | OS << "No inlined callsites in this function\n"; |
Diego Novillo | 7e02753 | 2015-11-12 17:58:14 +0000 | [diff] [blame] | 156 | } |
| 157 | } |
Diego Novillo | 64725c3 | 2015-11-17 19:04:46 +0000 | [diff] [blame] | 158 | |
| 159 | raw_ostream &llvm::sampleprof::operator<<(raw_ostream &OS, |
| 160 | const FunctionSamples &FS) { |
| 161 | FS.print(OS); |
| 162 | return OS; |
| 163 | } |
| 164 | |
Mircea Trofin | 77fc8fc | 2018-02-22 06:42:57 +0000 | [diff] [blame] | 165 | unsigned FunctionSamples::getOffset(const DILocation *DIL) { |
| 166 | return (DIL->getLine() - DIL->getScope()->getSubprogram()->getLine()) & |
| 167 | 0xffff; |
| 168 | } |
| 169 | |
| 170 | const FunctionSamples * |
| 171 | FunctionSamples::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 Ballman | 1d03d38 | 2017-10-15 14:32:27 +0000 | [diff] [blame] | 191 | #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) |
Eugene Zelenko | f4f67a0 | 2017-03-03 01:07:34 +0000 | [diff] [blame] | 192 | LLVM_DUMP_METHOD void FunctionSamples::dump() const { print(dbgs(), 0); } |
Matthias Braun | 88d2075 | 2017-01-28 02:02:38 +0000 | [diff] [blame] | 193 | #endif |