1 //===- lib/Transforms/Utils/FunctionImportUtils.cpp - Importing utilities -===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements the FunctionImportGlobalProcessing class, used
11 // to perform the necessary global value handling for function importing.
13 //===----------------------------------------------------------------------===//
15 #include "llvm/Transforms/Utils/FunctionImportUtils.h"
16 #include "llvm/IR/InstIterator.h"
19 /// Checks if we should import SGV as a definition, otherwise import as a
21 bool FunctionImportGlobalProcessing::doImportAsDefinition(
22 const GlobalValue *SGV, SetVector<GlobalValue *> *GlobalsToImport) {
24 // Only import the globals requested for importing.
25 if (!GlobalsToImport->count(const_cast<GlobalValue *>(SGV)))
28 assert(!isa<GlobalAlias>(SGV) &&
29 "Unexpected global alias in the import list.");
35 bool FunctionImportGlobalProcessing::doImportAsDefinition(
36 const GlobalValue *SGV) {
37 if (!isPerformingImport())
39 return FunctionImportGlobalProcessing::doImportAsDefinition(SGV,
43 bool FunctionImportGlobalProcessing::shouldPromoteLocalToGlobal(
44 const GlobalValue *SGV) {
45 assert(SGV->hasLocalLinkage());
46 // Both the imported references and the original local variable must
48 if (!isPerformingImport() && !isModuleExporting())
51 if (isPerformingImport()) {
52 assert((!GlobalsToImport->count(const_cast<GlobalValue *>(SGV)) ||
53 !isNonRenamableLocal(*SGV)) &&
54 "Attempting to promote non-renamable local");
55 // We don't know for sure yet if we are importing this value (as either
56 // a reference or a def), since we are simply walking all values in the
57 // module. But by necessity if we end up importing it and it is local,
58 // it must be promoted, so unconditionally promote all values in the
63 // When exporting, consult the index. We can have more than one local
64 // with the same GUID, in the case of same-named locals in different but
65 // same-named source files that were compiled in their respective directories
66 // (so the source file name and resulting GUID is the same). Find the one
68 auto Summary = ImportIndex.findSummaryInModule(
69 SGV->getGUID(), SGV->getParent()->getModuleIdentifier());
70 assert(Summary && "Missing summary for global value when exporting");
71 auto Linkage = Summary->linkage();
72 if (!GlobalValue::isLocalLinkage(Linkage)) {
73 assert(!isNonRenamableLocal(*SGV) &&
74 "Attempting to promote non-renamable local");
82 bool FunctionImportGlobalProcessing::isNonRenamableLocal(
83 const GlobalValue &GV) const {
84 if (!GV.hasLocalLinkage())
86 // This needs to stay in sync with the logic in buildModuleSummaryIndex.
89 if (Used.count(const_cast<GlobalValue *>(&GV)))
95 std::string FunctionImportGlobalProcessing::getName(const GlobalValue *SGV,
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. When importing,
100 // we rename all locals (not just those that are promoted) in order to
101 // avoid naming conflicts between locals imported from different modules.
102 if (SGV->hasLocalLinkage() && (DoPromote || isPerformingImport()))
103 return ModuleSummaryIndex::getGlobalNameForLocal(
105 ImportIndex.getModuleHash(SGV->getParent()->getModuleIdentifier()));
106 return SGV->getName();
109 GlobalValue::LinkageTypes
110 FunctionImportGlobalProcessing::getLinkage(const GlobalValue *SGV,
112 // Any local variable that is referenced by an exported function needs
113 // to be promoted to global scope. Since we don't currently know which
114 // functions reference which local variables/functions, we must treat
115 // all as potentially exported if this module is exporting anything.
116 if (isModuleExporting()) {
117 if (SGV->hasLocalLinkage() && DoPromote)
118 return GlobalValue::ExternalLinkage;
119 return SGV->getLinkage();
122 // Otherwise, if we aren't importing, no linkage change is needed.
123 if (!isPerformingImport())
124 return SGV->getLinkage();
126 switch (SGV->getLinkage()) {
127 case GlobalValue::LinkOnceODRLinkage:
128 case GlobalValue::ExternalLinkage:
129 // External and linkonce definitions are converted to available_externally
130 // definitions upon import, so that they are available for inlining
131 // and/or optimization, but are turned into declarations later
132 // during the EliminateAvailableExternally pass.
133 if (doImportAsDefinition(SGV) && !dyn_cast<GlobalAlias>(SGV))
134 return GlobalValue::AvailableExternallyLinkage;
135 // An imported external declaration stays external.
136 return SGV->getLinkage();
138 case GlobalValue::AvailableExternallyLinkage:
139 // An imported available_externally definition converts
140 // to external if imported as a declaration.
141 if (!doImportAsDefinition(SGV))
142 return GlobalValue::ExternalLinkage;
143 // An imported available_externally declaration stays that way.
144 return SGV->getLinkage();
146 case GlobalValue::LinkOnceAnyLinkage:
147 case GlobalValue::WeakAnyLinkage:
148 // Can't import linkonce_any/weak_any definitions correctly, or we might
149 // change the program semantics, since the linker will pick the first
150 // linkonce_any/weak_any definition and importing would change the order
151 // they are seen by the linker. The module linking caller needs to enforce
153 assert(!doImportAsDefinition(SGV));
154 // If imported as a declaration, it becomes external_weak.
155 return SGV->getLinkage();
157 case GlobalValue::WeakODRLinkage:
158 // For weak_odr linkage, there is a guarantee that all copies will be
159 // equivalent, so the issue described above for weak_any does not exist,
160 // and the definition can be imported. It can be treated similarly
161 // to an imported externally visible global value.
162 if (doImportAsDefinition(SGV) && !dyn_cast<GlobalAlias>(SGV))
163 return GlobalValue::AvailableExternallyLinkage;
165 return GlobalValue::ExternalLinkage;
167 case GlobalValue::AppendingLinkage:
168 // It would be incorrect to import an appending linkage variable,
169 // since it would cause global constructors/destructors to be
170 // executed multiple times. This should have already been handled
171 // by linkIfNeeded, and we will assert in shouldLinkFromSource
172 // if we try to import, so we simply return AppendingLinkage.
173 return GlobalValue::AppendingLinkage;
175 case GlobalValue::InternalLinkage:
176 case GlobalValue::PrivateLinkage:
177 // If we are promoting the local to global scope, it is handled
178 // similarly to a normal externally visible global.
180 if (doImportAsDefinition(SGV) && !dyn_cast<GlobalAlias>(SGV))
181 return GlobalValue::AvailableExternallyLinkage;
183 return GlobalValue::ExternalLinkage;
185 // A non-promoted imported local definition stays local.
186 // The ThinLTO pass will eventually force-import their definitions.
187 return SGV->getLinkage();
189 case GlobalValue::ExternalWeakLinkage:
190 // External weak doesn't apply to definitions, must be a declaration.
191 assert(!doImportAsDefinition(SGV));
192 // Linkage stays external_weak.
193 return SGV->getLinkage();
195 case GlobalValue::CommonLinkage:
196 // Linkage stays common on definitions.
197 // The ThinLTO pass will eventually force-import their definitions.
198 return SGV->getLinkage();
201 llvm_unreachable("unknown linkage type");
204 void FunctionImportGlobalProcessing::processGlobalForThinLTO(GlobalValue &GV) {
208 VI = ImportIndex.getValueInfo(GV.getGUID());
209 // Set synthetic function entry counts.
210 if (VI && ImportIndex.hasSyntheticEntryCounts()) {
211 if (Function *F = dyn_cast<Function>(&GV)) {
212 if (!F->isDeclaration()) {
213 for (auto &S : VI.getSummaryList()) {
214 FunctionSummary *FS = dyn_cast<FunctionSummary>(S->getBaseObject());
215 if (FS->modulePath() == M.getModuleIdentifier()) {
216 F->setEntryCount(Function::ProfileCount(FS->entryCount(),
217 Function::PCT_Synthetic));
224 // Check the summaries to see if the symbol gets resolved to a known local
226 if (VI && VI.isDSOLocal()) {
227 GV.setDSOLocal(true);
228 if (GV.hasDLLImportStorageClass())
229 GV.setDLLStorageClass(GlobalValue::DefaultStorageClass);
233 // Mark read-only variables which can be imported with specific attribute.
234 // We can't internalize them now because IRMover will fail to link variable
235 // definitions to their external declarations during ThinLTO import. We'll
236 // internalize read-only variables later, after import is finished.
237 // See internalizeImmutableGVs.
239 // If global value dead stripping is not enabled in summary then
240 // propagateConstants hasn't been run. We can't internalize GV
242 if (!GV.isDeclaration() && VI && ImportIndex.withGlobalValueDeadStripping()) {
243 const auto &SL = VI.getSummaryList();
244 auto *GVS = SL.empty() ? nullptr : dyn_cast<GlobalVarSummary>(SL[0].get());
245 if (GVS && GVS->isReadOnly())
246 cast<GlobalVariable>(&GV)->addAttribute("thinlto-internalize");
249 bool DoPromote = false;
250 if (GV.hasLocalLinkage() &&
251 ((DoPromote = shouldPromoteLocalToGlobal(&GV)) || isPerformingImport())) {
252 // Save the original name string before we rename GV below.
253 auto Name = GV.getName().str();
254 // Once we change the name or linkage it is difficult to determine
255 // again whether we should promote since shouldPromoteLocalToGlobal needs
256 // to locate the summary (based on GUID from name and linkage). Therefore,
257 // use DoPromote result saved above.
258 GV.setName(getName(&GV, DoPromote));
259 GV.setLinkage(getLinkage(&GV, DoPromote));
260 if (!GV.hasLocalLinkage())
261 GV.setVisibility(GlobalValue::HiddenVisibility);
263 // If we are renaming a COMDAT leader, ensure that we record the COMDAT
264 // for later renaming as well. This is required for COFF.
265 if (const auto *C = GV.getComdat())
266 if (C->getName() == Name)
267 RenamedComdats.try_emplace(C, M.getOrInsertComdat(GV.getName()));
269 GV.setLinkage(getLinkage(&GV, /* DoPromote */ false));
271 // Remove functions imported as available externally defs from comdats,
272 // as this is a declaration for the linker, and will be dropped eventually.
273 // It is illegal for comdats to contain declarations.
274 auto *GO = dyn_cast<GlobalObject>(&GV);
275 if (GO && GO->isDeclarationForLinker() && GO->hasComdat()) {
276 // The IRMover should not have placed any imported declarations in
277 // a comdat, so the only declaration that should be in a comdat
278 // at this point would be a definition imported as available_externally.
279 assert(GO->hasAvailableExternallyLinkage() &&
280 "Expected comdat on definition (possibly available external)");
281 GO->setComdat(nullptr);
285 void FunctionImportGlobalProcessing::processGlobalsForThinLTO() {
286 for (GlobalVariable &GV : M.globals())
287 processGlobalForThinLTO(GV);
288 for (Function &SF : M)
289 processGlobalForThinLTO(SF);
290 for (GlobalAlias &GA : M.aliases())
291 processGlobalForThinLTO(GA);
293 // Replace any COMDATS that required renaming (because the COMDAT leader was
294 // promoted and renamed).
295 if (!RenamedComdats.empty())
296 for (auto &GO : M.global_objects())
297 if (auto *C = GO.getComdat()) {
298 auto Replacement = RenamedComdats.find(C);
299 if (Replacement != RenamedComdats.end())
300 GO.setComdat(Replacement->second);
304 bool FunctionImportGlobalProcessing::run() {
305 processGlobalsForThinLTO();
309 bool llvm::renameModuleForThinLTO(Module &M, const ModuleSummaryIndex &Index,
310 SetVector<GlobalValue *> *GlobalsToImport) {
311 FunctionImportGlobalProcessing ThinLTOProcessing(M, Index, GlobalsToImport);
312 return ThinLTOProcessing.run();