LLVM  10.0.0svn
FunctionImportUtils.cpp
Go to the documentation of this file.
1 //===- lib/Transforms/Utils/FunctionImportUtils.cpp - Importing utilities -===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file implements the FunctionImportGlobalProcessing class, used
10 // to perform the necessary global value handling for function importing.
11 //
12 //===----------------------------------------------------------------------===//
13 
15 #include "llvm/IR/InstIterator.h"
16 using namespace llvm;
17 
18 /// Checks if we should import SGV as a definition, otherwise import as a
19 /// declaration.
20 bool FunctionImportGlobalProcessing::doImportAsDefinition(
21  const GlobalValue *SGV, SetVector<GlobalValue *> *GlobalsToImport) {
22 
23  // Only import the globals requested for importing.
24  if (!GlobalsToImport->count(const_cast<GlobalValue *>(SGV)))
25  return false;
26 
27  assert(!isa<GlobalAlias>(SGV) &&
28  "Unexpected global alias in the import list.");
29 
30  // Otherwise yes.
31  return true;
32 }
33 
34 bool FunctionImportGlobalProcessing::doImportAsDefinition(
35  const GlobalValue *SGV) {
36  if (!isPerformingImport())
37  return false;
38  return FunctionImportGlobalProcessing::doImportAsDefinition(SGV,
39  GlobalsToImport);
40 }
41 
42 bool FunctionImportGlobalProcessing::shouldPromoteLocalToGlobal(
43  const GlobalValue *SGV) {
44  assert(SGV->hasLocalLinkage());
45  // Both the imported references and the original local variable must
46  // be promoted.
47  if (!isPerformingImport() && !isModuleExporting())
48  return false;
49 
50  if (isPerformingImport()) {
51  assert((!GlobalsToImport->count(const_cast<GlobalValue *>(SGV)) ||
52  !isNonRenamableLocal(*SGV)) &&
53  "Attempting to promote non-renamable local");
54  // We don't know for sure yet if we are importing this value (as either
55  // a reference or a def), since we are simply walking all values in the
56  // module. But by necessity if we end up importing it and it is local,
57  // it must be promoted, so unconditionally promote all values in the
58  // importing module.
59  return true;
60  }
61 
62  // When exporting, consult the index. We can have more than one local
63  // with the same GUID, in the case of same-named locals in different but
64  // same-named source files that were compiled in their respective directories
65  // (so the source file name and resulting GUID is the same). Find the one
66  // in this module.
67  auto Summary = ImportIndex.findSummaryInModule(
68  SGV->getGUID(), SGV->getParent()->getModuleIdentifier());
69  assert(Summary && "Missing summary for global value when exporting");
70  auto Linkage = Summary->linkage();
71  if (!GlobalValue::isLocalLinkage(Linkage)) {
72  assert(!isNonRenamableLocal(*SGV) &&
73  "Attempting to promote non-renamable local");
74  return true;
75  }
76 
77  return false;
78 }
79 
80 #ifndef NDEBUG
81 bool FunctionImportGlobalProcessing::isNonRenamableLocal(
82  const GlobalValue &GV) const {
83  if (!GV.hasLocalLinkage())
84  return false;
85  // This needs to stay in sync with the logic in buildModuleSummaryIndex.
86  if (GV.hasSection())
87  return true;
88  if (Used.count(const_cast<GlobalValue *>(&GV)))
89  return true;
90  return false;
91 }
92 #endif
93 
94 std::string FunctionImportGlobalProcessing::getName(const GlobalValue *SGV,
95  bool DoPromote) {
96  // For locals that must be promoted to global scope, ensure that
97  // the promoted name uniquely identifies the copy in the original module,
98  // using the ID assigned during combined index creation. When importing,
99  // we rename all locals (not just those that are promoted) in order to
100  // avoid naming conflicts between locals imported from different modules.
101  if (SGV->hasLocalLinkage() && (DoPromote || isPerformingImport()))
103  SGV->getName(),
104  ImportIndex.getModuleHash(SGV->getParent()->getModuleIdentifier()));
105  return SGV->getName();
106 }
107 
109 FunctionImportGlobalProcessing::getLinkage(const GlobalValue *SGV,
110  bool DoPromote) {
111  // Any local variable that is referenced by an exported function needs
112  // to be promoted to global scope. Since we don't currently know which
113  // functions reference which local variables/functions, we must treat
114  // all as potentially exported if this module is exporting anything.
115  if (isModuleExporting()) {
116  if (SGV->hasLocalLinkage() && DoPromote)
118  return SGV->getLinkage();
119  }
120 
121  // Otherwise, if we aren't importing, no linkage change is needed.
122  if (!isPerformingImport())
123  return SGV->getLinkage();
124 
125  switch (SGV->getLinkage()) {
128  // External and linkonce definitions are converted to available_externally
129  // definitions upon import, so that they are available for inlining
130  // and/or optimization, but are turned into declarations later
131  // during the EliminateAvailableExternally pass.
132  if (doImportAsDefinition(SGV) && !isa<GlobalAlias>(SGV))
134  // An imported external declaration stays external.
135  return SGV->getLinkage();
136 
138  // An imported available_externally definition converts
139  // to external if imported as a declaration.
140  if (!doImportAsDefinition(SGV))
142  // An imported available_externally declaration stays that way.
143  return SGV->getLinkage();
144 
147  // Can't import linkonce_any/weak_any definitions correctly, or we might
148  // change the program semantics, since the linker will pick the first
149  // linkonce_any/weak_any definition and importing would change the order
150  // they are seen by the linker. The module linking caller needs to enforce
151  // this.
152  assert(!doImportAsDefinition(SGV));
153  // If imported as a declaration, it becomes external_weak.
154  return SGV->getLinkage();
155 
157  // For weak_odr linkage, there is a guarantee that all copies will be
158  // equivalent, so the issue described above for weak_any does not exist,
159  // and the definition can be imported. It can be treated similarly
160  // to an imported externally visible global value.
161  if (doImportAsDefinition(SGV) && !isa<GlobalAlias>(SGV))
163  else
165 
167  // It would be incorrect to import an appending linkage variable,
168  // since it would cause global constructors/destructors to be
169  // executed multiple times. This should have already been handled
170  // by linkIfNeeded, and we will assert in shouldLinkFromSource
171  // if we try to import, so we simply return AppendingLinkage.
173 
176  // If we are promoting the local to global scope, it is handled
177  // similarly to a normal externally visible global.
178  if (DoPromote) {
179  if (doImportAsDefinition(SGV) && !isa<GlobalAlias>(SGV))
181  else
183  }
184  // A non-promoted imported local definition stays local.
185  // The ThinLTO pass will eventually force-import their definitions.
186  return SGV->getLinkage();
187 
189  // External weak doesn't apply to definitions, must be a declaration.
190  assert(!doImportAsDefinition(SGV));
191  // Linkage stays external_weak.
192  return SGV->getLinkage();
193 
195  // Linkage stays common on definitions.
196  // The ThinLTO pass will eventually force-import their definitions.
197  return SGV->getLinkage();
198  }
199 
200  llvm_unreachable("unknown linkage type");
201 }
202 
203 void FunctionImportGlobalProcessing::processGlobalForThinLTO(GlobalValue &GV) {
204 
205  ValueInfo VI;
206  if (GV.hasName()) {
207  VI = ImportIndex.getValueInfo(GV.getGUID());
208  // Set synthetic function entry counts.
209  if (VI && ImportIndex.hasSyntheticEntryCounts()) {
210  if (Function *F = dyn_cast<Function>(&GV)) {
211  if (!F->isDeclaration()) {
212  for (auto &S : VI.getSummaryList()) {
213  FunctionSummary *FS = dyn_cast<FunctionSummary>(S->getBaseObject());
214  if (FS->modulePath() == M.getModuleIdentifier()) {
215  F->setEntryCount(Function::ProfileCount(FS->entryCount(),
217  break;
218  }
219  }
220  }
221  }
222  }
223  // Check the summaries to see if the symbol gets resolved to a known local
224  // definition.
225  if (VI && VI.isDSOLocal()) {
226  GV.setDSOLocal(true);
227  if (GV.hasDLLImportStorageClass())
229  }
230  }
231 
232  // Mark read/write-only variables which can be imported with specific
233  // attribute. We can't internalize them now because IRMover will fail
234  // to link variable definitions to their external declarations during
235  // ThinLTO import. We'll internalize read-only variables later, after
236  // import is finished. See internalizeGVsAfterImport.
237  //
238  // If global value dead stripping is not enabled in summary then
239  // propagateConstants hasn't been run. We can't internalize GV
240  // in such case.
241  if (!GV.isDeclaration() && VI && ImportIndex.withGlobalValueDeadStripping()) {
242  const auto &SL = VI.getSummaryList();
243  auto *GVS = SL.empty() ? nullptr : dyn_cast<GlobalVarSummary>(SL[0].get());
244  // At this stage "maybe" is "definitely"
245  if (GVS && (GVS->maybeReadOnly() || GVS->maybeWriteOnly()))
246  cast<GlobalVariable>(&GV)->addAttribute("thinlto-internalize");
247  }
248 
249  bool DoPromote = false;
250  if (GV.hasLocalLinkage() &&
251  ((DoPromote = shouldPromoteLocalToGlobal(&GV)) || isPerformingImport())) {
252  // Save the original name string before we rename GV below.
253  auto Name = GV.getName().str();
254  // Once we change the name or linkage it is difficult to determine
255  // again whether we should promote since shouldPromoteLocalToGlobal needs
256  // to locate the summary (based on GUID from name and linkage). Therefore,
257  // use DoPromote result saved above.
258  GV.setName(getName(&GV, DoPromote));
259  GV.setLinkage(getLinkage(&GV, DoPromote));
260  if (!GV.hasLocalLinkage())
262 
263  // If we are renaming a COMDAT leader, ensure that we record the COMDAT
264  // for later renaming as well. This is required for COFF.
265  if (const auto *C = GV.getComdat())
266  if (C->getName() == Name)
267  RenamedComdats.try_emplace(C, M.getOrInsertComdat(GV.getName()));
268  } else
269  GV.setLinkage(getLinkage(&GV, /* DoPromote */ false));
270 
271  // Remove functions imported as available externally defs from comdats,
272  // as this is a declaration for the linker, and will be dropped eventually.
273  // It is illegal for comdats to contain declarations.
274  auto *GO = dyn_cast<GlobalObject>(&GV);
275  if (GO && GO->isDeclarationForLinker() && GO->hasComdat()) {
276  // The IRMover should not have placed any imported declarations in
277  // a comdat, so the only declaration that should be in a comdat
278  // at this point would be a definition imported as available_externally.
279  assert(GO->hasAvailableExternallyLinkage() &&
280  "Expected comdat on definition (possibly available external)");
281  GO->setComdat(nullptr);
282  }
283 }
284 
285 void FunctionImportGlobalProcessing::processGlobalsForThinLTO() {
286  for (GlobalVariable &GV : M.globals())
287  processGlobalForThinLTO(GV);
288  for (Function &SF : M)
289  processGlobalForThinLTO(SF);
290  for (GlobalAlias &GA : M.aliases())
291  processGlobalForThinLTO(GA);
292 
293  // Replace any COMDATS that required renaming (because the COMDAT leader was
294  // promoted and renamed).
295  if (!RenamedComdats.empty())
296  for (auto &GO : M.global_objects())
297  if (auto *C = GO.getComdat()) {
298  auto Replacement = RenamedComdats.find(C);
299  if (Replacement != RenamedComdats.end())
300  GO.setComdat(Replacement->second);
301  }
302 }
303 
305  processGlobalsForThinLTO();
306  return false;
307 }
308 
310  SetVector<GlobalValue *> *GlobalsToImport) {
311  FunctionImportGlobalProcessing ThinLTOProcessing(M, Index, GlobalsToImport);
312  return ThinLTOProcessing.run();
313 }
void setVisibility(VisibilityTypes V)
Definition: GlobalValue.h:242
uint64_t CallInst * C
bool hasLocalLinkage() const
Definition: GlobalValue.h:445
Special purpose, only applies to global arrays.
Definition: GlobalValue.h:54
LLVM_NODISCARD std::string str() const
str - Get the contents as an std::string.
Definition: StringRef.h:218
This class represents lattice values for constants.
Definition: AllocatorList.h:23
A Module instance is used to store all the information related to an LLVM module. ...
Definition: Module.h:65
Same, but only replaced by something equivalent.
Definition: GlobalValue.h:53
amdgpu Simplify well known AMD library false FunctionCallee Value const Twine & Name
Global variable summary information to aid decisions and implementation of importing.
Available for inspection, not emission.
Definition: GlobalValue.h:49
Like Internal, but omit from symbol table.
Definition: GlobalValue.h:56
Externally visible function.
Definition: GlobalValue.h:48
Class to handle necessary GlobalValue changes required by ThinLTO function importing, including linkage changes and any necessary renaming.
bool hasDLLImportStorageClass() const
Definition: GlobalValue.h:265
F(f)
Tentative definitions.
Definition: GlobalValue.h:58
static bool isLocalLinkage(LinkageTypes Linkage)
Definition: GlobalValue.h:330
bool hasSection() const
Definition: GlobalValue.h:273
static std::string getGlobalNameForLocal(StringRef Name, ModuleHash ModHash)
Convenience method for creating a promoted global name for the given value name of a local...
void setName(const Twine &Name)
Change the name of the value.
Definition: Value.cpp:285
void setDLLStorageClass(DLLStorageClassTypes C)
Definition: GlobalValue.h:271
LinkageTypes getLinkage() const
Definition: GlobalValue.h:460
Class to hold module path string table and global value map, and encapsulate methods for operating on...
ArrayRef< std::unique_ptr< GlobalValueSummary > > getSummaryList() const
size_type count(const key_type &key) const
Count the number of elements of a given key in the SetVector.
Definition: SetVector.h:210
ExternalWeak linkage description.
Definition: GlobalValue.h:57
Same, but only replaced by something equivalent.
Definition: GlobalValue.h:51
bool renameModuleForThinLTO(Module &M, const ModuleSummaryIndex &Index, SetVector< GlobalValue *> *GlobalsToImport=nullptr)
Perform in-place global value handling on the given Module for exported local functions renamed and p...
bool hasName() const
Definition: Value.h:251
uint64_t entryCount() const
Get the synthetic entry count for this function.
const ModuleHash & getModuleHash(const StringRef ModPath) const
Get the module SHA1 hash recorded for the given module path.
static GUID getGUID(StringRef GlobalName)
Return a 64-bit global unique ID constructed from global value name (i.e.
Definition: GlobalValue.h:506
Comdat * getOrInsertComdat(StringRef Name)
Return the Comdat in the module with the specified name.
Definition: Module.cpp:482
Class to represent profile counts.
Definition: Function.h:260
const std::string & getModuleIdentifier() const
Get the module identifier which is, essentially, the name of the module.
Definition: Module.h:210
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
Struct that holds a reference to a particular GUID in a global value summary.
bool isDSOLocal() const
Keep one copy of function when linking (inline)
Definition: GlobalValue.h:50
void setLinkage(LinkageTypes LT)
Definition: GlobalValue.h:454
const Comdat * getComdat() const
Definition: Globals.cpp:175
LinkageTypes
An enumeration for the kinds of linkage for global values.
Definition: GlobalValue.h:47
ValueInfo getValueInfo(const GlobalValueSummaryMapTy::value_type &R) const
Return a ValueInfo for the index value_type (convenient when iterating index).
bool withGlobalValueDeadStripping() const
StringRef modulePath() const
Get the path to the module containing this function.
StringRef getName() const
Return a constant reference to the value&#39;s name.
Definition: Value.cpp:214
LLVM_NODISCARD std::enable_if<!is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
Definition: Casting.h:332
Keep one copy of named function when linking (weak)
Definition: GlobalValue.h:52
Rename collisions when linking (static functions).
Definition: GlobalValue.h:55
GlobalValueSummary * findSummaryInModule(ValueInfo VI, StringRef ModuleId) const
Find the summary for ValueInfo VI in module ModuleId, or nullptr if not found.
Function summary information to aid decisions and implementation of importing.
bool isDeclaration() const
Return true if the primary definition of this global value is outside of the current translation unit...
Definition: Globals.cpp:231
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
Module * getParent()
Get the module that this global value is contained inside of...
Definition: GlobalValue.h:575
A vector that has set insertion semantics.
Definition: SetVector.h:40
iterator_range< global_iterator > globals()
Definition: Module.h:587
void setDSOLocal(bool Local)
Definition: GlobalValue.h:281