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