Diego Novillo | de1ab26 | 2014-09-09 12:40:50 +0000 | [diff] [blame] | 1 | //===- SampleProfReader.cpp - Read LLVM sample profile data ---------------===// |
| 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 implements the class that reads LLVM sample profiles. It |
Diego Novillo | bb5605c | 2015-10-14 18:36:30 +0000 | [diff] [blame] | 11 | // supports three file formats: text, binary and gcov. |
Diego Novillo | de1ab26 | 2014-09-09 12:40:50 +0000 | [diff] [blame] | 12 | // |
Diego Novillo | bb5605c | 2015-10-14 18:36:30 +0000 | [diff] [blame] | 13 | // The textual representation is useful for debugging and testing purposes. The |
| 14 | // binary representation is more compact, resulting in smaller file sizes. |
Diego Novillo | de1ab26 | 2014-09-09 12:40:50 +0000 | [diff] [blame] | 15 | // |
Diego Novillo | bb5605c | 2015-10-14 18:36:30 +0000 | [diff] [blame] | 16 | // The gcov encoding is the one generated by GCC's AutoFDO profile creation |
| 17 | // tool (https://github.com/google/autofdo) |
Diego Novillo | de1ab26 | 2014-09-09 12:40:50 +0000 | [diff] [blame] | 18 | // |
Diego Novillo | bb5605c | 2015-10-14 18:36:30 +0000 | [diff] [blame] | 19 | // All three encodings can be used interchangeably as an input sample profile. |
Diego Novillo | de1ab26 | 2014-09-09 12:40:50 +0000 | [diff] [blame] | 20 | // |
Diego Novillo | de1ab26 | 2014-09-09 12:40:50 +0000 | [diff] [blame] | 21 | //===----------------------------------------------------------------------===// |
| 22 | |
| 23 | #include "llvm/ProfileData/SampleProfReader.h" |
Diego Novillo | b93483d | 2015-10-16 18:54:35 +0000 | [diff] [blame] | 24 | #include "llvm/ADT/DenseMap.h" |
Easwaran Raman | 40ee23d | 2016-02-19 03:15:33 +0000 | [diff] [blame] | 25 | #include "llvm/ADT/STLExtras.h" |
Eugene Zelenko | e78d131 | 2017-03-03 01:07:34 +0000 | [diff] [blame] | 26 | #include "llvm/ADT/StringRef.h" |
| 27 | #include "llvm/IR/ProfileSummary.h" |
| 28 | #include "llvm/ProfileData/ProfileCommon.h" |
| 29 | #include "llvm/ProfileData/SampleProf.h" |
Diego Novillo | de1ab26 | 2014-09-09 12:40:50 +0000 | [diff] [blame] | 30 | #include "llvm/Support/ErrorOr.h" |
Diego Novillo | c572e92 | 2014-10-30 18:00:06 +0000 | [diff] [blame] | 31 | #include "llvm/Support/LEB128.h" |
Diego Novillo | de1ab26 | 2014-09-09 12:40:50 +0000 | [diff] [blame] | 32 | #include "llvm/Support/LineIterator.h" |
Diego Novillo | c572e92 | 2014-10-30 18:00:06 +0000 | [diff] [blame] | 33 | #include "llvm/Support/MemoryBuffer.h" |
Eugene Zelenko | e78d131 | 2017-03-03 01:07:34 +0000 | [diff] [blame] | 34 | #include "llvm/Support/raw_ostream.h" |
| 35 | #include <algorithm> |
| 36 | #include <cstddef> |
| 37 | #include <cstdint> |
| 38 | #include <limits> |
| 39 | #include <memory> |
| 40 | #include <system_error> |
| 41 | #include <vector> |
Diego Novillo | de1ab26 | 2014-09-09 12:40:50 +0000 | [diff] [blame] | 42 | |
Diego Novillo | de1ab26 | 2014-09-09 12:40:50 +0000 | [diff] [blame] | 43 | using namespace llvm; |
Eugene Zelenko | e78d131 | 2017-03-03 01:07:34 +0000 | [diff] [blame] | 44 | using namespace sampleprof; |
Diego Novillo | de1ab26 | 2014-09-09 12:40:50 +0000 | [diff] [blame] | 45 | |
Diego Novillo | d5336ae | 2014-11-01 00:56:55 +0000 | [diff] [blame] | 46 | /// \brief Dump the function profile for \p FName. |
Diego Novillo | de1ab26 | 2014-09-09 12:40:50 +0000 | [diff] [blame] | 47 | /// |
Diego Novillo | de1ab26 | 2014-09-09 12:40:50 +0000 | [diff] [blame] | 48 | /// \param FName Name of the function to print. |
Diego Novillo | d5336ae | 2014-11-01 00:56:55 +0000 | [diff] [blame] | 49 | /// \param OS Stream to emit the output to. |
| 50 | void SampleProfileReader::dumpFunctionProfile(StringRef FName, |
| 51 | raw_ostream &OS) { |
Diego Novillo | 8e415a8 | 2015-11-13 20:24:28 +0000 | [diff] [blame] | 52 | OS << "Function: " << FName << ": " << Profiles[FName]; |
Diego Novillo | de1ab26 | 2014-09-09 12:40:50 +0000 | [diff] [blame] | 53 | } |
| 54 | |
Diego Novillo | d5336ae | 2014-11-01 00:56:55 +0000 | [diff] [blame] | 55 | /// \brief Dump all the function profiles found on stream \p OS. |
| 56 | void SampleProfileReader::dump(raw_ostream &OS) { |
| 57 | for (const auto &I : Profiles) |
| 58 | dumpFunctionProfile(I.getKey(), OS); |
Diego Novillo | de1ab26 | 2014-09-09 12:40:50 +0000 | [diff] [blame] | 59 | } |
| 60 | |
Dehao Chen | 6722688 | 2015-09-30 00:42:46 +0000 | [diff] [blame] | 61 | /// \brief Parse \p Input as function head. |
| 62 | /// |
| 63 | /// Parse one line of \p Input, and update function name in \p FName, |
| 64 | /// function's total sample count in \p NumSamples, function's entry |
| 65 | /// count in \p NumHeadSamples. |
| 66 | /// |
| 67 | /// \returns true if parsing is successful. |
| 68 | static bool ParseHead(const StringRef &Input, StringRef &FName, |
Diego Novillo | 38be333 | 2015-10-15 16:36:21 +0000 | [diff] [blame] | 69 | uint64_t &NumSamples, uint64_t &NumHeadSamples) { |
Dehao Chen | 6722688 | 2015-09-30 00:42:46 +0000 | [diff] [blame] | 70 | if (Input[0] == ' ') |
| 71 | return false; |
| 72 | size_t n2 = Input.rfind(':'); |
| 73 | size_t n1 = Input.rfind(':', n2 - 1); |
| 74 | FName = Input.substr(0, n1); |
| 75 | if (Input.substr(n1 + 1, n2 - n1 - 1).getAsInteger(10, NumSamples)) |
| 76 | return false; |
| 77 | if (Input.substr(n2 + 1).getAsInteger(10, NumHeadSamples)) |
| 78 | return false; |
| 79 | return true; |
| 80 | } |
| 81 | |
Dehao Chen | 1004241 | 2015-10-21 01:22:27 +0000 | [diff] [blame] | 82 | /// \brief Returns true if line offset \p L is legal (only has 16 bits). |
Dehao Chen | 57d1dda | 2016-03-03 18:09:32 +0000 | [diff] [blame] | 83 | static bool isOffsetLegal(unsigned L) { return (L & 0xffff) == L; } |
Dehao Chen | 1004241 | 2015-10-21 01:22:27 +0000 | [diff] [blame] | 84 | |
Dehao Chen | 6722688 | 2015-09-30 00:42:46 +0000 | [diff] [blame] | 85 | /// \brief Parse \p Input as line sample. |
| 86 | /// |
| 87 | /// \param Input input line. |
| 88 | /// \param IsCallsite true if the line represents an inlined callsite. |
| 89 | /// \param Depth the depth of the inline stack. |
| 90 | /// \param NumSamples total samples of the line/inlined callsite. |
| 91 | /// \param LineOffset line offset to the start of the function. |
| 92 | /// \param Discriminator discriminator of the line. |
| 93 | /// \param TargetCountMap map from indirect call target to count. |
| 94 | /// |
| 95 | /// returns true if parsing is successful. |
Diego Novillo | 38be333 | 2015-10-15 16:36:21 +0000 | [diff] [blame] | 96 | static bool ParseLine(const StringRef &Input, bool &IsCallsite, uint32_t &Depth, |
| 97 | uint64_t &NumSamples, uint32_t &LineOffset, |
| 98 | uint32_t &Discriminator, StringRef &CalleeName, |
| 99 | DenseMap<StringRef, uint64_t> &TargetCountMap) { |
Dehao Chen | 6722688 | 2015-09-30 00:42:46 +0000 | [diff] [blame] | 100 | for (Depth = 0; Input[Depth] == ' '; Depth++) |
| 101 | ; |
| 102 | if (Depth == 0) |
| 103 | return false; |
| 104 | |
| 105 | size_t n1 = Input.find(':'); |
| 106 | StringRef Loc = Input.substr(Depth, n1 - Depth); |
| 107 | size_t n2 = Loc.find('.'); |
| 108 | if (n2 == StringRef::npos) { |
Dehao Chen | 1004241 | 2015-10-21 01:22:27 +0000 | [diff] [blame] | 109 | if (Loc.getAsInteger(10, LineOffset) || !isOffsetLegal(LineOffset)) |
Dehao Chen | 6722688 | 2015-09-30 00:42:46 +0000 | [diff] [blame] | 110 | return false; |
| 111 | Discriminator = 0; |
| 112 | } else { |
| 113 | if (Loc.substr(0, n2).getAsInteger(10, LineOffset)) |
| 114 | return false; |
| 115 | if (Loc.substr(n2 + 1).getAsInteger(10, Discriminator)) |
| 116 | return false; |
| 117 | } |
| 118 | |
| 119 | StringRef Rest = Input.substr(n1 + 2); |
| 120 | if (Rest[0] >= '0' && Rest[0] <= '9') { |
| 121 | IsCallsite = false; |
| 122 | size_t n3 = Rest.find(' '); |
| 123 | if (n3 == StringRef::npos) { |
| 124 | if (Rest.getAsInteger(10, NumSamples)) |
| 125 | return false; |
| 126 | } else { |
| 127 | if (Rest.substr(0, n3).getAsInteger(10, NumSamples)) |
| 128 | return false; |
| 129 | } |
| 130 | while (n3 != StringRef::npos) { |
| 131 | n3 += Rest.substr(n3).find_first_not_of(' '); |
| 132 | Rest = Rest.substr(n3); |
| 133 | n3 = Rest.find(' '); |
| 134 | StringRef pair = Rest; |
| 135 | if (n3 != StringRef::npos) { |
| 136 | pair = Rest.substr(0, n3); |
| 137 | } |
Diego Novillo | 38be333 | 2015-10-15 16:36:21 +0000 | [diff] [blame] | 138 | size_t n4 = pair.find(':'); |
| 139 | uint64_t count; |
Dehao Chen | 6722688 | 2015-09-30 00:42:46 +0000 | [diff] [blame] | 140 | if (pair.substr(n4 + 1).getAsInteger(10, count)) |
| 141 | return false; |
| 142 | TargetCountMap[pair.substr(0, n4)] = count; |
| 143 | } |
| 144 | } else { |
| 145 | IsCallsite = true; |
Diego Novillo | 38be333 | 2015-10-15 16:36:21 +0000 | [diff] [blame] | 146 | size_t n3 = Rest.find_last_of(':'); |
Dehao Chen | 6722688 | 2015-09-30 00:42:46 +0000 | [diff] [blame] | 147 | CalleeName = Rest.substr(0, n3); |
| 148 | if (Rest.substr(n3 + 1).getAsInteger(10, NumSamples)) |
| 149 | return false; |
| 150 | } |
| 151 | return true; |
| 152 | } |
| 153 | |
Diego Novillo | de1ab26 | 2014-09-09 12:40:50 +0000 | [diff] [blame] | 154 | /// \brief Load samples from a text file. |
| 155 | /// |
| 156 | /// See the documentation at the top of the file for an explanation of |
| 157 | /// the expected format. |
| 158 | /// |
| 159 | /// \returns true if the file was loaded successfully, false otherwise. |
Diego Novillo | c572e92 | 2014-10-30 18:00:06 +0000 | [diff] [blame] | 160 | std::error_code SampleProfileReaderText::read() { |
| 161 | line_iterator LineIt(*Buffer, /*SkipBlanks=*/true, '#'); |
Nathan Slingerland | 48dd080 | 2015-12-16 21:45:43 +0000 | [diff] [blame] | 162 | sampleprof_error Result = sampleprof_error::success; |
Diego Novillo | de1ab26 | 2014-09-09 12:40:50 +0000 | [diff] [blame] | 163 | |
Diego Novillo | aae1ed8 | 2015-10-08 19:40:37 +0000 | [diff] [blame] | 164 | InlineCallStack InlineStack; |
Dehao Chen | 6722688 | 2015-09-30 00:42:46 +0000 | [diff] [blame] | 165 | |
| 166 | for (; !LineIt.is_at_eof(); ++LineIt) { |
| 167 | if ((*LineIt)[(*LineIt).find_first_not_of(' ')] == '#') |
| 168 | continue; |
Diego Novillo | de1ab26 | 2014-09-09 12:40:50 +0000 | [diff] [blame] | 169 | // Read the header of each function. |
| 170 | // |
| 171 | // Note that for function identifiers we are actually expecting |
| 172 | // mangled names, but we may not always get them. This happens when |
| 173 | // the compiler decides not to emit the function (e.g., it was inlined |
| 174 | // and removed). In this case, the binary will not have the linkage |
| 175 | // name for the function, so the profiler will emit the function's |
| 176 | // unmangled name, which may contain characters like ':' and '>' in its |
| 177 | // name (member functions, templates, etc). |
| 178 | // |
| 179 | // The only requirement we place on the identifier, then, is that it |
| 180 | // should not begin with a number. |
Dehao Chen | 6722688 | 2015-09-30 00:42:46 +0000 | [diff] [blame] | 181 | if ((*LineIt)[0] != ' ') { |
Diego Novillo | 38be333 | 2015-10-15 16:36:21 +0000 | [diff] [blame] | 182 | uint64_t NumSamples, NumHeadSamples; |
Dehao Chen | 6722688 | 2015-09-30 00:42:46 +0000 | [diff] [blame] | 183 | StringRef FName; |
| 184 | if (!ParseHead(*LineIt, FName, NumSamples, NumHeadSamples)) { |
| 185 | reportError(LineIt.line_number(), |
| 186 | "Expected 'mangled_name:NUM:NUM', found " + *LineIt); |
| 187 | return sampleprof_error::malformed; |
| 188 | } |
| 189 | Profiles[FName] = FunctionSamples(); |
| 190 | FunctionSamples &FProfile = Profiles[FName]; |
Dehao Chen | 57d1dda | 2016-03-03 18:09:32 +0000 | [diff] [blame] | 191 | FProfile.setName(FName); |
Nathan Slingerland | 48dd080 | 2015-12-16 21:45:43 +0000 | [diff] [blame] | 192 | MergeResult(Result, FProfile.addTotalSamples(NumSamples)); |
| 193 | MergeResult(Result, FProfile.addHeadSamples(NumHeadSamples)); |
Dehao Chen | 6722688 | 2015-09-30 00:42:46 +0000 | [diff] [blame] | 194 | InlineStack.clear(); |
| 195 | InlineStack.push_back(&FProfile); |
| 196 | } else { |
Diego Novillo | 38be333 | 2015-10-15 16:36:21 +0000 | [diff] [blame] | 197 | uint64_t NumSamples; |
Dehao Chen | 6722688 | 2015-09-30 00:42:46 +0000 | [diff] [blame] | 198 | StringRef FName; |
Diego Novillo | 38be333 | 2015-10-15 16:36:21 +0000 | [diff] [blame] | 199 | DenseMap<StringRef, uint64_t> TargetCountMap; |
Dehao Chen | 6722688 | 2015-09-30 00:42:46 +0000 | [diff] [blame] | 200 | bool IsCallsite; |
Diego Novillo | 38be333 | 2015-10-15 16:36:21 +0000 | [diff] [blame] | 201 | uint32_t Depth, LineOffset, Discriminator; |
Dehao Chen | 6722688 | 2015-09-30 00:42:46 +0000 | [diff] [blame] | 202 | if (!ParseLine(*LineIt, IsCallsite, Depth, NumSamples, LineOffset, |
| 203 | Discriminator, FName, TargetCountMap)) { |
Diego Novillo | 3376a78 | 2015-09-17 00:17:24 +0000 | [diff] [blame] | 204 | reportError(LineIt.line_number(), |
| 205 | "Expected 'NUM[.NUM]: NUM[ mangled_name:NUM]*', found " + |
| 206 | *LineIt); |
Diego Novillo | c572e92 | 2014-10-30 18:00:06 +0000 | [diff] [blame] | 207 | return sampleprof_error::malformed; |
Diego Novillo | de1ab26 | 2014-09-09 12:40:50 +0000 | [diff] [blame] | 208 | } |
Dehao Chen | 6722688 | 2015-09-30 00:42:46 +0000 | [diff] [blame] | 209 | if (IsCallsite) { |
| 210 | while (InlineStack.size() > Depth) { |
| 211 | InlineStack.pop_back(); |
Diego Novillo | c572e92 | 2014-10-30 18:00:06 +0000 | [diff] [blame] | 212 | } |
Dehao Chen | 6722688 | 2015-09-30 00:42:46 +0000 | [diff] [blame] | 213 | FunctionSamples &FSamples = InlineStack.back()->functionSamplesAt( |
Dehao Chen | 2c7ca9b | 2017-04-13 19:52:10 +0000 | [diff] [blame] | 214 | LineLocation(LineOffset, Discriminator))[FName]; |
Dehao Chen | 57d1dda | 2016-03-03 18:09:32 +0000 | [diff] [blame] | 215 | FSamples.setName(FName); |
Nathan Slingerland | 48dd080 | 2015-12-16 21:45:43 +0000 | [diff] [blame] | 216 | MergeResult(Result, FSamples.addTotalSamples(NumSamples)); |
Dehao Chen | 6722688 | 2015-09-30 00:42:46 +0000 | [diff] [blame] | 217 | InlineStack.push_back(&FSamples); |
| 218 | } else { |
| 219 | while (InlineStack.size() > Depth) { |
| 220 | InlineStack.pop_back(); |
| 221 | } |
| 222 | FunctionSamples &FProfile = *InlineStack.back(); |
| 223 | for (const auto &name_count : TargetCountMap) { |
Nathan Slingerland | 48dd080 | 2015-12-16 21:45:43 +0000 | [diff] [blame] | 224 | MergeResult(Result, FProfile.addCalledTargetSamples( |
| 225 | LineOffset, Discriminator, name_count.first, |
| 226 | name_count.second)); |
Dehao Chen | 6722688 | 2015-09-30 00:42:46 +0000 | [diff] [blame] | 227 | } |
Nathan Slingerland | 48dd080 | 2015-12-16 21:45:43 +0000 | [diff] [blame] | 228 | MergeResult(Result, FProfile.addBodySamples(LineOffset, Discriminator, |
| 229 | NumSamples)); |
Diego Novillo | c572e92 | 2014-10-30 18:00:06 +0000 | [diff] [blame] | 230 | } |
Diego Novillo | de1ab26 | 2014-09-09 12:40:50 +0000 | [diff] [blame] | 231 | } |
| 232 | } |
Easwaran Raman | 40ee23d | 2016-02-19 03:15:33 +0000 | [diff] [blame] | 233 | if (Result == sampleprof_error::success) |
| 234 | computeSummary(); |
Diego Novillo | de1ab26 | 2014-09-09 12:40:50 +0000 | [diff] [blame] | 235 | |
Nathan Slingerland | 48dd080 | 2015-12-16 21:45:43 +0000 | [diff] [blame] | 236 | return Result; |
Diego Novillo | de1ab26 | 2014-09-09 12:40:50 +0000 | [diff] [blame] | 237 | } |
| 238 | |
Nathan Slingerland | 4f82366 | 2015-11-13 03:47:58 +0000 | [diff] [blame] | 239 | bool SampleProfileReaderText::hasFormat(const MemoryBuffer &Buffer) { |
| 240 | bool result = false; |
| 241 | |
| 242 | // Check that the first non-comment line is a valid function header. |
| 243 | line_iterator LineIt(Buffer, /*SkipBlanks=*/true, '#'); |
| 244 | if (!LineIt.is_at_eof()) { |
| 245 | if ((*LineIt)[0] != ' ') { |
| 246 | uint64_t NumSamples, NumHeadSamples; |
| 247 | StringRef FName; |
| 248 | result = ParseHead(*LineIt, FName, NumSamples, NumHeadSamples); |
| 249 | } |
| 250 | } |
| 251 | |
| 252 | return result; |
| 253 | } |
| 254 | |
Diego Novillo | d5336ae | 2014-11-01 00:56:55 +0000 | [diff] [blame] | 255 | template <typename T> ErrorOr<T> SampleProfileReaderBinary::readNumber() { |
Diego Novillo | c572e92 | 2014-10-30 18:00:06 +0000 | [diff] [blame] | 256 | unsigned NumBytesRead = 0; |
| 257 | std::error_code EC; |
| 258 | uint64_t Val = decodeULEB128(Data, &NumBytesRead); |
| 259 | |
| 260 | if (Val > std::numeric_limits<T>::max()) |
| 261 | EC = sampleprof_error::malformed; |
| 262 | else if (Data + NumBytesRead > End) |
| 263 | EC = sampleprof_error::truncated; |
| 264 | else |
| 265 | EC = sampleprof_error::success; |
| 266 | |
| 267 | if (EC) { |
Diego Novillo | 3376a78 | 2015-09-17 00:17:24 +0000 | [diff] [blame] | 268 | reportError(0, EC.message()); |
Diego Novillo | c572e92 | 2014-10-30 18:00:06 +0000 | [diff] [blame] | 269 | return EC; |
| 270 | } |
| 271 | |
| 272 | Data += NumBytesRead; |
| 273 | return static_cast<T>(Val); |
| 274 | } |
| 275 | |
| 276 | ErrorOr<StringRef> SampleProfileReaderBinary::readString() { |
| 277 | std::error_code EC; |
| 278 | StringRef Str(reinterpret_cast<const char *>(Data)); |
| 279 | if (Data + Str.size() + 1 > End) { |
| 280 | EC = sampleprof_error::truncated; |
Diego Novillo | 3376a78 | 2015-09-17 00:17:24 +0000 | [diff] [blame] | 281 | reportError(0, EC.message()); |
Diego Novillo | c572e92 | 2014-10-30 18:00:06 +0000 | [diff] [blame] | 282 | return EC; |
| 283 | } |
| 284 | |
| 285 | Data += Str.size() + 1; |
| 286 | return Str; |
| 287 | } |
| 288 | |
Diego Novillo | 760c5a8 | 2015-10-13 22:48:46 +0000 | [diff] [blame] | 289 | ErrorOr<StringRef> SampleProfileReaderBinary::readStringFromTable() { |
| 290 | std::error_code EC; |
Diego Novillo | 38be333 | 2015-10-15 16:36:21 +0000 | [diff] [blame] | 291 | auto Idx = readNumber<uint32_t>(); |
Diego Novillo | 760c5a8 | 2015-10-13 22:48:46 +0000 | [diff] [blame] | 292 | if (std::error_code EC = Idx.getError()) |
| 293 | return EC; |
| 294 | if (*Idx >= NameTable.size()) |
| 295 | return sampleprof_error::truncated_name_table; |
| 296 | return NameTable[*Idx]; |
| 297 | } |
| 298 | |
Diego Novillo | a7f1e8e | 2015-10-09 17:54:24 +0000 | [diff] [blame] | 299 | std::error_code |
| 300 | SampleProfileReaderBinary::readProfile(FunctionSamples &FProfile) { |
Diego Novillo | b93483d | 2015-10-16 18:54:35 +0000 | [diff] [blame] | 301 | auto NumSamples = readNumber<uint64_t>(); |
| 302 | if (std::error_code EC = NumSamples.getError()) |
Diego Novillo | a7f1e8e | 2015-10-09 17:54:24 +0000 | [diff] [blame] | 303 | return EC; |
Diego Novillo | b93483d | 2015-10-16 18:54:35 +0000 | [diff] [blame] | 304 | FProfile.addTotalSamples(*NumSamples); |
Diego Novillo | a7f1e8e | 2015-10-09 17:54:24 +0000 | [diff] [blame] | 305 | |
| 306 | // Read the samples in the body. |
Diego Novillo | 38be333 | 2015-10-15 16:36:21 +0000 | [diff] [blame] | 307 | auto NumRecords = readNumber<uint32_t>(); |
Diego Novillo | a7f1e8e | 2015-10-09 17:54:24 +0000 | [diff] [blame] | 308 | if (std::error_code EC = NumRecords.getError()) |
| 309 | return EC; |
| 310 | |
Diego Novillo | 38be333 | 2015-10-15 16:36:21 +0000 | [diff] [blame] | 311 | for (uint32_t I = 0; I < *NumRecords; ++I) { |
Diego Novillo | a7f1e8e | 2015-10-09 17:54:24 +0000 | [diff] [blame] | 312 | auto LineOffset = readNumber<uint64_t>(); |
| 313 | if (std::error_code EC = LineOffset.getError()) |
| 314 | return EC; |
| 315 | |
Dehao Chen | 1004241 | 2015-10-21 01:22:27 +0000 | [diff] [blame] | 316 | if (!isOffsetLegal(*LineOffset)) { |
| 317 | return std::error_code(); |
| 318 | } |
| 319 | |
Diego Novillo | a7f1e8e | 2015-10-09 17:54:24 +0000 | [diff] [blame] | 320 | auto Discriminator = readNumber<uint64_t>(); |
| 321 | if (std::error_code EC = Discriminator.getError()) |
| 322 | return EC; |
| 323 | |
| 324 | auto NumSamples = readNumber<uint64_t>(); |
| 325 | if (std::error_code EC = NumSamples.getError()) |
| 326 | return EC; |
| 327 | |
Diego Novillo | 38be333 | 2015-10-15 16:36:21 +0000 | [diff] [blame] | 328 | auto NumCalls = readNumber<uint32_t>(); |
Diego Novillo | a7f1e8e | 2015-10-09 17:54:24 +0000 | [diff] [blame] | 329 | if (std::error_code EC = NumCalls.getError()) |
| 330 | return EC; |
| 331 | |
Diego Novillo | 38be333 | 2015-10-15 16:36:21 +0000 | [diff] [blame] | 332 | for (uint32_t J = 0; J < *NumCalls; ++J) { |
Diego Novillo | 760c5a8 | 2015-10-13 22:48:46 +0000 | [diff] [blame] | 333 | auto CalledFunction(readStringFromTable()); |
Diego Novillo | a7f1e8e | 2015-10-09 17:54:24 +0000 | [diff] [blame] | 334 | if (std::error_code EC = CalledFunction.getError()) |
| 335 | return EC; |
| 336 | |
| 337 | auto CalledFunctionSamples = readNumber<uint64_t>(); |
| 338 | if (std::error_code EC = CalledFunctionSamples.getError()) |
| 339 | return EC; |
| 340 | |
| 341 | FProfile.addCalledTargetSamples(*LineOffset, *Discriminator, |
| 342 | *CalledFunction, *CalledFunctionSamples); |
| 343 | } |
| 344 | |
| 345 | FProfile.addBodySamples(*LineOffset, *Discriminator, *NumSamples); |
| 346 | } |
| 347 | |
| 348 | // Read all the samples for inlined function calls. |
Diego Novillo | 38be333 | 2015-10-15 16:36:21 +0000 | [diff] [blame] | 349 | auto NumCallsites = readNumber<uint32_t>(); |
Diego Novillo | a7f1e8e | 2015-10-09 17:54:24 +0000 | [diff] [blame] | 350 | if (std::error_code EC = NumCallsites.getError()) |
| 351 | return EC; |
| 352 | |
Diego Novillo | 38be333 | 2015-10-15 16:36:21 +0000 | [diff] [blame] | 353 | for (uint32_t J = 0; J < *NumCallsites; ++J) { |
Diego Novillo | a7f1e8e | 2015-10-09 17:54:24 +0000 | [diff] [blame] | 354 | auto LineOffset = readNumber<uint64_t>(); |
| 355 | if (std::error_code EC = LineOffset.getError()) |
| 356 | return EC; |
| 357 | |
| 358 | auto Discriminator = readNumber<uint64_t>(); |
| 359 | if (std::error_code EC = Discriminator.getError()) |
| 360 | return EC; |
| 361 | |
Diego Novillo | 760c5a8 | 2015-10-13 22:48:46 +0000 | [diff] [blame] | 362 | auto FName(readStringFromTable()); |
Diego Novillo | a7f1e8e | 2015-10-09 17:54:24 +0000 | [diff] [blame] | 363 | if (std::error_code EC = FName.getError()) |
| 364 | return EC; |
| 365 | |
Dehao Chen | 2c7ca9b | 2017-04-13 19:52:10 +0000 | [diff] [blame] | 366 | FunctionSamples &CalleeProfile = FProfile.functionSamplesAt( |
| 367 | LineLocation(*LineOffset, *Discriminator))[*FName]; |
Dehao Chen | 57d1dda | 2016-03-03 18:09:32 +0000 | [diff] [blame] | 368 | CalleeProfile.setName(*FName); |
Diego Novillo | a7f1e8e | 2015-10-09 17:54:24 +0000 | [diff] [blame] | 369 | if (std::error_code EC = readProfile(CalleeProfile)) |
| 370 | return EC; |
| 371 | } |
| 372 | |
| 373 | return sampleprof_error::success; |
| 374 | } |
| 375 | |
Diego Novillo | c572e92 | 2014-10-30 18:00:06 +0000 | [diff] [blame] | 376 | std::error_code SampleProfileReaderBinary::read() { |
| 377 | while (!at_eof()) { |
Diego Novillo | b93483d | 2015-10-16 18:54:35 +0000 | [diff] [blame] | 378 | auto NumHeadSamples = readNumber<uint64_t>(); |
| 379 | if (std::error_code EC = NumHeadSamples.getError()) |
| 380 | return EC; |
| 381 | |
Diego Novillo | 760c5a8 | 2015-10-13 22:48:46 +0000 | [diff] [blame] | 382 | auto FName(readStringFromTable()); |
Diego Novillo | c572e92 | 2014-10-30 18:00:06 +0000 | [diff] [blame] | 383 | if (std::error_code EC = FName.getError()) |
| 384 | return EC; |
| 385 | |
| 386 | Profiles[*FName] = FunctionSamples(); |
| 387 | FunctionSamples &FProfile = Profiles[*FName]; |
Dehao Chen | 57d1dda | 2016-03-03 18:09:32 +0000 | [diff] [blame] | 388 | FProfile.setName(*FName); |
Diego Novillo | c572e92 | 2014-10-30 18:00:06 +0000 | [diff] [blame] | 389 | |
Diego Novillo | b93483d | 2015-10-16 18:54:35 +0000 | [diff] [blame] | 390 | FProfile.addHeadSamples(*NumHeadSamples); |
| 391 | |
Diego Novillo | a7f1e8e | 2015-10-09 17:54:24 +0000 | [diff] [blame] | 392 | if (std::error_code EC = readProfile(FProfile)) |
Diego Novillo | c572e92 | 2014-10-30 18:00:06 +0000 | [diff] [blame] | 393 | return EC; |
Diego Novillo | c572e92 | 2014-10-30 18:00:06 +0000 | [diff] [blame] | 394 | } |
| 395 | |
| 396 | return sampleprof_error::success; |
| 397 | } |
| 398 | |
| 399 | std::error_code SampleProfileReaderBinary::readHeader() { |
| 400 | Data = reinterpret_cast<const uint8_t *>(Buffer->getBufferStart()); |
| 401 | End = Data + Buffer->getBufferSize(); |
| 402 | |
| 403 | // Read and check the magic identifier. |
| 404 | auto Magic = readNumber<uint64_t>(); |
| 405 | if (std::error_code EC = Magic.getError()) |
| 406 | return EC; |
| 407 | else if (*Magic != SPMagic()) |
| 408 | return sampleprof_error::bad_magic; |
| 409 | |
| 410 | // Read the version number. |
| 411 | auto Version = readNumber<uint64_t>(); |
| 412 | if (std::error_code EC = Version.getError()) |
| 413 | return EC; |
| 414 | else if (*Version != SPVersion()) |
| 415 | return sampleprof_error::unsupported_version; |
| 416 | |
Easwaran Raman | 40ee23d | 2016-02-19 03:15:33 +0000 | [diff] [blame] | 417 | if (std::error_code EC = readSummary()) |
| 418 | return EC; |
| 419 | |
Diego Novillo | 760c5a8 | 2015-10-13 22:48:46 +0000 | [diff] [blame] | 420 | // Read the name table. |
Diego Novillo | 38be333 | 2015-10-15 16:36:21 +0000 | [diff] [blame] | 421 | auto Size = readNumber<uint32_t>(); |
Diego Novillo | 760c5a8 | 2015-10-13 22:48:46 +0000 | [diff] [blame] | 422 | if (std::error_code EC = Size.getError()) |
| 423 | return EC; |
| 424 | NameTable.reserve(*Size); |
Diego Novillo | 38be333 | 2015-10-15 16:36:21 +0000 | [diff] [blame] | 425 | for (uint32_t I = 0; I < *Size; ++I) { |
Diego Novillo | 760c5a8 | 2015-10-13 22:48:46 +0000 | [diff] [blame] | 426 | auto Name(readString()); |
| 427 | if (std::error_code EC = Name.getError()) |
| 428 | return EC; |
| 429 | NameTable.push_back(*Name); |
| 430 | } |
| 431 | |
Diego Novillo | c572e92 | 2014-10-30 18:00:06 +0000 | [diff] [blame] | 432 | return sampleprof_error::success; |
| 433 | } |
| 434 | |
Easwaran Raman | 40ee23d | 2016-02-19 03:15:33 +0000 | [diff] [blame] | 435 | std::error_code SampleProfileReaderBinary::readSummaryEntry( |
| 436 | std::vector<ProfileSummaryEntry> &Entries) { |
| 437 | auto Cutoff = readNumber<uint64_t>(); |
| 438 | if (std::error_code EC = Cutoff.getError()) |
| 439 | return EC; |
| 440 | |
| 441 | auto MinBlockCount = readNumber<uint64_t>(); |
| 442 | if (std::error_code EC = MinBlockCount.getError()) |
| 443 | return EC; |
| 444 | |
| 445 | auto NumBlocks = readNumber<uint64_t>(); |
| 446 | if (std::error_code EC = NumBlocks.getError()) |
| 447 | return EC; |
| 448 | |
| 449 | Entries.emplace_back(*Cutoff, *MinBlockCount, *NumBlocks); |
| 450 | return sampleprof_error::success; |
| 451 | } |
| 452 | |
| 453 | std::error_code SampleProfileReaderBinary::readSummary() { |
| 454 | auto TotalCount = readNumber<uint64_t>(); |
| 455 | if (std::error_code EC = TotalCount.getError()) |
| 456 | return EC; |
| 457 | |
| 458 | auto MaxBlockCount = readNumber<uint64_t>(); |
| 459 | if (std::error_code EC = MaxBlockCount.getError()) |
| 460 | return EC; |
| 461 | |
| 462 | auto MaxFunctionCount = readNumber<uint64_t>(); |
| 463 | if (std::error_code EC = MaxFunctionCount.getError()) |
| 464 | return EC; |
| 465 | |
| 466 | auto NumBlocks = readNumber<uint64_t>(); |
| 467 | if (std::error_code EC = NumBlocks.getError()) |
| 468 | return EC; |
| 469 | |
| 470 | auto NumFunctions = readNumber<uint64_t>(); |
| 471 | if (std::error_code EC = NumFunctions.getError()) |
| 472 | return EC; |
| 473 | |
| 474 | auto NumSummaryEntries = readNumber<uint64_t>(); |
| 475 | if (std::error_code EC = NumSummaryEntries.getError()) |
| 476 | return EC; |
| 477 | |
| 478 | std::vector<ProfileSummaryEntry> Entries; |
| 479 | for (unsigned i = 0; i < *NumSummaryEntries; i++) { |
| 480 | std::error_code EC = readSummaryEntry(Entries); |
| 481 | if (EC != sampleprof_error::success) |
| 482 | return EC; |
| 483 | } |
Easwaran Raman | 7cefdb8 | 2016-05-19 21:53:28 +0000 | [diff] [blame] | 484 | Summary = llvm::make_unique<ProfileSummary>( |
| 485 | ProfileSummary::PSK_Sample, Entries, *TotalCount, *MaxBlockCount, 0, |
| 486 | *MaxFunctionCount, *NumBlocks, *NumFunctions); |
Easwaran Raman | 40ee23d | 2016-02-19 03:15:33 +0000 | [diff] [blame] | 487 | |
| 488 | return sampleprof_error::success; |
| 489 | } |
| 490 | |
Diego Novillo | c572e92 | 2014-10-30 18:00:06 +0000 | [diff] [blame] | 491 | bool SampleProfileReaderBinary::hasFormat(const MemoryBuffer &Buffer) { |
| 492 | const uint8_t *Data = |
| 493 | reinterpret_cast<const uint8_t *>(Buffer.getBufferStart()); |
| 494 | uint64_t Magic = decodeULEB128(Data); |
| 495 | return Magic == SPMagic(); |
| 496 | } |
| 497 | |
Diego Novillo | 3376a78 | 2015-09-17 00:17:24 +0000 | [diff] [blame] | 498 | std::error_code SampleProfileReaderGCC::skipNextWord() { |
| 499 | uint32_t dummy; |
| 500 | if (!GcovBuffer.readInt(dummy)) |
| 501 | return sampleprof_error::truncated; |
| 502 | return sampleprof_error::success; |
| 503 | } |
| 504 | |
| 505 | template <typename T> ErrorOr<T> SampleProfileReaderGCC::readNumber() { |
| 506 | if (sizeof(T) <= sizeof(uint32_t)) { |
| 507 | uint32_t Val; |
| 508 | if (GcovBuffer.readInt(Val) && Val <= std::numeric_limits<T>::max()) |
| 509 | return static_cast<T>(Val); |
| 510 | } else if (sizeof(T) <= sizeof(uint64_t)) { |
| 511 | uint64_t Val; |
| 512 | if (GcovBuffer.readInt64(Val) && Val <= std::numeric_limits<T>::max()) |
| 513 | return static_cast<T>(Val); |
| 514 | } |
| 515 | |
| 516 | std::error_code EC = sampleprof_error::malformed; |
| 517 | reportError(0, EC.message()); |
| 518 | return EC; |
| 519 | } |
| 520 | |
| 521 | ErrorOr<StringRef> SampleProfileReaderGCC::readString() { |
| 522 | StringRef Str; |
| 523 | if (!GcovBuffer.readString(Str)) |
| 524 | return sampleprof_error::truncated; |
| 525 | return Str; |
| 526 | } |
| 527 | |
| 528 | std::error_code SampleProfileReaderGCC::readHeader() { |
| 529 | // Read the magic identifier. |
| 530 | if (!GcovBuffer.readGCDAFormat()) |
| 531 | return sampleprof_error::unrecognized_format; |
| 532 | |
| 533 | // Read the version number. Note - the GCC reader does not validate this |
| 534 | // version, but the profile creator generates v704. |
| 535 | GCOV::GCOVVersion version; |
| 536 | if (!GcovBuffer.readGCOVVersion(version)) |
| 537 | return sampleprof_error::unrecognized_format; |
| 538 | |
| 539 | if (version != GCOV::V704) |
| 540 | return sampleprof_error::unsupported_version; |
| 541 | |
| 542 | // Skip the empty integer. |
| 543 | if (std::error_code EC = skipNextWord()) |
| 544 | return EC; |
| 545 | |
| 546 | return sampleprof_error::success; |
| 547 | } |
| 548 | |
| 549 | std::error_code SampleProfileReaderGCC::readSectionTag(uint32_t Expected) { |
| 550 | uint32_t Tag; |
| 551 | if (!GcovBuffer.readInt(Tag)) |
| 552 | return sampleprof_error::truncated; |
| 553 | |
| 554 | if (Tag != Expected) |
| 555 | return sampleprof_error::malformed; |
| 556 | |
| 557 | if (std::error_code EC = skipNextWord()) |
| 558 | return EC; |
| 559 | |
| 560 | return sampleprof_error::success; |
| 561 | } |
| 562 | |
| 563 | std::error_code SampleProfileReaderGCC::readNameTable() { |
| 564 | if (std::error_code EC = readSectionTag(GCOVTagAFDOFileNames)) |
| 565 | return EC; |
| 566 | |
| 567 | uint32_t Size; |
| 568 | if (!GcovBuffer.readInt(Size)) |
| 569 | return sampleprof_error::truncated; |
| 570 | |
| 571 | for (uint32_t I = 0; I < Size; ++I) { |
| 572 | StringRef Str; |
| 573 | if (!GcovBuffer.readString(Str)) |
| 574 | return sampleprof_error::truncated; |
| 575 | Names.push_back(Str); |
| 576 | } |
| 577 | |
| 578 | return sampleprof_error::success; |
| 579 | } |
| 580 | |
| 581 | std::error_code SampleProfileReaderGCC::readFunctionProfiles() { |
| 582 | if (std::error_code EC = readSectionTag(GCOVTagAFDOFunction)) |
| 583 | return EC; |
| 584 | |
| 585 | uint32_t NumFunctions; |
| 586 | if (!GcovBuffer.readInt(NumFunctions)) |
| 587 | return sampleprof_error::truncated; |
| 588 | |
Diego Novillo | aae1ed8 | 2015-10-08 19:40:37 +0000 | [diff] [blame] | 589 | InlineCallStack Stack; |
Diego Novillo | 3376a78 | 2015-09-17 00:17:24 +0000 | [diff] [blame] | 590 | for (uint32_t I = 0; I < NumFunctions; ++I) |
Diego Novillo | aae1ed8 | 2015-10-08 19:40:37 +0000 | [diff] [blame] | 591 | if (std::error_code EC = readOneFunctionProfile(Stack, true, 0)) |
Diego Novillo | 3376a78 | 2015-09-17 00:17:24 +0000 | [diff] [blame] | 592 | return EC; |
| 593 | |
Easwaran Raman | 40ee23d | 2016-02-19 03:15:33 +0000 | [diff] [blame] | 594 | computeSummary(); |
Diego Novillo | 3376a78 | 2015-09-17 00:17:24 +0000 | [diff] [blame] | 595 | return sampleprof_error::success; |
| 596 | } |
| 597 | |
Diego Novillo | aae1ed8 | 2015-10-08 19:40:37 +0000 | [diff] [blame] | 598 | std::error_code SampleProfileReaderGCC::readOneFunctionProfile( |
| 599 | const InlineCallStack &InlineStack, bool Update, uint32_t Offset) { |
Diego Novillo | 3376a78 | 2015-09-17 00:17:24 +0000 | [diff] [blame] | 600 | uint64_t HeadCount = 0; |
Diego Novillo | aae1ed8 | 2015-10-08 19:40:37 +0000 | [diff] [blame] | 601 | if (InlineStack.size() == 0) |
Diego Novillo | 3376a78 | 2015-09-17 00:17:24 +0000 | [diff] [blame] | 602 | if (!GcovBuffer.readInt64(HeadCount)) |
| 603 | return sampleprof_error::truncated; |
| 604 | |
| 605 | uint32_t NameIdx; |
| 606 | if (!GcovBuffer.readInt(NameIdx)) |
| 607 | return sampleprof_error::truncated; |
| 608 | |
| 609 | StringRef Name(Names[NameIdx]); |
| 610 | |
| 611 | uint32_t NumPosCounts; |
| 612 | if (!GcovBuffer.readInt(NumPosCounts)) |
| 613 | return sampleprof_error::truncated; |
| 614 | |
Diego Novillo | aae1ed8 | 2015-10-08 19:40:37 +0000 | [diff] [blame] | 615 | uint32_t NumCallsites; |
| 616 | if (!GcovBuffer.readInt(NumCallsites)) |
Diego Novillo | 3376a78 | 2015-09-17 00:17:24 +0000 | [diff] [blame] | 617 | return sampleprof_error::truncated; |
| 618 | |
Diego Novillo | aae1ed8 | 2015-10-08 19:40:37 +0000 | [diff] [blame] | 619 | FunctionSamples *FProfile = nullptr; |
| 620 | if (InlineStack.size() == 0) { |
| 621 | // If this is a top function that we have already processed, do not |
| 622 | // update its profile again. This happens in the presence of |
| 623 | // function aliases. Since these aliases share the same function |
| 624 | // body, there will be identical replicated profiles for the |
| 625 | // original function. In this case, we simply not bother updating |
| 626 | // the profile of the original function. |
| 627 | FProfile = &Profiles[Name]; |
| 628 | FProfile->addHeadSamples(HeadCount); |
| 629 | if (FProfile->getTotalSamples() > 0) |
Diego Novillo | 3376a78 | 2015-09-17 00:17:24 +0000 | [diff] [blame] | 630 | Update = false; |
Diego Novillo | aae1ed8 | 2015-10-08 19:40:37 +0000 | [diff] [blame] | 631 | } else { |
| 632 | // Otherwise, we are reading an inlined instance. The top of the |
| 633 | // inline stack contains the profile of the caller. Insert this |
| 634 | // callee in the caller's CallsiteMap. |
| 635 | FunctionSamples *CallerProfile = InlineStack.front(); |
| 636 | uint32_t LineOffset = Offset >> 16; |
| 637 | uint32_t Discriminator = Offset & 0xffff; |
| 638 | FProfile = &CallerProfile->functionSamplesAt( |
Dehao Chen | 2c7ca9b | 2017-04-13 19:52:10 +0000 | [diff] [blame] | 639 | LineLocation(LineOffset, Discriminator))[Name]; |
Diego Novillo | 3376a78 | 2015-09-17 00:17:24 +0000 | [diff] [blame] | 640 | } |
Dehao Chen | 57d1dda | 2016-03-03 18:09:32 +0000 | [diff] [blame] | 641 | FProfile->setName(Name); |
Diego Novillo | 3376a78 | 2015-09-17 00:17:24 +0000 | [diff] [blame] | 642 | |
| 643 | for (uint32_t I = 0; I < NumPosCounts; ++I) { |
| 644 | uint32_t Offset; |
| 645 | if (!GcovBuffer.readInt(Offset)) |
| 646 | return sampleprof_error::truncated; |
| 647 | |
| 648 | uint32_t NumTargets; |
| 649 | if (!GcovBuffer.readInt(NumTargets)) |
| 650 | return sampleprof_error::truncated; |
| 651 | |
| 652 | uint64_t Count; |
| 653 | if (!GcovBuffer.readInt64(Count)) |
| 654 | return sampleprof_error::truncated; |
| 655 | |
Diego Novillo | aae1ed8 | 2015-10-08 19:40:37 +0000 | [diff] [blame] | 656 | // The line location is encoded in the offset as: |
| 657 | // high 16 bits: line offset to the start of the function. |
| 658 | // low 16 bits: discriminator. |
| 659 | uint32_t LineOffset = Offset >> 16; |
| 660 | uint32_t Discriminator = Offset & 0xffff; |
Diego Novillo | 3376a78 | 2015-09-17 00:17:24 +0000 | [diff] [blame] | 661 | |
Diego Novillo | aae1ed8 | 2015-10-08 19:40:37 +0000 | [diff] [blame] | 662 | InlineCallStack NewStack; |
| 663 | NewStack.push_back(FProfile); |
| 664 | NewStack.insert(NewStack.end(), InlineStack.begin(), InlineStack.end()); |
| 665 | if (Update) { |
| 666 | // Walk up the inline stack, adding the samples on this line to |
| 667 | // the total sample count of the callers in the chain. |
| 668 | for (auto CallerProfile : NewStack) |
| 669 | CallerProfile->addTotalSamples(Count); |
| 670 | |
| 671 | // Update the body samples for the current profile. |
| 672 | FProfile->addBodySamples(LineOffset, Discriminator, Count); |
| 673 | } |
| 674 | |
| 675 | // Process the list of functions called at an indirect call site. |
| 676 | // These are all the targets that a function pointer (or virtual |
| 677 | // function) resolved at runtime. |
Diego Novillo | 3376a78 | 2015-09-17 00:17:24 +0000 | [diff] [blame] | 678 | for (uint32_t J = 0; J < NumTargets; J++) { |
| 679 | uint32_t HistVal; |
| 680 | if (!GcovBuffer.readInt(HistVal)) |
| 681 | return sampleprof_error::truncated; |
| 682 | |
| 683 | if (HistVal != HIST_TYPE_INDIR_CALL_TOPN) |
| 684 | return sampleprof_error::malformed; |
| 685 | |
| 686 | uint64_t TargetIdx; |
| 687 | if (!GcovBuffer.readInt64(TargetIdx)) |
| 688 | return sampleprof_error::truncated; |
| 689 | StringRef TargetName(Names[TargetIdx]); |
| 690 | |
| 691 | uint64_t TargetCount; |
| 692 | if (!GcovBuffer.readInt64(TargetCount)) |
| 693 | return sampleprof_error::truncated; |
| 694 | |
Dehao Chen | 920677a | 2017-02-22 17:27:21 +0000 | [diff] [blame] | 695 | if (Update) |
| 696 | FProfile->addCalledTargetSamples(LineOffset, Discriminator, |
| 697 | TargetName, TargetCount); |
Diego Novillo | 3376a78 | 2015-09-17 00:17:24 +0000 | [diff] [blame] | 698 | } |
| 699 | } |
| 700 | |
Diego Novillo | aae1ed8 | 2015-10-08 19:40:37 +0000 | [diff] [blame] | 701 | // Process all the inlined callers into the current function. These |
| 702 | // are all the callsites that were inlined into this function. |
| 703 | for (uint32_t I = 0; I < NumCallsites; I++) { |
Diego Novillo | 3376a78 | 2015-09-17 00:17:24 +0000 | [diff] [blame] | 704 | // The offset is encoded as: |
| 705 | // high 16 bits: line offset to the start of the function. |
| 706 | // low 16 bits: discriminator. |
| 707 | uint32_t Offset; |
| 708 | if (!GcovBuffer.readInt(Offset)) |
| 709 | return sampleprof_error::truncated; |
Diego Novillo | aae1ed8 | 2015-10-08 19:40:37 +0000 | [diff] [blame] | 710 | InlineCallStack NewStack; |
| 711 | NewStack.push_back(FProfile); |
| 712 | NewStack.insert(NewStack.end(), InlineStack.begin(), InlineStack.end()); |
| 713 | if (std::error_code EC = readOneFunctionProfile(NewStack, Update, Offset)) |
Diego Novillo | 3376a78 | 2015-09-17 00:17:24 +0000 | [diff] [blame] | 714 | return EC; |
| 715 | } |
| 716 | |
| 717 | return sampleprof_error::success; |
| 718 | } |
| 719 | |
Diego Novillo | 3376a78 | 2015-09-17 00:17:24 +0000 | [diff] [blame] | 720 | /// \brief Read a GCC AutoFDO profile. |
| 721 | /// |
| 722 | /// This format is generated by the Linux Perf conversion tool at |
| 723 | /// https://github.com/google/autofdo. |
| 724 | std::error_code SampleProfileReaderGCC::read() { |
| 725 | // Read the string table. |
| 726 | if (std::error_code EC = readNameTable()) |
| 727 | return EC; |
| 728 | |
| 729 | // Read the source profile. |
| 730 | if (std::error_code EC = readFunctionProfiles()) |
| 731 | return EC; |
| 732 | |
Diego Novillo | 3376a78 | 2015-09-17 00:17:24 +0000 | [diff] [blame] | 733 | return sampleprof_error::success; |
| 734 | } |
| 735 | |
| 736 | bool SampleProfileReaderGCC::hasFormat(const MemoryBuffer &Buffer) { |
| 737 | StringRef Magic(reinterpret_cast<const char *>(Buffer.getBufferStart())); |
| 738 | return Magic == "adcg*704"; |
| 739 | } |
| 740 | |
Diego Novillo | c572e92 | 2014-10-30 18:00:06 +0000 | [diff] [blame] | 741 | /// \brief Prepare a memory buffer for the contents of \p Filename. |
Diego Novillo | de1ab26 | 2014-09-09 12:40:50 +0000 | [diff] [blame] | 742 | /// |
Diego Novillo | c572e92 | 2014-10-30 18:00:06 +0000 | [diff] [blame] | 743 | /// \returns an error code indicating the status of the buffer. |
Diego Novillo | fcd5560 | 2014-11-03 00:51:45 +0000 | [diff] [blame] | 744 | static ErrorOr<std::unique_ptr<MemoryBuffer>> |
Benjamin Kramer | 0da23a2 | 2016-05-29 10:31:00 +0000 | [diff] [blame] | 745 | setupMemoryBuffer(const Twine &Filename) { |
Diego Novillo | c572e92 | 2014-10-30 18:00:06 +0000 | [diff] [blame] | 746 | auto BufferOrErr = MemoryBuffer::getFileOrSTDIN(Filename); |
| 747 | if (std::error_code EC = BufferOrErr.getError()) |
| 748 | return EC; |
Diego Novillo | fcd5560 | 2014-11-03 00:51:45 +0000 | [diff] [blame] | 749 | auto Buffer = std::move(BufferOrErr.get()); |
Diego Novillo | c572e92 | 2014-10-30 18:00:06 +0000 | [diff] [blame] | 750 | |
| 751 | // Sanity check the file. |
Zachary Turner | 260fe3e | 2017-12-14 22:07:03 +0000 | [diff] [blame] | 752 | if (uint64_t(Buffer->getBufferSize()) > std::numeric_limits<uint32_t>::max()) |
Diego Novillo | c572e92 | 2014-10-30 18:00:06 +0000 | [diff] [blame] | 753 | return sampleprof_error::too_large; |
| 754 | |
Diego Novillo | fcd5560 | 2014-11-03 00:51:45 +0000 | [diff] [blame] | 755 | return std::move(Buffer); |
Diego Novillo | c572e92 | 2014-10-30 18:00:06 +0000 | [diff] [blame] | 756 | } |
| 757 | |
| 758 | /// \brief Create a sample profile reader based on the format of the input file. |
| 759 | /// |
| 760 | /// \param Filename The file to open. |
| 761 | /// |
Diego Novillo | c572e92 | 2014-10-30 18:00:06 +0000 | [diff] [blame] | 762 | /// \param C The LLVM context to use to emit diagnostics. |
| 763 | /// |
| 764 | /// \returns an error code indicating the status of the created reader. |
Diego Novillo | fcd5560 | 2014-11-03 00:51:45 +0000 | [diff] [blame] | 765 | ErrorOr<std::unique_ptr<SampleProfileReader>> |
Benjamin Kramer | 0da23a2 | 2016-05-29 10:31:00 +0000 | [diff] [blame] | 766 | SampleProfileReader::create(const Twine &Filename, LLVMContext &C) { |
Diego Novillo | fcd5560 | 2014-11-03 00:51:45 +0000 | [diff] [blame] | 767 | auto BufferOrError = setupMemoryBuffer(Filename); |
| 768 | if (std::error_code EC = BufferOrError.getError()) |
Diego Novillo | c572e92 | 2014-10-30 18:00:06 +0000 | [diff] [blame] | 769 | return EC; |
Nathan Slingerland | 51abea7 | 2015-12-10 17:21:42 +0000 | [diff] [blame] | 770 | return create(BufferOrError.get(), C); |
| 771 | } |
Diego Novillo | c572e92 | 2014-10-30 18:00:06 +0000 | [diff] [blame] | 772 | |
Nathan Slingerland | 51abea7 | 2015-12-10 17:21:42 +0000 | [diff] [blame] | 773 | /// \brief Create a sample profile reader based on the format of the input data. |
| 774 | /// |
| 775 | /// \param B The memory buffer to create the reader from (assumes ownership). |
| 776 | /// |
Nathan Slingerland | 51abea7 | 2015-12-10 17:21:42 +0000 | [diff] [blame] | 777 | /// \param C The LLVM context to use to emit diagnostics. |
| 778 | /// |
| 779 | /// \returns an error code indicating the status of the created reader. |
| 780 | ErrorOr<std::unique_ptr<SampleProfileReader>> |
| 781 | SampleProfileReader::create(std::unique_ptr<MemoryBuffer> &B, LLVMContext &C) { |
Diego Novillo | fcd5560 | 2014-11-03 00:51:45 +0000 | [diff] [blame] | 782 | std::unique_ptr<SampleProfileReader> Reader; |
Nathan Slingerland | 51abea7 | 2015-12-10 17:21:42 +0000 | [diff] [blame] | 783 | if (SampleProfileReaderBinary::hasFormat(*B)) |
| 784 | Reader.reset(new SampleProfileReaderBinary(std::move(B), C)); |
| 785 | else if (SampleProfileReaderGCC::hasFormat(*B)) |
| 786 | Reader.reset(new SampleProfileReaderGCC(std::move(B), C)); |
| 787 | else if (SampleProfileReaderText::hasFormat(*B)) |
| 788 | Reader.reset(new SampleProfileReaderText(std::move(B), C)); |
Nathan Slingerland | 4f82366 | 2015-11-13 03:47:58 +0000 | [diff] [blame] | 789 | else |
| 790 | return sampleprof_error::unrecognized_format; |
Diego Novillo | c572e92 | 2014-10-30 18:00:06 +0000 | [diff] [blame] | 791 | |
Diego Novillo | fcd5560 | 2014-11-03 00:51:45 +0000 | [diff] [blame] | 792 | if (std::error_code EC = Reader->readHeader()) |
| 793 | return EC; |
| 794 | |
| 795 | return std::move(Reader); |
Diego Novillo | de1ab26 | 2014-09-09 12:40:50 +0000 | [diff] [blame] | 796 | } |
Easwaran Raman | 40ee23d | 2016-02-19 03:15:33 +0000 | [diff] [blame] | 797 | |
| 798 | // For text and GCC file formats, we compute the summary after reading the |
| 799 | // profile. Binary format has the profile summary in its header. |
| 800 | void SampleProfileReader::computeSummary() { |
Easwaran Raman | e5a17e3 | 2016-05-19 21:07:12 +0000 | [diff] [blame] | 801 | SampleProfileSummaryBuilder Builder(ProfileSummaryBuilder::DefaultCutoffs); |
Easwaran Raman | 40ee23d | 2016-02-19 03:15:33 +0000 | [diff] [blame] | 802 | for (const auto &I : Profiles) { |
| 803 | const FunctionSamples &Profile = I.second; |
Easwaran Raman | e5a17e3 | 2016-05-19 21:07:12 +0000 | [diff] [blame] | 804 | Builder.addRecord(Profile); |
Easwaran Raman | 40ee23d | 2016-02-19 03:15:33 +0000 | [diff] [blame] | 805 | } |
Benjamin Kramer | 38de59e | 2016-05-20 09:18:37 +0000 | [diff] [blame] | 806 | Summary = Builder.getSummary(); |
Easwaran Raman | 40ee23d | 2016-02-19 03:15:33 +0000 | [diff] [blame] | 807 | } |