10b57cec5SDimitry Andric //===- lib/Transforms/Utils/FunctionImportUtils.cpp - Importing utilities -===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric //
90b57cec5SDimitry Andric // This file implements the FunctionImportGlobalProcessing class, used
100b57cec5SDimitry Andric // to perform the necessary global value handling for function importing.
110b57cec5SDimitry Andric //
120b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
130b57cec5SDimitry Andric 
140b57cec5SDimitry Andric #include "llvm/Transforms/Utils/FunctionImportUtils.h"
15bdd1243dSDimitry Andric #include "llvm/Support/CommandLine.h"
160b57cec5SDimitry Andric using namespace llvm;
170b57cec5SDimitry Andric 
18bdd1243dSDimitry Andric /// Uses the "source_filename" instead of a Module hash ID for the suffix of
19bdd1243dSDimitry Andric /// promoted locals during LTO. NOTE: This requires that the source filename
20bdd1243dSDimitry Andric /// has a unique name / path to avoid name collisions.
21bdd1243dSDimitry Andric static cl::opt<bool> UseSourceFilenameForPromotedLocals(
22bdd1243dSDimitry Andric     "use-source-filename-for-promoted-locals", cl::Hidden,
23bdd1243dSDimitry Andric     cl::desc("Uses the source file name instead of the Module hash. "
24bdd1243dSDimitry Andric              "This requires that the source filename has a unique name / "
25bdd1243dSDimitry Andric              "path to avoid name collisions."));
26bdd1243dSDimitry Andric 
270b57cec5SDimitry Andric /// Checks if we should import SGV as a definition, otherwise import as a
280b57cec5SDimitry Andric /// declaration.
doImportAsDefinition(const GlobalValue * SGV)290b57cec5SDimitry Andric bool FunctionImportGlobalProcessing::doImportAsDefinition(
30480093f4SDimitry Andric     const GlobalValue *SGV) {
31480093f4SDimitry Andric   if (!isPerformingImport())
32480093f4SDimitry Andric     return false;
330b57cec5SDimitry Andric 
340b57cec5SDimitry Andric   // Only import the globals requested for importing.
350b57cec5SDimitry Andric   if (!GlobalsToImport->count(const_cast<GlobalValue *>(SGV)))
360b57cec5SDimitry Andric     return false;
370b57cec5SDimitry Andric 
380b57cec5SDimitry Andric   assert(!isa<GlobalAlias>(SGV) &&
390b57cec5SDimitry Andric          "Unexpected global alias in the import list.");
400b57cec5SDimitry Andric 
410b57cec5SDimitry Andric   // Otherwise yes.
420b57cec5SDimitry Andric   return true;
430b57cec5SDimitry Andric }
440b57cec5SDimitry Andric 
shouldPromoteLocalToGlobal(const GlobalValue * SGV,ValueInfo VI)450b57cec5SDimitry Andric bool FunctionImportGlobalProcessing::shouldPromoteLocalToGlobal(
46480093f4SDimitry Andric     const GlobalValue *SGV, ValueInfo VI) {
470b57cec5SDimitry Andric   assert(SGV->hasLocalLinkage());
48fcaf7f86SDimitry Andric 
49fcaf7f86SDimitry Andric   // Ifuncs and ifunc alias does not have summary.
50fcaf7f86SDimitry Andric   if (isa<GlobalIFunc>(SGV) ||
51fcaf7f86SDimitry Andric       (isa<GlobalAlias>(SGV) &&
52fcaf7f86SDimitry Andric        isa<GlobalIFunc>(cast<GlobalAlias>(SGV)->getAliaseeObject())))
53fcaf7f86SDimitry Andric     return false;
54fcaf7f86SDimitry Andric 
550b57cec5SDimitry Andric   // Both the imported references and the original local variable must
560b57cec5SDimitry Andric   // be promoted.
570b57cec5SDimitry Andric   if (!isPerformingImport() && !isModuleExporting())
580b57cec5SDimitry Andric     return false;
590b57cec5SDimitry Andric 
600b57cec5SDimitry Andric   if (isPerformingImport()) {
610b57cec5SDimitry Andric     assert((!GlobalsToImport->count(const_cast<GlobalValue *>(SGV)) ||
620b57cec5SDimitry Andric             !isNonRenamableLocal(*SGV)) &&
630b57cec5SDimitry Andric            "Attempting to promote non-renamable local");
640b57cec5SDimitry Andric     // We don't know for sure yet if we are importing this value (as either
650b57cec5SDimitry Andric     // a reference or a def), since we are simply walking all values in the
660b57cec5SDimitry Andric     // module. But by necessity if we end up importing it and it is local,
670b57cec5SDimitry Andric     // it must be promoted, so unconditionally promote all values in the
680b57cec5SDimitry Andric     // importing module.
690b57cec5SDimitry Andric     return true;
700b57cec5SDimitry Andric   }
710b57cec5SDimitry Andric 
720b57cec5SDimitry Andric   // When exporting, consult the index. We can have more than one local
730b57cec5SDimitry Andric   // with the same GUID, in the case of same-named locals in different but
740b57cec5SDimitry Andric   // same-named source files that were compiled in their respective directories
750b57cec5SDimitry Andric   // (so the source file name and resulting GUID is the same). Find the one
760b57cec5SDimitry Andric   // in this module.
770b57cec5SDimitry Andric   auto Summary = ImportIndex.findSummaryInModule(
78480093f4SDimitry Andric       VI, SGV->getParent()->getModuleIdentifier());
790b57cec5SDimitry Andric   assert(Summary && "Missing summary for global value when exporting");
800b57cec5SDimitry Andric   auto Linkage = Summary->linkage();
810b57cec5SDimitry Andric   if (!GlobalValue::isLocalLinkage(Linkage)) {
820b57cec5SDimitry Andric     assert(!isNonRenamableLocal(*SGV) &&
830b57cec5SDimitry Andric            "Attempting to promote non-renamable local");
840b57cec5SDimitry Andric     return true;
850b57cec5SDimitry Andric   }
860b57cec5SDimitry Andric 
870b57cec5SDimitry Andric   return false;
880b57cec5SDimitry Andric }
890b57cec5SDimitry Andric 
900b57cec5SDimitry Andric #ifndef NDEBUG
isNonRenamableLocal(const GlobalValue & GV) const910b57cec5SDimitry Andric bool FunctionImportGlobalProcessing::isNonRenamableLocal(
920b57cec5SDimitry Andric     const GlobalValue &GV) const {
930b57cec5SDimitry Andric   if (!GV.hasLocalLinkage())
940b57cec5SDimitry Andric     return false;
950b57cec5SDimitry Andric   // This needs to stay in sync with the logic in buildModuleSummaryIndex.
960b57cec5SDimitry Andric   if (GV.hasSection())
970b57cec5SDimitry Andric     return true;
980b57cec5SDimitry Andric   if (Used.count(const_cast<GlobalValue *>(&GV)))
990b57cec5SDimitry Andric     return true;
1000b57cec5SDimitry Andric   return false;
1010b57cec5SDimitry Andric }
1020b57cec5SDimitry Andric #endif
1030b57cec5SDimitry Andric 
104480093f4SDimitry Andric std::string
getPromotedName(const GlobalValue * SGV)105480093f4SDimitry Andric FunctionImportGlobalProcessing::getPromotedName(const GlobalValue *SGV) {
106480093f4SDimitry Andric   assert(SGV->hasLocalLinkage());
107bdd1243dSDimitry Andric 
1080b57cec5SDimitry Andric   // For locals that must be promoted to global scope, ensure that
1090b57cec5SDimitry Andric   // the promoted name uniquely identifies the copy in the original module,
110480093f4SDimitry Andric   // using the ID assigned during combined index creation.
111bdd1243dSDimitry Andric   if (UseSourceFilenameForPromotedLocals &&
112bdd1243dSDimitry Andric       !SGV->getParent()->getSourceFileName().empty()) {
113bdd1243dSDimitry Andric     SmallString<256> Suffix(SGV->getParent()->getSourceFileName());
114bdd1243dSDimitry Andric     std::replace_if(std::begin(Suffix), std::end(Suffix),
115bdd1243dSDimitry Andric                     [&](char ch) { return !isAlnum(ch); }, '_');
116bdd1243dSDimitry Andric     return ModuleSummaryIndex::getGlobalNameForLocal(
117bdd1243dSDimitry Andric         SGV->getName(), Suffix);
118bdd1243dSDimitry Andric   }
119bdd1243dSDimitry Andric 
1200b57cec5SDimitry Andric   return ModuleSummaryIndex::getGlobalNameForLocal(
1210b57cec5SDimitry Andric       SGV->getName(),
1220b57cec5SDimitry Andric       ImportIndex.getModuleHash(SGV->getParent()->getModuleIdentifier()));
1230b57cec5SDimitry Andric }
1240b57cec5SDimitry Andric 
1250b57cec5SDimitry Andric GlobalValue::LinkageTypes
getLinkage(const GlobalValue * SGV,bool DoPromote)1260b57cec5SDimitry Andric FunctionImportGlobalProcessing::getLinkage(const GlobalValue *SGV,
1270b57cec5SDimitry Andric                                            bool DoPromote) {
1280b57cec5SDimitry Andric   // Any local variable that is referenced by an exported function needs
1290b57cec5SDimitry Andric   // to be promoted to global scope. Since we don't currently know which
1300b57cec5SDimitry Andric   // functions reference which local variables/functions, we must treat
1310b57cec5SDimitry Andric   // all as potentially exported if this module is exporting anything.
1320b57cec5SDimitry Andric   if (isModuleExporting()) {
1330b57cec5SDimitry Andric     if (SGV->hasLocalLinkage() && DoPromote)
1340b57cec5SDimitry Andric       return GlobalValue::ExternalLinkage;
1350b57cec5SDimitry Andric     return SGV->getLinkage();
1360b57cec5SDimitry Andric   }
1370b57cec5SDimitry Andric 
1380b57cec5SDimitry Andric   // Otherwise, if we aren't importing, no linkage change is needed.
1390b57cec5SDimitry Andric   if (!isPerformingImport())
1400b57cec5SDimitry Andric     return SGV->getLinkage();
1410b57cec5SDimitry Andric 
1420b57cec5SDimitry Andric   switch (SGV->getLinkage()) {
1430b57cec5SDimitry Andric   case GlobalValue::LinkOnceODRLinkage:
1440b57cec5SDimitry Andric   case GlobalValue::ExternalLinkage:
1450b57cec5SDimitry Andric     // External and linkonce definitions are converted to available_externally
1460b57cec5SDimitry Andric     // definitions upon import, so that they are available for inlining
1470b57cec5SDimitry Andric     // and/or optimization, but are turned into declarations later
1480b57cec5SDimitry Andric     // during the EliminateAvailableExternally pass.
1490b57cec5SDimitry Andric     if (doImportAsDefinition(SGV) && !isa<GlobalAlias>(SGV))
1500b57cec5SDimitry Andric       return GlobalValue::AvailableExternallyLinkage;
1510b57cec5SDimitry Andric     // An imported external declaration stays external.
1520b57cec5SDimitry Andric     return SGV->getLinkage();
1530b57cec5SDimitry Andric 
1540b57cec5SDimitry Andric   case GlobalValue::AvailableExternallyLinkage:
1550b57cec5SDimitry Andric     // An imported available_externally definition converts
1560b57cec5SDimitry Andric     // to external if imported as a declaration.
1570b57cec5SDimitry Andric     if (!doImportAsDefinition(SGV))
1580b57cec5SDimitry Andric       return GlobalValue::ExternalLinkage;
1590b57cec5SDimitry Andric     // An imported available_externally declaration stays that way.
1600b57cec5SDimitry Andric     return SGV->getLinkage();
1610b57cec5SDimitry Andric 
1620b57cec5SDimitry Andric   case GlobalValue::LinkOnceAnyLinkage:
1630b57cec5SDimitry Andric   case GlobalValue::WeakAnyLinkage:
1640b57cec5SDimitry Andric     // Can't import linkonce_any/weak_any definitions correctly, or we might
1650b57cec5SDimitry Andric     // change the program semantics, since the linker will pick the first
1660b57cec5SDimitry Andric     // linkonce_any/weak_any definition and importing would change the order
1670b57cec5SDimitry Andric     // they are seen by the linker. The module linking caller needs to enforce
1680b57cec5SDimitry Andric     // this.
1690b57cec5SDimitry Andric     assert(!doImportAsDefinition(SGV));
1700b57cec5SDimitry Andric     // If imported as a declaration, it becomes external_weak.
1710b57cec5SDimitry Andric     return SGV->getLinkage();
1720b57cec5SDimitry Andric 
1730b57cec5SDimitry Andric   case GlobalValue::WeakODRLinkage:
1740b57cec5SDimitry Andric     // For weak_odr linkage, there is a guarantee that all copies will be
1750b57cec5SDimitry Andric     // equivalent, so the issue described above for weak_any does not exist,
1760b57cec5SDimitry Andric     // and the definition can be imported. It can be treated similarly
1770b57cec5SDimitry Andric     // to an imported externally visible global value.
1780b57cec5SDimitry Andric     if (doImportAsDefinition(SGV) && !isa<GlobalAlias>(SGV))
1790b57cec5SDimitry Andric       return GlobalValue::AvailableExternallyLinkage;
1800b57cec5SDimitry Andric     else
1810b57cec5SDimitry Andric       return GlobalValue::ExternalLinkage;
1820b57cec5SDimitry Andric 
1830b57cec5SDimitry Andric   case GlobalValue::AppendingLinkage:
1840b57cec5SDimitry Andric     // It would be incorrect to import an appending linkage variable,
1850b57cec5SDimitry Andric     // since it would cause global constructors/destructors to be
1860b57cec5SDimitry Andric     // executed multiple times. This should have already been handled
1870b57cec5SDimitry Andric     // by linkIfNeeded, and we will assert in shouldLinkFromSource
1880b57cec5SDimitry Andric     // if we try to import, so we simply return AppendingLinkage.
1890b57cec5SDimitry Andric     return GlobalValue::AppendingLinkage;
1900b57cec5SDimitry Andric 
1910b57cec5SDimitry Andric   case GlobalValue::InternalLinkage:
1920b57cec5SDimitry Andric   case GlobalValue::PrivateLinkage:
1930b57cec5SDimitry Andric     // If we are promoting the local to global scope, it is handled
1940b57cec5SDimitry Andric     // similarly to a normal externally visible global.
1950b57cec5SDimitry Andric     if (DoPromote) {
1960b57cec5SDimitry Andric       if (doImportAsDefinition(SGV) && !isa<GlobalAlias>(SGV))
1970b57cec5SDimitry Andric         return GlobalValue::AvailableExternallyLinkage;
1980b57cec5SDimitry Andric       else
1990b57cec5SDimitry Andric         return GlobalValue::ExternalLinkage;
2000b57cec5SDimitry Andric     }
2010b57cec5SDimitry Andric     // A non-promoted imported local definition stays local.
2020b57cec5SDimitry Andric     // The ThinLTO pass will eventually force-import their definitions.
2030b57cec5SDimitry Andric     return SGV->getLinkage();
2040b57cec5SDimitry Andric 
2050b57cec5SDimitry Andric   case GlobalValue::ExternalWeakLinkage:
2060b57cec5SDimitry Andric     // External weak doesn't apply to definitions, must be a declaration.
2070b57cec5SDimitry Andric     assert(!doImportAsDefinition(SGV));
2080b57cec5SDimitry Andric     // Linkage stays external_weak.
2090b57cec5SDimitry Andric     return SGV->getLinkage();
2100b57cec5SDimitry Andric 
2110b57cec5SDimitry Andric   case GlobalValue::CommonLinkage:
2120b57cec5SDimitry Andric     // Linkage stays common on definitions.
2130b57cec5SDimitry Andric     // The ThinLTO pass will eventually force-import their definitions.
2140b57cec5SDimitry Andric     return SGV->getLinkage();
2150b57cec5SDimitry Andric   }
2160b57cec5SDimitry Andric 
2170b57cec5SDimitry Andric   llvm_unreachable("unknown linkage type");
2180b57cec5SDimitry Andric }
2190b57cec5SDimitry Andric 
processGlobalForThinLTO(GlobalValue & GV)2200b57cec5SDimitry Andric void FunctionImportGlobalProcessing::processGlobalForThinLTO(GlobalValue &GV) {
2210b57cec5SDimitry Andric 
2220b57cec5SDimitry Andric   ValueInfo VI;
2230b57cec5SDimitry Andric   if (GV.hasName()) {
2240b57cec5SDimitry Andric     VI = ImportIndex.getValueInfo(GV.getGUID());
2250b57cec5SDimitry Andric     // Set synthetic function entry counts.
2260b57cec5SDimitry Andric     if (VI && ImportIndex.hasSyntheticEntryCounts()) {
2270b57cec5SDimitry Andric       if (Function *F = dyn_cast<Function>(&GV)) {
2280b57cec5SDimitry Andric         if (!F->isDeclaration()) {
229bdd1243dSDimitry Andric           for (const auto &S : VI.getSummaryList()) {
2308bcb0991SDimitry Andric             auto *FS = cast<FunctionSummary>(S->getBaseObject());
2310b57cec5SDimitry Andric             if (FS->modulePath() == M.getModuleIdentifier()) {
2320b57cec5SDimitry Andric               F->setEntryCount(Function::ProfileCount(FS->entryCount(),
2330b57cec5SDimitry Andric                                                       Function::PCT_Synthetic));
2340b57cec5SDimitry Andric               break;
2350b57cec5SDimitry Andric             }
2360b57cec5SDimitry Andric           }
2370b57cec5SDimitry Andric         }
2380b57cec5SDimitry Andric       }
2390b57cec5SDimitry Andric     }
2400b57cec5SDimitry Andric   }
2410b57cec5SDimitry Andric 
242480093f4SDimitry Andric   // We should always have a ValueInfo (i.e. GV in index) for definitions when
243480093f4SDimitry Andric   // we are exporting, and also when importing that value.
244480093f4SDimitry Andric   assert(VI || GV.isDeclaration() ||
245480093f4SDimitry Andric          (isPerformingImport() && !doImportAsDefinition(&GV)));
246480093f4SDimitry Andric 
2470b57cec5SDimitry Andric   // Mark read/write-only variables which can be imported with specific
2480b57cec5SDimitry Andric   // attribute. We can't internalize them now because IRMover will fail
2490b57cec5SDimitry Andric   // to link variable definitions to their external declarations during
2500b57cec5SDimitry Andric   // ThinLTO import. We'll internalize read-only variables later, after
2510b57cec5SDimitry Andric   // import is finished. See internalizeGVsAfterImport.
2520b57cec5SDimitry Andric   //
2530b57cec5SDimitry Andric   // If global value dead stripping is not enabled in summary then
2540b57cec5SDimitry Andric   // propagateConstants hasn't been run. We can't internalize GV
2550b57cec5SDimitry Andric   // in such case.
256480093f4SDimitry Andric   if (!GV.isDeclaration() && VI && ImportIndex.withAttributePropagation()) {
257480093f4SDimitry Andric     if (GlobalVariable *V = dyn_cast<GlobalVariable>(&GV)) {
258480093f4SDimitry Andric       // We can have more than one local with the same GUID, in the case of
259480093f4SDimitry Andric       // same-named locals in different but same-named source files that were
260480093f4SDimitry Andric       // compiled in their respective directories (so the source file name
261480093f4SDimitry Andric       // and resulting GUID is the same). Find the one in this module.
262480093f4SDimitry Andric       // Handle the case where there is no summary found in this module. That
263480093f4SDimitry Andric       // can happen in the distributed ThinLTO backend, because the index only
264480093f4SDimitry Andric       // contains summaries from the source modules if they are being imported.
265480093f4SDimitry Andric       // We might have a non-null VI and get here even in that case if the name
266480093f4SDimitry Andric       // matches one in this module (e.g. weak or appending linkage).
267480093f4SDimitry Andric       auto *GVS = dyn_cast_or_null<GlobalVarSummary>(
268480093f4SDimitry Andric           ImportIndex.findSummaryInModule(VI, M.getModuleIdentifier()));
269480093f4SDimitry Andric       if (GVS &&
270480093f4SDimitry Andric           (ImportIndex.isReadOnly(GVS) || ImportIndex.isWriteOnly(GVS))) {
271480093f4SDimitry Andric         V->addAttribute("thinlto-internalize");
272480093f4SDimitry Andric         // Objects referenced by writeonly GV initializer should not be
273480093f4SDimitry Andric         // promoted, because there is no any kind of read access to them
274480093f4SDimitry Andric         // on behalf of this writeonly GV. To avoid promotion we convert
275480093f4SDimitry Andric         // GV initializer to 'zeroinitializer'. This effectively drops
276480093f4SDimitry Andric         // references in IR module (not in combined index), so we can
277480093f4SDimitry Andric         // ignore them when computing import. We do not export references
278480093f4SDimitry Andric         // of writeonly object. See computeImportForReferencedGlobals
279480093f4SDimitry Andric         if (ImportIndex.isWriteOnly(GVS))
280480093f4SDimitry Andric           V->setInitializer(Constant::getNullValue(V->getValueType()));
281480093f4SDimitry Andric       }
282480093f4SDimitry Andric     }
2830b57cec5SDimitry Andric   }
2840b57cec5SDimitry Andric 
285480093f4SDimitry Andric   if (GV.hasLocalLinkage() && shouldPromoteLocalToGlobal(&GV, VI)) {
2860b57cec5SDimitry Andric     // Save the original name string before we rename GV below.
2870b57cec5SDimitry Andric     auto Name = GV.getName().str();
288480093f4SDimitry Andric     GV.setName(getPromotedName(&GV));
289480093f4SDimitry Andric     GV.setLinkage(getLinkage(&GV, /* DoPromote */ true));
290480093f4SDimitry Andric     assert(!GV.hasLocalLinkage());
2910b57cec5SDimitry Andric     GV.setVisibility(GlobalValue::HiddenVisibility);
2920b57cec5SDimitry Andric 
2930b57cec5SDimitry Andric     // If we are renaming a COMDAT leader, ensure that we record the COMDAT
2940b57cec5SDimitry Andric     // for later renaming as well. This is required for COFF.
2950b57cec5SDimitry Andric     if (const auto *C = GV.getComdat())
2960b57cec5SDimitry Andric       if (C->getName() == Name)
2970b57cec5SDimitry Andric         RenamedComdats.try_emplace(C, M.getOrInsertComdat(GV.getName()));
2980b57cec5SDimitry Andric   } else
2990b57cec5SDimitry Andric     GV.setLinkage(getLinkage(&GV, /* DoPromote */ false));
3000b57cec5SDimitry Andric 
3015ffd83dbSDimitry Andric   // When ClearDSOLocalOnDeclarations is true, clear dso_local if GV is
3025ffd83dbSDimitry Andric   // converted to a declaration, to disable direct access. Don't do this if GV
3035ffd83dbSDimitry Andric   // is implicitly dso_local due to a non-default visibility.
304fe6060f1SDimitry Andric   if (ClearDSOLocalOnDeclarations &&
305fe6060f1SDimitry Andric       (GV.isDeclarationForLinker() ||
306fe6060f1SDimitry Andric        (isPerformingImport() && !doImportAsDefinition(&GV))) &&
3075ffd83dbSDimitry Andric       !GV.isImplicitDSOLocal()) {
3085ffd83dbSDimitry Andric     GV.setDSOLocal(false);
309fe6060f1SDimitry Andric   } else if (VI && VI.isDSOLocal(ImportIndex.withDSOLocalPropagation())) {
3105ffd83dbSDimitry Andric     // If all summaries are dso_local, symbol gets resolved to a known local
3115ffd83dbSDimitry Andric     // definition.
3125ffd83dbSDimitry Andric     GV.setDSOLocal(true);
3135ffd83dbSDimitry Andric     if (GV.hasDLLImportStorageClass())
3145ffd83dbSDimitry Andric       GV.setDLLStorageClass(GlobalValue::DefaultStorageClass);
3155ffd83dbSDimitry Andric   }
3165ffd83dbSDimitry Andric 
3170b57cec5SDimitry Andric   // Remove functions imported as available externally defs from comdats,
3180b57cec5SDimitry Andric   // as this is a declaration for the linker, and will be dropped eventually.
3190b57cec5SDimitry Andric   // It is illegal for comdats to contain declarations.
3200b57cec5SDimitry Andric   auto *GO = dyn_cast<GlobalObject>(&GV);
3210b57cec5SDimitry Andric   if (GO && GO->isDeclarationForLinker() && GO->hasComdat()) {
3220b57cec5SDimitry Andric     // The IRMover should not have placed any imported declarations in
3230b57cec5SDimitry Andric     // a comdat, so the only declaration that should be in a comdat
3240b57cec5SDimitry Andric     // at this point would be a definition imported as available_externally.
3250b57cec5SDimitry Andric     assert(GO->hasAvailableExternallyLinkage() &&
3260b57cec5SDimitry Andric            "Expected comdat on definition (possibly available external)");
3270b57cec5SDimitry Andric     GO->setComdat(nullptr);
3280b57cec5SDimitry Andric   }
3290b57cec5SDimitry Andric }
3300b57cec5SDimitry Andric 
processGlobalsForThinLTO()3310b57cec5SDimitry Andric void FunctionImportGlobalProcessing::processGlobalsForThinLTO() {
3320b57cec5SDimitry Andric   for (GlobalVariable &GV : M.globals())
3330b57cec5SDimitry Andric     processGlobalForThinLTO(GV);
3340b57cec5SDimitry Andric   for (Function &SF : M)
3350b57cec5SDimitry Andric     processGlobalForThinLTO(SF);
3360b57cec5SDimitry Andric   for (GlobalAlias &GA : M.aliases())
3370b57cec5SDimitry Andric     processGlobalForThinLTO(GA);
3380b57cec5SDimitry Andric 
3390b57cec5SDimitry Andric   // Replace any COMDATS that required renaming (because the COMDAT leader was
3400b57cec5SDimitry Andric   // promoted and renamed).
3410b57cec5SDimitry Andric   if (!RenamedComdats.empty())
3420b57cec5SDimitry Andric     for (auto &GO : M.global_objects())
3430b57cec5SDimitry Andric       if (auto *C = GO.getComdat()) {
3440b57cec5SDimitry Andric         auto Replacement = RenamedComdats.find(C);
3450b57cec5SDimitry Andric         if (Replacement != RenamedComdats.end())
3460b57cec5SDimitry Andric           GO.setComdat(Replacement->second);
3470b57cec5SDimitry Andric       }
3480b57cec5SDimitry Andric }
3490b57cec5SDimitry Andric 
run()3500b57cec5SDimitry Andric bool FunctionImportGlobalProcessing::run() {
3510b57cec5SDimitry Andric   processGlobalsForThinLTO();
3520b57cec5SDimitry Andric   return false;
3530b57cec5SDimitry Andric }
3540b57cec5SDimitry Andric 
renameModuleForThinLTO(Module & M,const ModuleSummaryIndex & Index,bool ClearDSOLocalOnDeclarations,SetVector<GlobalValue * > * GlobalsToImport)3550b57cec5SDimitry Andric bool llvm::renameModuleForThinLTO(Module &M, const ModuleSummaryIndex &Index,
3565ffd83dbSDimitry Andric                                   bool ClearDSOLocalOnDeclarations,
3570b57cec5SDimitry Andric                                   SetVector<GlobalValue *> *GlobalsToImport) {
3585ffd83dbSDimitry Andric   FunctionImportGlobalProcessing ThinLTOProcessing(M, Index, GlobalsToImport,
3595ffd83dbSDimitry Andric                                                    ClearDSOLocalOnDeclarations);
3600b57cec5SDimitry Andric   return ThinLTOProcessing.run();
3610b57cec5SDimitry Andric }
362