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" 26*fc97efa4Sserge-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. 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. 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. 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). 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. 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. 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. 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) 3515740bb80SHongtao Yu ProfileIsCSNested = 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"); 3615740bb80SHongtao Yu ProfileIsCSFlat = (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; 3675740bb80SHongtao Yu FunctionSamples::ProfileIsCSFlat = ProfileIsCSFlat; 3685740bb80SHongtao Yu FunctionSamples::ProfileIsCSNested = ProfileIsCSNested; 3696b989a17SWenlei He 37040ee23dbSEaswaran Raman if (Result == sampleprof_error::success) 37140ee23dbSEaswaran Raman computeSummary(); 372de1ab26fSDiego Novillo 37348dd080cSNathan Slingerland return Result; 374de1ab26fSDiego Novillo } 375de1ab26fSDiego Novillo 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 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 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> 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> 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 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 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 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 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 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 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 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> 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> 632b9db7036SHongtao Yu SampleProfileReaderExtBinaryBase::readSampleContextFromTable() { 6335740bb80SHongtao Yu if (ProfileIsCSFlat) { 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 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)) 6575740bb80SHongtao Yu FunctionSamples::ProfileIsCSFlat = ProfileIsCSFlat = true; 658ff0b634dSHongtao Yu if (hasSecFlag(Entry, SecProfSummaryFlags::SecFlagIsCSNested)) 65962ef77caSHongtao Yu FunctionSamples::ProfileIsCSNested = ProfileIsCSNested = 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 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 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 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 7805740bb80SHongtao Yu if (ProfileIsCSFlat) { 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"); 8505740bb80SHongtao Yu assert((!CSProfileCount || ProfileIsCSFlat) && 851a5d30421SWenlei He "Section flag should be consistent with actual profile"); 85209dcfe68SWei Mi return sampleprof_error::success; 85309dcfe68SWei Mi } 85409dcfe68SWei Mi 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 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 880b523790aSWei Mi if (!llvm::zlib::isAvailable()) 881b523790aSWei Mi return sampleprof_error::zlib_unavailable; 882798e59b8SWei Mi 883b523790aSWei Mi StringRef CompressedStrings(reinterpret_cast<const char *>(Data), 884b523790aSWei Mi *CompressSize); 885b523790aSWei Mi char *Buffer = Allocator.Allocate<char>(DecompressBufSize); 886283df8cfSWei Mi size_t UCSize = DecompressBufSize; 887b523790aSWei Mi llvm::Error E = 888283df8cfSWei Mi zlib::uncompress(CompressedStrings, Buffer, UCSize); 889b523790aSWei Mi if (E) 890b523790aSWei Mi return sampleprof_error::uncompress_failed; 891b523790aSWei Mi DecompressBuf = reinterpret_cast<const uint8_t *>(Buffer); 892798e59b8SWei Mi return sampleprof_error::success; 893798e59b8SWei Mi } 894798e59b8SWei Mi 8958c8ec1f6SWei Mi std::error_code SampleProfileReaderExtBinaryBase::readImpl() { 896077a9c70SWei Mi const uint8_t *BufStart = 897077a9c70SWei Mi reinterpret_cast<const uint8_t *>(Buffer->getBufferStart()); 898077a9c70SWei Mi 899077a9c70SWei Mi for (auto &Entry : SecHdrTable) { 900077a9c70SWei Mi // Skip empty section. 901077a9c70SWei Mi if (!Entry.Size) 902077a9c70SWei Mi continue; 903b523790aSWei Mi 90421b1ad03SWei Mi // Skip sections without context when SkipFlatProf is true. 90521b1ad03SWei Mi if (SkipFlatProf && hasSecFlag(Entry, SecCommonFlags::SecFlagFlat)) 90621b1ad03SWei Mi continue; 90721b1ad03SWei Mi 908077a9c70SWei Mi const uint8_t *SecStart = BufStart + Entry.Offset; 909b523790aSWei Mi uint64_t SecSize = Entry.Size; 910b523790aSWei Mi 911b523790aSWei Mi // If the section is compressed, decompress it into a buffer 912b523790aSWei Mi // DecompressBuf before reading the actual data. The pointee of 913b523790aSWei Mi // 'Data' will be changed to buffer hold by DecompressBuf 914b523790aSWei Mi // temporarily when reading the actual data. 915ebad6788SWei Mi bool isCompressed = hasSecFlag(Entry, SecCommonFlags::SecFlagCompress); 916b523790aSWei Mi if (isCompressed) { 917b523790aSWei Mi const uint8_t *DecompressBuf; 918b523790aSWei Mi uint64_t DecompressBufSize; 919b523790aSWei Mi if (std::error_code EC = decompressSection( 920b523790aSWei Mi SecStart, SecSize, DecompressBuf, DecompressBufSize)) 921077a9c70SWei Mi return EC; 922b523790aSWei Mi SecStart = DecompressBuf; 923b523790aSWei Mi SecSize = DecompressBufSize; 924b523790aSWei Mi } 925b523790aSWei Mi 926ebad6788SWei Mi if (std::error_code EC = readOneSection(SecStart, SecSize, Entry)) 927b523790aSWei Mi return EC; 928b523790aSWei Mi if (Data != SecStart + SecSize) 929be907324SWei Mi return sampleprof_error::malformed; 930b523790aSWei Mi 931b523790aSWei Mi // Change the pointee of 'Data' from DecompressBuf to original Buffer. 932b523790aSWei Mi if (isCompressed) { 933b523790aSWei Mi Data = BufStart + Entry.Offset; 934b523790aSWei Mi End = BufStart + Buffer->getBufferSize(); 935b523790aSWei Mi } 936be907324SWei Mi } 937be907324SWei Mi 938be907324SWei Mi return sampleprof_error::success; 939be907324SWei Mi } 940be907324SWei Mi 9418c8ec1f6SWei Mi std::error_code SampleProfileReaderCompactBinary::readImpl() { 942ee35784aSWei Mi // Collect functions used by current module if the Reader has been 943ee35784aSWei Mi // given a module. 944ee35784aSWei Mi bool LoadFuncsToBeUsed = collectFuncsFromModule(); 9456745ffe4SRong Xu ProfileIsFS = ProfileIsFSDisciminator; 94624201b64SRong Xu FunctionSamples::ProfileIsFS = ProfileIsFS; 947d3289544SWenlei He std::vector<uint64_t> OffsetsToUse; 948ee35784aSWei Mi if (!LoadFuncsToBeUsed) { 949ee35784aSWei Mi // load all the function profiles. 950d3289544SWenlei He for (auto FuncEntry : FuncOffsetTable) { 951d3289544SWenlei He OffsetsToUse.push_back(FuncEntry.second); 952d3289544SWenlei He } 953ee35784aSWei Mi } else { 954ee35784aSWei Mi // load function profiles on demand. 9556a14325dSWei Mi for (auto Name : FuncsToUse) { 9566a14325dSWei Mi auto GUID = std::to_string(MD5Hash(Name)); 9576a14325dSWei Mi auto iter = FuncOffsetTable.find(StringRef(GUID)); 9586a14325dSWei Mi if (iter == FuncOffsetTable.end()) 9596a14325dSWei Mi continue; 960d3289544SWenlei He OffsetsToUse.push_back(iter->second); 961d3289544SWenlei He } 962d3289544SWenlei He } 963d3289544SWenlei He 964d3289544SWenlei He for (auto Offset : OffsetsToUse) { 9656a14325dSWei Mi const uint8_t *SavedData = Data; 96609dcfe68SWei Mi if (std::error_code EC = readFuncProfile( 96709dcfe68SWei Mi reinterpret_cast<const uint8_t *>(Buffer->getBufferStart()) + 96809dcfe68SWei Mi Offset)) 9696a14325dSWei Mi return EC; 9706a14325dSWei Mi Data = SavedData; 9716a14325dSWei Mi } 972c572e92cSDiego Novillo return sampleprof_error::success; 973c572e92cSDiego Novillo } 974c572e92cSDiego Novillo 975a0c0857eSWei Mi std::error_code SampleProfileReaderRawBinary::verifySPMagic(uint64_t Magic) { 976a0c0857eSWei Mi if (Magic == SPMagic()) 977a0c0857eSWei Mi return sampleprof_error::success; 978a0c0857eSWei Mi return sampleprof_error::bad_magic; 979a0c0857eSWei Mi } 980a0c0857eSWei Mi 981be907324SWei Mi std::error_code SampleProfileReaderExtBinary::verifySPMagic(uint64_t Magic) { 982be907324SWei Mi if (Magic == SPMagic(SPF_Ext_Binary)) 983be907324SWei Mi return sampleprof_error::success; 984be907324SWei Mi return sampleprof_error::bad_magic; 985be907324SWei Mi } 986be907324SWei Mi 987a0c0857eSWei Mi std::error_code 988a0c0857eSWei Mi SampleProfileReaderCompactBinary::verifySPMagic(uint64_t Magic) { 989a0c0857eSWei Mi if (Magic == SPMagic(SPF_Compact_Binary)) 990a0c0857eSWei Mi return sampleprof_error::success; 991a0c0857eSWei Mi return sampleprof_error::bad_magic; 992a0c0857eSWei Mi } 993a0c0857eSWei Mi 994be907324SWei Mi std::error_code SampleProfileReaderBinary::readNameTable() { 995a0c0857eSWei Mi auto Size = readNumber<uint32_t>(); 996a0c0857eSWei Mi if (std::error_code EC = Size.getError()) 997a0c0857eSWei Mi return EC; 998a906e3ecSWei Mi NameTable.reserve(*Size + NameTable.size()); 999a0c0857eSWei Mi for (uint32_t I = 0; I < *Size; ++I) { 1000a0c0857eSWei Mi auto Name(readString()); 1001a0c0857eSWei Mi if (std::error_code EC = Name.getError()) 1002a0c0857eSWei Mi return EC; 1003a0c0857eSWei Mi NameTable.push_back(*Name); 1004a0c0857eSWei Mi } 1005a0c0857eSWei Mi 1006a0c0857eSWei Mi return sampleprof_error::success; 1007a0c0857eSWei Mi } 1008a0c0857eSWei Mi 100993953d41SWei Mi std::error_code SampleProfileReaderExtBinaryBase::readMD5NameTable() { 1010ebad6788SWei Mi auto Size = readNumber<uint64_t>(); 1011ebad6788SWei Mi if (std::error_code EC = Size.getError()) 1012ebad6788SWei Mi return EC; 1013ebad6788SWei Mi MD5StringBuf = std::make_unique<std::vector<std::string>>(); 1014ebad6788SWei Mi MD5StringBuf->reserve(*Size); 101564e76853SWei Mi if (FixedLengthMD5) { 101664e76853SWei Mi // Preallocate and initialize NameTable so we can check whether a name 101764e76853SWei Mi // index has been read before by checking whether the element in the 101864e76853SWei Mi // NameTable is empty, meanwhile readStringIndex can do the boundary 101964e76853SWei Mi // check using the size of NameTable. 102064e76853SWei Mi NameTable.resize(*Size + NameTable.size()); 102164e76853SWei Mi 102264e76853SWei Mi MD5NameMemStart = Data; 102364e76853SWei Mi Data = Data + (*Size) * sizeof(uint64_t); 102464e76853SWei Mi return sampleprof_error::success; 102564e76853SWei Mi } 102664e76853SWei Mi NameTable.reserve(*Size); 1027ebad6788SWei Mi for (uint32_t I = 0; I < *Size; ++I) { 1028ebad6788SWei Mi auto FID = readNumber<uint64_t>(); 1029ebad6788SWei Mi if (std::error_code EC = FID.getError()) 1030ebad6788SWei Mi return EC; 1031ebad6788SWei Mi MD5StringBuf->push_back(std::to_string(*FID)); 1032ebad6788SWei Mi // NameTable is a vector of StringRef. Here it is pushing back a 1033ebad6788SWei Mi // StringRef initialized with the last string in MD5stringBuf. 1034ebad6788SWei Mi NameTable.push_back(MD5StringBuf->back()); 1035ebad6788SWei Mi } 1036ebad6788SWei Mi return sampleprof_error::success; 1037ebad6788SWei Mi } 1038ebad6788SWei Mi 103993953d41SWei Mi std::error_code SampleProfileReaderExtBinaryBase::readNameTableSec(bool IsMD5) { 1040ebad6788SWei Mi if (IsMD5) 1041ebad6788SWei Mi return readMD5NameTable(); 1042ebad6788SWei Mi return SampleProfileReaderBinary::readNameTable(); 1043ebad6788SWei Mi } 1044ebad6788SWei Mi 1045b9db7036SHongtao Yu // Read in the CS name table section, which basically contains a list of context 1046b9db7036SHongtao Yu // vectors. Each element of a context vector, aka a frame, refers to the 1047b9db7036SHongtao Yu // underlying raw function names that are stored in the name table, as well as 1048b9db7036SHongtao Yu // a callsite identifier that only makes sense for non-leaf frames. 1049b9db7036SHongtao Yu std::error_code SampleProfileReaderExtBinaryBase::readCSNameTableSec() { 1050b9db7036SHongtao Yu auto Size = readNumber<uint32_t>(); 1051b9db7036SHongtao Yu if (std::error_code EC = Size.getError()) 1052b9db7036SHongtao Yu return EC; 1053b9db7036SHongtao Yu 1054b9db7036SHongtao Yu std::vector<SampleContextFrameVector> *PNameVec = 1055b9db7036SHongtao Yu new std::vector<SampleContextFrameVector>(); 1056b9db7036SHongtao Yu PNameVec->reserve(*Size); 1057b9db7036SHongtao Yu for (uint32_t I = 0; I < *Size; ++I) { 1058b9db7036SHongtao Yu PNameVec->emplace_back(SampleContextFrameVector()); 1059b9db7036SHongtao Yu auto ContextSize = readNumber<uint32_t>(); 1060b9db7036SHongtao Yu if (std::error_code EC = ContextSize.getError()) 1061b9db7036SHongtao Yu return EC; 1062b9db7036SHongtao Yu for (uint32_t J = 0; J < *ContextSize; ++J) { 1063ac068e01SHongtao Yu auto FName(readStringFromTable()); 1064ac068e01SHongtao Yu if (std::error_code EC = FName.getError()) 1065ac068e01SHongtao Yu return EC; 1066b9db7036SHongtao Yu auto LineOffset = readNumber<uint64_t>(); 1067b9db7036SHongtao Yu if (std::error_code EC = LineOffset.getError()) 1068b9db7036SHongtao Yu return EC; 1069ac068e01SHongtao Yu 1070b9db7036SHongtao Yu if (!isOffsetLegal(*LineOffset)) 1071b9db7036SHongtao Yu return std::error_code(); 10721410db70SWenlei He 1073b9db7036SHongtao Yu auto Discriminator = readNumber<uint64_t>(); 1074b9db7036SHongtao Yu if (std::error_code EC = Discriminator.getError()) 1075b9db7036SHongtao Yu return EC; 1076b9db7036SHongtao Yu 1077b9db7036SHongtao Yu PNameVec->back().emplace_back( 1078b9db7036SHongtao Yu FName.get(), LineLocation(LineOffset.get(), Discriminator.get())); 1079b9db7036SHongtao Yu } 1080b9db7036SHongtao Yu } 1081b9db7036SHongtao Yu 1082b9db7036SHongtao Yu // From this point the underlying object of CSNameTable should be immutable. 1083b9db7036SHongtao Yu CSNameTable.reset(PNameVec); 1084b9db7036SHongtao Yu return sampleprof_error::success; 1085b9db7036SHongtao Yu } 1086b9db7036SHongtao Yu 1087b9db7036SHongtao Yu std::error_code 1088b9db7036SHongtao Yu 10895740bb80SHongtao Yu SampleProfileReaderExtBinaryBase::readFuncMetadata(bool ProfileHasAttribute, 10905740bb80SHongtao Yu FunctionSamples *FProfile) { 10915740bb80SHongtao Yu if (Data < End) { 10921410db70SWenlei He if (ProfileIsProbeBased) { 1093ac068e01SHongtao Yu auto Checksum = readNumber<uint64_t>(); 1094ac068e01SHongtao Yu if (std::error_code EC = Checksum.getError()) 1095ac068e01SHongtao Yu return EC; 10965740bb80SHongtao Yu if (FProfile) 10975740bb80SHongtao Yu FProfile->setFunctionHash(*Checksum); 1098ac068e01SHongtao Yu } 1099224fee82SHongtao Yu 11001410db70SWenlei He if (ProfileHasAttribute) { 11011410db70SWenlei He auto Attributes = readNumber<uint32_t>(); 11021410db70SWenlei He if (std::error_code EC = Attributes.getError()) 11031410db70SWenlei He return EC; 11045740bb80SHongtao Yu if (FProfile) 11055740bb80SHongtao Yu FProfile->getContext().setAllAttributes(*Attributes); 11061410db70SWenlei He } 11075740bb80SHongtao Yu 11085740bb80SHongtao Yu if (!ProfileIsCSFlat) { 11095740bb80SHongtao Yu // Read all the attributes for inlined function calls. 11105740bb80SHongtao Yu auto NumCallsites = readNumber<uint32_t>(); 11115740bb80SHongtao Yu if (std::error_code EC = NumCallsites.getError()) 11125740bb80SHongtao Yu return EC; 11135740bb80SHongtao Yu 11145740bb80SHongtao Yu for (uint32_t J = 0; J < *NumCallsites; ++J) { 11155740bb80SHongtao Yu auto LineOffset = readNumber<uint64_t>(); 11165740bb80SHongtao Yu if (std::error_code EC = LineOffset.getError()) 11175740bb80SHongtao Yu return EC; 11185740bb80SHongtao Yu 11195740bb80SHongtao Yu auto Discriminator = readNumber<uint64_t>(); 11205740bb80SHongtao Yu if (std::error_code EC = Discriminator.getError()) 11215740bb80SHongtao Yu return EC; 11225740bb80SHongtao Yu 11235740bb80SHongtao Yu auto FContext(readSampleContextFromTable()); 11245740bb80SHongtao Yu if (std::error_code EC = FContext.getError()) 11255740bb80SHongtao Yu return EC; 11265740bb80SHongtao Yu 11275740bb80SHongtao Yu FunctionSamples *CalleeProfile = nullptr; 11285740bb80SHongtao Yu if (FProfile) { 11295740bb80SHongtao Yu CalleeProfile = const_cast<FunctionSamples *>( 11305740bb80SHongtao Yu &FProfile->functionSamplesAt(LineLocation( 11315740bb80SHongtao Yu *LineOffset, 11325740bb80SHongtao Yu *Discriminator))[std::string(FContext.get().getName())]); 11335740bb80SHongtao Yu } 11345740bb80SHongtao Yu if (std::error_code EC = 11355740bb80SHongtao Yu readFuncMetadata(ProfileHasAttribute, CalleeProfile)) 11365740bb80SHongtao Yu return EC; 11375740bb80SHongtao Yu } 11385740bb80SHongtao Yu } 11395740bb80SHongtao Yu } 11405740bb80SHongtao Yu 11415740bb80SHongtao Yu return sampleprof_error::success; 11425740bb80SHongtao Yu } 11435740bb80SHongtao Yu 11445740bb80SHongtao Yu std::error_code 11455740bb80SHongtao Yu SampleProfileReaderExtBinaryBase::readFuncMetadata(bool ProfileHasAttribute) { 11465740bb80SHongtao Yu while (Data < End) { 11475740bb80SHongtao Yu auto FContext(readSampleContextFromTable()); 11485740bb80SHongtao Yu if (std::error_code EC = FContext.getError()) 11495740bb80SHongtao Yu return EC; 11505740bb80SHongtao Yu FunctionSamples *FProfile = nullptr; 11515740bb80SHongtao Yu auto It = Profiles.find(*FContext); 11525740bb80SHongtao Yu if (It != Profiles.end()) 11535740bb80SHongtao Yu FProfile = &It->second; 11545740bb80SHongtao Yu 11555740bb80SHongtao Yu if (std::error_code EC = readFuncMetadata(ProfileHasAttribute, FProfile)) 11565740bb80SHongtao Yu return EC; 11571410db70SWenlei He } 11581410db70SWenlei He 1159224fee82SHongtao Yu assert(Data == End && "More data is read than expected"); 1160ac068e01SHongtao Yu return sampleprof_error::success; 1161ac068e01SHongtao Yu } 1162ac068e01SHongtao Yu 1163a0c0857eSWei Mi std::error_code SampleProfileReaderCompactBinary::readNameTable() { 1164a0c0857eSWei Mi auto Size = readNumber<uint64_t>(); 1165a0c0857eSWei Mi if (std::error_code EC = Size.getError()) 1166a0c0857eSWei Mi return EC; 1167a0c0857eSWei Mi NameTable.reserve(*Size); 1168a0c0857eSWei Mi for (uint32_t I = 0; I < *Size; ++I) { 1169a0c0857eSWei Mi auto FID = readNumber<uint64_t>(); 1170a0c0857eSWei Mi if (std::error_code EC = FID.getError()) 1171a0c0857eSWei Mi return EC; 1172a0c0857eSWei Mi NameTable.push_back(std::to_string(*FID)); 1173a0c0857eSWei Mi } 1174a0c0857eSWei Mi return sampleprof_error::success; 1175a0c0857eSWei Mi } 1176a0c0857eSWei Mi 1177a906e3ecSWei Mi std::error_code 1178a906e3ecSWei Mi SampleProfileReaderExtBinaryBase::readSecHdrTableEntry(uint32_t Idx) { 1179be907324SWei Mi SecHdrTableEntry Entry; 1180be907324SWei Mi auto Type = readUnencodedNumber<uint64_t>(); 1181be907324SWei Mi if (std::error_code EC = Type.getError()) 1182be907324SWei Mi return EC; 1183be907324SWei Mi Entry.Type = static_cast<SecType>(*Type); 1184c572e92cSDiego Novillo 1185b523790aSWei Mi auto Flags = readUnencodedNumber<uint64_t>(); 1186b523790aSWei Mi if (std::error_code EC = Flags.getError()) 1187be907324SWei Mi return EC; 1188b523790aSWei Mi Entry.Flags = *Flags; 1189be907324SWei Mi 1190be907324SWei Mi auto Offset = readUnencodedNumber<uint64_t>(); 1191be907324SWei Mi if (std::error_code EC = Offset.getError()) 1192be907324SWei Mi return EC; 1193be907324SWei Mi Entry.Offset = *Offset; 1194be907324SWei Mi 1195be907324SWei Mi auto Size = readUnencodedNumber<uint64_t>(); 1196be907324SWei Mi if (std::error_code EC = Size.getError()) 1197be907324SWei Mi return EC; 1198be907324SWei Mi Entry.Size = *Size; 1199be907324SWei Mi 1200a906e3ecSWei Mi Entry.LayoutIndex = Idx; 1201be907324SWei Mi SecHdrTable.push_back(std::move(Entry)); 1202be907324SWei Mi return sampleprof_error::success; 1203be907324SWei Mi } 1204be907324SWei Mi 1205be907324SWei Mi std::error_code SampleProfileReaderExtBinaryBase::readSecHdrTable() { 1206be907324SWei Mi auto EntryNum = readUnencodedNumber<uint64_t>(); 1207be907324SWei Mi if (std::error_code EC = EntryNum.getError()) 1208be907324SWei Mi return EC; 1209be907324SWei Mi 1210be907324SWei Mi for (uint32_t i = 0; i < (*EntryNum); i++) 1211a906e3ecSWei Mi if (std::error_code EC = readSecHdrTableEntry(i)) 1212be907324SWei Mi return EC; 1213be907324SWei Mi 1214be907324SWei Mi return sampleprof_error::success; 1215be907324SWei Mi } 1216be907324SWei Mi 1217be907324SWei Mi std::error_code SampleProfileReaderExtBinaryBase::readHeader() { 1218be907324SWei Mi const uint8_t *BufStart = 1219be907324SWei Mi reinterpret_cast<const uint8_t *>(Buffer->getBufferStart()); 1220be907324SWei Mi Data = BufStart; 1221be907324SWei Mi End = BufStart + Buffer->getBufferSize(); 1222be907324SWei Mi 1223be907324SWei Mi if (std::error_code EC = readMagicIdent()) 1224be907324SWei Mi return EC; 1225be907324SWei Mi 1226be907324SWei Mi if (std::error_code EC = readSecHdrTable()) 1227be907324SWei Mi return EC; 1228be907324SWei Mi 1229be907324SWei Mi return sampleprof_error::success; 1230be907324SWei Mi } 1231be907324SWei Mi 1232eee532cdSWei Mi uint64_t SampleProfileReaderExtBinaryBase::getSectionSize(SecType Type) { 1233a906e3ecSWei Mi uint64_t Size = 0; 1234eee532cdSWei Mi for (auto &Entry : SecHdrTable) { 1235eee532cdSWei Mi if (Entry.Type == Type) 1236a906e3ecSWei Mi Size += Entry.Size; 1237eee532cdSWei Mi } 1238a906e3ecSWei Mi return Size; 1239eee532cdSWei Mi } 1240eee532cdSWei Mi 1241eee532cdSWei Mi uint64_t SampleProfileReaderExtBinaryBase::getFileSize() { 124209dcfe68SWei Mi // Sections in SecHdrTable is not necessarily in the same order as 124309dcfe68SWei Mi // sections in the profile because section like FuncOffsetTable needs 124409dcfe68SWei Mi // to be written after section LBRProfile but needs to be read before 124509dcfe68SWei Mi // section LBRProfile, so we cannot simply use the last entry in 124609dcfe68SWei Mi // SecHdrTable to calculate the file size. 124709dcfe68SWei Mi uint64_t FileSize = 0; 124809dcfe68SWei Mi for (auto &Entry : SecHdrTable) { 124909dcfe68SWei Mi FileSize = std::max(Entry.Offset + Entry.Size, FileSize); 125009dcfe68SWei Mi } 125109dcfe68SWei Mi return FileSize; 1252eee532cdSWei Mi } 1253eee532cdSWei Mi 1254b49eac71SWei Mi static std::string getSecFlagsStr(const SecHdrTableEntry &Entry) { 1255b49eac71SWei Mi std::string Flags; 1256b49eac71SWei Mi if (hasSecFlag(Entry, SecCommonFlags::SecFlagCompress)) 1257b49eac71SWei Mi Flags.append("{compressed,"); 1258b49eac71SWei Mi else 1259b49eac71SWei Mi Flags.append("{"); 1260b49eac71SWei Mi 126121b1ad03SWei Mi if (hasSecFlag(Entry, SecCommonFlags::SecFlagFlat)) 126221b1ad03SWei Mi Flags.append("flat,"); 126321b1ad03SWei Mi 1264b49eac71SWei Mi switch (Entry.Type) { 1265b49eac71SWei Mi case SecNameTable: 126664e76853SWei Mi if (hasSecFlag(Entry, SecNameTableFlags::SecFlagFixedLengthMD5)) 126764e76853SWei Mi Flags.append("fixlenmd5,"); 126864e76853SWei Mi else if (hasSecFlag(Entry, SecNameTableFlags::SecFlagMD5Name)) 1269b49eac71SWei Mi Flags.append("md5,"); 1270ee35784aSWei Mi if (hasSecFlag(Entry, SecNameTableFlags::SecFlagUniqSuffix)) 1271ee35784aSWei Mi Flags.append("uniq,"); 1272b49eac71SWei Mi break; 1273b49eac71SWei Mi case SecProfSummary: 1274b49eac71SWei Mi if (hasSecFlag(Entry, SecProfSummaryFlags::SecFlagPartial)) 1275b49eac71SWei Mi Flags.append("partial,"); 1276a5d30421SWenlei He if (hasSecFlag(Entry, SecProfSummaryFlags::SecFlagFullContext)) 1277a5d30421SWenlei He Flags.append("context,"); 1278ff0b634dSHongtao Yu if (hasSecFlag(Entry, SecProfSummaryFlags::SecFlagIsCSNested)) 1279ff0b634dSHongtao Yu Flags.append("context-nested,"); 12806745ffe4SRong Xu if (hasSecFlag(Entry, SecProfSummaryFlags::SecFlagFSDiscriminator)) 12816745ffe4SRong Xu Flags.append("fs-discriminator,"); 1282b49eac71SWei Mi break; 1283f4711e0dSHongtao Yu case SecFuncOffsetTable: 1284f4711e0dSHongtao Yu if (hasSecFlag(Entry, SecFuncOffsetFlags::SecFlagOrdered)) 1285f4711e0dSHongtao Yu Flags.append("ordered,"); 1286f4711e0dSHongtao Yu break; 1287d0eb472fSHongtao Yu case SecFuncMetadata: 1288d0eb472fSHongtao Yu if (hasSecFlag(Entry, SecFuncMetadataFlags::SecFlagIsProbeBased)) 1289d0eb472fSHongtao Yu Flags.append("probe,"); 1290d0eb472fSHongtao Yu if (hasSecFlag(Entry, SecFuncMetadataFlags::SecFlagHasAttribute)) 1291d0eb472fSHongtao Yu Flags.append("attr,"); 1292d0eb472fSHongtao Yu break; 1293b49eac71SWei Mi default: 1294b49eac71SWei Mi break; 1295b49eac71SWei Mi } 1296b49eac71SWei Mi char &last = Flags.back(); 1297b49eac71SWei Mi if (last == ',') 1298b49eac71SWei Mi last = '}'; 1299b49eac71SWei Mi else 1300b49eac71SWei Mi Flags.append("}"); 1301b49eac71SWei Mi return Flags; 1302b49eac71SWei Mi } 1303b49eac71SWei Mi 1304eee532cdSWei Mi bool SampleProfileReaderExtBinaryBase::dumpSectionInfo(raw_ostream &OS) { 1305eee532cdSWei Mi uint64_t TotalSecsSize = 0; 1306eee532cdSWei Mi for (auto &Entry : SecHdrTable) { 1307eee532cdSWei Mi OS << getSecName(Entry.Type) << " - Offset: " << Entry.Offset 1308b49eac71SWei Mi << ", Size: " << Entry.Size << ", Flags: " << getSecFlagsStr(Entry) 1309b49eac71SWei Mi << "\n"; 1310b49eac71SWei Mi ; 1311a906e3ecSWei Mi TotalSecsSize += Entry.Size; 1312eee532cdSWei Mi } 1313eee532cdSWei Mi uint64_t HeaderSize = SecHdrTable.front().Offset; 1314eee532cdSWei Mi assert(HeaderSize + TotalSecsSize == getFileSize() && 1315eee532cdSWei Mi "Size of 'header + sections' doesn't match the total size of profile"); 1316eee532cdSWei Mi 1317eee532cdSWei Mi OS << "Header Size: " << HeaderSize << "\n"; 1318eee532cdSWei Mi OS << "Total Sections Size: " << TotalSecsSize << "\n"; 1319eee532cdSWei Mi OS << "File Size: " << getFileSize() << "\n"; 1320eee532cdSWei Mi return true; 1321eee532cdSWei Mi } 1322eee532cdSWei Mi 1323be907324SWei Mi std::error_code SampleProfileReaderBinary::readMagicIdent() { 1324c572e92cSDiego Novillo // Read and check the magic identifier. 1325c572e92cSDiego Novillo auto Magic = readNumber<uint64_t>(); 1326c572e92cSDiego Novillo if (std::error_code EC = Magic.getError()) 1327c572e92cSDiego Novillo return EC; 1328a0c0857eSWei Mi else if (std::error_code EC = verifySPMagic(*Magic)) 1329c6b96c8dSWei Mi return EC; 1330c572e92cSDiego Novillo 1331c572e92cSDiego Novillo // Read the version number. 1332c572e92cSDiego Novillo auto Version = readNumber<uint64_t>(); 1333c572e92cSDiego Novillo if (std::error_code EC = Version.getError()) 1334c572e92cSDiego Novillo return EC; 1335c572e92cSDiego Novillo else if (*Version != SPVersion()) 1336c572e92cSDiego Novillo return sampleprof_error::unsupported_version; 1337c572e92cSDiego Novillo 1338be907324SWei Mi return sampleprof_error::success; 1339be907324SWei Mi } 1340be907324SWei Mi 1341be907324SWei Mi std::error_code SampleProfileReaderBinary::readHeader() { 1342be907324SWei Mi Data = reinterpret_cast<const uint8_t *>(Buffer->getBufferStart()); 1343be907324SWei Mi End = Data + Buffer->getBufferSize(); 1344be907324SWei Mi 1345be907324SWei Mi if (std::error_code EC = readMagicIdent()) 1346be907324SWei Mi return EC; 1347be907324SWei Mi 134840ee23dbSEaswaran Raman if (std::error_code EC = readSummary()) 134940ee23dbSEaswaran Raman return EC; 135040ee23dbSEaswaran Raman 1351a0c0857eSWei Mi if (std::error_code EC = readNameTable()) 1352760c5a8fSDiego Novillo return EC; 1353c572e92cSDiego Novillo return sampleprof_error::success; 1354c572e92cSDiego Novillo } 1355c572e92cSDiego Novillo 13566a14325dSWei Mi std::error_code SampleProfileReaderCompactBinary::readHeader() { 13576a14325dSWei Mi SampleProfileReaderBinary::readHeader(); 13586a14325dSWei Mi if (std::error_code EC = readFuncOffsetTable()) 13596a14325dSWei Mi return EC; 13606a14325dSWei Mi return sampleprof_error::success; 13616a14325dSWei Mi } 13626a14325dSWei Mi 13636a14325dSWei Mi std::error_code SampleProfileReaderCompactBinary::readFuncOffsetTable() { 13646a14325dSWei Mi auto TableOffset = readUnencodedNumber<uint64_t>(); 13656a14325dSWei Mi if (std::error_code EC = TableOffset.getError()) 13666a14325dSWei Mi return EC; 13676a14325dSWei Mi 13686a14325dSWei Mi const uint8_t *SavedData = Data; 13696a14325dSWei Mi const uint8_t *TableStart = 13706a14325dSWei Mi reinterpret_cast<const uint8_t *>(Buffer->getBufferStart()) + 13716a14325dSWei Mi *TableOffset; 13726a14325dSWei Mi Data = TableStart; 13736a14325dSWei Mi 13746a14325dSWei Mi auto Size = readNumber<uint64_t>(); 13756a14325dSWei Mi if (std::error_code EC = Size.getError()) 13766a14325dSWei Mi return EC; 13776a14325dSWei Mi 13786a14325dSWei Mi FuncOffsetTable.reserve(*Size); 13796a14325dSWei Mi for (uint32_t I = 0; I < *Size; ++I) { 13806a14325dSWei Mi auto FName(readStringFromTable()); 13816a14325dSWei Mi if (std::error_code EC = FName.getError()) 13826a14325dSWei Mi return EC; 13836a14325dSWei Mi 13846a14325dSWei Mi auto Offset = readNumber<uint64_t>(); 13856a14325dSWei Mi if (std::error_code EC = Offset.getError()) 13866a14325dSWei Mi return EC; 13876a14325dSWei Mi 13886a14325dSWei Mi FuncOffsetTable[*FName] = *Offset; 13896a14325dSWei Mi } 13906a14325dSWei Mi End = TableStart; 13916a14325dSWei Mi Data = SavedData; 13926a14325dSWei Mi return sampleprof_error::success; 13936a14325dSWei Mi } 13946a14325dSWei Mi 1395ee35784aSWei Mi bool SampleProfileReaderCompactBinary::collectFuncsFromModule() { 1396ee35784aSWei Mi if (!M) 1397ee35784aSWei Mi return false; 13986a14325dSWei Mi FuncsToUse.clear(); 1399ee35784aSWei Mi for (auto &F : *M) 140009dcfe68SWei Mi FuncsToUse.insert(FunctionSamples::getCanonicalFnName(F)); 1401ee35784aSWei Mi return true; 14026a14325dSWei Mi } 14036a14325dSWei Mi 140440ee23dbSEaswaran Raman std::error_code SampleProfileReaderBinary::readSummaryEntry( 140540ee23dbSEaswaran Raman std::vector<ProfileSummaryEntry> &Entries) { 140640ee23dbSEaswaran Raman auto Cutoff = readNumber<uint64_t>(); 140740ee23dbSEaswaran Raman if (std::error_code EC = Cutoff.getError()) 140840ee23dbSEaswaran Raman return EC; 140940ee23dbSEaswaran Raman 141040ee23dbSEaswaran Raman auto MinBlockCount = readNumber<uint64_t>(); 141140ee23dbSEaswaran Raman if (std::error_code EC = MinBlockCount.getError()) 141240ee23dbSEaswaran Raman return EC; 141340ee23dbSEaswaran Raman 141440ee23dbSEaswaran Raman auto NumBlocks = readNumber<uint64_t>(); 141540ee23dbSEaswaran Raman if (std::error_code EC = NumBlocks.getError()) 141640ee23dbSEaswaran Raman return EC; 141740ee23dbSEaswaran Raman 141840ee23dbSEaswaran Raman Entries.emplace_back(*Cutoff, *MinBlockCount, *NumBlocks); 141940ee23dbSEaswaran Raman return sampleprof_error::success; 142040ee23dbSEaswaran Raman } 142140ee23dbSEaswaran Raman 142240ee23dbSEaswaran Raman std::error_code SampleProfileReaderBinary::readSummary() { 142340ee23dbSEaswaran Raman auto TotalCount = readNumber<uint64_t>(); 142440ee23dbSEaswaran Raman if (std::error_code EC = TotalCount.getError()) 142540ee23dbSEaswaran Raman return EC; 142640ee23dbSEaswaran Raman 142740ee23dbSEaswaran Raman auto MaxBlockCount = readNumber<uint64_t>(); 142840ee23dbSEaswaran Raman if (std::error_code EC = MaxBlockCount.getError()) 142940ee23dbSEaswaran Raman return EC; 143040ee23dbSEaswaran Raman 143140ee23dbSEaswaran Raman auto MaxFunctionCount = readNumber<uint64_t>(); 143240ee23dbSEaswaran Raman if (std::error_code EC = MaxFunctionCount.getError()) 143340ee23dbSEaswaran Raman return EC; 143440ee23dbSEaswaran Raman 143540ee23dbSEaswaran Raman auto NumBlocks = readNumber<uint64_t>(); 143640ee23dbSEaswaran Raman if (std::error_code EC = NumBlocks.getError()) 143740ee23dbSEaswaran Raman return EC; 143840ee23dbSEaswaran Raman 143940ee23dbSEaswaran Raman auto NumFunctions = readNumber<uint64_t>(); 144040ee23dbSEaswaran Raman if (std::error_code EC = NumFunctions.getError()) 144140ee23dbSEaswaran Raman return EC; 144240ee23dbSEaswaran Raman 144340ee23dbSEaswaran Raman auto NumSummaryEntries = readNumber<uint64_t>(); 144440ee23dbSEaswaran Raman if (std::error_code EC = NumSummaryEntries.getError()) 144540ee23dbSEaswaran Raman return EC; 144640ee23dbSEaswaran Raman 144740ee23dbSEaswaran Raman std::vector<ProfileSummaryEntry> Entries; 144840ee23dbSEaswaran Raman for (unsigned i = 0; i < *NumSummaryEntries; i++) { 144940ee23dbSEaswaran Raman std::error_code EC = readSummaryEntry(Entries); 145040ee23dbSEaswaran Raman if (EC != sampleprof_error::success) 145140ee23dbSEaswaran Raman return EC; 145240ee23dbSEaswaran Raman } 14530eaee545SJonas Devlieghere Summary = std::make_unique<ProfileSummary>( 14547cefdb81SEaswaran Raman ProfileSummary::PSK_Sample, Entries, *TotalCount, *MaxBlockCount, 0, 14557cefdb81SEaswaran Raman *MaxFunctionCount, *NumBlocks, *NumFunctions); 145640ee23dbSEaswaran Raman 145740ee23dbSEaswaran Raman return sampleprof_error::success; 145840ee23dbSEaswaran Raman } 145940ee23dbSEaswaran Raman 1460a0c0857eSWei Mi bool SampleProfileReaderRawBinary::hasFormat(const MemoryBuffer &Buffer) { 1461c572e92cSDiego Novillo const uint8_t *Data = 1462c572e92cSDiego Novillo reinterpret_cast<const uint8_t *>(Buffer.getBufferStart()); 1463c572e92cSDiego Novillo uint64_t Magic = decodeULEB128(Data); 1464c572e92cSDiego Novillo return Magic == SPMagic(); 1465c572e92cSDiego Novillo } 1466c572e92cSDiego Novillo 1467be907324SWei Mi bool SampleProfileReaderExtBinary::hasFormat(const MemoryBuffer &Buffer) { 1468be907324SWei Mi const uint8_t *Data = 1469be907324SWei Mi reinterpret_cast<const uint8_t *>(Buffer.getBufferStart()); 1470be907324SWei Mi uint64_t Magic = decodeULEB128(Data); 1471be907324SWei Mi return Magic == SPMagic(SPF_Ext_Binary); 1472be907324SWei Mi } 1473be907324SWei Mi 1474a0c0857eSWei Mi bool SampleProfileReaderCompactBinary::hasFormat(const MemoryBuffer &Buffer) { 1475a0c0857eSWei Mi const uint8_t *Data = 1476a0c0857eSWei Mi reinterpret_cast<const uint8_t *>(Buffer.getBufferStart()); 1477a0c0857eSWei Mi uint64_t Magic = decodeULEB128(Data); 1478a0c0857eSWei Mi return Magic == SPMagic(SPF_Compact_Binary); 1479a0c0857eSWei Mi } 1480a0c0857eSWei Mi 14813376a787SDiego Novillo std::error_code SampleProfileReaderGCC::skipNextWord() { 14823376a787SDiego Novillo uint32_t dummy; 14833376a787SDiego Novillo if (!GcovBuffer.readInt(dummy)) 14843376a787SDiego Novillo return sampleprof_error::truncated; 14853376a787SDiego Novillo return sampleprof_error::success; 14863376a787SDiego Novillo } 14873376a787SDiego Novillo 14883376a787SDiego Novillo template <typename T> ErrorOr<T> SampleProfileReaderGCC::readNumber() { 14893376a787SDiego Novillo if (sizeof(T) <= sizeof(uint32_t)) { 14903376a787SDiego Novillo uint32_t Val; 14913376a787SDiego Novillo if (GcovBuffer.readInt(Val) && Val <= std::numeric_limits<T>::max()) 14923376a787SDiego Novillo return static_cast<T>(Val); 14933376a787SDiego Novillo } else if (sizeof(T) <= sizeof(uint64_t)) { 14943376a787SDiego Novillo uint64_t Val; 14953376a787SDiego Novillo if (GcovBuffer.readInt64(Val) && Val <= std::numeric_limits<T>::max()) 14963376a787SDiego Novillo return static_cast<T>(Val); 14973376a787SDiego Novillo } 14983376a787SDiego Novillo 14993376a787SDiego Novillo std::error_code EC = sampleprof_error::malformed; 15003376a787SDiego Novillo reportError(0, EC.message()); 15013376a787SDiego Novillo return EC; 15023376a787SDiego Novillo } 15033376a787SDiego Novillo 15043376a787SDiego Novillo ErrorOr<StringRef> SampleProfileReaderGCC::readString() { 15053376a787SDiego Novillo StringRef Str; 15063376a787SDiego Novillo if (!GcovBuffer.readString(Str)) 15073376a787SDiego Novillo return sampleprof_error::truncated; 15083376a787SDiego Novillo return Str; 15093376a787SDiego Novillo } 15103376a787SDiego Novillo 15113376a787SDiego Novillo std::error_code SampleProfileReaderGCC::readHeader() { 15123376a787SDiego Novillo // Read the magic identifier. 15133376a787SDiego Novillo if (!GcovBuffer.readGCDAFormat()) 15143376a787SDiego Novillo return sampleprof_error::unrecognized_format; 15153376a787SDiego Novillo 15163376a787SDiego Novillo // Read the version number. Note - the GCC reader does not validate this 15173376a787SDiego Novillo // version, but the profile creator generates v704. 15183376a787SDiego Novillo GCOV::GCOVVersion version; 15193376a787SDiego Novillo if (!GcovBuffer.readGCOVVersion(version)) 15203376a787SDiego Novillo return sampleprof_error::unrecognized_format; 15213376a787SDiego Novillo 15222d00eb17SFangrui Song if (version != GCOV::V407) 15233376a787SDiego Novillo return sampleprof_error::unsupported_version; 15243376a787SDiego Novillo 15253376a787SDiego Novillo // Skip the empty integer. 15263376a787SDiego Novillo if (std::error_code EC = skipNextWord()) 15273376a787SDiego Novillo return EC; 15283376a787SDiego Novillo 15293376a787SDiego Novillo return sampleprof_error::success; 15303376a787SDiego Novillo } 15313376a787SDiego Novillo 15323376a787SDiego Novillo std::error_code SampleProfileReaderGCC::readSectionTag(uint32_t Expected) { 15333376a787SDiego Novillo uint32_t Tag; 15343376a787SDiego Novillo if (!GcovBuffer.readInt(Tag)) 15353376a787SDiego Novillo return sampleprof_error::truncated; 15363376a787SDiego Novillo 15373376a787SDiego Novillo if (Tag != Expected) 15383376a787SDiego Novillo return sampleprof_error::malformed; 15393376a787SDiego Novillo 15403376a787SDiego Novillo if (std::error_code EC = skipNextWord()) 15413376a787SDiego Novillo return EC; 15423376a787SDiego Novillo 15433376a787SDiego Novillo return sampleprof_error::success; 15443376a787SDiego Novillo } 15453376a787SDiego Novillo 15463376a787SDiego Novillo std::error_code SampleProfileReaderGCC::readNameTable() { 15473376a787SDiego Novillo if (std::error_code EC = readSectionTag(GCOVTagAFDOFileNames)) 15483376a787SDiego Novillo return EC; 15493376a787SDiego Novillo 15503376a787SDiego Novillo uint32_t Size; 15513376a787SDiego Novillo if (!GcovBuffer.readInt(Size)) 15523376a787SDiego Novillo return sampleprof_error::truncated; 15533376a787SDiego Novillo 15543376a787SDiego Novillo for (uint32_t I = 0; I < Size; ++I) { 15553376a787SDiego Novillo StringRef Str; 15563376a787SDiego Novillo if (!GcovBuffer.readString(Str)) 15573376a787SDiego Novillo return sampleprof_error::truncated; 1558adcd0268SBenjamin Kramer Names.push_back(std::string(Str)); 15593376a787SDiego Novillo } 15603376a787SDiego Novillo 15613376a787SDiego Novillo return sampleprof_error::success; 15623376a787SDiego Novillo } 15633376a787SDiego Novillo 15643376a787SDiego Novillo std::error_code SampleProfileReaderGCC::readFunctionProfiles() { 15653376a787SDiego Novillo if (std::error_code EC = readSectionTag(GCOVTagAFDOFunction)) 15663376a787SDiego Novillo return EC; 15673376a787SDiego Novillo 15683376a787SDiego Novillo uint32_t NumFunctions; 15693376a787SDiego Novillo if (!GcovBuffer.readInt(NumFunctions)) 15703376a787SDiego Novillo return sampleprof_error::truncated; 15713376a787SDiego Novillo 1572aae1ed8eSDiego Novillo InlineCallStack Stack; 15733376a787SDiego Novillo for (uint32_t I = 0; I < NumFunctions; ++I) 1574aae1ed8eSDiego Novillo if (std::error_code EC = readOneFunctionProfile(Stack, true, 0)) 15753376a787SDiego Novillo return EC; 15763376a787SDiego Novillo 157740ee23dbSEaswaran Raman computeSummary(); 15783376a787SDiego Novillo return sampleprof_error::success; 15793376a787SDiego Novillo } 15803376a787SDiego Novillo 1581aae1ed8eSDiego Novillo std::error_code SampleProfileReaderGCC::readOneFunctionProfile( 1582aae1ed8eSDiego Novillo const InlineCallStack &InlineStack, bool Update, uint32_t Offset) { 15833376a787SDiego Novillo uint64_t HeadCount = 0; 1584aae1ed8eSDiego Novillo if (InlineStack.size() == 0) 15853376a787SDiego Novillo if (!GcovBuffer.readInt64(HeadCount)) 15863376a787SDiego Novillo return sampleprof_error::truncated; 15873376a787SDiego Novillo 15883376a787SDiego Novillo uint32_t NameIdx; 15893376a787SDiego Novillo if (!GcovBuffer.readInt(NameIdx)) 15903376a787SDiego Novillo return sampleprof_error::truncated; 15913376a787SDiego Novillo 15923376a787SDiego Novillo StringRef Name(Names[NameIdx]); 15933376a787SDiego Novillo 15943376a787SDiego Novillo uint32_t NumPosCounts; 15953376a787SDiego Novillo if (!GcovBuffer.readInt(NumPosCounts)) 15963376a787SDiego Novillo return sampleprof_error::truncated; 15973376a787SDiego Novillo 1598aae1ed8eSDiego Novillo uint32_t NumCallsites; 1599aae1ed8eSDiego Novillo if (!GcovBuffer.readInt(NumCallsites)) 16003376a787SDiego Novillo return sampleprof_error::truncated; 16013376a787SDiego Novillo 1602aae1ed8eSDiego Novillo FunctionSamples *FProfile = nullptr; 1603aae1ed8eSDiego Novillo if (InlineStack.size() == 0) { 1604aae1ed8eSDiego Novillo // If this is a top function that we have already processed, do not 1605aae1ed8eSDiego Novillo // update its profile again. This happens in the presence of 1606aae1ed8eSDiego Novillo // function aliases. Since these aliases share the same function 1607aae1ed8eSDiego Novillo // body, there will be identical replicated profiles for the 1608aae1ed8eSDiego Novillo // original function. In this case, we simply not bother updating 1609aae1ed8eSDiego Novillo // the profile of the original function. 1610aae1ed8eSDiego Novillo FProfile = &Profiles[Name]; 1611aae1ed8eSDiego Novillo FProfile->addHeadSamples(HeadCount); 1612aae1ed8eSDiego Novillo if (FProfile->getTotalSamples() > 0) 16133376a787SDiego Novillo Update = false; 1614aae1ed8eSDiego Novillo } else { 1615aae1ed8eSDiego Novillo // Otherwise, we are reading an inlined instance. The top of the 1616aae1ed8eSDiego Novillo // inline stack contains the profile of the caller. Insert this 1617aae1ed8eSDiego Novillo // callee in the caller's CallsiteMap. 1618aae1ed8eSDiego Novillo FunctionSamples *CallerProfile = InlineStack.front(); 1619aae1ed8eSDiego Novillo uint32_t LineOffset = Offset >> 16; 1620aae1ed8eSDiego Novillo uint32_t Discriminator = Offset & 0xffff; 1621aae1ed8eSDiego Novillo FProfile = &CallerProfile->functionSamplesAt( 1622adcd0268SBenjamin Kramer LineLocation(LineOffset, Discriminator))[std::string(Name)]; 16233376a787SDiego Novillo } 162457d1dda5SDehao Chen FProfile->setName(Name); 16253376a787SDiego Novillo 16263376a787SDiego Novillo for (uint32_t I = 0; I < NumPosCounts; ++I) { 16273376a787SDiego Novillo uint32_t Offset; 16283376a787SDiego Novillo if (!GcovBuffer.readInt(Offset)) 16293376a787SDiego Novillo return sampleprof_error::truncated; 16303376a787SDiego Novillo 16313376a787SDiego Novillo uint32_t NumTargets; 16323376a787SDiego Novillo if (!GcovBuffer.readInt(NumTargets)) 16333376a787SDiego Novillo return sampleprof_error::truncated; 16343376a787SDiego Novillo 16353376a787SDiego Novillo uint64_t Count; 16363376a787SDiego Novillo if (!GcovBuffer.readInt64(Count)) 16373376a787SDiego Novillo return sampleprof_error::truncated; 16383376a787SDiego Novillo 1639aae1ed8eSDiego Novillo // The line location is encoded in the offset as: 1640aae1ed8eSDiego Novillo // high 16 bits: line offset to the start of the function. 1641aae1ed8eSDiego Novillo // low 16 bits: discriminator. 1642aae1ed8eSDiego Novillo uint32_t LineOffset = Offset >> 16; 1643aae1ed8eSDiego Novillo uint32_t Discriminator = Offset & 0xffff; 16443376a787SDiego Novillo 1645aae1ed8eSDiego Novillo InlineCallStack NewStack; 1646aae1ed8eSDiego Novillo NewStack.push_back(FProfile); 16471d0bc055SKazu Hirata llvm::append_range(NewStack, InlineStack); 1648aae1ed8eSDiego Novillo if (Update) { 1649aae1ed8eSDiego Novillo // Walk up the inline stack, adding the samples on this line to 1650aae1ed8eSDiego Novillo // the total sample count of the callers in the chain. 1651aae1ed8eSDiego Novillo for (auto CallerProfile : NewStack) 1652aae1ed8eSDiego Novillo CallerProfile->addTotalSamples(Count); 1653aae1ed8eSDiego Novillo 1654aae1ed8eSDiego Novillo // Update the body samples for the current profile. 1655aae1ed8eSDiego Novillo FProfile->addBodySamples(LineOffset, Discriminator, Count); 1656aae1ed8eSDiego Novillo } 1657aae1ed8eSDiego Novillo 1658aae1ed8eSDiego Novillo // Process the list of functions called at an indirect call site. 1659aae1ed8eSDiego Novillo // These are all the targets that a function pointer (or virtual 1660aae1ed8eSDiego Novillo // function) resolved at runtime. 16613376a787SDiego Novillo for (uint32_t J = 0; J < NumTargets; J++) { 16623376a787SDiego Novillo uint32_t HistVal; 16633376a787SDiego Novillo if (!GcovBuffer.readInt(HistVal)) 16643376a787SDiego Novillo return sampleprof_error::truncated; 16653376a787SDiego Novillo 16663376a787SDiego Novillo if (HistVal != HIST_TYPE_INDIR_CALL_TOPN) 16673376a787SDiego Novillo return sampleprof_error::malformed; 16683376a787SDiego Novillo 16693376a787SDiego Novillo uint64_t TargetIdx; 16703376a787SDiego Novillo if (!GcovBuffer.readInt64(TargetIdx)) 16713376a787SDiego Novillo return sampleprof_error::truncated; 16723376a787SDiego Novillo StringRef TargetName(Names[TargetIdx]); 16733376a787SDiego Novillo 16743376a787SDiego Novillo uint64_t TargetCount; 16753376a787SDiego Novillo if (!GcovBuffer.readInt64(TargetCount)) 16763376a787SDiego Novillo return sampleprof_error::truncated; 16773376a787SDiego Novillo 1678920677a9SDehao Chen if (Update) 1679920677a9SDehao Chen FProfile->addCalledTargetSamples(LineOffset, Discriminator, 1680aae1ed8eSDiego Novillo TargetName, TargetCount); 16813376a787SDiego Novillo } 16823376a787SDiego Novillo } 16833376a787SDiego Novillo 1684aae1ed8eSDiego Novillo // Process all the inlined callers into the current function. These 1685aae1ed8eSDiego Novillo // are all the callsites that were inlined into this function. 1686aae1ed8eSDiego Novillo for (uint32_t I = 0; I < NumCallsites; I++) { 16873376a787SDiego Novillo // The offset is encoded as: 16883376a787SDiego Novillo // high 16 bits: line offset to the start of the function. 16893376a787SDiego Novillo // low 16 bits: discriminator. 16903376a787SDiego Novillo uint32_t Offset; 16913376a787SDiego Novillo if (!GcovBuffer.readInt(Offset)) 16923376a787SDiego Novillo return sampleprof_error::truncated; 1693aae1ed8eSDiego Novillo InlineCallStack NewStack; 1694aae1ed8eSDiego Novillo NewStack.push_back(FProfile); 16951d0bc055SKazu Hirata llvm::append_range(NewStack, InlineStack); 1696aae1ed8eSDiego Novillo if (std::error_code EC = readOneFunctionProfile(NewStack, Update, Offset)) 16973376a787SDiego Novillo return EC; 16983376a787SDiego Novillo } 16993376a787SDiego Novillo 17003376a787SDiego Novillo return sampleprof_error::success; 17013376a787SDiego Novillo } 17023376a787SDiego Novillo 17035f8f34e4SAdrian Prantl /// Read a GCC AutoFDO profile. 17043376a787SDiego Novillo /// 17053376a787SDiego Novillo /// This format is generated by the Linux Perf conversion tool at 17063376a787SDiego Novillo /// https://github.com/google/autofdo. 17078c8ec1f6SWei Mi std::error_code SampleProfileReaderGCC::readImpl() { 17086745ffe4SRong Xu assert(!ProfileIsFSDisciminator && "Gcc profiles not support FSDisciminator"); 17093376a787SDiego Novillo // Read the string table. 17103376a787SDiego Novillo if (std::error_code EC = readNameTable()) 17113376a787SDiego Novillo return EC; 17123376a787SDiego Novillo 17133376a787SDiego Novillo // Read the source profile. 17143376a787SDiego Novillo if (std::error_code EC = readFunctionProfiles()) 17153376a787SDiego Novillo return EC; 17163376a787SDiego Novillo 17173376a787SDiego Novillo return sampleprof_error::success; 17183376a787SDiego Novillo } 17193376a787SDiego Novillo 17203376a787SDiego Novillo bool SampleProfileReaderGCC::hasFormat(const MemoryBuffer &Buffer) { 17213376a787SDiego Novillo StringRef Magic(reinterpret_cast<const char *>(Buffer.getBufferStart())); 17223376a787SDiego Novillo return Magic == "adcg*704"; 17233376a787SDiego Novillo } 17243376a787SDiego Novillo 17258c8ec1f6SWei Mi void SampleProfileReaderItaniumRemapper::applyRemapping(LLVMContext &Ctx) { 1726ebad6788SWei Mi // If the reader uses MD5 to represent string, we can't remap it because 172728436358SRichard Smith // we don't know what the original function names were. 1728ebad6788SWei Mi if (Reader.useMD5()) { 172928436358SRichard Smith Ctx.diagnose(DiagnosticInfoSampleProfile( 17308c8ec1f6SWei Mi Reader.getBuffer()->getBufferIdentifier(), 173128436358SRichard Smith "Profile data remapping cannot be applied to profile data " 173228436358SRichard Smith "in compact format (original mangled names are not available).", 173328436358SRichard Smith DS_Warning)); 17348c8ec1f6SWei Mi return; 173528436358SRichard Smith } 173628436358SRichard Smith 17376b989a17SWenlei He // CSSPGO-TODO: Remapper is not yet supported. 17386b989a17SWenlei He // We will need to remap the entire context string. 17398c8ec1f6SWei Mi assert(Remappings && "should be initialized while creating remapper"); 1740c67ccf5fSWei Mi for (auto &Sample : Reader.getProfiles()) { 1741c67ccf5fSWei Mi DenseSet<StringRef> NamesInSample; 1742c67ccf5fSWei Mi Sample.second.findAllNames(NamesInSample); 1743c67ccf5fSWei Mi for (auto &Name : NamesInSample) 1744c67ccf5fSWei Mi if (auto Key = Remappings->insert(Name)) 1745c67ccf5fSWei Mi NameMap.insert({Key, Name}); 1746c67ccf5fSWei Mi } 174728436358SRichard Smith 17488c8ec1f6SWei Mi RemappingApplied = true; 174928436358SRichard Smith } 175028436358SRichard Smith 1751c67ccf5fSWei Mi Optional<StringRef> 1752c67ccf5fSWei Mi SampleProfileReaderItaniumRemapper::lookUpNameInProfile(StringRef Fname) { 17538c8ec1f6SWei Mi if (auto Key = Remappings->lookup(Fname)) 1754c67ccf5fSWei Mi return NameMap.lookup(Key); 1755c67ccf5fSWei Mi return None; 175628436358SRichard Smith } 175728436358SRichard Smith 17585f8f34e4SAdrian Prantl /// Prepare a memory buffer for the contents of \p Filename. 1759de1ab26fSDiego Novillo /// 1760c572e92cSDiego Novillo /// \returns an error code indicating the status of the buffer. 1761fcd55607SDiego Novillo static ErrorOr<std::unique_ptr<MemoryBuffer>> 17620da23a27SBenjamin Kramer setupMemoryBuffer(const Twine &Filename) { 1763e71994a2SJonathan Crowther auto BufferOrErr = MemoryBuffer::getFileOrSTDIN(Filename, /*IsText=*/true); 1764c572e92cSDiego Novillo if (std::error_code EC = BufferOrErr.getError()) 1765c572e92cSDiego Novillo return EC; 1766fcd55607SDiego Novillo auto Buffer = std::move(BufferOrErr.get()); 1767c572e92cSDiego Novillo 176895875d24SZarko Todorovski // Check the file. 1769260fe3ecSZachary Turner if (uint64_t(Buffer->getBufferSize()) > std::numeric_limits<uint32_t>::max()) 1770c572e92cSDiego Novillo return sampleprof_error::too_large; 1771c572e92cSDiego Novillo 1772c55cf4afSBill Wendling return std::move(Buffer); 1773c572e92cSDiego Novillo } 1774c572e92cSDiego Novillo 17755f8f34e4SAdrian Prantl /// Create a sample profile reader based on the format of the input file. 1776c572e92cSDiego Novillo /// 1777c572e92cSDiego Novillo /// \param Filename The file to open. 1778c572e92cSDiego Novillo /// 1779c572e92cSDiego Novillo /// \param C The LLVM context to use to emit diagnostics. 1780c572e92cSDiego Novillo /// 17818d581857SRong Xu /// \param P The FSDiscriminatorPass. 17828d581857SRong Xu /// 17838c8ec1f6SWei Mi /// \param RemapFilename The file used for profile remapping. 17848c8ec1f6SWei Mi /// 1785c572e92cSDiego Novillo /// \returns an error code indicating the status of the created reader. 1786fcd55607SDiego Novillo ErrorOr<std::unique_ptr<SampleProfileReader>> 17878c8ec1f6SWei Mi SampleProfileReader::create(const std::string Filename, LLVMContext &C, 17888d581857SRong Xu FSDiscriminatorPass P, 17898c8ec1f6SWei Mi const std::string RemapFilename) { 1790fcd55607SDiego Novillo auto BufferOrError = setupMemoryBuffer(Filename); 1791fcd55607SDiego Novillo if (std::error_code EC = BufferOrError.getError()) 1792c572e92cSDiego Novillo return EC; 17938d581857SRong Xu return create(BufferOrError.get(), C, P, RemapFilename); 179451abea74SNathan Slingerland } 1795c572e92cSDiego Novillo 179628436358SRichard Smith /// Create a sample profile remapper from the given input, to remap the 179728436358SRichard Smith /// function names in the given profile data. 179828436358SRichard Smith /// 179928436358SRichard Smith /// \param Filename The file to open. 180028436358SRichard Smith /// 18018c8ec1f6SWei Mi /// \param Reader The profile reader the remapper is going to be applied to. 18028c8ec1f6SWei Mi /// 180328436358SRichard Smith /// \param C The LLVM context to use to emit diagnostics. 180428436358SRichard Smith /// 180528436358SRichard Smith /// \returns an error code indicating the status of the created reader. 18068c8ec1f6SWei Mi ErrorOr<std::unique_ptr<SampleProfileReaderItaniumRemapper>> 18078c8ec1f6SWei Mi SampleProfileReaderItaniumRemapper::create(const std::string Filename, 18088c8ec1f6SWei Mi SampleProfileReader &Reader, 18098c8ec1f6SWei Mi LLVMContext &C) { 181028436358SRichard Smith auto BufferOrError = setupMemoryBuffer(Filename); 181128436358SRichard Smith if (std::error_code EC = BufferOrError.getError()) 181228436358SRichard Smith return EC; 18138c8ec1f6SWei Mi return create(BufferOrError.get(), Reader, C); 18148c8ec1f6SWei Mi } 18158c8ec1f6SWei Mi 18168c8ec1f6SWei Mi /// Create a sample profile remapper from the given input, to remap the 18178c8ec1f6SWei Mi /// function names in the given profile data. 18188c8ec1f6SWei Mi /// 18198c8ec1f6SWei Mi /// \param B The memory buffer to create the reader from (assumes ownership). 18208c8ec1f6SWei Mi /// 18218c8ec1f6SWei Mi /// \param C The LLVM context to use to emit diagnostics. 18228c8ec1f6SWei Mi /// 18238c8ec1f6SWei Mi /// \param Reader The profile reader the remapper is going to be applied to. 18248c8ec1f6SWei Mi /// 18258c8ec1f6SWei Mi /// \returns an error code indicating the status of the created reader. 18268c8ec1f6SWei Mi ErrorOr<std::unique_ptr<SampleProfileReaderItaniumRemapper>> 18278c8ec1f6SWei Mi SampleProfileReaderItaniumRemapper::create(std::unique_ptr<MemoryBuffer> &B, 18288c8ec1f6SWei Mi SampleProfileReader &Reader, 18298c8ec1f6SWei Mi LLVMContext &C) { 18308c8ec1f6SWei Mi auto Remappings = std::make_unique<SymbolRemappingReader>(); 18318c8ec1f6SWei Mi if (Error E = Remappings->read(*B.get())) { 18328c8ec1f6SWei Mi handleAllErrors( 18338c8ec1f6SWei Mi std::move(E), [&](const SymbolRemappingParseError &ParseError) { 18348c8ec1f6SWei Mi C.diagnose(DiagnosticInfoSampleProfile(B->getBufferIdentifier(), 18358c8ec1f6SWei Mi ParseError.getLineNum(), 18368c8ec1f6SWei Mi ParseError.getMessage())); 18378c8ec1f6SWei Mi }); 18388c8ec1f6SWei Mi return sampleprof_error::malformed; 18398c8ec1f6SWei Mi } 18408c8ec1f6SWei Mi 18410eaee545SJonas Devlieghere return std::make_unique<SampleProfileReaderItaniumRemapper>( 18428c8ec1f6SWei Mi std::move(B), std::move(Remappings), Reader); 184328436358SRichard Smith } 184428436358SRichard Smith 18455f8f34e4SAdrian Prantl /// Create a sample profile reader based on the format of the input data. 184651abea74SNathan Slingerland /// 184751abea74SNathan Slingerland /// \param B The memory buffer to create the reader from (assumes ownership). 184851abea74SNathan Slingerland /// 184951abea74SNathan Slingerland /// \param C The LLVM context to use to emit diagnostics. 185051abea74SNathan Slingerland /// 18518d581857SRong Xu /// \param P The FSDiscriminatorPass. 18528d581857SRong Xu /// 18538c8ec1f6SWei Mi /// \param RemapFilename The file used for profile remapping. 18548c8ec1f6SWei Mi /// 185551abea74SNathan Slingerland /// \returns an error code indicating the status of the created reader. 185651abea74SNathan Slingerland ErrorOr<std::unique_ptr<SampleProfileReader>> 18578c8ec1f6SWei Mi SampleProfileReader::create(std::unique_ptr<MemoryBuffer> &B, LLVMContext &C, 18588d581857SRong Xu FSDiscriminatorPass P, 18598c8ec1f6SWei Mi const std::string RemapFilename) { 1860fcd55607SDiego Novillo std::unique_ptr<SampleProfileReader> Reader; 1861a0c0857eSWei Mi if (SampleProfileReaderRawBinary::hasFormat(*B)) 1862a0c0857eSWei Mi Reader.reset(new SampleProfileReaderRawBinary(std::move(B), C)); 1863be907324SWei Mi else if (SampleProfileReaderExtBinary::hasFormat(*B)) 1864be907324SWei Mi Reader.reset(new SampleProfileReaderExtBinary(std::move(B), C)); 1865a0c0857eSWei Mi else if (SampleProfileReaderCompactBinary::hasFormat(*B)) 1866a0c0857eSWei Mi Reader.reset(new SampleProfileReaderCompactBinary(std::move(B), C)); 186751abea74SNathan Slingerland else if (SampleProfileReaderGCC::hasFormat(*B)) 186851abea74SNathan Slingerland Reader.reset(new SampleProfileReaderGCC(std::move(B), C)); 186951abea74SNathan Slingerland else if (SampleProfileReaderText::hasFormat(*B)) 187051abea74SNathan Slingerland Reader.reset(new SampleProfileReaderText(std::move(B), C)); 18714f823667SNathan Slingerland else 18724f823667SNathan Slingerland return sampleprof_error::unrecognized_format; 1873c572e92cSDiego Novillo 18748c8ec1f6SWei Mi if (!RemapFilename.empty()) { 18758c8ec1f6SWei Mi auto ReaderOrErr = 18768c8ec1f6SWei Mi SampleProfileReaderItaniumRemapper::create(RemapFilename, *Reader, C); 18778c8ec1f6SWei Mi if (std::error_code EC = ReaderOrErr.getError()) { 18788c8ec1f6SWei Mi std::string Msg = "Could not create remapper: " + EC.message(); 18798c8ec1f6SWei Mi C.diagnose(DiagnosticInfoSampleProfile(RemapFilename, Msg)); 18808c8ec1f6SWei Mi return EC; 18818c8ec1f6SWei Mi } 18828c8ec1f6SWei Mi Reader->Remapper = std::move(ReaderOrErr.get()); 18838c8ec1f6SWei Mi } 18848c8ec1f6SWei Mi 1885be907324SWei Mi if (std::error_code EC = Reader->readHeader()) { 1886fcd55607SDiego Novillo return EC; 1887be907324SWei Mi } 1888fcd55607SDiego Novillo 18898d581857SRong Xu Reader->setDiscriminatorMaskedBitFrom(P); 18908d581857SRong Xu 1891c55cf4afSBill Wendling return std::move(Reader); 1892de1ab26fSDiego Novillo } 189340ee23dbSEaswaran Raman 189440ee23dbSEaswaran Raman // For text and GCC file formats, we compute the summary after reading the 189540ee23dbSEaswaran Raman // profile. Binary format has the profile summary in its header. 189640ee23dbSEaswaran Raman void SampleProfileReader::computeSummary() { 1897e5a17e3fSEaswaran Raman SampleProfileSummaryBuilder Builder(ProfileSummaryBuilder::DefaultCutoffs); 1898801d9cc7SWenlei He Summary = Builder.computeSummaryForProfiles(Profiles); 189940ee23dbSEaswaran Raman } 1900