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" 26de1ab26fSDiego Novillo #include "llvm/Support/Debug.h" 27de1ab26fSDiego Novillo #include "llvm/Support/ErrorOr.h" 28c572e92cSDiego Novillo #include "llvm/Support/LEB128.h" 29de1ab26fSDiego Novillo #include "llvm/Support/LineIterator.h" 30c572e92cSDiego Novillo #include "llvm/Support/MemoryBuffer.h" 31de1ab26fSDiego Novillo 32c572e92cSDiego Novillo using namespace llvm::sampleprof; 33de1ab26fSDiego Novillo using namespace llvm; 34de1ab26fSDiego Novillo 35de1ab26fSDiego Novillo /// \brief Dump the function profile for \p FName. 36de1ab26fSDiego Novillo /// 37de1ab26fSDiego Novillo /// \param FName Name of the function to print. 38d5336ae2SDiego Novillo /// \param OS Stream to emit the output to. 39d5336ae2SDiego Novillo void SampleProfileReader::dumpFunctionProfile(StringRef FName, 40d5336ae2SDiego Novillo raw_ostream &OS) { 418e415a82SDiego Novillo OS << "Function: " << FName << ": " << Profiles[FName]; 42de1ab26fSDiego Novillo } 43de1ab26fSDiego Novillo 44d5336ae2SDiego Novillo /// \brief Dump all the function profiles found on stream \p OS. 45d5336ae2SDiego Novillo void SampleProfileReader::dump(raw_ostream &OS) { 46d5336ae2SDiego Novillo for (const auto &I : Profiles) 47d5336ae2SDiego Novillo dumpFunctionProfile(I.getKey(), OS); 48de1ab26fSDiego Novillo } 49de1ab26fSDiego Novillo 506722688eSDehao Chen /// \brief Parse \p Input as function head. 516722688eSDehao Chen /// 526722688eSDehao Chen /// Parse one line of \p Input, and update function name in \p FName, 536722688eSDehao Chen /// function's total sample count in \p NumSamples, function's entry 546722688eSDehao Chen /// count in \p NumHeadSamples. 556722688eSDehao Chen /// 566722688eSDehao Chen /// \returns true if parsing is successful. 576722688eSDehao Chen static bool ParseHead(const StringRef &Input, StringRef &FName, 5838be3330SDiego Novillo uint64_t &NumSamples, uint64_t &NumHeadSamples) { 596722688eSDehao Chen if (Input[0] == ' ') 606722688eSDehao Chen return false; 616722688eSDehao Chen size_t n2 = Input.rfind(':'); 626722688eSDehao Chen size_t n1 = Input.rfind(':', n2 - 1); 636722688eSDehao Chen FName = Input.substr(0, n1); 646722688eSDehao Chen if (Input.substr(n1 + 1, n2 - n1 - 1).getAsInteger(10, NumSamples)) 656722688eSDehao Chen return false; 666722688eSDehao Chen if (Input.substr(n2 + 1).getAsInteger(10, NumHeadSamples)) 676722688eSDehao Chen return false; 686722688eSDehao Chen return true; 696722688eSDehao Chen } 706722688eSDehao Chen 7110042412SDehao Chen /// \brief Returns true if line offset \p L is legal (only has 16 bits). 7257d1dda5SDehao Chen static bool isOffsetLegal(unsigned L) { return (L & 0xffff) == L; } 7310042412SDehao Chen 746722688eSDehao Chen /// \brief Parse \p Input as line sample. 756722688eSDehao Chen /// 766722688eSDehao Chen /// \param Input input line. 776722688eSDehao Chen /// \param IsCallsite true if the line represents an inlined callsite. 786722688eSDehao Chen /// \param Depth the depth of the inline stack. 796722688eSDehao Chen /// \param NumSamples total samples of the line/inlined callsite. 806722688eSDehao Chen /// \param LineOffset line offset to the start of the function. 816722688eSDehao Chen /// \param Discriminator discriminator of the line. 826722688eSDehao Chen /// \param TargetCountMap map from indirect call target to count. 836722688eSDehao Chen /// 846722688eSDehao Chen /// returns true if parsing is successful. 8538be3330SDiego Novillo static bool ParseLine(const StringRef &Input, bool &IsCallsite, uint32_t &Depth, 8638be3330SDiego Novillo uint64_t &NumSamples, uint32_t &LineOffset, 8738be3330SDiego Novillo uint32_t &Discriminator, StringRef &CalleeName, 8838be3330SDiego Novillo DenseMap<StringRef, uint64_t> &TargetCountMap) { 896722688eSDehao Chen for (Depth = 0; Input[Depth] == ' '; Depth++) 906722688eSDehao Chen ; 916722688eSDehao Chen if (Depth == 0) 926722688eSDehao Chen return false; 936722688eSDehao Chen 946722688eSDehao Chen size_t n1 = Input.find(':'); 956722688eSDehao Chen StringRef Loc = Input.substr(Depth, n1 - Depth); 966722688eSDehao Chen size_t n2 = Loc.find('.'); 976722688eSDehao Chen if (n2 == StringRef::npos) { 9810042412SDehao Chen if (Loc.getAsInteger(10, LineOffset) || !isOffsetLegal(LineOffset)) 996722688eSDehao Chen return false; 1006722688eSDehao Chen Discriminator = 0; 1016722688eSDehao Chen } else { 1026722688eSDehao Chen if (Loc.substr(0, n2).getAsInteger(10, LineOffset)) 1036722688eSDehao Chen return false; 1046722688eSDehao Chen if (Loc.substr(n2 + 1).getAsInteger(10, Discriminator)) 1056722688eSDehao Chen return false; 1066722688eSDehao Chen } 1076722688eSDehao Chen 1086722688eSDehao Chen StringRef Rest = Input.substr(n1 + 2); 1096722688eSDehao Chen if (Rest[0] >= '0' && Rest[0] <= '9') { 1106722688eSDehao Chen IsCallsite = false; 1116722688eSDehao Chen size_t n3 = Rest.find(' '); 1126722688eSDehao Chen if (n3 == StringRef::npos) { 1136722688eSDehao Chen if (Rest.getAsInteger(10, NumSamples)) 1146722688eSDehao Chen return false; 1156722688eSDehao Chen } else { 1166722688eSDehao Chen if (Rest.substr(0, n3).getAsInteger(10, NumSamples)) 1176722688eSDehao Chen return false; 1186722688eSDehao Chen } 1196722688eSDehao Chen while (n3 != StringRef::npos) { 1206722688eSDehao Chen n3 += Rest.substr(n3).find_first_not_of(' '); 1216722688eSDehao Chen Rest = Rest.substr(n3); 1226722688eSDehao Chen n3 = Rest.find(' '); 1236722688eSDehao Chen StringRef pair = Rest; 1246722688eSDehao Chen if (n3 != StringRef::npos) { 1256722688eSDehao Chen pair = Rest.substr(0, n3); 1266722688eSDehao Chen } 12738be3330SDiego Novillo size_t n4 = pair.find(':'); 12838be3330SDiego Novillo uint64_t count; 1296722688eSDehao Chen if (pair.substr(n4 + 1).getAsInteger(10, count)) 1306722688eSDehao Chen return false; 1316722688eSDehao Chen TargetCountMap[pair.substr(0, n4)] = count; 1326722688eSDehao Chen } 1336722688eSDehao Chen } else { 1346722688eSDehao Chen IsCallsite = true; 13538be3330SDiego Novillo size_t n3 = Rest.find_last_of(':'); 1366722688eSDehao Chen CalleeName = Rest.substr(0, n3); 1376722688eSDehao Chen if (Rest.substr(n3 + 1).getAsInteger(10, NumSamples)) 1386722688eSDehao Chen return false; 1396722688eSDehao Chen } 1406722688eSDehao Chen return true; 1416722688eSDehao Chen } 1426722688eSDehao Chen 143de1ab26fSDiego Novillo /// \brief Load samples from a text file. 144de1ab26fSDiego Novillo /// 145de1ab26fSDiego Novillo /// See the documentation at the top of the file for an explanation of 146de1ab26fSDiego Novillo /// the expected format. 147de1ab26fSDiego Novillo /// 148de1ab26fSDiego Novillo /// \returns true if the file was loaded successfully, false otherwise. 149c572e92cSDiego Novillo std::error_code SampleProfileReaderText::read() { 150c572e92cSDiego Novillo line_iterator LineIt(*Buffer, /*SkipBlanks=*/true, '#'); 15148dd080cSNathan Slingerland sampleprof_error Result = sampleprof_error::success; 152de1ab26fSDiego Novillo 153aae1ed8eSDiego Novillo InlineCallStack InlineStack; 1546722688eSDehao Chen 1556722688eSDehao Chen for (; !LineIt.is_at_eof(); ++LineIt) { 1566722688eSDehao Chen if ((*LineIt)[(*LineIt).find_first_not_of(' ')] == '#') 1576722688eSDehao Chen continue; 158de1ab26fSDiego Novillo // Read the header of each function. 159de1ab26fSDiego Novillo // 160de1ab26fSDiego Novillo // Note that for function identifiers we are actually expecting 161de1ab26fSDiego Novillo // mangled names, but we may not always get them. This happens when 162de1ab26fSDiego Novillo // the compiler decides not to emit the function (e.g., it was inlined 163de1ab26fSDiego Novillo // and removed). In this case, the binary will not have the linkage 164de1ab26fSDiego Novillo // name for the function, so the profiler will emit the function's 165de1ab26fSDiego Novillo // unmangled name, which may contain characters like ':' and '>' in its 166de1ab26fSDiego Novillo // name (member functions, templates, etc). 167de1ab26fSDiego Novillo // 168de1ab26fSDiego Novillo // The only requirement we place on the identifier, then, is that it 169de1ab26fSDiego Novillo // should not begin with a number. 1706722688eSDehao Chen if ((*LineIt)[0] != ' ') { 17138be3330SDiego Novillo uint64_t NumSamples, NumHeadSamples; 1726722688eSDehao Chen StringRef FName; 1736722688eSDehao Chen if (!ParseHead(*LineIt, FName, NumSamples, NumHeadSamples)) { 1743376a787SDiego Novillo reportError(LineIt.line_number(), 175de1ab26fSDiego Novillo "Expected 'mangled_name:NUM:NUM', found " + *LineIt); 176c572e92cSDiego Novillo return sampleprof_error::malformed; 177de1ab26fSDiego Novillo } 178de1ab26fSDiego Novillo Profiles[FName] = FunctionSamples(); 179de1ab26fSDiego Novillo FunctionSamples &FProfile = Profiles[FName]; 18057d1dda5SDehao Chen FProfile.setName(FName); 18148dd080cSNathan Slingerland MergeResult(Result, FProfile.addTotalSamples(NumSamples)); 18248dd080cSNathan Slingerland MergeResult(Result, FProfile.addHeadSamples(NumHeadSamples)); 1836722688eSDehao Chen InlineStack.clear(); 1846722688eSDehao Chen InlineStack.push_back(&FProfile); 1856722688eSDehao Chen } else { 18638be3330SDiego Novillo uint64_t NumSamples; 1876722688eSDehao Chen StringRef FName; 18838be3330SDiego Novillo DenseMap<StringRef, uint64_t> TargetCountMap; 1896722688eSDehao Chen bool IsCallsite; 19038be3330SDiego Novillo uint32_t Depth, LineOffset, Discriminator; 1916722688eSDehao Chen if (!ParseLine(*LineIt, IsCallsite, Depth, NumSamples, LineOffset, 1926722688eSDehao Chen Discriminator, FName, TargetCountMap)) { 1933376a787SDiego Novillo reportError(LineIt.line_number(), 1943376a787SDiego Novillo "Expected 'NUM[.NUM]: NUM[ mangled_name:NUM]*', found " + 1953376a787SDiego Novillo *LineIt); 196c572e92cSDiego Novillo return sampleprof_error::malformed; 197de1ab26fSDiego Novillo } 1986722688eSDehao Chen if (IsCallsite) { 1996722688eSDehao Chen while (InlineStack.size() > Depth) { 2006722688eSDehao Chen InlineStack.pop_back(); 201c572e92cSDiego Novillo } 2026722688eSDehao Chen FunctionSamples &FSamples = InlineStack.back()->functionSamplesAt( 20357d1dda5SDehao Chen LineLocation(LineOffset, Discriminator)); 20457d1dda5SDehao Chen FSamples.setName(FName); 20548dd080cSNathan Slingerland MergeResult(Result, FSamples.addTotalSamples(NumSamples)); 2066722688eSDehao Chen InlineStack.push_back(&FSamples); 2076722688eSDehao Chen } else { 2086722688eSDehao Chen while (InlineStack.size() > Depth) { 2096722688eSDehao Chen InlineStack.pop_back(); 2106722688eSDehao Chen } 2116722688eSDehao Chen FunctionSamples &FProfile = *InlineStack.back(); 2126722688eSDehao Chen for (const auto &name_count : TargetCountMap) { 21348dd080cSNathan Slingerland MergeResult(Result, FProfile.addCalledTargetSamples( 21448dd080cSNathan Slingerland LineOffset, Discriminator, name_count.first, 21548dd080cSNathan Slingerland name_count.second)); 216c572e92cSDiego Novillo } 21748dd080cSNathan Slingerland MergeResult(Result, FProfile.addBodySamples(LineOffset, Discriminator, 21848dd080cSNathan Slingerland NumSamples)); 2196722688eSDehao Chen } 220de1ab26fSDiego Novillo } 221de1ab26fSDiego Novillo } 22240ee23dbSEaswaran Raman if (Result == sampleprof_error::success) 22340ee23dbSEaswaran Raman computeSummary(); 224de1ab26fSDiego Novillo 22548dd080cSNathan Slingerland return Result; 226de1ab26fSDiego Novillo } 227de1ab26fSDiego Novillo 2284f823667SNathan Slingerland bool SampleProfileReaderText::hasFormat(const MemoryBuffer &Buffer) { 2294f823667SNathan Slingerland bool result = false; 2304f823667SNathan Slingerland 2314f823667SNathan Slingerland // Check that the first non-comment line is a valid function header. 2324f823667SNathan Slingerland line_iterator LineIt(Buffer, /*SkipBlanks=*/true, '#'); 2334f823667SNathan Slingerland if (!LineIt.is_at_eof()) { 2344f823667SNathan Slingerland if ((*LineIt)[0] != ' ') { 2354f823667SNathan Slingerland uint64_t NumSamples, NumHeadSamples; 2364f823667SNathan Slingerland StringRef FName; 2374f823667SNathan Slingerland result = ParseHead(*LineIt, FName, NumSamples, NumHeadSamples); 2384f823667SNathan Slingerland } 2394f823667SNathan Slingerland } 2404f823667SNathan Slingerland 2414f823667SNathan Slingerland return result; 2424f823667SNathan Slingerland } 2434f823667SNathan Slingerland 244d5336ae2SDiego Novillo template <typename T> ErrorOr<T> SampleProfileReaderBinary::readNumber() { 245c572e92cSDiego Novillo unsigned NumBytesRead = 0; 246c572e92cSDiego Novillo std::error_code EC; 247c572e92cSDiego Novillo uint64_t Val = decodeULEB128(Data, &NumBytesRead); 248c572e92cSDiego Novillo 249c572e92cSDiego Novillo if (Val > std::numeric_limits<T>::max()) 250c572e92cSDiego Novillo EC = sampleprof_error::malformed; 251c572e92cSDiego Novillo else if (Data + NumBytesRead > End) 252c572e92cSDiego Novillo EC = sampleprof_error::truncated; 253c572e92cSDiego Novillo else 254c572e92cSDiego Novillo EC = sampleprof_error::success; 255c572e92cSDiego Novillo 256c572e92cSDiego Novillo if (EC) { 2573376a787SDiego Novillo reportError(0, EC.message()); 258c572e92cSDiego Novillo return EC; 259c572e92cSDiego Novillo } 260c572e92cSDiego Novillo 261c572e92cSDiego Novillo Data += NumBytesRead; 262c572e92cSDiego Novillo return static_cast<T>(Val); 263c572e92cSDiego Novillo } 264c572e92cSDiego Novillo 265c572e92cSDiego Novillo ErrorOr<StringRef> SampleProfileReaderBinary::readString() { 266c572e92cSDiego Novillo std::error_code EC; 267c572e92cSDiego Novillo StringRef Str(reinterpret_cast<const char *>(Data)); 268c572e92cSDiego Novillo if (Data + Str.size() + 1 > End) { 269c572e92cSDiego Novillo EC = sampleprof_error::truncated; 2703376a787SDiego Novillo reportError(0, EC.message()); 271c572e92cSDiego Novillo return EC; 272c572e92cSDiego Novillo } 273c572e92cSDiego Novillo 274c572e92cSDiego Novillo Data += Str.size() + 1; 275c572e92cSDiego Novillo return Str; 276c572e92cSDiego Novillo } 277c572e92cSDiego Novillo 278760c5a8fSDiego Novillo ErrorOr<StringRef> SampleProfileReaderBinary::readStringFromTable() { 279760c5a8fSDiego Novillo std::error_code EC; 28038be3330SDiego Novillo auto Idx = readNumber<uint32_t>(); 281760c5a8fSDiego Novillo if (std::error_code EC = Idx.getError()) 282760c5a8fSDiego Novillo return EC; 283760c5a8fSDiego Novillo if (*Idx >= NameTable.size()) 284760c5a8fSDiego Novillo return sampleprof_error::truncated_name_table; 285760c5a8fSDiego Novillo return NameTable[*Idx]; 286760c5a8fSDiego Novillo } 287760c5a8fSDiego Novillo 288a7f1e8efSDiego Novillo std::error_code 289a7f1e8efSDiego Novillo SampleProfileReaderBinary::readProfile(FunctionSamples &FProfile) { 290b93483dbSDiego Novillo auto NumSamples = readNumber<uint64_t>(); 291b93483dbSDiego Novillo if (std::error_code EC = NumSamples.getError()) 292c572e92cSDiego Novillo return EC; 293b93483dbSDiego Novillo FProfile.addTotalSamples(*NumSamples); 294c572e92cSDiego Novillo 295c572e92cSDiego Novillo // Read the samples in the body. 29638be3330SDiego Novillo auto NumRecords = readNumber<uint32_t>(); 297c572e92cSDiego Novillo if (std::error_code EC = NumRecords.getError()) 298c572e92cSDiego Novillo return EC; 299a7f1e8efSDiego Novillo 30038be3330SDiego Novillo for (uint32_t I = 0; I < *NumRecords; ++I) { 301c572e92cSDiego Novillo auto LineOffset = readNumber<uint64_t>(); 302c572e92cSDiego Novillo if (std::error_code EC = LineOffset.getError()) 303c572e92cSDiego Novillo return EC; 304c572e92cSDiego Novillo 30510042412SDehao Chen if (!isOffsetLegal(*LineOffset)) { 30610042412SDehao Chen return std::error_code(); 30710042412SDehao Chen } 30810042412SDehao Chen 309c572e92cSDiego Novillo auto Discriminator = readNumber<uint64_t>(); 310c572e92cSDiego Novillo if (std::error_code EC = Discriminator.getError()) 311c572e92cSDiego Novillo return EC; 312c572e92cSDiego Novillo 313c572e92cSDiego Novillo auto NumSamples = readNumber<uint64_t>(); 314c572e92cSDiego Novillo if (std::error_code EC = NumSamples.getError()) 315c572e92cSDiego Novillo return EC; 316c572e92cSDiego Novillo 31738be3330SDiego Novillo auto NumCalls = readNumber<uint32_t>(); 318c572e92cSDiego Novillo if (std::error_code EC = NumCalls.getError()) 319c572e92cSDiego Novillo return EC; 320c572e92cSDiego Novillo 32138be3330SDiego Novillo for (uint32_t J = 0; J < *NumCalls; ++J) { 322760c5a8fSDiego Novillo auto CalledFunction(readStringFromTable()); 323c572e92cSDiego Novillo if (std::error_code EC = CalledFunction.getError()) 324c572e92cSDiego Novillo return EC; 325c572e92cSDiego Novillo 326c572e92cSDiego Novillo auto CalledFunctionSamples = readNumber<uint64_t>(); 327c572e92cSDiego Novillo if (std::error_code EC = CalledFunctionSamples.getError()) 328c572e92cSDiego Novillo return EC; 329c572e92cSDiego Novillo 330c572e92cSDiego Novillo FProfile.addCalledTargetSamples(*LineOffset, *Discriminator, 331a7f1e8efSDiego Novillo *CalledFunction, *CalledFunctionSamples); 332c572e92cSDiego Novillo } 333c572e92cSDiego Novillo 334c572e92cSDiego Novillo FProfile.addBodySamples(*LineOffset, *Discriminator, *NumSamples); 335c572e92cSDiego Novillo } 336a7f1e8efSDiego Novillo 337a7f1e8efSDiego Novillo // Read all the samples for inlined function calls. 33838be3330SDiego Novillo auto NumCallsites = readNumber<uint32_t>(); 339a7f1e8efSDiego Novillo if (std::error_code EC = NumCallsites.getError()) 340a7f1e8efSDiego Novillo return EC; 341a7f1e8efSDiego Novillo 34238be3330SDiego Novillo for (uint32_t J = 0; J < *NumCallsites; ++J) { 343a7f1e8efSDiego Novillo auto LineOffset = readNumber<uint64_t>(); 344a7f1e8efSDiego Novillo if (std::error_code EC = LineOffset.getError()) 345a7f1e8efSDiego Novillo return EC; 346a7f1e8efSDiego Novillo 347a7f1e8efSDiego Novillo auto Discriminator = readNumber<uint64_t>(); 348a7f1e8efSDiego Novillo if (std::error_code EC = Discriminator.getError()) 349a7f1e8efSDiego Novillo return EC; 350a7f1e8efSDiego Novillo 351760c5a8fSDiego Novillo auto FName(readStringFromTable()); 352a7f1e8efSDiego Novillo if (std::error_code EC = FName.getError()) 353a7f1e8efSDiego Novillo return EC; 354a7f1e8efSDiego Novillo 35557d1dda5SDehao Chen FunctionSamples &CalleeProfile = 35657d1dda5SDehao Chen FProfile.functionSamplesAt(LineLocation(*LineOffset, *Discriminator)); 35757d1dda5SDehao Chen CalleeProfile.setName(*FName); 358a7f1e8efSDiego Novillo if (std::error_code EC = readProfile(CalleeProfile)) 359a7f1e8efSDiego Novillo return EC; 360a7f1e8efSDiego Novillo } 361a7f1e8efSDiego Novillo 362a7f1e8efSDiego Novillo return sampleprof_error::success; 363a7f1e8efSDiego Novillo } 364a7f1e8efSDiego Novillo 365a7f1e8efSDiego Novillo std::error_code SampleProfileReaderBinary::read() { 366a7f1e8efSDiego Novillo while (!at_eof()) { 367b93483dbSDiego Novillo auto NumHeadSamples = readNumber<uint64_t>(); 368b93483dbSDiego Novillo if (std::error_code EC = NumHeadSamples.getError()) 369b93483dbSDiego Novillo return EC; 370b93483dbSDiego Novillo 371760c5a8fSDiego Novillo auto FName(readStringFromTable()); 372a7f1e8efSDiego Novillo if (std::error_code EC = FName.getError()) 373a7f1e8efSDiego Novillo return EC; 374a7f1e8efSDiego Novillo 375a7f1e8efSDiego Novillo Profiles[*FName] = FunctionSamples(); 376a7f1e8efSDiego Novillo FunctionSamples &FProfile = Profiles[*FName]; 37757d1dda5SDehao Chen FProfile.setName(*FName); 378a7f1e8efSDiego Novillo 379b93483dbSDiego Novillo FProfile.addHeadSamples(*NumHeadSamples); 380b93483dbSDiego Novillo 381a7f1e8efSDiego Novillo if (std::error_code EC = readProfile(FProfile)) 382a7f1e8efSDiego Novillo return EC; 383c572e92cSDiego Novillo } 384c572e92cSDiego Novillo 385c572e92cSDiego Novillo return sampleprof_error::success; 386c572e92cSDiego Novillo } 387c572e92cSDiego Novillo 388c572e92cSDiego Novillo std::error_code SampleProfileReaderBinary::readHeader() { 389c572e92cSDiego Novillo Data = reinterpret_cast<const uint8_t *>(Buffer->getBufferStart()); 390c572e92cSDiego Novillo End = Data + Buffer->getBufferSize(); 391c572e92cSDiego Novillo 392c572e92cSDiego Novillo // Read and check the magic identifier. 393c572e92cSDiego Novillo auto Magic = readNumber<uint64_t>(); 394c572e92cSDiego Novillo if (std::error_code EC = Magic.getError()) 395c572e92cSDiego Novillo return EC; 396c572e92cSDiego Novillo else if (*Magic != SPMagic()) 397c572e92cSDiego Novillo return sampleprof_error::bad_magic; 398c572e92cSDiego Novillo 399c572e92cSDiego Novillo // Read the version number. 400c572e92cSDiego Novillo auto Version = readNumber<uint64_t>(); 401c572e92cSDiego Novillo if (std::error_code EC = Version.getError()) 402c572e92cSDiego Novillo return EC; 403c572e92cSDiego Novillo else if (*Version != SPVersion()) 404c572e92cSDiego Novillo return sampleprof_error::unsupported_version; 405c572e92cSDiego Novillo 40640ee23dbSEaswaran Raman if (std::error_code EC = readSummary()) 40740ee23dbSEaswaran Raman return EC; 40840ee23dbSEaswaran Raman 409760c5a8fSDiego Novillo // Read the name table. 41038be3330SDiego Novillo auto Size = readNumber<uint32_t>(); 411760c5a8fSDiego Novillo if (std::error_code EC = Size.getError()) 412760c5a8fSDiego Novillo return EC; 413760c5a8fSDiego Novillo NameTable.reserve(*Size); 41438be3330SDiego Novillo for (uint32_t I = 0; I < *Size; ++I) { 415760c5a8fSDiego Novillo auto Name(readString()); 416760c5a8fSDiego Novillo if (std::error_code EC = Name.getError()) 417760c5a8fSDiego Novillo return EC; 418760c5a8fSDiego Novillo NameTable.push_back(*Name); 419760c5a8fSDiego Novillo } 420760c5a8fSDiego Novillo 421c572e92cSDiego Novillo return sampleprof_error::success; 422c572e92cSDiego Novillo } 423c572e92cSDiego Novillo 42440ee23dbSEaswaran Raman std::error_code SampleProfileReaderBinary::readSummaryEntry( 42540ee23dbSEaswaran Raman std::vector<ProfileSummaryEntry> &Entries) { 42640ee23dbSEaswaran Raman auto Cutoff = readNumber<uint64_t>(); 42740ee23dbSEaswaran Raman if (std::error_code EC = Cutoff.getError()) 42840ee23dbSEaswaran Raman return EC; 42940ee23dbSEaswaran Raman 43040ee23dbSEaswaran Raman auto MinBlockCount = readNumber<uint64_t>(); 43140ee23dbSEaswaran Raman if (std::error_code EC = MinBlockCount.getError()) 43240ee23dbSEaswaran Raman return EC; 43340ee23dbSEaswaran Raman 43440ee23dbSEaswaran Raman auto NumBlocks = readNumber<uint64_t>(); 43540ee23dbSEaswaran Raman if (std::error_code EC = NumBlocks.getError()) 43640ee23dbSEaswaran Raman return EC; 43740ee23dbSEaswaran Raman 43840ee23dbSEaswaran Raman Entries.emplace_back(*Cutoff, *MinBlockCount, *NumBlocks); 43940ee23dbSEaswaran Raman return sampleprof_error::success; 44040ee23dbSEaswaran Raman } 44140ee23dbSEaswaran Raman 44240ee23dbSEaswaran Raman std::error_code SampleProfileReaderBinary::readSummary() { 44340ee23dbSEaswaran Raman auto TotalCount = readNumber<uint64_t>(); 44440ee23dbSEaswaran Raman if (std::error_code EC = TotalCount.getError()) 44540ee23dbSEaswaran Raman return EC; 44640ee23dbSEaswaran Raman 44740ee23dbSEaswaran Raman auto MaxBlockCount = readNumber<uint64_t>(); 44840ee23dbSEaswaran Raman if (std::error_code EC = MaxBlockCount.getError()) 44940ee23dbSEaswaran Raman return EC; 45040ee23dbSEaswaran Raman 45140ee23dbSEaswaran Raman auto MaxFunctionCount = readNumber<uint64_t>(); 45240ee23dbSEaswaran Raman if (std::error_code EC = MaxFunctionCount.getError()) 45340ee23dbSEaswaran Raman return EC; 45440ee23dbSEaswaran Raman 45540ee23dbSEaswaran Raman auto NumBlocks = readNumber<uint64_t>(); 45640ee23dbSEaswaran Raman if (std::error_code EC = NumBlocks.getError()) 45740ee23dbSEaswaran Raman return EC; 45840ee23dbSEaswaran Raman 45940ee23dbSEaswaran Raman auto NumFunctions = readNumber<uint64_t>(); 46040ee23dbSEaswaran Raman if (std::error_code EC = NumFunctions.getError()) 46140ee23dbSEaswaran Raman return EC; 46240ee23dbSEaswaran Raman 46340ee23dbSEaswaran Raman auto NumSummaryEntries = readNumber<uint64_t>(); 46440ee23dbSEaswaran Raman if (std::error_code EC = NumSummaryEntries.getError()) 46540ee23dbSEaswaran Raman return EC; 46640ee23dbSEaswaran Raman 46740ee23dbSEaswaran Raman std::vector<ProfileSummaryEntry> Entries; 46840ee23dbSEaswaran Raman for (unsigned i = 0; i < *NumSummaryEntries; i++) { 46940ee23dbSEaswaran Raman std::error_code EC = readSummaryEntry(Entries); 47040ee23dbSEaswaran Raman if (EC != sampleprof_error::success) 47140ee23dbSEaswaran Raman return EC; 47240ee23dbSEaswaran Raman } 4737cefdb81SEaswaran Raman Summary = llvm::make_unique<ProfileSummary>( 4747cefdb81SEaswaran Raman ProfileSummary::PSK_Sample, Entries, *TotalCount, *MaxBlockCount, 0, 4757cefdb81SEaswaran Raman *MaxFunctionCount, *NumBlocks, *NumFunctions); 47640ee23dbSEaswaran Raman 47740ee23dbSEaswaran Raman return sampleprof_error::success; 47840ee23dbSEaswaran Raman } 47940ee23dbSEaswaran Raman 480c572e92cSDiego Novillo bool SampleProfileReaderBinary::hasFormat(const MemoryBuffer &Buffer) { 481c572e92cSDiego Novillo const uint8_t *Data = 482c572e92cSDiego Novillo reinterpret_cast<const uint8_t *>(Buffer.getBufferStart()); 483c572e92cSDiego Novillo uint64_t Magic = decodeULEB128(Data); 484c572e92cSDiego Novillo return Magic == SPMagic(); 485c572e92cSDiego Novillo } 486c572e92cSDiego Novillo 4873376a787SDiego Novillo std::error_code SampleProfileReaderGCC::skipNextWord() { 4883376a787SDiego Novillo uint32_t dummy; 4893376a787SDiego Novillo if (!GcovBuffer.readInt(dummy)) 4903376a787SDiego Novillo return sampleprof_error::truncated; 4913376a787SDiego Novillo return sampleprof_error::success; 4923376a787SDiego Novillo } 4933376a787SDiego Novillo 4943376a787SDiego Novillo template <typename T> ErrorOr<T> SampleProfileReaderGCC::readNumber() { 4953376a787SDiego Novillo if (sizeof(T) <= sizeof(uint32_t)) { 4963376a787SDiego Novillo uint32_t Val; 4973376a787SDiego Novillo if (GcovBuffer.readInt(Val) && Val <= std::numeric_limits<T>::max()) 4983376a787SDiego Novillo return static_cast<T>(Val); 4993376a787SDiego Novillo } else if (sizeof(T) <= sizeof(uint64_t)) { 5003376a787SDiego Novillo uint64_t Val; 5013376a787SDiego Novillo if (GcovBuffer.readInt64(Val) && Val <= std::numeric_limits<T>::max()) 5023376a787SDiego Novillo return static_cast<T>(Val); 5033376a787SDiego Novillo } 5043376a787SDiego Novillo 5053376a787SDiego Novillo std::error_code EC = sampleprof_error::malformed; 5063376a787SDiego Novillo reportError(0, EC.message()); 5073376a787SDiego Novillo return EC; 5083376a787SDiego Novillo } 5093376a787SDiego Novillo 5103376a787SDiego Novillo ErrorOr<StringRef> SampleProfileReaderGCC::readString() { 5113376a787SDiego Novillo StringRef Str; 5123376a787SDiego Novillo if (!GcovBuffer.readString(Str)) 5133376a787SDiego Novillo return sampleprof_error::truncated; 5143376a787SDiego Novillo return Str; 5153376a787SDiego Novillo } 5163376a787SDiego Novillo 5173376a787SDiego Novillo std::error_code SampleProfileReaderGCC::readHeader() { 5183376a787SDiego Novillo // Read the magic identifier. 5193376a787SDiego Novillo if (!GcovBuffer.readGCDAFormat()) 5203376a787SDiego Novillo return sampleprof_error::unrecognized_format; 5213376a787SDiego Novillo 5223376a787SDiego Novillo // Read the version number. Note - the GCC reader does not validate this 5233376a787SDiego Novillo // version, but the profile creator generates v704. 5243376a787SDiego Novillo GCOV::GCOVVersion version; 5253376a787SDiego Novillo if (!GcovBuffer.readGCOVVersion(version)) 5263376a787SDiego Novillo return sampleprof_error::unrecognized_format; 5273376a787SDiego Novillo 5283376a787SDiego Novillo if (version != GCOV::V704) 5293376a787SDiego Novillo return sampleprof_error::unsupported_version; 5303376a787SDiego Novillo 5313376a787SDiego Novillo // Skip the empty integer. 5323376a787SDiego Novillo if (std::error_code EC = skipNextWord()) 5333376a787SDiego Novillo return EC; 5343376a787SDiego Novillo 5353376a787SDiego Novillo return sampleprof_error::success; 5363376a787SDiego Novillo } 5373376a787SDiego Novillo 5383376a787SDiego Novillo std::error_code SampleProfileReaderGCC::readSectionTag(uint32_t Expected) { 5393376a787SDiego Novillo uint32_t Tag; 5403376a787SDiego Novillo if (!GcovBuffer.readInt(Tag)) 5413376a787SDiego Novillo return sampleprof_error::truncated; 5423376a787SDiego Novillo 5433376a787SDiego Novillo if (Tag != Expected) 5443376a787SDiego Novillo return sampleprof_error::malformed; 5453376a787SDiego Novillo 5463376a787SDiego Novillo if (std::error_code EC = skipNextWord()) 5473376a787SDiego Novillo return EC; 5483376a787SDiego Novillo 5493376a787SDiego Novillo return sampleprof_error::success; 5503376a787SDiego Novillo } 5513376a787SDiego Novillo 5523376a787SDiego Novillo std::error_code SampleProfileReaderGCC::readNameTable() { 5533376a787SDiego Novillo if (std::error_code EC = readSectionTag(GCOVTagAFDOFileNames)) 5543376a787SDiego Novillo return EC; 5553376a787SDiego Novillo 5563376a787SDiego Novillo uint32_t Size; 5573376a787SDiego Novillo if (!GcovBuffer.readInt(Size)) 5583376a787SDiego Novillo return sampleprof_error::truncated; 5593376a787SDiego Novillo 5603376a787SDiego Novillo for (uint32_t I = 0; I < Size; ++I) { 5613376a787SDiego Novillo StringRef Str; 5623376a787SDiego Novillo if (!GcovBuffer.readString(Str)) 5633376a787SDiego Novillo return sampleprof_error::truncated; 5643376a787SDiego Novillo Names.push_back(Str); 5653376a787SDiego Novillo } 5663376a787SDiego Novillo 5673376a787SDiego Novillo return sampleprof_error::success; 5683376a787SDiego Novillo } 5693376a787SDiego Novillo 5703376a787SDiego Novillo std::error_code SampleProfileReaderGCC::readFunctionProfiles() { 5713376a787SDiego Novillo if (std::error_code EC = readSectionTag(GCOVTagAFDOFunction)) 5723376a787SDiego Novillo return EC; 5733376a787SDiego Novillo 5743376a787SDiego Novillo uint32_t NumFunctions; 5753376a787SDiego Novillo if (!GcovBuffer.readInt(NumFunctions)) 5763376a787SDiego Novillo return sampleprof_error::truncated; 5773376a787SDiego Novillo 578aae1ed8eSDiego Novillo InlineCallStack Stack; 5793376a787SDiego Novillo for (uint32_t I = 0; I < NumFunctions; ++I) 580aae1ed8eSDiego Novillo if (std::error_code EC = readOneFunctionProfile(Stack, true, 0)) 5813376a787SDiego Novillo return EC; 5823376a787SDiego Novillo 58340ee23dbSEaswaran Raman computeSummary(); 5843376a787SDiego Novillo return sampleprof_error::success; 5853376a787SDiego Novillo } 5863376a787SDiego Novillo 587aae1ed8eSDiego Novillo std::error_code SampleProfileReaderGCC::readOneFunctionProfile( 588aae1ed8eSDiego Novillo const InlineCallStack &InlineStack, bool Update, uint32_t Offset) { 5893376a787SDiego Novillo uint64_t HeadCount = 0; 590aae1ed8eSDiego Novillo if (InlineStack.size() == 0) 5913376a787SDiego Novillo if (!GcovBuffer.readInt64(HeadCount)) 5923376a787SDiego Novillo return sampleprof_error::truncated; 5933376a787SDiego Novillo 5943376a787SDiego Novillo uint32_t NameIdx; 5953376a787SDiego Novillo if (!GcovBuffer.readInt(NameIdx)) 5963376a787SDiego Novillo return sampleprof_error::truncated; 5973376a787SDiego Novillo 5983376a787SDiego Novillo StringRef Name(Names[NameIdx]); 5993376a787SDiego Novillo 6003376a787SDiego Novillo uint32_t NumPosCounts; 6013376a787SDiego Novillo if (!GcovBuffer.readInt(NumPosCounts)) 6023376a787SDiego Novillo return sampleprof_error::truncated; 6033376a787SDiego Novillo 604aae1ed8eSDiego Novillo uint32_t NumCallsites; 605aae1ed8eSDiego Novillo if (!GcovBuffer.readInt(NumCallsites)) 6063376a787SDiego Novillo return sampleprof_error::truncated; 6073376a787SDiego Novillo 608aae1ed8eSDiego Novillo FunctionSamples *FProfile = nullptr; 609aae1ed8eSDiego Novillo if (InlineStack.size() == 0) { 610aae1ed8eSDiego Novillo // If this is a top function that we have already processed, do not 611aae1ed8eSDiego Novillo // update its profile again. This happens in the presence of 612aae1ed8eSDiego Novillo // function aliases. Since these aliases share the same function 613aae1ed8eSDiego Novillo // body, there will be identical replicated profiles for the 614aae1ed8eSDiego Novillo // original function. In this case, we simply not bother updating 615aae1ed8eSDiego Novillo // the profile of the original function. 616aae1ed8eSDiego Novillo FProfile = &Profiles[Name]; 617aae1ed8eSDiego Novillo FProfile->addHeadSamples(HeadCount); 618aae1ed8eSDiego Novillo if (FProfile->getTotalSamples() > 0) 6193376a787SDiego Novillo Update = false; 620aae1ed8eSDiego Novillo } else { 621aae1ed8eSDiego Novillo // Otherwise, we are reading an inlined instance. The top of the 622aae1ed8eSDiego Novillo // inline stack contains the profile of the caller. Insert this 623aae1ed8eSDiego Novillo // callee in the caller's CallsiteMap. 624aae1ed8eSDiego Novillo FunctionSamples *CallerProfile = InlineStack.front(); 625aae1ed8eSDiego Novillo uint32_t LineOffset = Offset >> 16; 626aae1ed8eSDiego Novillo uint32_t Discriminator = Offset & 0xffff; 627aae1ed8eSDiego Novillo FProfile = &CallerProfile->functionSamplesAt( 62857d1dda5SDehao Chen LineLocation(LineOffset, Discriminator)); 6293376a787SDiego Novillo } 63057d1dda5SDehao Chen FProfile->setName(Name); 6313376a787SDiego Novillo 6323376a787SDiego Novillo for (uint32_t I = 0; I < NumPosCounts; ++I) { 6333376a787SDiego Novillo uint32_t Offset; 6343376a787SDiego Novillo if (!GcovBuffer.readInt(Offset)) 6353376a787SDiego Novillo return sampleprof_error::truncated; 6363376a787SDiego Novillo 6373376a787SDiego Novillo uint32_t NumTargets; 6383376a787SDiego Novillo if (!GcovBuffer.readInt(NumTargets)) 6393376a787SDiego Novillo return sampleprof_error::truncated; 6403376a787SDiego Novillo 6413376a787SDiego Novillo uint64_t Count; 6423376a787SDiego Novillo if (!GcovBuffer.readInt64(Count)) 6433376a787SDiego Novillo return sampleprof_error::truncated; 6443376a787SDiego Novillo 645aae1ed8eSDiego Novillo // The line location is encoded in the offset as: 646aae1ed8eSDiego Novillo // high 16 bits: line offset to the start of the function. 647aae1ed8eSDiego Novillo // low 16 bits: discriminator. 648aae1ed8eSDiego Novillo uint32_t LineOffset = Offset >> 16; 649aae1ed8eSDiego Novillo uint32_t Discriminator = Offset & 0xffff; 6503376a787SDiego Novillo 651aae1ed8eSDiego Novillo InlineCallStack NewStack; 652aae1ed8eSDiego Novillo NewStack.push_back(FProfile); 653aae1ed8eSDiego Novillo NewStack.insert(NewStack.end(), InlineStack.begin(), InlineStack.end()); 654aae1ed8eSDiego Novillo if (Update) { 655aae1ed8eSDiego Novillo // Walk up the inline stack, adding the samples on this line to 656aae1ed8eSDiego Novillo // the total sample count of the callers in the chain. 657aae1ed8eSDiego Novillo for (auto CallerProfile : NewStack) 658aae1ed8eSDiego Novillo CallerProfile->addTotalSamples(Count); 659aae1ed8eSDiego Novillo 660aae1ed8eSDiego Novillo // Update the body samples for the current profile. 661aae1ed8eSDiego Novillo FProfile->addBodySamples(LineOffset, Discriminator, Count); 662aae1ed8eSDiego Novillo } 663aae1ed8eSDiego Novillo 664aae1ed8eSDiego Novillo // Process the list of functions called at an indirect call site. 665aae1ed8eSDiego Novillo // These are all the targets that a function pointer (or virtual 666aae1ed8eSDiego Novillo // function) resolved at runtime. 6673376a787SDiego Novillo for (uint32_t J = 0; J < NumTargets; J++) { 6683376a787SDiego Novillo uint32_t HistVal; 6693376a787SDiego Novillo if (!GcovBuffer.readInt(HistVal)) 6703376a787SDiego Novillo return sampleprof_error::truncated; 6713376a787SDiego Novillo 6723376a787SDiego Novillo if (HistVal != HIST_TYPE_INDIR_CALL_TOPN) 6733376a787SDiego Novillo return sampleprof_error::malformed; 6743376a787SDiego Novillo 6753376a787SDiego Novillo uint64_t TargetIdx; 6763376a787SDiego Novillo if (!GcovBuffer.readInt64(TargetIdx)) 6773376a787SDiego Novillo return sampleprof_error::truncated; 6783376a787SDiego Novillo StringRef TargetName(Names[TargetIdx]); 6793376a787SDiego Novillo 6803376a787SDiego Novillo uint64_t TargetCount; 6813376a787SDiego Novillo if (!GcovBuffer.readInt64(TargetCount)) 6823376a787SDiego Novillo return sampleprof_error::truncated; 6833376a787SDiego Novillo 6843376a787SDiego Novillo if (Update) { 6853376a787SDiego Novillo FunctionSamples &TargetProfile = Profiles[TargetName]; 686aae1ed8eSDiego Novillo TargetProfile.addCalledTargetSamples(LineOffset, Discriminator, 687aae1ed8eSDiego Novillo TargetName, TargetCount); 6883376a787SDiego Novillo } 6893376a787SDiego Novillo } 6903376a787SDiego Novillo } 6913376a787SDiego Novillo 692aae1ed8eSDiego Novillo // Process all the inlined callers into the current function. These 693aae1ed8eSDiego Novillo // are all the callsites that were inlined into this function. 694aae1ed8eSDiego Novillo for (uint32_t I = 0; I < NumCallsites; I++) { 6953376a787SDiego Novillo // The offset is encoded as: 6963376a787SDiego Novillo // high 16 bits: line offset to the start of the function. 6973376a787SDiego Novillo // low 16 bits: discriminator. 6983376a787SDiego Novillo uint32_t Offset; 6993376a787SDiego Novillo if (!GcovBuffer.readInt(Offset)) 7003376a787SDiego Novillo return sampleprof_error::truncated; 701aae1ed8eSDiego Novillo InlineCallStack NewStack; 702aae1ed8eSDiego Novillo NewStack.push_back(FProfile); 703aae1ed8eSDiego Novillo NewStack.insert(NewStack.end(), InlineStack.begin(), InlineStack.end()); 704aae1ed8eSDiego Novillo if (std::error_code EC = readOneFunctionProfile(NewStack, Update, Offset)) 7053376a787SDiego Novillo return EC; 7063376a787SDiego Novillo } 7073376a787SDiego Novillo 7083376a787SDiego Novillo return sampleprof_error::success; 7093376a787SDiego Novillo } 7103376a787SDiego Novillo 7113376a787SDiego Novillo /// \brief Read a GCC AutoFDO profile. 7123376a787SDiego Novillo /// 7133376a787SDiego Novillo /// This format is generated by the Linux Perf conversion tool at 7143376a787SDiego Novillo /// https://github.com/google/autofdo. 7153376a787SDiego Novillo std::error_code SampleProfileReaderGCC::read() { 7163376a787SDiego Novillo // Read the string table. 7173376a787SDiego Novillo if (std::error_code EC = readNameTable()) 7183376a787SDiego Novillo return EC; 7193376a787SDiego Novillo 7203376a787SDiego Novillo // Read the source profile. 7213376a787SDiego Novillo if (std::error_code EC = readFunctionProfiles()) 7223376a787SDiego Novillo return EC; 7233376a787SDiego Novillo 7243376a787SDiego Novillo return sampleprof_error::success; 7253376a787SDiego Novillo } 7263376a787SDiego Novillo 7273376a787SDiego Novillo bool SampleProfileReaderGCC::hasFormat(const MemoryBuffer &Buffer) { 7283376a787SDiego Novillo StringRef Magic(reinterpret_cast<const char *>(Buffer.getBufferStart())); 7293376a787SDiego Novillo return Magic == "adcg*704"; 7303376a787SDiego Novillo } 7313376a787SDiego Novillo 732c572e92cSDiego Novillo /// \brief Prepare a memory buffer for the contents of \p Filename. 733de1ab26fSDiego Novillo /// 734c572e92cSDiego Novillo /// \returns an error code indicating the status of the buffer. 735fcd55607SDiego Novillo static ErrorOr<std::unique_ptr<MemoryBuffer>> 736fcd55607SDiego Novillo setupMemoryBuffer(std::string Filename) { 737c572e92cSDiego Novillo auto BufferOrErr = MemoryBuffer::getFileOrSTDIN(Filename); 738c572e92cSDiego Novillo if (std::error_code EC = BufferOrErr.getError()) 739c572e92cSDiego Novillo return EC; 740fcd55607SDiego Novillo auto Buffer = std::move(BufferOrErr.get()); 741c572e92cSDiego Novillo 742c572e92cSDiego Novillo // Sanity check the file. 74338be3330SDiego Novillo if (Buffer->getBufferSize() > std::numeric_limits<uint32_t>::max()) 744c572e92cSDiego Novillo return sampleprof_error::too_large; 745c572e92cSDiego Novillo 746fcd55607SDiego Novillo return std::move(Buffer); 747c572e92cSDiego Novillo } 748c572e92cSDiego Novillo 749c572e92cSDiego Novillo /// \brief Create a sample profile reader based on the format of the input file. 750c572e92cSDiego Novillo /// 751c572e92cSDiego Novillo /// \param Filename The file to open. 752c572e92cSDiego Novillo /// 753c572e92cSDiego Novillo /// \param Reader The reader to instantiate according to \p Filename's format. 754c572e92cSDiego Novillo /// 755c572e92cSDiego Novillo /// \param C The LLVM context to use to emit diagnostics. 756c572e92cSDiego Novillo /// 757c572e92cSDiego Novillo /// \returns an error code indicating the status of the created reader. 758fcd55607SDiego Novillo ErrorOr<std::unique_ptr<SampleProfileReader>> 759fcd55607SDiego Novillo SampleProfileReader::create(StringRef Filename, LLVMContext &C) { 760fcd55607SDiego Novillo auto BufferOrError = setupMemoryBuffer(Filename); 761fcd55607SDiego Novillo if (std::error_code EC = BufferOrError.getError()) 762c572e92cSDiego Novillo return EC; 76351abea74SNathan Slingerland return create(BufferOrError.get(), C); 76451abea74SNathan Slingerland } 765c572e92cSDiego Novillo 76651abea74SNathan Slingerland /// \brief Create a sample profile reader based on the format of the input data. 76751abea74SNathan Slingerland /// 76851abea74SNathan Slingerland /// \param B The memory buffer to create the reader from (assumes ownership). 76951abea74SNathan Slingerland /// 77051abea74SNathan Slingerland /// \param Reader The reader to instantiate according to \p Filename's format. 77151abea74SNathan Slingerland /// 77251abea74SNathan Slingerland /// \param C The LLVM context to use to emit diagnostics. 77351abea74SNathan Slingerland /// 77451abea74SNathan Slingerland /// \returns an error code indicating the status of the created reader. 77551abea74SNathan Slingerland ErrorOr<std::unique_ptr<SampleProfileReader>> 77651abea74SNathan Slingerland SampleProfileReader::create(std::unique_ptr<MemoryBuffer> &B, LLVMContext &C) { 777fcd55607SDiego Novillo std::unique_ptr<SampleProfileReader> Reader; 77851abea74SNathan Slingerland if (SampleProfileReaderBinary::hasFormat(*B)) 77951abea74SNathan Slingerland Reader.reset(new SampleProfileReaderBinary(std::move(B), C)); 78051abea74SNathan Slingerland else if (SampleProfileReaderGCC::hasFormat(*B)) 78151abea74SNathan Slingerland Reader.reset(new SampleProfileReaderGCC(std::move(B), C)); 78251abea74SNathan Slingerland else if (SampleProfileReaderText::hasFormat(*B)) 78351abea74SNathan Slingerland Reader.reset(new SampleProfileReaderText(std::move(B), C)); 7844f823667SNathan Slingerland else 7854f823667SNathan Slingerland return sampleprof_error::unrecognized_format; 786c572e92cSDiego Novillo 787fcd55607SDiego Novillo if (std::error_code EC = Reader->readHeader()) 788fcd55607SDiego Novillo return EC; 789fcd55607SDiego Novillo 790fcd55607SDiego Novillo return std::move(Reader); 791de1ab26fSDiego Novillo } 79240ee23dbSEaswaran Raman 79340ee23dbSEaswaran Raman // For text and GCC file formats, we compute the summary after reading the 79440ee23dbSEaswaran Raman // profile. Binary format has the profile summary in its header. 79540ee23dbSEaswaran Raman void SampleProfileReader::computeSummary() { 796e5a17e3fSEaswaran Raman SampleProfileSummaryBuilder Builder(ProfileSummaryBuilder::DefaultCutoffs); 79740ee23dbSEaswaran Raman for (const auto &I : Profiles) { 79840ee23dbSEaswaran Raman const FunctionSamples &Profile = I.second; 799e5a17e3fSEaswaran Raman Builder.addRecord(Profile); 80040ee23dbSEaswaran Raman } 801*38de59e4SBenjamin Kramer Summary = Builder.getSummary(); 80240ee23dbSEaswaran Raman } 803