[llvm-branch-commits] [llvm] [Inline]Update value profile for non-call instructions (PR #83769)
minglotus-6 wrote: Close this. Going to update in https://github.com/llvm/llvm-project/pull/81442 https://github.com/llvm/llvm-project/pull/83769 ___ llvm-branch-commits mailing list llvm-branch-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits
[llvm-branch-commits] [llvm] [Inline]Update value profile for non-call instructions (PR #83769)
https://github.com/minglotus-6 closed https://github.com/llvm/llvm-project/pull/83769 ___ llvm-branch-commits mailing list llvm-branch-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits
[llvm-branch-commits] [llvm] [Inline]Update value profile for non-call instructions (PR #83769)
https://github.com/minglotus-6 edited https://github.com/llvm/llvm-project/pull/83769 ___ llvm-branch-commits mailing list llvm-branch-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits
[llvm-branch-commits] [llvm] [ThinLTO][Bitcode] Generate import type in bitcode (PR #87600)
https://github.com/minglotus-6 edited https://github.com/llvm/llvm-project/pull/87600 ___ llvm-branch-commits mailing list llvm-branch-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits
[llvm-branch-commits] [llvm] [ThinLTO][Bitcode] Generate import type in bitcode (PR #87600)
https://github.com/minglotus-6 edited https://github.com/llvm/llvm-project/pull/87600 ___ llvm-branch-commits mailing list llvm-branch-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits
[llvm-branch-commits] [llvm] [ThinLTO][Bitcode] Generate import type in bitcode (PR #87600)
https://github.com/minglotus-6 converted_to_draft https://github.com/llvm/llvm-project/pull/87600 ___ llvm-branch-commits mailing list llvm-branch-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits
[llvm-branch-commits] [llvm] [TypeProf][IndirectCallPromotion]Implement vtable-based transformation (PR #81442)
https://github.com/minglotus-6 edited https://github.com/llvm/llvm-project/pull/81442 ___ llvm-branch-commits mailing list llvm-branch-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits
[llvm-branch-commits] [llvm] [ThinLTO][Bitcode] Generate import type in bitcode (PR #87600)
@@ -216,7 +216,8 @@ void gatherImportedSummariesForModule( StringRef ModulePath, const DenseMap , const FunctionImporter::ImportMapTy , -std::map ); +std::map , +GVSummaryPtrSet ); minglotus-6 wrote: done. https://github.com/llvm/llvm-project/pull/87600 ___ llvm-branch-commits mailing list llvm-branch-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits
[llvm-branch-commits] [llvm] [ThinLTO][Bitcode] Generate import type in bitcode (PR #87600)
https://github.com/minglotus-6 updated https://github.com/llvm/llvm-project/pull/87600 >From 001a785f664e3a16e61d1e350ea060b829f1856c Mon Sep 17 00:00:00 2001 From: mingmingl Date: Mon, 13 May 2024 20:51:25 -0700 Subject: [PATCH 1/3] update this patch as the second one --- llvm/include/llvm/Bitcode/BitcodeWriter.h | 9 +++-- .../llvm/LTO/legacy/ThinLTOCodeGenerator.h| 5 ++- .../llvm/Transforms/IPO/FunctionImport.h | 3 +- llvm/lib/Bitcode/Writer/BitcodeWriter.cpp | 38 +++ llvm/lib/LTO/LTO.cpp | 7 +++- llvm/lib/LTO/ThinLTOCodeGenerator.cpp | 11 -- llvm/lib/Transforms/IPO/FunctionImport.cpp| 5 ++- .../ThinLTO/X86/import_callee_declaration.ll | 13 +++ llvm/tools/llvm-lto/llvm-lto.cpp | 5 ++- 9 files changed, 66 insertions(+), 30 deletions(-) diff --git a/llvm/include/llvm/Bitcode/BitcodeWriter.h b/llvm/include/llvm/Bitcode/BitcodeWriter.h index 248d33f4502ef..a343f0e057631 100644 --- a/llvm/include/llvm/Bitcode/BitcodeWriter.h +++ b/llvm/include/llvm/Bitcode/BitcodeWriter.h @@ -102,7 +102,8 @@ class raw_ostream; void writeIndex( const ModuleSummaryIndex *Index, -const std::map *ModuleToSummariesForIndex); +const std::map *ModuleToSummariesForIndex, +const GVSummaryPtrSet *DecSummaries); }; /// Write the specified module to the specified raw output stream. @@ -147,10 +148,12 @@ class raw_ostream; /// where it will be written in a new bitcode block. This is used when /// writing the combined index file for ThinLTO. When writing a subset of the /// index for a distributed backend, provide the \p ModuleToSummariesForIndex - /// map. + /// map. \p DecSummaries specifies the set of summaries for which the + /// corresponding value should be imported as a declaration (prototype). void writeIndexToFile(const ModuleSummaryIndex , raw_ostream , const std::map -*ModuleToSummariesForIndex = nullptr); +*ModuleToSummariesForIndex = nullptr, +const GVSummaryPtrSet *DecSummaries = nullptr); /// If EmbedBitcode is set, save a copy of the llvm IR as data in the /// __LLVM,__bitcode section (.llvmbc on non-MacOS). diff --git a/llvm/include/llvm/LTO/legacy/ThinLTOCodeGenerator.h b/llvm/include/llvm/LTO/legacy/ThinLTOCodeGenerator.h index c450acda82ad0..f1337e82485c9 100644 --- a/llvm/include/llvm/LTO/legacy/ThinLTOCodeGenerator.h +++ b/llvm/include/llvm/LTO/legacy/ThinLTOCodeGenerator.h @@ -271,12 +271,13 @@ class ThinLTOCodeGenerator { const lto::InputFile ); /** - * Compute the list of summaries needed for importing into module. + * Compute the list of summaries and the subset of declaration summaries + * needed for importing into module. */ void gatherImportedSummariesForModule( Module , ModuleSummaryIndex , std::map , - const lto::InputFile ); + GVSummaryPtrSet , const lto::InputFile ); /** * Perform internalization. Index is updated to reflect linkage changes. diff --git a/llvm/include/llvm/Transforms/IPO/FunctionImport.h b/llvm/include/llvm/Transforms/IPO/FunctionImport.h index 024bba8105b89..f8b98d5f81770 100644 --- a/llvm/include/llvm/Transforms/IPO/FunctionImport.h +++ b/llvm/include/llvm/Transforms/IPO/FunctionImport.h @@ -216,7 +216,8 @@ void gatherImportedSummariesForModule( StringRef ModulePath, const DenseMap , const FunctionImporter::ImportMapTy , -std::map ); +std::map , +GVSummaryPtrSet ); /// Emit into \p OutputFilename the files module \p ModulePath will import from. std::error_code EmitImportsFiles( diff --git a/llvm/lib/Bitcode/Writer/BitcodeWriter.cpp b/llvm/lib/Bitcode/Writer/BitcodeWriter.cpp index 6d01e3b4d8218..7b89424194f9b 100644 --- a/llvm/lib/Bitcode/Writer/BitcodeWriter.cpp +++ b/llvm/lib/Bitcode/Writer/BitcodeWriter.cpp @@ -428,6 +428,11 @@ class IndexBitcodeWriter : public BitcodeWriterBase { /// The combined index to write to bitcode. const ModuleSummaryIndex + /// When writing combined summaries, provides the set of global value + /// summaries for which the value (function, function alias, etc) should be + /// imported as a declaration. + const GVSummaryPtrSet *DecSummaries = nullptr; + /// When writing a subset of the index for distributed backends, client /// provides a map of modules to the corresponding GUIDs/summaries to write. const std::map *ModuleToSummariesForIndex; @@ -452,11 +457,16 @@ class IndexBitcodeWriter : public BitcodeWriterBase { /// Constructs a IndexBitcodeWriter object for the given combined index, /// writing to the provided \p Buffer. When writing a subset of the index /// for a distributed backend, provide a \p ModuleToSummariesForIndex map. + /// If provided, \p ModuleToDecSummaries specifies the set of summaries for + /// which the
[llvm-branch-commits] [llvm] [CallPromotionUtils]Implement conditional indirect call promotion with vtable-based comparison (PR #81378)
https://github.com/minglotus-6 edited https://github.com/llvm/llvm-project/pull/81378 ___ llvm-branch-commits mailing list llvm-branch-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits
[llvm-branch-commits] [llvm] [ThinLTO][BitcodeWriter] Write import type in bitcode (PR #87600)
https://github.com/minglotus-6 edited https://github.com/llvm/llvm-project/pull/87600 ___ llvm-branch-commits mailing list llvm-branch-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits
[llvm-branch-commits] [llvm] [ThinLTO]Write import type for distributed ThinLTO (PR #87600)
https://github.com/minglotus-6 edited https://github.com/llvm/llvm-project/pull/87600 ___ llvm-branch-commits mailing list llvm-branch-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits
[llvm-branch-commits] [llvm] [ThinLTO][BitcodeWriter] Write import type in per-module combined summary (PR #87600)
https://github.com/minglotus-6 edited https://github.com/llvm/llvm-project/pull/87600 ___ llvm-branch-commits mailing list llvm-branch-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits
[llvm-branch-commits] [llvm] [NFC][CallPromotionUtils]Extract a helper function versionCallSiteWithCond from versionCallSite (PR #81181)
https://github.com/minglotus-6 edited https://github.com/llvm/llvm-project/pull/81181 ___ llvm-branch-commits mailing list llvm-branch-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits
[llvm-branch-commits] [llvm] [ThinLTO] Generate import status in per-module combined summary (PR #88024)
https://github.com/minglotus-6 updated https://github.com/llvm/llvm-project/pull/88024 >From cfb63d775d43a28b560d938346f1dd4b2dddc765 Mon Sep 17 00:00:00 2001 From: mingmingl Date: Thu, 4 Apr 2024 11:54:17 -0700 Subject: [PATCH 1/9] function import changes --- llvm/include/llvm/IR/ModuleSummaryIndex.h | 24 .../llvm/Transforms/IPO/FunctionImport.h | 18 ++- llvm/lib/LTO/LTO.cpp | 13 +- llvm/lib/LTO/LTOBackend.cpp | 5 +- llvm/lib/LTO/ThinLTOCodeGenerator.cpp | 9 +- llvm/lib/Transforms/IPO/FunctionImport.cpp| 130 -- llvm/tools/llvm-link/llvm-link.cpp| 2 +- 7 files changed, 146 insertions(+), 55 deletions(-) diff --git a/llvm/include/llvm/IR/ModuleSummaryIndex.h b/llvm/include/llvm/IR/ModuleSummaryIndex.h index 286b51bda0e2..259fe56ce5f6 100644 --- a/llvm/include/llvm/IR/ModuleSummaryIndex.h +++ b/llvm/include/llvm/IR/ModuleSummaryIndex.h @@ -296,6 +296,30 @@ template <> struct DenseMapInfo { static unsigned getHashValue(ValueInfo I) { return (uintptr_t)I.getRef(); } }; +struct SummaryImportInfo { + enum class ImportType : uint8_t { +NotImported = 0, +Declaration = 1, +Definition = 2, + }; + unsigned Type : 3; + SummaryImportInfo() : Type(static_cast(ImportType::NotImported)) {} + SummaryImportInfo(ImportType Type) : Type(static_cast(Type)) {} + + // FIXME: delete the first two set* helper function. + void updateType(ImportType InputType) { +Type = std::max(Type, static_cast(InputType)); + } + + bool isDefinition() const { +return static_cast(Type) == ImportType::Definition; + } + + bool isDeclaration() const { +return static_cast(Type) == ImportType::Declaration; + } +}; + /// Summary of memprof callsite metadata. struct CallsiteInfo { // Actual callee function. diff --git a/llvm/include/llvm/Transforms/IPO/FunctionImport.h b/llvm/include/llvm/Transforms/IPO/FunctionImport.h index c4d19e8641ec..9adc0c31eed4 100644 --- a/llvm/include/llvm/Transforms/IPO/FunctionImport.h +++ b/llvm/include/llvm/Transforms/IPO/FunctionImport.h @@ -33,7 +33,14 @@ class FunctionImporter { public: /// Set of functions to import from a source module. Each entry is a set /// containing all the GUIDs of all functions to import for a source module. - using FunctionsToImportTy = std::unordered_set; + using FunctionsToImportTy = DenseMap; + + // FIXME: Remove this. + enum ImportStatus { +NotImported, +ImportDeclaration, +ImportDefinition, + }; /// The different reasons selectCallee will chose not to import a /// candidate. @@ -99,8 +106,10 @@ class FunctionImporter { /// index's module path string table). using ImportMapTy = DenseMap; - /// The set contains an entry for every global value the module exports. - using ExportSetTy = DenseSet; + /// The map contains an entry for every global value the module exports, the + /// key being the value info, and the value is the summary-based import info. + /// FIXME: Does this set need to be a map? + using ExportSetTy = DenseMap; /// A function of this type is used to load modules referenced by the index. using ModuleLoaderTy = @@ -211,7 +220,8 @@ void gatherImportedSummariesForModule( StringRef ModulePath, const DenseMap , const FunctionImporter::ImportMapTy , -std::map ); +std::map , +ModuleToGVSummaryPtrSet ); /// Emit into \p OutputFilename the files module \p ModulePath will import from. std::error_code EmitImportsFiles( diff --git a/llvm/lib/LTO/LTO.cpp b/llvm/lib/LTO/LTO.cpp index 53060df7f503..ace533fe28c9 100644 --- a/llvm/lib/LTO/LTO.cpp +++ b/llvm/lib/LTO/LTO.cpp @@ -159,7 +159,7 @@ void llvm::computeLTOCacheKey( std::vector ExportsGUID; ExportsGUID.reserve(ExportList.size()); for (const auto : ExportList) { -auto GUID = VI.getGUID(); +auto GUID = VI.first.getGUID(); ExportsGUID.push_back(GUID); } @@ -205,7 +205,7 @@ void llvm::computeLTOCacheKey( AddUint64(Entry.getFunctions().size()); for (auto : Entry.getFunctions()) - AddUint64(Fn); + AddUint64(Fn.first); } // Include the hash for the resolved ODR. @@ -277,7 +277,7 @@ void llvm::computeLTOCacheKey( for (const ImportModule : ImportModulesVector) for (auto : ImpM.getFunctions()) { GlobalValueSummary *S = - Index.findSummaryInModule(ImpF, ImpM.getIdentifier()); + Index.findSummaryInModule(ImpF.first, ImpM.getIdentifier()); AddUsedThings(S); // If this is an alias, we also care about any types/etc. that the aliasee // may reference. @@ -1389,15 +1389,18 @@ class lto::ThinBackendProc { llvm::StringRef ModulePath, const std::string ) { std::map ModuleToSummariesForIndex; +ModuleToGVSummaryPtrSet ModuleToDeclarationSummaries; std::error_code EC; gatherImportedSummariesForModule(ModulePath, ModuleToDefinedGVSummaries, -
[llvm-branch-commits] [llvm] [ThinLTO] Generate import status in per-module combined summary (PR #88024)
minglotus-6 wrote: https://github.com/llvm/llvm-project/pull/87600 is a functional change and the diffbase of this patch, and `llvm/test/ThinLTO/X86/import_callee_declaration.ll` should be a test case for both patches. In the [diffbase](https://github.com/llvm/llvm-project/pull/87600), bitcode writer takes maps as additional parameters to populate import status, and it's not straightforward to construct regression tests there without this patch. I wonder if I shall introduce `cl::list` in llvm-lto/llvm-lto2 (as a repeated arg) to specify `filename:GUID` to test the diffbase alone. https://github.com/llvm/llvm-project/pull/88024 ___ llvm-branch-commits mailing list llvm-branch-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits
[llvm-branch-commits] [llvm] [ThinLTO] Generate import status in per-module combined summary (PR #88024)
https://github.com/minglotus-6 edited https://github.com/llvm/llvm-project/pull/88024 ___ llvm-branch-commits mailing list llvm-branch-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits
[llvm-branch-commits] [llvm] [nfc][ThinLTO] Generate import status in per-module combined summary (PR #88024)
minglotus-6 wrote: Tagging @jvoung since I cannot add you into reviewer. https://github.com/llvm/llvm-project/pull/88024 ___ llvm-branch-commits mailing list llvm-branch-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits
[llvm-branch-commits] [llvm] [nfc][ThinLTO] Generate import status in per-module combined summary (PR #88024)
https://github.com/minglotus-6 ready_for_review https://github.com/llvm/llvm-project/pull/88024 ___ llvm-branch-commits mailing list llvm-branch-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits
[llvm-branch-commits] [llvm] [nfc][ThinLTO] Generate import status in per-module combined summary (PR #88024)
https://github.com/minglotus-6 edited https://github.com/llvm/llvm-project/pull/88024 ___ llvm-branch-commits mailing list llvm-branch-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits
[llvm-branch-commits] [llvm] [nfc][ThinLTO] Generate import status in per-module combined summary (PR #88024)
https://github.com/minglotus-6 updated https://github.com/llvm/llvm-project/pull/88024 >From cfb63d775d43a28b560d938346f1dd4b2dddc765 Mon Sep 17 00:00:00 2001 From: mingmingl Date: Thu, 4 Apr 2024 11:54:17 -0700 Subject: [PATCH 1/8] function import changes --- llvm/include/llvm/IR/ModuleSummaryIndex.h | 24 .../llvm/Transforms/IPO/FunctionImport.h | 18 ++- llvm/lib/LTO/LTO.cpp | 13 +- llvm/lib/LTO/LTOBackend.cpp | 5 +- llvm/lib/LTO/ThinLTOCodeGenerator.cpp | 9 +- llvm/lib/Transforms/IPO/FunctionImport.cpp| 130 -- llvm/tools/llvm-link/llvm-link.cpp| 2 +- 7 files changed, 146 insertions(+), 55 deletions(-) diff --git a/llvm/include/llvm/IR/ModuleSummaryIndex.h b/llvm/include/llvm/IR/ModuleSummaryIndex.h index 286b51bda0e2c1..259fe56ce5f63e 100644 --- a/llvm/include/llvm/IR/ModuleSummaryIndex.h +++ b/llvm/include/llvm/IR/ModuleSummaryIndex.h @@ -296,6 +296,30 @@ template <> struct DenseMapInfo { static unsigned getHashValue(ValueInfo I) { return (uintptr_t)I.getRef(); } }; +struct SummaryImportInfo { + enum class ImportType : uint8_t { +NotImported = 0, +Declaration = 1, +Definition = 2, + }; + unsigned Type : 3; + SummaryImportInfo() : Type(static_cast(ImportType::NotImported)) {} + SummaryImportInfo(ImportType Type) : Type(static_cast(Type)) {} + + // FIXME: delete the first two set* helper function. + void updateType(ImportType InputType) { +Type = std::max(Type, static_cast(InputType)); + } + + bool isDefinition() const { +return static_cast(Type) == ImportType::Definition; + } + + bool isDeclaration() const { +return static_cast(Type) == ImportType::Declaration; + } +}; + /// Summary of memprof callsite metadata. struct CallsiteInfo { // Actual callee function. diff --git a/llvm/include/llvm/Transforms/IPO/FunctionImport.h b/llvm/include/llvm/Transforms/IPO/FunctionImport.h index c4d19e8641eca2..9adc0c31eed439 100644 --- a/llvm/include/llvm/Transforms/IPO/FunctionImport.h +++ b/llvm/include/llvm/Transforms/IPO/FunctionImport.h @@ -33,7 +33,14 @@ class FunctionImporter { public: /// Set of functions to import from a source module. Each entry is a set /// containing all the GUIDs of all functions to import for a source module. - using FunctionsToImportTy = std::unordered_set; + using FunctionsToImportTy = DenseMap; + + // FIXME: Remove this. + enum ImportStatus { +NotImported, +ImportDeclaration, +ImportDefinition, + }; /// The different reasons selectCallee will chose not to import a /// candidate. @@ -99,8 +106,10 @@ class FunctionImporter { /// index's module path string table). using ImportMapTy = DenseMap; - /// The set contains an entry for every global value the module exports. - using ExportSetTy = DenseSet; + /// The map contains an entry for every global value the module exports, the + /// key being the value info, and the value is the summary-based import info. + /// FIXME: Does this set need to be a map? + using ExportSetTy = DenseMap; /// A function of this type is used to load modules referenced by the index. using ModuleLoaderTy = @@ -211,7 +220,8 @@ void gatherImportedSummariesForModule( StringRef ModulePath, const DenseMap , const FunctionImporter::ImportMapTy , -std::map ); +std::map , +ModuleToGVSummaryPtrSet ); /// Emit into \p OutputFilename the files module \p ModulePath will import from. std::error_code EmitImportsFiles( diff --git a/llvm/lib/LTO/LTO.cpp b/llvm/lib/LTO/LTO.cpp index 53060df7f503e0..ace533fe28c92f 100644 --- a/llvm/lib/LTO/LTO.cpp +++ b/llvm/lib/LTO/LTO.cpp @@ -159,7 +159,7 @@ void llvm::computeLTOCacheKey( std::vector ExportsGUID; ExportsGUID.reserve(ExportList.size()); for (const auto : ExportList) { -auto GUID = VI.getGUID(); +auto GUID = VI.first.getGUID(); ExportsGUID.push_back(GUID); } @@ -205,7 +205,7 @@ void llvm::computeLTOCacheKey( AddUint64(Entry.getFunctions().size()); for (auto : Entry.getFunctions()) - AddUint64(Fn); + AddUint64(Fn.first); } // Include the hash for the resolved ODR. @@ -277,7 +277,7 @@ void llvm::computeLTOCacheKey( for (const ImportModule : ImportModulesVector) for (auto : ImpM.getFunctions()) { GlobalValueSummary *S = - Index.findSummaryInModule(ImpF, ImpM.getIdentifier()); + Index.findSummaryInModule(ImpF.first, ImpM.getIdentifier()); AddUsedThings(S); // If this is an alias, we also care about any types/etc. that the aliasee // may reference. @@ -1389,15 +1389,18 @@ class lto::ThinBackendProc { llvm::StringRef ModulePath, const std::string ) { std::map ModuleToSummariesForIndex; +ModuleToGVSummaryPtrSet ModuleToDeclarationSummaries; std::error_code EC; gatherImportedSummariesForModule(ModulePath,
[llvm-branch-commits] [llvm] [nfc][ThinLTO] Generate import status in per-module combined summary (PR #88024)
https://github.com/minglotus-6 updated https://github.com/llvm/llvm-project/pull/88024 >From cfb63d775d43a28b560d938346f1dd4b2dddc765 Mon Sep 17 00:00:00 2001 From: mingmingl Date: Thu, 4 Apr 2024 11:54:17 -0700 Subject: [PATCH 1/7] function import changes --- llvm/include/llvm/IR/ModuleSummaryIndex.h | 24 .../llvm/Transforms/IPO/FunctionImport.h | 18 ++- llvm/lib/LTO/LTO.cpp | 13 +- llvm/lib/LTO/LTOBackend.cpp | 5 +- llvm/lib/LTO/ThinLTOCodeGenerator.cpp | 9 +- llvm/lib/Transforms/IPO/FunctionImport.cpp| 130 -- llvm/tools/llvm-link/llvm-link.cpp| 2 +- 7 files changed, 146 insertions(+), 55 deletions(-) diff --git a/llvm/include/llvm/IR/ModuleSummaryIndex.h b/llvm/include/llvm/IR/ModuleSummaryIndex.h index 286b51bda0e2c1..259fe56ce5f63e 100644 --- a/llvm/include/llvm/IR/ModuleSummaryIndex.h +++ b/llvm/include/llvm/IR/ModuleSummaryIndex.h @@ -296,6 +296,30 @@ template <> struct DenseMapInfo { static unsigned getHashValue(ValueInfo I) { return (uintptr_t)I.getRef(); } }; +struct SummaryImportInfo { + enum class ImportType : uint8_t { +NotImported = 0, +Declaration = 1, +Definition = 2, + }; + unsigned Type : 3; + SummaryImportInfo() : Type(static_cast(ImportType::NotImported)) {} + SummaryImportInfo(ImportType Type) : Type(static_cast(Type)) {} + + // FIXME: delete the first two set* helper function. + void updateType(ImportType InputType) { +Type = std::max(Type, static_cast(InputType)); + } + + bool isDefinition() const { +return static_cast(Type) == ImportType::Definition; + } + + bool isDeclaration() const { +return static_cast(Type) == ImportType::Declaration; + } +}; + /// Summary of memprof callsite metadata. struct CallsiteInfo { // Actual callee function. diff --git a/llvm/include/llvm/Transforms/IPO/FunctionImport.h b/llvm/include/llvm/Transforms/IPO/FunctionImport.h index c4d19e8641eca2..9adc0c31eed439 100644 --- a/llvm/include/llvm/Transforms/IPO/FunctionImport.h +++ b/llvm/include/llvm/Transforms/IPO/FunctionImport.h @@ -33,7 +33,14 @@ class FunctionImporter { public: /// Set of functions to import from a source module. Each entry is a set /// containing all the GUIDs of all functions to import for a source module. - using FunctionsToImportTy = std::unordered_set; + using FunctionsToImportTy = DenseMap; + + // FIXME: Remove this. + enum ImportStatus { +NotImported, +ImportDeclaration, +ImportDefinition, + }; /// The different reasons selectCallee will chose not to import a /// candidate. @@ -99,8 +106,10 @@ class FunctionImporter { /// index's module path string table). using ImportMapTy = DenseMap; - /// The set contains an entry for every global value the module exports. - using ExportSetTy = DenseSet; + /// The map contains an entry for every global value the module exports, the + /// key being the value info, and the value is the summary-based import info. + /// FIXME: Does this set need to be a map? + using ExportSetTy = DenseMap; /// A function of this type is used to load modules referenced by the index. using ModuleLoaderTy = @@ -211,7 +220,8 @@ void gatherImportedSummariesForModule( StringRef ModulePath, const DenseMap , const FunctionImporter::ImportMapTy , -std::map ); +std::map , +ModuleToGVSummaryPtrSet ); /// Emit into \p OutputFilename the files module \p ModulePath will import from. std::error_code EmitImportsFiles( diff --git a/llvm/lib/LTO/LTO.cpp b/llvm/lib/LTO/LTO.cpp index 53060df7f503e0..ace533fe28c92f 100644 --- a/llvm/lib/LTO/LTO.cpp +++ b/llvm/lib/LTO/LTO.cpp @@ -159,7 +159,7 @@ void llvm::computeLTOCacheKey( std::vector ExportsGUID; ExportsGUID.reserve(ExportList.size()); for (const auto : ExportList) { -auto GUID = VI.getGUID(); +auto GUID = VI.first.getGUID(); ExportsGUID.push_back(GUID); } @@ -205,7 +205,7 @@ void llvm::computeLTOCacheKey( AddUint64(Entry.getFunctions().size()); for (auto : Entry.getFunctions()) - AddUint64(Fn); + AddUint64(Fn.first); } // Include the hash for the resolved ODR. @@ -277,7 +277,7 @@ void llvm::computeLTOCacheKey( for (const ImportModule : ImportModulesVector) for (auto : ImpM.getFunctions()) { GlobalValueSummary *S = - Index.findSummaryInModule(ImpF, ImpM.getIdentifier()); + Index.findSummaryInModule(ImpF.first, ImpM.getIdentifier()); AddUsedThings(S); // If this is an alias, we also care about any types/etc. that the aliasee // may reference. @@ -1389,15 +1389,18 @@ class lto::ThinBackendProc { llvm::StringRef ModulePath, const std::string ) { std::map ModuleToSummariesForIndex; +ModuleToGVSummaryPtrSet ModuleToDeclarationSummaries; std::error_code EC; gatherImportedSummariesForModule(ModulePath,
[llvm-branch-commits] [llvm] [nfc][ThinLTO] Generate import status in per-module combined summary (PR #88024)
https://github.com/minglotus-6 updated https://github.com/llvm/llvm-project/pull/88024 >From cfb63d775d43a28b560d938346f1dd4b2dddc765 Mon Sep 17 00:00:00 2001 From: mingmingl Date: Thu, 4 Apr 2024 11:54:17 -0700 Subject: [PATCH 1/5] function import changes --- llvm/include/llvm/IR/ModuleSummaryIndex.h | 24 .../llvm/Transforms/IPO/FunctionImport.h | 18 ++- llvm/lib/LTO/LTO.cpp | 13 +- llvm/lib/LTO/LTOBackend.cpp | 5 +- llvm/lib/LTO/ThinLTOCodeGenerator.cpp | 9 +- llvm/lib/Transforms/IPO/FunctionImport.cpp| 130 -- llvm/tools/llvm-link/llvm-link.cpp| 2 +- 7 files changed, 146 insertions(+), 55 deletions(-) diff --git a/llvm/include/llvm/IR/ModuleSummaryIndex.h b/llvm/include/llvm/IR/ModuleSummaryIndex.h index 286b51bda0e2c1..259fe56ce5f63e 100644 --- a/llvm/include/llvm/IR/ModuleSummaryIndex.h +++ b/llvm/include/llvm/IR/ModuleSummaryIndex.h @@ -296,6 +296,30 @@ template <> struct DenseMapInfo { static unsigned getHashValue(ValueInfo I) { return (uintptr_t)I.getRef(); } }; +struct SummaryImportInfo { + enum class ImportType : uint8_t { +NotImported = 0, +Declaration = 1, +Definition = 2, + }; + unsigned Type : 3; + SummaryImportInfo() : Type(static_cast(ImportType::NotImported)) {} + SummaryImportInfo(ImportType Type) : Type(static_cast(Type)) {} + + // FIXME: delete the first two set* helper function. + void updateType(ImportType InputType) { +Type = std::max(Type, static_cast(InputType)); + } + + bool isDefinition() const { +return static_cast(Type) == ImportType::Definition; + } + + bool isDeclaration() const { +return static_cast(Type) == ImportType::Declaration; + } +}; + /// Summary of memprof callsite metadata. struct CallsiteInfo { // Actual callee function. diff --git a/llvm/include/llvm/Transforms/IPO/FunctionImport.h b/llvm/include/llvm/Transforms/IPO/FunctionImport.h index c4d19e8641eca2..9adc0c31eed439 100644 --- a/llvm/include/llvm/Transforms/IPO/FunctionImport.h +++ b/llvm/include/llvm/Transforms/IPO/FunctionImport.h @@ -33,7 +33,14 @@ class FunctionImporter { public: /// Set of functions to import from a source module. Each entry is a set /// containing all the GUIDs of all functions to import for a source module. - using FunctionsToImportTy = std::unordered_set; + using FunctionsToImportTy = DenseMap; + + // FIXME: Remove this. + enum ImportStatus { +NotImported, +ImportDeclaration, +ImportDefinition, + }; /// The different reasons selectCallee will chose not to import a /// candidate. @@ -99,8 +106,10 @@ class FunctionImporter { /// index's module path string table). using ImportMapTy = DenseMap; - /// The set contains an entry for every global value the module exports. - using ExportSetTy = DenseSet; + /// The map contains an entry for every global value the module exports, the + /// key being the value info, and the value is the summary-based import info. + /// FIXME: Does this set need to be a map? + using ExportSetTy = DenseMap; /// A function of this type is used to load modules referenced by the index. using ModuleLoaderTy = @@ -211,7 +220,8 @@ void gatherImportedSummariesForModule( StringRef ModulePath, const DenseMap , const FunctionImporter::ImportMapTy , -std::map ); +std::map , +ModuleToGVSummaryPtrSet ); /// Emit into \p OutputFilename the files module \p ModulePath will import from. std::error_code EmitImportsFiles( diff --git a/llvm/lib/LTO/LTO.cpp b/llvm/lib/LTO/LTO.cpp index 53060df7f503e0..ace533fe28c92f 100644 --- a/llvm/lib/LTO/LTO.cpp +++ b/llvm/lib/LTO/LTO.cpp @@ -159,7 +159,7 @@ void llvm::computeLTOCacheKey( std::vector ExportsGUID; ExportsGUID.reserve(ExportList.size()); for (const auto : ExportList) { -auto GUID = VI.getGUID(); +auto GUID = VI.first.getGUID(); ExportsGUID.push_back(GUID); } @@ -205,7 +205,7 @@ void llvm::computeLTOCacheKey( AddUint64(Entry.getFunctions().size()); for (auto : Entry.getFunctions()) - AddUint64(Fn); + AddUint64(Fn.first); } // Include the hash for the resolved ODR. @@ -277,7 +277,7 @@ void llvm::computeLTOCacheKey( for (const ImportModule : ImportModulesVector) for (auto : ImpM.getFunctions()) { GlobalValueSummary *S = - Index.findSummaryInModule(ImpF, ImpM.getIdentifier()); + Index.findSummaryInModule(ImpF.first, ImpM.getIdentifier()); AddUsedThings(S); // If this is an alias, we also care about any types/etc. that the aliasee // may reference. @@ -1389,15 +1389,18 @@ class lto::ThinBackendProc { llvm::StringRef ModulePath, const std::string ) { std::map ModuleToSummariesForIndex; +ModuleToGVSummaryPtrSet ModuleToDeclarationSummaries; std::error_code EC; gatherImportedSummariesForModule(ModulePath,
[llvm-branch-commits] [llvm] [nfc][ThinLTO] Generate import status in per-module combined summary (PR #88024)
https://github.com/minglotus-6 updated https://github.com/llvm/llvm-project/pull/88024 >From cfb63d775d43a28b560d938346f1dd4b2dddc765 Mon Sep 17 00:00:00 2001 From: mingmingl Date: Thu, 4 Apr 2024 11:54:17 -0700 Subject: [PATCH 1/4] function import changes --- llvm/include/llvm/IR/ModuleSummaryIndex.h | 24 .../llvm/Transforms/IPO/FunctionImport.h | 18 ++- llvm/lib/LTO/LTO.cpp | 13 +- llvm/lib/LTO/LTOBackend.cpp | 5 +- llvm/lib/LTO/ThinLTOCodeGenerator.cpp | 9 +- llvm/lib/Transforms/IPO/FunctionImport.cpp| 130 -- llvm/tools/llvm-link/llvm-link.cpp| 2 +- 7 files changed, 146 insertions(+), 55 deletions(-) diff --git a/llvm/include/llvm/IR/ModuleSummaryIndex.h b/llvm/include/llvm/IR/ModuleSummaryIndex.h index 286b51bda0e2c1..259fe56ce5f63e 100644 --- a/llvm/include/llvm/IR/ModuleSummaryIndex.h +++ b/llvm/include/llvm/IR/ModuleSummaryIndex.h @@ -296,6 +296,30 @@ template <> struct DenseMapInfo { static unsigned getHashValue(ValueInfo I) { return (uintptr_t)I.getRef(); } }; +struct SummaryImportInfo { + enum class ImportType : uint8_t { +NotImported = 0, +Declaration = 1, +Definition = 2, + }; + unsigned Type : 3; + SummaryImportInfo() : Type(static_cast(ImportType::NotImported)) {} + SummaryImportInfo(ImportType Type) : Type(static_cast(Type)) {} + + // FIXME: delete the first two set* helper function. + void updateType(ImportType InputType) { +Type = std::max(Type, static_cast(InputType)); + } + + bool isDefinition() const { +return static_cast(Type) == ImportType::Definition; + } + + bool isDeclaration() const { +return static_cast(Type) == ImportType::Declaration; + } +}; + /// Summary of memprof callsite metadata. struct CallsiteInfo { // Actual callee function. diff --git a/llvm/include/llvm/Transforms/IPO/FunctionImport.h b/llvm/include/llvm/Transforms/IPO/FunctionImport.h index c4d19e8641eca2..9adc0c31eed439 100644 --- a/llvm/include/llvm/Transforms/IPO/FunctionImport.h +++ b/llvm/include/llvm/Transforms/IPO/FunctionImport.h @@ -33,7 +33,14 @@ class FunctionImporter { public: /// Set of functions to import from a source module. Each entry is a set /// containing all the GUIDs of all functions to import for a source module. - using FunctionsToImportTy = std::unordered_set; + using FunctionsToImportTy = DenseMap; + + // FIXME: Remove this. + enum ImportStatus { +NotImported, +ImportDeclaration, +ImportDefinition, + }; /// The different reasons selectCallee will chose not to import a /// candidate. @@ -99,8 +106,10 @@ class FunctionImporter { /// index's module path string table). using ImportMapTy = DenseMap; - /// The set contains an entry for every global value the module exports. - using ExportSetTy = DenseSet; + /// The map contains an entry for every global value the module exports, the + /// key being the value info, and the value is the summary-based import info. + /// FIXME: Does this set need to be a map? + using ExportSetTy = DenseMap; /// A function of this type is used to load modules referenced by the index. using ModuleLoaderTy = @@ -211,7 +220,8 @@ void gatherImportedSummariesForModule( StringRef ModulePath, const DenseMap , const FunctionImporter::ImportMapTy , -std::map ); +std::map , +ModuleToGVSummaryPtrSet ); /// Emit into \p OutputFilename the files module \p ModulePath will import from. std::error_code EmitImportsFiles( diff --git a/llvm/lib/LTO/LTO.cpp b/llvm/lib/LTO/LTO.cpp index 53060df7f503e0..ace533fe28c92f 100644 --- a/llvm/lib/LTO/LTO.cpp +++ b/llvm/lib/LTO/LTO.cpp @@ -159,7 +159,7 @@ void llvm::computeLTOCacheKey( std::vector ExportsGUID; ExportsGUID.reserve(ExportList.size()); for (const auto : ExportList) { -auto GUID = VI.getGUID(); +auto GUID = VI.first.getGUID(); ExportsGUID.push_back(GUID); } @@ -205,7 +205,7 @@ void llvm::computeLTOCacheKey( AddUint64(Entry.getFunctions().size()); for (auto : Entry.getFunctions()) - AddUint64(Fn); + AddUint64(Fn.first); } // Include the hash for the resolved ODR. @@ -277,7 +277,7 @@ void llvm::computeLTOCacheKey( for (const ImportModule : ImportModulesVector) for (auto : ImpM.getFunctions()) { GlobalValueSummary *S = - Index.findSummaryInModule(ImpF, ImpM.getIdentifier()); + Index.findSummaryInModule(ImpF.first, ImpM.getIdentifier()); AddUsedThings(S); // If this is an alias, we also care about any types/etc. that the aliasee // may reference. @@ -1389,15 +1389,18 @@ class lto::ThinBackendProc { llvm::StringRef ModulePath, const std::string ) { std::map ModuleToSummariesForIndex; +ModuleToGVSummaryPtrSet ModuleToDeclarationSummaries; std::error_code EC; gatherImportedSummariesForModule(ModulePath,
[llvm-branch-commits] [llvm] [nfc][ThinLTO] Generate import status in per-module combined summary (PR #88024)
https://github.com/minglotus-6 updated https://github.com/llvm/llvm-project/pull/88024 >From cfb63d775d43a28b560d938346f1dd4b2dddc765 Mon Sep 17 00:00:00 2001 From: mingmingl Date: Thu, 4 Apr 2024 11:54:17 -0700 Subject: [PATCH 1/3] function import changes --- llvm/include/llvm/IR/ModuleSummaryIndex.h | 24 .../llvm/Transforms/IPO/FunctionImport.h | 18 ++- llvm/lib/LTO/LTO.cpp | 13 +- llvm/lib/LTO/LTOBackend.cpp | 5 +- llvm/lib/LTO/ThinLTOCodeGenerator.cpp | 9 +- llvm/lib/Transforms/IPO/FunctionImport.cpp| 130 -- llvm/tools/llvm-link/llvm-link.cpp| 2 +- 7 files changed, 146 insertions(+), 55 deletions(-) diff --git a/llvm/include/llvm/IR/ModuleSummaryIndex.h b/llvm/include/llvm/IR/ModuleSummaryIndex.h index 286b51bda0e2c1..259fe56ce5f63e 100644 --- a/llvm/include/llvm/IR/ModuleSummaryIndex.h +++ b/llvm/include/llvm/IR/ModuleSummaryIndex.h @@ -296,6 +296,30 @@ template <> struct DenseMapInfo { static unsigned getHashValue(ValueInfo I) { return (uintptr_t)I.getRef(); } }; +struct SummaryImportInfo { + enum class ImportType : uint8_t { +NotImported = 0, +Declaration = 1, +Definition = 2, + }; + unsigned Type : 3; + SummaryImportInfo() : Type(static_cast(ImportType::NotImported)) {} + SummaryImportInfo(ImportType Type) : Type(static_cast(Type)) {} + + // FIXME: delete the first two set* helper function. + void updateType(ImportType InputType) { +Type = std::max(Type, static_cast(InputType)); + } + + bool isDefinition() const { +return static_cast(Type) == ImportType::Definition; + } + + bool isDeclaration() const { +return static_cast(Type) == ImportType::Declaration; + } +}; + /// Summary of memprof callsite metadata. struct CallsiteInfo { // Actual callee function. diff --git a/llvm/include/llvm/Transforms/IPO/FunctionImport.h b/llvm/include/llvm/Transforms/IPO/FunctionImport.h index c4d19e8641eca2..9adc0c31eed439 100644 --- a/llvm/include/llvm/Transforms/IPO/FunctionImport.h +++ b/llvm/include/llvm/Transforms/IPO/FunctionImport.h @@ -33,7 +33,14 @@ class FunctionImporter { public: /// Set of functions to import from a source module. Each entry is a set /// containing all the GUIDs of all functions to import for a source module. - using FunctionsToImportTy = std::unordered_set; + using FunctionsToImportTy = DenseMap; + + // FIXME: Remove this. + enum ImportStatus { +NotImported, +ImportDeclaration, +ImportDefinition, + }; /// The different reasons selectCallee will chose not to import a /// candidate. @@ -99,8 +106,10 @@ class FunctionImporter { /// index's module path string table). using ImportMapTy = DenseMap; - /// The set contains an entry for every global value the module exports. - using ExportSetTy = DenseSet; + /// The map contains an entry for every global value the module exports, the + /// key being the value info, and the value is the summary-based import info. + /// FIXME: Does this set need to be a map? + using ExportSetTy = DenseMap; /// A function of this type is used to load modules referenced by the index. using ModuleLoaderTy = @@ -211,7 +220,8 @@ void gatherImportedSummariesForModule( StringRef ModulePath, const DenseMap , const FunctionImporter::ImportMapTy , -std::map ); +std::map , +ModuleToGVSummaryPtrSet ); /// Emit into \p OutputFilename the files module \p ModulePath will import from. std::error_code EmitImportsFiles( diff --git a/llvm/lib/LTO/LTO.cpp b/llvm/lib/LTO/LTO.cpp index 53060df7f503e0..ace533fe28c92f 100644 --- a/llvm/lib/LTO/LTO.cpp +++ b/llvm/lib/LTO/LTO.cpp @@ -159,7 +159,7 @@ void llvm::computeLTOCacheKey( std::vector ExportsGUID; ExportsGUID.reserve(ExportList.size()); for (const auto : ExportList) { -auto GUID = VI.getGUID(); +auto GUID = VI.first.getGUID(); ExportsGUID.push_back(GUID); } @@ -205,7 +205,7 @@ void llvm::computeLTOCacheKey( AddUint64(Entry.getFunctions().size()); for (auto : Entry.getFunctions()) - AddUint64(Fn); + AddUint64(Fn.first); } // Include the hash for the resolved ODR. @@ -277,7 +277,7 @@ void llvm::computeLTOCacheKey( for (const ImportModule : ImportModulesVector) for (auto : ImpM.getFunctions()) { GlobalValueSummary *S = - Index.findSummaryInModule(ImpF, ImpM.getIdentifier()); + Index.findSummaryInModule(ImpF.first, ImpM.getIdentifier()); AddUsedThings(S); // If this is an alias, we also care about any types/etc. that the aliasee // may reference. @@ -1389,15 +1389,18 @@ class lto::ThinBackendProc { llvm::StringRef ModulePath, const std::string ) { std::map ModuleToSummariesForIndex; +ModuleToGVSummaryPtrSet ModuleToDeclarationSummaries; std::error_code EC; gatherImportedSummariesForModule(ModulePath,
[llvm-branch-commits] [llvm] [nfc][ThinLTO] Generate import status in per-module combined summary (PR #88024)
https://github.com/minglotus-6 updated https://github.com/llvm/llvm-project/pull/88024 >From cfb63d775d43a28b560d938346f1dd4b2dddc765 Mon Sep 17 00:00:00 2001 From: mingmingl Date: Thu, 4 Apr 2024 11:54:17 -0700 Subject: [PATCH 1/2] function import changes --- llvm/include/llvm/IR/ModuleSummaryIndex.h | 24 .../llvm/Transforms/IPO/FunctionImport.h | 18 ++- llvm/lib/LTO/LTO.cpp | 13 +- llvm/lib/LTO/LTOBackend.cpp | 5 +- llvm/lib/LTO/ThinLTOCodeGenerator.cpp | 9 +- llvm/lib/Transforms/IPO/FunctionImport.cpp| 130 -- llvm/tools/llvm-link/llvm-link.cpp| 2 +- 7 files changed, 146 insertions(+), 55 deletions(-) diff --git a/llvm/include/llvm/IR/ModuleSummaryIndex.h b/llvm/include/llvm/IR/ModuleSummaryIndex.h index 286b51bda0e2c1..259fe56ce5f63e 100644 --- a/llvm/include/llvm/IR/ModuleSummaryIndex.h +++ b/llvm/include/llvm/IR/ModuleSummaryIndex.h @@ -296,6 +296,30 @@ template <> struct DenseMapInfo { static unsigned getHashValue(ValueInfo I) { return (uintptr_t)I.getRef(); } }; +struct SummaryImportInfo { + enum class ImportType : uint8_t { +NotImported = 0, +Declaration = 1, +Definition = 2, + }; + unsigned Type : 3; + SummaryImportInfo() : Type(static_cast(ImportType::NotImported)) {} + SummaryImportInfo(ImportType Type) : Type(static_cast(Type)) {} + + // FIXME: delete the first two set* helper function. + void updateType(ImportType InputType) { +Type = std::max(Type, static_cast(InputType)); + } + + bool isDefinition() const { +return static_cast(Type) == ImportType::Definition; + } + + bool isDeclaration() const { +return static_cast(Type) == ImportType::Declaration; + } +}; + /// Summary of memprof callsite metadata. struct CallsiteInfo { // Actual callee function. diff --git a/llvm/include/llvm/Transforms/IPO/FunctionImport.h b/llvm/include/llvm/Transforms/IPO/FunctionImport.h index c4d19e8641eca2..9adc0c31eed439 100644 --- a/llvm/include/llvm/Transforms/IPO/FunctionImport.h +++ b/llvm/include/llvm/Transforms/IPO/FunctionImport.h @@ -33,7 +33,14 @@ class FunctionImporter { public: /// Set of functions to import from a source module. Each entry is a set /// containing all the GUIDs of all functions to import for a source module. - using FunctionsToImportTy = std::unordered_set; + using FunctionsToImportTy = DenseMap; + + // FIXME: Remove this. + enum ImportStatus { +NotImported, +ImportDeclaration, +ImportDefinition, + }; /// The different reasons selectCallee will chose not to import a /// candidate. @@ -99,8 +106,10 @@ class FunctionImporter { /// index's module path string table). using ImportMapTy = DenseMap; - /// The set contains an entry for every global value the module exports. - using ExportSetTy = DenseSet; + /// The map contains an entry for every global value the module exports, the + /// key being the value info, and the value is the summary-based import info. + /// FIXME: Does this set need to be a map? + using ExportSetTy = DenseMap; /// A function of this type is used to load modules referenced by the index. using ModuleLoaderTy = @@ -211,7 +220,8 @@ void gatherImportedSummariesForModule( StringRef ModulePath, const DenseMap , const FunctionImporter::ImportMapTy , -std::map ); +std::map , +ModuleToGVSummaryPtrSet ); /// Emit into \p OutputFilename the files module \p ModulePath will import from. std::error_code EmitImportsFiles( diff --git a/llvm/lib/LTO/LTO.cpp b/llvm/lib/LTO/LTO.cpp index 53060df7f503e0..ace533fe28c92f 100644 --- a/llvm/lib/LTO/LTO.cpp +++ b/llvm/lib/LTO/LTO.cpp @@ -159,7 +159,7 @@ void llvm::computeLTOCacheKey( std::vector ExportsGUID; ExportsGUID.reserve(ExportList.size()); for (const auto : ExportList) { -auto GUID = VI.getGUID(); +auto GUID = VI.first.getGUID(); ExportsGUID.push_back(GUID); } @@ -205,7 +205,7 @@ void llvm::computeLTOCacheKey( AddUint64(Entry.getFunctions().size()); for (auto : Entry.getFunctions()) - AddUint64(Fn); + AddUint64(Fn.first); } // Include the hash for the resolved ODR. @@ -277,7 +277,7 @@ void llvm::computeLTOCacheKey( for (const ImportModule : ImportModulesVector) for (auto : ImpM.getFunctions()) { GlobalValueSummary *S = - Index.findSummaryInModule(ImpF, ImpM.getIdentifier()); + Index.findSummaryInModule(ImpF.first, ImpM.getIdentifier()); AddUsedThings(S); // If this is an alias, we also care about any types/etc. that the aliasee // may reference. @@ -1389,15 +1389,18 @@ class lto::ThinBackendProc { llvm::StringRef ModulePath, const std::string ) { std::map ModuleToSummariesForIndex; +ModuleToGVSummaryPtrSet ModuleToDeclarationSummaries; std::error_code EC; gatherImportedSummariesForModule(ModulePath,
[llvm-branch-commits] [compiler-rt] [llvm] [InstrProf] Add vtables with type metadata into symtab to look it up with GUID (PR #81051)
https://github.com/minglotus-6 updated https://github.com/llvm/llvm-project/pull/81051 error: too big or took too long to generate ___ llvm-branch-commits mailing list llvm-branch-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits
[llvm-branch-commits] [llvm] [InstrProf] Add vtables with type metadata into symtab to look it up with GUID (PR #81051)
https://github.com/minglotus-6 converted_to_draft https://github.com/llvm/llvm-project/pull/81051 ___ llvm-branch-commits mailing list llvm-branch-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits
[llvm-branch-commits] [NFC]Extract the heuristic to find vtable for an indirect call into a helper function (PR #81024)
https://github.com/minglotus-6 converted_to_draft https://github.com/llvm/llvm-project/pull/81024 ___ llvm-branch-commits mailing list llvm-branch-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits
[llvm-branch-commits] [clang] [compiler-rt] [flang] [libc] [libcxx] [llvm] [mlir] [NFC][IndirectCallProm] Refactor function-based conditional devirtualization and indirect call value profile update in
https://github.com/minglotus-6 updated https://github.com/llvm/llvm-project/pull/80762 error: too big or took too long to generate ___ llvm-branch-commits mailing list llvm-branch-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits
[llvm-branch-commits] [llvm] [NFC][IndirectCallProm] Refactor function-based conditional devirtualization and indirect call value profile update into one helper function (PR #80762)
https://github.com/minglotus-6 converted_to_draft https://github.com/llvm/llvm-project/pull/80762 ___ llvm-branch-commits mailing list llvm-branch-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits
[llvm-branch-commits] [llvm] [nfc][ThinLTO] Generate import status in per-module combined summary (PR #88024)
https://github.com/minglotus-6 created https://github.com/llvm/llvm-project/pull/88024 This is still working in progress. Need to update all callers of 'ComputeImportForModule' properly >From cfb63d775d43a28b560d938346f1dd4b2dddc765 Mon Sep 17 00:00:00 2001 From: mingmingl Date: Thu, 4 Apr 2024 11:54:17 -0700 Subject: [PATCH] function import changes --- llvm/include/llvm/IR/ModuleSummaryIndex.h | 24 .../llvm/Transforms/IPO/FunctionImport.h | 18 ++- llvm/lib/LTO/LTO.cpp | 13 +- llvm/lib/LTO/LTOBackend.cpp | 5 +- llvm/lib/LTO/ThinLTOCodeGenerator.cpp | 9 +- llvm/lib/Transforms/IPO/FunctionImport.cpp| 130 -- llvm/tools/llvm-link/llvm-link.cpp| 2 +- 7 files changed, 146 insertions(+), 55 deletions(-) diff --git a/llvm/include/llvm/IR/ModuleSummaryIndex.h b/llvm/include/llvm/IR/ModuleSummaryIndex.h index 286b51bda0e2c1..259fe56ce5f63e 100644 --- a/llvm/include/llvm/IR/ModuleSummaryIndex.h +++ b/llvm/include/llvm/IR/ModuleSummaryIndex.h @@ -296,6 +296,30 @@ template <> struct DenseMapInfo { static unsigned getHashValue(ValueInfo I) { return (uintptr_t)I.getRef(); } }; +struct SummaryImportInfo { + enum class ImportType : uint8_t { +NotImported = 0, +Declaration = 1, +Definition = 2, + }; + unsigned Type : 3; + SummaryImportInfo() : Type(static_cast(ImportType::NotImported)) {} + SummaryImportInfo(ImportType Type) : Type(static_cast(Type)) {} + + // FIXME: delete the first two set* helper function. + void updateType(ImportType InputType) { +Type = std::max(Type, static_cast(InputType)); + } + + bool isDefinition() const { +return static_cast(Type) == ImportType::Definition; + } + + bool isDeclaration() const { +return static_cast(Type) == ImportType::Declaration; + } +}; + /// Summary of memprof callsite metadata. struct CallsiteInfo { // Actual callee function. diff --git a/llvm/include/llvm/Transforms/IPO/FunctionImport.h b/llvm/include/llvm/Transforms/IPO/FunctionImport.h index c4d19e8641eca2..9adc0c31eed439 100644 --- a/llvm/include/llvm/Transforms/IPO/FunctionImport.h +++ b/llvm/include/llvm/Transforms/IPO/FunctionImport.h @@ -33,7 +33,14 @@ class FunctionImporter { public: /// Set of functions to import from a source module. Each entry is a set /// containing all the GUIDs of all functions to import for a source module. - using FunctionsToImportTy = std::unordered_set; + using FunctionsToImportTy = DenseMap; + + // FIXME: Remove this. + enum ImportStatus { +NotImported, +ImportDeclaration, +ImportDefinition, + }; /// The different reasons selectCallee will chose not to import a /// candidate. @@ -99,8 +106,10 @@ class FunctionImporter { /// index's module path string table). using ImportMapTy = DenseMap; - /// The set contains an entry for every global value the module exports. - using ExportSetTy = DenseSet; + /// The map contains an entry for every global value the module exports, the + /// key being the value info, and the value is the summary-based import info. + /// FIXME: Does this set need to be a map? + using ExportSetTy = DenseMap; /// A function of this type is used to load modules referenced by the index. using ModuleLoaderTy = @@ -211,7 +220,8 @@ void gatherImportedSummariesForModule( StringRef ModulePath, const DenseMap , const FunctionImporter::ImportMapTy , -std::map ); +std::map , +ModuleToGVSummaryPtrSet ); /// Emit into \p OutputFilename the files module \p ModulePath will import from. std::error_code EmitImportsFiles( diff --git a/llvm/lib/LTO/LTO.cpp b/llvm/lib/LTO/LTO.cpp index 53060df7f503e0..ace533fe28c92f 100644 --- a/llvm/lib/LTO/LTO.cpp +++ b/llvm/lib/LTO/LTO.cpp @@ -159,7 +159,7 @@ void llvm::computeLTOCacheKey( std::vector ExportsGUID; ExportsGUID.reserve(ExportList.size()); for (const auto : ExportList) { -auto GUID = VI.getGUID(); +auto GUID = VI.first.getGUID(); ExportsGUID.push_back(GUID); } @@ -205,7 +205,7 @@ void llvm::computeLTOCacheKey( AddUint64(Entry.getFunctions().size()); for (auto : Entry.getFunctions()) - AddUint64(Fn); + AddUint64(Fn.first); } // Include the hash for the resolved ODR. @@ -277,7 +277,7 @@ void llvm::computeLTOCacheKey( for (const ImportModule : ImportModulesVector) for (auto : ImpM.getFunctions()) { GlobalValueSummary *S = - Index.findSummaryInModule(ImpF, ImpM.getIdentifier()); + Index.findSummaryInModule(ImpF.first, ImpM.getIdentifier()); AddUsedThings(S); // If this is an alias, we also care about any types/etc. that the aliasee // may reference. @@ -1389,15 +1389,18 @@ class lto::ThinBackendProc { llvm::StringRef ModulePath, const std::string ) { std::map ModuleToSummariesForIndex; +ModuleToGVSummaryPtrSet
[llvm-branch-commits] [clang] [clang-tools-extra] [compiler-rt] [flang] [libc] [lld] [llvm] [mlir] [openmp] [ThinLTO][TypeProf] Implement vtable def import (PR #79381)
https://github.com/minglotus-6 updated https://github.com/llvm/llvm-project/pull/79381 error: too big or took too long to generate ___ llvm-branch-commits mailing list llvm-branch-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits
[llvm-branch-commits] [llvm] [Inline][PGO] After inline, update profile for invoke instruction in both cloned instruction in the caller and original callee (PR #83809)
minglotus-6 wrote: > The invoke instruction can have 3 different kinds of prof data > > 1. call count (if a direct call) > 2. VP profile data (if an indirect call) > 3. branch weights for landing pad. > 4. can coexist with 2) and does not need to be updated. Is there an existing > test coverage for type 1) update? Added test cases for 1 and 2 in the pre-commit [PR](https://github.com/llvm/llvm-project/pull/83780) so the diff in this one is clearer. As discussed offline, non-count prof data (i.e., representing taken or not taken branches, associated with `br` or `switchinst`, etc) doesn't need scaling so no extra work needed. PTAL. https://github.com/llvm/llvm-project/pull/83809 ___ llvm-branch-commits mailing list llvm-branch-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits
[llvm-branch-commits] [llvm] [Inline][PGO] After inline, update profile for invoke instruction in both cloned instruction in the caller and original callee (PR #83809)
https://github.com/minglotus-6 updated https://github.com/llvm/llvm-project/pull/83809 >From 9575b83ea40012ecbfbf301a24ec89de0726ffd4 Mon Sep 17 00:00:00 2001 From: mingmingl Date: Mon, 4 Mar 2024 00:43:55 -0800 Subject: [PATCH] update profile for invoke instruction in caller and callee after inline --- llvm/include/llvm/IR/Instructions.h | 3 + llvm/lib/IR/Instructions.cpp | 12 ++ llvm/lib/Transforms/Utils/InlineFunction.cpp | 11 +- .../Inline/update_invoke_value_profile.ll | 185 ++ 4 files changed, 209 insertions(+), 2 deletions(-) create mode 100644 llvm/test/Transforms/Inline/update_invoke_value_profile.ll diff --git a/llvm/include/llvm/IR/Instructions.h b/llvm/include/llvm/IR/Instructions.h index bc357074e5cb21..1146b3fa3ae244 100644 --- a/llvm/include/llvm/IR/Instructions.h +++ b/llvm/include/llvm/IR/Instructions.h @@ -4360,6 +4360,9 @@ class InvokeInst : public CallBase { unsigned getNumSuccessors() const { return 2; } + /// Updates profile metadata by scaling it by \p S / \p T. + void updateProfWeight(uint64_t S, uint64_t T); + // Methods for support type inquiry through isa, cast, and dyn_cast: static bool classof(const Instruction *I) { return (I->getOpcode() == Instruction::Invoke); diff --git a/llvm/lib/IR/Instructions.cpp b/llvm/lib/IR/Instructions.cpp index 9ae71acd523c36..920ce67f118991 100644 --- a/llvm/lib/IR/Instructions.cpp +++ b/llvm/lib/IR/Instructions.cpp @@ -918,6 +918,18 @@ LandingPadInst *InvokeInst::getLandingPadInst() const { return cast(getUnwindDest()->getFirstNonPHI()); } +void InvokeInst::updateProfWeight(uint64_t S, uint64_t T) { + if (T == 0) { +LLVM_DEBUG(dbgs() << "Attempting to update profile weights will result in " + "div by 0. Ignoring. Likely the function " + << getParent()->getParent()->getName() + << " has 0 entry count, and contains call instructions " + "with non-zero prof info."); +return; + } + scaleProfData(*this, S, T); +} + //===--===// //CallBrInst Implementation //===--===// diff --git a/llvm/lib/Transforms/Utils/InlineFunction.cpp b/llvm/lib/Transforms/Utils/InlineFunction.cpp index f68fdb26f28173..75b0d0669e9228 100644 --- a/llvm/lib/Transforms/Utils/InlineFunction.cpp +++ b/llvm/lib/Transforms/Utils/InlineFunction.cpp @@ -1909,10 +1909,14 @@ void llvm::updateProfileCallee( // During inlining ? if (VMap) { uint64_t CloneEntryCount = PriorEntryCount - NewEntryCount; -for (auto Entry : *VMap) +for (auto Entry : *VMap) { if (isa(Entry.first)) if (auto *CI = dyn_cast_or_null(Entry.second)) CI->updateProfWeight(CloneEntryCount, PriorEntryCount); + if (isa(Entry.first)) +if (auto *II = dyn_cast_or_null(Entry.second)) + II->updateProfWeight(CloneEntryCount, PriorEntryCount); +} } if (EntryDelta) { @@ -1921,9 +1925,12 @@ void llvm::updateProfileCallee( for (BasicBlock : *Callee) // No need to update the callsite if it is pruned during inlining. if (!VMap || VMap->count()) -for (Instruction : BB) +for (Instruction : BB) { if (CallInst *CI = dyn_cast()) CI->updateProfWeight(NewEntryCount, PriorEntryCount); + if (InvokeInst *II = dyn_cast()) +II->updateProfWeight(NewEntryCount, PriorEntryCount); +} } } diff --git a/llvm/test/Transforms/Inline/update_invoke_value_profile.ll b/llvm/test/Transforms/Inline/update_invoke_value_profile.ll new file mode 100644 index 00..ac5597a41fce61 --- /dev/null +++ b/llvm/test/Transforms/Inline/update_invoke_value_profile.ll @@ -0,0 +1,185 @@ +; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 4 + +; RUN: opt < %s -passes='require,cgscc(inline)' -inline-threshold=1000 -S | FileCheck %s + +target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32:64-S128" +target triple = "x86_64-unknown-linux-gnu" + +%class.Error = type { i32 } +@_ZTI5Error = external constant { ptr, ptr } + +define i32 @callee(ptr %b) personality ptr @__gxx_personality_v0 !prof !17 { +; CHECK-LABEL: define i32 @callee( +; CHECK-SAME: ptr [[B:%.*]]) personality ptr @__gxx_personality_v0 !prof [[PROF0:![0-9]+]] { +; CHECK-NEXT: entry: +; CHECK-NEXT:[[E:%.*]] = alloca [[CLASS_ERROR:%.*]], align 8 +; CHECK-NEXT:[[VTABLE:%.*]] = load ptr, ptr [[B]], align 8 +; CHECK-NEXT:[[TMP0:%.*]] = load ptr, ptr [[VTABLE]], align 8 +; CHECK-NEXT:[[CALL:%.*]] = invoke i32 [[TMP0]](ptr [[B]]) +; CHECK-NEXT:to label [[TRY_CONT:%.*]] unwind label [[LPAD:%.*]], !prof [[PROF1:![0-9]+]] +; CHECK: lpad: +; CHECK-NEXT:[[TMP1:%.*]] =
[llvm-branch-commits] [llvm] [Inline][PGO] After inline, update profile for invoke instruction in both cloned instruction in the caller and original callee (PR #83809)
minglotus-6 wrote: > The invoke instruction can have 3 different kinds of prof data > > 1. call count (if a direct call) > 2. VP profile data (if an indirect call) > 3. branch weights for landing pad. > 4. can coexist with 2) and does not need to be updated. thanks for the list. I get the point that 1 and 2 are mutually exclusive. For my understanding, which one out of {1, 2, 3} doesn't need to be updated? > Is there an existing test coverage for type 1) update? Searching under `Transforms/Inline` directory, [inline-hot-callsite.ll](https://github.com/llvm/llvm-project/blob/4a4fb930a539c91eb4e9d8b1ea427a7cef72d054/llvm/test/Transforms/Inline/inline-hot-callsite.ll#L61) is an existing test with `!prof` annotated `invoke`, but the type is 3 (branch weights) not 1 (call count). Meanwhile, non-call instructions could have branch weights (notably `SwitchInst` and `IndirectBrInst`), and https://gcc.godbolt.org/z/8jjjPEcao shows '!prof' for 'switch' isn't updated after inline. I'll probably add the 'switch' test case together with the `callbr` [test case ](https://gcc.godbolt.org/z/b36P8o3K1). https://github.com/llvm/llvm-project/pull/83809 ___ llvm-branch-commits mailing list llvm-branch-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits
[llvm-branch-commits] [llvm] [Inline][PGO] After inline, update profile for invoke instruction in both cloned instruction in the caller and original callee (PR #83809)
@@ -918,6 +918,18 @@ LandingPadInst *InvokeInst::getLandingPadInst() const { return cast(getUnwindDest()->getFirstNonPHI()); } +void InvokeInst::updateProfWeight(uint64_t S, uint64_t T) { minglotus-6 wrote: > Is there a reason why branch weight !prof cannot be attached to callbr? nvm seems doc is just stale according to IR verifier [code](https://github.com/llvm/llvm-project/blob/083d8aa03aca55b88098a91e41e41a8e321a5721/llvm/lib/IR/Verifier.cpp#L4758-L4759) https://github.com/llvm/llvm-project/pull/83809 ___ llvm-branch-commits mailing list llvm-branch-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits
[llvm-branch-commits] [llvm] [Inline][PGO] After inline, update profile for invoke instruction in both cloned instruction in the caller and original callee (PR #83809)
@@ -918,6 +918,18 @@ LandingPadInst *InvokeInst::getLandingPadInst() const { return cast(getUnwindDest()->getFirstNonPHI()); } +void InvokeInst::updateProfWeight(uint64_t S, uint64_t T) { minglotus-6 wrote: also from https://news.ycombinator.com/item?id=21018901, _Support for asm goto, enabling for example the mainline Linux kernel for x86_64 to build with Clang_ https://github.com/llvm/llvm-project/pull/83809 ___ llvm-branch-commits mailing list llvm-branch-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits
[llvm-branch-commits] [llvm] [Inline][PGO] After inline, update profile for invoke instruction in both cloned instruction in the caller and original callee (PR #83809)
@@ -918,6 +918,18 @@ LandingPadInst *InvokeInst::getLandingPadInst() const { return cast(getUnwindDest()->getFirstNonPHI()); } +void InvokeInst::updateProfWeight(uint64_t S, uint64_t T) { minglotus-6 wrote: https://llvm.org/docs/BranchWeightMetadata.html#other mentions _Other terminator instructions are not allowed to contain Branch Weight Metadata_, indicating `callbr` instruction shouldn't have branch weight metadata attached. Is there a reason why branch weight `!prof` cannot be attached to `callbr`? https://github.com/llvm/llvm-project/pull/83809 ___ llvm-branch-commits mailing list llvm-branch-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits
[llvm-branch-commits] [llvm] [Inline][PGO] After inline, update profile for invoke instruction in both cloned instruction in the caller and original callee (PR #83809)
@@ -918,6 +918,18 @@ LandingPadInst *InvokeInst::getLandingPadInst() const { return cast(getUnwindDest()->getFirstNonPHI()); } +void InvokeInst::updateProfWeight(uint64_t S, uint64_t T) { minglotus-6 wrote: It makes sense to move them to `CallBase`. Will need to construct a test case (possibly based on https://github.com/llvm/llvm-project/blob/main/clang/test/CodeGen/asm-goto.c) - The difference between 1) current patch and 2) moving this to 'CallBase' and calling `updateProfWeight` on a `CallBase` is that, branch weight of terminating instruction `callbr` ([lowered](https://llvm.org/docs/LangRef.html#callbr-instruction) from inline assembly [goto](https://gcc.gnu.org/onlinedocs/gcc/Extended-Asm.html#GotoLabels)) will also be updated. https://github.com/llvm/llvm-project/pull/83809 ___ llvm-branch-commits mailing list llvm-branch-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits
[llvm-branch-commits] [llvm] [Inline][PGO] After inline, update profile for invoke instruction in both cloned instruction in the caller and original callee (PR #83809)
@@ -0,0 +1,185 @@ +; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 4 + +; RUN: opt < %s -passes='require,cgscc(inline)' -inline-threshold=1000 -S | FileCheck %s + +target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32:64-S128" +target triple = "x86_64-unknown-linux-gnu" + +%class.Error = type { i32 } +@_ZTI5Error = external constant { ptr, ptr } + +define i32 @callee(ptr %b) personality ptr @__gxx_personality_v0 !prof !17 { minglotus-6 wrote: The IR is reduced from this C++ example https://gcc.godbolt.org/z/brq413zf9 https://github.com/llvm/llvm-project/pull/83809 ___ llvm-branch-commits mailing list llvm-branch-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits
[llvm-branch-commits] [llvm] [Inline][PGO] After inline, update profile for invoke instruction in both cloned instruction in the caller and original callee (PR #83809)
https://github.com/minglotus-6 ready_for_review https://github.com/llvm/llvm-project/pull/83809 ___ llvm-branch-commits mailing list llvm-branch-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits
[llvm-branch-commits] [llvm] [Inline][PGO] After inline, update profile for invoke instruction in both cloned instruction in the caller and original callee (PR #83809)
https://github.com/minglotus-6 edited https://github.com/llvm/llvm-project/pull/83809 ___ llvm-branch-commits mailing list llvm-branch-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits
[llvm-branch-commits] [llvm] [Inline][PGO] After inline, update profile for invoke instruction in both cloned instruction in the caller and original callee (PR #83809)
https://github.com/minglotus-6 edited https://github.com/llvm/llvm-project/pull/83809 ___ llvm-branch-commits mailing list llvm-branch-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits
[llvm-branch-commits] [llvm] [Inline][PGO] After inline, update profile for invoke instruction in both cloned instruction in the caller and original callee (PR #83809)
https://github.com/minglotus-6 created https://github.com/llvm/llvm-project/pull/83809 None >From 9575b83ea40012ecbfbf301a24ec89de0726ffd4 Mon Sep 17 00:00:00 2001 From: mingmingl Date: Mon, 4 Mar 2024 00:43:55 -0800 Subject: [PATCH] update profile for invoke instruction in caller and callee after inline --- llvm/include/llvm/IR/Instructions.h | 3 + llvm/lib/IR/Instructions.cpp | 12 ++ llvm/lib/Transforms/Utils/InlineFunction.cpp | 11 +- .../Inline/update_invoke_value_profile.ll | 185 ++ 4 files changed, 209 insertions(+), 2 deletions(-) create mode 100644 llvm/test/Transforms/Inline/update_invoke_value_profile.ll diff --git a/llvm/include/llvm/IR/Instructions.h b/llvm/include/llvm/IR/Instructions.h index bc357074e5cb21..1146b3fa3ae244 100644 --- a/llvm/include/llvm/IR/Instructions.h +++ b/llvm/include/llvm/IR/Instructions.h @@ -4360,6 +4360,9 @@ class InvokeInst : public CallBase { unsigned getNumSuccessors() const { return 2; } + /// Updates profile metadata by scaling it by \p S / \p T. + void updateProfWeight(uint64_t S, uint64_t T); + // Methods for support type inquiry through isa, cast, and dyn_cast: static bool classof(const Instruction *I) { return (I->getOpcode() == Instruction::Invoke); diff --git a/llvm/lib/IR/Instructions.cpp b/llvm/lib/IR/Instructions.cpp index 9ae71acd523c36..920ce67f118991 100644 --- a/llvm/lib/IR/Instructions.cpp +++ b/llvm/lib/IR/Instructions.cpp @@ -918,6 +918,18 @@ LandingPadInst *InvokeInst::getLandingPadInst() const { return cast(getUnwindDest()->getFirstNonPHI()); } +void InvokeInst::updateProfWeight(uint64_t S, uint64_t T) { + if (T == 0) { +LLVM_DEBUG(dbgs() << "Attempting to update profile weights will result in " + "div by 0. Ignoring. Likely the function " + << getParent()->getParent()->getName() + << " has 0 entry count, and contains call instructions " + "with non-zero prof info."); +return; + } + scaleProfData(*this, S, T); +} + //===--===// //CallBrInst Implementation //===--===// diff --git a/llvm/lib/Transforms/Utils/InlineFunction.cpp b/llvm/lib/Transforms/Utils/InlineFunction.cpp index f68fdb26f28173..75b0d0669e9228 100644 --- a/llvm/lib/Transforms/Utils/InlineFunction.cpp +++ b/llvm/lib/Transforms/Utils/InlineFunction.cpp @@ -1909,10 +1909,14 @@ void llvm::updateProfileCallee( // During inlining ? if (VMap) { uint64_t CloneEntryCount = PriorEntryCount - NewEntryCount; -for (auto Entry : *VMap) +for (auto Entry : *VMap) { if (isa(Entry.first)) if (auto *CI = dyn_cast_or_null(Entry.second)) CI->updateProfWeight(CloneEntryCount, PriorEntryCount); + if (isa(Entry.first)) +if (auto *II = dyn_cast_or_null(Entry.second)) + II->updateProfWeight(CloneEntryCount, PriorEntryCount); +} } if (EntryDelta) { @@ -1921,9 +1925,12 @@ void llvm::updateProfileCallee( for (BasicBlock : *Callee) // No need to update the callsite if it is pruned during inlining. if (!VMap || VMap->count()) -for (Instruction : BB) +for (Instruction : BB) { if (CallInst *CI = dyn_cast()) CI->updateProfWeight(NewEntryCount, PriorEntryCount); + if (InvokeInst *II = dyn_cast()) +II->updateProfWeight(NewEntryCount, PriorEntryCount); +} } } diff --git a/llvm/test/Transforms/Inline/update_invoke_value_profile.ll b/llvm/test/Transforms/Inline/update_invoke_value_profile.ll new file mode 100644 index 00..ac5597a41fce61 --- /dev/null +++ b/llvm/test/Transforms/Inline/update_invoke_value_profile.ll @@ -0,0 +1,185 @@ +; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 4 + +; RUN: opt < %s -passes='require,cgscc(inline)' -inline-threshold=1000 -S | FileCheck %s + +target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32:64-S128" +target triple = "x86_64-unknown-linux-gnu" + +%class.Error = type { i32 } +@_ZTI5Error = external constant { ptr, ptr } + +define i32 @callee(ptr %b) personality ptr @__gxx_personality_v0 !prof !17 { +; CHECK-LABEL: define i32 @callee( +; CHECK-SAME: ptr [[B:%.*]]) personality ptr @__gxx_personality_v0 !prof [[PROF0:![0-9]+]] { +; CHECK-NEXT: entry: +; CHECK-NEXT:[[E:%.*]] = alloca [[CLASS_ERROR:%.*]], align 8 +; CHECK-NEXT:[[VTABLE:%.*]] = load ptr, ptr [[B]], align 8 +; CHECK-NEXT:[[TMP0:%.*]] = load ptr, ptr [[VTABLE]], align 8 +; CHECK-NEXT:[[CALL:%.*]] = invoke i32 [[TMP0]](ptr [[B]]) +; CHECK-NEXT:to label [[TRY_CONT:%.*]] unwind label [[LPAD:%.*]], !prof [[PROF1:![0-9]+]] +; CHECK: lpad: +; CHECK-NEXT:
[llvm-branch-commits] [llvm] [Inline]Update value profile for non-call instructions (PR #83769)
https://github.com/minglotus-6 edited https://github.com/llvm/llvm-project/pull/83769 ___ llvm-branch-commits mailing list llvm-branch-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits
[llvm-branch-commits] [llvm] [Inline]Update value profile for non-call instructions (PR #83769)
https://github.com/minglotus-6 created https://github.com/llvm/llvm-project/pull/83769 None >From 04a2bca6ee0fbea6a9dc84f59e8bf4a41f8ae230 Mon Sep 17 00:00:00 2001 From: mingmingl Date: Sun, 3 Mar 2024 22:16:03 -0800 Subject: [PATCH] [Inline]Update value profile for non-call instructions --- llvm/include/llvm/IR/ProfDataUtils.h | 3 + llvm/lib/IR/ProfDataUtils.cpp | 32 +++ llvm/lib/Transforms/Utils/InlineFunction.cpp | 26 +- .../Transforms/Inline/update_value_profile.ll | 89 +++ 4 files changed, 147 insertions(+), 3 deletions(-) create mode 100644 llvm/test/Transforms/Inline/update_value_profile.ll diff --git a/llvm/include/llvm/IR/ProfDataUtils.h b/llvm/include/llvm/IR/ProfDataUtils.h index 255fa2ff1c7906..2010c4bc1e8b34 100644 --- a/llvm/include/llvm/IR/ProfDataUtils.h +++ b/llvm/include/llvm/IR/ProfDataUtils.h @@ -108,5 +108,8 @@ bool extractProfTotalWeight(const Instruction , uint64_t ); /// a `prof` metadata reference to instruction `I`. void setBranchWeights(Instruction , ArrayRef Weights); +/// Scaling value profile 'ProfData' using the ratio of S/T. +MDNode *scaleValueProfile(const MDNode *ProfData, uint64_t S, uint64_t T); + } // namespace llvm #endif diff --git a/llvm/lib/IR/ProfDataUtils.cpp b/llvm/lib/IR/ProfDataUtils.cpp index dcb057c1b25fd8..db91a66bf493ec 100644 --- a/llvm/lib/IR/ProfDataUtils.cpp +++ b/llvm/lib/IR/ProfDataUtils.cpp @@ -190,4 +190,36 @@ void setBranchWeights(Instruction , ArrayRef Weights) { I.setMetadata(LLVMContext::MD_prof, BranchWeights); } +MDNode *scaleValueProfile(const MDNode *ProfData, uint64_t S, uint64_t T) { + if (ProfData == nullptr) +return nullptr; + assert( + dyn_cast(ProfData->getOperand(0))->getString().equals("VP") && + "Expects value profile metadata"); + LLVMContext = ProfData->getContext(); + MDBuilder MDB(C); + APInt APS(128, S), APT(128, T); + + SmallVector Vals; + Vals.push_back(ProfData->getOperand(0)); + for (unsigned i = 1; i < ProfData->getNumOperands(); i += 2) { +Vals.push_back(ProfData->getOperand(i)); +uint64_t Count = +mdconst::dyn_extract(ProfData->getOperand(i + 1)) +->getValue() +.getZExtValue(); +// Don't scale the magic number. +if (Count == NOMORE_ICP_MAGICNUM) { + Vals.push_back(ProfData->getOperand(i + 1)); + continue; +} +// Using APInt::div may be expensive, but most cases should fit 64 bits. +APInt Val(128, Count); +Val *= APS; +Vals.push_back(MDB.createConstant(ConstantInt::get( +Type::getInt64Ty(C), Val.udiv(APT).getLimitedValue(; + } + return MDNode::get(C, Vals); +} + } // namespace llvm diff --git a/llvm/lib/Transforms/Utils/InlineFunction.cpp b/llvm/lib/Transforms/Utils/InlineFunction.cpp index d4d4bf5ebdf36e..7cc1641a207aef 100644 --- a/llvm/lib/Transforms/Utils/InlineFunction.cpp +++ b/llvm/lib/Transforms/Utils/InlineFunction.cpp @@ -23,6 +23,7 @@ #include "llvm/Analysis/BlockFrequencyInfo.h" #include "llvm/Analysis/CallGraph.h" #include "llvm/Analysis/CaptureTracking.h" +#include "llvm/Analysis/IndirectCallVisitor.h" #include "llvm/Analysis/InstructionSimplify.h" #include "llvm/Analysis/MemoryProfileInfo.h" #include "llvm/Analysis/ObjCARCAnalysisUtils.h" @@ -30,8 +31,8 @@ #include "llvm/Analysis/ProfileSummaryInfo.h" #include "llvm/Analysis/ValueTracking.h" #include "llvm/Analysis/VectorUtils.h" -#include "llvm/IR/AttributeMask.h" #include "llvm/IR/Argument.h" +#include "llvm/IR/AttributeMask.h" #include "llvm/IR/BasicBlock.h" #include "llvm/IR/CFG.h" #include "llvm/IR/Constant.h" @@ -55,6 +56,7 @@ #include "llvm/IR/MDBuilder.h" #include "llvm/IR/Metadata.h" #include "llvm/IR/Module.h" +#include "llvm/IR/ProfDataUtils.h" #include "llvm/IR/Type.h" #include "llvm/IR/User.h" #include "llvm/IR/Value.h" @@ -1910,9 +1912,18 @@ void llvm::updateProfileCallee( if (VMap) { uint64_t CloneEntryCount = PriorEntryCount - NewEntryCount; for (auto Entry : *VMap) + // FIXME: Update the profiles for invoke instruction after inline if (isa(Entry.first)) -if (auto *CI = dyn_cast_or_null(Entry.second)) +if (auto *CI = dyn_cast_or_null(Entry.second)) { CI->updateProfWeight(CloneEntryCount, PriorEntryCount); + Instruction *VPtr = + PGOIndirectCallVisitor::tryGetVTableInstruction(CI); + if (VPtr) +VPtr->setMetadata( +LLVMContext::MD_prof, +scaleValueProfile(VPtr->getMetadata(LLVMContext::MD_prof), + CloneEntryCount, PriorEntryCount)); +} } if (EntryDelta) { @@ -1922,8 +1933,17 @@ void llvm::updateProfileCallee( // No need to update the callsite if it is pruned during inlining. if (!VMap || VMap->count()) for (Instruction : BB) - if (CallInst *CI = dyn_cast()) + // FIXME: Update the profiles for invoke instruction after
[llvm-branch-commits] [llvm] [nfc][InstrProfiling]For comdat setting helper function, move comment closer to the code (PR #83757)
https://github.com/minglotus-6 edited https://github.com/llvm/llvm-project/pull/83757 ___ llvm-branch-commits mailing list llvm-branch-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits
[llvm-branch-commits] [llvm] [nfc][InstrProfiling]For comdat setting helper function, move comment closer to the code (PR #83757)
https://github.com/minglotus-6 updated https://github.com/llvm/llvm-project/pull/83757 >From 13099c90449036731b834e27aa8fb1c238ab8669 Mon Sep 17 00:00:00 2001 From: mingmingl Date: Sun, 3 Mar 2024 19:02:09 -0800 Subject: [PATCH 1/2] [nfc][InstrProfiling]For comdat setting helper function, move comment closer to the code --- .../Instrumentation/InstrProfiling.cpp| 53 +++ 1 file changed, 31 insertions(+), 22 deletions(-) diff --git a/llvm/lib/Transforms/Instrumentation/InstrProfiling.cpp b/llvm/lib/Transforms/Instrumentation/InstrProfiling.cpp index d5d55dec6382fb..84c6aef4c998a7 100644 --- a/llvm/lib/Transforms/Instrumentation/InstrProfiling.cpp +++ b/llvm/lib/Transforms/Instrumentation/InstrProfiling.cpp @@ -206,8 +206,8 @@ class InstrLowerer final { const bool IsCS; std::function GetTLI; - const bool DataReferencedByCode; + struct PerFunctionProfileData { uint32_t NumValueSites[IPVK_Last + 1] = {}; GlobalVariable *RegionCounters = nullptr; @@ -1193,18 +1193,42 @@ static bool needsRuntimeRegistrationOfSectionRange(const Triple ) { } void InstrLowerer::maybeSetComdat(GlobalVariable *GV, Function *Fn, - StringRef VarName) { + StringRef CounterGroupName) { + // Place lowered global variables in a comdat group if the associated function + // is a COMDAT. This will make sure that only one copy of global variable + // (e.g. function counters) of the COMDAT function will be emitted after + // linking. bool NeedComdat = needsComdatForCounter(*Fn, M); + bool UseComdat = (NeedComdat || TT.isOSBinFormatELF()); if (!UseComdat) return; - StringRef GroupName = - TT.isOSBinFormatCOFF() && DataReferencedByCode ? GV->getName() : VarName; + // Keep in mind that this pass may run before the inliner, so we need to + // create a new comdat group (for counters, profiling data, etc). If we use + // the comdat of the parent function, that will result in relocations against + // discarded sections. + // + // If the data variable is referenced by code, non-counter variables (notably + // profiling data) and counters have to be in different comdats for COFF + // because the Visual C++ linker will report duplicate symbol errors if there + // are multiple external symbols with the same name marked + // IMAGE_COMDAT_SELECT_ASSOCIATIVE. + StringRef GroupName = TT.isOSBinFormatCOFF() && DataReferencedByCode +? GV->getName() +: CounterGroupName; Comdat *C = M.getOrInsertComdat(GroupName); - if (!NeedComdat) + + if (!NeedComdat) { +// Object file format must be ELF since `UseComdat && !NeedComdat` is true. +// +// For ELF, when not using COMDAT, put counters, data and values into a +// nodeduplicate COMDAT which is lowered to a zero-flag section group. This +// allows -z start-stop-gc to discard the entire group when the function is +// discarded. C->setSelectionKind(Comdat::NoDeduplicate); + } GV->setComdat(C); // COFF doesn't allow the comdat group leader to have private linkage, so // upgrade private linkage to internal linkage to produce a symbol table @@ -1238,23 +1262,7 @@ GlobalVariable *InstrLowerer::setupProfileSection(InstrProfInstBase *Inc, Linkage = GlobalValue::PrivateLinkage; Visibility = GlobalValue::DefaultVisibility; } - // Move the name variable to the right section. Place them in a COMDAT group - // if the associated function is a COMDAT. This will make sure that only one - // copy of counters of the COMDAT function will be emitted after linking. Keep - // in mind that this pass may run before the inliner, so we need to create a - // new comdat group for the counters and profiling data. If we use the comdat - // of the parent function, that will result in relocations against discarded - // sections. - // - // If the data variable is referenced by code, counters and data have to be - // in different comdats for COFF because the Visual C++ linker will report - // duplicate symbol errors if there are multiple external symbols with the - // same name marked IMAGE_COMDAT_SELECT_ASSOCIATIVE. - // - // For ELF, when not using COMDAT, put counters, data and values into a - // nodeduplicate COMDAT which is lowered to a zero-flag section group. This - // allows -z start-stop-gc to discard the entire group when the function is - // discarded. + // Move the name variable to the right section. bool Renamed; GlobalVariable *Ptr; StringRef VarPrefix; @@ -1524,6 +1532,7 @@ void InstrLowerer::createDataVariable(InstrProfCntrInstBase *Inc) { Data->setSection( getInstrProfSectionName(DataSectionKind, TT.getObjectFormat())); Data->setAlignment(Align(INSTR_PROF_DATA_ALIGNMENT)); + maybeSetComdat(Data, Fn, CntsVarName); PD.DataVar = Data; >From 28301273f25dac0219f591dd1ee18ef587ccec24 Mon Sep 17 00:00:00 2001
[llvm-branch-commits] [llvm] [nfc][InstrProfiling]For comdat setting helper function, move comment closer to the code (PR #83757)
https://github.com/minglotus-6 created https://github.com/llvm/llvm-project/pull/83757 None >From 13099c90449036731b834e27aa8fb1c238ab8669 Mon Sep 17 00:00:00 2001 From: mingmingl Date: Sun, 3 Mar 2024 19:02:09 -0800 Subject: [PATCH] [nfc][InstrProfiling]For comdat setting helper function, move comment closer to the code --- .../Instrumentation/InstrProfiling.cpp| 53 +++ 1 file changed, 31 insertions(+), 22 deletions(-) diff --git a/llvm/lib/Transforms/Instrumentation/InstrProfiling.cpp b/llvm/lib/Transforms/Instrumentation/InstrProfiling.cpp index d5d55dec6382fb..84c6aef4c998a7 100644 --- a/llvm/lib/Transforms/Instrumentation/InstrProfiling.cpp +++ b/llvm/lib/Transforms/Instrumentation/InstrProfiling.cpp @@ -206,8 +206,8 @@ class InstrLowerer final { const bool IsCS; std::function GetTLI; - const bool DataReferencedByCode; + struct PerFunctionProfileData { uint32_t NumValueSites[IPVK_Last + 1] = {}; GlobalVariable *RegionCounters = nullptr; @@ -1193,18 +1193,42 @@ static bool needsRuntimeRegistrationOfSectionRange(const Triple ) { } void InstrLowerer::maybeSetComdat(GlobalVariable *GV, Function *Fn, - StringRef VarName) { + StringRef CounterGroupName) { + // Place lowered global variables in a comdat group if the associated function + // is a COMDAT. This will make sure that only one copy of global variable + // (e.g. function counters) of the COMDAT function will be emitted after + // linking. bool NeedComdat = needsComdatForCounter(*Fn, M); + bool UseComdat = (NeedComdat || TT.isOSBinFormatELF()); if (!UseComdat) return; - StringRef GroupName = - TT.isOSBinFormatCOFF() && DataReferencedByCode ? GV->getName() : VarName; + // Keep in mind that this pass may run before the inliner, so we need to + // create a new comdat group (for counters, profiling data, etc). If we use + // the comdat of the parent function, that will result in relocations against + // discarded sections. + // + // If the data variable is referenced by code, non-counter variables (notably + // profiling data) and counters have to be in different comdats for COFF + // because the Visual C++ linker will report duplicate symbol errors if there + // are multiple external symbols with the same name marked + // IMAGE_COMDAT_SELECT_ASSOCIATIVE. + StringRef GroupName = TT.isOSBinFormatCOFF() && DataReferencedByCode +? GV->getName() +: CounterGroupName; Comdat *C = M.getOrInsertComdat(GroupName); - if (!NeedComdat) + + if (!NeedComdat) { +// Object file format must be ELF since `UseComdat && !NeedComdat` is true. +// +// For ELF, when not using COMDAT, put counters, data and values into a +// nodeduplicate COMDAT which is lowered to a zero-flag section group. This +// allows -z start-stop-gc to discard the entire group when the function is +// discarded. C->setSelectionKind(Comdat::NoDeduplicate); + } GV->setComdat(C); // COFF doesn't allow the comdat group leader to have private linkage, so // upgrade private linkage to internal linkage to produce a symbol table @@ -1238,23 +1262,7 @@ GlobalVariable *InstrLowerer::setupProfileSection(InstrProfInstBase *Inc, Linkage = GlobalValue::PrivateLinkage; Visibility = GlobalValue::DefaultVisibility; } - // Move the name variable to the right section. Place them in a COMDAT group - // if the associated function is a COMDAT. This will make sure that only one - // copy of counters of the COMDAT function will be emitted after linking. Keep - // in mind that this pass may run before the inliner, so we need to create a - // new comdat group for the counters and profiling data. If we use the comdat - // of the parent function, that will result in relocations against discarded - // sections. - // - // If the data variable is referenced by code, counters and data have to be - // in different comdats for COFF because the Visual C++ linker will report - // duplicate symbol errors if there are multiple external symbols with the - // same name marked IMAGE_COMDAT_SELECT_ASSOCIATIVE. - // - // For ELF, when not using COMDAT, put counters, data and values into a - // nodeduplicate COMDAT which is lowered to a zero-flag section group. This - // allows -z start-stop-gc to discard the entire group when the function is - // discarded. + // Move the name variable to the right section. bool Renamed; GlobalVariable *Ptr; StringRef VarPrefix; @@ -1524,6 +1532,7 @@ void InstrLowerer::createDataVariable(InstrProfCntrInstBase *Inc) { Data->setSection( getInstrProfSectionName(DataSectionKind, TT.getObjectFormat())); Data->setAlignment(Align(INSTR_PROF_DATA_ALIGNMENT)); + maybeSetComdat(Data, Fn, CntsVarName); PD.DataVar = Data; ___ llvm-branch-commits
[llvm-branch-commits] [compiler-rt] [llvm] A copy of https://github.com/llvm/llvm-project/pull/66825. Actually this is a superset (a copy plus the thinlto-import change) (PR #80761)
https://github.com/minglotus-6 edited https://github.com/llvm/llvm-project/pull/80761 ___ llvm-branch-commits mailing list llvm-branch-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits
[llvm-branch-commits] [compiler-rt] [llvm] A copy of https://github.com/llvm/llvm-project/pull/66825. Actually this is a superset (a copy plus the thinlto-import change) (PR #80761)
https://github.com/minglotus-6 edited https://github.com/llvm/llvm-project/pull/80761 ___ llvm-branch-commits mailing list llvm-branch-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits
[llvm-branch-commits] [llvm] [CallPromotionUtils]Implement conditional indirect call promotion with vtable-based comparison (PR #81378)
https://github.com/minglotus-6 edited https://github.com/llvm/llvm-project/pull/81378 ___ llvm-branch-commits mailing list llvm-branch-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits
[llvm-branch-commits] [compiler-rt] [llvm] A copy of https://github.com/llvm/llvm-project/pull/66825. Actually this is a superset (a copy plus the thinlto-import change) (PR #80761)
https://github.com/minglotus-6 edited https://github.com/llvm/llvm-project/pull/80761 ___ llvm-branch-commits mailing list llvm-branch-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits
[llvm-branch-commits] [compiler-rt] [llvm] [Placeholder] (PR #80761)
https://github.com/minglotus-6 edited https://github.com/llvm/llvm-project/pull/80761 ___ llvm-branch-commits mailing list llvm-branch-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits
[llvm-branch-commits] [llvm] [InstrProf] Add vtables with type metadata into symtab to look it up with GUID (PR #81051)
@@ -479,15 +479,37 @@ Error InstrProfSymtab::create(Module , bool InLTO) { continue; Types.clear(); G.getMetadata(LLVMContext::MD_type, Types); -if (!Types.empty()) { - MD5VTableMap.emplace_back(G.getGUID(), ); -} +if (Types.empty()) + continue; +if (Error E = addVTableWithName( + G, getIRPGOObjectName(G, InLTO, /* PGONameMetadata */ nullptr))) +return E; } Sorted = false; finalizeSymtab(); return Error::success(); } +Error InstrProfSymtab::addVTableWithName(GlobalVariable , + StringRef VTablePGOName) { + + auto mapName = [&](StringRef Name) -> Error { +if (Error E = addVTableName(Name)) minglotus-6 wrote: And in the latest [commit](https://github.com/llvm/llvm-project/pull/81051/commits/003319d8d1f791dbe0c34bf82f0043f71b330d68) I decided to call `addSymbolName` and added some comments around. It's a change in the diffbase to make `addFuncName` a [wrapper](https://github.com/llvm/llvm-project/pull/66825/files#diff-da37bd91946b34b8d50ced64d8e9f79455b61e8d29b304d0ae9fe3148bc6R552) around [`addSymbolName`](https://github.com/llvm/llvm-project/pull/66825/files#diff-da37bd91946b34b8d50ced64d8e9f79455b61e8d29b304d0ae9fe3148bc6R534). Meanwhile as the functions from the diffbase (mainly `InstrProf.h/cpp`) starts to be used in subsequent patches, I'll start to get the first instrumentation patch (https://github.com/llvm/llvm-project/pull/66825) into main branch. https://github.com/llvm/llvm-project/pull/81051 ___ llvm-branch-commits mailing list llvm-branch-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits
[llvm-branch-commits] [llvm] [InstrProf] Add vtables with type metadata into symtab to look it up with GUID (PR #81051)
https://github.com/minglotus-6 ready_for_review https://github.com/llvm/llvm-project/pull/81051 ___ llvm-branch-commits mailing list llvm-branch-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits
[llvm-branch-commits] [llvm] [InstrProf] Add vtables with type metadata into symtab to look it up with GUID (PR #81051)
@@ -479,15 +479,37 @@ Error InstrProfSymtab::create(Module , bool InLTO) { continue; Types.clear(); G.getMetadata(LLVMContext::MD_type, Types); -if (!Types.empty()) { - MD5VTableMap.emplace_back(G.getGUID(), ); -} +if (Types.empty()) + continue; +if (Error E = addVTableWithName( + G, getIRPGOObjectName(G, InLTO, /* PGONameMetadata */ nullptr))) +return E; } Sorted = false; finalizeSymtab(); return Error::success(); } +Error InstrProfSymtab::addVTableWithName(GlobalVariable , + StringRef VTablePGOName) { + + auto mapName = [&](StringRef Name) -> Error { +if (Error E = addVTableName(Name)) minglotus-6 wrote: This is [defined](https://github.com/llvm/llvm-project/pull/66825/files#diff-da37bd91946b34b8d50ced64d8e9f79455b61e8d29b304d0ae9fe3148bc6R556-R564) in InstrProf.h in the PR to instrument vtables and [used](https://github.com/llvm/llvm-project/pull/66825/files#diff-da37bd91946b34b8d50ced64d8e9f79455b61e8d29b304d0ae9fe3148bc6R642-R645) to add vtable strings from raw profiles (`InstrProfSymtab::create` called [here](https://github.com/llvm/llvm-project/pull/66825/files#diff-b196b796c5a396c7cdf93b347fe47e2b29b72d0b7dd0e2b88abb964d376ee50eR544-R545)) The diff is not shown in this PR, but I got a [link](https://github.com/llvm/llvm-project/pull/81051/files#diff-da37bd91946b34b8d50ced64d8e9f79455b61e8d29b304d0ae9fe3148bc6L579-L587) pointing to it. https://github.com/llvm/llvm-project/pull/81051 ___ llvm-branch-commits mailing list llvm-branch-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits
[llvm-branch-commits] [llvm] [InstrProf] Add vtables with type metadata into symtab to look it up with GUID (PR #81051)
https://github.com/minglotus-6 updated https://github.com/llvm/llvm-project/pull/81051 >From 66dbbfef52bdc092cbd4ed619bba38c003f6063d Mon Sep 17 00:00:00 2001 From: mingmingl Date: Thu, 8 Feb 2024 09:07:27 -0800 Subject: [PATCH 1/2] [InstrProf] Add vtables with type metadata into symtab to look it up with GUID --- llvm/include/llvm/ProfileData/InstrProf.h| 19 + llvm/lib/ProfileData/InstrProf.cpp | 87 ++-- llvm/unittests/ProfileData/InstrProfTest.cpp | 55 + 3 files changed, 138 insertions(+), 23 deletions(-) diff --git a/llvm/include/llvm/ProfileData/InstrProf.h b/llvm/include/llvm/ProfileData/InstrProf.h index 53108a093bf4dd..6e799cf8aa273e 100644 --- a/llvm/include/llvm/ProfileData/InstrProf.h +++ b/llvm/include/llvm/ProfileData/InstrProf.h @@ -487,8 +487,25 @@ class InstrProfSymtab { return "** External Symbol **"; } + // Returns the canonical name of the given PGOName by stripping the names + // suffixes that begins with ".". If MayHaveUniqueSuffix is true, ".__uniq." + // suffix is kept in the canonical name. + StringRef getCanonicalName(StringRef PGOName, bool MayHaveUniqueSuffix); + + // Add the function into the symbol table, by creating the following + // map entries: + // - + // - + // - + // - + // - (instrprof_error::malformed, @@ -665,6 +683,7 @@ void InstrProfSymtab::finalizeSymtab() { if (Sorted) return; llvm::sort(MD5NameMap, less_first()); + llvm::sort(MD5VTableMap, less_first()); llvm::sort(MD5FuncMap, less_first()); llvm::sort(AddrToMD5Map, less_first()); AddrToMD5Map.erase(std::unique(AddrToMD5Map.begin(), AddrToMD5Map.end()), diff --git a/llvm/lib/ProfileData/InstrProf.cpp b/llvm/lib/ProfileData/InstrProf.cpp index 9ebcba10c860ff..a09a2bb0ba77cb 100644 --- a/llvm/lib/ProfileData/InstrProf.cpp +++ b/llvm/lib/ProfileData/InstrProf.cpp @@ -480,7 +480,9 @@ Error InstrProfSymtab::create(Module , bool InLTO) { Types.clear(); G.getMetadata(LLVMContext::MD_type, Types); if (!Types.empty()) { - MD5VTableMap.emplace_back(G.getGUID(), ); + if (Error E = addVTableWithName( + G, getIRPGOObjectName(G, InLTO, /* PGONameMetadata */ nullptr))) +return E; } } Sorted = false; @@ -488,6 +490,30 @@ Error InstrProfSymtab::create(Module , bool InLTO) { return Error::success(); } +Error InstrProfSymtab::addVTableWithName(GlobalVariable , + StringRef VTablePGOName) { + if (Error E = addVTableName(VTablePGOName)) +return E; + + MD5VTableMap.emplace_back(GlobalValue::getGUID(VTablePGOName), ); + + // NOTE: `-funique-internal-linkage-names` doesn't uniqufy vtables, so no + // need to check ".__uniq." + + // If a local-linkage vtable is promoted to have external linkage in ThinLTO, + // it will have `.llvm.` in its name. Use the name before externalization. + StringRef CanonicalName = + getCanonicalName(VTablePGOName, /* MayHaveUniqueSuffix= */ false); + if (CanonicalName != VTablePGOName) { +if (Error E = addVTableName(CanonicalName)) + return E; + +MD5VTableMap.emplace_back(GlobalValue::getGUID(CanonicalName), ); + } + + return Error::success(); +} + /// \c NameStrings is a string composed of one of more possibly encoded /// sub-strings. The substrings are separated by 0 or more zero bytes. This /// method decodes the string and calls `NameCallback` for each substring. @@ -560,35 +586,50 @@ Error InstrProfSymtab::initVTableNamesFromCompressedStrings( std::bind(::addVTableName, this, std::placeholders::_1)); } -Error InstrProfSymtab::addFuncWithName(Function , StringRef PGOFuncName) { - if (Error E = addFuncName(PGOFuncName)) -return E; - MD5FuncMap.emplace_back(Function::getGUID(PGOFuncName), ); +StringRef InstrProfSymtab::getCanonicalName(StringRef PGOName, +bool MayHaveUniqueSuffix) { + size_t pos = 0; // In ThinLTO, local function may have been promoted to global and have // suffix ".llvm." added to the function name. We need to add the // stripped function name to the symbol table so that we can find a match // from profile. // - // We may have other suffixes similar as ".llvm." which are needed to - // be stripped before the matching, but ".__uniq." suffix which is used - // to differentiate internal linkage functions in different modules - // should be kept. Now this is the only suffix with the pattern ".xxx" - // which is kept before matching. - const std::string UniqSuffix = ".__uniq."; - auto pos = PGOFuncName.find(UniqSuffix); - // Search '.' after ".__uniq." if ".__uniq." exists, otherwise - // search '.' from the beginning. - if (pos != std::string::npos) -pos += UniqSuffix.length(); - else -pos = 0; - pos = PGOFuncName.find('.', pos); - if (pos != std::string::npos && pos != 0) { -StringRef OtherFuncName = PGOFuncName.substr(0, pos); -if (Error E =
[llvm-branch-commits] [llvm] [InstrProf] Add vtables with type metadata into symtab to look it up with GUID (PR #81051)
https://github.com/minglotus-6 updated https://github.com/llvm/llvm-project/pull/81051 >From 66dbbfef52bdc092cbd4ed619bba38c003f6063d Mon Sep 17 00:00:00 2001 From: mingmingl Date: Thu, 8 Feb 2024 09:07:27 -0800 Subject: [PATCH] [InstrProf] Add vtables with type metadata into symtab to look it up with GUID --- llvm/include/llvm/ProfileData/InstrProf.h| 19 + llvm/lib/ProfileData/InstrProf.cpp | 87 ++-- llvm/unittests/ProfileData/InstrProfTest.cpp | 55 + 3 files changed, 138 insertions(+), 23 deletions(-) diff --git a/llvm/include/llvm/ProfileData/InstrProf.h b/llvm/include/llvm/ProfileData/InstrProf.h index 53108a093bf4dd..6e799cf8aa273e 100644 --- a/llvm/include/llvm/ProfileData/InstrProf.h +++ b/llvm/include/llvm/ProfileData/InstrProf.h @@ -487,8 +487,25 @@ class InstrProfSymtab { return "** External Symbol **"; } + // Returns the canonical name of the given PGOName by stripping the names + // suffixes that begins with ".". If MayHaveUniqueSuffix is true, ".__uniq." + // suffix is kept in the canonical name. + StringRef getCanonicalName(StringRef PGOName, bool MayHaveUniqueSuffix); + + // Add the function into the symbol table, by creating the following + // map entries: + // - + // - + // - + // - + // - (instrprof_error::malformed, @@ -665,6 +683,7 @@ void InstrProfSymtab::finalizeSymtab() { if (Sorted) return; llvm::sort(MD5NameMap, less_first()); + llvm::sort(MD5VTableMap, less_first()); llvm::sort(MD5FuncMap, less_first()); llvm::sort(AddrToMD5Map, less_first()); AddrToMD5Map.erase(std::unique(AddrToMD5Map.begin(), AddrToMD5Map.end()), diff --git a/llvm/lib/ProfileData/InstrProf.cpp b/llvm/lib/ProfileData/InstrProf.cpp index 9ebcba10c860ff..a09a2bb0ba77cb 100644 --- a/llvm/lib/ProfileData/InstrProf.cpp +++ b/llvm/lib/ProfileData/InstrProf.cpp @@ -480,7 +480,9 @@ Error InstrProfSymtab::create(Module , bool InLTO) { Types.clear(); G.getMetadata(LLVMContext::MD_type, Types); if (!Types.empty()) { - MD5VTableMap.emplace_back(G.getGUID(), ); + if (Error E = addVTableWithName( + G, getIRPGOObjectName(G, InLTO, /* PGONameMetadata */ nullptr))) +return E; } } Sorted = false; @@ -488,6 +490,30 @@ Error InstrProfSymtab::create(Module , bool InLTO) { return Error::success(); } +Error InstrProfSymtab::addVTableWithName(GlobalVariable , + StringRef VTablePGOName) { + if (Error E = addVTableName(VTablePGOName)) +return E; + + MD5VTableMap.emplace_back(GlobalValue::getGUID(VTablePGOName), ); + + // NOTE: `-funique-internal-linkage-names` doesn't uniqufy vtables, so no + // need to check ".__uniq." + + // If a local-linkage vtable is promoted to have external linkage in ThinLTO, + // it will have `.llvm.` in its name. Use the name before externalization. + StringRef CanonicalName = + getCanonicalName(VTablePGOName, /* MayHaveUniqueSuffix= */ false); + if (CanonicalName != VTablePGOName) { +if (Error E = addVTableName(CanonicalName)) + return E; + +MD5VTableMap.emplace_back(GlobalValue::getGUID(CanonicalName), ); + } + + return Error::success(); +} + /// \c NameStrings is a string composed of one of more possibly encoded /// sub-strings. The substrings are separated by 0 or more zero bytes. This /// method decodes the string and calls `NameCallback` for each substring. @@ -560,35 +586,50 @@ Error InstrProfSymtab::initVTableNamesFromCompressedStrings( std::bind(::addVTableName, this, std::placeholders::_1)); } -Error InstrProfSymtab::addFuncWithName(Function , StringRef PGOFuncName) { - if (Error E = addFuncName(PGOFuncName)) -return E; - MD5FuncMap.emplace_back(Function::getGUID(PGOFuncName), ); +StringRef InstrProfSymtab::getCanonicalName(StringRef PGOName, +bool MayHaveUniqueSuffix) { + size_t pos = 0; // In ThinLTO, local function may have been promoted to global and have // suffix ".llvm." added to the function name. We need to add the // stripped function name to the symbol table so that we can find a match // from profile. // - // We may have other suffixes similar as ".llvm." which are needed to - // be stripped before the matching, but ".__uniq." suffix which is used - // to differentiate internal linkage functions in different modules - // should be kept. Now this is the only suffix with the pattern ".xxx" - // which is kept before matching. - const std::string UniqSuffix = ".__uniq."; - auto pos = PGOFuncName.find(UniqSuffix); - // Search '.' after ".__uniq." if ".__uniq." exists, otherwise - // search '.' from the beginning. - if (pos != std::string::npos) -pos += UniqSuffix.length(); - else -pos = 0; - pos = PGOFuncName.find('.', pos); - if (pos != std::string::npos && pos != 0) { -StringRef OtherFuncName = PGOFuncName.substr(0, pos); -if (Error E =
[llvm-branch-commits] [llvm] [InstrProf] Add vtables with type metadata into symtab to look it up with GUID (PR #81051)
https://github.com/minglotus-6 converted_to_draft https://github.com/llvm/llvm-project/pull/81051 ___ llvm-branch-commits mailing list llvm-branch-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits
[llvm-branch-commits] [llvm] [InstrProf] Add vtables with type metadata into symtab to look it up with GUID (PR #81051)
@@ -560,35 +586,50 @@ Error InstrProfSymtab::initVTableNamesFromCompressedStrings( std::bind(::addVTableName, this, std::placeholders::_1)); } -Error InstrProfSymtab::addFuncWithName(Function , StringRef PGOFuncName) { - if (Error E = addFuncName(PGOFuncName)) -return E; - MD5FuncMap.emplace_back(Function::getGUID(PGOFuncName), ); +StringRef InstrProfSymtab::getCanonicalName(StringRef PGOName, minglotus-6 wrote: Unifying the two (and parameterizing the difference between instr and sample profiles like [`FunctionSamples::HasUniqSuffix`](https://github.com/llvm/llvm-project/blob/91dcf53abd34fa836a126c706f87b810d299d802/llvm/include/llvm/ProfileData/SampleProf.h#L) as function arguments) makes sense, and the [nfc patch](https://github.com/llvm/llvm-project/pull/81547) has a FIXME to do this. A new file named `llvm/lib/ProfileData/ProfileCommon.cpp` seems a good place for the helper function. Currently `llvm/include/llvm/ProfileData/ProfileCommon.h` contains common classes used by sample and instr profiles. https://github.com/llvm/llvm-project/pull/81051 ___ llvm-branch-commits mailing list llvm-branch-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits
[llvm-branch-commits] [llvm] [NFC][CallPromotionUtils]Extract a helper function versionCallSiteWithCond from versionCallSite (PR #81181)
@@ -267,7 +264,6 @@ static void createRetBitCast(CallBase , Type *RetTy, CastInst **RetBitCast) { /// Is replaced by the following: /// /// cond_bb: -/// %cond = icmp eq i32 ()* %ptr, @func minglotus-6 wrote: done. https://github.com/llvm/llvm-project/pull/81181 ___ llvm-branch-commits mailing list llvm-branch-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits
[llvm-branch-commits] [llvm] [NFC][CallPromotionUtils]Extract a helper function versionCallSiteWithCond from versionCallSite (PR #81181)
https://github.com/minglotus-6 updated https://github.com/llvm/llvm-project/pull/81181 >From 7ebae253ab1808bca328453f68af2b595d07176e Mon Sep 17 00:00:00 2001 From: mingmingl Date: Thu, 8 Feb 2024 11:32:50 -0800 Subject: [PATCH 1/2] [NFC][CallPromotionUtils]Extract a helper function versionCallSiteWithCond from versionCallSite --- .../Transforms/Utils/CallPromotionUtils.cpp | 36 +++ 1 file changed, 21 insertions(+), 15 deletions(-) diff --git a/llvm/lib/Transforms/Utils/CallPromotionUtils.cpp b/llvm/lib/Transforms/Utils/CallPromotionUtils.cpp index 4e84927f1cfc90..d0cf0792eface0 100644 --- a/llvm/lib/Transforms/Utils/CallPromotionUtils.cpp +++ b/llvm/lib/Transforms/Utils/CallPromotionUtils.cpp @@ -188,10 +188,9 @@ static void createRetBitCast(CallBase , Type *RetTy, CastInst **RetBitCast) { /// Predicate and clone the given call site. /// /// This function creates an if-then-else structure at the location of the call -/// site. The "if" condition compares the call site's called value to the given -/// callee. The original call site is moved into the "else" block, and a clone -/// of the call site is placed in the "then" block. The cloned instruction is -/// returned. +/// site. The "if" condition is specified by `Cond`. +/// The original call site is moved into the "else" block, and a clone of the +/// call site is placed in the "then" block. The cloned instruction is returned. /// /// For example, the call instruction below: /// @@ -202,7 +201,6 @@ static void createRetBitCast(CallBase , Type *RetTy, CastInst **RetBitCast) { /// Is replace by the following: /// /// orig_bb: -/// %cond = icmp eq i32 ()* %ptr, @func /// br i1 %cond, %then_bb, %else_bb /// /// then_bb: @@ -232,7 +230,6 @@ static void createRetBitCast(CallBase , Type *RetTy, CastInst **RetBitCast) { /// Is replace by the following: /// /// orig_bb: -/// %cond = icmp eq i32 ()* %ptr, @func /// br i1 %cond, %then_bb, %else_bb /// /// then_bb: @@ -267,7 +264,6 @@ static void createRetBitCast(CallBase , Type *RetTy, CastInst **RetBitCast) { /// Is replaced by the following: /// /// cond_bb: -/// %cond = icmp eq i32 ()* %ptr, @func /// br i1 %cond, %then_bb, %orig_bb /// /// then_bb: @@ -280,19 +276,13 @@ static void createRetBitCast(CallBase , Type *RetTy, CastInst **RetBitCast) { /// ; The original call instruction stays in its original block. /// %t0 = musttail call i32 %ptr() /// ret %t0 -CallBase ::versionCallSite(CallBase , Value *Callee, -MDNode *BranchWeights) { +static CallBase (CallBase , Value *Cond, + MDNode *BranchWeights) { IRBuilder<> Builder(); CallBase *OrigInst = BasicBlock *OrigBlock = OrigInst->getParent(); - // Create the compare. The called value and callee must have the same type to - // be compared. - if (CB.getCalledOperand()->getType() != Callee->getType()) -Callee = Builder.CreateBitCast(Callee, CB.getCalledOperand()->getType()); - auto *Cond = Builder.CreateICmpEQ(CB.getCalledOperand(), Callee); - if (OrigInst->isMustTailCall()) { // Create an if-then structure. The original instruction stays in its block, // and a clone of the original instruction is placed in the "then" block. @@ -380,6 +370,22 @@ CallBase ::versionCallSite(CallBase , Value *Callee, return *NewInst; } +// Predicate and clone the given call site usingc condition `CB.callee == +// Callee`. See the comment `versionCallSiteWithCond` for the transformation. +CallBase ::versionCallSite(CallBase , Value *Callee, +MDNode *BranchWeights) { + + IRBuilder<> Builder(); + + // Create the compare. The called value and callee must have the same type to + // be compared. + if (CB.getCalledOperand()->getType() != Callee->getType()) +Callee = Builder.CreateBitCast(Callee, CB.getCalledOperand()->getType()); + auto *Cond = Builder.CreateICmpEQ(CB.getCalledOperand(), Callee); + + return versionCallSiteWithCond(CB, Cond, BranchWeights); +} + bool llvm::isLegalToPromote(const CallBase , Function *Callee, const char **FailureReason) { assert(!CB.getCalledFunction() && "Only indirect call sites can be promoted"); >From e774f69e9e960966399aa5569664c254ff0040bd Mon Sep 17 00:00:00 2001 From: mingmingl Date: Mon, 12 Feb 2024 15:38:07 -0800 Subject: [PATCH 2/2] resolve review feedback --- llvm/lib/Transforms/Utils/CallPromotionUtils.cpp | 9 ++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/llvm/lib/Transforms/Utils/CallPromotionUtils.cpp b/llvm/lib/Transforms/Utils/CallPromotionUtils.cpp index d0cf0792eface0..47e6d299ae8607 100644 --- a/llvm/lib/Transforms/Utils/CallPromotionUtils.cpp +++ b/llvm/lib/Transforms/Utils/CallPromotionUtils.cpp @@ -188,9 +188,9 @@ static void createRetBitCast(CallBase , Type *RetTy, CastInst **RetBitCast) { /// Predicate and
[llvm-branch-commits] [llvm] [InstrProf] Add vtables with type metadata into symtab to look it up with GUID (PR #81051)
minglotus-6 wrote: > Can you pull out the part that does the refactoring into getCanonicalName and > adds comments etc into a separate nfc patch, so that this one is just about > adding vtables? Sure! I'm thinking about doing the nfc patch into LLVM main branch given `getCanonicalName` simplifies `addFuncWithName` a little bit. But it should be a quick [click](https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/proposing-changes-to-your-work-with-pull-requests/changing-the-base-branch-of-a-pull-request) to switch the base branch to merge a PR into at any time. https://github.com/llvm/llvm-project/pull/81051 ___ llvm-branch-commits mailing list llvm-branch-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits
[llvm-branch-commits] [llvm] [CallPromotionUtils]Implement conditional indirect call promotion with vtable-based comparison (PR #81378)
https://github.com/minglotus-6 edited https://github.com/llvm/llvm-project/pull/81378 ___ llvm-branch-commits mailing list llvm-branch-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits
[llvm-branch-commits] [llvm] [CallPromotionUtils]Implement conditional indirect call promotion with vtable-based comparison (PR #81378)
https://github.com/minglotus-6 edited https://github.com/llvm/llvm-project/pull/81378 ___ llvm-branch-commits mailing list llvm-branch-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits
[llvm-branch-commits] [llvm] [CallPromotionUtils]Implement conditional indirect call promotion with vtable-based comparison (PR #81378)
https://github.com/minglotus-6 ready_for_review https://github.com/llvm/llvm-project/pull/81378 ___ llvm-branch-commits mailing list llvm-branch-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits
[llvm-branch-commits] [llvm] [CallPromotionUtils]Implement conditional indirect call promotion with vtable-based comparison (PR #81378)
https://github.com/minglotus-6 edited https://github.com/llvm/llvm-project/pull/81378 ___ llvm-branch-commits mailing list llvm-branch-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits
[llvm-branch-commits] [llvm] [CallPromotionUtils]Implement conditional indirect call promotion with vtable-based comparison (PR #81378)
https://github.com/minglotus-6 updated https://github.com/llvm/llvm-project/pull/81378 >From ac5dc1bf77b67cbf0aa5e2c8fb6a7ce0080fb501 Mon Sep 17 00:00:00 2001 From: mingmingl Date: Sat, 10 Feb 2024 12:03:25 -0800 Subject: [PATCH 1/3] [CallPromotionUtils]Implement conditional indirect call promotion with vtable-based comparison --- .../Transforms/Utils/CallPromotionUtils.h | 50 ++- .../Transforms/Utils/CallPromotionUtils.cpp | 64 - .../Utils/CallPromotionUtilsTest.cpp | 127 ++ 3 files changed, 233 insertions(+), 8 deletions(-) diff --git a/llvm/include/llvm/Transforms/Utils/CallPromotionUtils.h b/llvm/include/llvm/Transforms/Utils/CallPromotionUtils.h index fcb384ec361339..5f3a71206876c6 100644 --- a/llvm/include/llvm/Transforms/Utils/CallPromotionUtils.h +++ b/llvm/include/llvm/Transforms/Utils/CallPromotionUtils.h @@ -14,10 +14,17 @@ #ifndef LLVM_TRANSFORMS_UTILS_CALLPROMOTIONUTILS_H #define LLVM_TRANSFORMS_UTILS_CALLPROMOTIONUTILS_H +#include + +#include "llvm/ADT/ArrayRef.h" + namespace llvm { +class Constant; class CallBase; class CastInst; class Function; +class GlobalVariable; +class Instruction; class MDNode; class Value; @@ -41,7 +48,9 @@ bool isLegalToPromote(const CallBase , Function *Callee, CallBase (CallBase , Function *Callee, CastInst **RetBitCast = nullptr); -/// Promote the given indirect call site to conditionally call \p Callee. +/// Promote the given indirect call site to conditionally call \p Callee. The +/// promoted direct call instruction is predicated on `CB.getCalledOperand() == +/// Callee`. /// /// This function creates an if-then-else structure at the location of the call /// site. The original call site is moved into the "else" block. A clone of the @@ -51,6 +60,31 @@ CallBase (CallBase , Function *Callee, CallBase (CallBase , Function *Callee, MDNode *BranchWeights = nullptr); +/// This is similar to `promoteCallWithIfThenElse` except that the condition to +/// promote a virtual call is that \p VPtr is the same as any of \p +/// AddressPoints. +/// +/// This function is expected to be used on virtual calls (a subset of indirect +/// calls). \p VPtr is the virtual table address stored in the objects, and +/// \p AddressPoints contains address points of vtables to be compared with. +/// +/// It's the responsibility of caller to guarantee the transformation +/// correctness (by specifying \p VPtr and \p AddressPoints properly). +/// +/// This function doesn't sink the address-calculation instructions of indirect +/// callee to the indirect call fallback. The subsequent passes (e.g. +/// inst-combine) should sink them if possible and handle the sink of debug +/// intrinsics together. +CallBase (CallBase , Instruction *VPtr, + Function *Callee, + ArrayRef AddressPoints, + MDNode *BranchWeights); + +/// Returns a constant representing the vtable's address point specified by the +/// offset. Caller should ensure \p AddressPointOffset is valid. +Constant *getVTableAddressPointOffset(GlobalVariable *VTable, + uint32_t AddressPointOffset); + /// Try to promote (devirtualize) a virtual call on an Alloca. Return true on /// success. /// @@ -74,13 +108,17 @@ CallBase (CallBase , Function *Callee, /// bool tryPromoteCall(CallBase ); +/// Predicate and clone the given call site using the given condition. +CallBase (CallBase , Value *Cond, + MDNode *BranchWeights); + /// Predicate and clone the given call site. /// -/// This function creates an if-then-else structure at the location of the call -/// site. The "if" condition compares the call site's called value to the given -/// callee. The original call site is moved into the "else" block, and a clone -/// of the call site is placed in the "then" block. The cloned instruction is -/// returned. +/// This function creates an if-then-else structure at the location of the +/// call site. The "if" condition compares the call site's called value to +/// the given callee. The original call site is moved into the "else" block, +/// and a clone of the call site is placed in the "then" block. The cloned +/// instruction is returned. CallBase (CallBase , Value *Callee, MDNode *BranchWeights); } // end namespace llvm diff --git a/llvm/lib/Transforms/Utils/CallPromotionUtils.cpp b/llvm/lib/Transforms/Utils/CallPromotionUtils.cpp index d0cf0792eface0..ea855b9a4d8416 100644 --- a/llvm/lib/Transforms/Utils/CallPromotionUtils.cpp +++ b/llvm/lib/Transforms/Utils/CallPromotionUtils.cpp @@ -12,9 +12,11 @@ //===--===// #include "llvm/Transforms/Utils/CallPromotionUtils.h" +#include "llvm/ADT/STLExtras.h" #include "llvm/Analysis/Loads.h" #include
[llvm-branch-commits] [llvm] [CallPromotionUtils]Implement conditional indirect call promotion with vtable-based comparison (PR #81378)
https://github.com/minglotus-6 updated https://github.com/llvm/llvm-project/pull/81378 >From ac5dc1bf77b67cbf0aa5e2c8fb6a7ce0080fb501 Mon Sep 17 00:00:00 2001 From: mingmingl Date: Sat, 10 Feb 2024 12:03:25 -0800 Subject: [PATCH 1/2] [CallPromotionUtils]Implement conditional indirect call promotion with vtable-based comparison --- .../Transforms/Utils/CallPromotionUtils.h | 50 ++- .../Transforms/Utils/CallPromotionUtils.cpp | 64 - .../Utils/CallPromotionUtilsTest.cpp | 127 ++ 3 files changed, 233 insertions(+), 8 deletions(-) diff --git a/llvm/include/llvm/Transforms/Utils/CallPromotionUtils.h b/llvm/include/llvm/Transforms/Utils/CallPromotionUtils.h index fcb384ec361339..5f3a71206876c6 100644 --- a/llvm/include/llvm/Transforms/Utils/CallPromotionUtils.h +++ b/llvm/include/llvm/Transforms/Utils/CallPromotionUtils.h @@ -14,10 +14,17 @@ #ifndef LLVM_TRANSFORMS_UTILS_CALLPROMOTIONUTILS_H #define LLVM_TRANSFORMS_UTILS_CALLPROMOTIONUTILS_H +#include + +#include "llvm/ADT/ArrayRef.h" + namespace llvm { +class Constant; class CallBase; class CastInst; class Function; +class GlobalVariable; +class Instruction; class MDNode; class Value; @@ -41,7 +48,9 @@ bool isLegalToPromote(const CallBase , Function *Callee, CallBase (CallBase , Function *Callee, CastInst **RetBitCast = nullptr); -/// Promote the given indirect call site to conditionally call \p Callee. +/// Promote the given indirect call site to conditionally call \p Callee. The +/// promoted direct call instruction is predicated on `CB.getCalledOperand() == +/// Callee`. /// /// This function creates an if-then-else structure at the location of the call /// site. The original call site is moved into the "else" block. A clone of the @@ -51,6 +60,31 @@ CallBase (CallBase , Function *Callee, CallBase (CallBase , Function *Callee, MDNode *BranchWeights = nullptr); +/// This is similar to `promoteCallWithIfThenElse` except that the condition to +/// promote a virtual call is that \p VPtr is the same as any of \p +/// AddressPoints. +/// +/// This function is expected to be used on virtual calls (a subset of indirect +/// calls). \p VPtr is the virtual table address stored in the objects, and +/// \p AddressPoints contains address points of vtables to be compared with. +/// +/// It's the responsibility of caller to guarantee the transformation +/// correctness (by specifying \p VPtr and \p AddressPoints properly). +/// +/// This function doesn't sink the address-calculation instructions of indirect +/// callee to the indirect call fallback. The subsequent passes (e.g. +/// inst-combine) should sink them if possible and handle the sink of debug +/// intrinsics together. +CallBase (CallBase , Instruction *VPtr, + Function *Callee, + ArrayRef AddressPoints, + MDNode *BranchWeights); + +/// Returns a constant representing the vtable's address point specified by the +/// offset. Caller should ensure \p AddressPointOffset is valid. +Constant *getVTableAddressPointOffset(GlobalVariable *VTable, + uint32_t AddressPointOffset); + /// Try to promote (devirtualize) a virtual call on an Alloca. Return true on /// success. /// @@ -74,13 +108,17 @@ CallBase (CallBase , Function *Callee, /// bool tryPromoteCall(CallBase ); +/// Predicate and clone the given call site using the given condition. +CallBase (CallBase , Value *Cond, + MDNode *BranchWeights); + /// Predicate and clone the given call site. /// -/// This function creates an if-then-else structure at the location of the call -/// site. The "if" condition compares the call site's called value to the given -/// callee. The original call site is moved into the "else" block, and a clone -/// of the call site is placed in the "then" block. The cloned instruction is -/// returned. +/// This function creates an if-then-else structure at the location of the +/// call site. The "if" condition compares the call site's called value to +/// the given callee. The original call site is moved into the "else" block, +/// and a clone of the call site is placed in the "then" block. The cloned +/// instruction is returned. CallBase (CallBase , Value *Callee, MDNode *BranchWeights); } // end namespace llvm diff --git a/llvm/lib/Transforms/Utils/CallPromotionUtils.cpp b/llvm/lib/Transforms/Utils/CallPromotionUtils.cpp index d0cf0792eface0..ea855b9a4d8416 100644 --- a/llvm/lib/Transforms/Utils/CallPromotionUtils.cpp +++ b/llvm/lib/Transforms/Utils/CallPromotionUtils.cpp @@ -12,9 +12,11 @@ //===--===// #include "llvm/Transforms/Utils/CallPromotionUtils.h" +#include "llvm/ADT/STLExtras.h" #include "llvm/Analysis/Loads.h" #include
[llvm-branch-commits] [llvm] [NFC][CallPromotionUtils]Extract a helper function versionCallSiteWithCond from versionCallSite (PR #81181)
https://github.com/minglotus-6 edited https://github.com/llvm/llvm-project/pull/81181 ___ llvm-branch-commits mailing list llvm-branch-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits
[llvm-branch-commits] [llvm] [NFC][CallPromotionUtils]Extract a helper function versionCallSiteWithCond from versionCallSite (PR #81181)
https://github.com/minglotus-6 ready_for_review https://github.com/llvm/llvm-project/pull/81181 ___ llvm-branch-commits mailing list llvm-branch-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits
[llvm-branch-commits] [llvm] [TypeProf][IndirectCallPromotion]Implement vtable-based transformation (PR #81442)
https://github.com/minglotus-6 edited https://github.com/llvm/llvm-project/pull/81442 ___ llvm-branch-commits mailing list llvm-branch-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits
[llvm-branch-commits] [llvm] [CallPromotionUtils]Implement conditional indirect call promotion with vtable-based comparison (PR #81378)
https://github.com/minglotus-6 edited https://github.com/llvm/llvm-project/pull/81378 ___ llvm-branch-commits mailing list llvm-branch-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits
[llvm-branch-commits] [llvm] [CallPromotionUtils]Implement conditional indirect call promotion with vtable-based comparison (PR #81378)
https://github.com/minglotus-6 created https://github.com/llvm/llvm-project/pull/81378 None >From ac5dc1bf77b67cbf0aa5e2c8fb6a7ce0080fb501 Mon Sep 17 00:00:00 2001 From: mingmingl Date: Sat, 10 Feb 2024 12:03:25 -0800 Subject: [PATCH] [CallPromotionUtils]Implement conditional indirect call promotion with vtable-based comparison --- .../Transforms/Utils/CallPromotionUtils.h | 50 ++- .../Transforms/Utils/CallPromotionUtils.cpp | 64 - .../Utils/CallPromotionUtilsTest.cpp | 127 ++ 3 files changed, 233 insertions(+), 8 deletions(-) diff --git a/llvm/include/llvm/Transforms/Utils/CallPromotionUtils.h b/llvm/include/llvm/Transforms/Utils/CallPromotionUtils.h index fcb384ec361339..5f3a71206876c6 100644 --- a/llvm/include/llvm/Transforms/Utils/CallPromotionUtils.h +++ b/llvm/include/llvm/Transforms/Utils/CallPromotionUtils.h @@ -14,10 +14,17 @@ #ifndef LLVM_TRANSFORMS_UTILS_CALLPROMOTIONUTILS_H #define LLVM_TRANSFORMS_UTILS_CALLPROMOTIONUTILS_H +#include + +#include "llvm/ADT/ArrayRef.h" + namespace llvm { +class Constant; class CallBase; class CastInst; class Function; +class GlobalVariable; +class Instruction; class MDNode; class Value; @@ -41,7 +48,9 @@ bool isLegalToPromote(const CallBase , Function *Callee, CallBase (CallBase , Function *Callee, CastInst **RetBitCast = nullptr); -/// Promote the given indirect call site to conditionally call \p Callee. +/// Promote the given indirect call site to conditionally call \p Callee. The +/// promoted direct call instruction is predicated on `CB.getCalledOperand() == +/// Callee`. /// /// This function creates an if-then-else structure at the location of the call /// site. The original call site is moved into the "else" block. A clone of the @@ -51,6 +60,31 @@ CallBase (CallBase , Function *Callee, CallBase (CallBase , Function *Callee, MDNode *BranchWeights = nullptr); +/// This is similar to `promoteCallWithIfThenElse` except that the condition to +/// promote a virtual call is that \p VPtr is the same as any of \p +/// AddressPoints. +/// +/// This function is expected to be used on virtual calls (a subset of indirect +/// calls). \p VPtr is the virtual table address stored in the objects, and +/// \p AddressPoints contains address points of vtables to be compared with. +/// +/// It's the responsibility of caller to guarantee the transformation +/// correctness (by specifying \p VPtr and \p AddressPoints properly). +/// +/// This function doesn't sink the address-calculation instructions of indirect +/// callee to the indirect call fallback. The subsequent passes (e.g. +/// inst-combine) should sink them if possible and handle the sink of debug +/// intrinsics together. +CallBase (CallBase , Instruction *VPtr, + Function *Callee, + ArrayRef AddressPoints, + MDNode *BranchWeights); + +/// Returns a constant representing the vtable's address point specified by the +/// offset. Caller should ensure \p AddressPointOffset is valid. +Constant *getVTableAddressPointOffset(GlobalVariable *VTable, + uint32_t AddressPointOffset); + /// Try to promote (devirtualize) a virtual call on an Alloca. Return true on /// success. /// @@ -74,13 +108,17 @@ CallBase (CallBase , Function *Callee, /// bool tryPromoteCall(CallBase ); +/// Predicate and clone the given call site using the given condition. +CallBase (CallBase , Value *Cond, + MDNode *BranchWeights); + /// Predicate and clone the given call site. /// -/// This function creates an if-then-else structure at the location of the call -/// site. The "if" condition compares the call site's called value to the given -/// callee. The original call site is moved into the "else" block, and a clone -/// of the call site is placed in the "then" block. The cloned instruction is -/// returned. +/// This function creates an if-then-else structure at the location of the +/// call site. The "if" condition compares the call site's called value to +/// the given callee. The original call site is moved into the "else" block, +/// and a clone of the call site is placed in the "then" block. The cloned +/// instruction is returned. CallBase (CallBase , Value *Callee, MDNode *BranchWeights); } // end namespace llvm diff --git a/llvm/lib/Transforms/Utils/CallPromotionUtils.cpp b/llvm/lib/Transforms/Utils/CallPromotionUtils.cpp index d0cf0792eface0..ea855b9a4d8416 100644 --- a/llvm/lib/Transforms/Utils/CallPromotionUtils.cpp +++ b/llvm/lib/Transforms/Utils/CallPromotionUtils.cpp @@ -12,9 +12,11 @@ //===--===// #include "llvm/Transforms/Utils/CallPromotionUtils.h" +#include "llvm/ADT/STLExtras.h" #include "llvm/Analysis/Loads.h" #include
[llvm-branch-commits] [llvm] [NFC][CallPromotionUtils]Extract a helper function versionCallSiteWithCond from versionCallSite (PR #81181)
https://github.com/minglotus-6 edited https://github.com/llvm/llvm-project/pull/81181 ___ llvm-branch-commits mailing list llvm-branch-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits
[llvm-branch-commits] [llvm] [NFC][CallPromotionUtils]Extract a helper function versionCallSiteWithhCond from versionCallSite (PR #81181)
https://github.com/minglotus-6 created https://github.com/llvm/llvm-project/pull/81181 The parent patch is https://github.com/llvm/llvm-project/pull/81051 >From 7ebae253ab1808bca328453f68af2b595d07176e Mon Sep 17 00:00:00 2001 From: mingmingl Date: Thu, 8 Feb 2024 11:32:50 -0800 Subject: [PATCH] [NFC][CallPromotionUtils]Extract a helper function versionCallSiteWithCond from versionCallSite --- .../Transforms/Utils/CallPromotionUtils.cpp | 36 +++ 1 file changed, 21 insertions(+), 15 deletions(-) diff --git a/llvm/lib/Transforms/Utils/CallPromotionUtils.cpp b/llvm/lib/Transforms/Utils/CallPromotionUtils.cpp index 4e84927f1cfc90..d0cf0792eface0 100644 --- a/llvm/lib/Transforms/Utils/CallPromotionUtils.cpp +++ b/llvm/lib/Transforms/Utils/CallPromotionUtils.cpp @@ -188,10 +188,9 @@ static void createRetBitCast(CallBase , Type *RetTy, CastInst **RetBitCast) { /// Predicate and clone the given call site. /// /// This function creates an if-then-else structure at the location of the call -/// site. The "if" condition compares the call site's called value to the given -/// callee. The original call site is moved into the "else" block, and a clone -/// of the call site is placed in the "then" block. The cloned instruction is -/// returned. +/// site. The "if" condition is specified by `Cond`. +/// The original call site is moved into the "else" block, and a clone of the +/// call site is placed in the "then" block. The cloned instruction is returned. /// /// For example, the call instruction below: /// @@ -202,7 +201,6 @@ static void createRetBitCast(CallBase , Type *RetTy, CastInst **RetBitCast) { /// Is replace by the following: /// /// orig_bb: -/// %cond = icmp eq i32 ()* %ptr, @func /// br i1 %cond, %then_bb, %else_bb /// /// then_bb: @@ -232,7 +230,6 @@ static void createRetBitCast(CallBase , Type *RetTy, CastInst **RetBitCast) { /// Is replace by the following: /// /// orig_bb: -/// %cond = icmp eq i32 ()* %ptr, @func /// br i1 %cond, %then_bb, %else_bb /// /// then_bb: @@ -267,7 +264,6 @@ static void createRetBitCast(CallBase , Type *RetTy, CastInst **RetBitCast) { /// Is replaced by the following: /// /// cond_bb: -/// %cond = icmp eq i32 ()* %ptr, @func /// br i1 %cond, %then_bb, %orig_bb /// /// then_bb: @@ -280,19 +276,13 @@ static void createRetBitCast(CallBase , Type *RetTy, CastInst **RetBitCast) { /// ; The original call instruction stays in its original block. /// %t0 = musttail call i32 %ptr() /// ret %t0 -CallBase ::versionCallSite(CallBase , Value *Callee, -MDNode *BranchWeights) { +static CallBase (CallBase , Value *Cond, + MDNode *BranchWeights) { IRBuilder<> Builder(); CallBase *OrigInst = BasicBlock *OrigBlock = OrigInst->getParent(); - // Create the compare. The called value and callee must have the same type to - // be compared. - if (CB.getCalledOperand()->getType() != Callee->getType()) -Callee = Builder.CreateBitCast(Callee, CB.getCalledOperand()->getType()); - auto *Cond = Builder.CreateICmpEQ(CB.getCalledOperand(), Callee); - if (OrigInst->isMustTailCall()) { // Create an if-then structure. The original instruction stays in its block, // and a clone of the original instruction is placed in the "then" block. @@ -380,6 +370,22 @@ CallBase ::versionCallSite(CallBase , Value *Callee, return *NewInst; } +// Predicate and clone the given call site usingc condition `CB.callee == +// Callee`. See the comment `versionCallSiteWithCond` for the transformation. +CallBase ::versionCallSite(CallBase , Value *Callee, +MDNode *BranchWeights) { + + IRBuilder<> Builder(); + + // Create the compare. The called value and callee must have the same type to + // be compared. + if (CB.getCalledOperand()->getType() != Callee->getType()) +Callee = Builder.CreateBitCast(Callee, CB.getCalledOperand()->getType()); + auto *Cond = Builder.CreateICmpEQ(CB.getCalledOperand(), Callee); + + return versionCallSiteWithCond(CB, Cond, BranchWeights); +} + bool llvm::isLegalToPromote(const CallBase , Function *Callee, const char **FailureReason) { assert(!CB.getCalledFunction() && "Only indirect call sites can be promoted"); ___ llvm-branch-commits mailing list llvm-branch-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits
[llvm-branch-commits] [llvm] [InstrProf] Add vtables with type metadata into symtab to look it up with GUID (PR #81051)
minglotus-6 wrote: > > Whoops! Sorry! Please don't review this. > > Something goes wrong with my fixup. > > Let me know when it is ready for review. Thanks! This is ready for review now. * I tried to merge the [nfc](https://github.com/llvm/llvm-project/pull/81054) such that the diff of this PR doesn't show the NFC diff. I *think* what I did wrong is to run `git merge main` on the source branch `users/minglotus-6/spr/instrprof` of this PR, that way the fixup commit caused the mess. * I ended up fixing by running `git checkout ` from remote, and then `git checkout -b ` and apply the intended diff in ``. However this comes with a force push which overwrite the commit history in the PR. Not 100% sure but I *think* I should create `` on top of `` rather than from `` in the first place in order to preserve commit history in the future. https://github.com/llvm/llvm-project/pull/81051 ___ llvm-branch-commits mailing list llvm-branch-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits
[llvm-branch-commits] [llvm] [InstrProf] Add vtables with type metadata into symtab to look it up with GUID (PR #81051)
https://github.com/minglotus-6 ready_for_review https://github.com/llvm/llvm-project/pull/81051 ___ llvm-branch-commits mailing list llvm-branch-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits
[llvm-branch-commits] [llvm] [InstrProf] Add vtables with type metadata into symtab to look it up with GUID (PR #81051)
https://github.com/minglotus-6 updated https://github.com/llvm/llvm-project/pull/81051 >From 66dbbfef52bdc092cbd4ed619bba38c003f6063d Mon Sep 17 00:00:00 2001 From: mingmingl Date: Thu, 8 Feb 2024 09:07:27 -0800 Subject: [PATCH] [InstrProf] Add vtables with type metadata into symtab to look it up with GUID --- llvm/include/llvm/ProfileData/InstrProf.h| 19 + llvm/lib/ProfileData/InstrProf.cpp | 87 ++-- llvm/unittests/ProfileData/InstrProfTest.cpp | 55 + 3 files changed, 138 insertions(+), 23 deletions(-) diff --git a/llvm/include/llvm/ProfileData/InstrProf.h b/llvm/include/llvm/ProfileData/InstrProf.h index 53108a093bf4dd..6e799cf8aa273e 100644 --- a/llvm/include/llvm/ProfileData/InstrProf.h +++ b/llvm/include/llvm/ProfileData/InstrProf.h @@ -487,8 +487,25 @@ class InstrProfSymtab { return "** External Symbol **"; } + // Returns the canonical name of the given PGOName by stripping the names + // suffixes that begins with ".". If MayHaveUniqueSuffix is true, ".__uniq." + // suffix is kept in the canonical name. + StringRef getCanonicalName(StringRef PGOName, bool MayHaveUniqueSuffix); + + // Add the function into the symbol table, by creating the following + // map entries: + // - + // - + // - + // - + // - (instrprof_error::malformed, @@ -665,6 +683,7 @@ void InstrProfSymtab::finalizeSymtab() { if (Sorted) return; llvm::sort(MD5NameMap, less_first()); + llvm::sort(MD5VTableMap, less_first()); llvm::sort(MD5FuncMap, less_first()); llvm::sort(AddrToMD5Map, less_first()); AddrToMD5Map.erase(std::unique(AddrToMD5Map.begin(), AddrToMD5Map.end()), diff --git a/llvm/lib/ProfileData/InstrProf.cpp b/llvm/lib/ProfileData/InstrProf.cpp index 9ebcba10c860ff..a09a2bb0ba77cb 100644 --- a/llvm/lib/ProfileData/InstrProf.cpp +++ b/llvm/lib/ProfileData/InstrProf.cpp @@ -480,7 +480,9 @@ Error InstrProfSymtab::create(Module , bool InLTO) { Types.clear(); G.getMetadata(LLVMContext::MD_type, Types); if (!Types.empty()) { - MD5VTableMap.emplace_back(G.getGUID(), ); + if (Error E = addVTableWithName( + G, getIRPGOObjectName(G, InLTO, /* PGONameMetadata */ nullptr))) +return E; } } Sorted = false; @@ -488,6 +490,30 @@ Error InstrProfSymtab::create(Module , bool InLTO) { return Error::success(); } +Error InstrProfSymtab::addVTableWithName(GlobalVariable , + StringRef VTablePGOName) { + if (Error E = addVTableName(VTablePGOName)) +return E; + + MD5VTableMap.emplace_back(GlobalValue::getGUID(VTablePGOName), ); + + // NOTE: `-funique-internal-linkage-names` doesn't uniqufy vtables, so no + // need to check ".__uniq." + + // If a local-linkage vtable is promoted to have external linkage in ThinLTO, + // it will have `.llvm.` in its name. Use the name before externalization. + StringRef CanonicalName = + getCanonicalName(VTablePGOName, /* MayHaveUniqueSuffix= */ false); + if (CanonicalName != VTablePGOName) { +if (Error E = addVTableName(CanonicalName)) + return E; + +MD5VTableMap.emplace_back(GlobalValue::getGUID(CanonicalName), ); + } + + return Error::success(); +} + /// \c NameStrings is a string composed of one of more possibly encoded /// sub-strings. The substrings are separated by 0 or more zero bytes. This /// method decodes the string and calls `NameCallback` for each substring. @@ -560,35 +586,50 @@ Error InstrProfSymtab::initVTableNamesFromCompressedStrings( std::bind(::addVTableName, this, std::placeholders::_1)); } -Error InstrProfSymtab::addFuncWithName(Function , StringRef PGOFuncName) { - if (Error E = addFuncName(PGOFuncName)) -return E; - MD5FuncMap.emplace_back(Function::getGUID(PGOFuncName), ); +StringRef InstrProfSymtab::getCanonicalName(StringRef PGOName, +bool MayHaveUniqueSuffix) { + size_t pos = 0; // In ThinLTO, local function may have been promoted to global and have // suffix ".llvm." added to the function name. We need to add the // stripped function name to the symbol table so that we can find a match // from profile. // - // We may have other suffixes similar as ".llvm." which are needed to - // be stripped before the matching, but ".__uniq." suffix which is used - // to differentiate internal linkage functions in different modules - // should be kept. Now this is the only suffix with the pattern ".xxx" - // which is kept before matching. - const std::string UniqSuffix = ".__uniq."; - auto pos = PGOFuncName.find(UniqSuffix); - // Search '.' after ".__uniq." if ".__uniq." exists, otherwise - // search '.' from the beginning. - if (pos != std::string::npos) -pos += UniqSuffix.length(); - else -pos = 0; - pos = PGOFuncName.find('.', pos); - if (pos != std::string::npos && pos != 0) { -StringRef OtherFuncName = PGOFuncName.substr(0, pos); -if (Error E =
[llvm-branch-commits] [llvm] [InstrProf] Add vtables with type metadata into symtab to look it up with GUID (PR #81051)
https://github.com/minglotus-6 updated https://github.com/llvm/llvm-project/pull/81051 >From 58f8fdbc9d2862a2579aece6c313bd19b4329e34 Mon Sep 17 00:00:00 2001 From: mingmingl Date: Wed, 7 Feb 2024 23:07:36 -0800 Subject: [PATCH] [InstrProf] Add vtables with type metadata into symtab to look it up with GUID --- llvm/include/llvm/ProfileData/InstrProf.h| 19 llvm/lib/ProfileData/InstrProf.cpp | 94 ++-- llvm/unittests/ProfileData/InstrProfTest.cpp | 47 ++ 3 files changed, 132 insertions(+), 28 deletions(-) diff --git a/llvm/include/llvm/ProfileData/InstrProf.h b/llvm/include/llvm/ProfileData/InstrProf.h index 53108a093bf4d..6e799cf8aa273 100644 --- a/llvm/include/llvm/ProfileData/InstrProf.h +++ b/llvm/include/llvm/ProfileData/InstrProf.h @@ -487,8 +487,25 @@ class InstrProfSymtab { return "** External Symbol **"; } + // Returns the canonical name of the given PGOName by stripping the names + // suffixes that begins with ".". If MayHaveUniqueSuffix is true, ".__uniq." + // suffix is kept in the canonical name. + StringRef getCanonicalName(StringRef PGOName, bool MayHaveUniqueSuffix); + + // Add the function into the symbol table, by creating the following + // map entries: + // - + // - + // - + // - + // - (instrprof_error::malformed, @@ -665,6 +683,7 @@ void InstrProfSymtab::finalizeSymtab() { if (Sorted) return; llvm::sort(MD5NameMap, less_first()); + llvm::sort(MD5VTableMap, less_first()); llvm::sort(MD5FuncMap, less_first()); llvm::sort(AddrToMD5Map, less_first()); AddrToMD5Map.erase(std::unique(AddrToMD5Map.begin(), AddrToMD5Map.end()), diff --git a/llvm/lib/ProfileData/InstrProf.cpp b/llvm/lib/ProfileData/InstrProf.cpp index 9ebcba10c860f..edd6bf2c2971d 100644 --- a/llvm/lib/ProfileData/InstrProf.cpp +++ b/llvm/lib/ProfileData/InstrProf.cpp @@ -480,7 +480,9 @@ Error InstrProfSymtab::create(Module , bool InLTO) { Types.clear(); G.getMetadata(LLVMContext::MD_type, Types); if (!Types.empty()) { - MD5VTableMap.emplace_back(G.getGUID(), ); + if (Error E = addVTableWithName( + G, getIRPGOObjectName(G, InLTO, /* PGONameMetadata */ nullptr))) +return E; } } Sorted = false; @@ -488,6 +490,30 @@ Error InstrProfSymtab::create(Module , bool InLTO) { return Error::success(); } +Error InstrProfSymtab::addVTableWithName(GlobalVariable , + StringRef VTablePGOName) { + if (Error E = addVTableName(VTablePGOName)) +return E; + + MD5VTableMap.emplace_back(GlobalValue::getGUID(VTablePGOName), ); + + // NOTE: `-funique-internal-linkage-names` doesn't uniqufy vtables, so no + // need to check ".__uniq." + + // If a local-linkage vtable is promoted to have external linkage in ThinLTO, + // it will have `.llvm.` in its name. Use the name before externalization. + StringRef CanonicalName = + getCanonicalName(VTablePGOName, /* MayHaveUniqueSuffix= */ false); + if (CanonicalName != VTablePGOName) { +if (Error E = addVTableName(CanonicalName)) + return E; + +MD5VTableMap.emplace_back(GlobalValue::getGUID(CanonicalName), ); + } + + return Error::success(); +} + /// \c NameStrings is a string composed of one of more possibly encoded /// sub-strings. The substrings are separated by 0 or more zero bytes. This /// method decodes the string and calls `NameCallback` for each substring. @@ -560,35 +586,50 @@ Error InstrProfSymtab::initVTableNamesFromCompressedStrings( std::bind(::addVTableName, this, std::placeholders::_1)); } -Error InstrProfSymtab::addFuncWithName(Function , StringRef PGOFuncName) { - if (Error E = addFuncName(PGOFuncName)) -return E; - MD5FuncMap.emplace_back(Function::getGUID(PGOFuncName), ); +StringRef InstrProfSymtab::getCanonicalName(StringRef PGOName, +bool MayHaveUniqueSuffix) { + size_t pos = 0; // In ThinLTO, local function may have been promoted to global and have // suffix ".llvm." added to the function name. We need to add the // stripped function name to the symbol table so that we can find a match // from profile. // - // We may have other suffixes similar as ".llvm." which are needed to - // be stripped before the matching, but ".__uniq." suffix which is used - // to differentiate internal linkage functions in different modules - // should be kept. Now this is the only suffix with the pattern ".xxx" - // which is kept before matching. - const std::string UniqSuffix = ".__uniq."; - auto pos = PGOFuncName.find(UniqSuffix); - // Search '.' after ".__uniq." if ".__uniq." exists, otherwise - // search '.' from the beginning. - if (pos != std::string::npos) -pos += UniqSuffix.length(); - else -pos = 0; - pos = PGOFuncName.find('.', pos); - if (pos != std::string::npos && pos != 0) { -StringRef OtherFuncName = PGOFuncName.substr(0, pos); -if (Error E =
[llvm-branch-commits] [llvm] [InstrProf] Add vtables with type metadata into symtab to look it up with GUID (PR #81051)
https://github.com/minglotus-6 updated https://github.com/llvm/llvm-project/pull/81051 >From a3cb7002be9d7efe9f7678911da9dd68b561b785 Mon Sep 17 00:00:00 2001 From: mingmingl Date: Wed, 7 Feb 2024 22:18:50 -0800 Subject: [PATCH] fixup --- llvm/include/llvm/ProfileData/InstrProf.h| 11 + llvm/lib/ProfileData/InstrProf.cpp | 42 ++-- llvm/tools/llvm-profdata/llvm-profdata.cpp | 2 +- llvm/unittests/ProfileData/InstrProfTest.cpp | 10 ++--- 4 files changed, 27 insertions(+), 38 deletions(-) diff --git a/llvm/include/llvm/ProfileData/InstrProf.h b/llvm/include/llvm/ProfileData/InstrProf.h index d5d3c7f6b4b34..6e799cf8aa273 100644 --- a/llvm/include/llvm/ProfileData/InstrProf.h +++ b/llvm/include/llvm/ProfileData/InstrProf.h @@ -198,18 +198,9 @@ std::string getPGOFuncName(StringRef RawFuncName, /// called from LTO optimization passes. std::string getIRPGOFuncName(const Function , bool InLTO = false); -/// Returns the PGO object name. This function has some special handling -/// when called in LTO optimization: -/// - In LTO mode, returns the name in `PGONameMetadata` if exists, otherwise -/// returns the IRPGO name as if the GO has non-local linkage. -/// - In non-LTO mode, returns the IRPGO name as it is. -/// More rationale in the comment of function implementation. -std::string getIRPGOObjectName(const GlobalObject , bool InLTO = false, - MDNode *PGONameMetadata = nullptr); - /// \return the filename and the function name parsed from the output of /// \c getIRPGOFuncName() -std::pair getParsedIRPGOFuncName(StringRef IRPGOFuncName); +std::pair getParsedIRPGOName(StringRef IRPGOName); /// Return the name of the global variable used to store a function /// name in PGO instrumentation. \c FuncName is the IRPGO function name diff --git a/llvm/lib/ProfileData/InstrProf.cpp b/llvm/lib/ProfileData/InstrProf.cpp index 3b05faf0cc5d3..c280454fdeb1e 100644 --- a/llvm/lib/ProfileData/InstrProf.cpp +++ b/llvm/lib/ProfileData/InstrProf.cpp @@ -325,20 +325,21 @@ static std::optional lookupPGONameFromMetadata(MDNode *MD) { return {}; } -std::string getIRPGOObjectName(const GlobalObject , bool InLTO, - MDNode *PGONameMetadata) { - // The following only applies when calling in - // LTO passes (when \c InLTO is true): LTO's internalization privatizes many - // global linkage symbols. This happens after value profile annotation, but - // those internal linkage functions should not have a source prefix. - // Additionally, for ThinLTO mode, exported internal functions are promoted - // and renamed. We need to ensure that the original internal PGO name is - // used when computing the GUID that is compared against the profiled GUIDs. - // To differentiate compiler generated internal symbols from original ones, - // PGOFuncName meta data are created and attached to the original internal - // symbols in the value profile annotation step - // (PGOUseFunc::annotateIndirectCallSites). If a symbol does not have the meta - // data, its original linkage must be non-internal. +// Returns the PGO object name. This function has some special handling +// when called in LTO optimization. The following only applies when calling in +// LTO passes (when \c InLTO is true): LTO's internalization privatizes many +// global linkage symbols. This happens after value profile annotation, but +// those internal linkage functions should not have a source prefix. +// Additionally, for ThinLTO mode, exported internal functions are promoted +// and renamed. We need to ensure that the original internal PGO name is +// used when computing the GUID that is compared against the profiled GUIDs. +// To differentiate compiler generated internal symbols from original ones, +// PGOFuncName meta data are created and attached to the original internal +// symbols in the value profile annotation step +// (PGOUseFunc::annotateIndirectCallSites). If a symbol does not have the meta +// data, its original linkage must be non-internal. +static std::string getIRPGOObjectName(const GlobalObject , bool InLTO, + MDNode *PGONameMetadata) { if (!InLTO) { auto FileName = getStrippedSourceFileName(GO); return getIRPGONameForGlobalObject(GO, GO.getLinkage(), FileName); @@ -390,13 +391,12 @@ std::string getPGOName(const GlobalVariable , bool InLTO) { return getIRPGOObjectName(V, InLTO, nullptr /* PGONameMetadata */); } -// See getIRPGOFuncName() for a discription of the format. -std::pair -getParsedIRPGOFuncName(StringRef IRPGOFuncName) { - auto [FileName, FuncName] = IRPGOFuncName.split(';'); - if (FuncName.empty()) -return std::make_pair(StringRef(), IRPGOFuncName); - return std::make_pair(FileName, FuncName); +// See getIRPGOObjectName() for a discription of the format. +std::pair getParsedIRPGOName(StringRef IRPGOName) { + auto [FileName, MangledName] =
[llvm-branch-commits] [InstrProf] Add vtables with type metadata into symtab to look it up with GUID (PR #81051)
https://github.com/minglotus-6 updated https://github.com/llvm/llvm-project/pull/81051 ___ llvm-branch-commits mailing list llvm-branch-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits
[llvm-branch-commits] [llvm] [NFC][IndirectCallProm] Refactor function-based conditional devirtualization and indirect call value profile update into one helper function (PR #80762)
https://github.com/minglotus-6 edited https://github.com/llvm/llvm-project/pull/80762 ___ llvm-branch-commits mailing list llvm-branch-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits
[llvm-branch-commits] [llvm] [NFC][IndirectCallProm] Refactor function-based conditional devirtualization and indirect call value profile update into one helper function (PR #80762)
https://github.com/minglotus-6 edited https://github.com/llvm/llvm-project/pull/80762 ___ llvm-branch-commits mailing list llvm-branch-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits
[llvm-branch-commits] [NFC]Extract the heuristic to find vtable for an indirect call into a helper function (PR #81024)
https://github.com/minglotus-6 ready_for_review https://github.com/llvm/llvm-project/pull/81024 ___ llvm-branch-commits mailing list llvm-branch-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits
[llvm-branch-commits] [llvm] [NFC] (PR #80762)
https://github.com/minglotus-6 ready_for_review https://github.com/llvm/llvm-project/pull/80762 ___ llvm-branch-commits mailing list llvm-branch-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits
[llvm-branch-commits] [llvm] [NFC] (PR #80762)
@@ -287,7 +290,18 @@ uint32_t IndirectCallPromoter::tryToPromote( NumOfPGOICallPromotion++; NumPromoted++; } - return NumPromoted; + + const bool Changed = (NumPromoted != 0); minglotus-6 wrote: done. https://github.com/llvm/llvm-project/pull/80762 ___ llvm-branch-commits mailing list llvm-branch-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits
[llvm-branch-commits] [llvm] [NFC] (PR #80762)
@@ -1260,6 +1260,8 @@ void annotateValueSite(Module , Instruction , ArrayRef VDs, uint64_t Sum, InstrProfValueKind ValueKind, uint32_t MaxMDCount) { + if (VDs.empty()) minglotus-6 wrote: yes, the check `NumPromoted == NumVals` is removed so `NumVals` doesn't need to be passed as an argument to `tryToPromoteWithFuncCmp`. - `NumVals == ICallProfDataRef.size()` is [always true](https://github.com/llvm/llvm-project/blob/0d7f232baf6103529844c8977324bd45b21ad923/llvm/lib/Analysis/IndirectCallPromotionAnalysis.cpp#L98), and `NumPromoted <= NumVals` is also true, so `ICallProfDataRef.slice(NumPromoted)` still returns a valid ArrayRef after the check is removed. On a second thought, when `if(TotalCount != 0)` is true, it's also guaranteed `ICallProfDataRef.slice(NumPromoted)` is not empty so that `ICallProfDataRef.slice(NumPromoted)` returns a valid ArrayRef. Added an assert (`assert(NumPromoted <= ICallProfDataRef.size()`) just to make the condition that `.slice` relies on more explicit. https://github.com/llvm/llvm-project/pull/80762 ___ llvm-branch-commits mailing list llvm-branch-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits
[llvm-branch-commits] [llvm] [NFC] (PR #80762)
https://github.com/minglotus-6 updated https://github.com/llvm/llvm-project/pull/80762 >From 2a03c530e775faa79d2e800a38a4228b38dfea35 Mon Sep 17 00:00:00 2001 From: mingmingl Date: Wed, 7 Feb 2024 18:20:51 -0800 Subject: [PATCH] resolve review feedback Created using spr 1.3.4 --- .../Instrumentation/IndirectCallPromotion.cpp | 20 +++ 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/llvm/lib/Transforms/Instrumentation/IndirectCallPromotion.cpp b/llvm/lib/Transforms/Instrumentation/IndirectCallPromotion.cpp index 6a44a32bb34dc9..23a7c6a20aecbc 100644 --- a/llvm/lib/Transforms/Instrumentation/IndirectCallPromotion.cpp +++ b/llvm/lib/Transforms/Instrumentation/IndirectCallPromotion.cpp @@ -291,17 +291,21 @@ bool IndirectCallPromoter::tryToPromoteWithFuncCmp( NumPromoted++; } - const bool Changed = (NumPromoted != 0); + if (NumPromoted == 0) +return false; - if (Changed) { -CB.setMetadata(LLVMContext::MD_prof, nullptr); + // Adjust the MD.prof metadata. First delete the old one. + CB.setMetadata(LLVMContext::MD_prof, nullptr); -if (TotalCount != 0) - annotateValueSite(*F.getParent(), CB, ICallProfDataRef.slice(NumPromoted), -TotalCount, IPVK_IndirectCallTarget, NumCandidates); - } + assert(NumPromoted <= ICallProfDataRef.size() && + "Number of promoted functions should not be greater than the number " + "of values in profile metadata"); + // Annotate the remaining value profiles if counter is not zero. + if (TotalCount != 0) +annotateValueSite(*F.getParent(), CB, ICallProfDataRef.slice(NumPromoted), + TotalCount, IPVK_IndirectCallTarget, NumCandidates); - return Changed; + return true; } // Traverse all the indirect-call callsite and get the value profile ___ llvm-branch-commits mailing list llvm-branch-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits
[llvm-branch-commits] [llvm] [NFC] (PR #80762)
https://github.com/minglotus-6 edited https://github.com/llvm/llvm-project/pull/80762 ___ llvm-branch-commits mailing list llvm-branch-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits
[llvm-branch-commits] [InstrProf] Add vtables with type metadata into symtab to look it up with GUID (PR #81051)
https://github.com/minglotus-6 ready_for_review https://github.com/llvm/llvm-project/pull/81051 ___ llvm-branch-commits mailing list llvm-branch-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits
[llvm-branch-commits] [InstrProf] Add vtables with type metadata into symtab to look it up with GUID (PR #81051)
@@ -1628,6 +1664,17 @@ TEST(SymtabTest, instr_prof_symtab_module_test) { EXPECT_EQ(StringRef(PGOName), PGOFuncName); EXPECT_THAT(PGOFuncName.str(), EndsWith(Funcs[I].str())); } + + StringRef VTables[] = {"ExternalGV", "PrivateGV"}; + for (size_t I = 0; I < std::size(VTables); I++) { +GlobalVariable *GV = +M->getGlobalVariable(VTables[I], /* AllowInternal=*/true); + +std::string IRPGOName = getIRPGOObjectName(*GV); +EXPECT_EQ(IRPGOName, ProfSymtab.getFuncOrVarName( + IndexedInstrProf::ComputeHash(IRPGOName))); +EXPECT_EQ(VTables[I], getParsedIRPGOFuncName(IRPGOName).second); minglotus-6 wrote: An NFC patch (https://github.com/llvm/llvm-project/pull/81054) was created to generalize the function name. https://github.com/llvm/llvm-project/pull/81051 ___ llvm-branch-commits mailing list llvm-branch-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits
[llvm-branch-commits] [NFC][IndirectCallProm] Refactor function-based conditional devirtualization and indirect call value profile update into one helper function (PR #80762)
https://github.com/minglotus-6 edited https://github.com/llvm/llvm-project/pull/80762 ___ llvm-branch-commits mailing list llvm-branch-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits
[llvm-branch-commits] [NFC]Extract the heuristic to find vtable for an indirect call into a helper function (PR #81024)
https://github.com/minglotus-6 edited https://github.com/llvm/llvm-project/pull/81024 ___ llvm-branch-commits mailing list llvm-branch-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits
[llvm-branch-commits] [NFC]Extract the heuristic to find vtable for an indirect call into a helper function (PR #81024)
https://github.com/minglotus-6 edited https://github.com/llvm/llvm-project/pull/81024 ___ llvm-branch-commits mailing list llvm-branch-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits
[llvm-branch-commits] [NFC][IndirectCallProm] Refactor function-based conditional devirtualization and indirect call value profile update into one helper function (PR #80762)
https://github.com/minglotus-6 edited https://github.com/llvm/llvm-project/pull/80762 ___ llvm-branch-commits mailing list llvm-branch-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits
[llvm-branch-commits] [InstrProf] Add vtables with type metadata into symtab to look it up with GUID (PR #81051)
https://github.com/minglotus-6 edited https://github.com/llvm/llvm-project/pull/81051 ___ llvm-branch-commits mailing list llvm-branch-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits
[llvm-branch-commits] [InstrProf] Add vtables with type metadata into symtab to look it up with GUID (PR #81051)
https://github.com/minglotus-6 edited https://github.com/llvm/llvm-project/pull/81051 ___ llvm-branch-commits mailing list llvm-branch-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits