Back to home page

EIC code displayed by LXR

 
 

    


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

0001 //===- FunctionImportUtils.h - Importing support utilities -----*- 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 // This file defines the FunctionImportGlobalProcessing class which is used
0010 // to perform the necessary global value handling for function importing.
0011 //
0012 //===----------------------------------------------------------------------===//
0013 
0014 #ifndef LLVM_TRANSFORMS_UTILS_FUNCTIONIMPORTUTILS_H
0015 #define LLVM_TRANSFORMS_UTILS_FUNCTIONIMPORTUTILS_H
0016 
0017 #include "llvm/ADT/SetVector.h"
0018 #include "llvm/IR/ModuleSummaryIndex.h"
0019 
0020 namespace llvm {
0021 class Module;
0022 
0023 /// Class to handle necessary GlobalValue changes required by ThinLTO
0024 /// function importing, including linkage changes and any necessary renaming.
0025 class FunctionImportGlobalProcessing {
0026   /// The Module which we are exporting or importing functions from.
0027   Module &M;
0028 
0029   /// Module summary index passed in for function importing/exporting handling.
0030   const ModuleSummaryIndex &ImportIndex;
0031 
0032   /// Globals to import from this module, all other functions will be
0033   /// imported as declarations instead of definitions.
0034   SetVector<GlobalValue *> *GlobalsToImport;
0035 
0036   /// Set to true if the given ModuleSummaryIndex contains any functions
0037   /// from this source module, in which case we must conservatively assume
0038   /// that any of its functions may be imported into another module
0039   /// as part of a different backend compilation process.
0040   bool HasExportedFunctions = false;
0041 
0042   /// Set to true (only applicatable to ELF -fpic) if dso_local should be
0043   /// dropped for a declaration.
0044   ///
0045   /// On ELF, the assembler is conservative and assumes a global default
0046   /// visibility symbol can be interposable. No direct access relocation is
0047   /// allowed, if the definition is not in the translation unit, even if the
0048   /// definition is available in the linkage unit. Thus we need to clear
0049   /// dso_local to disable direct access.
0050   ///
0051   /// This flag should not be set for -fno-pic or -fpie, which would
0052   /// unnecessarily disable direct access.
0053   bool ClearDSOLocalOnDeclarations;
0054 
0055   /// Set of llvm.*used values, in order to validate that we don't try
0056   /// to promote any non-renamable values.
0057   SmallPtrSet<GlobalValue *, 4> Used;
0058 
0059   /// Keep track of any COMDATs that require renaming (because COMDAT
0060   /// leader was promoted and renamed). Maps from original COMDAT to one
0061   /// with new name.
0062   DenseMap<const Comdat *, Comdat *> RenamedComdats;
0063 
0064   /// Check if we should promote the given local value to global scope.
0065   bool shouldPromoteLocalToGlobal(const GlobalValue *SGV, ValueInfo VI);
0066 
0067 #ifndef NDEBUG
0068   /// Check if the given value is a local that can't be renamed (promoted).
0069   /// Only used in assertion checking, and disabled under NDEBUG since the Used
0070   /// set will not be populated.
0071   bool isNonRenamableLocal(const GlobalValue &GV) const;
0072 #endif
0073 
0074   /// Helper methods to check if we are importing from or potentially
0075   /// exporting from the current source module.
0076   bool isPerformingImport() const { return GlobalsToImport != nullptr; }
0077   bool isModuleExporting() const { return HasExportedFunctions; }
0078 
0079   /// If we are importing from the source module, checks if we should
0080   /// import SGV as a definition, otherwise import as a declaration.
0081   bool doImportAsDefinition(const GlobalValue *SGV);
0082 
0083   /// Get the name for a local SGV that should be promoted and renamed to global
0084   /// scope in the linked destination module.
0085   std::string getPromotedName(const GlobalValue *SGV);
0086 
0087   /// Process globals so that they can be used in ThinLTO. This includes
0088   /// promoting local variables so that they can be reference externally by
0089   /// thin lto imported globals and converting strong external globals to
0090   /// available_externally.
0091   void processGlobalsForThinLTO();
0092   void processGlobalForThinLTO(GlobalValue &GV);
0093 
0094   /// Get the new linkage for SGV that should be used in the linked destination
0095   /// module. Specifically, for ThinLTO importing or exporting it may need
0096   /// to be adjusted. When \p DoPromote is true then we must adjust the
0097   /// linkage for a required promotion of a local to global scope.
0098   GlobalValue::LinkageTypes getLinkage(const GlobalValue *SGV, bool DoPromote);
0099 
0100 public:
0101   FunctionImportGlobalProcessing(Module &M, const ModuleSummaryIndex &Index,
0102                                  SetVector<GlobalValue *> *GlobalsToImport,
0103                                  bool ClearDSOLocalOnDeclarations)
0104       : M(M), ImportIndex(Index), GlobalsToImport(GlobalsToImport),
0105         ClearDSOLocalOnDeclarations(ClearDSOLocalOnDeclarations) {
0106     // If we have a ModuleSummaryIndex but no function to import,
0107     // then this is the primary module being compiled in a ThinLTO
0108     // backend compilation, and we need to see if it has functions that
0109     // may be exported to another backend compilation.
0110     if (!GlobalsToImport)
0111       HasExportedFunctions = ImportIndex.hasExportedFunctions(M);
0112 
0113 #ifndef NDEBUG
0114     SmallVector<GlobalValue *, 4> Vec;
0115     // First collect those in the llvm.used set.
0116     collectUsedGlobalVariables(M, Vec, /*CompilerUsed=*/false);
0117     // Next collect those in the llvm.compiler.used set.
0118     collectUsedGlobalVariables(M, Vec, /*CompilerUsed=*/true);
0119     Used = {Vec.begin(), Vec.end()};
0120 #endif
0121   }
0122 
0123   void run();
0124 };
0125 
0126 /// Perform in-place global value handling on the given Module for
0127 /// exported local functions renamed and promoted for ThinLTO.
0128 void renameModuleForThinLTO(
0129     Module &M, const ModuleSummaryIndex &Index,
0130     bool ClearDSOLocalOnDeclarations,
0131     SetVector<GlobalValue *> *GlobalsToImport = nullptr);
0132 
0133 } // End llvm namespace
0134 
0135 #endif