FunctionImportUtils.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===- FunctionImportUtils.h - Importing support utilities -----*- C++ -*-===//
  7. //
  8. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  9. // See https://llvm.org/LICENSE.txt for license information.
  10. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  11. //
  12. //===----------------------------------------------------------------------===//
  13. //
  14. // This file defines the FunctionImportGlobalProcessing class which is used
  15. // to perform the necessary global value handling for function importing.
  16. //
  17. //===----------------------------------------------------------------------===//
  18. #ifndef LLVM_TRANSFORMS_UTILS_FUNCTIONIMPORTUTILS_H
  19. #define LLVM_TRANSFORMS_UTILS_FUNCTIONIMPORTUTILS_H
  20. #include "llvm/ADT/SetVector.h"
  21. #include "llvm/IR/ModuleSummaryIndex.h"
  22. namespace llvm {
  23. class Module;
  24. /// Class to handle necessary GlobalValue changes required by ThinLTO
  25. /// function importing, including linkage changes and any necessary renaming.
  26. class FunctionImportGlobalProcessing {
  27. /// The Module which we are exporting or importing functions from.
  28. Module &M;
  29. /// Module summary index passed in for function importing/exporting handling.
  30. const ModuleSummaryIndex &ImportIndex;
  31. /// Globals to import from this module, all other functions will be
  32. /// imported as declarations instead of definitions.
  33. SetVector<GlobalValue *> *GlobalsToImport;
  34. /// Set to true if the given ModuleSummaryIndex contains any functions
  35. /// from this source module, in which case we must conservatively assume
  36. /// that any of its functions may be imported into another module
  37. /// as part of a different backend compilation process.
  38. bool HasExportedFunctions = false;
  39. /// Set to true (only applicatable to ELF -fpic) if dso_local should be
  40. /// dropped for a declaration.
  41. ///
  42. /// On ELF, the assembler is conservative and assumes a global default
  43. /// visibility symbol can be interposable. No direct access relocation is
  44. /// allowed, if the definition is not in the translation unit, even if the
  45. /// definition is available in the linkage unit. Thus we need to clear
  46. /// dso_local to disable direct access.
  47. ///
  48. /// This flag should not be set for -fno-pic or -fpie, which would
  49. /// unnecessarily disable direct access.
  50. bool ClearDSOLocalOnDeclarations;
  51. /// Set of llvm.*used values, in order to validate that we don't try
  52. /// to promote any non-renamable values.
  53. SmallPtrSet<GlobalValue *, 4> Used;
  54. /// Keep track of any COMDATs that require renaming (because COMDAT
  55. /// leader was promoted and renamed). Maps from original COMDAT to one
  56. /// with new name.
  57. DenseMap<const Comdat *, Comdat *> RenamedComdats;
  58. /// Check if we should promote the given local value to global scope.
  59. bool shouldPromoteLocalToGlobal(const GlobalValue *SGV, ValueInfo VI);
  60. #ifndef NDEBUG
  61. /// Check if the given value is a local that can't be renamed (promoted).
  62. /// Only used in assertion checking, and disabled under NDEBUG since the Used
  63. /// set will not be populated.
  64. bool isNonRenamableLocal(const GlobalValue &GV) const;
  65. #endif
  66. /// Helper methods to check if we are importing from or potentially
  67. /// exporting from the current source module.
  68. bool isPerformingImport() const { return GlobalsToImport != nullptr; }
  69. bool isModuleExporting() const { return HasExportedFunctions; }
  70. /// If we are importing from the source module, checks if we should
  71. /// import SGV as a definition, otherwise import as a declaration.
  72. bool doImportAsDefinition(const GlobalValue *SGV);
  73. /// Get the name for a local SGV that should be promoted and renamed to global
  74. /// scope in the linked destination module.
  75. std::string getPromotedName(const GlobalValue *SGV);
  76. /// Process globals so that they can be used in ThinLTO. This includes
  77. /// promoting local variables so that they can be reference externally by
  78. /// thin lto imported globals and converting strong external globals to
  79. /// available_externally.
  80. void processGlobalsForThinLTO();
  81. void processGlobalForThinLTO(GlobalValue &GV);
  82. /// Get the new linkage for SGV that should be used in the linked destination
  83. /// module. Specifically, for ThinLTO importing or exporting it may need
  84. /// to be adjusted. When \p DoPromote is true then we must adjust the
  85. /// linkage for a required promotion of a local to global scope.
  86. GlobalValue::LinkageTypes getLinkage(const GlobalValue *SGV, bool DoPromote);
  87. public:
  88. FunctionImportGlobalProcessing(Module &M, const ModuleSummaryIndex &Index,
  89. SetVector<GlobalValue *> *GlobalsToImport,
  90. bool ClearDSOLocalOnDeclarations)
  91. : M(M), ImportIndex(Index), GlobalsToImport(GlobalsToImport),
  92. ClearDSOLocalOnDeclarations(ClearDSOLocalOnDeclarations) {
  93. // If we have a ModuleSummaryIndex but no function to import,
  94. // then this is the primary module being compiled in a ThinLTO
  95. // backend compilation, and we need to see if it has functions that
  96. // may be exported to another backend compilation.
  97. if (!GlobalsToImport)
  98. HasExportedFunctions = ImportIndex.hasExportedFunctions(M);
  99. #ifndef NDEBUG
  100. SmallVector<GlobalValue *, 4> Vec;
  101. // First collect those in the llvm.used set.
  102. collectUsedGlobalVariables(M, Vec, /*CompilerUsed=*/false);
  103. // Next collect those in the llvm.compiler.used set.
  104. collectUsedGlobalVariables(M, Vec, /*CompilerUsed=*/true);
  105. Used = {Vec.begin(), Vec.end()};
  106. #endif
  107. }
  108. bool run();
  109. };
  110. /// Perform in-place global value handling on the given Module for
  111. /// exported local functions renamed and promoted for ThinLTO.
  112. bool renameModuleForThinLTO(
  113. Module &M, const ModuleSummaryIndex &Index,
  114. bool ClearDSOLocalOnDeclarations,
  115. SetVector<GlobalValue *> *GlobalsToImport = nullptr);
  116. } // End llvm namespace
  117. #endif
  118. #ifdef __GNUC__
  119. #pragma GCC diagnostic pop
  120. #endif