1de1ab26fSDiego Novillo //===- SampleProfReader.cpp - Read LLVM sample profile data ---------------===//
2de1ab26fSDiego Novillo //
32946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
42946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
52946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6de1ab26fSDiego Novillo //
7de1ab26fSDiego Novillo //===----------------------------------------------------------------------===//
8de1ab26fSDiego Novillo //
9de1ab26fSDiego Novillo // This file implements the class that reads LLVM sample profiles. It
10bb5605caSDiego Novillo // supports three file formats: text, binary and gcov.
11de1ab26fSDiego Novillo //
12bb5605caSDiego Novillo // The textual representation is useful for debugging and testing purposes. The
13bb5605caSDiego Novillo // binary representation is more compact, resulting in smaller file sizes.
14de1ab26fSDiego Novillo //
15bb5605caSDiego Novillo // The gcov encoding is the one generated by GCC's AutoFDO profile creation
16bb5605caSDiego Novillo // tool (https://github.com/google/autofdo)
17de1ab26fSDiego Novillo //
18bb5605caSDiego Novillo // All three encodings can be used interchangeably as an input sample profile.
19de1ab26fSDiego Novillo //
20de1ab26fSDiego Novillo //===----------------------------------------------------------------------===//
21de1ab26fSDiego Novillo 
22de1ab26fSDiego Novillo #include "llvm/ProfileData/SampleProfReader.h"
23b93483dbSDiego Novillo #include "llvm/ADT/DenseMap.h"
2440ee23dbSEaswaran Raman #include "llvm/ADT/STLExtras.h"
25e78d131aSEugene Zelenko #include "llvm/ADT/StringRef.h"
26e78d131aSEugene Zelenko #include "llvm/IR/ProfileSummary.h"
27e78d131aSEugene Zelenko #include "llvm/ProfileData/ProfileCommon.h"
28e78d131aSEugene Zelenko #include "llvm/ProfileData/SampleProf.h"
29b523790aSWei Mi #include "llvm/Support/Compression.h"
30de1ab26fSDiego Novillo #include "llvm/Support/ErrorOr.h"
31c572e92cSDiego Novillo #include "llvm/Support/LEB128.h"
32de1ab26fSDiego Novillo #include "llvm/Support/LineIterator.h"
336a14325dSWei Mi #include "llvm/Support/MD5.h"
34c572e92cSDiego Novillo #include "llvm/Support/MemoryBuffer.h"
35e78d131aSEugene Zelenko #include "llvm/Support/raw_ostream.h"
36e78d131aSEugene Zelenko #include <algorithm>
37e78d131aSEugene Zelenko #include <cstddef>
38e78d131aSEugene Zelenko #include <cstdint>
39e78d131aSEugene Zelenko #include <limits>
40e78d131aSEugene Zelenko #include <memory>
41e78d131aSEugene Zelenko #include <system_error>
42e78d131aSEugene Zelenko #include <vector>
43de1ab26fSDiego Novillo 
44de1ab26fSDiego Novillo using namespace llvm;
45e78d131aSEugene Zelenko using namespace sampleprof;
46de1ab26fSDiego Novillo 
475f8f34e4SAdrian Prantl /// Dump the function profile for \p FName.
48de1ab26fSDiego Novillo ///
49de1ab26fSDiego Novillo /// \param FName Name of the function to print.
50d5336ae2SDiego Novillo /// \param OS Stream to emit the output to.
51d5336ae2SDiego Novillo void SampleProfileReader::dumpFunctionProfile(StringRef FName,
52d5336ae2SDiego Novillo                                               raw_ostream &OS) {
538e415a82SDiego Novillo   OS << "Function: " << FName << ": " << Profiles[FName];
54de1ab26fSDiego Novillo }
55de1ab26fSDiego Novillo 
565f8f34e4SAdrian Prantl /// Dump all the function profiles found on stream \p OS.
57d5336ae2SDiego Novillo void SampleProfileReader::dump(raw_ostream &OS) {
58d5336ae2SDiego Novillo   for (const auto &I : Profiles)
59d5336ae2SDiego Novillo     dumpFunctionProfile(I.getKey(), OS);
60de1ab26fSDiego Novillo }
61de1ab26fSDiego Novillo 
625f8f34e4SAdrian Prantl /// Parse \p Input as function head.
636722688eSDehao Chen ///
646722688eSDehao Chen /// Parse one line of \p Input, and update function name in \p FName,
656722688eSDehao Chen /// function's total sample count in \p NumSamples, function's entry
666722688eSDehao Chen /// count in \p NumHeadSamples.
676722688eSDehao Chen ///
686722688eSDehao Chen /// \returns true if parsing is successful.
696722688eSDehao Chen static bool ParseHead(const StringRef &Input, StringRef &FName,
7038be3330SDiego Novillo                       uint64_t &NumSamples, uint64_t &NumHeadSamples) {
716722688eSDehao Chen   if (Input[0] == ' ')
726722688eSDehao Chen     return false;
736722688eSDehao Chen   size_t n2 = Input.rfind(':');
746722688eSDehao Chen   size_t n1 = Input.rfind(':', n2 - 1);
756722688eSDehao Chen   FName = Input.substr(0, n1);
766722688eSDehao Chen   if (Input.substr(n1 + 1, n2 - n1 - 1).getAsInteger(10, NumSamples))
776722688eSDehao Chen     return false;
786722688eSDehao Chen   if (Input.substr(n2 + 1).getAsInteger(10, NumHeadSamples))
796722688eSDehao Chen     return false;
806722688eSDehao Chen   return true;
816722688eSDehao Chen }
826722688eSDehao Chen 
835f8f34e4SAdrian Prantl /// Returns true if line offset \p L is legal (only has 16 bits).
8457d1dda5SDehao Chen static bool isOffsetLegal(unsigned L) { return (L & 0xffff) == L; }
8510042412SDehao Chen 
86ac068e01SHongtao Yu /// Parse \p Input that contains metadata.
87ac068e01SHongtao Yu /// Possible metadata:
88ac068e01SHongtao Yu /// - CFG Checksum information:
89ac068e01SHongtao Yu ///     !CFGChecksum: 12345
90ac068e01SHongtao Yu /// Stores the FunctionHash (a.k.a. CFG Checksum) into \p FunctionHash.
91ac068e01SHongtao Yu static bool parseMetadata(const StringRef &Input, uint64_t &FunctionHash) {
92ac068e01SHongtao Yu   if (!Input.startswith("!CFGChecksum:"))
93ac068e01SHongtao Yu     return false;
94ac068e01SHongtao Yu 
95ac068e01SHongtao Yu   StringRef CFGInfo = Input.substr(strlen("!CFGChecksum:")).trim();
96ac068e01SHongtao Yu   return !CFGInfo.getAsInteger(10, FunctionHash);
97ac068e01SHongtao Yu }
98ac068e01SHongtao Yu 
99ac068e01SHongtao Yu enum class LineType {
100ac068e01SHongtao Yu   CallSiteProfile,
101ac068e01SHongtao Yu   BodyProfile,
102ac068e01SHongtao Yu   Metadata,
103ac068e01SHongtao Yu };
104ac068e01SHongtao Yu 
1055f8f34e4SAdrian Prantl /// Parse \p Input as line sample.
1066722688eSDehao Chen ///
1076722688eSDehao Chen /// \param Input input line.
108ac068e01SHongtao Yu /// \param LineTy Type of this line.
1096722688eSDehao Chen /// \param Depth the depth of the inline stack.
1106722688eSDehao Chen /// \param NumSamples total samples of the line/inlined callsite.
1116722688eSDehao Chen /// \param LineOffset line offset to the start of the function.
1126722688eSDehao Chen /// \param Discriminator discriminator of the line.
1136722688eSDehao Chen /// \param TargetCountMap map from indirect call target to count.
114ac068e01SHongtao Yu /// \param FunctionHash the function's CFG hash, used by pseudo probe.
1156722688eSDehao Chen ///
1166722688eSDehao Chen /// returns true if parsing is successful.
117ac068e01SHongtao Yu static bool ParseLine(const StringRef &Input, LineType &LineTy, uint32_t &Depth,
11838be3330SDiego Novillo                       uint64_t &NumSamples, uint32_t &LineOffset,
11938be3330SDiego Novillo                       uint32_t &Discriminator, StringRef &CalleeName,
120ac068e01SHongtao Yu                       DenseMap<StringRef, uint64_t> &TargetCountMap,
121ac068e01SHongtao Yu                       uint64_t &FunctionHash) {
1226722688eSDehao Chen   for (Depth = 0; Input[Depth] == ' '; Depth++)
1236722688eSDehao Chen     ;
1246722688eSDehao Chen   if (Depth == 0)
1256722688eSDehao Chen     return false;
1266722688eSDehao Chen 
127ac068e01SHongtao Yu   if (Depth == 1 && Input[Depth] == '!') {
128ac068e01SHongtao Yu     LineTy = LineType::Metadata;
129ac068e01SHongtao Yu     return parseMetadata(Input.substr(Depth), FunctionHash);
130ac068e01SHongtao Yu   }
131ac068e01SHongtao Yu 
1326722688eSDehao Chen   size_t n1 = Input.find(':');
1336722688eSDehao Chen   StringRef Loc = Input.substr(Depth, n1 - Depth);
1346722688eSDehao Chen   size_t n2 = Loc.find('.');
1356722688eSDehao Chen   if (n2 == StringRef::npos) {
13610042412SDehao Chen     if (Loc.getAsInteger(10, LineOffset) || !isOffsetLegal(LineOffset))
1376722688eSDehao Chen       return false;
1386722688eSDehao Chen     Discriminator = 0;
1396722688eSDehao Chen   } else {
1406722688eSDehao Chen     if (Loc.substr(0, n2).getAsInteger(10, LineOffset))
1416722688eSDehao Chen       return false;
1426722688eSDehao Chen     if (Loc.substr(n2 + 1).getAsInteger(10, Discriminator))
1436722688eSDehao Chen       return false;
1446722688eSDehao Chen   }
1456722688eSDehao Chen 
1466722688eSDehao Chen   StringRef Rest = Input.substr(n1 + 2);
147551aaa24SKazu Hirata   if (isDigit(Rest[0])) {
148ac068e01SHongtao Yu     LineTy = LineType::BodyProfile;
1496722688eSDehao Chen     size_t n3 = Rest.find(' ');
1506722688eSDehao Chen     if (n3 == StringRef::npos) {
1516722688eSDehao Chen       if (Rest.getAsInteger(10, NumSamples))
1526722688eSDehao Chen         return false;
1536722688eSDehao Chen     } else {
1546722688eSDehao Chen       if (Rest.substr(0, n3).getAsInteger(10, NumSamples))
1556722688eSDehao Chen         return false;
1566722688eSDehao Chen     }
157984ab0f1SWei Mi     // Find call targets and their sample counts.
158984ab0f1SWei Mi     // Note: In some cases, there are symbols in the profile which are not
159984ab0f1SWei Mi     // mangled. To accommodate such cases, use colon + integer pairs as the
160984ab0f1SWei Mi     // anchor points.
161984ab0f1SWei Mi     // An example:
162984ab0f1SWei Mi     // _M_construct<char *>:1000 string_view<std::allocator<char> >:437
163984ab0f1SWei Mi     // ":1000" and ":437" are used as anchor points so the string above will
164984ab0f1SWei Mi     // be interpreted as
165984ab0f1SWei Mi     // target: _M_construct<char *>
166984ab0f1SWei Mi     // count: 1000
167984ab0f1SWei Mi     // target: string_view<std::allocator<char> >
168984ab0f1SWei Mi     // count: 437
1696722688eSDehao Chen     while (n3 != StringRef::npos) {
1706722688eSDehao Chen       n3 += Rest.substr(n3).find_first_not_of(' ');
1716722688eSDehao Chen       Rest = Rest.substr(n3);
172984ab0f1SWei Mi       n3 = Rest.find_first_of(':');
173984ab0f1SWei Mi       if (n3 == StringRef::npos || n3 == 0)
1746722688eSDehao Chen         return false;
175984ab0f1SWei Mi 
176984ab0f1SWei Mi       StringRef Target;
177984ab0f1SWei Mi       uint64_t count, n4;
178984ab0f1SWei Mi       while (true) {
179984ab0f1SWei Mi         // Get the segment after the current colon.
180984ab0f1SWei Mi         StringRef AfterColon = Rest.substr(n3 + 1);
181984ab0f1SWei Mi         // Get the target symbol before the current colon.
182984ab0f1SWei Mi         Target = Rest.substr(0, n3);
183984ab0f1SWei Mi         // Check if the word after the current colon is an integer.
184984ab0f1SWei Mi         n4 = AfterColon.find_first_of(' ');
185984ab0f1SWei Mi         n4 = (n4 != StringRef::npos) ? n3 + n4 + 1 : Rest.size();
186984ab0f1SWei Mi         StringRef WordAfterColon = Rest.substr(n3 + 1, n4 - n3 - 1);
187984ab0f1SWei Mi         if (!WordAfterColon.getAsInteger(10, count))
188984ab0f1SWei Mi           break;
189984ab0f1SWei Mi 
190984ab0f1SWei Mi         // Try to find the next colon.
191984ab0f1SWei Mi         uint64_t n5 = AfterColon.find_first_of(':');
192984ab0f1SWei Mi         if (n5 == StringRef::npos)
193984ab0f1SWei Mi           return false;
194984ab0f1SWei Mi         n3 += n5 + 1;
195984ab0f1SWei Mi       }
196984ab0f1SWei Mi 
197984ab0f1SWei Mi       // An anchor point is found. Save the {target, count} pair
198984ab0f1SWei Mi       TargetCountMap[Target] = count;
199984ab0f1SWei Mi       if (n4 == Rest.size())
200984ab0f1SWei Mi         break;
201984ab0f1SWei Mi       // Change n3 to the next blank space after colon + integer pair.
202984ab0f1SWei Mi       n3 = n4;
2036722688eSDehao Chen     }
2046722688eSDehao Chen   } else {
205ac068e01SHongtao Yu     LineTy = LineType::CallSiteProfile;
20638be3330SDiego Novillo     size_t n3 = Rest.find_last_of(':');
2076722688eSDehao Chen     CalleeName = Rest.substr(0, n3);
2086722688eSDehao Chen     if (Rest.substr(n3 + 1).getAsInteger(10, NumSamples))
2096722688eSDehao Chen       return false;
2106722688eSDehao Chen   }
2116722688eSDehao Chen   return true;
2126722688eSDehao Chen }
2136722688eSDehao Chen 
2145f8f34e4SAdrian Prantl /// Load samples from a text file.
215de1ab26fSDiego Novillo ///
216de1ab26fSDiego Novillo /// See the documentation at the top of the file for an explanation of
217de1ab26fSDiego Novillo /// the expected format.
218de1ab26fSDiego Novillo ///
219de1ab26fSDiego Novillo /// \returns true if the file was loaded successfully, false otherwise.
2208c8ec1f6SWei Mi std::error_code SampleProfileReaderText::readImpl() {
221c572e92cSDiego Novillo   line_iterator LineIt(*Buffer, /*SkipBlanks=*/true, '#');
22248dd080cSNathan Slingerland   sampleprof_error Result = sampleprof_error::success;
223de1ab26fSDiego Novillo 
224aae1ed8eSDiego Novillo   InlineCallStack InlineStack;
225ac068e01SHongtao Yu   uint32_t ProbeProfileCount = 0;
226ac068e01SHongtao Yu 
227ac068e01SHongtao Yu   // SeenMetadata tracks whether we have processed metadata for the current
228ac068e01SHongtao Yu   // top-level function profile.
229ac068e01SHongtao Yu   bool SeenMetadata = false;
2306722688eSDehao Chen 
2316722688eSDehao Chen   for (; !LineIt.is_at_eof(); ++LineIt) {
2326722688eSDehao Chen     if ((*LineIt)[(*LineIt).find_first_not_of(' ')] == '#')
2336722688eSDehao Chen       continue;
234de1ab26fSDiego Novillo     // Read the header of each function.
235de1ab26fSDiego Novillo     //
236de1ab26fSDiego Novillo     // Note that for function identifiers we are actually expecting
237de1ab26fSDiego Novillo     // mangled names, but we may not always get them. This happens when
238de1ab26fSDiego Novillo     // the compiler decides not to emit the function (e.g., it was inlined
239de1ab26fSDiego Novillo     // and removed). In this case, the binary will not have the linkage
240de1ab26fSDiego Novillo     // name for the function, so the profiler will emit the function's
241de1ab26fSDiego Novillo     // unmangled name, which may contain characters like ':' and '>' in its
242de1ab26fSDiego Novillo     // name (member functions, templates, etc).
243de1ab26fSDiego Novillo     //
244de1ab26fSDiego Novillo     // The only requirement we place on the identifier, then, is that it
245de1ab26fSDiego Novillo     // should not begin with a number.
2466722688eSDehao Chen     if ((*LineIt)[0] != ' ') {
24738be3330SDiego Novillo       uint64_t NumSamples, NumHeadSamples;
2486722688eSDehao Chen       StringRef FName;
2496722688eSDehao Chen       if (!ParseHead(*LineIt, FName, NumSamples, NumHeadSamples)) {
2503376a787SDiego Novillo         reportError(LineIt.line_number(),
251de1ab26fSDiego Novillo                     "Expected 'mangled_name:NUM:NUM', found " + *LineIt);
252c572e92cSDiego Novillo         return sampleprof_error::malformed;
253de1ab26fSDiego Novillo       }
254ac068e01SHongtao Yu       SeenMetadata = false;
2556b989a17SWenlei He       SampleContext FContext(FName);
2566b989a17SWenlei He       if (FContext.hasContext())
2576b989a17SWenlei He         ++CSProfileCount;
2586b989a17SWenlei He       Profiles[FContext] = FunctionSamples();
2596b989a17SWenlei He       FunctionSamples &FProfile = Profiles[FContext];
260*7e99bddfSHongtao Yu       FProfile.setName(FContext.getNameWithoutContext());
2616b989a17SWenlei He       FProfile.setContext(FContext);
26248dd080cSNathan Slingerland       MergeResult(Result, FProfile.addTotalSamples(NumSamples));
26348dd080cSNathan Slingerland       MergeResult(Result, FProfile.addHeadSamples(NumHeadSamples));
2646722688eSDehao Chen       InlineStack.clear();
2656722688eSDehao Chen       InlineStack.push_back(&FProfile);
2666722688eSDehao Chen     } else {
26738be3330SDiego Novillo       uint64_t NumSamples;
2686722688eSDehao Chen       StringRef FName;
26938be3330SDiego Novillo       DenseMap<StringRef, uint64_t> TargetCountMap;
27038be3330SDiego Novillo       uint32_t Depth, LineOffset, Discriminator;
271ac068e01SHongtao Yu       LineType LineTy;
272ac068e01SHongtao Yu       uint64_t FunctionHash;
273ac068e01SHongtao Yu       if (!ParseLine(*LineIt, LineTy, Depth, NumSamples, LineOffset,
274ac068e01SHongtao Yu                      Discriminator, FName, TargetCountMap, FunctionHash)) {
2753376a787SDiego Novillo         reportError(LineIt.line_number(),
2763376a787SDiego Novillo                     "Expected 'NUM[.NUM]: NUM[ mangled_name:NUM]*', found " +
2773376a787SDiego Novillo                         *LineIt);
278c572e92cSDiego Novillo         return sampleprof_error::malformed;
279de1ab26fSDiego Novillo       }
280ac068e01SHongtao Yu       if (SeenMetadata && LineTy != LineType::Metadata) {
281ac068e01SHongtao Yu         // Metadata must be put at the end of a function profile.
282ac068e01SHongtao Yu         reportError(LineIt.line_number(),
283ac068e01SHongtao Yu                     "Found non-metadata after metadata: " + *LineIt);
284ac068e01SHongtao Yu         return sampleprof_error::malformed;
285ac068e01SHongtao Yu       }
2866722688eSDehao Chen       while (InlineStack.size() > Depth) {
2876722688eSDehao Chen         InlineStack.pop_back();
288c572e92cSDiego Novillo       }
289ac068e01SHongtao Yu       switch (LineTy) {
290ac068e01SHongtao Yu       case LineType::CallSiteProfile: {
2916722688eSDehao Chen         FunctionSamples &FSamples = InlineStack.back()->functionSamplesAt(
292adcd0268SBenjamin Kramer             LineLocation(LineOffset, Discriminator))[std::string(FName)];
29357d1dda5SDehao Chen         FSamples.setName(FName);
29448dd080cSNathan Slingerland         MergeResult(Result, FSamples.addTotalSamples(NumSamples));
2956722688eSDehao Chen         InlineStack.push_back(&FSamples);
296ac068e01SHongtao Yu         break;
297ac068e01SHongtao Yu       }
298ac068e01SHongtao Yu       case LineType::BodyProfile: {
2996722688eSDehao Chen         while (InlineStack.size() > Depth) {
3006722688eSDehao Chen           InlineStack.pop_back();
3016722688eSDehao Chen         }
3026722688eSDehao Chen         FunctionSamples &FProfile = *InlineStack.back();
3036722688eSDehao Chen         for (const auto &name_count : TargetCountMap) {
30448dd080cSNathan Slingerland           MergeResult(Result, FProfile.addCalledTargetSamples(
30548dd080cSNathan Slingerland                                   LineOffset, Discriminator, name_count.first,
30648dd080cSNathan Slingerland                                   name_count.second));
307c572e92cSDiego Novillo         }
30848dd080cSNathan Slingerland         MergeResult(Result, FProfile.addBodySamples(LineOffset, Discriminator,
30948dd080cSNathan Slingerland                                                     NumSamples));
310ac068e01SHongtao Yu         break;
311ac068e01SHongtao Yu       }
312ac068e01SHongtao Yu       case LineType::Metadata: {
313ac068e01SHongtao Yu         FunctionSamples &FProfile = *InlineStack.back();
314ac068e01SHongtao Yu         FProfile.setFunctionHash(FunctionHash);
315ac068e01SHongtao Yu         ++ProbeProfileCount;
316ac068e01SHongtao Yu         SeenMetadata = true;
317ac068e01SHongtao Yu         break;
318ac068e01SHongtao Yu       }
3196722688eSDehao Chen       }
320de1ab26fSDiego Novillo     }
321de1ab26fSDiego Novillo   }
3226b989a17SWenlei He 
323*7e99bddfSHongtao Yu   assert((CSProfileCount == 0 || CSProfileCount == Profiles.size()) &&
3246b989a17SWenlei He          "Cannot have both context-sensitive and regular profile");
3256b989a17SWenlei He   ProfileIsCS = (CSProfileCount > 0);
326ac068e01SHongtao Yu   assert((ProbeProfileCount == 0 || ProbeProfileCount == Profiles.size()) &&
327ac068e01SHongtao Yu          "Cannot have both probe-based profiles and regular profiles");
328ac068e01SHongtao Yu   ProfileIsProbeBased = (ProbeProfileCount > 0);
329ac068e01SHongtao Yu   FunctionSamples::ProfileIsProbeBased = ProfileIsProbeBased;
330*7e99bddfSHongtao Yu   FunctionSamples::ProfileIsCS = ProfileIsCS;
3316b989a17SWenlei He 
33240ee23dbSEaswaran Raman   if (Result == sampleprof_error::success)
33340ee23dbSEaswaran Raman     computeSummary();
334de1ab26fSDiego Novillo 
33548dd080cSNathan Slingerland   return Result;
336de1ab26fSDiego Novillo }
337de1ab26fSDiego Novillo 
3384f823667SNathan Slingerland bool SampleProfileReaderText::hasFormat(const MemoryBuffer &Buffer) {
3394f823667SNathan Slingerland   bool result = false;
3404f823667SNathan Slingerland 
3414f823667SNathan Slingerland   // Check that the first non-comment line is a valid function header.
3424f823667SNathan Slingerland   line_iterator LineIt(Buffer, /*SkipBlanks=*/true, '#');
3434f823667SNathan Slingerland   if (!LineIt.is_at_eof()) {
3444f823667SNathan Slingerland     if ((*LineIt)[0] != ' ') {
3454f823667SNathan Slingerland       uint64_t NumSamples, NumHeadSamples;
3464f823667SNathan Slingerland       StringRef FName;
3474f823667SNathan Slingerland       result = ParseHead(*LineIt, FName, NumSamples, NumHeadSamples);
3484f823667SNathan Slingerland     }
3494f823667SNathan Slingerland   }
3504f823667SNathan Slingerland 
3514f823667SNathan Slingerland   return result;
3524f823667SNathan Slingerland }
3534f823667SNathan Slingerland 
354d5336ae2SDiego Novillo template <typename T> ErrorOr<T> SampleProfileReaderBinary::readNumber() {
355c572e92cSDiego Novillo   unsigned NumBytesRead = 0;
356c572e92cSDiego Novillo   std::error_code EC;
357c572e92cSDiego Novillo   uint64_t Val = decodeULEB128(Data, &NumBytesRead);
358c572e92cSDiego Novillo 
359c572e92cSDiego Novillo   if (Val > std::numeric_limits<T>::max())
360c572e92cSDiego Novillo     EC = sampleprof_error::malformed;
361c572e92cSDiego Novillo   else if (Data + NumBytesRead > End)
362c572e92cSDiego Novillo     EC = sampleprof_error::truncated;
363c572e92cSDiego Novillo   else
364c572e92cSDiego Novillo     EC = sampleprof_error::success;
365c572e92cSDiego Novillo 
366c572e92cSDiego Novillo   if (EC) {
3673376a787SDiego Novillo     reportError(0, EC.message());
368c572e92cSDiego Novillo     return EC;
369c572e92cSDiego Novillo   }
370c572e92cSDiego Novillo 
371c572e92cSDiego Novillo   Data += NumBytesRead;
372c572e92cSDiego Novillo   return static_cast<T>(Val);
373c572e92cSDiego Novillo }
374c572e92cSDiego Novillo 
375c572e92cSDiego Novillo ErrorOr<StringRef> SampleProfileReaderBinary::readString() {
376c572e92cSDiego Novillo   std::error_code EC;
377c572e92cSDiego Novillo   StringRef Str(reinterpret_cast<const char *>(Data));
378c572e92cSDiego Novillo   if (Data + Str.size() + 1 > End) {
379c572e92cSDiego Novillo     EC = sampleprof_error::truncated;
3803376a787SDiego Novillo     reportError(0, EC.message());
381c572e92cSDiego Novillo     return EC;
382c572e92cSDiego Novillo   }
383c572e92cSDiego Novillo 
384c572e92cSDiego Novillo   Data += Str.size() + 1;
385c572e92cSDiego Novillo   return Str;
386c572e92cSDiego Novillo }
387c572e92cSDiego Novillo 
388a0c0857eSWei Mi template <typename T>
3896a14325dSWei Mi ErrorOr<T> SampleProfileReaderBinary::readUnencodedNumber() {
3906a14325dSWei Mi   std::error_code EC;
3916a14325dSWei Mi 
3926a14325dSWei Mi   if (Data + sizeof(T) > End) {
3936a14325dSWei Mi     EC = sampleprof_error::truncated;
3946a14325dSWei Mi     reportError(0, EC.message());
3956a14325dSWei Mi     return EC;
3966a14325dSWei Mi   }
3976a14325dSWei Mi 
3986a14325dSWei Mi   using namespace support;
3996a14325dSWei Mi   T Val = endian::readNext<T, little, unaligned>(Data);
4006a14325dSWei Mi   return Val;
4016a14325dSWei Mi }
4026a14325dSWei Mi 
4036a14325dSWei Mi template <typename T>
404a0c0857eSWei Mi inline ErrorOr<uint32_t> SampleProfileReaderBinary::readStringIndex(T &Table) {
405760c5a8fSDiego Novillo   std::error_code EC;
40638be3330SDiego Novillo   auto Idx = readNumber<uint32_t>();
407760c5a8fSDiego Novillo   if (std::error_code EC = Idx.getError())
408760c5a8fSDiego Novillo     return EC;
409a0c0857eSWei Mi   if (*Idx >= Table.size())
410760c5a8fSDiego Novillo     return sampleprof_error::truncated_name_table;
411a0c0857eSWei Mi   return *Idx;
412a0c0857eSWei Mi }
413a0c0857eSWei Mi 
414be907324SWei Mi ErrorOr<StringRef> SampleProfileReaderBinary::readStringFromTable() {
415a0c0857eSWei Mi   auto Idx = readStringIndex(NameTable);
416a0c0857eSWei Mi   if (std::error_code EC = Idx.getError())
417a0c0857eSWei Mi     return EC;
418a0c0857eSWei Mi 
419760c5a8fSDiego Novillo   return NameTable[*Idx];
420760c5a8fSDiego Novillo }
421760c5a8fSDiego Novillo 
42264e76853SWei Mi ErrorOr<StringRef> SampleProfileReaderExtBinaryBase::readStringFromTable() {
42364e76853SWei Mi   if (!FixedLengthMD5)
42464e76853SWei Mi     return SampleProfileReaderBinary::readStringFromTable();
42564e76853SWei Mi 
42664e76853SWei Mi   // read NameTable index.
42764e76853SWei Mi   auto Idx = readStringIndex(NameTable);
42864e76853SWei Mi   if (std::error_code EC = Idx.getError())
42964e76853SWei Mi     return EC;
43064e76853SWei Mi 
43164e76853SWei Mi   // Check whether the name to be accessed has been accessed before,
43264e76853SWei Mi   // if not, read it from memory directly.
43364e76853SWei Mi   StringRef &SR = NameTable[*Idx];
43464e76853SWei Mi   if (SR.empty()) {
43564e76853SWei Mi     const uint8_t *SavedData = Data;
43664e76853SWei Mi     Data = MD5NameMemStart + ((*Idx) * sizeof(uint64_t));
43764e76853SWei Mi     auto FID = readUnencodedNumber<uint64_t>();
43864e76853SWei Mi     if (std::error_code EC = FID.getError())
43964e76853SWei Mi       return EC;
44064e76853SWei Mi     // Save the string converted from uint64_t in MD5StringBuf. All the
44164e76853SWei Mi     // references to the name are all StringRefs refering to the string
44264e76853SWei Mi     // in MD5StringBuf.
44364e76853SWei Mi     MD5StringBuf->push_back(std::to_string(*FID));
44464e76853SWei Mi     SR = MD5StringBuf->back();
44564e76853SWei Mi     Data = SavedData;
44664e76853SWei Mi   }
44764e76853SWei Mi   return SR;
44864e76853SWei Mi }
44964e76853SWei Mi 
450a0c0857eSWei Mi ErrorOr<StringRef> SampleProfileReaderCompactBinary::readStringFromTable() {
451a0c0857eSWei Mi   auto Idx = readStringIndex(NameTable);
452a0c0857eSWei Mi   if (std::error_code EC = Idx.getError())
453a0c0857eSWei Mi     return EC;
454a0c0857eSWei Mi 
455a0c0857eSWei Mi   return StringRef(NameTable[*Idx]);
456a0c0857eSWei Mi }
457a0c0857eSWei Mi 
458a7f1e8efSDiego Novillo std::error_code
459a7f1e8efSDiego Novillo SampleProfileReaderBinary::readProfile(FunctionSamples &FProfile) {
460b93483dbSDiego Novillo   auto NumSamples = readNumber<uint64_t>();
461b93483dbSDiego Novillo   if (std::error_code EC = NumSamples.getError())
462c572e92cSDiego Novillo     return EC;
463b93483dbSDiego Novillo   FProfile.addTotalSamples(*NumSamples);
464c572e92cSDiego Novillo 
465c572e92cSDiego Novillo   // Read the samples in the body.
46638be3330SDiego Novillo   auto NumRecords = readNumber<uint32_t>();
467c572e92cSDiego Novillo   if (std::error_code EC = NumRecords.getError())
468c572e92cSDiego Novillo     return EC;
469a7f1e8efSDiego Novillo 
47038be3330SDiego Novillo   for (uint32_t I = 0; I < *NumRecords; ++I) {
471c572e92cSDiego Novillo     auto LineOffset = readNumber<uint64_t>();
472c572e92cSDiego Novillo     if (std::error_code EC = LineOffset.getError())
473c572e92cSDiego Novillo       return EC;
474c572e92cSDiego Novillo 
47510042412SDehao Chen     if (!isOffsetLegal(*LineOffset)) {
47610042412SDehao Chen       return std::error_code();
47710042412SDehao Chen     }
47810042412SDehao Chen 
479c572e92cSDiego Novillo     auto Discriminator = readNumber<uint64_t>();
480c572e92cSDiego Novillo     if (std::error_code EC = Discriminator.getError())
481c572e92cSDiego Novillo       return EC;
482c572e92cSDiego Novillo 
483c572e92cSDiego Novillo     auto NumSamples = readNumber<uint64_t>();
484c572e92cSDiego Novillo     if (std::error_code EC = NumSamples.getError())
485c572e92cSDiego Novillo       return EC;
486c572e92cSDiego Novillo 
48738be3330SDiego Novillo     auto NumCalls = readNumber<uint32_t>();
488c572e92cSDiego Novillo     if (std::error_code EC = NumCalls.getError())
489c572e92cSDiego Novillo       return EC;
490c572e92cSDiego Novillo 
49138be3330SDiego Novillo     for (uint32_t J = 0; J < *NumCalls; ++J) {
492760c5a8fSDiego Novillo       auto CalledFunction(readStringFromTable());
493c572e92cSDiego Novillo       if (std::error_code EC = CalledFunction.getError())
494c572e92cSDiego Novillo         return EC;
495c572e92cSDiego Novillo 
496c572e92cSDiego Novillo       auto CalledFunctionSamples = readNumber<uint64_t>();
497c572e92cSDiego Novillo       if (std::error_code EC = CalledFunctionSamples.getError())
498c572e92cSDiego Novillo         return EC;
499c572e92cSDiego Novillo 
500c572e92cSDiego Novillo       FProfile.addCalledTargetSamples(*LineOffset, *Discriminator,
501a7f1e8efSDiego Novillo                                       *CalledFunction, *CalledFunctionSamples);
502c572e92cSDiego Novillo     }
503c572e92cSDiego Novillo 
504c572e92cSDiego Novillo     FProfile.addBodySamples(*LineOffset, *Discriminator, *NumSamples);
505c572e92cSDiego Novillo   }
506a7f1e8efSDiego Novillo 
507a7f1e8efSDiego Novillo   // Read all the samples for inlined function calls.
50838be3330SDiego Novillo   auto NumCallsites = readNumber<uint32_t>();
509a7f1e8efSDiego Novillo   if (std::error_code EC = NumCallsites.getError())
510a7f1e8efSDiego Novillo     return EC;
511a7f1e8efSDiego Novillo 
51238be3330SDiego Novillo   for (uint32_t J = 0; J < *NumCallsites; ++J) {
513a7f1e8efSDiego Novillo     auto LineOffset = readNumber<uint64_t>();
514a7f1e8efSDiego Novillo     if (std::error_code EC = LineOffset.getError())
515a7f1e8efSDiego Novillo       return EC;
516a7f1e8efSDiego Novillo 
517a7f1e8efSDiego Novillo     auto Discriminator = readNumber<uint64_t>();
518a7f1e8efSDiego Novillo     if (std::error_code EC = Discriminator.getError())
519a7f1e8efSDiego Novillo       return EC;
520a7f1e8efSDiego Novillo 
521760c5a8fSDiego Novillo     auto FName(readStringFromTable());
522a7f1e8efSDiego Novillo     if (std::error_code EC = FName.getError())
523a7f1e8efSDiego Novillo       return EC;
524a7f1e8efSDiego Novillo 
5252c7ca9b5SDehao Chen     FunctionSamples &CalleeProfile = FProfile.functionSamplesAt(
526adcd0268SBenjamin Kramer         LineLocation(*LineOffset, *Discriminator))[std::string(*FName)];
52757d1dda5SDehao Chen     CalleeProfile.setName(*FName);
528a7f1e8efSDiego Novillo     if (std::error_code EC = readProfile(CalleeProfile))
529a7f1e8efSDiego Novillo       return EC;
530a7f1e8efSDiego Novillo   }
531a7f1e8efSDiego Novillo 
532a7f1e8efSDiego Novillo   return sampleprof_error::success;
533a7f1e8efSDiego Novillo }
534a7f1e8efSDiego Novillo 
53509dcfe68SWei Mi std::error_code
53609dcfe68SWei Mi SampleProfileReaderBinary::readFuncProfile(const uint8_t *Start) {
53709dcfe68SWei Mi   Data = Start;
538b93483dbSDiego Novillo   auto NumHeadSamples = readNumber<uint64_t>();
539b93483dbSDiego Novillo   if (std::error_code EC = NumHeadSamples.getError())
540b93483dbSDiego Novillo     return EC;
541b93483dbSDiego Novillo 
542760c5a8fSDiego Novillo   auto FName(readStringFromTable());
543a7f1e8efSDiego Novillo   if (std::error_code EC = FName.getError())
544a7f1e8efSDiego Novillo     return EC;
545a7f1e8efSDiego Novillo 
546*7e99bddfSHongtao Yu   SampleContext FContext(*FName);
547*7e99bddfSHongtao Yu   Profiles[FContext] = FunctionSamples();
548*7e99bddfSHongtao Yu   FunctionSamples &FProfile = Profiles[FContext];
549*7e99bddfSHongtao Yu   FProfile.setName(FContext.getNameWithoutContext());
550*7e99bddfSHongtao Yu   FProfile.setContext(FContext);
551b93483dbSDiego Novillo   FProfile.addHeadSamples(*NumHeadSamples);
552b93483dbSDiego Novillo 
553*7e99bddfSHongtao Yu   if (FContext.hasContext())
554*7e99bddfSHongtao Yu     CSProfileCount++;
555*7e99bddfSHongtao Yu 
556a7f1e8efSDiego Novillo   if (std::error_code EC = readProfile(FProfile))
557a7f1e8efSDiego Novillo     return EC;
5586a14325dSWei Mi   return sampleprof_error::success;
559c572e92cSDiego Novillo }
560c572e92cSDiego Novillo 
5618c8ec1f6SWei Mi std::error_code SampleProfileReaderBinary::readImpl() {
5626a14325dSWei Mi   while (!at_eof()) {
56309dcfe68SWei Mi     if (std::error_code EC = readFuncProfile(Data))
5646a14325dSWei Mi       return EC;
5656a14325dSWei Mi   }
5666a14325dSWei Mi 
5676a14325dSWei Mi   return sampleprof_error::success;
5686a14325dSWei Mi }
5696a14325dSWei Mi 
57093953d41SWei Mi std::error_code SampleProfileReaderExtBinaryBase::readOneSection(
571ebad6788SWei Mi     const uint8_t *Start, uint64_t Size, const SecHdrTableEntry &Entry) {
572077a9c70SWei Mi   Data = Start;
573b523790aSWei Mi   End = Start + Size;
574ebad6788SWei Mi   switch (Entry.Type) {
575be907324SWei Mi   case SecProfSummary:
576be907324SWei Mi     if (std::error_code EC = readSummary())
577be907324SWei Mi       return EC;
578b49eac71SWei Mi     if (hasSecFlag(Entry, SecProfSummaryFlags::SecFlagPartial))
579b49eac71SWei Mi       Summary->setPartialProfile(true);
580be907324SWei Mi     break;
58164e76853SWei Mi   case SecNameTable: {
58264e76853SWei Mi     FixedLengthMD5 =
58364e76853SWei Mi         hasSecFlag(Entry, SecNameTableFlags::SecFlagFixedLengthMD5);
58464e76853SWei Mi     bool UseMD5 = hasSecFlag(Entry, SecNameTableFlags::SecFlagMD5Name);
58564e76853SWei Mi     assert((!FixedLengthMD5 || UseMD5) &&
58664e76853SWei Mi            "If FixedLengthMD5 is true, UseMD5 has to be true");
58764e76853SWei Mi     if (std::error_code EC = readNameTableSec(UseMD5))
588be907324SWei Mi       return EC;
589be907324SWei Mi     break;
59064e76853SWei Mi   }
591be907324SWei Mi   case SecLBRProfile:
59209dcfe68SWei Mi     if (std::error_code EC = readFuncProfiles())
593be907324SWei Mi       return EC;
594be907324SWei Mi     break;
59509dcfe68SWei Mi   case SecFuncOffsetTable:
59609dcfe68SWei Mi     if (std::error_code EC = readFuncOffsetTable())
597798e59b8SWei Mi       return EC;
598798e59b8SWei Mi     break;
599ac068e01SHongtao Yu   case SecFuncMetadata:
600ac068e01SHongtao Yu     ProfileIsProbeBased =
601ac068e01SHongtao Yu         hasSecFlag(Entry, SecFuncMetadataFlags::SecFlagIsProbeBased);
602ac068e01SHongtao Yu     FunctionSamples::ProfileIsProbeBased = ProfileIsProbeBased;
603ac068e01SHongtao Yu     if (std::error_code EC = readFuncMetadata())
604ac068e01SHongtao Yu       return EC;
605ac068e01SHongtao Yu     break;
60693953d41SWei Mi   case SecProfileSymbolList:
60793953d41SWei Mi     if (std::error_code EC = readProfileSymbolList())
60893953d41SWei Mi       return EC;
60993953d41SWei Mi     break;
610be907324SWei Mi   default:
61193953d41SWei Mi     if (std::error_code EC = readCustomSection(Entry))
61293953d41SWei Mi       return EC;
613077a9c70SWei Mi     break;
614be907324SWei Mi   }
615077a9c70SWei Mi   return sampleprof_error::success;
616077a9c70SWei Mi }
617077a9c70SWei Mi 
61893953d41SWei Mi void SampleProfileReaderExtBinaryBase::collectFuncsFrom(const Module &M) {
61909dcfe68SWei Mi   UseAllFuncs = false;
62009dcfe68SWei Mi   FuncsToUse.clear();
62109dcfe68SWei Mi   for (auto &F : M)
62209dcfe68SWei Mi     FuncsToUse.insert(FunctionSamples::getCanonicalFnName(F));
62309dcfe68SWei Mi }
62409dcfe68SWei Mi 
62593953d41SWei Mi std::error_code SampleProfileReaderExtBinaryBase::readFuncOffsetTable() {
626a906e3ecSWei Mi   // If there are more than one FuncOffsetTable, the profile read associated
627a906e3ecSWei Mi   // with previous FuncOffsetTable has to be done before next FuncOffsetTable
628a906e3ecSWei Mi   // is read.
629a906e3ecSWei Mi   FuncOffsetTable.clear();
630a906e3ecSWei Mi 
63109dcfe68SWei Mi   auto Size = readNumber<uint64_t>();
63209dcfe68SWei Mi   if (std::error_code EC = Size.getError())
63309dcfe68SWei Mi     return EC;
63409dcfe68SWei Mi 
63509dcfe68SWei Mi   FuncOffsetTable.reserve(*Size);
63609dcfe68SWei Mi   for (uint32_t I = 0; I < *Size; ++I) {
63709dcfe68SWei Mi     auto FName(readStringFromTable());
63809dcfe68SWei Mi     if (std::error_code EC = FName.getError())
63909dcfe68SWei Mi       return EC;
64009dcfe68SWei Mi 
64109dcfe68SWei Mi     auto Offset = readNumber<uint64_t>();
64209dcfe68SWei Mi     if (std::error_code EC = Offset.getError())
64309dcfe68SWei Mi       return EC;
64409dcfe68SWei Mi 
64509dcfe68SWei Mi     FuncOffsetTable[*FName] = *Offset;
64609dcfe68SWei Mi   }
64709dcfe68SWei Mi   return sampleprof_error::success;
64809dcfe68SWei Mi }
64909dcfe68SWei Mi 
65093953d41SWei Mi std::error_code SampleProfileReaderExtBinaryBase::readFuncProfiles() {
65109dcfe68SWei Mi   const uint8_t *Start = Data;
65209dcfe68SWei Mi   if (UseAllFuncs) {
65309dcfe68SWei Mi     while (Data < End) {
65409dcfe68SWei Mi       if (std::error_code EC = readFuncProfile(Data))
65509dcfe68SWei Mi         return EC;
65609dcfe68SWei Mi     }
65709dcfe68SWei Mi     assert(Data == End && "More data is read than expected");
658*7e99bddfSHongtao Yu   } else {
6598c8ec1f6SWei Mi     if (Remapper) {
66009dcfe68SWei Mi       for (auto Name : FuncsToUse) {
6618c8ec1f6SWei Mi         Remapper->insert(Name);
6628c8ec1f6SWei Mi       }
6638c8ec1f6SWei Mi     }
6648c8ec1f6SWei Mi 
665ebad6788SWei Mi     if (useMD5()) {
666ebad6788SWei Mi       for (auto Name : FuncsToUse) {
667ebad6788SWei Mi         auto GUID = std::to_string(MD5Hash(Name));
668ebad6788SWei Mi         auto iter = FuncOffsetTable.find(StringRef(GUID));
669ebad6788SWei Mi         if (iter == FuncOffsetTable.end())
670ebad6788SWei Mi           continue;
671ebad6788SWei Mi         const uint8_t *FuncProfileAddr = Start + iter->second;
672ebad6788SWei Mi         assert(FuncProfileAddr < End && "out of LBRProfile section");
673ebad6788SWei Mi         if (std::error_code EC = readFuncProfile(FuncProfileAddr))
674ebad6788SWei Mi           return EC;
675ebad6788SWei Mi       }
676ebad6788SWei Mi     } else {
6778c8ec1f6SWei Mi       for (auto NameOffset : FuncOffsetTable) {
678*7e99bddfSHongtao Yu         SampleContext FContext(NameOffset.first);
679*7e99bddfSHongtao Yu         auto FuncName = FContext.getNameWithoutContext();
6808c8ec1f6SWei Mi         if (!FuncsToUse.count(FuncName) &&
6818c8ec1f6SWei Mi             (!Remapper || !Remapper->exist(FuncName)))
68209dcfe68SWei Mi           continue;
6838c8ec1f6SWei Mi         const uint8_t *FuncProfileAddr = Start + NameOffset.second;
68409dcfe68SWei Mi         assert(FuncProfileAddr < End && "out of LBRProfile section");
68509dcfe68SWei Mi         if (std::error_code EC = readFuncProfile(FuncProfileAddr))
68609dcfe68SWei Mi           return EC;
68709dcfe68SWei Mi       }
688ebad6788SWei Mi     }
68909dcfe68SWei Mi     Data = End;
690*7e99bddfSHongtao Yu   }
691*7e99bddfSHongtao Yu 
692*7e99bddfSHongtao Yu   assert((CSProfileCount == 0 || CSProfileCount == Profiles.size()) &&
693*7e99bddfSHongtao Yu          "Cannot have both context-sensitive and regular profile");
694*7e99bddfSHongtao Yu   ProfileIsCS = (CSProfileCount > 0);
695*7e99bddfSHongtao Yu   FunctionSamples::ProfileIsCS = ProfileIsCS;
69609dcfe68SWei Mi   return sampleprof_error::success;
69709dcfe68SWei Mi }
69809dcfe68SWei Mi 
69993953d41SWei Mi std::error_code SampleProfileReaderExtBinaryBase::readProfileSymbolList() {
700b523790aSWei Mi   if (!ProfSymList)
701b523790aSWei Mi     ProfSymList = std::make_unique<ProfileSymbolList>();
702b523790aSWei Mi 
70309dcfe68SWei Mi   if (std::error_code EC = ProfSymList->read(Data, End - Data))
704798e59b8SWei Mi     return EC;
705798e59b8SWei Mi 
70609dcfe68SWei Mi   Data = End;
707b523790aSWei Mi   return sampleprof_error::success;
708b523790aSWei Mi }
709b523790aSWei Mi 
710b523790aSWei Mi std::error_code SampleProfileReaderExtBinaryBase::decompressSection(
711b523790aSWei Mi     const uint8_t *SecStart, const uint64_t SecSize,
712b523790aSWei Mi     const uint8_t *&DecompressBuf, uint64_t &DecompressBufSize) {
713b523790aSWei Mi   Data = SecStart;
714b523790aSWei Mi   End = SecStart + SecSize;
715b523790aSWei Mi   auto DecompressSize = readNumber<uint64_t>();
716b523790aSWei Mi   if (std::error_code EC = DecompressSize.getError())
717b523790aSWei Mi     return EC;
718b523790aSWei Mi   DecompressBufSize = *DecompressSize;
719b523790aSWei Mi 
720798e59b8SWei Mi   auto CompressSize = readNumber<uint64_t>();
721798e59b8SWei Mi   if (std::error_code EC = CompressSize.getError())
722798e59b8SWei Mi     return EC;
723798e59b8SWei Mi 
724b523790aSWei Mi   if (!llvm::zlib::isAvailable())
725b523790aSWei Mi     return sampleprof_error::zlib_unavailable;
726798e59b8SWei Mi 
727b523790aSWei Mi   StringRef CompressedStrings(reinterpret_cast<const char *>(Data),
728b523790aSWei Mi                               *CompressSize);
729b523790aSWei Mi   char *Buffer = Allocator.Allocate<char>(DecompressBufSize);
730283df8cfSWei Mi   size_t UCSize = DecompressBufSize;
731b523790aSWei Mi   llvm::Error E =
732283df8cfSWei Mi       zlib::uncompress(CompressedStrings, Buffer, UCSize);
733b523790aSWei Mi   if (E)
734b523790aSWei Mi     return sampleprof_error::uncompress_failed;
735b523790aSWei Mi   DecompressBuf = reinterpret_cast<const uint8_t *>(Buffer);
736798e59b8SWei Mi   return sampleprof_error::success;
737798e59b8SWei Mi }
738798e59b8SWei Mi 
7398c8ec1f6SWei Mi std::error_code SampleProfileReaderExtBinaryBase::readImpl() {
740077a9c70SWei Mi   const uint8_t *BufStart =
741077a9c70SWei Mi       reinterpret_cast<const uint8_t *>(Buffer->getBufferStart());
742077a9c70SWei Mi 
743077a9c70SWei Mi   for (auto &Entry : SecHdrTable) {
744077a9c70SWei Mi     // Skip empty section.
745077a9c70SWei Mi     if (!Entry.Size)
746077a9c70SWei Mi       continue;
747b523790aSWei Mi 
74821b1ad03SWei Mi     // Skip sections without context when SkipFlatProf is true.
74921b1ad03SWei Mi     if (SkipFlatProf && hasSecFlag(Entry, SecCommonFlags::SecFlagFlat))
75021b1ad03SWei Mi       continue;
75121b1ad03SWei Mi 
752077a9c70SWei Mi     const uint8_t *SecStart = BufStart + Entry.Offset;
753b523790aSWei Mi     uint64_t SecSize = Entry.Size;
754b523790aSWei Mi 
755b523790aSWei Mi     // If the section is compressed, decompress it into a buffer
756b523790aSWei Mi     // DecompressBuf before reading the actual data. The pointee of
757b523790aSWei Mi     // 'Data' will be changed to buffer hold by DecompressBuf
758b523790aSWei Mi     // temporarily when reading the actual data.
759ebad6788SWei Mi     bool isCompressed = hasSecFlag(Entry, SecCommonFlags::SecFlagCompress);
760b523790aSWei Mi     if (isCompressed) {
761b523790aSWei Mi       const uint8_t *DecompressBuf;
762b523790aSWei Mi       uint64_t DecompressBufSize;
763b523790aSWei Mi       if (std::error_code EC = decompressSection(
764b523790aSWei Mi               SecStart, SecSize, DecompressBuf, DecompressBufSize))
765077a9c70SWei Mi         return EC;
766b523790aSWei Mi       SecStart = DecompressBuf;
767b523790aSWei Mi       SecSize = DecompressBufSize;
768b523790aSWei Mi     }
769b523790aSWei Mi 
770ebad6788SWei Mi     if (std::error_code EC = readOneSection(SecStart, SecSize, Entry))
771b523790aSWei Mi       return EC;
772b523790aSWei Mi     if (Data != SecStart + SecSize)
773be907324SWei Mi       return sampleprof_error::malformed;
774b523790aSWei Mi 
775b523790aSWei Mi     // Change the pointee of 'Data' from DecompressBuf to original Buffer.
776b523790aSWei Mi     if (isCompressed) {
777b523790aSWei Mi       Data = BufStart + Entry.Offset;
778b523790aSWei Mi       End = BufStart + Buffer->getBufferSize();
779b523790aSWei Mi     }
780be907324SWei Mi   }
781be907324SWei Mi 
782be907324SWei Mi   return sampleprof_error::success;
783be907324SWei Mi }
784be907324SWei Mi 
7858c8ec1f6SWei Mi std::error_code SampleProfileReaderCompactBinary::readImpl() {
786d3289544SWenlei He   std::vector<uint64_t> OffsetsToUse;
787d3289544SWenlei He   if (UseAllFuncs) {
788d3289544SWenlei He     for (auto FuncEntry : FuncOffsetTable) {
789d3289544SWenlei He       OffsetsToUse.push_back(FuncEntry.second);
790d3289544SWenlei He     }
791d3289544SWenlei He   }
792d3289544SWenlei He   else {
7936a14325dSWei Mi     for (auto Name : FuncsToUse) {
7946a14325dSWei Mi       auto GUID = std::to_string(MD5Hash(Name));
7956a14325dSWei Mi       auto iter = FuncOffsetTable.find(StringRef(GUID));
7966a14325dSWei Mi       if (iter == FuncOffsetTable.end())
7976a14325dSWei Mi         continue;
798d3289544SWenlei He       OffsetsToUse.push_back(iter->second);
799d3289544SWenlei He     }
800d3289544SWenlei He   }
801d3289544SWenlei He 
802d3289544SWenlei He   for (auto Offset : OffsetsToUse) {
8036a14325dSWei Mi     const uint8_t *SavedData = Data;
80409dcfe68SWei Mi     if (std::error_code EC = readFuncProfile(
80509dcfe68SWei Mi             reinterpret_cast<const uint8_t *>(Buffer->getBufferStart()) +
80609dcfe68SWei Mi             Offset))
8076a14325dSWei Mi       return EC;
8086a14325dSWei Mi     Data = SavedData;
8096a14325dSWei Mi   }
810c572e92cSDiego Novillo   return sampleprof_error::success;
811c572e92cSDiego Novillo }
812c572e92cSDiego Novillo 
813a0c0857eSWei Mi std::error_code SampleProfileReaderRawBinary::verifySPMagic(uint64_t Magic) {
814a0c0857eSWei Mi   if (Magic == SPMagic())
815a0c0857eSWei Mi     return sampleprof_error::success;
816a0c0857eSWei Mi   return sampleprof_error::bad_magic;
817a0c0857eSWei Mi }
818a0c0857eSWei Mi 
819be907324SWei Mi std::error_code SampleProfileReaderExtBinary::verifySPMagic(uint64_t Magic) {
820be907324SWei Mi   if (Magic == SPMagic(SPF_Ext_Binary))
821be907324SWei Mi     return sampleprof_error::success;
822be907324SWei Mi   return sampleprof_error::bad_magic;
823be907324SWei Mi }
824be907324SWei Mi 
825a0c0857eSWei Mi std::error_code
826a0c0857eSWei Mi SampleProfileReaderCompactBinary::verifySPMagic(uint64_t Magic) {
827a0c0857eSWei Mi   if (Magic == SPMagic(SPF_Compact_Binary))
828a0c0857eSWei Mi     return sampleprof_error::success;
829a0c0857eSWei Mi   return sampleprof_error::bad_magic;
830a0c0857eSWei Mi }
831a0c0857eSWei Mi 
832be907324SWei Mi std::error_code SampleProfileReaderBinary::readNameTable() {
833a0c0857eSWei Mi   auto Size = readNumber<uint32_t>();
834a0c0857eSWei Mi   if (std::error_code EC = Size.getError())
835a0c0857eSWei Mi     return EC;
836a906e3ecSWei Mi   NameTable.reserve(*Size + NameTable.size());
837a0c0857eSWei Mi   for (uint32_t I = 0; I < *Size; ++I) {
838a0c0857eSWei Mi     auto Name(readString());
839a0c0857eSWei Mi     if (std::error_code EC = Name.getError())
840a0c0857eSWei Mi       return EC;
841a0c0857eSWei Mi     NameTable.push_back(*Name);
842a0c0857eSWei Mi   }
843a0c0857eSWei Mi 
844a0c0857eSWei Mi   return sampleprof_error::success;
845a0c0857eSWei Mi }
846a0c0857eSWei Mi 
84793953d41SWei Mi std::error_code SampleProfileReaderExtBinaryBase::readMD5NameTable() {
848ebad6788SWei Mi   auto Size = readNumber<uint64_t>();
849ebad6788SWei Mi   if (std::error_code EC = Size.getError())
850ebad6788SWei Mi     return EC;
851ebad6788SWei Mi   MD5StringBuf = std::make_unique<std::vector<std::string>>();
852ebad6788SWei Mi   MD5StringBuf->reserve(*Size);
85364e76853SWei Mi   if (FixedLengthMD5) {
85464e76853SWei Mi     // Preallocate and initialize NameTable so we can check whether a name
85564e76853SWei Mi     // index has been read before by checking whether the element in the
85664e76853SWei Mi     // NameTable is empty, meanwhile readStringIndex can do the boundary
85764e76853SWei Mi     // check using the size of NameTable.
85864e76853SWei Mi     NameTable.resize(*Size + NameTable.size());
85964e76853SWei Mi 
86064e76853SWei Mi     MD5NameMemStart = Data;
86164e76853SWei Mi     Data = Data + (*Size) * sizeof(uint64_t);
86264e76853SWei Mi     return sampleprof_error::success;
86364e76853SWei Mi   }
86464e76853SWei Mi   NameTable.reserve(*Size);
865ebad6788SWei Mi   for (uint32_t I = 0; I < *Size; ++I) {
866ebad6788SWei Mi     auto FID = readNumber<uint64_t>();
867ebad6788SWei Mi     if (std::error_code EC = FID.getError())
868ebad6788SWei Mi       return EC;
869ebad6788SWei Mi     MD5StringBuf->push_back(std::to_string(*FID));
870ebad6788SWei Mi     // NameTable is a vector of StringRef. Here it is pushing back a
871ebad6788SWei Mi     // StringRef initialized with the last string in MD5stringBuf.
872ebad6788SWei Mi     NameTable.push_back(MD5StringBuf->back());
873ebad6788SWei Mi   }
874ebad6788SWei Mi   return sampleprof_error::success;
875ebad6788SWei Mi }
876ebad6788SWei Mi 
87793953d41SWei Mi std::error_code SampleProfileReaderExtBinaryBase::readNameTableSec(bool IsMD5) {
878ebad6788SWei Mi   if (IsMD5)
879ebad6788SWei Mi     return readMD5NameTable();
880ebad6788SWei Mi   return SampleProfileReaderBinary::readNameTable();
881ebad6788SWei Mi }
882ebad6788SWei Mi 
883ac068e01SHongtao Yu std::error_code SampleProfileReaderExtBinaryBase::readFuncMetadata() {
884ac068e01SHongtao Yu   if (!ProfileIsProbeBased)
885ac068e01SHongtao Yu     return sampleprof_error::success;
886ac068e01SHongtao Yu   for (unsigned I = 0; I < Profiles.size(); ++I) {
887ac068e01SHongtao Yu     auto FName(readStringFromTable());
888ac068e01SHongtao Yu     if (std::error_code EC = FName.getError())
889ac068e01SHongtao Yu       return EC;
890ac068e01SHongtao Yu 
891ac068e01SHongtao Yu     auto Checksum = readNumber<uint64_t>();
892ac068e01SHongtao Yu     if (std::error_code EC = Checksum.getError())
893ac068e01SHongtao Yu       return EC;
894ac068e01SHongtao Yu 
895*7e99bddfSHongtao Yu     SampleContext FContext(*FName);
896*7e99bddfSHongtao Yu     Profiles[FContext].setFunctionHash(*Checksum);
897ac068e01SHongtao Yu   }
898ac068e01SHongtao Yu   return sampleprof_error::success;
899ac068e01SHongtao Yu }
900ac068e01SHongtao Yu 
901a0c0857eSWei Mi std::error_code SampleProfileReaderCompactBinary::readNameTable() {
902a0c0857eSWei Mi   auto Size = readNumber<uint64_t>();
903a0c0857eSWei Mi   if (std::error_code EC = Size.getError())
904a0c0857eSWei Mi     return EC;
905a0c0857eSWei Mi   NameTable.reserve(*Size);
906a0c0857eSWei Mi   for (uint32_t I = 0; I < *Size; ++I) {
907a0c0857eSWei Mi     auto FID = readNumber<uint64_t>();
908a0c0857eSWei Mi     if (std::error_code EC = FID.getError())
909a0c0857eSWei Mi       return EC;
910a0c0857eSWei Mi     NameTable.push_back(std::to_string(*FID));
911a0c0857eSWei Mi   }
912a0c0857eSWei Mi   return sampleprof_error::success;
913a0c0857eSWei Mi }
914a0c0857eSWei Mi 
915a906e3ecSWei Mi std::error_code
916a906e3ecSWei Mi SampleProfileReaderExtBinaryBase::readSecHdrTableEntry(uint32_t Idx) {
917be907324SWei Mi   SecHdrTableEntry Entry;
918be907324SWei Mi   auto Type = readUnencodedNumber<uint64_t>();
919be907324SWei Mi   if (std::error_code EC = Type.getError())
920be907324SWei Mi     return EC;
921be907324SWei Mi   Entry.Type = static_cast<SecType>(*Type);
922c572e92cSDiego Novillo 
923b523790aSWei Mi   auto Flags = readUnencodedNumber<uint64_t>();
924b523790aSWei Mi   if (std::error_code EC = Flags.getError())
925be907324SWei Mi     return EC;
926b523790aSWei Mi   Entry.Flags = *Flags;
927be907324SWei Mi 
928be907324SWei Mi   auto Offset = readUnencodedNumber<uint64_t>();
929be907324SWei Mi   if (std::error_code EC = Offset.getError())
930be907324SWei Mi     return EC;
931be907324SWei Mi   Entry.Offset = *Offset;
932be907324SWei Mi 
933be907324SWei Mi   auto Size = readUnencodedNumber<uint64_t>();
934be907324SWei Mi   if (std::error_code EC = Size.getError())
935be907324SWei Mi     return EC;
936be907324SWei Mi   Entry.Size = *Size;
937be907324SWei Mi 
938a906e3ecSWei Mi   Entry.LayoutIndex = Idx;
939be907324SWei Mi   SecHdrTable.push_back(std::move(Entry));
940be907324SWei Mi   return sampleprof_error::success;
941be907324SWei Mi }
942be907324SWei Mi 
943be907324SWei Mi std::error_code SampleProfileReaderExtBinaryBase::readSecHdrTable() {
944be907324SWei Mi   auto EntryNum = readUnencodedNumber<uint64_t>();
945be907324SWei Mi   if (std::error_code EC = EntryNum.getError())
946be907324SWei Mi     return EC;
947be907324SWei Mi 
948be907324SWei Mi   for (uint32_t i = 0; i < (*EntryNum); i++)
949a906e3ecSWei Mi     if (std::error_code EC = readSecHdrTableEntry(i))
950be907324SWei Mi       return EC;
951be907324SWei Mi 
952be907324SWei Mi   return sampleprof_error::success;
953be907324SWei Mi }
954be907324SWei Mi 
955be907324SWei Mi std::error_code SampleProfileReaderExtBinaryBase::readHeader() {
956be907324SWei Mi   const uint8_t *BufStart =
957be907324SWei Mi       reinterpret_cast<const uint8_t *>(Buffer->getBufferStart());
958be907324SWei Mi   Data = BufStart;
959be907324SWei Mi   End = BufStart + Buffer->getBufferSize();
960be907324SWei Mi 
961be907324SWei Mi   if (std::error_code EC = readMagicIdent())
962be907324SWei Mi     return EC;
963be907324SWei Mi 
964be907324SWei Mi   if (std::error_code EC = readSecHdrTable())
965be907324SWei Mi     return EC;
966be907324SWei Mi 
967be907324SWei Mi   return sampleprof_error::success;
968be907324SWei Mi }
969be907324SWei Mi 
970eee532cdSWei Mi uint64_t SampleProfileReaderExtBinaryBase::getSectionSize(SecType Type) {
971a906e3ecSWei Mi   uint64_t Size = 0;
972eee532cdSWei Mi   for (auto &Entry : SecHdrTable) {
973eee532cdSWei Mi     if (Entry.Type == Type)
974a906e3ecSWei Mi       Size += Entry.Size;
975eee532cdSWei Mi   }
976a906e3ecSWei Mi   return Size;
977eee532cdSWei Mi }
978eee532cdSWei Mi 
979eee532cdSWei Mi uint64_t SampleProfileReaderExtBinaryBase::getFileSize() {
98009dcfe68SWei Mi   // Sections in SecHdrTable is not necessarily in the same order as
98109dcfe68SWei Mi   // sections in the profile because section like FuncOffsetTable needs
98209dcfe68SWei Mi   // to be written after section LBRProfile but needs to be read before
98309dcfe68SWei Mi   // section LBRProfile, so we cannot simply use the last entry in
98409dcfe68SWei Mi   // SecHdrTable to calculate the file size.
98509dcfe68SWei Mi   uint64_t FileSize = 0;
98609dcfe68SWei Mi   for (auto &Entry : SecHdrTable) {
98709dcfe68SWei Mi     FileSize = std::max(Entry.Offset + Entry.Size, FileSize);
98809dcfe68SWei Mi   }
98909dcfe68SWei Mi   return FileSize;
990eee532cdSWei Mi }
991eee532cdSWei Mi 
992b49eac71SWei Mi static std::string getSecFlagsStr(const SecHdrTableEntry &Entry) {
993b49eac71SWei Mi   std::string Flags;
994b49eac71SWei Mi   if (hasSecFlag(Entry, SecCommonFlags::SecFlagCompress))
995b49eac71SWei Mi     Flags.append("{compressed,");
996b49eac71SWei Mi   else
997b49eac71SWei Mi     Flags.append("{");
998b49eac71SWei Mi 
99921b1ad03SWei Mi   if (hasSecFlag(Entry, SecCommonFlags::SecFlagFlat))
100021b1ad03SWei Mi     Flags.append("flat,");
100121b1ad03SWei Mi 
1002b49eac71SWei Mi   switch (Entry.Type) {
1003b49eac71SWei Mi   case SecNameTable:
100464e76853SWei Mi     if (hasSecFlag(Entry, SecNameTableFlags::SecFlagFixedLengthMD5))
100564e76853SWei Mi       Flags.append("fixlenmd5,");
100664e76853SWei Mi     else if (hasSecFlag(Entry, SecNameTableFlags::SecFlagMD5Name))
1007b49eac71SWei Mi       Flags.append("md5,");
1008b49eac71SWei Mi     break;
1009b49eac71SWei Mi   case SecProfSummary:
1010b49eac71SWei Mi     if (hasSecFlag(Entry, SecProfSummaryFlags::SecFlagPartial))
1011b49eac71SWei Mi       Flags.append("partial,");
1012b49eac71SWei Mi     break;
1013b49eac71SWei Mi   default:
1014b49eac71SWei Mi     break;
1015b49eac71SWei Mi   }
1016b49eac71SWei Mi   char &last = Flags.back();
1017b49eac71SWei Mi   if (last == ',')
1018b49eac71SWei Mi     last = '}';
1019b49eac71SWei Mi   else
1020b49eac71SWei Mi     Flags.append("}");
1021b49eac71SWei Mi   return Flags;
1022b49eac71SWei Mi }
1023b49eac71SWei Mi 
1024eee532cdSWei Mi bool SampleProfileReaderExtBinaryBase::dumpSectionInfo(raw_ostream &OS) {
1025eee532cdSWei Mi   uint64_t TotalSecsSize = 0;
1026eee532cdSWei Mi   for (auto &Entry : SecHdrTable) {
1027eee532cdSWei Mi     OS << getSecName(Entry.Type) << " - Offset: " << Entry.Offset
1028b49eac71SWei Mi        << ", Size: " << Entry.Size << ", Flags: " << getSecFlagsStr(Entry)
1029b49eac71SWei Mi        << "\n";
1030b49eac71SWei Mi     ;
1031a906e3ecSWei Mi     TotalSecsSize += Entry.Size;
1032eee532cdSWei Mi   }
1033eee532cdSWei Mi   uint64_t HeaderSize = SecHdrTable.front().Offset;
1034eee532cdSWei Mi   assert(HeaderSize + TotalSecsSize == getFileSize() &&
1035eee532cdSWei Mi          "Size of 'header + sections' doesn't match the total size of profile");
1036eee532cdSWei Mi 
1037eee532cdSWei Mi   OS << "Header Size: " << HeaderSize << "\n";
1038eee532cdSWei Mi   OS << "Total Sections Size: " << TotalSecsSize << "\n";
1039eee532cdSWei Mi   OS << "File Size: " << getFileSize() << "\n";
1040eee532cdSWei Mi   return true;
1041eee532cdSWei Mi }
1042eee532cdSWei Mi 
1043be907324SWei Mi std::error_code SampleProfileReaderBinary::readMagicIdent() {
1044c572e92cSDiego Novillo   // Read and check the magic identifier.
1045c572e92cSDiego Novillo   auto Magic = readNumber<uint64_t>();
1046c572e92cSDiego Novillo   if (std::error_code EC = Magic.getError())
1047c572e92cSDiego Novillo     return EC;
1048a0c0857eSWei Mi   else if (std::error_code EC = verifySPMagic(*Magic))
1049c6b96c8dSWei Mi     return EC;
1050c572e92cSDiego Novillo 
1051c572e92cSDiego Novillo   // Read the version number.
1052c572e92cSDiego Novillo   auto Version = readNumber<uint64_t>();
1053c572e92cSDiego Novillo   if (std::error_code EC = Version.getError())
1054c572e92cSDiego Novillo     return EC;
1055c572e92cSDiego Novillo   else if (*Version != SPVersion())
1056c572e92cSDiego Novillo     return sampleprof_error::unsupported_version;
1057c572e92cSDiego Novillo 
1058be907324SWei Mi   return sampleprof_error::success;
1059be907324SWei Mi }
1060be907324SWei Mi 
1061be907324SWei Mi std::error_code SampleProfileReaderBinary::readHeader() {
1062be907324SWei Mi   Data = reinterpret_cast<const uint8_t *>(Buffer->getBufferStart());
1063be907324SWei Mi   End = Data + Buffer->getBufferSize();
1064be907324SWei Mi 
1065be907324SWei Mi   if (std::error_code EC = readMagicIdent())
1066be907324SWei Mi     return EC;
1067be907324SWei Mi 
106840ee23dbSEaswaran Raman   if (std::error_code EC = readSummary())
106940ee23dbSEaswaran Raman     return EC;
107040ee23dbSEaswaran Raman 
1071a0c0857eSWei Mi   if (std::error_code EC = readNameTable())
1072760c5a8fSDiego Novillo     return EC;
1073c572e92cSDiego Novillo   return sampleprof_error::success;
1074c572e92cSDiego Novillo }
1075c572e92cSDiego Novillo 
10766a14325dSWei Mi std::error_code SampleProfileReaderCompactBinary::readHeader() {
10776a14325dSWei Mi   SampleProfileReaderBinary::readHeader();
10786a14325dSWei Mi   if (std::error_code EC = readFuncOffsetTable())
10796a14325dSWei Mi     return EC;
10806a14325dSWei Mi   return sampleprof_error::success;
10816a14325dSWei Mi }
10826a14325dSWei Mi 
10836a14325dSWei Mi std::error_code SampleProfileReaderCompactBinary::readFuncOffsetTable() {
10846a14325dSWei Mi   auto TableOffset = readUnencodedNumber<uint64_t>();
10856a14325dSWei Mi   if (std::error_code EC = TableOffset.getError())
10866a14325dSWei Mi     return EC;
10876a14325dSWei Mi 
10886a14325dSWei Mi   const uint8_t *SavedData = Data;
10896a14325dSWei Mi   const uint8_t *TableStart =
10906a14325dSWei Mi       reinterpret_cast<const uint8_t *>(Buffer->getBufferStart()) +
10916a14325dSWei Mi       *TableOffset;
10926a14325dSWei Mi   Data = TableStart;
10936a14325dSWei Mi 
10946a14325dSWei Mi   auto Size = readNumber<uint64_t>();
10956a14325dSWei Mi   if (std::error_code EC = Size.getError())
10966a14325dSWei Mi     return EC;
10976a14325dSWei Mi 
10986a14325dSWei Mi   FuncOffsetTable.reserve(*Size);
10996a14325dSWei Mi   for (uint32_t I = 0; I < *Size; ++I) {
11006a14325dSWei Mi     auto FName(readStringFromTable());
11016a14325dSWei Mi     if (std::error_code EC = FName.getError())
11026a14325dSWei Mi       return EC;
11036a14325dSWei Mi 
11046a14325dSWei Mi     auto Offset = readNumber<uint64_t>();
11056a14325dSWei Mi     if (std::error_code EC = Offset.getError())
11066a14325dSWei Mi       return EC;
11076a14325dSWei Mi 
11086a14325dSWei Mi     FuncOffsetTable[*FName] = *Offset;
11096a14325dSWei Mi   }
11106a14325dSWei Mi   End = TableStart;
11116a14325dSWei Mi   Data = SavedData;
11126a14325dSWei Mi   return sampleprof_error::success;
11136a14325dSWei Mi }
11146a14325dSWei Mi 
111509dcfe68SWei Mi void SampleProfileReaderCompactBinary::collectFuncsFrom(const Module &M) {
1116d3289544SWenlei He   UseAllFuncs = false;
11176a14325dSWei Mi   FuncsToUse.clear();
111809dcfe68SWei Mi   for (auto &F : M)
111909dcfe68SWei Mi     FuncsToUse.insert(FunctionSamples::getCanonicalFnName(F));
11206a14325dSWei Mi }
11216a14325dSWei Mi 
112240ee23dbSEaswaran Raman std::error_code SampleProfileReaderBinary::readSummaryEntry(
112340ee23dbSEaswaran Raman     std::vector<ProfileSummaryEntry> &Entries) {
112440ee23dbSEaswaran Raman   auto Cutoff = readNumber<uint64_t>();
112540ee23dbSEaswaran Raman   if (std::error_code EC = Cutoff.getError())
112640ee23dbSEaswaran Raman     return EC;
112740ee23dbSEaswaran Raman 
112840ee23dbSEaswaran Raman   auto MinBlockCount = readNumber<uint64_t>();
112940ee23dbSEaswaran Raman   if (std::error_code EC = MinBlockCount.getError())
113040ee23dbSEaswaran Raman     return EC;
113140ee23dbSEaswaran Raman 
113240ee23dbSEaswaran Raman   auto NumBlocks = readNumber<uint64_t>();
113340ee23dbSEaswaran Raman   if (std::error_code EC = NumBlocks.getError())
113440ee23dbSEaswaran Raman     return EC;
113540ee23dbSEaswaran Raman 
113640ee23dbSEaswaran Raman   Entries.emplace_back(*Cutoff, *MinBlockCount, *NumBlocks);
113740ee23dbSEaswaran Raman   return sampleprof_error::success;
113840ee23dbSEaswaran Raman }
113940ee23dbSEaswaran Raman 
114040ee23dbSEaswaran Raman std::error_code SampleProfileReaderBinary::readSummary() {
114140ee23dbSEaswaran Raman   auto TotalCount = readNumber<uint64_t>();
114240ee23dbSEaswaran Raman   if (std::error_code EC = TotalCount.getError())
114340ee23dbSEaswaran Raman     return EC;
114440ee23dbSEaswaran Raman 
114540ee23dbSEaswaran Raman   auto MaxBlockCount = readNumber<uint64_t>();
114640ee23dbSEaswaran Raman   if (std::error_code EC = MaxBlockCount.getError())
114740ee23dbSEaswaran Raman     return EC;
114840ee23dbSEaswaran Raman 
114940ee23dbSEaswaran Raman   auto MaxFunctionCount = readNumber<uint64_t>();
115040ee23dbSEaswaran Raman   if (std::error_code EC = MaxFunctionCount.getError())
115140ee23dbSEaswaran Raman     return EC;
115240ee23dbSEaswaran Raman 
115340ee23dbSEaswaran Raman   auto NumBlocks = readNumber<uint64_t>();
115440ee23dbSEaswaran Raman   if (std::error_code EC = NumBlocks.getError())
115540ee23dbSEaswaran Raman     return EC;
115640ee23dbSEaswaran Raman 
115740ee23dbSEaswaran Raman   auto NumFunctions = readNumber<uint64_t>();
115840ee23dbSEaswaran Raman   if (std::error_code EC = NumFunctions.getError())
115940ee23dbSEaswaran Raman     return EC;
116040ee23dbSEaswaran Raman 
116140ee23dbSEaswaran Raman   auto NumSummaryEntries = readNumber<uint64_t>();
116240ee23dbSEaswaran Raman   if (std::error_code EC = NumSummaryEntries.getError())
116340ee23dbSEaswaran Raman     return EC;
116440ee23dbSEaswaran Raman 
116540ee23dbSEaswaran Raman   std::vector<ProfileSummaryEntry> Entries;
116640ee23dbSEaswaran Raman   for (unsigned i = 0; i < *NumSummaryEntries; i++) {
116740ee23dbSEaswaran Raman     std::error_code EC = readSummaryEntry(Entries);
116840ee23dbSEaswaran Raman     if (EC != sampleprof_error::success)
116940ee23dbSEaswaran Raman       return EC;
117040ee23dbSEaswaran Raman   }
11710eaee545SJonas Devlieghere   Summary = std::make_unique<ProfileSummary>(
11727cefdb81SEaswaran Raman       ProfileSummary::PSK_Sample, Entries, *TotalCount, *MaxBlockCount, 0,
11737cefdb81SEaswaran Raman       *MaxFunctionCount, *NumBlocks, *NumFunctions);
117440ee23dbSEaswaran Raman 
117540ee23dbSEaswaran Raman   return sampleprof_error::success;
117640ee23dbSEaswaran Raman }
117740ee23dbSEaswaran Raman 
1178a0c0857eSWei Mi bool SampleProfileReaderRawBinary::hasFormat(const MemoryBuffer &Buffer) {
1179c572e92cSDiego Novillo   const uint8_t *Data =
1180c572e92cSDiego Novillo       reinterpret_cast<const uint8_t *>(Buffer.getBufferStart());
1181c572e92cSDiego Novillo   uint64_t Magic = decodeULEB128(Data);
1182c572e92cSDiego Novillo   return Magic == SPMagic();
1183c572e92cSDiego Novillo }
1184c572e92cSDiego Novillo 
1185be907324SWei Mi bool SampleProfileReaderExtBinary::hasFormat(const MemoryBuffer &Buffer) {
1186be907324SWei Mi   const uint8_t *Data =
1187be907324SWei Mi       reinterpret_cast<const uint8_t *>(Buffer.getBufferStart());
1188be907324SWei Mi   uint64_t Magic = decodeULEB128(Data);
1189be907324SWei Mi   return Magic == SPMagic(SPF_Ext_Binary);
1190be907324SWei Mi }
1191be907324SWei Mi 
1192a0c0857eSWei Mi bool SampleProfileReaderCompactBinary::hasFormat(const MemoryBuffer &Buffer) {
1193a0c0857eSWei Mi   const uint8_t *Data =
1194a0c0857eSWei Mi       reinterpret_cast<const uint8_t *>(Buffer.getBufferStart());
1195a0c0857eSWei Mi   uint64_t Magic = decodeULEB128(Data);
1196a0c0857eSWei Mi   return Magic == SPMagic(SPF_Compact_Binary);
1197a0c0857eSWei Mi }
1198a0c0857eSWei Mi 
11993376a787SDiego Novillo std::error_code SampleProfileReaderGCC::skipNextWord() {
12003376a787SDiego Novillo   uint32_t dummy;
12013376a787SDiego Novillo   if (!GcovBuffer.readInt(dummy))
12023376a787SDiego Novillo     return sampleprof_error::truncated;
12033376a787SDiego Novillo   return sampleprof_error::success;
12043376a787SDiego Novillo }
12053376a787SDiego Novillo 
12063376a787SDiego Novillo template <typename T> ErrorOr<T> SampleProfileReaderGCC::readNumber() {
12073376a787SDiego Novillo   if (sizeof(T) <= sizeof(uint32_t)) {
12083376a787SDiego Novillo     uint32_t Val;
12093376a787SDiego Novillo     if (GcovBuffer.readInt(Val) && Val <= std::numeric_limits<T>::max())
12103376a787SDiego Novillo       return static_cast<T>(Val);
12113376a787SDiego Novillo   } else if (sizeof(T) <= sizeof(uint64_t)) {
12123376a787SDiego Novillo     uint64_t Val;
12133376a787SDiego Novillo     if (GcovBuffer.readInt64(Val) && Val <= std::numeric_limits<T>::max())
12143376a787SDiego Novillo       return static_cast<T>(Val);
12153376a787SDiego Novillo   }
12163376a787SDiego Novillo 
12173376a787SDiego Novillo   std::error_code EC = sampleprof_error::malformed;
12183376a787SDiego Novillo   reportError(0, EC.message());
12193376a787SDiego Novillo   return EC;
12203376a787SDiego Novillo }
12213376a787SDiego Novillo 
12223376a787SDiego Novillo ErrorOr<StringRef> SampleProfileReaderGCC::readString() {
12233376a787SDiego Novillo   StringRef Str;
12243376a787SDiego Novillo   if (!GcovBuffer.readString(Str))
12253376a787SDiego Novillo     return sampleprof_error::truncated;
12263376a787SDiego Novillo   return Str;
12273376a787SDiego Novillo }
12283376a787SDiego Novillo 
12293376a787SDiego Novillo std::error_code SampleProfileReaderGCC::readHeader() {
12303376a787SDiego Novillo   // Read the magic identifier.
12313376a787SDiego Novillo   if (!GcovBuffer.readGCDAFormat())
12323376a787SDiego Novillo     return sampleprof_error::unrecognized_format;
12333376a787SDiego Novillo 
12343376a787SDiego Novillo   // Read the version number. Note - the GCC reader does not validate this
12353376a787SDiego Novillo   // version, but the profile creator generates v704.
12363376a787SDiego Novillo   GCOV::GCOVVersion version;
12373376a787SDiego Novillo   if (!GcovBuffer.readGCOVVersion(version))
12383376a787SDiego Novillo     return sampleprof_error::unrecognized_format;
12393376a787SDiego Novillo 
12402d00eb17SFangrui Song   if (version != GCOV::V407)
12413376a787SDiego Novillo     return sampleprof_error::unsupported_version;
12423376a787SDiego Novillo 
12433376a787SDiego Novillo   // Skip the empty integer.
12443376a787SDiego Novillo   if (std::error_code EC = skipNextWord())
12453376a787SDiego Novillo     return EC;
12463376a787SDiego Novillo 
12473376a787SDiego Novillo   return sampleprof_error::success;
12483376a787SDiego Novillo }
12493376a787SDiego Novillo 
12503376a787SDiego Novillo std::error_code SampleProfileReaderGCC::readSectionTag(uint32_t Expected) {
12513376a787SDiego Novillo   uint32_t Tag;
12523376a787SDiego Novillo   if (!GcovBuffer.readInt(Tag))
12533376a787SDiego Novillo     return sampleprof_error::truncated;
12543376a787SDiego Novillo 
12553376a787SDiego Novillo   if (Tag != Expected)
12563376a787SDiego Novillo     return sampleprof_error::malformed;
12573376a787SDiego Novillo 
12583376a787SDiego Novillo   if (std::error_code EC = skipNextWord())
12593376a787SDiego Novillo     return EC;
12603376a787SDiego Novillo 
12613376a787SDiego Novillo   return sampleprof_error::success;
12623376a787SDiego Novillo }
12633376a787SDiego Novillo 
12643376a787SDiego Novillo std::error_code SampleProfileReaderGCC::readNameTable() {
12653376a787SDiego Novillo   if (std::error_code EC = readSectionTag(GCOVTagAFDOFileNames))
12663376a787SDiego Novillo     return EC;
12673376a787SDiego Novillo 
12683376a787SDiego Novillo   uint32_t Size;
12693376a787SDiego Novillo   if (!GcovBuffer.readInt(Size))
12703376a787SDiego Novillo     return sampleprof_error::truncated;
12713376a787SDiego Novillo 
12723376a787SDiego Novillo   for (uint32_t I = 0; I < Size; ++I) {
12733376a787SDiego Novillo     StringRef Str;
12743376a787SDiego Novillo     if (!GcovBuffer.readString(Str))
12753376a787SDiego Novillo       return sampleprof_error::truncated;
1276adcd0268SBenjamin Kramer     Names.push_back(std::string(Str));
12773376a787SDiego Novillo   }
12783376a787SDiego Novillo 
12793376a787SDiego Novillo   return sampleprof_error::success;
12803376a787SDiego Novillo }
12813376a787SDiego Novillo 
12823376a787SDiego Novillo std::error_code SampleProfileReaderGCC::readFunctionProfiles() {
12833376a787SDiego Novillo   if (std::error_code EC = readSectionTag(GCOVTagAFDOFunction))
12843376a787SDiego Novillo     return EC;
12853376a787SDiego Novillo 
12863376a787SDiego Novillo   uint32_t NumFunctions;
12873376a787SDiego Novillo   if (!GcovBuffer.readInt(NumFunctions))
12883376a787SDiego Novillo     return sampleprof_error::truncated;
12893376a787SDiego Novillo 
1290aae1ed8eSDiego Novillo   InlineCallStack Stack;
12913376a787SDiego Novillo   for (uint32_t I = 0; I < NumFunctions; ++I)
1292aae1ed8eSDiego Novillo     if (std::error_code EC = readOneFunctionProfile(Stack, true, 0))
12933376a787SDiego Novillo       return EC;
12943376a787SDiego Novillo 
129540ee23dbSEaswaran Raman   computeSummary();
12963376a787SDiego Novillo   return sampleprof_error::success;
12973376a787SDiego Novillo }
12983376a787SDiego Novillo 
1299aae1ed8eSDiego Novillo std::error_code SampleProfileReaderGCC::readOneFunctionProfile(
1300aae1ed8eSDiego Novillo     const InlineCallStack &InlineStack, bool Update, uint32_t Offset) {
13013376a787SDiego Novillo   uint64_t HeadCount = 0;
1302aae1ed8eSDiego Novillo   if (InlineStack.size() == 0)
13033376a787SDiego Novillo     if (!GcovBuffer.readInt64(HeadCount))
13043376a787SDiego Novillo       return sampleprof_error::truncated;
13053376a787SDiego Novillo 
13063376a787SDiego Novillo   uint32_t NameIdx;
13073376a787SDiego Novillo   if (!GcovBuffer.readInt(NameIdx))
13083376a787SDiego Novillo     return sampleprof_error::truncated;
13093376a787SDiego Novillo 
13103376a787SDiego Novillo   StringRef Name(Names[NameIdx]);
13113376a787SDiego Novillo 
13123376a787SDiego Novillo   uint32_t NumPosCounts;
13133376a787SDiego Novillo   if (!GcovBuffer.readInt(NumPosCounts))
13143376a787SDiego Novillo     return sampleprof_error::truncated;
13153376a787SDiego Novillo 
1316aae1ed8eSDiego Novillo   uint32_t NumCallsites;
1317aae1ed8eSDiego Novillo   if (!GcovBuffer.readInt(NumCallsites))
13183376a787SDiego Novillo     return sampleprof_error::truncated;
13193376a787SDiego Novillo 
1320aae1ed8eSDiego Novillo   FunctionSamples *FProfile = nullptr;
1321aae1ed8eSDiego Novillo   if (InlineStack.size() == 0) {
1322aae1ed8eSDiego Novillo     // If this is a top function that we have already processed, do not
1323aae1ed8eSDiego Novillo     // update its profile again.  This happens in the presence of
1324aae1ed8eSDiego Novillo     // function aliases.  Since these aliases share the same function
1325aae1ed8eSDiego Novillo     // body, there will be identical replicated profiles for the
1326aae1ed8eSDiego Novillo     // original function.  In this case, we simply not bother updating
1327aae1ed8eSDiego Novillo     // the profile of the original function.
1328aae1ed8eSDiego Novillo     FProfile = &Profiles[Name];
1329aae1ed8eSDiego Novillo     FProfile->addHeadSamples(HeadCount);
1330aae1ed8eSDiego Novillo     if (FProfile->getTotalSamples() > 0)
13313376a787SDiego Novillo       Update = false;
1332aae1ed8eSDiego Novillo   } else {
1333aae1ed8eSDiego Novillo     // Otherwise, we are reading an inlined instance. The top of the
1334aae1ed8eSDiego Novillo     // inline stack contains the profile of the caller. Insert this
1335aae1ed8eSDiego Novillo     // callee in the caller's CallsiteMap.
1336aae1ed8eSDiego Novillo     FunctionSamples *CallerProfile = InlineStack.front();
1337aae1ed8eSDiego Novillo     uint32_t LineOffset = Offset >> 16;
1338aae1ed8eSDiego Novillo     uint32_t Discriminator = Offset & 0xffff;
1339aae1ed8eSDiego Novillo     FProfile = &CallerProfile->functionSamplesAt(
1340adcd0268SBenjamin Kramer         LineLocation(LineOffset, Discriminator))[std::string(Name)];
13413376a787SDiego Novillo   }
134257d1dda5SDehao Chen   FProfile->setName(Name);
13433376a787SDiego Novillo 
13443376a787SDiego Novillo   for (uint32_t I = 0; I < NumPosCounts; ++I) {
13453376a787SDiego Novillo     uint32_t Offset;
13463376a787SDiego Novillo     if (!GcovBuffer.readInt(Offset))
13473376a787SDiego Novillo       return sampleprof_error::truncated;
13483376a787SDiego Novillo 
13493376a787SDiego Novillo     uint32_t NumTargets;
13503376a787SDiego Novillo     if (!GcovBuffer.readInt(NumTargets))
13513376a787SDiego Novillo       return sampleprof_error::truncated;
13523376a787SDiego Novillo 
13533376a787SDiego Novillo     uint64_t Count;
13543376a787SDiego Novillo     if (!GcovBuffer.readInt64(Count))
13553376a787SDiego Novillo       return sampleprof_error::truncated;
13563376a787SDiego Novillo 
1357aae1ed8eSDiego Novillo     // The line location is encoded in the offset as:
1358aae1ed8eSDiego Novillo     //   high 16 bits: line offset to the start of the function.
1359aae1ed8eSDiego Novillo     //   low 16 bits: discriminator.
1360aae1ed8eSDiego Novillo     uint32_t LineOffset = Offset >> 16;
1361aae1ed8eSDiego Novillo     uint32_t Discriminator = Offset & 0xffff;
13623376a787SDiego Novillo 
1363aae1ed8eSDiego Novillo     InlineCallStack NewStack;
1364aae1ed8eSDiego Novillo     NewStack.push_back(FProfile);
13651d0bc055SKazu Hirata     llvm::append_range(NewStack, InlineStack);
1366aae1ed8eSDiego Novillo     if (Update) {
1367aae1ed8eSDiego Novillo       // Walk up the inline stack, adding the samples on this line to
1368aae1ed8eSDiego Novillo       // the total sample count of the callers in the chain.
1369aae1ed8eSDiego Novillo       for (auto CallerProfile : NewStack)
1370aae1ed8eSDiego Novillo         CallerProfile->addTotalSamples(Count);
1371aae1ed8eSDiego Novillo 
1372aae1ed8eSDiego Novillo       // Update the body samples for the current profile.
1373aae1ed8eSDiego Novillo       FProfile->addBodySamples(LineOffset, Discriminator, Count);
1374aae1ed8eSDiego Novillo     }
1375aae1ed8eSDiego Novillo 
1376aae1ed8eSDiego Novillo     // Process the list of functions called at an indirect call site.
1377aae1ed8eSDiego Novillo     // These are all the targets that a function pointer (or virtual
1378aae1ed8eSDiego Novillo     // function) resolved at runtime.
13793376a787SDiego Novillo     for (uint32_t J = 0; J < NumTargets; J++) {
13803376a787SDiego Novillo       uint32_t HistVal;
13813376a787SDiego Novillo       if (!GcovBuffer.readInt(HistVal))
13823376a787SDiego Novillo         return sampleprof_error::truncated;
13833376a787SDiego Novillo 
13843376a787SDiego Novillo       if (HistVal != HIST_TYPE_INDIR_CALL_TOPN)
13853376a787SDiego Novillo         return sampleprof_error::malformed;
13863376a787SDiego Novillo 
13873376a787SDiego Novillo       uint64_t TargetIdx;
13883376a787SDiego Novillo       if (!GcovBuffer.readInt64(TargetIdx))
13893376a787SDiego Novillo         return sampleprof_error::truncated;
13903376a787SDiego Novillo       StringRef TargetName(Names[TargetIdx]);
13913376a787SDiego Novillo 
13923376a787SDiego Novillo       uint64_t TargetCount;
13933376a787SDiego Novillo       if (!GcovBuffer.readInt64(TargetCount))
13943376a787SDiego Novillo         return sampleprof_error::truncated;
13953376a787SDiego Novillo 
1396920677a9SDehao Chen       if (Update)
1397920677a9SDehao Chen         FProfile->addCalledTargetSamples(LineOffset, Discriminator,
1398aae1ed8eSDiego Novillo                                          TargetName, TargetCount);
13993376a787SDiego Novillo     }
14003376a787SDiego Novillo   }
14013376a787SDiego Novillo 
1402aae1ed8eSDiego Novillo   // Process all the inlined callers into the current function. These
1403aae1ed8eSDiego Novillo   // are all the callsites that were inlined into this function.
1404aae1ed8eSDiego Novillo   for (uint32_t I = 0; I < NumCallsites; I++) {
14053376a787SDiego Novillo     // The offset is encoded as:
14063376a787SDiego Novillo     //   high 16 bits: line offset to the start of the function.
14073376a787SDiego Novillo     //   low 16 bits: discriminator.
14083376a787SDiego Novillo     uint32_t Offset;
14093376a787SDiego Novillo     if (!GcovBuffer.readInt(Offset))
14103376a787SDiego Novillo       return sampleprof_error::truncated;
1411aae1ed8eSDiego Novillo     InlineCallStack NewStack;
1412aae1ed8eSDiego Novillo     NewStack.push_back(FProfile);
14131d0bc055SKazu Hirata     llvm::append_range(NewStack, InlineStack);
1414aae1ed8eSDiego Novillo     if (std::error_code EC = readOneFunctionProfile(NewStack, Update, Offset))
14153376a787SDiego Novillo       return EC;
14163376a787SDiego Novillo   }
14173376a787SDiego Novillo 
14183376a787SDiego Novillo   return sampleprof_error::success;
14193376a787SDiego Novillo }
14203376a787SDiego Novillo 
14215f8f34e4SAdrian Prantl /// Read a GCC AutoFDO profile.
14223376a787SDiego Novillo ///
14233376a787SDiego Novillo /// This format is generated by the Linux Perf conversion tool at
14243376a787SDiego Novillo /// https://github.com/google/autofdo.
14258c8ec1f6SWei Mi std::error_code SampleProfileReaderGCC::readImpl() {
14263376a787SDiego Novillo   // Read the string table.
14273376a787SDiego Novillo   if (std::error_code EC = readNameTable())
14283376a787SDiego Novillo     return EC;
14293376a787SDiego Novillo 
14303376a787SDiego Novillo   // Read the source profile.
14313376a787SDiego Novillo   if (std::error_code EC = readFunctionProfiles())
14323376a787SDiego Novillo     return EC;
14333376a787SDiego Novillo 
14343376a787SDiego Novillo   return sampleprof_error::success;
14353376a787SDiego Novillo }
14363376a787SDiego Novillo 
14373376a787SDiego Novillo bool SampleProfileReaderGCC::hasFormat(const MemoryBuffer &Buffer) {
14383376a787SDiego Novillo   StringRef Magic(reinterpret_cast<const char *>(Buffer.getBufferStart()));
14393376a787SDiego Novillo   return Magic == "adcg*704";
14403376a787SDiego Novillo }
14413376a787SDiego Novillo 
14428c8ec1f6SWei Mi void SampleProfileReaderItaniumRemapper::applyRemapping(LLVMContext &Ctx) {
1443ebad6788SWei Mi   // If the reader uses MD5 to represent string, we can't remap it because
144428436358SRichard Smith   // we don't know what the original function names were.
1445ebad6788SWei Mi   if (Reader.useMD5()) {
144628436358SRichard Smith     Ctx.diagnose(DiagnosticInfoSampleProfile(
14478c8ec1f6SWei Mi         Reader.getBuffer()->getBufferIdentifier(),
144828436358SRichard Smith         "Profile data remapping cannot be applied to profile data "
144928436358SRichard Smith         "in compact format (original mangled names are not available).",
145028436358SRichard Smith         DS_Warning));
14518c8ec1f6SWei Mi     return;
145228436358SRichard Smith   }
145328436358SRichard Smith 
14546b989a17SWenlei He   // CSSPGO-TODO: Remapper is not yet supported.
14556b989a17SWenlei He   // We will need to remap the entire context string.
14568c8ec1f6SWei Mi   assert(Remappings && "should be initialized while creating remapper");
1457c67ccf5fSWei Mi   for (auto &Sample : Reader.getProfiles()) {
1458c67ccf5fSWei Mi     DenseSet<StringRef> NamesInSample;
1459c67ccf5fSWei Mi     Sample.second.findAllNames(NamesInSample);
1460c67ccf5fSWei Mi     for (auto &Name : NamesInSample)
1461c67ccf5fSWei Mi       if (auto Key = Remappings->insert(Name))
1462c67ccf5fSWei Mi         NameMap.insert({Key, Name});
1463c67ccf5fSWei Mi   }
146428436358SRichard Smith 
14658c8ec1f6SWei Mi   RemappingApplied = true;
146628436358SRichard Smith }
146728436358SRichard Smith 
1468c67ccf5fSWei Mi Optional<StringRef>
1469c67ccf5fSWei Mi SampleProfileReaderItaniumRemapper::lookUpNameInProfile(StringRef Fname) {
14708c8ec1f6SWei Mi   if (auto Key = Remappings->lookup(Fname))
1471c67ccf5fSWei Mi     return NameMap.lookup(Key);
1472c67ccf5fSWei Mi   return None;
147328436358SRichard Smith }
147428436358SRichard Smith 
14755f8f34e4SAdrian Prantl /// Prepare a memory buffer for the contents of \p Filename.
1476de1ab26fSDiego Novillo ///
1477c572e92cSDiego Novillo /// \returns an error code indicating the status of the buffer.
1478fcd55607SDiego Novillo static ErrorOr<std::unique_ptr<MemoryBuffer>>
14790da23a27SBenjamin Kramer setupMemoryBuffer(const Twine &Filename) {
1480c572e92cSDiego Novillo   auto BufferOrErr = MemoryBuffer::getFileOrSTDIN(Filename);
1481c572e92cSDiego Novillo   if (std::error_code EC = BufferOrErr.getError())
1482c572e92cSDiego Novillo     return EC;
1483fcd55607SDiego Novillo   auto Buffer = std::move(BufferOrErr.get());
1484c572e92cSDiego Novillo 
1485c572e92cSDiego Novillo   // Sanity check the file.
1486260fe3ecSZachary Turner   if (uint64_t(Buffer->getBufferSize()) > std::numeric_limits<uint32_t>::max())
1487c572e92cSDiego Novillo     return sampleprof_error::too_large;
1488c572e92cSDiego Novillo 
1489c55cf4afSBill Wendling   return std::move(Buffer);
1490c572e92cSDiego Novillo }
1491c572e92cSDiego Novillo 
14925f8f34e4SAdrian Prantl /// Create a sample profile reader based on the format of the input file.
1493c572e92cSDiego Novillo ///
1494c572e92cSDiego Novillo /// \param Filename The file to open.
1495c572e92cSDiego Novillo ///
1496c572e92cSDiego Novillo /// \param C The LLVM context to use to emit diagnostics.
1497c572e92cSDiego Novillo ///
14988c8ec1f6SWei Mi /// \param RemapFilename The file used for profile remapping.
14998c8ec1f6SWei Mi ///
1500c572e92cSDiego Novillo /// \returns an error code indicating the status of the created reader.
1501fcd55607SDiego Novillo ErrorOr<std::unique_ptr<SampleProfileReader>>
15028c8ec1f6SWei Mi SampleProfileReader::create(const std::string Filename, LLVMContext &C,
15038c8ec1f6SWei Mi                             const std::string RemapFilename) {
1504fcd55607SDiego Novillo   auto BufferOrError = setupMemoryBuffer(Filename);
1505fcd55607SDiego Novillo   if (std::error_code EC = BufferOrError.getError())
1506c572e92cSDiego Novillo     return EC;
15078c8ec1f6SWei Mi   return create(BufferOrError.get(), C, RemapFilename);
150851abea74SNathan Slingerland }
1509c572e92cSDiego Novillo 
151028436358SRichard Smith /// Create a sample profile remapper from the given input, to remap the
151128436358SRichard Smith /// function names in the given profile data.
151228436358SRichard Smith ///
151328436358SRichard Smith /// \param Filename The file to open.
151428436358SRichard Smith ///
15158c8ec1f6SWei Mi /// \param Reader The profile reader the remapper is going to be applied to.
15168c8ec1f6SWei Mi ///
151728436358SRichard Smith /// \param C The LLVM context to use to emit diagnostics.
151828436358SRichard Smith ///
151928436358SRichard Smith /// \returns an error code indicating the status of the created reader.
15208c8ec1f6SWei Mi ErrorOr<std::unique_ptr<SampleProfileReaderItaniumRemapper>>
15218c8ec1f6SWei Mi SampleProfileReaderItaniumRemapper::create(const std::string Filename,
15228c8ec1f6SWei Mi                                            SampleProfileReader &Reader,
15238c8ec1f6SWei Mi                                            LLVMContext &C) {
152428436358SRichard Smith   auto BufferOrError = setupMemoryBuffer(Filename);
152528436358SRichard Smith   if (std::error_code EC = BufferOrError.getError())
152628436358SRichard Smith     return EC;
15278c8ec1f6SWei Mi   return create(BufferOrError.get(), Reader, C);
15288c8ec1f6SWei Mi }
15298c8ec1f6SWei Mi 
15308c8ec1f6SWei Mi /// Create a sample profile remapper from the given input, to remap the
15318c8ec1f6SWei Mi /// function names in the given profile data.
15328c8ec1f6SWei Mi ///
15338c8ec1f6SWei Mi /// \param B The memory buffer to create the reader from (assumes ownership).
15348c8ec1f6SWei Mi ///
15358c8ec1f6SWei Mi /// \param C The LLVM context to use to emit diagnostics.
15368c8ec1f6SWei Mi ///
15378c8ec1f6SWei Mi /// \param Reader The profile reader the remapper is going to be applied to.
15388c8ec1f6SWei Mi ///
15398c8ec1f6SWei Mi /// \returns an error code indicating the status of the created reader.
15408c8ec1f6SWei Mi ErrorOr<std::unique_ptr<SampleProfileReaderItaniumRemapper>>
15418c8ec1f6SWei Mi SampleProfileReaderItaniumRemapper::create(std::unique_ptr<MemoryBuffer> &B,
15428c8ec1f6SWei Mi                                            SampleProfileReader &Reader,
15438c8ec1f6SWei Mi                                            LLVMContext &C) {
15448c8ec1f6SWei Mi   auto Remappings = std::make_unique<SymbolRemappingReader>();
15458c8ec1f6SWei Mi   if (Error E = Remappings->read(*B.get())) {
15468c8ec1f6SWei Mi     handleAllErrors(
15478c8ec1f6SWei Mi         std::move(E), [&](const SymbolRemappingParseError &ParseError) {
15488c8ec1f6SWei Mi           C.diagnose(DiagnosticInfoSampleProfile(B->getBufferIdentifier(),
15498c8ec1f6SWei Mi                                                  ParseError.getLineNum(),
15508c8ec1f6SWei Mi                                                  ParseError.getMessage()));
15518c8ec1f6SWei Mi         });
15528c8ec1f6SWei Mi     return sampleprof_error::malformed;
15538c8ec1f6SWei Mi   }
15548c8ec1f6SWei Mi 
15550eaee545SJonas Devlieghere   return std::make_unique<SampleProfileReaderItaniumRemapper>(
15568c8ec1f6SWei Mi       std::move(B), std::move(Remappings), Reader);
155728436358SRichard Smith }
155828436358SRichard Smith 
15595f8f34e4SAdrian Prantl /// Create a sample profile reader based on the format of the input data.
156051abea74SNathan Slingerland ///
156151abea74SNathan Slingerland /// \param B The memory buffer to create the reader from (assumes ownership).
156251abea74SNathan Slingerland ///
156351abea74SNathan Slingerland /// \param C The LLVM context to use to emit diagnostics.
156451abea74SNathan Slingerland ///
15658c8ec1f6SWei Mi /// \param RemapFilename The file used for profile remapping.
15668c8ec1f6SWei Mi ///
156751abea74SNathan Slingerland /// \returns an error code indicating the status of the created reader.
156851abea74SNathan Slingerland ErrorOr<std::unique_ptr<SampleProfileReader>>
15698c8ec1f6SWei Mi SampleProfileReader::create(std::unique_ptr<MemoryBuffer> &B, LLVMContext &C,
15708c8ec1f6SWei Mi                             const std::string RemapFilename) {
1571fcd55607SDiego Novillo   std::unique_ptr<SampleProfileReader> Reader;
1572a0c0857eSWei Mi   if (SampleProfileReaderRawBinary::hasFormat(*B))
1573a0c0857eSWei Mi     Reader.reset(new SampleProfileReaderRawBinary(std::move(B), C));
1574be907324SWei Mi   else if (SampleProfileReaderExtBinary::hasFormat(*B))
1575be907324SWei Mi     Reader.reset(new SampleProfileReaderExtBinary(std::move(B), C));
1576a0c0857eSWei Mi   else if (SampleProfileReaderCompactBinary::hasFormat(*B))
1577a0c0857eSWei Mi     Reader.reset(new SampleProfileReaderCompactBinary(std::move(B), C));
157851abea74SNathan Slingerland   else if (SampleProfileReaderGCC::hasFormat(*B))
157951abea74SNathan Slingerland     Reader.reset(new SampleProfileReaderGCC(std::move(B), C));
158051abea74SNathan Slingerland   else if (SampleProfileReaderText::hasFormat(*B))
158151abea74SNathan Slingerland     Reader.reset(new SampleProfileReaderText(std::move(B), C));
15824f823667SNathan Slingerland   else
15834f823667SNathan Slingerland     return sampleprof_error::unrecognized_format;
1584c572e92cSDiego Novillo 
15858c8ec1f6SWei Mi   if (!RemapFilename.empty()) {
15868c8ec1f6SWei Mi     auto ReaderOrErr =
15878c8ec1f6SWei Mi         SampleProfileReaderItaniumRemapper::create(RemapFilename, *Reader, C);
15888c8ec1f6SWei Mi     if (std::error_code EC = ReaderOrErr.getError()) {
15898c8ec1f6SWei Mi       std::string Msg = "Could not create remapper: " + EC.message();
15908c8ec1f6SWei Mi       C.diagnose(DiagnosticInfoSampleProfile(RemapFilename, Msg));
15918c8ec1f6SWei Mi       return EC;
15928c8ec1f6SWei Mi     }
15938c8ec1f6SWei Mi     Reader->Remapper = std::move(ReaderOrErr.get());
15948c8ec1f6SWei Mi   }
15958c8ec1f6SWei Mi 
159694d44c97SWei Mi   FunctionSamples::Format = Reader->getFormat();
1597be907324SWei Mi   if (std::error_code EC = Reader->readHeader()) {
1598fcd55607SDiego Novillo     return EC;
1599be907324SWei Mi   }
1600fcd55607SDiego Novillo 
1601c55cf4afSBill Wendling   return std::move(Reader);
1602de1ab26fSDiego Novillo }
160340ee23dbSEaswaran Raman 
160440ee23dbSEaswaran Raman // For text and GCC file formats, we compute the summary after reading the
160540ee23dbSEaswaran Raman // profile. Binary format has the profile summary in its header.
160640ee23dbSEaswaran Raman void SampleProfileReader::computeSummary() {
1607e5a17e3fSEaswaran Raman   SampleProfileSummaryBuilder Builder(ProfileSummaryBuilder::DefaultCutoffs);
160840ee23dbSEaswaran Raman   for (const auto &I : Profiles) {
160940ee23dbSEaswaran Raman     const FunctionSamples &Profile = I.second;
1610e5a17e3fSEaswaran Raman     Builder.addRecord(Profile);
161140ee23dbSEaswaran Raman   }
161238de59e4SBenjamin Kramer   Summary = Builder.getSummary();
161340ee23dbSEaswaran Raman }
1614