1488a800aSTeresa Johnson //===- lib/Transforms/Utils/FunctionImportUtils.cpp - Importing utilities -===// 2488a800aSTeresa Johnson // 3488a800aSTeresa Johnson // The LLVM Compiler Infrastructure 4488a800aSTeresa Johnson // 5488a800aSTeresa Johnson // This file is distributed under the University of Illinois Open Source 6488a800aSTeresa Johnson // License. See LICENSE.TXT for details. 7488a800aSTeresa Johnson // 8488a800aSTeresa Johnson //===----------------------------------------------------------------------===// 9488a800aSTeresa Johnson // 10488a800aSTeresa Johnson // This file implements the FunctionImportGlobalProcessing class, used 11488a800aSTeresa Johnson // to perform the necessary global value handling for function importing. 12488a800aSTeresa Johnson // 13488a800aSTeresa Johnson //===----------------------------------------------------------------------===// 14488a800aSTeresa Johnson 153b132e34SMehdi Amini #include "llvm/Analysis/ModuleSummaryAnalysis.h" 16488a800aSTeresa Johnson #include "llvm/Transforms/Utils/FunctionImportUtils.h" 17df5ef871STeresa Johnson #include "llvm/IR/InstIterator.h" 18df5ef871STeresa Johnson #include "llvm/IR/Instructions.h" 19488a800aSTeresa Johnson using namespace llvm; 20488a800aSTeresa Johnson 21488a800aSTeresa Johnson /// Checks if we should import SGV as a definition, otherwise import as a 22488a800aSTeresa Johnson /// declaration. 23488a800aSTeresa Johnson bool FunctionImportGlobalProcessing::doImportAsDefinition( 248d05185aSMehdi Amini const GlobalValue *SGV, DenseSet<const GlobalValue *> *GlobalsToImport) { 258d05185aSMehdi Amini 268d05185aSMehdi Amini // For alias, we tie the definition to the base object. Extract it and recurse 278d05185aSMehdi Amini if (auto *GA = dyn_cast<GlobalAlias>(SGV)) { 28488a800aSTeresa Johnson if (GA->hasWeakAnyLinkage()) 29488a800aSTeresa Johnson return false; 30488a800aSTeresa Johnson const GlobalObject *GO = GA->getBaseObject(); 31488a800aSTeresa Johnson if (!GO->hasLinkOnceODRLinkage()) 32488a800aSTeresa Johnson return false; 33488a800aSTeresa Johnson return FunctionImportGlobalProcessing::doImportAsDefinition( 348d05185aSMehdi Amini GO, GlobalsToImport); 35488a800aSTeresa Johnson } 368d05185aSMehdi Amini // Only import the globals requested for importing. 378d05185aSMehdi Amini if (GlobalsToImport->count(SGV)) 38488a800aSTeresa Johnson return true; 39488a800aSTeresa Johnson // Otherwise no. 40488a800aSTeresa Johnson return false; 41488a800aSTeresa Johnson } 42488a800aSTeresa Johnson 43488a800aSTeresa Johnson bool FunctionImportGlobalProcessing::doImportAsDefinition( 44488a800aSTeresa Johnson const GlobalValue *SGV) { 45488a800aSTeresa Johnson if (!isPerformingImport()) 46488a800aSTeresa Johnson return false; 478d05185aSMehdi Amini return FunctionImportGlobalProcessing::doImportAsDefinition(SGV, 488d05185aSMehdi Amini GlobalsToImport); 49488a800aSTeresa Johnson } 50488a800aSTeresa Johnson 5138d4df71STeresa Johnson bool FunctionImportGlobalProcessing::shouldPromoteLocalToGlobal( 52488a800aSTeresa Johnson const GlobalValue *SGV) { 53488a800aSTeresa Johnson assert(SGV->hasLocalLinkage()); 54488a800aSTeresa Johnson // Both the imported references and the original local variable must 55488a800aSTeresa Johnson // be promoted. 56488a800aSTeresa Johnson if (!isPerformingImport() && !isModuleExporting()) 57488a800aSTeresa Johnson return false; 58488a800aSTeresa Johnson 59488a800aSTeresa Johnson // Local const variables never need to be promoted unless they are address 60488a800aSTeresa Johnson // taken. The imported uses can simply use the clone created in this module. 61488a800aSTeresa Johnson // For now we are conservative in determining which variables are not 62488a800aSTeresa Johnson // address taken by checking the unnamed addr flag. To be more aggressive, 63488a800aSTeresa Johnson // the address taken information must be checked earlier during parsing 6426ab5772STeresa Johnson // of the module and recorded in the summary index for use when importing 65488a800aSTeresa Johnson // from that module. 66488a800aSTeresa Johnson auto *GVar = dyn_cast<GlobalVariable>(SGV); 6796efdd61SPeter Collingbourne if (GVar && GVar->isConstant() && GVar->hasGlobalUnnamedAddr()) 68488a800aSTeresa Johnson return false; 69488a800aSTeresa Johnson 70*0515fb8dSTeresa Johnson // If we are exporting, we need to see whether this value is marked 71*0515fb8dSTeresa Johnson // as NoRename in the summary. If we are importing, we may not have 72*0515fb8dSTeresa Johnson // a summary in the distributed backend case (only summaries for values 73*0515fb8dSTeresa Johnson // importes as defs, not references, are included in the index passed 74*0515fb8dSTeresa Johnson // to the distributed backends). 75*0515fb8dSTeresa Johnson auto Summaries = ImportIndex.findGlobalValueSummaryList(SGV->getGUID()); 76*0515fb8dSTeresa Johnson if (Summaries == ImportIndex.end()) 77*0515fb8dSTeresa Johnson // Assert that this is an import - we should always have access to the 78*0515fb8dSTeresa Johnson // summary when exporting. 79*0515fb8dSTeresa Johnson assert(isPerformingImport() && 80*0515fb8dSTeresa Johnson "Missing summary for global value when exporting"); 81*0515fb8dSTeresa Johnson else { 82*0515fb8dSTeresa Johnson assert(Summaries->second.size() == 1 && "Local has more than one summary"); 83*0515fb8dSTeresa Johnson if (Summaries->second.front()->noRename()) { 84*0515fb8dSTeresa Johnson assert((isModuleExporting() || !GlobalsToImport->count(SGV)) && 85*0515fb8dSTeresa Johnson "Imported a non-renamable local value"); 86b4e1e829SMehdi Amini return false; 87*0515fb8dSTeresa Johnson } 88*0515fb8dSTeresa Johnson } 89b4e1e829SMehdi Amini 90488a800aSTeresa Johnson // Eventually we only need to promote functions in the exporting module that 91488a800aSTeresa Johnson // are referenced by a potentially exported function (i.e. one that is in the 9226ab5772STeresa Johnson // summary index). 93488a800aSTeresa Johnson return true; 94488a800aSTeresa Johnson } 95488a800aSTeresa Johnson 961b9c2be8STeresa Johnson std::string FunctionImportGlobalProcessing::getName(const GlobalValue *SGV, 971b9c2be8STeresa Johnson bool DoPromote) { 98488a800aSTeresa Johnson // For locals that must be promoted to global scope, ensure that 99488a800aSTeresa Johnson // the promoted name uniquely identifies the copy in the original module, 100488a800aSTeresa Johnson // using the ID assigned during combined index creation. When importing, 101488a800aSTeresa Johnson // we rename all locals (not just those that are promoted) in order to 102488a800aSTeresa Johnson // avoid naming conflicts between locals imported from different modules. 1031b9c2be8STeresa Johnson if (SGV->hasLocalLinkage() && (DoPromote || isPerformingImport())) 10426ab5772STeresa Johnson return ModuleSummaryIndex::getGlobalNameForLocal( 105488a800aSTeresa Johnson SGV->getName(), 106ae280e54SMehdi Amini ImportIndex.getModuleHash(SGV->getParent()->getModuleIdentifier())); 107488a800aSTeresa Johnson return SGV->getName(); 108488a800aSTeresa Johnson } 109488a800aSTeresa Johnson 110488a800aSTeresa Johnson GlobalValue::LinkageTypes 1111b9c2be8STeresa Johnson FunctionImportGlobalProcessing::getLinkage(const GlobalValue *SGV, 1121b9c2be8STeresa Johnson bool DoPromote) { 113488a800aSTeresa Johnson // Any local variable that is referenced by an exported function needs 114488a800aSTeresa Johnson // to be promoted to global scope. Since we don't currently know which 115488a800aSTeresa Johnson // functions reference which local variables/functions, we must treat 116488a800aSTeresa Johnson // all as potentially exported if this module is exporting anything. 117488a800aSTeresa Johnson if (isModuleExporting()) { 1181b9c2be8STeresa Johnson if (SGV->hasLocalLinkage() && DoPromote) 119488a800aSTeresa Johnson return GlobalValue::ExternalLinkage; 120488a800aSTeresa Johnson return SGV->getLinkage(); 121488a800aSTeresa Johnson } 122488a800aSTeresa Johnson 123488a800aSTeresa Johnson // Otherwise, if we aren't importing, no linkage change is needed. 124488a800aSTeresa Johnson if (!isPerformingImport()) 125488a800aSTeresa Johnson return SGV->getLinkage(); 126488a800aSTeresa Johnson 127488a800aSTeresa Johnson switch (SGV->getLinkage()) { 128488a800aSTeresa Johnson case GlobalValue::ExternalLinkage: 129488a800aSTeresa Johnson // External defnitions are converted to available_externally 130488a800aSTeresa Johnson // definitions upon import, so that they are available for inlining 131488a800aSTeresa Johnson // and/or optimization, but are turned into declarations later 132488a800aSTeresa Johnson // during the EliminateAvailableExternally pass. 133488a800aSTeresa Johnson if (doImportAsDefinition(SGV) && !dyn_cast<GlobalAlias>(SGV)) 134488a800aSTeresa Johnson return GlobalValue::AvailableExternallyLinkage; 135488a800aSTeresa Johnson // An imported external declaration stays external. 136488a800aSTeresa Johnson return SGV->getLinkage(); 137488a800aSTeresa Johnson 138488a800aSTeresa Johnson case GlobalValue::AvailableExternallyLinkage: 139488a800aSTeresa Johnson // An imported available_externally definition converts 140488a800aSTeresa Johnson // to external if imported as a declaration. 141488a800aSTeresa Johnson if (!doImportAsDefinition(SGV)) 142488a800aSTeresa Johnson return GlobalValue::ExternalLinkage; 143488a800aSTeresa Johnson // An imported available_externally declaration stays that way. 144488a800aSTeresa Johnson return SGV->getLinkage(); 145488a800aSTeresa Johnson 146488a800aSTeresa Johnson case GlobalValue::LinkOnceAnyLinkage: 147488a800aSTeresa Johnson case GlobalValue::LinkOnceODRLinkage: 148488a800aSTeresa Johnson // These both stay the same when importing the definition. 149488a800aSTeresa Johnson // The ThinLTO pass will eventually force-import their definitions. 150488a800aSTeresa Johnson return SGV->getLinkage(); 151488a800aSTeresa Johnson 152488a800aSTeresa Johnson case GlobalValue::WeakAnyLinkage: 153488a800aSTeresa Johnson // Can't import weak_any definitions correctly, or we might change the 154488a800aSTeresa Johnson // program semantics, since the linker will pick the first weak_any 155488a800aSTeresa Johnson // definition and importing would change the order they are seen by the 156488a800aSTeresa Johnson // linker. The module linking caller needs to enforce this. 157488a800aSTeresa Johnson assert(!doImportAsDefinition(SGV)); 158488a800aSTeresa Johnson // If imported as a declaration, it becomes external_weak. 159bb3a1d92SMehdi Amini return SGV->getLinkage(); 160488a800aSTeresa Johnson 161488a800aSTeresa Johnson case GlobalValue::WeakODRLinkage: 162488a800aSTeresa Johnson // For weak_odr linkage, there is a guarantee that all copies will be 163488a800aSTeresa Johnson // equivalent, so the issue described above for weak_any does not exist, 164488a800aSTeresa Johnson // and the definition can be imported. It can be treated similarly 165488a800aSTeresa Johnson // to an imported externally visible global value. 166488a800aSTeresa Johnson if (doImportAsDefinition(SGV) && !dyn_cast<GlobalAlias>(SGV)) 167488a800aSTeresa Johnson return GlobalValue::AvailableExternallyLinkage; 168488a800aSTeresa Johnson else 169488a800aSTeresa Johnson return GlobalValue::ExternalLinkage; 170488a800aSTeresa Johnson 171488a800aSTeresa Johnson case GlobalValue::AppendingLinkage: 172488a800aSTeresa Johnson // It would be incorrect to import an appending linkage variable, 173488a800aSTeresa Johnson // since it would cause global constructors/destructors to be 174488a800aSTeresa Johnson // executed multiple times. This should have already been handled 175488a800aSTeresa Johnson // by linkIfNeeded, and we will assert in shouldLinkFromSource 176488a800aSTeresa Johnson // if we try to import, so we simply return AppendingLinkage. 177488a800aSTeresa Johnson return GlobalValue::AppendingLinkage; 178488a800aSTeresa Johnson 179488a800aSTeresa Johnson case GlobalValue::InternalLinkage: 180488a800aSTeresa Johnson case GlobalValue::PrivateLinkage: 181488a800aSTeresa Johnson // If we are promoting the local to global scope, it is handled 182488a800aSTeresa Johnson // similarly to a normal externally visible global. 1831b9c2be8STeresa Johnson if (DoPromote) { 184488a800aSTeresa Johnson if (doImportAsDefinition(SGV) && !dyn_cast<GlobalAlias>(SGV)) 185488a800aSTeresa Johnson return GlobalValue::AvailableExternallyLinkage; 186488a800aSTeresa Johnson else 187488a800aSTeresa Johnson return GlobalValue::ExternalLinkage; 188488a800aSTeresa Johnson } 189488a800aSTeresa Johnson // A non-promoted imported local definition stays local. 190488a800aSTeresa Johnson // The ThinLTO pass will eventually force-import their definitions. 191488a800aSTeresa Johnson return SGV->getLinkage(); 192488a800aSTeresa Johnson 193488a800aSTeresa Johnson case GlobalValue::ExternalWeakLinkage: 194488a800aSTeresa Johnson // External weak doesn't apply to definitions, must be a declaration. 195488a800aSTeresa Johnson assert(!doImportAsDefinition(SGV)); 196488a800aSTeresa Johnson // Linkage stays external_weak. 197488a800aSTeresa Johnson return SGV->getLinkage(); 198488a800aSTeresa Johnson 199488a800aSTeresa Johnson case GlobalValue::CommonLinkage: 200488a800aSTeresa Johnson // Linkage stays common on definitions. 201488a800aSTeresa Johnson // The ThinLTO pass will eventually force-import their definitions. 202488a800aSTeresa Johnson return SGV->getLinkage(); 203488a800aSTeresa Johnson } 204488a800aSTeresa Johnson 205488a800aSTeresa Johnson llvm_unreachable("unknown linkage type"); 206488a800aSTeresa Johnson } 207488a800aSTeresa Johnson 208488a800aSTeresa Johnson void FunctionImportGlobalProcessing::processGlobalForThinLTO(GlobalValue &GV) { 2091b9c2be8STeresa Johnson bool DoPromote = false; 210488a800aSTeresa Johnson if (GV.hasLocalLinkage() && 21138d4df71STeresa Johnson ((DoPromote = shouldPromoteLocalToGlobal(&GV)) || isPerformingImport())) { 2121b9c2be8STeresa Johnson // Once we change the name or linkage it is difficult to determine 21338d4df71STeresa Johnson // again whether we should promote since shouldPromoteLocalToGlobal needs 2141b9c2be8STeresa Johnson // to locate the summary (based on GUID from name and linkage). Therefore, 2151b9c2be8STeresa Johnson // use DoPromote result saved above. 2161b9c2be8STeresa Johnson GV.setName(getName(&GV, DoPromote)); 2171b9c2be8STeresa Johnson GV.setLinkage(getLinkage(&GV, DoPromote)); 218488a800aSTeresa Johnson if (!GV.hasLocalLinkage()) 219488a800aSTeresa Johnson GV.setVisibility(GlobalValue::HiddenVisibility); 220488a800aSTeresa Johnson } else 2211b9c2be8STeresa Johnson GV.setLinkage(getLinkage(&GV, /* DoPromote */ false)); 222488a800aSTeresa Johnson 223488a800aSTeresa Johnson // Remove functions imported as available externally defs from comdats, 224488a800aSTeresa Johnson // as this is a declaration for the linker, and will be dropped eventually. 225488a800aSTeresa Johnson // It is illegal for comdats to contain declarations. 226488a800aSTeresa Johnson auto *GO = dyn_cast_or_null<GlobalObject>(&GV); 227488a800aSTeresa Johnson if (GO && GO->isDeclarationForLinker() && GO->hasComdat()) { 228488a800aSTeresa Johnson // The IRMover should not have placed any imported declarations in 229488a800aSTeresa Johnson // a comdat, so the only declaration that should be in a comdat 230488a800aSTeresa Johnson // at this point would be a definition imported as available_externally. 231488a800aSTeresa Johnson assert(GO->hasAvailableExternallyLinkage() && 232488a800aSTeresa Johnson "Expected comdat on definition (possibly available external)"); 233488a800aSTeresa Johnson GO->setComdat(nullptr); 234488a800aSTeresa Johnson } 235488a800aSTeresa Johnson } 236488a800aSTeresa Johnson 237488a800aSTeresa Johnson void FunctionImportGlobalProcessing::processGlobalsForThinLTO() { 238488a800aSTeresa Johnson for (GlobalVariable &GV : M.globals()) 239488a800aSTeresa Johnson processGlobalForThinLTO(GV); 240488a800aSTeresa Johnson for (Function &SF : M) 241488a800aSTeresa Johnson processGlobalForThinLTO(SF); 242488a800aSTeresa Johnson for (GlobalAlias &GA : M.aliases()) 243488a800aSTeresa Johnson processGlobalForThinLTO(GA); 244488a800aSTeresa Johnson } 245488a800aSTeresa Johnson 246488a800aSTeresa Johnson bool FunctionImportGlobalProcessing::run() { 247488a800aSTeresa Johnson processGlobalsForThinLTO(); 248488a800aSTeresa Johnson return false; 249488a800aSTeresa Johnson } 250488a800aSTeresa Johnson 2518d05185aSMehdi Amini bool llvm::renameModuleForThinLTO( 2528d05185aSMehdi Amini Module &M, const ModuleSummaryIndex &Index, 2538d05185aSMehdi Amini DenseSet<const GlobalValue *> *GlobalsToImport) { 2548d05185aSMehdi Amini FunctionImportGlobalProcessing ThinLTOProcessing(M, Index, GlobalsToImport); 255488a800aSTeresa Johnson return ThinLTOProcessing.run(); 256488a800aSTeresa Johnson } 257