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"
26fc97efa4Sserge-sans-paille #include "llvm/IR/Module.h"
27e78d131aSEugene Zelenko #include "llvm/IR/ProfileSummary.h"
28e78d131aSEugene Zelenko #include "llvm/ProfileData/ProfileCommon.h"
29e78d131aSEugene Zelenko #include "llvm/ProfileData/SampleProf.h"
306745ffe4SRong Xu #include "llvm/Support/CommandLine.h"
31b523790aSWei Mi #include "llvm/Support/Compression.h"
32de1ab26fSDiego Novillo #include "llvm/Support/ErrorOr.h"
33c572e92cSDiego Novillo #include "llvm/Support/LEB128.h"
34de1ab26fSDiego Novillo #include "llvm/Support/LineIterator.h"
356a14325dSWei Mi #include "llvm/Support/MD5.h"
36c572e92cSDiego Novillo #include "llvm/Support/MemoryBuffer.h"
37e78d131aSEugene Zelenko #include "llvm/Support/raw_ostream.h"
38e78d131aSEugene Zelenko #include <algorithm>
39e78d131aSEugene Zelenko #include <cstddef>
40e78d131aSEugene Zelenko #include <cstdint>
41e78d131aSEugene Zelenko #include <limits>
42e78d131aSEugene Zelenko #include <memory>
43e78d131aSEugene Zelenko #include <system_error>
44e78d131aSEugene Zelenko #include <vector>
45de1ab26fSDiego Novillo
46de1ab26fSDiego Novillo using namespace llvm;
47e78d131aSEugene Zelenko using namespace sampleprof;
48de1ab26fSDiego Novillo
496745ffe4SRong Xu #define DEBUG_TYPE "samplepgo-reader"
506745ffe4SRong Xu
516745ffe4SRong Xu // This internal option specifies if the profile uses FS discriminators.
526745ffe4SRong Xu // It only applies to text, binary and compact binary format profiles.
536745ffe4SRong Xu // For ext-binary format profiles, the flag is set in the summary.
546745ffe4SRong Xu static cl::opt<bool> ProfileIsFSDisciminator(
556745ffe4SRong Xu "profile-isfs", cl::Hidden, cl::init(false),
569b8425e4SRong Xu cl::desc("Profile uses flow sensitive discriminators"));
576745ffe4SRong Xu
585f8f34e4SAdrian Prantl /// Dump the function profile for \p FName.
59de1ab26fSDiego Novillo ///
60cb8d96e7SSimon Pilgrim /// \param FContext Name + context of the function to print.
61d5336ae2SDiego Novillo /// \param OS Stream to emit the output to.
dumpFunctionProfile(SampleContext FContext,raw_ostream & OS)62b9db7036SHongtao Yu void SampleProfileReader::dumpFunctionProfile(SampleContext FContext,
63d5336ae2SDiego Novillo raw_ostream &OS) {
64b9db7036SHongtao Yu OS << "Function: " << FContext.toString() << ": " << Profiles[FContext];
65de1ab26fSDiego Novillo }
66de1ab26fSDiego Novillo
675f8f34e4SAdrian Prantl /// Dump all the function profiles found on stream \p OS.
dump(raw_ostream & OS)68d5336ae2SDiego Novillo void SampleProfileReader::dump(raw_ostream &OS) {
69f27fee62SHongtao Yu std::vector<NameFunctionSamples> V;
70f27fee62SHongtao Yu sortFuncProfiles(Profiles, V);
71f27fee62SHongtao Yu for (const auto &I : V)
72f27fee62SHongtao Yu dumpFunctionProfile(I.first, OS);
73de1ab26fSDiego Novillo }
74de1ab26fSDiego Novillo
755f8f34e4SAdrian Prantl /// Parse \p Input as function head.
766722688eSDehao Chen ///
776722688eSDehao Chen /// Parse one line of \p Input, and update function name in \p FName,
786722688eSDehao Chen /// function's total sample count in \p NumSamples, function's entry
796722688eSDehao Chen /// count in \p NumHeadSamples.
806722688eSDehao Chen ///
816722688eSDehao Chen /// \returns true if parsing is successful.
ParseHead(const StringRef & Input,StringRef & FName,uint64_t & NumSamples,uint64_t & NumHeadSamples)826722688eSDehao Chen static bool ParseHead(const StringRef &Input, StringRef &FName,
8338be3330SDiego Novillo uint64_t &NumSamples, uint64_t &NumHeadSamples) {
846722688eSDehao Chen if (Input[0] == ' ')
856722688eSDehao Chen return false;
866722688eSDehao Chen size_t n2 = Input.rfind(':');
876722688eSDehao Chen size_t n1 = Input.rfind(':', n2 - 1);
886722688eSDehao Chen FName = Input.substr(0, n1);
896722688eSDehao Chen if (Input.substr(n1 + 1, n2 - n1 - 1).getAsInteger(10, NumSamples))
906722688eSDehao Chen return false;
916722688eSDehao Chen if (Input.substr(n2 + 1).getAsInteger(10, NumHeadSamples))
926722688eSDehao Chen return false;
936722688eSDehao Chen return true;
946722688eSDehao Chen }
956722688eSDehao Chen
965f8f34e4SAdrian Prantl /// Returns true if line offset \p L is legal (only has 16 bits).
isOffsetLegal(unsigned L)9757d1dda5SDehao Chen static bool isOffsetLegal(unsigned L) { return (L & 0xffff) == L; }
9810042412SDehao Chen
99ac068e01SHongtao Yu /// Parse \p Input that contains metadata.
100ac068e01SHongtao Yu /// Possible metadata:
101ac068e01SHongtao Yu /// - CFG Checksum information:
102ac068e01SHongtao Yu /// !CFGChecksum: 12345
1031410db70SWenlei He /// - CFG Checksum information:
1041410db70SWenlei He /// !Attributes: 1
105ac068e01SHongtao Yu /// Stores the FunctionHash (a.k.a. CFG Checksum) into \p FunctionHash.
parseMetadata(const StringRef & Input,uint64_t & FunctionHash,uint32_t & Attributes)1061410db70SWenlei He static bool parseMetadata(const StringRef &Input, uint64_t &FunctionHash,
1071410db70SWenlei He uint32_t &Attributes) {
1081410db70SWenlei He if (Input.startswith("!CFGChecksum:")) {
109ac068e01SHongtao Yu StringRef CFGInfo = Input.substr(strlen("!CFGChecksum:")).trim();
110ac068e01SHongtao Yu return !CFGInfo.getAsInteger(10, FunctionHash);
111ac068e01SHongtao Yu }
112ac068e01SHongtao Yu
1131410db70SWenlei He if (Input.startswith("!Attributes:")) {
1141410db70SWenlei He StringRef Attrib = Input.substr(strlen("!Attributes:")).trim();
1151410db70SWenlei He return !Attrib.getAsInteger(10, Attributes);
1161410db70SWenlei He }
1171410db70SWenlei He
1181410db70SWenlei He return false;
1191410db70SWenlei He }
1201410db70SWenlei He
121ac068e01SHongtao Yu enum class LineType {
122ac068e01SHongtao Yu CallSiteProfile,
123ac068e01SHongtao Yu BodyProfile,
124ac068e01SHongtao Yu Metadata,
125ac068e01SHongtao Yu };
126ac068e01SHongtao Yu
1275f8f34e4SAdrian Prantl /// Parse \p Input as line sample.
1286722688eSDehao Chen ///
1296722688eSDehao Chen /// \param Input input line.
130ac068e01SHongtao Yu /// \param LineTy Type of this line.
1316722688eSDehao Chen /// \param Depth the depth of the inline stack.
1326722688eSDehao Chen /// \param NumSamples total samples of the line/inlined callsite.
1336722688eSDehao Chen /// \param LineOffset line offset to the start of the function.
1346722688eSDehao Chen /// \param Discriminator discriminator of the line.
1356722688eSDehao Chen /// \param TargetCountMap map from indirect call target to count.
136ac068e01SHongtao Yu /// \param FunctionHash the function's CFG hash, used by pseudo probe.
1376722688eSDehao Chen ///
1386722688eSDehao Chen /// returns true if parsing is successful.
ParseLine(const StringRef & Input,LineType & LineTy,uint32_t & Depth,uint64_t & NumSamples,uint32_t & LineOffset,uint32_t & Discriminator,StringRef & CalleeName,DenseMap<StringRef,uint64_t> & TargetCountMap,uint64_t & FunctionHash,uint32_t & Attributes)139ac068e01SHongtao Yu static bool ParseLine(const StringRef &Input, LineType &LineTy, uint32_t &Depth,
14038be3330SDiego Novillo uint64_t &NumSamples, uint32_t &LineOffset,
14138be3330SDiego Novillo uint32_t &Discriminator, StringRef &CalleeName,
142ac068e01SHongtao Yu DenseMap<StringRef, uint64_t> &TargetCountMap,
1431410db70SWenlei He uint64_t &FunctionHash, uint32_t &Attributes) {
1446722688eSDehao Chen for (Depth = 0; Input[Depth] == ' '; Depth++)
1456722688eSDehao Chen ;
1466722688eSDehao Chen if (Depth == 0)
1476722688eSDehao Chen return false;
1486722688eSDehao Chen
1495740bb80SHongtao Yu if (Input[Depth] == '!') {
150ac068e01SHongtao Yu LineTy = LineType::Metadata;
1511410db70SWenlei He return parseMetadata(Input.substr(Depth), FunctionHash, Attributes);
152ac068e01SHongtao Yu }
153ac068e01SHongtao Yu
1546722688eSDehao Chen size_t n1 = Input.find(':');
1556722688eSDehao Chen StringRef Loc = Input.substr(Depth, n1 - Depth);
1566722688eSDehao Chen size_t n2 = Loc.find('.');
1576722688eSDehao Chen if (n2 == StringRef::npos) {
15810042412SDehao Chen if (Loc.getAsInteger(10, LineOffset) || !isOffsetLegal(LineOffset))
1596722688eSDehao Chen return false;
1606722688eSDehao Chen Discriminator = 0;
1616722688eSDehao Chen } else {
1626722688eSDehao Chen if (Loc.substr(0, n2).getAsInteger(10, LineOffset))
1636722688eSDehao Chen return false;
1646722688eSDehao Chen if (Loc.substr(n2 + 1).getAsInteger(10, Discriminator))
1656722688eSDehao Chen return false;
1666722688eSDehao Chen }
1676722688eSDehao Chen
1686722688eSDehao Chen StringRef Rest = Input.substr(n1 + 2);
169551aaa24SKazu Hirata if (isDigit(Rest[0])) {
170ac068e01SHongtao Yu LineTy = LineType::BodyProfile;
1716722688eSDehao Chen size_t n3 = Rest.find(' ');
1726722688eSDehao Chen if (n3 == StringRef::npos) {
1736722688eSDehao Chen if (Rest.getAsInteger(10, NumSamples))
1746722688eSDehao Chen return false;
1756722688eSDehao Chen } else {
1766722688eSDehao Chen if (Rest.substr(0, n3).getAsInteger(10, NumSamples))
1776722688eSDehao Chen return false;
1786722688eSDehao Chen }
179984ab0f1SWei Mi // Find call targets and their sample counts.
180984ab0f1SWei Mi // Note: In some cases, there are symbols in the profile which are not
181984ab0f1SWei Mi // mangled. To accommodate such cases, use colon + integer pairs as the
182984ab0f1SWei Mi // anchor points.
183984ab0f1SWei Mi // An example:
184984ab0f1SWei Mi // _M_construct<char *>:1000 string_view<std::allocator<char> >:437
185984ab0f1SWei Mi // ":1000" and ":437" are used as anchor points so the string above will
186984ab0f1SWei Mi // be interpreted as
187984ab0f1SWei Mi // target: _M_construct<char *>
188984ab0f1SWei Mi // count: 1000
189984ab0f1SWei Mi // target: string_view<std::allocator<char> >
190984ab0f1SWei Mi // count: 437
1916722688eSDehao Chen while (n3 != StringRef::npos) {
1926722688eSDehao Chen n3 += Rest.substr(n3).find_first_not_of(' ');
1936722688eSDehao Chen Rest = Rest.substr(n3);
194984ab0f1SWei Mi n3 = Rest.find_first_of(':');
195984ab0f1SWei Mi if (n3 == StringRef::npos || n3 == 0)
1966722688eSDehao Chen return false;
197984ab0f1SWei Mi
198984ab0f1SWei Mi StringRef Target;
199984ab0f1SWei Mi uint64_t count, n4;
200984ab0f1SWei Mi while (true) {
201984ab0f1SWei Mi // Get the segment after the current colon.
202984ab0f1SWei Mi StringRef AfterColon = Rest.substr(n3 + 1);
203984ab0f1SWei Mi // Get the target symbol before the current colon.
204984ab0f1SWei Mi Target = Rest.substr(0, n3);
205984ab0f1SWei Mi // Check if the word after the current colon is an integer.
206984ab0f1SWei Mi n4 = AfterColon.find_first_of(' ');
207984ab0f1SWei Mi n4 = (n4 != StringRef::npos) ? n3 + n4 + 1 : Rest.size();
208984ab0f1SWei Mi StringRef WordAfterColon = Rest.substr(n3 + 1, n4 - n3 - 1);
209984ab0f1SWei Mi if (!WordAfterColon.getAsInteger(10, count))
210984ab0f1SWei Mi break;
211984ab0f1SWei Mi
212984ab0f1SWei Mi // Try to find the next colon.
213984ab0f1SWei Mi uint64_t n5 = AfterColon.find_first_of(':');
214984ab0f1SWei Mi if (n5 == StringRef::npos)
215984ab0f1SWei Mi return false;
216984ab0f1SWei Mi n3 += n5 + 1;
217984ab0f1SWei Mi }
218984ab0f1SWei Mi
219984ab0f1SWei Mi // An anchor point is found. Save the {target, count} pair
220984ab0f1SWei Mi TargetCountMap[Target] = count;
221984ab0f1SWei Mi if (n4 == Rest.size())
222984ab0f1SWei Mi break;
223984ab0f1SWei Mi // Change n3 to the next blank space after colon + integer pair.
224984ab0f1SWei Mi n3 = n4;
2256722688eSDehao Chen }
2266722688eSDehao Chen } else {
227ac068e01SHongtao Yu LineTy = LineType::CallSiteProfile;
22838be3330SDiego Novillo size_t n3 = Rest.find_last_of(':');
2296722688eSDehao Chen CalleeName = Rest.substr(0, n3);
2306722688eSDehao Chen if (Rest.substr(n3 + 1).getAsInteger(10, NumSamples))
2316722688eSDehao Chen return false;
2326722688eSDehao Chen }
2336722688eSDehao Chen return true;
2346722688eSDehao Chen }
2356722688eSDehao Chen
2365f8f34e4SAdrian Prantl /// Load samples from a text file.
237de1ab26fSDiego Novillo ///
238de1ab26fSDiego Novillo /// See the documentation at the top of the file for an explanation of
239de1ab26fSDiego Novillo /// the expected format.
240de1ab26fSDiego Novillo ///
241de1ab26fSDiego Novillo /// \returns true if the file was loaded successfully, false otherwise.
readImpl()2428c8ec1f6SWei Mi std::error_code SampleProfileReaderText::readImpl() {
243c572e92cSDiego Novillo line_iterator LineIt(*Buffer, /*SkipBlanks=*/true, '#');
24448dd080cSNathan Slingerland sampleprof_error Result = sampleprof_error::success;
245de1ab26fSDiego Novillo
246aae1ed8eSDiego Novillo InlineCallStack InlineStack;
2475740bb80SHongtao Yu uint32_t TopLevelProbeProfileCount = 0;
248ac068e01SHongtao Yu
2495740bb80SHongtao Yu // DepthMetadata tracks whether we have processed metadata for the current
2505740bb80SHongtao Yu // top-level or nested function profile.
2515740bb80SHongtao Yu uint32_t DepthMetadata = 0;
2526722688eSDehao Chen
2536745ffe4SRong Xu ProfileIsFS = ProfileIsFSDisciminator;
25424201b64SRong Xu FunctionSamples::ProfileIsFS = ProfileIsFS;
2556722688eSDehao Chen for (; !LineIt.is_at_eof(); ++LineIt) {
2566722688eSDehao Chen if ((*LineIt)[(*LineIt).find_first_not_of(' ')] == '#')
2576722688eSDehao Chen continue;
258de1ab26fSDiego Novillo // Read the header of each function.
259de1ab26fSDiego Novillo //
260de1ab26fSDiego Novillo // Note that for function identifiers we are actually expecting
261de1ab26fSDiego Novillo // mangled names, but we may not always get them. This happens when
262de1ab26fSDiego Novillo // the compiler decides not to emit the function (e.g., it was inlined
263de1ab26fSDiego Novillo // and removed). In this case, the binary will not have the linkage
264de1ab26fSDiego Novillo // name for the function, so the profiler will emit the function's
265de1ab26fSDiego Novillo // unmangled name, which may contain characters like ':' and '>' in its
266de1ab26fSDiego Novillo // name (member functions, templates, etc).
267de1ab26fSDiego Novillo //
268de1ab26fSDiego Novillo // The only requirement we place on the identifier, then, is that it
269de1ab26fSDiego Novillo // should not begin with a number.
2706722688eSDehao Chen if ((*LineIt)[0] != ' ') {
27138be3330SDiego Novillo uint64_t NumSamples, NumHeadSamples;
2726722688eSDehao Chen StringRef FName;
2736722688eSDehao Chen if (!ParseHead(*LineIt, FName, NumSamples, NumHeadSamples)) {
2743376a787SDiego Novillo reportError(LineIt.line_number(),
275de1ab26fSDiego Novillo "Expected 'mangled_name:NUM:NUM', found " + *LineIt);
276c572e92cSDiego Novillo return sampleprof_error::malformed;
277de1ab26fSDiego Novillo }
2785740bb80SHongtao Yu DepthMetadata = 0;
279b9db7036SHongtao Yu SampleContext FContext(FName, CSNameTable);
2806b989a17SWenlei He if (FContext.hasContext())
2816b989a17SWenlei He ++CSProfileCount;
2826b989a17SWenlei He Profiles[FContext] = FunctionSamples();
2836b989a17SWenlei He FunctionSamples &FProfile = Profiles[FContext];
2846b989a17SWenlei He FProfile.setContext(FContext);
28548dd080cSNathan Slingerland MergeResult(Result, FProfile.addTotalSamples(NumSamples));
28648dd080cSNathan Slingerland MergeResult(Result, FProfile.addHeadSamples(NumHeadSamples));
2876722688eSDehao Chen InlineStack.clear();
2886722688eSDehao Chen InlineStack.push_back(&FProfile);
2896722688eSDehao Chen } else {
29038be3330SDiego Novillo uint64_t NumSamples;
2916722688eSDehao Chen StringRef FName;
29238be3330SDiego Novillo DenseMap<StringRef, uint64_t> TargetCountMap;
29338be3330SDiego Novillo uint32_t Depth, LineOffset, Discriminator;
294ac068e01SHongtao Yu LineType LineTy;
2951410db70SWenlei He uint64_t FunctionHash = 0;
2961410db70SWenlei He uint32_t Attributes = 0;
297ac068e01SHongtao Yu if (!ParseLine(*LineIt, LineTy, Depth, NumSamples, LineOffset,
2981410db70SWenlei He Discriminator, FName, TargetCountMap, FunctionHash,
2991410db70SWenlei He Attributes)) {
3003376a787SDiego Novillo reportError(LineIt.line_number(),
3013376a787SDiego Novillo "Expected 'NUM[.NUM]: NUM[ mangled_name:NUM]*', found " +
3023376a787SDiego Novillo *LineIt);
303c572e92cSDiego Novillo return sampleprof_error::malformed;
304de1ab26fSDiego Novillo }
3055740bb80SHongtao Yu if (LineTy != LineType::Metadata && Depth == DepthMetadata) {
306ac068e01SHongtao Yu // Metadata must be put at the end of a function profile.
307ac068e01SHongtao Yu reportError(LineIt.line_number(),
308ac068e01SHongtao Yu "Found non-metadata after metadata: " + *LineIt);
309ac068e01SHongtao Yu return sampleprof_error::malformed;
310ac068e01SHongtao Yu }
3116745ffe4SRong Xu
3126745ffe4SRong Xu // Here we handle FS discriminators.
3136745ffe4SRong Xu Discriminator &= getDiscriminatorMask();
3146745ffe4SRong Xu
3156722688eSDehao Chen while (InlineStack.size() > Depth) {
3166722688eSDehao Chen InlineStack.pop_back();
317c572e92cSDiego Novillo }
318ac068e01SHongtao Yu switch (LineTy) {
319ac068e01SHongtao Yu case LineType::CallSiteProfile: {
3206722688eSDehao Chen FunctionSamples &FSamples = InlineStack.back()->functionSamplesAt(
321adcd0268SBenjamin Kramer LineLocation(LineOffset, Discriminator))[std::string(FName)];
32257d1dda5SDehao Chen FSamples.setName(FName);
32348dd080cSNathan Slingerland MergeResult(Result, FSamples.addTotalSamples(NumSamples));
3246722688eSDehao Chen InlineStack.push_back(&FSamples);
3255740bb80SHongtao Yu DepthMetadata = 0;
326ac068e01SHongtao Yu break;
327ac068e01SHongtao Yu }
328ac068e01SHongtao Yu case LineType::BodyProfile: {
3296722688eSDehao Chen while (InlineStack.size() > Depth) {
3306722688eSDehao Chen InlineStack.pop_back();
3316722688eSDehao Chen }
3326722688eSDehao Chen FunctionSamples &FProfile = *InlineStack.back();
3336722688eSDehao Chen for (const auto &name_count : TargetCountMap) {
33448dd080cSNathan Slingerland MergeResult(Result, FProfile.addCalledTargetSamples(
33548dd080cSNathan Slingerland LineOffset, Discriminator, name_count.first,
33648dd080cSNathan Slingerland name_count.second));
337c572e92cSDiego Novillo }
33848dd080cSNathan Slingerland MergeResult(Result, FProfile.addBodySamples(LineOffset, Discriminator,
33948dd080cSNathan Slingerland NumSamples));
340ac068e01SHongtao Yu break;
341ac068e01SHongtao Yu }
342ac068e01SHongtao Yu case LineType::Metadata: {
343ac068e01SHongtao Yu FunctionSamples &FProfile = *InlineStack.back();
3441410db70SWenlei He if (FunctionHash) {
345ac068e01SHongtao Yu FProfile.setFunctionHash(FunctionHash);
3465740bb80SHongtao Yu if (Depth == 1)
3475740bb80SHongtao Yu ++TopLevelProbeProfileCount;
3481410db70SWenlei He }
3491410db70SWenlei He FProfile.getContext().setAllAttributes(Attributes);
3505740bb80SHongtao Yu if (Attributes & (uint32_t)ContextShouldBeInlined)
351e36786d1SHongtao Yu ProfileIsPreInlined = true;
3525740bb80SHongtao Yu DepthMetadata = Depth;
353ac068e01SHongtao Yu break;
354ac068e01SHongtao Yu }
3556722688eSDehao Chen }
356de1ab26fSDiego Novillo }
357de1ab26fSDiego Novillo }
3586b989a17SWenlei He
3597e99bddfSHongtao Yu assert((CSProfileCount == 0 || CSProfileCount == Profiles.size()) &&
3606b989a17SWenlei He "Cannot have both context-sensitive and regular profile");
361e36786d1SHongtao Yu ProfileIsCS = (CSProfileCount > 0);
3625740bb80SHongtao Yu assert((TopLevelProbeProfileCount == 0 ||
3635740bb80SHongtao Yu TopLevelProbeProfileCount == Profiles.size()) &&
364ac068e01SHongtao Yu "Cannot have both probe-based profiles and regular profiles");
3655740bb80SHongtao Yu ProfileIsProbeBased = (TopLevelProbeProfileCount > 0);
366ac068e01SHongtao Yu FunctionSamples::ProfileIsProbeBased = ProfileIsProbeBased;
367e36786d1SHongtao Yu FunctionSamples::ProfileIsCS = ProfileIsCS;
368e36786d1SHongtao Yu FunctionSamples::ProfileIsPreInlined = ProfileIsPreInlined;
3696b989a17SWenlei He
37040ee23dbSEaswaran Raman if (Result == sampleprof_error::success)
37140ee23dbSEaswaran Raman computeSummary();
372de1ab26fSDiego Novillo
37348dd080cSNathan Slingerland return Result;
374de1ab26fSDiego Novillo }
375de1ab26fSDiego Novillo
hasFormat(const MemoryBuffer & Buffer)3764f823667SNathan Slingerland bool SampleProfileReaderText::hasFormat(const MemoryBuffer &Buffer) {
3774f823667SNathan Slingerland bool result = false;
3784f823667SNathan Slingerland
3794f823667SNathan Slingerland // Check that the first non-comment line is a valid function header.
3804f823667SNathan Slingerland line_iterator LineIt(Buffer, /*SkipBlanks=*/true, '#');
3814f823667SNathan Slingerland if (!LineIt.is_at_eof()) {
3824f823667SNathan Slingerland if ((*LineIt)[0] != ' ') {
3834f823667SNathan Slingerland uint64_t NumSamples, NumHeadSamples;
3844f823667SNathan Slingerland StringRef FName;
3854f823667SNathan Slingerland result = ParseHead(*LineIt, FName, NumSamples, NumHeadSamples);
3864f823667SNathan Slingerland }
3874f823667SNathan Slingerland }
3884f823667SNathan Slingerland
3894f823667SNathan Slingerland return result;
3904f823667SNathan Slingerland }
3914f823667SNathan Slingerland
readNumber()392d5336ae2SDiego Novillo template <typename T> ErrorOr<T> SampleProfileReaderBinary::readNumber() {
393c572e92cSDiego Novillo unsigned NumBytesRead = 0;
394c572e92cSDiego Novillo std::error_code EC;
395c572e92cSDiego Novillo uint64_t Val = decodeULEB128(Data, &NumBytesRead);
396c572e92cSDiego Novillo
397c572e92cSDiego Novillo if (Val > std::numeric_limits<T>::max())
398c572e92cSDiego Novillo EC = sampleprof_error::malformed;
399c572e92cSDiego Novillo else if (Data + NumBytesRead > End)
400c572e92cSDiego Novillo EC = sampleprof_error::truncated;
401c572e92cSDiego Novillo else
402c572e92cSDiego Novillo EC = sampleprof_error::success;
403c572e92cSDiego Novillo
404c572e92cSDiego Novillo if (EC) {
4053376a787SDiego Novillo reportError(0, EC.message());
406c572e92cSDiego Novillo return EC;
407c572e92cSDiego Novillo }
408c572e92cSDiego Novillo
409c572e92cSDiego Novillo Data += NumBytesRead;
410c572e92cSDiego Novillo return static_cast<T>(Val);
411c572e92cSDiego Novillo }
412c572e92cSDiego Novillo
readString()413c572e92cSDiego Novillo ErrorOr<StringRef> SampleProfileReaderBinary::readString() {
414c572e92cSDiego Novillo std::error_code EC;
415c572e92cSDiego Novillo StringRef Str(reinterpret_cast<const char *>(Data));
416c572e92cSDiego Novillo if (Data + Str.size() + 1 > End) {
417c572e92cSDiego Novillo EC = sampleprof_error::truncated;
4183376a787SDiego Novillo reportError(0, EC.message());
419c572e92cSDiego Novillo return EC;
420c572e92cSDiego Novillo }
421c572e92cSDiego Novillo
422c572e92cSDiego Novillo Data += Str.size() + 1;
423c572e92cSDiego Novillo return Str;
424c572e92cSDiego Novillo }
425c572e92cSDiego Novillo
426a0c0857eSWei Mi template <typename T>
readUnencodedNumber()4276a14325dSWei Mi ErrorOr<T> SampleProfileReaderBinary::readUnencodedNumber() {
4286a14325dSWei Mi std::error_code EC;
4296a14325dSWei Mi
4306a14325dSWei Mi if (Data + sizeof(T) > End) {
4316a14325dSWei Mi EC = sampleprof_error::truncated;
4326a14325dSWei Mi reportError(0, EC.message());
4336a14325dSWei Mi return EC;
4346a14325dSWei Mi }
4356a14325dSWei Mi
4366a14325dSWei Mi using namespace support;
4376a14325dSWei Mi T Val = endian::readNext<T, little, unaligned>(Data);
4386a14325dSWei Mi return Val;
4396a14325dSWei Mi }
4406a14325dSWei Mi
4416a14325dSWei Mi template <typename T>
readStringIndex(T & Table)442a0c0857eSWei Mi inline ErrorOr<uint32_t> SampleProfileReaderBinary::readStringIndex(T &Table) {
443760c5a8fSDiego Novillo std::error_code EC;
44438be3330SDiego Novillo auto Idx = readNumber<uint32_t>();
445760c5a8fSDiego Novillo if (std::error_code EC = Idx.getError())
446760c5a8fSDiego Novillo return EC;
447a0c0857eSWei Mi if (*Idx >= Table.size())
448760c5a8fSDiego Novillo return sampleprof_error::truncated_name_table;
449a0c0857eSWei Mi return *Idx;
450a0c0857eSWei Mi }
451a0c0857eSWei Mi
readStringFromTable()452be907324SWei Mi ErrorOr<StringRef> SampleProfileReaderBinary::readStringFromTable() {
453a0c0857eSWei Mi auto Idx = readStringIndex(NameTable);
454a0c0857eSWei Mi if (std::error_code EC = Idx.getError())
455a0c0857eSWei Mi return EC;
456a0c0857eSWei Mi
457760c5a8fSDiego Novillo return NameTable[*Idx];
458760c5a8fSDiego Novillo }
459760c5a8fSDiego Novillo
readSampleContextFromTable()460b9db7036SHongtao Yu ErrorOr<SampleContext> SampleProfileReaderBinary::readSampleContextFromTable() {
461b9db7036SHongtao Yu auto FName(readStringFromTable());
462b9db7036SHongtao Yu if (std::error_code EC = FName.getError())
463b9db7036SHongtao Yu return EC;
464b9db7036SHongtao Yu return SampleContext(*FName);
465b9db7036SHongtao Yu }
466b9db7036SHongtao Yu
readStringFromTable()46764e76853SWei Mi ErrorOr<StringRef> SampleProfileReaderExtBinaryBase::readStringFromTable() {
46864e76853SWei Mi if (!FixedLengthMD5)
46964e76853SWei Mi return SampleProfileReaderBinary::readStringFromTable();
47064e76853SWei Mi
47164e76853SWei Mi // read NameTable index.
47264e76853SWei Mi auto Idx = readStringIndex(NameTable);
47364e76853SWei Mi if (std::error_code EC = Idx.getError())
47464e76853SWei Mi return EC;
47564e76853SWei Mi
47664e76853SWei Mi // Check whether the name to be accessed has been accessed before,
47764e76853SWei Mi // if not, read it from memory directly.
47864e76853SWei Mi StringRef &SR = NameTable[*Idx];
47964e76853SWei Mi if (SR.empty()) {
48064e76853SWei Mi const uint8_t *SavedData = Data;
48164e76853SWei Mi Data = MD5NameMemStart + ((*Idx) * sizeof(uint64_t));
48264e76853SWei Mi auto FID = readUnencodedNumber<uint64_t>();
48364e76853SWei Mi if (std::error_code EC = FID.getError())
48464e76853SWei Mi return EC;
48564e76853SWei Mi // Save the string converted from uint64_t in MD5StringBuf. All the
48664e76853SWei Mi // references to the name are all StringRefs refering to the string
48764e76853SWei Mi // in MD5StringBuf.
48864e76853SWei Mi MD5StringBuf->push_back(std::to_string(*FID));
48964e76853SWei Mi SR = MD5StringBuf->back();
49064e76853SWei Mi Data = SavedData;
49164e76853SWei Mi }
49264e76853SWei Mi return SR;
49364e76853SWei Mi }
49464e76853SWei Mi
readStringFromTable()495a0c0857eSWei Mi ErrorOr<StringRef> SampleProfileReaderCompactBinary::readStringFromTable() {
496a0c0857eSWei Mi auto Idx = readStringIndex(NameTable);
497a0c0857eSWei Mi if (std::error_code EC = Idx.getError())
498a0c0857eSWei Mi return EC;
499a0c0857eSWei Mi
500a0c0857eSWei Mi return StringRef(NameTable[*Idx]);
501a0c0857eSWei Mi }
502a0c0857eSWei Mi
503a7f1e8efSDiego Novillo std::error_code
readProfile(FunctionSamples & FProfile)504a7f1e8efSDiego Novillo SampleProfileReaderBinary::readProfile(FunctionSamples &FProfile) {
505b93483dbSDiego Novillo auto NumSamples = readNumber<uint64_t>();
506b93483dbSDiego Novillo if (std::error_code EC = NumSamples.getError())
507c572e92cSDiego Novillo return EC;
508b93483dbSDiego Novillo FProfile.addTotalSamples(*NumSamples);
509c572e92cSDiego Novillo
510c572e92cSDiego Novillo // Read the samples in the body.
51138be3330SDiego Novillo auto NumRecords = readNumber<uint32_t>();
512c572e92cSDiego Novillo if (std::error_code EC = NumRecords.getError())
513c572e92cSDiego Novillo return EC;
514a7f1e8efSDiego Novillo
51538be3330SDiego Novillo for (uint32_t I = 0; I < *NumRecords; ++I) {
516c572e92cSDiego Novillo auto LineOffset = readNumber<uint64_t>();
517c572e92cSDiego Novillo if (std::error_code EC = LineOffset.getError())
518c572e92cSDiego Novillo return EC;
519c572e92cSDiego Novillo
52010042412SDehao Chen if (!isOffsetLegal(*LineOffset)) {
52110042412SDehao Chen return std::error_code();
52210042412SDehao Chen }
52310042412SDehao Chen
524c572e92cSDiego Novillo auto Discriminator = readNumber<uint64_t>();
525c572e92cSDiego Novillo if (std::error_code EC = Discriminator.getError())
526c572e92cSDiego Novillo return EC;
527c572e92cSDiego Novillo
528c572e92cSDiego Novillo auto NumSamples = readNumber<uint64_t>();
529c572e92cSDiego Novillo if (std::error_code EC = NumSamples.getError())
530c572e92cSDiego Novillo return EC;
531c572e92cSDiego Novillo
53238be3330SDiego Novillo auto NumCalls = readNumber<uint32_t>();
533c572e92cSDiego Novillo if (std::error_code EC = NumCalls.getError())
534c572e92cSDiego Novillo return EC;
535c572e92cSDiego Novillo
5366745ffe4SRong Xu // Here we handle FS discriminators:
5376745ffe4SRong Xu uint32_t DiscriminatorVal = (*Discriminator) & getDiscriminatorMask();
5386745ffe4SRong Xu
53938be3330SDiego Novillo for (uint32_t J = 0; J < *NumCalls; ++J) {
540760c5a8fSDiego Novillo auto CalledFunction(readStringFromTable());
541c572e92cSDiego Novillo if (std::error_code EC = CalledFunction.getError())
542c572e92cSDiego Novillo return EC;
543c572e92cSDiego Novillo
544c572e92cSDiego Novillo auto CalledFunctionSamples = readNumber<uint64_t>();
545c572e92cSDiego Novillo if (std::error_code EC = CalledFunctionSamples.getError())
546c572e92cSDiego Novillo return EC;
547c572e92cSDiego Novillo
5486745ffe4SRong Xu FProfile.addCalledTargetSamples(*LineOffset, DiscriminatorVal,
549a7f1e8efSDiego Novillo *CalledFunction, *CalledFunctionSamples);
550c572e92cSDiego Novillo }
551c572e92cSDiego Novillo
5526745ffe4SRong Xu FProfile.addBodySamples(*LineOffset, DiscriminatorVal, *NumSamples);
553c572e92cSDiego Novillo }
554a7f1e8efSDiego Novillo
555a7f1e8efSDiego Novillo // Read all the samples for inlined function calls.
55638be3330SDiego Novillo auto NumCallsites = readNumber<uint32_t>();
557a7f1e8efSDiego Novillo if (std::error_code EC = NumCallsites.getError())
558a7f1e8efSDiego Novillo return EC;
559a7f1e8efSDiego Novillo
56038be3330SDiego Novillo for (uint32_t J = 0; J < *NumCallsites; ++J) {
561a7f1e8efSDiego Novillo auto LineOffset = readNumber<uint64_t>();
562a7f1e8efSDiego Novillo if (std::error_code EC = LineOffset.getError())
563a7f1e8efSDiego Novillo return EC;
564a7f1e8efSDiego Novillo
565a7f1e8efSDiego Novillo auto Discriminator = readNumber<uint64_t>();
566a7f1e8efSDiego Novillo if (std::error_code EC = Discriminator.getError())
567a7f1e8efSDiego Novillo return EC;
568a7f1e8efSDiego Novillo
569760c5a8fSDiego Novillo auto FName(readStringFromTable());
570a7f1e8efSDiego Novillo if (std::error_code EC = FName.getError())
571a7f1e8efSDiego Novillo return EC;
572a7f1e8efSDiego Novillo
5736745ffe4SRong Xu // Here we handle FS discriminators:
5746745ffe4SRong Xu uint32_t DiscriminatorVal = (*Discriminator) & getDiscriminatorMask();
5756745ffe4SRong Xu
5762c7ca9b5SDehao Chen FunctionSamples &CalleeProfile = FProfile.functionSamplesAt(
5776745ffe4SRong Xu LineLocation(*LineOffset, DiscriminatorVal))[std::string(*FName)];
57857d1dda5SDehao Chen CalleeProfile.setName(*FName);
579a7f1e8efSDiego Novillo if (std::error_code EC = readProfile(CalleeProfile))
580a7f1e8efSDiego Novillo return EC;
581a7f1e8efSDiego Novillo }
582a7f1e8efSDiego Novillo
583a7f1e8efSDiego Novillo return sampleprof_error::success;
584a7f1e8efSDiego Novillo }
585a7f1e8efSDiego Novillo
58609dcfe68SWei Mi std::error_code
readFuncProfile(const uint8_t * Start)58709dcfe68SWei Mi SampleProfileReaderBinary::readFuncProfile(const uint8_t *Start) {
58809dcfe68SWei Mi Data = Start;
589b93483dbSDiego Novillo auto NumHeadSamples = readNumber<uint64_t>();
590b93483dbSDiego Novillo if (std::error_code EC = NumHeadSamples.getError())
591b93483dbSDiego Novillo return EC;
592b93483dbSDiego Novillo
593b9db7036SHongtao Yu ErrorOr<SampleContext> FContext(readSampleContextFromTable());
594b9db7036SHongtao Yu if (std::error_code EC = FContext.getError())
595a7f1e8efSDiego Novillo return EC;
596a7f1e8efSDiego Novillo
597b9db7036SHongtao Yu Profiles[*FContext] = FunctionSamples();
598b9db7036SHongtao Yu FunctionSamples &FProfile = Profiles[*FContext];
599b9db7036SHongtao Yu FProfile.setContext(*FContext);
600b93483dbSDiego Novillo FProfile.addHeadSamples(*NumHeadSamples);
601b93483dbSDiego Novillo
602b9db7036SHongtao Yu if (FContext->hasContext())
6037e99bddfSHongtao Yu CSProfileCount++;
6047e99bddfSHongtao Yu
605a7f1e8efSDiego Novillo if (std::error_code EC = readProfile(FProfile))
606a7f1e8efSDiego Novillo return EC;
6076a14325dSWei Mi return sampleprof_error::success;
608c572e92cSDiego Novillo }
609c572e92cSDiego Novillo
readImpl()6108c8ec1f6SWei Mi std::error_code SampleProfileReaderBinary::readImpl() {
6116745ffe4SRong Xu ProfileIsFS = ProfileIsFSDisciminator;
61224201b64SRong Xu FunctionSamples::ProfileIsFS = ProfileIsFS;
6136a14325dSWei Mi while (!at_eof()) {
61409dcfe68SWei Mi if (std::error_code EC = readFuncProfile(Data))
6156a14325dSWei Mi return EC;
6166a14325dSWei Mi }
6176a14325dSWei Mi
6186a14325dSWei Mi return sampleprof_error::success;
6196a14325dSWei Mi }
6206a14325dSWei Mi
621b9db7036SHongtao Yu ErrorOr<SampleContextFrames>
readContextFromTable()622b9db7036SHongtao Yu SampleProfileReaderExtBinaryBase::readContextFromTable() {
623b9db7036SHongtao Yu auto ContextIdx = readNumber<uint32_t>();
624b9db7036SHongtao Yu if (std::error_code EC = ContextIdx.getError())
625b9db7036SHongtao Yu return EC;
626b9db7036SHongtao Yu if (*ContextIdx >= CSNameTable->size())
627b9db7036SHongtao Yu return sampleprof_error::truncated_name_table;
628b9db7036SHongtao Yu return (*CSNameTable)[*ContextIdx];
629b9db7036SHongtao Yu }
630b9db7036SHongtao Yu
631b9db7036SHongtao Yu ErrorOr<SampleContext>
readSampleContextFromTable()632b9db7036SHongtao Yu SampleProfileReaderExtBinaryBase::readSampleContextFromTable() {
633e36786d1SHongtao Yu if (ProfileIsCS) {
634b9db7036SHongtao Yu auto FContext(readContextFromTable());
635b9db7036SHongtao Yu if (std::error_code EC = FContext.getError())
636b9db7036SHongtao Yu return EC;
637b9db7036SHongtao Yu return SampleContext(*FContext);
638b9db7036SHongtao Yu } else {
639b9db7036SHongtao Yu auto FName(readStringFromTable());
640b9db7036SHongtao Yu if (std::error_code EC = FName.getError())
641b9db7036SHongtao Yu return EC;
642b9db7036SHongtao Yu return SampleContext(*FName);
643b9db7036SHongtao Yu }
644b9db7036SHongtao Yu }
645b9db7036SHongtao Yu
readOneSection(const uint8_t * Start,uint64_t Size,const SecHdrTableEntry & Entry)64693953d41SWei Mi std::error_code SampleProfileReaderExtBinaryBase::readOneSection(
647ebad6788SWei Mi const uint8_t *Start, uint64_t Size, const SecHdrTableEntry &Entry) {
648077a9c70SWei Mi Data = Start;
649b523790aSWei Mi End = Start + Size;
650ebad6788SWei Mi switch (Entry.Type) {
651be907324SWei Mi case SecProfSummary:
652be907324SWei Mi if (std::error_code EC = readSummary())
653be907324SWei Mi return EC;
654b49eac71SWei Mi if (hasSecFlag(Entry, SecProfSummaryFlags::SecFlagPartial))
655b49eac71SWei Mi Summary->setPartialProfile(true);
656a5d30421SWenlei He if (hasSecFlag(Entry, SecProfSummaryFlags::SecFlagFullContext))
657e36786d1SHongtao Yu FunctionSamples::ProfileIsCS = ProfileIsCS = true;
658e36786d1SHongtao Yu if (hasSecFlag(Entry, SecProfSummaryFlags::SecFlagIsPreInlined))
659e36786d1SHongtao Yu FunctionSamples::ProfileIsPreInlined = ProfileIsPreInlined = true;
6606745ffe4SRong Xu if (hasSecFlag(Entry, SecProfSummaryFlags::SecFlagFSDiscriminator))
6616745ffe4SRong Xu FunctionSamples::ProfileIsFS = ProfileIsFS = true;
662be907324SWei Mi break;
66364e76853SWei Mi case SecNameTable: {
66464e76853SWei Mi FixedLengthMD5 =
66564e76853SWei Mi hasSecFlag(Entry, SecNameTableFlags::SecFlagFixedLengthMD5);
66664e76853SWei Mi bool UseMD5 = hasSecFlag(Entry, SecNameTableFlags::SecFlagMD5Name);
66764e76853SWei Mi assert((!FixedLengthMD5 || UseMD5) &&
66864e76853SWei Mi "If FixedLengthMD5 is true, UseMD5 has to be true");
669ee35784aSWei Mi FunctionSamples::HasUniqSuffix =
670ee35784aSWei Mi hasSecFlag(Entry, SecNameTableFlags::SecFlagUniqSuffix);
67164e76853SWei Mi if (std::error_code EC = readNameTableSec(UseMD5))
672be907324SWei Mi return EC;
673be907324SWei Mi break;
67464e76853SWei Mi }
675b9db7036SHongtao Yu case SecCSNameTable: {
676b9db7036SHongtao Yu if (std::error_code EC = readCSNameTableSec())
677b9db7036SHongtao Yu return EC;
678b9db7036SHongtao Yu break;
679b9db7036SHongtao Yu }
680be907324SWei Mi case SecLBRProfile:
68109dcfe68SWei Mi if (std::error_code EC = readFuncProfiles())
682be907324SWei Mi return EC;
683be907324SWei Mi break;
68409dcfe68SWei Mi case SecFuncOffsetTable:
685f4711e0dSHongtao Yu FuncOffsetsOrdered = hasSecFlag(Entry, SecFuncOffsetFlags::SecFlagOrdered);
68609dcfe68SWei Mi if (std::error_code EC = readFuncOffsetTable())
687798e59b8SWei Mi return EC;
688798e59b8SWei Mi break;
6891410db70SWenlei He case SecFuncMetadata: {
690ac068e01SHongtao Yu ProfileIsProbeBased =
691ac068e01SHongtao Yu hasSecFlag(Entry, SecFuncMetadataFlags::SecFlagIsProbeBased);
692ac068e01SHongtao Yu FunctionSamples::ProfileIsProbeBased = ProfileIsProbeBased;
6931410db70SWenlei He bool HasAttribute =
6941410db70SWenlei He hasSecFlag(Entry, SecFuncMetadataFlags::SecFlagHasAttribute);
6951410db70SWenlei He if (std::error_code EC = readFuncMetadata(HasAttribute))
696ac068e01SHongtao Yu return EC;
697ac068e01SHongtao Yu break;
6981410db70SWenlei He }
69993953d41SWei Mi case SecProfileSymbolList:
70093953d41SWei Mi if (std::error_code EC = readProfileSymbolList())
70193953d41SWei Mi return EC;
70293953d41SWei Mi break;
703be907324SWei Mi default:
70493953d41SWei Mi if (std::error_code EC = readCustomSection(Entry))
70593953d41SWei Mi return EC;
706077a9c70SWei Mi break;
707be907324SWei Mi }
708077a9c70SWei Mi return sampleprof_error::success;
709077a9c70SWei Mi }
710077a9c70SWei Mi
collectFuncsFromModule()711ee35784aSWei Mi bool SampleProfileReaderExtBinaryBase::collectFuncsFromModule() {
712ee35784aSWei Mi if (!M)
713ee35784aSWei Mi return false;
71409dcfe68SWei Mi FuncsToUse.clear();
715ee35784aSWei Mi for (auto &F : *M)
71609dcfe68SWei Mi FuncsToUse.insert(FunctionSamples::getCanonicalFnName(F));
717ee35784aSWei Mi return true;
71809dcfe68SWei Mi }
71909dcfe68SWei Mi
readFuncOffsetTable()72093953d41SWei Mi std::error_code SampleProfileReaderExtBinaryBase::readFuncOffsetTable() {
721a906e3ecSWei Mi // If there are more than one FuncOffsetTable, the profile read associated
722a906e3ecSWei Mi // with previous FuncOffsetTable has to be done before next FuncOffsetTable
723a906e3ecSWei Mi // is read.
724a906e3ecSWei Mi FuncOffsetTable.clear();
725a906e3ecSWei Mi
72609dcfe68SWei Mi auto Size = readNumber<uint64_t>();
72709dcfe68SWei Mi if (std::error_code EC = Size.getError())
72809dcfe68SWei Mi return EC;
72909dcfe68SWei Mi
73009dcfe68SWei Mi FuncOffsetTable.reserve(*Size);
731f4711e0dSHongtao Yu
732f4711e0dSHongtao Yu if (FuncOffsetsOrdered) {
733f4711e0dSHongtao Yu OrderedFuncOffsets =
734f4711e0dSHongtao Yu std::make_unique<std::vector<std::pair<SampleContext, uint64_t>>>();
735f4711e0dSHongtao Yu OrderedFuncOffsets->reserve(*Size);
736f4711e0dSHongtao Yu }
737f4711e0dSHongtao Yu
73809dcfe68SWei Mi for (uint32_t I = 0; I < *Size; ++I) {
739f4711e0dSHongtao Yu auto FContext(readSampleContextFromTable());
740f4711e0dSHongtao Yu if (std::error_code EC = FContext.getError())
74109dcfe68SWei Mi return EC;
74209dcfe68SWei Mi
74309dcfe68SWei Mi auto Offset = readNumber<uint64_t>();
74409dcfe68SWei Mi if (std::error_code EC = Offset.getError())
74509dcfe68SWei Mi return EC;
74609dcfe68SWei Mi
747f4711e0dSHongtao Yu FuncOffsetTable[*FContext] = *Offset;
748f4711e0dSHongtao Yu if (FuncOffsetsOrdered)
749f4711e0dSHongtao Yu OrderedFuncOffsets->emplace_back(*FContext, *Offset);
75009dcfe68SWei Mi }
751f4711e0dSHongtao Yu
75209dcfe68SWei Mi return sampleprof_error::success;
75309dcfe68SWei Mi }
75409dcfe68SWei Mi
readFuncProfiles()75593953d41SWei Mi std::error_code SampleProfileReaderExtBinaryBase::readFuncProfiles() {
756ee35784aSWei Mi // Collect functions used by current module if the Reader has been
757ee35784aSWei Mi // given a module.
758ee35784aSWei Mi // collectFuncsFromModule uses FunctionSamples::getCanonicalFnName
759ee35784aSWei Mi // which will query FunctionSamples::HasUniqSuffix, so it has to be
760ee35784aSWei Mi // called after FunctionSamples::HasUniqSuffix is set, i.e. after
761ee35784aSWei Mi // NameTable section is read.
762ee35784aSWei Mi bool LoadFuncsToBeUsed = collectFuncsFromModule();
763ee35784aSWei Mi
764ee35784aSWei Mi // When LoadFuncsToBeUsed is false, load all the function profiles.
76509dcfe68SWei Mi const uint8_t *Start = Data;
766ee35784aSWei Mi if (!LoadFuncsToBeUsed) {
76709dcfe68SWei Mi while (Data < End) {
76809dcfe68SWei Mi if (std::error_code EC = readFuncProfile(Data))
76909dcfe68SWei Mi return EC;
77009dcfe68SWei Mi }
77109dcfe68SWei Mi assert(Data == End && "More data is read than expected");
7727e99bddfSHongtao Yu } else {
773ee35784aSWei Mi // Load function profiles on demand.
7748c8ec1f6SWei Mi if (Remapper) {
77509dcfe68SWei Mi for (auto Name : FuncsToUse) {
7768c8ec1f6SWei Mi Remapper->insert(Name);
7778c8ec1f6SWei Mi }
7788c8ec1f6SWei Mi }
7798c8ec1f6SWei Mi
780e36786d1SHongtao Yu if (ProfileIsCS) {
7817ca80300SHongtao Yu DenseSet<uint64_t> FuncGuidsToUse;
7827ca80300SHongtao Yu if (useMD5()) {
7837ca80300SHongtao Yu for (auto Name : FuncsToUse)
7847ca80300SHongtao Yu FuncGuidsToUse.insert(Function::getGUID(Name));
7857ca80300SHongtao Yu }
7867ca80300SHongtao Yu
787f4711e0dSHongtao Yu // For each function in current module, load all context profiles for
788f4711e0dSHongtao Yu // the function as well as their callee contexts which can help profile
789f4711e0dSHongtao Yu // guided importing for ThinLTO. This can be achieved by walking
790f4711e0dSHongtao Yu // through an ordered context container, where contexts are laid out
791f4711e0dSHongtao Yu // as if they were walked in preorder of a context trie. While
792f4711e0dSHongtao Yu // traversing the trie, a link to the highest common ancestor node is
793f4711e0dSHongtao Yu // kept so that all of its decendants will be loaded.
794f4711e0dSHongtao Yu assert(OrderedFuncOffsets.get() &&
795f4711e0dSHongtao Yu "func offset table should always be sorted in CS profile");
796f4711e0dSHongtao Yu const SampleContext *CommonContext = nullptr;
797f4711e0dSHongtao Yu for (const auto &NameOffset : *OrderedFuncOffsets) {
798f4711e0dSHongtao Yu const auto &FContext = NameOffset.first;
799f4711e0dSHongtao Yu auto FName = FContext.getName();
800f4711e0dSHongtao Yu // For function in the current module, keep its farthest ancestor
801f4711e0dSHongtao Yu // context. This can be used to load itself and its child and
802f4711e0dSHongtao Yu // sibling contexts.
803f4711e0dSHongtao Yu if ((useMD5() && FuncGuidsToUse.count(std::stoull(FName.data()))) ||
804f4711e0dSHongtao Yu (!useMD5() && (FuncsToUse.count(FName) ||
805f4711e0dSHongtao Yu (Remapper && Remapper->exist(FName))))) {
806f4711e0dSHongtao Yu if (!CommonContext || !CommonContext->IsPrefixOf(FContext))
807f4711e0dSHongtao Yu CommonContext = &FContext;
808f4711e0dSHongtao Yu }
809a5d30421SWenlei He
810f4711e0dSHongtao Yu if (CommonContext == &FContext ||
811f4711e0dSHongtao Yu (CommonContext && CommonContext->IsPrefixOf(FContext))) {
812f4711e0dSHongtao Yu // Load profile for the current context which originated from
813f4711e0dSHongtao Yu // the common ancestor.
814f4711e0dSHongtao Yu const uint8_t *FuncProfileAddr = Start + NameOffset.second;
815a5d30421SWenlei He assert(FuncProfileAddr < End && "out of LBRProfile section");
816a5d30421SWenlei He if (std::error_code EC = readFuncProfile(FuncProfileAddr))
817a5d30421SWenlei He return EC;
818a5d30421SWenlei He }
819a5d30421SWenlei He }
820ebad6788SWei Mi } else {
8217ca80300SHongtao Yu if (useMD5()) {
8227ca80300SHongtao Yu for (auto Name : FuncsToUse) {
8237ca80300SHongtao Yu auto GUID = std::to_string(MD5Hash(Name));
8247ca80300SHongtao Yu auto iter = FuncOffsetTable.find(StringRef(GUID));
8257ca80300SHongtao Yu if (iter == FuncOffsetTable.end())
8267ca80300SHongtao Yu continue;
8277ca80300SHongtao Yu const uint8_t *FuncProfileAddr = Start + iter->second;
8287ca80300SHongtao Yu assert(FuncProfileAddr < End && "out of LBRProfile section");
8297ca80300SHongtao Yu if (std::error_code EC = readFuncProfile(FuncProfileAddr))
8307ca80300SHongtao Yu return EC;
8317ca80300SHongtao Yu }
8327ca80300SHongtao Yu } else {
8338c8ec1f6SWei Mi for (auto NameOffset : FuncOffsetTable) {
8347e99bddfSHongtao Yu SampleContext FContext(NameOffset.first);
835b9db7036SHongtao Yu auto FuncName = FContext.getName();
8368c8ec1f6SWei Mi if (!FuncsToUse.count(FuncName) &&
8378c8ec1f6SWei Mi (!Remapper || !Remapper->exist(FuncName)))
83809dcfe68SWei Mi continue;
8398c8ec1f6SWei Mi const uint8_t *FuncProfileAddr = Start + NameOffset.second;
84009dcfe68SWei Mi assert(FuncProfileAddr < End && "out of LBRProfile section");
84109dcfe68SWei Mi if (std::error_code EC = readFuncProfile(FuncProfileAddr))
84209dcfe68SWei Mi return EC;
84309dcfe68SWei Mi }
844ebad6788SWei Mi }
8457ca80300SHongtao Yu }
84609dcfe68SWei Mi Data = End;
8477e99bddfSHongtao Yu }
8487e99bddfSHongtao Yu assert((CSProfileCount == 0 || CSProfileCount == Profiles.size()) &&
8497e99bddfSHongtao Yu "Cannot have both context-sensitive and regular profile");
850e36786d1SHongtao Yu assert((!CSProfileCount || ProfileIsCS) &&
851a5d30421SWenlei He "Section flag should be consistent with actual profile");
85209dcfe68SWei Mi return sampleprof_error::success;
85309dcfe68SWei Mi }
85409dcfe68SWei Mi
readProfileSymbolList()85593953d41SWei Mi std::error_code SampleProfileReaderExtBinaryBase::readProfileSymbolList() {
856b523790aSWei Mi if (!ProfSymList)
857b523790aSWei Mi ProfSymList = std::make_unique<ProfileSymbolList>();
858b523790aSWei Mi
85909dcfe68SWei Mi if (std::error_code EC = ProfSymList->read(Data, End - Data))
860798e59b8SWei Mi return EC;
861798e59b8SWei Mi
86209dcfe68SWei Mi Data = End;
863b523790aSWei Mi return sampleprof_error::success;
864b523790aSWei Mi }
865b523790aSWei Mi
decompressSection(const uint8_t * SecStart,const uint64_t SecSize,const uint8_t * & DecompressBuf,uint64_t & DecompressBufSize)866b523790aSWei Mi std::error_code SampleProfileReaderExtBinaryBase::decompressSection(
867b523790aSWei Mi const uint8_t *SecStart, const uint64_t SecSize,
868b523790aSWei Mi const uint8_t *&DecompressBuf, uint64_t &DecompressBufSize) {
869b523790aSWei Mi Data = SecStart;
870b523790aSWei Mi End = SecStart + SecSize;
871b523790aSWei Mi auto DecompressSize = readNumber<uint64_t>();
872b523790aSWei Mi if (std::error_code EC = DecompressSize.getError())
873b523790aSWei Mi return EC;
874b523790aSWei Mi DecompressBufSize = *DecompressSize;
875b523790aSWei Mi
876798e59b8SWei Mi auto CompressSize = readNumber<uint64_t>();
877798e59b8SWei Mi if (std::error_code EC = CompressSize.getError())
878798e59b8SWei Mi return EC;
879798e59b8SWei Mi
880ea61750cSCole Kissane if (!llvm::compression::zlib::isAvailable())
881b523790aSWei Mi return sampleprof_error::zlib_unavailable;
882798e59b8SWei Mi
883*e690137dSFangrui Song uint8_t *Buffer = Allocator.Allocate<uint8_t>(DecompressBufSize);
884283df8cfSWei Mi size_t UCSize = DecompressBufSize;
885*e690137dSFangrui Song llvm::Error E = compression::zlib::uncompress(
886*e690137dSFangrui Song makeArrayRef(Data, *CompressSize), Buffer, UCSize);
887b523790aSWei Mi if (E)
888b523790aSWei Mi return sampleprof_error::uncompress_failed;
889b523790aSWei Mi DecompressBuf = reinterpret_cast<const uint8_t *>(Buffer);
890798e59b8SWei Mi return sampleprof_error::success;
891798e59b8SWei Mi }
892798e59b8SWei Mi
readImpl()8938c8ec1f6SWei Mi std::error_code SampleProfileReaderExtBinaryBase::readImpl() {
894077a9c70SWei Mi const uint8_t *BufStart =
895077a9c70SWei Mi reinterpret_cast<const uint8_t *>(Buffer->getBufferStart());
896077a9c70SWei Mi
897077a9c70SWei Mi for (auto &Entry : SecHdrTable) {
898077a9c70SWei Mi // Skip empty section.
899077a9c70SWei Mi if (!Entry.Size)
900077a9c70SWei Mi continue;
901b523790aSWei Mi
90221b1ad03SWei Mi // Skip sections without context when SkipFlatProf is true.
90321b1ad03SWei Mi if (SkipFlatProf && hasSecFlag(Entry, SecCommonFlags::SecFlagFlat))
90421b1ad03SWei Mi continue;
90521b1ad03SWei Mi
906077a9c70SWei Mi const uint8_t *SecStart = BufStart + Entry.Offset;
907b523790aSWei Mi uint64_t SecSize = Entry.Size;
908b523790aSWei Mi
909b523790aSWei Mi // If the section is compressed, decompress it into a buffer
910b523790aSWei Mi // DecompressBuf before reading the actual data. The pointee of
911b523790aSWei Mi // 'Data' will be changed to buffer hold by DecompressBuf
912b523790aSWei Mi // temporarily when reading the actual data.
913ebad6788SWei Mi bool isCompressed = hasSecFlag(Entry, SecCommonFlags::SecFlagCompress);
914b523790aSWei Mi if (isCompressed) {
915b523790aSWei Mi const uint8_t *DecompressBuf;
916b523790aSWei Mi uint64_t DecompressBufSize;
917b523790aSWei Mi if (std::error_code EC = decompressSection(
918b523790aSWei Mi SecStart, SecSize, DecompressBuf, DecompressBufSize))
919077a9c70SWei Mi return EC;
920b523790aSWei Mi SecStart = DecompressBuf;
921b523790aSWei Mi SecSize = DecompressBufSize;
922b523790aSWei Mi }
923b523790aSWei Mi
924ebad6788SWei Mi if (std::error_code EC = readOneSection(SecStart, SecSize, Entry))
925b523790aSWei Mi return EC;
926b523790aSWei Mi if (Data != SecStart + SecSize)
927be907324SWei Mi return sampleprof_error::malformed;
928b523790aSWei Mi
929b523790aSWei Mi // Change the pointee of 'Data' from DecompressBuf to original Buffer.
930b523790aSWei Mi if (isCompressed) {
931b523790aSWei Mi Data = BufStart + Entry.Offset;
932b523790aSWei Mi End = BufStart + Buffer->getBufferSize();
933b523790aSWei Mi }
934be907324SWei Mi }
935be907324SWei Mi
936be907324SWei Mi return sampleprof_error::success;
937be907324SWei Mi }
938be907324SWei Mi
readImpl()9398c8ec1f6SWei Mi std::error_code SampleProfileReaderCompactBinary::readImpl() {
940ee35784aSWei Mi // Collect functions used by current module if the Reader has been
941ee35784aSWei Mi // given a module.
942ee35784aSWei Mi bool LoadFuncsToBeUsed = collectFuncsFromModule();
9436745ffe4SRong Xu ProfileIsFS = ProfileIsFSDisciminator;
94424201b64SRong Xu FunctionSamples::ProfileIsFS = ProfileIsFS;
945d3289544SWenlei He std::vector<uint64_t> OffsetsToUse;
946ee35784aSWei Mi if (!LoadFuncsToBeUsed) {
947ee35784aSWei Mi // load all the function profiles.
948d3289544SWenlei He for (auto FuncEntry : FuncOffsetTable) {
949d3289544SWenlei He OffsetsToUse.push_back(FuncEntry.second);
950d3289544SWenlei He }
951ee35784aSWei Mi } else {
952ee35784aSWei Mi // load function profiles on demand.
9536a14325dSWei Mi for (auto Name : FuncsToUse) {
9546a14325dSWei Mi auto GUID = std::to_string(MD5Hash(Name));
9556a14325dSWei Mi auto iter = FuncOffsetTable.find(StringRef(GUID));
9566a14325dSWei Mi if (iter == FuncOffsetTable.end())
9576a14325dSWei Mi continue;
958d3289544SWenlei He OffsetsToUse.push_back(iter->second);
959d3289544SWenlei He }
960d3289544SWenlei He }
961d3289544SWenlei He
962d3289544SWenlei He for (auto Offset : OffsetsToUse) {
9636a14325dSWei Mi const uint8_t *SavedData = Data;
96409dcfe68SWei Mi if (std::error_code EC = readFuncProfile(
96509dcfe68SWei Mi reinterpret_cast<const uint8_t *>(Buffer->getBufferStart()) +
96609dcfe68SWei Mi Offset))
9676a14325dSWei Mi return EC;
9686a14325dSWei Mi Data = SavedData;
9696a14325dSWei Mi }
970c572e92cSDiego Novillo return sampleprof_error::success;
971c572e92cSDiego Novillo }
972c572e92cSDiego Novillo
verifySPMagic(uint64_t Magic)973a0c0857eSWei Mi std::error_code SampleProfileReaderRawBinary::verifySPMagic(uint64_t Magic) {
974a0c0857eSWei Mi if (Magic == SPMagic())
975a0c0857eSWei Mi return sampleprof_error::success;
976a0c0857eSWei Mi return sampleprof_error::bad_magic;
977a0c0857eSWei Mi }
978a0c0857eSWei Mi
verifySPMagic(uint64_t Magic)979be907324SWei Mi std::error_code SampleProfileReaderExtBinary::verifySPMagic(uint64_t Magic) {
980be907324SWei Mi if (Magic == SPMagic(SPF_Ext_Binary))
981be907324SWei Mi return sampleprof_error::success;
982be907324SWei Mi return sampleprof_error::bad_magic;
983be907324SWei Mi }
984be907324SWei Mi
985a0c0857eSWei Mi std::error_code
verifySPMagic(uint64_t Magic)986a0c0857eSWei Mi SampleProfileReaderCompactBinary::verifySPMagic(uint64_t Magic) {
987a0c0857eSWei Mi if (Magic == SPMagic(SPF_Compact_Binary))
988a0c0857eSWei Mi return sampleprof_error::success;
989a0c0857eSWei Mi return sampleprof_error::bad_magic;
990a0c0857eSWei Mi }
991a0c0857eSWei Mi
readNameTable()992be907324SWei Mi std::error_code SampleProfileReaderBinary::readNameTable() {
993a0c0857eSWei Mi auto Size = readNumber<uint32_t>();
994a0c0857eSWei Mi if (std::error_code EC = Size.getError())
995a0c0857eSWei Mi return EC;
996a906e3ecSWei Mi NameTable.reserve(*Size + NameTable.size());
997a0c0857eSWei Mi for (uint32_t I = 0; I < *Size; ++I) {
998a0c0857eSWei Mi auto Name(readString());
999a0c0857eSWei Mi if (std::error_code EC = Name.getError())
1000a0c0857eSWei Mi return EC;
1001a0c0857eSWei Mi NameTable.push_back(*Name);
1002a0c0857eSWei Mi }
1003a0c0857eSWei Mi
1004a0c0857eSWei Mi return sampleprof_error::success;
1005a0c0857eSWei Mi }
1006a0c0857eSWei Mi
readMD5NameTable()100793953d41SWei Mi std::error_code SampleProfileReaderExtBinaryBase::readMD5NameTable() {
1008ebad6788SWei Mi auto Size = readNumber<uint64_t>();
1009ebad6788SWei Mi if (std::error_code EC = Size.getError())
1010ebad6788SWei Mi return EC;
1011ebad6788SWei Mi MD5StringBuf = std::make_unique<std::vector<std::string>>();
1012ebad6788SWei Mi MD5StringBuf->reserve(*Size);
101364e76853SWei Mi if (FixedLengthMD5) {
101464e76853SWei Mi // Preallocate and initialize NameTable so we can check whether a name
101564e76853SWei Mi // index has been read before by checking whether the element in the
101664e76853SWei Mi // NameTable is empty, meanwhile readStringIndex can do the boundary
101764e76853SWei Mi // check using the size of NameTable.
101864e76853SWei Mi NameTable.resize(*Size + NameTable.size());
101964e76853SWei Mi
102064e76853SWei Mi MD5NameMemStart = Data;
102164e76853SWei Mi Data = Data + (*Size) * sizeof(uint64_t);
102264e76853SWei Mi return sampleprof_error::success;
102364e76853SWei Mi }
102464e76853SWei Mi NameTable.reserve(*Size);
1025ebad6788SWei Mi for (uint32_t I = 0; I < *Size; ++I) {
1026ebad6788SWei Mi auto FID = readNumber<uint64_t>();
1027ebad6788SWei Mi if (std::error_code EC = FID.getError())
1028ebad6788SWei Mi return EC;
1029ebad6788SWei Mi MD5StringBuf->push_back(std::to_string(*FID));
1030ebad6788SWei Mi // NameTable is a vector of StringRef. Here it is pushing back a
1031ebad6788SWei Mi // StringRef initialized with the last string in MD5stringBuf.
1032ebad6788SWei Mi NameTable.push_back(MD5StringBuf->back());
1033ebad6788SWei Mi }
1034ebad6788SWei Mi return sampleprof_error::success;
1035ebad6788SWei Mi }
1036ebad6788SWei Mi
readNameTableSec(bool IsMD5)103793953d41SWei Mi std::error_code SampleProfileReaderExtBinaryBase::readNameTableSec(bool IsMD5) {
1038ebad6788SWei Mi if (IsMD5)
1039ebad6788SWei Mi return readMD5NameTable();
1040ebad6788SWei Mi return SampleProfileReaderBinary::readNameTable();
1041ebad6788SWei Mi }
1042ebad6788SWei Mi
1043b9db7036SHongtao Yu // Read in the CS name table section, which basically contains a list of context
1044b9db7036SHongtao Yu // vectors. Each element of a context vector, aka a frame, refers to the
1045b9db7036SHongtao Yu // underlying raw function names that are stored in the name table, as well as
1046b9db7036SHongtao Yu // a callsite identifier that only makes sense for non-leaf frames.
readCSNameTableSec()1047b9db7036SHongtao Yu std::error_code SampleProfileReaderExtBinaryBase::readCSNameTableSec() {
1048b9db7036SHongtao Yu auto Size = readNumber<uint32_t>();
1049b9db7036SHongtao Yu if (std::error_code EC = Size.getError())
1050b9db7036SHongtao Yu return EC;
1051b9db7036SHongtao Yu
1052b9db7036SHongtao Yu std::vector<SampleContextFrameVector> *PNameVec =
1053b9db7036SHongtao Yu new std::vector<SampleContextFrameVector>();
1054b9db7036SHongtao Yu PNameVec->reserve(*Size);
1055b9db7036SHongtao Yu for (uint32_t I = 0; I < *Size; ++I) {
1056b9db7036SHongtao Yu PNameVec->emplace_back(SampleContextFrameVector());
1057b9db7036SHongtao Yu auto ContextSize = readNumber<uint32_t>();
1058b9db7036SHongtao Yu if (std::error_code EC = ContextSize.getError())
1059b9db7036SHongtao Yu return EC;
1060b9db7036SHongtao Yu for (uint32_t J = 0; J < *ContextSize; ++J) {
1061ac068e01SHongtao Yu auto FName(readStringFromTable());
1062ac068e01SHongtao Yu if (std::error_code EC = FName.getError())
1063ac068e01SHongtao Yu return EC;
1064b9db7036SHongtao Yu auto LineOffset = readNumber<uint64_t>();
1065b9db7036SHongtao Yu if (std::error_code EC = LineOffset.getError())
1066b9db7036SHongtao Yu return EC;
1067ac068e01SHongtao Yu
1068b9db7036SHongtao Yu if (!isOffsetLegal(*LineOffset))
1069b9db7036SHongtao Yu return std::error_code();
10701410db70SWenlei He
1071b9db7036SHongtao Yu auto Discriminator = readNumber<uint64_t>();
1072b9db7036SHongtao Yu if (std::error_code EC = Discriminator.getError())
1073b9db7036SHongtao Yu return EC;
1074b9db7036SHongtao Yu
1075b9db7036SHongtao Yu PNameVec->back().emplace_back(
1076b9db7036SHongtao Yu FName.get(), LineLocation(LineOffset.get(), Discriminator.get()));
1077b9db7036SHongtao Yu }
1078b9db7036SHongtao Yu }
1079b9db7036SHongtao Yu
1080b9db7036SHongtao Yu // From this point the underlying object of CSNameTable should be immutable.
1081b9db7036SHongtao Yu CSNameTable.reset(PNameVec);
1082b9db7036SHongtao Yu return sampleprof_error::success;
1083b9db7036SHongtao Yu }
1084b9db7036SHongtao Yu
1085b9db7036SHongtao Yu std::error_code
1086b9db7036SHongtao Yu
readFuncMetadata(bool ProfileHasAttribute,FunctionSamples * FProfile)10875740bb80SHongtao Yu SampleProfileReaderExtBinaryBase::readFuncMetadata(bool ProfileHasAttribute,
10885740bb80SHongtao Yu FunctionSamples *FProfile) {
10895740bb80SHongtao Yu if (Data < End) {
10901410db70SWenlei He if (ProfileIsProbeBased) {
1091ac068e01SHongtao Yu auto Checksum = readNumber<uint64_t>();
1092ac068e01SHongtao Yu if (std::error_code EC = Checksum.getError())
1093ac068e01SHongtao Yu return EC;
10945740bb80SHongtao Yu if (FProfile)
10955740bb80SHongtao Yu FProfile->setFunctionHash(*Checksum);
1096ac068e01SHongtao Yu }
1097224fee82SHongtao Yu
10981410db70SWenlei He if (ProfileHasAttribute) {
10991410db70SWenlei He auto Attributes = readNumber<uint32_t>();
11001410db70SWenlei He if (std::error_code EC = Attributes.getError())
11011410db70SWenlei He return EC;
11025740bb80SHongtao Yu if (FProfile)
11035740bb80SHongtao Yu FProfile->getContext().setAllAttributes(*Attributes);
11041410db70SWenlei He }
11055740bb80SHongtao Yu
1106e36786d1SHongtao Yu if (!ProfileIsCS) {
11075740bb80SHongtao Yu // Read all the attributes for inlined function calls.
11085740bb80SHongtao Yu auto NumCallsites = readNumber<uint32_t>();
11095740bb80SHongtao Yu if (std::error_code EC = NumCallsites.getError())
11105740bb80SHongtao Yu return EC;
11115740bb80SHongtao Yu
11125740bb80SHongtao Yu for (uint32_t J = 0; J < *NumCallsites; ++J) {
11135740bb80SHongtao Yu auto LineOffset = readNumber<uint64_t>();
11145740bb80SHongtao Yu if (std::error_code EC = LineOffset.getError())
11155740bb80SHongtao Yu return EC;
11165740bb80SHongtao Yu
11175740bb80SHongtao Yu auto Discriminator = readNumber<uint64_t>();
11185740bb80SHongtao Yu if (std::error_code EC = Discriminator.getError())
11195740bb80SHongtao Yu return EC;
11205740bb80SHongtao Yu
11215740bb80SHongtao Yu auto FContext(readSampleContextFromTable());
11225740bb80SHongtao Yu if (std::error_code EC = FContext.getError())
11235740bb80SHongtao Yu return EC;
11245740bb80SHongtao Yu
11255740bb80SHongtao Yu FunctionSamples *CalleeProfile = nullptr;
11265740bb80SHongtao Yu if (FProfile) {
11275740bb80SHongtao Yu CalleeProfile = const_cast<FunctionSamples *>(
11285740bb80SHongtao Yu &FProfile->functionSamplesAt(LineLocation(
11295740bb80SHongtao Yu *LineOffset,
11305740bb80SHongtao Yu *Discriminator))[std::string(FContext.get().getName())]);
11315740bb80SHongtao Yu }
11325740bb80SHongtao Yu if (std::error_code EC =
11335740bb80SHongtao Yu readFuncMetadata(ProfileHasAttribute, CalleeProfile))
11345740bb80SHongtao Yu return EC;
11355740bb80SHongtao Yu }
11365740bb80SHongtao Yu }
11375740bb80SHongtao Yu }
11385740bb80SHongtao Yu
11395740bb80SHongtao Yu return sampleprof_error::success;
11405740bb80SHongtao Yu }
11415740bb80SHongtao Yu
11425740bb80SHongtao Yu std::error_code
readFuncMetadata(bool ProfileHasAttribute)11435740bb80SHongtao Yu SampleProfileReaderExtBinaryBase::readFuncMetadata(bool ProfileHasAttribute) {
11445740bb80SHongtao Yu while (Data < End) {
11455740bb80SHongtao Yu auto FContext(readSampleContextFromTable());
11465740bb80SHongtao Yu if (std::error_code EC = FContext.getError())
11475740bb80SHongtao Yu return EC;
11485740bb80SHongtao Yu FunctionSamples *FProfile = nullptr;
11495740bb80SHongtao Yu auto It = Profiles.find(*FContext);
11505740bb80SHongtao Yu if (It != Profiles.end())
11515740bb80SHongtao Yu FProfile = &It->second;
11525740bb80SHongtao Yu
11535740bb80SHongtao Yu if (std::error_code EC = readFuncMetadata(ProfileHasAttribute, FProfile))
11545740bb80SHongtao Yu return EC;
11551410db70SWenlei He }
11561410db70SWenlei He
1157224fee82SHongtao Yu assert(Data == End && "More data is read than expected");
1158ac068e01SHongtao Yu return sampleprof_error::success;
1159ac068e01SHongtao Yu }
1160ac068e01SHongtao Yu
readNameTable()1161a0c0857eSWei Mi std::error_code SampleProfileReaderCompactBinary::readNameTable() {
1162a0c0857eSWei Mi auto Size = readNumber<uint64_t>();
1163a0c0857eSWei Mi if (std::error_code EC = Size.getError())
1164a0c0857eSWei Mi return EC;
1165a0c0857eSWei Mi NameTable.reserve(*Size);
1166a0c0857eSWei Mi for (uint32_t I = 0; I < *Size; ++I) {
1167a0c0857eSWei Mi auto FID = readNumber<uint64_t>();
1168a0c0857eSWei Mi if (std::error_code EC = FID.getError())
1169a0c0857eSWei Mi return EC;
1170a0c0857eSWei Mi NameTable.push_back(std::to_string(*FID));
1171a0c0857eSWei Mi }
1172a0c0857eSWei Mi return sampleprof_error::success;
1173a0c0857eSWei Mi }
1174a0c0857eSWei Mi
1175a906e3ecSWei Mi std::error_code
readSecHdrTableEntry(uint32_t Idx)1176a906e3ecSWei Mi SampleProfileReaderExtBinaryBase::readSecHdrTableEntry(uint32_t Idx) {
1177be907324SWei Mi SecHdrTableEntry Entry;
1178be907324SWei Mi auto Type = readUnencodedNumber<uint64_t>();
1179be907324SWei Mi if (std::error_code EC = Type.getError())
1180be907324SWei Mi return EC;
1181be907324SWei Mi Entry.Type = static_cast<SecType>(*Type);
1182c572e92cSDiego Novillo
1183b523790aSWei Mi auto Flags = readUnencodedNumber<uint64_t>();
1184b523790aSWei Mi if (std::error_code EC = Flags.getError())
1185be907324SWei Mi return EC;
1186b523790aSWei Mi Entry.Flags = *Flags;
1187be907324SWei Mi
1188be907324SWei Mi auto Offset = readUnencodedNumber<uint64_t>();
1189be907324SWei Mi if (std::error_code EC = Offset.getError())
1190be907324SWei Mi return EC;
1191be907324SWei Mi Entry.Offset = *Offset;
1192be907324SWei Mi
1193be907324SWei Mi auto Size = readUnencodedNumber<uint64_t>();
1194be907324SWei Mi if (std::error_code EC = Size.getError())
1195be907324SWei Mi return EC;
1196be907324SWei Mi Entry.Size = *Size;
1197be907324SWei Mi
1198a906e3ecSWei Mi Entry.LayoutIndex = Idx;
1199be907324SWei Mi SecHdrTable.push_back(std::move(Entry));
1200be907324SWei Mi return sampleprof_error::success;
1201be907324SWei Mi }
1202be907324SWei Mi
readSecHdrTable()1203be907324SWei Mi std::error_code SampleProfileReaderExtBinaryBase::readSecHdrTable() {
1204be907324SWei Mi auto EntryNum = readUnencodedNumber<uint64_t>();
1205be907324SWei Mi if (std::error_code EC = EntryNum.getError())
1206be907324SWei Mi return EC;
1207be907324SWei Mi
1208be907324SWei Mi for (uint32_t i = 0; i < (*EntryNum); i++)
1209a906e3ecSWei Mi if (std::error_code EC = readSecHdrTableEntry(i))
1210be907324SWei Mi return EC;
1211be907324SWei Mi
1212be907324SWei Mi return sampleprof_error::success;
1213be907324SWei Mi }
1214be907324SWei Mi
readHeader()1215be907324SWei Mi std::error_code SampleProfileReaderExtBinaryBase::readHeader() {
1216be907324SWei Mi const uint8_t *BufStart =
1217be907324SWei Mi reinterpret_cast<const uint8_t *>(Buffer->getBufferStart());
1218be907324SWei Mi Data = BufStart;
1219be907324SWei Mi End = BufStart + Buffer->getBufferSize();
1220be907324SWei Mi
1221be907324SWei Mi if (std::error_code EC = readMagicIdent())
1222be907324SWei Mi return EC;
1223be907324SWei Mi
1224be907324SWei Mi if (std::error_code EC = readSecHdrTable())
1225be907324SWei Mi return EC;
1226be907324SWei Mi
1227be907324SWei Mi return sampleprof_error::success;
1228be907324SWei Mi }
1229be907324SWei Mi
getSectionSize(SecType Type)1230eee532cdSWei Mi uint64_t SampleProfileReaderExtBinaryBase::getSectionSize(SecType Type) {
1231a906e3ecSWei Mi uint64_t Size = 0;
1232eee532cdSWei Mi for (auto &Entry : SecHdrTable) {
1233eee532cdSWei Mi if (Entry.Type == Type)
1234a906e3ecSWei Mi Size += Entry.Size;
1235eee532cdSWei Mi }
1236a906e3ecSWei Mi return Size;
1237eee532cdSWei Mi }
1238eee532cdSWei Mi
getFileSize()1239eee532cdSWei Mi uint64_t SampleProfileReaderExtBinaryBase::getFileSize() {
124009dcfe68SWei Mi // Sections in SecHdrTable is not necessarily in the same order as
124109dcfe68SWei Mi // sections in the profile because section like FuncOffsetTable needs
124209dcfe68SWei Mi // to be written after section LBRProfile but needs to be read before
124309dcfe68SWei Mi // section LBRProfile, so we cannot simply use the last entry in
124409dcfe68SWei Mi // SecHdrTable to calculate the file size.
124509dcfe68SWei Mi uint64_t FileSize = 0;
124609dcfe68SWei Mi for (auto &Entry : SecHdrTable) {
124709dcfe68SWei Mi FileSize = std::max(Entry.Offset + Entry.Size, FileSize);
124809dcfe68SWei Mi }
124909dcfe68SWei Mi return FileSize;
1250eee532cdSWei Mi }
1251eee532cdSWei Mi
getSecFlagsStr(const SecHdrTableEntry & Entry)1252b49eac71SWei Mi static std::string getSecFlagsStr(const SecHdrTableEntry &Entry) {
1253b49eac71SWei Mi std::string Flags;
1254b49eac71SWei Mi if (hasSecFlag(Entry, SecCommonFlags::SecFlagCompress))
1255b49eac71SWei Mi Flags.append("{compressed,");
1256b49eac71SWei Mi else
1257b49eac71SWei Mi Flags.append("{");
1258b49eac71SWei Mi
125921b1ad03SWei Mi if (hasSecFlag(Entry, SecCommonFlags::SecFlagFlat))
126021b1ad03SWei Mi Flags.append("flat,");
126121b1ad03SWei Mi
1262b49eac71SWei Mi switch (Entry.Type) {
1263b49eac71SWei Mi case SecNameTable:
126464e76853SWei Mi if (hasSecFlag(Entry, SecNameTableFlags::SecFlagFixedLengthMD5))
126564e76853SWei Mi Flags.append("fixlenmd5,");
126664e76853SWei Mi else if (hasSecFlag(Entry, SecNameTableFlags::SecFlagMD5Name))
1267b49eac71SWei Mi Flags.append("md5,");
1268ee35784aSWei Mi if (hasSecFlag(Entry, SecNameTableFlags::SecFlagUniqSuffix))
1269ee35784aSWei Mi Flags.append("uniq,");
1270b49eac71SWei Mi break;
1271b49eac71SWei Mi case SecProfSummary:
1272b49eac71SWei Mi if (hasSecFlag(Entry, SecProfSummaryFlags::SecFlagPartial))
1273b49eac71SWei Mi Flags.append("partial,");
1274a5d30421SWenlei He if (hasSecFlag(Entry, SecProfSummaryFlags::SecFlagFullContext))
1275a5d30421SWenlei He Flags.append("context,");
1276e36786d1SHongtao Yu if (hasSecFlag(Entry, SecProfSummaryFlags::SecFlagIsPreInlined))
1277e36786d1SHongtao Yu Flags.append("preInlined,");
12786745ffe4SRong Xu if (hasSecFlag(Entry, SecProfSummaryFlags::SecFlagFSDiscriminator))
12796745ffe4SRong Xu Flags.append("fs-discriminator,");
1280b49eac71SWei Mi break;
1281f4711e0dSHongtao Yu case SecFuncOffsetTable:
1282f4711e0dSHongtao Yu if (hasSecFlag(Entry, SecFuncOffsetFlags::SecFlagOrdered))
1283f4711e0dSHongtao Yu Flags.append("ordered,");
1284f4711e0dSHongtao Yu break;
1285d0eb472fSHongtao Yu case SecFuncMetadata:
1286d0eb472fSHongtao Yu if (hasSecFlag(Entry, SecFuncMetadataFlags::SecFlagIsProbeBased))
1287d0eb472fSHongtao Yu Flags.append("probe,");
1288d0eb472fSHongtao Yu if (hasSecFlag(Entry, SecFuncMetadataFlags::SecFlagHasAttribute))
1289d0eb472fSHongtao Yu Flags.append("attr,");
1290d0eb472fSHongtao Yu break;
1291b49eac71SWei Mi default:
1292b49eac71SWei Mi break;
1293b49eac71SWei Mi }
1294b49eac71SWei Mi char &last = Flags.back();
1295b49eac71SWei Mi if (last == ',')
1296b49eac71SWei Mi last = '}';
1297b49eac71SWei Mi else
1298b49eac71SWei Mi Flags.append("}");
1299b49eac71SWei Mi return Flags;
1300b49eac71SWei Mi }
1301b49eac71SWei Mi
dumpSectionInfo(raw_ostream & OS)1302eee532cdSWei Mi bool SampleProfileReaderExtBinaryBase::dumpSectionInfo(raw_ostream &OS) {
1303eee532cdSWei Mi uint64_t TotalSecsSize = 0;
1304eee532cdSWei Mi for (auto &Entry : SecHdrTable) {
1305eee532cdSWei Mi OS << getSecName(Entry.Type) << " - Offset: " << Entry.Offset
1306b49eac71SWei Mi << ", Size: " << Entry.Size << ", Flags: " << getSecFlagsStr(Entry)
1307b49eac71SWei Mi << "\n";
1308b49eac71SWei Mi ;
1309a906e3ecSWei Mi TotalSecsSize += Entry.Size;
1310eee532cdSWei Mi }
1311eee532cdSWei Mi uint64_t HeaderSize = SecHdrTable.front().Offset;
1312eee532cdSWei Mi assert(HeaderSize + TotalSecsSize == getFileSize() &&
1313eee532cdSWei Mi "Size of 'header + sections' doesn't match the total size of profile");
1314eee532cdSWei Mi
1315eee532cdSWei Mi OS << "Header Size: " << HeaderSize << "\n";
1316eee532cdSWei Mi OS << "Total Sections Size: " << TotalSecsSize << "\n";
1317eee532cdSWei Mi OS << "File Size: " << getFileSize() << "\n";
1318eee532cdSWei Mi return true;
1319eee532cdSWei Mi }
1320eee532cdSWei Mi
readMagicIdent()1321be907324SWei Mi std::error_code SampleProfileReaderBinary::readMagicIdent() {
1322c572e92cSDiego Novillo // Read and check the magic identifier.
1323c572e92cSDiego Novillo auto Magic = readNumber<uint64_t>();
1324c572e92cSDiego Novillo if (std::error_code EC = Magic.getError())
1325c572e92cSDiego Novillo return EC;
1326a0c0857eSWei Mi else if (std::error_code EC = verifySPMagic(*Magic))
1327c6b96c8dSWei Mi return EC;
1328c572e92cSDiego Novillo
1329c572e92cSDiego Novillo // Read the version number.
1330c572e92cSDiego Novillo auto Version = readNumber<uint64_t>();
1331c572e92cSDiego Novillo if (std::error_code EC = Version.getError())
1332c572e92cSDiego Novillo return EC;
1333c572e92cSDiego Novillo else if (*Version != SPVersion())
1334c572e92cSDiego Novillo return sampleprof_error::unsupported_version;
1335c572e92cSDiego Novillo
1336be907324SWei Mi return sampleprof_error::success;
1337be907324SWei Mi }
1338be907324SWei Mi
readHeader()1339be907324SWei Mi std::error_code SampleProfileReaderBinary::readHeader() {
1340be907324SWei Mi Data = reinterpret_cast<const uint8_t *>(Buffer->getBufferStart());
1341be907324SWei Mi End = Data + Buffer->getBufferSize();
1342be907324SWei Mi
1343be907324SWei Mi if (std::error_code EC = readMagicIdent())
1344be907324SWei Mi return EC;
1345be907324SWei Mi
134640ee23dbSEaswaran Raman if (std::error_code EC = readSummary())
134740ee23dbSEaswaran Raman return EC;
134840ee23dbSEaswaran Raman
1349a0c0857eSWei Mi if (std::error_code EC = readNameTable())
1350760c5a8fSDiego Novillo return EC;
1351c572e92cSDiego Novillo return sampleprof_error::success;
1352c572e92cSDiego Novillo }
1353c572e92cSDiego Novillo
readHeader()13546a14325dSWei Mi std::error_code SampleProfileReaderCompactBinary::readHeader() {
13556a14325dSWei Mi SampleProfileReaderBinary::readHeader();
13566a14325dSWei Mi if (std::error_code EC = readFuncOffsetTable())
13576a14325dSWei Mi return EC;
13586a14325dSWei Mi return sampleprof_error::success;
13596a14325dSWei Mi }
13606a14325dSWei Mi
readFuncOffsetTable()13616a14325dSWei Mi std::error_code SampleProfileReaderCompactBinary::readFuncOffsetTable() {
13626a14325dSWei Mi auto TableOffset = readUnencodedNumber<uint64_t>();
13636a14325dSWei Mi if (std::error_code EC = TableOffset.getError())
13646a14325dSWei Mi return EC;
13656a14325dSWei Mi
13666a14325dSWei Mi const uint8_t *SavedData = Data;
13676a14325dSWei Mi const uint8_t *TableStart =
13686a14325dSWei Mi reinterpret_cast<const uint8_t *>(Buffer->getBufferStart()) +
13696a14325dSWei Mi *TableOffset;
13706a14325dSWei Mi Data = TableStart;
13716a14325dSWei Mi
13726a14325dSWei Mi auto Size = readNumber<uint64_t>();
13736a14325dSWei Mi if (std::error_code EC = Size.getError())
13746a14325dSWei Mi return EC;
13756a14325dSWei Mi
13766a14325dSWei Mi FuncOffsetTable.reserve(*Size);
13776a14325dSWei Mi for (uint32_t I = 0; I < *Size; ++I) {
13786a14325dSWei Mi auto FName(readStringFromTable());
13796a14325dSWei Mi if (std::error_code EC = FName.getError())
13806a14325dSWei Mi return EC;
13816a14325dSWei Mi
13826a14325dSWei Mi auto Offset = readNumber<uint64_t>();
13836a14325dSWei Mi if (std::error_code EC = Offset.getError())
13846a14325dSWei Mi return EC;
13856a14325dSWei Mi
13866a14325dSWei Mi FuncOffsetTable[*FName] = *Offset;
13876a14325dSWei Mi }
13886a14325dSWei Mi End = TableStart;
13896a14325dSWei Mi Data = SavedData;
13906a14325dSWei Mi return sampleprof_error::success;
13916a14325dSWei Mi }
13926a14325dSWei Mi
collectFuncsFromModule()1393ee35784aSWei Mi bool SampleProfileReaderCompactBinary::collectFuncsFromModule() {
1394ee35784aSWei Mi if (!M)
1395ee35784aSWei Mi return false;
13966a14325dSWei Mi FuncsToUse.clear();
1397ee35784aSWei Mi for (auto &F : *M)
139809dcfe68SWei Mi FuncsToUse.insert(FunctionSamples::getCanonicalFnName(F));
1399ee35784aSWei Mi return true;
14006a14325dSWei Mi }
14016a14325dSWei Mi
readSummaryEntry(std::vector<ProfileSummaryEntry> & Entries)140240ee23dbSEaswaran Raman std::error_code SampleProfileReaderBinary::readSummaryEntry(
140340ee23dbSEaswaran Raman std::vector<ProfileSummaryEntry> &Entries) {
140440ee23dbSEaswaran Raman auto Cutoff = readNumber<uint64_t>();
140540ee23dbSEaswaran Raman if (std::error_code EC = Cutoff.getError())
140640ee23dbSEaswaran Raman return EC;
140740ee23dbSEaswaran Raman
140840ee23dbSEaswaran Raman auto MinBlockCount = readNumber<uint64_t>();
140940ee23dbSEaswaran Raman if (std::error_code EC = MinBlockCount.getError())
141040ee23dbSEaswaran Raman return EC;
141140ee23dbSEaswaran Raman
141240ee23dbSEaswaran Raman auto NumBlocks = readNumber<uint64_t>();
141340ee23dbSEaswaran Raman if (std::error_code EC = NumBlocks.getError())
141440ee23dbSEaswaran Raman return EC;
141540ee23dbSEaswaran Raman
141640ee23dbSEaswaran Raman Entries.emplace_back(*Cutoff, *MinBlockCount, *NumBlocks);
141740ee23dbSEaswaran Raman return sampleprof_error::success;
141840ee23dbSEaswaran Raman }
141940ee23dbSEaswaran Raman
readSummary()142040ee23dbSEaswaran Raman std::error_code SampleProfileReaderBinary::readSummary() {
142140ee23dbSEaswaran Raman auto TotalCount = readNumber<uint64_t>();
142240ee23dbSEaswaran Raman if (std::error_code EC = TotalCount.getError())
142340ee23dbSEaswaran Raman return EC;
142440ee23dbSEaswaran Raman
142540ee23dbSEaswaran Raman auto MaxBlockCount = readNumber<uint64_t>();
142640ee23dbSEaswaran Raman if (std::error_code EC = MaxBlockCount.getError())
142740ee23dbSEaswaran Raman return EC;
142840ee23dbSEaswaran Raman
142940ee23dbSEaswaran Raman auto MaxFunctionCount = readNumber<uint64_t>();
143040ee23dbSEaswaran Raman if (std::error_code EC = MaxFunctionCount.getError())
143140ee23dbSEaswaran Raman return EC;
143240ee23dbSEaswaran Raman
143340ee23dbSEaswaran Raman auto NumBlocks = readNumber<uint64_t>();
143440ee23dbSEaswaran Raman if (std::error_code EC = NumBlocks.getError())
143540ee23dbSEaswaran Raman return EC;
143640ee23dbSEaswaran Raman
143740ee23dbSEaswaran Raman auto NumFunctions = readNumber<uint64_t>();
143840ee23dbSEaswaran Raman if (std::error_code EC = NumFunctions.getError())
143940ee23dbSEaswaran Raman return EC;
144040ee23dbSEaswaran Raman
144140ee23dbSEaswaran Raman auto NumSummaryEntries = readNumber<uint64_t>();
144240ee23dbSEaswaran Raman if (std::error_code EC = NumSummaryEntries.getError())
144340ee23dbSEaswaran Raman return EC;
144440ee23dbSEaswaran Raman
144540ee23dbSEaswaran Raman std::vector<ProfileSummaryEntry> Entries;
144640ee23dbSEaswaran Raman for (unsigned i = 0; i < *NumSummaryEntries; i++) {
144740ee23dbSEaswaran Raman std::error_code EC = readSummaryEntry(Entries);
144840ee23dbSEaswaran Raman if (EC != sampleprof_error::success)
144940ee23dbSEaswaran Raman return EC;
145040ee23dbSEaswaran Raman }
14510eaee545SJonas Devlieghere Summary = std::make_unique<ProfileSummary>(
14527cefdb81SEaswaran Raman ProfileSummary::PSK_Sample, Entries, *TotalCount, *MaxBlockCount, 0,
14537cefdb81SEaswaran Raman *MaxFunctionCount, *NumBlocks, *NumFunctions);
145440ee23dbSEaswaran Raman
145540ee23dbSEaswaran Raman return sampleprof_error::success;
145640ee23dbSEaswaran Raman }
145740ee23dbSEaswaran Raman
hasFormat(const MemoryBuffer & Buffer)1458a0c0857eSWei Mi bool SampleProfileReaderRawBinary::hasFormat(const MemoryBuffer &Buffer) {
1459c572e92cSDiego Novillo const uint8_t *Data =
1460c572e92cSDiego Novillo reinterpret_cast<const uint8_t *>(Buffer.getBufferStart());
1461c572e92cSDiego Novillo uint64_t Magic = decodeULEB128(Data);
1462c572e92cSDiego Novillo return Magic == SPMagic();
1463c572e92cSDiego Novillo }
1464c572e92cSDiego Novillo
hasFormat(const MemoryBuffer & Buffer)1465be907324SWei Mi bool SampleProfileReaderExtBinary::hasFormat(const MemoryBuffer &Buffer) {
1466be907324SWei Mi const uint8_t *Data =
1467be907324SWei Mi reinterpret_cast<const uint8_t *>(Buffer.getBufferStart());
1468be907324SWei Mi uint64_t Magic = decodeULEB128(Data);
1469be907324SWei Mi return Magic == SPMagic(SPF_Ext_Binary);
1470be907324SWei Mi }
1471be907324SWei Mi
hasFormat(const MemoryBuffer & Buffer)1472a0c0857eSWei Mi bool SampleProfileReaderCompactBinary::hasFormat(const MemoryBuffer &Buffer) {
1473a0c0857eSWei Mi const uint8_t *Data =
1474a0c0857eSWei Mi reinterpret_cast<const uint8_t *>(Buffer.getBufferStart());
1475a0c0857eSWei Mi uint64_t Magic = decodeULEB128(Data);
1476a0c0857eSWei Mi return Magic == SPMagic(SPF_Compact_Binary);
1477a0c0857eSWei Mi }
1478a0c0857eSWei Mi
skipNextWord()14793376a787SDiego Novillo std::error_code SampleProfileReaderGCC::skipNextWord() {
14803376a787SDiego Novillo uint32_t dummy;
14813376a787SDiego Novillo if (!GcovBuffer.readInt(dummy))
14823376a787SDiego Novillo return sampleprof_error::truncated;
14833376a787SDiego Novillo return sampleprof_error::success;
14843376a787SDiego Novillo }
14853376a787SDiego Novillo
readNumber()14863376a787SDiego Novillo template <typename T> ErrorOr<T> SampleProfileReaderGCC::readNumber() {
14873376a787SDiego Novillo if (sizeof(T) <= sizeof(uint32_t)) {
14883376a787SDiego Novillo uint32_t Val;
14893376a787SDiego Novillo if (GcovBuffer.readInt(Val) && Val <= std::numeric_limits<T>::max())
14903376a787SDiego Novillo return static_cast<T>(Val);
14913376a787SDiego Novillo } else if (sizeof(T) <= sizeof(uint64_t)) {
14923376a787SDiego Novillo uint64_t Val;
14933376a787SDiego Novillo if (GcovBuffer.readInt64(Val) && Val <= std::numeric_limits<T>::max())
14943376a787SDiego Novillo return static_cast<T>(Val);
14953376a787SDiego Novillo }
14963376a787SDiego Novillo
14973376a787SDiego Novillo std::error_code EC = sampleprof_error::malformed;
14983376a787SDiego Novillo reportError(0, EC.message());
14993376a787SDiego Novillo return EC;
15003376a787SDiego Novillo }
15013376a787SDiego Novillo
readString()15023376a787SDiego Novillo ErrorOr<StringRef> SampleProfileReaderGCC::readString() {
15033376a787SDiego Novillo StringRef Str;
15043376a787SDiego Novillo if (!GcovBuffer.readString(Str))
15053376a787SDiego Novillo return sampleprof_error::truncated;
15063376a787SDiego Novillo return Str;
15073376a787SDiego Novillo }
15083376a787SDiego Novillo
readHeader()15093376a787SDiego Novillo std::error_code SampleProfileReaderGCC::readHeader() {
15103376a787SDiego Novillo // Read the magic identifier.
15113376a787SDiego Novillo if (!GcovBuffer.readGCDAFormat())
15123376a787SDiego Novillo return sampleprof_error::unrecognized_format;
15133376a787SDiego Novillo
15143376a787SDiego Novillo // Read the version number. Note - the GCC reader does not validate this
15153376a787SDiego Novillo // version, but the profile creator generates v704.
15163376a787SDiego Novillo GCOV::GCOVVersion version;
15173376a787SDiego Novillo if (!GcovBuffer.readGCOVVersion(version))
15183376a787SDiego Novillo return sampleprof_error::unrecognized_format;
15193376a787SDiego Novillo
15202d00eb17SFangrui Song if (version != GCOV::V407)
15213376a787SDiego Novillo return sampleprof_error::unsupported_version;
15223376a787SDiego Novillo
15233376a787SDiego Novillo // Skip the empty integer.
15243376a787SDiego Novillo if (std::error_code EC = skipNextWord())
15253376a787SDiego Novillo return EC;
15263376a787SDiego Novillo
15273376a787SDiego Novillo return sampleprof_error::success;
15283376a787SDiego Novillo }
15293376a787SDiego Novillo
readSectionTag(uint32_t Expected)15303376a787SDiego Novillo std::error_code SampleProfileReaderGCC::readSectionTag(uint32_t Expected) {
15313376a787SDiego Novillo uint32_t Tag;
15323376a787SDiego Novillo if (!GcovBuffer.readInt(Tag))
15333376a787SDiego Novillo return sampleprof_error::truncated;
15343376a787SDiego Novillo
15353376a787SDiego Novillo if (Tag != Expected)
15363376a787SDiego Novillo return sampleprof_error::malformed;
15373376a787SDiego Novillo
15383376a787SDiego Novillo if (std::error_code EC = skipNextWord())
15393376a787SDiego Novillo return EC;
15403376a787SDiego Novillo
15413376a787SDiego Novillo return sampleprof_error::success;
15423376a787SDiego Novillo }
15433376a787SDiego Novillo
readNameTable()15443376a787SDiego Novillo std::error_code SampleProfileReaderGCC::readNameTable() {
15453376a787SDiego Novillo if (std::error_code EC = readSectionTag(GCOVTagAFDOFileNames))
15463376a787SDiego Novillo return EC;
15473376a787SDiego Novillo
15483376a787SDiego Novillo uint32_t Size;
15493376a787SDiego Novillo if (!GcovBuffer.readInt(Size))
15503376a787SDiego Novillo return sampleprof_error::truncated;
15513376a787SDiego Novillo
15523376a787SDiego Novillo for (uint32_t I = 0; I < Size; ++I) {
15533376a787SDiego Novillo StringRef Str;
15543376a787SDiego Novillo if (!GcovBuffer.readString(Str))
15553376a787SDiego Novillo return sampleprof_error::truncated;
1556adcd0268SBenjamin Kramer Names.push_back(std::string(Str));
15573376a787SDiego Novillo }
15583376a787SDiego Novillo
15593376a787SDiego Novillo return sampleprof_error::success;
15603376a787SDiego Novillo }
15613376a787SDiego Novillo
readFunctionProfiles()15623376a787SDiego Novillo std::error_code SampleProfileReaderGCC::readFunctionProfiles() {
15633376a787SDiego Novillo if (std::error_code EC = readSectionTag(GCOVTagAFDOFunction))
15643376a787SDiego Novillo return EC;
15653376a787SDiego Novillo
15663376a787SDiego Novillo uint32_t NumFunctions;
15673376a787SDiego Novillo if (!GcovBuffer.readInt(NumFunctions))
15683376a787SDiego Novillo return sampleprof_error::truncated;
15693376a787SDiego Novillo
1570aae1ed8eSDiego Novillo InlineCallStack Stack;
15713376a787SDiego Novillo for (uint32_t I = 0; I < NumFunctions; ++I)
1572aae1ed8eSDiego Novillo if (std::error_code EC = readOneFunctionProfile(Stack, true, 0))
15733376a787SDiego Novillo return EC;
15743376a787SDiego Novillo
157540ee23dbSEaswaran Raman computeSummary();
15763376a787SDiego Novillo return sampleprof_error::success;
15773376a787SDiego Novillo }
15783376a787SDiego Novillo
readOneFunctionProfile(const InlineCallStack & InlineStack,bool Update,uint32_t Offset)1579aae1ed8eSDiego Novillo std::error_code SampleProfileReaderGCC::readOneFunctionProfile(
1580aae1ed8eSDiego Novillo const InlineCallStack &InlineStack, bool Update, uint32_t Offset) {
15813376a787SDiego Novillo uint64_t HeadCount = 0;
1582aae1ed8eSDiego Novillo if (InlineStack.size() == 0)
15833376a787SDiego Novillo if (!GcovBuffer.readInt64(HeadCount))
15843376a787SDiego Novillo return sampleprof_error::truncated;
15853376a787SDiego Novillo
15863376a787SDiego Novillo uint32_t NameIdx;
15873376a787SDiego Novillo if (!GcovBuffer.readInt(NameIdx))
15883376a787SDiego Novillo return sampleprof_error::truncated;
15893376a787SDiego Novillo
15903376a787SDiego Novillo StringRef Name(Names[NameIdx]);
15913376a787SDiego Novillo
15923376a787SDiego Novillo uint32_t NumPosCounts;
15933376a787SDiego Novillo if (!GcovBuffer.readInt(NumPosCounts))
15943376a787SDiego Novillo return sampleprof_error::truncated;
15953376a787SDiego Novillo
1596aae1ed8eSDiego Novillo uint32_t NumCallsites;
1597aae1ed8eSDiego Novillo if (!GcovBuffer.readInt(NumCallsites))
15983376a787SDiego Novillo return sampleprof_error::truncated;
15993376a787SDiego Novillo
1600aae1ed8eSDiego Novillo FunctionSamples *FProfile = nullptr;
1601aae1ed8eSDiego Novillo if (InlineStack.size() == 0) {
1602aae1ed8eSDiego Novillo // If this is a top function that we have already processed, do not
1603aae1ed8eSDiego Novillo // update its profile again. This happens in the presence of
1604aae1ed8eSDiego Novillo // function aliases. Since these aliases share the same function
1605aae1ed8eSDiego Novillo // body, there will be identical replicated profiles for the
1606aae1ed8eSDiego Novillo // original function. In this case, we simply not bother updating
1607aae1ed8eSDiego Novillo // the profile of the original function.
1608aae1ed8eSDiego Novillo FProfile = &Profiles[Name];
1609aae1ed8eSDiego Novillo FProfile->addHeadSamples(HeadCount);
1610aae1ed8eSDiego Novillo if (FProfile->getTotalSamples() > 0)
16113376a787SDiego Novillo Update = false;
1612aae1ed8eSDiego Novillo } else {
1613aae1ed8eSDiego Novillo // Otherwise, we are reading an inlined instance. The top of the
1614aae1ed8eSDiego Novillo // inline stack contains the profile of the caller. Insert this
1615aae1ed8eSDiego Novillo // callee in the caller's CallsiteMap.
1616aae1ed8eSDiego Novillo FunctionSamples *CallerProfile = InlineStack.front();
1617aae1ed8eSDiego Novillo uint32_t LineOffset = Offset >> 16;
1618aae1ed8eSDiego Novillo uint32_t Discriminator = Offset & 0xffff;
1619aae1ed8eSDiego Novillo FProfile = &CallerProfile->functionSamplesAt(
1620adcd0268SBenjamin Kramer LineLocation(LineOffset, Discriminator))[std::string(Name)];
16213376a787SDiego Novillo }
162257d1dda5SDehao Chen FProfile->setName(Name);
16233376a787SDiego Novillo
16243376a787SDiego Novillo for (uint32_t I = 0; I < NumPosCounts; ++I) {
16253376a787SDiego Novillo uint32_t Offset;
16263376a787SDiego Novillo if (!GcovBuffer.readInt(Offset))
16273376a787SDiego Novillo return sampleprof_error::truncated;
16283376a787SDiego Novillo
16293376a787SDiego Novillo uint32_t NumTargets;
16303376a787SDiego Novillo if (!GcovBuffer.readInt(NumTargets))
16313376a787SDiego Novillo return sampleprof_error::truncated;
16323376a787SDiego Novillo
16333376a787SDiego Novillo uint64_t Count;
16343376a787SDiego Novillo if (!GcovBuffer.readInt64(Count))
16353376a787SDiego Novillo return sampleprof_error::truncated;
16363376a787SDiego Novillo
1637aae1ed8eSDiego Novillo // The line location is encoded in the offset as:
1638aae1ed8eSDiego Novillo // high 16 bits: line offset to the start of the function.
1639aae1ed8eSDiego Novillo // low 16 bits: discriminator.
1640aae1ed8eSDiego Novillo uint32_t LineOffset = Offset >> 16;
1641aae1ed8eSDiego Novillo uint32_t Discriminator = Offset & 0xffff;
16423376a787SDiego Novillo
1643aae1ed8eSDiego Novillo InlineCallStack NewStack;
1644aae1ed8eSDiego Novillo NewStack.push_back(FProfile);
16451d0bc055SKazu Hirata llvm::append_range(NewStack, InlineStack);
1646aae1ed8eSDiego Novillo if (Update) {
1647aae1ed8eSDiego Novillo // Walk up the inline stack, adding the samples on this line to
1648aae1ed8eSDiego Novillo // the total sample count of the callers in the chain.
1649aae1ed8eSDiego Novillo for (auto CallerProfile : NewStack)
1650aae1ed8eSDiego Novillo CallerProfile->addTotalSamples(Count);
1651aae1ed8eSDiego Novillo
1652aae1ed8eSDiego Novillo // Update the body samples for the current profile.
1653aae1ed8eSDiego Novillo FProfile->addBodySamples(LineOffset, Discriminator, Count);
1654aae1ed8eSDiego Novillo }
1655aae1ed8eSDiego Novillo
1656aae1ed8eSDiego Novillo // Process the list of functions called at an indirect call site.
1657aae1ed8eSDiego Novillo // These are all the targets that a function pointer (or virtual
1658aae1ed8eSDiego Novillo // function) resolved at runtime.
16593376a787SDiego Novillo for (uint32_t J = 0; J < NumTargets; J++) {
16603376a787SDiego Novillo uint32_t HistVal;
16613376a787SDiego Novillo if (!GcovBuffer.readInt(HistVal))
16623376a787SDiego Novillo return sampleprof_error::truncated;
16633376a787SDiego Novillo
16643376a787SDiego Novillo if (HistVal != HIST_TYPE_INDIR_CALL_TOPN)
16653376a787SDiego Novillo return sampleprof_error::malformed;
16663376a787SDiego Novillo
16673376a787SDiego Novillo uint64_t TargetIdx;
16683376a787SDiego Novillo if (!GcovBuffer.readInt64(TargetIdx))
16693376a787SDiego Novillo return sampleprof_error::truncated;
16703376a787SDiego Novillo StringRef TargetName(Names[TargetIdx]);
16713376a787SDiego Novillo
16723376a787SDiego Novillo uint64_t TargetCount;
16733376a787SDiego Novillo if (!GcovBuffer.readInt64(TargetCount))
16743376a787SDiego Novillo return sampleprof_error::truncated;
16753376a787SDiego Novillo
1676920677a9SDehao Chen if (Update)
1677920677a9SDehao Chen FProfile->addCalledTargetSamples(LineOffset, Discriminator,
1678aae1ed8eSDiego Novillo TargetName, TargetCount);
16793376a787SDiego Novillo }
16803376a787SDiego Novillo }
16813376a787SDiego Novillo
1682aae1ed8eSDiego Novillo // Process all the inlined callers into the current function. These
1683aae1ed8eSDiego Novillo // are all the callsites that were inlined into this function.
1684aae1ed8eSDiego Novillo for (uint32_t I = 0; I < NumCallsites; I++) {
16853376a787SDiego Novillo // The offset is encoded as:
16863376a787SDiego Novillo // high 16 bits: line offset to the start of the function.
16873376a787SDiego Novillo // low 16 bits: discriminator.
16883376a787SDiego Novillo uint32_t Offset;
16893376a787SDiego Novillo if (!GcovBuffer.readInt(Offset))
16903376a787SDiego Novillo return sampleprof_error::truncated;
1691aae1ed8eSDiego Novillo InlineCallStack NewStack;
1692aae1ed8eSDiego Novillo NewStack.push_back(FProfile);
16931d0bc055SKazu Hirata llvm::append_range(NewStack, InlineStack);
1694aae1ed8eSDiego Novillo if (std::error_code EC = readOneFunctionProfile(NewStack, Update, Offset))
16953376a787SDiego Novillo return EC;
16963376a787SDiego Novillo }
16973376a787SDiego Novillo
16983376a787SDiego Novillo return sampleprof_error::success;
16993376a787SDiego Novillo }
17003376a787SDiego Novillo
17015f8f34e4SAdrian Prantl /// Read a GCC AutoFDO profile.
17023376a787SDiego Novillo ///
17033376a787SDiego Novillo /// This format is generated by the Linux Perf conversion tool at
17043376a787SDiego Novillo /// https://github.com/google/autofdo.
readImpl()17058c8ec1f6SWei Mi std::error_code SampleProfileReaderGCC::readImpl() {
17066745ffe4SRong Xu assert(!ProfileIsFSDisciminator && "Gcc profiles not support FSDisciminator");
17073376a787SDiego Novillo // Read the string table.
17083376a787SDiego Novillo if (std::error_code EC = readNameTable())
17093376a787SDiego Novillo return EC;
17103376a787SDiego Novillo
17113376a787SDiego Novillo // Read the source profile.
17123376a787SDiego Novillo if (std::error_code EC = readFunctionProfiles())
17133376a787SDiego Novillo return EC;
17143376a787SDiego Novillo
17153376a787SDiego Novillo return sampleprof_error::success;
17163376a787SDiego Novillo }
17173376a787SDiego Novillo
hasFormat(const MemoryBuffer & Buffer)17183376a787SDiego Novillo bool SampleProfileReaderGCC::hasFormat(const MemoryBuffer &Buffer) {
17193376a787SDiego Novillo StringRef Magic(reinterpret_cast<const char *>(Buffer.getBufferStart()));
17203376a787SDiego Novillo return Magic == "adcg*704";
17213376a787SDiego Novillo }
17223376a787SDiego Novillo
applyRemapping(LLVMContext & Ctx)17238c8ec1f6SWei Mi void SampleProfileReaderItaniumRemapper::applyRemapping(LLVMContext &Ctx) {
1724ebad6788SWei Mi // If the reader uses MD5 to represent string, we can't remap it because
172528436358SRichard Smith // we don't know what the original function names were.
1726ebad6788SWei Mi if (Reader.useMD5()) {
172728436358SRichard Smith Ctx.diagnose(DiagnosticInfoSampleProfile(
17288c8ec1f6SWei Mi Reader.getBuffer()->getBufferIdentifier(),
172928436358SRichard Smith "Profile data remapping cannot be applied to profile data "
173028436358SRichard Smith "in compact format (original mangled names are not available).",
173128436358SRichard Smith DS_Warning));
17328c8ec1f6SWei Mi return;
173328436358SRichard Smith }
173428436358SRichard Smith
17356b989a17SWenlei He // CSSPGO-TODO: Remapper is not yet supported.
17366b989a17SWenlei He // We will need to remap the entire context string.
17378c8ec1f6SWei Mi assert(Remappings && "should be initialized while creating remapper");
1738c67ccf5fSWei Mi for (auto &Sample : Reader.getProfiles()) {
1739c67ccf5fSWei Mi DenseSet<StringRef> NamesInSample;
1740c67ccf5fSWei Mi Sample.second.findAllNames(NamesInSample);
1741c67ccf5fSWei Mi for (auto &Name : NamesInSample)
1742c67ccf5fSWei Mi if (auto Key = Remappings->insert(Name))
1743c67ccf5fSWei Mi NameMap.insert({Key, Name});
1744c67ccf5fSWei Mi }
174528436358SRichard Smith
17468c8ec1f6SWei Mi RemappingApplied = true;
174728436358SRichard Smith }
174828436358SRichard Smith
1749c67ccf5fSWei Mi Optional<StringRef>
lookUpNameInProfile(StringRef Fname)1750c67ccf5fSWei Mi SampleProfileReaderItaniumRemapper::lookUpNameInProfile(StringRef Fname) {
17518c8ec1f6SWei Mi if (auto Key = Remappings->lookup(Fname))
1752c67ccf5fSWei Mi return NameMap.lookup(Key);
1753c67ccf5fSWei Mi return None;
175428436358SRichard Smith }
175528436358SRichard Smith
17565f8f34e4SAdrian Prantl /// Prepare a memory buffer for the contents of \p Filename.
1757de1ab26fSDiego Novillo ///
1758c572e92cSDiego Novillo /// \returns an error code indicating the status of the buffer.
1759fcd55607SDiego Novillo static ErrorOr<std::unique_ptr<MemoryBuffer>>
setupMemoryBuffer(const Twine & Filename)17600da23a27SBenjamin Kramer setupMemoryBuffer(const Twine &Filename) {
1761e71994a2SJonathan Crowther auto BufferOrErr = MemoryBuffer::getFileOrSTDIN(Filename, /*IsText=*/true);
1762c572e92cSDiego Novillo if (std::error_code EC = BufferOrErr.getError())
1763c572e92cSDiego Novillo return EC;
1764fcd55607SDiego Novillo auto Buffer = std::move(BufferOrErr.get());
1765c572e92cSDiego Novillo
176695875d24SZarko Todorovski // Check the file.
1767260fe3ecSZachary Turner if (uint64_t(Buffer->getBufferSize()) > std::numeric_limits<uint32_t>::max())
1768c572e92cSDiego Novillo return sampleprof_error::too_large;
1769c572e92cSDiego Novillo
1770c55cf4afSBill Wendling return std::move(Buffer);
1771c572e92cSDiego Novillo }
1772c572e92cSDiego Novillo
17735f8f34e4SAdrian Prantl /// Create a sample profile reader based on the format of the input file.
1774c572e92cSDiego Novillo ///
1775c572e92cSDiego Novillo /// \param Filename The file to open.
1776c572e92cSDiego Novillo ///
1777c572e92cSDiego Novillo /// \param C The LLVM context to use to emit diagnostics.
1778c572e92cSDiego Novillo ///
17798d581857SRong Xu /// \param P The FSDiscriminatorPass.
17808d581857SRong Xu ///
17818c8ec1f6SWei Mi /// \param RemapFilename The file used for profile remapping.
17828c8ec1f6SWei Mi ///
1783c572e92cSDiego Novillo /// \returns an error code indicating the status of the created reader.
1784fcd55607SDiego Novillo ErrorOr<std::unique_ptr<SampleProfileReader>>
create(const std::string Filename,LLVMContext & C,FSDiscriminatorPass P,const std::string RemapFilename)17858c8ec1f6SWei Mi SampleProfileReader::create(const std::string Filename, LLVMContext &C,
17868d581857SRong Xu FSDiscriminatorPass P,
17878c8ec1f6SWei Mi const std::string RemapFilename) {
1788fcd55607SDiego Novillo auto BufferOrError = setupMemoryBuffer(Filename);
1789fcd55607SDiego Novillo if (std::error_code EC = BufferOrError.getError())
1790c572e92cSDiego Novillo return EC;
17918d581857SRong Xu return create(BufferOrError.get(), C, P, RemapFilename);
179251abea74SNathan Slingerland }
1793c572e92cSDiego Novillo
179428436358SRichard Smith /// Create a sample profile remapper from the given input, to remap the
179528436358SRichard Smith /// function names in the given profile data.
179628436358SRichard Smith ///
179728436358SRichard Smith /// \param Filename The file to open.
179828436358SRichard Smith ///
17998c8ec1f6SWei Mi /// \param Reader The profile reader the remapper is going to be applied to.
18008c8ec1f6SWei Mi ///
180128436358SRichard Smith /// \param C The LLVM context to use to emit diagnostics.
180228436358SRichard Smith ///
180328436358SRichard Smith /// \returns an error code indicating the status of the created reader.
18048c8ec1f6SWei Mi ErrorOr<std::unique_ptr<SampleProfileReaderItaniumRemapper>>
create(const std::string Filename,SampleProfileReader & Reader,LLVMContext & C)18058c8ec1f6SWei Mi SampleProfileReaderItaniumRemapper::create(const std::string Filename,
18068c8ec1f6SWei Mi SampleProfileReader &Reader,
18078c8ec1f6SWei Mi LLVMContext &C) {
180828436358SRichard Smith auto BufferOrError = setupMemoryBuffer(Filename);
180928436358SRichard Smith if (std::error_code EC = BufferOrError.getError())
181028436358SRichard Smith return EC;
18118c8ec1f6SWei Mi return create(BufferOrError.get(), Reader, C);
18128c8ec1f6SWei Mi }
18138c8ec1f6SWei Mi
18148c8ec1f6SWei Mi /// Create a sample profile remapper from the given input, to remap the
18158c8ec1f6SWei Mi /// function names in the given profile data.
18168c8ec1f6SWei Mi ///
18178c8ec1f6SWei Mi /// \param B The memory buffer to create the reader from (assumes ownership).
18188c8ec1f6SWei Mi ///
18198c8ec1f6SWei Mi /// \param C The LLVM context to use to emit diagnostics.
18208c8ec1f6SWei Mi ///
18218c8ec1f6SWei Mi /// \param Reader The profile reader the remapper is going to be applied to.
18228c8ec1f6SWei Mi ///
18238c8ec1f6SWei Mi /// \returns an error code indicating the status of the created reader.
18248c8ec1f6SWei Mi ErrorOr<std::unique_ptr<SampleProfileReaderItaniumRemapper>>
create(std::unique_ptr<MemoryBuffer> & B,SampleProfileReader & Reader,LLVMContext & C)18258c8ec1f6SWei Mi SampleProfileReaderItaniumRemapper::create(std::unique_ptr<MemoryBuffer> &B,
18268c8ec1f6SWei Mi SampleProfileReader &Reader,
18278c8ec1f6SWei Mi LLVMContext &C) {
18288c8ec1f6SWei Mi auto Remappings = std::make_unique<SymbolRemappingReader>();
1829d3e5f0abSKazu Hirata if (Error E = Remappings->read(*B)) {
18308c8ec1f6SWei Mi handleAllErrors(
18318c8ec1f6SWei Mi std::move(E), [&](const SymbolRemappingParseError &ParseError) {
18328c8ec1f6SWei Mi C.diagnose(DiagnosticInfoSampleProfile(B->getBufferIdentifier(),
18338c8ec1f6SWei Mi ParseError.getLineNum(),
18348c8ec1f6SWei Mi ParseError.getMessage()));
18358c8ec1f6SWei Mi });
18368c8ec1f6SWei Mi return sampleprof_error::malformed;
18378c8ec1f6SWei Mi }
18388c8ec1f6SWei Mi
18390eaee545SJonas Devlieghere return std::make_unique<SampleProfileReaderItaniumRemapper>(
18408c8ec1f6SWei Mi std::move(B), std::move(Remappings), Reader);
184128436358SRichard Smith }
184228436358SRichard Smith
18435f8f34e4SAdrian Prantl /// Create a sample profile reader based on the format of the input data.
184451abea74SNathan Slingerland ///
184551abea74SNathan Slingerland /// \param B The memory buffer to create the reader from (assumes ownership).
184651abea74SNathan Slingerland ///
184751abea74SNathan Slingerland /// \param C The LLVM context to use to emit diagnostics.
184851abea74SNathan Slingerland ///
18498d581857SRong Xu /// \param P The FSDiscriminatorPass.
18508d581857SRong Xu ///
18518c8ec1f6SWei Mi /// \param RemapFilename The file used for profile remapping.
18528c8ec1f6SWei Mi ///
185351abea74SNathan Slingerland /// \returns an error code indicating the status of the created reader.
185451abea74SNathan Slingerland ErrorOr<std::unique_ptr<SampleProfileReader>>
create(std::unique_ptr<MemoryBuffer> & B,LLVMContext & C,FSDiscriminatorPass P,const std::string RemapFilename)18558c8ec1f6SWei Mi SampleProfileReader::create(std::unique_ptr<MemoryBuffer> &B, LLVMContext &C,
18568d581857SRong Xu FSDiscriminatorPass P,
18578c8ec1f6SWei Mi const std::string RemapFilename) {
1858fcd55607SDiego Novillo std::unique_ptr<SampleProfileReader> Reader;
1859a0c0857eSWei Mi if (SampleProfileReaderRawBinary::hasFormat(*B))
1860a0c0857eSWei Mi Reader.reset(new SampleProfileReaderRawBinary(std::move(B), C));
1861be907324SWei Mi else if (SampleProfileReaderExtBinary::hasFormat(*B))
1862be907324SWei Mi Reader.reset(new SampleProfileReaderExtBinary(std::move(B), C));
1863a0c0857eSWei Mi else if (SampleProfileReaderCompactBinary::hasFormat(*B))
1864a0c0857eSWei Mi Reader.reset(new SampleProfileReaderCompactBinary(std::move(B), C));
186551abea74SNathan Slingerland else if (SampleProfileReaderGCC::hasFormat(*B))
186651abea74SNathan Slingerland Reader.reset(new SampleProfileReaderGCC(std::move(B), C));
186751abea74SNathan Slingerland else if (SampleProfileReaderText::hasFormat(*B))
186851abea74SNathan Slingerland Reader.reset(new SampleProfileReaderText(std::move(B), C));
18694f823667SNathan Slingerland else
18704f823667SNathan Slingerland return sampleprof_error::unrecognized_format;
1871c572e92cSDiego Novillo
18728c8ec1f6SWei Mi if (!RemapFilename.empty()) {
18738c8ec1f6SWei Mi auto ReaderOrErr =
18748c8ec1f6SWei Mi SampleProfileReaderItaniumRemapper::create(RemapFilename, *Reader, C);
18758c8ec1f6SWei Mi if (std::error_code EC = ReaderOrErr.getError()) {
18768c8ec1f6SWei Mi std::string Msg = "Could not create remapper: " + EC.message();
18778c8ec1f6SWei Mi C.diagnose(DiagnosticInfoSampleProfile(RemapFilename, Msg));
18788c8ec1f6SWei Mi return EC;
18798c8ec1f6SWei Mi }
18808c8ec1f6SWei Mi Reader->Remapper = std::move(ReaderOrErr.get());
18818c8ec1f6SWei Mi }
18828c8ec1f6SWei Mi
1883be907324SWei Mi if (std::error_code EC = Reader->readHeader()) {
1884fcd55607SDiego Novillo return EC;
1885be907324SWei Mi }
1886fcd55607SDiego Novillo
18878d581857SRong Xu Reader->setDiscriminatorMaskedBitFrom(P);
18888d581857SRong Xu
1889c55cf4afSBill Wendling return std::move(Reader);
1890de1ab26fSDiego Novillo }
189140ee23dbSEaswaran Raman
189240ee23dbSEaswaran Raman // For text and GCC file formats, we compute the summary after reading the
189340ee23dbSEaswaran Raman // profile. Binary format has the profile summary in its header.
computeSummary()189440ee23dbSEaswaran Raman void SampleProfileReader::computeSummary() {
1895e5a17e3fSEaswaran Raman SampleProfileSummaryBuilder Builder(ProfileSummaryBuilder::DefaultCutoffs);
1896801d9cc7SWenlei He Summary = Builder.computeSummaryForProfiles(Profiles);
189740ee23dbSEaswaran Raman }
1898