LLVM 22.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
16using namespace llvm;
17
18/// Uses the "source_filename" instead of a Module hash ID for the suffix of
19/// promoted locals during LTO. NOTE: This requires that the source filename
20/// has a unique name / path to avoid name collisions.
22 "use-source-filename-for-promoted-locals", cl::Hidden,
23 cl::desc("Uses the source file name instead of the Module hash. "
24 "This requires that the source filename has a unique name / "
25 "path to avoid name collisions."));
26
28 "thinlto-move-symbols",
30 "Move the symbols with the given name. This will delete these symbols "
31 "wherever they are originally defined, and make sure their "
32 "linkage is External where they are imported. It is meant to be "
33 "used with the name of contextual profiling roots."),
35
37 Module &M, const ModuleSummaryIndex &Index,
38 SetVector<GlobalValue *> *GlobalsToImport, bool ClearDSOLocalOnDeclarations)
39 : M(M), ImportIndex(Index), GlobalsToImport(GlobalsToImport),
40 ClearDSOLocalOnDeclarations(ClearDSOLocalOnDeclarations) {
41 // If we have a ModuleSummaryIndex but no function to import,
42 // then this is the primary module being compiled in a ThinLTO
43 // backend compilation, and we need to see if it has functions that
44 // may be exported to another backend compilation.
45 if (!GlobalsToImport)
46 HasExportedFunctions = ImportIndex.hasExportedFunctions(M);
47
48#ifndef NDEBUG
50 // First collect those in the llvm.used set.
51 collectUsedGlobalVariables(M, Vec, /*CompilerUsed=*/false);
52 // Next collect those in the llvm.compiler.used set.
53 collectUsedGlobalVariables(M, Vec, /*CompilerUsed=*/true);
54 Used = {llvm::from_range, Vec};
55#endif
56 SymbolsToMove.insert_range(MoveSymbolGUID);
57}
58
59/// Checks if we should import SGV as a definition, otherwise import as a
60/// declaration.
61bool FunctionImportGlobalProcessing::doImportAsDefinition(
62 const GlobalValue *SGV) {
63 if (!isPerformingImport())
64 return false;
65
66 // Only import the globals requested for importing.
67 if (!GlobalsToImport->count(const_cast<GlobalValue *>(SGV)))
68 return false;
69
70 assert(!isa<GlobalAlias>(SGV) &&
71 "Unexpected global alias in the import list.");
72
73 // Otherwise yes.
74 return true;
75}
76
77bool FunctionImportGlobalProcessing::shouldPromoteLocalToGlobal(
78 const GlobalValue *SGV, ValueInfo VI) {
79 assert(SGV->hasLocalLinkage());
80
81 // Ifuncs and ifunc alias does not have summary.
82 if (isa<GlobalIFunc>(SGV) ||
83 (isa<GlobalAlias>(SGV) &&
84 isa<GlobalIFunc>(cast<GlobalAlias>(SGV)->getAliaseeObject())))
85 return false;
86
87 // Both the imported references and the original local variable must
88 // be promoted.
89 if (!isPerformingImport() && !isModuleExporting())
90 return false;
91
92 if (isPerformingImport()) {
93 assert((!GlobalsToImport->count(const_cast<GlobalValue *>(SGV)) ||
94 !isNonRenamableLocal(*SGV)) &&
95 "Attempting to promote non-renamable local");
96 // We don't know for sure yet if we are importing this value (as either
97 // a reference or a def), since we are simply walking all values in the
98 // module. But by necessity if we end up importing it and it is local,
99 // it must be promoted, so unconditionally promote all values in the
100 // importing module.
101 return true;
102 }
103
104 // When exporting, consult the index. We can have more than one local
105 // with the same GUID, in the case of same-named locals in different but
106 // same-named source files that were compiled in their respective directories
107 // (so the source file name and resulting GUID is the same). Find the one
108 // in this module.
109 auto Summary = ImportIndex.findSummaryInModule(
110 VI, SGV->getParent()->getModuleIdentifier());
111 assert(Summary && "Missing summary for global value when exporting");
112 auto Linkage = Summary->linkage();
113 if (!GlobalValue::isLocalLinkage(Linkage)) {
114 assert(!isNonRenamableLocal(*SGV) &&
115 "Attempting to promote non-renamable local");
116 return true;
117 }
118
119 return false;
120}
121
122#ifndef NDEBUG
123bool FunctionImportGlobalProcessing::isNonRenamableLocal(
124 const GlobalValue &GV) const {
125 if (!GV.hasLocalLinkage())
126 return false;
127 // This needs to stay in sync with the logic in buildModuleSummaryIndex.
128 if (GV.hasSection())
129 return true;
130 if (Used.count(const_cast<GlobalValue *>(&GV)))
131 return true;
132 return false;
133}
134#endif
135
136std::string
137FunctionImportGlobalProcessing::getPromotedName(const GlobalValue *SGV) {
138 assert(SGV->hasLocalLinkage());
139
140 // For locals that must be promoted to global scope, ensure that
141 // the promoted name uniquely identifies the copy in the original module,
142 // using the ID assigned during combined index creation.
144 !SGV->getParent()->getSourceFileName().empty()) {
146 std::replace_if(std::begin(Suffix), std::end(Suffix),
147 [&](char ch) { return !isAlnum(ch); }, '_');
149 SGV->getName(), Suffix);
150 }
151
153 SGV->getName(),
154 ImportIndex.getModuleHash(SGV->getParent()->getModuleIdentifier()));
155}
156
158FunctionImportGlobalProcessing::getLinkage(const GlobalValue *SGV,
159 bool DoPromote) {
160 // Any local variable that is referenced by an exported function needs
161 // to be promoted to global scope. Since we don't currently know which
162 // functions reference which local variables/functions, we must treat
163 // all as potentially exported if this module is exporting anything.
164 if (isModuleExporting()) {
165 if (SGV->hasLocalLinkage() && DoPromote)
167 return SGV->getLinkage();
168 }
169
170 // Otherwise, if we aren't importing, no linkage change is needed.
171 if (!isPerformingImport())
172 return SGV->getLinkage();
173
174 switch (SGV->getLinkage()) {
177 // External and linkonce definitions are converted to available_externally
178 // definitions upon import, so that they are available for inlining
179 // and/or optimization, but are turned into declarations later
180 // during the EliminateAvailableExternally pass.
181 if (doImportAsDefinition(SGV) && !isa<GlobalAlias>(SGV))
182 return SymbolsToMove.contains(SGV->getGUID())
185 // An imported external declaration stays external.
186 return SGV->getLinkage();
187
189 // An imported available_externally definition converts
190 // to external if imported as a declaration.
191 if (!doImportAsDefinition(SGV))
193 // An imported available_externally declaration stays that way.
194 return SGV->getLinkage();
195
198 // Can't import linkonce_any/weak_any definitions correctly, or we might
199 // change the program semantics, since the linker will pick the first
200 // linkonce_any/weak_any definition and importing would change the order
201 // they are seen by the linker. The module linking caller needs to enforce
202 // this.
203 assert(!doImportAsDefinition(SGV));
204 // If imported as a declaration, it becomes external_weak.
205 return SGV->getLinkage();
206
208 // For weak_odr linkage, there is a guarantee that all copies will be
209 // equivalent, so the issue described above for weak_any does not exist,
210 // and the definition can be imported. It can be treated similarly
211 // to an imported externally visible global value.
212 if (doImportAsDefinition(SGV) && !isa<GlobalAlias>(SGV))
214 else
216
218 // It would be incorrect to import an appending linkage variable,
219 // since it would cause global constructors/destructors to be
220 // executed multiple times. This should have already been handled
221 // by linkIfNeeded, and we will assert in shouldLinkFromSource
222 // if we try to import, so we simply return AppendingLinkage.
224
227 // If we are promoting the local to global scope, it is handled
228 // similarly to a normal externally visible global.
229 if (DoPromote) {
230 if (doImportAsDefinition(SGV) && !isa<GlobalAlias>(SGV))
232 else
234 }
235 // A non-promoted imported local definition stays local.
236 // The ThinLTO pass will eventually force-import their definitions.
237 return SGV->getLinkage();
238
240 // External weak doesn't apply to definitions, must be a declaration.
241 assert(!doImportAsDefinition(SGV));
242 // Linkage stays external_weak.
243 return SGV->getLinkage();
244
246 // Linkage stays common on definitions.
247 // The ThinLTO pass will eventually force-import their definitions.
248 return SGV->getLinkage();
249 }
250
251 llvm_unreachable("unknown linkage type");
252}
253
254void FunctionImportGlobalProcessing::processGlobalForThinLTO(GlobalValue &GV) {
255
257 if (GV.hasName())
258 VI = ImportIndex.getValueInfo(GV.getGUID());
259
260 // We should always have a ValueInfo (i.e. GV in index) for definitions when
261 // we are exporting, and also when importing that value.
262 assert(VI || GV.isDeclaration() ||
263 (isPerformingImport() && !doImportAsDefinition(&GV)));
264
265 // Mark read/write-only variables which can be imported with specific
266 // attribute. We can't internalize them now because IRMover will fail
267 // to link variable definitions to their external declarations during
268 // ThinLTO import. We'll internalize read-only variables later, after
269 // import is finished. See internalizeGVsAfterImport.
270 //
271 // If global value dead stripping is not enabled in summary then
272 // propagateConstants hasn't been run. We can't internalize GV
273 // in such case.
274 if (!GV.isDeclaration() && VI && ImportIndex.withAttributePropagation()) {
275 if (GlobalVariable *V = dyn_cast<GlobalVariable>(&GV)) {
276 // We can have more than one local with the same GUID, in the case of
277 // same-named locals in different but same-named source files that were
278 // compiled in their respective directories (so the source file name
279 // and resulting GUID is the same). Find the one in this module.
280 // Handle the case where there is no summary found in this module. That
281 // can happen in the distributed ThinLTO backend, because the index only
282 // contains summaries from the source modules if they are being imported.
283 // We might have a non-null VI and get here even in that case if the name
284 // matches one in this module (e.g. weak or appending linkage).
285 auto *GVS = dyn_cast_or_null<GlobalVarSummary>(
286 ImportIndex.findSummaryInModule(VI, M.getModuleIdentifier()));
287 if (GVS &&
288 (ImportIndex.isReadOnly(GVS) || ImportIndex.isWriteOnly(GVS))) {
289 V->addAttribute("thinlto-internalize");
290 // Objects referenced by writeonly GV initializer should not be
291 // promoted, because there is no any kind of read access to them
292 // on behalf of this writeonly GV. To avoid promotion we convert
293 // GV initializer to 'zeroinitializer'. This effectively drops
294 // references in IR module (not in combined index), so we can
295 // ignore them when computing import. We do not export references
296 // of writeonly object. See computeImportForReferencedGlobals
297 if (ImportIndex.isWriteOnly(GVS))
298 V->setInitializer(Constant::getNullValue(V->getValueType()));
299 }
300 }
301 }
302
303 if (GV.hasLocalLinkage() && shouldPromoteLocalToGlobal(&GV, VI)) {
304 // Save the original name string before we rename GV below.
305 auto Name = GV.getName().str();
306 GV.setName(getPromotedName(&GV));
307 GV.setLinkage(getLinkage(&GV, /* DoPromote */ true));
308 assert(!GV.hasLocalLinkage());
310
311 // If we are renaming a COMDAT leader, ensure that we record the COMDAT
312 // for later renaming as well. This is required for COFF.
313 if (const auto *C = GV.getComdat())
314 if (C->getName() == Name)
315 RenamedComdats.try_emplace(C, M.getOrInsertComdat(GV.getName()));
316 } else
317 GV.setLinkage(getLinkage(&GV, /* DoPromote */ false));
318
319 // When ClearDSOLocalOnDeclarations is true, clear dso_local if GV is
320 // converted to a declaration, to disable direct access. Don't do this if GV
321 // is implicitly dso_local due to a non-default visibility.
322 if (ClearDSOLocalOnDeclarations &&
324 (isPerformingImport() && !doImportAsDefinition(&GV))) &&
325 !GV.isImplicitDSOLocal()) {
326 GV.setDSOLocal(false);
327 } else if (VI && VI.isDSOLocal(ImportIndex.withDSOLocalPropagation())) {
328 // If all summaries are dso_local, symbol gets resolved to a known local
329 // definition.
330 GV.setDSOLocal(true);
333 }
334
335 // Remove functions imported as available externally defs from comdats,
336 // as this is a declaration for the linker, and will be dropped eventually.
337 // It is illegal for comdats to contain declarations.
338 auto *GO = dyn_cast<GlobalObject>(&GV);
339 if (GO && GO->isDeclarationForLinker() && GO->hasComdat()) {
340 // The IRMover should not have placed any imported declarations in
341 // a comdat, so the only declaration that should be in a comdat
342 // at this point would be a definition imported as available_externally.
343 assert(GO->hasAvailableExternallyLinkage() &&
344 "Expected comdat on definition (possibly available external)");
345 GO->setComdat(nullptr);
346 }
347}
348
349void FunctionImportGlobalProcessing::processGlobalsForThinLTO() {
350 for (GlobalVariable &GV : M.globals())
351 processGlobalForThinLTO(GV);
352 for (Function &SF : M)
353 processGlobalForThinLTO(SF);
354 for (GlobalAlias &GA : M.aliases())
355 processGlobalForThinLTO(GA);
356
357 // Replace any COMDATS that required renaming (because the COMDAT leader was
358 // promoted and renamed).
359 if (!RenamedComdats.empty())
360 for (auto &GO : M.global_objects())
361 if (auto *C = GO.getComdat()) {
362 auto Replacement = RenamedComdats.find(C);
363 if (Replacement != RenamedComdats.end())
364 GO.setComdat(Replacement->second);
365 }
366}
367
368void FunctionImportGlobalProcessing::run() { processGlobalsForThinLTO(); }
369
371 bool ClearDSOLocalOnDeclarations,
372 SetVector<GlobalValue *> *GlobalsToImport) {
373 FunctionImportGlobalProcessing ThinLTOProcessing(M, Index, GlobalsToImport,
374 ClearDSOLocalOnDeclarations);
375 ThinLTOProcessing.run();
376}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
std::string Name
static cl::opt< bool > UseSourceFilenameForPromotedLocals("use-source-filename-for-promoted-locals", cl::Hidden, cl::desc("Uses the source file name instead of the Module hash. " "This requires that the source filename has a unique name / " "path to avoid name collisions."))
Uses the "source_filename" instead of a Module hash ID for the suffix of promoted locals during LTO.
cl::list< GlobalValue::GUID > MoveSymbolGUID("thinlto-move-symbols", cl::desc("Move the symbols with the given name. This will delete these symbols " "wherever they are originally defined, and make sure their " "linkage is External where they are imported. It is meant to be " "used with the name of contextual profiling roots."), cl::Hidden)
cl::list< GlobalValue::GUID > MoveSymbolGUID
static LLVM_ABI Constant * getNullValue(Type *Ty)
Constructor to create a '0' constant of arbitrary type.
Definition: Constants.cpp:373
Class to handle necessary GlobalValue changes required by ThinLTO function importing,...
LLVM_ABI FunctionImportGlobalProcessing(Module &M, const ModuleSummaryIndex &Index, SetVector< GlobalValue * > *GlobalsToImport, bool ClearDSOLocalOnDeclarations)
bool isImplicitDSOLocal() const
Definition: GlobalValue.h:300
static bool isLocalLinkage(LinkageTypes Linkage)
Definition: GlobalValue.h:411
LLVM_ABI bool isDeclaration() const
Return true if the primary definition of this global value is outside of the current translation unit...
Definition: Globals.cpp:316
LinkageTypes getLinkage() const
Definition: GlobalValue.h:548
bool hasLocalLinkage() const
Definition: GlobalValue.h:530
void setDLLStorageClass(DLLStorageClassTypes C)
Definition: GlobalValue.h:286
LLVM_ABI const Comdat * getComdat() const
Definition: Globals.cpp:201
bool hasDLLImportStorageClass() const
Definition: GlobalValue.h:280
void setLinkage(LinkageTypes LT)
Definition: GlobalValue.h:539
bool isDeclarationForLinker() const
Definition: GlobalValue.h:625
GUID getGUID() const
Return a 64-bit global unique ID constructed from global value name (i.e.
Definition: GlobalValue.h:600
Module * getParent()
Get the module that this global value is contained inside of...
Definition: GlobalValue.h:663
void setDSOLocal(bool Local)
Definition: GlobalValue.h:305
@ HiddenVisibility
The GV is hidden.
Definition: GlobalValue.h:69
bool hasSection() const
Definition: GlobalValue.h:292
void setVisibility(VisibilityTypes V)
Definition: GlobalValue.h:256
LinkageTypes
An enumeration for the kinds of linkage for global values.
Definition: GlobalValue.h:52
@ PrivateLinkage
Like Internal, but omit from symbol table.
Definition: GlobalValue.h:61
@ CommonLinkage
Tentative definitions.
Definition: GlobalValue.h:63
@ InternalLinkage
Rename collisions when linking (static functions).
Definition: GlobalValue.h:60
@ LinkOnceAnyLinkage
Keep one copy of function when linking (inline)
Definition: GlobalValue.h:55
@ WeakODRLinkage
Same, but only replaced by something equivalent.
Definition: GlobalValue.h:58
@ ExternalLinkage
Externally visible function.
Definition: GlobalValue.h:53
@ WeakAnyLinkage
Keep one copy of named function when linking (weak)
Definition: GlobalValue.h:57
@ AppendingLinkage
Special purpose, only applies to global arrays.
Definition: GlobalValue.h:59
@ AvailableExternallyLinkage
Available for inspection, not emission.
Definition: GlobalValue.h:54
@ ExternalWeakLinkage
ExternalWeak linkage description.
Definition: GlobalValue.h:62
@ LinkOnceODRLinkage
Same, but only replaced by something equivalent.
Definition: GlobalValue.h:56
Class to hold module path string table and global value map, and encapsulate methods for operating on...
bool isReadOnly(const GlobalVarSummary *GVS) const
bool isWriteOnly(const GlobalVarSummary *GVS) const
ValueInfo getValueInfo(const GlobalValueSummaryMapTy::value_type &R) const
Return a ValueInfo for the index value_type (convenient when iterating index).
const ModuleHash & getModuleHash(const StringRef ModPath) const
Get the module SHA1 hash recorded for the given module path.
bool hasExportedFunctions(const Module &M) const
Check if the given Module has any functions available for exporting in the index.
static std::string getGlobalNameForLocal(StringRef Name, ModuleHash ModHash)
Convenience method for creating a promoted global name for the given value name of a local,...
GlobalValueSummary * findSummaryInModule(ValueInfo VI, StringRef ModuleId) const
Find the summary for ValueInfo VI in module ModuleId, or nullptr if not found.
A Module instance is used to store all the information related to an LLVM module.
Definition: Module.h:67
const std::string & getSourceFileName() const
Get the module's original source file name.
Definition: Module.h:263
iterator_range< global_iterator > globals()
Definition: Module.h:684
const std::string & getModuleIdentifier() const
Get the module identifier which is, essentially, the name of the module.
Definition: Module.h:252
Comdat * getOrInsertComdat(StringRef Name)
Return the Comdat in the module with the specified name.
Definition: Module.cpp:609
A vector that has set insertion semantics.
Definition: SetVector.h:59
SmallString - A SmallString is just a SmallVector with methods and accessors that make it work better...
Definition: SmallString.h:26
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1197
std::string str() const
str - Get the contents as an std::string.
Definition: StringRef.h:233
LLVM_ABI void setName(const Twine &Name)
Change the name of the value.
Definition: Value.cpp:390
bool hasName() const
Definition: Value.h:262
LLVM_ABI StringRef getName() const
Return a constant reference to the value's name.
Definition: Value.cpp:322
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
@ C
The default llvm calling convention, compatible with C.
Definition: CallingConv.h:34
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
constexpr from_range_t from_range
LLVM_ABI void 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...
LLVM_ABI GlobalVariable * collectUsedGlobalVariables(const Module &M, SmallVectorImpl< GlobalValue * > &Vec, bool CompilerUsed)
Given "llvm.used" or "llvm.compiler.used" as a global name, collect the initializer elements of that ...
Definition: Module.cpp:863
Struct that holds a reference to a particular GUID in a global value summary.