Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:44:40

0001 //===- llvm/Transforms/IPO/FunctionImport.h - ThinLTO importing -*- C++ -*-===//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //
0007 //===----------------------------------------------------------------------===//
0008 
0009 #ifndef LLVM_TRANSFORMS_IPO_FUNCTIONIMPORT_H
0010 #define LLVM_TRANSFORMS_IPO_FUNCTIONIMPORT_H
0011 
0012 #include "llvm/ADT/DenseSet.h"
0013 #include "llvm/ADT/MapVector.h"
0014 #include "llvm/ADT/StringRef.h"
0015 #include "llvm/IR/GlobalValue.h"
0016 #include "llvm/IR/ModuleSummaryIndex.h"
0017 #include "llvm/IR/PassManager.h"
0018 #include "llvm/Support/Error.h"
0019 #include <functional>
0020 #include <memory>
0021 #include <system_error>
0022 #include <utility>
0023 
0024 namespace llvm {
0025 
0026 class Module;
0027 
0028 /// The function importer is automatically importing function from other modules
0029 /// based on the provided summary informations.
0030 class FunctionImporter {
0031 public:
0032   /// The different reasons selectCallee will chose not to import a
0033   /// candidate.
0034   enum class ImportFailureReason {
0035     None,
0036     // We can encounter a global variable instead of a function in rare
0037     // situations with SamplePGO. See comments where this failure type is
0038     // set for more details.
0039     GlobalVar,
0040     // Found to be globally dead, so we don't bother importing.
0041     NotLive,
0042     // Instruction count over the current threshold.
0043     TooLarge,
0044     // Don't import something with interposable linkage as we can't inline it
0045     // anyway.
0046     InterposableLinkage,
0047     // Generally we won't end up failing due to this reason, as we expect
0048     // to find at least one summary for the GUID that is global or a local
0049     // in the referenced module for direct calls.
0050     LocalLinkageNotInModule,
0051     // This corresponds to the NotEligibleToImport being set on the summary,
0052     // which can happen in a few different cases (e.g. local that can't be
0053     // renamed or promoted because it is referenced on a llvm*.used variable).
0054     NotEligible,
0055     // This corresponds to NoInline being set on the function summary,
0056     // which will happen if it is known that the inliner will not be able
0057     // to inline the function (e.g. it is marked with a NoInline attribute).
0058     NoInline
0059   };
0060 
0061   /// Information optionally tracked for candidates the importer decided
0062   /// not to import. Used for optional stat printing.
0063   struct ImportFailureInfo {
0064     // The ValueInfo corresponding to the candidate. We save an index hash
0065     // table lookup for each GUID by stashing this here.
0066     ValueInfo VI;
0067     // The maximum call edge hotness for all failed imports of this candidate.
0068     CalleeInfo::HotnessType MaxHotness;
0069     // most recent reason for failing to import (doesn't necessarily correspond
0070     // to the attempt with the maximum hotness).
0071     ImportFailureReason Reason;
0072     // The number of times we tried to import candidate but failed.
0073     unsigned Attempts;
0074     ImportFailureInfo(ValueInfo VI, CalleeInfo::HotnessType MaxHotness,
0075                       ImportFailureReason Reason, unsigned Attempts)
0076         : VI(VI), MaxHotness(MaxHotness), Reason(Reason), Attempts(Attempts) {}
0077   };
0078 
0079   /// Map of callee GUID considered for import into a given module to a pair
0080   /// consisting of the largest threshold applied when deciding whether to
0081   /// import it and, if we decided to import, a pointer to the summary instance
0082   /// imported. If we decided not to import, the summary will be nullptr.
0083   using ImportThresholdsTy =
0084       DenseMap<GlobalValue::GUID,
0085                std::tuple<unsigned, const GlobalValueSummary *,
0086                           std::unique_ptr<ImportFailureInfo>>>;
0087 
0088   // Issues import IDs.  Each ID uniquely corresponds to a tuple of
0089   // (FromModule, GUID, Definition/Declaration).
0090   //
0091   // The import IDs make the import list space efficient by referring to each
0092   // import with a 32-bit integer ID while maintaining a central table that maps
0093   // those integer IDs to tuples of (FromModule, GUID, Def/Decl).
0094   //
0095   // In one large application, a pair of (FromModule, GUID) is mentioned in
0096   // import lists more than 50 times on average across all destination modules.
0097   // Mentioning the 32-byte tuple:
0098   //
0099   // std::tuple<StringRef, GlobalValue::GUID, GlobalValueSummary::ImportKind>
0100   //
0101   // 50 times by value in various import lists would be costly.  We can reduce
0102   // the memory footprint of import lists by placing one copy in a central table
0103   // and referring to it with 32-bit integer IDs.
0104   //
0105   // To save space within the central table, we only store pairs of
0106   // (FromModule, GUID) in the central table.  In the actual 32-bit integer ID,
0107   // the top 31 bits index into the central table while the bottom 1 bit
0108   // indicates whether an ID is for GlobalValueSummary::Declaration or
0109   // GlobalValueSummary::Definition.
0110   class ImportIDTable {
0111   public:
0112     using ImportIDTy = uint32_t;
0113 
0114     ImportIDTable() = default;
0115 
0116     // Something is wrong with the application logic if we need to make a copy
0117     // of this and potentially make a fork.
0118     ImportIDTable(const ImportIDTable &) = delete;
0119     ImportIDTable &operator=(const ImportIDTable &) = delete;
0120 
0121     // Create a pair of import IDs [Def, Decl] for a given pair of FromModule
0122     // and GUID.
0123     std::pair<ImportIDTy, ImportIDTy> createImportIDs(StringRef FromModule,
0124                                                       GlobalValue::GUID GUID) {
0125       auto Key = std::make_pair(FromModule, GUID);
0126       auto InsertResult = TheTable.try_emplace(Key, TheTable.size());
0127       return makeIDPair(InsertResult.first->second);
0128     }
0129 
0130     // Get a pair of previously created import IDs [Def, Decl] for a given pair
0131     // of FromModule and GUID.  Returns std::nullopt if not available.
0132     std::optional<std::pair<ImportIDTy, ImportIDTy>>
0133     getImportIDs(StringRef FromModule, GlobalValue::GUID GUID) {
0134       auto Key = std::make_pair(FromModule, GUID);
0135       auto It = TheTable.find(Key);
0136       if (It != TheTable.end())
0137         return makeIDPair(It->second);
0138       return std::nullopt;
0139     }
0140 
0141     // Return a tuple of [FromModule, GUID, Def/Decl] that a given ImportID
0142     // corresponds to.
0143     std::tuple<StringRef, GlobalValue::GUID, GlobalValueSummary::ImportKind>
0144     lookup(ImportIDTy ImportID) const {
0145       GlobalValueSummary::ImportKind Kind =
0146           (ImportID & 1) ? GlobalValueSummary::Declaration
0147                          : GlobalValueSummary::Definition;
0148       auto It = TheTable.begin() + (ImportID >> 1);
0149       StringRef FromModule = It->first.first;
0150       GlobalValue::GUID GUID = It->first.second;
0151       return std::make_tuple(FromModule, GUID, Kind);
0152     }
0153 
0154     // The same as lookup above.  Useful for map_iterator.
0155     std::tuple<StringRef, GlobalValue::GUID, GlobalValueSummary::ImportKind>
0156     operator()(ImportIDTable::ImportIDTy ImportID) const {
0157       return lookup(ImportID);
0158     }
0159 
0160   private:
0161     // Make a pair of import IDs [Def, Decl] from an index into TheTable.
0162     static std::pair<ImportIDTy, ImportIDTy> makeIDPair(ImportIDTy Index) {
0163       ImportIDTy Def = Index << 1;
0164       ImportIDTy Decl = Def | 1;
0165       return std::make_pair(Def, Decl);
0166     }
0167 
0168     MapVector<std::pair<StringRef, GlobalValue::GUID>, ImportIDTy> TheTable;
0169   };
0170 
0171   // Forward-declare SortedImportList for ImportMapTy.
0172   class SortedImportList;
0173 
0174   /// The map maintains the list of imports.  Conceptually, it is a collection
0175   /// of tuples of the form:
0176   ///
0177   ///   (The name of the source module, GUID, Definition/Declaration)
0178   ///
0179   /// The name of the source module is the module identifier to pass to the
0180   /// ModuleLoader.  The module identifier strings must be owned elsewhere,
0181   /// typically by the in-memory ModuleSummaryIndex the importing decisions are
0182   /// made from (the module path for each summary is owned by the index's module
0183   /// path string table).
0184   class ImportMapTy {
0185   public:
0186     enum class AddDefinitionStatus {
0187       // No change was made to the list of imports or whether each import should
0188       // be imported as a declaration or definition.
0189       NoChange,
0190       // Successfully added the given GUID to be imported as a definition. There
0191       // was no existing entry with the same GUID as a declaration.
0192       Inserted,
0193       // An existing with the given GUID was changed to a definition.
0194       ChangedToDefinition,
0195     };
0196 
0197     ImportMapTy() = delete;
0198     ImportMapTy(ImportIDTable &IDs) : IDs(IDs) {}
0199 
0200     // Add the given GUID to ImportList as a definition.  If the same GUID has
0201     // been added as a declaration previously, that entry is overridden.
0202     AddDefinitionStatus addDefinition(StringRef FromModule,
0203                                       GlobalValue::GUID GUID);
0204 
0205     // Add the given GUID to ImportList as a declaration.  If the same GUID has
0206     // been added as a definition previously, that entry takes precedence, and
0207     // no change is made.
0208     void maybeAddDeclaration(StringRef FromModule, GlobalValue::GUID GUID);
0209 
0210     void addGUID(StringRef FromModule, GlobalValue::GUID GUID,
0211                  GlobalValueSummary::ImportKind ImportKind) {
0212       if (ImportKind == GlobalValueSummary::Definition)
0213         addDefinition(FromModule, GUID);
0214       else
0215         maybeAddDeclaration(FromModule, GUID);
0216     }
0217 
0218     // Return the list of source modules sorted in the ascending alphabetical
0219     // order.
0220     SmallVector<StringRef, 0> getSourceModules() const;
0221 
0222     std::optional<GlobalValueSummary::ImportKind>
0223     getImportType(StringRef FromModule, GlobalValue::GUID GUID) const;
0224 
0225     // Iterate over the import list.  The caller gets tuples of FromModule,
0226     // GUID, and ImportKind instead of import IDs.  std::cref below prevents
0227     // map_iterator from deep-copying IDs.
0228     auto begin() const { return map_iterator(Imports.begin(), std::cref(IDs)); }
0229     auto end() const { return map_iterator(Imports.end(), std::cref(IDs)); }
0230 
0231     friend class SortedImportList;
0232 
0233   private:
0234     ImportIDTable &IDs;
0235     DenseSet<ImportIDTable::ImportIDTy> Imports;
0236   };
0237 
0238   // A read-only copy of ImportMapTy with its contents sorted according to the
0239   // given comparison function.
0240   class SortedImportList {
0241   public:
0242     SortedImportList(const ImportMapTy &ImportMap,
0243                      llvm::function_ref<
0244                          bool(const std::pair<StringRef, GlobalValue::GUID> &,
0245                               const std::pair<StringRef, GlobalValue::GUID> &)>
0246                          Comp)
0247         : IDs(ImportMap.IDs), Imports(iterator_range(ImportMap.Imports)) {
0248       llvm::sort(Imports, [&](ImportIDTable::ImportIDTy L,
0249                               ImportIDTable::ImportIDTy R) {
0250         auto Lookup = [&](ImportIDTable::ImportIDTy Id)
0251             -> std::pair<StringRef, GlobalValue::GUID> {
0252           auto Tuple = IDs.lookup(Id);
0253           return std::make_pair(std::get<0>(Tuple), std::get<1>(Tuple));
0254         };
0255         return Comp(Lookup(L), Lookup(R));
0256       });
0257     }
0258 
0259     // Iterate over the import list.  The caller gets tuples of FromModule,
0260     // GUID, and ImportKind instead of import IDs.  std::cref below prevents
0261     // map_iterator from deep-copying IDs.
0262     auto begin() const { return map_iterator(Imports.begin(), std::cref(IDs)); }
0263     auto end() const { return map_iterator(Imports.end(), std::cref(IDs)); }
0264 
0265   private:
0266     const ImportIDTable &IDs;
0267     SmallVector<ImportIDTable::ImportIDTy, 0> Imports;
0268   };
0269 
0270   // A map from destination modules to lists of imports.
0271   class ImportListsTy {
0272   public:
0273     ImportListsTy() : EmptyList(ImportIDs) {}
0274     ImportListsTy(size_t Size) : EmptyList(ImportIDs), ListsImpl(Size) {}
0275 
0276     ImportMapTy &operator[](StringRef DestMod) {
0277       return ListsImpl.try_emplace(DestMod, ImportIDs).first->second;
0278     }
0279 
0280     const ImportMapTy &lookup(StringRef DestMod) const {
0281       auto It = ListsImpl.find(DestMod);
0282       if (It != ListsImpl.end())
0283         return It->second;
0284       return EmptyList;
0285     }
0286 
0287     size_t size() const { return ListsImpl.size(); }
0288 
0289     using const_iterator = DenseMap<StringRef, ImportMapTy>::const_iterator;
0290     const_iterator begin() const { return ListsImpl.begin(); }
0291     const_iterator end() const { return ListsImpl.end(); }
0292 
0293   private:
0294     ImportMapTy EmptyList;
0295     DenseMap<StringRef, ImportMapTy> ListsImpl;
0296     ImportIDTable ImportIDs;
0297   };
0298 
0299   /// The set contains an entry for every global value that the module exports.
0300   /// Depending on the user context, this container is allowed to contain
0301   /// definitions, declarations or a mix of both.
0302   using ExportSetTy = DenseSet<ValueInfo>;
0303 
0304   /// A function of this type is used to load modules referenced by the index.
0305   using ModuleLoaderTy =
0306       std::function<Expected<std::unique_ptr<Module>>(StringRef Identifier)>;
0307 
0308   /// Create a Function Importer.
0309   FunctionImporter(const ModuleSummaryIndex &Index, ModuleLoaderTy ModuleLoader,
0310                    bool ClearDSOLocalOnDeclarations)
0311       : Index(Index), ModuleLoader(std::move(ModuleLoader)),
0312         ClearDSOLocalOnDeclarations(ClearDSOLocalOnDeclarations) {}
0313 
0314   /// Import functions in Module \p M based on the supplied import list.
0315   Expected<bool> importFunctions(Module &M, const ImportMapTy &ImportList);
0316 
0317 private:
0318   /// The summaries index used to trigger importing.
0319   const ModuleSummaryIndex &Index;
0320 
0321   /// Factory function to load a Module for a given identifier
0322   ModuleLoaderTy ModuleLoader;
0323 
0324   /// See the comment of ClearDSOLocalOnDeclarations in
0325   /// Utils/FunctionImportUtils.h.
0326   bool ClearDSOLocalOnDeclarations;
0327 };
0328 
0329 /// The function importing pass
0330 class FunctionImportPass : public PassInfoMixin<FunctionImportPass> {
0331 public:
0332   PreservedAnalyses run(Module &M, ModuleAnalysisManager &AM);
0333 };
0334 
0335 /// Compute all the imports and exports for every module in the Index.
0336 ///
0337 /// \p ModuleToDefinedGVSummaries contains for each Module a map
0338 /// (GUID -> Summary) for every global defined in the module.
0339 ///
0340 /// \p isPrevailing is a callback that will be called with a global value's GUID
0341 /// and summary and should return whether the module corresponding to the
0342 /// summary contains the linker-prevailing copy of that value.
0343 ///
0344 /// \p ImportLists will be populated with an entry for every Module we are
0345 /// importing into. This entry is itself a map that can be passed to
0346 /// FunctionImporter::importFunctions() above (see description there).
0347 ///
0348 /// \p ExportLists contains for each Module the set of globals (GUID) that will
0349 /// be imported by another module, or referenced by such a function. I.e. this
0350 /// is the set of globals that need to be promoted/renamed appropriately.
0351 ///
0352 /// The module identifier strings that are the keys of the above two maps
0353 /// are owned by the in-memory ModuleSummaryIndex the importing decisions
0354 /// are made from (the module path for each summary is owned by the index's
0355 /// module path string table).
0356 void ComputeCrossModuleImport(
0357     const ModuleSummaryIndex &Index,
0358     const DenseMap<StringRef, GVSummaryMapTy> &ModuleToDefinedGVSummaries,
0359     function_ref<bool(GlobalValue::GUID, const GlobalValueSummary *)>
0360         isPrevailing,
0361     FunctionImporter::ImportListsTy &ImportLists,
0362     DenseMap<StringRef, FunctionImporter::ExportSetTy> &ExportLists);
0363 
0364 /// PrevailingType enum used as a return type of callback passed
0365 /// to computeDeadSymbolsAndUpdateIndirectCalls. Yes and No values used when
0366 /// status explicitly set by symbols resolution, otherwise status is Unknown.
0367 enum class PrevailingType { Yes, No, Unknown };
0368 
0369 /// Update call edges for indirect calls to local functions added from
0370 /// SamplePGO when needed. Normally this is done during
0371 /// computeDeadSymbolsAndUpdateIndirectCalls, but can be called standalone
0372 /// when that is not called (e.g. during testing).
0373 void updateIndirectCalls(ModuleSummaryIndex &Index);
0374 
0375 /// Compute all the symbols that are "dead": i.e these that can't be reached
0376 /// in the graph from any of the given symbols listed in
0377 /// \p GUIDPreservedSymbols. Non-prevailing symbols are symbols without a
0378 /// prevailing copy anywhere in IR and are normally dead, \p isPrevailing
0379 /// predicate returns status of symbol.
0380 /// Also update call edges for indirect calls to local functions added from
0381 /// SamplePGO when needed.
0382 void computeDeadSymbolsAndUpdateIndirectCalls(
0383     ModuleSummaryIndex &Index,
0384     const DenseSet<GlobalValue::GUID> &GUIDPreservedSymbols,
0385     function_ref<PrevailingType(GlobalValue::GUID)> isPrevailing);
0386 
0387 /// Compute dead symbols and run constant propagation in combined index
0388 /// after that.
0389 void computeDeadSymbolsWithConstProp(
0390     ModuleSummaryIndex &Index,
0391     const DenseSet<GlobalValue::GUID> &GUIDPreservedSymbols,
0392     function_ref<PrevailingType(GlobalValue::GUID)> isPrevailing,
0393     bool ImportEnabled);
0394 
0395 /// Converts value \p GV to declaration, or replaces with a declaration if
0396 /// it is an alias. Returns true if converted, false if replaced.
0397 bool convertToDeclaration(GlobalValue &GV);
0398 
0399 /// Compute the set of summaries needed for a ThinLTO backend compilation of
0400 /// \p ModulePath.
0401 //
0402 /// This includes summaries from that module (in case any global summary based
0403 /// optimizations were recorded) and from any definitions in other modules that
0404 /// should be imported.
0405 //
0406 /// \p ModuleToSummariesForIndex will be populated with the needed summaries
0407 /// from each required module path. Use a std::map instead of StringMap to get
0408 /// stable order for bitcode emission.
0409 ///
0410 /// \p DecSummaries will be popluated with the subset of of summary pointers
0411 /// that have 'declaration' import type among all summaries the module need.
0412 void gatherImportedSummariesForModule(
0413     StringRef ModulePath,
0414     const DenseMap<StringRef, GVSummaryMapTy> &ModuleToDefinedGVSummaries,
0415     const FunctionImporter::ImportMapTy &ImportList,
0416     ModuleToSummariesForIndexTy &ModuleToSummariesForIndex,
0417     GVSummaryPtrSet &DecSummaries);
0418 
0419 /// Emit into \p OutputFilename the files module \p ModulePath will import from.
0420 Error EmitImportsFiles(
0421     StringRef ModulePath, StringRef OutputFilename,
0422     const ModuleToSummariesForIndexTy &ModuleToSummariesForIndex);
0423 
0424 /// Based on the information recorded in the summaries during global
0425 /// summary-based analysis:
0426 /// 1. Resolve prevailing symbol linkages and constrain visibility (CanAutoHide
0427 ///    and consider visibility from other definitions for ELF) in \p TheModule
0428 /// 2. (optional) Apply propagated function attributes to \p TheModule if
0429 ///    PropagateAttrs is true
0430 void thinLTOFinalizeInModule(Module &TheModule,
0431                              const GVSummaryMapTy &DefinedGlobals,
0432                              bool PropagateAttrs);
0433 
0434 /// Internalize \p TheModule based on the information recorded in the summaries
0435 /// during global summary-based analysis.
0436 void thinLTOInternalizeModule(Module &TheModule,
0437                               const GVSummaryMapTy &DefinedGlobals);
0438 
0439 } // end namespace llvm
0440 
0441 #endif // LLVM_TRANSFORMS_IPO_FUNCTIONIMPORT_H