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