13ca95b02SDimitry Andric //===- lib/Transforms/Utils/FunctionImportUtils.cpp - Importing utilities -===//
23ca95b02SDimitry Andric //
33ca95b02SDimitry Andric // The LLVM Compiler Infrastructure
43ca95b02SDimitry Andric //
53ca95b02SDimitry Andric // This file is distributed under the University of Illinois Open Source
63ca95b02SDimitry Andric // License. See LICENSE.TXT for details.
73ca95b02SDimitry Andric //
83ca95b02SDimitry Andric //===----------------------------------------------------------------------===//
93ca95b02SDimitry Andric //
103ca95b02SDimitry Andric // This file implements the FunctionImportGlobalProcessing class, used
113ca95b02SDimitry Andric // to perform the necessary global value handling for function importing.
123ca95b02SDimitry Andric //
133ca95b02SDimitry Andric //===----------------------------------------------------------------------===//
143ca95b02SDimitry Andric
153ca95b02SDimitry Andric #include "llvm/Transforms/Utils/FunctionImportUtils.h"
163ca95b02SDimitry Andric #include "llvm/IR/InstIterator.h"
173ca95b02SDimitry Andric using namespace llvm;
183ca95b02SDimitry Andric
193ca95b02SDimitry Andric /// Checks if we should import SGV as a definition, otherwise import as a
203ca95b02SDimitry Andric /// declaration.
doImportAsDefinition(const GlobalValue * SGV,SetVector<GlobalValue * > * GlobalsToImport)213ca95b02SDimitry Andric bool FunctionImportGlobalProcessing::doImportAsDefinition(
227a7e6055SDimitry Andric const GlobalValue *SGV, SetVector<GlobalValue *> *GlobalsToImport) {
233ca95b02SDimitry Andric
243ca95b02SDimitry Andric // Only import the globals requested for importing.
252cab237bSDimitry Andric if (!GlobalsToImport->count(const_cast<GlobalValue *>(SGV)))
263ca95b02SDimitry Andric return false;
272cab237bSDimitry Andric
282cab237bSDimitry Andric assert(!isa<GlobalAlias>(SGV) &&
292cab237bSDimitry Andric "Unexpected global alias in the import list.");
302cab237bSDimitry Andric
312cab237bSDimitry Andric // Otherwise yes.
322cab237bSDimitry Andric return true;
333ca95b02SDimitry Andric }
343ca95b02SDimitry Andric
doImportAsDefinition(const GlobalValue * SGV)353ca95b02SDimitry Andric bool FunctionImportGlobalProcessing::doImportAsDefinition(
363ca95b02SDimitry Andric const GlobalValue *SGV) {
373ca95b02SDimitry Andric if (!isPerformingImport())
383ca95b02SDimitry Andric return false;
393ca95b02SDimitry Andric return FunctionImportGlobalProcessing::doImportAsDefinition(SGV,
403ca95b02SDimitry Andric GlobalsToImport);
413ca95b02SDimitry Andric }
423ca95b02SDimitry Andric
shouldPromoteLocalToGlobal(const GlobalValue * SGV)43d88c1a5aSDimitry Andric bool FunctionImportGlobalProcessing::shouldPromoteLocalToGlobal(
443ca95b02SDimitry Andric const GlobalValue *SGV) {
453ca95b02SDimitry Andric assert(SGV->hasLocalLinkage());
463ca95b02SDimitry Andric // Both the imported references and the original local variable must
473ca95b02SDimitry Andric // be promoted.
483ca95b02SDimitry Andric if (!isPerformingImport() && !isModuleExporting())
493ca95b02SDimitry Andric return false;
503ca95b02SDimitry Andric
51d88c1a5aSDimitry Andric if (isPerformingImport()) {
527a7e6055SDimitry Andric assert((!GlobalsToImport->count(const_cast<GlobalValue *>(SGV)) ||
537a7e6055SDimitry Andric !isNonRenamableLocal(*SGV)) &&
5495ec533aSDimitry Andric "Attempting to promote non-renamable local");
55d88c1a5aSDimitry Andric // We don't know for sure yet if we are importing this value (as either
56d88c1a5aSDimitry Andric // a reference or a def), since we are simply walking all values in the
57d88c1a5aSDimitry Andric // module. But by necessity if we end up importing it and it is local,
58d88c1a5aSDimitry Andric // it must be promoted, so unconditionally promote all values in the
59d88c1a5aSDimitry Andric // importing module.
603ca95b02SDimitry Andric return true;
613ca95b02SDimitry Andric }
623ca95b02SDimitry Andric
6324e2fe98SDimitry Andric // When exporting, consult the index. We can have more than one local
6424e2fe98SDimitry Andric // with the same GUID, in the case of same-named locals in different but
6524e2fe98SDimitry Andric // same-named source files that were compiled in their respective directories
6624e2fe98SDimitry Andric // (so the source file name and resulting GUID is the same). Find the one
6724e2fe98SDimitry Andric // in this module.
6824e2fe98SDimitry Andric auto Summary = ImportIndex.findSummaryInModule(
6924e2fe98SDimitry Andric SGV->getGUID(), SGV->getParent()->getModuleIdentifier());
7024e2fe98SDimitry Andric assert(Summary && "Missing summary for global value when exporting");
7124e2fe98SDimitry Andric auto Linkage = Summary->linkage();
72d88c1a5aSDimitry Andric if (!GlobalValue::isLocalLinkage(Linkage)) {
7395ec533aSDimitry Andric assert(!isNonRenamableLocal(*SGV) &&
7495ec533aSDimitry Andric "Attempting to promote non-renamable local");
75d88c1a5aSDimitry Andric return true;
76d88c1a5aSDimitry Andric }
77d88c1a5aSDimitry Andric
78d88c1a5aSDimitry Andric return false;
79d88c1a5aSDimitry Andric }
80d88c1a5aSDimitry Andric
8195ec533aSDimitry Andric #ifndef NDEBUG
isNonRenamableLocal(const GlobalValue & GV) const8295ec533aSDimitry Andric bool FunctionImportGlobalProcessing::isNonRenamableLocal(
8395ec533aSDimitry Andric const GlobalValue &GV) const {
8495ec533aSDimitry Andric if (!GV.hasLocalLinkage())
8595ec533aSDimitry Andric return false;
8695ec533aSDimitry Andric // This needs to stay in sync with the logic in buildModuleSummaryIndex.
8795ec533aSDimitry Andric if (GV.hasSection())
8895ec533aSDimitry Andric return true;
8995ec533aSDimitry Andric if (Used.count(const_cast<GlobalValue *>(&GV)))
9095ec533aSDimitry Andric return true;
9195ec533aSDimitry Andric return false;
9295ec533aSDimitry Andric }
9395ec533aSDimitry Andric #endif
9495ec533aSDimitry Andric
getName(const GlobalValue * SGV,bool DoPromote)95d88c1a5aSDimitry Andric std::string FunctionImportGlobalProcessing::getName(const GlobalValue *SGV,
96d88c1a5aSDimitry Andric bool DoPromote) {
973ca95b02SDimitry Andric // For locals that must be promoted to global scope, ensure that
983ca95b02SDimitry Andric // the promoted name uniquely identifies the copy in the original module,
993ca95b02SDimitry Andric // using the ID assigned during combined index creation. When importing,
1003ca95b02SDimitry Andric // we rename all locals (not just those that are promoted) in order to
1013ca95b02SDimitry Andric // avoid naming conflicts between locals imported from different modules.
102d88c1a5aSDimitry Andric if (SGV->hasLocalLinkage() && (DoPromote || isPerformingImport()))
1033ca95b02SDimitry Andric return ModuleSummaryIndex::getGlobalNameForLocal(
1043ca95b02SDimitry Andric SGV->getName(),
1053ca95b02SDimitry Andric ImportIndex.getModuleHash(SGV->getParent()->getModuleIdentifier()));
1063ca95b02SDimitry Andric return SGV->getName();
1073ca95b02SDimitry Andric }
1083ca95b02SDimitry Andric
1093ca95b02SDimitry Andric GlobalValue::LinkageTypes
getLinkage(const GlobalValue * SGV,bool DoPromote)110d88c1a5aSDimitry Andric FunctionImportGlobalProcessing::getLinkage(const GlobalValue *SGV,
111d88c1a5aSDimitry Andric bool DoPromote) {
1123ca95b02SDimitry Andric // Any local variable that is referenced by an exported function needs
1133ca95b02SDimitry Andric // to be promoted to global scope. Since we don't currently know which
1143ca95b02SDimitry Andric // functions reference which local variables/functions, we must treat
1153ca95b02SDimitry Andric // all as potentially exported if this module is exporting anything.
1163ca95b02SDimitry Andric if (isModuleExporting()) {
117d88c1a5aSDimitry Andric if (SGV->hasLocalLinkage() && DoPromote)
1183ca95b02SDimitry Andric return GlobalValue::ExternalLinkage;
1193ca95b02SDimitry Andric return SGV->getLinkage();
1203ca95b02SDimitry Andric }
1213ca95b02SDimitry Andric
1223ca95b02SDimitry Andric // Otherwise, if we aren't importing, no linkage change is needed.
1233ca95b02SDimitry Andric if (!isPerformingImport())
1243ca95b02SDimitry Andric return SGV->getLinkage();
1253ca95b02SDimitry Andric
1263ca95b02SDimitry Andric switch (SGV->getLinkage()) {
1272cab237bSDimitry Andric case GlobalValue::LinkOnceODRLinkage:
1283ca95b02SDimitry Andric case GlobalValue::ExternalLinkage:
1292cab237bSDimitry Andric // External and linkonce definitions are converted to available_externally
1303ca95b02SDimitry Andric // definitions upon import, so that they are available for inlining
1313ca95b02SDimitry Andric // and/or optimization, but are turned into declarations later
1323ca95b02SDimitry Andric // during the EliminateAvailableExternally pass.
1333ca95b02SDimitry Andric if (doImportAsDefinition(SGV) && !dyn_cast<GlobalAlias>(SGV))
1343ca95b02SDimitry Andric return GlobalValue::AvailableExternallyLinkage;
1353ca95b02SDimitry Andric // An imported external declaration stays external.
1363ca95b02SDimitry Andric return SGV->getLinkage();
1373ca95b02SDimitry Andric
1383ca95b02SDimitry Andric case GlobalValue::AvailableExternallyLinkage:
1393ca95b02SDimitry Andric // An imported available_externally definition converts
1403ca95b02SDimitry Andric // to external if imported as a declaration.
1413ca95b02SDimitry Andric if (!doImportAsDefinition(SGV))
1423ca95b02SDimitry Andric return GlobalValue::ExternalLinkage;
1433ca95b02SDimitry Andric // An imported available_externally declaration stays that way.
1443ca95b02SDimitry Andric return SGV->getLinkage();
1453ca95b02SDimitry Andric
146*b5893f02SDimitry Andric case GlobalValue::LinkOnceAnyLinkage:
1473ca95b02SDimitry Andric case GlobalValue::WeakAnyLinkage:
148*b5893f02SDimitry Andric // Can't import linkonce_any/weak_any definitions correctly, or we might
149*b5893f02SDimitry Andric // change the program semantics, since the linker will pick the first
150*b5893f02SDimitry Andric // linkonce_any/weak_any definition and importing would change the order
151*b5893f02SDimitry Andric // they are seen by the linker. The module linking caller needs to enforce
152*b5893f02SDimitry Andric // this.
1533ca95b02SDimitry Andric assert(!doImportAsDefinition(SGV));
1543ca95b02SDimitry Andric // If imported as a declaration, it becomes external_weak.
1553ca95b02SDimitry Andric return SGV->getLinkage();
1563ca95b02SDimitry Andric
1573ca95b02SDimitry Andric case GlobalValue::WeakODRLinkage:
1583ca95b02SDimitry Andric // For weak_odr linkage, there is a guarantee that all copies will be
1593ca95b02SDimitry Andric // equivalent, so the issue described above for weak_any does not exist,
1603ca95b02SDimitry Andric // and the definition can be imported. It can be treated similarly
1613ca95b02SDimitry Andric // to an imported externally visible global value.
1623ca95b02SDimitry Andric if (doImportAsDefinition(SGV) && !dyn_cast<GlobalAlias>(SGV))
1633ca95b02SDimitry Andric return GlobalValue::AvailableExternallyLinkage;
1643ca95b02SDimitry Andric else
1653ca95b02SDimitry Andric return GlobalValue::ExternalLinkage;
1663ca95b02SDimitry Andric
1673ca95b02SDimitry Andric case GlobalValue::AppendingLinkage:
1683ca95b02SDimitry Andric // It would be incorrect to import an appending linkage variable,
1693ca95b02SDimitry Andric // since it would cause global constructors/destructors to be
1703ca95b02SDimitry Andric // executed multiple times. This should have already been handled
1713ca95b02SDimitry Andric // by linkIfNeeded, and we will assert in shouldLinkFromSource
1723ca95b02SDimitry Andric // if we try to import, so we simply return AppendingLinkage.
1733ca95b02SDimitry Andric return GlobalValue::AppendingLinkage;
1743ca95b02SDimitry Andric
1753ca95b02SDimitry Andric case GlobalValue::InternalLinkage:
1763ca95b02SDimitry Andric case GlobalValue::PrivateLinkage:
1773ca95b02SDimitry Andric // If we are promoting the local to global scope, it is handled
1783ca95b02SDimitry Andric // similarly to a normal externally visible global.
179d88c1a5aSDimitry Andric if (DoPromote) {
1803ca95b02SDimitry Andric if (doImportAsDefinition(SGV) && !dyn_cast<GlobalAlias>(SGV))
1813ca95b02SDimitry Andric return GlobalValue::AvailableExternallyLinkage;
1823ca95b02SDimitry Andric else
1833ca95b02SDimitry Andric return GlobalValue::ExternalLinkage;
1843ca95b02SDimitry Andric }
1853ca95b02SDimitry Andric // A non-promoted imported local definition stays local.
1863ca95b02SDimitry Andric // The ThinLTO pass will eventually force-import their definitions.
1873ca95b02SDimitry Andric return SGV->getLinkage();
1883ca95b02SDimitry Andric
1893ca95b02SDimitry Andric case GlobalValue::ExternalWeakLinkage:
1903ca95b02SDimitry Andric // External weak doesn't apply to definitions, must be a declaration.
1913ca95b02SDimitry Andric assert(!doImportAsDefinition(SGV));
1923ca95b02SDimitry Andric // Linkage stays external_weak.
1933ca95b02SDimitry Andric return SGV->getLinkage();
1943ca95b02SDimitry Andric
1953ca95b02SDimitry Andric case GlobalValue::CommonLinkage:
1963ca95b02SDimitry Andric // Linkage stays common on definitions.
1973ca95b02SDimitry Andric // The ThinLTO pass will eventually force-import their definitions.
1983ca95b02SDimitry Andric return SGV->getLinkage();
1993ca95b02SDimitry Andric }
2003ca95b02SDimitry Andric
2013ca95b02SDimitry Andric llvm_unreachable("unknown linkage type");
2023ca95b02SDimitry Andric }
2033ca95b02SDimitry Andric
processGlobalForThinLTO(GlobalValue & GV)2043ca95b02SDimitry Andric void FunctionImportGlobalProcessing::processGlobalForThinLTO(GlobalValue &GV) {
2052cab237bSDimitry Andric
206*b5893f02SDimitry Andric ValueInfo VI;
207*b5893f02SDimitry Andric if (GV.hasName()) {
208*b5893f02SDimitry Andric VI = ImportIndex.getValueInfo(GV.getGUID());
209*b5893f02SDimitry Andric // Set synthetic function entry counts.
210*b5893f02SDimitry Andric if (VI && ImportIndex.hasSyntheticEntryCounts()) {
211*b5893f02SDimitry Andric if (Function *F = dyn_cast<Function>(&GV)) {
212*b5893f02SDimitry Andric if (!F->isDeclaration()) {
213*b5893f02SDimitry Andric for (auto &S : VI.getSummaryList()) {
214*b5893f02SDimitry Andric FunctionSummary *FS = dyn_cast<FunctionSummary>(S->getBaseObject());
215*b5893f02SDimitry Andric if (FS->modulePath() == M.getModuleIdentifier()) {
216*b5893f02SDimitry Andric F->setEntryCount(Function::ProfileCount(FS->entryCount(),
217*b5893f02SDimitry Andric Function::PCT_Synthetic));
218*b5893f02SDimitry Andric break;
219*b5893f02SDimitry Andric }
220*b5893f02SDimitry Andric }
221*b5893f02SDimitry Andric }
222*b5893f02SDimitry Andric }
223*b5893f02SDimitry Andric }
2242cab237bSDimitry Andric // Check the summaries to see if the symbol gets resolved to a known local
2252cab237bSDimitry Andric // definition.
2264ba319b5SDimitry Andric if (VI && VI.isDSOLocal()) {
2272cab237bSDimitry Andric GV.setDSOLocal(true);
2284ba319b5SDimitry Andric if (GV.hasDLLImportStorageClass())
2294ba319b5SDimitry Andric GV.setDLLStorageClass(GlobalValue::DefaultStorageClass);
2302cab237bSDimitry Andric }
2312cab237bSDimitry Andric }
2322cab237bSDimitry Andric
233*b5893f02SDimitry Andric // Mark read-only variables which can be imported with specific attribute.
234*b5893f02SDimitry Andric // We can't internalize them now because IRMover will fail to link variable
235*b5893f02SDimitry Andric // definitions to their external declarations during ThinLTO import. We'll
236*b5893f02SDimitry Andric // internalize read-only variables later, after import is finished.
237*b5893f02SDimitry Andric // See internalizeImmutableGVs.
238*b5893f02SDimitry Andric //
239*b5893f02SDimitry Andric // If global value dead stripping is not enabled in summary then
240*b5893f02SDimitry Andric // propagateConstants hasn't been run. We can't internalize GV
241*b5893f02SDimitry Andric // in such case.
242*b5893f02SDimitry Andric if (!GV.isDeclaration() && VI && ImportIndex.withGlobalValueDeadStripping()) {
243*b5893f02SDimitry Andric const auto &SL = VI.getSummaryList();
244*b5893f02SDimitry Andric auto *GVS = SL.empty() ? nullptr : dyn_cast<GlobalVarSummary>(SL[0].get());
245*b5893f02SDimitry Andric if (GVS && GVS->isReadOnly())
246*b5893f02SDimitry Andric cast<GlobalVariable>(&GV)->addAttribute("thinlto-internalize");
247*b5893f02SDimitry Andric }
248*b5893f02SDimitry Andric
249d88c1a5aSDimitry Andric bool DoPromote = false;
2503ca95b02SDimitry Andric if (GV.hasLocalLinkage() &&
251d88c1a5aSDimitry Andric ((DoPromote = shouldPromoteLocalToGlobal(&GV)) || isPerformingImport())) {
252*b5893f02SDimitry Andric // Save the original name string before we rename GV below.
253*b5893f02SDimitry Andric auto Name = GV.getName().str();
254d88c1a5aSDimitry Andric // Once we change the name or linkage it is difficult to determine
255d88c1a5aSDimitry Andric // again whether we should promote since shouldPromoteLocalToGlobal needs
256d88c1a5aSDimitry Andric // to locate the summary (based on GUID from name and linkage). Therefore,
257d88c1a5aSDimitry Andric // use DoPromote result saved above.
258d88c1a5aSDimitry Andric GV.setName(getName(&GV, DoPromote));
259d88c1a5aSDimitry Andric GV.setLinkage(getLinkage(&GV, DoPromote));
2603ca95b02SDimitry Andric if (!GV.hasLocalLinkage())
2613ca95b02SDimitry Andric GV.setVisibility(GlobalValue::HiddenVisibility);
262*b5893f02SDimitry Andric
263*b5893f02SDimitry Andric // If we are renaming a COMDAT leader, ensure that we record the COMDAT
264*b5893f02SDimitry Andric // for later renaming as well. This is required for COFF.
265*b5893f02SDimitry Andric if (const auto *C = GV.getComdat())
266*b5893f02SDimitry Andric if (C->getName() == Name)
267*b5893f02SDimitry Andric RenamedComdats.try_emplace(C, M.getOrInsertComdat(GV.getName()));
2683ca95b02SDimitry Andric } else
269d88c1a5aSDimitry Andric GV.setLinkage(getLinkage(&GV, /* DoPromote */ false));
2703ca95b02SDimitry Andric
2713ca95b02SDimitry Andric // Remove functions imported as available externally defs from comdats,
2723ca95b02SDimitry Andric // as this is a declaration for the linker, and will be dropped eventually.
2733ca95b02SDimitry Andric // It is illegal for comdats to contain declarations.
274*b5893f02SDimitry Andric auto *GO = dyn_cast<GlobalObject>(&GV);
2753ca95b02SDimitry Andric if (GO && GO->isDeclarationForLinker() && GO->hasComdat()) {
2763ca95b02SDimitry Andric // The IRMover should not have placed any imported declarations in
2773ca95b02SDimitry Andric // a comdat, so the only declaration that should be in a comdat
2783ca95b02SDimitry Andric // at this point would be a definition imported as available_externally.
2793ca95b02SDimitry Andric assert(GO->hasAvailableExternallyLinkage() &&
2803ca95b02SDimitry Andric "Expected comdat on definition (possibly available external)");
2813ca95b02SDimitry Andric GO->setComdat(nullptr);
2823ca95b02SDimitry Andric }
2833ca95b02SDimitry Andric }
2843ca95b02SDimitry Andric
processGlobalsForThinLTO()2853ca95b02SDimitry Andric void FunctionImportGlobalProcessing::processGlobalsForThinLTO() {
2863ca95b02SDimitry Andric for (GlobalVariable &GV : M.globals())
2873ca95b02SDimitry Andric processGlobalForThinLTO(GV);
2883ca95b02SDimitry Andric for (Function &SF : M)
2893ca95b02SDimitry Andric processGlobalForThinLTO(SF);
2903ca95b02SDimitry Andric for (GlobalAlias &GA : M.aliases())
2913ca95b02SDimitry Andric processGlobalForThinLTO(GA);
292*b5893f02SDimitry Andric
293*b5893f02SDimitry Andric // Replace any COMDATS that required renaming (because the COMDAT leader was
294*b5893f02SDimitry Andric // promoted and renamed).
295*b5893f02SDimitry Andric if (!RenamedComdats.empty())
296*b5893f02SDimitry Andric for (auto &GO : M.global_objects())
297*b5893f02SDimitry Andric if (auto *C = GO.getComdat()) {
298*b5893f02SDimitry Andric auto Replacement = RenamedComdats.find(C);
299*b5893f02SDimitry Andric if (Replacement != RenamedComdats.end())
300*b5893f02SDimitry Andric GO.setComdat(Replacement->second);
301*b5893f02SDimitry Andric }
3023ca95b02SDimitry Andric }
3033ca95b02SDimitry Andric
run()3043ca95b02SDimitry Andric bool FunctionImportGlobalProcessing::run() {
3053ca95b02SDimitry Andric processGlobalsForThinLTO();
3063ca95b02SDimitry Andric return false;
3073ca95b02SDimitry Andric }
3083ca95b02SDimitry Andric
renameModuleForThinLTO(Module & M,const ModuleSummaryIndex & Index,SetVector<GlobalValue * > * GlobalsToImport)3097a7e6055SDimitry Andric bool llvm::renameModuleForThinLTO(Module &M, const ModuleSummaryIndex &Index,
3107a7e6055SDimitry Andric SetVector<GlobalValue *> *GlobalsToImport) {
3113ca95b02SDimitry Andric FunctionImportGlobalProcessing ThinLTOProcessing(M, Index, GlobalsToImport);
3123ca95b02SDimitry Andric return ThinLTOProcessing.run();
3133ca95b02SDimitry Andric }
314