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