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