diff options
13 files changed, 128 insertions, 54 deletions
diff --git a/llvm/include/llvm/ProfileData/SampleProf.h b/llvm/include/llvm/ProfileData/SampleProf.h index 88551b8..b929545 100644 --- a/llvm/include/llvm/ProfileData/SampleProf.h +++ b/llvm/include/llvm/ProfileData/SampleProf.h @@ -315,7 +315,9 @@ struct LineLocationHash { raw_ostream &operator<<(raw_ostream &OS, const LineLocation &Loc); -using TypeMap = std::map<FunctionId, uint64_t>; +/// Key represents the id of a vtable and value represents its count. +/// TODO: Rename FunctionId to SymbolId. +using TypeCountMap = std::map<FunctionId, uint64_t>; /// Representation of a single sample record. /// @@ -406,8 +408,8 @@ public: uint64_t getSamples() const { return NumSamples; } const CallTargetMap &getCallTargets() const { return CallTargets; } - const TypeMap &getTypes() const { return TypeCounts; } - TypeMap &getTypes() { return TypeCounts; } + const TypeCountMap &getTypes() const { return TypeCounts; } + TypeCountMap &getTypes() { return TypeCounts; } const SortedCallTargetSet getSortedCallTargets() const { return sortCallTargets(CallTargets); } @@ -456,7 +458,8 @@ public: private: uint64_t NumSamples = 0; CallTargetMap CallTargets; - TypeMap TypeCounts; + // The vtable types and their counts in this sample record. + TypeCountMap TypeCounts; }; raw_ostream &operator<<(raw_ostream &OS, const SampleRecord &Sample); @@ -752,7 +755,7 @@ using BodySampleMap = std::map<LineLocation, SampleRecord>; // memory, which is *very* significant for large profiles. using FunctionSamplesMap = std::map<FunctionId, FunctionSamples>; using CallsiteSampleMap = std::map<LineLocation, FunctionSamplesMap>; -using CallsiteTypeMap = std::map<LineLocation, TypeMap>; +using CallsiteTypeMap = std::map<LineLocation, TypeCountMap>; using LocToLocMap = std::unordered_map<LineLocation, LineLocation, LineLocationHash>; @@ -810,19 +813,21 @@ public: Func, Num, Weight); } - sampleprof_error addTypeSamples(uint32_t LineOffset, uint32_t Discriminator, - FunctionId Func, uint64_t Num, - uint64_t Weight = 1) { - return BodySamples[LineLocation(LineOffset, Discriminator)].addTypeCount( - Func, Num, Weight); + sampleprof_error addFunctionBodyTypeSamples(const LineLocation &Loc, + FunctionId Func, uint64_t Num, + uint64_t Weight = 1) { + return BodySamples[Loc].addTypeCount(Func, Num, Weight); } - sampleprof_error addTypeSamples(const LineLocation &Loc, FunctionId Func, - uint64_t Num, uint64_t Weight = 1) { - return BodySamples[Loc].addTypeCount(Func, Num, Weight); + sampleprof_error addFunctionBodyTypeSamples(uint32_t LineOffset, + uint32_t Discriminator, + FunctionId Func, uint64_t Num, + uint64_t Weight = 1) { + return addFunctionBodyTypeSamples(LineLocation(LineOffset, Discriminator), + Func, Num, Weight); } - TypeMap &getTypeSamples(const LineLocation &Loc) { + TypeCountMap &getFunctionBodyTypeSamples(const LineLocation &Loc) { return BodySamples[Loc].getTypes(); } @@ -951,7 +956,7 @@ public: return &Iter->second; } - const TypeMap *findTypeSamplesAt(const LineLocation &Loc) const { + const TypeCountMap *findTypeSamplesAt(const LineLocation &Loc) const { auto Iter = VirtualCallsiteTypes.find(mapIRLocToProfileLoc(Loc)); if (Iter == VirtualCallsiteTypes.end()) return nullptr; @@ -1023,7 +1028,7 @@ public: return VirtualCallsiteTypes; } - TypeMap& getTypeSamplesAt(const LineLocation &Loc) { + TypeCountMap &getTypeSamplesAt(const LineLocation &Loc) { return VirtualCallsiteTypes[mapIRLocToProfileLoc(Loc)]; } @@ -1087,9 +1092,9 @@ public: mergeSampleProfErrors(Result, FSMap[Rec.first].merge(Rec.second, Weight)); } - for (const auto &[Loc, TypeCountMap] : Other.getCallsiteTypes()) { - TypeMap &TypeCounts = getTypeSamplesAt(Loc); - for (const auto &[Type, Count] : TypeCountMap) { + for (const auto &[Loc, TypeMap] : Other.getCallsiteTypes()) { + TypeCountMap &TypeCounts = getTypeSamplesAt(Loc); + for (const auto &[Type, Count] : TypeMap) { TypeCounts[Type] = SaturatingMultiplyAdd(Count, Weight, TypeCounts[Type]); } diff --git a/llvm/include/llvm/ProfileData/SampleProfReader.h b/llvm/include/llvm/ProfileData/SampleProfReader.h index 6ab3b65..bb6a577 100644 --- a/llvm/include/llvm/ProfileData/SampleProfReader.h +++ b/llvm/include/llvm/ProfileData/SampleProfReader.h @@ -872,7 +872,8 @@ private: std::error_code readCallsiteVTableProf(FunctionSamples &FProfile) override; - std::error_code readTypeMap(TypeMap& M); + std::error_code readTypeMap(TypeCountMap &M); + public: SampleProfileReaderExtBinary(std::unique_ptr<MemoryBuffer> B, LLVMContext &C, SampleProfileFormat Format = SPF_Ext_Binary) diff --git a/llvm/include/llvm/ProfileData/SampleProfWriter.h b/llvm/include/llvm/ProfileData/SampleProfWriter.h index fe3121d..e443196 100644 --- a/llvm/include/llvm/ProfileData/SampleProfWriter.h +++ b/llvm/include/llvm/ProfileData/SampleProfWriter.h @@ -219,7 +219,7 @@ protected: writeCallsiteType(const FunctionSamples &FunctionSample, raw_ostream &OS) { return sampleprof_error::success; } - virtual void addTypeNames(const TypeMap &M) {} + virtual void addTypeNames(const TypeCountMap &M) {} std::error_code writeHeader(const SampleProfileMap &ProfileMap) override; std::error_code writeSummary(); virtual std::error_code writeContextIdx(const SampleContext &Context); @@ -430,7 +430,9 @@ protected: std::error_code writeCallsiteType(const FunctionSamples &FunctionSample, raw_ostream &OS) override; - void addTypeNames(const TypeMap &M) override { + /// Add the type names to NameTable. + /// TODO: Use map_range to map keys only. + void addTypeNames(const TypeCountMap &M) override { if (!WriteVTableProf) return; // Add type name to TypeNameTable. @@ -454,7 +456,7 @@ private: "Unsupported layout"); } - std::error_code writeTypeMap(const TypeMap &Map, raw_ostream& OS); + std::error_code writeTypeMap(const TypeCountMap &Map, raw_ostream &OS); // TODO:This should be configurable by flag. bool WriteVTableProf = false; diff --git a/llvm/lib/ProfileData/SampleProfReader.cpp b/llvm/lib/ProfileData/SampleProfReader.cpp index 6b3766d..55771ad 100644 --- a/llvm/lib/ProfileData/SampleProfReader.cpp +++ b/llvm/lib/ProfileData/SampleProfReader.cpp @@ -448,7 +448,7 @@ std::error_code SampleProfileReaderText::readImpl() { } case LineType::CallSiteTypeProfile: { - TypeMap &Map = InlineStack.back()->getTypeSamplesAt( + auto &Map = InlineStack.back()->getTypeSamplesAt( LineLocation(LineOffset, Discriminator)); for (const auto [Type, Count] : TypeCountMap) Map[FunctionId(Type)] += Count; @@ -461,10 +461,10 @@ std::error_code SampleProfileReaderText::readImpl() { } FunctionSamples &FProfile = *InlineStack.back(); for (const auto &name_count : TypeCountMap) { - mergeSampleProfErrors( - Result, FProfile.addTypeSamples(LineOffset, Discriminator, - FunctionId(name_count.first), - name_count.second)); + mergeSampleProfErrors(Result, FProfile.addFunctionBodyTypeSamples( + LineOffset, Discriminator, + FunctionId(name_count.first), + name_count.second)); } break; } @@ -653,7 +653,7 @@ SampleProfileReaderBinary::readSampleContextFromTable() { return std::make_pair(Context, Hash); } -std::error_code SampleProfileReaderExtBinary::readTypeMap(TypeMap &M) { +std::error_code SampleProfileReaderExtBinary::readTypeMap(TypeCountMap &M) { auto NumVTableTypes = readNumber<uint32_t>(); if (std::error_code EC = NumVTableTypes.getError()) return EC; @@ -667,7 +667,6 @@ std::error_code SampleProfileReaderExtBinary::readTypeMap(TypeMap &M) { if (std::error_code EC = VTableSamples.getError()) return EC; - errs() << "readTypeMap\t" << *VTableType << "\t" << *VTableSamples << "\n"; M.insert(std::make_pair(*VTableType, *VTableSamples)); } return sampleprof_error::success; @@ -679,7 +678,7 @@ SampleProfileReaderExtBinary::readVTableProf(const LineLocation &Loc, if (!ReadVTableProf) return sampleprof_error::success; - return readTypeMap(FProfile.getTypeSamples(Loc)); + return readTypeMap(FProfile.getFunctionBodyTypeSamples(Loc)); } std::error_code SampleProfileReaderExtBinary::readCallsiteVTableProf( @@ -800,10 +799,7 @@ SampleProfileReaderBinary::readProfile(FunctionSamples &FProfile) { return EC; } - std::error_code EC = readCallsiteVTableProf(FProfile); - errs() << "readFunctionSample\t"; - FProfile.print(errs(), 2); - return EC; + return readCallsiteVTableProf(FProfile); } std::error_code @@ -866,7 +862,6 @@ std::error_code SampleProfileReaderExtBinaryBase::readOneSection( if (hasSecFlag(Entry, SecProfSummaryFlags::SecFlagFSDiscriminator)) FunctionSamples::ProfileIsFS = ProfileIsFS = true; if (hasSecFlag(Entry, SecProfSummaryFlags::SecFlagHasVTableTypeProf)) { - errs() << "SampleProfileReaderExtBinaryBase::readVTableProf\n"; ReadVTableProf = true; } break; diff --git a/llvm/lib/ProfileData/SampleProfWriter.cpp b/llvm/lib/ProfileData/SampleProfWriter.cpp index dde9173..2965d6c 100644 --- a/llvm/lib/ProfileData/SampleProfWriter.cpp +++ b/llvm/lib/ProfileData/SampleProfWriter.cpp @@ -636,7 +636,8 @@ std::error_code SampleProfileWriterText::writeSample(const FunctionSamples &S) { return EC; } - if (const TypeMap *Map = S.findTypeSamplesAt(Loc); Map && !Map->empty()) { + if (const TypeCountMap *Map = S.findTypeSamplesAt(Loc); + Map && !Map->empty()) { OS.indent(Indent); Loc.print(OS); OS << ": "; @@ -850,12 +851,11 @@ std::error_code SampleProfileWriterExtBinaryBase::writeHeader( return sampleprof_error::success; } -std::error_code SampleProfileWriterExtBinary::writeTypeMap(const TypeMap &Map, - raw_ostream &OS) { +std::error_code +SampleProfileWriterExtBinary::writeTypeMap(const TypeCountMap &Map, + raw_ostream &OS) { encodeULEB128(Map.size(), OS); for (const auto &[TypeName, TypeSamples] : Map) { - errs() << "TypeName: " << TypeName << "\t" << "TypeSamples: " << TypeSamples - << "\n"; if (std::error_code EC = writeNameIdx(TypeName)) return EC; encodeULEB128(TypeSamples, OS); diff --git a/llvm/test/tools/llvm-profdata/Inputs/profile-symbol-list-ext.expected b/llvm/test/tools/llvm-profdata/Inputs/profile-symbol-list-ext.expected new file mode 100644 index 0000000..a7912ea --- /dev/null +++ b/llvm/test/tools/llvm-profdata/Inputs/profile-symbol-list-ext.expected @@ -0,0 +1,43 @@ +Function: main: 368038, 0, 7 sampled lines +Samples collected in the function's body { + 4: 1068 + 4.2: 1068 + 5: 2150 + 5.1: 2150 + 6: 4160 + 7: 1068 + 9: 4128, calls: _Z3bari:2942 _Z3fooi:1262, types: vtable_bar:2942 vtable_foo:1260 +} +Samples collected in inlined callsites { + 10: inlined callee: inline1: 2000, 0, 1 sampled lines + Samples collected in the function's body { + 1: 2000 + } + No inlined callsites in this function + 10: inlined callee: inline2: 4000, 0, 1 sampled lines + Samples collected in the function's body { + 1: 4000 + } + No inlined callsites in this function + 10: vtables: inline1_vtable:2000 inline2_vtable:4000 +} +Function: _Z3bari: 40602, 2874, 1 sampled lines +Samples collected in the function's body { + 1: 2874 +} +No inlined callsites in this function +Function: _Z3fooi: 15422, 1220, 1 sampled lines +Samples collected in the function's body { + 1: 1220 +} +No inlined callsites in this function +======== Dump profile symbol list ======== +_Z3goov +_Z3sumii +__libc_csu_fini +__libc_csu_init +_dl_relocate_static_pie +_fini +_init +_start +main diff --git a/llvm/test/tools/llvm-profdata/Inputs/profile-symbol-list.expected b/llvm/test/tools/llvm-profdata/Inputs/profile-symbol-list.expected index a7912ea..bd528b4 100644 --- a/llvm/test/tools/llvm-profdata/Inputs/profile-symbol-list.expected +++ b/llvm/test/tools/llvm-profdata/Inputs/profile-symbol-list.expected @@ -6,7 +6,7 @@ Samples collected in the function's body { 5.1: 2150 6: 4160 7: 1068 - 9: 4128, calls: _Z3bari:2942 _Z3fooi:1262, types: vtable_bar:2942 vtable_foo:1260 + 9: 4128, calls: _Z3bari:2942 _Z3fooi:1262 } Samples collected in inlined callsites { 10: inlined callee: inline1: 2000, 0, 1 sampled lines @@ -19,7 +19,6 @@ Samples collected in inlined callsites { 1: 4000 } No inlined callsites in this function - 10: vtables: inline1_vtable:2000 inline2_vtable:4000 } Function: _Z3bari: 40602, 2874, 1 sampled lines Samples collected in the function's body { diff --git a/llvm/test/tools/llvm-profdata/Inputs/sample-profile-ext.proftext b/llvm/test/tools/llvm-profdata/Inputs/sample-profile-ext.proftext new file mode 100644 index 0000000..0a12512 --- /dev/null +++ b/llvm/test/tools/llvm-profdata/Inputs/sample-profile-ext.proftext @@ -0,0 +1,18 @@ +main:184019:0 + 4: 534 + 4.2: 534 + 5: 1075 + 5.1: 1075 + 6: 2080 + 7: 534 + 9: 2064 _Z3bari:1471 _Z3fooi:631 + 9: vtable_bar:1471 vtable_foo:630 // CallTargetVtables + 10: inline1:1000 + 1: 1000 + 10: inline2:2000 + 1: 2000 + 10: inline1_vtable:1000 inline2_vtable:2000 // CallSiteVtables +_Z3bari:20301:1437 + 1: 1437 +_Z3fooi:7711:610 + 1: 610 diff --git a/llvm/test/tools/llvm-profdata/Inputs/sample-profile.proftext b/llvm/test/tools/llvm-profdata/Inputs/sample-profile.proftext index 0a12512..f9f87df 100644 --- a/llvm/test/tools/llvm-profdata/Inputs/sample-profile.proftext +++ b/llvm/test/tools/llvm-profdata/Inputs/sample-profile.proftext @@ -6,12 +6,10 @@ main:184019:0 6: 2080 7: 534 9: 2064 _Z3bari:1471 _Z3fooi:631 - 9: vtable_bar:1471 vtable_foo:630 // CallTargetVtables 10: inline1:1000 1: 1000 10: inline2:2000 1: 2000 - 10: inline1_vtable:1000 inline2_vtable:2000 // CallSiteVtables _Z3bari:20301:1437 1: 1437 _Z3fooi:7711:610 diff --git a/llvm/test/tools/llvm-profdata/profile-symbol-list-compress.test b/llvm/test/tools/llvm-profdata/profile-symbol-list-compress.test index b445695..9a95a62 100644 --- a/llvm/test/tools/llvm-profdata/profile-symbol-list-compress.test +++ b/llvm/test/tools/llvm-profdata/profile-symbol-list-compress.test @@ -4,3 +4,9 @@ REQUIRES: zlib ; RUN: llvm-profdata merge -sample -extbinary -compress-all-sections %t.1.output %t.2.output -o %t.3.output ; RUN: llvm-profdata show -sample -show-prof-sym-list %t.3.output > %t.4.output ; RUN: diff -b %S/Inputs/profile-symbol-list.expected %t.4.output + +; RUN: llvm-profdata merge -sample -extbinary -compress-all-sections -prof-sym-list=%S/Inputs/profile-symbol-list-1.text %S/Inputs/sample-profile-ext.proftext -o %t.1.output +; RUN: llvm-profdata merge -sample -extbinary -compress-all-sections -prof-sym-list=%S/Inputs/profile-symbol-list-2.text %S/Inputs/sample-profile-ext.proftext -o %t.2.output +; RUN: llvm-profdata merge -sample -extbinary -compress-all-sections %t.1.output %t.2.output -o %t.3.output +; RUN: llvm-profdata show -sample -show-prof-sym-list %t.3.output > %t.4.output +; RUN: diff -b %S/Inputs/profile-symbol-list-ext.expected %t.4.output diff --git a/llvm/test/tools/llvm-profdata/profile-symbol-list.test b/llvm/test/tools/llvm-profdata/profile-symbol-list.test index 39dcd11..8c5f9d6 100644 --- a/llvm/test/tools/llvm-profdata/profile-symbol-list.test +++ b/llvm/test/tools/llvm-profdata/profile-symbol-list.test @@ -7,3 +7,9 @@ ; RUN: llvm-profdata show -sample -show-sec-info-only %t.5.output | FileCheck %s -check-prefix=NOSYMLIST ; NOSYMLIST: ProfileSymbolListSection {{.*}} Size: 0 + +; RUN: llvm-profdata merge -sample -extbinary -prof-sym-list=%S/Inputs/profile-symbol-list-1.text %S/Inputs/sample-profile-ext.proftext -o %t.1.output +; RUN: llvm-profdata merge -sample -extbinary -prof-sym-list=%S/Inputs/profile-symbol-list-2.text %S/Inputs/sample-profile-ext.proftext -o %t.2.output +; RUN: llvm-profdata merge -sample -extbinary %t.1.output %t.2.output -o %t.3.output +; RUN: llvm-profdata show -sample -show-prof-sym-list %t.3.output > %t.4.output +; RUN: diff -b %S/Inputs/profile-symbol-list-ext.expected %t.4.output diff --git a/llvm/test/tools/llvm-profdata/roundtrip.test b/llvm/test/tools/llvm-profdata/roundtrip.test index 3746ac1..0cf5183 100644 --- a/llvm/test/tools/llvm-profdata/roundtrip.test +++ b/llvm/test/tools/llvm-profdata/roundtrip.test @@ -6,13 +6,18 @@ RUN: llvm-profdata show -o %t.1.proftext -all-functions -text %t.1.profdata RUN: diff -b %t.1.proftext %S/Inputs/IR_profile.proftext RUN: llvm-profdata merge --sample --binary -output=%t.2.profdata %S/Inputs/sample-profile.proftext RUN: llvm-profdata merge --sample --text -output=%t.2.proftext %t.2.profdata -COM: diff -b %t.2.proftext %S/Inputs/sample-profile.proftext +RUN: diff -b %t.2.proftext %S/Inputs/sample-profile.proftext # Round trip from text --> extbinary --> text RUN: llvm-profdata merge --sample --extbinary -output=%t.3.profdata %S/Inputs/sample-profile.proftext RUN: llvm-profdata merge --sample --text -output=%t.3.proftext %t.3.profdata RUN: diff -b %t.3.proftext %S/Inputs/sample-profile.proftext # Round trip from text --> binary --> extbinary --> text -COM: llvm-profdata merge --sample --binary -output=%t.4.profdata %S/Inputs/sample-profile.proftext -COM: llvm-profdata merge --sample --extbinary -output=%t.5.profdata %t.4.profdata -COM: llvm-profdata merge --sample --text -output=%t.4.proftext %t.5.profdata -COM: diff -b %t.4.proftext %S/Inputs/sample-profile.proftext +RUN: llvm-profdata merge --sample --binary -output=%t.4.profdata %S/Inputs/sample-profile.proftext +RUN: llvm-profdata merge --sample --extbinary -output=%t.5.profdata %t.4.profdata +RUN: llvm-profdata merge --sample --text -output=%t.4.proftext %t.5.profdata +RUN: diff -b %t.4.proftext %S/Inputs/sample-profile.proftext +# Round trip from text --> extbinary --> text. +# The text profile is supported by ext-binary profile but not binary profile format. +RUN: llvm-profdata merge --sample --extbinary --output=%t.5.profdata %S/Inputs/sample-profile-ext.proftext +RUN: llvm-profdata merge --sample --text --output=%t.5.proftext %t.5.profdata +RUN: diff -b %t.5.proftext %S/Inputs/sample-profile-ext.proftext diff --git a/llvm/tools/llvm-profdata/llvm-profdata.cpp b/llvm/tools/llvm-profdata/llvm-profdata.cpp index 122b7e1..b19dd30 100644 --- a/llvm/tools/llvm-profdata/llvm-profdata.cpp +++ b/llvm/tools/llvm-profdata/llvm-profdata.cpp @@ -1630,13 +1630,9 @@ static void mergeSampleProfile(const WeightedFileVector &Inputs, Remapper ? remapSamples(I->second, *Remapper, Result) : FunctionSamples(); FunctionSamples &Samples = Remapper ? Remapped : I->second; - errs() << "llvm-profdata.cpp\tfunction samples:\t"; - Samples.print(errs(), 2); SampleContext FContext = Samples.getContext(); mergeSampleProfErrors(Result, ProfileMap[FContext].merge(Samples, Input.Weight)); - errs() << "llvm-profdata.cpp\tmerged samples:\t"; - ProfileMap[FContext].print(errs(), 2); if (Result != sampleprof_error::success) { std::error_code EC = make_error_code(Result); handleMergeWriterError(errorCodeToError(EC), Input.Filename, |