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);
147*551aaa24SKazu 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;
2256b989a17SWenlei He   int CSProfileCount = 0;
2266b989a17SWenlei He   int RegularProfileCount = 0;
227ac068e01SHongtao Yu   uint32_t ProbeProfileCount = 0;
228ac068e01SHongtao Yu 
229ac068e01SHongtao Yu   // SeenMetadata tracks whether we have processed metadata for the current
230ac068e01SHongtao Yu   // top-level function profile.
231ac068e01SHongtao Yu   bool SeenMetadata = false;
2326722688eSDehao Chen 
2336722688eSDehao Chen   for (; !LineIt.is_at_eof(); ++LineIt) {
2346722688eSDehao Chen     if ((*LineIt)[(*LineIt).find_first_not_of(' ')] == '#')
2356722688eSDehao Chen       continue;
236de1ab26fSDiego Novillo     // Read the header of each function.
237de1ab26fSDiego Novillo     //
238de1ab26fSDiego Novillo     // Note that for function identifiers we are actually expecting
239de1ab26fSDiego Novillo     // mangled names, but we may not always get them. This happens when
240de1ab26fSDiego Novillo     // the compiler decides not to emit the function (e.g., it was inlined
241de1ab26fSDiego Novillo     // and removed). In this case, the binary will not have the linkage
242de1ab26fSDiego Novillo     // name for the function, so the profiler will emit the function's
243de1ab26fSDiego Novillo     // unmangled name, which may contain characters like ':' and '>' in its
244de1ab26fSDiego Novillo     // name (member functions, templates, etc).
245de1ab26fSDiego Novillo     //
246de1ab26fSDiego Novillo     // The only requirement we place on the identifier, then, is that it
247de1ab26fSDiego Novillo     // should not begin with a number.
2486722688eSDehao Chen     if ((*LineIt)[0] != ' ') {
24938be3330SDiego Novillo       uint64_t NumSamples, NumHeadSamples;
2506722688eSDehao Chen       StringRef FName;
2516722688eSDehao Chen       if (!ParseHead(*LineIt, FName, NumSamples, NumHeadSamples)) {
2523376a787SDiego Novillo         reportError(LineIt.line_number(),
253de1ab26fSDiego Novillo                     "Expected 'mangled_name:NUM:NUM', found " + *LineIt);
254c572e92cSDiego Novillo         return sampleprof_error::malformed;
255de1ab26fSDiego Novillo       }
256ac068e01SHongtao Yu       SeenMetadata = false;
2576b989a17SWenlei He       SampleContext FContext(FName);
2586b989a17SWenlei He       if (FContext.hasContext())
2596b989a17SWenlei He         ++CSProfileCount;
2606b989a17SWenlei He       else
2616b989a17SWenlei He         ++RegularProfileCount;
2626b989a17SWenlei He       Profiles[FContext] = FunctionSamples();
2636b989a17SWenlei He       FunctionSamples &FProfile = Profiles[FContext];
2646b989a17SWenlei He       FProfile.setName(FContext.getName());
2656b989a17SWenlei He       FProfile.setContext(FContext);
26648dd080cSNathan Slingerland       MergeResult(Result, FProfile.addTotalSamples(NumSamples));
26748dd080cSNathan Slingerland       MergeResult(Result, FProfile.addHeadSamples(NumHeadSamples));
2686722688eSDehao Chen       InlineStack.clear();
2696722688eSDehao Chen       InlineStack.push_back(&FProfile);
2706722688eSDehao Chen     } else {
27138be3330SDiego Novillo       uint64_t NumSamples;
2726722688eSDehao Chen       StringRef FName;
27338be3330SDiego Novillo       DenseMap<StringRef, uint64_t> TargetCountMap;
27438be3330SDiego Novillo       uint32_t Depth, LineOffset, Discriminator;
275ac068e01SHongtao Yu       LineType LineTy;
276ac068e01SHongtao Yu       uint64_t FunctionHash;
277ac068e01SHongtao Yu       if (!ParseLine(*LineIt, LineTy, Depth, NumSamples, LineOffset,
278ac068e01SHongtao Yu                      Discriminator, FName, TargetCountMap, FunctionHash)) {
2793376a787SDiego Novillo         reportError(LineIt.line_number(),
2803376a787SDiego Novillo                     "Expected 'NUM[.NUM]: NUM[ mangled_name:NUM]*', found " +
2813376a787SDiego Novillo                         *LineIt);
282c572e92cSDiego Novillo         return sampleprof_error::malformed;
283de1ab26fSDiego Novillo       }
284ac068e01SHongtao Yu       if (SeenMetadata && LineTy != LineType::Metadata) {
285ac068e01SHongtao Yu         // Metadata must be put at the end of a function profile.
286ac068e01SHongtao Yu         reportError(LineIt.line_number(),
287ac068e01SHongtao Yu                     "Found non-metadata after metadata: " + *LineIt);
288ac068e01SHongtao Yu         return sampleprof_error::malformed;
289ac068e01SHongtao Yu       }
2906722688eSDehao Chen       while (InlineStack.size() > Depth) {
2916722688eSDehao Chen         InlineStack.pop_back();
292c572e92cSDiego Novillo       }
293ac068e01SHongtao Yu       switch (LineTy) {
294ac068e01SHongtao Yu       case LineType::CallSiteProfile: {
2956722688eSDehao Chen         FunctionSamples &FSamples = InlineStack.back()->functionSamplesAt(
296adcd0268SBenjamin Kramer             LineLocation(LineOffset, Discriminator))[std::string(FName)];
29757d1dda5SDehao Chen         FSamples.setName(FName);
29848dd080cSNathan Slingerland         MergeResult(Result, FSamples.addTotalSamples(NumSamples));
2996722688eSDehao Chen         InlineStack.push_back(&FSamples);
300ac068e01SHongtao Yu         break;
301ac068e01SHongtao Yu       }
302ac068e01SHongtao Yu       case LineType::BodyProfile: {
3036722688eSDehao Chen         while (InlineStack.size() > Depth) {
3046722688eSDehao Chen           InlineStack.pop_back();
3056722688eSDehao Chen         }
3066722688eSDehao Chen         FunctionSamples &FProfile = *InlineStack.back();
3076722688eSDehao Chen         for (const auto &name_count : TargetCountMap) {
30848dd080cSNathan Slingerland           MergeResult(Result, FProfile.addCalledTargetSamples(
30948dd080cSNathan Slingerland                                   LineOffset, Discriminator, name_count.first,
31048dd080cSNathan Slingerland                                   name_count.second));
311c572e92cSDiego Novillo         }
31248dd080cSNathan Slingerland         MergeResult(Result, FProfile.addBodySamples(LineOffset, Discriminator,
31348dd080cSNathan Slingerland                                                     NumSamples));
314ac068e01SHongtao Yu         break;
315ac068e01SHongtao Yu       }
316ac068e01SHongtao Yu       case LineType::Metadata: {
317ac068e01SHongtao Yu         FunctionSamples &FProfile = *InlineStack.back();
318ac068e01SHongtao Yu         FProfile.setFunctionHash(FunctionHash);
319ac068e01SHongtao Yu         ++ProbeProfileCount;
320ac068e01SHongtao Yu         SeenMetadata = true;
321ac068e01SHongtao Yu         break;
322ac068e01SHongtao Yu       }
3236722688eSDehao Chen       }
324de1ab26fSDiego Novillo     }
325de1ab26fSDiego Novillo   }
3266b989a17SWenlei He 
3276b989a17SWenlei He   assert((RegularProfileCount == 0 || CSProfileCount == 0) &&
3286b989a17SWenlei He          "Cannot have both context-sensitive and regular profile");
3296b989a17SWenlei He   ProfileIsCS = (CSProfileCount > 0);
330ac068e01SHongtao Yu   assert((ProbeProfileCount == 0 || ProbeProfileCount == Profiles.size()) &&
331ac068e01SHongtao Yu          "Cannot have both probe-based profiles and regular profiles");
332ac068e01SHongtao Yu   ProfileIsProbeBased = (ProbeProfileCount > 0);
333ac068e01SHongtao Yu   FunctionSamples::ProfileIsProbeBased = ProfileIsProbeBased;
3346b989a17SWenlei He 
33540ee23dbSEaswaran Raman   if (Result == sampleprof_error::success)
33640ee23dbSEaswaran Raman     computeSummary();
337de1ab26fSDiego Novillo 
33848dd080cSNathan Slingerland   return Result;
339de1ab26fSDiego Novillo }
340de1ab26fSDiego Novillo 
3414f823667SNathan Slingerland bool SampleProfileReaderText::hasFormat(const MemoryBuffer &Buffer) {
3424f823667SNathan Slingerland   bool result = false;
3434f823667SNathan Slingerland 
3444f823667SNathan Slingerland   // Check that the first non-comment line is a valid function header.
3454f823667SNathan Slingerland   line_iterator LineIt(Buffer, /*SkipBlanks=*/true, '#');
3464f823667SNathan Slingerland   if (!LineIt.is_at_eof()) {
3474f823667SNathan Slingerland     if ((*LineIt)[0] != ' ') {
3484f823667SNathan Slingerland       uint64_t NumSamples, NumHeadSamples;
3494f823667SNathan Slingerland       StringRef FName;
3504f823667SNathan Slingerland       result = ParseHead(*LineIt, FName, NumSamples, NumHeadSamples);
3514f823667SNathan Slingerland     }
3524f823667SNathan Slingerland   }
3534f823667SNathan Slingerland 
3544f823667SNathan Slingerland   return result;
3554f823667SNathan Slingerland }
3564f823667SNathan Slingerland 
357d5336ae2SDiego Novillo template <typename T> ErrorOr<T> SampleProfileReaderBinary::readNumber() {
358c572e92cSDiego Novillo   unsigned NumBytesRead = 0;
359c572e92cSDiego Novillo   std::error_code EC;
360c572e92cSDiego Novillo   uint64_t Val = decodeULEB128(Data, &NumBytesRead);
361c572e92cSDiego Novillo 
362c572e92cSDiego Novillo   if (Val > std::numeric_limits<T>::max())
363c572e92cSDiego Novillo     EC = sampleprof_error::malformed;
364c572e92cSDiego Novillo   else if (Data + NumBytesRead > End)
365c572e92cSDiego Novillo     EC = sampleprof_error::truncated;
366c572e92cSDiego Novillo   else
367c572e92cSDiego Novillo     EC = sampleprof_error::success;
368c572e92cSDiego Novillo 
369c572e92cSDiego Novillo   if (EC) {
3703376a787SDiego Novillo     reportError(0, EC.message());
371c572e92cSDiego Novillo     return EC;
372c572e92cSDiego Novillo   }
373c572e92cSDiego Novillo 
374c572e92cSDiego Novillo   Data += NumBytesRead;
375c572e92cSDiego Novillo   return static_cast<T>(Val);
376c572e92cSDiego Novillo }
377c572e92cSDiego Novillo 
378c572e92cSDiego Novillo ErrorOr<StringRef> SampleProfileReaderBinary::readString() {
379c572e92cSDiego Novillo   std::error_code EC;
380c572e92cSDiego Novillo   StringRef Str(reinterpret_cast<const char *>(Data));
381c572e92cSDiego Novillo   if (Data + Str.size() + 1 > End) {
382c572e92cSDiego Novillo     EC = sampleprof_error::truncated;
3833376a787SDiego Novillo     reportError(0, EC.message());
384c572e92cSDiego Novillo     return EC;
385c572e92cSDiego Novillo   }
386c572e92cSDiego Novillo 
387c572e92cSDiego Novillo   Data += Str.size() + 1;
388c572e92cSDiego Novillo   return Str;
389c572e92cSDiego Novillo }
390c572e92cSDiego Novillo 
391a0c0857eSWei Mi template <typename T>
3926a14325dSWei Mi ErrorOr<T> SampleProfileReaderBinary::readUnencodedNumber() {
3936a14325dSWei Mi   std::error_code EC;
3946a14325dSWei Mi 
3956a14325dSWei Mi   if (Data + sizeof(T) > End) {
3966a14325dSWei Mi     EC = sampleprof_error::truncated;
3976a14325dSWei Mi     reportError(0, EC.message());
3986a14325dSWei Mi     return EC;
3996a14325dSWei Mi   }
4006a14325dSWei Mi 
4016a14325dSWei Mi   using namespace support;
4026a14325dSWei Mi   T Val = endian::readNext<T, little, unaligned>(Data);
4036a14325dSWei Mi   return Val;
4046a14325dSWei Mi }
4056a14325dSWei Mi 
4066a14325dSWei Mi template <typename T>
407a0c0857eSWei Mi inline ErrorOr<uint32_t> SampleProfileReaderBinary::readStringIndex(T &Table) {
408760c5a8fSDiego Novillo   std::error_code EC;
40938be3330SDiego Novillo   auto Idx = readNumber<uint32_t>();
410760c5a8fSDiego Novillo   if (std::error_code EC = Idx.getError())
411760c5a8fSDiego Novillo     return EC;
412a0c0857eSWei Mi   if (*Idx >= Table.size())
413760c5a8fSDiego Novillo     return sampleprof_error::truncated_name_table;
414a0c0857eSWei Mi   return *Idx;
415a0c0857eSWei Mi }
416a0c0857eSWei Mi 
417be907324SWei Mi ErrorOr<StringRef> SampleProfileReaderBinary::readStringFromTable() {
418a0c0857eSWei Mi   auto Idx = readStringIndex(NameTable);
419a0c0857eSWei Mi   if (std::error_code EC = Idx.getError())
420a0c0857eSWei Mi     return EC;
421a0c0857eSWei Mi 
422760c5a8fSDiego Novillo   return NameTable[*Idx];
423760c5a8fSDiego Novillo }
424760c5a8fSDiego Novillo 
42564e76853SWei Mi ErrorOr<StringRef> SampleProfileReaderExtBinaryBase::readStringFromTable() {
42664e76853SWei Mi   if (!FixedLengthMD5)
42764e76853SWei Mi     return SampleProfileReaderBinary::readStringFromTable();
42864e76853SWei Mi 
42964e76853SWei Mi   // read NameTable index.
43064e76853SWei Mi   auto Idx = readStringIndex(NameTable);
43164e76853SWei Mi   if (std::error_code EC = Idx.getError())
43264e76853SWei Mi     return EC;
43364e76853SWei Mi 
43464e76853SWei Mi   // Check whether the name to be accessed has been accessed before,
43564e76853SWei Mi   // if not, read it from memory directly.
43664e76853SWei Mi   StringRef &SR = NameTable[*Idx];
43764e76853SWei Mi   if (SR.empty()) {
43864e76853SWei Mi     const uint8_t *SavedData = Data;
43964e76853SWei Mi     Data = MD5NameMemStart + ((*Idx) * sizeof(uint64_t));
44064e76853SWei Mi     auto FID = readUnencodedNumber<uint64_t>();
44164e76853SWei Mi     if (std::error_code EC = FID.getError())
44264e76853SWei Mi       return EC;
44364e76853SWei Mi     // Save the string converted from uint64_t in MD5StringBuf. All the
44464e76853SWei Mi     // references to the name are all StringRefs refering to the string
44564e76853SWei Mi     // in MD5StringBuf.
44664e76853SWei Mi     MD5StringBuf->push_back(std::to_string(*FID));
44764e76853SWei Mi     SR = MD5StringBuf->back();
44864e76853SWei Mi     Data = SavedData;
44964e76853SWei Mi   }
45064e76853SWei Mi   return SR;
45164e76853SWei Mi }
45264e76853SWei Mi 
453a0c0857eSWei Mi ErrorOr<StringRef> SampleProfileReaderCompactBinary::readStringFromTable() {
454a0c0857eSWei Mi   auto Idx = readStringIndex(NameTable);
455a0c0857eSWei Mi   if (std::error_code EC = Idx.getError())
456a0c0857eSWei Mi     return EC;
457a0c0857eSWei Mi 
458a0c0857eSWei Mi   return StringRef(NameTable[*Idx]);
459a0c0857eSWei Mi }
460a0c0857eSWei Mi 
461a7f1e8efSDiego Novillo std::error_code
462a7f1e8efSDiego Novillo SampleProfileReaderBinary::readProfile(FunctionSamples &FProfile) {
463b93483dbSDiego Novillo   auto NumSamples = readNumber<uint64_t>();
464b93483dbSDiego Novillo   if (std::error_code EC = NumSamples.getError())
465c572e92cSDiego Novillo     return EC;
466b93483dbSDiego Novillo   FProfile.addTotalSamples(*NumSamples);
467c572e92cSDiego Novillo 
468c572e92cSDiego Novillo   // Read the samples in the body.
46938be3330SDiego Novillo   auto NumRecords = readNumber<uint32_t>();
470c572e92cSDiego Novillo   if (std::error_code EC = NumRecords.getError())
471c572e92cSDiego Novillo     return EC;
472a7f1e8efSDiego Novillo 
47338be3330SDiego Novillo   for (uint32_t I = 0; I < *NumRecords; ++I) {
474c572e92cSDiego Novillo     auto LineOffset = readNumber<uint64_t>();
475c572e92cSDiego Novillo     if (std::error_code EC = LineOffset.getError())
476c572e92cSDiego Novillo       return EC;
477c572e92cSDiego Novillo 
47810042412SDehao Chen     if (!isOffsetLegal(*LineOffset)) {
47910042412SDehao Chen       return std::error_code();
48010042412SDehao Chen     }
48110042412SDehao Chen 
482c572e92cSDiego Novillo     auto Discriminator = readNumber<uint64_t>();
483c572e92cSDiego Novillo     if (std::error_code EC = Discriminator.getError())
484c572e92cSDiego Novillo       return EC;
485c572e92cSDiego Novillo 
486c572e92cSDiego Novillo     auto NumSamples = readNumber<uint64_t>();
487c572e92cSDiego Novillo     if (std::error_code EC = NumSamples.getError())
488c572e92cSDiego Novillo       return EC;
489c572e92cSDiego Novillo 
49038be3330SDiego Novillo     auto NumCalls = readNumber<uint32_t>();
491c572e92cSDiego Novillo     if (std::error_code EC = NumCalls.getError())
492c572e92cSDiego Novillo       return EC;
493c572e92cSDiego Novillo 
49438be3330SDiego Novillo     for (uint32_t J = 0; J < *NumCalls; ++J) {
495760c5a8fSDiego Novillo       auto CalledFunction(readStringFromTable());
496c572e92cSDiego Novillo       if (std::error_code EC = CalledFunction.getError())
497c572e92cSDiego Novillo         return EC;
498c572e92cSDiego Novillo 
499c572e92cSDiego Novillo       auto CalledFunctionSamples = readNumber<uint64_t>();
500c572e92cSDiego Novillo       if (std::error_code EC = CalledFunctionSamples.getError())
501c572e92cSDiego Novillo         return EC;
502c572e92cSDiego Novillo 
503c572e92cSDiego Novillo       FProfile.addCalledTargetSamples(*LineOffset, *Discriminator,
504a7f1e8efSDiego Novillo                                       *CalledFunction, *CalledFunctionSamples);
505c572e92cSDiego Novillo     }
506c572e92cSDiego Novillo 
507c572e92cSDiego Novillo     FProfile.addBodySamples(*LineOffset, *Discriminator, *NumSamples);
508c572e92cSDiego Novillo   }
509a7f1e8efSDiego Novillo 
510a7f1e8efSDiego Novillo   // Read all the samples for inlined function calls.
51138be3330SDiego Novillo   auto NumCallsites = readNumber<uint32_t>();
512a7f1e8efSDiego Novillo   if (std::error_code EC = NumCallsites.getError())
513a7f1e8efSDiego Novillo     return EC;
514a7f1e8efSDiego Novillo 
51538be3330SDiego Novillo   for (uint32_t J = 0; J < *NumCallsites; ++J) {
516a7f1e8efSDiego Novillo     auto LineOffset = readNumber<uint64_t>();
517a7f1e8efSDiego Novillo     if (std::error_code EC = LineOffset.getError())
518a7f1e8efSDiego Novillo       return EC;
519a7f1e8efSDiego Novillo 
520a7f1e8efSDiego Novillo     auto Discriminator = readNumber<uint64_t>();
521a7f1e8efSDiego Novillo     if (std::error_code EC = Discriminator.getError())
522a7f1e8efSDiego Novillo       return EC;
523a7f1e8efSDiego Novillo 
524760c5a8fSDiego Novillo     auto FName(readStringFromTable());
525a7f1e8efSDiego Novillo     if (std::error_code EC = FName.getError())
526a7f1e8efSDiego Novillo       return EC;
527a7f1e8efSDiego Novillo 
5282c7ca9b5SDehao Chen     FunctionSamples &CalleeProfile = FProfile.functionSamplesAt(
529adcd0268SBenjamin Kramer         LineLocation(*LineOffset, *Discriminator))[std::string(*FName)];
53057d1dda5SDehao Chen     CalleeProfile.setName(*FName);
531a7f1e8efSDiego Novillo     if (std::error_code EC = readProfile(CalleeProfile))
532a7f1e8efSDiego Novillo       return EC;
533a7f1e8efSDiego Novillo   }
534a7f1e8efSDiego Novillo 
535a7f1e8efSDiego Novillo   return sampleprof_error::success;
536a7f1e8efSDiego Novillo }
537a7f1e8efSDiego Novillo 
53809dcfe68SWei Mi std::error_code
53909dcfe68SWei Mi SampleProfileReaderBinary::readFuncProfile(const uint8_t *Start) {
54009dcfe68SWei Mi   Data = Start;
541b93483dbSDiego Novillo   auto NumHeadSamples = readNumber<uint64_t>();
542b93483dbSDiego Novillo   if (std::error_code EC = NumHeadSamples.getError())
543b93483dbSDiego Novillo     return EC;
544b93483dbSDiego Novillo 
545760c5a8fSDiego Novillo   auto FName(readStringFromTable());
546a7f1e8efSDiego Novillo   if (std::error_code EC = FName.getError())
547a7f1e8efSDiego Novillo     return EC;
548a7f1e8efSDiego Novillo 
549a7f1e8efSDiego Novillo   Profiles[*FName] = FunctionSamples();
550a7f1e8efSDiego Novillo   FunctionSamples &FProfile = Profiles[*FName];
55157d1dda5SDehao Chen   FProfile.setName(*FName);
552a7f1e8efSDiego Novillo 
553b93483dbSDiego Novillo   FProfile.addHeadSamples(*NumHeadSamples);
554b93483dbSDiego Novillo 
555a7f1e8efSDiego Novillo   if (std::error_code EC = readProfile(FProfile))
556a7f1e8efSDiego Novillo     return EC;
5576a14325dSWei Mi   return sampleprof_error::success;
558c572e92cSDiego Novillo }
559c572e92cSDiego Novillo 
5608c8ec1f6SWei Mi std::error_code SampleProfileReaderBinary::readImpl() {
5616a14325dSWei Mi   while (!at_eof()) {
56209dcfe68SWei Mi     if (std::error_code EC = readFuncProfile(Data))
5636a14325dSWei Mi       return EC;
5646a14325dSWei Mi   }
5656a14325dSWei Mi 
5666a14325dSWei Mi   return sampleprof_error::success;
5676a14325dSWei Mi }
5686a14325dSWei Mi 
56993953d41SWei Mi std::error_code SampleProfileReaderExtBinaryBase::readOneSection(
570ebad6788SWei Mi     const uint8_t *Start, uint64_t Size, const SecHdrTableEntry &Entry) {
571077a9c70SWei Mi   Data = Start;
572b523790aSWei Mi   End = Start + Size;
573ebad6788SWei Mi   switch (Entry.Type) {
574be907324SWei Mi   case SecProfSummary:
575be907324SWei Mi     if (std::error_code EC = readSummary())
576be907324SWei Mi       return EC;
577b49eac71SWei Mi     if (hasSecFlag(Entry, SecProfSummaryFlags::SecFlagPartial))
578b49eac71SWei Mi       Summary->setPartialProfile(true);
579be907324SWei Mi     break;
58064e76853SWei Mi   case SecNameTable: {
58164e76853SWei Mi     FixedLengthMD5 =
58264e76853SWei Mi         hasSecFlag(Entry, SecNameTableFlags::SecFlagFixedLengthMD5);
58364e76853SWei Mi     bool UseMD5 = hasSecFlag(Entry, SecNameTableFlags::SecFlagMD5Name);
58464e76853SWei Mi     assert((!FixedLengthMD5 || UseMD5) &&
58564e76853SWei Mi            "If FixedLengthMD5 is true, UseMD5 has to be true");
58664e76853SWei Mi     if (std::error_code EC = readNameTableSec(UseMD5))
587be907324SWei Mi       return EC;
588be907324SWei Mi     break;
58964e76853SWei Mi   }
590be907324SWei Mi   case SecLBRProfile:
59109dcfe68SWei Mi     if (std::error_code EC = readFuncProfiles())
592be907324SWei Mi       return EC;
593be907324SWei Mi     break;
59409dcfe68SWei Mi   case SecFuncOffsetTable:
59509dcfe68SWei Mi     if (std::error_code EC = readFuncOffsetTable())
596798e59b8SWei Mi       return EC;
597798e59b8SWei Mi     break;
598ac068e01SHongtao Yu   case SecFuncMetadata:
599ac068e01SHongtao Yu     ProfileIsProbeBased =
600ac068e01SHongtao Yu         hasSecFlag(Entry, SecFuncMetadataFlags::SecFlagIsProbeBased);
601ac068e01SHongtao Yu     FunctionSamples::ProfileIsProbeBased = ProfileIsProbeBased;
602ac068e01SHongtao Yu     if (std::error_code EC = readFuncMetadata())
603ac068e01SHongtao Yu       return EC;
604ac068e01SHongtao Yu     break;
60593953d41SWei Mi   case SecProfileSymbolList:
60693953d41SWei Mi     if (std::error_code EC = readProfileSymbolList())
60793953d41SWei Mi       return EC;
60893953d41SWei Mi     break;
609be907324SWei Mi   default:
61093953d41SWei Mi     if (std::error_code EC = readCustomSection(Entry))
61193953d41SWei Mi       return EC;
612077a9c70SWei Mi     break;
613be907324SWei Mi   }
614077a9c70SWei Mi   return sampleprof_error::success;
615077a9c70SWei Mi }
616077a9c70SWei Mi 
61793953d41SWei Mi void SampleProfileReaderExtBinaryBase::collectFuncsFrom(const Module &M) {
61809dcfe68SWei Mi   UseAllFuncs = false;
61909dcfe68SWei Mi   FuncsToUse.clear();
62009dcfe68SWei Mi   for (auto &F : M)
62109dcfe68SWei Mi     FuncsToUse.insert(FunctionSamples::getCanonicalFnName(F));
62209dcfe68SWei Mi }
62309dcfe68SWei Mi 
62493953d41SWei Mi std::error_code SampleProfileReaderExtBinaryBase::readFuncOffsetTable() {
625a906e3ecSWei Mi   // If there are more than one FuncOffsetTable, the profile read associated
626a906e3ecSWei Mi   // with previous FuncOffsetTable has to be done before next FuncOffsetTable
627a906e3ecSWei Mi   // is read.
628a906e3ecSWei Mi   FuncOffsetTable.clear();
629a906e3ecSWei Mi 
63009dcfe68SWei Mi   auto Size = readNumber<uint64_t>();
63109dcfe68SWei Mi   if (std::error_code EC = Size.getError())
63209dcfe68SWei Mi     return EC;
63309dcfe68SWei Mi 
63409dcfe68SWei Mi   FuncOffsetTable.reserve(*Size);
63509dcfe68SWei Mi   for (uint32_t I = 0; I < *Size; ++I) {
63609dcfe68SWei Mi     auto FName(readStringFromTable());
63709dcfe68SWei Mi     if (std::error_code EC = FName.getError())
63809dcfe68SWei Mi       return EC;
63909dcfe68SWei Mi 
64009dcfe68SWei Mi     auto Offset = readNumber<uint64_t>();
64109dcfe68SWei Mi     if (std::error_code EC = Offset.getError())
64209dcfe68SWei Mi       return EC;
64309dcfe68SWei Mi 
64409dcfe68SWei Mi     FuncOffsetTable[*FName] = *Offset;
64509dcfe68SWei Mi   }
64609dcfe68SWei Mi   return sampleprof_error::success;
64709dcfe68SWei Mi }
64809dcfe68SWei Mi 
64993953d41SWei Mi std::error_code SampleProfileReaderExtBinaryBase::readFuncProfiles() {
65009dcfe68SWei Mi   const uint8_t *Start = Data;
65109dcfe68SWei Mi   if (UseAllFuncs) {
65209dcfe68SWei Mi     while (Data < End) {
65309dcfe68SWei Mi       if (std::error_code EC = readFuncProfile(Data))
65409dcfe68SWei Mi         return EC;
65509dcfe68SWei Mi     }
65609dcfe68SWei Mi     assert(Data == End && "More data is read than expected");
65709dcfe68SWei Mi     return sampleprof_error::success;
65809dcfe68SWei Mi   }
65909dcfe68SWei Mi 
6608c8ec1f6SWei Mi   if (Remapper) {
66109dcfe68SWei Mi     for (auto Name : FuncsToUse) {
6628c8ec1f6SWei Mi       Remapper->insert(Name);
6638c8ec1f6SWei Mi     }
6648c8ec1f6SWei Mi   }
6658c8ec1f6SWei Mi 
666ebad6788SWei Mi   if (useMD5()) {
667ebad6788SWei Mi     for (auto Name : FuncsToUse) {
668ebad6788SWei Mi       auto GUID = std::to_string(MD5Hash(Name));
669ebad6788SWei Mi       auto iter = FuncOffsetTable.find(StringRef(GUID));
670ebad6788SWei Mi       if (iter == FuncOffsetTable.end())
671ebad6788SWei Mi         continue;
672ebad6788SWei Mi       const uint8_t *FuncProfileAddr = Start + iter->second;
673ebad6788SWei Mi       assert(FuncProfileAddr < End && "out of LBRProfile section");
674ebad6788SWei Mi       if (std::error_code EC = readFuncProfile(FuncProfileAddr))
675ebad6788SWei Mi         return EC;
676ebad6788SWei Mi     }
677ebad6788SWei Mi   } else {
6788c8ec1f6SWei Mi     for (auto NameOffset : FuncOffsetTable) {
6798c8ec1f6SWei Mi       auto FuncName = NameOffset.first;
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   }
6898c8ec1f6SWei Mi 
69009dcfe68SWei Mi   Data = End;
69109dcfe68SWei Mi   return sampleprof_error::success;
69209dcfe68SWei Mi }
69309dcfe68SWei Mi 
69493953d41SWei Mi std::error_code SampleProfileReaderExtBinaryBase::readProfileSymbolList() {
695b523790aSWei Mi   if (!ProfSymList)
696b523790aSWei Mi     ProfSymList = std::make_unique<ProfileSymbolList>();
697b523790aSWei Mi 
69809dcfe68SWei Mi   if (std::error_code EC = ProfSymList->read(Data, End - Data))
699798e59b8SWei Mi     return EC;
700798e59b8SWei Mi 
70109dcfe68SWei Mi   Data = End;
702b523790aSWei Mi   return sampleprof_error::success;
703b523790aSWei Mi }
704b523790aSWei Mi 
705b523790aSWei Mi std::error_code SampleProfileReaderExtBinaryBase::decompressSection(
706b523790aSWei Mi     const uint8_t *SecStart, const uint64_t SecSize,
707b523790aSWei Mi     const uint8_t *&DecompressBuf, uint64_t &DecompressBufSize) {
708b523790aSWei Mi   Data = SecStart;
709b523790aSWei Mi   End = SecStart + SecSize;
710b523790aSWei Mi   auto DecompressSize = readNumber<uint64_t>();
711b523790aSWei Mi   if (std::error_code EC = DecompressSize.getError())
712b523790aSWei Mi     return EC;
713b523790aSWei Mi   DecompressBufSize = *DecompressSize;
714b523790aSWei Mi 
715798e59b8SWei Mi   auto CompressSize = readNumber<uint64_t>();
716798e59b8SWei Mi   if (std::error_code EC = CompressSize.getError())
717798e59b8SWei Mi     return EC;
718798e59b8SWei Mi 
719b523790aSWei Mi   if (!llvm::zlib::isAvailable())
720b523790aSWei Mi     return sampleprof_error::zlib_unavailable;
721798e59b8SWei Mi 
722b523790aSWei Mi   StringRef CompressedStrings(reinterpret_cast<const char *>(Data),
723b523790aSWei Mi                               *CompressSize);
724b523790aSWei Mi   char *Buffer = Allocator.Allocate<char>(DecompressBufSize);
725283df8cfSWei Mi   size_t UCSize = DecompressBufSize;
726b523790aSWei Mi   llvm::Error E =
727283df8cfSWei Mi       zlib::uncompress(CompressedStrings, Buffer, UCSize);
728b523790aSWei Mi   if (E)
729b523790aSWei Mi     return sampleprof_error::uncompress_failed;
730b523790aSWei Mi   DecompressBuf = reinterpret_cast<const uint8_t *>(Buffer);
731798e59b8SWei Mi   return sampleprof_error::success;
732798e59b8SWei Mi }
733798e59b8SWei Mi 
7348c8ec1f6SWei Mi std::error_code SampleProfileReaderExtBinaryBase::readImpl() {
735077a9c70SWei Mi   const uint8_t *BufStart =
736077a9c70SWei Mi       reinterpret_cast<const uint8_t *>(Buffer->getBufferStart());
737077a9c70SWei Mi 
738077a9c70SWei Mi   for (auto &Entry : SecHdrTable) {
739077a9c70SWei Mi     // Skip empty section.
740077a9c70SWei Mi     if (!Entry.Size)
741077a9c70SWei Mi       continue;
742b523790aSWei Mi 
74321b1ad03SWei Mi     // Skip sections without context when SkipFlatProf is true.
74421b1ad03SWei Mi     if (SkipFlatProf && hasSecFlag(Entry, SecCommonFlags::SecFlagFlat))
74521b1ad03SWei Mi       continue;
74621b1ad03SWei Mi 
747077a9c70SWei Mi     const uint8_t *SecStart = BufStart + Entry.Offset;
748b523790aSWei Mi     uint64_t SecSize = Entry.Size;
749b523790aSWei Mi 
750b523790aSWei Mi     // If the section is compressed, decompress it into a buffer
751b523790aSWei Mi     // DecompressBuf before reading the actual data. The pointee of
752b523790aSWei Mi     // 'Data' will be changed to buffer hold by DecompressBuf
753b523790aSWei Mi     // temporarily when reading the actual data.
754ebad6788SWei Mi     bool isCompressed = hasSecFlag(Entry, SecCommonFlags::SecFlagCompress);
755b523790aSWei Mi     if (isCompressed) {
756b523790aSWei Mi       const uint8_t *DecompressBuf;
757b523790aSWei Mi       uint64_t DecompressBufSize;
758b523790aSWei Mi       if (std::error_code EC = decompressSection(
759b523790aSWei Mi               SecStart, SecSize, DecompressBuf, DecompressBufSize))
760077a9c70SWei Mi         return EC;
761b523790aSWei Mi       SecStart = DecompressBuf;
762b523790aSWei Mi       SecSize = DecompressBufSize;
763b523790aSWei Mi     }
764b523790aSWei Mi 
765ebad6788SWei Mi     if (std::error_code EC = readOneSection(SecStart, SecSize, Entry))
766b523790aSWei Mi       return EC;
767b523790aSWei Mi     if (Data != SecStart + SecSize)
768be907324SWei Mi       return sampleprof_error::malformed;
769b523790aSWei Mi 
770b523790aSWei Mi     // Change the pointee of 'Data' from DecompressBuf to original Buffer.
771b523790aSWei Mi     if (isCompressed) {
772b523790aSWei Mi       Data = BufStart + Entry.Offset;
773b523790aSWei Mi       End = BufStart + Buffer->getBufferSize();
774b523790aSWei Mi     }
775be907324SWei Mi   }
776be907324SWei Mi 
777be907324SWei Mi   return sampleprof_error::success;
778be907324SWei Mi }
779be907324SWei Mi 
7808c8ec1f6SWei Mi std::error_code SampleProfileReaderCompactBinary::readImpl() {
781d3289544SWenlei He   std::vector<uint64_t> OffsetsToUse;
782d3289544SWenlei He   if (UseAllFuncs) {
783d3289544SWenlei He     for (auto FuncEntry : FuncOffsetTable) {
784d3289544SWenlei He       OffsetsToUse.push_back(FuncEntry.second);
785d3289544SWenlei He     }
786d3289544SWenlei He   }
787d3289544SWenlei He   else {
7886a14325dSWei Mi     for (auto Name : FuncsToUse) {
7896a14325dSWei Mi       auto GUID = std::to_string(MD5Hash(Name));
7906a14325dSWei Mi       auto iter = FuncOffsetTable.find(StringRef(GUID));
7916a14325dSWei Mi       if (iter == FuncOffsetTable.end())
7926a14325dSWei Mi         continue;
793d3289544SWenlei He       OffsetsToUse.push_back(iter->second);
794d3289544SWenlei He     }
795d3289544SWenlei He   }
796d3289544SWenlei He 
797d3289544SWenlei He   for (auto Offset : OffsetsToUse) {
7986a14325dSWei Mi     const uint8_t *SavedData = Data;
79909dcfe68SWei Mi     if (std::error_code EC = readFuncProfile(
80009dcfe68SWei Mi             reinterpret_cast<const uint8_t *>(Buffer->getBufferStart()) +
80109dcfe68SWei Mi             Offset))
8026a14325dSWei Mi       return EC;
8036a14325dSWei Mi     Data = SavedData;
8046a14325dSWei Mi   }
805c572e92cSDiego Novillo   return sampleprof_error::success;
806c572e92cSDiego Novillo }
807c572e92cSDiego Novillo 
808a0c0857eSWei Mi std::error_code SampleProfileReaderRawBinary::verifySPMagic(uint64_t Magic) {
809a0c0857eSWei Mi   if (Magic == SPMagic())
810a0c0857eSWei Mi     return sampleprof_error::success;
811a0c0857eSWei Mi   return sampleprof_error::bad_magic;
812a0c0857eSWei Mi }
813a0c0857eSWei Mi 
814be907324SWei Mi std::error_code SampleProfileReaderExtBinary::verifySPMagic(uint64_t Magic) {
815be907324SWei Mi   if (Magic == SPMagic(SPF_Ext_Binary))
816be907324SWei Mi     return sampleprof_error::success;
817be907324SWei Mi   return sampleprof_error::bad_magic;
818be907324SWei Mi }
819be907324SWei Mi 
820a0c0857eSWei Mi std::error_code
821a0c0857eSWei Mi SampleProfileReaderCompactBinary::verifySPMagic(uint64_t Magic) {
822a0c0857eSWei Mi   if (Magic == SPMagic(SPF_Compact_Binary))
823a0c0857eSWei Mi     return sampleprof_error::success;
824a0c0857eSWei Mi   return sampleprof_error::bad_magic;
825a0c0857eSWei Mi }
826a0c0857eSWei Mi 
827be907324SWei Mi std::error_code SampleProfileReaderBinary::readNameTable() {
828a0c0857eSWei Mi   auto Size = readNumber<uint32_t>();
829a0c0857eSWei Mi   if (std::error_code EC = Size.getError())
830a0c0857eSWei Mi     return EC;
831a906e3ecSWei Mi   NameTable.reserve(*Size + NameTable.size());
832a0c0857eSWei Mi   for (uint32_t I = 0; I < *Size; ++I) {
833a0c0857eSWei Mi     auto Name(readString());
834a0c0857eSWei Mi     if (std::error_code EC = Name.getError())
835a0c0857eSWei Mi       return EC;
836a0c0857eSWei Mi     NameTable.push_back(*Name);
837a0c0857eSWei Mi   }
838a0c0857eSWei Mi 
839a0c0857eSWei Mi   return sampleprof_error::success;
840a0c0857eSWei Mi }
841a0c0857eSWei Mi 
84293953d41SWei Mi std::error_code SampleProfileReaderExtBinaryBase::readMD5NameTable() {
843ebad6788SWei Mi   auto Size = readNumber<uint64_t>();
844ebad6788SWei Mi   if (std::error_code EC = Size.getError())
845ebad6788SWei Mi     return EC;
846ebad6788SWei Mi   MD5StringBuf = std::make_unique<std::vector<std::string>>();
847ebad6788SWei Mi   MD5StringBuf->reserve(*Size);
84864e76853SWei Mi   if (FixedLengthMD5) {
84964e76853SWei Mi     // Preallocate and initialize NameTable so we can check whether a name
85064e76853SWei Mi     // index has been read before by checking whether the element in the
85164e76853SWei Mi     // NameTable is empty, meanwhile readStringIndex can do the boundary
85264e76853SWei Mi     // check using the size of NameTable.
85364e76853SWei Mi     NameTable.resize(*Size + NameTable.size());
85464e76853SWei Mi 
85564e76853SWei Mi     MD5NameMemStart = Data;
85664e76853SWei Mi     Data = Data + (*Size) * sizeof(uint64_t);
85764e76853SWei Mi     return sampleprof_error::success;
85864e76853SWei Mi   }
85964e76853SWei Mi   NameTable.reserve(*Size);
860ebad6788SWei Mi   for (uint32_t I = 0; I < *Size; ++I) {
861ebad6788SWei Mi     auto FID = readNumber<uint64_t>();
862ebad6788SWei Mi     if (std::error_code EC = FID.getError())
863ebad6788SWei Mi       return EC;
864ebad6788SWei Mi     MD5StringBuf->push_back(std::to_string(*FID));
865ebad6788SWei Mi     // NameTable is a vector of StringRef. Here it is pushing back a
866ebad6788SWei Mi     // StringRef initialized with the last string in MD5stringBuf.
867ebad6788SWei Mi     NameTable.push_back(MD5StringBuf->back());
868ebad6788SWei Mi   }
869ebad6788SWei Mi   return sampleprof_error::success;
870ebad6788SWei Mi }
871ebad6788SWei Mi 
87293953d41SWei Mi std::error_code SampleProfileReaderExtBinaryBase::readNameTableSec(bool IsMD5) {
873ebad6788SWei Mi   if (IsMD5)
874ebad6788SWei Mi     return readMD5NameTable();
875ebad6788SWei Mi   return SampleProfileReaderBinary::readNameTable();
876ebad6788SWei Mi }
877ebad6788SWei Mi 
878ac068e01SHongtao Yu std::error_code SampleProfileReaderExtBinaryBase::readFuncMetadata() {
879ac068e01SHongtao Yu   if (!ProfileIsProbeBased)
880ac068e01SHongtao Yu     return sampleprof_error::success;
881ac068e01SHongtao Yu   for (unsigned I = 0; I < Profiles.size(); ++I) {
882ac068e01SHongtao Yu     auto FName(readStringFromTable());
883ac068e01SHongtao Yu     if (std::error_code EC = FName.getError())
884ac068e01SHongtao Yu       return EC;
885ac068e01SHongtao Yu 
886ac068e01SHongtao Yu     auto Checksum = readNumber<uint64_t>();
887ac068e01SHongtao Yu     if (std::error_code EC = Checksum.getError())
888ac068e01SHongtao Yu       return EC;
889ac068e01SHongtao Yu 
890ac068e01SHongtao Yu     Profiles[*FName].setFunctionHash(*Checksum);
891ac068e01SHongtao Yu   }
892ac068e01SHongtao Yu   return sampleprof_error::success;
893ac068e01SHongtao Yu }
894ac068e01SHongtao Yu 
895a0c0857eSWei Mi std::error_code SampleProfileReaderCompactBinary::readNameTable() {
896a0c0857eSWei Mi   auto Size = readNumber<uint64_t>();
897a0c0857eSWei Mi   if (std::error_code EC = Size.getError())
898a0c0857eSWei Mi     return EC;
899a0c0857eSWei Mi   NameTable.reserve(*Size);
900a0c0857eSWei Mi   for (uint32_t I = 0; I < *Size; ++I) {
901a0c0857eSWei Mi     auto FID = readNumber<uint64_t>();
902a0c0857eSWei Mi     if (std::error_code EC = FID.getError())
903a0c0857eSWei Mi       return EC;
904a0c0857eSWei Mi     NameTable.push_back(std::to_string(*FID));
905a0c0857eSWei Mi   }
906a0c0857eSWei Mi   return sampleprof_error::success;
907a0c0857eSWei Mi }
908a0c0857eSWei Mi 
909a906e3ecSWei Mi std::error_code
910a906e3ecSWei Mi SampleProfileReaderExtBinaryBase::readSecHdrTableEntry(uint32_t Idx) {
911be907324SWei Mi   SecHdrTableEntry Entry;
912be907324SWei Mi   auto Type = readUnencodedNumber<uint64_t>();
913be907324SWei Mi   if (std::error_code EC = Type.getError())
914be907324SWei Mi     return EC;
915be907324SWei Mi   Entry.Type = static_cast<SecType>(*Type);
916c572e92cSDiego Novillo 
917b523790aSWei Mi   auto Flags = readUnencodedNumber<uint64_t>();
918b523790aSWei Mi   if (std::error_code EC = Flags.getError())
919be907324SWei Mi     return EC;
920b523790aSWei Mi   Entry.Flags = *Flags;
921be907324SWei Mi 
922be907324SWei Mi   auto Offset = readUnencodedNumber<uint64_t>();
923be907324SWei Mi   if (std::error_code EC = Offset.getError())
924be907324SWei Mi     return EC;
925be907324SWei Mi   Entry.Offset = *Offset;
926be907324SWei Mi 
927be907324SWei Mi   auto Size = readUnencodedNumber<uint64_t>();
928be907324SWei Mi   if (std::error_code EC = Size.getError())
929be907324SWei Mi     return EC;
930be907324SWei Mi   Entry.Size = *Size;
931be907324SWei Mi 
932a906e3ecSWei Mi   Entry.LayoutIndex = Idx;
933be907324SWei Mi   SecHdrTable.push_back(std::move(Entry));
934be907324SWei Mi   return sampleprof_error::success;
935be907324SWei Mi }
936be907324SWei Mi 
937be907324SWei Mi std::error_code SampleProfileReaderExtBinaryBase::readSecHdrTable() {
938be907324SWei Mi   auto EntryNum = readUnencodedNumber<uint64_t>();
939be907324SWei Mi   if (std::error_code EC = EntryNum.getError())
940be907324SWei Mi     return EC;
941be907324SWei Mi 
942be907324SWei Mi   for (uint32_t i = 0; i < (*EntryNum); i++)
943a906e3ecSWei Mi     if (std::error_code EC = readSecHdrTableEntry(i))
944be907324SWei Mi       return EC;
945be907324SWei Mi 
946be907324SWei Mi   return sampleprof_error::success;
947be907324SWei Mi }
948be907324SWei Mi 
949be907324SWei Mi std::error_code SampleProfileReaderExtBinaryBase::readHeader() {
950be907324SWei Mi   const uint8_t *BufStart =
951be907324SWei Mi       reinterpret_cast<const uint8_t *>(Buffer->getBufferStart());
952be907324SWei Mi   Data = BufStart;
953be907324SWei Mi   End = BufStart + Buffer->getBufferSize();
954be907324SWei Mi 
955be907324SWei Mi   if (std::error_code EC = readMagicIdent())
956be907324SWei Mi     return EC;
957be907324SWei Mi 
958be907324SWei Mi   if (std::error_code EC = readSecHdrTable())
959be907324SWei Mi     return EC;
960be907324SWei Mi 
961be907324SWei Mi   return sampleprof_error::success;
962be907324SWei Mi }
963be907324SWei Mi 
964eee532cdSWei Mi uint64_t SampleProfileReaderExtBinaryBase::getSectionSize(SecType Type) {
965a906e3ecSWei Mi   uint64_t Size = 0;
966eee532cdSWei Mi   for (auto &Entry : SecHdrTable) {
967eee532cdSWei Mi     if (Entry.Type == Type)
968a906e3ecSWei Mi       Size += Entry.Size;
969eee532cdSWei Mi   }
970a906e3ecSWei Mi   return Size;
971eee532cdSWei Mi }
972eee532cdSWei Mi 
973eee532cdSWei Mi uint64_t SampleProfileReaderExtBinaryBase::getFileSize() {
97409dcfe68SWei Mi   // Sections in SecHdrTable is not necessarily in the same order as
97509dcfe68SWei Mi   // sections in the profile because section like FuncOffsetTable needs
97609dcfe68SWei Mi   // to be written after section LBRProfile but needs to be read before
97709dcfe68SWei Mi   // section LBRProfile, so we cannot simply use the last entry in
97809dcfe68SWei Mi   // SecHdrTable to calculate the file size.
97909dcfe68SWei Mi   uint64_t FileSize = 0;
98009dcfe68SWei Mi   for (auto &Entry : SecHdrTable) {
98109dcfe68SWei Mi     FileSize = std::max(Entry.Offset + Entry.Size, FileSize);
98209dcfe68SWei Mi   }
98309dcfe68SWei Mi   return FileSize;
984eee532cdSWei Mi }
985eee532cdSWei Mi 
986b49eac71SWei Mi static std::string getSecFlagsStr(const SecHdrTableEntry &Entry) {
987b49eac71SWei Mi   std::string Flags;
988b49eac71SWei Mi   if (hasSecFlag(Entry, SecCommonFlags::SecFlagCompress))
989b49eac71SWei Mi     Flags.append("{compressed,");
990b49eac71SWei Mi   else
991b49eac71SWei Mi     Flags.append("{");
992b49eac71SWei Mi 
99321b1ad03SWei Mi   if (hasSecFlag(Entry, SecCommonFlags::SecFlagFlat))
99421b1ad03SWei Mi     Flags.append("flat,");
99521b1ad03SWei Mi 
996b49eac71SWei Mi   switch (Entry.Type) {
997b49eac71SWei Mi   case SecNameTable:
99864e76853SWei Mi     if (hasSecFlag(Entry, SecNameTableFlags::SecFlagFixedLengthMD5))
99964e76853SWei Mi       Flags.append("fixlenmd5,");
100064e76853SWei Mi     else if (hasSecFlag(Entry, SecNameTableFlags::SecFlagMD5Name))
1001b49eac71SWei Mi       Flags.append("md5,");
1002b49eac71SWei Mi     break;
1003b49eac71SWei Mi   case SecProfSummary:
1004b49eac71SWei Mi     if (hasSecFlag(Entry, SecProfSummaryFlags::SecFlagPartial))
1005b49eac71SWei Mi       Flags.append("partial,");
1006b49eac71SWei Mi     break;
1007b49eac71SWei Mi   default:
1008b49eac71SWei Mi     break;
1009b49eac71SWei Mi   }
1010b49eac71SWei Mi   char &last = Flags.back();
1011b49eac71SWei Mi   if (last == ',')
1012b49eac71SWei Mi     last = '}';
1013b49eac71SWei Mi   else
1014b49eac71SWei Mi     Flags.append("}");
1015b49eac71SWei Mi   return Flags;
1016b49eac71SWei Mi }
1017b49eac71SWei Mi 
1018eee532cdSWei Mi bool SampleProfileReaderExtBinaryBase::dumpSectionInfo(raw_ostream &OS) {
1019eee532cdSWei Mi   uint64_t TotalSecsSize = 0;
1020eee532cdSWei Mi   for (auto &Entry : SecHdrTable) {
1021eee532cdSWei Mi     OS << getSecName(Entry.Type) << " - Offset: " << Entry.Offset
1022b49eac71SWei Mi        << ", Size: " << Entry.Size << ", Flags: " << getSecFlagsStr(Entry)
1023b49eac71SWei Mi        << "\n";
1024b49eac71SWei Mi     ;
1025a906e3ecSWei Mi     TotalSecsSize += Entry.Size;
1026eee532cdSWei Mi   }
1027eee532cdSWei Mi   uint64_t HeaderSize = SecHdrTable.front().Offset;
1028eee532cdSWei Mi   assert(HeaderSize + TotalSecsSize == getFileSize() &&
1029eee532cdSWei Mi          "Size of 'header + sections' doesn't match the total size of profile");
1030eee532cdSWei Mi 
1031eee532cdSWei Mi   OS << "Header Size: " << HeaderSize << "\n";
1032eee532cdSWei Mi   OS << "Total Sections Size: " << TotalSecsSize << "\n";
1033eee532cdSWei Mi   OS << "File Size: " << getFileSize() << "\n";
1034eee532cdSWei Mi   return true;
1035eee532cdSWei Mi }
1036eee532cdSWei Mi 
1037be907324SWei Mi std::error_code SampleProfileReaderBinary::readMagicIdent() {
1038c572e92cSDiego Novillo   // Read and check the magic identifier.
1039c572e92cSDiego Novillo   auto Magic = readNumber<uint64_t>();
1040c572e92cSDiego Novillo   if (std::error_code EC = Magic.getError())
1041c572e92cSDiego Novillo     return EC;
1042a0c0857eSWei Mi   else if (std::error_code EC = verifySPMagic(*Magic))
1043c6b96c8dSWei Mi     return EC;
1044c572e92cSDiego Novillo 
1045c572e92cSDiego Novillo   // Read the version number.
1046c572e92cSDiego Novillo   auto Version = readNumber<uint64_t>();
1047c572e92cSDiego Novillo   if (std::error_code EC = Version.getError())
1048c572e92cSDiego Novillo     return EC;
1049c572e92cSDiego Novillo   else if (*Version != SPVersion())
1050c572e92cSDiego Novillo     return sampleprof_error::unsupported_version;
1051c572e92cSDiego Novillo 
1052be907324SWei Mi   return sampleprof_error::success;
1053be907324SWei Mi }
1054be907324SWei Mi 
1055be907324SWei Mi std::error_code SampleProfileReaderBinary::readHeader() {
1056be907324SWei Mi   Data = reinterpret_cast<const uint8_t *>(Buffer->getBufferStart());
1057be907324SWei Mi   End = Data + Buffer->getBufferSize();
1058be907324SWei Mi 
1059be907324SWei Mi   if (std::error_code EC = readMagicIdent())
1060be907324SWei Mi     return EC;
1061be907324SWei Mi 
106240ee23dbSEaswaran Raman   if (std::error_code EC = readSummary())
106340ee23dbSEaswaran Raman     return EC;
106440ee23dbSEaswaran Raman 
1065a0c0857eSWei Mi   if (std::error_code EC = readNameTable())
1066760c5a8fSDiego Novillo     return EC;
1067c572e92cSDiego Novillo   return sampleprof_error::success;
1068c572e92cSDiego Novillo }
1069c572e92cSDiego Novillo 
10706a14325dSWei Mi std::error_code SampleProfileReaderCompactBinary::readHeader() {
10716a14325dSWei Mi   SampleProfileReaderBinary::readHeader();
10726a14325dSWei Mi   if (std::error_code EC = readFuncOffsetTable())
10736a14325dSWei Mi     return EC;
10746a14325dSWei Mi   return sampleprof_error::success;
10756a14325dSWei Mi }
10766a14325dSWei Mi 
10776a14325dSWei Mi std::error_code SampleProfileReaderCompactBinary::readFuncOffsetTable() {
10786a14325dSWei Mi   auto TableOffset = readUnencodedNumber<uint64_t>();
10796a14325dSWei Mi   if (std::error_code EC = TableOffset.getError())
10806a14325dSWei Mi     return EC;
10816a14325dSWei Mi 
10826a14325dSWei Mi   const uint8_t *SavedData = Data;
10836a14325dSWei Mi   const uint8_t *TableStart =
10846a14325dSWei Mi       reinterpret_cast<const uint8_t *>(Buffer->getBufferStart()) +
10856a14325dSWei Mi       *TableOffset;
10866a14325dSWei Mi   Data = TableStart;
10876a14325dSWei Mi 
10886a14325dSWei Mi   auto Size = readNumber<uint64_t>();
10896a14325dSWei Mi   if (std::error_code EC = Size.getError())
10906a14325dSWei Mi     return EC;
10916a14325dSWei Mi 
10926a14325dSWei Mi   FuncOffsetTable.reserve(*Size);
10936a14325dSWei Mi   for (uint32_t I = 0; I < *Size; ++I) {
10946a14325dSWei Mi     auto FName(readStringFromTable());
10956a14325dSWei Mi     if (std::error_code EC = FName.getError())
10966a14325dSWei Mi       return EC;
10976a14325dSWei Mi 
10986a14325dSWei Mi     auto Offset = readNumber<uint64_t>();
10996a14325dSWei Mi     if (std::error_code EC = Offset.getError())
11006a14325dSWei Mi       return EC;
11016a14325dSWei Mi 
11026a14325dSWei Mi     FuncOffsetTable[*FName] = *Offset;
11036a14325dSWei Mi   }
11046a14325dSWei Mi   End = TableStart;
11056a14325dSWei Mi   Data = SavedData;
11066a14325dSWei Mi   return sampleprof_error::success;
11076a14325dSWei Mi }
11086a14325dSWei Mi 
110909dcfe68SWei Mi void SampleProfileReaderCompactBinary::collectFuncsFrom(const Module &M) {
1110d3289544SWenlei He   UseAllFuncs = false;
11116a14325dSWei Mi   FuncsToUse.clear();
111209dcfe68SWei Mi   for (auto &F : M)
111309dcfe68SWei Mi     FuncsToUse.insert(FunctionSamples::getCanonicalFnName(F));
11146a14325dSWei Mi }
11156a14325dSWei Mi 
111640ee23dbSEaswaran Raman std::error_code SampleProfileReaderBinary::readSummaryEntry(
111740ee23dbSEaswaran Raman     std::vector<ProfileSummaryEntry> &Entries) {
111840ee23dbSEaswaran Raman   auto Cutoff = readNumber<uint64_t>();
111940ee23dbSEaswaran Raman   if (std::error_code EC = Cutoff.getError())
112040ee23dbSEaswaran Raman     return EC;
112140ee23dbSEaswaran Raman 
112240ee23dbSEaswaran Raman   auto MinBlockCount = readNumber<uint64_t>();
112340ee23dbSEaswaran Raman   if (std::error_code EC = MinBlockCount.getError())
112440ee23dbSEaswaran Raman     return EC;
112540ee23dbSEaswaran Raman 
112640ee23dbSEaswaran Raman   auto NumBlocks = readNumber<uint64_t>();
112740ee23dbSEaswaran Raman   if (std::error_code EC = NumBlocks.getError())
112840ee23dbSEaswaran Raman     return EC;
112940ee23dbSEaswaran Raman 
113040ee23dbSEaswaran Raman   Entries.emplace_back(*Cutoff, *MinBlockCount, *NumBlocks);
113140ee23dbSEaswaran Raman   return sampleprof_error::success;
113240ee23dbSEaswaran Raman }
113340ee23dbSEaswaran Raman 
113440ee23dbSEaswaran Raman std::error_code SampleProfileReaderBinary::readSummary() {
113540ee23dbSEaswaran Raman   auto TotalCount = readNumber<uint64_t>();
113640ee23dbSEaswaran Raman   if (std::error_code EC = TotalCount.getError())
113740ee23dbSEaswaran Raman     return EC;
113840ee23dbSEaswaran Raman 
113940ee23dbSEaswaran Raman   auto MaxBlockCount = readNumber<uint64_t>();
114040ee23dbSEaswaran Raman   if (std::error_code EC = MaxBlockCount.getError())
114140ee23dbSEaswaran Raman     return EC;
114240ee23dbSEaswaran Raman 
114340ee23dbSEaswaran Raman   auto MaxFunctionCount = readNumber<uint64_t>();
114440ee23dbSEaswaran Raman   if (std::error_code EC = MaxFunctionCount.getError())
114540ee23dbSEaswaran Raman     return EC;
114640ee23dbSEaswaran Raman 
114740ee23dbSEaswaran Raman   auto NumBlocks = readNumber<uint64_t>();
114840ee23dbSEaswaran Raman   if (std::error_code EC = NumBlocks.getError())
114940ee23dbSEaswaran Raman     return EC;
115040ee23dbSEaswaran Raman 
115140ee23dbSEaswaran Raman   auto NumFunctions = readNumber<uint64_t>();
115240ee23dbSEaswaran Raman   if (std::error_code EC = NumFunctions.getError())
115340ee23dbSEaswaran Raman     return EC;
115440ee23dbSEaswaran Raman 
115540ee23dbSEaswaran Raman   auto NumSummaryEntries = readNumber<uint64_t>();
115640ee23dbSEaswaran Raman   if (std::error_code EC = NumSummaryEntries.getError())
115740ee23dbSEaswaran Raman     return EC;
115840ee23dbSEaswaran Raman 
115940ee23dbSEaswaran Raman   std::vector<ProfileSummaryEntry> Entries;
116040ee23dbSEaswaran Raman   for (unsigned i = 0; i < *NumSummaryEntries; i++) {
116140ee23dbSEaswaran Raman     std::error_code EC = readSummaryEntry(Entries);
116240ee23dbSEaswaran Raman     if (EC != sampleprof_error::success)
116340ee23dbSEaswaran Raman       return EC;
116440ee23dbSEaswaran Raman   }
11650eaee545SJonas Devlieghere   Summary = std::make_unique<ProfileSummary>(
11667cefdb81SEaswaran Raman       ProfileSummary::PSK_Sample, Entries, *TotalCount, *MaxBlockCount, 0,
11677cefdb81SEaswaran Raman       *MaxFunctionCount, *NumBlocks, *NumFunctions);
116840ee23dbSEaswaran Raman 
116940ee23dbSEaswaran Raman   return sampleprof_error::success;
117040ee23dbSEaswaran Raman }
117140ee23dbSEaswaran Raman 
1172a0c0857eSWei Mi bool SampleProfileReaderRawBinary::hasFormat(const MemoryBuffer &Buffer) {
1173c572e92cSDiego Novillo   const uint8_t *Data =
1174c572e92cSDiego Novillo       reinterpret_cast<const uint8_t *>(Buffer.getBufferStart());
1175c572e92cSDiego Novillo   uint64_t Magic = decodeULEB128(Data);
1176c572e92cSDiego Novillo   return Magic == SPMagic();
1177c572e92cSDiego Novillo }
1178c572e92cSDiego Novillo 
1179be907324SWei Mi bool SampleProfileReaderExtBinary::hasFormat(const MemoryBuffer &Buffer) {
1180be907324SWei Mi   const uint8_t *Data =
1181be907324SWei Mi       reinterpret_cast<const uint8_t *>(Buffer.getBufferStart());
1182be907324SWei Mi   uint64_t Magic = decodeULEB128(Data);
1183be907324SWei Mi   return Magic == SPMagic(SPF_Ext_Binary);
1184be907324SWei Mi }
1185be907324SWei Mi 
1186a0c0857eSWei Mi bool SampleProfileReaderCompactBinary::hasFormat(const MemoryBuffer &Buffer) {
1187a0c0857eSWei Mi   const uint8_t *Data =
1188a0c0857eSWei Mi       reinterpret_cast<const uint8_t *>(Buffer.getBufferStart());
1189a0c0857eSWei Mi   uint64_t Magic = decodeULEB128(Data);
1190a0c0857eSWei Mi   return Magic == SPMagic(SPF_Compact_Binary);
1191a0c0857eSWei Mi }
1192a0c0857eSWei Mi 
11933376a787SDiego Novillo std::error_code SampleProfileReaderGCC::skipNextWord() {
11943376a787SDiego Novillo   uint32_t dummy;
11953376a787SDiego Novillo   if (!GcovBuffer.readInt(dummy))
11963376a787SDiego Novillo     return sampleprof_error::truncated;
11973376a787SDiego Novillo   return sampleprof_error::success;
11983376a787SDiego Novillo }
11993376a787SDiego Novillo 
12003376a787SDiego Novillo template <typename T> ErrorOr<T> SampleProfileReaderGCC::readNumber() {
12013376a787SDiego Novillo   if (sizeof(T) <= sizeof(uint32_t)) {
12023376a787SDiego Novillo     uint32_t Val;
12033376a787SDiego Novillo     if (GcovBuffer.readInt(Val) && Val <= std::numeric_limits<T>::max())
12043376a787SDiego Novillo       return static_cast<T>(Val);
12053376a787SDiego Novillo   } else if (sizeof(T) <= sizeof(uint64_t)) {
12063376a787SDiego Novillo     uint64_t Val;
12073376a787SDiego Novillo     if (GcovBuffer.readInt64(Val) && Val <= std::numeric_limits<T>::max())
12083376a787SDiego Novillo       return static_cast<T>(Val);
12093376a787SDiego Novillo   }
12103376a787SDiego Novillo 
12113376a787SDiego Novillo   std::error_code EC = sampleprof_error::malformed;
12123376a787SDiego Novillo   reportError(0, EC.message());
12133376a787SDiego Novillo   return EC;
12143376a787SDiego Novillo }
12153376a787SDiego Novillo 
12163376a787SDiego Novillo ErrorOr<StringRef> SampleProfileReaderGCC::readString() {
12173376a787SDiego Novillo   StringRef Str;
12183376a787SDiego Novillo   if (!GcovBuffer.readString(Str))
12193376a787SDiego Novillo     return sampleprof_error::truncated;
12203376a787SDiego Novillo   return Str;
12213376a787SDiego Novillo }
12223376a787SDiego Novillo 
12233376a787SDiego Novillo std::error_code SampleProfileReaderGCC::readHeader() {
12243376a787SDiego Novillo   // Read the magic identifier.
12253376a787SDiego Novillo   if (!GcovBuffer.readGCDAFormat())
12263376a787SDiego Novillo     return sampleprof_error::unrecognized_format;
12273376a787SDiego Novillo 
12283376a787SDiego Novillo   // Read the version number. Note - the GCC reader does not validate this
12293376a787SDiego Novillo   // version, but the profile creator generates v704.
12303376a787SDiego Novillo   GCOV::GCOVVersion version;
12313376a787SDiego Novillo   if (!GcovBuffer.readGCOVVersion(version))
12323376a787SDiego Novillo     return sampleprof_error::unrecognized_format;
12333376a787SDiego Novillo 
12342d00eb17SFangrui Song   if (version != GCOV::V407)
12353376a787SDiego Novillo     return sampleprof_error::unsupported_version;
12363376a787SDiego Novillo 
12373376a787SDiego Novillo   // Skip the empty integer.
12383376a787SDiego Novillo   if (std::error_code EC = skipNextWord())
12393376a787SDiego Novillo     return EC;
12403376a787SDiego Novillo 
12413376a787SDiego Novillo   return sampleprof_error::success;
12423376a787SDiego Novillo }
12433376a787SDiego Novillo 
12443376a787SDiego Novillo std::error_code SampleProfileReaderGCC::readSectionTag(uint32_t Expected) {
12453376a787SDiego Novillo   uint32_t Tag;
12463376a787SDiego Novillo   if (!GcovBuffer.readInt(Tag))
12473376a787SDiego Novillo     return sampleprof_error::truncated;
12483376a787SDiego Novillo 
12493376a787SDiego Novillo   if (Tag != Expected)
12503376a787SDiego Novillo     return sampleprof_error::malformed;
12513376a787SDiego Novillo 
12523376a787SDiego Novillo   if (std::error_code EC = skipNextWord())
12533376a787SDiego Novillo     return EC;
12543376a787SDiego Novillo 
12553376a787SDiego Novillo   return sampleprof_error::success;
12563376a787SDiego Novillo }
12573376a787SDiego Novillo 
12583376a787SDiego Novillo std::error_code SampleProfileReaderGCC::readNameTable() {
12593376a787SDiego Novillo   if (std::error_code EC = readSectionTag(GCOVTagAFDOFileNames))
12603376a787SDiego Novillo     return EC;
12613376a787SDiego Novillo 
12623376a787SDiego Novillo   uint32_t Size;
12633376a787SDiego Novillo   if (!GcovBuffer.readInt(Size))
12643376a787SDiego Novillo     return sampleprof_error::truncated;
12653376a787SDiego Novillo 
12663376a787SDiego Novillo   for (uint32_t I = 0; I < Size; ++I) {
12673376a787SDiego Novillo     StringRef Str;
12683376a787SDiego Novillo     if (!GcovBuffer.readString(Str))
12693376a787SDiego Novillo       return sampleprof_error::truncated;
1270adcd0268SBenjamin Kramer     Names.push_back(std::string(Str));
12713376a787SDiego Novillo   }
12723376a787SDiego Novillo 
12733376a787SDiego Novillo   return sampleprof_error::success;
12743376a787SDiego Novillo }
12753376a787SDiego Novillo 
12763376a787SDiego Novillo std::error_code SampleProfileReaderGCC::readFunctionProfiles() {
12773376a787SDiego Novillo   if (std::error_code EC = readSectionTag(GCOVTagAFDOFunction))
12783376a787SDiego Novillo     return EC;
12793376a787SDiego Novillo 
12803376a787SDiego Novillo   uint32_t NumFunctions;
12813376a787SDiego Novillo   if (!GcovBuffer.readInt(NumFunctions))
12823376a787SDiego Novillo     return sampleprof_error::truncated;
12833376a787SDiego Novillo 
1284aae1ed8eSDiego Novillo   InlineCallStack Stack;
12853376a787SDiego Novillo   for (uint32_t I = 0; I < NumFunctions; ++I)
1286aae1ed8eSDiego Novillo     if (std::error_code EC = readOneFunctionProfile(Stack, true, 0))
12873376a787SDiego Novillo       return EC;
12883376a787SDiego Novillo 
128940ee23dbSEaswaran Raman   computeSummary();
12903376a787SDiego Novillo   return sampleprof_error::success;
12913376a787SDiego Novillo }
12923376a787SDiego Novillo 
1293aae1ed8eSDiego Novillo std::error_code SampleProfileReaderGCC::readOneFunctionProfile(
1294aae1ed8eSDiego Novillo     const InlineCallStack &InlineStack, bool Update, uint32_t Offset) {
12953376a787SDiego Novillo   uint64_t HeadCount = 0;
1296aae1ed8eSDiego Novillo   if (InlineStack.size() == 0)
12973376a787SDiego Novillo     if (!GcovBuffer.readInt64(HeadCount))
12983376a787SDiego Novillo       return sampleprof_error::truncated;
12993376a787SDiego Novillo 
13003376a787SDiego Novillo   uint32_t NameIdx;
13013376a787SDiego Novillo   if (!GcovBuffer.readInt(NameIdx))
13023376a787SDiego Novillo     return sampleprof_error::truncated;
13033376a787SDiego Novillo 
13043376a787SDiego Novillo   StringRef Name(Names[NameIdx]);
13053376a787SDiego Novillo 
13063376a787SDiego Novillo   uint32_t NumPosCounts;
13073376a787SDiego Novillo   if (!GcovBuffer.readInt(NumPosCounts))
13083376a787SDiego Novillo     return sampleprof_error::truncated;
13093376a787SDiego Novillo 
1310aae1ed8eSDiego Novillo   uint32_t NumCallsites;
1311aae1ed8eSDiego Novillo   if (!GcovBuffer.readInt(NumCallsites))
13123376a787SDiego Novillo     return sampleprof_error::truncated;
13133376a787SDiego Novillo 
1314aae1ed8eSDiego Novillo   FunctionSamples *FProfile = nullptr;
1315aae1ed8eSDiego Novillo   if (InlineStack.size() == 0) {
1316aae1ed8eSDiego Novillo     // If this is a top function that we have already processed, do not
1317aae1ed8eSDiego Novillo     // update its profile again.  This happens in the presence of
1318aae1ed8eSDiego Novillo     // function aliases.  Since these aliases share the same function
1319aae1ed8eSDiego Novillo     // body, there will be identical replicated profiles for the
1320aae1ed8eSDiego Novillo     // original function.  In this case, we simply not bother updating
1321aae1ed8eSDiego Novillo     // the profile of the original function.
1322aae1ed8eSDiego Novillo     FProfile = &Profiles[Name];
1323aae1ed8eSDiego Novillo     FProfile->addHeadSamples(HeadCount);
1324aae1ed8eSDiego Novillo     if (FProfile->getTotalSamples() > 0)
13253376a787SDiego Novillo       Update = false;
1326aae1ed8eSDiego Novillo   } else {
1327aae1ed8eSDiego Novillo     // Otherwise, we are reading an inlined instance. The top of the
1328aae1ed8eSDiego Novillo     // inline stack contains the profile of the caller. Insert this
1329aae1ed8eSDiego Novillo     // callee in the caller's CallsiteMap.
1330aae1ed8eSDiego Novillo     FunctionSamples *CallerProfile = InlineStack.front();
1331aae1ed8eSDiego Novillo     uint32_t LineOffset = Offset >> 16;
1332aae1ed8eSDiego Novillo     uint32_t Discriminator = Offset & 0xffff;
1333aae1ed8eSDiego Novillo     FProfile = &CallerProfile->functionSamplesAt(
1334adcd0268SBenjamin Kramer         LineLocation(LineOffset, Discriminator))[std::string(Name)];
13353376a787SDiego Novillo   }
133657d1dda5SDehao Chen   FProfile->setName(Name);
13373376a787SDiego Novillo 
13383376a787SDiego Novillo   for (uint32_t I = 0; I < NumPosCounts; ++I) {
13393376a787SDiego Novillo     uint32_t Offset;
13403376a787SDiego Novillo     if (!GcovBuffer.readInt(Offset))
13413376a787SDiego Novillo       return sampleprof_error::truncated;
13423376a787SDiego Novillo 
13433376a787SDiego Novillo     uint32_t NumTargets;
13443376a787SDiego Novillo     if (!GcovBuffer.readInt(NumTargets))
13453376a787SDiego Novillo       return sampleprof_error::truncated;
13463376a787SDiego Novillo 
13473376a787SDiego Novillo     uint64_t Count;
13483376a787SDiego Novillo     if (!GcovBuffer.readInt64(Count))
13493376a787SDiego Novillo       return sampleprof_error::truncated;
13503376a787SDiego Novillo 
1351aae1ed8eSDiego Novillo     // The line location is encoded in the offset as:
1352aae1ed8eSDiego Novillo     //   high 16 bits: line offset to the start of the function.
1353aae1ed8eSDiego Novillo     //   low 16 bits: discriminator.
1354aae1ed8eSDiego Novillo     uint32_t LineOffset = Offset >> 16;
1355aae1ed8eSDiego Novillo     uint32_t Discriminator = Offset & 0xffff;
13563376a787SDiego Novillo 
1357aae1ed8eSDiego Novillo     InlineCallStack NewStack;
1358aae1ed8eSDiego Novillo     NewStack.push_back(FProfile);
13591d0bc055SKazu Hirata     llvm::append_range(NewStack, InlineStack);
1360aae1ed8eSDiego Novillo     if (Update) {
1361aae1ed8eSDiego Novillo       // Walk up the inline stack, adding the samples on this line to
1362aae1ed8eSDiego Novillo       // the total sample count of the callers in the chain.
1363aae1ed8eSDiego Novillo       for (auto CallerProfile : NewStack)
1364aae1ed8eSDiego Novillo         CallerProfile->addTotalSamples(Count);
1365aae1ed8eSDiego Novillo 
1366aae1ed8eSDiego Novillo       // Update the body samples for the current profile.
1367aae1ed8eSDiego Novillo       FProfile->addBodySamples(LineOffset, Discriminator, Count);
1368aae1ed8eSDiego Novillo     }
1369aae1ed8eSDiego Novillo 
1370aae1ed8eSDiego Novillo     // Process the list of functions called at an indirect call site.
1371aae1ed8eSDiego Novillo     // These are all the targets that a function pointer (or virtual
1372aae1ed8eSDiego Novillo     // function) resolved at runtime.
13733376a787SDiego Novillo     for (uint32_t J = 0; J < NumTargets; J++) {
13743376a787SDiego Novillo       uint32_t HistVal;
13753376a787SDiego Novillo       if (!GcovBuffer.readInt(HistVal))
13763376a787SDiego Novillo         return sampleprof_error::truncated;
13773376a787SDiego Novillo 
13783376a787SDiego Novillo       if (HistVal != HIST_TYPE_INDIR_CALL_TOPN)
13793376a787SDiego Novillo         return sampleprof_error::malformed;
13803376a787SDiego Novillo 
13813376a787SDiego Novillo       uint64_t TargetIdx;
13823376a787SDiego Novillo       if (!GcovBuffer.readInt64(TargetIdx))
13833376a787SDiego Novillo         return sampleprof_error::truncated;
13843376a787SDiego Novillo       StringRef TargetName(Names[TargetIdx]);
13853376a787SDiego Novillo 
13863376a787SDiego Novillo       uint64_t TargetCount;
13873376a787SDiego Novillo       if (!GcovBuffer.readInt64(TargetCount))
13883376a787SDiego Novillo         return sampleprof_error::truncated;
13893376a787SDiego Novillo 
1390920677a9SDehao Chen       if (Update)
1391920677a9SDehao Chen         FProfile->addCalledTargetSamples(LineOffset, Discriminator,
1392aae1ed8eSDiego Novillo                                          TargetName, TargetCount);
13933376a787SDiego Novillo     }
13943376a787SDiego Novillo   }
13953376a787SDiego Novillo 
1396aae1ed8eSDiego Novillo   // Process all the inlined callers into the current function. These
1397aae1ed8eSDiego Novillo   // are all the callsites that were inlined into this function.
1398aae1ed8eSDiego Novillo   for (uint32_t I = 0; I < NumCallsites; I++) {
13993376a787SDiego Novillo     // The offset is encoded as:
14003376a787SDiego Novillo     //   high 16 bits: line offset to the start of the function.
14013376a787SDiego Novillo     //   low 16 bits: discriminator.
14023376a787SDiego Novillo     uint32_t Offset;
14033376a787SDiego Novillo     if (!GcovBuffer.readInt(Offset))
14043376a787SDiego Novillo       return sampleprof_error::truncated;
1405aae1ed8eSDiego Novillo     InlineCallStack NewStack;
1406aae1ed8eSDiego Novillo     NewStack.push_back(FProfile);
14071d0bc055SKazu Hirata     llvm::append_range(NewStack, InlineStack);
1408aae1ed8eSDiego Novillo     if (std::error_code EC = readOneFunctionProfile(NewStack, Update, Offset))
14093376a787SDiego Novillo       return EC;
14103376a787SDiego Novillo   }
14113376a787SDiego Novillo 
14123376a787SDiego Novillo   return sampleprof_error::success;
14133376a787SDiego Novillo }
14143376a787SDiego Novillo 
14155f8f34e4SAdrian Prantl /// Read a GCC AutoFDO profile.
14163376a787SDiego Novillo ///
14173376a787SDiego Novillo /// This format is generated by the Linux Perf conversion tool at
14183376a787SDiego Novillo /// https://github.com/google/autofdo.
14198c8ec1f6SWei Mi std::error_code SampleProfileReaderGCC::readImpl() {
14203376a787SDiego Novillo   // Read the string table.
14213376a787SDiego Novillo   if (std::error_code EC = readNameTable())
14223376a787SDiego Novillo     return EC;
14233376a787SDiego Novillo 
14243376a787SDiego Novillo   // Read the source profile.
14253376a787SDiego Novillo   if (std::error_code EC = readFunctionProfiles())
14263376a787SDiego Novillo     return EC;
14273376a787SDiego Novillo 
14283376a787SDiego Novillo   return sampleprof_error::success;
14293376a787SDiego Novillo }
14303376a787SDiego Novillo 
14313376a787SDiego Novillo bool SampleProfileReaderGCC::hasFormat(const MemoryBuffer &Buffer) {
14323376a787SDiego Novillo   StringRef Magic(reinterpret_cast<const char *>(Buffer.getBufferStart()));
14333376a787SDiego Novillo   return Magic == "adcg*704";
14343376a787SDiego Novillo }
14353376a787SDiego Novillo 
14368c8ec1f6SWei Mi void SampleProfileReaderItaniumRemapper::applyRemapping(LLVMContext &Ctx) {
1437ebad6788SWei Mi   // If the reader uses MD5 to represent string, we can't remap it because
143828436358SRichard Smith   // we don't know what the original function names were.
1439ebad6788SWei Mi   if (Reader.useMD5()) {
144028436358SRichard Smith     Ctx.diagnose(DiagnosticInfoSampleProfile(
14418c8ec1f6SWei Mi         Reader.getBuffer()->getBufferIdentifier(),
144228436358SRichard Smith         "Profile data remapping cannot be applied to profile data "
144328436358SRichard Smith         "in compact format (original mangled names are not available).",
144428436358SRichard Smith         DS_Warning));
14458c8ec1f6SWei Mi     return;
144628436358SRichard Smith   }
144728436358SRichard Smith 
14486b989a17SWenlei He   // CSSPGO-TODO: Remapper is not yet supported.
14496b989a17SWenlei He   // We will need to remap the entire context string.
14508c8ec1f6SWei Mi   assert(Remappings && "should be initialized while creating remapper");
1451c67ccf5fSWei Mi   for (auto &Sample : Reader.getProfiles()) {
1452c67ccf5fSWei Mi     DenseSet<StringRef> NamesInSample;
1453c67ccf5fSWei Mi     Sample.second.findAllNames(NamesInSample);
1454c67ccf5fSWei Mi     for (auto &Name : NamesInSample)
1455c67ccf5fSWei Mi       if (auto Key = Remappings->insert(Name))
1456c67ccf5fSWei Mi         NameMap.insert({Key, Name});
1457c67ccf5fSWei Mi   }
145828436358SRichard Smith 
14598c8ec1f6SWei Mi   RemappingApplied = true;
146028436358SRichard Smith }
146128436358SRichard Smith 
1462c67ccf5fSWei Mi Optional<StringRef>
1463c67ccf5fSWei Mi SampleProfileReaderItaniumRemapper::lookUpNameInProfile(StringRef Fname) {
14648c8ec1f6SWei Mi   if (auto Key = Remappings->lookup(Fname))
1465c67ccf5fSWei Mi     return NameMap.lookup(Key);
1466c67ccf5fSWei Mi   return None;
146728436358SRichard Smith }
146828436358SRichard Smith 
14695f8f34e4SAdrian Prantl /// Prepare a memory buffer for the contents of \p Filename.
1470de1ab26fSDiego Novillo ///
1471c572e92cSDiego Novillo /// \returns an error code indicating the status of the buffer.
1472fcd55607SDiego Novillo static ErrorOr<std::unique_ptr<MemoryBuffer>>
14730da23a27SBenjamin Kramer setupMemoryBuffer(const Twine &Filename) {
1474c572e92cSDiego Novillo   auto BufferOrErr = MemoryBuffer::getFileOrSTDIN(Filename);
1475c572e92cSDiego Novillo   if (std::error_code EC = BufferOrErr.getError())
1476c572e92cSDiego Novillo     return EC;
1477fcd55607SDiego Novillo   auto Buffer = std::move(BufferOrErr.get());
1478c572e92cSDiego Novillo 
1479c572e92cSDiego Novillo   // Sanity check the file.
1480260fe3ecSZachary Turner   if (uint64_t(Buffer->getBufferSize()) > std::numeric_limits<uint32_t>::max())
1481c572e92cSDiego Novillo     return sampleprof_error::too_large;
1482c572e92cSDiego Novillo 
1483c55cf4afSBill Wendling   return std::move(Buffer);
1484c572e92cSDiego Novillo }
1485c572e92cSDiego Novillo 
14865f8f34e4SAdrian Prantl /// Create a sample profile reader based on the format of the input file.
1487c572e92cSDiego Novillo ///
1488c572e92cSDiego Novillo /// \param Filename The file to open.
1489c572e92cSDiego Novillo ///
1490c572e92cSDiego Novillo /// \param C The LLVM context to use to emit diagnostics.
1491c572e92cSDiego Novillo ///
14928c8ec1f6SWei Mi /// \param RemapFilename The file used for profile remapping.
14938c8ec1f6SWei Mi ///
1494c572e92cSDiego Novillo /// \returns an error code indicating the status of the created reader.
1495fcd55607SDiego Novillo ErrorOr<std::unique_ptr<SampleProfileReader>>
14968c8ec1f6SWei Mi SampleProfileReader::create(const std::string Filename, LLVMContext &C,
14978c8ec1f6SWei Mi                             const std::string RemapFilename) {
1498fcd55607SDiego Novillo   auto BufferOrError = setupMemoryBuffer(Filename);
1499fcd55607SDiego Novillo   if (std::error_code EC = BufferOrError.getError())
1500c572e92cSDiego Novillo     return EC;
15018c8ec1f6SWei Mi   return create(BufferOrError.get(), C, RemapFilename);
150251abea74SNathan Slingerland }
1503c572e92cSDiego Novillo 
150428436358SRichard Smith /// Create a sample profile remapper from the given input, to remap the
150528436358SRichard Smith /// function names in the given profile data.
150628436358SRichard Smith ///
150728436358SRichard Smith /// \param Filename The file to open.
150828436358SRichard Smith ///
15098c8ec1f6SWei Mi /// \param Reader The profile reader the remapper is going to be applied to.
15108c8ec1f6SWei Mi ///
151128436358SRichard Smith /// \param C The LLVM context to use to emit diagnostics.
151228436358SRichard Smith ///
151328436358SRichard Smith /// \returns an error code indicating the status of the created reader.
15148c8ec1f6SWei Mi ErrorOr<std::unique_ptr<SampleProfileReaderItaniumRemapper>>
15158c8ec1f6SWei Mi SampleProfileReaderItaniumRemapper::create(const std::string Filename,
15168c8ec1f6SWei Mi                                            SampleProfileReader &Reader,
15178c8ec1f6SWei Mi                                            LLVMContext &C) {
151828436358SRichard Smith   auto BufferOrError = setupMemoryBuffer(Filename);
151928436358SRichard Smith   if (std::error_code EC = BufferOrError.getError())
152028436358SRichard Smith     return EC;
15218c8ec1f6SWei Mi   return create(BufferOrError.get(), Reader, C);
15228c8ec1f6SWei Mi }
15238c8ec1f6SWei Mi 
15248c8ec1f6SWei Mi /// Create a sample profile remapper from the given input, to remap the
15258c8ec1f6SWei Mi /// function names in the given profile data.
15268c8ec1f6SWei Mi ///
15278c8ec1f6SWei Mi /// \param B The memory buffer to create the reader from (assumes ownership).
15288c8ec1f6SWei Mi ///
15298c8ec1f6SWei Mi /// \param C The LLVM context to use to emit diagnostics.
15308c8ec1f6SWei Mi ///
15318c8ec1f6SWei Mi /// \param Reader The profile reader the remapper is going to be applied to.
15328c8ec1f6SWei Mi ///
15338c8ec1f6SWei Mi /// \returns an error code indicating the status of the created reader.
15348c8ec1f6SWei Mi ErrorOr<std::unique_ptr<SampleProfileReaderItaniumRemapper>>
15358c8ec1f6SWei Mi SampleProfileReaderItaniumRemapper::create(std::unique_ptr<MemoryBuffer> &B,
15368c8ec1f6SWei Mi                                            SampleProfileReader &Reader,
15378c8ec1f6SWei Mi                                            LLVMContext &C) {
15388c8ec1f6SWei Mi   auto Remappings = std::make_unique<SymbolRemappingReader>();
15398c8ec1f6SWei Mi   if (Error E = Remappings->read(*B.get())) {
15408c8ec1f6SWei Mi     handleAllErrors(
15418c8ec1f6SWei Mi         std::move(E), [&](const SymbolRemappingParseError &ParseError) {
15428c8ec1f6SWei Mi           C.diagnose(DiagnosticInfoSampleProfile(B->getBufferIdentifier(),
15438c8ec1f6SWei Mi                                                  ParseError.getLineNum(),
15448c8ec1f6SWei Mi                                                  ParseError.getMessage()));
15458c8ec1f6SWei Mi         });
15468c8ec1f6SWei Mi     return sampleprof_error::malformed;
15478c8ec1f6SWei Mi   }
15488c8ec1f6SWei Mi 
15490eaee545SJonas Devlieghere   return std::make_unique<SampleProfileReaderItaniumRemapper>(
15508c8ec1f6SWei Mi       std::move(B), std::move(Remappings), Reader);
155128436358SRichard Smith }
155228436358SRichard Smith 
15535f8f34e4SAdrian Prantl /// Create a sample profile reader based on the format of the input data.
155451abea74SNathan Slingerland ///
155551abea74SNathan Slingerland /// \param B The memory buffer to create the reader from (assumes ownership).
155651abea74SNathan Slingerland ///
155751abea74SNathan Slingerland /// \param C The LLVM context to use to emit diagnostics.
155851abea74SNathan Slingerland ///
15598c8ec1f6SWei Mi /// \param RemapFilename The file used for profile remapping.
15608c8ec1f6SWei Mi ///
156151abea74SNathan Slingerland /// \returns an error code indicating the status of the created reader.
156251abea74SNathan Slingerland ErrorOr<std::unique_ptr<SampleProfileReader>>
15638c8ec1f6SWei Mi SampleProfileReader::create(std::unique_ptr<MemoryBuffer> &B, LLVMContext &C,
15648c8ec1f6SWei Mi                             const std::string RemapFilename) {
1565fcd55607SDiego Novillo   std::unique_ptr<SampleProfileReader> Reader;
1566a0c0857eSWei Mi   if (SampleProfileReaderRawBinary::hasFormat(*B))
1567a0c0857eSWei Mi     Reader.reset(new SampleProfileReaderRawBinary(std::move(B), C));
1568be907324SWei Mi   else if (SampleProfileReaderExtBinary::hasFormat(*B))
1569be907324SWei Mi     Reader.reset(new SampleProfileReaderExtBinary(std::move(B), C));
1570a0c0857eSWei Mi   else if (SampleProfileReaderCompactBinary::hasFormat(*B))
1571a0c0857eSWei Mi     Reader.reset(new SampleProfileReaderCompactBinary(std::move(B), C));
157251abea74SNathan Slingerland   else if (SampleProfileReaderGCC::hasFormat(*B))
157351abea74SNathan Slingerland     Reader.reset(new SampleProfileReaderGCC(std::move(B), C));
157451abea74SNathan Slingerland   else if (SampleProfileReaderText::hasFormat(*B))
157551abea74SNathan Slingerland     Reader.reset(new SampleProfileReaderText(std::move(B), C));
15764f823667SNathan Slingerland   else
15774f823667SNathan Slingerland     return sampleprof_error::unrecognized_format;
1578c572e92cSDiego Novillo 
15798c8ec1f6SWei Mi   if (!RemapFilename.empty()) {
15808c8ec1f6SWei Mi     auto ReaderOrErr =
15818c8ec1f6SWei Mi         SampleProfileReaderItaniumRemapper::create(RemapFilename, *Reader, C);
15828c8ec1f6SWei Mi     if (std::error_code EC = ReaderOrErr.getError()) {
15838c8ec1f6SWei Mi       std::string Msg = "Could not create remapper: " + EC.message();
15848c8ec1f6SWei Mi       C.diagnose(DiagnosticInfoSampleProfile(RemapFilename, Msg));
15858c8ec1f6SWei Mi       return EC;
15868c8ec1f6SWei Mi     }
15878c8ec1f6SWei Mi     Reader->Remapper = std::move(ReaderOrErr.get());
15888c8ec1f6SWei Mi   }
15898c8ec1f6SWei Mi 
159094d44c97SWei Mi   FunctionSamples::Format = Reader->getFormat();
1591be907324SWei Mi   if (std::error_code EC = Reader->readHeader()) {
1592fcd55607SDiego Novillo     return EC;
1593be907324SWei Mi   }
1594fcd55607SDiego Novillo 
1595c55cf4afSBill Wendling   return std::move(Reader);
1596de1ab26fSDiego Novillo }
159740ee23dbSEaswaran Raman 
159840ee23dbSEaswaran Raman // For text and GCC file formats, we compute the summary after reading the
159940ee23dbSEaswaran Raman // profile. Binary format has the profile summary in its header.
160040ee23dbSEaswaran Raman void SampleProfileReader::computeSummary() {
1601e5a17e3fSEaswaran Raman   SampleProfileSummaryBuilder Builder(ProfileSummaryBuilder::DefaultCutoffs);
160240ee23dbSEaswaran Raman   for (const auto &I : Profiles) {
160340ee23dbSEaswaran Raman     const FunctionSamples &Profile = I.second;
1604e5a17e3fSEaswaran Raman     Builder.addRecord(Profile);
160540ee23dbSEaswaran Raman   }
160638de59e4SBenjamin Kramer   Summary = Builder.getSummary();
160740ee23dbSEaswaran Raman }
1608