Index: include/llvm/ProfileData/SampleProf.h =================================================================== --- include/llvm/ProfileData/SampleProf.h +++ include/llvm/ProfileData/SampleProf.h @@ -83,6 +83,7 @@ SPF_Text = 0x1, SPF_Compact_Binary = 0x2, SPF_GCC = 0x3, + SPF_Ext_Binary = 0x4, SPF_Binary = 0xff }; @@ -105,6 +106,19 @@ static inline uint64_t SPVersion() { return 103; } +// Section Type used by SampleProfileExtBinaryReader and +// SampleProfileExtBinaryWriter. +enum SecType { SecProfSummary = 0, SecNameTable = 1, SecFuncProfile = 2 }; + +// Entry type of section header table used by SampleProfileExtBinaryReader +// and SampleProfileExtBinaryWriter. +struct SecHdrTableEntry { + SecType Type; + uint64_t Flag; + uint64_t Offset; + uint64_t Size; +}; + /// Represents the relative location of an instruction. /// /// Instruction locations are specified by the line offset from the Index: include/llvm/ProfileData/SampleProfReader.h =================================================================== --- include/llvm/ProfileData/SampleProfReader.h +++ include/llvm/ProfileData/SampleProfReader.h @@ -416,6 +416,15 @@ /// Read the contents of the given profile instance. std::error_code readProfile(FunctionSamples &FProfile); + /// Read the contents of Magic number and Version number. + std::error_code readMagicIdent(); + + /// Read profile summary. + std::error_code readSummary(); + + /// Read the whole name table. + virtual std::error_code readNameTable() = 0; + /// Points to the current location in the buffer. const uint8_t *Data = nullptr; @@ -426,12 +435,6 @@ std::error_code readSummaryEntry(std::vector &Entries); virtual std::error_code verifySPMagic(uint64_t Magic) = 0; - /// Read profile summary. - std::error_code readSummary(); - - /// Read the whole name table. - virtual std::error_code readNameTable() = 0; - /// Read a string indirectly via the name table. virtual ErrorOr readStringFromTable() = 0; }; @@ -441,16 +444,52 @@ /// Function name table. std::vector NameTable; virtual std::error_code verifySPMagic(uint64_t Magic) override; - virtual std::error_code readNameTable() override; /// Read a string indirectly via the name table. virtual ErrorOr readStringFromTable() override; +protected: + virtual std::error_code readNameTable() override; + +public: + SampleProfileReaderRawBinary(std::unique_ptr B, LLVMContext &C, + SampleProfileFormat Format = SPF_Binary) + : SampleProfileReaderBinary(std::move(B), C, Format) {} + + /// \brief Return true if \p Buffer is in the format supported by this class. + static bool hasFormat(const MemoryBuffer &Buffer); +}; + +/// SampleProfileReaderExtBinary is a binary format which is more easily to be +/// extend than SampleProfileReaderRawBinary. +/// SampleProfileReaderExtBinary format is organized in sections except the +/// magic and version number at the beginning. There is a section table before +/// all the sections, and each entry in the table describes the entry type, +/// start, size and attributes. The format in each section is defined by the +/// section itself. +/// It is easy to add a new section while maintaining the backward +/// compatibility of the profile. Nothing extra needs to be done. If we want +/// to extend an existing section, like add cache misses information in +/// addition to the sample count in the profile body, we can add a new section +/// with the extension and retire the existing section, and we could choose +/// to keep the parser of the old section if we want the reader to be able +/// to read both new and old format profile. +class SampleProfileReaderExtBinary : public SampleProfileReaderRawBinary { +private: + std::vector SecHdrTable; + std::error_code readSecHdrTableEntry(); + std::error_code readSecHdrTable(); + virtual std::error_code readHeader() override; + virtual std::error_code verifySPMagic(uint64_t Magic) override; + public: - SampleProfileReaderRawBinary(std::unique_ptr B, LLVMContext &C) - : SampleProfileReaderBinary(std::move(B), C, SPF_Binary) {} + SampleProfileReaderExtBinary(std::unique_ptr B, LLVMContext &C) + : SampleProfileReaderRawBinary(std::move(B), C, SPF_Ext_Binary) {} /// \brief Return true if \p Buffer is in the format supported by this class. static bool hasFormat(const MemoryBuffer &Buffer); + + /// Read sample profiles in extensible format from the associated file. + std::error_code read() override; }; class SampleProfileReaderCompactBinary : public SampleProfileReaderBinary { Index: include/llvm/ProfileData/SampleProfWriter.h =================================================================== --- include/llvm/ProfileData/SampleProfWriter.h +++ include/llvm/ProfileData/SampleProfWriter.h @@ -36,7 +36,7 @@ /// Write sample profiles in \p S. /// /// \returns status code of the file update operation. - virtual std::error_code write(const FunctionSamples &S) = 0; + virtual std::error_code writeSample(const FunctionSamples &S) = 0; /// Write all the sample profiles in the given map of samples. /// @@ -64,6 +64,10 @@ virtual std::error_code writeHeader(const StringMap &ProfileMap) = 0; + // Write function profiles to the profile file. + virtual std::error_code + writeFuncProfiles(const StringMap &ProfileMap); + /// Output stream where to emit the profile to. std::unique_ptr OutputStream; @@ -72,12 +76,15 @@ /// Compute summary for this profile. void computeSummary(const StringMap &ProfileMap); + + /// Profile format. + SampleProfileFormat Format; }; /// Sample-based profile writer (text format). class SampleProfileWriterText : public SampleProfileWriter { public: - std::error_code write(const FunctionSamples &S) override; + std::error_code writeSample(const FunctionSamples &S) override; protected: SampleProfileWriterText(std::unique_ptr &OS) @@ -102,13 +109,13 @@ /// Sample-based profile writer (binary format). class SampleProfileWriterBinary : public SampleProfileWriter { public: - virtual std::error_code write(const FunctionSamples &S) override; + virtual std::error_code writeSample(const FunctionSamples &S) override; SampleProfileWriterBinary(std::unique_ptr &OS) : SampleProfileWriter(OS) {} protected: virtual std::error_code writeNameTable() = 0; - virtual std::error_code writeMagicIdent() = 0; + virtual std::error_code writeMagicIdent(SampleProfileFormat Format); virtual std::error_code writeHeader(const StringMap &ProfileMap) override; std::error_code writeSummary(); @@ -118,10 +125,10 @@ MapVector NameTable; -private: void addName(StringRef FName); void addNames(const FunctionSamples &S); +private: friend ErrorOr> SampleProfileWriter::create(std::unique_ptr &OS, SampleProfileFormat Format); @@ -132,7 +139,31 @@ protected: virtual std::error_code writeNameTable() override; - virtual std::error_code writeMagicIdent() override; +}; + +class SampleProfileWriterExtBinary : public SampleProfileWriterRawBinary { + using SampleProfileWriterRawBinary::SampleProfileWriterRawBinary; + +public: + virtual std::error_code + write(const StringMap &ProfileMap) override; + +private: + void allocSecHdrTable(); + std::error_code writeSecHdrTable(); + void markSectionStart(); + void addNewSection(SecType Sec); + virtual std::error_code + writeHeader(const StringMap &ProfileMap) override; + + // The location where the output stream starts. + uint64_t FileStart; + // The location in the output stream marked as the start of a new section. + uint64_t SectionStart; + // The location in the output stream where the SecHdrTable should be + // written to. + uint64_t SecHdrTableOffset; + std::vector SecHdrTable; }; // CompactBinary is a compact format of binary profile which both reduces @@ -169,7 +200,7 @@ using SampleProfileWriterBinary::SampleProfileWriterBinary; public: - virtual std::error_code write(const FunctionSamples &S) override; + virtual std::error_code writeSample(const FunctionSamples &S) override; virtual std::error_code write(const StringMap &ProfileMap) override; @@ -181,7 +212,6 @@ /// towards profile start. uint64_t TableOffset; virtual std::error_code writeNameTable() override; - virtual std::error_code writeMagicIdent() override; virtual std::error_code writeHeader(const StringMap &ProfileMap) override; std::error_code writeFuncOffsetTable(); Index: lib/ProfileData/SampleProfReader.cpp =================================================================== --- lib/ProfileData/SampleProfReader.cpp +++ lib/ProfileData/SampleProfReader.cpp @@ -467,6 +467,40 @@ return sampleprof_error::success; } +std::error_code SampleProfileReaderExtBinary::read() { + const uint8_t *BufStart = + reinterpret_cast(Buffer->getBufferStart()); + + for (auto &Entry : SecHdrTable) { + // Skip empty section. + if (!Entry.Size) + continue; + Data = BufStart + Entry.Offset; + switch (Entry.Type) { + case SecProfSummary: + if (std::error_code EC = readSummary()) + return EC; + break; + case SecNameTable: + if (std::error_code EC = readNameTable()) + return EC; + break; + case SecFuncProfile: + while (Data < BufStart + Entry.Offset + Entry.Size) { + if (std::error_code EC = readFuncProfile()) + return EC; + } + break; + default: + continue; + } + if (Data != BufStart + Entry.Offset + Entry.Size) + return sampleprof_error::malformed; + } + + return sampleprof_error::success; +} + std::error_code SampleProfileReaderCompactBinary::read() { std::vector OffsetsToUse; if (UseAllFuncs) { @@ -501,6 +535,12 @@ return sampleprof_error::bad_magic; } +std::error_code SampleProfileReaderExtBinary::verifySPMagic(uint64_t Magic) { + if (Magic == SPMagic(SPF_Ext_Binary)) + return sampleprof_error::success; + return sampleprof_error::bad_magic; +} + std::error_code SampleProfileReaderCompactBinary::verifySPMagic(uint64_t Magic) { if (Magic == SPMagic(SPF_Compact_Binary)) @@ -537,10 +577,60 @@ return sampleprof_error::success; } -std::error_code SampleProfileReaderBinary::readHeader() { - Data = reinterpret_cast(Buffer->getBufferStart()); - End = Data + Buffer->getBufferSize(); +std::error_code SampleProfileReaderExtBinary::readSecHdrTableEntry() { + SecHdrTableEntry Entry; + auto Type = readUnencodedNumber(); + if (std::error_code EC = Type.getError()) + return EC; + Entry.Type = static_cast(*Type); + + auto Flag = readUnencodedNumber(); + if (std::error_code EC = Flag.getError()) + return EC; + Entry.Flag = *Flag; + + auto Offset = readUnencodedNumber(); + if (std::error_code EC = Offset.getError()) + return EC; + Entry.Offset = *Offset; + auto Size = readUnencodedNumber(); + if (std::error_code EC = Size.getError()) + return EC; + Entry.Size = *Size; + + SecHdrTable.push_back(std::move(Entry)); + return sampleprof_error::success; +} + +std::error_code SampleProfileReaderExtBinary::readSecHdrTable() { + auto EntryNum = readUnencodedNumber(); + if (std::error_code EC = EntryNum.getError()) + return EC; + + for (uint32_t i = 0; i < (*EntryNum); i++) + if (std::error_code EC = readSecHdrTableEntry()) + return EC; + + return sampleprof_error::success; +} + +std::error_code SampleProfileReaderExtBinary::readHeader() { + const uint8_t *BufStart = + reinterpret_cast(Buffer->getBufferStart()); + Data = BufStart; + End = BufStart + Buffer->getBufferSize(); + + if (std::error_code EC = readMagicIdent()) + return EC; + + if (std::error_code EC = readSecHdrTable()) + return EC; + + return sampleprof_error::success; +} + +std::error_code SampleProfileReaderBinary::readMagicIdent() { // Read and check the magic identifier. auto Magic = readNumber(); if (std::error_code EC = Magic.getError()) @@ -555,6 +645,16 @@ else if (*Version != SPVersion()) return sampleprof_error::unsupported_version; + return sampleprof_error::success; +} + +std::error_code SampleProfileReaderBinary::readHeader() { + Data = reinterpret_cast(Buffer->getBufferStart()); + End = Data + Buffer->getBufferSize(); + + if (std::error_code EC = readMagicIdent()) + return EC; + if (std::error_code EC = readSummary()) return EC; @@ -674,6 +774,13 @@ return Magic == SPMagic(); } +bool SampleProfileReaderExtBinary::hasFormat(const MemoryBuffer &Buffer) { + const uint8_t *Data = + reinterpret_cast(Buffer.getBufferStart()); + uint64_t Magic = decodeULEB128(Data); + return Magic == SPMagic(SPF_Ext_Binary); +} + bool SampleProfileReaderCompactBinary::hasFormat(const MemoryBuffer &Buffer) { const uint8_t *Data = reinterpret_cast(Buffer.getBufferStart()); @@ -1023,6 +1130,8 @@ std::unique_ptr Reader; if (SampleProfileReaderRawBinary::hasFormat(*B)) Reader.reset(new SampleProfileReaderRawBinary(std::move(B), C)); + else if (SampleProfileReaderExtBinary::hasFormat(*B)) + Reader.reset(new SampleProfileReaderExtBinary(std::move(B), C)); else if (SampleProfileReaderCompactBinary::hasFormat(*B)) Reader.reset(new SampleProfileReaderCompactBinary(std::move(B), C)); else if (SampleProfileReaderGCC::hasFormat(*B)) @@ -1033,8 +1142,9 @@ return sampleprof_error::unrecognized_format; FunctionSamples::Format = Reader->getFormat(); - if (std::error_code EC = Reader->readHeader()) + if (std::error_code EC = Reader->readHeader()) { return EC; + } return std::move(Reader); } Index: lib/ProfileData/SampleProfWriter.cpp =================================================================== --- lib/ProfileData/SampleProfWriter.cpp +++ lib/ProfileData/SampleProfWriter.cpp @@ -39,11 +39,8 @@ using namespace llvm; using namespace sampleprof; -std::error_code -SampleProfileWriter::write(const StringMap &ProfileMap) { - if (std::error_code EC = writeHeader(ProfileMap)) - return EC; - +std::error_code SampleProfileWriter::writeFuncProfiles( + const StringMap &ProfileMap) { // Sort the ProfileMap by total samples. typedef std::pair NameFunctionSamples; std::vector V; @@ -58,12 +55,73 @@ }); for (const auto &I : V) { - if (std::error_code EC = write(*I.second)) + if (std::error_code EC = writeSample(*I.second)) return EC; } return sampleprof_error::success; } +std::error_code +SampleProfileWriter::write(const StringMap &ProfileMap) { + if (std::error_code EC = writeHeader(ProfileMap)) + return EC; + + if (std::error_code EC = writeFuncProfiles(ProfileMap)) + return EC; + + return sampleprof_error::success; +} + +void SampleProfileWriterExtBinary::markSectionStart() { + SectionStart = OutputStream->tell(); +} + +/// Add a new section into section header table. Note that AddNewSection +/// depends on markSectionStart or previous call of AddNewSection to mark +/// the start position of current section (saved in SectionStart). +/// AddNewSection will treat the content between position saved in +/// SectionStart and current position as a new section and add it to +/// SecHdrTable. AddNewSection will also automatically mark the end of +/// current section as a new section start. This is to avoid calling +/// markSectionStart repeatedly. If the end of previous section is not +/// the position you want to start the new section, call markSectionStart +/// before AddNewSection to mark the correct position. +void SampleProfileWriterExtBinary::addNewSection(SecType Sec) { + uint64_t SectionEnd = OutputStream->tell(); + SecHdrTable.push_back( + {Sec, 0, SectionStart - FileStart, SectionEnd - SectionStart}); + SectionStart = SectionEnd; +} + +std::error_code SampleProfileWriterExtBinary::write( + const StringMap &ProfileMap) { + if (std::error_code EC = writeHeader(ProfileMap)) + return EC; + + markSectionStart(); + computeSummary(ProfileMap); + if (auto EC = writeSummary()) + return EC; + addNewSection(SecProfSummary); + + // Generate the name table for all the functions referenced in the profile. + for (const auto &I : ProfileMap) { + addName(I.first()); + addNames(I.second); + } + writeNameTable(); + addNewSection(SecNameTable); + + if (std::error_code EC = writeFuncProfiles(ProfileMap)) + return EC; + addNewSection(SecFuncProfile); + + if (std::error_code EC = writeSecHdrTable()) + return EC; + + return sampleprof_error::success; +} + std::error_code SampleProfileWriterCompactBinary::write( const StringMap &ProfileMap) { if (std::error_code EC = SampleProfileWriter::write(ProfileMap)) @@ -81,7 +139,7 @@ /// /// The format used here is more structured and deliberate because /// it needs to be parsed by the SampleProfileReaderText class. -std::error_code SampleProfileWriterText::write(const FunctionSamples &S) { +std::error_code SampleProfileWriterText::writeSample(const FunctionSamples &S) { auto &OS = *OutputStream; OS << S.getName() << ":" << S.getTotalSamples(); if (Indent == 0) @@ -117,7 +175,7 @@ OS << Loc.LineOffset << ": "; else OS << Loc.LineOffset << "." << Loc.Discriminator << ": "; - if (std::error_code EC = write(CalleeSamples)) + if (std::error_code EC = writeSample(CalleeSamples)) return EC; } Indent -= 1; @@ -214,25 +272,18 @@ return sampleprof_error::success; } -std::error_code SampleProfileWriterRawBinary::writeMagicIdent() { - auto &OS = *OutputStream; - // Write file magic identifier. - encodeULEB128(SPMagic(), OS); - encodeULEB128(SPVersion(), OS); - return sampleprof_error::success; -} - -std::error_code SampleProfileWriterCompactBinary::writeMagicIdent() { +std::error_code +SampleProfileWriterBinary::writeMagicIdent(SampleProfileFormat Format) { auto &OS = *OutputStream; // Write file magic identifier. - encodeULEB128(SPMagic(SPF_Compact_Binary), OS); + encodeULEB128(SPMagic(Format), OS); encodeULEB128(SPVersion(), OS); return sampleprof_error::success; } std::error_code SampleProfileWriterBinary::writeHeader( const StringMap &ProfileMap) { - writeMagicIdent(); + writeMagicIdent(Format); computeSummary(ProfileMap); if (auto EC = writeSummary()) @@ -248,6 +299,68 @@ return sampleprof_error::success; } +SecType SectionList[] = {SecProfSummary, SecNameTable, SecFuncProfile}; + +void SampleProfileWriterExtBinary::allocSecHdrTable() { + support::endian::Writer Writer(*OutputStream, support::little); + + uint32_t SecNumber = sizeof(SectionList) / sizeof(SecType); + Writer.write(static_cast(SecNumber)); + SecHdrTableOffset = OutputStream->tell(); + for (uint32_t i = 0; i < SecNumber; i++) { + Writer.write(static_cast(-1)); + Writer.write(static_cast(-1)); + Writer.write(static_cast(-1)); + Writer.write(static_cast(-1)); + } +} + +std::error_code SampleProfileWriterExtBinary::writeSecHdrTable() { + auto &OFS = static_cast(*OutputStream); + uint64_t Saved = OutputStream->tell(); + + // Set OutputStream to the location saved in SecHdrTableOffset. + if (OFS.seek(SecHdrTableOffset) == (uint64_t)-1) + return sampleprof_error::ostream_seek_unsupported; + support::endian::Writer Writer(*OutputStream, support::little); + + DenseMap IndexMap; + for (uint32_t i = 0; i < SecHdrTable.size(); i++) { + IndexMap.insert({static_cast(SecHdrTable[i].Type), i}); + } + + // Write the sections in the order specified in SectionList. + // That is the sections order Reader will see. Note that the + // sections order in which Reader expects to read may be different + // from the order in which Writer is able to write, so we need + // to adjust the order in SecHdrTable to be consistent with + // SectionList when we write SecHdrTable to the memory. + uint32_t SecNumber = sizeof(SectionList) / sizeof(SecType); + for (uint32_t i = 0; i < SecNumber; i++) { + uint32_t idx = IndexMap[static_cast(SectionList[i])]; + Writer.write(static_cast(SecHdrTable[idx].Type)); + Writer.write(static_cast(SecHdrTable[idx].Flag)); + Writer.write(static_cast(SecHdrTable[idx].Offset)); + Writer.write(static_cast(SecHdrTable[idx].Size)); + } + + // Reset OutputStream. + if (OFS.seek(Saved) == (uint64_t)-1) + return sampleprof_error::ostream_seek_unsupported; + + return sampleprof_error::success; +} + +std::error_code SampleProfileWriterExtBinary::writeHeader( + const StringMap &ProfileMap) { + auto &OS = *OutputStream; + FileStart = OS.tell(); + writeMagicIdent(Format); + + allocSecHdrTable(); + return sampleprof_error::success; +} + std::error_code SampleProfileWriterCompactBinary::writeHeader( const StringMap &ProfileMap) { support::endian::Writer Writer(*OutputStream, support::little); @@ -324,13 +437,14 @@ /// Write samples of a top-level function to a binary file. /// /// \returns true if the samples were written successfully, false otherwise. -std::error_code SampleProfileWriterBinary::write(const FunctionSamples &S) { +std::error_code +SampleProfileWriterBinary::writeSample(const FunctionSamples &S) { encodeULEB128(S.getHeadSamples(), *OutputStream); return writeBody(S); } std::error_code -SampleProfileWriterCompactBinary::write(const FunctionSamples &S) { +SampleProfileWriterCompactBinary::writeSample(const FunctionSamples &S) { uint64_t Offset = OutputStream->tell(); StringRef Name = S.getName(); FuncOffsetTable[Name] = Offset; @@ -349,7 +463,8 @@ SampleProfileWriter::create(StringRef Filename, SampleProfileFormat Format) { std::error_code EC; std::unique_ptr OS; - if (Format == SPF_Binary || Format == SPF_Compact_Binary) + if (Format == SPF_Binary || Format == SPF_Ext_Binary || + Format == SPF_Compact_Binary) OS.reset(new raw_fd_ostream(Filename, EC, sys::fs::OF_None)); else OS.reset(new raw_fd_ostream(Filename, EC, sys::fs::OF_Text)); @@ -374,6 +489,8 @@ if (Format == SPF_Binary) Writer.reset(new SampleProfileWriterRawBinary(OS)); + else if (Format == SPF_Ext_Binary) + Writer.reset(new SampleProfileWriterExtBinary(OS)); else if (Format == SPF_Compact_Binary) Writer.reset(new SampleProfileWriterCompactBinary(OS)); else if (Format == SPF_Text) @@ -386,6 +503,7 @@ if (EC) return EC; + Writer->Format = Format; return std::move(Writer); } Index: test/Transforms/SampleProfile/compact-binary-profile.ll =================================================================== --- test/Transforms/SampleProfile/compact-binary-profile.ll +++ test/Transforms/SampleProfile/compact-binary-profile.ll @@ -1,121 +0,0 @@ -; RUN: opt < %s -sample-profile -sample-profile-file=%S/Inputs/inline.prof -S | FileCheck %s -; RUN: opt < %s -passes=sample-profile -sample-profile-file=%S/Inputs/inline.prof -S | FileCheck %s -; RUN: opt < %s -sample-profile -sample-profile-file=%S/Inputs/inline.compactbinary.afdo -S | FileCheck %s -; RUN: opt < %s -passes=sample-profile -sample-profile-file=%S/Inputs/inline.compactbinary.afdo -S | FileCheck %s - -; Original C++ test case -; -; #include -; -; int sum(int x, int y) { -; return x + y; -; } -; -; int main() { -; int s, i = 0; -; while (i++ < 20000 * 20000) -; if (i != 100) s = sum(i, s); else s = 30; -; printf("sum is %d\n", s); -; return 0; -; } -; -@.str = private unnamed_addr constant [11 x i8] c"sum is %d\0A\00", align 1 - -; Check sample-profile phase using compactbinary format profile will annotate -; the IR with exactly the same result as using text format. -; CHECK: br i1 %cmp, label %while.body, label %while.end{{.*}} !prof ![[IDX1:[0-9]*]] -; CHECK: br i1 %cmp1, label %if.then, label %if.else{{.*}} !prof ![[IDX2:[0-9]*]] -; CHECK: call i32 (i8*, ...) @printf{{.*}} !prof ![[IDX3:[0-9]*]] -; CHECK: = !{!"TotalCount", i64 26781} -; CHECK: = !{!"MaxCount", i64 5553} -; CHECK: ![[IDX1]] = !{!"branch_weights", i32 5392, i32 163} -; CHECK: ![[IDX2]] = !{!"branch_weights", i32 5280, i32 113} -; CHECK: ![[IDX3]] = !{!"branch_weights", i32 1} - -; Function Attrs: nounwind uwtable -define i32 @_Z3sumii(i32 %x, i32 %y) !dbg !4 { -entry: - %x.addr = alloca i32, align 4 - %y.addr = alloca i32, align 4 - store i32 %x, i32* %x.addr, align 4 - store i32 %y, i32* %y.addr, align 4 - %0 = load i32, i32* %x.addr, align 4, !dbg !11 - %1 = load i32, i32* %y.addr, align 4, !dbg !11 - %add = add nsw i32 %0, %1, !dbg !11 - ret i32 %add, !dbg !11 -} - -; Function Attrs: uwtable -define i32 @main() !dbg !7 { -entry: - %retval = alloca i32, align 4 - %s = alloca i32, align 4 - %i = alloca i32, align 4 - store i32 0, i32* %retval - store i32 0, i32* %i, align 4, !dbg !12 - br label %while.cond, !dbg !13 - -while.cond: ; preds = %if.end, %entry - %0 = load i32, i32* %i, align 4, !dbg !14 - %inc = add nsw i32 %0, 1, !dbg !14 - store i32 %inc, i32* %i, align 4, !dbg !14 - %cmp = icmp slt i32 %0, 400000000, !dbg !14 - br i1 %cmp, label %while.body, label %while.end, !dbg !14 - -while.body: ; preds = %while.cond - %1 = load i32, i32* %i, align 4, !dbg !16 - %cmp1 = icmp ne i32 %1, 100, !dbg !16 - br i1 %cmp1, label %if.then, label %if.else, !dbg !16 - - -if.then: ; preds = %while.body - %2 = load i32, i32* %i, align 4, !dbg !18 - %3 = load i32, i32* %s, align 4, !dbg !18 - %call = call i32 @_Z3sumii(i32 %2, i32 %3), !dbg !18 - store i32 %call, i32* %s, align 4, !dbg !18 - br label %if.end, !dbg !18 - -if.else: ; preds = %while.body - store i32 30, i32* %s, align 4, !dbg !20 - br label %if.end - -if.end: ; preds = %if.else, %if.then - br label %while.cond, !dbg !22 - -while.end: ; preds = %while.cond - %4 = load i32, i32* %s, align 4, !dbg !24 - %call2 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i32 0, i32 0), i32 %4), !dbg !24 - ret i32 0, !dbg !25 -} - -declare i32 @printf(i8*, ...) #2 - -!llvm.dbg.cu = !{!0} -!llvm.module.flags = !{!8, !9} -!llvm.ident = !{!10} - -!0 = distinct !DICompileUnit(language: DW_LANG_C_plus_plus, producer: "clang version 3.5 ", isOptimized: false, emissionKind: NoDebug, file: !1, enums: !2, retainedTypes: !2, globals: !2, imports: !2) -!1 = !DIFile(filename: "calls.cc", directory: ".") -!2 = !{} -!4 = distinct !DISubprogram(name: "sum", line: 3, isLocal: false, isDefinition: true, virtualIndex: 6, flags: DIFlagPrototyped, isOptimized: false, unit: !0, scopeLine: 3, file: !1, scope: !5, type: !6, retainedNodes: !2) -!5 = !DIFile(filename: "calls.cc", directory: ".") -!6 = !DISubroutineType(types: !2) -!7 = distinct !DISubprogram(name: "main", line: 7, isLocal: false, isDefinition: true, virtualIndex: 6, flags: DIFlagPrototyped, isOptimized: false, unit: !0, scopeLine: 7, file: !1, scope: !5, type: !6, retainedNodes: !2) -!8 = !{i32 2, !"Dwarf Version", i32 4} -!9 = !{i32 1, !"Debug Info Version", i32 3} -!10 = !{!"clang version 3.5 "} -!11 = !DILocation(line: 4, scope: !4) -!12 = !DILocation(line: 8, scope: !7) -!13 = !DILocation(line: 9, scope: !7) -!14 = !DILocation(line: 9, scope: !15) -!15 = !DILexicalBlockFile(discriminator: 2, file: !1, scope: !7) -!16 = !DILocation(line: 10, scope: !17) -!17 = distinct !DILexicalBlock(line: 10, column: 0, file: !1, scope: !7) -!18 = !DILocation(line: 10, scope: !19) -!19 = !DILexicalBlockFile(discriminator: 2, file: !1, scope: !17) -!20 = !DILocation(line: 10, scope: !21) -!21 = !DILexicalBlockFile(discriminator: 4, file: !1, scope: !17) -!22 = !DILocation(line: 10, scope: !23) -!23 = !DILexicalBlockFile(discriminator: 6, file: !1, scope: !17) -!24 = !DILocation(line: 11, scope: !7) -!25 = !DILocation(line: 12, scope: !7) Index: test/Transforms/SampleProfile/profile-format.ll =================================================================== --- test/Transforms/SampleProfile/profile-format.ll +++ test/Transforms/SampleProfile/profile-format.ll @@ -0,0 +1,123 @@ +; RUN: opt < %s -sample-profile -sample-profile-file=%S/Inputs/inline.prof -S | FileCheck %s +; RUN: opt < %s -passes=sample-profile -sample-profile-file=%S/Inputs/inline.prof -S | FileCheck %s +; RUN: opt < %s -sample-profile -sample-profile-file=%S/Inputs/inline.compactbinary.afdo -S | FileCheck %s +; RUN: opt < %s -passes=sample-profile -sample-profile-file=%S/Inputs/inline.compactbinary.afdo -S | FileCheck %s +; RUN: opt < %s -sample-profile -sample-profile-file=%S/Inputs/inline.extbinary.afdo -S | FileCheck %s +; RUN: opt < %s -passes=sample-profile -sample-profile-file=%S/Inputs/inline.extbinary.afdo -S | FileCheck %s + +; Original C++ test case +; +; #include +; +; int sum(int x, int y) { +; return x + y; +; } +; +; int main() { +; int s, i = 0; +; while (i++ < 20000 * 20000) +; if (i != 100) s = sum(i, s); else s = 30; +; printf("sum is %d\n", s); +; return 0; +; } +; +@.str = private unnamed_addr constant [11 x i8] c"sum is %d\0A\00", align 1 + +; Check sample-profile phase using compactbinary or extbinary format profile +; will annotate the IR with exactly the same result as using text format. +; CHECK: br i1 %cmp, label %while.body, label %while.end{{.*}} !prof ![[IDX1:[0-9]*]] +; CHECK: br i1 %cmp1, label %if.then, label %if.else{{.*}} !prof ![[IDX2:[0-9]*]] +; CHECK: call i32 (i8*, ...) @printf{{.*}} !prof ![[IDX3:[0-9]*]] +; CHECK: = !{!"TotalCount", i64 26781} +; CHECK: = !{!"MaxCount", i64 5553} +; CHECK: ![[IDX1]] = !{!"branch_weights", i32 5392, i32 163} +; CHECK: ![[IDX2]] = !{!"branch_weights", i32 5280, i32 113} +; CHECK: ![[IDX3]] = !{!"branch_weights", i32 1} + +; Function Attrs: nounwind uwtable +define i32 @_Z3sumii(i32 %x, i32 %y) !dbg !4 { +entry: + %x.addr = alloca i32, align 4 + %y.addr = alloca i32, align 4 + store i32 %x, i32* %x.addr, align 4 + store i32 %y, i32* %y.addr, align 4 + %0 = load i32, i32* %x.addr, align 4, !dbg !11 + %1 = load i32, i32* %y.addr, align 4, !dbg !11 + %add = add nsw i32 %0, %1, !dbg !11 + ret i32 %add, !dbg !11 +} + +; Function Attrs: uwtable +define i32 @main() !dbg !7 { +entry: + %retval = alloca i32, align 4 + %s = alloca i32, align 4 + %i = alloca i32, align 4 + store i32 0, i32* %retval + store i32 0, i32* %i, align 4, !dbg !12 + br label %while.cond, !dbg !13 + +while.cond: ; preds = %if.end, %entry + %0 = load i32, i32* %i, align 4, !dbg !14 + %inc = add nsw i32 %0, 1, !dbg !14 + store i32 %inc, i32* %i, align 4, !dbg !14 + %cmp = icmp slt i32 %0, 400000000, !dbg !14 + br i1 %cmp, label %while.body, label %while.end, !dbg !14 + +while.body: ; preds = %while.cond + %1 = load i32, i32* %i, align 4, !dbg !16 + %cmp1 = icmp ne i32 %1, 100, !dbg !16 + br i1 %cmp1, label %if.then, label %if.else, !dbg !16 + + +if.then: ; preds = %while.body + %2 = load i32, i32* %i, align 4, !dbg !18 + %3 = load i32, i32* %s, align 4, !dbg !18 + %call = call i32 @_Z3sumii(i32 %2, i32 %3), !dbg !18 + store i32 %call, i32* %s, align 4, !dbg !18 + br label %if.end, !dbg !18 + +if.else: ; preds = %while.body + store i32 30, i32* %s, align 4, !dbg !20 + br label %if.end + +if.end: ; preds = %if.else, %if.then + br label %while.cond, !dbg !22 + +while.end: ; preds = %while.cond + %4 = load i32, i32* %s, align 4, !dbg !24 + %call2 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i32 0, i32 0), i32 %4), !dbg !24 + ret i32 0, !dbg !25 +} + +declare i32 @printf(i8*, ...) #2 + +!llvm.dbg.cu = !{!0} +!llvm.module.flags = !{!8, !9} +!llvm.ident = !{!10} + +!0 = distinct !DICompileUnit(language: DW_LANG_C_plus_plus, producer: "clang version 3.5 ", isOptimized: false, emissionKind: NoDebug, file: !1, enums: !2, retainedTypes: !2, globals: !2, imports: !2) +!1 = !DIFile(filename: "calls.cc", directory: ".") +!2 = !{} +!4 = distinct !DISubprogram(name: "sum", line: 3, isLocal: false, isDefinition: true, virtualIndex: 6, flags: DIFlagPrototyped, isOptimized: false, unit: !0, scopeLine: 3, file: !1, scope: !5, type: !6, retainedNodes: !2) +!5 = !DIFile(filename: "calls.cc", directory: ".") +!6 = !DISubroutineType(types: !2) +!7 = distinct !DISubprogram(name: "main", line: 7, isLocal: false, isDefinition: true, virtualIndex: 6, flags: DIFlagPrototyped, isOptimized: false, unit: !0, scopeLine: 7, file: !1, scope: !5, type: !6, retainedNodes: !2) +!8 = !{i32 2, !"Dwarf Version", i32 4} +!9 = !{i32 1, !"Debug Info Version", i32 3} +!10 = !{!"clang version 3.5 "} +!11 = !DILocation(line: 4, scope: !4) +!12 = !DILocation(line: 8, scope: !7) +!13 = !DILocation(line: 9, scope: !7) +!14 = !DILocation(line: 9, scope: !15) +!15 = !DILexicalBlockFile(discriminator: 2, file: !1, scope: !7) +!16 = !DILocation(line: 10, scope: !17) +!17 = distinct !DILexicalBlock(line: 10, column: 0, file: !1, scope: !7) +!18 = !DILocation(line: 10, scope: !19) +!19 = !DILexicalBlockFile(discriminator: 2, file: !1, scope: !17) +!20 = !DILocation(line: 10, scope: !21) +!21 = !DILexicalBlockFile(discriminator: 4, file: !1, scope: !17) +!22 = !DILocation(line: 10, scope: !23) +!23 = !DILexicalBlockFile(discriminator: 6, file: !1, scope: !17) +!24 = !DILocation(line: 11, scope: !7) +!25 = !DILocation(line: 12, scope: !7) Index: tools/llvm-profdata/llvm-profdata.cpp =================================================================== --- tools/llvm-profdata/llvm-profdata.cpp +++ tools/llvm-profdata/llvm-profdata.cpp @@ -37,6 +37,7 @@ PF_None = 0, PF_Text, PF_Compact_Binary, + PF_Ext_Binary, PF_GCC, PF_Binary }; @@ -314,7 +315,7 @@ exitWithError("Cannot write indexed profdata format to stdout."); if (OutputFormat != PF_Binary && OutputFormat != PF_Compact_Binary && - OutputFormat != PF_Text) + OutputFormat != PF_Ext_Binary && OutputFormat != PF_Text) exitWithError("Unknown format is specified."); std::mutex ErrorLock; @@ -425,8 +426,12 @@ } static sampleprof::SampleProfileFormat FormatMap[] = { - sampleprof::SPF_None, sampleprof::SPF_Text, sampleprof::SPF_Compact_Binary, - sampleprof::SPF_GCC, sampleprof::SPF_Binary}; + sampleprof::SPF_None, + sampleprof::SPF_Text, + sampleprof::SPF_Compact_Binary, + sampleprof::SPF_Ext_Binary, + sampleprof::SPF_GCC, + sampleprof::SPF_Binary}; static void mergeSampleProfile(const WeightedFileVector &Inputs, SymbolRemapper *Remapper, Index: unittests/ProfileData/SampleProfTest.cpp =================================================================== --- unittests/ProfileData/SampleProfTest.cpp +++ unittests/ProfileData/SampleProfTest.cpp @@ -285,6 +285,10 @@ testRoundTrip(SampleProfileFormat::SPF_Compact_Binary, false); } +TEST_F(SampleProfTest, roundtrip_ext_binary_profile) { + testRoundTrip(SampleProfileFormat::SPF_Ext_Binary, false); +} + TEST_F(SampleProfTest, remap_text_profile) { testRoundTrip(SampleProfileFormat::SPF_Text, true); } @@ -293,6 +297,10 @@ testRoundTrip(SampleProfileFormat::SPF_Binary, true); } +TEST_F(SampleProfTest, remap_ext_binary_profile) { + testRoundTrip(SampleProfileFormat::SPF_Ext_Binary, true); +} + TEST_F(SampleProfTest, sample_overflow_saturation) { const uint64_t Max = std::numeric_limits::max(); sampleprof_error Result;