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/Analysis/ModuleSummaryAnalysis.h" 16 #include "llvm/Transforms/Utils/FunctionImportUtils.h" 17 #include "llvm/IR/InstIterator.h" 18 #include "llvm/IR/Instructions.h" 19 using namespace llvm; 20 21 /// Checks if we should import SGV as a definition, otherwise import as a 22 /// declaration. 23 bool FunctionImportGlobalProcessing::doImportAsDefinition( 24 const GlobalValue *SGV, DenseSet<const GlobalValue *> *GlobalsToImport) { 25 26 // For alias, we tie the definition to the base object. Extract it and recurse 27 if (auto *GA = dyn_cast<GlobalAlias>(SGV)) { 28 if (GA->hasWeakAnyLinkage()) 29 return false; 30 const GlobalObject *GO = GA->getBaseObject(); 31 if (!GO->hasLinkOnceODRLinkage()) 32 return false; 33 return FunctionImportGlobalProcessing::doImportAsDefinition( 34 GO, GlobalsToImport); 35 } 36 // Only import the globals requested for importing. 37 if (GlobalsToImport->count(SGV)) 38 return true; 39 // Otherwise no. 40 return false; 41 } 42 43 bool FunctionImportGlobalProcessing::doImportAsDefinition( 44 const GlobalValue *SGV) { 45 if (!isPerformingImport()) 46 return false; 47 return FunctionImportGlobalProcessing::doImportAsDefinition(SGV, 48 GlobalsToImport); 49 } 50 51 bool FunctionImportGlobalProcessing::shouldPromoteLocalToGlobal( 52 const GlobalValue *SGV) { 53 assert(SGV->hasLocalLinkage()); 54 // Both the imported references and the original local variable must 55 // be promoted. 56 if (!isPerformingImport() && !isModuleExporting()) 57 return false; 58 59 if (isPerformingImport()) { 60 assert((!GlobalsToImport->count(SGV) || !isNonRenamableLocal(*SGV)) && 61 "Attempting to promote non-renamable local"); 62 // We don't know for sure yet if we are importing this value (as either 63 // a reference or a def), since we are simply walking all values in the 64 // module. But by necessity if we end up importing it and it is local, 65 // it must be promoted, so unconditionally promote all values in the 66 // importing module. 67 return true; 68 } 69 70 // When exporting, consult the index. 71 auto Summaries = ImportIndex.findGlobalValueSummaryList(SGV->getGUID()); 72 assert(Summaries != ImportIndex.end() && 73 "Missing summary for global value when exporting"); 74 assert(Summaries->second.size() == 1 && "Local has more than one summary"); 75 auto Linkage = Summaries->second.front()->linkage(); 76 if (!GlobalValue::isLocalLinkage(Linkage)) { 77 assert(!isNonRenamableLocal(*SGV) && 78 "Attempting to promote non-renamable local"); 79 return true; 80 } 81 82 return false; 83 } 84 85 #ifndef NDEBUG 86 bool FunctionImportGlobalProcessing::isNonRenamableLocal( 87 const GlobalValue &GV) const { 88 if (!GV.hasLocalLinkage()) 89 return false; 90 // This needs to stay in sync with the logic in buildModuleSummaryIndex. 91 if (GV.hasSection()) 92 return true; 93 if (Used.count(const_cast<GlobalValue *>(&GV))) 94 return true; 95 return false; 96 } 97 #endif 98 99 std::string FunctionImportGlobalProcessing::getName(const GlobalValue *SGV, 100 bool DoPromote) { 101 // For locals that must be promoted to global scope, ensure that 102 // the promoted name uniquely identifies the copy in the original module, 103 // using the ID assigned during combined index creation. When importing, 104 // we rename all locals (not just those that are promoted) in order to 105 // avoid naming conflicts between locals imported from different modules. 106 if (SGV->hasLocalLinkage() && (DoPromote || isPerformingImport())) 107 return ModuleSummaryIndex::getGlobalNameForLocal( 108 SGV->getName(), 109 ImportIndex.getModuleHash(SGV->getParent()->getModuleIdentifier())); 110 return SGV->getName(); 111 } 112 113 GlobalValue::LinkageTypes 114 FunctionImportGlobalProcessing::getLinkage(const GlobalValue *SGV, 115 bool DoPromote) { 116 // Any local variable that is referenced by an exported function needs 117 // to be promoted to global scope. Since we don't currently know which 118 // functions reference which local variables/functions, we must treat 119 // all as potentially exported if this module is exporting anything. 120 if (isModuleExporting()) { 121 if (SGV->hasLocalLinkage() && DoPromote) 122 return GlobalValue::ExternalLinkage; 123 return SGV->getLinkage(); 124 } 125 126 // Otherwise, if we aren't importing, no linkage change is needed. 127 if (!isPerformingImport()) 128 return SGV->getLinkage(); 129 130 switch (SGV->getLinkage()) { 131 case GlobalValue::ExternalLinkage: 132 // External defnitions are converted to available_externally 133 // definitions upon import, so that they are available for inlining 134 // and/or optimization, but are turned into declarations later 135 // during the EliminateAvailableExternally pass. 136 if (doImportAsDefinition(SGV) && !dyn_cast<GlobalAlias>(SGV)) 137 return GlobalValue::AvailableExternallyLinkage; 138 // An imported external declaration stays external. 139 return SGV->getLinkage(); 140 141 case GlobalValue::AvailableExternallyLinkage: 142 // An imported available_externally definition converts 143 // to external if imported as a declaration. 144 if (!doImportAsDefinition(SGV)) 145 return GlobalValue::ExternalLinkage; 146 // An imported available_externally declaration stays that way. 147 return SGV->getLinkage(); 148 149 case GlobalValue::LinkOnceAnyLinkage: 150 case GlobalValue::LinkOnceODRLinkage: 151 // These both stay the same when importing the definition. 152 // The ThinLTO pass will eventually force-import their definitions. 153 return SGV->getLinkage(); 154 155 case GlobalValue::WeakAnyLinkage: 156 // Can't import weak_any definitions correctly, or we might change the 157 // program semantics, since the linker will pick the first weak_any 158 // definition and importing would change the order they are seen by the 159 // linker. The module linking caller needs to enforce this. 160 assert(!doImportAsDefinition(SGV)); 161 // If imported as a declaration, it becomes external_weak. 162 return SGV->getLinkage(); 163 164 case GlobalValue::WeakODRLinkage: 165 // For weak_odr linkage, there is a guarantee that all copies will be 166 // equivalent, so the issue described above for weak_any does not exist, 167 // and the definition can be imported. It can be treated similarly 168 // to an imported externally visible global value. 169 if (doImportAsDefinition(SGV) && !dyn_cast<GlobalAlias>(SGV)) 170 return GlobalValue::AvailableExternallyLinkage; 171 else 172 return GlobalValue::ExternalLinkage; 173 174 case GlobalValue::AppendingLinkage: 175 // It would be incorrect to import an appending linkage variable, 176 // since it would cause global constructors/destructors to be 177 // executed multiple times. This should have already been handled 178 // by linkIfNeeded, and we will assert in shouldLinkFromSource 179 // if we try to import, so we simply return AppendingLinkage. 180 return GlobalValue::AppendingLinkage; 181 182 case GlobalValue::InternalLinkage: 183 case GlobalValue::PrivateLinkage: 184 // If we are promoting the local to global scope, it is handled 185 // similarly to a normal externally visible global. 186 if (DoPromote) { 187 if (doImportAsDefinition(SGV) && !dyn_cast<GlobalAlias>(SGV)) 188 return GlobalValue::AvailableExternallyLinkage; 189 else 190 return GlobalValue::ExternalLinkage; 191 } 192 // A non-promoted imported local definition stays local. 193 // The ThinLTO pass will eventually force-import their definitions. 194 return SGV->getLinkage(); 195 196 case GlobalValue::ExternalWeakLinkage: 197 // External weak doesn't apply to definitions, must be a declaration. 198 assert(!doImportAsDefinition(SGV)); 199 // Linkage stays external_weak. 200 return SGV->getLinkage(); 201 202 case GlobalValue::CommonLinkage: 203 // Linkage stays common on definitions. 204 // The ThinLTO pass will eventually force-import their definitions. 205 return SGV->getLinkage(); 206 } 207 208 llvm_unreachable("unknown linkage type"); 209 } 210 211 void FunctionImportGlobalProcessing::processGlobalForThinLTO(GlobalValue &GV) { 212 bool DoPromote = false; 213 if (GV.hasLocalLinkage() && 214 ((DoPromote = shouldPromoteLocalToGlobal(&GV)) || isPerformingImport())) { 215 // Once we change the name or linkage it is difficult to determine 216 // again whether we should promote since shouldPromoteLocalToGlobal needs 217 // to locate the summary (based on GUID from name and linkage). Therefore, 218 // use DoPromote result saved above. 219 GV.setName(getName(&GV, DoPromote)); 220 GV.setLinkage(getLinkage(&GV, DoPromote)); 221 if (!GV.hasLocalLinkage()) 222 GV.setVisibility(GlobalValue::HiddenVisibility); 223 } else 224 GV.setLinkage(getLinkage(&GV, /* DoPromote */ false)); 225 226 // Remove functions imported as available externally defs from comdats, 227 // as this is a declaration for the linker, and will be dropped eventually. 228 // It is illegal for comdats to contain declarations. 229 auto *GO = dyn_cast_or_null<GlobalObject>(&GV); 230 if (GO && GO->isDeclarationForLinker() && GO->hasComdat()) { 231 // The IRMover should not have placed any imported declarations in 232 // a comdat, so the only declaration that should be in a comdat 233 // at this point would be a definition imported as available_externally. 234 assert(GO->hasAvailableExternallyLinkage() && 235 "Expected comdat on definition (possibly available external)"); 236 GO->setComdat(nullptr); 237 } 238 } 239 240 void FunctionImportGlobalProcessing::processGlobalsForThinLTO() { 241 for (GlobalVariable &GV : M.globals()) 242 processGlobalForThinLTO(GV); 243 for (Function &SF : M) 244 processGlobalForThinLTO(SF); 245 for (GlobalAlias &GA : M.aliases()) 246 processGlobalForThinLTO(GA); 247 } 248 249 bool FunctionImportGlobalProcessing::run() { 250 processGlobalsForThinLTO(); 251 return false; 252 } 253 254 bool llvm::renameModuleForThinLTO( 255 Module &M, const ModuleSummaryIndex &Index, 256 DenseSet<const GlobalValue *> *GlobalsToImport) { 257 FunctionImportGlobalProcessing ThinLTOProcessing(M, Index, GlobalsToImport); 258 return ThinLTOProcessing.run(); 259 } 260