Teresa Johnson | 488a800 | 2016-02-10 18:11:31 +0000 | [diff] [blame] | 1 | //===- lib/Transforms/Utils/FunctionImportUtils.cpp - Importing utilities -===// |
| 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 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 |
Teresa Johnson | 488a800 | 2016-02-10 18:11:31 +0000 | [diff] [blame] | 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" |
Teresa Johnson | df5ef87 | 2016-04-27 14:19:38 +0000 | [diff] [blame] | 15 | #include "llvm/IR/InstIterator.h" |
Teresa Johnson | 488a800 | 2016-02-10 18:11:31 +0000 | [diff] [blame] | 16 | using namespace llvm; |
| 17 | |
| 18 | /// Checks if we should import SGV as a definition, otherwise import as a |
| 19 | /// declaration. |
| 20 | bool FunctionImportGlobalProcessing::doImportAsDefinition( |
Peter Collingbourne | 6d8f817 | 2017-02-03 16:56:27 +0000 | [diff] [blame] | 21 | const GlobalValue *SGV, SetVector<GlobalValue *> *GlobalsToImport) { |
Mehdi Amini | 8d05185 | 2016-03-19 00:40:31 +0000 | [diff] [blame] | 22 | |
Mehdi Amini | 8d05185 | 2016-03-19 00:40:31 +0000 | [diff] [blame] | 23 | // Only import the globals requested for importing. |
David Blaikie | 72c0b1c | 2017-07-27 15:28:10 +0000 | [diff] [blame] | 24 | if (!GlobalsToImport->count(const_cast<GlobalValue *>(SGV))) |
| 25 | return false; |
David Blaikie | 2f0cc47 | 2017-07-27 15:09:06 +0000 | [diff] [blame] | 26 | |
| 27 | assert(!isa<GlobalAlias>(SGV) && |
| 28 | "Unexpected global alias in the import list."); |
| 29 | |
David Blaikie | 72c0b1c | 2017-07-27 15:28:10 +0000 | [diff] [blame] | 30 | // Otherwise yes. |
| 31 | return true; |
Teresa Johnson | 488a800 | 2016-02-10 18:11:31 +0000 | [diff] [blame] | 32 | } |
| 33 | |
| 34 | bool FunctionImportGlobalProcessing::doImportAsDefinition( |
| 35 | const GlobalValue *SGV) { |
| 36 | if (!isPerformingImport()) |
| 37 | return false; |
Mehdi Amini | 8d05185 | 2016-03-19 00:40:31 +0000 | [diff] [blame] | 38 | return FunctionImportGlobalProcessing::doImportAsDefinition(SGV, |
| 39 | GlobalsToImport); |
Teresa Johnson | 488a800 | 2016-02-10 18:11:31 +0000 | [diff] [blame] | 40 | } |
| 41 | |
Teresa Johnson | 38d4df7 | 2016-10-29 21:52:23 +0000 | [diff] [blame] | 42 | bool FunctionImportGlobalProcessing::shouldPromoteLocalToGlobal( |
Teresa Johnson | 488a800 | 2016-02-10 18:11:31 +0000 | [diff] [blame] | 43 | const GlobalValue *SGV) { |
| 44 | assert(SGV->hasLocalLinkage()); |
| 45 | // Both the imported references and the original local variable must |
| 46 | // be promoted. |
| 47 | if (!isPerformingImport() && !isModuleExporting()) |
| 48 | return false; |
| 49 | |
Teresa Johnson | 4fef68c | 2016-11-14 19:21:41 +0000 | [diff] [blame] | 50 | if (isPerformingImport()) { |
Peter Collingbourne | 6d8f817 | 2017-02-03 16:56:27 +0000 | [diff] [blame] | 51 | assert((!GlobalsToImport->count(const_cast<GlobalValue *>(SGV)) || |
| 52 | !isNonRenamableLocal(*SGV)) && |
Teresa Johnson | 2b60384 | 2017-01-05 15:10:10 +0000 | [diff] [blame] | 53 | "Attempting to promote non-renamable local"); |
Teresa Johnson | 4fef68c | 2016-11-14 19:21:41 +0000 | [diff] [blame] | 54 | // We don't know for sure yet if we are importing this value (as either |
| 55 | // a reference or a def), since we are simply walking all values in the |
| 56 | // module. But by necessity if we end up importing it and it is local, |
| 57 | // it must be promoted, so unconditionally promote all values in the |
| 58 | // importing module. |
| 59 | return true; |
Teresa Johnson | 0515fb8 | 2016-11-03 01:07:16 +0000 | [diff] [blame] | 60 | } |
Mehdi Amini | b4e1e82 | 2016-04-27 00:32:13 +0000 | [diff] [blame] | 61 | |
Teresa Johnson | 9006d52 | 2017-01-06 23:38:41 +0000 | [diff] [blame] | 62 | // When exporting, consult the index. We can have more than one local |
| 63 | // with the same GUID, in the case of same-named locals in different but |
| 64 | // same-named source files that were compiled in their respective directories |
| 65 | // (so the source file name and resulting GUID is the same). Find the one |
| 66 | // in this module. |
| 67 | auto Summary = ImportIndex.findSummaryInModule( |
| 68 | SGV->getGUID(), SGV->getParent()->getModuleIdentifier()); |
| 69 | assert(Summary && "Missing summary for global value when exporting"); |
| 70 | auto Linkage = Summary->linkage(); |
Teresa Johnson | 4fef68c | 2016-11-14 19:21:41 +0000 | [diff] [blame] | 71 | if (!GlobalValue::isLocalLinkage(Linkage)) { |
Teresa Johnson | 519465b | 2017-01-05 14:32:16 +0000 | [diff] [blame] | 72 | assert(!isNonRenamableLocal(*SGV) && |
| 73 | "Attempting to promote non-renamable local"); |
Teresa Johnson | 4fef68c | 2016-11-14 19:21:41 +0000 | [diff] [blame] | 74 | return true; |
| 75 | } |
| 76 | |
| 77 | return false; |
Teresa Johnson | 488a800 | 2016-02-10 18:11:31 +0000 | [diff] [blame] | 78 | } |
| 79 | |
Teresa Johnson | 519465b | 2017-01-05 14:32:16 +0000 | [diff] [blame] | 80 | #ifndef NDEBUG |
| 81 | bool FunctionImportGlobalProcessing::isNonRenamableLocal( |
| 82 | const GlobalValue &GV) const { |
| 83 | if (!GV.hasLocalLinkage()) |
| 84 | return false; |
| 85 | // This needs to stay in sync with the logic in buildModuleSummaryIndex. |
| 86 | if (GV.hasSection()) |
| 87 | return true; |
| 88 | if (Used.count(const_cast<GlobalValue *>(&GV))) |
| 89 | return true; |
| 90 | return false; |
| 91 | } |
| 92 | #endif |
| 93 | |
Teresa Johnson | 1b9c2be | 2016-10-29 21:31:48 +0000 | [diff] [blame] | 94 | std::string FunctionImportGlobalProcessing::getName(const GlobalValue *SGV, |
| 95 | bool DoPromote) { |
Teresa Johnson | 488a800 | 2016-02-10 18:11:31 +0000 | [diff] [blame] | 96 | // For locals that must be promoted to global scope, ensure that |
| 97 | // the promoted name uniquely identifies the copy in the original module, |
| 98 | // using the ID assigned during combined index creation. When importing, |
| 99 | // we rename all locals (not just those that are promoted) in order to |
| 100 | // avoid naming conflicts between locals imported from different modules. |
Teresa Johnson | 1b9c2be | 2016-10-29 21:31:48 +0000 | [diff] [blame] | 101 | if (SGV->hasLocalLinkage() && (DoPromote || isPerformingImport())) |
Teresa Johnson | 26ab577 | 2016-03-15 00:04:37 +0000 | [diff] [blame] | 102 | return ModuleSummaryIndex::getGlobalNameForLocal( |
Teresa Johnson | 488a800 | 2016-02-10 18:11:31 +0000 | [diff] [blame] | 103 | SGV->getName(), |
Mehdi Amini | ae280e5 | 2016-04-11 23:26:46 +0000 | [diff] [blame] | 104 | ImportIndex.getModuleHash(SGV->getParent()->getModuleIdentifier())); |
Teresa Johnson | 488a800 | 2016-02-10 18:11:31 +0000 | [diff] [blame] | 105 | return SGV->getName(); |
| 106 | } |
| 107 | |
| 108 | GlobalValue::LinkageTypes |
Teresa Johnson | 1b9c2be | 2016-10-29 21:31:48 +0000 | [diff] [blame] | 109 | FunctionImportGlobalProcessing::getLinkage(const GlobalValue *SGV, |
| 110 | bool DoPromote) { |
Teresa Johnson | 488a800 | 2016-02-10 18:11:31 +0000 | [diff] [blame] | 111 | // Any local variable that is referenced by an exported function needs |
| 112 | // to be promoted to global scope. Since we don't currently know which |
| 113 | // functions reference which local variables/functions, we must treat |
| 114 | // all as potentially exported if this module is exporting anything. |
| 115 | if (isModuleExporting()) { |
Teresa Johnson | 1b9c2be | 2016-10-29 21:31:48 +0000 | [diff] [blame] | 116 | if (SGV->hasLocalLinkage() && DoPromote) |
Teresa Johnson | 488a800 | 2016-02-10 18:11:31 +0000 | [diff] [blame] | 117 | return GlobalValue::ExternalLinkage; |
| 118 | return SGV->getLinkage(); |
| 119 | } |
| 120 | |
| 121 | // Otherwise, if we aren't importing, no linkage change is needed. |
| 122 | if (!isPerformingImport()) |
| 123 | return SGV->getLinkage(); |
| 124 | |
| 125 | switch (SGV->getLinkage()) { |
David Blaikie | 2f0cc47 | 2017-07-27 15:09:06 +0000 | [diff] [blame] | 126 | case GlobalValue::LinkOnceODRLinkage: |
Teresa Johnson | 488a800 | 2016-02-10 18:11:31 +0000 | [diff] [blame] | 127 | case GlobalValue::ExternalLinkage: |
David Blaikie | 2f0cc47 | 2017-07-27 15:09:06 +0000 | [diff] [blame] | 128 | // External and linkonce definitions are converted to available_externally |
Teresa Johnson | 488a800 | 2016-02-10 18:11:31 +0000 | [diff] [blame] | 129 | // definitions upon import, so that they are available for inlining |
| 130 | // and/or optimization, but are turned into declarations later |
| 131 | // during the EliminateAvailableExternally pass. |
| 132 | if (doImportAsDefinition(SGV) && !dyn_cast<GlobalAlias>(SGV)) |
| 133 | return GlobalValue::AvailableExternallyLinkage; |
| 134 | // An imported external declaration stays external. |
| 135 | return SGV->getLinkage(); |
| 136 | |
| 137 | case GlobalValue::AvailableExternallyLinkage: |
| 138 | // An imported available_externally definition converts |
| 139 | // to external if imported as a declaration. |
| 140 | if (!doImportAsDefinition(SGV)) |
| 141 | return GlobalValue::ExternalLinkage; |
| 142 | // An imported available_externally declaration stays that way. |
| 143 | return SGV->getLinkage(); |
| 144 | |
Xin Tong | 53e52e4 | 2018-11-28 15:16:35 +0000 | [diff] [blame] | 145 | case GlobalValue::LinkOnceAnyLinkage: |
Teresa Johnson | 488a800 | 2016-02-10 18:11:31 +0000 | [diff] [blame] | 146 | case GlobalValue::WeakAnyLinkage: |
Xin Tong | 53e52e4 | 2018-11-28 15:16:35 +0000 | [diff] [blame] | 147 | // Can't import linkonce_any/weak_any definitions correctly, or we might |
| 148 | // change the program semantics, since the linker will pick the first |
| 149 | // linkonce_any/weak_any definition and importing would change the order |
| 150 | // they are seen by the linker. The module linking caller needs to enforce |
| 151 | // this. |
Teresa Johnson | 488a800 | 2016-02-10 18:11:31 +0000 | [diff] [blame] | 152 | assert(!doImportAsDefinition(SGV)); |
| 153 | // If imported as a declaration, it becomes external_weak. |
Mehdi Amini | bb3a1d9 | 2016-04-20 04:18:11 +0000 | [diff] [blame] | 154 | return SGV->getLinkage(); |
Teresa Johnson | 488a800 | 2016-02-10 18:11:31 +0000 | [diff] [blame] | 155 | |
| 156 | case GlobalValue::WeakODRLinkage: |
| 157 | // For weak_odr linkage, there is a guarantee that all copies will be |
| 158 | // equivalent, so the issue described above for weak_any does not exist, |
| 159 | // and the definition can be imported. It can be treated similarly |
| 160 | // to an imported externally visible global value. |
| 161 | if (doImportAsDefinition(SGV) && !dyn_cast<GlobalAlias>(SGV)) |
| 162 | return GlobalValue::AvailableExternallyLinkage; |
| 163 | else |
| 164 | return GlobalValue::ExternalLinkage; |
| 165 | |
| 166 | case GlobalValue::AppendingLinkage: |
| 167 | // It would be incorrect to import an appending linkage variable, |
| 168 | // since it would cause global constructors/destructors to be |
| 169 | // executed multiple times. This should have already been handled |
| 170 | // by linkIfNeeded, and we will assert in shouldLinkFromSource |
| 171 | // if we try to import, so we simply return AppendingLinkage. |
| 172 | return GlobalValue::AppendingLinkage; |
| 173 | |
| 174 | case GlobalValue::InternalLinkage: |
| 175 | case GlobalValue::PrivateLinkage: |
| 176 | // If we are promoting the local to global scope, it is handled |
| 177 | // similarly to a normal externally visible global. |
Teresa Johnson | 1b9c2be | 2016-10-29 21:31:48 +0000 | [diff] [blame] | 178 | if (DoPromote) { |
Teresa Johnson | 488a800 | 2016-02-10 18:11:31 +0000 | [diff] [blame] | 179 | if (doImportAsDefinition(SGV) && !dyn_cast<GlobalAlias>(SGV)) |
| 180 | return GlobalValue::AvailableExternallyLinkage; |
| 181 | else |
| 182 | return GlobalValue::ExternalLinkage; |
| 183 | } |
| 184 | // A non-promoted imported local definition stays local. |
| 185 | // The ThinLTO pass will eventually force-import their definitions. |
| 186 | return SGV->getLinkage(); |
| 187 | |
| 188 | case GlobalValue::ExternalWeakLinkage: |
| 189 | // External weak doesn't apply to definitions, must be a declaration. |
| 190 | assert(!doImportAsDefinition(SGV)); |
| 191 | // Linkage stays external_weak. |
| 192 | return SGV->getLinkage(); |
| 193 | |
| 194 | case GlobalValue::CommonLinkage: |
| 195 | // Linkage stays common on definitions. |
| 196 | // The ThinLTO pass will eventually force-import their definitions. |
| 197 | return SGV->getLinkage(); |
| 198 | } |
| 199 | |
| 200 | llvm_unreachable("unknown linkage type"); |
| 201 | } |
| 202 | |
| 203 | void FunctionImportGlobalProcessing::processGlobalForThinLTO(GlobalValue &GV) { |
Sean Fertile | 4595a91 | 2017-11-04 17:04:39 +0000 | [diff] [blame] | 204 | |
Eugene Leviant | bf46e74 | 2018-11-16 07:08:00 +0000 | [diff] [blame] | 205 | ValueInfo VI; |
Sean Fertile | 4595a91 | 2017-11-04 17:04:39 +0000 | [diff] [blame] | 206 | if (GV.hasName()) { |
Eugene Leviant | bf46e74 | 2018-11-16 07:08:00 +0000 | [diff] [blame] | 207 | VI = ImportIndex.getValueInfo(GV.getGUID()); |
Easwaran Raman | 5a7056f | 2018-12-13 19:54:27 +0000 | [diff] [blame] | 208 | // Set synthetic function entry counts. |
| 209 | if (VI && ImportIndex.hasSyntheticEntryCounts()) { |
| 210 | if (Function *F = dyn_cast<Function>(&GV)) { |
| 211 | if (!F->isDeclaration()) { |
| 212 | for (auto &S : VI.getSummaryList()) { |
| 213 | FunctionSummary *FS = dyn_cast<FunctionSummary>(S->getBaseObject()); |
| 214 | if (FS->modulePath() == M.getModuleIdentifier()) { |
| 215 | F->setEntryCount(Function::ProfileCount(FS->entryCount(), |
| 216 | Function::PCT_Synthetic)); |
| 217 | break; |
| 218 | } |
| 219 | } |
| 220 | } |
| 221 | } |
| 222 | } |
| 223 | // Check the summaries to see if the symbol gets resolved to a known local |
| 224 | // definition. |
Rafael Espindola | f5220fb | 2018-03-13 15:24:51 +0000 | [diff] [blame] | 225 | if (VI && VI.isDSOLocal()) { |
Peter Collingbourne | b4edfb9 | 2018-02-05 17:17:51 +0000 | [diff] [blame] | 226 | GV.setDSOLocal(true); |
Rafael Espindola | f5220fb | 2018-03-13 15:24:51 +0000 | [diff] [blame] | 227 | if (GV.hasDLLImportStorageClass()) |
| 228 | GV.setDLLStorageClass(GlobalValue::DefaultStorageClass); |
| 229 | } |
Sean Fertile | 4595a91 | 2017-11-04 17:04:39 +0000 | [diff] [blame] | 230 | } |
| 231 | |
Eugene Leviant | bf46e74 | 2018-11-16 07:08:00 +0000 | [diff] [blame] | 232 | // Mark read-only variables which can be imported with specific attribute. |
| 233 | // We can't internalize them now because IRMover will fail to link variable |
| 234 | // definitions to their external declarations during ThinLTO import. We'll |
| 235 | // internalize read-only variables later, after import is finished. |
| 236 | // See internalizeImmutableGVs. |
| 237 | // |
| 238 | // If global value dead stripping is not enabled in summary then |
| 239 | // propagateConstants hasn't been run. We can't internalize GV |
| 240 | // in such case. |
| 241 | if (!GV.isDeclaration() && VI && ImportIndex.withGlobalValueDeadStripping()) { |
| 242 | const auto &SL = VI.getSummaryList(); |
| 243 | auto *GVS = SL.empty() ? nullptr : dyn_cast<GlobalVarSummary>(SL[0].get()); |
| 244 | if (GVS && GVS->isReadOnly()) |
| 245 | cast<GlobalVariable>(&GV)->addAttribute("thinlto-internalize"); |
| 246 | } |
| 247 | |
Teresa Johnson | 1b9c2be | 2016-10-29 21:31:48 +0000 | [diff] [blame] | 248 | bool DoPromote = false; |
Teresa Johnson | 488a800 | 2016-02-10 18:11:31 +0000 | [diff] [blame] | 249 | if (GV.hasLocalLinkage() && |
Teresa Johnson | 38d4df7 | 2016-10-29 21:52:23 +0000 | [diff] [blame] | 250 | ((DoPromote = shouldPromoteLocalToGlobal(&GV)) || isPerformingImport())) { |
Teresa Johnson | f59242e | 2019-01-31 17:18:11 +0000 | [diff] [blame] | 251 | // Save the original name string before we rename GV below. |
| 252 | auto Name = GV.getName().str(); |
Teresa Johnson | 1b9c2be | 2016-10-29 21:31:48 +0000 | [diff] [blame] | 253 | // Once we change the name or linkage it is difficult to determine |
Teresa Johnson | 38d4df7 | 2016-10-29 21:52:23 +0000 | [diff] [blame] | 254 | // again whether we should promote since shouldPromoteLocalToGlobal needs |
Teresa Johnson | 1b9c2be | 2016-10-29 21:31:48 +0000 | [diff] [blame] | 255 | // to locate the summary (based on GUID from name and linkage). Therefore, |
| 256 | // use DoPromote result saved above. |
| 257 | GV.setName(getName(&GV, DoPromote)); |
| 258 | GV.setLinkage(getLinkage(&GV, DoPromote)); |
Teresa Johnson | 488a800 | 2016-02-10 18:11:31 +0000 | [diff] [blame] | 259 | if (!GV.hasLocalLinkage()) |
| 260 | GV.setVisibility(GlobalValue::HiddenVisibility); |
Teresa Johnson | f59242e | 2019-01-31 17:18:11 +0000 | [diff] [blame] | 261 | |
| 262 | // If we are renaming a COMDAT leader, ensure that we record the COMDAT |
| 263 | // for later renaming as well. This is required for COFF. |
| 264 | if (const auto *C = GV.getComdat()) |
| 265 | if (C->getName() == Name) |
| 266 | RenamedComdats.try_emplace(C, M.getOrInsertComdat(GV.getName())); |
Teresa Johnson | 488a800 | 2016-02-10 18:11:31 +0000 | [diff] [blame] | 267 | } else |
Teresa Johnson | 1b9c2be | 2016-10-29 21:31:48 +0000 | [diff] [blame] | 268 | GV.setLinkage(getLinkage(&GV, /* DoPromote */ false)); |
Teresa Johnson | 488a800 | 2016-02-10 18:11:31 +0000 | [diff] [blame] | 269 | |
| 270 | // Remove functions imported as available externally defs from comdats, |
| 271 | // as this is a declaration for the linker, and will be dropped eventually. |
| 272 | // It is illegal for comdats to contain declarations. |
Eugene Leviant | bf46e74 | 2018-11-16 07:08:00 +0000 | [diff] [blame] | 273 | auto *GO = dyn_cast<GlobalObject>(&GV); |
Teresa Johnson | 488a800 | 2016-02-10 18:11:31 +0000 | [diff] [blame] | 274 | if (GO && GO->isDeclarationForLinker() && GO->hasComdat()) { |
| 275 | // The IRMover should not have placed any imported declarations in |
| 276 | // a comdat, so the only declaration that should be in a comdat |
| 277 | // at this point would be a definition imported as available_externally. |
| 278 | assert(GO->hasAvailableExternallyLinkage() && |
| 279 | "Expected comdat on definition (possibly available external)"); |
| 280 | GO->setComdat(nullptr); |
| 281 | } |
| 282 | } |
| 283 | |
| 284 | void FunctionImportGlobalProcessing::processGlobalsForThinLTO() { |
| 285 | for (GlobalVariable &GV : M.globals()) |
| 286 | processGlobalForThinLTO(GV); |
| 287 | for (Function &SF : M) |
| 288 | processGlobalForThinLTO(SF); |
| 289 | for (GlobalAlias &GA : M.aliases()) |
| 290 | processGlobalForThinLTO(GA); |
Teresa Johnson | f59242e | 2019-01-31 17:18:11 +0000 | [diff] [blame] | 291 | |
| 292 | // Replace any COMDATS that required renaming (because the COMDAT leader was |
| 293 | // promoted and renamed). |
| 294 | if (!RenamedComdats.empty()) |
| 295 | for (auto &GO : M.global_objects()) |
| 296 | if (auto *C = GO.getComdat()) { |
| 297 | auto Replacement = RenamedComdats.find(C); |
| 298 | if (Replacement != RenamedComdats.end()) |
| 299 | GO.setComdat(Replacement->second); |
| 300 | } |
Teresa Johnson | 488a800 | 2016-02-10 18:11:31 +0000 | [diff] [blame] | 301 | } |
| 302 | |
| 303 | bool FunctionImportGlobalProcessing::run() { |
| 304 | processGlobalsForThinLTO(); |
| 305 | return false; |
| 306 | } |
| 307 | |
Peter Collingbourne | 6d8f817 | 2017-02-03 16:56:27 +0000 | [diff] [blame] | 308 | bool llvm::renameModuleForThinLTO(Module &M, const ModuleSummaryIndex &Index, |
| 309 | SetVector<GlobalValue *> *GlobalsToImport) { |
Mehdi Amini | 8d05185 | 2016-03-19 00:40:31 +0000 | [diff] [blame] | 310 | FunctionImportGlobalProcessing ThinLTOProcessing(M, Index, GlobalsToImport); |
Teresa Johnson | 488a800 | 2016-02-10 18:11:31 +0000 | [diff] [blame] | 311 | return ThinLTOProcessing.run(); |
| 312 | } |