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