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 15488a800aSTeresa Johnson #include "llvm/Transforms/Utils/FunctionImportUtils.h" 16488a800aSTeresa Johnson using namespace llvm; 17488a800aSTeresa Johnson 18488a800aSTeresa Johnson /// Checks if we should import SGV as a definition, otherwise import as a 19488a800aSTeresa Johnson /// declaration. 20488a800aSTeresa Johnson bool FunctionImportGlobalProcessing::doImportAsDefinition( 218d05185aSMehdi Amini const GlobalValue *SGV, DenseSet<const GlobalValue *> *GlobalsToImport) { 228d05185aSMehdi Amini 238d05185aSMehdi Amini // For alias, we tie the definition to the base object. Extract it and recurse 248d05185aSMehdi Amini if (auto *GA = dyn_cast<GlobalAlias>(SGV)) { 25488a800aSTeresa Johnson if (GA->hasWeakAnyLinkage()) 26488a800aSTeresa Johnson return false; 27488a800aSTeresa Johnson const GlobalObject *GO = GA->getBaseObject(); 28488a800aSTeresa Johnson if (!GO->hasLinkOnceODRLinkage()) 29488a800aSTeresa Johnson return false; 30488a800aSTeresa Johnson return FunctionImportGlobalProcessing::doImportAsDefinition( 318d05185aSMehdi Amini GO, GlobalsToImport); 32488a800aSTeresa Johnson } 338d05185aSMehdi Amini // Only import the globals requested for importing. 348d05185aSMehdi Amini if (GlobalsToImport->count(SGV)) 35488a800aSTeresa Johnson return true; 36488a800aSTeresa Johnson // Otherwise no. 37488a800aSTeresa Johnson return false; 38488a800aSTeresa Johnson } 39488a800aSTeresa Johnson 40488a800aSTeresa Johnson bool FunctionImportGlobalProcessing::doImportAsDefinition( 41488a800aSTeresa Johnson const GlobalValue *SGV) { 42488a800aSTeresa Johnson if (!isPerformingImport()) 43488a800aSTeresa Johnson return false; 448d05185aSMehdi Amini return FunctionImportGlobalProcessing::doImportAsDefinition(SGV, 458d05185aSMehdi Amini GlobalsToImport); 46488a800aSTeresa Johnson } 47488a800aSTeresa Johnson 48488a800aSTeresa Johnson bool FunctionImportGlobalProcessing::doPromoteLocalToGlobal( 49488a800aSTeresa Johnson const GlobalValue *SGV) { 50488a800aSTeresa Johnson assert(SGV->hasLocalLinkage()); 51488a800aSTeresa Johnson // Both the imported references and the original local variable must 52488a800aSTeresa Johnson // be promoted. 53488a800aSTeresa Johnson if (!isPerformingImport() && !isModuleExporting()) 54488a800aSTeresa Johnson return false; 55488a800aSTeresa Johnson 56488a800aSTeresa Johnson // Local const variables never need to be promoted unless they are address 57488a800aSTeresa Johnson // taken. The imported uses can simply use the clone created in this module. 58488a800aSTeresa Johnson // For now we are conservative in determining which variables are not 59488a800aSTeresa Johnson // address taken by checking the unnamed addr flag. To be more aggressive, 60488a800aSTeresa Johnson // the address taken information must be checked earlier during parsing 6126ab5772STeresa Johnson // of the module and recorded in the summary index for use when importing 62488a800aSTeresa Johnson // from that module. 63488a800aSTeresa Johnson auto *GVar = dyn_cast<GlobalVariable>(SGV); 64488a800aSTeresa Johnson if (GVar && GVar->isConstant() && GVar->hasUnnamedAddr()) 65488a800aSTeresa Johnson return false; 66488a800aSTeresa Johnson 67*b4e1e829SMehdi Amini if (GVar && GVar->hasSection()) 68*b4e1e829SMehdi Amini // Some sections like "__DATA,__cfstring" are "magic" and promotion is not 69*b4e1e829SMehdi Amini // allowed. Just disable promotion on any GVar with sections right now. 70*b4e1e829SMehdi Amini return false; 71*b4e1e829SMehdi Amini 72488a800aSTeresa Johnson // Eventually we only need to promote functions in the exporting module that 73488a800aSTeresa Johnson // are referenced by a potentially exported function (i.e. one that is in the 7426ab5772STeresa Johnson // summary index). 75488a800aSTeresa Johnson return true; 76488a800aSTeresa Johnson } 77488a800aSTeresa Johnson 78488a800aSTeresa Johnson std::string FunctionImportGlobalProcessing::getName(const GlobalValue *SGV) { 79488a800aSTeresa Johnson // For locals that must be promoted to global scope, ensure that 80488a800aSTeresa Johnson // the promoted name uniquely identifies the copy in the original module, 81488a800aSTeresa Johnson // using the ID assigned during combined index creation. When importing, 82488a800aSTeresa Johnson // we rename all locals (not just those that are promoted) in order to 83488a800aSTeresa Johnson // avoid naming conflicts between locals imported from different modules. 84488a800aSTeresa Johnson if (SGV->hasLocalLinkage() && 85488a800aSTeresa Johnson (doPromoteLocalToGlobal(SGV) || isPerformingImport())) 8626ab5772STeresa Johnson return ModuleSummaryIndex::getGlobalNameForLocal( 87488a800aSTeresa Johnson SGV->getName(), 88ae280e54SMehdi Amini ImportIndex.getModuleHash(SGV->getParent()->getModuleIdentifier())); 89488a800aSTeresa Johnson return SGV->getName(); 90488a800aSTeresa Johnson } 91488a800aSTeresa Johnson 92488a800aSTeresa Johnson GlobalValue::LinkageTypes 93488a800aSTeresa Johnson FunctionImportGlobalProcessing::getLinkage(const GlobalValue *SGV) { 94488a800aSTeresa Johnson // Any local variable that is referenced by an exported function needs 95488a800aSTeresa Johnson // to be promoted to global scope. Since we don't currently know which 96488a800aSTeresa Johnson // functions reference which local variables/functions, we must treat 97488a800aSTeresa Johnson // all as potentially exported if this module is exporting anything. 98488a800aSTeresa Johnson if (isModuleExporting()) { 99488a800aSTeresa Johnson if (SGV->hasLocalLinkage() && doPromoteLocalToGlobal(SGV)) 100488a800aSTeresa Johnson return GlobalValue::ExternalLinkage; 101488a800aSTeresa Johnson return SGV->getLinkage(); 102488a800aSTeresa Johnson } 103488a800aSTeresa Johnson 104488a800aSTeresa Johnson // Otherwise, if we aren't importing, no linkage change is needed. 105488a800aSTeresa Johnson if (!isPerformingImport()) 106488a800aSTeresa Johnson return SGV->getLinkage(); 107488a800aSTeresa Johnson 108488a800aSTeresa Johnson switch (SGV->getLinkage()) { 109488a800aSTeresa Johnson case GlobalValue::ExternalLinkage: 110488a800aSTeresa Johnson // External defnitions are converted to available_externally 111488a800aSTeresa Johnson // definitions upon import, so that they are available for inlining 112488a800aSTeresa Johnson // and/or optimization, but are turned into declarations later 113488a800aSTeresa Johnson // during the EliminateAvailableExternally pass. 114488a800aSTeresa Johnson if (doImportAsDefinition(SGV) && !dyn_cast<GlobalAlias>(SGV)) 115488a800aSTeresa Johnson return GlobalValue::AvailableExternallyLinkage; 116488a800aSTeresa Johnson // An imported external declaration stays external. 117488a800aSTeresa Johnson return SGV->getLinkage(); 118488a800aSTeresa Johnson 119488a800aSTeresa Johnson case GlobalValue::AvailableExternallyLinkage: 120488a800aSTeresa Johnson // An imported available_externally definition converts 121488a800aSTeresa Johnson // to external if imported as a declaration. 122488a800aSTeresa Johnson if (!doImportAsDefinition(SGV)) 123488a800aSTeresa Johnson return GlobalValue::ExternalLinkage; 124488a800aSTeresa Johnson // An imported available_externally declaration stays that way. 125488a800aSTeresa Johnson return SGV->getLinkage(); 126488a800aSTeresa Johnson 127488a800aSTeresa Johnson case GlobalValue::LinkOnceAnyLinkage: 128488a800aSTeresa Johnson case GlobalValue::LinkOnceODRLinkage: 129488a800aSTeresa Johnson // These both stay the same when importing the definition. 130488a800aSTeresa Johnson // The ThinLTO pass will eventually force-import their definitions. 131488a800aSTeresa Johnson return SGV->getLinkage(); 132488a800aSTeresa Johnson 133488a800aSTeresa Johnson case GlobalValue::WeakAnyLinkage: 134488a800aSTeresa Johnson // Can't import weak_any definitions correctly, or we might change the 135488a800aSTeresa Johnson // program semantics, since the linker will pick the first weak_any 136488a800aSTeresa Johnson // definition and importing would change the order they are seen by the 137488a800aSTeresa Johnson // linker. The module linking caller needs to enforce this. 138488a800aSTeresa Johnson assert(!doImportAsDefinition(SGV)); 139488a800aSTeresa Johnson // If imported as a declaration, it becomes external_weak. 140bb3a1d92SMehdi Amini return SGV->getLinkage(); 141488a800aSTeresa Johnson 142488a800aSTeresa Johnson case GlobalValue::WeakODRLinkage: 143488a800aSTeresa Johnson // For weak_odr linkage, there is a guarantee that all copies will be 144488a800aSTeresa Johnson // equivalent, so the issue described above for weak_any does not exist, 145488a800aSTeresa Johnson // and the definition can be imported. It can be treated similarly 146488a800aSTeresa Johnson // to an imported externally visible global value. 147488a800aSTeresa Johnson if (doImportAsDefinition(SGV) && !dyn_cast<GlobalAlias>(SGV)) 148488a800aSTeresa Johnson return GlobalValue::AvailableExternallyLinkage; 149488a800aSTeresa Johnson else 150488a800aSTeresa Johnson return GlobalValue::ExternalLinkage; 151488a800aSTeresa Johnson 152488a800aSTeresa Johnson case GlobalValue::AppendingLinkage: 153488a800aSTeresa Johnson // It would be incorrect to import an appending linkage variable, 154488a800aSTeresa Johnson // since it would cause global constructors/destructors to be 155488a800aSTeresa Johnson // executed multiple times. This should have already been handled 156488a800aSTeresa Johnson // by linkIfNeeded, and we will assert in shouldLinkFromSource 157488a800aSTeresa Johnson // if we try to import, so we simply return AppendingLinkage. 158488a800aSTeresa Johnson return GlobalValue::AppendingLinkage; 159488a800aSTeresa Johnson 160488a800aSTeresa Johnson case GlobalValue::InternalLinkage: 161488a800aSTeresa Johnson case GlobalValue::PrivateLinkage: 162488a800aSTeresa Johnson // If we are promoting the local to global scope, it is handled 163488a800aSTeresa Johnson // similarly to a normal externally visible global. 164488a800aSTeresa Johnson if (doPromoteLocalToGlobal(SGV)) { 165488a800aSTeresa Johnson if (doImportAsDefinition(SGV) && !dyn_cast<GlobalAlias>(SGV)) 166488a800aSTeresa Johnson return GlobalValue::AvailableExternallyLinkage; 167488a800aSTeresa Johnson else 168488a800aSTeresa Johnson return GlobalValue::ExternalLinkage; 169488a800aSTeresa Johnson } 170488a800aSTeresa Johnson // A non-promoted imported local definition stays local. 171488a800aSTeresa Johnson // The ThinLTO pass will eventually force-import their definitions. 172488a800aSTeresa Johnson return SGV->getLinkage(); 173488a800aSTeresa Johnson 174488a800aSTeresa Johnson case GlobalValue::ExternalWeakLinkage: 175488a800aSTeresa Johnson // External weak doesn't apply to definitions, must be a declaration. 176488a800aSTeresa Johnson assert(!doImportAsDefinition(SGV)); 177488a800aSTeresa Johnson // Linkage stays external_weak. 178488a800aSTeresa Johnson return SGV->getLinkage(); 179488a800aSTeresa Johnson 180488a800aSTeresa Johnson case GlobalValue::CommonLinkage: 181488a800aSTeresa Johnson // Linkage stays common on definitions. 182488a800aSTeresa Johnson // The ThinLTO pass will eventually force-import their definitions. 183488a800aSTeresa Johnson return SGV->getLinkage(); 184488a800aSTeresa Johnson } 185488a800aSTeresa Johnson 186488a800aSTeresa Johnson llvm_unreachable("unknown linkage type"); 187488a800aSTeresa Johnson } 188488a800aSTeresa Johnson 189488a800aSTeresa Johnson void FunctionImportGlobalProcessing::processGlobalForThinLTO(GlobalValue &GV) { 190488a800aSTeresa Johnson if (GV.hasLocalLinkage() && 191488a800aSTeresa Johnson (doPromoteLocalToGlobal(&GV) || isPerformingImport())) { 192488a800aSTeresa Johnson GV.setName(getName(&GV)); 193488a800aSTeresa Johnson GV.setLinkage(getLinkage(&GV)); 194488a800aSTeresa Johnson if (!GV.hasLocalLinkage()) 195488a800aSTeresa Johnson GV.setVisibility(GlobalValue::HiddenVisibility); 196488a800aSTeresa Johnson } else 197488a800aSTeresa Johnson GV.setLinkage(getLinkage(&GV)); 198488a800aSTeresa Johnson 199488a800aSTeresa Johnson // Remove functions imported as available externally defs from comdats, 200488a800aSTeresa Johnson // as this is a declaration for the linker, and will be dropped eventually. 201488a800aSTeresa Johnson // It is illegal for comdats to contain declarations. 202488a800aSTeresa Johnson auto *GO = dyn_cast_or_null<GlobalObject>(&GV); 203488a800aSTeresa Johnson if (GO && GO->isDeclarationForLinker() && GO->hasComdat()) { 204488a800aSTeresa Johnson // The IRMover should not have placed any imported declarations in 205488a800aSTeresa Johnson // a comdat, so the only declaration that should be in a comdat 206488a800aSTeresa Johnson // at this point would be a definition imported as available_externally. 207488a800aSTeresa Johnson assert(GO->hasAvailableExternallyLinkage() && 208488a800aSTeresa Johnson "Expected comdat on definition (possibly available external)"); 209488a800aSTeresa Johnson GO->setComdat(nullptr); 210488a800aSTeresa Johnson } 211488a800aSTeresa Johnson } 212488a800aSTeresa Johnson 213488a800aSTeresa Johnson void FunctionImportGlobalProcessing::processGlobalsForThinLTO() { 214b35cc691STeresa Johnson // We cannot currently promote or rename anything that is in llvm.used, 215b35cc691STeresa Johnson // since any such value may have a use that won't see the new name. 216b35cc691STeresa Johnson // Specifically, any uses within inline assembly are not visible to the 217b35cc691STeresa Johnson // compiler. Prevent changing any such values on the exporting side, 218b35cc691STeresa Johnson // since we would already have guarded against an import from this module by 219b35cc691STeresa Johnson // suppressing its index generation. See comments on what is required 220b35cc691STeresa Johnson // in order to implement a finer grained solution in 221b35cc691STeresa Johnson // ModuleSummaryIndexBuilder::ModuleSummaryIndexBuilder(). 222b35cc691STeresa Johnson SmallPtrSet<GlobalValue *, 8> Used; 223b35cc691STeresa Johnson collectUsedGlobalVariables(M, Used, /*CompilerUsed*/ false); 224b35cc691STeresa Johnson for (GlobalValue *V : Used) { 225b35cc691STeresa Johnson if (!V->hasLocalLinkage()) 226b35cc691STeresa Johnson continue; 227b35cc691STeresa Johnson // We would have blocked importing from this module by suppressing index 228b35cc691STeresa Johnson // generation. 229b35cc691STeresa Johnson assert(!isPerformingImport() && 230b35cc691STeresa Johnson "Should have blocked importing from module with local used"); 231b35cc691STeresa Johnson return; 232b35cc691STeresa Johnson } 233b35cc691STeresa Johnson 234488a800aSTeresa Johnson for (GlobalVariable &GV : M.globals()) 235488a800aSTeresa Johnson processGlobalForThinLTO(GV); 236488a800aSTeresa Johnson for (Function &SF : M) 237488a800aSTeresa Johnson processGlobalForThinLTO(SF); 238488a800aSTeresa Johnson for (GlobalAlias &GA : M.aliases()) 239488a800aSTeresa Johnson processGlobalForThinLTO(GA); 240488a800aSTeresa Johnson } 241488a800aSTeresa Johnson 242488a800aSTeresa Johnson bool FunctionImportGlobalProcessing::run() { 243488a800aSTeresa Johnson processGlobalsForThinLTO(); 244488a800aSTeresa Johnson return false; 245488a800aSTeresa Johnson } 246488a800aSTeresa Johnson 2478d05185aSMehdi Amini bool llvm::renameModuleForThinLTO( 2488d05185aSMehdi Amini Module &M, const ModuleSummaryIndex &Index, 2498d05185aSMehdi Amini DenseSet<const GlobalValue *> *GlobalsToImport) { 2508d05185aSMehdi Amini FunctionImportGlobalProcessing ThinLTOProcessing(M, Index, GlobalsToImport); 251488a800aSTeresa Johnson return ThinLTOProcessing.run(); 252488a800aSTeresa Johnson } 253