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