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