1de1ab26fSDiego Novillo //===- SampleProfReader.cpp - Read LLVM sample profile data ---------------===//
2de1ab26fSDiego Novillo //
3de1ab26fSDiego Novillo //                      The LLVM Compiler Infrastructure
4de1ab26fSDiego Novillo //
5de1ab26fSDiego Novillo // This file is distributed under the University of Illinois Open Source
6de1ab26fSDiego Novillo // License. See LICENSE.TXT for details.
7de1ab26fSDiego Novillo //
8de1ab26fSDiego Novillo //===----------------------------------------------------------------------===//
9de1ab26fSDiego Novillo //
10de1ab26fSDiego Novillo // This file implements the class that reads LLVM sample profiles. It
11bb5605caSDiego Novillo // supports three file formats: text, binary and gcov.
12de1ab26fSDiego Novillo //
13bb5605caSDiego Novillo // The textual representation is useful for debugging and testing purposes. The
14bb5605caSDiego Novillo // binary representation is more compact, resulting in smaller file sizes.
15de1ab26fSDiego Novillo //
16bb5605caSDiego Novillo // The gcov encoding is the one generated by GCC's AutoFDO profile creation
17bb5605caSDiego Novillo // tool (https://github.com/google/autofdo)
18de1ab26fSDiego Novillo //
19bb5605caSDiego Novillo // All three encodings can be used interchangeably as an input sample profile.
20de1ab26fSDiego Novillo //
21de1ab26fSDiego Novillo //===----------------------------------------------------------------------===//
22de1ab26fSDiego Novillo 
23de1ab26fSDiego Novillo #include "llvm/ProfileData/SampleProfReader.h"
24b93483dbSDiego Novillo #include "llvm/ADT/DenseMap.h"
2540ee23dbSEaswaran Raman #include "llvm/ADT/STLExtras.h"
26e78d131aSEugene Zelenko #include "llvm/ADT/StringRef.h"
27e78d131aSEugene Zelenko #include "llvm/IR/ProfileSummary.h"
28e78d131aSEugene Zelenko #include "llvm/ProfileData/ProfileCommon.h"
29e78d131aSEugene Zelenko #include "llvm/ProfileData/SampleProf.h"
30de1ab26fSDiego Novillo #include "llvm/Support/ErrorOr.h"
31c572e92cSDiego Novillo #include "llvm/Support/LEB128.h"
32de1ab26fSDiego Novillo #include "llvm/Support/LineIterator.h"
33c572e92cSDiego Novillo #include "llvm/Support/MemoryBuffer.h"
34e78d131aSEugene Zelenko #include "llvm/Support/raw_ostream.h"
35e78d131aSEugene Zelenko #include <algorithm>
36e78d131aSEugene Zelenko #include <cstddef>
37e78d131aSEugene Zelenko #include <cstdint>
38e78d131aSEugene Zelenko #include <limits>
39e78d131aSEugene Zelenko #include <memory>
40e78d131aSEugene Zelenko #include <system_error>
41e78d131aSEugene Zelenko #include <vector>
42de1ab26fSDiego Novillo 
43de1ab26fSDiego Novillo using namespace llvm;
44e78d131aSEugene Zelenko using namespace sampleprof;
45de1ab26fSDiego Novillo 
46de1ab26fSDiego Novillo /// \brief Dump the function profile for \p FName.
47de1ab26fSDiego Novillo ///
48de1ab26fSDiego Novillo /// \param FName Name of the function to print.
49d5336ae2SDiego Novillo /// \param OS Stream to emit the output to.
50d5336ae2SDiego Novillo void SampleProfileReader::dumpFunctionProfile(StringRef FName,
51d5336ae2SDiego Novillo                                               raw_ostream &OS) {
528e415a82SDiego Novillo   OS << "Function: " << FName << ": " << Profiles[FName];
53de1ab26fSDiego Novillo }
54de1ab26fSDiego Novillo 
55d5336ae2SDiego Novillo /// \brief Dump all the function profiles found on stream \p OS.
56d5336ae2SDiego Novillo void SampleProfileReader::dump(raw_ostream &OS) {
57d5336ae2SDiego Novillo   for (const auto &I : Profiles)
58d5336ae2SDiego Novillo     dumpFunctionProfile(I.getKey(), OS);
59de1ab26fSDiego Novillo }
60de1ab26fSDiego Novillo 
616722688eSDehao Chen /// \brief Parse \p Input as function head.
626722688eSDehao Chen ///
636722688eSDehao Chen /// Parse one line of \p Input, and update function name in \p FName,
646722688eSDehao Chen /// function's total sample count in \p NumSamples, function's entry
656722688eSDehao Chen /// count in \p NumHeadSamples.
666722688eSDehao Chen ///
676722688eSDehao Chen /// \returns true if parsing is successful.
686722688eSDehao Chen static bool ParseHead(const StringRef &Input, StringRef &FName,
6938be3330SDiego Novillo                       uint64_t &NumSamples, uint64_t &NumHeadSamples) {
706722688eSDehao Chen   if (Input[0] == ' ')
716722688eSDehao Chen     return false;
726722688eSDehao Chen   size_t n2 = Input.rfind(':');
736722688eSDehao Chen   size_t n1 = Input.rfind(':', n2 - 1);
746722688eSDehao Chen   FName = Input.substr(0, n1);
756722688eSDehao Chen   if (Input.substr(n1 + 1, n2 - n1 - 1).getAsInteger(10, NumSamples))
766722688eSDehao Chen     return false;
776722688eSDehao Chen   if (Input.substr(n2 + 1).getAsInteger(10, NumHeadSamples))
786722688eSDehao Chen     return false;
796722688eSDehao Chen   return true;
806722688eSDehao Chen }
816722688eSDehao Chen 
8210042412SDehao Chen /// \brief Returns true if line offset \p L is legal (only has 16 bits).
8357d1dda5SDehao Chen static bool isOffsetLegal(unsigned L) { return (L & 0xffff) == L; }
8410042412SDehao Chen 
856722688eSDehao Chen /// \brief Parse \p Input as line sample.
866722688eSDehao Chen ///
876722688eSDehao Chen /// \param Input input line.
886722688eSDehao Chen /// \param IsCallsite true if the line represents an inlined callsite.
896722688eSDehao Chen /// \param Depth the depth of the inline stack.
906722688eSDehao Chen /// \param NumSamples total samples of the line/inlined callsite.
916722688eSDehao Chen /// \param LineOffset line offset to the start of the function.
926722688eSDehao Chen /// \param Discriminator discriminator of the line.
936722688eSDehao Chen /// \param TargetCountMap map from indirect call target to count.
946722688eSDehao Chen ///
956722688eSDehao Chen /// returns true if parsing is successful.
9638be3330SDiego Novillo static bool ParseLine(const StringRef &Input, bool &IsCallsite, uint32_t &Depth,
9738be3330SDiego Novillo                       uint64_t &NumSamples, uint32_t &LineOffset,
9838be3330SDiego Novillo                       uint32_t &Discriminator, StringRef &CalleeName,
9938be3330SDiego Novillo                       DenseMap<StringRef, uint64_t> &TargetCountMap) {
1006722688eSDehao Chen   for (Depth = 0; Input[Depth] == ' '; Depth++)
1016722688eSDehao Chen     ;
1026722688eSDehao Chen   if (Depth == 0)
1036722688eSDehao Chen     return false;
1046722688eSDehao Chen 
1056722688eSDehao Chen   size_t n1 = Input.find(':');
1066722688eSDehao Chen   StringRef Loc = Input.substr(Depth, n1 - Depth);
1076722688eSDehao Chen   size_t n2 = Loc.find('.');
1086722688eSDehao Chen   if (n2 == StringRef::npos) {
10910042412SDehao Chen     if (Loc.getAsInteger(10, LineOffset) || !isOffsetLegal(LineOffset))
1106722688eSDehao Chen       return false;
1116722688eSDehao Chen     Discriminator = 0;
1126722688eSDehao Chen   } else {
1136722688eSDehao Chen     if (Loc.substr(0, n2).getAsInteger(10, LineOffset))
1146722688eSDehao Chen       return false;
1156722688eSDehao Chen     if (Loc.substr(n2 + 1).getAsInteger(10, Discriminator))
1166722688eSDehao Chen       return false;
1176722688eSDehao Chen   }
1186722688eSDehao Chen 
1196722688eSDehao Chen   StringRef Rest = Input.substr(n1 + 2);
1206722688eSDehao Chen   if (Rest[0] >= '0' && Rest[0] <= '9') {
1216722688eSDehao Chen     IsCallsite = false;
1226722688eSDehao Chen     size_t n3 = Rest.find(' ');
1236722688eSDehao Chen     if (n3 == StringRef::npos) {
1246722688eSDehao Chen       if (Rest.getAsInteger(10, NumSamples))
1256722688eSDehao Chen         return false;
1266722688eSDehao Chen     } else {
1276722688eSDehao Chen       if (Rest.substr(0, n3).getAsInteger(10, NumSamples))
1286722688eSDehao Chen         return false;
1296722688eSDehao Chen     }
1306722688eSDehao Chen     while (n3 != StringRef::npos) {
1316722688eSDehao Chen       n3 += Rest.substr(n3).find_first_not_of(' ');
1326722688eSDehao Chen       Rest = Rest.substr(n3);
1336722688eSDehao Chen       n3 = Rest.find(' ');
1346722688eSDehao Chen       StringRef pair = Rest;
1356722688eSDehao Chen       if (n3 != StringRef::npos) {
1366722688eSDehao Chen         pair = Rest.substr(0, n3);
1376722688eSDehao Chen       }
13838be3330SDiego Novillo       size_t n4 = pair.find(':');
13938be3330SDiego Novillo       uint64_t count;
1406722688eSDehao Chen       if (pair.substr(n4 + 1).getAsInteger(10, count))
1416722688eSDehao Chen         return false;
1426722688eSDehao Chen       TargetCountMap[pair.substr(0, n4)] = count;
1436722688eSDehao Chen     }
1446722688eSDehao Chen   } else {
1456722688eSDehao Chen     IsCallsite = true;
14638be3330SDiego Novillo     size_t n3 = Rest.find_last_of(':');
1476722688eSDehao Chen     CalleeName = Rest.substr(0, n3);
1486722688eSDehao Chen     if (Rest.substr(n3 + 1).getAsInteger(10, NumSamples))
1496722688eSDehao Chen       return false;
1506722688eSDehao Chen   }
1516722688eSDehao Chen   return true;
1526722688eSDehao Chen }
1536722688eSDehao Chen 
154de1ab26fSDiego Novillo /// \brief Load samples from a text file.
155de1ab26fSDiego Novillo ///
156de1ab26fSDiego Novillo /// See the documentation at the top of the file for an explanation of
157de1ab26fSDiego Novillo /// the expected format.
158de1ab26fSDiego Novillo ///
159de1ab26fSDiego Novillo /// \returns true if the file was loaded successfully, false otherwise.
160c572e92cSDiego Novillo std::error_code SampleProfileReaderText::read() {
161c572e92cSDiego Novillo   line_iterator LineIt(*Buffer, /*SkipBlanks=*/true, '#');
16248dd080cSNathan Slingerland   sampleprof_error Result = sampleprof_error::success;
163de1ab26fSDiego Novillo 
164aae1ed8eSDiego Novillo   InlineCallStack InlineStack;
1656722688eSDehao Chen 
1666722688eSDehao Chen   for (; !LineIt.is_at_eof(); ++LineIt) {
1676722688eSDehao Chen     if ((*LineIt)[(*LineIt).find_first_not_of(' ')] == '#')
1686722688eSDehao Chen       continue;
169de1ab26fSDiego Novillo     // Read the header of each function.
170de1ab26fSDiego Novillo     //
171de1ab26fSDiego Novillo     // Note that for function identifiers we are actually expecting
172de1ab26fSDiego Novillo     // mangled names, but we may not always get them. This happens when
173de1ab26fSDiego Novillo     // the compiler decides not to emit the function (e.g., it was inlined
174de1ab26fSDiego Novillo     // and removed). In this case, the binary will not have the linkage
175de1ab26fSDiego Novillo     // name for the function, so the profiler will emit the function's
176de1ab26fSDiego Novillo     // unmangled name, which may contain characters like ':' and '>' in its
177de1ab26fSDiego Novillo     // name (member functions, templates, etc).
178de1ab26fSDiego Novillo     //
179de1ab26fSDiego Novillo     // The only requirement we place on the identifier, then, is that it
180de1ab26fSDiego Novillo     // should not begin with a number.
1816722688eSDehao Chen     if ((*LineIt)[0] != ' ') {
18238be3330SDiego Novillo       uint64_t NumSamples, NumHeadSamples;
1836722688eSDehao Chen       StringRef FName;
1846722688eSDehao Chen       if (!ParseHead(*LineIt, FName, NumSamples, NumHeadSamples)) {
1853376a787SDiego Novillo         reportError(LineIt.line_number(),
186de1ab26fSDiego Novillo                     "Expected 'mangled_name:NUM:NUM', found " + *LineIt);
187c572e92cSDiego Novillo         return sampleprof_error::malformed;
188de1ab26fSDiego Novillo       }
189de1ab26fSDiego Novillo       Profiles[FName] = FunctionSamples();
190de1ab26fSDiego Novillo       FunctionSamples &FProfile = Profiles[FName];
19157d1dda5SDehao Chen       FProfile.setName(FName);
19248dd080cSNathan Slingerland       MergeResult(Result, FProfile.addTotalSamples(NumSamples));
19348dd080cSNathan Slingerland       MergeResult(Result, FProfile.addHeadSamples(NumHeadSamples));
1946722688eSDehao Chen       InlineStack.clear();
1956722688eSDehao Chen       InlineStack.push_back(&FProfile);
1966722688eSDehao Chen     } else {
19738be3330SDiego Novillo       uint64_t NumSamples;
1986722688eSDehao Chen       StringRef FName;
19938be3330SDiego Novillo       DenseMap<StringRef, uint64_t> TargetCountMap;
2006722688eSDehao Chen       bool IsCallsite;
20138be3330SDiego Novillo       uint32_t Depth, LineOffset, Discriminator;
2026722688eSDehao Chen       if (!ParseLine(*LineIt, IsCallsite, Depth, NumSamples, LineOffset,
2036722688eSDehao Chen                      Discriminator, FName, TargetCountMap)) {
2043376a787SDiego Novillo         reportError(LineIt.line_number(),
2053376a787SDiego Novillo                     "Expected 'NUM[.NUM]: NUM[ mangled_name:NUM]*', found " +
2063376a787SDiego Novillo                         *LineIt);
207c572e92cSDiego Novillo         return sampleprof_error::malformed;
208de1ab26fSDiego Novillo       }
2096722688eSDehao Chen       if (IsCallsite) {
2106722688eSDehao Chen         while (InlineStack.size() > Depth) {
2116722688eSDehao Chen           InlineStack.pop_back();
212c572e92cSDiego Novillo         }
2136722688eSDehao Chen         FunctionSamples &FSamples = InlineStack.back()->functionSamplesAt(
2142c7ca9b5SDehao Chen             LineLocation(LineOffset, Discriminator))[FName];
21557d1dda5SDehao Chen         FSamples.setName(FName);
21648dd080cSNathan Slingerland         MergeResult(Result, FSamples.addTotalSamples(NumSamples));
2176722688eSDehao Chen         InlineStack.push_back(&FSamples);
2186722688eSDehao Chen       } else {
2196722688eSDehao Chen         while (InlineStack.size() > Depth) {
2206722688eSDehao Chen           InlineStack.pop_back();
2216722688eSDehao Chen         }
2226722688eSDehao Chen         FunctionSamples &FProfile = *InlineStack.back();
2236722688eSDehao Chen         for (const auto &name_count : TargetCountMap) {
22448dd080cSNathan Slingerland           MergeResult(Result, FProfile.addCalledTargetSamples(
22548dd080cSNathan Slingerland                                   LineOffset, Discriminator, name_count.first,
22648dd080cSNathan Slingerland                                   name_count.second));
227c572e92cSDiego Novillo         }
22848dd080cSNathan Slingerland         MergeResult(Result, FProfile.addBodySamples(LineOffset, Discriminator,
22948dd080cSNathan Slingerland                                                     NumSamples));
2306722688eSDehao Chen       }
231de1ab26fSDiego Novillo     }
232de1ab26fSDiego Novillo   }
23340ee23dbSEaswaran Raman   if (Result == sampleprof_error::success)
23440ee23dbSEaswaran Raman     computeSummary();
235de1ab26fSDiego Novillo 
23648dd080cSNathan Slingerland   return Result;
237de1ab26fSDiego Novillo }
238de1ab26fSDiego Novillo 
2394f823667SNathan Slingerland bool SampleProfileReaderText::hasFormat(const MemoryBuffer &Buffer) {
2404f823667SNathan Slingerland   bool result = false;
2414f823667SNathan Slingerland 
2424f823667SNathan Slingerland   // Check that the first non-comment line is a valid function header.
2434f823667SNathan Slingerland   line_iterator LineIt(Buffer, /*SkipBlanks=*/true, '#');
2444f823667SNathan Slingerland   if (!LineIt.is_at_eof()) {
2454f823667SNathan Slingerland     if ((*LineIt)[0] != ' ') {
2464f823667SNathan Slingerland       uint64_t NumSamples, NumHeadSamples;
2474f823667SNathan Slingerland       StringRef FName;
2484f823667SNathan Slingerland       result = ParseHead(*LineIt, FName, NumSamples, NumHeadSamples);
2494f823667SNathan Slingerland     }
2504f823667SNathan Slingerland   }
2514f823667SNathan Slingerland 
2524f823667SNathan Slingerland   return result;
2534f823667SNathan Slingerland }
2544f823667SNathan Slingerland 
255d5336ae2SDiego Novillo template <typename T> ErrorOr<T> SampleProfileReaderBinary::readNumber() {
256c572e92cSDiego Novillo   unsigned NumBytesRead = 0;
257c572e92cSDiego Novillo   std::error_code EC;
258c572e92cSDiego Novillo   uint64_t Val = decodeULEB128(Data, &NumBytesRead);
259c572e92cSDiego Novillo 
260c572e92cSDiego Novillo   if (Val > std::numeric_limits<T>::max())
261c572e92cSDiego Novillo     EC = sampleprof_error::malformed;
262c572e92cSDiego Novillo   else if (Data + NumBytesRead > End)
263c572e92cSDiego Novillo     EC = sampleprof_error::truncated;
264c572e92cSDiego Novillo   else
265c572e92cSDiego Novillo     EC = sampleprof_error::success;
266c572e92cSDiego Novillo 
267c572e92cSDiego Novillo   if (EC) {
2683376a787SDiego Novillo     reportError(0, EC.message());
269c572e92cSDiego Novillo     return EC;
270c572e92cSDiego Novillo   }
271c572e92cSDiego Novillo 
272c572e92cSDiego Novillo   Data += NumBytesRead;
273c572e92cSDiego Novillo   return static_cast<T>(Val);
274c572e92cSDiego Novillo }
275c572e92cSDiego Novillo 
276c572e92cSDiego Novillo ErrorOr<StringRef> SampleProfileReaderBinary::readString() {
277c572e92cSDiego Novillo   std::error_code EC;
278c572e92cSDiego Novillo   StringRef Str(reinterpret_cast<const char *>(Data));
279c572e92cSDiego Novillo   if (Data + Str.size() + 1 > End) {
280c572e92cSDiego Novillo     EC = sampleprof_error::truncated;
2813376a787SDiego Novillo     reportError(0, EC.message());
282c572e92cSDiego Novillo     return EC;
283c572e92cSDiego Novillo   }
284c572e92cSDiego Novillo 
285c572e92cSDiego Novillo   Data += Str.size() + 1;
286c572e92cSDiego Novillo   return Str;
287c572e92cSDiego Novillo }
288c572e92cSDiego Novillo 
289760c5a8fSDiego Novillo ErrorOr<StringRef> SampleProfileReaderBinary::readStringFromTable() {
290760c5a8fSDiego Novillo   std::error_code EC;
29138be3330SDiego Novillo   auto Idx = readNumber<uint32_t>();
292760c5a8fSDiego Novillo   if (std::error_code EC = Idx.getError())
293760c5a8fSDiego Novillo     return EC;
294760c5a8fSDiego Novillo   if (*Idx >= NameTable.size())
295760c5a8fSDiego Novillo     return sampleprof_error::truncated_name_table;
296760c5a8fSDiego Novillo   return NameTable[*Idx];
297760c5a8fSDiego Novillo }
298760c5a8fSDiego Novillo 
299a7f1e8efSDiego Novillo std::error_code
300a7f1e8efSDiego Novillo SampleProfileReaderBinary::readProfile(FunctionSamples &FProfile) {
301b93483dbSDiego Novillo   auto NumSamples = readNumber<uint64_t>();
302b93483dbSDiego Novillo   if (std::error_code EC = NumSamples.getError())
303c572e92cSDiego Novillo     return EC;
304b93483dbSDiego Novillo   FProfile.addTotalSamples(*NumSamples);
305c572e92cSDiego Novillo 
306c572e92cSDiego Novillo   // Read the samples in the body.
30738be3330SDiego Novillo   auto NumRecords = readNumber<uint32_t>();
308c572e92cSDiego Novillo   if (std::error_code EC = NumRecords.getError())
309c572e92cSDiego Novillo     return EC;
310a7f1e8efSDiego Novillo 
31138be3330SDiego Novillo   for (uint32_t I = 0; I < *NumRecords; ++I) {
312c572e92cSDiego Novillo     auto LineOffset = readNumber<uint64_t>();
313c572e92cSDiego Novillo     if (std::error_code EC = LineOffset.getError())
314c572e92cSDiego Novillo       return EC;
315c572e92cSDiego Novillo 
31610042412SDehao Chen     if (!isOffsetLegal(*LineOffset)) {
31710042412SDehao Chen       return std::error_code();
31810042412SDehao Chen     }
31910042412SDehao Chen 
320c572e92cSDiego Novillo     auto Discriminator = readNumber<uint64_t>();
321c572e92cSDiego Novillo     if (std::error_code EC = Discriminator.getError())
322c572e92cSDiego Novillo       return EC;
323c572e92cSDiego Novillo 
324c572e92cSDiego Novillo     auto NumSamples = readNumber<uint64_t>();
325c572e92cSDiego Novillo     if (std::error_code EC = NumSamples.getError())
326c572e92cSDiego Novillo       return EC;
327c572e92cSDiego Novillo 
32838be3330SDiego Novillo     auto NumCalls = readNumber<uint32_t>();
329c572e92cSDiego Novillo     if (std::error_code EC = NumCalls.getError())
330c572e92cSDiego Novillo       return EC;
331c572e92cSDiego Novillo 
33238be3330SDiego Novillo     for (uint32_t J = 0; J < *NumCalls; ++J) {
333760c5a8fSDiego Novillo       auto CalledFunction(readStringFromTable());
334c572e92cSDiego Novillo       if (std::error_code EC = CalledFunction.getError())
335c572e92cSDiego Novillo         return EC;
336c572e92cSDiego Novillo 
337c572e92cSDiego Novillo       auto CalledFunctionSamples = readNumber<uint64_t>();
338c572e92cSDiego Novillo       if (std::error_code EC = CalledFunctionSamples.getError())
339c572e92cSDiego Novillo         return EC;
340c572e92cSDiego Novillo 
341c572e92cSDiego Novillo       FProfile.addCalledTargetSamples(*LineOffset, *Discriminator,
342a7f1e8efSDiego Novillo                                       *CalledFunction, *CalledFunctionSamples);
343c572e92cSDiego Novillo     }
344c572e92cSDiego Novillo 
345c572e92cSDiego Novillo     FProfile.addBodySamples(*LineOffset, *Discriminator, *NumSamples);
346c572e92cSDiego Novillo   }
347a7f1e8efSDiego Novillo 
348a7f1e8efSDiego Novillo   // Read all the samples for inlined function calls.
34938be3330SDiego Novillo   auto NumCallsites = readNumber<uint32_t>();
350a7f1e8efSDiego Novillo   if (std::error_code EC = NumCallsites.getError())
351a7f1e8efSDiego Novillo     return EC;
352a7f1e8efSDiego Novillo 
35338be3330SDiego Novillo   for (uint32_t J = 0; J < *NumCallsites; ++J) {
354a7f1e8efSDiego Novillo     auto LineOffset = readNumber<uint64_t>();
355a7f1e8efSDiego Novillo     if (std::error_code EC = LineOffset.getError())
356a7f1e8efSDiego Novillo       return EC;
357a7f1e8efSDiego Novillo 
358a7f1e8efSDiego Novillo     auto Discriminator = readNumber<uint64_t>();
359a7f1e8efSDiego Novillo     if (std::error_code EC = Discriminator.getError())
360a7f1e8efSDiego Novillo       return EC;
361a7f1e8efSDiego Novillo 
362760c5a8fSDiego Novillo     auto FName(readStringFromTable());
363a7f1e8efSDiego Novillo     if (std::error_code EC = FName.getError())
364a7f1e8efSDiego Novillo       return EC;
365a7f1e8efSDiego Novillo 
3662c7ca9b5SDehao Chen     FunctionSamples &CalleeProfile = FProfile.functionSamplesAt(
3672c7ca9b5SDehao Chen         LineLocation(*LineOffset, *Discriminator))[*FName];
36857d1dda5SDehao Chen     CalleeProfile.setName(*FName);
369a7f1e8efSDiego Novillo     if (std::error_code EC = readProfile(CalleeProfile))
370a7f1e8efSDiego Novillo       return EC;
371a7f1e8efSDiego Novillo   }
372a7f1e8efSDiego Novillo 
373a7f1e8efSDiego Novillo   return sampleprof_error::success;
374a7f1e8efSDiego Novillo }
375a7f1e8efSDiego Novillo 
376a7f1e8efSDiego Novillo std::error_code SampleProfileReaderBinary::read() {
377a7f1e8efSDiego Novillo   while (!at_eof()) {
378b93483dbSDiego Novillo     auto NumHeadSamples = readNumber<uint64_t>();
379b93483dbSDiego Novillo     if (std::error_code EC = NumHeadSamples.getError())
380b93483dbSDiego Novillo       return EC;
381b93483dbSDiego Novillo 
382760c5a8fSDiego Novillo     auto FName(readStringFromTable());
383a7f1e8efSDiego Novillo     if (std::error_code EC = FName.getError())
384a7f1e8efSDiego Novillo       return EC;
385a7f1e8efSDiego Novillo 
386a7f1e8efSDiego Novillo     Profiles[*FName] = FunctionSamples();
387a7f1e8efSDiego Novillo     FunctionSamples &FProfile = Profiles[*FName];
38857d1dda5SDehao Chen     FProfile.setName(*FName);
389a7f1e8efSDiego Novillo 
390b93483dbSDiego Novillo     FProfile.addHeadSamples(*NumHeadSamples);
391b93483dbSDiego Novillo 
392a7f1e8efSDiego Novillo     if (std::error_code EC = readProfile(FProfile))
393a7f1e8efSDiego Novillo       return EC;
394c572e92cSDiego Novillo   }
395c572e92cSDiego Novillo 
396c572e92cSDiego Novillo   return sampleprof_error::success;
397c572e92cSDiego Novillo }
398c572e92cSDiego Novillo 
399c572e92cSDiego Novillo std::error_code SampleProfileReaderBinary::readHeader() {
400c572e92cSDiego Novillo   Data = reinterpret_cast<const uint8_t *>(Buffer->getBufferStart());
401c572e92cSDiego Novillo   End = Data + Buffer->getBufferSize();
402c572e92cSDiego Novillo 
403c572e92cSDiego Novillo   // Read and check the magic identifier.
404c572e92cSDiego Novillo   auto Magic = readNumber<uint64_t>();
405c572e92cSDiego Novillo   if (std::error_code EC = Magic.getError())
406c572e92cSDiego Novillo     return EC;
407c572e92cSDiego Novillo   else if (*Magic != SPMagic())
408c572e92cSDiego Novillo     return sampleprof_error::bad_magic;
409c572e92cSDiego Novillo 
410c572e92cSDiego Novillo   // Read the version number.
411c572e92cSDiego Novillo   auto Version = readNumber<uint64_t>();
412c572e92cSDiego Novillo   if (std::error_code EC = Version.getError())
413c572e92cSDiego Novillo     return EC;
414c572e92cSDiego Novillo   else if (*Version != SPVersion())
415c572e92cSDiego Novillo     return sampleprof_error::unsupported_version;
416c572e92cSDiego Novillo 
41740ee23dbSEaswaran Raman   if (std::error_code EC = readSummary())
41840ee23dbSEaswaran Raman     return EC;
41940ee23dbSEaswaran Raman 
420760c5a8fSDiego Novillo   // Read the name table.
42138be3330SDiego Novillo   auto Size = readNumber<uint32_t>();
422760c5a8fSDiego Novillo   if (std::error_code EC = Size.getError())
423760c5a8fSDiego Novillo     return EC;
424760c5a8fSDiego Novillo   NameTable.reserve(*Size);
42538be3330SDiego Novillo   for (uint32_t I = 0; I < *Size; ++I) {
426760c5a8fSDiego Novillo     auto Name(readString());
427760c5a8fSDiego Novillo     if (std::error_code EC = Name.getError())
428760c5a8fSDiego Novillo       return EC;
429760c5a8fSDiego Novillo     NameTable.push_back(*Name);
430760c5a8fSDiego Novillo   }
431760c5a8fSDiego Novillo 
432c572e92cSDiego Novillo   return sampleprof_error::success;
433c572e92cSDiego Novillo }
434c572e92cSDiego Novillo 
43540ee23dbSEaswaran Raman std::error_code SampleProfileReaderBinary::readSummaryEntry(
43640ee23dbSEaswaran Raman     std::vector<ProfileSummaryEntry> &Entries) {
43740ee23dbSEaswaran Raman   auto Cutoff = readNumber<uint64_t>();
43840ee23dbSEaswaran Raman   if (std::error_code EC = Cutoff.getError())
43940ee23dbSEaswaran Raman     return EC;
44040ee23dbSEaswaran Raman 
44140ee23dbSEaswaran Raman   auto MinBlockCount = readNumber<uint64_t>();
44240ee23dbSEaswaran Raman   if (std::error_code EC = MinBlockCount.getError())
44340ee23dbSEaswaran Raman     return EC;
44440ee23dbSEaswaran Raman 
44540ee23dbSEaswaran Raman   auto NumBlocks = readNumber<uint64_t>();
44640ee23dbSEaswaran Raman   if (std::error_code EC = NumBlocks.getError())
44740ee23dbSEaswaran Raman     return EC;
44840ee23dbSEaswaran Raman 
44940ee23dbSEaswaran Raman   Entries.emplace_back(*Cutoff, *MinBlockCount, *NumBlocks);
45040ee23dbSEaswaran Raman   return sampleprof_error::success;
45140ee23dbSEaswaran Raman }
45240ee23dbSEaswaran Raman 
45340ee23dbSEaswaran Raman std::error_code SampleProfileReaderBinary::readSummary() {
45440ee23dbSEaswaran Raman   auto TotalCount = readNumber<uint64_t>();
45540ee23dbSEaswaran Raman   if (std::error_code EC = TotalCount.getError())
45640ee23dbSEaswaran Raman     return EC;
45740ee23dbSEaswaran Raman 
45840ee23dbSEaswaran Raman   auto MaxBlockCount = readNumber<uint64_t>();
45940ee23dbSEaswaran Raman   if (std::error_code EC = MaxBlockCount.getError())
46040ee23dbSEaswaran Raman     return EC;
46140ee23dbSEaswaran Raman 
46240ee23dbSEaswaran Raman   auto MaxFunctionCount = readNumber<uint64_t>();
46340ee23dbSEaswaran Raman   if (std::error_code EC = MaxFunctionCount.getError())
46440ee23dbSEaswaran Raman     return EC;
46540ee23dbSEaswaran Raman 
46640ee23dbSEaswaran Raman   auto NumBlocks = readNumber<uint64_t>();
46740ee23dbSEaswaran Raman   if (std::error_code EC = NumBlocks.getError())
46840ee23dbSEaswaran Raman     return EC;
46940ee23dbSEaswaran Raman 
47040ee23dbSEaswaran Raman   auto NumFunctions = readNumber<uint64_t>();
47140ee23dbSEaswaran Raman   if (std::error_code EC = NumFunctions.getError())
47240ee23dbSEaswaran Raman     return EC;
47340ee23dbSEaswaran Raman 
47440ee23dbSEaswaran Raman   auto NumSummaryEntries = readNumber<uint64_t>();
47540ee23dbSEaswaran Raman   if (std::error_code EC = NumSummaryEntries.getError())
47640ee23dbSEaswaran Raman     return EC;
47740ee23dbSEaswaran Raman 
47840ee23dbSEaswaran Raman   std::vector<ProfileSummaryEntry> Entries;
47940ee23dbSEaswaran Raman   for (unsigned i = 0; i < *NumSummaryEntries; i++) {
48040ee23dbSEaswaran Raman     std::error_code EC = readSummaryEntry(Entries);
48140ee23dbSEaswaran Raman     if (EC != sampleprof_error::success)
48240ee23dbSEaswaran Raman       return EC;
48340ee23dbSEaswaran Raman   }
4847cefdb81SEaswaran Raman   Summary = llvm::make_unique<ProfileSummary>(
4857cefdb81SEaswaran Raman       ProfileSummary::PSK_Sample, Entries, *TotalCount, *MaxBlockCount, 0,
4867cefdb81SEaswaran Raman       *MaxFunctionCount, *NumBlocks, *NumFunctions);
48740ee23dbSEaswaran Raman 
48840ee23dbSEaswaran Raman   return sampleprof_error::success;
48940ee23dbSEaswaran Raman }
49040ee23dbSEaswaran Raman 
491c572e92cSDiego Novillo bool SampleProfileReaderBinary::hasFormat(const MemoryBuffer &Buffer) {
492c572e92cSDiego Novillo   const uint8_t *Data =
493c572e92cSDiego Novillo       reinterpret_cast<const uint8_t *>(Buffer.getBufferStart());
494c572e92cSDiego Novillo   uint64_t Magic = decodeULEB128(Data);
495c572e92cSDiego Novillo   return Magic == SPMagic();
496c572e92cSDiego Novillo }
497c572e92cSDiego Novillo 
4983376a787SDiego Novillo std::error_code SampleProfileReaderGCC::skipNextWord() {
4993376a787SDiego Novillo   uint32_t dummy;
5003376a787SDiego Novillo   if (!GcovBuffer.readInt(dummy))
5013376a787SDiego Novillo     return sampleprof_error::truncated;
5023376a787SDiego Novillo   return sampleprof_error::success;
5033376a787SDiego Novillo }
5043376a787SDiego Novillo 
5053376a787SDiego Novillo template <typename T> ErrorOr<T> SampleProfileReaderGCC::readNumber() {
5063376a787SDiego Novillo   if (sizeof(T) <= sizeof(uint32_t)) {
5073376a787SDiego Novillo     uint32_t Val;
5083376a787SDiego Novillo     if (GcovBuffer.readInt(Val) && Val <= std::numeric_limits<T>::max())
5093376a787SDiego Novillo       return static_cast<T>(Val);
5103376a787SDiego Novillo   } else if (sizeof(T) <= sizeof(uint64_t)) {
5113376a787SDiego Novillo     uint64_t Val;
5123376a787SDiego Novillo     if (GcovBuffer.readInt64(Val) && Val <= std::numeric_limits<T>::max())
5133376a787SDiego Novillo       return static_cast<T>(Val);
5143376a787SDiego Novillo   }
5153376a787SDiego Novillo 
5163376a787SDiego Novillo   std::error_code EC = sampleprof_error::malformed;
5173376a787SDiego Novillo   reportError(0, EC.message());
5183376a787SDiego Novillo   return EC;
5193376a787SDiego Novillo }
5203376a787SDiego Novillo 
5213376a787SDiego Novillo ErrorOr<StringRef> SampleProfileReaderGCC::readString() {
5223376a787SDiego Novillo   StringRef Str;
5233376a787SDiego Novillo   if (!GcovBuffer.readString(Str))
5243376a787SDiego Novillo     return sampleprof_error::truncated;
5253376a787SDiego Novillo   return Str;
5263376a787SDiego Novillo }
5273376a787SDiego Novillo 
5283376a787SDiego Novillo std::error_code SampleProfileReaderGCC::readHeader() {
5293376a787SDiego Novillo   // Read the magic identifier.
5303376a787SDiego Novillo   if (!GcovBuffer.readGCDAFormat())
5313376a787SDiego Novillo     return sampleprof_error::unrecognized_format;
5323376a787SDiego Novillo 
5333376a787SDiego Novillo   // Read the version number. Note - the GCC reader does not validate this
5343376a787SDiego Novillo   // version, but the profile creator generates v704.
5353376a787SDiego Novillo   GCOV::GCOVVersion version;
5363376a787SDiego Novillo   if (!GcovBuffer.readGCOVVersion(version))
5373376a787SDiego Novillo     return sampleprof_error::unrecognized_format;
5383376a787SDiego Novillo 
5393376a787SDiego Novillo   if (version != GCOV::V704)
5403376a787SDiego Novillo     return sampleprof_error::unsupported_version;
5413376a787SDiego Novillo 
5423376a787SDiego Novillo   // Skip the empty integer.
5433376a787SDiego Novillo   if (std::error_code EC = skipNextWord())
5443376a787SDiego Novillo     return EC;
5453376a787SDiego Novillo 
5463376a787SDiego Novillo   return sampleprof_error::success;
5473376a787SDiego Novillo }
5483376a787SDiego Novillo 
5493376a787SDiego Novillo std::error_code SampleProfileReaderGCC::readSectionTag(uint32_t Expected) {
5503376a787SDiego Novillo   uint32_t Tag;
5513376a787SDiego Novillo   if (!GcovBuffer.readInt(Tag))
5523376a787SDiego Novillo     return sampleprof_error::truncated;
5533376a787SDiego Novillo 
5543376a787SDiego Novillo   if (Tag != Expected)
5553376a787SDiego Novillo     return sampleprof_error::malformed;
5563376a787SDiego Novillo 
5573376a787SDiego Novillo   if (std::error_code EC = skipNextWord())
5583376a787SDiego Novillo     return EC;
5593376a787SDiego Novillo 
5603376a787SDiego Novillo   return sampleprof_error::success;
5613376a787SDiego Novillo }
5623376a787SDiego Novillo 
5633376a787SDiego Novillo std::error_code SampleProfileReaderGCC::readNameTable() {
5643376a787SDiego Novillo   if (std::error_code EC = readSectionTag(GCOVTagAFDOFileNames))
5653376a787SDiego Novillo     return EC;
5663376a787SDiego Novillo 
5673376a787SDiego Novillo   uint32_t Size;
5683376a787SDiego Novillo   if (!GcovBuffer.readInt(Size))
5693376a787SDiego Novillo     return sampleprof_error::truncated;
5703376a787SDiego Novillo 
5713376a787SDiego Novillo   for (uint32_t I = 0; I < Size; ++I) {
5723376a787SDiego Novillo     StringRef Str;
5733376a787SDiego Novillo     if (!GcovBuffer.readString(Str))
5743376a787SDiego Novillo       return sampleprof_error::truncated;
5753376a787SDiego Novillo     Names.push_back(Str);
5763376a787SDiego Novillo   }
5773376a787SDiego Novillo 
5783376a787SDiego Novillo   return sampleprof_error::success;
5793376a787SDiego Novillo }
5803376a787SDiego Novillo 
5813376a787SDiego Novillo std::error_code SampleProfileReaderGCC::readFunctionProfiles() {
5823376a787SDiego Novillo   if (std::error_code EC = readSectionTag(GCOVTagAFDOFunction))
5833376a787SDiego Novillo     return EC;
5843376a787SDiego Novillo 
5853376a787SDiego Novillo   uint32_t NumFunctions;
5863376a787SDiego Novillo   if (!GcovBuffer.readInt(NumFunctions))
5873376a787SDiego Novillo     return sampleprof_error::truncated;
5883376a787SDiego Novillo 
589aae1ed8eSDiego Novillo   InlineCallStack Stack;
5903376a787SDiego Novillo   for (uint32_t I = 0; I < NumFunctions; ++I)
591aae1ed8eSDiego Novillo     if (std::error_code EC = readOneFunctionProfile(Stack, true, 0))
5923376a787SDiego Novillo       return EC;
5933376a787SDiego Novillo 
59440ee23dbSEaswaran Raman   computeSummary();
5953376a787SDiego Novillo   return sampleprof_error::success;
5963376a787SDiego Novillo }
5973376a787SDiego Novillo 
598aae1ed8eSDiego Novillo std::error_code SampleProfileReaderGCC::readOneFunctionProfile(
599aae1ed8eSDiego Novillo     const InlineCallStack &InlineStack, bool Update, uint32_t Offset) {
6003376a787SDiego Novillo   uint64_t HeadCount = 0;
601aae1ed8eSDiego Novillo   if (InlineStack.size() == 0)
6023376a787SDiego Novillo     if (!GcovBuffer.readInt64(HeadCount))
6033376a787SDiego Novillo       return sampleprof_error::truncated;
6043376a787SDiego Novillo 
6053376a787SDiego Novillo   uint32_t NameIdx;
6063376a787SDiego Novillo   if (!GcovBuffer.readInt(NameIdx))
6073376a787SDiego Novillo     return sampleprof_error::truncated;
6083376a787SDiego Novillo 
6093376a787SDiego Novillo   StringRef Name(Names[NameIdx]);
6103376a787SDiego Novillo 
6113376a787SDiego Novillo   uint32_t NumPosCounts;
6123376a787SDiego Novillo   if (!GcovBuffer.readInt(NumPosCounts))
6133376a787SDiego Novillo     return sampleprof_error::truncated;
6143376a787SDiego Novillo 
615aae1ed8eSDiego Novillo   uint32_t NumCallsites;
616aae1ed8eSDiego Novillo   if (!GcovBuffer.readInt(NumCallsites))
6173376a787SDiego Novillo     return sampleprof_error::truncated;
6183376a787SDiego Novillo 
619aae1ed8eSDiego Novillo   FunctionSamples *FProfile = nullptr;
620aae1ed8eSDiego Novillo   if (InlineStack.size() == 0) {
621aae1ed8eSDiego Novillo     // If this is a top function that we have already processed, do not
622aae1ed8eSDiego Novillo     // update its profile again.  This happens in the presence of
623aae1ed8eSDiego Novillo     // function aliases.  Since these aliases share the same function
624aae1ed8eSDiego Novillo     // body, there will be identical replicated profiles for the
625aae1ed8eSDiego Novillo     // original function.  In this case, we simply not bother updating
626aae1ed8eSDiego Novillo     // the profile of the original function.
627aae1ed8eSDiego Novillo     FProfile = &Profiles[Name];
628aae1ed8eSDiego Novillo     FProfile->addHeadSamples(HeadCount);
629aae1ed8eSDiego Novillo     if (FProfile->getTotalSamples() > 0)
6303376a787SDiego Novillo       Update = false;
631aae1ed8eSDiego Novillo   } else {
632aae1ed8eSDiego Novillo     // Otherwise, we are reading an inlined instance. The top of the
633aae1ed8eSDiego Novillo     // inline stack contains the profile of the caller. Insert this
634aae1ed8eSDiego Novillo     // callee in the caller's CallsiteMap.
635aae1ed8eSDiego Novillo     FunctionSamples *CallerProfile = InlineStack.front();
636aae1ed8eSDiego Novillo     uint32_t LineOffset = Offset >> 16;
637aae1ed8eSDiego Novillo     uint32_t Discriminator = Offset & 0xffff;
638aae1ed8eSDiego Novillo     FProfile = &CallerProfile->functionSamplesAt(
6392c7ca9b5SDehao Chen         LineLocation(LineOffset, Discriminator))[Name];
6403376a787SDiego Novillo   }
64157d1dda5SDehao Chen   FProfile->setName(Name);
6423376a787SDiego Novillo 
6433376a787SDiego Novillo   for (uint32_t I = 0; I < NumPosCounts; ++I) {
6443376a787SDiego Novillo     uint32_t Offset;
6453376a787SDiego Novillo     if (!GcovBuffer.readInt(Offset))
6463376a787SDiego Novillo       return sampleprof_error::truncated;
6473376a787SDiego Novillo 
6483376a787SDiego Novillo     uint32_t NumTargets;
6493376a787SDiego Novillo     if (!GcovBuffer.readInt(NumTargets))
6503376a787SDiego Novillo       return sampleprof_error::truncated;
6513376a787SDiego Novillo 
6523376a787SDiego Novillo     uint64_t Count;
6533376a787SDiego Novillo     if (!GcovBuffer.readInt64(Count))
6543376a787SDiego Novillo       return sampleprof_error::truncated;
6553376a787SDiego Novillo 
656aae1ed8eSDiego Novillo     // The line location is encoded in the offset as:
657aae1ed8eSDiego Novillo     //   high 16 bits: line offset to the start of the function.
658aae1ed8eSDiego Novillo     //   low 16 bits: discriminator.
659aae1ed8eSDiego Novillo     uint32_t LineOffset = Offset >> 16;
660aae1ed8eSDiego Novillo     uint32_t Discriminator = Offset & 0xffff;
6613376a787SDiego Novillo 
662aae1ed8eSDiego Novillo     InlineCallStack NewStack;
663aae1ed8eSDiego Novillo     NewStack.push_back(FProfile);
664aae1ed8eSDiego Novillo     NewStack.insert(NewStack.end(), InlineStack.begin(), InlineStack.end());
665aae1ed8eSDiego Novillo     if (Update) {
666aae1ed8eSDiego Novillo       // Walk up the inline stack, adding the samples on this line to
667aae1ed8eSDiego Novillo       // the total sample count of the callers in the chain.
668aae1ed8eSDiego Novillo       for (auto CallerProfile : NewStack)
669aae1ed8eSDiego Novillo         CallerProfile->addTotalSamples(Count);
670aae1ed8eSDiego Novillo 
671aae1ed8eSDiego Novillo       // Update the body samples for the current profile.
672aae1ed8eSDiego Novillo       FProfile->addBodySamples(LineOffset, Discriminator, Count);
673aae1ed8eSDiego Novillo     }
674aae1ed8eSDiego Novillo 
675aae1ed8eSDiego Novillo     // Process the list of functions called at an indirect call site.
676aae1ed8eSDiego Novillo     // These are all the targets that a function pointer (or virtual
677aae1ed8eSDiego Novillo     // function) resolved at runtime.
6783376a787SDiego Novillo     for (uint32_t J = 0; J < NumTargets; J++) {
6793376a787SDiego Novillo       uint32_t HistVal;
6803376a787SDiego Novillo       if (!GcovBuffer.readInt(HistVal))
6813376a787SDiego Novillo         return sampleprof_error::truncated;
6823376a787SDiego Novillo 
6833376a787SDiego Novillo       if (HistVal != HIST_TYPE_INDIR_CALL_TOPN)
6843376a787SDiego Novillo         return sampleprof_error::malformed;
6853376a787SDiego Novillo 
6863376a787SDiego Novillo       uint64_t TargetIdx;
6873376a787SDiego Novillo       if (!GcovBuffer.readInt64(TargetIdx))
6883376a787SDiego Novillo         return sampleprof_error::truncated;
6893376a787SDiego Novillo       StringRef TargetName(Names[TargetIdx]);
6903376a787SDiego Novillo 
6913376a787SDiego Novillo       uint64_t TargetCount;
6923376a787SDiego Novillo       if (!GcovBuffer.readInt64(TargetCount))
6933376a787SDiego Novillo         return sampleprof_error::truncated;
6943376a787SDiego Novillo 
695920677a9SDehao Chen       if (Update)
696920677a9SDehao Chen         FProfile->addCalledTargetSamples(LineOffset, Discriminator,
697aae1ed8eSDiego Novillo                                          TargetName, TargetCount);
6983376a787SDiego Novillo     }
6993376a787SDiego Novillo   }
7003376a787SDiego Novillo 
701aae1ed8eSDiego Novillo   // Process all the inlined callers into the current function. These
702aae1ed8eSDiego Novillo   // are all the callsites that were inlined into this function.
703aae1ed8eSDiego Novillo   for (uint32_t I = 0; I < NumCallsites; I++) {
7043376a787SDiego Novillo     // The offset is encoded as:
7053376a787SDiego Novillo     //   high 16 bits: line offset to the start of the function.
7063376a787SDiego Novillo     //   low 16 bits: discriminator.
7073376a787SDiego Novillo     uint32_t Offset;
7083376a787SDiego Novillo     if (!GcovBuffer.readInt(Offset))
7093376a787SDiego Novillo       return sampleprof_error::truncated;
710aae1ed8eSDiego Novillo     InlineCallStack NewStack;
711aae1ed8eSDiego Novillo     NewStack.push_back(FProfile);
712aae1ed8eSDiego Novillo     NewStack.insert(NewStack.end(), InlineStack.begin(), InlineStack.end());
713aae1ed8eSDiego Novillo     if (std::error_code EC = readOneFunctionProfile(NewStack, Update, Offset))
7143376a787SDiego Novillo       return EC;
7153376a787SDiego Novillo   }
7163376a787SDiego Novillo 
7173376a787SDiego Novillo   return sampleprof_error::success;
7183376a787SDiego Novillo }
7193376a787SDiego Novillo 
7203376a787SDiego Novillo /// \brief Read a GCC AutoFDO profile.
7213376a787SDiego Novillo ///
7223376a787SDiego Novillo /// This format is generated by the Linux Perf conversion tool at
7233376a787SDiego Novillo /// https://github.com/google/autofdo.
7243376a787SDiego Novillo std::error_code SampleProfileReaderGCC::read() {
7253376a787SDiego Novillo   // Read the string table.
7263376a787SDiego Novillo   if (std::error_code EC = readNameTable())
7273376a787SDiego Novillo     return EC;
7283376a787SDiego Novillo 
7293376a787SDiego Novillo   // Read the source profile.
7303376a787SDiego Novillo   if (std::error_code EC = readFunctionProfiles())
7313376a787SDiego Novillo     return EC;
7323376a787SDiego Novillo 
7333376a787SDiego Novillo   return sampleprof_error::success;
7343376a787SDiego Novillo }
7353376a787SDiego Novillo 
7363376a787SDiego Novillo bool SampleProfileReaderGCC::hasFormat(const MemoryBuffer &Buffer) {
7373376a787SDiego Novillo   StringRef Magic(reinterpret_cast<const char *>(Buffer.getBufferStart()));
7383376a787SDiego Novillo   return Magic == "adcg*704";
7393376a787SDiego Novillo }
7403376a787SDiego Novillo 
741c572e92cSDiego Novillo /// \brief Prepare a memory buffer for the contents of \p Filename.
742de1ab26fSDiego Novillo ///
743c572e92cSDiego Novillo /// \returns an error code indicating the status of the buffer.
744fcd55607SDiego Novillo static ErrorOr<std::unique_ptr<MemoryBuffer>>
7450da23a27SBenjamin Kramer setupMemoryBuffer(const Twine &Filename) {
746c572e92cSDiego Novillo   auto BufferOrErr = MemoryBuffer::getFileOrSTDIN(Filename);
747c572e92cSDiego Novillo   if (std::error_code EC = BufferOrErr.getError())
748c572e92cSDiego Novillo     return EC;
749fcd55607SDiego Novillo   auto Buffer = std::move(BufferOrErr.get());
750c572e92cSDiego Novillo 
751c572e92cSDiego Novillo   // Sanity check the file.
752*260fe3ecSZachary Turner   if (uint64_t(Buffer->getBufferSize()) > std::numeric_limits<uint32_t>::max())
753c572e92cSDiego Novillo     return sampleprof_error::too_large;
754c572e92cSDiego Novillo 
755fcd55607SDiego Novillo   return std::move(Buffer);
756c572e92cSDiego Novillo }
757c572e92cSDiego Novillo 
758c572e92cSDiego Novillo /// \brief Create a sample profile reader based on the format of the input file.
759c572e92cSDiego Novillo ///
760c572e92cSDiego Novillo /// \param Filename The file to open.
761c572e92cSDiego Novillo ///
762c572e92cSDiego Novillo /// \param C The LLVM context to use to emit diagnostics.
763c572e92cSDiego Novillo ///
764c572e92cSDiego Novillo /// \returns an error code indicating the status of the created reader.
765fcd55607SDiego Novillo ErrorOr<std::unique_ptr<SampleProfileReader>>
7660da23a27SBenjamin Kramer SampleProfileReader::create(const Twine &Filename, LLVMContext &C) {
767fcd55607SDiego Novillo   auto BufferOrError = setupMemoryBuffer(Filename);
768fcd55607SDiego Novillo   if (std::error_code EC = BufferOrError.getError())
769c572e92cSDiego Novillo     return EC;
77051abea74SNathan Slingerland   return create(BufferOrError.get(), C);
77151abea74SNathan Slingerland }
772c572e92cSDiego Novillo 
77351abea74SNathan Slingerland /// \brief Create a sample profile reader based on the format of the input data.
77451abea74SNathan Slingerland ///
77551abea74SNathan Slingerland /// \param B The memory buffer to create the reader from (assumes ownership).
77651abea74SNathan Slingerland ///
77751abea74SNathan Slingerland /// \param C The LLVM context to use to emit diagnostics.
77851abea74SNathan Slingerland ///
77951abea74SNathan Slingerland /// \returns an error code indicating the status of the created reader.
78051abea74SNathan Slingerland ErrorOr<std::unique_ptr<SampleProfileReader>>
78151abea74SNathan Slingerland SampleProfileReader::create(std::unique_ptr<MemoryBuffer> &B, LLVMContext &C) {
782fcd55607SDiego Novillo   std::unique_ptr<SampleProfileReader> Reader;
78351abea74SNathan Slingerland   if (SampleProfileReaderBinary::hasFormat(*B))
78451abea74SNathan Slingerland     Reader.reset(new SampleProfileReaderBinary(std::move(B), C));
78551abea74SNathan Slingerland   else if (SampleProfileReaderGCC::hasFormat(*B))
78651abea74SNathan Slingerland     Reader.reset(new SampleProfileReaderGCC(std::move(B), C));
78751abea74SNathan Slingerland   else if (SampleProfileReaderText::hasFormat(*B))
78851abea74SNathan Slingerland     Reader.reset(new SampleProfileReaderText(std::move(B), C));
7894f823667SNathan Slingerland   else
7904f823667SNathan Slingerland     return sampleprof_error::unrecognized_format;
791c572e92cSDiego Novillo 
792fcd55607SDiego Novillo   if (std::error_code EC = Reader->readHeader())
793fcd55607SDiego Novillo     return EC;
794fcd55607SDiego Novillo 
795fcd55607SDiego Novillo   return std::move(Reader);
796de1ab26fSDiego Novillo }
79740ee23dbSEaswaran Raman 
79840ee23dbSEaswaran Raman // For text and GCC file formats, we compute the summary after reading the
79940ee23dbSEaswaran Raman // profile. Binary format has the profile summary in its header.
80040ee23dbSEaswaran Raman void SampleProfileReader::computeSummary() {
801e5a17e3fSEaswaran Raman   SampleProfileSummaryBuilder Builder(ProfileSummaryBuilder::DefaultCutoffs);
80240ee23dbSEaswaran Raman   for (const auto &I : Profiles) {
80340ee23dbSEaswaran Raman     const FunctionSamples &Profile = I.second;
804e5a17e3fSEaswaran Raman     Builder.addRecord(Profile);
80540ee23dbSEaswaran Raman   }
80638de59e4SBenjamin Kramer   Summary = Builder.getSummary();
80740ee23dbSEaswaran Raman }
808