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