1de1ab26fSDiego Novillo //===- SampleProfReader.cpp - Read LLVM sample profile data ---------------===// 2de1ab26fSDiego Novillo // 3de1ab26fSDiego Novillo // The LLVM Compiler Infrastructure 4de1ab26fSDiego Novillo // 5de1ab26fSDiego Novillo // This file is distributed under the University of Illinois Open Source 6de1ab26fSDiego Novillo // License. See LICENSE.TXT for details. 7de1ab26fSDiego Novillo // 8de1ab26fSDiego Novillo //===----------------------------------------------------------------------===// 9de1ab26fSDiego Novillo // 10de1ab26fSDiego Novillo // This file implements the class that reads LLVM sample profiles. It 11bb5605caSDiego Novillo // supports three file formats: text, binary and gcov. 12de1ab26fSDiego Novillo // 13bb5605caSDiego Novillo // The textual representation is useful for debugging and testing purposes. The 14bb5605caSDiego Novillo // binary representation is more compact, resulting in smaller file sizes. 15de1ab26fSDiego Novillo // 16bb5605caSDiego Novillo // The gcov encoding is the one generated by GCC's AutoFDO profile creation 17bb5605caSDiego Novillo // tool (https://github.com/google/autofdo) 18de1ab26fSDiego Novillo // 19bb5605caSDiego Novillo // All three encodings can be used interchangeably as an input sample profile. 20de1ab26fSDiego Novillo // 21de1ab26fSDiego Novillo //===----------------------------------------------------------------------===// 22de1ab26fSDiego Novillo 23de1ab26fSDiego Novillo #include "llvm/ProfileData/SampleProfReader.h" 24b93483dbSDiego Novillo #include "llvm/ADT/DenseMap.h" 2540ee23dbSEaswaran Raman #include "llvm/ADT/STLExtras.h" 26e78d131aSEugene Zelenko #include "llvm/ADT/StringRef.h" 27e78d131aSEugene Zelenko #include "llvm/IR/ProfileSummary.h" 28e78d131aSEugene Zelenko #include "llvm/ProfileData/ProfileCommon.h" 29e78d131aSEugene Zelenko #include "llvm/ProfileData/SampleProf.h" 30de1ab26fSDiego Novillo #include "llvm/Support/ErrorOr.h" 31c572e92cSDiego Novillo #include "llvm/Support/LEB128.h" 32de1ab26fSDiego Novillo #include "llvm/Support/LineIterator.h" 33c572e92cSDiego Novillo #include "llvm/Support/MemoryBuffer.h" 34e78d131aSEugene Zelenko #include "llvm/Support/raw_ostream.h" 35e78d131aSEugene Zelenko #include <algorithm> 36e78d131aSEugene Zelenko #include <cstddef> 37e78d131aSEugene Zelenko #include <cstdint> 38e78d131aSEugene Zelenko #include <limits> 39e78d131aSEugene Zelenko #include <memory> 40e78d131aSEugene Zelenko #include <system_error> 41e78d131aSEugene Zelenko #include <vector> 42de1ab26fSDiego Novillo 43de1ab26fSDiego Novillo using namespace llvm; 44e78d131aSEugene Zelenko using namespace sampleprof; 45de1ab26fSDiego Novillo 465f8f34e4SAdrian Prantl /// Dump the function profile for \p FName. 47de1ab26fSDiego Novillo /// 48de1ab26fSDiego Novillo /// \param FName Name of the function to print. 49d5336ae2SDiego Novillo /// \param OS Stream to emit the output to. 50d5336ae2SDiego Novillo void SampleProfileReader::dumpFunctionProfile(StringRef FName, 51d5336ae2SDiego Novillo raw_ostream &OS) { 528e415a82SDiego Novillo OS << "Function: " << FName << ": " << Profiles[FName]; 53de1ab26fSDiego Novillo } 54de1ab26fSDiego Novillo 555f8f34e4SAdrian Prantl /// Dump all the function profiles found on stream \p OS. 56d5336ae2SDiego Novillo void SampleProfileReader::dump(raw_ostream &OS) { 57d5336ae2SDiego Novillo for (const auto &I : Profiles) 58d5336ae2SDiego Novillo dumpFunctionProfile(I.getKey(), OS); 59de1ab26fSDiego Novillo } 60de1ab26fSDiego Novillo 615f8f34e4SAdrian Prantl /// Parse \p Input as function head. 626722688eSDehao Chen /// 636722688eSDehao Chen /// Parse one line of \p Input, and update function name in \p FName, 646722688eSDehao Chen /// function's total sample count in \p NumSamples, function's entry 656722688eSDehao Chen /// count in \p NumHeadSamples. 666722688eSDehao Chen /// 676722688eSDehao Chen /// \returns true if parsing is successful. 686722688eSDehao Chen static bool ParseHead(const StringRef &Input, StringRef &FName, 6938be3330SDiego Novillo uint64_t &NumSamples, uint64_t &NumHeadSamples) { 706722688eSDehao Chen if (Input[0] == ' ') 716722688eSDehao Chen return false; 726722688eSDehao Chen size_t n2 = Input.rfind(':'); 736722688eSDehao Chen size_t n1 = Input.rfind(':', n2 - 1); 746722688eSDehao Chen FName = Input.substr(0, n1); 756722688eSDehao Chen if (Input.substr(n1 + 1, n2 - n1 - 1).getAsInteger(10, NumSamples)) 766722688eSDehao Chen return false; 776722688eSDehao Chen if (Input.substr(n2 + 1).getAsInteger(10, NumHeadSamples)) 786722688eSDehao Chen return false; 796722688eSDehao Chen return true; 806722688eSDehao Chen } 816722688eSDehao Chen 825f8f34e4SAdrian Prantl /// Returns true if line offset \p L is legal (only has 16 bits). 8357d1dda5SDehao Chen static bool isOffsetLegal(unsigned L) { return (L & 0xffff) == L; } 8410042412SDehao Chen 855f8f34e4SAdrian Prantl /// Parse \p Input as line sample. 866722688eSDehao Chen /// 876722688eSDehao Chen /// \param Input input line. 886722688eSDehao Chen /// \param IsCallsite true if the line represents an inlined callsite. 896722688eSDehao Chen /// \param Depth the depth of the inline stack. 906722688eSDehao Chen /// \param NumSamples total samples of the line/inlined callsite. 916722688eSDehao Chen /// \param LineOffset line offset to the start of the function. 926722688eSDehao Chen /// \param Discriminator discriminator of the line. 936722688eSDehao Chen /// \param TargetCountMap map from indirect call target to count. 946722688eSDehao Chen /// 956722688eSDehao Chen /// returns true if parsing is successful. 9638be3330SDiego Novillo static bool ParseLine(const StringRef &Input, bool &IsCallsite, uint32_t &Depth, 9738be3330SDiego Novillo uint64_t &NumSamples, uint32_t &LineOffset, 9838be3330SDiego Novillo uint32_t &Discriminator, StringRef &CalleeName, 9938be3330SDiego Novillo DenseMap<StringRef, uint64_t> &TargetCountMap) { 1006722688eSDehao Chen for (Depth = 0; Input[Depth] == ' '; Depth++) 1016722688eSDehao Chen ; 1026722688eSDehao Chen if (Depth == 0) 1036722688eSDehao Chen return false; 1046722688eSDehao Chen 1056722688eSDehao Chen size_t n1 = Input.find(':'); 1066722688eSDehao Chen StringRef Loc = Input.substr(Depth, n1 - Depth); 1076722688eSDehao Chen size_t n2 = Loc.find('.'); 1086722688eSDehao Chen if (n2 == StringRef::npos) { 10910042412SDehao Chen if (Loc.getAsInteger(10, LineOffset) || !isOffsetLegal(LineOffset)) 1106722688eSDehao Chen return false; 1116722688eSDehao Chen Discriminator = 0; 1126722688eSDehao Chen } else { 1136722688eSDehao Chen if (Loc.substr(0, n2).getAsInteger(10, LineOffset)) 1146722688eSDehao Chen return false; 1156722688eSDehao Chen if (Loc.substr(n2 + 1).getAsInteger(10, Discriminator)) 1166722688eSDehao Chen return false; 1176722688eSDehao Chen } 1186722688eSDehao Chen 1196722688eSDehao Chen StringRef Rest = Input.substr(n1 + 2); 1206722688eSDehao Chen if (Rest[0] >= '0' && Rest[0] <= '9') { 1216722688eSDehao Chen IsCallsite = false; 1226722688eSDehao Chen size_t n3 = Rest.find(' '); 1236722688eSDehao Chen if (n3 == StringRef::npos) { 1246722688eSDehao Chen if (Rest.getAsInteger(10, NumSamples)) 1256722688eSDehao Chen return false; 1266722688eSDehao Chen } else { 1276722688eSDehao Chen if (Rest.substr(0, n3).getAsInteger(10, NumSamples)) 1286722688eSDehao Chen return false; 1296722688eSDehao Chen } 130984ab0f1SWei Mi // Find call targets and their sample counts. 131984ab0f1SWei Mi // Note: In some cases, there are symbols in the profile which are not 132984ab0f1SWei Mi // mangled. To accommodate such cases, use colon + integer pairs as the 133984ab0f1SWei Mi // anchor points. 134984ab0f1SWei Mi // An example: 135984ab0f1SWei Mi // _M_construct<char *>:1000 string_view<std::allocator<char> >:437 136984ab0f1SWei Mi // ":1000" and ":437" are used as anchor points so the string above will 137984ab0f1SWei Mi // be interpreted as 138984ab0f1SWei Mi // target: _M_construct<char *> 139984ab0f1SWei Mi // count: 1000 140984ab0f1SWei Mi // target: string_view<std::allocator<char> > 141984ab0f1SWei Mi // count: 437 1426722688eSDehao Chen while (n3 != StringRef::npos) { 1436722688eSDehao Chen n3 += Rest.substr(n3).find_first_not_of(' '); 1446722688eSDehao Chen Rest = Rest.substr(n3); 145984ab0f1SWei Mi n3 = Rest.find_first_of(':'); 146984ab0f1SWei Mi if (n3 == StringRef::npos || n3 == 0) 1476722688eSDehao Chen return false; 148984ab0f1SWei Mi 149984ab0f1SWei Mi StringRef Target; 150984ab0f1SWei Mi uint64_t count, n4; 151984ab0f1SWei Mi while (true) { 152984ab0f1SWei Mi // Get the segment after the current colon. 153984ab0f1SWei Mi StringRef AfterColon = Rest.substr(n3 + 1); 154984ab0f1SWei Mi // Get the target symbol before the current colon. 155984ab0f1SWei Mi Target = Rest.substr(0, n3); 156984ab0f1SWei Mi // Check if the word after the current colon is an integer. 157984ab0f1SWei Mi n4 = AfterColon.find_first_of(' '); 158984ab0f1SWei Mi n4 = (n4 != StringRef::npos) ? n3 + n4 + 1 : Rest.size(); 159984ab0f1SWei Mi StringRef WordAfterColon = Rest.substr(n3 + 1, n4 - n3 - 1); 160984ab0f1SWei Mi if (!WordAfterColon.getAsInteger(10, count)) 161984ab0f1SWei Mi break; 162984ab0f1SWei Mi 163984ab0f1SWei Mi // Try to find the next colon. 164984ab0f1SWei Mi uint64_t n5 = AfterColon.find_first_of(':'); 165984ab0f1SWei Mi if (n5 == StringRef::npos) 166984ab0f1SWei Mi return false; 167984ab0f1SWei Mi n3 += n5 + 1; 168984ab0f1SWei Mi } 169984ab0f1SWei Mi 170984ab0f1SWei Mi // An anchor point is found. Save the {target, count} pair 171984ab0f1SWei Mi TargetCountMap[Target] = count; 172984ab0f1SWei Mi if (n4 == Rest.size()) 173984ab0f1SWei Mi break; 174984ab0f1SWei Mi // Change n3 to the next blank space after colon + integer pair. 175984ab0f1SWei Mi n3 = n4; 1766722688eSDehao Chen } 1776722688eSDehao Chen } else { 1786722688eSDehao Chen IsCallsite = true; 17938be3330SDiego Novillo size_t n3 = Rest.find_last_of(':'); 1806722688eSDehao Chen CalleeName = Rest.substr(0, n3); 1816722688eSDehao Chen if (Rest.substr(n3 + 1).getAsInteger(10, NumSamples)) 1826722688eSDehao Chen return false; 1836722688eSDehao Chen } 1846722688eSDehao Chen return true; 1856722688eSDehao Chen } 1866722688eSDehao Chen 1875f8f34e4SAdrian Prantl /// Load samples from a text file. 188de1ab26fSDiego Novillo /// 189de1ab26fSDiego Novillo /// See the documentation at the top of the file for an explanation of 190de1ab26fSDiego Novillo /// the expected format. 191de1ab26fSDiego Novillo /// 192de1ab26fSDiego Novillo /// \returns true if the file was loaded successfully, false otherwise. 193c572e92cSDiego Novillo std::error_code SampleProfileReaderText::read() { 194c572e92cSDiego Novillo line_iterator LineIt(*Buffer, /*SkipBlanks=*/true, '#'); 19548dd080cSNathan Slingerland sampleprof_error Result = sampleprof_error::success; 196de1ab26fSDiego Novillo 197aae1ed8eSDiego Novillo InlineCallStack InlineStack; 1986722688eSDehao Chen 1996722688eSDehao Chen for (; !LineIt.is_at_eof(); ++LineIt) { 2006722688eSDehao Chen if ((*LineIt)[(*LineIt).find_first_not_of(' ')] == '#') 2016722688eSDehao Chen continue; 202de1ab26fSDiego Novillo // Read the header of each function. 203de1ab26fSDiego Novillo // 204de1ab26fSDiego Novillo // Note that for function identifiers we are actually expecting 205de1ab26fSDiego Novillo // mangled names, but we may not always get them. This happens when 206de1ab26fSDiego Novillo // the compiler decides not to emit the function (e.g., it was inlined 207de1ab26fSDiego Novillo // and removed). In this case, the binary will not have the linkage 208de1ab26fSDiego Novillo // name for the function, so the profiler will emit the function's 209de1ab26fSDiego Novillo // unmangled name, which may contain characters like ':' and '>' in its 210de1ab26fSDiego Novillo // name (member functions, templates, etc). 211de1ab26fSDiego Novillo // 212de1ab26fSDiego Novillo // The only requirement we place on the identifier, then, is that it 213de1ab26fSDiego Novillo // should not begin with a number. 2146722688eSDehao Chen if ((*LineIt)[0] != ' ') { 21538be3330SDiego Novillo uint64_t NumSamples, NumHeadSamples; 2166722688eSDehao Chen StringRef FName; 2176722688eSDehao Chen if (!ParseHead(*LineIt, FName, NumSamples, NumHeadSamples)) { 2183376a787SDiego Novillo reportError(LineIt.line_number(), 219de1ab26fSDiego Novillo "Expected 'mangled_name:NUM:NUM', found " + *LineIt); 220c572e92cSDiego Novillo return sampleprof_error::malformed; 221de1ab26fSDiego Novillo } 222de1ab26fSDiego Novillo Profiles[FName] = FunctionSamples(); 223de1ab26fSDiego Novillo FunctionSamples &FProfile = Profiles[FName]; 22457d1dda5SDehao Chen FProfile.setName(FName); 22548dd080cSNathan Slingerland MergeResult(Result, FProfile.addTotalSamples(NumSamples)); 22648dd080cSNathan Slingerland MergeResult(Result, FProfile.addHeadSamples(NumHeadSamples)); 2276722688eSDehao Chen InlineStack.clear(); 2286722688eSDehao Chen InlineStack.push_back(&FProfile); 2296722688eSDehao Chen } else { 23038be3330SDiego Novillo uint64_t NumSamples; 2316722688eSDehao Chen StringRef FName; 23238be3330SDiego Novillo DenseMap<StringRef, uint64_t> TargetCountMap; 2336722688eSDehao Chen bool IsCallsite; 23438be3330SDiego Novillo uint32_t Depth, LineOffset, Discriminator; 2356722688eSDehao Chen if (!ParseLine(*LineIt, IsCallsite, Depth, NumSamples, LineOffset, 2366722688eSDehao Chen Discriminator, FName, TargetCountMap)) { 2373376a787SDiego Novillo reportError(LineIt.line_number(), 2383376a787SDiego Novillo "Expected 'NUM[.NUM]: NUM[ mangled_name:NUM]*', found " + 2393376a787SDiego Novillo *LineIt); 240c572e92cSDiego Novillo return sampleprof_error::malformed; 241de1ab26fSDiego Novillo } 2426722688eSDehao Chen if (IsCallsite) { 2436722688eSDehao Chen while (InlineStack.size() > Depth) { 2446722688eSDehao Chen InlineStack.pop_back(); 245c572e92cSDiego Novillo } 2466722688eSDehao Chen FunctionSamples &FSamples = InlineStack.back()->functionSamplesAt( 2472c7ca9b5SDehao Chen LineLocation(LineOffset, Discriminator))[FName]; 24857d1dda5SDehao Chen FSamples.setName(FName); 24948dd080cSNathan Slingerland MergeResult(Result, FSamples.addTotalSamples(NumSamples)); 2506722688eSDehao Chen InlineStack.push_back(&FSamples); 2516722688eSDehao Chen } else { 2526722688eSDehao Chen while (InlineStack.size() > Depth) { 2536722688eSDehao Chen InlineStack.pop_back(); 2546722688eSDehao Chen } 2556722688eSDehao Chen FunctionSamples &FProfile = *InlineStack.back(); 2566722688eSDehao Chen for (const auto &name_count : TargetCountMap) { 25748dd080cSNathan Slingerland MergeResult(Result, FProfile.addCalledTargetSamples( 25848dd080cSNathan Slingerland LineOffset, Discriminator, name_count.first, 25948dd080cSNathan Slingerland name_count.second)); 260c572e92cSDiego Novillo } 26148dd080cSNathan Slingerland MergeResult(Result, FProfile.addBodySamples(LineOffset, Discriminator, 26248dd080cSNathan Slingerland NumSamples)); 2636722688eSDehao Chen } 264de1ab26fSDiego Novillo } 265de1ab26fSDiego Novillo } 26640ee23dbSEaswaran Raman if (Result == sampleprof_error::success) 26740ee23dbSEaswaran Raman computeSummary(); 268de1ab26fSDiego Novillo 26948dd080cSNathan Slingerland return Result; 270de1ab26fSDiego Novillo } 271de1ab26fSDiego Novillo 2724f823667SNathan Slingerland bool SampleProfileReaderText::hasFormat(const MemoryBuffer &Buffer) { 2734f823667SNathan Slingerland bool result = false; 2744f823667SNathan Slingerland 2754f823667SNathan Slingerland // Check that the first non-comment line is a valid function header. 2764f823667SNathan Slingerland line_iterator LineIt(Buffer, /*SkipBlanks=*/true, '#'); 2774f823667SNathan Slingerland if (!LineIt.is_at_eof()) { 2784f823667SNathan Slingerland if ((*LineIt)[0] != ' ') { 2794f823667SNathan Slingerland uint64_t NumSamples, NumHeadSamples; 2804f823667SNathan Slingerland StringRef FName; 2814f823667SNathan Slingerland result = ParseHead(*LineIt, FName, NumSamples, NumHeadSamples); 2824f823667SNathan Slingerland } 2834f823667SNathan Slingerland } 2844f823667SNathan Slingerland 2854f823667SNathan Slingerland return result; 2864f823667SNathan Slingerland } 2874f823667SNathan Slingerland 288d5336ae2SDiego Novillo template <typename T> ErrorOr<T> SampleProfileReaderBinary::readNumber() { 289c572e92cSDiego Novillo unsigned NumBytesRead = 0; 290c572e92cSDiego Novillo std::error_code EC; 291c572e92cSDiego Novillo uint64_t Val = decodeULEB128(Data, &NumBytesRead); 292c572e92cSDiego Novillo 293c572e92cSDiego Novillo if (Val > std::numeric_limits<T>::max()) 294c572e92cSDiego Novillo EC = sampleprof_error::malformed; 295c572e92cSDiego Novillo else if (Data + NumBytesRead > End) 296c572e92cSDiego Novillo EC = sampleprof_error::truncated; 297c572e92cSDiego Novillo else 298c572e92cSDiego Novillo EC = sampleprof_error::success; 299c572e92cSDiego Novillo 300c572e92cSDiego Novillo if (EC) { 3013376a787SDiego Novillo reportError(0, EC.message()); 302c572e92cSDiego Novillo return EC; 303c572e92cSDiego Novillo } 304c572e92cSDiego Novillo 305c572e92cSDiego Novillo Data += NumBytesRead; 306c572e92cSDiego Novillo return static_cast<T>(Val); 307c572e92cSDiego Novillo } 308c572e92cSDiego Novillo 309c572e92cSDiego Novillo ErrorOr<StringRef> SampleProfileReaderBinary::readString() { 310c572e92cSDiego Novillo std::error_code EC; 311c572e92cSDiego Novillo StringRef Str(reinterpret_cast<const char *>(Data)); 312c572e92cSDiego Novillo if (Data + Str.size() + 1 > End) { 313c572e92cSDiego Novillo EC = sampleprof_error::truncated; 3143376a787SDiego Novillo reportError(0, EC.message()); 315c572e92cSDiego Novillo return EC; 316c572e92cSDiego Novillo } 317c572e92cSDiego Novillo 318c572e92cSDiego Novillo Data += Str.size() + 1; 319c572e92cSDiego Novillo return Str; 320c572e92cSDiego Novillo } 321c572e92cSDiego Novillo 322a0c0857eSWei Mi template <typename T> 323a0c0857eSWei Mi inline ErrorOr<uint32_t> SampleProfileReaderBinary::readStringIndex(T &Table) { 324760c5a8fSDiego Novillo std::error_code EC; 32538be3330SDiego Novillo auto Idx = readNumber<uint32_t>(); 326760c5a8fSDiego Novillo if (std::error_code EC = Idx.getError()) 327760c5a8fSDiego Novillo return EC; 328a0c0857eSWei Mi if (*Idx >= Table.size()) 329760c5a8fSDiego Novillo return sampleprof_error::truncated_name_table; 330a0c0857eSWei Mi return *Idx; 331a0c0857eSWei Mi } 332a0c0857eSWei Mi 333a0c0857eSWei Mi ErrorOr<StringRef> SampleProfileReaderRawBinary::readStringFromTable() { 334a0c0857eSWei Mi auto Idx = readStringIndex(NameTable); 335a0c0857eSWei Mi if (std::error_code EC = Idx.getError()) 336a0c0857eSWei Mi return EC; 337a0c0857eSWei Mi 338760c5a8fSDiego Novillo return NameTable[*Idx]; 339760c5a8fSDiego Novillo } 340760c5a8fSDiego Novillo 341a0c0857eSWei Mi ErrorOr<StringRef> SampleProfileReaderCompactBinary::readStringFromTable() { 342a0c0857eSWei Mi auto Idx = readStringIndex(NameTable); 343a0c0857eSWei Mi if (std::error_code EC = Idx.getError()) 344a0c0857eSWei Mi return EC; 345a0c0857eSWei Mi 346a0c0857eSWei Mi return StringRef(NameTable[*Idx]); 347a0c0857eSWei Mi } 348a0c0857eSWei Mi 349a7f1e8efSDiego Novillo std::error_code 350a7f1e8efSDiego Novillo SampleProfileReaderBinary::readProfile(FunctionSamples &FProfile) { 351b93483dbSDiego Novillo auto NumSamples = readNumber<uint64_t>(); 352b93483dbSDiego Novillo if (std::error_code EC = NumSamples.getError()) 353c572e92cSDiego Novillo return EC; 354b93483dbSDiego Novillo FProfile.addTotalSamples(*NumSamples); 355c572e92cSDiego Novillo 356c572e92cSDiego Novillo // Read the samples in the body. 35738be3330SDiego Novillo auto NumRecords = readNumber<uint32_t>(); 358c572e92cSDiego Novillo if (std::error_code EC = NumRecords.getError()) 359c572e92cSDiego Novillo return EC; 360a7f1e8efSDiego Novillo 36138be3330SDiego Novillo for (uint32_t I = 0; I < *NumRecords; ++I) { 362c572e92cSDiego Novillo auto LineOffset = readNumber<uint64_t>(); 363c572e92cSDiego Novillo if (std::error_code EC = LineOffset.getError()) 364c572e92cSDiego Novillo return EC; 365c572e92cSDiego Novillo 36610042412SDehao Chen if (!isOffsetLegal(*LineOffset)) { 36710042412SDehao Chen return std::error_code(); 36810042412SDehao Chen } 36910042412SDehao Chen 370c572e92cSDiego Novillo auto Discriminator = readNumber<uint64_t>(); 371c572e92cSDiego Novillo if (std::error_code EC = Discriminator.getError()) 372c572e92cSDiego Novillo return EC; 373c572e92cSDiego Novillo 374c572e92cSDiego Novillo auto NumSamples = readNumber<uint64_t>(); 375c572e92cSDiego Novillo if (std::error_code EC = NumSamples.getError()) 376c572e92cSDiego Novillo return EC; 377c572e92cSDiego Novillo 37838be3330SDiego Novillo auto NumCalls = readNumber<uint32_t>(); 379c572e92cSDiego Novillo if (std::error_code EC = NumCalls.getError()) 380c572e92cSDiego Novillo return EC; 381c572e92cSDiego Novillo 38238be3330SDiego Novillo for (uint32_t J = 0; J < *NumCalls; ++J) { 383760c5a8fSDiego Novillo auto CalledFunction(readStringFromTable()); 384c572e92cSDiego Novillo if (std::error_code EC = CalledFunction.getError()) 385c572e92cSDiego Novillo return EC; 386c572e92cSDiego Novillo 387c572e92cSDiego Novillo auto CalledFunctionSamples = readNumber<uint64_t>(); 388c572e92cSDiego Novillo if (std::error_code EC = CalledFunctionSamples.getError()) 389c572e92cSDiego Novillo return EC; 390c572e92cSDiego Novillo 391c572e92cSDiego Novillo FProfile.addCalledTargetSamples(*LineOffset, *Discriminator, 392a7f1e8efSDiego Novillo *CalledFunction, *CalledFunctionSamples); 393c572e92cSDiego Novillo } 394c572e92cSDiego Novillo 395c572e92cSDiego Novillo FProfile.addBodySamples(*LineOffset, *Discriminator, *NumSamples); 396c572e92cSDiego Novillo } 397a7f1e8efSDiego Novillo 398a7f1e8efSDiego Novillo // Read all the samples for inlined function calls. 39938be3330SDiego Novillo auto NumCallsites = readNumber<uint32_t>(); 400a7f1e8efSDiego Novillo if (std::error_code EC = NumCallsites.getError()) 401a7f1e8efSDiego Novillo return EC; 402a7f1e8efSDiego Novillo 40338be3330SDiego Novillo for (uint32_t J = 0; J < *NumCallsites; ++J) { 404a7f1e8efSDiego Novillo auto LineOffset = readNumber<uint64_t>(); 405a7f1e8efSDiego Novillo if (std::error_code EC = LineOffset.getError()) 406a7f1e8efSDiego Novillo return EC; 407a7f1e8efSDiego Novillo 408a7f1e8efSDiego Novillo auto Discriminator = readNumber<uint64_t>(); 409a7f1e8efSDiego Novillo if (std::error_code EC = Discriminator.getError()) 410a7f1e8efSDiego Novillo return EC; 411a7f1e8efSDiego Novillo 412760c5a8fSDiego Novillo auto FName(readStringFromTable()); 413a7f1e8efSDiego Novillo if (std::error_code EC = FName.getError()) 414a7f1e8efSDiego Novillo return EC; 415a7f1e8efSDiego Novillo 4162c7ca9b5SDehao Chen FunctionSamples &CalleeProfile = FProfile.functionSamplesAt( 4172c7ca9b5SDehao Chen LineLocation(*LineOffset, *Discriminator))[*FName]; 41857d1dda5SDehao Chen CalleeProfile.setName(*FName); 419a7f1e8efSDiego Novillo if (std::error_code EC = readProfile(CalleeProfile)) 420a7f1e8efSDiego Novillo return EC; 421a7f1e8efSDiego Novillo } 422a7f1e8efSDiego Novillo 423a7f1e8efSDiego Novillo return sampleprof_error::success; 424a7f1e8efSDiego Novillo } 425a7f1e8efSDiego Novillo 426a7f1e8efSDiego Novillo std::error_code SampleProfileReaderBinary::read() { 427a7f1e8efSDiego Novillo while (!at_eof()) { 428b93483dbSDiego Novillo auto NumHeadSamples = readNumber<uint64_t>(); 429b93483dbSDiego Novillo if (std::error_code EC = NumHeadSamples.getError()) 430b93483dbSDiego Novillo return EC; 431b93483dbSDiego Novillo 432760c5a8fSDiego Novillo auto FName(readStringFromTable()); 433a7f1e8efSDiego Novillo if (std::error_code EC = FName.getError()) 434a7f1e8efSDiego Novillo return EC; 435a7f1e8efSDiego Novillo 436a7f1e8efSDiego Novillo Profiles[*FName] = FunctionSamples(); 437a7f1e8efSDiego Novillo FunctionSamples &FProfile = Profiles[*FName]; 43857d1dda5SDehao Chen FProfile.setName(*FName); 439a7f1e8efSDiego Novillo 440b93483dbSDiego Novillo FProfile.addHeadSamples(*NumHeadSamples); 441b93483dbSDiego Novillo 442a7f1e8efSDiego Novillo if (std::error_code EC = readProfile(FProfile)) 443a7f1e8efSDiego Novillo return EC; 444c572e92cSDiego Novillo } 445c572e92cSDiego Novillo 446c572e92cSDiego Novillo return sampleprof_error::success; 447c572e92cSDiego Novillo } 448c572e92cSDiego Novillo 449a0c0857eSWei Mi std::error_code SampleProfileReaderRawBinary::verifySPMagic(uint64_t Magic) { 450a0c0857eSWei Mi if (Magic == SPMagic()) 451a0c0857eSWei Mi return sampleprof_error::success; 452a0c0857eSWei Mi return sampleprof_error::bad_magic; 453a0c0857eSWei Mi } 454a0c0857eSWei Mi 455a0c0857eSWei Mi std::error_code 456a0c0857eSWei Mi SampleProfileReaderCompactBinary::verifySPMagic(uint64_t Magic) { 457a0c0857eSWei Mi if (Magic == SPMagic(SPF_Compact_Binary)) 458a0c0857eSWei Mi return sampleprof_error::success; 459a0c0857eSWei Mi return sampleprof_error::bad_magic; 460a0c0857eSWei Mi } 461a0c0857eSWei Mi 462a0c0857eSWei Mi std::error_code SampleProfileReaderRawBinary::readNameTable() { 463a0c0857eSWei Mi auto Size = readNumber<uint32_t>(); 464a0c0857eSWei Mi if (std::error_code EC = Size.getError()) 465a0c0857eSWei Mi return EC; 466a0c0857eSWei Mi NameTable.reserve(*Size); 467a0c0857eSWei Mi for (uint32_t I = 0; I < *Size; ++I) { 468a0c0857eSWei Mi auto Name(readString()); 469a0c0857eSWei Mi if (std::error_code EC = Name.getError()) 470a0c0857eSWei Mi return EC; 471a0c0857eSWei Mi NameTable.push_back(*Name); 472a0c0857eSWei Mi } 473a0c0857eSWei Mi 474a0c0857eSWei Mi return sampleprof_error::success; 475a0c0857eSWei Mi } 476a0c0857eSWei Mi 477a0c0857eSWei Mi std::error_code SampleProfileReaderCompactBinary::readNameTable() { 478a0c0857eSWei Mi auto Size = readNumber<uint64_t>(); 479a0c0857eSWei Mi if (std::error_code EC = Size.getError()) 480a0c0857eSWei Mi return EC; 481a0c0857eSWei Mi NameTable.reserve(*Size); 482a0c0857eSWei Mi for (uint32_t I = 0; I < *Size; ++I) { 483a0c0857eSWei Mi auto FID = readNumber<uint64_t>(); 484a0c0857eSWei Mi if (std::error_code EC = FID.getError()) 485a0c0857eSWei Mi return EC; 486a0c0857eSWei Mi NameTable.push_back(std::to_string(*FID)); 487a0c0857eSWei Mi } 488a0c0857eSWei Mi return sampleprof_error::success; 489a0c0857eSWei Mi } 490a0c0857eSWei Mi 491c572e92cSDiego Novillo std::error_code SampleProfileReaderBinary::readHeader() { 492c572e92cSDiego Novillo Data = reinterpret_cast<const uint8_t *>(Buffer->getBufferStart()); 493c572e92cSDiego Novillo End = Data + Buffer->getBufferSize(); 494c572e92cSDiego Novillo 495c572e92cSDiego Novillo // Read and check the magic identifier. 496c572e92cSDiego Novillo auto Magic = readNumber<uint64_t>(); 497c572e92cSDiego Novillo if (std::error_code EC = Magic.getError()) 498c572e92cSDiego Novillo return EC; 499a0c0857eSWei Mi else if (std::error_code EC = verifySPMagic(*Magic)) 500*c6b96c8dSWei Mi return EC; 501c572e92cSDiego Novillo 502c572e92cSDiego Novillo // Read the version number. 503c572e92cSDiego Novillo auto Version = readNumber<uint64_t>(); 504c572e92cSDiego Novillo if (std::error_code EC = Version.getError()) 505c572e92cSDiego Novillo return EC; 506c572e92cSDiego Novillo else if (*Version != SPVersion()) 507c572e92cSDiego Novillo return sampleprof_error::unsupported_version; 508c572e92cSDiego Novillo 50940ee23dbSEaswaran Raman if (std::error_code EC = readSummary()) 51040ee23dbSEaswaran Raman return EC; 51140ee23dbSEaswaran Raman 512a0c0857eSWei Mi if (std::error_code EC = readNameTable()) 513760c5a8fSDiego Novillo return EC; 514c572e92cSDiego Novillo return sampleprof_error::success; 515c572e92cSDiego Novillo } 516c572e92cSDiego Novillo 51740ee23dbSEaswaran Raman std::error_code SampleProfileReaderBinary::readSummaryEntry( 51840ee23dbSEaswaran Raman std::vector<ProfileSummaryEntry> &Entries) { 51940ee23dbSEaswaran Raman auto Cutoff = readNumber<uint64_t>(); 52040ee23dbSEaswaran Raman if (std::error_code EC = Cutoff.getError()) 52140ee23dbSEaswaran Raman return EC; 52240ee23dbSEaswaran Raman 52340ee23dbSEaswaran Raman auto MinBlockCount = readNumber<uint64_t>(); 52440ee23dbSEaswaran Raman if (std::error_code EC = MinBlockCount.getError()) 52540ee23dbSEaswaran Raman return EC; 52640ee23dbSEaswaran Raman 52740ee23dbSEaswaran Raman auto NumBlocks = readNumber<uint64_t>(); 52840ee23dbSEaswaran Raman if (std::error_code EC = NumBlocks.getError()) 52940ee23dbSEaswaran Raman return EC; 53040ee23dbSEaswaran Raman 53140ee23dbSEaswaran Raman Entries.emplace_back(*Cutoff, *MinBlockCount, *NumBlocks); 53240ee23dbSEaswaran Raman return sampleprof_error::success; 53340ee23dbSEaswaran Raman } 53440ee23dbSEaswaran Raman 53540ee23dbSEaswaran Raman std::error_code SampleProfileReaderBinary::readSummary() { 53640ee23dbSEaswaran Raman auto TotalCount = readNumber<uint64_t>(); 53740ee23dbSEaswaran Raman if (std::error_code EC = TotalCount.getError()) 53840ee23dbSEaswaran Raman return EC; 53940ee23dbSEaswaran Raman 54040ee23dbSEaswaran Raman auto MaxBlockCount = readNumber<uint64_t>(); 54140ee23dbSEaswaran Raman if (std::error_code EC = MaxBlockCount.getError()) 54240ee23dbSEaswaran Raman return EC; 54340ee23dbSEaswaran Raman 54440ee23dbSEaswaran Raman auto MaxFunctionCount = readNumber<uint64_t>(); 54540ee23dbSEaswaran Raman if (std::error_code EC = MaxFunctionCount.getError()) 54640ee23dbSEaswaran Raman return EC; 54740ee23dbSEaswaran Raman 54840ee23dbSEaswaran Raman auto NumBlocks = readNumber<uint64_t>(); 54940ee23dbSEaswaran Raman if (std::error_code EC = NumBlocks.getError()) 55040ee23dbSEaswaran Raman return EC; 55140ee23dbSEaswaran Raman 55240ee23dbSEaswaran Raman auto NumFunctions = readNumber<uint64_t>(); 55340ee23dbSEaswaran Raman if (std::error_code EC = NumFunctions.getError()) 55440ee23dbSEaswaran Raman return EC; 55540ee23dbSEaswaran Raman 55640ee23dbSEaswaran Raman auto NumSummaryEntries = readNumber<uint64_t>(); 55740ee23dbSEaswaran Raman if (std::error_code EC = NumSummaryEntries.getError()) 55840ee23dbSEaswaran Raman return EC; 55940ee23dbSEaswaran Raman 56040ee23dbSEaswaran Raman std::vector<ProfileSummaryEntry> Entries; 56140ee23dbSEaswaran Raman for (unsigned i = 0; i < *NumSummaryEntries; i++) { 56240ee23dbSEaswaran Raman std::error_code EC = readSummaryEntry(Entries); 56340ee23dbSEaswaran Raman if (EC != sampleprof_error::success) 56440ee23dbSEaswaran Raman return EC; 56540ee23dbSEaswaran Raman } 5667cefdb81SEaswaran Raman Summary = llvm::make_unique<ProfileSummary>( 5677cefdb81SEaswaran Raman ProfileSummary::PSK_Sample, Entries, *TotalCount, *MaxBlockCount, 0, 5687cefdb81SEaswaran Raman *MaxFunctionCount, *NumBlocks, *NumFunctions); 56940ee23dbSEaswaran Raman 57040ee23dbSEaswaran Raman return sampleprof_error::success; 57140ee23dbSEaswaran Raman } 57240ee23dbSEaswaran Raman 573a0c0857eSWei Mi bool SampleProfileReaderRawBinary::hasFormat(const MemoryBuffer &Buffer) { 574c572e92cSDiego Novillo const uint8_t *Data = 575c572e92cSDiego Novillo reinterpret_cast<const uint8_t *>(Buffer.getBufferStart()); 576c572e92cSDiego Novillo uint64_t Magic = decodeULEB128(Data); 577c572e92cSDiego Novillo return Magic == SPMagic(); 578c572e92cSDiego Novillo } 579c572e92cSDiego Novillo 580a0c0857eSWei Mi bool SampleProfileReaderCompactBinary::hasFormat(const MemoryBuffer &Buffer) { 581a0c0857eSWei Mi const uint8_t *Data = 582a0c0857eSWei Mi reinterpret_cast<const uint8_t *>(Buffer.getBufferStart()); 583a0c0857eSWei Mi uint64_t Magic = decodeULEB128(Data); 584a0c0857eSWei Mi return Magic == SPMagic(SPF_Compact_Binary); 585a0c0857eSWei Mi } 586a0c0857eSWei Mi 5873376a787SDiego Novillo std::error_code SampleProfileReaderGCC::skipNextWord() { 5883376a787SDiego Novillo uint32_t dummy; 5893376a787SDiego Novillo if (!GcovBuffer.readInt(dummy)) 5903376a787SDiego Novillo return sampleprof_error::truncated; 5913376a787SDiego Novillo return sampleprof_error::success; 5923376a787SDiego Novillo } 5933376a787SDiego Novillo 5943376a787SDiego Novillo template <typename T> ErrorOr<T> SampleProfileReaderGCC::readNumber() { 5953376a787SDiego Novillo if (sizeof(T) <= sizeof(uint32_t)) { 5963376a787SDiego Novillo uint32_t Val; 5973376a787SDiego Novillo if (GcovBuffer.readInt(Val) && Val <= std::numeric_limits<T>::max()) 5983376a787SDiego Novillo return static_cast<T>(Val); 5993376a787SDiego Novillo } else if (sizeof(T) <= sizeof(uint64_t)) { 6003376a787SDiego Novillo uint64_t Val; 6013376a787SDiego Novillo if (GcovBuffer.readInt64(Val) && Val <= std::numeric_limits<T>::max()) 6023376a787SDiego Novillo return static_cast<T>(Val); 6033376a787SDiego Novillo } 6043376a787SDiego Novillo 6053376a787SDiego Novillo std::error_code EC = sampleprof_error::malformed; 6063376a787SDiego Novillo reportError(0, EC.message()); 6073376a787SDiego Novillo return EC; 6083376a787SDiego Novillo } 6093376a787SDiego Novillo 6103376a787SDiego Novillo ErrorOr<StringRef> SampleProfileReaderGCC::readString() { 6113376a787SDiego Novillo StringRef Str; 6123376a787SDiego Novillo if (!GcovBuffer.readString(Str)) 6133376a787SDiego Novillo return sampleprof_error::truncated; 6143376a787SDiego Novillo return Str; 6153376a787SDiego Novillo } 6163376a787SDiego Novillo 6173376a787SDiego Novillo std::error_code SampleProfileReaderGCC::readHeader() { 6183376a787SDiego Novillo // Read the magic identifier. 6193376a787SDiego Novillo if (!GcovBuffer.readGCDAFormat()) 6203376a787SDiego Novillo return sampleprof_error::unrecognized_format; 6213376a787SDiego Novillo 6223376a787SDiego Novillo // Read the version number. Note - the GCC reader does not validate this 6233376a787SDiego Novillo // version, but the profile creator generates v704. 6243376a787SDiego Novillo GCOV::GCOVVersion version; 6253376a787SDiego Novillo if (!GcovBuffer.readGCOVVersion(version)) 6263376a787SDiego Novillo return sampleprof_error::unrecognized_format; 6273376a787SDiego Novillo 6283376a787SDiego Novillo if (version != GCOV::V704) 6293376a787SDiego Novillo return sampleprof_error::unsupported_version; 6303376a787SDiego Novillo 6313376a787SDiego Novillo // Skip the empty integer. 6323376a787SDiego Novillo if (std::error_code EC = skipNextWord()) 6333376a787SDiego Novillo return EC; 6343376a787SDiego Novillo 6353376a787SDiego Novillo return sampleprof_error::success; 6363376a787SDiego Novillo } 6373376a787SDiego Novillo 6383376a787SDiego Novillo std::error_code SampleProfileReaderGCC::readSectionTag(uint32_t Expected) { 6393376a787SDiego Novillo uint32_t Tag; 6403376a787SDiego Novillo if (!GcovBuffer.readInt(Tag)) 6413376a787SDiego Novillo return sampleprof_error::truncated; 6423376a787SDiego Novillo 6433376a787SDiego Novillo if (Tag != Expected) 6443376a787SDiego Novillo return sampleprof_error::malformed; 6453376a787SDiego Novillo 6463376a787SDiego Novillo if (std::error_code EC = skipNextWord()) 6473376a787SDiego Novillo return EC; 6483376a787SDiego Novillo 6493376a787SDiego Novillo return sampleprof_error::success; 6503376a787SDiego Novillo } 6513376a787SDiego Novillo 6523376a787SDiego Novillo std::error_code SampleProfileReaderGCC::readNameTable() { 6533376a787SDiego Novillo if (std::error_code EC = readSectionTag(GCOVTagAFDOFileNames)) 6543376a787SDiego Novillo return EC; 6553376a787SDiego Novillo 6563376a787SDiego Novillo uint32_t Size; 6573376a787SDiego Novillo if (!GcovBuffer.readInt(Size)) 6583376a787SDiego Novillo return sampleprof_error::truncated; 6593376a787SDiego Novillo 6603376a787SDiego Novillo for (uint32_t I = 0; I < Size; ++I) { 6613376a787SDiego Novillo StringRef Str; 6623376a787SDiego Novillo if (!GcovBuffer.readString(Str)) 6633376a787SDiego Novillo return sampleprof_error::truncated; 6643376a787SDiego Novillo Names.push_back(Str); 6653376a787SDiego Novillo } 6663376a787SDiego Novillo 6673376a787SDiego Novillo return sampleprof_error::success; 6683376a787SDiego Novillo } 6693376a787SDiego Novillo 6703376a787SDiego Novillo std::error_code SampleProfileReaderGCC::readFunctionProfiles() { 6713376a787SDiego Novillo if (std::error_code EC = readSectionTag(GCOVTagAFDOFunction)) 6723376a787SDiego Novillo return EC; 6733376a787SDiego Novillo 6743376a787SDiego Novillo uint32_t NumFunctions; 6753376a787SDiego Novillo if (!GcovBuffer.readInt(NumFunctions)) 6763376a787SDiego Novillo return sampleprof_error::truncated; 6773376a787SDiego Novillo 678aae1ed8eSDiego Novillo InlineCallStack Stack; 6793376a787SDiego Novillo for (uint32_t I = 0; I < NumFunctions; ++I) 680aae1ed8eSDiego Novillo if (std::error_code EC = readOneFunctionProfile(Stack, true, 0)) 6813376a787SDiego Novillo return EC; 6823376a787SDiego Novillo 68340ee23dbSEaswaran Raman computeSummary(); 6843376a787SDiego Novillo return sampleprof_error::success; 6853376a787SDiego Novillo } 6863376a787SDiego Novillo 687aae1ed8eSDiego Novillo std::error_code SampleProfileReaderGCC::readOneFunctionProfile( 688aae1ed8eSDiego Novillo const InlineCallStack &InlineStack, bool Update, uint32_t Offset) { 6893376a787SDiego Novillo uint64_t HeadCount = 0; 690aae1ed8eSDiego Novillo if (InlineStack.size() == 0) 6913376a787SDiego Novillo if (!GcovBuffer.readInt64(HeadCount)) 6923376a787SDiego Novillo return sampleprof_error::truncated; 6933376a787SDiego Novillo 6943376a787SDiego Novillo uint32_t NameIdx; 6953376a787SDiego Novillo if (!GcovBuffer.readInt(NameIdx)) 6963376a787SDiego Novillo return sampleprof_error::truncated; 6973376a787SDiego Novillo 6983376a787SDiego Novillo StringRef Name(Names[NameIdx]); 6993376a787SDiego Novillo 7003376a787SDiego Novillo uint32_t NumPosCounts; 7013376a787SDiego Novillo if (!GcovBuffer.readInt(NumPosCounts)) 7023376a787SDiego Novillo return sampleprof_error::truncated; 7033376a787SDiego Novillo 704aae1ed8eSDiego Novillo uint32_t NumCallsites; 705aae1ed8eSDiego Novillo if (!GcovBuffer.readInt(NumCallsites)) 7063376a787SDiego Novillo return sampleprof_error::truncated; 7073376a787SDiego Novillo 708aae1ed8eSDiego Novillo FunctionSamples *FProfile = nullptr; 709aae1ed8eSDiego Novillo if (InlineStack.size() == 0) { 710aae1ed8eSDiego Novillo // If this is a top function that we have already processed, do not 711aae1ed8eSDiego Novillo // update its profile again. This happens in the presence of 712aae1ed8eSDiego Novillo // function aliases. Since these aliases share the same function 713aae1ed8eSDiego Novillo // body, there will be identical replicated profiles for the 714aae1ed8eSDiego Novillo // original function. In this case, we simply not bother updating 715aae1ed8eSDiego Novillo // the profile of the original function. 716aae1ed8eSDiego Novillo FProfile = &Profiles[Name]; 717aae1ed8eSDiego Novillo FProfile->addHeadSamples(HeadCount); 718aae1ed8eSDiego Novillo if (FProfile->getTotalSamples() > 0) 7193376a787SDiego Novillo Update = false; 720aae1ed8eSDiego Novillo } else { 721aae1ed8eSDiego Novillo // Otherwise, we are reading an inlined instance. The top of the 722aae1ed8eSDiego Novillo // inline stack contains the profile of the caller. Insert this 723aae1ed8eSDiego Novillo // callee in the caller's CallsiteMap. 724aae1ed8eSDiego Novillo FunctionSamples *CallerProfile = InlineStack.front(); 725aae1ed8eSDiego Novillo uint32_t LineOffset = Offset >> 16; 726aae1ed8eSDiego Novillo uint32_t Discriminator = Offset & 0xffff; 727aae1ed8eSDiego Novillo FProfile = &CallerProfile->functionSamplesAt( 7282c7ca9b5SDehao Chen LineLocation(LineOffset, Discriminator))[Name]; 7293376a787SDiego Novillo } 73057d1dda5SDehao Chen FProfile->setName(Name); 7313376a787SDiego Novillo 7323376a787SDiego Novillo for (uint32_t I = 0; I < NumPosCounts; ++I) { 7333376a787SDiego Novillo uint32_t Offset; 7343376a787SDiego Novillo if (!GcovBuffer.readInt(Offset)) 7353376a787SDiego Novillo return sampleprof_error::truncated; 7363376a787SDiego Novillo 7373376a787SDiego Novillo uint32_t NumTargets; 7383376a787SDiego Novillo if (!GcovBuffer.readInt(NumTargets)) 7393376a787SDiego Novillo return sampleprof_error::truncated; 7403376a787SDiego Novillo 7413376a787SDiego Novillo uint64_t Count; 7423376a787SDiego Novillo if (!GcovBuffer.readInt64(Count)) 7433376a787SDiego Novillo return sampleprof_error::truncated; 7443376a787SDiego Novillo 745aae1ed8eSDiego Novillo // The line location is encoded in the offset as: 746aae1ed8eSDiego Novillo // high 16 bits: line offset to the start of the function. 747aae1ed8eSDiego Novillo // low 16 bits: discriminator. 748aae1ed8eSDiego Novillo uint32_t LineOffset = Offset >> 16; 749aae1ed8eSDiego Novillo uint32_t Discriminator = Offset & 0xffff; 7503376a787SDiego Novillo 751aae1ed8eSDiego Novillo InlineCallStack NewStack; 752aae1ed8eSDiego Novillo NewStack.push_back(FProfile); 753aae1ed8eSDiego Novillo NewStack.insert(NewStack.end(), InlineStack.begin(), InlineStack.end()); 754aae1ed8eSDiego Novillo if (Update) { 755aae1ed8eSDiego Novillo // Walk up the inline stack, adding the samples on this line to 756aae1ed8eSDiego Novillo // the total sample count of the callers in the chain. 757aae1ed8eSDiego Novillo for (auto CallerProfile : NewStack) 758aae1ed8eSDiego Novillo CallerProfile->addTotalSamples(Count); 759aae1ed8eSDiego Novillo 760aae1ed8eSDiego Novillo // Update the body samples for the current profile. 761aae1ed8eSDiego Novillo FProfile->addBodySamples(LineOffset, Discriminator, Count); 762aae1ed8eSDiego Novillo } 763aae1ed8eSDiego Novillo 764aae1ed8eSDiego Novillo // Process the list of functions called at an indirect call site. 765aae1ed8eSDiego Novillo // These are all the targets that a function pointer (or virtual 766aae1ed8eSDiego Novillo // function) resolved at runtime. 7673376a787SDiego Novillo for (uint32_t J = 0; J < NumTargets; J++) { 7683376a787SDiego Novillo uint32_t HistVal; 7693376a787SDiego Novillo if (!GcovBuffer.readInt(HistVal)) 7703376a787SDiego Novillo return sampleprof_error::truncated; 7713376a787SDiego Novillo 7723376a787SDiego Novillo if (HistVal != HIST_TYPE_INDIR_CALL_TOPN) 7733376a787SDiego Novillo return sampleprof_error::malformed; 7743376a787SDiego Novillo 7753376a787SDiego Novillo uint64_t TargetIdx; 7763376a787SDiego Novillo if (!GcovBuffer.readInt64(TargetIdx)) 7773376a787SDiego Novillo return sampleprof_error::truncated; 7783376a787SDiego Novillo StringRef TargetName(Names[TargetIdx]); 7793376a787SDiego Novillo 7803376a787SDiego Novillo uint64_t TargetCount; 7813376a787SDiego Novillo if (!GcovBuffer.readInt64(TargetCount)) 7823376a787SDiego Novillo return sampleprof_error::truncated; 7833376a787SDiego Novillo 784920677a9SDehao Chen if (Update) 785920677a9SDehao Chen FProfile->addCalledTargetSamples(LineOffset, Discriminator, 786aae1ed8eSDiego Novillo TargetName, TargetCount); 7873376a787SDiego Novillo } 7883376a787SDiego Novillo } 7893376a787SDiego Novillo 790aae1ed8eSDiego Novillo // Process all the inlined callers into the current function. These 791aae1ed8eSDiego Novillo // are all the callsites that were inlined into this function. 792aae1ed8eSDiego Novillo for (uint32_t I = 0; I < NumCallsites; I++) { 7933376a787SDiego Novillo // The offset is encoded as: 7943376a787SDiego Novillo // high 16 bits: line offset to the start of the function. 7953376a787SDiego Novillo // low 16 bits: discriminator. 7963376a787SDiego Novillo uint32_t Offset; 7973376a787SDiego Novillo if (!GcovBuffer.readInt(Offset)) 7983376a787SDiego Novillo return sampleprof_error::truncated; 799aae1ed8eSDiego Novillo InlineCallStack NewStack; 800aae1ed8eSDiego Novillo NewStack.push_back(FProfile); 801aae1ed8eSDiego Novillo NewStack.insert(NewStack.end(), InlineStack.begin(), InlineStack.end()); 802aae1ed8eSDiego Novillo if (std::error_code EC = readOneFunctionProfile(NewStack, Update, Offset)) 8033376a787SDiego Novillo return EC; 8043376a787SDiego Novillo } 8053376a787SDiego Novillo 8063376a787SDiego Novillo return sampleprof_error::success; 8073376a787SDiego Novillo } 8083376a787SDiego Novillo 8095f8f34e4SAdrian Prantl /// Read a GCC AutoFDO profile. 8103376a787SDiego Novillo /// 8113376a787SDiego Novillo /// This format is generated by the Linux Perf conversion tool at 8123376a787SDiego Novillo /// https://github.com/google/autofdo. 8133376a787SDiego Novillo std::error_code SampleProfileReaderGCC::read() { 8143376a787SDiego Novillo // Read the string table. 8153376a787SDiego Novillo if (std::error_code EC = readNameTable()) 8163376a787SDiego Novillo return EC; 8173376a787SDiego Novillo 8183376a787SDiego Novillo // Read the source profile. 8193376a787SDiego Novillo if (std::error_code EC = readFunctionProfiles()) 8203376a787SDiego Novillo return EC; 8213376a787SDiego Novillo 8223376a787SDiego Novillo return sampleprof_error::success; 8233376a787SDiego Novillo } 8243376a787SDiego Novillo 8253376a787SDiego Novillo bool SampleProfileReaderGCC::hasFormat(const MemoryBuffer &Buffer) { 8263376a787SDiego Novillo StringRef Magic(reinterpret_cast<const char *>(Buffer.getBufferStart())); 8273376a787SDiego Novillo return Magic == "adcg*704"; 8283376a787SDiego Novillo } 8293376a787SDiego Novillo 8305f8f34e4SAdrian Prantl /// Prepare a memory buffer for the contents of \p Filename. 831de1ab26fSDiego Novillo /// 832c572e92cSDiego Novillo /// \returns an error code indicating the status of the buffer. 833fcd55607SDiego Novillo static ErrorOr<std::unique_ptr<MemoryBuffer>> 8340da23a27SBenjamin Kramer setupMemoryBuffer(const Twine &Filename) { 835c572e92cSDiego Novillo auto BufferOrErr = MemoryBuffer::getFileOrSTDIN(Filename); 836c572e92cSDiego Novillo if (std::error_code EC = BufferOrErr.getError()) 837c572e92cSDiego Novillo return EC; 838fcd55607SDiego Novillo auto Buffer = std::move(BufferOrErr.get()); 839c572e92cSDiego Novillo 840c572e92cSDiego Novillo // Sanity check the file. 841260fe3ecSZachary Turner if (uint64_t(Buffer->getBufferSize()) > std::numeric_limits<uint32_t>::max()) 842c572e92cSDiego Novillo return sampleprof_error::too_large; 843c572e92cSDiego Novillo 844fcd55607SDiego Novillo return std::move(Buffer); 845c572e92cSDiego Novillo } 846c572e92cSDiego Novillo 8475f8f34e4SAdrian Prantl /// Create a sample profile reader based on the format of the input file. 848c572e92cSDiego Novillo /// 849c572e92cSDiego Novillo /// \param Filename The file to open. 850c572e92cSDiego Novillo /// 851c572e92cSDiego Novillo /// \param C The LLVM context to use to emit diagnostics. 852c572e92cSDiego Novillo /// 853c572e92cSDiego Novillo /// \returns an error code indicating the status of the created reader. 854fcd55607SDiego Novillo ErrorOr<std::unique_ptr<SampleProfileReader>> 8550da23a27SBenjamin Kramer SampleProfileReader::create(const Twine &Filename, LLVMContext &C) { 856fcd55607SDiego Novillo auto BufferOrError = setupMemoryBuffer(Filename); 857fcd55607SDiego Novillo if (std::error_code EC = BufferOrError.getError()) 858c572e92cSDiego Novillo return EC; 85951abea74SNathan Slingerland return create(BufferOrError.get(), C); 86051abea74SNathan Slingerland } 861c572e92cSDiego Novillo 8625f8f34e4SAdrian Prantl /// Create a sample profile reader based on the format of the input data. 86351abea74SNathan Slingerland /// 86451abea74SNathan Slingerland /// \param B The memory buffer to create the reader from (assumes ownership). 86551abea74SNathan Slingerland /// 86651abea74SNathan Slingerland /// \param C The LLVM context to use to emit diagnostics. 86751abea74SNathan Slingerland /// 86851abea74SNathan Slingerland /// \returns an error code indicating the status of the created reader. 86951abea74SNathan Slingerland ErrorOr<std::unique_ptr<SampleProfileReader>> 87051abea74SNathan Slingerland SampleProfileReader::create(std::unique_ptr<MemoryBuffer> &B, LLVMContext &C) { 871fcd55607SDiego Novillo std::unique_ptr<SampleProfileReader> Reader; 872a0c0857eSWei Mi if (SampleProfileReaderRawBinary::hasFormat(*B)) 873a0c0857eSWei Mi Reader.reset(new SampleProfileReaderRawBinary(std::move(B), C)); 874a0c0857eSWei Mi else if (SampleProfileReaderCompactBinary::hasFormat(*B)) 875a0c0857eSWei Mi Reader.reset(new SampleProfileReaderCompactBinary(std::move(B), C)); 87651abea74SNathan Slingerland else if (SampleProfileReaderGCC::hasFormat(*B)) 87751abea74SNathan Slingerland Reader.reset(new SampleProfileReaderGCC(std::move(B), C)); 87851abea74SNathan Slingerland else if (SampleProfileReaderText::hasFormat(*B)) 87951abea74SNathan Slingerland Reader.reset(new SampleProfileReaderText(std::move(B), C)); 8804f823667SNathan Slingerland else 8814f823667SNathan Slingerland return sampleprof_error::unrecognized_format; 882c572e92cSDiego Novillo 883fcd55607SDiego Novillo if (std::error_code EC = Reader->readHeader()) 884fcd55607SDiego Novillo return EC; 885fcd55607SDiego Novillo 886fcd55607SDiego Novillo return std::move(Reader); 887de1ab26fSDiego Novillo } 88840ee23dbSEaswaran Raman 88940ee23dbSEaswaran Raman // For text and GCC file formats, we compute the summary after reading the 89040ee23dbSEaswaran Raman // profile. Binary format has the profile summary in its header. 89140ee23dbSEaswaran Raman void SampleProfileReader::computeSummary() { 892e5a17e3fSEaswaran Raman SampleProfileSummaryBuilder Builder(ProfileSummaryBuilder::DefaultCutoffs); 89340ee23dbSEaswaran Raman for (const auto &I : Profiles) { 89440ee23dbSEaswaran Raman const FunctionSamples &Profile = I.second; 895e5a17e3fSEaswaran Raman Builder.addRecord(Profile); 89640ee23dbSEaswaran Raman } 89738de59e4SBenjamin Kramer Summary = Builder.getSummary(); 89840ee23dbSEaswaran Raman } 899