FunctionImportUtils.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. //===- lib/Transforms/Utils/FunctionImportUtils.cpp - Importing utilities -===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. //
  10. // This file implements the FunctionImportGlobalProcessing class, used
  11. // to perform the necessary global value handling for function importing.
  12. //
  13. //===----------------------------------------------------------------------===//
  14. #include "llvm/Transforms/Utils/FunctionImportUtils.h"
  15. #include "llvm/IR/InstIterator.h"
  16. using namespace llvm;
  17. /// Checks if we should import SGV as a definition, otherwise import as a
  18. /// declaration.
  19. bool FunctionImportGlobalProcessing::doImportAsDefinition(
  20. const GlobalValue *SGV, SetVector<GlobalValue *> *GlobalsToImport) {
  21. // Only import the globals requested for importing.
  22. if (!GlobalsToImport->count(const_cast<GlobalValue *>(SGV)))
  23. return false;
  24. assert(!isa<GlobalAlias>(SGV) &&
  25. "Unexpected global alias in the import list.");
  26. // Otherwise yes.
  27. return true;
  28. }
  29. bool FunctionImportGlobalProcessing::doImportAsDefinition(
  30. const GlobalValue *SGV) {
  31. if (!isPerformingImport())
  32. return false;
  33. return FunctionImportGlobalProcessing::doImportAsDefinition(SGV,
  34. GlobalsToImport);
  35. }
  36. bool FunctionImportGlobalProcessing::shouldPromoteLocalToGlobal(
  37. const GlobalValue *SGV) {
  38. assert(SGV->hasLocalLinkage());
  39. // Both the imported references and the original local variable must
  40. // be promoted.
  41. if (!isPerformingImport() && !isModuleExporting())
  42. return false;
  43. if (isPerformingImport()) {
  44. assert((!GlobalsToImport->count(const_cast<GlobalValue *>(SGV)) ||
  45. !isNonRenamableLocal(*SGV)) &&
  46. "Attempting to promote non-renamable local");
  47. // We don't know for sure yet if we are importing this value (as either
  48. // a reference or a def), since we are simply walking all values in the
  49. // module. But by necessity if we end up importing it and it is local,
  50. // it must be promoted, so unconditionally promote all values in the
  51. // importing module.
  52. return true;
  53. }
  54. // When exporting, consult the index. We can have more than one local
  55. // with the same GUID, in the case of same-named locals in different but
  56. // same-named source files that were compiled in their respective directories
  57. // (so the source file name and resulting GUID is the same). Find the one
  58. // in this module.
  59. auto Summary = ImportIndex.findSummaryInModule(
  60. SGV->getGUID(), SGV->getParent()->getModuleIdentifier());
  61. assert(Summary && "Missing summary for global value when exporting");
  62. auto Linkage = Summary->linkage();
  63. if (!GlobalValue::isLocalLinkage(Linkage)) {
  64. assert(!isNonRenamableLocal(*SGV) &&
  65. "Attempting to promote non-renamable local");
  66. return true;
  67. }
  68. return false;
  69. }
  70. #ifndef NDEBUG
  71. bool FunctionImportGlobalProcessing::isNonRenamableLocal(
  72. const GlobalValue &GV) const {
  73. if (!GV.hasLocalLinkage())
  74. return false;
  75. // This needs to stay in sync with the logic in buildModuleSummaryIndex.
  76. if (GV.hasSection())
  77. return true;
  78. if (Used.count(const_cast<GlobalValue *>(&GV)))
  79. return true;
  80. return false;
  81. }
  82. #endif
  83. std::string FunctionImportGlobalProcessing::getName(const GlobalValue *SGV,
  84. bool DoPromote) {
  85. // For locals that must be promoted to global scope, ensure that
  86. // the promoted name uniquely identifies the copy in the original module,
  87. // using the ID assigned during combined index creation. When importing,
  88. // we rename all locals (not just those that are promoted) in order to
  89. // avoid naming conflicts between locals imported from different modules.
  90. if (SGV->hasLocalLinkage() && (DoPromote || isPerformingImport()))
  91. return ModuleSummaryIndex::getGlobalNameForLocal(
  92. SGV->getName(),
  93. ImportIndex.getModuleHash(SGV->getParent()->getModuleIdentifier()));
  94. return SGV->getName();
  95. }
  96. GlobalValue::LinkageTypes
  97. FunctionImportGlobalProcessing::getLinkage(const GlobalValue *SGV,
  98. bool DoPromote) {
  99. // Any local variable that is referenced by an exported function needs
  100. // to be promoted to global scope. Since we don't currently know which
  101. // functions reference which local variables/functions, we must treat
  102. // all as potentially exported if this module is exporting anything.
  103. if (isModuleExporting()) {
  104. if (SGV->hasLocalLinkage() && DoPromote)
  105. return GlobalValue::ExternalLinkage;
  106. return SGV->getLinkage();
  107. }
  108. // Otherwise, if we aren't importing, no linkage change is needed.
  109. if (!isPerformingImport())
  110. return SGV->getLinkage();
  111. switch (SGV->getLinkage()) {
  112. case GlobalValue::LinkOnceAnyLinkage:
  113. case GlobalValue::LinkOnceODRLinkage:
  114. case GlobalValue::ExternalLinkage:
  115. // External and linkonce definitions are converted to available_externally
  116. // definitions upon import, so that they are available for inlining
  117. // and/or optimization, but are turned into declarations later
  118. // during the EliminateAvailableExternally pass.
  119. if (doImportAsDefinition(SGV) && !dyn_cast<GlobalAlias>(SGV))
  120. return GlobalValue::AvailableExternallyLinkage;
  121. // An imported external declaration stays external.
  122. return SGV->getLinkage();
  123. case GlobalValue::AvailableExternallyLinkage:
  124. // An imported available_externally definition converts
  125. // to external if imported as a declaration.
  126. if (!doImportAsDefinition(SGV))
  127. return GlobalValue::ExternalLinkage;
  128. // An imported available_externally declaration stays that way.
  129. return SGV->getLinkage();
  130. case GlobalValue::WeakAnyLinkage:
  131. // Can't import weak_any definitions correctly, or we might change the
  132. // program semantics, since the linker will pick the first weak_any
  133. // definition and importing would change the order they are seen by the
  134. // linker. The module linking caller needs to enforce this.
  135. assert(!doImportAsDefinition(SGV));
  136. // If imported as a declaration, it becomes external_weak.
  137. return SGV->getLinkage();
  138. case GlobalValue::WeakODRLinkage:
  139. // For weak_odr linkage, there is a guarantee that all copies will be
  140. // equivalent, so the issue described above for weak_any does not exist,
  141. // and the definition can be imported. It can be treated similarly
  142. // to an imported externally visible global value.
  143. if (doImportAsDefinition(SGV) && !dyn_cast<GlobalAlias>(SGV))
  144. return GlobalValue::AvailableExternallyLinkage;
  145. else
  146. return GlobalValue::ExternalLinkage;
  147. case GlobalValue::AppendingLinkage:
  148. // It would be incorrect to import an appending linkage variable,
  149. // since it would cause global constructors/destructors to be
  150. // executed multiple times. This should have already been handled
  151. // by linkIfNeeded, and we will assert in shouldLinkFromSource
  152. // if we try to import, so we simply return AppendingLinkage.
  153. return GlobalValue::AppendingLinkage;
  154. case GlobalValue::InternalLinkage:
  155. case GlobalValue::PrivateLinkage:
  156. // If we are promoting the local to global scope, it is handled
  157. // similarly to a normal externally visible global.
  158. if (DoPromote) {
  159. if (doImportAsDefinition(SGV) && !dyn_cast<GlobalAlias>(SGV))
  160. return GlobalValue::AvailableExternallyLinkage;
  161. else
  162. return GlobalValue::ExternalLinkage;
  163. }
  164. // A non-promoted imported local definition stays local.
  165. // The ThinLTO pass will eventually force-import their definitions.
  166. return SGV->getLinkage();
  167. case GlobalValue::ExternalWeakLinkage:
  168. // External weak doesn't apply to definitions, must be a declaration.
  169. assert(!doImportAsDefinition(SGV));
  170. // Linkage stays external_weak.
  171. return SGV->getLinkage();
  172. case GlobalValue::CommonLinkage:
  173. // Linkage stays common on definitions.
  174. // The ThinLTO pass will eventually force-import their definitions.
  175. return SGV->getLinkage();
  176. }
  177. llvm_unreachable("unknown linkage type");
  178. }
  179. void FunctionImportGlobalProcessing::processGlobalForThinLTO(GlobalValue &GV) {
  180. // Check the summaries to see if the symbol gets resolved to a known local
  181. // definition.
  182. ValueInfo VI;
  183. if (GV.hasName()) {
  184. VI = ImportIndex.getValueInfo(GV.getGUID());
  185. if (VI && VI.isDSOLocal()) {
  186. GV.setDSOLocal(true);
  187. if (GV.hasDLLImportStorageClass())
  188. GV.setDLLStorageClass(GlobalValue::DefaultStorageClass);
  189. }
  190. }
  191. // Mark read-only variables which can be imported with specific attribute.
  192. // We can't internalize them now because IRMover will fail to link variable
  193. // definitions to their external declarations during ThinLTO import. We'll
  194. // internalize read-only variables later, after import is finished.
  195. // See internalizeImmutableGVs.
  196. //
  197. // If global value dead stripping is not enabled in summary then
  198. // propagateConstants hasn't been run (may be because we're using
  199. // distriuted import. We can't internalize GV in such case.
  200. if (!GV.isDeclaration() && VI && ImportIndex.withGlobalValueDeadStripping()) {
  201. const auto &SL = VI.getSummaryList();
  202. auto *GVS = SL.empty() ? nullptr : dyn_cast<GlobalVarSummary>(SL[0].get());
  203. if (GVS && GVS->isReadOnly())
  204. cast<GlobalVariable>(&GV)->addAttribute("thinlto-internalize");
  205. }
  206. bool DoPromote = false;
  207. if (GV.hasLocalLinkage() &&
  208. ((DoPromote = shouldPromoteLocalToGlobal(&GV)) || isPerformingImport())) {
  209. // Once we change the name or linkage it is difficult to determine
  210. // again whether we should promote since shouldPromoteLocalToGlobal needs
  211. // to locate the summary (based on GUID from name and linkage). Therefore,
  212. // use DoPromote result saved above.
  213. GV.setName(getName(&GV, DoPromote));
  214. GV.setLinkage(getLinkage(&GV, DoPromote));
  215. if (!GV.hasLocalLinkage())
  216. GV.setVisibility(GlobalValue::HiddenVisibility);
  217. } else
  218. GV.setLinkage(getLinkage(&GV, /* DoPromote */ false));
  219. // Remove functions imported as available externally defs from comdats,
  220. // as this is a declaration for the linker, and will be dropped eventually.
  221. // It is illegal for comdats to contain declarations.
  222. auto *GO = dyn_cast<GlobalObject>(&GV);
  223. if (GO && GO->isDeclarationForLinker() && GO->hasComdat()) {
  224. // The IRMover should not have placed any imported declarations in
  225. // a comdat, so the only declaration that should be in a comdat
  226. // at this point would be a definition imported as available_externally.
  227. assert(GO->hasAvailableExternallyLinkage() &&
  228. "Expected comdat on definition (possibly available external)");
  229. GO->setComdat(nullptr);
  230. }
  231. }
  232. void FunctionImportGlobalProcessing::processGlobalsForThinLTO() {
  233. for (GlobalVariable &GV : M.globals())
  234. processGlobalForThinLTO(GV);
  235. for (Function &SF : M)
  236. processGlobalForThinLTO(SF);
  237. for (GlobalAlias &GA : M.aliases())
  238. processGlobalForThinLTO(GA);
  239. }
  240. bool FunctionImportGlobalProcessing::run() {
  241. processGlobalsForThinLTO();
  242. return false;
  243. }
  244. bool llvm::renameModuleForThinLTO(Module &M, const ModuleSummaryIndex &Index,
  245. SetVector<GlobalValue *> *GlobalsToImport) {
  246. FunctionImportGlobalProcessing ThinLTOProcessing(M, Index, GlobalsToImport);
  247. return ThinLTOProcessing.run();
  248. }