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
14#include "llvm/Transforms/Utils/FunctionImportUtils.h"
15#include "llvm/Support/CommandLine.h"
16#include "llvm/Support/TimeProfiler.h"
17
18using namespace llvm;
19
20namespace llvm {
21
22/// Uses the "source_filename" instead of a Module hash ID for the suffix of
23/// promoted locals during LTO. NOTE: This requires that the source filename
24/// has a unique name / path to avoid name collisions.
25static cl::opt<bool> UseSourceFilenameForPromotedLocals(
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
31/// FIXME: The current optimization that avoids unnecessary renaming of
32/// promoted locals is incompatible with distributed ThinLTO and therefore
33/// must be enabled by default.
34cl::opt<bool>
35 AlwaysRenamePromotedLocals("always-rename-promoted-locals", cl::init(Val: true),
36 cl::Hidden,
37 cl::desc("Always rename promoted locals."));
38
39cl::list<GlobalValue::GUID> MoveSymbolGUID(
40 "thinlto-move-symbols",
41 cl::desc(
42 "Move the symbols with the given name. This will delete these symbols "
43 "wherever they are originally defined, and make sure their "
44 "linkage is External where they are imported. It is meant to be "
45 "used with the name of contextual profiling roots."),
46 cl::Hidden);
47
48} // end namespace llvm
49
50FunctionImportGlobalProcessing::FunctionImportGlobalProcessing(
51 Module &M, const ModuleSummaryIndex &Index,
52 SetVector<GlobalValue *> *GlobalsToImport, bool ClearDSOLocalOnDeclarations)
53 : M(M), ImportIndex(Index), GlobalsToImport(GlobalsToImport),
54 ClearDSOLocalOnDeclarations(ClearDSOLocalOnDeclarations) {
55 // If we have a ModuleSummaryIndex but no function to import,
56 // then this is the primary module being compiled in a ThinLTO
57 // backend compilation, and we need to see if it has functions that
58 // may be exported to another backend compilation.
59 if (!GlobalsToImport)
60 HasExportedFunctions = ImportIndex.hasExportedFunctions(M);
61
62#ifndef NDEBUG
63 SmallVector<GlobalValue *, 4> Vec;
64 // First collect those in the llvm.used set.
65 collectUsedGlobalVariables(M, Vec, /*CompilerUsed=*/false);
66 // Next collect those in the llvm.compiler.used set.
67 collectUsedGlobalVariables(M, Vec, /*CompilerUsed=*/true);
68 Used = {llvm::from_range, Vec};
69#endif
70 SymbolsToMove.insert_range(R&: MoveSymbolGUID);
71}
72
73/// Checks if we should import SGV as a definition, otherwise import as a
74/// declaration.
75bool FunctionImportGlobalProcessing::doImportAsDefinition(
76 const GlobalValue *SGV) {
77 if (!isPerformingImport())
78 return false;
79
80 // Only import the globals requested for importing.
81 if (!GlobalsToImport->count(key: const_cast<GlobalValue *>(SGV)))
82 return false;
83
84 assert(!isa<GlobalAlias>(SGV) &&
85 "Unexpected global alias in the import list.");
86
87 // Otherwise yes.
88 return true;
89}
90
91bool FunctionImportGlobalProcessing::shouldPromoteLocalToGlobal(
92 const GlobalValue *SGV, GlobalValueSummary *Summary) {
93 assert(SGV->hasLocalLinkage());
94
95 // Ifuncs and ifunc alias does not have summary.
96 if (isa<GlobalIFunc>(Val: SGV) ||
97 (isa<GlobalAlias>(Val: SGV) &&
98 isa<GlobalIFunc>(Val: cast<GlobalAlias>(Val: SGV)->getAliaseeObject())))
99 return false;
100
101 // Both the imported references and the original local variable must
102 // be promoted.
103 if (!isPerformingImport() && !isModuleExporting())
104 return false;
105
106 if (isPerformingImport()) {
107 assert((!GlobalsToImport->count(const_cast<GlobalValue *>(SGV)) ||
108 !isNonRenamableLocal(*SGV)) &&
109 "Attempting to promote non-renamable local");
110 // We don't know for sure yet if we are importing this value (as either
111 // a reference or a def), since we are simply walking all values in the
112 // module. But by necessity if we end up importing it and it is local,
113 // it must be promoted, so unconditionally promote all values in the
114 // importing module.
115 return true;
116 }
117
118 // When exporting, consult the index. We can have more than one local
119 // with the same GUID, in the case of same-named locals in different but
120 // same-named source files that were compiled in their respective directories
121 // (so the source file name and resulting GUID is the same). Find the one
122 // in this module.
123 assert(Summary && "Missing summary for global value when exporting");
124 auto Linkage = Summary->linkage();
125 if (!GlobalValue::isLocalLinkage(Linkage)) {
126 assert(!isNonRenamableLocal(*SGV) &&
127 "Attempting to promote non-renamable local");
128 return true;
129 }
130
131 return false;
132}
133
134#ifndef NDEBUG
135bool FunctionImportGlobalProcessing::isNonRenamableLocal(
136 const GlobalValue &GV) const {
137 if (!GV.hasLocalLinkage())
138 return false;
139 // This needs to stay in sync with the logic in buildModuleSummaryIndex.
140 if (GV.hasSection())
141 return true;
142 if (Used.count(const_cast<GlobalValue *>(&GV)))
143 return true;
144 return false;
145}
146#endif
147
148std::string
149FunctionImportGlobalProcessing::getPromotedName(const GlobalValue *SGV) {
150 assert(SGV->hasLocalLinkage());
151
152 // For locals that must be promoted to global scope, ensure that
153 // the promoted name uniquely identifies the copy in the original module,
154 // using the ID assigned during combined index creation.
155 if (UseSourceFilenameForPromotedLocals &&
156 !SGV->getParent()->getSourceFileName().empty()) {
157 SmallString<256> Suffix(SGV->getParent()->getSourceFileName());
158 std::replace_if(first: std::begin(cont&: Suffix), last: std::end(cont&: Suffix),
159 pred: [&](char ch) { return !isAlnum(C: ch); }, new_value: '_');
160 return ModuleSummaryIndex::getGlobalNameForLocal(
161 Name: SGV->getName(), Suffix);
162 }
163
164 return ModuleSummaryIndex::getGlobalNameForLocal(
165 Name: SGV->getName(),
166 ModHash: ImportIndex.getModuleHash(ModPath: SGV->getParent()->getModuleIdentifier()));
167}
168
169GlobalValue::LinkageTypes
170FunctionImportGlobalProcessing::getLinkage(const GlobalValue *SGV,
171 bool DoPromote) {
172 // Any local variable that is referenced by an exported function needs
173 // to be promoted to global scope. Since we don't currently know which
174 // functions reference which local variables/functions, we must treat
175 // all as potentially exported if this module is exporting anything.
176 if (isModuleExporting()) {
177 if (SGV->hasLocalLinkage() && DoPromote)
178 return GlobalValue::ExternalLinkage;
179 return SGV->getLinkage();
180 }
181
182 // Otherwise, if we aren't importing, no linkage change is needed.
183 if (!isPerformingImport())
184 return SGV->getLinkage();
185
186 switch (SGV->getLinkage()) {
187 case GlobalValue::LinkOnceODRLinkage:
188 case GlobalValue::ExternalLinkage:
189 // External and linkonce definitions are converted to available_externally
190 // definitions upon import, so that they are available for inlining
191 // and/or optimization, but are turned into declarations later
192 // during the EliminateAvailableExternally pass.
193 if (doImportAsDefinition(SGV) && !isa<GlobalAlias>(Val: SGV))
194 return SymbolsToMove.contains(V: SGV->getGUID())
195 ? GlobalValue::ExternalLinkage
196 : GlobalValue::AvailableExternallyLinkage;
197 // An imported external declaration stays external.
198 return SGV->getLinkage();
199
200 case GlobalValue::AvailableExternallyLinkage:
201 // An imported available_externally definition converts
202 // to external if imported as a declaration.
203 if (!doImportAsDefinition(SGV))
204 return GlobalValue::ExternalLinkage;
205 // An imported available_externally declaration stays that way.
206 return SGV->getLinkage();
207
208 case GlobalValue::LinkOnceAnyLinkage:
209 case GlobalValue::WeakAnyLinkage:
210 // Can't import linkonce_any/weak_any definitions correctly, or we might
211 // change the program semantics, since the linker will pick the first
212 // linkonce_any/weak_any definition and importing would change the order
213 // they are seen by the linker. The module linking caller needs to enforce
214 // this.
215 assert(!doImportAsDefinition(SGV));
216 // If imported as a declaration, it becomes external_weak.
217 return SGV->getLinkage();
218
219 case GlobalValue::WeakODRLinkage:
220 // For weak_odr linkage, there is a guarantee that all copies will be
221 // equivalent, so the issue described above for weak_any does not exist,
222 // and the definition can be imported. It can be treated similarly
223 // to an imported externally visible global value.
224 if (doImportAsDefinition(SGV) && !isa<GlobalAlias>(Val: SGV))
225 return GlobalValue::AvailableExternallyLinkage;
226 else
227 return GlobalValue::ExternalLinkage;
228
229 case GlobalValue::AppendingLinkage:
230 // It would be incorrect to import an appending linkage variable,
231 // since it would cause global constructors/destructors to be
232 // executed multiple times. This should have already been handled
233 // by linkIfNeeded, and we will assert in shouldLinkFromSource
234 // if we try to import, so we simply return AppendingLinkage.
235 return GlobalValue::AppendingLinkage;
236
237 case GlobalValue::InternalLinkage:
238 case GlobalValue::PrivateLinkage:
239 // If we are promoting the local to global scope, it is handled
240 // similarly to a normal externally visible global.
241 if (DoPromote) {
242 if (doImportAsDefinition(SGV) && !isa<GlobalAlias>(Val: SGV))
243 return GlobalValue::AvailableExternallyLinkage;
244 else
245 return GlobalValue::ExternalLinkage;
246 }
247 // A non-promoted imported local definition stays local.
248 // The ThinLTO pass will eventually force-import their definitions.
249 return SGV->getLinkage();
250
251 case GlobalValue::ExternalWeakLinkage:
252 // External weak doesn't apply to definitions, must be a declaration.
253 assert(!doImportAsDefinition(SGV));
254 // Linkage stays external_weak.
255 return SGV->getLinkage();
256
257 case GlobalValue::CommonLinkage:
258 // Linkage stays common on definitions.
259 // The ThinLTO pass will eventually force-import their definitions.
260 return SGV->getLinkage();
261 }
262
263 llvm_unreachable("unknown linkage type");
264}
265
266void FunctionImportGlobalProcessing::processGlobalForThinLTO(GlobalValue &GV) {
267
268 ValueInfo VI;
269 if (GV.hasName())
270 VI = ImportIndex.getValueInfo(GUID: GV.getGUID());
271
272 // We should always have a ValueInfo (i.e. GV in index) for definitions when
273 // we are exporting, and also when importing that value.
274 assert(VI || GV.isDeclaration() ||
275 (isPerformingImport() && !doImportAsDefinition(&GV)));
276
277 // Mark read/write-only variables which can be imported with specific
278 // attribute. We can't internalize them now because IRMover will fail
279 // to link variable definitions to their external declarations during
280 // ThinLTO import. We'll internalize read-only variables later, after
281 // import is finished. See internalizeGVsAfterImport.
282 //
283 // If global value dead stripping is not enabled in summary then
284 // propagateConstants hasn't been run. We can't internalize GV
285 // in such case.
286 if (!GV.isDeclaration() && VI && ImportIndex.withAttributePropagation()) {
287 if (GlobalVariable *V = dyn_cast<GlobalVariable>(Val: &GV)) {
288 // We can have more than one local with the same GUID, in the case of
289 // same-named locals in different but same-named source files that were
290 // compiled in their respective directories (so the source file name
291 // and resulting GUID is the same). Find the one in this module.
292 // Handle the case where there is no summary found in this module. That
293 // can happen in the distributed ThinLTO backend, because the index only
294 // contains summaries from the source modules if they are being imported.
295 // We might have a non-null VI and get here even in that case if the name
296 // matches one in this module (e.g. weak or appending linkage).
297 auto *GVS = dyn_cast_or_null<GlobalVarSummary>(
298 Val: ImportIndex.findSummaryInModule(VI, ModuleId: M.getModuleIdentifier()));
299 if (GVS &&
300 (ImportIndex.isReadOnly(GVS) || ImportIndex.isWriteOnly(GVS))) {
301 V->addAttribute(Kind: "thinlto-internalize");
302 // Objects referenced by writeonly GV initializer should not be
303 // promoted, because there is no any kind of read access to them
304 // on behalf of this writeonly GV. To avoid promotion we convert
305 // GV initializer to 'zeroinitializer'. This effectively drops
306 // references in IR module (not in combined index), so we can
307 // ignore them when computing import. We do not export references
308 // of writeonly object. See computeImportForReferencedGlobals
309 if (ImportIndex.isWriteOnly(GVS))
310 V->setInitializer(Constant::getNullValue(Ty: V->getValueType()));
311 }
312 }
313 }
314
315 GlobalValueSummary *Summary = nullptr;
316 if (VI && GV.hasLocalLinkage())
317 Summary = ImportIndex.findSummaryInModule(
318 VI, ModuleId: GV.getParent()->getModuleIdentifier());
319
320 if (GV.hasLocalLinkage() && shouldPromoteLocalToGlobal(SGV: &GV, Summary)) {
321 // Save the original name string before we rename GV below.
322 auto Name = GV.getName().str();
323 if (AlwaysRenamePromotedLocals || !Summary ||
324 !Summary->noRenameOnPromotion())
325 GV.setName(getPromotedName(SGV: &GV));
326
327 GV.setLinkage(getLinkage(SGV: &GV, /* DoPromote */ true));
328 assert(!GV.hasLocalLinkage());
329 GV.setVisibility(GlobalValue::HiddenVisibility);
330
331 // If we are renaming a COMDAT leader, ensure that we record the COMDAT
332 // for later renaming as well. This is required for COFF.
333 if (const auto *C = GV.getComdat())
334 if (C->getName() == Name)
335 RenamedComdats.try_emplace(Key: C, Args: M.getOrInsertComdat(Name: GV.getName()));
336 } else
337 GV.setLinkage(getLinkage(SGV: &GV, /* DoPromote */ false));
338
339 // When ClearDSOLocalOnDeclarations is true, clear dso_local if GV is
340 // converted to a declaration, to disable direct access. Don't do this if GV
341 // is implicitly dso_local due to a non-default visibility.
342 if (ClearDSOLocalOnDeclarations &&
343 (GV.isDeclarationForLinker() ||
344 (isPerformingImport() && !doImportAsDefinition(SGV: &GV))) &&
345 !GV.isImplicitDSOLocal()) {
346 GV.setDSOLocal(false);
347 } else if (VI && VI.isDSOLocal(WithDSOLocalPropagation: ImportIndex.withDSOLocalPropagation())) {
348 // If all summaries are dso_local, symbol gets resolved to a known local
349 // definition.
350 GV.setDSOLocal(true);
351 if (GV.hasDLLImportStorageClass())
352 GV.setDLLStorageClass(GlobalValue::DefaultStorageClass);
353 }
354
355 // Remove functions imported as available externally defs from comdats,
356 // as this is a declaration for the linker, and will be dropped eventually.
357 // It is illegal for comdats to contain declarations.
358 auto *GO = dyn_cast<GlobalObject>(Val: &GV);
359 if (GO && GO->isDeclarationForLinker() && GO->hasComdat()) {
360 // The IRMover should not have placed any imported declarations in
361 // a comdat, so the only declaration that should be in a comdat
362 // at this point would be a definition imported as available_externally.
363 assert(GO->hasAvailableExternallyLinkage() &&
364 "Expected comdat on definition (possibly available external)");
365 GO->setComdat(nullptr);
366 }
367}
368
369void FunctionImportGlobalProcessing::processGlobalsForThinLTO() {
370 for (GlobalVariable &GV : M.globals())
371 processGlobalForThinLTO(GV);
372 for (Function &SF : M)
373 processGlobalForThinLTO(GV&: SF);
374 for (GlobalAlias &GA : M.aliases())
375 processGlobalForThinLTO(GV&: GA);
376
377 // Replace any COMDATS that required renaming (because the COMDAT leader was
378 // promoted and renamed).
379 if (!RenamedComdats.empty())
380 for (auto &GO : M.global_objects())
381 if (auto *C = GO.getComdat()) {
382 auto Replacement = RenamedComdats.find(Val: C);
383 if (Replacement != RenamedComdats.end())
384 GO.setComdat(Replacement->second);
385 }
386}
387
388void FunctionImportGlobalProcessing::run() { processGlobalsForThinLTO(); }
389
390void llvm::renameModuleForThinLTO(Module &M, const ModuleSummaryIndex &Index,
391 bool ClearDSOLocalOnDeclarations,
392 SetVector<GlobalValue *> *GlobalsToImport) {
393 llvm::TimeTraceScope timeScope("Rename module for ThinLTO");
394 FunctionImportGlobalProcessing ThinLTOProcessing(M, Index, GlobalsToImport,
395 ClearDSOLocalOnDeclarations);
396 ThinLTOProcessing.run();
397}
398