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