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