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];
2607e99bddfSHongtao 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 
3237e99bddfSHongtao 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;
3307e99bddfSHongtao 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 
5467e99bddfSHongtao Yu   SampleContext FContext(*FName);
5477e99bddfSHongtao Yu   Profiles[FContext] = FunctionSamples();
5487e99bddfSHongtao Yu   FunctionSamples &FProfile = Profiles[FContext];
5497e99bddfSHongtao Yu   FProfile.setName(FContext.getNameWithoutContext());
5507e99bddfSHongtao Yu   FProfile.setContext(FContext);
551b93483dbSDiego Novillo   FProfile.addHeadSamples(*NumHeadSamples);
552b93483dbSDiego Novillo 
5537e99bddfSHongtao Yu   if (FContext.hasContext())
5547e99bddfSHongtao Yu     CSProfileCount++;
5557e99bddfSHongtao 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");
587*ee35784aSWei Mi     FunctionSamples::HasUniqSuffix =
588*ee35784aSWei Mi         hasSecFlag(Entry, SecNameTableFlags::SecFlagUniqSuffix);
58964e76853SWei Mi     if (std::error_code EC = readNameTableSec(UseMD5))
590be907324SWei Mi       return EC;
591be907324SWei Mi     break;
59264e76853SWei Mi   }
593be907324SWei Mi   case SecLBRProfile:
59409dcfe68SWei Mi     if (std::error_code EC = readFuncProfiles())
595be907324SWei Mi       return EC;
596be907324SWei Mi     break;
59709dcfe68SWei Mi   case SecFuncOffsetTable:
59809dcfe68SWei Mi     if (std::error_code EC = readFuncOffsetTable())
599798e59b8SWei Mi       return EC;
600798e59b8SWei Mi     break;
601ac068e01SHongtao Yu   case SecFuncMetadata:
602ac068e01SHongtao Yu     ProfileIsProbeBased =
603ac068e01SHongtao Yu         hasSecFlag(Entry, SecFuncMetadataFlags::SecFlagIsProbeBased);
604ac068e01SHongtao Yu     FunctionSamples::ProfileIsProbeBased = ProfileIsProbeBased;
605ac068e01SHongtao Yu     if (std::error_code EC = readFuncMetadata())
606ac068e01SHongtao Yu       return EC;
607ac068e01SHongtao Yu     break;
60893953d41SWei Mi   case SecProfileSymbolList:
60993953d41SWei Mi     if (std::error_code EC = readProfileSymbolList())
61093953d41SWei Mi       return EC;
61193953d41SWei Mi     break;
612be907324SWei Mi   default:
61393953d41SWei Mi     if (std::error_code EC = readCustomSection(Entry))
61493953d41SWei Mi       return EC;
615077a9c70SWei Mi     break;
616be907324SWei Mi   }
617077a9c70SWei Mi   return sampleprof_error::success;
618077a9c70SWei Mi }
619077a9c70SWei Mi 
620*ee35784aSWei Mi bool SampleProfileReaderExtBinaryBase::collectFuncsFromModule() {
621*ee35784aSWei Mi   if (!M)
622*ee35784aSWei Mi     return false;
62309dcfe68SWei Mi   FuncsToUse.clear();
624*ee35784aSWei Mi   for (auto &F : *M)
62509dcfe68SWei Mi     FuncsToUse.insert(FunctionSamples::getCanonicalFnName(F));
626*ee35784aSWei Mi   return true;
62709dcfe68SWei Mi }
62809dcfe68SWei Mi 
62993953d41SWei Mi std::error_code SampleProfileReaderExtBinaryBase::readFuncOffsetTable() {
630a906e3ecSWei Mi   // If there are more than one FuncOffsetTable, the profile read associated
631a906e3ecSWei Mi   // with previous FuncOffsetTable has to be done before next FuncOffsetTable
632a906e3ecSWei Mi   // is read.
633a906e3ecSWei Mi   FuncOffsetTable.clear();
634a906e3ecSWei Mi 
63509dcfe68SWei Mi   auto Size = readNumber<uint64_t>();
63609dcfe68SWei Mi   if (std::error_code EC = Size.getError())
63709dcfe68SWei Mi     return EC;
63809dcfe68SWei Mi 
63909dcfe68SWei Mi   FuncOffsetTable.reserve(*Size);
64009dcfe68SWei Mi   for (uint32_t I = 0; I < *Size; ++I) {
64109dcfe68SWei Mi     auto FName(readStringFromTable());
64209dcfe68SWei Mi     if (std::error_code EC = FName.getError())
64309dcfe68SWei Mi       return EC;
64409dcfe68SWei Mi 
64509dcfe68SWei Mi     auto Offset = readNumber<uint64_t>();
64609dcfe68SWei Mi     if (std::error_code EC = Offset.getError())
64709dcfe68SWei Mi       return EC;
64809dcfe68SWei Mi 
64909dcfe68SWei Mi     FuncOffsetTable[*FName] = *Offset;
65009dcfe68SWei Mi   }
65109dcfe68SWei Mi   return sampleprof_error::success;
65209dcfe68SWei Mi }
65309dcfe68SWei Mi 
65493953d41SWei Mi std::error_code SampleProfileReaderExtBinaryBase::readFuncProfiles() {
655*ee35784aSWei Mi   // Collect functions used by current module if the Reader has been
656*ee35784aSWei Mi   // given a module.
657*ee35784aSWei Mi   // collectFuncsFromModule uses FunctionSamples::getCanonicalFnName
658*ee35784aSWei Mi   // which will query FunctionSamples::HasUniqSuffix, so it has to be
659*ee35784aSWei Mi   // called after FunctionSamples::HasUniqSuffix is set, i.e. after
660*ee35784aSWei Mi   // NameTable section is read.
661*ee35784aSWei Mi   bool LoadFuncsToBeUsed = collectFuncsFromModule();
662*ee35784aSWei Mi 
663*ee35784aSWei Mi   // When LoadFuncsToBeUsed is false, load all the function profiles.
66409dcfe68SWei Mi   const uint8_t *Start = Data;
665*ee35784aSWei Mi   if (!LoadFuncsToBeUsed) {
66609dcfe68SWei Mi     while (Data < End) {
66709dcfe68SWei Mi       if (std::error_code EC = readFuncProfile(Data))
66809dcfe68SWei Mi         return EC;
66909dcfe68SWei Mi     }
67009dcfe68SWei Mi     assert(Data == End && "More data is read than expected");
6717e99bddfSHongtao Yu   } else {
672*ee35784aSWei Mi     // Load function profiles on demand.
6738c8ec1f6SWei Mi     if (Remapper) {
67409dcfe68SWei Mi       for (auto Name : FuncsToUse) {
6758c8ec1f6SWei Mi         Remapper->insert(Name);
6768c8ec1f6SWei Mi       }
6778c8ec1f6SWei Mi     }
6788c8ec1f6SWei Mi 
679ebad6788SWei Mi     if (useMD5()) {
680ebad6788SWei Mi       for (auto Name : FuncsToUse) {
681ebad6788SWei Mi         auto GUID = std::to_string(MD5Hash(Name));
682ebad6788SWei Mi         auto iter = FuncOffsetTable.find(StringRef(GUID));
683ebad6788SWei Mi         if (iter == FuncOffsetTable.end())
684ebad6788SWei Mi           continue;
685ebad6788SWei Mi         const uint8_t *FuncProfileAddr = Start + iter->second;
686ebad6788SWei Mi         assert(FuncProfileAddr < End && "out of LBRProfile section");
687ebad6788SWei Mi         if (std::error_code EC = readFuncProfile(FuncProfileAddr))
688ebad6788SWei Mi           return EC;
689ebad6788SWei Mi       }
690ebad6788SWei Mi     } else {
6918c8ec1f6SWei Mi       for (auto NameOffset : FuncOffsetTable) {
6927e99bddfSHongtao Yu         SampleContext FContext(NameOffset.first);
6937e99bddfSHongtao Yu         auto FuncName = FContext.getNameWithoutContext();
6948c8ec1f6SWei Mi         if (!FuncsToUse.count(FuncName) &&
6958c8ec1f6SWei Mi             (!Remapper || !Remapper->exist(FuncName)))
69609dcfe68SWei Mi           continue;
6978c8ec1f6SWei Mi         const uint8_t *FuncProfileAddr = Start + NameOffset.second;
69809dcfe68SWei Mi         assert(FuncProfileAddr < End && "out of LBRProfile section");
69909dcfe68SWei Mi         if (std::error_code EC = readFuncProfile(FuncProfileAddr))
70009dcfe68SWei Mi           return EC;
70109dcfe68SWei Mi       }
702ebad6788SWei Mi     }
70309dcfe68SWei Mi     Data = End;
7047e99bddfSHongtao Yu   }
7057e99bddfSHongtao Yu   assert((CSProfileCount == 0 || CSProfileCount == Profiles.size()) &&
7067e99bddfSHongtao Yu          "Cannot have both context-sensitive and regular profile");
7077e99bddfSHongtao Yu   ProfileIsCS = (CSProfileCount > 0);
7087e99bddfSHongtao Yu   FunctionSamples::ProfileIsCS = ProfileIsCS;
70909dcfe68SWei Mi   return sampleprof_error::success;
71009dcfe68SWei Mi }
71109dcfe68SWei Mi 
71293953d41SWei Mi std::error_code SampleProfileReaderExtBinaryBase::readProfileSymbolList() {
713b523790aSWei Mi   if (!ProfSymList)
714b523790aSWei Mi     ProfSymList = std::make_unique<ProfileSymbolList>();
715b523790aSWei Mi 
71609dcfe68SWei Mi   if (std::error_code EC = ProfSymList->read(Data, End - Data))
717798e59b8SWei Mi     return EC;
718798e59b8SWei Mi 
71909dcfe68SWei Mi   Data = End;
720b523790aSWei Mi   return sampleprof_error::success;
721b523790aSWei Mi }
722b523790aSWei Mi 
723b523790aSWei Mi std::error_code SampleProfileReaderExtBinaryBase::decompressSection(
724b523790aSWei Mi     const uint8_t *SecStart, const uint64_t SecSize,
725b523790aSWei Mi     const uint8_t *&DecompressBuf, uint64_t &DecompressBufSize) {
726b523790aSWei Mi   Data = SecStart;
727b523790aSWei Mi   End = SecStart + SecSize;
728b523790aSWei Mi   auto DecompressSize = readNumber<uint64_t>();
729b523790aSWei Mi   if (std::error_code EC = DecompressSize.getError())
730b523790aSWei Mi     return EC;
731b523790aSWei Mi   DecompressBufSize = *DecompressSize;
732b523790aSWei Mi 
733798e59b8SWei Mi   auto CompressSize = readNumber<uint64_t>();
734798e59b8SWei Mi   if (std::error_code EC = CompressSize.getError())
735798e59b8SWei Mi     return EC;
736798e59b8SWei Mi 
737b523790aSWei Mi   if (!llvm::zlib::isAvailable())
738b523790aSWei Mi     return sampleprof_error::zlib_unavailable;
739798e59b8SWei Mi 
740b523790aSWei Mi   StringRef CompressedStrings(reinterpret_cast<const char *>(Data),
741b523790aSWei Mi                               *CompressSize);
742b523790aSWei Mi   char *Buffer = Allocator.Allocate<char>(DecompressBufSize);
743283df8cfSWei Mi   size_t UCSize = DecompressBufSize;
744b523790aSWei Mi   llvm::Error E =
745283df8cfSWei Mi       zlib::uncompress(CompressedStrings, Buffer, UCSize);
746b523790aSWei Mi   if (E)
747b523790aSWei Mi     return sampleprof_error::uncompress_failed;
748b523790aSWei Mi   DecompressBuf = reinterpret_cast<const uint8_t *>(Buffer);
749798e59b8SWei Mi   return sampleprof_error::success;
750798e59b8SWei Mi }
751798e59b8SWei Mi 
7528c8ec1f6SWei Mi std::error_code SampleProfileReaderExtBinaryBase::readImpl() {
753077a9c70SWei Mi   const uint8_t *BufStart =
754077a9c70SWei Mi       reinterpret_cast<const uint8_t *>(Buffer->getBufferStart());
755077a9c70SWei Mi 
756077a9c70SWei Mi   for (auto &Entry : SecHdrTable) {
757077a9c70SWei Mi     // Skip empty section.
758077a9c70SWei Mi     if (!Entry.Size)
759077a9c70SWei Mi       continue;
760b523790aSWei Mi 
76121b1ad03SWei Mi     // Skip sections without context when SkipFlatProf is true.
76221b1ad03SWei Mi     if (SkipFlatProf && hasSecFlag(Entry, SecCommonFlags::SecFlagFlat))
76321b1ad03SWei Mi       continue;
76421b1ad03SWei Mi 
765077a9c70SWei Mi     const uint8_t *SecStart = BufStart + Entry.Offset;
766b523790aSWei Mi     uint64_t SecSize = Entry.Size;
767b523790aSWei Mi 
768b523790aSWei Mi     // If the section is compressed, decompress it into a buffer
769b523790aSWei Mi     // DecompressBuf before reading the actual data. The pointee of
770b523790aSWei Mi     // 'Data' will be changed to buffer hold by DecompressBuf
771b523790aSWei Mi     // temporarily when reading the actual data.
772ebad6788SWei Mi     bool isCompressed = hasSecFlag(Entry, SecCommonFlags::SecFlagCompress);
773b523790aSWei Mi     if (isCompressed) {
774b523790aSWei Mi       const uint8_t *DecompressBuf;
775b523790aSWei Mi       uint64_t DecompressBufSize;
776b523790aSWei Mi       if (std::error_code EC = decompressSection(
777b523790aSWei Mi               SecStart, SecSize, DecompressBuf, DecompressBufSize))
778077a9c70SWei Mi         return EC;
779b523790aSWei Mi       SecStart = DecompressBuf;
780b523790aSWei Mi       SecSize = DecompressBufSize;
781b523790aSWei Mi     }
782b523790aSWei Mi 
783ebad6788SWei Mi     if (std::error_code EC = readOneSection(SecStart, SecSize, Entry))
784b523790aSWei Mi       return EC;
785b523790aSWei Mi     if (Data != SecStart + SecSize)
786be907324SWei Mi       return sampleprof_error::malformed;
787b523790aSWei Mi 
788b523790aSWei Mi     // Change the pointee of 'Data' from DecompressBuf to original Buffer.
789b523790aSWei Mi     if (isCompressed) {
790b523790aSWei Mi       Data = BufStart + Entry.Offset;
791b523790aSWei Mi       End = BufStart + Buffer->getBufferSize();
792b523790aSWei Mi     }
793be907324SWei Mi   }
794be907324SWei Mi 
795be907324SWei Mi   return sampleprof_error::success;
796be907324SWei Mi }
797be907324SWei Mi 
7988c8ec1f6SWei Mi std::error_code SampleProfileReaderCompactBinary::readImpl() {
799*ee35784aSWei Mi   // Collect functions used by current module if the Reader has been
800*ee35784aSWei Mi   // given a module.
801*ee35784aSWei Mi   bool LoadFuncsToBeUsed = collectFuncsFromModule();
802*ee35784aSWei Mi 
803d3289544SWenlei He   std::vector<uint64_t> OffsetsToUse;
804*ee35784aSWei Mi   if (!LoadFuncsToBeUsed) {
805*ee35784aSWei Mi     // load all the function profiles.
806d3289544SWenlei He     for (auto FuncEntry : FuncOffsetTable) {
807d3289544SWenlei He       OffsetsToUse.push_back(FuncEntry.second);
808d3289544SWenlei He     }
809*ee35784aSWei Mi   } else {
810*ee35784aSWei Mi     // load function profiles on demand.
8116a14325dSWei Mi     for (auto Name : FuncsToUse) {
8126a14325dSWei Mi       auto GUID = std::to_string(MD5Hash(Name));
8136a14325dSWei Mi       auto iter = FuncOffsetTable.find(StringRef(GUID));
8146a14325dSWei Mi       if (iter == FuncOffsetTable.end())
8156a14325dSWei Mi         continue;
816d3289544SWenlei He       OffsetsToUse.push_back(iter->second);
817d3289544SWenlei He     }
818d3289544SWenlei He   }
819d3289544SWenlei He 
820d3289544SWenlei He   for (auto Offset : OffsetsToUse) {
8216a14325dSWei Mi     const uint8_t *SavedData = Data;
82209dcfe68SWei Mi     if (std::error_code EC = readFuncProfile(
82309dcfe68SWei Mi             reinterpret_cast<const uint8_t *>(Buffer->getBufferStart()) +
82409dcfe68SWei Mi             Offset))
8256a14325dSWei Mi       return EC;
8266a14325dSWei Mi     Data = SavedData;
8276a14325dSWei Mi   }
828c572e92cSDiego Novillo   return sampleprof_error::success;
829c572e92cSDiego Novillo }
830c572e92cSDiego Novillo 
831a0c0857eSWei Mi std::error_code SampleProfileReaderRawBinary::verifySPMagic(uint64_t Magic) {
832a0c0857eSWei Mi   if (Magic == SPMagic())
833a0c0857eSWei Mi     return sampleprof_error::success;
834a0c0857eSWei Mi   return sampleprof_error::bad_magic;
835a0c0857eSWei Mi }
836a0c0857eSWei Mi 
837be907324SWei Mi std::error_code SampleProfileReaderExtBinary::verifySPMagic(uint64_t Magic) {
838be907324SWei Mi   if (Magic == SPMagic(SPF_Ext_Binary))
839be907324SWei Mi     return sampleprof_error::success;
840be907324SWei Mi   return sampleprof_error::bad_magic;
841be907324SWei Mi }
842be907324SWei Mi 
843a0c0857eSWei Mi std::error_code
844a0c0857eSWei Mi SampleProfileReaderCompactBinary::verifySPMagic(uint64_t Magic) {
845a0c0857eSWei Mi   if (Magic == SPMagic(SPF_Compact_Binary))
846a0c0857eSWei Mi     return sampleprof_error::success;
847a0c0857eSWei Mi   return sampleprof_error::bad_magic;
848a0c0857eSWei Mi }
849a0c0857eSWei Mi 
850be907324SWei Mi std::error_code SampleProfileReaderBinary::readNameTable() {
851a0c0857eSWei Mi   auto Size = readNumber<uint32_t>();
852a0c0857eSWei Mi   if (std::error_code EC = Size.getError())
853a0c0857eSWei Mi     return EC;
854a906e3ecSWei Mi   NameTable.reserve(*Size + NameTable.size());
855a0c0857eSWei Mi   for (uint32_t I = 0; I < *Size; ++I) {
856a0c0857eSWei Mi     auto Name(readString());
857a0c0857eSWei Mi     if (std::error_code EC = Name.getError())
858a0c0857eSWei Mi       return EC;
859a0c0857eSWei Mi     NameTable.push_back(*Name);
860a0c0857eSWei Mi   }
861a0c0857eSWei Mi 
862a0c0857eSWei Mi   return sampleprof_error::success;
863a0c0857eSWei Mi }
864a0c0857eSWei Mi 
86593953d41SWei Mi std::error_code SampleProfileReaderExtBinaryBase::readMD5NameTable() {
866ebad6788SWei Mi   auto Size = readNumber<uint64_t>();
867ebad6788SWei Mi   if (std::error_code EC = Size.getError())
868ebad6788SWei Mi     return EC;
869ebad6788SWei Mi   MD5StringBuf = std::make_unique<std::vector<std::string>>();
870ebad6788SWei Mi   MD5StringBuf->reserve(*Size);
87164e76853SWei Mi   if (FixedLengthMD5) {
87264e76853SWei Mi     // Preallocate and initialize NameTable so we can check whether a name
87364e76853SWei Mi     // index has been read before by checking whether the element in the
87464e76853SWei Mi     // NameTable is empty, meanwhile readStringIndex can do the boundary
87564e76853SWei Mi     // check using the size of NameTable.
87664e76853SWei Mi     NameTable.resize(*Size + NameTable.size());
87764e76853SWei Mi 
87864e76853SWei Mi     MD5NameMemStart = Data;
87964e76853SWei Mi     Data = Data + (*Size) * sizeof(uint64_t);
88064e76853SWei Mi     return sampleprof_error::success;
88164e76853SWei Mi   }
88264e76853SWei Mi   NameTable.reserve(*Size);
883ebad6788SWei Mi   for (uint32_t I = 0; I < *Size; ++I) {
884ebad6788SWei Mi     auto FID = readNumber<uint64_t>();
885ebad6788SWei Mi     if (std::error_code EC = FID.getError())
886ebad6788SWei Mi       return EC;
887ebad6788SWei Mi     MD5StringBuf->push_back(std::to_string(*FID));
888ebad6788SWei Mi     // NameTable is a vector of StringRef. Here it is pushing back a
889ebad6788SWei Mi     // StringRef initialized with the last string in MD5stringBuf.
890ebad6788SWei Mi     NameTable.push_back(MD5StringBuf->back());
891ebad6788SWei Mi   }
892ebad6788SWei Mi   return sampleprof_error::success;
893ebad6788SWei Mi }
894ebad6788SWei Mi 
89593953d41SWei Mi std::error_code SampleProfileReaderExtBinaryBase::readNameTableSec(bool IsMD5) {
896ebad6788SWei Mi   if (IsMD5)
897ebad6788SWei Mi     return readMD5NameTable();
898ebad6788SWei Mi   return SampleProfileReaderBinary::readNameTable();
899ebad6788SWei Mi }
900ebad6788SWei Mi 
901ac068e01SHongtao Yu std::error_code SampleProfileReaderExtBinaryBase::readFuncMetadata() {
902ac068e01SHongtao Yu   if (!ProfileIsProbeBased)
903ac068e01SHongtao Yu     return sampleprof_error::success;
904224fee82SHongtao Yu   while (Data < End) {
905ac068e01SHongtao Yu     auto FName(readStringFromTable());
906ac068e01SHongtao Yu     if (std::error_code EC = FName.getError())
907ac068e01SHongtao Yu       return EC;
908ac068e01SHongtao Yu 
909ac068e01SHongtao Yu     auto Checksum = readNumber<uint64_t>();
910ac068e01SHongtao Yu     if (std::error_code EC = Checksum.getError())
911ac068e01SHongtao Yu       return EC;
912ac068e01SHongtao Yu 
9137e99bddfSHongtao Yu     SampleContext FContext(*FName);
914224fee82SHongtao Yu     // No need to load metadata for profiles that are not loaded in the current
915224fee82SHongtao Yu     // module.
916224fee82SHongtao Yu     if (Profiles.count(FContext))
9177e99bddfSHongtao Yu       Profiles[FContext].setFunctionHash(*Checksum);
918ac068e01SHongtao Yu   }
919224fee82SHongtao Yu 
920224fee82SHongtao Yu   assert(Data == End && "More data is read than expected");
921ac068e01SHongtao Yu   return sampleprof_error::success;
922ac068e01SHongtao Yu }
923ac068e01SHongtao Yu 
924a0c0857eSWei Mi std::error_code SampleProfileReaderCompactBinary::readNameTable() {
925a0c0857eSWei Mi   auto Size = readNumber<uint64_t>();
926a0c0857eSWei Mi   if (std::error_code EC = Size.getError())
927a0c0857eSWei Mi     return EC;
928a0c0857eSWei Mi   NameTable.reserve(*Size);
929a0c0857eSWei Mi   for (uint32_t I = 0; I < *Size; ++I) {
930a0c0857eSWei Mi     auto FID = readNumber<uint64_t>();
931a0c0857eSWei Mi     if (std::error_code EC = FID.getError())
932a0c0857eSWei Mi       return EC;
933a0c0857eSWei Mi     NameTable.push_back(std::to_string(*FID));
934a0c0857eSWei Mi   }
935a0c0857eSWei Mi   return sampleprof_error::success;
936a0c0857eSWei Mi }
937a0c0857eSWei Mi 
938a906e3ecSWei Mi std::error_code
939a906e3ecSWei Mi SampleProfileReaderExtBinaryBase::readSecHdrTableEntry(uint32_t Idx) {
940be907324SWei Mi   SecHdrTableEntry Entry;
941be907324SWei Mi   auto Type = readUnencodedNumber<uint64_t>();
942be907324SWei Mi   if (std::error_code EC = Type.getError())
943be907324SWei Mi     return EC;
944be907324SWei Mi   Entry.Type = static_cast<SecType>(*Type);
945c572e92cSDiego Novillo 
946b523790aSWei Mi   auto Flags = readUnencodedNumber<uint64_t>();
947b523790aSWei Mi   if (std::error_code EC = Flags.getError())
948be907324SWei Mi     return EC;
949b523790aSWei Mi   Entry.Flags = *Flags;
950be907324SWei Mi 
951be907324SWei Mi   auto Offset = readUnencodedNumber<uint64_t>();
952be907324SWei Mi   if (std::error_code EC = Offset.getError())
953be907324SWei Mi     return EC;
954be907324SWei Mi   Entry.Offset = *Offset;
955be907324SWei Mi 
956be907324SWei Mi   auto Size = readUnencodedNumber<uint64_t>();
957be907324SWei Mi   if (std::error_code EC = Size.getError())
958be907324SWei Mi     return EC;
959be907324SWei Mi   Entry.Size = *Size;
960be907324SWei Mi 
961a906e3ecSWei Mi   Entry.LayoutIndex = Idx;
962be907324SWei Mi   SecHdrTable.push_back(std::move(Entry));
963be907324SWei Mi   return sampleprof_error::success;
964be907324SWei Mi }
965be907324SWei Mi 
966be907324SWei Mi std::error_code SampleProfileReaderExtBinaryBase::readSecHdrTable() {
967be907324SWei Mi   auto EntryNum = readUnencodedNumber<uint64_t>();
968be907324SWei Mi   if (std::error_code EC = EntryNum.getError())
969be907324SWei Mi     return EC;
970be907324SWei Mi 
971be907324SWei Mi   for (uint32_t i = 0; i < (*EntryNum); i++)
972a906e3ecSWei Mi     if (std::error_code EC = readSecHdrTableEntry(i))
973be907324SWei Mi       return EC;
974be907324SWei Mi 
975be907324SWei Mi   return sampleprof_error::success;
976be907324SWei Mi }
977be907324SWei Mi 
978be907324SWei Mi std::error_code SampleProfileReaderExtBinaryBase::readHeader() {
979be907324SWei Mi   const uint8_t *BufStart =
980be907324SWei Mi       reinterpret_cast<const uint8_t *>(Buffer->getBufferStart());
981be907324SWei Mi   Data = BufStart;
982be907324SWei Mi   End = BufStart + Buffer->getBufferSize();
983be907324SWei Mi 
984be907324SWei Mi   if (std::error_code EC = readMagicIdent())
985be907324SWei Mi     return EC;
986be907324SWei Mi 
987be907324SWei Mi   if (std::error_code EC = readSecHdrTable())
988be907324SWei Mi     return EC;
989be907324SWei Mi 
990be907324SWei Mi   return sampleprof_error::success;
991be907324SWei Mi }
992be907324SWei Mi 
993eee532cdSWei Mi uint64_t SampleProfileReaderExtBinaryBase::getSectionSize(SecType Type) {
994a906e3ecSWei Mi   uint64_t Size = 0;
995eee532cdSWei Mi   for (auto &Entry : SecHdrTable) {
996eee532cdSWei Mi     if (Entry.Type == Type)
997a906e3ecSWei Mi       Size += Entry.Size;
998eee532cdSWei Mi   }
999a906e3ecSWei Mi   return Size;
1000eee532cdSWei Mi }
1001eee532cdSWei Mi 
1002eee532cdSWei Mi uint64_t SampleProfileReaderExtBinaryBase::getFileSize() {
100309dcfe68SWei Mi   // Sections in SecHdrTable is not necessarily in the same order as
100409dcfe68SWei Mi   // sections in the profile because section like FuncOffsetTable needs
100509dcfe68SWei Mi   // to be written after section LBRProfile but needs to be read before
100609dcfe68SWei Mi   // section LBRProfile, so we cannot simply use the last entry in
100709dcfe68SWei Mi   // SecHdrTable to calculate the file size.
100809dcfe68SWei Mi   uint64_t FileSize = 0;
100909dcfe68SWei Mi   for (auto &Entry : SecHdrTable) {
101009dcfe68SWei Mi     FileSize = std::max(Entry.Offset + Entry.Size, FileSize);
101109dcfe68SWei Mi   }
101209dcfe68SWei Mi   return FileSize;
1013eee532cdSWei Mi }
1014eee532cdSWei Mi 
1015b49eac71SWei Mi static std::string getSecFlagsStr(const SecHdrTableEntry &Entry) {
1016b49eac71SWei Mi   std::string Flags;
1017b49eac71SWei Mi   if (hasSecFlag(Entry, SecCommonFlags::SecFlagCompress))
1018b49eac71SWei Mi     Flags.append("{compressed,");
1019b49eac71SWei Mi   else
1020b49eac71SWei Mi     Flags.append("{");
1021b49eac71SWei Mi 
102221b1ad03SWei Mi   if (hasSecFlag(Entry, SecCommonFlags::SecFlagFlat))
102321b1ad03SWei Mi     Flags.append("flat,");
102421b1ad03SWei Mi 
1025b49eac71SWei Mi   switch (Entry.Type) {
1026b49eac71SWei Mi   case SecNameTable:
102764e76853SWei Mi     if (hasSecFlag(Entry, SecNameTableFlags::SecFlagFixedLengthMD5))
102864e76853SWei Mi       Flags.append("fixlenmd5,");
102964e76853SWei Mi     else if (hasSecFlag(Entry, SecNameTableFlags::SecFlagMD5Name))
1030b49eac71SWei Mi       Flags.append("md5,");
1031*ee35784aSWei Mi     if (hasSecFlag(Entry, SecNameTableFlags::SecFlagUniqSuffix))
1032*ee35784aSWei Mi       Flags.append("uniq,");
1033b49eac71SWei Mi     break;
1034b49eac71SWei Mi   case SecProfSummary:
1035b49eac71SWei Mi     if (hasSecFlag(Entry, SecProfSummaryFlags::SecFlagPartial))
1036b49eac71SWei Mi       Flags.append("partial,");
1037b49eac71SWei Mi     break;
1038b49eac71SWei Mi   default:
1039b49eac71SWei Mi     break;
1040b49eac71SWei Mi   }
1041b49eac71SWei Mi   char &last = Flags.back();
1042b49eac71SWei Mi   if (last == ',')
1043b49eac71SWei Mi     last = '}';
1044b49eac71SWei Mi   else
1045b49eac71SWei Mi     Flags.append("}");
1046b49eac71SWei Mi   return Flags;
1047b49eac71SWei Mi }
1048b49eac71SWei Mi 
1049eee532cdSWei Mi bool SampleProfileReaderExtBinaryBase::dumpSectionInfo(raw_ostream &OS) {
1050eee532cdSWei Mi   uint64_t TotalSecsSize = 0;
1051eee532cdSWei Mi   for (auto &Entry : SecHdrTable) {
1052eee532cdSWei Mi     OS << getSecName(Entry.Type) << " - Offset: " << Entry.Offset
1053b49eac71SWei Mi        << ", Size: " << Entry.Size << ", Flags: " << getSecFlagsStr(Entry)
1054b49eac71SWei Mi        << "\n";
1055b49eac71SWei Mi     ;
1056a906e3ecSWei Mi     TotalSecsSize += Entry.Size;
1057eee532cdSWei Mi   }
1058eee532cdSWei Mi   uint64_t HeaderSize = SecHdrTable.front().Offset;
1059eee532cdSWei Mi   assert(HeaderSize + TotalSecsSize == getFileSize() &&
1060eee532cdSWei Mi          "Size of 'header + sections' doesn't match the total size of profile");
1061eee532cdSWei Mi 
1062eee532cdSWei Mi   OS << "Header Size: " << HeaderSize << "\n";
1063eee532cdSWei Mi   OS << "Total Sections Size: " << TotalSecsSize << "\n";
1064eee532cdSWei Mi   OS << "File Size: " << getFileSize() << "\n";
1065eee532cdSWei Mi   return true;
1066eee532cdSWei Mi }
1067eee532cdSWei Mi 
1068be907324SWei Mi std::error_code SampleProfileReaderBinary::readMagicIdent() {
1069c572e92cSDiego Novillo   // Read and check the magic identifier.
1070c572e92cSDiego Novillo   auto Magic = readNumber<uint64_t>();
1071c572e92cSDiego Novillo   if (std::error_code EC = Magic.getError())
1072c572e92cSDiego Novillo     return EC;
1073a0c0857eSWei Mi   else if (std::error_code EC = verifySPMagic(*Magic))
1074c6b96c8dSWei Mi     return EC;
1075c572e92cSDiego Novillo 
1076c572e92cSDiego Novillo   // Read the version number.
1077c572e92cSDiego Novillo   auto Version = readNumber<uint64_t>();
1078c572e92cSDiego Novillo   if (std::error_code EC = Version.getError())
1079c572e92cSDiego Novillo     return EC;
1080c572e92cSDiego Novillo   else if (*Version != SPVersion())
1081c572e92cSDiego Novillo     return sampleprof_error::unsupported_version;
1082c572e92cSDiego Novillo 
1083be907324SWei Mi   return sampleprof_error::success;
1084be907324SWei Mi }
1085be907324SWei Mi 
1086be907324SWei Mi std::error_code SampleProfileReaderBinary::readHeader() {
1087be907324SWei Mi   Data = reinterpret_cast<const uint8_t *>(Buffer->getBufferStart());
1088be907324SWei Mi   End = Data + Buffer->getBufferSize();
1089be907324SWei Mi 
1090be907324SWei Mi   if (std::error_code EC = readMagicIdent())
1091be907324SWei Mi     return EC;
1092be907324SWei Mi 
109340ee23dbSEaswaran Raman   if (std::error_code EC = readSummary())
109440ee23dbSEaswaran Raman     return EC;
109540ee23dbSEaswaran Raman 
1096a0c0857eSWei Mi   if (std::error_code EC = readNameTable())
1097760c5a8fSDiego Novillo     return EC;
1098c572e92cSDiego Novillo   return sampleprof_error::success;
1099c572e92cSDiego Novillo }
1100c572e92cSDiego Novillo 
11016a14325dSWei Mi std::error_code SampleProfileReaderCompactBinary::readHeader() {
11026a14325dSWei Mi   SampleProfileReaderBinary::readHeader();
11036a14325dSWei Mi   if (std::error_code EC = readFuncOffsetTable())
11046a14325dSWei Mi     return EC;
11056a14325dSWei Mi   return sampleprof_error::success;
11066a14325dSWei Mi }
11076a14325dSWei Mi 
11086a14325dSWei Mi std::error_code SampleProfileReaderCompactBinary::readFuncOffsetTable() {
11096a14325dSWei Mi   auto TableOffset = readUnencodedNumber<uint64_t>();
11106a14325dSWei Mi   if (std::error_code EC = TableOffset.getError())
11116a14325dSWei Mi     return EC;
11126a14325dSWei Mi 
11136a14325dSWei Mi   const uint8_t *SavedData = Data;
11146a14325dSWei Mi   const uint8_t *TableStart =
11156a14325dSWei Mi       reinterpret_cast<const uint8_t *>(Buffer->getBufferStart()) +
11166a14325dSWei Mi       *TableOffset;
11176a14325dSWei Mi   Data = TableStart;
11186a14325dSWei Mi 
11196a14325dSWei Mi   auto Size = readNumber<uint64_t>();
11206a14325dSWei Mi   if (std::error_code EC = Size.getError())
11216a14325dSWei Mi     return EC;
11226a14325dSWei Mi 
11236a14325dSWei Mi   FuncOffsetTable.reserve(*Size);
11246a14325dSWei Mi   for (uint32_t I = 0; I < *Size; ++I) {
11256a14325dSWei Mi     auto FName(readStringFromTable());
11266a14325dSWei Mi     if (std::error_code EC = FName.getError())
11276a14325dSWei Mi       return EC;
11286a14325dSWei Mi 
11296a14325dSWei Mi     auto Offset = readNumber<uint64_t>();
11306a14325dSWei Mi     if (std::error_code EC = Offset.getError())
11316a14325dSWei Mi       return EC;
11326a14325dSWei Mi 
11336a14325dSWei Mi     FuncOffsetTable[*FName] = *Offset;
11346a14325dSWei Mi   }
11356a14325dSWei Mi   End = TableStart;
11366a14325dSWei Mi   Data = SavedData;
11376a14325dSWei Mi   return sampleprof_error::success;
11386a14325dSWei Mi }
11396a14325dSWei Mi 
1140*ee35784aSWei Mi bool SampleProfileReaderCompactBinary::collectFuncsFromModule() {
1141*ee35784aSWei Mi   if (!M)
1142*ee35784aSWei Mi     return false;
11436a14325dSWei Mi   FuncsToUse.clear();
1144*ee35784aSWei Mi   for (auto &F : *M)
114509dcfe68SWei Mi     FuncsToUse.insert(FunctionSamples::getCanonicalFnName(F));
1146*ee35784aSWei Mi   return true;
11476a14325dSWei Mi }
11486a14325dSWei Mi 
114940ee23dbSEaswaran Raman std::error_code SampleProfileReaderBinary::readSummaryEntry(
115040ee23dbSEaswaran Raman     std::vector<ProfileSummaryEntry> &Entries) {
115140ee23dbSEaswaran Raman   auto Cutoff = readNumber<uint64_t>();
115240ee23dbSEaswaran Raman   if (std::error_code EC = Cutoff.getError())
115340ee23dbSEaswaran Raman     return EC;
115440ee23dbSEaswaran Raman 
115540ee23dbSEaswaran Raman   auto MinBlockCount = readNumber<uint64_t>();
115640ee23dbSEaswaran Raman   if (std::error_code EC = MinBlockCount.getError())
115740ee23dbSEaswaran Raman     return EC;
115840ee23dbSEaswaran Raman 
115940ee23dbSEaswaran Raman   auto NumBlocks = readNumber<uint64_t>();
116040ee23dbSEaswaran Raman   if (std::error_code EC = NumBlocks.getError())
116140ee23dbSEaswaran Raman     return EC;
116240ee23dbSEaswaran Raman 
116340ee23dbSEaswaran Raman   Entries.emplace_back(*Cutoff, *MinBlockCount, *NumBlocks);
116440ee23dbSEaswaran Raman   return sampleprof_error::success;
116540ee23dbSEaswaran Raman }
116640ee23dbSEaswaran Raman 
116740ee23dbSEaswaran Raman std::error_code SampleProfileReaderBinary::readSummary() {
116840ee23dbSEaswaran Raman   auto TotalCount = readNumber<uint64_t>();
116940ee23dbSEaswaran Raman   if (std::error_code EC = TotalCount.getError())
117040ee23dbSEaswaran Raman     return EC;
117140ee23dbSEaswaran Raman 
117240ee23dbSEaswaran Raman   auto MaxBlockCount = readNumber<uint64_t>();
117340ee23dbSEaswaran Raman   if (std::error_code EC = MaxBlockCount.getError())
117440ee23dbSEaswaran Raman     return EC;
117540ee23dbSEaswaran Raman 
117640ee23dbSEaswaran Raman   auto MaxFunctionCount = readNumber<uint64_t>();
117740ee23dbSEaswaran Raman   if (std::error_code EC = MaxFunctionCount.getError())
117840ee23dbSEaswaran Raman     return EC;
117940ee23dbSEaswaran Raman 
118040ee23dbSEaswaran Raman   auto NumBlocks = readNumber<uint64_t>();
118140ee23dbSEaswaran Raman   if (std::error_code EC = NumBlocks.getError())
118240ee23dbSEaswaran Raman     return EC;
118340ee23dbSEaswaran Raman 
118440ee23dbSEaswaran Raman   auto NumFunctions = readNumber<uint64_t>();
118540ee23dbSEaswaran Raman   if (std::error_code EC = NumFunctions.getError())
118640ee23dbSEaswaran Raman     return EC;
118740ee23dbSEaswaran Raman 
118840ee23dbSEaswaran Raman   auto NumSummaryEntries = readNumber<uint64_t>();
118940ee23dbSEaswaran Raman   if (std::error_code EC = NumSummaryEntries.getError())
119040ee23dbSEaswaran Raman     return EC;
119140ee23dbSEaswaran Raman 
119240ee23dbSEaswaran Raman   std::vector<ProfileSummaryEntry> Entries;
119340ee23dbSEaswaran Raman   for (unsigned i = 0; i < *NumSummaryEntries; i++) {
119440ee23dbSEaswaran Raman     std::error_code EC = readSummaryEntry(Entries);
119540ee23dbSEaswaran Raman     if (EC != sampleprof_error::success)
119640ee23dbSEaswaran Raman       return EC;
119740ee23dbSEaswaran Raman   }
11980eaee545SJonas Devlieghere   Summary = std::make_unique<ProfileSummary>(
11997cefdb81SEaswaran Raman       ProfileSummary::PSK_Sample, Entries, *TotalCount, *MaxBlockCount, 0,
12007cefdb81SEaswaran Raman       *MaxFunctionCount, *NumBlocks, *NumFunctions);
120140ee23dbSEaswaran Raman 
120240ee23dbSEaswaran Raman   return sampleprof_error::success;
120340ee23dbSEaswaran Raman }
120440ee23dbSEaswaran Raman 
1205a0c0857eSWei Mi bool SampleProfileReaderRawBinary::hasFormat(const MemoryBuffer &Buffer) {
1206c572e92cSDiego Novillo   const uint8_t *Data =
1207c572e92cSDiego Novillo       reinterpret_cast<const uint8_t *>(Buffer.getBufferStart());
1208c572e92cSDiego Novillo   uint64_t Magic = decodeULEB128(Data);
1209c572e92cSDiego Novillo   return Magic == SPMagic();
1210c572e92cSDiego Novillo }
1211c572e92cSDiego Novillo 
1212be907324SWei Mi bool SampleProfileReaderExtBinary::hasFormat(const MemoryBuffer &Buffer) {
1213be907324SWei Mi   const uint8_t *Data =
1214be907324SWei Mi       reinterpret_cast<const uint8_t *>(Buffer.getBufferStart());
1215be907324SWei Mi   uint64_t Magic = decodeULEB128(Data);
1216be907324SWei Mi   return Magic == SPMagic(SPF_Ext_Binary);
1217be907324SWei Mi }
1218be907324SWei Mi 
1219a0c0857eSWei Mi bool SampleProfileReaderCompactBinary::hasFormat(const MemoryBuffer &Buffer) {
1220a0c0857eSWei Mi   const uint8_t *Data =
1221a0c0857eSWei Mi       reinterpret_cast<const uint8_t *>(Buffer.getBufferStart());
1222a0c0857eSWei Mi   uint64_t Magic = decodeULEB128(Data);
1223a0c0857eSWei Mi   return Magic == SPMagic(SPF_Compact_Binary);
1224a0c0857eSWei Mi }
1225a0c0857eSWei Mi 
12263376a787SDiego Novillo std::error_code SampleProfileReaderGCC::skipNextWord() {
12273376a787SDiego Novillo   uint32_t dummy;
12283376a787SDiego Novillo   if (!GcovBuffer.readInt(dummy))
12293376a787SDiego Novillo     return sampleprof_error::truncated;
12303376a787SDiego Novillo   return sampleprof_error::success;
12313376a787SDiego Novillo }
12323376a787SDiego Novillo 
12333376a787SDiego Novillo template <typename T> ErrorOr<T> SampleProfileReaderGCC::readNumber() {
12343376a787SDiego Novillo   if (sizeof(T) <= sizeof(uint32_t)) {
12353376a787SDiego Novillo     uint32_t Val;
12363376a787SDiego Novillo     if (GcovBuffer.readInt(Val) && Val <= std::numeric_limits<T>::max())
12373376a787SDiego Novillo       return static_cast<T>(Val);
12383376a787SDiego Novillo   } else if (sizeof(T) <= sizeof(uint64_t)) {
12393376a787SDiego Novillo     uint64_t Val;
12403376a787SDiego Novillo     if (GcovBuffer.readInt64(Val) && Val <= std::numeric_limits<T>::max())
12413376a787SDiego Novillo       return static_cast<T>(Val);
12423376a787SDiego Novillo   }
12433376a787SDiego Novillo 
12443376a787SDiego Novillo   std::error_code EC = sampleprof_error::malformed;
12453376a787SDiego Novillo   reportError(0, EC.message());
12463376a787SDiego Novillo   return EC;
12473376a787SDiego Novillo }
12483376a787SDiego Novillo 
12493376a787SDiego Novillo ErrorOr<StringRef> SampleProfileReaderGCC::readString() {
12503376a787SDiego Novillo   StringRef Str;
12513376a787SDiego Novillo   if (!GcovBuffer.readString(Str))
12523376a787SDiego Novillo     return sampleprof_error::truncated;
12533376a787SDiego Novillo   return Str;
12543376a787SDiego Novillo }
12553376a787SDiego Novillo 
12563376a787SDiego Novillo std::error_code SampleProfileReaderGCC::readHeader() {
12573376a787SDiego Novillo   // Read the magic identifier.
12583376a787SDiego Novillo   if (!GcovBuffer.readGCDAFormat())
12593376a787SDiego Novillo     return sampleprof_error::unrecognized_format;
12603376a787SDiego Novillo 
12613376a787SDiego Novillo   // Read the version number. Note - the GCC reader does not validate this
12623376a787SDiego Novillo   // version, but the profile creator generates v704.
12633376a787SDiego Novillo   GCOV::GCOVVersion version;
12643376a787SDiego Novillo   if (!GcovBuffer.readGCOVVersion(version))
12653376a787SDiego Novillo     return sampleprof_error::unrecognized_format;
12663376a787SDiego Novillo 
12672d00eb17SFangrui Song   if (version != GCOV::V407)
12683376a787SDiego Novillo     return sampleprof_error::unsupported_version;
12693376a787SDiego Novillo 
12703376a787SDiego Novillo   // Skip the empty integer.
12713376a787SDiego Novillo   if (std::error_code EC = skipNextWord())
12723376a787SDiego Novillo     return EC;
12733376a787SDiego Novillo 
12743376a787SDiego Novillo   return sampleprof_error::success;
12753376a787SDiego Novillo }
12763376a787SDiego Novillo 
12773376a787SDiego Novillo std::error_code SampleProfileReaderGCC::readSectionTag(uint32_t Expected) {
12783376a787SDiego Novillo   uint32_t Tag;
12793376a787SDiego Novillo   if (!GcovBuffer.readInt(Tag))
12803376a787SDiego Novillo     return sampleprof_error::truncated;
12813376a787SDiego Novillo 
12823376a787SDiego Novillo   if (Tag != Expected)
12833376a787SDiego Novillo     return sampleprof_error::malformed;
12843376a787SDiego Novillo 
12853376a787SDiego Novillo   if (std::error_code EC = skipNextWord())
12863376a787SDiego Novillo     return EC;
12873376a787SDiego Novillo 
12883376a787SDiego Novillo   return sampleprof_error::success;
12893376a787SDiego Novillo }
12903376a787SDiego Novillo 
12913376a787SDiego Novillo std::error_code SampleProfileReaderGCC::readNameTable() {
12923376a787SDiego Novillo   if (std::error_code EC = readSectionTag(GCOVTagAFDOFileNames))
12933376a787SDiego Novillo     return EC;
12943376a787SDiego Novillo 
12953376a787SDiego Novillo   uint32_t Size;
12963376a787SDiego Novillo   if (!GcovBuffer.readInt(Size))
12973376a787SDiego Novillo     return sampleprof_error::truncated;
12983376a787SDiego Novillo 
12993376a787SDiego Novillo   for (uint32_t I = 0; I < Size; ++I) {
13003376a787SDiego Novillo     StringRef Str;
13013376a787SDiego Novillo     if (!GcovBuffer.readString(Str))
13023376a787SDiego Novillo       return sampleprof_error::truncated;
1303adcd0268SBenjamin Kramer     Names.push_back(std::string(Str));
13043376a787SDiego Novillo   }
13053376a787SDiego Novillo 
13063376a787SDiego Novillo   return sampleprof_error::success;
13073376a787SDiego Novillo }
13083376a787SDiego Novillo 
13093376a787SDiego Novillo std::error_code SampleProfileReaderGCC::readFunctionProfiles() {
13103376a787SDiego Novillo   if (std::error_code EC = readSectionTag(GCOVTagAFDOFunction))
13113376a787SDiego Novillo     return EC;
13123376a787SDiego Novillo 
13133376a787SDiego Novillo   uint32_t NumFunctions;
13143376a787SDiego Novillo   if (!GcovBuffer.readInt(NumFunctions))
13153376a787SDiego Novillo     return sampleprof_error::truncated;
13163376a787SDiego Novillo 
1317aae1ed8eSDiego Novillo   InlineCallStack Stack;
13183376a787SDiego Novillo   for (uint32_t I = 0; I < NumFunctions; ++I)
1319aae1ed8eSDiego Novillo     if (std::error_code EC = readOneFunctionProfile(Stack, true, 0))
13203376a787SDiego Novillo       return EC;
13213376a787SDiego Novillo 
132240ee23dbSEaswaran Raman   computeSummary();
13233376a787SDiego Novillo   return sampleprof_error::success;
13243376a787SDiego Novillo }
13253376a787SDiego Novillo 
1326aae1ed8eSDiego Novillo std::error_code SampleProfileReaderGCC::readOneFunctionProfile(
1327aae1ed8eSDiego Novillo     const InlineCallStack &InlineStack, bool Update, uint32_t Offset) {
13283376a787SDiego Novillo   uint64_t HeadCount = 0;
1329aae1ed8eSDiego Novillo   if (InlineStack.size() == 0)
13303376a787SDiego Novillo     if (!GcovBuffer.readInt64(HeadCount))
13313376a787SDiego Novillo       return sampleprof_error::truncated;
13323376a787SDiego Novillo 
13333376a787SDiego Novillo   uint32_t NameIdx;
13343376a787SDiego Novillo   if (!GcovBuffer.readInt(NameIdx))
13353376a787SDiego Novillo     return sampleprof_error::truncated;
13363376a787SDiego Novillo 
13373376a787SDiego Novillo   StringRef Name(Names[NameIdx]);
13383376a787SDiego Novillo 
13393376a787SDiego Novillo   uint32_t NumPosCounts;
13403376a787SDiego Novillo   if (!GcovBuffer.readInt(NumPosCounts))
13413376a787SDiego Novillo     return sampleprof_error::truncated;
13423376a787SDiego Novillo 
1343aae1ed8eSDiego Novillo   uint32_t NumCallsites;
1344aae1ed8eSDiego Novillo   if (!GcovBuffer.readInt(NumCallsites))
13453376a787SDiego Novillo     return sampleprof_error::truncated;
13463376a787SDiego Novillo 
1347aae1ed8eSDiego Novillo   FunctionSamples *FProfile = nullptr;
1348aae1ed8eSDiego Novillo   if (InlineStack.size() == 0) {
1349aae1ed8eSDiego Novillo     // If this is a top function that we have already processed, do not
1350aae1ed8eSDiego Novillo     // update its profile again.  This happens in the presence of
1351aae1ed8eSDiego Novillo     // function aliases.  Since these aliases share the same function
1352aae1ed8eSDiego Novillo     // body, there will be identical replicated profiles for the
1353aae1ed8eSDiego Novillo     // original function.  In this case, we simply not bother updating
1354aae1ed8eSDiego Novillo     // the profile of the original function.
1355aae1ed8eSDiego Novillo     FProfile = &Profiles[Name];
1356aae1ed8eSDiego Novillo     FProfile->addHeadSamples(HeadCount);
1357aae1ed8eSDiego Novillo     if (FProfile->getTotalSamples() > 0)
13583376a787SDiego Novillo       Update = false;
1359aae1ed8eSDiego Novillo   } else {
1360aae1ed8eSDiego Novillo     // Otherwise, we are reading an inlined instance. The top of the
1361aae1ed8eSDiego Novillo     // inline stack contains the profile of the caller. Insert this
1362aae1ed8eSDiego Novillo     // callee in the caller's CallsiteMap.
1363aae1ed8eSDiego Novillo     FunctionSamples *CallerProfile = InlineStack.front();
1364aae1ed8eSDiego Novillo     uint32_t LineOffset = Offset >> 16;
1365aae1ed8eSDiego Novillo     uint32_t Discriminator = Offset & 0xffff;
1366aae1ed8eSDiego Novillo     FProfile = &CallerProfile->functionSamplesAt(
1367adcd0268SBenjamin Kramer         LineLocation(LineOffset, Discriminator))[std::string(Name)];
13683376a787SDiego Novillo   }
136957d1dda5SDehao Chen   FProfile->setName(Name);
13703376a787SDiego Novillo 
13713376a787SDiego Novillo   for (uint32_t I = 0; I < NumPosCounts; ++I) {
13723376a787SDiego Novillo     uint32_t Offset;
13733376a787SDiego Novillo     if (!GcovBuffer.readInt(Offset))
13743376a787SDiego Novillo       return sampleprof_error::truncated;
13753376a787SDiego Novillo 
13763376a787SDiego Novillo     uint32_t NumTargets;
13773376a787SDiego Novillo     if (!GcovBuffer.readInt(NumTargets))
13783376a787SDiego Novillo       return sampleprof_error::truncated;
13793376a787SDiego Novillo 
13803376a787SDiego Novillo     uint64_t Count;
13813376a787SDiego Novillo     if (!GcovBuffer.readInt64(Count))
13823376a787SDiego Novillo       return sampleprof_error::truncated;
13833376a787SDiego Novillo 
1384aae1ed8eSDiego Novillo     // The line location is encoded in the offset as:
1385aae1ed8eSDiego Novillo     //   high 16 bits: line offset to the start of the function.
1386aae1ed8eSDiego Novillo     //   low 16 bits: discriminator.
1387aae1ed8eSDiego Novillo     uint32_t LineOffset = Offset >> 16;
1388aae1ed8eSDiego Novillo     uint32_t Discriminator = Offset & 0xffff;
13893376a787SDiego Novillo 
1390aae1ed8eSDiego Novillo     InlineCallStack NewStack;
1391aae1ed8eSDiego Novillo     NewStack.push_back(FProfile);
13921d0bc055SKazu Hirata     llvm::append_range(NewStack, InlineStack);
1393aae1ed8eSDiego Novillo     if (Update) {
1394aae1ed8eSDiego Novillo       // Walk up the inline stack, adding the samples on this line to
1395aae1ed8eSDiego Novillo       // the total sample count of the callers in the chain.
1396aae1ed8eSDiego Novillo       for (auto CallerProfile : NewStack)
1397aae1ed8eSDiego Novillo         CallerProfile->addTotalSamples(Count);
1398aae1ed8eSDiego Novillo 
1399aae1ed8eSDiego Novillo       // Update the body samples for the current profile.
1400aae1ed8eSDiego Novillo       FProfile->addBodySamples(LineOffset, Discriminator, Count);
1401aae1ed8eSDiego Novillo     }
1402aae1ed8eSDiego Novillo 
1403aae1ed8eSDiego Novillo     // Process the list of functions called at an indirect call site.
1404aae1ed8eSDiego Novillo     // These are all the targets that a function pointer (or virtual
1405aae1ed8eSDiego Novillo     // function) resolved at runtime.
14063376a787SDiego Novillo     for (uint32_t J = 0; J < NumTargets; J++) {
14073376a787SDiego Novillo       uint32_t HistVal;
14083376a787SDiego Novillo       if (!GcovBuffer.readInt(HistVal))
14093376a787SDiego Novillo         return sampleprof_error::truncated;
14103376a787SDiego Novillo 
14113376a787SDiego Novillo       if (HistVal != HIST_TYPE_INDIR_CALL_TOPN)
14123376a787SDiego Novillo         return sampleprof_error::malformed;
14133376a787SDiego Novillo 
14143376a787SDiego Novillo       uint64_t TargetIdx;
14153376a787SDiego Novillo       if (!GcovBuffer.readInt64(TargetIdx))
14163376a787SDiego Novillo         return sampleprof_error::truncated;
14173376a787SDiego Novillo       StringRef TargetName(Names[TargetIdx]);
14183376a787SDiego Novillo 
14193376a787SDiego Novillo       uint64_t TargetCount;
14203376a787SDiego Novillo       if (!GcovBuffer.readInt64(TargetCount))
14213376a787SDiego Novillo         return sampleprof_error::truncated;
14223376a787SDiego Novillo 
1423920677a9SDehao Chen       if (Update)
1424920677a9SDehao Chen         FProfile->addCalledTargetSamples(LineOffset, Discriminator,
1425aae1ed8eSDiego Novillo                                          TargetName, TargetCount);
14263376a787SDiego Novillo     }
14273376a787SDiego Novillo   }
14283376a787SDiego Novillo 
1429aae1ed8eSDiego Novillo   // Process all the inlined callers into the current function. These
1430aae1ed8eSDiego Novillo   // are all the callsites that were inlined into this function.
1431aae1ed8eSDiego Novillo   for (uint32_t I = 0; I < NumCallsites; I++) {
14323376a787SDiego Novillo     // The offset is encoded as:
14333376a787SDiego Novillo     //   high 16 bits: line offset to the start of the function.
14343376a787SDiego Novillo     //   low 16 bits: discriminator.
14353376a787SDiego Novillo     uint32_t Offset;
14363376a787SDiego Novillo     if (!GcovBuffer.readInt(Offset))
14373376a787SDiego Novillo       return sampleprof_error::truncated;
1438aae1ed8eSDiego Novillo     InlineCallStack NewStack;
1439aae1ed8eSDiego Novillo     NewStack.push_back(FProfile);
14401d0bc055SKazu Hirata     llvm::append_range(NewStack, InlineStack);
1441aae1ed8eSDiego Novillo     if (std::error_code EC = readOneFunctionProfile(NewStack, Update, Offset))
14423376a787SDiego Novillo       return EC;
14433376a787SDiego Novillo   }
14443376a787SDiego Novillo 
14453376a787SDiego Novillo   return sampleprof_error::success;
14463376a787SDiego Novillo }
14473376a787SDiego Novillo 
14485f8f34e4SAdrian Prantl /// Read a GCC AutoFDO profile.
14493376a787SDiego Novillo ///
14503376a787SDiego Novillo /// This format is generated by the Linux Perf conversion tool at
14513376a787SDiego Novillo /// https://github.com/google/autofdo.
14528c8ec1f6SWei Mi std::error_code SampleProfileReaderGCC::readImpl() {
14533376a787SDiego Novillo   // Read the string table.
14543376a787SDiego Novillo   if (std::error_code EC = readNameTable())
14553376a787SDiego Novillo     return EC;
14563376a787SDiego Novillo 
14573376a787SDiego Novillo   // Read the source profile.
14583376a787SDiego Novillo   if (std::error_code EC = readFunctionProfiles())
14593376a787SDiego Novillo     return EC;
14603376a787SDiego Novillo 
14613376a787SDiego Novillo   return sampleprof_error::success;
14623376a787SDiego Novillo }
14633376a787SDiego Novillo 
14643376a787SDiego Novillo bool SampleProfileReaderGCC::hasFormat(const MemoryBuffer &Buffer) {
14653376a787SDiego Novillo   StringRef Magic(reinterpret_cast<const char *>(Buffer.getBufferStart()));
14663376a787SDiego Novillo   return Magic == "adcg*704";
14673376a787SDiego Novillo }
14683376a787SDiego Novillo 
14698c8ec1f6SWei Mi void SampleProfileReaderItaniumRemapper::applyRemapping(LLVMContext &Ctx) {
1470ebad6788SWei Mi   // If the reader uses MD5 to represent string, we can't remap it because
147128436358SRichard Smith   // we don't know what the original function names were.
1472ebad6788SWei Mi   if (Reader.useMD5()) {
147328436358SRichard Smith     Ctx.diagnose(DiagnosticInfoSampleProfile(
14748c8ec1f6SWei Mi         Reader.getBuffer()->getBufferIdentifier(),
147528436358SRichard Smith         "Profile data remapping cannot be applied to profile data "
147628436358SRichard Smith         "in compact format (original mangled names are not available).",
147728436358SRichard Smith         DS_Warning));
14788c8ec1f6SWei Mi     return;
147928436358SRichard Smith   }
148028436358SRichard Smith 
14816b989a17SWenlei He   // CSSPGO-TODO: Remapper is not yet supported.
14826b989a17SWenlei He   // We will need to remap the entire context string.
14838c8ec1f6SWei Mi   assert(Remappings && "should be initialized while creating remapper");
1484c67ccf5fSWei Mi   for (auto &Sample : Reader.getProfiles()) {
1485c67ccf5fSWei Mi     DenseSet<StringRef> NamesInSample;
1486c67ccf5fSWei Mi     Sample.second.findAllNames(NamesInSample);
1487c67ccf5fSWei Mi     for (auto &Name : NamesInSample)
1488c67ccf5fSWei Mi       if (auto Key = Remappings->insert(Name))
1489c67ccf5fSWei Mi         NameMap.insert({Key, Name});
1490c67ccf5fSWei Mi   }
149128436358SRichard Smith 
14928c8ec1f6SWei Mi   RemappingApplied = true;
149328436358SRichard Smith }
149428436358SRichard Smith 
1495c67ccf5fSWei Mi Optional<StringRef>
1496c67ccf5fSWei Mi SampleProfileReaderItaniumRemapper::lookUpNameInProfile(StringRef Fname) {
14978c8ec1f6SWei Mi   if (auto Key = Remappings->lookup(Fname))
1498c67ccf5fSWei Mi     return NameMap.lookup(Key);
1499c67ccf5fSWei Mi   return None;
150028436358SRichard Smith }
150128436358SRichard Smith 
15025f8f34e4SAdrian Prantl /// Prepare a memory buffer for the contents of \p Filename.
1503de1ab26fSDiego Novillo ///
1504c572e92cSDiego Novillo /// \returns an error code indicating the status of the buffer.
1505fcd55607SDiego Novillo static ErrorOr<std::unique_ptr<MemoryBuffer>>
15060da23a27SBenjamin Kramer setupMemoryBuffer(const Twine &Filename) {
1507c572e92cSDiego Novillo   auto BufferOrErr = MemoryBuffer::getFileOrSTDIN(Filename);
1508c572e92cSDiego Novillo   if (std::error_code EC = BufferOrErr.getError())
1509c572e92cSDiego Novillo     return EC;
1510fcd55607SDiego Novillo   auto Buffer = std::move(BufferOrErr.get());
1511c572e92cSDiego Novillo 
1512c572e92cSDiego Novillo   // Sanity check the file.
1513260fe3ecSZachary Turner   if (uint64_t(Buffer->getBufferSize()) > std::numeric_limits<uint32_t>::max())
1514c572e92cSDiego Novillo     return sampleprof_error::too_large;
1515c572e92cSDiego Novillo 
1516c55cf4afSBill Wendling   return std::move(Buffer);
1517c572e92cSDiego Novillo }
1518c572e92cSDiego Novillo 
15195f8f34e4SAdrian Prantl /// Create a sample profile reader based on the format of the input file.
1520c572e92cSDiego Novillo ///
1521c572e92cSDiego Novillo /// \param Filename The file to open.
1522c572e92cSDiego Novillo ///
1523c572e92cSDiego Novillo /// \param C The LLVM context to use to emit diagnostics.
1524c572e92cSDiego Novillo ///
15258c8ec1f6SWei Mi /// \param RemapFilename The file used for profile remapping.
15268c8ec1f6SWei Mi ///
1527c572e92cSDiego Novillo /// \returns an error code indicating the status of the created reader.
1528fcd55607SDiego Novillo ErrorOr<std::unique_ptr<SampleProfileReader>>
15298c8ec1f6SWei Mi SampleProfileReader::create(const std::string Filename, LLVMContext &C,
15308c8ec1f6SWei Mi                             const std::string RemapFilename) {
1531fcd55607SDiego Novillo   auto BufferOrError = setupMemoryBuffer(Filename);
1532fcd55607SDiego Novillo   if (std::error_code EC = BufferOrError.getError())
1533c572e92cSDiego Novillo     return EC;
15348c8ec1f6SWei Mi   return create(BufferOrError.get(), C, RemapFilename);
153551abea74SNathan Slingerland }
1536c572e92cSDiego Novillo 
153728436358SRichard Smith /// Create a sample profile remapper from the given input, to remap the
153828436358SRichard Smith /// function names in the given profile data.
153928436358SRichard Smith ///
154028436358SRichard Smith /// \param Filename The file to open.
154128436358SRichard Smith ///
15428c8ec1f6SWei Mi /// \param Reader The profile reader the remapper is going to be applied to.
15438c8ec1f6SWei Mi ///
154428436358SRichard Smith /// \param C The LLVM context to use to emit diagnostics.
154528436358SRichard Smith ///
154628436358SRichard Smith /// \returns an error code indicating the status of the created reader.
15478c8ec1f6SWei Mi ErrorOr<std::unique_ptr<SampleProfileReaderItaniumRemapper>>
15488c8ec1f6SWei Mi SampleProfileReaderItaniumRemapper::create(const std::string Filename,
15498c8ec1f6SWei Mi                                            SampleProfileReader &Reader,
15508c8ec1f6SWei Mi                                            LLVMContext &C) {
155128436358SRichard Smith   auto BufferOrError = setupMemoryBuffer(Filename);
155228436358SRichard Smith   if (std::error_code EC = BufferOrError.getError())
155328436358SRichard Smith     return EC;
15548c8ec1f6SWei Mi   return create(BufferOrError.get(), Reader, C);
15558c8ec1f6SWei Mi }
15568c8ec1f6SWei Mi 
15578c8ec1f6SWei Mi /// Create a sample profile remapper from the given input, to remap the
15588c8ec1f6SWei Mi /// function names in the given profile data.
15598c8ec1f6SWei Mi ///
15608c8ec1f6SWei Mi /// \param B The memory buffer to create the reader from (assumes ownership).
15618c8ec1f6SWei Mi ///
15628c8ec1f6SWei Mi /// \param C The LLVM context to use to emit diagnostics.
15638c8ec1f6SWei Mi ///
15648c8ec1f6SWei Mi /// \param Reader The profile reader the remapper is going to be applied to.
15658c8ec1f6SWei Mi ///
15668c8ec1f6SWei Mi /// \returns an error code indicating the status of the created reader.
15678c8ec1f6SWei Mi ErrorOr<std::unique_ptr<SampleProfileReaderItaniumRemapper>>
15688c8ec1f6SWei Mi SampleProfileReaderItaniumRemapper::create(std::unique_ptr<MemoryBuffer> &B,
15698c8ec1f6SWei Mi                                            SampleProfileReader &Reader,
15708c8ec1f6SWei Mi                                            LLVMContext &C) {
15718c8ec1f6SWei Mi   auto Remappings = std::make_unique<SymbolRemappingReader>();
15728c8ec1f6SWei Mi   if (Error E = Remappings->read(*B.get())) {
15738c8ec1f6SWei Mi     handleAllErrors(
15748c8ec1f6SWei Mi         std::move(E), [&](const SymbolRemappingParseError &ParseError) {
15758c8ec1f6SWei Mi           C.diagnose(DiagnosticInfoSampleProfile(B->getBufferIdentifier(),
15768c8ec1f6SWei Mi                                                  ParseError.getLineNum(),
15778c8ec1f6SWei Mi                                                  ParseError.getMessage()));
15788c8ec1f6SWei Mi         });
15798c8ec1f6SWei Mi     return sampleprof_error::malformed;
15808c8ec1f6SWei Mi   }
15818c8ec1f6SWei Mi 
15820eaee545SJonas Devlieghere   return std::make_unique<SampleProfileReaderItaniumRemapper>(
15838c8ec1f6SWei Mi       std::move(B), std::move(Remappings), Reader);
158428436358SRichard Smith }
158528436358SRichard Smith 
15865f8f34e4SAdrian Prantl /// Create a sample profile reader based on the format of the input data.
158751abea74SNathan Slingerland ///
158851abea74SNathan Slingerland /// \param B The memory buffer to create the reader from (assumes ownership).
158951abea74SNathan Slingerland ///
159051abea74SNathan Slingerland /// \param C The LLVM context to use to emit diagnostics.
159151abea74SNathan Slingerland ///
15928c8ec1f6SWei Mi /// \param RemapFilename The file used for profile remapping.
15938c8ec1f6SWei Mi ///
159451abea74SNathan Slingerland /// \returns an error code indicating the status of the created reader.
159551abea74SNathan Slingerland ErrorOr<std::unique_ptr<SampleProfileReader>>
15968c8ec1f6SWei Mi SampleProfileReader::create(std::unique_ptr<MemoryBuffer> &B, LLVMContext &C,
15978c8ec1f6SWei Mi                             const std::string RemapFilename) {
1598fcd55607SDiego Novillo   std::unique_ptr<SampleProfileReader> Reader;
1599a0c0857eSWei Mi   if (SampleProfileReaderRawBinary::hasFormat(*B))
1600a0c0857eSWei Mi     Reader.reset(new SampleProfileReaderRawBinary(std::move(B), C));
1601be907324SWei Mi   else if (SampleProfileReaderExtBinary::hasFormat(*B))
1602be907324SWei Mi     Reader.reset(new SampleProfileReaderExtBinary(std::move(B), C));
1603a0c0857eSWei Mi   else if (SampleProfileReaderCompactBinary::hasFormat(*B))
1604a0c0857eSWei Mi     Reader.reset(new SampleProfileReaderCompactBinary(std::move(B), C));
160551abea74SNathan Slingerland   else if (SampleProfileReaderGCC::hasFormat(*B))
160651abea74SNathan Slingerland     Reader.reset(new SampleProfileReaderGCC(std::move(B), C));
160751abea74SNathan Slingerland   else if (SampleProfileReaderText::hasFormat(*B))
160851abea74SNathan Slingerland     Reader.reset(new SampleProfileReaderText(std::move(B), C));
16094f823667SNathan Slingerland   else
16104f823667SNathan Slingerland     return sampleprof_error::unrecognized_format;
1611c572e92cSDiego Novillo 
16128c8ec1f6SWei Mi   if (!RemapFilename.empty()) {
16138c8ec1f6SWei Mi     auto ReaderOrErr =
16148c8ec1f6SWei Mi         SampleProfileReaderItaniumRemapper::create(RemapFilename, *Reader, C);
16158c8ec1f6SWei Mi     if (std::error_code EC = ReaderOrErr.getError()) {
16168c8ec1f6SWei Mi       std::string Msg = "Could not create remapper: " + EC.message();
16178c8ec1f6SWei Mi       C.diagnose(DiagnosticInfoSampleProfile(RemapFilename, Msg));
16188c8ec1f6SWei Mi       return EC;
16198c8ec1f6SWei Mi     }
16208c8ec1f6SWei Mi     Reader->Remapper = std::move(ReaderOrErr.get());
16218c8ec1f6SWei Mi   }
16228c8ec1f6SWei Mi 
162394d44c97SWei Mi   FunctionSamples::Format = Reader->getFormat();
1624be907324SWei Mi   if (std::error_code EC = Reader->readHeader()) {
1625fcd55607SDiego Novillo     return EC;
1626be907324SWei Mi   }
1627fcd55607SDiego Novillo 
1628c55cf4afSBill Wendling   return std::move(Reader);
1629de1ab26fSDiego Novillo }
163040ee23dbSEaswaran Raman 
163140ee23dbSEaswaran Raman // For text and GCC file formats, we compute the summary after reading the
163240ee23dbSEaswaran Raman // profile. Binary format has the profile summary in its header.
163340ee23dbSEaswaran Raman void SampleProfileReader::computeSummary() {
1634e5a17e3fSEaswaran Raman   SampleProfileSummaryBuilder Builder(ProfileSummaryBuilder::DefaultCutoffs);
1635801d9cc7SWenlei He   Summary = Builder.computeSummaryForProfiles(Profiles);
163640ee23dbSEaswaran Raman }
1637