xref: /freebsd/contrib/llvm-project/llvm/lib/ProfileData/SampleProfReader.cpp (revision a7623790fb345e6dc986dfd31df0ace115e6f2e4)
1 //===- SampleProfReader.cpp - Read LLVM sample profile data ---------------===//
2 //
3 // 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
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file implements the class that reads LLVM sample profiles. It
10 // supports three file formats: text, binary and gcov.
11 //
12 // The textual representation is useful for debugging and testing purposes. The
13 // binary representation is more compact, resulting in smaller file sizes.
14 //
15 // The gcov encoding is the one generated by GCC's AutoFDO profile creation
16 // tool (https://github.com/google/autofdo)
17 //
18 // All three encodings can be used interchangeably as an input sample profile.
19 //
20 //===----------------------------------------------------------------------===//
21 
22 #include "llvm/ProfileData/SampleProfReader.h"
23 #include "llvm/ADT/DenseMap.h"
24 #include "llvm/ADT/STLExtras.h"
25 #include "llvm/ADT/StringRef.h"
26 #include "llvm/IR/ProfileSummary.h"
27 #include "llvm/ProfileData/ProfileCommon.h"
28 #include "llvm/ProfileData/SampleProf.h"
29 #include "llvm/Support/Compression.h"
30 #include "llvm/Support/ErrorOr.h"
31 #include "llvm/Support/LEB128.h"
32 #include "llvm/Support/LineIterator.h"
33 #include "llvm/Support/MD5.h"
34 #include "llvm/Support/MemoryBuffer.h"
35 #include "llvm/Support/raw_ostream.h"
36 #include <algorithm>
37 #include <cstddef>
38 #include <cstdint>
39 #include <limits>
40 #include <memory>
41 #include <system_error>
42 #include <vector>
43 
44 using namespace llvm;
45 using namespace sampleprof;
46 
47 /// Dump the function profile for \p FName.
48 ///
49 /// \param FName Name of the function to print.
50 /// \param OS Stream to emit the output to.
51 void SampleProfileReader::dumpFunctionProfile(StringRef FName,
52                                               raw_ostream &OS) {
53   OS << "Function: " << FName << ": " << Profiles[FName];
54 }
55 
56 /// Dump all the function profiles found on stream \p OS.
57 void SampleProfileReader::dump(raw_ostream &OS) {
58   for (const auto &I : Profiles)
59     dumpFunctionProfile(I.getKey(), OS);
60 }
61 
62 /// Parse \p Input as function head.
63 ///
64 /// Parse one line of \p Input, and update function name in \p FName,
65 /// function's total sample count in \p NumSamples, function's entry
66 /// count in \p NumHeadSamples.
67 ///
68 /// \returns true if parsing is successful.
69 static bool ParseHead(const StringRef &Input, StringRef &FName,
70                       uint64_t &NumSamples, uint64_t &NumHeadSamples) {
71   if (Input[0] == ' ')
72     return false;
73   size_t n2 = Input.rfind(':');
74   size_t n1 = Input.rfind(':', n2 - 1);
75   FName = Input.substr(0, n1);
76   if (Input.substr(n1 + 1, n2 - n1 - 1).getAsInteger(10, NumSamples))
77     return false;
78   if (Input.substr(n2 + 1).getAsInteger(10, NumHeadSamples))
79     return false;
80   return true;
81 }
82 
83 /// Returns true if line offset \p L is legal (only has 16 bits).
84 static bool isOffsetLegal(unsigned L) { return (L & 0xffff) == L; }
85 
86 /// Parse \p Input as line sample.
87 ///
88 /// \param Input input line.
89 /// \param IsCallsite true if the line represents an inlined callsite.
90 /// \param Depth the depth of the inline stack.
91 /// \param NumSamples total samples of the line/inlined callsite.
92 /// \param LineOffset line offset to the start of the function.
93 /// \param Discriminator discriminator of the line.
94 /// \param TargetCountMap map from indirect call target to count.
95 ///
96 /// returns true if parsing is successful.
97 static bool ParseLine(const StringRef &Input, bool &IsCallsite, uint32_t &Depth,
98                       uint64_t &NumSamples, uint32_t &LineOffset,
99                       uint32_t &Discriminator, StringRef &CalleeName,
100                       DenseMap<StringRef, uint64_t> &TargetCountMap) {
101   for (Depth = 0; Input[Depth] == ' '; Depth++)
102     ;
103   if (Depth == 0)
104     return false;
105 
106   size_t n1 = Input.find(':');
107   StringRef Loc = Input.substr(Depth, n1 - Depth);
108   size_t n2 = Loc.find('.');
109   if (n2 == StringRef::npos) {
110     if (Loc.getAsInteger(10, LineOffset) || !isOffsetLegal(LineOffset))
111       return false;
112     Discriminator = 0;
113   } else {
114     if (Loc.substr(0, n2).getAsInteger(10, LineOffset))
115       return false;
116     if (Loc.substr(n2 + 1).getAsInteger(10, Discriminator))
117       return false;
118   }
119 
120   StringRef Rest = Input.substr(n1 + 2);
121   if (Rest[0] >= '0' && Rest[0] <= '9') {
122     IsCallsite = false;
123     size_t n3 = Rest.find(' ');
124     if (n3 == StringRef::npos) {
125       if (Rest.getAsInteger(10, NumSamples))
126         return false;
127     } else {
128       if (Rest.substr(0, n3).getAsInteger(10, NumSamples))
129         return false;
130     }
131     // Find call targets and their sample counts.
132     // Note: In some cases, there are symbols in the profile which are not
133     // mangled. To accommodate such cases, use colon + integer pairs as the
134     // anchor points.
135     // An example:
136     // _M_construct<char *>:1000 string_view<std::allocator<char> >:437
137     // ":1000" and ":437" are used as anchor points so the string above will
138     // be interpreted as
139     // target: _M_construct<char *>
140     // count: 1000
141     // target: string_view<std::allocator<char> >
142     // count: 437
143     while (n3 != StringRef::npos) {
144       n3 += Rest.substr(n3).find_first_not_of(' ');
145       Rest = Rest.substr(n3);
146       n3 = Rest.find_first_of(':');
147       if (n3 == StringRef::npos || n3 == 0)
148         return false;
149 
150       StringRef Target;
151       uint64_t count, n4;
152       while (true) {
153         // Get the segment after the current colon.
154         StringRef AfterColon = Rest.substr(n3 + 1);
155         // Get the target symbol before the current colon.
156         Target = Rest.substr(0, n3);
157         // Check if the word after the current colon is an integer.
158         n4 = AfterColon.find_first_of(' ');
159         n4 = (n4 != StringRef::npos) ? n3 + n4 + 1 : Rest.size();
160         StringRef WordAfterColon = Rest.substr(n3 + 1, n4 - n3 - 1);
161         if (!WordAfterColon.getAsInteger(10, count))
162           break;
163 
164         // Try to find the next colon.
165         uint64_t n5 = AfterColon.find_first_of(':');
166         if (n5 == StringRef::npos)
167           return false;
168         n3 += n5 + 1;
169       }
170 
171       // An anchor point is found. Save the {target, count} pair
172       TargetCountMap[Target] = count;
173       if (n4 == Rest.size())
174         break;
175       // Change n3 to the next blank space after colon + integer pair.
176       n3 = n4;
177     }
178   } else {
179     IsCallsite = true;
180     size_t n3 = Rest.find_last_of(':');
181     CalleeName = Rest.substr(0, n3);
182     if (Rest.substr(n3 + 1).getAsInteger(10, NumSamples))
183       return false;
184   }
185   return true;
186 }
187 
188 /// Load samples from a text file.
189 ///
190 /// See the documentation at the top of the file for an explanation of
191 /// the expected format.
192 ///
193 /// \returns true if the file was loaded successfully, false otherwise.
194 std::error_code SampleProfileReaderText::readImpl() {
195   line_iterator LineIt(*Buffer, /*SkipBlanks=*/true, '#');
196   sampleprof_error Result = sampleprof_error::success;
197 
198   InlineCallStack InlineStack;
199 
200   for (; !LineIt.is_at_eof(); ++LineIt) {
201     if ((*LineIt)[(*LineIt).find_first_not_of(' ')] == '#')
202       continue;
203     // Read the header of each function.
204     //
205     // Note that for function identifiers we are actually expecting
206     // mangled names, but we may not always get them. This happens when
207     // the compiler decides not to emit the function (e.g., it was inlined
208     // and removed). In this case, the binary will not have the linkage
209     // name for the function, so the profiler will emit the function's
210     // unmangled name, which may contain characters like ':' and '>' in its
211     // name (member functions, templates, etc).
212     //
213     // The only requirement we place on the identifier, then, is that it
214     // should not begin with a number.
215     if ((*LineIt)[0] != ' ') {
216       uint64_t NumSamples, NumHeadSamples;
217       StringRef FName;
218       if (!ParseHead(*LineIt, FName, NumSamples, NumHeadSamples)) {
219         reportError(LineIt.line_number(),
220                     "Expected 'mangled_name:NUM:NUM', found " + *LineIt);
221         return sampleprof_error::malformed;
222       }
223       Profiles[FName] = FunctionSamples();
224       FunctionSamples &FProfile = Profiles[FName];
225       FProfile.setName(FName);
226       MergeResult(Result, FProfile.addTotalSamples(NumSamples));
227       MergeResult(Result, FProfile.addHeadSamples(NumHeadSamples));
228       InlineStack.clear();
229       InlineStack.push_back(&FProfile);
230     } else {
231       uint64_t NumSamples;
232       StringRef FName;
233       DenseMap<StringRef, uint64_t> TargetCountMap;
234       bool IsCallsite;
235       uint32_t Depth, LineOffset, Discriminator;
236       if (!ParseLine(*LineIt, IsCallsite, Depth, NumSamples, LineOffset,
237                      Discriminator, FName, TargetCountMap)) {
238         reportError(LineIt.line_number(),
239                     "Expected 'NUM[.NUM]: NUM[ mangled_name:NUM]*', found " +
240                         *LineIt);
241         return sampleprof_error::malformed;
242       }
243       if (IsCallsite) {
244         while (InlineStack.size() > Depth) {
245           InlineStack.pop_back();
246         }
247         FunctionSamples &FSamples = InlineStack.back()->functionSamplesAt(
248             LineLocation(LineOffset, Discriminator))[std::string(FName)];
249         FSamples.setName(FName);
250         MergeResult(Result, FSamples.addTotalSamples(NumSamples));
251         InlineStack.push_back(&FSamples);
252       } else {
253         while (InlineStack.size() > Depth) {
254           InlineStack.pop_back();
255         }
256         FunctionSamples &FProfile = *InlineStack.back();
257         for (const auto &name_count : TargetCountMap) {
258           MergeResult(Result, FProfile.addCalledTargetSamples(
259                                   LineOffset, Discriminator, name_count.first,
260                                   name_count.second));
261         }
262         MergeResult(Result, FProfile.addBodySamples(LineOffset, Discriminator,
263                                                     NumSamples));
264       }
265     }
266   }
267   if (Result == sampleprof_error::success)
268     computeSummary();
269 
270   return Result;
271 }
272 
273 bool SampleProfileReaderText::hasFormat(const MemoryBuffer &Buffer) {
274   bool result = false;
275 
276   // Check that the first non-comment line is a valid function header.
277   line_iterator LineIt(Buffer, /*SkipBlanks=*/true, '#');
278   if (!LineIt.is_at_eof()) {
279     if ((*LineIt)[0] != ' ') {
280       uint64_t NumSamples, NumHeadSamples;
281       StringRef FName;
282       result = ParseHead(*LineIt, FName, NumSamples, NumHeadSamples);
283     }
284   }
285 
286   return result;
287 }
288 
289 template <typename T> ErrorOr<T> SampleProfileReaderBinary::readNumber() {
290   unsigned NumBytesRead = 0;
291   std::error_code EC;
292   uint64_t Val = decodeULEB128(Data, &NumBytesRead);
293 
294   if (Val > std::numeric_limits<T>::max())
295     EC = sampleprof_error::malformed;
296   else if (Data + NumBytesRead > End)
297     EC = sampleprof_error::truncated;
298   else
299     EC = sampleprof_error::success;
300 
301   if (EC) {
302     reportError(0, EC.message());
303     return EC;
304   }
305 
306   Data += NumBytesRead;
307   return static_cast<T>(Val);
308 }
309 
310 ErrorOr<StringRef> SampleProfileReaderBinary::readString() {
311   std::error_code EC;
312   StringRef Str(reinterpret_cast<const char *>(Data));
313   if (Data + Str.size() + 1 > End) {
314     EC = sampleprof_error::truncated;
315     reportError(0, EC.message());
316     return EC;
317   }
318 
319   Data += Str.size() + 1;
320   return Str;
321 }
322 
323 template <typename T>
324 ErrorOr<T> SampleProfileReaderBinary::readUnencodedNumber() {
325   std::error_code EC;
326 
327   if (Data + sizeof(T) > End) {
328     EC = sampleprof_error::truncated;
329     reportError(0, EC.message());
330     return EC;
331   }
332 
333   using namespace support;
334   T Val = endian::readNext<T, little, unaligned>(Data);
335   return Val;
336 }
337 
338 template <typename T>
339 inline ErrorOr<uint32_t> SampleProfileReaderBinary::readStringIndex(T &Table) {
340   std::error_code EC;
341   auto Idx = readNumber<uint32_t>();
342   if (std::error_code EC = Idx.getError())
343     return EC;
344   if (*Idx >= Table.size())
345     return sampleprof_error::truncated_name_table;
346   return *Idx;
347 }
348 
349 ErrorOr<StringRef> SampleProfileReaderBinary::readStringFromTable() {
350   auto Idx = readStringIndex(NameTable);
351   if (std::error_code EC = Idx.getError())
352     return EC;
353 
354   return NameTable[*Idx];
355 }
356 
357 ErrorOr<StringRef> SampleProfileReaderCompactBinary::readStringFromTable() {
358   auto Idx = readStringIndex(NameTable);
359   if (std::error_code EC = Idx.getError())
360     return EC;
361 
362   return StringRef(NameTable[*Idx]);
363 }
364 
365 std::error_code
366 SampleProfileReaderBinary::readProfile(FunctionSamples &FProfile) {
367   auto NumSamples = readNumber<uint64_t>();
368   if (std::error_code EC = NumSamples.getError())
369     return EC;
370   FProfile.addTotalSamples(*NumSamples);
371 
372   // Read the samples in the body.
373   auto NumRecords = readNumber<uint32_t>();
374   if (std::error_code EC = NumRecords.getError())
375     return EC;
376 
377   for (uint32_t I = 0; I < *NumRecords; ++I) {
378     auto LineOffset = readNumber<uint64_t>();
379     if (std::error_code EC = LineOffset.getError())
380       return EC;
381 
382     if (!isOffsetLegal(*LineOffset)) {
383       return std::error_code();
384     }
385 
386     auto Discriminator = readNumber<uint64_t>();
387     if (std::error_code EC = Discriminator.getError())
388       return EC;
389 
390     auto NumSamples = readNumber<uint64_t>();
391     if (std::error_code EC = NumSamples.getError())
392       return EC;
393 
394     auto NumCalls = readNumber<uint32_t>();
395     if (std::error_code EC = NumCalls.getError())
396       return EC;
397 
398     for (uint32_t J = 0; J < *NumCalls; ++J) {
399       auto CalledFunction(readStringFromTable());
400       if (std::error_code EC = CalledFunction.getError())
401         return EC;
402 
403       auto CalledFunctionSamples = readNumber<uint64_t>();
404       if (std::error_code EC = CalledFunctionSamples.getError())
405         return EC;
406 
407       FProfile.addCalledTargetSamples(*LineOffset, *Discriminator,
408                                       *CalledFunction, *CalledFunctionSamples);
409     }
410 
411     FProfile.addBodySamples(*LineOffset, *Discriminator, *NumSamples);
412   }
413 
414   // Read all the samples for inlined function calls.
415   auto NumCallsites = readNumber<uint32_t>();
416   if (std::error_code EC = NumCallsites.getError())
417     return EC;
418 
419   for (uint32_t J = 0; J < *NumCallsites; ++J) {
420     auto LineOffset = readNumber<uint64_t>();
421     if (std::error_code EC = LineOffset.getError())
422       return EC;
423 
424     auto Discriminator = readNumber<uint64_t>();
425     if (std::error_code EC = Discriminator.getError())
426       return EC;
427 
428     auto FName(readStringFromTable());
429     if (std::error_code EC = FName.getError())
430       return EC;
431 
432     FunctionSamples &CalleeProfile = FProfile.functionSamplesAt(
433         LineLocation(*LineOffset, *Discriminator))[std::string(*FName)];
434     CalleeProfile.setName(*FName);
435     if (std::error_code EC = readProfile(CalleeProfile))
436       return EC;
437   }
438 
439   return sampleprof_error::success;
440 }
441 
442 std::error_code
443 SampleProfileReaderBinary::readFuncProfile(const uint8_t *Start) {
444   Data = Start;
445   auto NumHeadSamples = readNumber<uint64_t>();
446   if (std::error_code EC = NumHeadSamples.getError())
447     return EC;
448 
449   auto FName(readStringFromTable());
450   if (std::error_code EC = FName.getError())
451     return EC;
452 
453   Profiles[*FName] = FunctionSamples();
454   FunctionSamples &FProfile = Profiles[*FName];
455   FProfile.setName(*FName);
456 
457   FProfile.addHeadSamples(*NumHeadSamples);
458 
459   if (std::error_code EC = readProfile(FProfile))
460     return EC;
461   return sampleprof_error::success;
462 }
463 
464 std::error_code SampleProfileReaderBinary::readImpl() {
465   while (!at_eof()) {
466     if (std::error_code EC = readFuncProfile(Data))
467       return EC;
468   }
469 
470   return sampleprof_error::success;
471 }
472 
473 std::error_code SampleProfileReaderExtBinary::readOneSection(
474     const uint8_t *Start, uint64_t Size, const SecHdrTableEntry &Entry) {
475   Data = Start;
476   End = Start + Size;
477   switch (Entry.Type) {
478   case SecProfSummary:
479     if (std::error_code EC = readSummary())
480       return EC;
481     if (hasSecFlag(Entry, SecProfSummaryFlags::SecFlagPartial))
482       Summary->setPartialProfile(true);
483     break;
484   case SecNameTable:
485     if (std::error_code EC = readNameTableSec(
486             hasSecFlag(Entry, SecNameTableFlags::SecFlagMD5Name)))
487       return EC;
488     break;
489   case SecLBRProfile:
490     if (std::error_code EC = readFuncProfiles())
491       return EC;
492     break;
493   case SecProfileSymbolList:
494     if (std::error_code EC = readProfileSymbolList())
495       return EC;
496     break;
497   case SecFuncOffsetTable:
498     if (std::error_code EC = readFuncOffsetTable())
499       return EC;
500     break;
501   default:
502     break;
503   }
504   return sampleprof_error::success;
505 }
506 
507 void SampleProfileReaderExtBinary::collectFuncsFrom(const Module &M) {
508   UseAllFuncs = false;
509   FuncsToUse.clear();
510   for (auto &F : M)
511     FuncsToUse.insert(FunctionSamples::getCanonicalFnName(F));
512 }
513 
514 std::error_code SampleProfileReaderExtBinary::readFuncOffsetTable() {
515   auto Size = readNumber<uint64_t>();
516   if (std::error_code EC = Size.getError())
517     return EC;
518 
519   FuncOffsetTable.reserve(*Size);
520   for (uint32_t I = 0; I < *Size; ++I) {
521     auto FName(readStringFromTable());
522     if (std::error_code EC = FName.getError())
523       return EC;
524 
525     auto Offset = readNumber<uint64_t>();
526     if (std::error_code EC = Offset.getError())
527       return EC;
528 
529     FuncOffsetTable[*FName] = *Offset;
530   }
531   return sampleprof_error::success;
532 }
533 
534 std::error_code SampleProfileReaderExtBinary::readFuncProfiles() {
535   const uint8_t *Start = Data;
536   if (UseAllFuncs) {
537     while (Data < End) {
538       if (std::error_code EC = readFuncProfile(Data))
539         return EC;
540     }
541     assert(Data == End && "More data is read than expected");
542     return sampleprof_error::success;
543   }
544 
545   if (Remapper) {
546     for (auto Name : FuncsToUse) {
547       Remapper->insert(Name);
548     }
549   }
550 
551   if (useMD5()) {
552     for (auto Name : FuncsToUse) {
553       auto GUID = std::to_string(MD5Hash(Name));
554       auto iter = FuncOffsetTable.find(StringRef(GUID));
555       if (iter == FuncOffsetTable.end())
556         continue;
557       const uint8_t *FuncProfileAddr = Start + iter->second;
558       assert(FuncProfileAddr < End && "out of LBRProfile section");
559       if (std::error_code EC = readFuncProfile(FuncProfileAddr))
560         return EC;
561     }
562   } else {
563     for (auto NameOffset : FuncOffsetTable) {
564       auto FuncName = NameOffset.first;
565       if (!FuncsToUse.count(FuncName) &&
566           (!Remapper || !Remapper->exist(FuncName)))
567         continue;
568       const uint8_t *FuncProfileAddr = Start + NameOffset.second;
569       assert(FuncProfileAddr < End && "out of LBRProfile section");
570       if (std::error_code EC = readFuncProfile(FuncProfileAddr))
571         return EC;
572     }
573   }
574 
575   Data = End;
576   return sampleprof_error::success;
577 }
578 
579 std::error_code SampleProfileReaderExtBinary::readProfileSymbolList() {
580   if (!ProfSymList)
581     ProfSymList = std::make_unique<ProfileSymbolList>();
582 
583   if (std::error_code EC = ProfSymList->read(Data, End - Data))
584     return EC;
585 
586   Data = End;
587   return sampleprof_error::success;
588 }
589 
590 std::error_code SampleProfileReaderExtBinaryBase::decompressSection(
591     const uint8_t *SecStart, const uint64_t SecSize,
592     const uint8_t *&DecompressBuf, uint64_t &DecompressBufSize) {
593   Data = SecStart;
594   End = SecStart + SecSize;
595   auto DecompressSize = readNumber<uint64_t>();
596   if (std::error_code EC = DecompressSize.getError())
597     return EC;
598   DecompressBufSize = *DecompressSize;
599 
600   auto CompressSize = readNumber<uint64_t>();
601   if (std::error_code EC = CompressSize.getError())
602     return EC;
603 
604   if (!llvm::zlib::isAvailable())
605     return sampleprof_error::zlib_unavailable;
606 
607   StringRef CompressedStrings(reinterpret_cast<const char *>(Data),
608                               *CompressSize);
609   char *Buffer = Allocator.Allocate<char>(DecompressBufSize);
610   size_t UCSize = DecompressBufSize;
611   llvm::Error E =
612       zlib::uncompress(CompressedStrings, Buffer, UCSize);
613   if (E)
614     return sampleprof_error::uncompress_failed;
615   DecompressBuf = reinterpret_cast<const uint8_t *>(Buffer);
616   return sampleprof_error::success;
617 }
618 
619 std::error_code SampleProfileReaderExtBinaryBase::readImpl() {
620   const uint8_t *BufStart =
621       reinterpret_cast<const uint8_t *>(Buffer->getBufferStart());
622 
623   for (auto &Entry : SecHdrTable) {
624     // Skip empty section.
625     if (!Entry.Size)
626       continue;
627 
628     const uint8_t *SecStart = BufStart + Entry.Offset;
629     uint64_t SecSize = Entry.Size;
630 
631     // If the section is compressed, decompress it into a buffer
632     // DecompressBuf before reading the actual data. The pointee of
633     // 'Data' will be changed to buffer hold by DecompressBuf
634     // temporarily when reading the actual data.
635     bool isCompressed = hasSecFlag(Entry, SecCommonFlags::SecFlagCompress);
636     if (isCompressed) {
637       const uint8_t *DecompressBuf;
638       uint64_t DecompressBufSize;
639       if (std::error_code EC = decompressSection(
640               SecStart, SecSize, DecompressBuf, DecompressBufSize))
641         return EC;
642       SecStart = DecompressBuf;
643       SecSize = DecompressBufSize;
644     }
645 
646     if (std::error_code EC = readOneSection(SecStart, SecSize, Entry))
647       return EC;
648     if (Data != SecStart + SecSize)
649       return sampleprof_error::malformed;
650 
651     // Change the pointee of 'Data' from DecompressBuf to original Buffer.
652     if (isCompressed) {
653       Data = BufStart + Entry.Offset;
654       End = BufStart + Buffer->getBufferSize();
655     }
656   }
657 
658   return sampleprof_error::success;
659 }
660 
661 std::error_code SampleProfileReaderCompactBinary::readImpl() {
662   std::vector<uint64_t> OffsetsToUse;
663   if (UseAllFuncs) {
664     for (auto FuncEntry : FuncOffsetTable) {
665       OffsetsToUse.push_back(FuncEntry.second);
666     }
667   }
668   else {
669     for (auto Name : FuncsToUse) {
670       auto GUID = std::to_string(MD5Hash(Name));
671       auto iter = FuncOffsetTable.find(StringRef(GUID));
672       if (iter == FuncOffsetTable.end())
673         continue;
674       OffsetsToUse.push_back(iter->second);
675     }
676   }
677 
678   for (auto Offset : OffsetsToUse) {
679     const uint8_t *SavedData = Data;
680     if (std::error_code EC = readFuncProfile(
681             reinterpret_cast<const uint8_t *>(Buffer->getBufferStart()) +
682             Offset))
683       return EC;
684     Data = SavedData;
685   }
686   return sampleprof_error::success;
687 }
688 
689 std::error_code SampleProfileReaderRawBinary::verifySPMagic(uint64_t Magic) {
690   if (Magic == SPMagic())
691     return sampleprof_error::success;
692   return sampleprof_error::bad_magic;
693 }
694 
695 std::error_code SampleProfileReaderExtBinary::verifySPMagic(uint64_t Magic) {
696   if (Magic == SPMagic(SPF_Ext_Binary))
697     return sampleprof_error::success;
698   return sampleprof_error::bad_magic;
699 }
700 
701 std::error_code
702 SampleProfileReaderCompactBinary::verifySPMagic(uint64_t Magic) {
703   if (Magic == SPMagic(SPF_Compact_Binary))
704     return sampleprof_error::success;
705   return sampleprof_error::bad_magic;
706 }
707 
708 std::error_code SampleProfileReaderBinary::readNameTable() {
709   auto Size = readNumber<uint32_t>();
710   if (std::error_code EC = Size.getError())
711     return EC;
712   NameTable.reserve(*Size);
713   for (uint32_t I = 0; I < *Size; ++I) {
714     auto Name(readString());
715     if (std::error_code EC = Name.getError())
716       return EC;
717     NameTable.push_back(*Name);
718   }
719 
720   return sampleprof_error::success;
721 }
722 
723 std::error_code SampleProfileReaderExtBinary::readMD5NameTable() {
724   auto Size = readNumber<uint64_t>();
725   if (std::error_code EC = Size.getError())
726     return EC;
727   NameTable.reserve(*Size);
728   MD5StringBuf = std::make_unique<std::vector<std::string>>();
729   MD5StringBuf->reserve(*Size);
730   for (uint32_t I = 0; I < *Size; ++I) {
731     auto FID = readNumber<uint64_t>();
732     if (std::error_code EC = FID.getError())
733       return EC;
734     MD5StringBuf->push_back(std::to_string(*FID));
735     // NameTable is a vector of StringRef. Here it is pushing back a
736     // StringRef initialized with the last string in MD5stringBuf.
737     NameTable.push_back(MD5StringBuf->back());
738   }
739   return sampleprof_error::success;
740 }
741 
742 std::error_code SampleProfileReaderExtBinary::readNameTableSec(bool IsMD5) {
743   if (IsMD5)
744     return readMD5NameTable();
745   return SampleProfileReaderBinary::readNameTable();
746 }
747 
748 std::error_code SampleProfileReaderCompactBinary::readNameTable() {
749   auto Size = readNumber<uint64_t>();
750   if (std::error_code EC = Size.getError())
751     return EC;
752   NameTable.reserve(*Size);
753   for (uint32_t I = 0; I < *Size; ++I) {
754     auto FID = readNumber<uint64_t>();
755     if (std::error_code EC = FID.getError())
756       return EC;
757     NameTable.push_back(std::to_string(*FID));
758   }
759   return sampleprof_error::success;
760 }
761 
762 std::error_code SampleProfileReaderExtBinaryBase::readSecHdrTableEntry() {
763   SecHdrTableEntry Entry;
764   auto Type = readUnencodedNumber<uint64_t>();
765   if (std::error_code EC = Type.getError())
766     return EC;
767   Entry.Type = static_cast<SecType>(*Type);
768 
769   auto Flags = readUnencodedNumber<uint64_t>();
770   if (std::error_code EC = Flags.getError())
771     return EC;
772   Entry.Flags = *Flags;
773 
774   auto Offset = readUnencodedNumber<uint64_t>();
775   if (std::error_code EC = Offset.getError())
776     return EC;
777   Entry.Offset = *Offset;
778 
779   auto Size = readUnencodedNumber<uint64_t>();
780   if (std::error_code EC = Size.getError())
781     return EC;
782   Entry.Size = *Size;
783 
784   SecHdrTable.push_back(std::move(Entry));
785   return sampleprof_error::success;
786 }
787 
788 std::error_code SampleProfileReaderExtBinaryBase::readSecHdrTable() {
789   auto EntryNum = readUnencodedNumber<uint64_t>();
790   if (std::error_code EC = EntryNum.getError())
791     return EC;
792 
793   for (uint32_t i = 0; i < (*EntryNum); i++)
794     if (std::error_code EC = readSecHdrTableEntry())
795       return EC;
796 
797   return sampleprof_error::success;
798 }
799 
800 std::error_code SampleProfileReaderExtBinaryBase::readHeader() {
801   const uint8_t *BufStart =
802       reinterpret_cast<const uint8_t *>(Buffer->getBufferStart());
803   Data = BufStart;
804   End = BufStart + Buffer->getBufferSize();
805 
806   if (std::error_code EC = readMagicIdent())
807     return EC;
808 
809   if (std::error_code EC = readSecHdrTable())
810     return EC;
811 
812   return sampleprof_error::success;
813 }
814 
815 uint64_t SampleProfileReaderExtBinaryBase::getSectionSize(SecType Type) {
816   for (auto &Entry : SecHdrTable) {
817     if (Entry.Type == Type)
818       return Entry.Size;
819   }
820   return 0;
821 }
822 
823 uint64_t SampleProfileReaderExtBinaryBase::getFileSize() {
824   // Sections in SecHdrTable is not necessarily in the same order as
825   // sections in the profile because section like FuncOffsetTable needs
826   // to be written after section LBRProfile but needs to be read before
827   // section LBRProfile, so we cannot simply use the last entry in
828   // SecHdrTable to calculate the file size.
829   uint64_t FileSize = 0;
830   for (auto &Entry : SecHdrTable) {
831     FileSize = std::max(Entry.Offset + Entry.Size, FileSize);
832   }
833   return FileSize;
834 }
835 
836 static std::string getSecFlagsStr(const SecHdrTableEntry &Entry) {
837   std::string Flags;
838   if (hasSecFlag(Entry, SecCommonFlags::SecFlagCompress))
839     Flags.append("{compressed,");
840   else
841     Flags.append("{");
842 
843   switch (Entry.Type) {
844   case SecNameTable:
845     if (hasSecFlag(Entry, SecNameTableFlags::SecFlagMD5Name))
846       Flags.append("md5,");
847     break;
848   case SecProfSummary:
849     if (hasSecFlag(Entry, SecProfSummaryFlags::SecFlagPartial))
850       Flags.append("partial,");
851     break;
852   default:
853     break;
854   }
855   char &last = Flags.back();
856   if (last == ',')
857     last = '}';
858   else
859     Flags.append("}");
860   return Flags;
861 }
862 
863 bool SampleProfileReaderExtBinaryBase::dumpSectionInfo(raw_ostream &OS) {
864   uint64_t TotalSecsSize = 0;
865   for (auto &Entry : SecHdrTable) {
866     OS << getSecName(Entry.Type) << " - Offset: " << Entry.Offset
867        << ", Size: " << Entry.Size << ", Flags: " << getSecFlagsStr(Entry)
868        << "\n";
869     ;
870     TotalSecsSize += getSectionSize(Entry.Type);
871   }
872   uint64_t HeaderSize = SecHdrTable.front().Offset;
873   assert(HeaderSize + TotalSecsSize == getFileSize() &&
874          "Size of 'header + sections' doesn't match the total size of profile");
875 
876   OS << "Header Size: " << HeaderSize << "\n";
877   OS << "Total Sections Size: " << TotalSecsSize << "\n";
878   OS << "File Size: " << getFileSize() << "\n";
879   return true;
880 }
881 
882 std::error_code SampleProfileReaderBinary::readMagicIdent() {
883   // Read and check the magic identifier.
884   auto Magic = readNumber<uint64_t>();
885   if (std::error_code EC = Magic.getError())
886     return EC;
887   else if (std::error_code EC = verifySPMagic(*Magic))
888     return EC;
889 
890   // Read the version number.
891   auto Version = readNumber<uint64_t>();
892   if (std::error_code EC = Version.getError())
893     return EC;
894   else if (*Version != SPVersion())
895     return sampleprof_error::unsupported_version;
896 
897   return sampleprof_error::success;
898 }
899 
900 std::error_code SampleProfileReaderBinary::readHeader() {
901   Data = reinterpret_cast<const uint8_t *>(Buffer->getBufferStart());
902   End = Data + Buffer->getBufferSize();
903 
904   if (std::error_code EC = readMagicIdent())
905     return EC;
906 
907   if (std::error_code EC = readSummary())
908     return EC;
909 
910   if (std::error_code EC = readNameTable())
911     return EC;
912   return sampleprof_error::success;
913 }
914 
915 std::error_code SampleProfileReaderCompactBinary::readHeader() {
916   SampleProfileReaderBinary::readHeader();
917   if (std::error_code EC = readFuncOffsetTable())
918     return EC;
919   return sampleprof_error::success;
920 }
921 
922 std::error_code SampleProfileReaderCompactBinary::readFuncOffsetTable() {
923   auto TableOffset = readUnencodedNumber<uint64_t>();
924   if (std::error_code EC = TableOffset.getError())
925     return EC;
926 
927   const uint8_t *SavedData = Data;
928   const uint8_t *TableStart =
929       reinterpret_cast<const uint8_t *>(Buffer->getBufferStart()) +
930       *TableOffset;
931   Data = TableStart;
932 
933   auto Size = readNumber<uint64_t>();
934   if (std::error_code EC = Size.getError())
935     return EC;
936 
937   FuncOffsetTable.reserve(*Size);
938   for (uint32_t I = 0; I < *Size; ++I) {
939     auto FName(readStringFromTable());
940     if (std::error_code EC = FName.getError())
941       return EC;
942 
943     auto Offset = readNumber<uint64_t>();
944     if (std::error_code EC = Offset.getError())
945       return EC;
946 
947     FuncOffsetTable[*FName] = *Offset;
948   }
949   End = TableStart;
950   Data = SavedData;
951   return sampleprof_error::success;
952 }
953 
954 void SampleProfileReaderCompactBinary::collectFuncsFrom(const Module &M) {
955   UseAllFuncs = false;
956   FuncsToUse.clear();
957   for (auto &F : M)
958     FuncsToUse.insert(FunctionSamples::getCanonicalFnName(F));
959 }
960 
961 std::error_code SampleProfileReaderBinary::readSummaryEntry(
962     std::vector<ProfileSummaryEntry> &Entries) {
963   auto Cutoff = readNumber<uint64_t>();
964   if (std::error_code EC = Cutoff.getError())
965     return EC;
966 
967   auto MinBlockCount = readNumber<uint64_t>();
968   if (std::error_code EC = MinBlockCount.getError())
969     return EC;
970 
971   auto NumBlocks = readNumber<uint64_t>();
972   if (std::error_code EC = NumBlocks.getError())
973     return EC;
974 
975   Entries.emplace_back(*Cutoff, *MinBlockCount, *NumBlocks);
976   return sampleprof_error::success;
977 }
978 
979 std::error_code SampleProfileReaderBinary::readSummary() {
980   auto TotalCount = readNumber<uint64_t>();
981   if (std::error_code EC = TotalCount.getError())
982     return EC;
983 
984   auto MaxBlockCount = readNumber<uint64_t>();
985   if (std::error_code EC = MaxBlockCount.getError())
986     return EC;
987 
988   auto MaxFunctionCount = readNumber<uint64_t>();
989   if (std::error_code EC = MaxFunctionCount.getError())
990     return EC;
991 
992   auto NumBlocks = readNumber<uint64_t>();
993   if (std::error_code EC = NumBlocks.getError())
994     return EC;
995 
996   auto NumFunctions = readNumber<uint64_t>();
997   if (std::error_code EC = NumFunctions.getError())
998     return EC;
999 
1000   auto NumSummaryEntries = readNumber<uint64_t>();
1001   if (std::error_code EC = NumSummaryEntries.getError())
1002     return EC;
1003 
1004   std::vector<ProfileSummaryEntry> Entries;
1005   for (unsigned i = 0; i < *NumSummaryEntries; i++) {
1006     std::error_code EC = readSummaryEntry(Entries);
1007     if (EC != sampleprof_error::success)
1008       return EC;
1009   }
1010   Summary = std::make_unique<ProfileSummary>(
1011       ProfileSummary::PSK_Sample, Entries, *TotalCount, *MaxBlockCount, 0,
1012       *MaxFunctionCount, *NumBlocks, *NumFunctions);
1013 
1014   return sampleprof_error::success;
1015 }
1016 
1017 bool SampleProfileReaderRawBinary::hasFormat(const MemoryBuffer &Buffer) {
1018   const uint8_t *Data =
1019       reinterpret_cast<const uint8_t *>(Buffer.getBufferStart());
1020   uint64_t Magic = decodeULEB128(Data);
1021   return Magic == SPMagic();
1022 }
1023 
1024 bool SampleProfileReaderExtBinary::hasFormat(const MemoryBuffer &Buffer) {
1025   const uint8_t *Data =
1026       reinterpret_cast<const uint8_t *>(Buffer.getBufferStart());
1027   uint64_t Magic = decodeULEB128(Data);
1028   return Magic == SPMagic(SPF_Ext_Binary);
1029 }
1030 
1031 bool SampleProfileReaderCompactBinary::hasFormat(const MemoryBuffer &Buffer) {
1032   const uint8_t *Data =
1033       reinterpret_cast<const uint8_t *>(Buffer.getBufferStart());
1034   uint64_t Magic = decodeULEB128(Data);
1035   return Magic == SPMagic(SPF_Compact_Binary);
1036 }
1037 
1038 std::error_code SampleProfileReaderGCC::skipNextWord() {
1039   uint32_t dummy;
1040   if (!GcovBuffer.readInt(dummy))
1041     return sampleprof_error::truncated;
1042   return sampleprof_error::success;
1043 }
1044 
1045 template <typename T> ErrorOr<T> SampleProfileReaderGCC::readNumber() {
1046   if (sizeof(T) <= sizeof(uint32_t)) {
1047     uint32_t Val;
1048     if (GcovBuffer.readInt(Val) && Val <= std::numeric_limits<T>::max())
1049       return static_cast<T>(Val);
1050   } else if (sizeof(T) <= sizeof(uint64_t)) {
1051     uint64_t Val;
1052     if (GcovBuffer.readInt64(Val) && Val <= std::numeric_limits<T>::max())
1053       return static_cast<T>(Val);
1054   }
1055 
1056   std::error_code EC = sampleprof_error::malformed;
1057   reportError(0, EC.message());
1058   return EC;
1059 }
1060 
1061 ErrorOr<StringRef> SampleProfileReaderGCC::readString() {
1062   StringRef Str;
1063   if (!GcovBuffer.readString(Str))
1064     return sampleprof_error::truncated;
1065   return Str;
1066 }
1067 
1068 std::error_code SampleProfileReaderGCC::readHeader() {
1069   // Read the magic identifier.
1070   if (!GcovBuffer.readGCDAFormat())
1071     return sampleprof_error::unrecognized_format;
1072 
1073   // Read the version number. Note - the GCC reader does not validate this
1074   // version, but the profile creator generates v704.
1075   GCOV::GCOVVersion version;
1076   if (!GcovBuffer.readGCOVVersion(version))
1077     return sampleprof_error::unrecognized_format;
1078 
1079   if (version != GCOV::V407)
1080     return sampleprof_error::unsupported_version;
1081 
1082   // Skip the empty integer.
1083   if (std::error_code EC = skipNextWord())
1084     return EC;
1085 
1086   return sampleprof_error::success;
1087 }
1088 
1089 std::error_code SampleProfileReaderGCC::readSectionTag(uint32_t Expected) {
1090   uint32_t Tag;
1091   if (!GcovBuffer.readInt(Tag))
1092     return sampleprof_error::truncated;
1093 
1094   if (Tag != Expected)
1095     return sampleprof_error::malformed;
1096 
1097   if (std::error_code EC = skipNextWord())
1098     return EC;
1099 
1100   return sampleprof_error::success;
1101 }
1102 
1103 std::error_code SampleProfileReaderGCC::readNameTable() {
1104   if (std::error_code EC = readSectionTag(GCOVTagAFDOFileNames))
1105     return EC;
1106 
1107   uint32_t Size;
1108   if (!GcovBuffer.readInt(Size))
1109     return sampleprof_error::truncated;
1110 
1111   for (uint32_t I = 0; I < Size; ++I) {
1112     StringRef Str;
1113     if (!GcovBuffer.readString(Str))
1114       return sampleprof_error::truncated;
1115     Names.push_back(std::string(Str));
1116   }
1117 
1118   return sampleprof_error::success;
1119 }
1120 
1121 std::error_code SampleProfileReaderGCC::readFunctionProfiles() {
1122   if (std::error_code EC = readSectionTag(GCOVTagAFDOFunction))
1123     return EC;
1124 
1125   uint32_t NumFunctions;
1126   if (!GcovBuffer.readInt(NumFunctions))
1127     return sampleprof_error::truncated;
1128 
1129   InlineCallStack Stack;
1130   for (uint32_t I = 0; I < NumFunctions; ++I)
1131     if (std::error_code EC = readOneFunctionProfile(Stack, true, 0))
1132       return EC;
1133 
1134   computeSummary();
1135   return sampleprof_error::success;
1136 }
1137 
1138 std::error_code SampleProfileReaderGCC::readOneFunctionProfile(
1139     const InlineCallStack &InlineStack, bool Update, uint32_t Offset) {
1140   uint64_t HeadCount = 0;
1141   if (InlineStack.size() == 0)
1142     if (!GcovBuffer.readInt64(HeadCount))
1143       return sampleprof_error::truncated;
1144 
1145   uint32_t NameIdx;
1146   if (!GcovBuffer.readInt(NameIdx))
1147     return sampleprof_error::truncated;
1148 
1149   StringRef Name(Names[NameIdx]);
1150 
1151   uint32_t NumPosCounts;
1152   if (!GcovBuffer.readInt(NumPosCounts))
1153     return sampleprof_error::truncated;
1154 
1155   uint32_t NumCallsites;
1156   if (!GcovBuffer.readInt(NumCallsites))
1157     return sampleprof_error::truncated;
1158 
1159   FunctionSamples *FProfile = nullptr;
1160   if (InlineStack.size() == 0) {
1161     // If this is a top function that we have already processed, do not
1162     // update its profile again.  This happens in the presence of
1163     // function aliases.  Since these aliases share the same function
1164     // body, there will be identical replicated profiles for the
1165     // original function.  In this case, we simply not bother updating
1166     // the profile of the original function.
1167     FProfile = &Profiles[Name];
1168     FProfile->addHeadSamples(HeadCount);
1169     if (FProfile->getTotalSamples() > 0)
1170       Update = false;
1171   } else {
1172     // Otherwise, we are reading an inlined instance. The top of the
1173     // inline stack contains the profile of the caller. Insert this
1174     // callee in the caller's CallsiteMap.
1175     FunctionSamples *CallerProfile = InlineStack.front();
1176     uint32_t LineOffset = Offset >> 16;
1177     uint32_t Discriminator = Offset & 0xffff;
1178     FProfile = &CallerProfile->functionSamplesAt(
1179         LineLocation(LineOffset, Discriminator))[std::string(Name)];
1180   }
1181   FProfile->setName(Name);
1182 
1183   for (uint32_t I = 0; I < NumPosCounts; ++I) {
1184     uint32_t Offset;
1185     if (!GcovBuffer.readInt(Offset))
1186       return sampleprof_error::truncated;
1187 
1188     uint32_t NumTargets;
1189     if (!GcovBuffer.readInt(NumTargets))
1190       return sampleprof_error::truncated;
1191 
1192     uint64_t Count;
1193     if (!GcovBuffer.readInt64(Count))
1194       return sampleprof_error::truncated;
1195 
1196     // The line location is encoded in the offset as:
1197     //   high 16 bits: line offset to the start of the function.
1198     //   low 16 bits: discriminator.
1199     uint32_t LineOffset = Offset >> 16;
1200     uint32_t Discriminator = Offset & 0xffff;
1201 
1202     InlineCallStack NewStack;
1203     NewStack.push_back(FProfile);
1204     NewStack.insert(NewStack.end(), InlineStack.begin(), InlineStack.end());
1205     if (Update) {
1206       // Walk up the inline stack, adding the samples on this line to
1207       // the total sample count of the callers in the chain.
1208       for (auto CallerProfile : NewStack)
1209         CallerProfile->addTotalSamples(Count);
1210 
1211       // Update the body samples for the current profile.
1212       FProfile->addBodySamples(LineOffset, Discriminator, Count);
1213     }
1214 
1215     // Process the list of functions called at an indirect call site.
1216     // These are all the targets that a function pointer (or virtual
1217     // function) resolved at runtime.
1218     for (uint32_t J = 0; J < NumTargets; J++) {
1219       uint32_t HistVal;
1220       if (!GcovBuffer.readInt(HistVal))
1221         return sampleprof_error::truncated;
1222 
1223       if (HistVal != HIST_TYPE_INDIR_CALL_TOPN)
1224         return sampleprof_error::malformed;
1225 
1226       uint64_t TargetIdx;
1227       if (!GcovBuffer.readInt64(TargetIdx))
1228         return sampleprof_error::truncated;
1229       StringRef TargetName(Names[TargetIdx]);
1230 
1231       uint64_t TargetCount;
1232       if (!GcovBuffer.readInt64(TargetCount))
1233         return sampleprof_error::truncated;
1234 
1235       if (Update)
1236         FProfile->addCalledTargetSamples(LineOffset, Discriminator,
1237                                          TargetName, TargetCount);
1238     }
1239   }
1240 
1241   // Process all the inlined callers into the current function. These
1242   // are all the callsites that were inlined into this function.
1243   for (uint32_t I = 0; I < NumCallsites; I++) {
1244     // The offset is encoded as:
1245     //   high 16 bits: line offset to the start of the function.
1246     //   low 16 bits: discriminator.
1247     uint32_t Offset;
1248     if (!GcovBuffer.readInt(Offset))
1249       return sampleprof_error::truncated;
1250     InlineCallStack NewStack;
1251     NewStack.push_back(FProfile);
1252     NewStack.insert(NewStack.end(), InlineStack.begin(), InlineStack.end());
1253     if (std::error_code EC = readOneFunctionProfile(NewStack, Update, Offset))
1254       return EC;
1255   }
1256 
1257   return sampleprof_error::success;
1258 }
1259 
1260 /// Read a GCC AutoFDO profile.
1261 ///
1262 /// This format is generated by the Linux Perf conversion tool at
1263 /// https://github.com/google/autofdo.
1264 std::error_code SampleProfileReaderGCC::readImpl() {
1265   // Read the string table.
1266   if (std::error_code EC = readNameTable())
1267     return EC;
1268 
1269   // Read the source profile.
1270   if (std::error_code EC = readFunctionProfiles())
1271     return EC;
1272 
1273   return sampleprof_error::success;
1274 }
1275 
1276 bool SampleProfileReaderGCC::hasFormat(const MemoryBuffer &Buffer) {
1277   StringRef Magic(reinterpret_cast<const char *>(Buffer.getBufferStart()));
1278   return Magic == "adcg*704";
1279 }
1280 
1281 void SampleProfileReaderItaniumRemapper::applyRemapping(LLVMContext &Ctx) {
1282   // If the reader uses MD5 to represent string, we can't remap it because
1283   // we don't know what the original function names were.
1284   if (Reader.useMD5()) {
1285     Ctx.diagnose(DiagnosticInfoSampleProfile(
1286         Reader.getBuffer()->getBufferIdentifier(),
1287         "Profile data remapping cannot be applied to profile data "
1288         "in compact format (original mangled names are not available).",
1289         DS_Warning));
1290     return;
1291   }
1292 
1293   assert(Remappings && "should be initialized while creating remapper");
1294   for (auto &Sample : Reader.getProfiles())
1295     if (auto Key = Remappings->insert(Sample.first()))
1296       SampleMap.insert({Key, &Sample.second});
1297 
1298   RemappingApplied = true;
1299 }
1300 
1301 FunctionSamples *
1302 SampleProfileReaderItaniumRemapper::getSamplesFor(StringRef Fname) {
1303   if (auto Key = Remappings->lookup(Fname))
1304     return SampleMap.lookup(Key);
1305   return nullptr;
1306 }
1307 
1308 /// Prepare a memory buffer for the contents of \p Filename.
1309 ///
1310 /// \returns an error code indicating the status of the buffer.
1311 static ErrorOr<std::unique_ptr<MemoryBuffer>>
1312 setupMemoryBuffer(const Twine &Filename) {
1313   auto BufferOrErr = MemoryBuffer::getFileOrSTDIN(Filename);
1314   if (std::error_code EC = BufferOrErr.getError())
1315     return EC;
1316   auto Buffer = std::move(BufferOrErr.get());
1317 
1318   // Sanity check the file.
1319   if (uint64_t(Buffer->getBufferSize()) > std::numeric_limits<uint32_t>::max())
1320     return sampleprof_error::too_large;
1321 
1322   return std::move(Buffer);
1323 }
1324 
1325 /// Create a sample profile reader based on the format of the input file.
1326 ///
1327 /// \param Filename The file to open.
1328 ///
1329 /// \param C The LLVM context to use to emit diagnostics.
1330 ///
1331 /// \param RemapFilename The file used for profile remapping.
1332 ///
1333 /// \returns an error code indicating the status of the created reader.
1334 ErrorOr<std::unique_ptr<SampleProfileReader>>
1335 SampleProfileReader::create(const std::string Filename, LLVMContext &C,
1336                             const std::string RemapFilename) {
1337   auto BufferOrError = setupMemoryBuffer(Filename);
1338   if (std::error_code EC = BufferOrError.getError())
1339     return EC;
1340   return create(BufferOrError.get(), C, RemapFilename);
1341 }
1342 
1343 /// Create a sample profile remapper from the given input, to remap the
1344 /// function names in the given profile data.
1345 ///
1346 /// \param Filename The file to open.
1347 ///
1348 /// \param Reader The profile reader the remapper is going to be applied to.
1349 ///
1350 /// \param C The LLVM context to use to emit diagnostics.
1351 ///
1352 /// \returns an error code indicating the status of the created reader.
1353 ErrorOr<std::unique_ptr<SampleProfileReaderItaniumRemapper>>
1354 SampleProfileReaderItaniumRemapper::create(const std::string Filename,
1355                                            SampleProfileReader &Reader,
1356                                            LLVMContext &C) {
1357   auto BufferOrError = setupMemoryBuffer(Filename);
1358   if (std::error_code EC = BufferOrError.getError())
1359     return EC;
1360   return create(BufferOrError.get(), Reader, C);
1361 }
1362 
1363 /// Create a sample profile remapper from the given input, to remap the
1364 /// function names in the given profile data.
1365 ///
1366 /// \param B The memory buffer to create the reader from (assumes ownership).
1367 ///
1368 /// \param C The LLVM context to use to emit diagnostics.
1369 ///
1370 /// \param Reader The profile reader the remapper is going to be applied to.
1371 ///
1372 /// \returns an error code indicating the status of the created reader.
1373 ErrorOr<std::unique_ptr<SampleProfileReaderItaniumRemapper>>
1374 SampleProfileReaderItaniumRemapper::create(std::unique_ptr<MemoryBuffer> &B,
1375                                            SampleProfileReader &Reader,
1376                                            LLVMContext &C) {
1377   auto Remappings = std::make_unique<SymbolRemappingReader>();
1378   if (Error E = Remappings->read(*B.get())) {
1379     handleAllErrors(
1380         std::move(E), [&](const SymbolRemappingParseError &ParseError) {
1381           C.diagnose(DiagnosticInfoSampleProfile(B->getBufferIdentifier(),
1382                                                  ParseError.getLineNum(),
1383                                                  ParseError.getMessage()));
1384         });
1385     return sampleprof_error::malformed;
1386   }
1387 
1388   return std::make_unique<SampleProfileReaderItaniumRemapper>(
1389       std::move(B), std::move(Remappings), Reader);
1390 }
1391 
1392 /// Create a sample profile reader based on the format of the input data.
1393 ///
1394 /// \param B The memory buffer to create the reader from (assumes ownership).
1395 ///
1396 /// \param C The LLVM context to use to emit diagnostics.
1397 ///
1398 /// \param RemapFilename The file used for profile remapping.
1399 ///
1400 /// \returns an error code indicating the status of the created reader.
1401 ErrorOr<std::unique_ptr<SampleProfileReader>>
1402 SampleProfileReader::create(std::unique_ptr<MemoryBuffer> &B, LLVMContext &C,
1403                             const std::string RemapFilename) {
1404   std::unique_ptr<SampleProfileReader> Reader;
1405   if (SampleProfileReaderRawBinary::hasFormat(*B))
1406     Reader.reset(new SampleProfileReaderRawBinary(std::move(B), C));
1407   else if (SampleProfileReaderExtBinary::hasFormat(*B))
1408     Reader.reset(new SampleProfileReaderExtBinary(std::move(B), C));
1409   else if (SampleProfileReaderCompactBinary::hasFormat(*B))
1410     Reader.reset(new SampleProfileReaderCompactBinary(std::move(B), C));
1411   else if (SampleProfileReaderGCC::hasFormat(*B))
1412     Reader.reset(new SampleProfileReaderGCC(std::move(B), C));
1413   else if (SampleProfileReaderText::hasFormat(*B))
1414     Reader.reset(new SampleProfileReaderText(std::move(B), C));
1415   else
1416     return sampleprof_error::unrecognized_format;
1417 
1418   if (!RemapFilename.empty()) {
1419     auto ReaderOrErr =
1420         SampleProfileReaderItaniumRemapper::create(RemapFilename, *Reader, C);
1421     if (std::error_code EC = ReaderOrErr.getError()) {
1422       std::string Msg = "Could not create remapper: " + EC.message();
1423       C.diagnose(DiagnosticInfoSampleProfile(RemapFilename, Msg));
1424       return EC;
1425     }
1426     Reader->Remapper = std::move(ReaderOrErr.get());
1427   }
1428 
1429   FunctionSamples::Format = Reader->getFormat();
1430   if (std::error_code EC = Reader->readHeader()) {
1431     return EC;
1432   }
1433 
1434   return std::move(Reader);
1435 }
1436 
1437 // For text and GCC file formats, we compute the summary after reading the
1438 // profile. Binary format has the profile summary in its header.
1439 void SampleProfileReader::computeSummary() {
1440   SampleProfileSummaryBuilder Builder(ProfileSummaryBuilder::DefaultCutoffs);
1441   for (const auto &I : Profiles) {
1442     const FunctionSamples &Profile = I.second;
1443     Builder.addRecord(Profile);
1444   }
1445   Summary = Builder.getSummary();
1446 }
1447