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