LLVM: lib/Transforms/Utils/FunctionImportUtils.cpp Source File (original) (raw)
1
2
3
4
5
6
7
8
9
10
11
12
13
16using namespace llvm;
17
18
19
20
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
27
28
29bool FunctionImportGlobalProcessing::doImportAsDefinition(
31 if (!isPerformingImport())
32 return false;
33
34
35 if (!GlobalsToImport->count(const_cast<GlobalValue *>(SGV)))
36 return false;
37
38 assert(!isa(SGV) &&
39 "Unexpected global alias in the import list.");
40
41
42 return true;
43}
44
45bool FunctionImportGlobalProcessing::shouldPromoteLocalToGlobal(
48
49
50 if (isa(SGV) ||
51 (isa(SGV) &&
52 isa(cast(SGV)->getAliaseeObject())))
53 return false;
54
55
56
57 if (!isPerformingImport() && !isModuleExporting())
58 return false;
59
60 if (isPerformingImport()) {
61 assert((!GlobalsToImport->count(const_cast<GlobalValue *>(SGV)) ||
62 !isNonRenamableLocal(*SGV)) &&
63 "Attempting to promote non-renamable local");
64
65
66
67
68
69 return true;
70 }
71
72
73
74
75
76
79 assert(Summary && "Missing summary for global value when exporting");
82 assert(!isNonRenamableLocal(*SGV) &&
83 "Attempting to promote non-renamable local");
84 return true;
85 }
86
87 return false;
88}
89
90#ifndef NDEBUG
91bool FunctionImportGlobalProcessing::isNonRenamableLocal(
94 return false;
95
97 return true;
98 if (Used.count(const_cast<GlobalValue *>(&GV)))
99 return true;
100 return false;
101}
102#endif
103
104std::string
105FunctionImportGlobalProcessing::getPromotedName(const GlobalValue *SGV) {
107
108
109
110
114 std::replace_if(std::begin(Suffix), std::end(Suffix),
115 [&](char ch) { return !isAlnum(ch); }, '_');
118 }
119
123}
124
126FunctionImportGlobalProcessing::getLinkage(const GlobalValue *SGV,
127 bool DoPromote) {
128
129
130
131
132 if (isModuleExporting()) {
136 }
137
138
139 if (!isPerformingImport())
141
145
146
147
148
149 if (doImportAsDefinition(SGV) && !isa(SGV))
151
153
155
156
157 if (!doImportAsDefinition(SGV))
159
161
164
165
166
167
168
169 assert(!doImportAsDefinition(SGV));
170
172
174
175
176
177
178 if (doImportAsDefinition(SGV) && !isa(SGV))
180 else
182
184
185
186
187
188
190
193
194
195 if (DoPromote) {
196 if (doImportAsDefinition(SGV) && !isa(SGV))
198 else
200 }
201
202
204
206
207 assert(!doImportAsDefinition(SGV));
208
210
212
213
215 }
216
218}
219
220void FunctionImportGlobalProcessing::processGlobalForThinLTO(GlobalValue &GV) {
221
225
226
227
229 (isPerformingImport() && !doImportAsDefinition(&GV)));
230
231
232
233
234
235
236
237
238
239
241 if (GlobalVariable *V = dyn_cast(&GV)) {
242
243
244
245
246
247
248
249
250
251 auto *GVS = dyn_cast_or_null(
253 if (GVS &&
255 V->addAttribute("thinlto-internalize");
256
257
258
259
260
261
262
265 }
266 }
267 }
268
269 if (GV.hasLocalLinkage() && shouldPromoteLocalToGlobal(&GV, VI)) {
270
272 GV.setName(getPromotedName(&GV));
273 GV.setLinkage(getLinkage(&GV, true));
276
277
278
282 } else
283 GV.setLinkage(getLinkage(&GV, false));
284
285
286
287
288 if (ClearDSOLocalOnDeclarations &&
290 (isPerformingImport() && !doImportAsDefinition(&GV))) &&
294
295
299 }
300
301
302
303
304 auto *GO = dyn_cast(&GV);
305 if (GO && GO->isDeclarationForLinker() && GO->hasComdat()) {
306
307
308
309 assert(GO->hasAvailableExternallyLinkage() &&
310 "Expected comdat on definition (possibly available external)");
311 GO->setComdat(nullptr);
312 }
313}
314
315void FunctionImportGlobalProcessing::processGlobalsForThinLTO() {
317 processGlobalForThinLTO(GV);
319 processGlobalForThinLTO(SF);
321 processGlobalForThinLTO(GA);
322
323
324
325 if (!RenamedComdats.empty())
326 for (auto &GO : M.global_objects())
327 if (auto *C = GO.getComdat()) {
328 auto Replacement = RenamedComdats.find(C);
329 if (Replacement != RenamedComdats.end())
330 GO.setComdat(Replacement->second);
331 }
332}
333
335
337 bool ClearDSOLocalOnDeclarations,
340 ClearDSOLocalOnDeclarations);
341 ThinLTOProcessing.run();
342}
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.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
static Constant * getNullValue(Type *Ty)
Constructor to create a '0' constant of arbitrary type.
Class to handle necessary GlobalValue changes required by ThinLTO function importing,...
bool isImplicitDSOLocal() const
static bool isLocalLinkage(LinkageTypes Linkage)
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)
const Comdat * getComdat() const
bool hasDLLImportStorageClass() const
static GUID getGUID(StringRef GlobalName)
Return a 64-bit global unique ID constructed from global value name (i.e.
void setLinkage(LinkageTypes LT)
bool isDeclarationForLinker() const
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...
bool withAttributePropagation() const
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.
static std::string getGlobalNameForLocal(StringRef Name, ModuleHash ModHash)
Convenience method for creating a promoted global name for the given value name of a local,...
bool withDSOLocalPropagation() const
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.
const std::string & getSourceFileName() const
Get the module's original source file name.
iterator_range< global_iterator > globals()
const std::string & getModuleIdentifier() const
Get the module identifier which is, essentially, the name of the module.
Comdat * getOrInsertComdat(StringRef Name)
Return the Comdat in the module with the specified name.
A vector that has set insertion semantics.
SmallString - A SmallString is just a SmallVector with methods and accessors that make it work better...
std::string str() const
str - Get the contents as an std::string.
void setName(const Twine &Name)
Change the name of the value.
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.
Linkage
Describes symbol linkage. This can be used to resolve definition clashes.
This is an optimization pass for GlobalISel generic memory operations.
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...
Struct that holds a reference to a particular GUID in a global value summary.