Teresa Johnson | 488a800 | 2016-02-10 18:11:31 +0000 | [diff] [blame] | 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/Transforms/Utils/FunctionImportUtils.h" |
Teresa Johnson | df5ef87 | 2016-04-27 14:19:38 +0000 | [diff] [blame] | 16 | #include "llvm/IR/InstIterator.h" |
Teresa Johnson | 488a800 | 2016-02-10 18:11:31 +0000 | [diff] [blame] | 17 | using namespace llvm; |
| 18 | |
| 19 | /// Checks if we should import SGV as a definition, otherwise import as a |
| 20 | /// declaration. |
| 21 | bool FunctionImportGlobalProcessing::doImportAsDefinition( |
Peter Collingbourne | 6d8f817 | 2017-02-03 16:56:27 +0000 | [diff] [blame] | 22 | const GlobalValue *SGV, SetVector<GlobalValue *> *GlobalsToImport) { |
Mehdi Amini | 8d05185 | 2016-03-19 00:40:31 +0000 | [diff] [blame] | 23 | |
Mehdi Amini | 8d05185 | 2016-03-19 00:40:31 +0000 | [diff] [blame] | 24 | // Only import the globals requested for importing. |
David Blaikie | 72c0b1c | 2017-07-27 15:28:10 +0000 | [diff] [blame] | 25 | if (!GlobalsToImport->count(const_cast<GlobalValue *>(SGV))) |
| 26 | return false; |
David Blaikie | 2f0cc47 | 2017-07-27 15:09:06 +0000 | [diff] [blame] | 27 | |
| 28 | assert(!isa<GlobalAlias>(SGV) && |
| 29 | "Unexpected global alias in the import list."); |
| 30 | |
David Blaikie | 72c0b1c | 2017-07-27 15:28:10 +0000 | [diff] [blame] | 31 | // Otherwise yes. |
| 32 | return true; |
Teresa Johnson | 488a800 | 2016-02-10 18:11:31 +0000 | [diff] [blame] | 33 | } |
| 34 | |
| 35 | bool FunctionImportGlobalProcessing::doImportAsDefinition( |
| 36 | const GlobalValue *SGV) { |
| 37 | if (!isPerformingImport()) |
| 38 | return false; |
Mehdi Amini | 8d05185 | 2016-03-19 00:40:31 +0000 | [diff] [blame] | 39 | return FunctionImportGlobalProcessing::doImportAsDefinition(SGV, |
| 40 | GlobalsToImport); |
Teresa Johnson | 488a800 | 2016-02-10 18:11:31 +0000 | [diff] [blame] | 41 | } |
| 42 | |
Teresa Johnson | 38d4df7 | 2016-10-29 21:52:23 +0000 | [diff] [blame] | 43 | bool FunctionImportGlobalProcessing::shouldPromoteLocalToGlobal( |
Teresa Johnson | 488a800 | 2016-02-10 18:11:31 +0000 | [diff] [blame] | 44 | const GlobalValue *SGV) { |
| 45 | assert(SGV->hasLocalLinkage()); |
| 46 | // Both the imported references and the original local variable must |
| 47 | // be promoted. |
| 48 | if (!isPerformingImport() && !isModuleExporting()) |
| 49 | return false; |
| 50 | |
Teresa Johnson | 4fef68c | 2016-11-14 19:21:41 +0000 | [diff] [blame] | 51 | if (isPerformingImport()) { |
Peter Collingbourne | 6d8f817 | 2017-02-03 16:56:27 +0000 | [diff] [blame] | 52 | assert((!GlobalsToImport->count(const_cast<GlobalValue *>(SGV)) || |
| 53 | !isNonRenamableLocal(*SGV)) && |
Teresa Johnson | 2b60384 | 2017-01-05 15:10:10 +0000 | [diff] [blame] | 54 | "Attempting to promote non-renamable local"); |
Teresa Johnson | 4fef68c | 2016-11-14 19:21:41 +0000 | [diff] [blame] | 55 | // We don't know for sure yet if we are importing this value (as either |
| 56 | // a reference or a def), since we are simply walking all values in the |
| 57 | // module. But by necessity if we end up importing it and it is local, |
| 58 | // it must be promoted, so unconditionally promote all values in the |
| 59 | // importing module. |
| 60 | return true; |
Teresa Johnson | 0515fb8 | 2016-11-03 01:07:16 +0000 | [diff] [blame] | 61 | } |
Mehdi Amini | b4e1e82 | 2016-04-27 00:32:13 +0000 | [diff] [blame] | 62 | |
Teresa Johnson | 9006d52 | 2017-01-06 23:38:41 +0000 | [diff] [blame] | 63 | // When exporting, consult the index. We can have more than one local |
| 64 | // with the same GUID, in the case of same-named locals in different but |
| 65 | // same-named source files that were compiled in their respective directories |
| 66 | // (so the source file name and resulting GUID is the same). Find the one |
| 67 | // in this module. |
| 68 | auto Summary = ImportIndex.findSummaryInModule( |
| 69 | SGV->getGUID(), SGV->getParent()->getModuleIdentifier()); |
| 70 | assert(Summary && "Missing summary for global value when exporting"); |
| 71 | auto Linkage = Summary->linkage(); |
Teresa Johnson | 4fef68c | 2016-11-14 19:21:41 +0000 | [diff] [blame] | 72 | if (!GlobalValue::isLocalLinkage(Linkage)) { |
Teresa Johnson | 519465b | 2017-01-05 14:32:16 +0000 | [diff] [blame] | 73 | assert(!isNonRenamableLocal(*SGV) && |
| 74 | "Attempting to promote non-renamable local"); |
Teresa Johnson | 4fef68c | 2016-11-14 19:21:41 +0000 | [diff] [blame] | 75 | return true; |
| 76 | } |
| 77 | |
| 78 | return false; |
Teresa Johnson | 488a800 | 2016-02-10 18:11:31 +0000 | [diff] [blame] | 79 | } |
| 80 | |
Teresa Johnson | 519465b | 2017-01-05 14:32:16 +0000 | [diff] [blame] | 81 | #ifndef NDEBUG |
| 82 | bool FunctionImportGlobalProcessing::isNonRenamableLocal( |
| 83 | const GlobalValue &GV) const { |
| 84 | if (!GV.hasLocalLinkage()) |
| 85 | return false; |
| 86 | // This needs to stay in sync with the logic in buildModuleSummaryIndex. |
| 87 | if (GV.hasSection()) |
| 88 | return true; |
| 89 | if (Used.count(const_cast<GlobalValue *>(&GV))) |
| 90 | return true; |
| 91 | return false; |
| 92 | } |
| 93 | #endif |
| 94 | |
Teresa Johnson | 1b9c2be | 2016-10-29 21:31:48 +0000 | [diff] [blame] | 95 | std::string FunctionImportGlobalProcessing::getName(const GlobalValue *SGV, |
| 96 | bool DoPromote) { |
Teresa Johnson | 488a800 | 2016-02-10 18:11:31 +0000 | [diff] [blame] | 97 | // For locals that must be promoted to global scope, ensure that |
| 98 | // the promoted name uniquely identifies the copy in the original module, |
| 99 | // using the ID assigned during combined index creation. When importing, |
| 100 | // we rename all locals (not just those that are promoted) in order to |
| 101 | // avoid naming conflicts between locals imported from different modules. |
Teresa Johnson | 1b9c2be | 2016-10-29 21:31:48 +0000 | [diff] [blame] | 102 | if (SGV->hasLocalLinkage() && (DoPromote || isPerformingImport())) |
Teresa Johnson | 26ab577 | 2016-03-15 00:04:37 +0000 | [diff] [blame] | 103 | return ModuleSummaryIndex::getGlobalNameForLocal( |
Teresa Johnson | 488a800 | 2016-02-10 18:11:31 +0000 | [diff] [blame] | 104 | SGV->getName(), |
Mehdi Amini | ae280e5 | 2016-04-11 23:26:46 +0000 | [diff] [blame] | 105 | ImportIndex.getModuleHash(SGV->getParent()->getModuleIdentifier())); |
Teresa Johnson | 488a800 | 2016-02-10 18:11:31 +0000 | [diff] [blame] | 106 | return SGV->getName(); |
| 107 | } |
| 108 | |
| 109 | GlobalValue::LinkageTypes |
Teresa Johnson | 1b9c2be | 2016-10-29 21:31:48 +0000 | [diff] [blame] | 110 | FunctionImportGlobalProcessing::getLinkage(const GlobalValue *SGV, |
| 111 | bool DoPromote) { |
Teresa Johnson | 488a800 | 2016-02-10 18:11:31 +0000 | [diff] [blame] | 112 | // Any local variable that is referenced by an exported function needs |
| 113 | // to be promoted to global scope. Since we don't currently know which |
| 114 | // functions reference which local variables/functions, we must treat |
| 115 | // all as potentially exported if this module is exporting anything. |
| 116 | if (isModuleExporting()) { |
Teresa Johnson | 1b9c2be | 2016-10-29 21:31:48 +0000 | [diff] [blame] | 117 | if (SGV->hasLocalLinkage() && DoPromote) |
Teresa Johnson | 488a800 | 2016-02-10 18:11:31 +0000 | [diff] [blame] | 118 | return GlobalValue::ExternalLinkage; |
| 119 | return SGV->getLinkage(); |
| 120 | } |
| 121 | |
| 122 | // Otherwise, if we aren't importing, no linkage change is needed. |
| 123 | if (!isPerformingImport()) |
| 124 | return SGV->getLinkage(); |
| 125 | |
| 126 | switch (SGV->getLinkage()) { |
David Blaikie | 2f0cc47 | 2017-07-27 15:09:06 +0000 | [diff] [blame] | 127 | case GlobalValue::LinkOnceAnyLinkage: |
| 128 | case GlobalValue::LinkOnceODRLinkage: |
Teresa Johnson | 488a800 | 2016-02-10 18:11:31 +0000 | [diff] [blame] | 129 | case GlobalValue::ExternalLinkage: |
David Blaikie | 2f0cc47 | 2017-07-27 15:09:06 +0000 | [diff] [blame] | 130 | // External and linkonce definitions are converted to available_externally |
Teresa Johnson | 488a800 | 2016-02-10 18:11:31 +0000 | [diff] [blame] | 131 | // definitions upon import, so that they are available for inlining |
| 132 | // and/or optimization, but are turned into declarations later |
| 133 | // during the EliminateAvailableExternally pass. |
| 134 | if (doImportAsDefinition(SGV) && !dyn_cast<GlobalAlias>(SGV)) |
| 135 | return GlobalValue::AvailableExternallyLinkage; |
| 136 | // An imported external declaration stays external. |
| 137 | return SGV->getLinkage(); |
| 138 | |
| 139 | case GlobalValue::AvailableExternallyLinkage: |
| 140 | // An imported available_externally definition converts |
| 141 | // to external if imported as a declaration. |
| 142 | if (!doImportAsDefinition(SGV)) |
| 143 | return GlobalValue::ExternalLinkage; |
| 144 | // An imported available_externally declaration stays that way. |
| 145 | return SGV->getLinkage(); |
| 146 | |
Teresa Johnson | 488a800 | 2016-02-10 18:11:31 +0000 | [diff] [blame] | 147 | case GlobalValue::WeakAnyLinkage: |
| 148 | // Can't import weak_any definitions correctly, or we might change the |
| 149 | // program semantics, since the linker will pick the first weak_any |
| 150 | // definition and importing would change the order they are seen by the |
| 151 | // linker. The module linking caller needs to enforce this. |
| 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 | |
| 205 | // Check the summaries to see if the symbol gets resolved to a known local |
| 206 | // definition. |
| 207 | if (GV.hasName()) { |
| 208 | ValueInfo VI = ImportIndex.getValueInfo(GV.getGUID()); |
| 209 | if (VI) { |
| 210 | // Need to check all summaries are local in case of hash collisions. |
| 211 | bool IsLocal = VI.getSummaryList().size() && |
| 212 | llvm::all_of(VI.getSummaryList(), |
| 213 | [](const std::unique_ptr<GlobalValueSummary> &Summary) { |
| 214 | return Summary->isDSOLocal(); |
| 215 | }); |
| 216 | if (IsLocal) |
| 217 | GV.setDSOLocal(true); |
| 218 | } |
| 219 | } |
| 220 | |
Teresa Johnson | 1b9c2be | 2016-10-29 21:31:48 +0000 | [diff] [blame] | 221 | bool DoPromote = false; |
Teresa Johnson | 488a800 | 2016-02-10 18:11:31 +0000 | [diff] [blame] | 222 | if (GV.hasLocalLinkage() && |
Teresa Johnson | 38d4df7 | 2016-10-29 21:52:23 +0000 | [diff] [blame] | 223 | ((DoPromote = shouldPromoteLocalToGlobal(&GV)) || isPerformingImport())) { |
Teresa Johnson | 1b9c2be | 2016-10-29 21:31:48 +0000 | [diff] [blame] | 224 | // Once we change the name or linkage it is difficult to determine |
Teresa Johnson | 38d4df7 | 2016-10-29 21:52:23 +0000 | [diff] [blame] | 225 | // again whether we should promote since shouldPromoteLocalToGlobal needs |
Teresa Johnson | 1b9c2be | 2016-10-29 21:31:48 +0000 | [diff] [blame] | 226 | // to locate the summary (based on GUID from name and linkage). Therefore, |
| 227 | // use DoPromote result saved above. |
| 228 | GV.setName(getName(&GV, DoPromote)); |
| 229 | GV.setLinkage(getLinkage(&GV, DoPromote)); |
Teresa Johnson | 488a800 | 2016-02-10 18:11:31 +0000 | [diff] [blame] | 230 | if (!GV.hasLocalLinkage()) |
| 231 | GV.setVisibility(GlobalValue::HiddenVisibility); |
Teresa Johnson | 488a800 | 2016-02-10 18:11:31 +0000 | [diff] [blame] | 232 | } else |
Teresa Johnson | 1b9c2be | 2016-10-29 21:31:48 +0000 | [diff] [blame] | 233 | GV.setLinkage(getLinkage(&GV, /* DoPromote */ false)); |
Teresa Johnson | 488a800 | 2016-02-10 18:11:31 +0000 | [diff] [blame] | 234 | |
| 235 | // Remove functions imported as available externally defs from comdats, |
| 236 | // as this is a declaration for the linker, and will be dropped eventually. |
| 237 | // It is illegal for comdats to contain declarations. |
| 238 | auto *GO = dyn_cast_or_null<GlobalObject>(&GV); |
| 239 | if (GO && GO->isDeclarationForLinker() && GO->hasComdat()) { |
| 240 | // The IRMover should not have placed any imported declarations in |
| 241 | // a comdat, so the only declaration that should be in a comdat |
| 242 | // at this point would be a definition imported as available_externally. |
| 243 | assert(GO->hasAvailableExternallyLinkage() && |
| 244 | "Expected comdat on definition (possibly available external)"); |
| 245 | GO->setComdat(nullptr); |
| 246 | } |
| 247 | } |
| 248 | |
| 249 | void FunctionImportGlobalProcessing::processGlobalsForThinLTO() { |
| 250 | for (GlobalVariable &GV : M.globals()) |
| 251 | processGlobalForThinLTO(GV); |
| 252 | for (Function &SF : M) |
| 253 | processGlobalForThinLTO(SF); |
| 254 | for (GlobalAlias &GA : M.aliases()) |
| 255 | processGlobalForThinLTO(GA); |
| 256 | } |
| 257 | |
| 258 | bool FunctionImportGlobalProcessing::run() { |
| 259 | processGlobalsForThinLTO(); |
| 260 | return false; |
| 261 | } |
| 262 | |
Peter Collingbourne | 6d8f817 | 2017-02-03 16:56:27 +0000 | [diff] [blame] | 263 | bool llvm::renameModuleForThinLTO(Module &M, const ModuleSummaryIndex &Index, |
| 264 | SetVector<GlobalValue *> *GlobalsToImport) { |
Mehdi Amini | 8d05185 | 2016-03-19 00:40:31 +0000 | [diff] [blame] | 265 | FunctionImportGlobalProcessing ThinLTOProcessing(M, Index, GlobalsToImport); |
Teresa Johnson | 488a800 | 2016-02-10 18:11:31 +0000 | [diff] [blame] | 266 | return ThinLTOProcessing.run(); |
| 267 | } |