LLVM  14.0.0git
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/Constants.h"
16 #include "llvm/IR/InstIterator.h"
17 using namespace llvm;
18 
19 /// Checks if we should import SGV as a definition, otherwise import as a
20 /// declaration.
21 bool FunctionImportGlobalProcessing::doImportAsDefinition(
22  const GlobalValue *SGV) {
23  if (!isPerformingImport())
24  return false;
25 
26  // Only import the globals requested for importing.
27  if (!GlobalsToImport->count(const_cast<GlobalValue *>(SGV)))
28  return false;
29 
30  assert(!isa<GlobalAlias>(SGV) &&
31  "Unexpected global alias in the import list.");
32 
33  // Otherwise yes.
34  return true;
35 }
36 
37 bool FunctionImportGlobalProcessing::shouldPromoteLocalToGlobal(
38  const GlobalValue *SGV, ValueInfo VI) {
39  assert(SGV->hasLocalLinkage());
40  // Both the imported references and the original local variable must
41  // be promoted.
42  if (!isPerformingImport() && !isModuleExporting())
43  return false;
44 
45  if (isPerformingImport()) {
46  assert((!GlobalsToImport->count(const_cast<GlobalValue *>(SGV)) ||
47  !isNonRenamableLocal(*SGV)) &&
48  "Attempting to promote non-renamable local");
49  // We don't know for sure yet if we are importing this value (as either
50  // a reference or a def), since we are simply walking all values in the
51  // module. But by necessity if we end up importing it and it is local,
52  // it must be promoted, so unconditionally promote all values in the
53  // importing module.
54  return true;
55  }
56 
57  // When exporting, consult the index. We can have more than one local
58  // with the same GUID, in the case of same-named locals in different but
59  // same-named source files that were compiled in their respective directories
60  // (so the source file name and resulting GUID is the same). Find the one
61  // in this module.
62  auto Summary = ImportIndex.findSummaryInModule(
63  VI, SGV->getParent()->getModuleIdentifier());
64  assert(Summary && "Missing summary for global value when exporting");
65  auto Linkage = Summary->linkage();
67  assert(!isNonRenamableLocal(*SGV) &&
68  "Attempting to promote non-renamable local");
69  return true;
70  }
71 
72  return false;
73 }
74 
75 #ifndef NDEBUG
76 bool FunctionImportGlobalProcessing::isNonRenamableLocal(
77  const GlobalValue &GV) const {
78  if (!GV.hasLocalLinkage())
79  return false;
80  // This needs to stay in sync with the logic in buildModuleSummaryIndex.
81  if (GV.hasSection())
82  return true;
83  if (Used.count(const_cast<GlobalValue *>(&GV)))
84  return true;
85  return false;
86 }
87 #endif
88 
89 std::string
90 FunctionImportGlobalProcessing::getPromotedName(const GlobalValue *SGV) {
91  assert(SGV->hasLocalLinkage());
92  // For locals that must be promoted to global scope, ensure that
93  // the promoted name uniquely identifies the copy in the original module,
94  // using the ID assigned during combined index creation.
96  SGV->getName(),
97  ImportIndex.getModuleHash(SGV->getParent()->getModuleIdentifier()));
98 }
99 
101 FunctionImportGlobalProcessing::getLinkage(const GlobalValue *SGV,
102  bool DoPromote) {
103  // Any local variable that is referenced by an exported function needs
104  // to be promoted to global scope. Since we don't currently know which
105  // functions reference which local variables/functions, we must treat
106  // all as potentially exported if this module is exporting anything.
107  if (isModuleExporting()) {
108  if (SGV->hasLocalLinkage() && DoPromote)
110  return SGV->getLinkage();
111  }
112 
113  // Otherwise, if we aren't importing, no linkage change is needed.
114  if (!isPerformingImport())
115  return SGV->getLinkage();
116 
117  switch (SGV->getLinkage()) {
120  // External and linkonce definitions are converted to available_externally
121  // definitions upon import, so that they are available for inlining
122  // and/or optimization, but are turned into declarations later
123  // during the EliminateAvailableExternally pass.
124  if (doImportAsDefinition(SGV) && !isa<GlobalAlias>(SGV))
126  // An imported external declaration stays external.
127  return SGV->getLinkage();
128 
130  // An imported available_externally definition converts
131  // to external if imported as a declaration.
132  if (!doImportAsDefinition(SGV))
134  // An imported available_externally declaration stays that way.
135  return SGV->getLinkage();
136 
139  // Can't import linkonce_any/weak_any definitions correctly, or we might
140  // change the program semantics, since the linker will pick the first
141  // linkonce_any/weak_any definition and importing would change the order
142  // they are seen by the linker. The module linking caller needs to enforce
143  // this.
144  assert(!doImportAsDefinition(SGV));
145  // If imported as a declaration, it becomes external_weak.
146  return SGV->getLinkage();
147 
149  // For weak_odr linkage, there is a guarantee that all copies will be
150  // equivalent, so the issue described above for weak_any does not exist,
151  // and the definition can be imported. It can be treated similarly
152  // to an imported externally visible global value.
153  if (doImportAsDefinition(SGV) && !isa<GlobalAlias>(SGV))
155  else
157 
159  // It would be incorrect to import an appending linkage variable,
160  // since it would cause global constructors/destructors to be
161  // executed multiple times. This should have already been handled
162  // by linkIfNeeded, and we will assert in shouldLinkFromSource
163  // if we try to import, so we simply return AppendingLinkage.
165 
168  // If we are promoting the local to global scope, it is handled
169  // similarly to a normal externally visible global.
170  if (DoPromote) {
171  if (doImportAsDefinition(SGV) && !isa<GlobalAlias>(SGV))
173  else
175  }
176  // A non-promoted imported local definition stays local.
177  // The ThinLTO pass will eventually force-import their definitions.
178  return SGV->getLinkage();
179 
181  // External weak doesn't apply to definitions, must be a declaration.
182  assert(!doImportAsDefinition(SGV));
183  // Linkage stays external_weak.
184  return SGV->getLinkage();
185 
187  // Linkage stays common on definitions.
188  // The ThinLTO pass will eventually force-import their definitions.
189  return SGV->getLinkage();
190  }
191 
192  llvm_unreachable("unknown linkage type");
193 }
194 
195 void FunctionImportGlobalProcessing::processGlobalForThinLTO(GlobalValue &GV) {
196 
197  ValueInfo VI;
198  if (GV.hasName()) {
199  VI = ImportIndex.getValueInfo(GV.getGUID());
200  // Set synthetic function entry counts.
201  if (VI && ImportIndex.hasSyntheticEntryCounts()) {
202  if (Function *F = dyn_cast<Function>(&GV)) {
203  if (!F->isDeclaration()) {
204  for (auto &S : VI.getSummaryList()) {
205  auto *FS = cast<FunctionSummary>(S->getBaseObject());
206  if (FS->modulePath() == M.getModuleIdentifier()) {
207  F->setEntryCount(Function::ProfileCount(FS->entryCount(),
209  break;
210  }
211  }
212  }
213  }
214  }
215  }
216 
217  // We should always have a ValueInfo (i.e. GV in index) for definitions when
218  // we are exporting, and also when importing that value.
219  assert(VI || GV.isDeclaration() ||
220  (isPerformingImport() && !doImportAsDefinition(&GV)));
221 
222  // Mark read/write-only variables which can be imported with specific
223  // attribute. We can't internalize them now because IRMover will fail
224  // to link variable definitions to their external declarations during
225  // ThinLTO import. We'll internalize read-only variables later, after
226  // import is finished. See internalizeGVsAfterImport.
227  //
228  // If global value dead stripping is not enabled in summary then
229  // propagateConstants hasn't been run. We can't internalize GV
230  // in such case.
231  if (!GV.isDeclaration() && VI && ImportIndex.withAttributePropagation()) {
232  if (GlobalVariable *V = dyn_cast<GlobalVariable>(&GV)) {
233  // We can have more than one local with the same GUID, in the case of
234  // same-named locals in different but same-named source files that were
235  // compiled in their respective directories (so the source file name
236  // and resulting GUID is the same). Find the one in this module.
237  // Handle the case where there is no summary found in this module. That
238  // can happen in the distributed ThinLTO backend, because the index only
239  // contains summaries from the source modules if they are being imported.
240  // We might have a non-null VI and get here even in that case if the name
241  // matches one in this module (e.g. weak or appending linkage).
242  auto *GVS = dyn_cast_or_null<GlobalVarSummary>(
243  ImportIndex.findSummaryInModule(VI, M.getModuleIdentifier()));
244  if (GVS &&
245  (ImportIndex.isReadOnly(GVS) || ImportIndex.isWriteOnly(GVS))) {
246  V->addAttribute("thinlto-internalize");
247  // Objects referenced by writeonly GV initializer should not be
248  // promoted, because there is no any kind of read access to them
249  // on behalf of this writeonly GV. To avoid promotion we convert
250  // GV initializer to 'zeroinitializer'. This effectively drops
251  // references in IR module (not in combined index), so we can
252  // ignore them when computing import. We do not export references
253  // of writeonly object. See computeImportForReferencedGlobals
254  if (ImportIndex.isWriteOnly(GVS))
255  V->setInitializer(Constant::getNullValue(V->getValueType()));
256  }
257  }
258  }
259 
260  if (GV.hasLocalLinkage() && shouldPromoteLocalToGlobal(&GV, VI)) {
261  // Save the original name string before we rename GV below.
262  auto Name = GV.getName().str();
263  GV.setName(getPromotedName(&GV));
264  GV.setLinkage(getLinkage(&GV, /* DoPromote */ true));
265  assert(!GV.hasLocalLinkage());
267 
268  // If we are renaming a COMDAT leader, ensure that we record the COMDAT
269  // for later renaming as well. This is required for COFF.
270  if (const auto *C = GV.getComdat())
271  if (C->getName() == Name)
272  RenamedComdats.try_emplace(C, M.getOrInsertComdat(GV.getName()));
273  } else
274  GV.setLinkage(getLinkage(&GV, /* DoPromote */ false));
275 
276  // When ClearDSOLocalOnDeclarations is true, clear dso_local if GV is
277  // converted to a declaration, to disable direct access. Don't do this if GV
278  // is implicitly dso_local due to a non-default visibility.
279  if (ClearDSOLocalOnDeclarations &&
280  (GV.isDeclarationForLinker() ||
281  (isPerformingImport() && !doImportAsDefinition(&GV))) &&
282  !GV.isImplicitDSOLocal()) {
283  GV.setDSOLocal(false);
284  } else if (VI && VI.isDSOLocal(ImportIndex.withDSOLocalPropagation())) {
285  // If all summaries are dso_local, symbol gets resolved to a known local
286  // definition.
287  GV.setDSOLocal(true);
288  if (GV.hasDLLImportStorageClass())
290  }
291 
292  // Remove functions imported as available externally defs from comdats,
293  // as this is a declaration for the linker, and will be dropped eventually.
294  // It is illegal for comdats to contain declarations.
295  auto *GO = dyn_cast<GlobalObject>(&GV);
296  if (GO && GO->isDeclarationForLinker() && GO->hasComdat()) {
297  // The IRMover should not have placed any imported declarations in
298  // a comdat, so the only declaration that should be in a comdat
299  // at this point would be a definition imported as available_externally.
300  assert(GO->hasAvailableExternallyLinkage() &&
301  "Expected comdat on definition (possibly available external)");
302  GO->setComdat(nullptr);
303  }
304 }
305 
306 void FunctionImportGlobalProcessing::processGlobalsForThinLTO() {
307  for (GlobalVariable &GV : M.globals())
308  processGlobalForThinLTO(GV);
309  for (Function &SF : M)
310  processGlobalForThinLTO(SF);
311  for (GlobalAlias &GA : M.aliases())
312  processGlobalForThinLTO(GA);
313 
314  // Replace any COMDATS that required renaming (because the COMDAT leader was
315  // promoted and renamed).
316  if (!RenamedComdats.empty())
317  for (auto &GO : M.global_objects())
318  if (auto *C = GO.getComdat()) {
319  auto Replacement = RenamedComdats.find(C);
320  if (Replacement != RenamedComdats.end())
321  GO.setComdat(Replacement->second);
322  }
323 }
324 
326  processGlobalsForThinLTO();
327  return false;
328 }
329 
331  bool ClearDSOLocalOnDeclarations,
332  SetVector<GlobalValue *> *GlobalsToImport) {
333  FunctionImportGlobalProcessing ThinLTOProcessing(M, Index, GlobalsToImport,
334  ClearDSOLocalOnDeclarations);
335  return ThinLTOProcessing.run();
336 }
llvm::FunctionImportGlobalProcessing::run
bool run()
Definition: FunctionImportUtils.cpp:325
llvm::ModuleSummaryIndex::hasSyntheticEntryCounts
bool hasSyntheticEntryCounts() const
Definition: ModuleSummaryIndex.h:1231
FunctionImportUtils.h
llvm
---------------------— PointerInfo ------------------------------------—
Definition: AllocatorList.h:23
M
We currently emits eax Perhaps this is what we really should generate is Is imull three or four cycles eax eax The current instruction priority is based on pattern complexity The former is more complex because it folds a load so the latter will not be emitted Perhaps we should use AddedComplexity to give LEA32r a higher priority We should always try to match LEA first since the LEA matching code does some estimate to determine whether the match is profitable if we care more about code then imull is better It s two bytes shorter than movl leal On a Pentium M
Definition: README.txt:252
llvm::GlobalValue::LinkOnceAnyLinkage
@ LinkOnceAnyLinkage
Keep one copy of function when linking (inline)
Definition: GlobalValue.h:50
llvm::ModuleSummaryIndex::isReadOnly
bool isReadOnly(const GlobalVarSummary *GVS) const
Definition: ModuleSummaryIndex.h:1224
llvm::ModuleSummaryIndex::getModuleHash
const ModuleHash & getModuleHash(const StringRef ModPath) const
Get the module SHA1 hash recorded for the given module path.
Definition: ModuleSummaryIndex.h:1394
InstIterator.h
llvm::GlobalValue::getLinkage
LinkageTypes getLinkage() const
Definition: GlobalValue.h:461
llvm::GlobalValue::HiddenVisibility
@ HiddenVisibility
The GV is hidden.
Definition: GlobalValue.h:64
llvm::GlobalValue::isImplicitDSOLocal
bool isImplicitDSOLocal() const
Definition: GlobalValue.h:275
llvm::Function
Definition: Function.h:61
llvm::renameModuleForThinLTO
bool renameModuleForThinLTO(Module &M, const ModuleSummaryIndex &Index, bool ClearDSOLocalOnDeclarations, SetVector< GlobalValue * > *GlobalsToImport=nullptr)
Perform in-place global value handling on the given Module for exported local functions renamed and p...
Definition: FunctionImportUtils.cpp:330
llvm::GlobalValue::isLocalLinkage
static bool isLocalLinkage(LinkageTypes Linkage)
Definition: GlobalValue.h:329
llvm::Value::hasName
bool hasName() const
Definition: Value.h:262
llvm::GlobalVariable
Definition: GlobalVariable.h:40
llvm::GlobalAlias
Definition: GlobalAlias.h:27
llvm::GlobalValue::hasSection
bool hasSection() const
Definition: GlobalValue.h:267
llvm::GlobalValue::DefaultStorageClass
@ DefaultStorageClass
Definition: GlobalValue.h:70
llvm::GlobalValue::LinkageTypes
LinkageTypes
An enumeration for the kinds of linkage for global values.
Definition: GlobalValue.h:47
llvm::GlobalValue::setDSOLocal
void setDSOLocal(bool Local)
Definition: GlobalValue.h:280
F
#define F(x, y, z)
Definition: MD5.cpp:56
llvm::ModuleSummaryIndex::withDSOLocalPropagation
bool withDSOLocalPropagation() const
Definition: ModuleSummaryIndex.h:1221
llvm::Function::PCT_Synthetic
@ PCT_Synthetic
Definition: Function.h:287
llvm::GlobalValue::isDeclaration
bool isDeclaration() const
Return true if the primary definition of this global value is outside of the current translation unit...
Definition: Globals.cpp:228
Constants.h
llvm::Module::globals
iterator_range< global_iterator > globals()
Definition: Module.h:611
C
(vector float) vec_cmpeq(*A, *B) C
Definition: README_ALTIVEC.txt:86
llvm::Value::setName
void setName(const Twine &Name)
Change the name of the value.
Definition: Value.cpp:364
llvm::ModuleSummaryIndex::findSummaryInModule
GlobalValueSummary * findSummaryInModule(ValueInfo VI, StringRef ModuleId) const
Find the summary for ValueInfo VI in module ModuleId, or nullptr if not found.
Definition: ModuleSummaryIndex.h:1342
llvm::GlobalValue::InternalLinkage
@ InternalLinkage
Rename collisions when linking (static functions).
Definition: GlobalValue.h:55
llvm::StringRef::str
LLVM_NODISCARD std::string str() const
str - Get the contents as an std::string.
Definition: StringRef.h:245
llvm::GlobalValue::getGUID
static GUID getGUID(StringRef GlobalName)
Return a 64-bit global unique ID constructed from global value name (i.e.
Definition: GlobalValue.h:507
llvm::ValueInfo
Struct that holds a reference to a particular GUID in a global value summary.
Definition: ModuleSummaryIndex.h:168
llvm::GlobalValue
Definition: GlobalValue.h:44
VI
@ VI
Definition: SIInstrInfo.cpp:7542
Index
uint32_t Index
Definition: ELFObjHandler.cpp:84
llvm::GlobalValue::getParent
Module * getParent()
Get the module that this global value is contained inside of...
Definition: GlobalValue.h:572
llvm::GlobalValue::WeakAnyLinkage
@ WeakAnyLinkage
Keep one copy of named function when linking (weak)
Definition: GlobalValue.h:52
llvm::ModuleSummaryIndex::getValueInfo
ValueInfo getValueInfo(const GlobalValueSummaryMapTy::value_type &R) const
Return a ValueInfo for the index value_type (convenient when iterating index).
Definition: ModuleSummaryIndex.h:1256
llvm::X86AS::FS
@ FS
Definition: X86.h:188
llvm::GlobalValue::setLinkage
void setLinkage(LinkageTypes LT)
Definition: GlobalValue.h:454
llvm::Module::getOrInsertComdat
Comdat * getOrInsertComdat(StringRef Name)
Return the Comdat in the module with the specified name.
Definition: Module.cpp:585
assert
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
llvm::GlobalValue::hasLocalLinkage
bool hasLocalLinkage() const
Definition: GlobalValue.h:445
llvm::Module::getModuleIdentifier
const std::string & getModuleIdentifier() const
Get the module identifier which is, essentially, the name of the module.
Definition: Module.h:227
llvm::Module
A Module instance is used to store all the information related to an LLVM module.
Definition: Module.h:67
llvm::GlobalValue::AppendingLinkage
@ AppendingLinkage
Special purpose, only applies to global arrays.
Definition: GlobalValue.h:54
llvm_unreachable
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
Definition: ErrorHandling.h:136
llvm::GlobalValue::WeakODRLinkage
@ WeakODRLinkage
Same, but only replaced by something equivalent.
Definition: GlobalValue.h:53
llvm::GlobalValue::setDLLStorageClass
void setDLLStorageClass(DLLStorageClassTypes C)
Definition: GlobalValue.h:265
S
add sub stmia L5 ldr r0 bl L_printf $stub Instead of a and a wouldn t it be better to do three moves *Return an aggregate type is even return S
Definition: README.txt:210
llvm::GlobalValue::AvailableExternallyLinkage
@ AvailableExternallyLinkage
Available for inspection, not emission.
Definition: GlobalValue.h:49
llvm::Value::getName
StringRef getName() const
Return a constant reference to the value's name.
Definition: Value.cpp:297
llvm::GlobalValue::CommonLinkage
@ CommonLinkage
Tentative definitions.
Definition: GlobalValue.h:58
llvm::GraphProgram::Name
Name
Definition: GraphWriter.h:52
llvm::Constant::getNullValue
static Constant * getNullValue(Type *Ty)
Constructor to create a '0' constant of arbitrary type.
Definition: Constants.cpp:346
llvm::GlobalValue::getComdat
const Comdat * getComdat() const
Definition: Globals.cpp:172
llvm::FunctionImportGlobalProcessing
Class to handle necessary GlobalValue changes required by ThinLTO function importing,...
Definition: FunctionImportUtils.h:25
llvm::GlobalValue::ExternalLinkage
@ ExternalLinkage
Externally visible function.
Definition: GlobalValue.h:48
llvm::ModuleSummaryIndex::isWriteOnly
bool isWriteOnly(const GlobalVarSummary *GVS) const
Definition: ModuleSummaryIndex.h:1227
llvm::GlobalValue::PrivateLinkage
@ PrivateLinkage
Like Internal, but omit from symbol table.
Definition: GlobalValue.h:56
llvm::ModuleSummaryIndex
Class to hold module path string table and global value map, and encapsulate methods for operating on...
Definition: ModuleSummaryIndex.h:1028
llvm::ModuleSummaryIndex::getGlobalNameForLocal
static std::string getGlobalNameForLocal(StringRef Name, ModuleHash ModHash)
Convenience method for creating a promoted global name for the given value name of a local,...
Definition: ModuleSummaryIndex.h:1402
llvm::GlobalValue::isDeclarationForLinker
bool isDeclarationForLinker() const
Definition: GlobalValue.h:534
llvm::GlobalValue::hasDLLImportStorageClass
bool hasDLLImportStorageClass() const
Definition: GlobalValue.h:259
llvm::ModuleSummaryIndex::withAttributePropagation
bool withAttributePropagation() const
Definition: ModuleSummaryIndex.h:1216
llvm::Function::ProfileCount
Class to represent profile counts.
Definition: Function.h:292
llvm::GlobalValue::ExternalWeakLinkage
@ ExternalWeakLinkage
ExternalWeak linkage description.
Definition: GlobalValue.h:57
llvm::SetVector
A vector that has set insertion semantics.
Definition: SetVector.h:40
llvm::GlobalValue::LinkOnceODRLinkage
@ LinkOnceODRLinkage
Same, but only replaced by something equivalent.
Definition: GlobalValue.h:51
llvm::GlobalValue::setVisibility
void setVisibility(VisibilityTypes V)
Definition: GlobalValue.h:235