blob: a9772e31da5095ccfa3415c52d1d6a4d6eaa6424 [file] [log] [blame]
Teresa Johnson488a8002016-02-10 18:11:31 +00001//===- lib/Transforms/Utils/FunctionImportUtils.cpp - Importing utilities -===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the FunctionImportGlobalProcessing class, used
11// to perform the necessary global value handling for function importing.
12//
13//===----------------------------------------------------------------------===//
14
15#include "llvm/Transforms/Utils/FunctionImportUtils.h"
Teresa Johnsondf5ef872016-04-27 14:19:38 +000016#include "llvm/IR/InstIterator.h"
Teresa Johnson488a8002016-02-10 18:11:31 +000017using namespace llvm;
18
19/// Checks if we should import SGV as a definition, otherwise import as a
20/// declaration.
21bool FunctionImportGlobalProcessing::doImportAsDefinition(
Peter Collingbourne6d8f8172017-02-03 16:56:27 +000022 const GlobalValue *SGV, SetVector<GlobalValue *> *GlobalsToImport) {
Mehdi Amini8d051852016-03-19 00:40:31 +000023
Mehdi Amini8d051852016-03-19 00:40:31 +000024 // Only import the globals requested for importing.
David Blaikie72c0b1c2017-07-27 15:28:10 +000025 if (!GlobalsToImport->count(const_cast<GlobalValue *>(SGV)))
26 return false;
David Blaikie2f0cc472017-07-27 15:09:06 +000027
28 assert(!isa<GlobalAlias>(SGV) &&
29 "Unexpected global alias in the import list.");
30
David Blaikie72c0b1c2017-07-27 15:28:10 +000031 // Otherwise yes.
32 return true;
Teresa Johnson488a8002016-02-10 18:11:31 +000033}
34
35bool FunctionImportGlobalProcessing::doImportAsDefinition(
36 const GlobalValue *SGV) {
37 if (!isPerformingImport())
38 return false;
Mehdi Amini8d051852016-03-19 00:40:31 +000039 return FunctionImportGlobalProcessing::doImportAsDefinition(SGV,
40 GlobalsToImport);
Teresa Johnson488a8002016-02-10 18:11:31 +000041}
42
Teresa Johnson38d4df72016-10-29 21:52:23 +000043bool FunctionImportGlobalProcessing::shouldPromoteLocalToGlobal(
Teresa Johnson488a8002016-02-10 18:11:31 +000044 const GlobalValue *SGV) {
45 assert(SGV->hasLocalLinkage());
46 // Both the imported references and the original local variable must
47 // be promoted.
48 if (!isPerformingImport() && !isModuleExporting())
49 return false;
50
Teresa Johnson4fef68c2016-11-14 19:21:41 +000051 if (isPerformingImport()) {
Peter Collingbourne6d8f8172017-02-03 16:56:27 +000052 assert((!GlobalsToImport->count(const_cast<GlobalValue *>(SGV)) ||
53 !isNonRenamableLocal(*SGV)) &&
Teresa Johnson2b603842017-01-05 15:10:10 +000054 "Attempting to promote non-renamable local");
Teresa Johnson4fef68c2016-11-14 19:21:41 +000055 // 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
59 // importing module.
60 return true;
Teresa Johnson0515fb82016-11-03 01:07:16 +000061 }
Mehdi Aminib4e1e822016-04-27 00:32:13 +000062
Teresa Johnson9006d522017-01-06 23:38:41 +000063 // 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
67 // in this module.
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();
Teresa Johnson4fef68c2016-11-14 19:21:41 +000072 if (!GlobalValue::isLocalLinkage(Linkage)) {
Teresa Johnson519465b2017-01-05 14:32:16 +000073 assert(!isNonRenamableLocal(*SGV) &&
74 "Attempting to promote non-renamable local");
Teresa Johnson4fef68c2016-11-14 19:21:41 +000075 return true;
76 }
77
78 return false;
Teresa Johnson488a8002016-02-10 18:11:31 +000079}
80
Teresa Johnson519465b2017-01-05 14:32:16 +000081#ifndef NDEBUG
82bool FunctionImportGlobalProcessing::isNonRenamableLocal(
83 const GlobalValue &GV) const {
84 if (!GV.hasLocalLinkage())
85 return false;
86 // This needs to stay in sync with the logic in buildModuleSummaryIndex.
87 if (GV.hasSection())
88 return true;
89 if (Used.count(const_cast<GlobalValue *>(&GV)))
90 return true;
91 return false;
92}
93#endif
94
Teresa Johnson1b9c2be2016-10-29 21:31:48 +000095std::string FunctionImportGlobalProcessing::getName(const GlobalValue *SGV,
96 bool DoPromote) {
Teresa Johnson488a8002016-02-10 18:11:31 +000097 // 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.
Teresa Johnson1b9c2be2016-10-29 21:31:48 +0000102 if (SGV->hasLocalLinkage() && (DoPromote || isPerformingImport()))
Teresa Johnson26ab5772016-03-15 00:04:37 +0000103 return ModuleSummaryIndex::getGlobalNameForLocal(
Teresa Johnson488a8002016-02-10 18:11:31 +0000104 SGV->getName(),
Mehdi Aminiae280e52016-04-11 23:26:46 +0000105 ImportIndex.getModuleHash(SGV->getParent()->getModuleIdentifier()));
Teresa Johnson488a8002016-02-10 18:11:31 +0000106 return SGV->getName();
107}
108
109GlobalValue::LinkageTypes
Teresa Johnson1b9c2be2016-10-29 21:31:48 +0000110FunctionImportGlobalProcessing::getLinkage(const GlobalValue *SGV,
111 bool DoPromote) {
Teresa Johnson488a8002016-02-10 18:11:31 +0000112 // 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()) {
Teresa Johnson1b9c2be2016-10-29 21:31:48 +0000117 if (SGV->hasLocalLinkage() && DoPromote)
Teresa Johnson488a8002016-02-10 18:11:31 +0000118 return GlobalValue::ExternalLinkage;
119 return SGV->getLinkage();
120 }
121
122 // Otherwise, if we aren't importing, no linkage change is needed.
123 if (!isPerformingImport())
124 return SGV->getLinkage();
125
126 switch (SGV->getLinkage()) {
David Blaikie2f0cc472017-07-27 15:09:06 +0000127 case GlobalValue::LinkOnceODRLinkage:
Teresa Johnson488a8002016-02-10 18:11:31 +0000128 case GlobalValue::ExternalLinkage:
David Blaikie2f0cc472017-07-27 15:09:06 +0000129 // External and linkonce definitions are converted to available_externally
Teresa Johnson488a8002016-02-10 18:11:31 +0000130 // 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();
137
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();
145
Xin Tong53e52e42018-11-28 15:16:35 +0000146 case GlobalValue::LinkOnceAnyLinkage:
Teresa Johnson488a8002016-02-10 18:11:31 +0000147 case GlobalValue::WeakAnyLinkage:
Xin Tong53e52e42018-11-28 15:16:35 +0000148 // 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
152 // this.
Teresa Johnson488a8002016-02-10 18:11:31 +0000153 assert(!doImportAsDefinition(SGV));
154 // If imported as a declaration, it becomes external_weak.
Mehdi Aminibb3a1d92016-04-20 04:18:11 +0000155 return SGV->getLinkage();
Teresa Johnson488a8002016-02-10 18:11:31 +0000156
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;
164 else
165 return GlobalValue::ExternalLinkage;
166
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;
174
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.
Teresa Johnson1b9c2be2016-10-29 21:31:48 +0000179 if (DoPromote) {
Teresa Johnson488a8002016-02-10 18:11:31 +0000180 if (doImportAsDefinition(SGV) && !dyn_cast<GlobalAlias>(SGV))
181 return GlobalValue::AvailableExternallyLinkage;
182 else
183 return GlobalValue::ExternalLinkage;
184 }
185 // A non-promoted imported local definition stays local.
186 // The ThinLTO pass will eventually force-import their definitions.
187 return SGV->getLinkage();
188
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();
194
195 case GlobalValue::CommonLinkage:
196 // Linkage stays common on definitions.
197 // The ThinLTO pass will eventually force-import their definitions.
198 return SGV->getLinkage();
199 }
200
201 llvm_unreachable("unknown linkage type");
202}
203
204void FunctionImportGlobalProcessing::processGlobalForThinLTO(GlobalValue &GV) {
Sean Fertile4595a912017-11-04 17:04:39 +0000205
Eugene Leviantbf46e742018-11-16 07:08:00 +0000206 ValueInfo VI;
Sean Fertile4595a912017-11-04 17:04:39 +0000207 if (GV.hasName()) {
Eugene Leviantbf46e742018-11-16 07:08:00 +0000208 VI = ImportIndex.getValueInfo(GV.getGUID());
Easwaran Raman5a7056f2018-12-13 19:54:27 +0000209 // 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));
218 break;
219 }
220 }
221 }
222 }
223 }
224 // Check the summaries to see if the symbol gets resolved to a known local
225 // definition.
Rafael Espindolaf5220fb2018-03-13 15:24:51 +0000226 if (VI && VI.isDSOLocal()) {
Peter Collingbourneb4edfb92018-02-05 17:17:51 +0000227 GV.setDSOLocal(true);
Rafael Espindolaf5220fb2018-03-13 15:24:51 +0000228 if (GV.hasDLLImportStorageClass())
229 GV.setDLLStorageClass(GlobalValue::DefaultStorageClass);
230 }
Sean Fertile4595a912017-11-04 17:04:39 +0000231 }
232
Eugene Leviantbf46e742018-11-16 07:08:00 +0000233 // 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.
238 //
239 // If global value dead stripping is not enabled in summary then
240 // propagateConstants hasn't been run. We can't internalize GV
241 // in such case.
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");
247 }
248
Teresa Johnson1b9c2be2016-10-29 21:31:48 +0000249 bool DoPromote = false;
Teresa Johnson488a8002016-02-10 18:11:31 +0000250 if (GV.hasLocalLinkage() &&
Teresa Johnson38d4df72016-10-29 21:52:23 +0000251 ((DoPromote = shouldPromoteLocalToGlobal(&GV)) || isPerformingImport())) {
Teresa Johnson1b9c2be2016-10-29 21:31:48 +0000252 // Once we change the name or linkage it is difficult to determine
Teresa Johnson38d4df72016-10-29 21:52:23 +0000253 // again whether we should promote since shouldPromoteLocalToGlobal needs
Teresa Johnson1b9c2be2016-10-29 21:31:48 +0000254 // to locate the summary (based on GUID from name and linkage). Therefore,
255 // use DoPromote result saved above.
256 GV.setName(getName(&GV, DoPromote));
257 GV.setLinkage(getLinkage(&GV, DoPromote));
Teresa Johnson488a8002016-02-10 18:11:31 +0000258 if (!GV.hasLocalLinkage())
259 GV.setVisibility(GlobalValue::HiddenVisibility);
Teresa Johnson488a8002016-02-10 18:11:31 +0000260 } else
Teresa Johnson1b9c2be2016-10-29 21:31:48 +0000261 GV.setLinkage(getLinkage(&GV, /* DoPromote */ false));
Teresa Johnson488a8002016-02-10 18:11:31 +0000262
263 // Remove functions imported as available externally defs from comdats,
264 // as this is a declaration for the linker, and will be dropped eventually.
265 // It is illegal for comdats to contain declarations.
Eugene Leviantbf46e742018-11-16 07:08:00 +0000266 auto *GO = dyn_cast<GlobalObject>(&GV);
Teresa Johnson488a8002016-02-10 18:11:31 +0000267 if (GO && GO->isDeclarationForLinker() && GO->hasComdat()) {
268 // The IRMover should not have placed any imported declarations in
269 // a comdat, so the only declaration that should be in a comdat
270 // at this point would be a definition imported as available_externally.
271 assert(GO->hasAvailableExternallyLinkage() &&
272 "Expected comdat on definition (possibly available external)");
273 GO->setComdat(nullptr);
274 }
275}
276
277void FunctionImportGlobalProcessing::processGlobalsForThinLTO() {
278 for (GlobalVariable &GV : M.globals())
279 processGlobalForThinLTO(GV);
280 for (Function &SF : M)
281 processGlobalForThinLTO(SF);
282 for (GlobalAlias &GA : M.aliases())
283 processGlobalForThinLTO(GA);
284}
285
286bool FunctionImportGlobalProcessing::run() {
287 processGlobalsForThinLTO();
288 return false;
289}
290
Peter Collingbourne6d8f8172017-02-03 16:56:27 +0000291bool llvm::renameModuleForThinLTO(Module &M, const ModuleSummaryIndex &Index,
292 SetVector<GlobalValue *> *GlobalsToImport) {
Mehdi Amini8d051852016-03-19 00:40:31 +0000293 FunctionImportGlobalProcessing ThinLTOProcessing(M, Index, GlobalsToImport);
Teresa Johnson488a8002016-02-10 18:11:31 +0000294 return ThinLTOProcessing.run();
295}