blob: 192b6c711562d289c7f8f3c7a0c9ae1b6465d82d [file] [log] [blame]
Diego Novillode1ab262014-09-09 12:40:50 +00001//===- SampleProfReader.cpp - Read LLVM sample profile data ---------------===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Diego Novillode1ab262014-09-09 12:40:50 +00006//
7//===----------------------------------------------------------------------===//
8//
9// This file implements the class that reads LLVM sample profiles. It
Diego Novillobb5605c2015-10-14 18:36:30 +000010// supports three file formats: text, binary and gcov.
Diego Novillode1ab262014-09-09 12:40:50 +000011//
Diego Novillobb5605c2015-10-14 18:36:30 +000012// The textual representation is useful for debugging and testing purposes. The
13// binary representation is more compact, resulting in smaller file sizes.
Diego Novillode1ab262014-09-09 12:40:50 +000014//
Diego Novillobb5605c2015-10-14 18:36:30 +000015// The gcov encoding is the one generated by GCC's AutoFDO profile creation
16// tool (https://github.com/google/autofdo)
Diego Novillode1ab262014-09-09 12:40:50 +000017//
Diego Novillobb5605c2015-10-14 18:36:30 +000018// All three encodings can be used interchangeably as an input sample profile.
Diego Novillode1ab262014-09-09 12:40:50 +000019//
Diego Novillode1ab262014-09-09 12:40:50 +000020//===----------------------------------------------------------------------===//
21
22#include "llvm/ProfileData/SampleProfReader.h"
Diego Novillob93483d2015-10-16 18:54:35 +000023#include "llvm/ADT/DenseMap.h"
Easwaran Raman40ee23d2016-02-19 03:15:33 +000024#include "llvm/ADT/STLExtras.h"
Eugene Zelenkoe78d1312017-03-03 01:07:34 +000025#include "llvm/ADT/StringRef.h"
26#include "llvm/IR/ProfileSummary.h"
27#include "llvm/ProfileData/ProfileCommon.h"
28#include "llvm/ProfileData/SampleProf.h"
Diego Novillode1ab262014-09-09 12:40:50 +000029#include "llvm/Support/ErrorOr.h"
Diego Novilloc572e922014-10-30 18:00:06 +000030#include "llvm/Support/LEB128.h"
Diego Novillode1ab262014-09-09 12:40:50 +000031#include "llvm/Support/LineIterator.h"
Wei Mi6a143252018-09-14 20:52:59 +000032#include "llvm/Support/MD5.h"
Diego Novilloc572e922014-10-30 18:00:06 +000033#include "llvm/Support/MemoryBuffer.h"
Eugene Zelenkoe78d1312017-03-03 01:07:34 +000034#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 Novillode1ab262014-09-09 12:40:50 +000042
Diego Novillode1ab262014-09-09 12:40:50 +000043using namespace llvm;
Eugene Zelenkoe78d1312017-03-03 01:07:34 +000044using namespace sampleprof;
Diego Novillode1ab262014-09-09 12:40:50 +000045
Adrian Prantl5f8f34e42018-05-01 15:54:18 +000046/// Dump the function profile for \p FName.
Diego Novillode1ab262014-09-09 12:40:50 +000047///
Diego Novillode1ab262014-09-09 12:40:50 +000048/// \param FName Name of the function to print.
Diego Novillod5336ae2014-11-01 00:56:55 +000049/// \param OS Stream to emit the output to.
50void SampleProfileReader::dumpFunctionProfile(StringRef FName,
51 raw_ostream &OS) {
Diego Novillo8e415a82015-11-13 20:24:28 +000052 OS << "Function: " << FName << ": " << Profiles[FName];
Diego Novillode1ab262014-09-09 12:40:50 +000053}
54
Adrian Prantl5f8f34e42018-05-01 15:54:18 +000055/// Dump all the function profiles found on stream \p OS.
Diego Novillod5336ae2014-11-01 00:56:55 +000056void SampleProfileReader::dump(raw_ostream &OS) {
57 for (const auto &I : Profiles)
58 dumpFunctionProfile(I.getKey(), OS);
Diego Novillode1ab262014-09-09 12:40:50 +000059}
60
Adrian Prantl5f8f34e42018-05-01 15:54:18 +000061/// Parse \p Input as function head.
Dehao Chen67226882015-09-30 00:42:46 +000062///
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.
68static bool ParseHead(const StringRef &Input, StringRef &FName,
Diego Novillo38be3332015-10-15 16:36:21 +000069 uint64_t &NumSamples, uint64_t &NumHeadSamples) {
Dehao Chen67226882015-09-30 00:42:46 +000070 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 Prantl5f8f34e42018-05-01 15:54:18 +000082/// Returns true if line offset \p L is legal (only has 16 bits).
Dehao Chen57d1dda2016-03-03 18:09:32 +000083static bool isOffsetLegal(unsigned L) { return (L & 0xffff) == L; }
Dehao Chen10042412015-10-21 01:22:27 +000084
Adrian Prantl5f8f34e42018-05-01 15:54:18 +000085/// Parse \p Input as line sample.
Dehao Chen67226882015-09-30 00:42:46 +000086///
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 Novillo38be3332015-10-15 16:36:21 +000096static 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 Chen67226882015-09-30 00:42:46 +0000100 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 Chen10042412015-10-21 01:22:27 +0000109 if (Loc.getAsInteger(10, LineOffset) || !isOffsetLegal(LineOffset))
Dehao Chen67226882015-09-30 00:42:46 +0000110 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 Mi984ab0f2018-03-07 16:45:33 +0000130 // 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 Chen67226882015-09-30 00:42:46 +0000142 while (n3 != StringRef::npos) {
143 n3 += Rest.substr(n3).find_first_not_of(' ');
144 Rest = Rest.substr(n3);
Wei Mi984ab0f2018-03-07 16:45:33 +0000145 n3 = Rest.find_first_of(':');
146 if (n3 == StringRef::npos || n3 == 0)
Dehao Chen67226882015-09-30 00:42:46 +0000147 return false;
Wei Mi984ab0f2018-03-07 16:45:33 +0000148
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 Chen67226882015-09-30 00:42:46 +0000176 }
177 } else {
178 IsCallsite = true;
Diego Novillo38be3332015-10-15 16:36:21 +0000179 size_t n3 = Rest.find_last_of(':');
Dehao Chen67226882015-09-30 00:42:46 +0000180 CalleeName = Rest.substr(0, n3);
181 if (Rest.substr(n3 + 1).getAsInteger(10, NumSamples))
182 return false;
183 }
184 return true;
185}
186
Adrian Prantl5f8f34e42018-05-01 15:54:18 +0000187/// Load samples from a text file.
Diego Novillode1ab262014-09-09 12:40:50 +0000188///
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 Novilloc572e922014-10-30 18:00:06 +0000193std::error_code SampleProfileReaderText::read() {
194 line_iterator LineIt(*Buffer, /*SkipBlanks=*/true, '#');
Nathan Slingerland48dd0802015-12-16 21:45:43 +0000195 sampleprof_error Result = sampleprof_error::success;
Diego Novillode1ab262014-09-09 12:40:50 +0000196
Diego Novilloaae1ed82015-10-08 19:40:37 +0000197 InlineCallStack InlineStack;
Dehao Chen67226882015-09-30 00:42:46 +0000198
199 for (; !LineIt.is_at_eof(); ++LineIt) {
200 if ((*LineIt)[(*LineIt).find_first_not_of(' ')] == '#')
201 continue;
Diego Novillode1ab262014-09-09 12:40:50 +0000202 // 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 Chen67226882015-09-30 00:42:46 +0000214 if ((*LineIt)[0] != ' ') {
Diego Novillo38be3332015-10-15 16:36:21 +0000215 uint64_t NumSamples, NumHeadSamples;
Dehao Chen67226882015-09-30 00:42:46 +0000216 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 Chen57d1dda2016-03-03 18:09:32 +0000224 FProfile.setName(FName);
Nathan Slingerland48dd0802015-12-16 21:45:43 +0000225 MergeResult(Result, FProfile.addTotalSamples(NumSamples));
226 MergeResult(Result, FProfile.addHeadSamples(NumHeadSamples));
Dehao Chen67226882015-09-30 00:42:46 +0000227 InlineStack.clear();
228 InlineStack.push_back(&FProfile);
229 } else {
Diego Novillo38be3332015-10-15 16:36:21 +0000230 uint64_t NumSamples;
Dehao Chen67226882015-09-30 00:42:46 +0000231 StringRef FName;
Diego Novillo38be3332015-10-15 16:36:21 +0000232 DenseMap<StringRef, uint64_t> TargetCountMap;
Dehao Chen67226882015-09-30 00:42:46 +0000233 bool IsCallsite;
Diego Novillo38be3332015-10-15 16:36:21 +0000234 uint32_t Depth, LineOffset, Discriminator;
Dehao Chen67226882015-09-30 00:42:46 +0000235 if (!ParseLine(*LineIt, IsCallsite, Depth, NumSamples, LineOffset,
236 Discriminator, FName, TargetCountMap)) {
Diego Novillo3376a782015-09-17 00:17:24 +0000237 reportError(LineIt.line_number(),
238 "Expected 'NUM[.NUM]: NUM[ mangled_name:NUM]*', found " +
239 *LineIt);
Diego Novilloc572e922014-10-30 18:00:06 +0000240 return sampleprof_error::malformed;
Diego Novillode1ab262014-09-09 12:40:50 +0000241 }
Dehao Chen67226882015-09-30 00:42:46 +0000242 if (IsCallsite) {
243 while (InlineStack.size() > Depth) {
244 InlineStack.pop_back();
Diego Novilloc572e922014-10-30 18:00:06 +0000245 }
Dehao Chen67226882015-09-30 00:42:46 +0000246 FunctionSamples &FSamples = InlineStack.back()->functionSamplesAt(
Dehao Chen2c7ca9b2017-04-13 19:52:10 +0000247 LineLocation(LineOffset, Discriminator))[FName];
Dehao Chen57d1dda2016-03-03 18:09:32 +0000248 FSamples.setName(FName);
Nathan Slingerland48dd0802015-12-16 21:45:43 +0000249 MergeResult(Result, FSamples.addTotalSamples(NumSamples));
Dehao Chen67226882015-09-30 00:42:46 +0000250 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 Slingerland48dd0802015-12-16 21:45:43 +0000257 MergeResult(Result, FProfile.addCalledTargetSamples(
258 LineOffset, Discriminator, name_count.first,
259 name_count.second));
Dehao Chen67226882015-09-30 00:42:46 +0000260 }
Nathan Slingerland48dd0802015-12-16 21:45:43 +0000261 MergeResult(Result, FProfile.addBodySamples(LineOffset, Discriminator,
262 NumSamples));
Diego Novilloc572e922014-10-30 18:00:06 +0000263 }
Diego Novillode1ab262014-09-09 12:40:50 +0000264 }
265 }
Easwaran Raman40ee23d2016-02-19 03:15:33 +0000266 if (Result == sampleprof_error::success)
267 computeSummary();
Diego Novillode1ab262014-09-09 12:40:50 +0000268
Nathan Slingerland48dd0802015-12-16 21:45:43 +0000269 return Result;
Diego Novillode1ab262014-09-09 12:40:50 +0000270}
271
Nathan Slingerland4f823662015-11-13 03:47:58 +0000272bool 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 Novillod5336ae2014-11-01 00:56:55 +0000288template <typename T> ErrorOr<T> SampleProfileReaderBinary::readNumber() {
Diego Novilloc572e922014-10-30 18:00:06 +0000289 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 Novillo3376a782015-09-17 00:17:24 +0000301 reportError(0, EC.message());
Diego Novilloc572e922014-10-30 18:00:06 +0000302 return EC;
303 }
304
305 Data += NumBytesRead;
306 return static_cast<T>(Val);
307}
308
309ErrorOr<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 Novillo3376a782015-09-17 00:17:24 +0000314 reportError(0, EC.message());
Diego Novilloc572e922014-10-30 18:00:06 +0000315 return EC;
316 }
317
318 Data += Str.size() + 1;
319 return Str;
320}
321
Wei Mia0c08572018-06-11 22:40:43 +0000322template <typename T>
Wei Mi6a143252018-09-14 20:52:59 +0000323ErrorOr<T> SampleProfileReaderBinary::readUnencodedNumber() {
324 std::error_code EC;
325
326 if (Data + sizeof(T) > End) {
327 EC = sampleprof_error::truncated;
328 reportError(0, EC.message());
329 return EC;
330 }
331
332 using namespace support;
333 T Val = endian::readNext<T, little, unaligned>(Data);
334 return Val;
335}
336
337template <typename T>
Wei Mia0c08572018-06-11 22:40:43 +0000338inline ErrorOr<uint32_t> SampleProfileReaderBinary::readStringIndex(T &Table) {
Diego Novillo760c5a82015-10-13 22:48:46 +0000339 std::error_code EC;
Diego Novillo38be3332015-10-15 16:36:21 +0000340 auto Idx = readNumber<uint32_t>();
Diego Novillo760c5a82015-10-13 22:48:46 +0000341 if (std::error_code EC = Idx.getError())
342 return EC;
Wei Mia0c08572018-06-11 22:40:43 +0000343 if (*Idx >= Table.size())
Diego Novillo760c5a82015-10-13 22:48:46 +0000344 return sampleprof_error::truncated_name_table;
Wei Mia0c08572018-06-11 22:40:43 +0000345 return *Idx;
346}
347
348ErrorOr<StringRef> SampleProfileReaderRawBinary::readStringFromTable() {
349 auto Idx = readStringIndex(NameTable);
350 if (std::error_code EC = Idx.getError())
351 return EC;
352
Diego Novillo760c5a82015-10-13 22:48:46 +0000353 return NameTable[*Idx];
354}
355
Wei Mia0c08572018-06-11 22:40:43 +0000356ErrorOr<StringRef> SampleProfileReaderCompactBinary::readStringFromTable() {
357 auto Idx = readStringIndex(NameTable);
358 if (std::error_code EC = Idx.getError())
359 return EC;
360
361 return StringRef(NameTable[*Idx]);
362}
363
Diego Novilloa7f1e8e2015-10-09 17:54:24 +0000364std::error_code
365SampleProfileReaderBinary::readProfile(FunctionSamples &FProfile) {
Diego Novillob93483d2015-10-16 18:54:35 +0000366 auto NumSamples = readNumber<uint64_t>();
367 if (std::error_code EC = NumSamples.getError())
Diego Novilloa7f1e8e2015-10-09 17:54:24 +0000368 return EC;
Diego Novillob93483d2015-10-16 18:54:35 +0000369 FProfile.addTotalSamples(*NumSamples);
Diego Novilloa7f1e8e2015-10-09 17:54:24 +0000370
371 // Read the samples in the body.
Diego Novillo38be3332015-10-15 16:36:21 +0000372 auto NumRecords = readNumber<uint32_t>();
Diego Novilloa7f1e8e2015-10-09 17:54:24 +0000373 if (std::error_code EC = NumRecords.getError())
374 return EC;
375
Diego Novillo38be3332015-10-15 16:36:21 +0000376 for (uint32_t I = 0; I < *NumRecords; ++I) {
Diego Novilloa7f1e8e2015-10-09 17:54:24 +0000377 auto LineOffset = readNumber<uint64_t>();
378 if (std::error_code EC = LineOffset.getError())
379 return EC;
380
Dehao Chen10042412015-10-21 01:22:27 +0000381 if (!isOffsetLegal(*LineOffset)) {
382 return std::error_code();
383 }
384
Diego Novilloa7f1e8e2015-10-09 17:54:24 +0000385 auto Discriminator = readNumber<uint64_t>();
386 if (std::error_code EC = Discriminator.getError())
387 return EC;
388
389 auto NumSamples = readNumber<uint64_t>();
390 if (std::error_code EC = NumSamples.getError())
391 return EC;
392
Diego Novillo38be3332015-10-15 16:36:21 +0000393 auto NumCalls = readNumber<uint32_t>();
Diego Novilloa7f1e8e2015-10-09 17:54:24 +0000394 if (std::error_code EC = NumCalls.getError())
395 return EC;
396
Diego Novillo38be3332015-10-15 16:36:21 +0000397 for (uint32_t J = 0; J < *NumCalls; ++J) {
Diego Novillo760c5a82015-10-13 22:48:46 +0000398 auto CalledFunction(readStringFromTable());
Diego Novilloa7f1e8e2015-10-09 17:54:24 +0000399 if (std::error_code EC = CalledFunction.getError())
400 return EC;
401
402 auto CalledFunctionSamples = readNumber<uint64_t>();
403 if (std::error_code EC = CalledFunctionSamples.getError())
404 return EC;
405
406 FProfile.addCalledTargetSamples(*LineOffset, *Discriminator,
407 *CalledFunction, *CalledFunctionSamples);
408 }
409
410 FProfile.addBodySamples(*LineOffset, *Discriminator, *NumSamples);
411 }
412
413 // Read all the samples for inlined function calls.
Diego Novillo38be3332015-10-15 16:36:21 +0000414 auto NumCallsites = readNumber<uint32_t>();
Diego Novilloa7f1e8e2015-10-09 17:54:24 +0000415 if (std::error_code EC = NumCallsites.getError())
416 return EC;
417
Diego Novillo38be3332015-10-15 16:36:21 +0000418 for (uint32_t J = 0; J < *NumCallsites; ++J) {
Diego Novilloa7f1e8e2015-10-09 17:54:24 +0000419 auto LineOffset = readNumber<uint64_t>();
420 if (std::error_code EC = LineOffset.getError())
421 return EC;
422
423 auto Discriminator = readNumber<uint64_t>();
424 if (std::error_code EC = Discriminator.getError())
425 return EC;
426
Diego Novillo760c5a82015-10-13 22:48:46 +0000427 auto FName(readStringFromTable());
Diego Novilloa7f1e8e2015-10-09 17:54:24 +0000428 if (std::error_code EC = FName.getError())
429 return EC;
430
Dehao Chen2c7ca9b2017-04-13 19:52:10 +0000431 FunctionSamples &CalleeProfile = FProfile.functionSamplesAt(
432 LineLocation(*LineOffset, *Discriminator))[*FName];
Dehao Chen57d1dda2016-03-03 18:09:32 +0000433 CalleeProfile.setName(*FName);
Diego Novilloa7f1e8e2015-10-09 17:54:24 +0000434 if (std::error_code EC = readProfile(CalleeProfile))
435 return EC;
436 }
437
438 return sampleprof_error::success;
439}
440
Wei Mi6a143252018-09-14 20:52:59 +0000441std::error_code SampleProfileReaderBinary::readFuncProfile() {
442 auto NumHeadSamples = readNumber<uint64_t>();
443 if (std::error_code EC = NumHeadSamples.getError())
444 return EC;
445
446 auto FName(readStringFromTable());
447 if (std::error_code EC = FName.getError())
448 return EC;
449
450 Profiles[*FName] = FunctionSamples();
451 FunctionSamples &FProfile = Profiles[*FName];
452 FProfile.setName(*FName);
453
454 FProfile.addHeadSamples(*NumHeadSamples);
455
456 if (std::error_code EC = readProfile(FProfile))
457 return EC;
458 return sampleprof_error::success;
459}
460
Diego Novilloc572e922014-10-30 18:00:06 +0000461std::error_code SampleProfileReaderBinary::read() {
462 while (!at_eof()) {
Wei Mi6a143252018-09-14 20:52:59 +0000463 if (std::error_code EC = readFuncProfile())
Diego Novilloc572e922014-10-30 18:00:06 +0000464 return EC;
Diego Novilloc572e922014-10-30 18:00:06 +0000465 }
466
467 return sampleprof_error::success;
468}
469
Wei Mi6a143252018-09-14 20:52:59 +0000470std::error_code SampleProfileReaderCompactBinary::read() {
471 for (auto Name : FuncsToUse) {
472 auto GUID = std::to_string(MD5Hash(Name));
473 auto iter = FuncOffsetTable.find(StringRef(GUID));
474 if (iter == FuncOffsetTable.end())
475 continue;
476 const uint8_t *SavedData = Data;
477 Data = reinterpret_cast<const uint8_t *>(Buffer->getBufferStart()) +
478 iter->second;
479 if (std::error_code EC = readFuncProfile())
480 return EC;
481 Data = SavedData;
482 }
483 return sampleprof_error::success;
484}
485
Wei Mia0c08572018-06-11 22:40:43 +0000486std::error_code SampleProfileReaderRawBinary::verifySPMagic(uint64_t Magic) {
487 if (Magic == SPMagic())
488 return sampleprof_error::success;
489 return sampleprof_error::bad_magic;
490}
491
492std::error_code
493SampleProfileReaderCompactBinary::verifySPMagic(uint64_t Magic) {
494 if (Magic == SPMagic(SPF_Compact_Binary))
495 return sampleprof_error::success;
496 return sampleprof_error::bad_magic;
497}
498
499std::error_code SampleProfileReaderRawBinary::readNameTable() {
500 auto Size = readNumber<uint32_t>();
501 if (std::error_code EC = Size.getError())
502 return EC;
503 NameTable.reserve(*Size);
504 for (uint32_t I = 0; I < *Size; ++I) {
505 auto Name(readString());
506 if (std::error_code EC = Name.getError())
507 return EC;
508 NameTable.push_back(*Name);
509 }
510
511 return sampleprof_error::success;
512}
513
514std::error_code SampleProfileReaderCompactBinary::readNameTable() {
515 auto Size = readNumber<uint64_t>();
516 if (std::error_code EC = Size.getError())
517 return EC;
518 NameTable.reserve(*Size);
519 for (uint32_t I = 0; I < *Size; ++I) {
520 auto FID = readNumber<uint64_t>();
521 if (std::error_code EC = FID.getError())
522 return EC;
523 NameTable.push_back(std::to_string(*FID));
524 }
525 return sampleprof_error::success;
526}
527
Diego Novilloc572e922014-10-30 18:00:06 +0000528std::error_code SampleProfileReaderBinary::readHeader() {
529 Data = reinterpret_cast<const uint8_t *>(Buffer->getBufferStart());
530 End = Data + Buffer->getBufferSize();
531
532 // Read and check the magic identifier.
533 auto Magic = readNumber<uint64_t>();
534 if (std::error_code EC = Magic.getError())
535 return EC;
Wei Mia0c08572018-06-11 22:40:43 +0000536 else if (std::error_code EC = verifySPMagic(*Magic))
Wei Mic6b96c82018-06-11 23:09:04 +0000537 return EC;
Diego Novilloc572e922014-10-30 18:00:06 +0000538
539 // Read the version number.
540 auto Version = readNumber<uint64_t>();
541 if (std::error_code EC = Version.getError())
542 return EC;
543 else if (*Version != SPVersion())
544 return sampleprof_error::unsupported_version;
545
Easwaran Raman40ee23d2016-02-19 03:15:33 +0000546 if (std::error_code EC = readSummary())
547 return EC;
548
Wei Mia0c08572018-06-11 22:40:43 +0000549 if (std::error_code EC = readNameTable())
Diego Novillo760c5a82015-10-13 22:48:46 +0000550 return EC;
Diego Novilloc572e922014-10-30 18:00:06 +0000551 return sampleprof_error::success;
552}
553
Wei Mi6a143252018-09-14 20:52:59 +0000554std::error_code SampleProfileReaderCompactBinary::readHeader() {
555 SampleProfileReaderBinary::readHeader();
556 if (std::error_code EC = readFuncOffsetTable())
557 return EC;
558 return sampleprof_error::success;
559}
560
561std::error_code SampleProfileReaderCompactBinary::readFuncOffsetTable() {
562 auto TableOffset = readUnencodedNumber<uint64_t>();
563 if (std::error_code EC = TableOffset.getError())
564 return EC;
565
566 const uint8_t *SavedData = Data;
567 const uint8_t *TableStart =
568 reinterpret_cast<const uint8_t *>(Buffer->getBufferStart()) +
569 *TableOffset;
570 Data = TableStart;
571
572 auto Size = readNumber<uint64_t>();
573 if (std::error_code EC = Size.getError())
574 return EC;
575
576 FuncOffsetTable.reserve(*Size);
577 for (uint32_t I = 0; I < *Size; ++I) {
578 auto FName(readStringFromTable());
579 if (std::error_code EC = FName.getError())
580 return EC;
581
582 auto Offset = readNumber<uint64_t>();
583 if (std::error_code EC = Offset.getError())
584 return EC;
585
586 FuncOffsetTable[*FName] = *Offset;
587 }
588 End = TableStart;
589 Data = SavedData;
590 return sampleprof_error::success;
591}
592
593void SampleProfileReaderCompactBinary::collectFuncsToUse(const Module &M) {
594 FuncsToUse.clear();
595 for (auto &F : M) {
Than McIntosh9f96f1f2019-03-14 13:56:49 +0000596 StringRef CanonName = FunctionSamples::getCanonicalFnName(F);
597 FuncsToUse.insert(CanonName);
Wei Mi6a143252018-09-14 20:52:59 +0000598 }
599}
600
Easwaran Raman40ee23d2016-02-19 03:15:33 +0000601std::error_code SampleProfileReaderBinary::readSummaryEntry(
602 std::vector<ProfileSummaryEntry> &Entries) {
603 auto Cutoff = readNumber<uint64_t>();
604 if (std::error_code EC = Cutoff.getError())
605 return EC;
606
607 auto MinBlockCount = readNumber<uint64_t>();
608 if (std::error_code EC = MinBlockCount.getError())
609 return EC;
610
611 auto NumBlocks = readNumber<uint64_t>();
612 if (std::error_code EC = NumBlocks.getError())
613 return EC;
614
615 Entries.emplace_back(*Cutoff, *MinBlockCount, *NumBlocks);
616 return sampleprof_error::success;
617}
618
619std::error_code SampleProfileReaderBinary::readSummary() {
620 auto TotalCount = readNumber<uint64_t>();
621 if (std::error_code EC = TotalCount.getError())
622 return EC;
623
624 auto MaxBlockCount = readNumber<uint64_t>();
625 if (std::error_code EC = MaxBlockCount.getError())
626 return EC;
627
628 auto MaxFunctionCount = readNumber<uint64_t>();
629 if (std::error_code EC = MaxFunctionCount.getError())
630 return EC;
631
632 auto NumBlocks = readNumber<uint64_t>();
633 if (std::error_code EC = NumBlocks.getError())
634 return EC;
635
636 auto NumFunctions = readNumber<uint64_t>();
637 if (std::error_code EC = NumFunctions.getError())
638 return EC;
639
640 auto NumSummaryEntries = readNumber<uint64_t>();
641 if (std::error_code EC = NumSummaryEntries.getError())
642 return EC;
643
644 std::vector<ProfileSummaryEntry> Entries;
645 for (unsigned i = 0; i < *NumSummaryEntries; i++) {
646 std::error_code EC = readSummaryEntry(Entries);
647 if (EC != sampleprof_error::success)
648 return EC;
649 }
Easwaran Raman7cefdb82016-05-19 21:53:28 +0000650 Summary = llvm::make_unique<ProfileSummary>(
651 ProfileSummary::PSK_Sample, Entries, *TotalCount, *MaxBlockCount, 0,
652 *MaxFunctionCount, *NumBlocks, *NumFunctions);
Easwaran Raman40ee23d2016-02-19 03:15:33 +0000653
654 return sampleprof_error::success;
655}
656
Wei Mia0c08572018-06-11 22:40:43 +0000657bool SampleProfileReaderRawBinary::hasFormat(const MemoryBuffer &Buffer) {
Diego Novilloc572e922014-10-30 18:00:06 +0000658 const uint8_t *Data =
659 reinterpret_cast<const uint8_t *>(Buffer.getBufferStart());
660 uint64_t Magic = decodeULEB128(Data);
661 return Magic == SPMagic();
662}
663
Wei Mia0c08572018-06-11 22:40:43 +0000664bool SampleProfileReaderCompactBinary::hasFormat(const MemoryBuffer &Buffer) {
665 const uint8_t *Data =
666 reinterpret_cast<const uint8_t *>(Buffer.getBufferStart());
667 uint64_t Magic = decodeULEB128(Data);
668 return Magic == SPMagic(SPF_Compact_Binary);
669}
670
Diego Novillo3376a782015-09-17 00:17:24 +0000671std::error_code SampleProfileReaderGCC::skipNextWord() {
672 uint32_t dummy;
673 if (!GcovBuffer.readInt(dummy))
674 return sampleprof_error::truncated;
675 return sampleprof_error::success;
676}
677
678template <typename T> ErrorOr<T> SampleProfileReaderGCC::readNumber() {
679 if (sizeof(T) <= sizeof(uint32_t)) {
680 uint32_t Val;
681 if (GcovBuffer.readInt(Val) && Val <= std::numeric_limits<T>::max())
682 return static_cast<T>(Val);
683 } else if (sizeof(T) <= sizeof(uint64_t)) {
684 uint64_t Val;
685 if (GcovBuffer.readInt64(Val) && Val <= std::numeric_limits<T>::max())
686 return static_cast<T>(Val);
687 }
688
689 std::error_code EC = sampleprof_error::malformed;
690 reportError(0, EC.message());
691 return EC;
692}
693
694ErrorOr<StringRef> SampleProfileReaderGCC::readString() {
695 StringRef Str;
696 if (!GcovBuffer.readString(Str))
697 return sampleprof_error::truncated;
698 return Str;
699}
700
701std::error_code SampleProfileReaderGCC::readHeader() {
702 // Read the magic identifier.
703 if (!GcovBuffer.readGCDAFormat())
704 return sampleprof_error::unrecognized_format;
705
706 // Read the version number. Note - the GCC reader does not validate this
707 // version, but the profile creator generates v704.
708 GCOV::GCOVVersion version;
709 if (!GcovBuffer.readGCOVVersion(version))
710 return sampleprof_error::unrecognized_format;
711
712 if (version != GCOV::V704)
713 return sampleprof_error::unsupported_version;
714
715 // Skip the empty integer.
716 if (std::error_code EC = skipNextWord())
717 return EC;
718
719 return sampleprof_error::success;
720}
721
722std::error_code SampleProfileReaderGCC::readSectionTag(uint32_t Expected) {
723 uint32_t Tag;
724 if (!GcovBuffer.readInt(Tag))
725 return sampleprof_error::truncated;
726
727 if (Tag != Expected)
728 return sampleprof_error::malformed;
729
730 if (std::error_code EC = skipNextWord())
731 return EC;
732
733 return sampleprof_error::success;
734}
735
736std::error_code SampleProfileReaderGCC::readNameTable() {
737 if (std::error_code EC = readSectionTag(GCOVTagAFDOFileNames))
738 return EC;
739
740 uint32_t Size;
741 if (!GcovBuffer.readInt(Size))
742 return sampleprof_error::truncated;
743
744 for (uint32_t I = 0; I < Size; ++I) {
745 StringRef Str;
746 if (!GcovBuffer.readString(Str))
747 return sampleprof_error::truncated;
748 Names.push_back(Str);
749 }
750
751 return sampleprof_error::success;
752}
753
754std::error_code SampleProfileReaderGCC::readFunctionProfiles() {
755 if (std::error_code EC = readSectionTag(GCOVTagAFDOFunction))
756 return EC;
757
758 uint32_t NumFunctions;
759 if (!GcovBuffer.readInt(NumFunctions))
760 return sampleprof_error::truncated;
761
Diego Novilloaae1ed82015-10-08 19:40:37 +0000762 InlineCallStack Stack;
Diego Novillo3376a782015-09-17 00:17:24 +0000763 for (uint32_t I = 0; I < NumFunctions; ++I)
Diego Novilloaae1ed82015-10-08 19:40:37 +0000764 if (std::error_code EC = readOneFunctionProfile(Stack, true, 0))
Diego Novillo3376a782015-09-17 00:17:24 +0000765 return EC;
766
Easwaran Raman40ee23d2016-02-19 03:15:33 +0000767 computeSummary();
Diego Novillo3376a782015-09-17 00:17:24 +0000768 return sampleprof_error::success;
769}
770
Diego Novilloaae1ed82015-10-08 19:40:37 +0000771std::error_code SampleProfileReaderGCC::readOneFunctionProfile(
772 const InlineCallStack &InlineStack, bool Update, uint32_t Offset) {
Diego Novillo3376a782015-09-17 00:17:24 +0000773 uint64_t HeadCount = 0;
Diego Novilloaae1ed82015-10-08 19:40:37 +0000774 if (InlineStack.size() == 0)
Diego Novillo3376a782015-09-17 00:17:24 +0000775 if (!GcovBuffer.readInt64(HeadCount))
776 return sampleprof_error::truncated;
777
778 uint32_t NameIdx;
779 if (!GcovBuffer.readInt(NameIdx))
780 return sampleprof_error::truncated;
781
782 StringRef Name(Names[NameIdx]);
783
784 uint32_t NumPosCounts;
785 if (!GcovBuffer.readInt(NumPosCounts))
786 return sampleprof_error::truncated;
787
Diego Novilloaae1ed82015-10-08 19:40:37 +0000788 uint32_t NumCallsites;
789 if (!GcovBuffer.readInt(NumCallsites))
Diego Novillo3376a782015-09-17 00:17:24 +0000790 return sampleprof_error::truncated;
791
Diego Novilloaae1ed82015-10-08 19:40:37 +0000792 FunctionSamples *FProfile = nullptr;
793 if (InlineStack.size() == 0) {
794 // If this is a top function that we have already processed, do not
795 // update its profile again. This happens in the presence of
796 // function aliases. Since these aliases share the same function
797 // body, there will be identical replicated profiles for the
798 // original function. In this case, we simply not bother updating
799 // the profile of the original function.
800 FProfile = &Profiles[Name];
801 FProfile->addHeadSamples(HeadCount);
802 if (FProfile->getTotalSamples() > 0)
Diego Novillo3376a782015-09-17 00:17:24 +0000803 Update = false;
Diego Novilloaae1ed82015-10-08 19:40:37 +0000804 } else {
805 // Otherwise, we are reading an inlined instance. The top of the
806 // inline stack contains the profile of the caller. Insert this
807 // callee in the caller's CallsiteMap.
808 FunctionSamples *CallerProfile = InlineStack.front();
809 uint32_t LineOffset = Offset >> 16;
810 uint32_t Discriminator = Offset & 0xffff;
811 FProfile = &CallerProfile->functionSamplesAt(
Dehao Chen2c7ca9b2017-04-13 19:52:10 +0000812 LineLocation(LineOffset, Discriminator))[Name];
Diego Novillo3376a782015-09-17 00:17:24 +0000813 }
Dehao Chen57d1dda2016-03-03 18:09:32 +0000814 FProfile->setName(Name);
Diego Novillo3376a782015-09-17 00:17:24 +0000815
816 for (uint32_t I = 0; I < NumPosCounts; ++I) {
817 uint32_t Offset;
818 if (!GcovBuffer.readInt(Offset))
819 return sampleprof_error::truncated;
820
821 uint32_t NumTargets;
822 if (!GcovBuffer.readInt(NumTargets))
823 return sampleprof_error::truncated;
824
825 uint64_t Count;
826 if (!GcovBuffer.readInt64(Count))
827 return sampleprof_error::truncated;
828
Diego Novilloaae1ed82015-10-08 19:40:37 +0000829 // The line location is encoded in the offset as:
830 // high 16 bits: line offset to the start of the function.
831 // low 16 bits: discriminator.
832 uint32_t LineOffset = Offset >> 16;
833 uint32_t Discriminator = Offset & 0xffff;
Diego Novillo3376a782015-09-17 00:17:24 +0000834
Diego Novilloaae1ed82015-10-08 19:40:37 +0000835 InlineCallStack NewStack;
836 NewStack.push_back(FProfile);
837 NewStack.insert(NewStack.end(), InlineStack.begin(), InlineStack.end());
838 if (Update) {
839 // Walk up the inline stack, adding the samples on this line to
840 // the total sample count of the callers in the chain.
841 for (auto CallerProfile : NewStack)
842 CallerProfile->addTotalSamples(Count);
843
844 // Update the body samples for the current profile.
845 FProfile->addBodySamples(LineOffset, Discriminator, Count);
846 }
847
848 // Process the list of functions called at an indirect call site.
849 // These are all the targets that a function pointer (or virtual
850 // function) resolved at runtime.
Diego Novillo3376a782015-09-17 00:17:24 +0000851 for (uint32_t J = 0; J < NumTargets; J++) {
852 uint32_t HistVal;
853 if (!GcovBuffer.readInt(HistVal))
854 return sampleprof_error::truncated;
855
856 if (HistVal != HIST_TYPE_INDIR_CALL_TOPN)
857 return sampleprof_error::malformed;
858
859 uint64_t TargetIdx;
860 if (!GcovBuffer.readInt64(TargetIdx))
861 return sampleprof_error::truncated;
862 StringRef TargetName(Names[TargetIdx]);
863
864 uint64_t TargetCount;
865 if (!GcovBuffer.readInt64(TargetCount))
866 return sampleprof_error::truncated;
867
Dehao Chen920677a2017-02-22 17:27:21 +0000868 if (Update)
869 FProfile->addCalledTargetSamples(LineOffset, Discriminator,
870 TargetName, TargetCount);
Diego Novillo3376a782015-09-17 00:17:24 +0000871 }
872 }
873
Diego Novilloaae1ed82015-10-08 19:40:37 +0000874 // Process all the inlined callers into the current function. These
875 // are all the callsites that were inlined into this function.
876 for (uint32_t I = 0; I < NumCallsites; I++) {
Diego Novillo3376a782015-09-17 00:17:24 +0000877 // The offset is encoded as:
878 // high 16 bits: line offset to the start of the function.
879 // low 16 bits: discriminator.
880 uint32_t Offset;
881 if (!GcovBuffer.readInt(Offset))
882 return sampleprof_error::truncated;
Diego Novilloaae1ed82015-10-08 19:40:37 +0000883 InlineCallStack NewStack;
884 NewStack.push_back(FProfile);
885 NewStack.insert(NewStack.end(), InlineStack.begin(), InlineStack.end());
886 if (std::error_code EC = readOneFunctionProfile(NewStack, Update, Offset))
Diego Novillo3376a782015-09-17 00:17:24 +0000887 return EC;
888 }
889
890 return sampleprof_error::success;
891}
892
Adrian Prantl5f8f34e42018-05-01 15:54:18 +0000893/// Read a GCC AutoFDO profile.
Diego Novillo3376a782015-09-17 00:17:24 +0000894///
895/// This format is generated by the Linux Perf conversion tool at
896/// https://github.com/google/autofdo.
897std::error_code SampleProfileReaderGCC::read() {
898 // Read the string table.
899 if (std::error_code EC = readNameTable())
900 return EC;
901
902 // Read the source profile.
903 if (std::error_code EC = readFunctionProfiles())
904 return EC;
905
Diego Novillo3376a782015-09-17 00:17:24 +0000906 return sampleprof_error::success;
907}
908
909bool SampleProfileReaderGCC::hasFormat(const MemoryBuffer &Buffer) {
910 StringRef Magic(reinterpret_cast<const char *>(Buffer.getBufferStart()));
911 return Magic == "adcg*704";
912}
913
Richard Smith28436352018-10-10 21:31:01 +0000914std::error_code SampleProfileReaderItaniumRemapper::read() {
915 // If the underlying data is in compact format, we can't remap it because
916 // we don't know what the original function names were.
917 if (getFormat() == SPF_Compact_Binary) {
918 Ctx.diagnose(DiagnosticInfoSampleProfile(
919 Buffer->getBufferIdentifier(),
920 "Profile data remapping cannot be applied to profile data "
921 "in compact format (original mangled names are not available).",
922 DS_Warning));
923 return sampleprof_error::success;
924 }
925
926 if (Error E = Remappings.read(*Buffer)) {
927 handleAllErrors(
928 std::move(E), [&](const SymbolRemappingParseError &ParseError) {
929 reportError(ParseError.getLineNum(), ParseError.getMessage());
930 });
931 return sampleprof_error::malformed;
932 }
933
934 for (auto &Sample : getProfiles())
935 if (auto Key = Remappings.insert(Sample.first()))
936 SampleMap.insert({Key, &Sample.second});
937
938 return sampleprof_error::success;
939}
940
941FunctionSamples *
942SampleProfileReaderItaniumRemapper::getSamplesFor(StringRef Fname) {
943 if (auto Key = Remappings.lookup(Fname))
944 return SampleMap.lookup(Key);
945 return SampleProfileReader::getSamplesFor(Fname);
946}
947
Adrian Prantl5f8f34e42018-05-01 15:54:18 +0000948/// Prepare a memory buffer for the contents of \p Filename.
Diego Novillode1ab262014-09-09 12:40:50 +0000949///
Diego Novilloc572e922014-10-30 18:00:06 +0000950/// \returns an error code indicating the status of the buffer.
Diego Novillofcd55602014-11-03 00:51:45 +0000951static ErrorOr<std::unique_ptr<MemoryBuffer>>
Benjamin Kramer0da23a22016-05-29 10:31:00 +0000952setupMemoryBuffer(const Twine &Filename) {
Diego Novilloc572e922014-10-30 18:00:06 +0000953 auto BufferOrErr = MemoryBuffer::getFileOrSTDIN(Filename);
954 if (std::error_code EC = BufferOrErr.getError())
955 return EC;
Diego Novillofcd55602014-11-03 00:51:45 +0000956 auto Buffer = std::move(BufferOrErr.get());
Diego Novilloc572e922014-10-30 18:00:06 +0000957
958 // Sanity check the file.
Zachary Turner260fe3e2017-12-14 22:07:03 +0000959 if (uint64_t(Buffer->getBufferSize()) > std::numeric_limits<uint32_t>::max())
Diego Novilloc572e922014-10-30 18:00:06 +0000960 return sampleprof_error::too_large;
961
Diego Novillofcd55602014-11-03 00:51:45 +0000962 return std::move(Buffer);
Diego Novilloc572e922014-10-30 18:00:06 +0000963}
964
Adrian Prantl5f8f34e42018-05-01 15:54:18 +0000965/// Create a sample profile reader based on the format of the input file.
Diego Novilloc572e922014-10-30 18:00:06 +0000966///
967/// \param Filename The file to open.
968///
Diego Novilloc572e922014-10-30 18:00:06 +0000969/// \param C The LLVM context to use to emit diagnostics.
970///
971/// \returns an error code indicating the status of the created reader.
Diego Novillofcd55602014-11-03 00:51:45 +0000972ErrorOr<std::unique_ptr<SampleProfileReader>>
Benjamin Kramer0da23a22016-05-29 10:31:00 +0000973SampleProfileReader::create(const Twine &Filename, LLVMContext &C) {
Diego Novillofcd55602014-11-03 00:51:45 +0000974 auto BufferOrError = setupMemoryBuffer(Filename);
975 if (std::error_code EC = BufferOrError.getError())
Diego Novilloc572e922014-10-30 18:00:06 +0000976 return EC;
Nathan Slingerland51abea72015-12-10 17:21:42 +0000977 return create(BufferOrError.get(), C);
978}
Diego Novilloc572e922014-10-30 18:00:06 +0000979
Richard Smith28436352018-10-10 21:31:01 +0000980/// Create a sample profile remapper from the given input, to remap the
981/// function names in the given profile data.
982///
983/// \param Filename The file to open.
984///
985/// \param C The LLVM context to use to emit diagnostics.
986///
987/// \param Underlying The underlying profile data reader to remap.
988///
989/// \returns an error code indicating the status of the created reader.
990ErrorOr<std::unique_ptr<SampleProfileReader>>
991SampleProfileReaderItaniumRemapper::create(
992 const Twine &Filename, LLVMContext &C,
993 std::unique_ptr<SampleProfileReader> Underlying) {
994 auto BufferOrError = setupMemoryBuffer(Filename);
995 if (std::error_code EC = BufferOrError.getError())
996 return EC;
997 return llvm::make_unique<SampleProfileReaderItaniumRemapper>(
998 std::move(BufferOrError.get()), C, std::move(Underlying));
999}
1000
Adrian Prantl5f8f34e42018-05-01 15:54:18 +00001001/// Create a sample profile reader based on the format of the input data.
Nathan Slingerland51abea72015-12-10 17:21:42 +00001002///
1003/// \param B The memory buffer to create the reader from (assumes ownership).
1004///
Nathan Slingerland51abea72015-12-10 17:21:42 +00001005/// \param C The LLVM context to use to emit diagnostics.
1006///
1007/// \returns an error code indicating the status of the created reader.
1008ErrorOr<std::unique_ptr<SampleProfileReader>>
1009SampleProfileReader::create(std::unique_ptr<MemoryBuffer> &B, LLVMContext &C) {
Diego Novillofcd55602014-11-03 00:51:45 +00001010 std::unique_ptr<SampleProfileReader> Reader;
Wei Mia0c08572018-06-11 22:40:43 +00001011 if (SampleProfileReaderRawBinary::hasFormat(*B))
1012 Reader.reset(new SampleProfileReaderRawBinary(std::move(B), C));
1013 else if (SampleProfileReaderCompactBinary::hasFormat(*B))
1014 Reader.reset(new SampleProfileReaderCompactBinary(std::move(B), C));
Nathan Slingerland51abea72015-12-10 17:21:42 +00001015 else if (SampleProfileReaderGCC::hasFormat(*B))
1016 Reader.reset(new SampleProfileReaderGCC(std::move(B), C));
1017 else if (SampleProfileReaderText::hasFormat(*B))
1018 Reader.reset(new SampleProfileReaderText(std::move(B), C));
Nathan Slingerland4f823662015-11-13 03:47:58 +00001019 else
1020 return sampleprof_error::unrecognized_format;
Diego Novilloc572e922014-10-30 18:00:06 +00001021
Wei Mi94d44c92018-09-06 22:03:37 +00001022 FunctionSamples::Format = Reader->getFormat();
Diego Novillofcd55602014-11-03 00:51:45 +00001023 if (std::error_code EC = Reader->readHeader())
1024 return EC;
1025
1026 return std::move(Reader);
Diego Novillode1ab262014-09-09 12:40:50 +00001027}
Easwaran Raman40ee23d2016-02-19 03:15:33 +00001028
1029// For text and GCC file formats, we compute the summary after reading the
1030// profile. Binary format has the profile summary in its header.
1031void SampleProfileReader::computeSummary() {
Easwaran Ramane5a17e32016-05-19 21:07:12 +00001032 SampleProfileSummaryBuilder Builder(ProfileSummaryBuilder::DefaultCutoffs);
Easwaran Raman40ee23d2016-02-19 03:15:33 +00001033 for (const auto &I : Profiles) {
1034 const FunctionSamples &Profile = I.second;
Easwaran Ramane5a17e32016-05-19 21:07:12 +00001035 Builder.addRecord(Profile);
Easwaran Raman40ee23d2016-02-19 03:15:33 +00001036 }
Benjamin Kramer38de59e2016-05-20 09:18:37 +00001037 Summary = Builder.getSummary();
Easwaran Raman40ee23d2016-02-19 03:15:33 +00001038}