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