1 //===- lib/Transforms/Utils/FunctionImportUtils.cpp - Importing utilities -===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 // 10 // This file implements the FunctionImportGlobalProcessing class, used 11 // to perform the necessary global value handling for function importing. 12 // 13 //===----------------------------------------------------------------------===// 14 15 #include "llvm/Transforms/Utils/FunctionImportUtils.h" 16 using namespace llvm; 17 18 /// Checks if we should import SGV as a definition, otherwise import as a 19 /// declaration. 20 bool FunctionImportGlobalProcessing::doImportAsDefinition( 21 const GlobalValue *SGV, DenseSet<const GlobalValue *> *GlobalsToImport) { 22 23 // For alias, we tie the definition to the base object. Extract it and recurse 24 if (auto *GA = dyn_cast<GlobalAlias>(SGV)) { 25 if (GA->hasWeakAnyLinkage()) 26 return false; 27 const GlobalObject *GO = GA->getBaseObject(); 28 if (!GO->hasLinkOnceODRLinkage()) 29 return false; 30 return FunctionImportGlobalProcessing::doImportAsDefinition( 31 GO, GlobalsToImport); 32 } 33 // Only import the globals requested for importing. 34 if (GlobalsToImport->count(SGV)) 35 return true; 36 // Otherwise no. 37 return false; 38 } 39 40 bool FunctionImportGlobalProcessing::doImportAsDefinition( 41 const GlobalValue *SGV) { 42 if (!isPerformingImport()) 43 return false; 44 return FunctionImportGlobalProcessing::doImportAsDefinition(SGV, 45 GlobalsToImport); 46 } 47 48 bool FunctionImportGlobalProcessing::doPromoteLocalToGlobal( 49 const GlobalValue *SGV) { 50 assert(SGV->hasLocalLinkage()); 51 // Both the imported references and the original local variable must 52 // be promoted. 53 if (!isPerformingImport() && !isModuleExporting()) 54 return false; 55 56 // Local const variables never need to be promoted unless they are address 57 // taken. The imported uses can simply use the clone created in this module. 58 // For now we are conservative in determining which variables are not 59 // address taken by checking the unnamed addr flag. To be more aggressive, 60 // the address taken information must be checked earlier during parsing 61 // of the module and recorded in the summary index for use when importing 62 // from that module. 63 auto *GVar = dyn_cast<GlobalVariable>(SGV); 64 if (GVar && GVar->isConstant() && GVar->hasUnnamedAddr()) 65 return false; 66 67 // Eventually we only need to promote functions in the exporting module that 68 // are referenced by a potentially exported function (i.e. one that is in the 69 // summary index). 70 return true; 71 } 72 73 std::string FunctionImportGlobalProcessing::getName(const GlobalValue *SGV) { 74 // For locals that must be promoted to global scope, ensure that 75 // the promoted name uniquely identifies the copy in the original module, 76 // using the ID assigned during combined index creation. When importing, 77 // we rename all locals (not just those that are promoted) in order to 78 // avoid naming conflicts between locals imported from different modules. 79 if (SGV->hasLocalLinkage() && 80 (doPromoteLocalToGlobal(SGV) || isPerformingImport())) 81 return ModuleSummaryIndex::getGlobalNameForLocal( 82 SGV->getName(), 83 ImportIndex.getModuleHash(SGV->getParent()->getModuleIdentifier())); 84 return SGV->getName(); 85 } 86 87 GlobalValue::LinkageTypes 88 FunctionImportGlobalProcessing::getLinkage(const GlobalValue *SGV) { 89 // Any local variable that is referenced by an exported function needs 90 // to be promoted to global scope. Since we don't currently know which 91 // functions reference which local variables/functions, we must treat 92 // all as potentially exported if this module is exporting anything. 93 if (isModuleExporting()) { 94 if (SGV->hasLocalLinkage() && doPromoteLocalToGlobal(SGV)) 95 return GlobalValue::ExternalLinkage; 96 return SGV->getLinkage(); 97 } 98 99 // Otherwise, if we aren't importing, no linkage change is needed. 100 if (!isPerformingImport()) 101 return SGV->getLinkage(); 102 103 switch (SGV->getLinkage()) { 104 case GlobalValue::ExternalLinkage: 105 // External defnitions are converted to available_externally 106 // definitions upon import, so that they are available for inlining 107 // and/or optimization, but are turned into declarations later 108 // during the EliminateAvailableExternally pass. 109 if (doImportAsDefinition(SGV) && !dyn_cast<GlobalAlias>(SGV)) 110 return GlobalValue::AvailableExternallyLinkage; 111 // An imported external declaration stays external. 112 return SGV->getLinkage(); 113 114 case GlobalValue::AvailableExternallyLinkage: 115 // An imported available_externally definition converts 116 // to external if imported as a declaration. 117 if (!doImportAsDefinition(SGV)) 118 return GlobalValue::ExternalLinkage; 119 // An imported available_externally declaration stays that way. 120 return SGV->getLinkage(); 121 122 case GlobalValue::LinkOnceAnyLinkage: 123 case GlobalValue::LinkOnceODRLinkage: 124 // These both stay the same when importing the definition. 125 // The ThinLTO pass will eventually force-import their definitions. 126 return SGV->getLinkage(); 127 128 case GlobalValue::WeakAnyLinkage: 129 // Can't import weak_any definitions correctly, or we might change the 130 // program semantics, since the linker will pick the first weak_any 131 // definition and importing would change the order they are seen by the 132 // linker. The module linking caller needs to enforce this. 133 assert(!doImportAsDefinition(SGV)); 134 // If imported as a declaration, it becomes external_weak. 135 return GlobalValue::ExternalWeakLinkage; 136 137 case GlobalValue::WeakODRLinkage: 138 // For weak_odr linkage, there is a guarantee that all copies will be 139 // equivalent, so the issue described above for weak_any does not exist, 140 // and the definition can be imported. It can be treated similarly 141 // to an imported externally visible global value. 142 if (doImportAsDefinition(SGV) && !dyn_cast<GlobalAlias>(SGV)) 143 return GlobalValue::AvailableExternallyLinkage; 144 else 145 return GlobalValue::ExternalLinkage; 146 147 case GlobalValue::AppendingLinkage: 148 // It would be incorrect to import an appending linkage variable, 149 // since it would cause global constructors/destructors to be 150 // executed multiple times. This should have already been handled 151 // by linkIfNeeded, and we will assert in shouldLinkFromSource 152 // if we try to import, so we simply return AppendingLinkage. 153 return GlobalValue::AppendingLinkage; 154 155 case GlobalValue::InternalLinkage: 156 case GlobalValue::PrivateLinkage: 157 // If we are promoting the local to global scope, it is handled 158 // similarly to a normal externally visible global. 159 if (doPromoteLocalToGlobal(SGV)) { 160 if (doImportAsDefinition(SGV) && !dyn_cast<GlobalAlias>(SGV)) 161 return GlobalValue::AvailableExternallyLinkage; 162 else 163 return GlobalValue::ExternalLinkage; 164 } 165 // A non-promoted imported local definition stays local. 166 // The ThinLTO pass will eventually force-import their definitions. 167 return SGV->getLinkage(); 168 169 case GlobalValue::ExternalWeakLinkage: 170 // External weak doesn't apply to definitions, must be a declaration. 171 assert(!doImportAsDefinition(SGV)); 172 // Linkage stays external_weak. 173 return SGV->getLinkage(); 174 175 case GlobalValue::CommonLinkage: 176 // Linkage stays common on definitions. 177 // The ThinLTO pass will eventually force-import their definitions. 178 return SGV->getLinkage(); 179 } 180 181 llvm_unreachable("unknown linkage type"); 182 } 183 184 void FunctionImportGlobalProcessing::processGlobalForThinLTO(GlobalValue &GV) { 185 if (GV.hasLocalLinkage() && 186 (doPromoteLocalToGlobal(&GV) || isPerformingImport())) { 187 GV.setName(getName(&GV)); 188 GV.setLinkage(getLinkage(&GV)); 189 if (!GV.hasLocalLinkage()) 190 GV.setVisibility(GlobalValue::HiddenVisibility); 191 } else 192 GV.setLinkage(getLinkage(&GV)); 193 194 // Remove functions imported as available externally defs from comdats, 195 // as this is a declaration for the linker, and will be dropped eventually. 196 // It is illegal for comdats to contain declarations. 197 auto *GO = dyn_cast_or_null<GlobalObject>(&GV); 198 if (GO && GO->isDeclarationForLinker() && GO->hasComdat()) { 199 // The IRMover should not have placed any imported declarations in 200 // a comdat, so the only declaration that should be in a comdat 201 // at this point would be a definition imported as available_externally. 202 assert(GO->hasAvailableExternallyLinkage() && 203 "Expected comdat on definition (possibly available external)"); 204 GO->setComdat(nullptr); 205 } 206 } 207 208 void FunctionImportGlobalProcessing::processGlobalsForThinLTO() { 209 for (GlobalVariable &GV : M.globals()) 210 processGlobalForThinLTO(GV); 211 for (Function &SF : M) 212 processGlobalForThinLTO(SF); 213 for (GlobalAlias &GA : M.aliases()) 214 processGlobalForThinLTO(GA); 215 } 216 217 bool FunctionImportGlobalProcessing::run() { 218 processGlobalsForThinLTO(); 219 return false; 220 } 221 222 bool llvm::renameModuleForThinLTO( 223 Module &M, const ModuleSummaryIndex &Index, 224 DenseSet<const GlobalValue *> *GlobalsToImport) { 225 FunctionImportGlobalProcessing ThinLTOProcessing(M, Index, GlobalsToImport); 226 return ThinLTOProcessing.run(); 227 } 228