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