1 //===- lib/Transforms/Utils/FunctionImportUtils.cpp - Importing utilities -===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 // 9 // This file implements the FunctionImportGlobalProcessing class, used 10 // to perform the necessary global value handling for function importing. 11 // 12 //===----------------------------------------------------------------------===// 13 14 #include "llvm/Transforms/Utils/FunctionImportUtils.h" 15 using namespace llvm; 16 17 /// Checks if we should import SGV as a definition, otherwise import as a 18 /// declaration. 19 bool FunctionImportGlobalProcessing::doImportAsDefinition( 20 const GlobalValue *SGV) { 21 if (!isPerformingImport()) 22 return false; 23 24 // Only import the globals requested for importing. 25 if (!GlobalsToImport->count(const_cast<GlobalValue *>(SGV))) 26 return false; 27 28 assert(!isa<GlobalAlias>(SGV) && 29 "Unexpected global alias in the import list."); 30 31 // Otherwise yes. 32 return true; 33 } 34 35 bool FunctionImportGlobalProcessing::shouldPromoteLocalToGlobal( 36 const GlobalValue *SGV, ValueInfo VI) { 37 assert(SGV->hasLocalLinkage()); 38 // Both the imported references and the original local variable must 39 // be promoted. 40 if (!isPerformingImport() && !isModuleExporting()) 41 return false; 42 43 if (isPerformingImport()) { 44 assert((!GlobalsToImport->count(const_cast<GlobalValue *>(SGV)) || 45 !isNonRenamableLocal(*SGV)) && 46 "Attempting to promote non-renamable local"); 47 // We don't know for sure yet if we are importing this value (as either 48 // a reference or a def), since we are simply walking all values in the 49 // module. But by necessity if we end up importing it and it is local, 50 // it must be promoted, so unconditionally promote all values in the 51 // importing module. 52 return true; 53 } 54 55 // When exporting, consult the index. We can have more than one local 56 // with the same GUID, in the case of same-named locals in different but 57 // same-named source files that were compiled in their respective directories 58 // (so the source file name and resulting GUID is the same). Find the one 59 // in this module. 60 auto Summary = ImportIndex.findSummaryInModule( 61 VI, SGV->getParent()->getModuleIdentifier()); 62 assert(Summary && "Missing summary for global value when exporting"); 63 auto Linkage = Summary->linkage(); 64 if (!GlobalValue::isLocalLinkage(Linkage)) { 65 assert(!isNonRenamableLocal(*SGV) && 66 "Attempting to promote non-renamable local"); 67 return true; 68 } 69 70 return false; 71 } 72 73 #ifndef NDEBUG 74 bool FunctionImportGlobalProcessing::isNonRenamableLocal( 75 const GlobalValue &GV) const { 76 if (!GV.hasLocalLinkage()) 77 return false; 78 // This needs to stay in sync with the logic in buildModuleSummaryIndex. 79 if (GV.hasSection()) 80 return true; 81 if (Used.count(const_cast<GlobalValue *>(&GV))) 82 return true; 83 return false; 84 } 85 #endif 86 87 std::string 88 FunctionImportGlobalProcessing::getPromotedName(const GlobalValue *SGV) { 89 assert(SGV->hasLocalLinkage()); 90 // For locals that must be promoted to global scope, ensure that 91 // the promoted name uniquely identifies the copy in the original module, 92 // using the ID assigned during combined index creation. 93 return ModuleSummaryIndex::getGlobalNameForLocal( 94 SGV->getName(), 95 ImportIndex.getModuleHash(SGV->getParent()->getModuleIdentifier())); 96 } 97 98 GlobalValue::LinkageTypes 99 FunctionImportGlobalProcessing::getLinkage(const GlobalValue *SGV, 100 bool DoPromote) { 101 // Any local variable that is referenced by an exported function needs 102 // to be promoted to global scope. Since we don't currently know which 103 // functions reference which local variables/functions, we must treat 104 // all as potentially exported if this module is exporting anything. 105 if (isModuleExporting()) { 106 if (SGV->hasLocalLinkage() && DoPromote) 107 return GlobalValue::ExternalLinkage; 108 return SGV->getLinkage(); 109 } 110 111 // Otherwise, if we aren't importing, no linkage change is needed. 112 if (!isPerformingImport()) 113 return SGV->getLinkage(); 114 115 switch (SGV->getLinkage()) { 116 case GlobalValue::LinkOnceODRLinkage: 117 case GlobalValue::ExternalLinkage: 118 // External and linkonce definitions are converted to available_externally 119 // definitions upon import, so that they are available for inlining 120 // and/or optimization, but are turned into declarations later 121 // during the EliminateAvailableExternally pass. 122 if (doImportAsDefinition(SGV) && !isa<GlobalAlias>(SGV)) 123 return GlobalValue::AvailableExternallyLinkage; 124 // An imported external declaration stays external. 125 return SGV->getLinkage(); 126 127 case GlobalValue::AvailableExternallyLinkage: 128 // An imported available_externally definition converts 129 // to external if imported as a declaration. 130 if (!doImportAsDefinition(SGV)) 131 return GlobalValue::ExternalLinkage; 132 // An imported available_externally declaration stays that way. 133 return SGV->getLinkage(); 134 135 case GlobalValue::LinkOnceAnyLinkage: 136 case GlobalValue::WeakAnyLinkage: 137 // Can't import linkonce_any/weak_any definitions correctly, or we might 138 // change the program semantics, since the linker will pick the first 139 // linkonce_any/weak_any definition and importing would change the order 140 // they are seen by the linker. The module linking caller needs to enforce 141 // this. 142 assert(!doImportAsDefinition(SGV)); 143 // If imported as a declaration, it becomes external_weak. 144 return SGV->getLinkage(); 145 146 case GlobalValue::WeakODRLinkage: 147 // For weak_odr linkage, there is a guarantee that all copies will be 148 // equivalent, so the issue described above for weak_any does not exist, 149 // and the definition can be imported. It can be treated similarly 150 // to an imported externally visible global value. 151 if (doImportAsDefinition(SGV) && !isa<GlobalAlias>(SGV)) 152 return GlobalValue::AvailableExternallyLinkage; 153 else 154 return GlobalValue::ExternalLinkage; 155 156 case GlobalValue::AppendingLinkage: 157 // It would be incorrect to import an appending linkage variable, 158 // since it would cause global constructors/destructors to be 159 // executed multiple times. This should have already been handled 160 // by linkIfNeeded, and we will assert in shouldLinkFromSource 161 // if we try to import, so we simply return AppendingLinkage. 162 return GlobalValue::AppendingLinkage; 163 164 case GlobalValue::InternalLinkage: 165 case GlobalValue::PrivateLinkage: 166 // If we are promoting the local to global scope, it is handled 167 // similarly to a normal externally visible global. 168 if (DoPromote) { 169 if (doImportAsDefinition(SGV) && !isa<GlobalAlias>(SGV)) 170 return GlobalValue::AvailableExternallyLinkage; 171 else 172 return GlobalValue::ExternalLinkage; 173 } 174 // A non-promoted imported local definition stays local. 175 // The ThinLTO pass will eventually force-import their definitions. 176 return SGV->getLinkage(); 177 178 case GlobalValue::ExternalWeakLinkage: 179 // External weak doesn't apply to definitions, must be a declaration. 180 assert(!doImportAsDefinition(SGV)); 181 // Linkage stays external_weak. 182 return SGV->getLinkage(); 183 184 case GlobalValue::CommonLinkage: 185 // Linkage stays common on definitions. 186 // The ThinLTO pass will eventually force-import their definitions. 187 return SGV->getLinkage(); 188 } 189 190 llvm_unreachable("unknown linkage type"); 191 } 192 193 void FunctionImportGlobalProcessing::processGlobalForThinLTO(GlobalValue &GV) { 194 195 ValueInfo VI; 196 if (GV.hasName()) { 197 VI = ImportIndex.getValueInfo(GV.getGUID()); 198 // Set synthetic function entry counts. 199 if (VI && ImportIndex.hasSyntheticEntryCounts()) { 200 if (Function *F = dyn_cast<Function>(&GV)) { 201 if (!F->isDeclaration()) { 202 for (auto &S : VI.getSummaryList()) { 203 auto *FS = cast<FunctionSummary>(S->getBaseObject()); 204 if (FS->modulePath() == M.getModuleIdentifier()) { 205 F->setEntryCount(Function::ProfileCount(FS->entryCount(), 206 Function::PCT_Synthetic)); 207 break; 208 } 209 } 210 } 211 } 212 } 213 } 214 215 // We should always have a ValueInfo (i.e. GV in index) for definitions when 216 // we are exporting, and also when importing that value. 217 assert(VI || GV.isDeclaration() || 218 (isPerformingImport() && !doImportAsDefinition(&GV))); 219 220 // Mark read/write-only variables which can be imported with specific 221 // attribute. We can't internalize them now because IRMover will fail 222 // to link variable definitions to their external declarations during 223 // ThinLTO import. We'll internalize read-only variables later, after 224 // import is finished. See internalizeGVsAfterImport. 225 // 226 // If global value dead stripping is not enabled in summary then 227 // propagateConstants hasn't been run. We can't internalize GV 228 // in such case. 229 if (!GV.isDeclaration() && VI && ImportIndex.withAttributePropagation()) { 230 if (GlobalVariable *V = dyn_cast<GlobalVariable>(&GV)) { 231 // We can have more than one local with the same GUID, in the case of 232 // same-named locals in different but same-named source files that were 233 // compiled in their respective directories (so the source file name 234 // and resulting GUID is the same). Find the one in this module. 235 // Handle the case where there is no summary found in this module. That 236 // can happen in the distributed ThinLTO backend, because the index only 237 // contains summaries from the source modules if they are being imported. 238 // We might have a non-null VI and get here even in that case if the name 239 // matches one in this module (e.g. weak or appending linkage). 240 auto *GVS = dyn_cast_or_null<GlobalVarSummary>( 241 ImportIndex.findSummaryInModule(VI, M.getModuleIdentifier())); 242 if (GVS && 243 (ImportIndex.isReadOnly(GVS) || ImportIndex.isWriteOnly(GVS))) { 244 V->addAttribute("thinlto-internalize"); 245 // Objects referenced by writeonly GV initializer should not be 246 // promoted, because there is no any kind of read access to them 247 // on behalf of this writeonly GV. To avoid promotion we convert 248 // GV initializer to 'zeroinitializer'. This effectively drops 249 // references in IR module (not in combined index), so we can 250 // ignore them when computing import. We do not export references 251 // of writeonly object. See computeImportForReferencedGlobals 252 if (ImportIndex.isWriteOnly(GVS)) 253 V->setInitializer(Constant::getNullValue(V->getValueType())); 254 } 255 } 256 } 257 258 if (GV.hasLocalLinkage() && shouldPromoteLocalToGlobal(&GV, VI)) { 259 // Save the original name string before we rename GV below. 260 auto Name = GV.getName().str(); 261 GV.setName(getPromotedName(&GV)); 262 GV.setLinkage(getLinkage(&GV, /* DoPromote */ true)); 263 assert(!GV.hasLocalLinkage()); 264 GV.setVisibility(GlobalValue::HiddenVisibility); 265 266 // If we are renaming a COMDAT leader, ensure that we record the COMDAT 267 // for later renaming as well. This is required for COFF. 268 if (const auto *C = GV.getComdat()) 269 if (C->getName() == Name) 270 RenamedComdats.try_emplace(C, M.getOrInsertComdat(GV.getName())); 271 } else 272 GV.setLinkage(getLinkage(&GV, /* DoPromote */ false)); 273 274 // When ClearDSOLocalOnDeclarations is true, clear dso_local if GV is 275 // converted to a declaration, to disable direct access. Don't do this if GV 276 // is implicitly dso_local due to a non-default visibility. 277 if (ClearDSOLocalOnDeclarations && 278 (GV.isDeclarationForLinker() || 279 (isPerformingImport() && !doImportAsDefinition(&GV))) && 280 !GV.isImplicitDSOLocal()) { 281 GV.setDSOLocal(false); 282 } else if (VI && VI.isDSOLocal(ImportIndex.withDSOLocalPropagation())) { 283 // If all summaries are dso_local, symbol gets resolved to a known local 284 // definition. 285 GV.setDSOLocal(true); 286 if (GV.hasDLLImportStorageClass()) 287 GV.setDLLStorageClass(GlobalValue::DefaultStorageClass); 288 } 289 290 // Remove functions imported as available externally defs from comdats, 291 // as this is a declaration for the linker, and will be dropped eventually. 292 // It is illegal for comdats to contain declarations. 293 auto *GO = dyn_cast<GlobalObject>(&GV); 294 if (GO && GO->isDeclarationForLinker() && GO->hasComdat()) { 295 // The IRMover should not have placed any imported declarations in 296 // a comdat, so the only declaration that should be in a comdat 297 // at this point would be a definition imported as available_externally. 298 assert(GO->hasAvailableExternallyLinkage() && 299 "Expected comdat on definition (possibly available external)"); 300 GO->setComdat(nullptr); 301 } 302 } 303 304 void FunctionImportGlobalProcessing::processGlobalsForThinLTO() { 305 for (GlobalVariable &GV : M.globals()) 306 processGlobalForThinLTO(GV); 307 for (Function &SF : M) 308 processGlobalForThinLTO(SF); 309 for (GlobalAlias &GA : M.aliases()) 310 processGlobalForThinLTO(GA); 311 312 // Replace any COMDATS that required renaming (because the COMDAT leader was 313 // promoted and renamed). 314 if (!RenamedComdats.empty()) 315 for (auto &GO : M.global_objects()) 316 if (auto *C = GO.getComdat()) { 317 auto Replacement = RenamedComdats.find(C); 318 if (Replacement != RenamedComdats.end()) 319 GO.setComdat(Replacement->second); 320 } 321 } 322 323 bool FunctionImportGlobalProcessing::run() { 324 processGlobalsForThinLTO(); 325 return false; 326 } 327 328 bool llvm::renameModuleForThinLTO(Module &M, const ModuleSummaryIndex &Index, 329 bool ClearDSOLocalOnDeclarations, 330 SetVector<GlobalValue *> *GlobalsToImport) { 331 FunctionImportGlobalProcessing ThinLTOProcessing(M, Index, GlobalsToImport, 332 ClearDSOLocalOnDeclarations); 333 return ThinLTOProcessing.run(); 334 } 335