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