LLVM: lib/Transforms/Utils/FunctionImportUtils.cpp Source File (original) (raw)
1
2
3
4
5
6
7
8
9
10
11
12
13
17
18using namespace llvm;
19
20namespace llvm {
21
22
23
24
26 "use-source-filename-for-promoted-locals", cl::Hidden,
27 cl::desc("Uses the source file name instead of the Module hash. "
28 "This requires that the source filename has a unique name / "
29 "path to avoid name collisions."));
30
32 "thinlto-move-symbols",
34 "Move the symbols with the given name. This will delete these symbols "
35 "wherever they are originally defined, and make sure their "
36 "linkage is External where they are imported. It is meant to be "
37 "used with the name of contextual profiling roots."),
39
40}
41
45 : M(M), ImportIndex(Index), GlobalsToImport(GlobalsToImport),
46 ClearDSOLocalOnDeclarations(ClearDSOLocalOnDeclarations) {
47
48
49
50
51 if (!GlobalsToImport)
52 HasExportedFunctions = ImportIndex.hasExportedFunctions(M);
53
56
58
61#endif
63}
64
65
66
67bool FunctionImportGlobalProcessing::doImportAsDefinition(
69 if (!isPerformingImport())
70 return false;
71
72
73 if (!GlobalsToImport->count(const_cast<GlobalValue *>(SGV)))
74 return false;
75
77 "Unexpected global alias in the import list.");
78
79
80 return true;
81}
82
83bool FunctionImportGlobalProcessing::shouldPromoteLocalToGlobal(
86
87
91 return false;
92
93
94
95 if (!isPerformingImport() && !isModuleExporting())
96 return false;
97
98 if (isPerformingImport()) {
99 assert((!GlobalsToImport->count(const_cast<GlobalValue *>(SGV)) ||
101 "Attempting to promote non-renamable local");
102
103
104
105
106
107 return true;
108 }
109
110
111
112
113
114
115 auto Summary = ImportIndex.findSummaryInModule(
117 assert(Summary && "Missing summary for global value when exporting");
120 assert(!isNonRenamableLocal(*SGV) &&
121 "Attempting to promote non-renamable local");
122 return true;
123 }
124
125 return false;
126}
127
128#ifndef NDEBUG
129bool FunctionImportGlobalProcessing::isNonRenamableLocal(
132 return false;
133
135 return true;
136 if (Used.count(const_cast<GlobalValue *>(&GV)))
137 return true;
138 return false;
139}
140#endif
141
142std::string
143FunctionImportGlobalProcessing::getPromotedName(const GlobalValue *SGV) {
145
146
147
148
152 std::replace_if(std::begin(Suffix), std::end(Suffix),
153 [&](char ch) { return (ch); }, '_');
156 }
157
161}
162
164FunctionImportGlobalProcessing::getLinkage(const GlobalValue *SGV,
165 bool DoPromote) {
166
167
168
169
170 if (isModuleExporting()) {
174 }
175
176
177 if (!isPerformingImport())
179
183
184
185
186
188 return SymbolsToMove.contains(SGV->getGUID())
191
193
195
196
197 if (!doImportAsDefinition(SGV))
199
201
204
205
206
207
208
209 assert(!doImportAsDefinition(SGV));
210
212
214
215
216
217
220 else
222
224
225
226
227
228
230
233
234
235 if (DoPromote) {
238 else
240 }
241
242
244
246
247 assert(!doImportAsDefinition(SGV));
248
250
252
253
255 }
256
258}
259
260void FunctionImportGlobalProcessing::processGlobalForThinLTO(GlobalValue &GV) {
261
262 ValueInfo VI;
264 VI = ImportIndex.getValueInfo(GV.getGUID());
265
266
267
269 (isPerformingImport() && !doImportAsDefinition(&GV)));
270
271
272
273
274
275
276
277
278
279
280 if (!GV.isDeclaration() && VI && ImportIndex.withAttributePropagation()) {
282
283
284
285
286
287
288
289
290
292 ImportIndex.findSummaryInModule(VI, M.getModuleIdentifier()));
293 if (GVS &&
294 (ImportIndex.isReadOnly(GVS) || ImportIndex.isWriteOnly(GVS))) {
295 V->addAttribute("thinlto-internalize");
296
297
298
299
300
301
302
303 if (ImportIndex.isWriteOnly(GVS))
305 }
306 }
307 }
308
309 if (GV.hasLocalLinkage() && shouldPromoteLocalToGlobal(&GV, VI)) {
310
312 GV.setName(getPromotedName(&GV));
313 GV.setLinkage(getLinkage(&GV, true));
316
317
318
320 if (C->getName() == Name)
321 RenamedComdats.try_emplace(C, M.getOrInsertComdat(GV.getName()));
322 } else
323 GV.setLinkage(getLinkage(&GV, false));
324
325
326
327
328 if (ClearDSOLocalOnDeclarations &&
330 (isPerformingImport() && !doImportAsDefinition(&GV))) &&
333 } else if (VI && VI.isDSOLocal(ImportIndex.withDSOLocalPropagation())) {
334
335
339 }
340
341
342
343
345 if (GO && GO->isDeclarationForLinker() && GO->hasComdat()) {
346
347
348
349 assert(GO->hasAvailableExternallyLinkage() &&
350 "Expected comdat on definition (possibly available external)");
351 GO->setComdat(nullptr);
352 }
353}
354
355void FunctionImportGlobalProcessing::processGlobalsForThinLTO() {
356 for (GlobalVariable &GV : M.globals())
357 processGlobalForThinLTO(GV);
358 for (Function &SF : M)
359 processGlobalForThinLTO(SF);
360 for (GlobalAlias &GA : M.aliases())
361 processGlobalForThinLTO(GA);
362
363
364
365 if (!RenamedComdats.empty())
366 for (auto &GO : M.global_objects())
367 if (auto *C = GO.getComdat()) {
368 auto Replacement = RenamedComdats.find(C);
369 if (Replacement != RenamedComdats.end())
370 GO.setComdat(Replacement->second);
371 }
372}
373
375
377 bool ClearDSOLocalOnDeclarations,
381 ClearDSOLocalOnDeclarations);
382 ThinLTOProcessing.run();
383}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
static bool isNonRenamableLocal(const GlobalValue &GV)
static LLVM_ABI Constant * getNullValue(Type *Ty)
Constructor to create a '0' constant of arbitrary type.
Class to handle necessary GlobalValue changes required by ThinLTO function importing,...
LLVM_ABI FunctionImportGlobalProcessing(Module &M, const ModuleSummaryIndex &Index, SetVector< GlobalValue * > *GlobalsToImport, bool ClearDSOLocalOnDeclarations)
Definition FunctionImportUtils.cpp:42
LLVM_ABI void run()
Definition FunctionImportUtils.cpp:374
bool isImplicitDSOLocal() const
static bool isLocalLinkage(LinkageTypes Linkage)
LLVM_ABI bool isDeclaration() const
Return true if the primary definition of this global value is outside of the current translation unit...
LinkageTypes getLinkage() const
bool hasLocalLinkage() const
void setDLLStorageClass(DLLStorageClassTypes C)
LLVM_ABI const Comdat * getComdat() const
bool hasDLLImportStorageClass() const
void setLinkage(LinkageTypes LT)
bool isDeclarationForLinker() const
GUID getGUID() const
Return a 64-bit global unique ID constructed from global value name (i.e.
Module * getParent()
Get the module that this global value is contained inside of...
void setDSOLocal(bool Local)
@ HiddenVisibility
The GV is hidden.
void setVisibility(VisibilityTypes V)
LinkageTypes
An enumeration for the kinds of linkage for global values.
@ PrivateLinkage
Like Internal, but omit from symbol table.
@ CommonLinkage
Tentative definitions.
@ InternalLinkage
Rename collisions when linking (static functions).
@ LinkOnceAnyLinkage
Keep one copy of function when linking (inline)
@ WeakODRLinkage
Same, but only replaced by something equivalent.
@ ExternalLinkage
Externally visible function.
@ WeakAnyLinkage
Keep one copy of named function when linking (weak)
@ AppendingLinkage
Special purpose, only applies to global arrays.
@ AvailableExternallyLinkage
Available for inspection, not emission.
@ ExternalWeakLinkage
ExternalWeak linkage description.
@ LinkOnceODRLinkage
Same, but only replaced by something equivalent.
Class to hold module path string table and global value map, and encapsulate methods for operating on...
static std::string getGlobalNameForLocal(StringRef Name, ModuleHash ModHash)
Convenience method for creating a promoted global name for the given value name of a local,...
A Module instance is used to store all the information related to an LLVM module.
const std::string & getSourceFileName() const
Get the module's original source file name.
const std::string & getModuleIdentifier() const
Get the module identifier which is, essentially, the name of the module.
A vector that has set insertion semantics.
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
std::string str() const
str - Get the contents as an std::string.
The TimeTraceScope is a helper class to call the begin and end functions of the time trace profiler.
LLVM_ABI void setName(const Twine &Name)
Change the name of the value.
LLVM_ABI StringRef getName() const
Return a constant reference to the value's name.
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
@ C
The default llvm calling convention, compatible with C.
This is an optimization pass for GlobalISel generic memory operations.
cl::list< GlobalValue::GUID > MoveSymbolGUID
decltype(auto) dyn_cast(const From &Val)
dyn_cast - Return the argument parameter cast to the specified type.
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...
Definition FunctionImportUtils.cpp:376
auto dyn_cast_or_null(const Y &Val)
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.
bool isAlnum(char C)
Checks whether character C is either a decimal digit or an uppercase or lowercase letter as classifie...
bool isa(const From &Val)
isa - Return true if the parameter to the template is an instance of one of the template type argu...
decltype(auto) cast(const From &Val)
cast - Return the argument parameter cast to the specified type.
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 ...
Struct that holds a reference to a particular GUID in a global value summary.