Teresa Johnson | df6edc5 | 2016-05-23 22:54:06 +0000 | [diff] [blame] | 1 | //===-LTO.cpp - LLVM Link Time Optimizer ----------------------------------===// |
| 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 functions and classes used to support LTO. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "llvm/LTO/LTO.h" |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 15 | #include "llvm/Analysis/TargetLibraryInfo.h" |
| 16 | #include "llvm/Analysis/TargetTransformInfo.h" |
Teresa Johnson | ad17679 | 2016-11-11 05:34:58 +0000 | [diff] [blame] | 17 | #include "llvm/Bitcode/BitcodeReader.h" |
| 18 | #include "llvm/Bitcode/BitcodeWriter.h" |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 19 | #include "llvm/CodeGen/Analysis.h" |
| 20 | #include "llvm/IR/AutoUpgrade.h" |
| 21 | #include "llvm/IR/DiagnosticPrinter.h" |
| 22 | #include "llvm/IR/LegacyPassManager.h" |
| 23 | #include "llvm/LTO/LTOBackend.h" |
| 24 | #include "llvm/Linker/IRMover.h" |
| 25 | #include "llvm/Object/ModuleSummaryIndexObjectFile.h" |
| 26 | #include "llvm/Support/ManagedStatic.h" |
Teresa Johnson | df6edc5 | 2016-05-23 22:54:06 +0000 | [diff] [blame] | 27 | #include "llvm/Support/MemoryBuffer.h" |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 28 | #include "llvm/Support/Path.h" |
Mehdi Amini | adc0e26 | 2016-08-23 21:30:12 +0000 | [diff] [blame] | 29 | #include "llvm/Support/SHA1.h" |
Teresa Johnson | df6edc5 | 2016-05-23 22:54:06 +0000 | [diff] [blame] | 30 | #include "llvm/Support/SourceMgr.h" |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 31 | #include "llvm/Support/TargetRegistry.h" |
| 32 | #include "llvm/Support/ThreadPool.h" |
Teresa Johnson | ec544c5 | 2016-10-19 17:35:01 +0000 | [diff] [blame] | 33 | #include "llvm/Support/Threading.h" |
Teresa Johnson | df6edc5 | 2016-05-23 22:54:06 +0000 | [diff] [blame] | 34 | #include "llvm/Support/raw_ostream.h" |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 35 | #include "llvm/Target/TargetMachine.h" |
| 36 | #include "llvm/Target/TargetOptions.h" |
| 37 | #include "llvm/Transforms/IPO.h" |
| 38 | #include "llvm/Transforms/IPO/PassManagerBuilder.h" |
| 39 | #include "llvm/Transforms/Utils/SplitModule.h" |
Teresa Johnson | df6edc5 | 2016-05-23 22:54:06 +0000 | [diff] [blame] | 40 | |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 41 | #include <set> |
| 42 | |
| 43 | using namespace llvm; |
| 44 | using namespace lto; |
| 45 | using namespace object; |
Teresa Johnson | df6edc5 | 2016-05-23 22:54:06 +0000 | [diff] [blame] | 46 | |
Mehdi Amini | adc0e26 | 2016-08-23 21:30:12 +0000 | [diff] [blame] | 47 | #define DEBUG_TYPE "lto" |
| 48 | |
| 49 | // Returns a unique hash for the Module considering the current list of |
| 50 | // export/import and other global analysis results. |
| 51 | // The hash is produced in \p Key. |
| 52 | static void computeCacheKey( |
Peter Collingbourne | f425752 | 2016-12-08 05:28:30 +0000 | [diff] [blame] | 53 | SmallString<40> &Key, const Config &Conf, const ModuleSummaryIndex &Index, |
| 54 | StringRef ModuleID, const FunctionImporter::ImportMapTy &ImportList, |
Mehdi Amini | adc0e26 | 2016-08-23 21:30:12 +0000 | [diff] [blame] | 55 | const FunctionImporter::ExportSetTy &ExportList, |
| 56 | const std::map<GlobalValue::GUID, GlobalValue::LinkageTypes> &ResolvedODR, |
| 57 | const GVSummaryMapTy &DefinedGlobals) { |
| 58 | // Compute the unique hash for this entry. |
| 59 | // This is based on the current compiler version, the module itself, the |
| 60 | // export list, the hash for every single module in the import list, the |
| 61 | // list of ResolvedODR for the module, and the list of preserved symbols. |
| 62 | SHA1 Hasher; |
| 63 | |
| 64 | // Start with the compiler revision |
| 65 | Hasher.update(LLVM_VERSION_STRING); |
| 66 | #ifdef HAVE_LLVM_REVISION |
| 67 | Hasher.update(LLVM_REVISION); |
| 68 | #endif |
| 69 | |
Peter Collingbourne | f425752 | 2016-12-08 05:28:30 +0000 | [diff] [blame] | 70 | // Include the parts of the LTO configuration that affect code generation. |
| 71 | auto AddString = [&](StringRef Str) { |
| 72 | Hasher.update(Str); |
| 73 | Hasher.update(ArrayRef<uint8_t>{0}); |
| 74 | }; |
| 75 | auto AddUnsigned = [&](unsigned I) { |
| 76 | uint8_t Data[4]; |
| 77 | Data[0] = I; |
| 78 | Data[1] = I >> 8; |
| 79 | Data[2] = I >> 16; |
| 80 | Data[3] = I >> 24; |
| 81 | Hasher.update(ArrayRef<uint8_t>{Data, 4}); |
| 82 | }; |
| 83 | AddString(Conf.CPU); |
| 84 | // FIXME: Hash more of Options. For now all clients initialize Options from |
| 85 | // command-line flags (which is unsupported in production), but may set |
| 86 | // RelaxELFRelocations. The clang driver can also pass FunctionSections, |
| 87 | // DataSections and DebuggerTuning via command line flags. |
| 88 | AddUnsigned(Conf.Options.RelaxELFRelocations); |
| 89 | AddUnsigned(Conf.Options.FunctionSections); |
| 90 | AddUnsigned(Conf.Options.DataSections); |
| 91 | AddUnsigned((unsigned)Conf.Options.DebuggerTuning); |
| 92 | for (auto &A : Conf.MAttrs) |
| 93 | AddString(A); |
| 94 | AddUnsigned(Conf.RelocModel); |
| 95 | AddUnsigned(Conf.CodeModel); |
| 96 | AddUnsigned(Conf.CGOptLevel); |
| 97 | AddUnsigned(Conf.OptLevel); |
| 98 | AddString(Conf.OptPipeline); |
| 99 | AddString(Conf.AAPipeline); |
| 100 | AddString(Conf.OverrideTriple); |
| 101 | AddString(Conf.DefaultTriple); |
| 102 | |
Mehdi Amini | adc0e26 | 2016-08-23 21:30:12 +0000 | [diff] [blame] | 103 | // Include the hash for the current module |
| 104 | auto ModHash = Index.getModuleHash(ModuleID); |
| 105 | Hasher.update(ArrayRef<uint8_t>((uint8_t *)&ModHash[0], sizeof(ModHash))); |
| 106 | for (auto F : ExportList) |
| 107 | // The export list can impact the internalization, be conservative here |
| 108 | Hasher.update(ArrayRef<uint8_t>((uint8_t *)&F, sizeof(F))); |
| 109 | |
| 110 | // Include the hash for every module we import functions from |
| 111 | for (auto &Entry : ImportList) { |
| 112 | auto ModHash = Index.getModuleHash(Entry.first()); |
| 113 | Hasher.update(ArrayRef<uint8_t>((uint8_t *)&ModHash[0], sizeof(ModHash))); |
| 114 | } |
| 115 | |
| 116 | // Include the hash for the resolved ODR. |
| 117 | for (auto &Entry : ResolvedODR) { |
| 118 | Hasher.update(ArrayRef<uint8_t>((const uint8_t *)&Entry.first, |
| 119 | sizeof(GlobalValue::GUID))); |
| 120 | Hasher.update(ArrayRef<uint8_t>((const uint8_t *)&Entry.second, |
| 121 | sizeof(GlobalValue::LinkageTypes))); |
| 122 | } |
| 123 | |
| 124 | // Include the hash for the linkage type to reflect internalization and weak |
| 125 | // resolution. |
| 126 | for (auto &GS : DefinedGlobals) { |
| 127 | GlobalValue::LinkageTypes Linkage = GS.second->linkage(); |
| 128 | Hasher.update( |
| 129 | ArrayRef<uint8_t>((const uint8_t *)&Linkage, sizeof(Linkage))); |
| 130 | } |
| 131 | |
Dehao Chen | 2797800 | 2016-12-16 16:48:46 +0000 | [diff] [blame] | 132 | if (!Conf.SampleProfile.empty()) { |
| 133 | auto FileOrErr = MemoryBuffer::getFile(Conf.SampleProfile); |
| 134 | if (FileOrErr) |
| 135 | Hasher.update(FileOrErr.get()->getBuffer()); |
| 136 | } |
| 137 | |
Mehdi Amini | adc0e26 | 2016-08-23 21:30:12 +0000 | [diff] [blame] | 138 | Key = toHex(Hasher.result()); |
| 139 | } |
| 140 | |
Teresa Johnson | 04c9a2d | 2016-05-25 14:03:11 +0000 | [diff] [blame] | 141 | static void thinLTOResolveWeakForLinkerGUID( |
| 142 | GlobalValueSummaryList &GVSummaryList, GlobalValue::GUID GUID, |
| 143 | DenseSet<GlobalValueSummary *> &GlobalInvolvedWithAlias, |
Benjamin Kramer | d3f4c05 | 2016-06-12 16:13:55 +0000 | [diff] [blame] | 144 | function_ref<bool(GlobalValue::GUID, const GlobalValueSummary *)> |
Teresa Johnson | 04c9a2d | 2016-05-25 14:03:11 +0000 | [diff] [blame] | 145 | isPrevailing, |
Benjamin Kramer | d3f4c05 | 2016-06-12 16:13:55 +0000 | [diff] [blame] | 146 | function_ref<void(StringRef, GlobalValue::GUID, GlobalValue::LinkageTypes)> |
Teresa Johnson | 04c9a2d | 2016-05-25 14:03:11 +0000 | [diff] [blame] | 147 | recordNewLinkage) { |
Teresa Johnson | 04c9a2d | 2016-05-25 14:03:11 +0000 | [diff] [blame] | 148 | for (auto &S : GVSummaryList) { |
Teresa Johnson | 04c9a2d | 2016-05-25 14:03:11 +0000 | [diff] [blame] | 149 | GlobalValue::LinkageTypes OriginalLinkage = S->linkage(); |
| 150 | if (!GlobalValue::isWeakForLinker(OriginalLinkage)) |
| 151 | continue; |
Peter Collingbourne | 73589f3 | 2016-07-07 18:31:51 +0000 | [diff] [blame] | 152 | // We need to emit only one of these. The prevailing module will keep it, |
Teresa Johnson | 04c9a2d | 2016-05-25 14:03:11 +0000 | [diff] [blame] | 153 | // but turned into a weak, while the others will drop it when possible. |
Teresa Johnson | 3bc8abd | 2016-10-30 05:15:23 +0000 | [diff] [blame] | 154 | // This is both a compile-time optimization and a correctness |
| 155 | // transformation. This is necessary for correctness when we have exported |
| 156 | // a reference - we need to convert the linkonce to weak to |
| 157 | // ensure a copy is kept to satisfy the exported reference. |
| 158 | // FIXME: We may want to split the compile time and correctness |
| 159 | // aspects into separate routines. |
Peter Collingbourne | 73589f3 | 2016-07-07 18:31:51 +0000 | [diff] [blame] | 160 | if (isPrevailing(GUID, S.get())) { |
Teresa Johnson | 28c03b5 | 2016-05-26 14:16:52 +0000 | [diff] [blame] | 161 | if (GlobalValue::isLinkOnceLinkage(OriginalLinkage)) |
| 162 | S->setLinkage(GlobalValue::getWeakLinkage( |
| 163 | GlobalValue::isLinkOnceODRLinkage(OriginalLinkage))); |
Teresa Johnson | 04c9a2d | 2016-05-25 14:03:11 +0000 | [diff] [blame] | 164 | } |
Teresa Johnson | 3bc8abd | 2016-10-30 05:15:23 +0000 | [diff] [blame] | 165 | // Alias and aliasee can't be turned into available_externally. |
Teresa Johnson | 04c9a2d | 2016-05-25 14:03:11 +0000 | [diff] [blame] | 166 | else if (!isa<AliasSummary>(S.get()) && |
Teresa Johnson | 4566c6d | 2017-01-20 21:54:58 +0000 | [diff] [blame] | 167 | !GlobalInvolvedWithAlias.count(S.get())) |
Teresa Johnson | 04c9a2d | 2016-05-25 14:03:11 +0000 | [diff] [blame] | 168 | S->setLinkage(GlobalValue::AvailableExternallyLinkage); |
| 169 | if (S->linkage() != OriginalLinkage) |
| 170 | recordNewLinkage(S->modulePath(), GUID, S->linkage()); |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | // Resolve Weak and LinkOnce values in the \p Index. |
| 175 | // |
| 176 | // We'd like to drop these functions if they are no longer referenced in the |
| 177 | // current module. However there is a chance that another module is still |
| 178 | // referencing them because of the import. We make sure we always emit at least |
| 179 | // one copy. |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 180 | void llvm::thinLTOResolveWeakForLinkerInIndex( |
Teresa Johnson | 04c9a2d | 2016-05-25 14:03:11 +0000 | [diff] [blame] | 181 | ModuleSummaryIndex &Index, |
Benjamin Kramer | d3f4c05 | 2016-06-12 16:13:55 +0000 | [diff] [blame] | 182 | function_ref<bool(GlobalValue::GUID, const GlobalValueSummary *)> |
Teresa Johnson | 04c9a2d | 2016-05-25 14:03:11 +0000 | [diff] [blame] | 183 | isPrevailing, |
Benjamin Kramer | d3f4c05 | 2016-06-12 16:13:55 +0000 | [diff] [blame] | 184 | function_ref<void(StringRef, GlobalValue::GUID, GlobalValue::LinkageTypes)> |
Teresa Johnson | 04c9a2d | 2016-05-25 14:03:11 +0000 | [diff] [blame] | 185 | recordNewLinkage) { |
Teresa Johnson | 04c9a2d | 2016-05-25 14:03:11 +0000 | [diff] [blame] | 186 | // We won't optimize the globals that are referenced by an alias for now |
| 187 | // Ideally we should turn the alias into a global and duplicate the definition |
| 188 | // when needed. |
| 189 | DenseSet<GlobalValueSummary *> GlobalInvolvedWithAlias; |
| 190 | for (auto &I : Index) |
| 191 | for (auto &S : I.second) |
| 192 | if (auto AS = dyn_cast<AliasSummary>(S.get())) |
| 193 | GlobalInvolvedWithAlias.insert(&AS->getAliasee()); |
| 194 | |
| 195 | for (auto &I : Index) |
| 196 | thinLTOResolveWeakForLinkerGUID(I.second, I.first, GlobalInvolvedWithAlias, |
Peter Collingbourne | 73589f3 | 2016-07-07 18:31:51 +0000 | [diff] [blame] | 197 | isPrevailing, recordNewLinkage); |
Teresa Johnson | 04c9a2d | 2016-05-25 14:03:11 +0000 | [diff] [blame] | 198 | } |
| 199 | |
| 200 | static void thinLTOInternalizeAndPromoteGUID( |
| 201 | GlobalValueSummaryList &GVSummaryList, GlobalValue::GUID GUID, |
Benjamin Kramer | d3f4c05 | 2016-06-12 16:13:55 +0000 | [diff] [blame] | 202 | function_ref<bool(StringRef, GlobalValue::GUID)> isExported) { |
Teresa Johnson | 04c9a2d | 2016-05-25 14:03:11 +0000 | [diff] [blame] | 203 | for (auto &S : GVSummaryList) { |
| 204 | if (isExported(S->modulePath(), GUID)) { |
| 205 | if (GlobalValue::isLocalLinkage(S->linkage())) |
| 206 | S->setLinkage(GlobalValue::ExternalLinkage); |
| 207 | } else if (!GlobalValue::isLocalLinkage(S->linkage())) |
| 208 | S->setLinkage(GlobalValue::InternalLinkage); |
| 209 | } |
| 210 | } |
| 211 | |
| 212 | // Update the linkages in the given \p Index to mark exported values |
| 213 | // as external and non-exported values as internal. |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 214 | void llvm::thinLTOInternalizeAndPromoteInIndex( |
Teresa Johnson | 04c9a2d | 2016-05-25 14:03:11 +0000 | [diff] [blame] | 215 | ModuleSummaryIndex &Index, |
Benjamin Kramer | d3f4c05 | 2016-06-12 16:13:55 +0000 | [diff] [blame] | 216 | function_ref<bool(StringRef, GlobalValue::GUID)> isExported) { |
Teresa Johnson | 04c9a2d | 2016-05-25 14:03:11 +0000 | [diff] [blame] | 217 | for (auto &I : Index) |
| 218 | thinLTOInternalizeAndPromoteGUID(I.second, I.first, isExported); |
| 219 | } |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 220 | |
Peter Collingbourne | 1a0720e | 2016-12-14 01:17:59 +0000 | [diff] [blame] | 221 | struct InputFile::InputModule { |
| 222 | BitcodeModule BM; |
| 223 | std::unique_ptr<Module> Mod; |
| 224 | |
| 225 | // The range of ModuleSymbolTable entries for this input module. |
| 226 | size_t SymBegin, SymEnd; |
| 227 | }; |
| 228 | |
| 229 | // Requires a destructor for std::vector<InputModule>. |
| 230 | InputFile::~InputFile() = default; |
| 231 | |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 232 | Expected<std::unique_ptr<InputFile>> InputFile::create(MemoryBufferRef Object) { |
| 233 | std::unique_ptr<InputFile> File(new InputFile); |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 234 | |
Peter Collingbourne | ad90369 | 2016-12-13 19:43:49 +0000 | [diff] [blame] | 235 | ErrorOr<MemoryBufferRef> BCOrErr = |
| 236 | IRObjectFile::findBitcodeInMemBuffer(Object); |
| 237 | if (!BCOrErr) |
| 238 | return errorCodeToError(BCOrErr.getError()); |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 239 | |
Peter Collingbourne | 1a0720e | 2016-12-14 01:17:59 +0000 | [diff] [blame] | 240 | Expected<std::vector<BitcodeModule>> BMsOrErr = |
| 241 | getBitcodeModuleList(*BCOrErr); |
| 242 | if (!BMsOrErr) |
| 243 | return BMsOrErr.takeError(); |
Peter Collingbourne | ad90369 | 2016-12-13 19:43:49 +0000 | [diff] [blame] | 244 | |
Peter Collingbourne | 1a0720e | 2016-12-14 01:17:59 +0000 | [diff] [blame] | 245 | if (BMsOrErr->empty()) |
| 246 | return make_error<StringError>("Bitcode file does not contain any modules", |
| 247 | inconvertibleErrorCode()); |
Peter Collingbourne | ad90369 | 2016-12-13 19:43:49 +0000 | [diff] [blame] | 248 | |
Peter Collingbourne | 1a0720e | 2016-12-14 01:17:59 +0000 | [diff] [blame] | 249 | // Create an InputModule for each module in the InputFile, and add it to the |
| 250 | // ModuleSymbolTable. |
| 251 | for (auto BM : *BMsOrErr) { |
| 252 | Expected<std::unique_ptr<Module>> MOrErr = |
Teresa Johnson | a61f5e3 | 2016-12-16 21:25:01 +0000 | [diff] [blame] | 253 | BM.getLazyModule(File->Ctx, /*ShouldLazyLoadMetadata*/ true, |
| 254 | /*IsImporting*/ false); |
Peter Collingbourne | 1a0720e | 2016-12-14 01:17:59 +0000 | [diff] [blame] | 255 | if (!MOrErr) |
| 256 | return MOrErr.takeError(); |
| 257 | |
| 258 | size_t SymBegin = File->SymTab.symbols().size(); |
| 259 | File->SymTab.addModule(MOrErr->get()); |
| 260 | size_t SymEnd = File->SymTab.symbols().size(); |
| 261 | |
| 262 | for (const auto &C : (*MOrErr)->getComdatSymbolTable()) { |
| 263 | auto P = File->ComdatMap.insert( |
| 264 | std::make_pair(&C.second, File->Comdats.size())); |
| 265 | assert(P.second); |
| 266 | (void)P; |
| 267 | File->Comdats.push_back(C.first()); |
| 268 | } |
| 269 | |
| 270 | File->Mods.push_back({BM, std::move(*MOrErr), SymBegin, SymEnd}); |
Rafael Espindola | 7912110 | 2016-10-25 12:02:03 +0000 | [diff] [blame] | 271 | } |
| 272 | |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 273 | return std::move(File); |
| 274 | } |
| 275 | |
Rafael Espindola | 7912110 | 2016-10-25 12:02:03 +0000 | [diff] [blame] | 276 | Expected<int> InputFile::Symbol::getComdatIndex() const { |
Peter Collingbourne | ad90369 | 2016-12-13 19:43:49 +0000 | [diff] [blame] | 277 | if (!isGV()) |
Rafael Espindola | 7912110 | 2016-10-25 12:02:03 +0000 | [diff] [blame] | 278 | return -1; |
Peter Collingbourne | ad90369 | 2016-12-13 19:43:49 +0000 | [diff] [blame] | 279 | const GlobalObject *GO = getGV()->getBaseObject(); |
| 280 | if (!GO) |
| 281 | return make_error<StringError>("Unable to determine comdat of alias!", |
| 282 | inconvertibleErrorCode()); |
Rafael Espindola | 7912110 | 2016-10-25 12:02:03 +0000 | [diff] [blame] | 283 | if (const Comdat *C = GO->getComdat()) { |
| 284 | auto I = File->ComdatMap.find(C); |
| 285 | assert(I != File->ComdatMap.end()); |
| 286 | return I->second; |
| 287 | } |
| 288 | return -1; |
| 289 | } |
| 290 | |
Peter Collingbourne | 1a0720e | 2016-12-14 01:17:59 +0000 | [diff] [blame] | 291 | StringRef InputFile::getName() const { |
| 292 | return Mods[0].BM.getModuleIdentifier(); |
| 293 | } |
| 294 | |
| 295 | StringRef InputFile::getSourceFileName() const { |
| 296 | return Mods[0].Mod->getSourceFileName(); |
| 297 | } |
| 298 | |
| 299 | iterator_range<InputFile::symbol_iterator> |
| 300 | InputFile::module_symbols(InputModule &IM) { |
| 301 | return llvm::make_range( |
| 302 | symbol_iterator(SymTab.symbols().data() + IM.SymBegin, SymTab, this), |
| 303 | symbol_iterator(SymTab.symbols().data() + IM.SymEnd, SymTab, this)); |
| 304 | } |
| 305 | |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 306 | LTO::RegularLTOState::RegularLTOState(unsigned ParallelCodeGenParallelismLevel, |
| 307 | Config &Conf) |
| 308 | : ParallelCodeGenParallelismLevel(ParallelCodeGenParallelismLevel), |
Mehdi Amini | e749453 | 2016-08-23 18:39:12 +0000 | [diff] [blame] | 309 | Ctx(Conf) {} |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 310 | |
| 311 | LTO::ThinLTOState::ThinLTOState(ThinBackend Backend) : Backend(Backend) { |
| 312 | if (!Backend) |
Teresa Johnson | ec544c5 | 2016-10-19 17:35:01 +0000 | [diff] [blame] | 313 | this->Backend = |
| 314 | createInProcessThinBackend(llvm::heavyweight_hardware_concurrency()); |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 315 | } |
| 316 | |
| 317 | LTO::LTO(Config Conf, ThinBackend Backend, |
| 318 | unsigned ParallelCodeGenParallelismLevel) |
| 319 | : Conf(std::move(Conf)), |
| 320 | RegularLTO(ParallelCodeGenParallelismLevel, this->Conf), |
Mehdi Amini | 026ddbb | 2016-08-19 05:56:37 +0000 | [diff] [blame] | 321 | ThinLTO(std::move(Backend)) {} |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 322 | |
Peter Collingbourne | 1a0720e | 2016-12-14 01:17:59 +0000 | [diff] [blame] | 323 | // Requires a destructor for MapVector<BitcodeModule>. |
| 324 | LTO::~LTO() = default; |
| 325 | |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 326 | // Add the given symbol to the GlobalResolutions map, and resolve its partition. |
Peter Collingbourne | ad90369 | 2016-12-13 19:43:49 +0000 | [diff] [blame] | 327 | void LTO::addSymbolToGlobalRes(SmallPtrSet<GlobalValue *, 8> &Used, |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 328 | const InputFile::Symbol &Sym, |
Teresa Johnson | faa7506 | 2016-08-11 20:38:39 +0000 | [diff] [blame] | 329 | SymbolResolution Res, unsigned Partition) { |
Peter Collingbourne | ad90369 | 2016-12-13 19:43:49 +0000 | [diff] [blame] | 330 | GlobalValue *GV = Sym.isGV() ? Sym.getGV() : nullptr; |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 331 | |
| 332 | auto &GlobalRes = GlobalResolutions[Sym.getName()]; |
| 333 | if (GV) { |
| 334 | GlobalRes.UnnamedAddr &= GV->hasGlobalUnnamedAddr(); |
| 335 | if (Res.Prevailing) |
| 336 | GlobalRes.IRName = GV->getName(); |
| 337 | } |
Teresa Johnson | 6c475a7 | 2017-01-05 21:34:18 +0000 | [diff] [blame] | 338 | // Set the partition to external if we know it is used elsewhere, e.g. |
| 339 | // it is visible to a regular object, is referenced from llvm.compiler_used, |
| 340 | // or was already recorded as being referenced from a different partition. |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 341 | if (Res.VisibleToRegularObj || (GV && Used.count(GV)) || |
| 342 | (GlobalRes.Partition != GlobalResolution::Unknown && |
Teresa Johnson | 6c475a7 | 2017-01-05 21:34:18 +0000 | [diff] [blame] | 343 | GlobalRes.Partition != Partition)) { |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 344 | GlobalRes.Partition = GlobalResolution::External; |
Teresa Johnson | 6c475a7 | 2017-01-05 21:34:18 +0000 | [diff] [blame] | 345 | } else |
| 346 | // First recorded reference, save the current partition. |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 347 | GlobalRes.Partition = Partition; |
Teresa Johnson | 6c475a7 | 2017-01-05 21:34:18 +0000 | [diff] [blame] | 348 | |
| 349 | // Flag as visible outside of ThinLTO if visible from a regular object or |
| 350 | // if this is a reference in the regular LTO partition. |
| 351 | GlobalRes.VisibleOutsideThinLTO |= |
| 352 | (Res.VisibleToRegularObj || (Partition == GlobalResolution::RegularLTO)); |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 353 | } |
| 354 | |
Rafael Espindola | 7775c33 | 2016-08-26 20:19:35 +0000 | [diff] [blame] | 355 | static void writeToResolutionFile(raw_ostream &OS, InputFile *Input, |
| 356 | ArrayRef<SymbolResolution> Res) { |
Peter Collingbourne | 1a0720e | 2016-12-14 01:17:59 +0000 | [diff] [blame] | 357 | StringRef Path = Input->getName(); |
Rafael Espindola | 7775c33 | 2016-08-26 20:19:35 +0000 | [diff] [blame] | 358 | OS << Path << '\n'; |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 359 | auto ResI = Res.begin(); |
| 360 | for (const InputFile::Symbol &Sym : Input->symbols()) { |
| 361 | assert(ResI != Res.end()); |
| 362 | SymbolResolution Res = *ResI++; |
| 363 | |
Rafael Espindola | 7775c33 | 2016-08-26 20:19:35 +0000 | [diff] [blame] | 364 | OS << "-r=" << Path << ',' << Sym.getName() << ','; |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 365 | if (Res.Prevailing) |
Rafael Espindola | 7775c33 | 2016-08-26 20:19:35 +0000 | [diff] [blame] | 366 | OS << 'p'; |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 367 | if (Res.FinalDefinitionInLinkageUnit) |
Rafael Espindola | 7775c33 | 2016-08-26 20:19:35 +0000 | [diff] [blame] | 368 | OS << 'l'; |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 369 | if (Res.VisibleToRegularObj) |
Rafael Espindola | 7775c33 | 2016-08-26 20:19:35 +0000 | [diff] [blame] | 370 | OS << 'x'; |
| 371 | OS << '\n'; |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 372 | } |
Peter Collingbourne | 58ffcfb | 2017-01-19 23:10:14 +0000 | [diff] [blame] | 373 | OS.flush(); |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 374 | assert(ResI == Res.end()); |
| 375 | } |
| 376 | |
| 377 | Error LTO::add(std::unique_ptr<InputFile> Input, |
| 378 | ArrayRef<SymbolResolution> Res) { |
| 379 | assert(!CalledGetMaxTasks); |
| 380 | |
| 381 | if (Conf.ResolutionFile) |
Rafael Espindola | 7775c33 | 2016-08-26 20:19:35 +0000 | [diff] [blame] | 382 | writeToResolutionFile(*Conf.ResolutionFile, Input.get(), Res); |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 383 | |
Peter Collingbourne | 1a0720e | 2016-12-14 01:17:59 +0000 | [diff] [blame] | 384 | const SymbolResolution *ResI = Res.begin(); |
| 385 | for (InputFile::InputModule &IM : Input->Mods) |
| 386 | if (Error Err = addModule(*Input, IM, ResI, Res.end())) |
| 387 | return Err; |
| 388 | |
| 389 | assert(ResI == Res.end()); |
| 390 | return Error::success(); |
| 391 | } |
| 392 | |
| 393 | Error LTO::addModule(InputFile &Input, InputFile::InputModule &IM, |
| 394 | const SymbolResolution *&ResI, |
| 395 | const SymbolResolution *ResE) { |
Mehdi Amini | 9989f80 | 2016-08-19 15:35:44 +0000 | [diff] [blame] | 396 | // FIXME: move to backend |
Peter Collingbourne | 1a0720e | 2016-12-14 01:17:59 +0000 | [diff] [blame] | 397 | Module &M = *IM.Mod; |
Davide Italiano | 2ceb628 | 2016-12-14 21:57:04 +0000 | [diff] [blame] | 398 | |
| 399 | if (M.getDataLayoutStr().empty()) |
| 400 | return make_error<StringError>("input module has no datalayout", |
| 401 | inconvertibleErrorCode()); |
| 402 | |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 403 | if (!Conf.OverrideTriple.empty()) |
| 404 | M.setTargetTriple(Conf.OverrideTriple); |
| 405 | else if (M.getTargetTriple().empty()) |
| 406 | M.setTargetTriple(Conf.DefaultTriple); |
| 407 | |
Peter Collingbourne | 1a0720e | 2016-12-14 01:17:59 +0000 | [diff] [blame] | 408 | Expected<bool> HasThinLTOSummary = IM.BM.hasSummary(); |
Peter Collingbourne | cd513a4 | 2016-11-11 19:50:24 +0000 | [diff] [blame] | 409 | if (!HasThinLTOSummary) |
| 410 | return HasThinLTOSummary.takeError(); |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 411 | |
Peter Collingbourne | cd513a4 | 2016-11-11 19:50:24 +0000 | [diff] [blame] | 412 | if (*HasThinLTOSummary) |
Peter Collingbourne | 1a0720e | 2016-12-14 01:17:59 +0000 | [diff] [blame] | 413 | return addThinLTO(IM.BM, M, Input.module_symbols(IM), ResI, ResE); |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 414 | else |
Peter Collingbourne | 1a0720e | 2016-12-14 01:17:59 +0000 | [diff] [blame] | 415 | return addRegularLTO(IM.BM, ResI, ResE); |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 416 | } |
| 417 | |
| 418 | // Add a regular LTO object to the link. |
Peter Collingbourne | 1a0720e | 2016-12-14 01:17:59 +0000 | [diff] [blame] | 419 | Error LTO::addRegularLTO(BitcodeModule BM, const SymbolResolution *&ResI, |
| 420 | const SymbolResolution *ResE) { |
Mehdi Amini | e749453 | 2016-08-23 18:39:12 +0000 | [diff] [blame] | 421 | if (!RegularLTO.CombinedModule) { |
| 422 | RegularLTO.CombinedModule = |
| 423 | llvm::make_unique<Module>("ld-temp.o", RegularLTO.Ctx); |
| 424 | RegularLTO.Mover = llvm::make_unique<IRMover>(*RegularLTO.CombinedModule); |
| 425 | } |
Peter Collingbourne | ad90369 | 2016-12-13 19:43:49 +0000 | [diff] [blame] | 426 | Expected<std::unique_ptr<Module>> MOrErr = |
Teresa Johnson | a61f5e3 | 2016-12-16 21:25:01 +0000 | [diff] [blame] | 427 | BM.getLazyModule(RegularLTO.Ctx, /*ShouldLazyLoadMetadata*/ true, |
| 428 | /*IsImporting*/ false); |
Peter Collingbourne | ad90369 | 2016-12-13 19:43:49 +0000 | [diff] [blame] | 429 | if (!MOrErr) |
| 430 | return MOrErr.takeError(); |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 431 | |
Peter Collingbourne | ad90369 | 2016-12-13 19:43:49 +0000 | [diff] [blame] | 432 | Module &M = **MOrErr; |
Peter Collingbourne | 7f00d0a | 2016-11-09 17:49:19 +0000 | [diff] [blame] | 433 | if (Error Err = M.materializeMetadata()) |
| 434 | return Err; |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 435 | UpgradeDebugInfo(M); |
| 436 | |
Peter Collingbourne | ad90369 | 2016-12-13 19:43:49 +0000 | [diff] [blame] | 437 | ModuleSymbolTable SymTab; |
| 438 | SymTab.addModule(&M); |
| 439 | |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 440 | SmallPtrSet<GlobalValue *, 8> Used; |
| 441 | collectUsedGlobalVariables(M, Used, /*CompilerUsed*/ false); |
| 442 | |
| 443 | std::vector<GlobalValue *> Keep; |
| 444 | |
| 445 | for (GlobalVariable &GV : M.globals()) |
| 446 | if (GV.hasAppendingLinkage()) |
| 447 | Keep.push_back(&GV); |
| 448 | |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 449 | for (const InputFile::Symbol &Sym : |
Peter Collingbourne | ad90369 | 2016-12-13 19:43:49 +0000 | [diff] [blame] | 450 | make_range(InputFile::symbol_iterator(SymTab.symbols().begin(), SymTab, |
| 451 | nullptr), |
| 452 | InputFile::symbol_iterator(SymTab.symbols().end(), SymTab, |
| 453 | nullptr))) { |
Peter Collingbourne | 1a0720e | 2016-12-14 01:17:59 +0000 | [diff] [blame] | 454 | assert(ResI != ResE); |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 455 | SymbolResolution Res = *ResI++; |
Peter Collingbourne | ad90369 | 2016-12-13 19:43:49 +0000 | [diff] [blame] | 456 | addSymbolToGlobalRes(Used, Sym, Res, 0); |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 457 | |
Peter Collingbourne | c387e70 | 2017-02-02 05:12:15 +0000 | [diff] [blame^] | 458 | if (Sym.isGV()) { |
Peter Collingbourne | ad90369 | 2016-12-13 19:43:49 +0000 | [diff] [blame] | 459 | GlobalValue *GV = Sym.getGV(); |
Peter Collingbourne | c387e70 | 2017-02-02 05:12:15 +0000 | [diff] [blame^] | 460 | if (Res.Prevailing) { |
| 461 | if (Sym.getFlags() & object::BasicSymbolRef::SF_Undefined) |
| 462 | continue; |
| 463 | Keep.push_back(GV); |
| 464 | switch (GV->getLinkage()) { |
| 465 | default: |
| 466 | break; |
| 467 | case GlobalValue::LinkOnceAnyLinkage: |
| 468 | GV->setLinkage(GlobalValue::WeakAnyLinkage); |
| 469 | break; |
| 470 | case GlobalValue::LinkOnceODRLinkage: |
| 471 | GV->setLinkage(GlobalValue::WeakODRLinkage); |
| 472 | break; |
| 473 | } |
| 474 | } else if (GV->hasAvailableExternallyLinkage()) { |
| 475 | // We can link available_externally symbols even if they are |
| 476 | // non-prevailing. |
| 477 | GlobalValue *CombinedGV = |
| 478 | RegularLTO.CombinedModule->getNamedValue(GV->getName()); |
| 479 | if (!CombinedGV || CombinedGV->isDeclaration()) |
| 480 | Keep.push_back(GV); |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 481 | } |
| 482 | } |
Mehdi Amini | b2f46d1d | 2016-09-14 21:05:04 +0000 | [diff] [blame] | 483 | // Common resolution: collect the maximum size/alignment over all commons. |
| 484 | // We also record if we see an instance of a common as prevailing, so that |
| 485 | // if none is prevailing we can ignore it later. |
Mehdi Amini | dc4c8cf | 2016-08-22 06:25:46 +0000 | [diff] [blame] | 486 | if (Sym.getFlags() & object::BasicSymbolRef::SF_Common) { |
Peter Collingbourne | fb8c2a4 | 2016-12-01 02:51:12 +0000 | [diff] [blame] | 487 | // FIXME: We should figure out what to do about commons defined by asm. |
| 488 | // For now they aren't reported correctly by ModuleSymbolTable. |
Peter Collingbourne | ad90369 | 2016-12-13 19:43:49 +0000 | [diff] [blame] | 489 | auto &CommonRes = RegularLTO.Commons[Sym.getGV()->getName()]; |
Mehdi Amini | dc4c8cf | 2016-08-22 06:25:46 +0000 | [diff] [blame] | 490 | CommonRes.Size = std::max(CommonRes.Size, Sym.getCommonSize()); |
| 491 | CommonRes.Align = std::max(CommonRes.Align, Sym.getCommonAlignment()); |
Mehdi Amini | b2f46d1d | 2016-09-14 21:05:04 +0000 | [diff] [blame] | 492 | CommonRes.Prevailing |= Res.Prevailing; |
Mehdi Amini | dc4c8cf | 2016-08-22 06:25:46 +0000 | [diff] [blame] | 493 | } |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 494 | |
| 495 | // FIXME: use proposed local attribute for FinalDefinitionInLinkageUnit. |
| 496 | } |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 497 | |
Peter Collingbourne | ad90369 | 2016-12-13 19:43:49 +0000 | [diff] [blame] | 498 | return RegularLTO.Mover->move(std::move(*MOrErr), Keep, |
Teresa Johnson | 4b9b379 | 2016-10-12 18:39:29 +0000 | [diff] [blame] | 499 | [](GlobalValue &, IRMover::ValueAdder) {}, |
Teresa Johnson | 040cc16 | 2016-12-12 16:09:30 +0000 | [diff] [blame] | 500 | /* LinkModuleInlineAsm */ true, |
| 501 | /* IsPerformingImport */ false); |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 502 | } |
| 503 | |
| 504 | // Add a ThinLTO object to the link. |
Peter Collingbourne | 1a0720e | 2016-12-14 01:17:59 +0000 | [diff] [blame] | 505 | // FIXME: This function should not need to take as many parameters once we have |
| 506 | // a bitcode symbol table. |
| 507 | Error LTO::addThinLTO(BitcodeModule BM, Module &M, |
| 508 | iterator_range<InputFile::symbol_iterator> Syms, |
| 509 | const SymbolResolution *&ResI, |
| 510 | const SymbolResolution *ResE) { |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 511 | SmallPtrSet<GlobalValue *, 8> Used; |
| 512 | collectUsedGlobalVariables(M, Used, /*CompilerUsed*/ false); |
| 513 | |
Peter Collingbourne | 1a0720e | 2016-12-14 01:17:59 +0000 | [diff] [blame] | 514 | Expected<std::unique_ptr<ModuleSummaryIndex>> SummaryOrErr = BM.getSummary(); |
| 515 | if (!SummaryOrErr) |
| 516 | return SummaryOrErr.takeError(); |
| 517 | ThinLTO.CombinedIndex.mergeFrom(std::move(*SummaryOrErr), |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 518 | ThinLTO.ModuleMap.size()); |
| 519 | |
Peter Collingbourne | 1a0720e | 2016-12-14 01:17:59 +0000 | [diff] [blame] | 520 | for (const InputFile::Symbol &Sym : Syms) { |
| 521 | assert(ResI != ResE); |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 522 | SymbolResolution Res = *ResI++; |
Peter Collingbourne | ad90369 | 2016-12-13 19:43:49 +0000 | [diff] [blame] | 523 | addSymbolToGlobalRes(Used, Sym, Res, ThinLTO.ModuleMap.size() + 1); |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 524 | |
Peter Collingbourne | ad90369 | 2016-12-13 19:43:49 +0000 | [diff] [blame] | 525 | if (Res.Prevailing && Sym.isGV()) |
| 526 | ThinLTO.PrevailingModuleForGUID[Sym.getGV()->getGUID()] = |
Peter Collingbourne | 1a0720e | 2016-12-14 01:17:59 +0000 | [diff] [blame] | 527 | BM.getModuleIdentifier(); |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 528 | } |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 529 | |
Peter Collingbourne | 1a0720e | 2016-12-14 01:17:59 +0000 | [diff] [blame] | 530 | if (!ThinLTO.ModuleMap.insert({BM.getModuleIdentifier(), BM}).second) |
| 531 | return make_error<StringError>( |
| 532 | "Expected at most one ThinLTO module per bitcode file", |
| 533 | inconvertibleErrorCode()); |
| 534 | |
Mehdi Amini | 41af430 | 2016-11-11 04:28:40 +0000 | [diff] [blame] | 535 | return Error::success(); |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 536 | } |
| 537 | |
Teresa Johnson | faa7506 | 2016-08-11 20:38:39 +0000 | [diff] [blame] | 538 | unsigned LTO::getMaxTasks() const { |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 539 | CalledGetMaxTasks = true; |
| 540 | return RegularLTO.ParallelCodeGenParallelismLevel + ThinLTO.ModuleMap.size(); |
| 541 | } |
| 542 | |
Peter Collingbourne | 80186a5 | 2016-09-23 21:33:43 +0000 | [diff] [blame] | 543 | Error LTO::run(AddStreamFn AddStream, NativeObjectCache Cache) { |
Teresa Johnson | 8dd61ae | 2016-09-16 13:54:19 +0000 | [diff] [blame] | 544 | // Save the status of having a regularLTO combined module, as |
| 545 | // this is needed for generating the ThinLTO Task ID, and |
| 546 | // the CombinedModule will be moved at the end of runRegularLTO. |
| 547 | bool HasRegularLTO = RegularLTO.CombinedModule != nullptr; |
Mehdi Amini | e749453 | 2016-08-23 18:39:12 +0000 | [diff] [blame] | 548 | // Invoke regular LTO if there was a regular LTO module to start with. |
Teresa Johnson | 8dd61ae | 2016-09-16 13:54:19 +0000 | [diff] [blame] | 549 | if (HasRegularLTO) |
Peter Collingbourne | 80186a5 | 2016-09-23 21:33:43 +0000 | [diff] [blame] | 550 | if (auto E = runRegularLTO(AddStream)) |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 551 | return E; |
Peter Collingbourne | 80186a5 | 2016-09-23 21:33:43 +0000 | [diff] [blame] | 552 | return runThinLTO(AddStream, Cache, HasRegularLTO); |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 553 | } |
| 554 | |
Peter Collingbourne | 80186a5 | 2016-09-23 21:33:43 +0000 | [diff] [blame] | 555 | Error LTO::runRegularLTO(AddStreamFn AddStream) { |
Mehdi Amini | dc4c8cf | 2016-08-22 06:25:46 +0000 | [diff] [blame] | 556 | // Make sure commons have the right size/alignment: we kept the largest from |
| 557 | // all the prevailing when adding the inputs, and we apply it here. |
Teresa Johnson | e2e621a | 2016-08-27 04:41:22 +0000 | [diff] [blame] | 558 | const DataLayout &DL = RegularLTO.CombinedModule->getDataLayout(); |
Mehdi Amini | dc4c8cf | 2016-08-22 06:25:46 +0000 | [diff] [blame] | 559 | for (auto &I : RegularLTO.Commons) { |
Mehdi Amini | b2f46d1d | 2016-09-14 21:05:04 +0000 | [diff] [blame] | 560 | if (!I.second.Prevailing) |
| 561 | // Don't do anything if no instance of this common was prevailing. |
| 562 | continue; |
Mehdi Amini | dc4c8cf | 2016-08-22 06:25:46 +0000 | [diff] [blame] | 563 | GlobalVariable *OldGV = RegularLTO.CombinedModule->getNamedGlobal(I.first); |
Teresa Johnson | e2e621a | 2016-08-27 04:41:22 +0000 | [diff] [blame] | 564 | if (OldGV && DL.getTypeAllocSize(OldGV->getValueType()) == I.second.Size) { |
Mehdi Amini | dc4c8cf | 2016-08-22 06:25:46 +0000 | [diff] [blame] | 565 | // Don't create a new global if the type is already correct, just make |
| 566 | // sure the alignment is correct. |
| 567 | OldGV->setAlignment(I.second.Align); |
| 568 | continue; |
| 569 | } |
Teresa Johnson | e2e621a | 2016-08-27 04:41:22 +0000 | [diff] [blame] | 570 | ArrayType *Ty = |
| 571 | ArrayType::get(Type::getInt8Ty(RegularLTO.Ctx), I.second.Size); |
Mehdi Amini | dc4c8cf | 2016-08-22 06:25:46 +0000 | [diff] [blame] | 572 | auto *GV = new GlobalVariable(*RegularLTO.CombinedModule, Ty, false, |
| 573 | GlobalValue::CommonLinkage, |
| 574 | ConstantAggregateZero::get(Ty), ""); |
| 575 | GV->setAlignment(I.second.Align); |
| 576 | if (OldGV) { |
| 577 | OldGV->replaceAllUsesWith(ConstantExpr::getBitCast(GV, OldGV->getType())); |
| 578 | GV->takeName(OldGV); |
| 579 | OldGV->eraseFromParent(); |
| 580 | } else { |
| 581 | GV->setName(I.first); |
| 582 | } |
| 583 | } |
| 584 | |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 585 | if (Conf.PreOptModuleHook && |
| 586 | !Conf.PreOptModuleHook(0, *RegularLTO.CombinedModule)) |
Mehdi Amini | 41af430 | 2016-11-11 04:28:40 +0000 | [diff] [blame] | 587 | return Error::success(); |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 588 | |
Mehdi Amini | d310b47 | 2016-08-22 06:25:41 +0000 | [diff] [blame] | 589 | if (!Conf.CodeGenOnly) { |
| 590 | for (const auto &R : GlobalResolutions) { |
| 591 | if (R.second.IRName.empty()) |
| 592 | continue; |
| 593 | if (R.second.Partition != 0 && |
| 594 | R.second.Partition != GlobalResolution::External) |
| 595 | continue; |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 596 | |
Mehdi Amini | d310b47 | 2016-08-22 06:25:41 +0000 | [diff] [blame] | 597 | GlobalValue *GV = |
| 598 | RegularLTO.CombinedModule->getNamedValue(R.second.IRName); |
| 599 | // Ignore symbols defined in other partitions. |
| 600 | if (!GV || GV->hasLocalLinkage()) |
| 601 | continue; |
| 602 | GV->setUnnamedAddr(R.second.UnnamedAddr ? GlobalValue::UnnamedAddr::Global |
| 603 | : GlobalValue::UnnamedAddr::None); |
| 604 | if (R.second.Partition == 0) |
| 605 | GV->setLinkage(GlobalValue::InternalLinkage); |
| 606 | } |
| 607 | |
| 608 | if (Conf.PostInternalizeModuleHook && |
| 609 | !Conf.PostInternalizeModuleHook(0, *RegularLTO.CombinedModule)) |
Mehdi Amini | 41af430 | 2016-11-11 04:28:40 +0000 | [diff] [blame] | 610 | return Error::success(); |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 611 | } |
Peter Collingbourne | 80186a5 | 2016-09-23 21:33:43 +0000 | [diff] [blame] | 612 | return backend(Conf, AddStream, RegularLTO.ParallelCodeGenParallelismLevel, |
Peter Collingbourne | e02b74e | 2017-01-20 22:18:52 +0000 | [diff] [blame] | 613 | std::move(RegularLTO.CombinedModule), ThinLTO.CombinedIndex); |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 614 | } |
| 615 | |
| 616 | /// This class defines the interface to the ThinLTO backend. |
| 617 | class lto::ThinBackendProc { |
| 618 | protected: |
| 619 | Config &Conf; |
| 620 | ModuleSummaryIndex &CombinedIndex; |
Mehdi Amini | 767e145 | 2016-09-06 03:23:45 +0000 | [diff] [blame] | 621 | const StringMap<GVSummaryMapTy> &ModuleToDefinedGVSummaries; |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 622 | |
| 623 | public: |
| 624 | ThinBackendProc(Config &Conf, ModuleSummaryIndex &CombinedIndex, |
Mehdi Amini | 767e145 | 2016-09-06 03:23:45 +0000 | [diff] [blame] | 625 | const StringMap<GVSummaryMapTy> &ModuleToDefinedGVSummaries) |
Mehdi Amini | 18b9111 | 2016-08-19 06:10:03 +0000 | [diff] [blame] | 626 | : Conf(Conf), CombinedIndex(CombinedIndex), |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 627 | ModuleToDefinedGVSummaries(ModuleToDefinedGVSummaries) {} |
| 628 | |
| 629 | virtual ~ThinBackendProc() {} |
Mehdi Amini | adc0e26 | 2016-08-23 21:30:12 +0000 | [diff] [blame] | 630 | virtual Error start( |
Peter Collingbourne | 1a0720e | 2016-12-14 01:17:59 +0000 | [diff] [blame] | 631 | unsigned Task, BitcodeModule BM, |
Mehdi Amini | adc0e26 | 2016-08-23 21:30:12 +0000 | [diff] [blame] | 632 | const FunctionImporter::ImportMapTy &ImportList, |
| 633 | const FunctionImporter::ExportSetTy &ExportList, |
| 634 | const std::map<GlobalValue::GUID, GlobalValue::LinkageTypes> &ResolvedODR, |
Peter Collingbourne | 1a0720e | 2016-12-14 01:17:59 +0000 | [diff] [blame] | 635 | MapVector<StringRef, BitcodeModule> &ModuleMap) = 0; |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 636 | virtual Error wait() = 0; |
| 637 | }; |
| 638 | |
Benjamin Kramer | ffd3715 | 2016-11-19 20:44:26 +0000 | [diff] [blame] | 639 | namespace { |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 640 | class InProcessThinBackend : public ThinBackendProc { |
| 641 | ThreadPool BackendThreadPool; |
Peter Collingbourne | 80186a5 | 2016-09-23 21:33:43 +0000 | [diff] [blame] | 642 | AddStreamFn AddStream; |
| 643 | NativeObjectCache Cache; |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 644 | |
| 645 | Optional<Error> Err; |
| 646 | std::mutex ErrMu; |
| 647 | |
| 648 | public: |
Mehdi Amini | 767e145 | 2016-09-06 03:23:45 +0000 | [diff] [blame] | 649 | InProcessThinBackend( |
| 650 | Config &Conf, ModuleSummaryIndex &CombinedIndex, |
| 651 | unsigned ThinLTOParallelismLevel, |
| 652 | const StringMap<GVSummaryMapTy> &ModuleToDefinedGVSummaries, |
Peter Collingbourne | 80186a5 | 2016-09-23 21:33:43 +0000 | [diff] [blame] | 653 | AddStreamFn AddStream, NativeObjectCache Cache) |
Mehdi Amini | 18b9111 | 2016-08-19 06:10:03 +0000 | [diff] [blame] | 654 | : ThinBackendProc(Conf, CombinedIndex, ModuleToDefinedGVSummaries), |
| 655 | BackendThreadPool(ThinLTOParallelismLevel), |
Peter Collingbourne | 80186a5 | 2016-09-23 21:33:43 +0000 | [diff] [blame] | 656 | AddStream(std::move(AddStream)), Cache(std::move(Cache)) {} |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 657 | |
Mehdi Amini | adc0e26 | 2016-08-23 21:30:12 +0000 | [diff] [blame] | 658 | Error runThinLTOBackendThread( |
Peter Collingbourne | 80186a5 | 2016-09-23 21:33:43 +0000 | [diff] [blame] | 659 | AddStreamFn AddStream, NativeObjectCache Cache, unsigned Task, |
Peter Collingbourne | 1a0720e | 2016-12-14 01:17:59 +0000 | [diff] [blame] | 660 | BitcodeModule BM, ModuleSummaryIndex &CombinedIndex, |
Mehdi Amini | adc0e26 | 2016-08-23 21:30:12 +0000 | [diff] [blame] | 661 | const FunctionImporter::ImportMapTy &ImportList, |
| 662 | const FunctionImporter::ExportSetTy &ExportList, |
| 663 | const std::map<GlobalValue::GUID, GlobalValue::LinkageTypes> &ResolvedODR, |
| 664 | const GVSummaryMapTy &DefinedGlobals, |
Peter Collingbourne | 1a0720e | 2016-12-14 01:17:59 +0000 | [diff] [blame] | 665 | MapVector<StringRef, BitcodeModule> &ModuleMap) { |
Peter Collingbourne | 80186a5 | 2016-09-23 21:33:43 +0000 | [diff] [blame] | 666 | auto RunThinBackend = [&](AddStreamFn AddStream) { |
| 667 | LTOLLVMContext BackendContext(Conf); |
Peter Collingbourne | 1a0720e | 2016-12-14 01:17:59 +0000 | [diff] [blame] | 668 | Expected<std::unique_ptr<Module>> MOrErr = BM.parseModule(BackendContext); |
Peter Collingbourne | d9445c4 | 2016-11-13 07:00:17 +0000 | [diff] [blame] | 669 | if (!MOrErr) |
| 670 | return MOrErr.takeError(); |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 671 | |
Peter Collingbourne | 80186a5 | 2016-09-23 21:33:43 +0000 | [diff] [blame] | 672 | return thinBackend(Conf, Task, AddStream, **MOrErr, CombinedIndex, |
| 673 | ImportList, DefinedGlobals, ModuleMap); |
Mehdi Amini | adc0e26 | 2016-08-23 21:30:12 +0000 | [diff] [blame] | 674 | }; |
Peter Collingbourne | 80186a5 | 2016-09-23 21:33:43 +0000 | [diff] [blame] | 675 | |
Peter Collingbourne | 1a0720e | 2016-12-14 01:17:59 +0000 | [diff] [blame] | 676 | auto ModuleID = BM.getModuleIdentifier(); |
Mehdi Amini | f82bda0 | 2016-10-08 04:44:23 +0000 | [diff] [blame] | 677 | |
| 678 | if (!Cache || !CombinedIndex.modulePaths().count(ModuleID) || |
| 679 | all_of(CombinedIndex.getModuleHash(ModuleID), |
| 680 | [](uint32_t V) { return V == 0; })) |
| 681 | // Cache disabled or no entry for this module in the combined index or |
| 682 | // no module hash. |
Peter Collingbourne | 80186a5 | 2016-09-23 21:33:43 +0000 | [diff] [blame] | 683 | return RunThinBackend(AddStream); |
| 684 | |
| 685 | SmallString<40> Key; |
| 686 | // The module may be cached, this helps handling it. |
Peter Collingbourne | f425752 | 2016-12-08 05:28:30 +0000 | [diff] [blame] | 687 | computeCacheKey(Key, Conf, CombinedIndex, ModuleID, ImportList, ExportList, |
Mehdi Amini | 00fa140 | 2016-10-08 04:44:18 +0000 | [diff] [blame] | 688 | ResolvedODR, DefinedGlobals); |
Peter Collingbourne | 80186a5 | 2016-09-23 21:33:43 +0000 | [diff] [blame] | 689 | if (AddStreamFn CacheAddStream = Cache(Task, Key)) |
| 690 | return RunThinBackend(CacheAddStream); |
| 691 | |
Mehdi Amini | 41af430 | 2016-11-11 04:28:40 +0000 | [diff] [blame] | 692 | return Error::success(); |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 693 | } |
| 694 | |
Mehdi Amini | adc0e26 | 2016-08-23 21:30:12 +0000 | [diff] [blame] | 695 | Error start( |
Peter Collingbourne | 1a0720e | 2016-12-14 01:17:59 +0000 | [diff] [blame] | 696 | unsigned Task, BitcodeModule BM, |
Mehdi Amini | adc0e26 | 2016-08-23 21:30:12 +0000 | [diff] [blame] | 697 | const FunctionImporter::ImportMapTy &ImportList, |
| 698 | const FunctionImporter::ExportSetTy &ExportList, |
| 699 | const std::map<GlobalValue::GUID, GlobalValue::LinkageTypes> &ResolvedODR, |
Peter Collingbourne | 1a0720e | 2016-12-14 01:17:59 +0000 | [diff] [blame] | 700 | MapVector<StringRef, BitcodeModule> &ModuleMap) override { |
| 701 | StringRef ModulePath = BM.getModuleIdentifier(); |
Mehdi Amini | 767e145 | 2016-09-06 03:23:45 +0000 | [diff] [blame] | 702 | assert(ModuleToDefinedGVSummaries.count(ModulePath)); |
| 703 | const GVSummaryMapTy &DefinedGlobals = |
| 704 | ModuleToDefinedGVSummaries.find(ModulePath)->second; |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 705 | BackendThreadPool.async( |
Peter Collingbourne | 1a0720e | 2016-12-14 01:17:59 +0000 | [diff] [blame] | 706 | [=](BitcodeModule BM, ModuleSummaryIndex &CombinedIndex, |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 707 | const FunctionImporter::ImportMapTy &ImportList, |
Mehdi Amini | adc0e26 | 2016-08-23 21:30:12 +0000 | [diff] [blame] | 708 | const FunctionImporter::ExportSetTy &ExportList, |
| 709 | const std::map<GlobalValue::GUID, GlobalValue::LinkageTypes> |
| 710 | &ResolvedODR, |
Mehdi Amini | 767e145 | 2016-09-06 03:23:45 +0000 | [diff] [blame] | 711 | const GVSummaryMapTy &DefinedGlobals, |
Peter Collingbourne | 1a0720e | 2016-12-14 01:17:59 +0000 | [diff] [blame] | 712 | MapVector<StringRef, BitcodeModule> &ModuleMap) { |
Mehdi Amini | adc0e26 | 2016-08-23 21:30:12 +0000 | [diff] [blame] | 713 | Error E = runThinLTOBackendThread( |
Peter Collingbourne | 1a0720e | 2016-12-14 01:17:59 +0000 | [diff] [blame] | 714 | AddStream, Cache, Task, BM, CombinedIndex, ImportList, |
Peter Collingbourne | 80186a5 | 2016-09-23 21:33:43 +0000 | [diff] [blame] | 715 | ExportList, ResolvedODR, DefinedGlobals, ModuleMap); |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 716 | if (E) { |
| 717 | std::unique_lock<std::mutex> L(ErrMu); |
| 718 | if (Err) |
| 719 | Err = joinErrors(std::move(*Err), std::move(E)); |
| 720 | else |
| 721 | Err = std::move(E); |
| 722 | } |
| 723 | }, |
Peter Collingbourne | 1a0720e | 2016-12-14 01:17:59 +0000 | [diff] [blame] | 724 | BM, std::ref(CombinedIndex), std::ref(ImportList), |
Mehdi Amini | 767e145 | 2016-09-06 03:23:45 +0000 | [diff] [blame] | 725 | std::ref(ExportList), std::ref(ResolvedODR), std::ref(DefinedGlobals), |
| 726 | std::ref(ModuleMap)); |
Mehdi Amini | 41af430 | 2016-11-11 04:28:40 +0000 | [diff] [blame] | 727 | return Error::success(); |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 728 | } |
| 729 | |
| 730 | Error wait() override { |
| 731 | BackendThreadPool.wait(); |
| 732 | if (Err) |
| 733 | return std::move(*Err); |
| 734 | else |
Mehdi Amini | 41af430 | 2016-11-11 04:28:40 +0000 | [diff] [blame] | 735 | return Error::success(); |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 736 | } |
| 737 | }; |
Benjamin Kramer | ffd3715 | 2016-11-19 20:44:26 +0000 | [diff] [blame] | 738 | } // end anonymous namespace |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 739 | |
| 740 | ThinBackend lto::createInProcessThinBackend(unsigned ParallelismLevel) { |
| 741 | return [=](Config &Conf, ModuleSummaryIndex &CombinedIndex, |
Mehdi Amini | 767e145 | 2016-09-06 03:23:45 +0000 | [diff] [blame] | 742 | const StringMap<GVSummaryMapTy> &ModuleToDefinedGVSummaries, |
Peter Collingbourne | 80186a5 | 2016-09-23 21:33:43 +0000 | [diff] [blame] | 743 | AddStreamFn AddStream, NativeObjectCache Cache) { |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 744 | return llvm::make_unique<InProcessThinBackend>( |
| 745 | Conf, CombinedIndex, ParallelismLevel, ModuleToDefinedGVSummaries, |
Peter Collingbourne | 80186a5 | 2016-09-23 21:33:43 +0000 | [diff] [blame] | 746 | AddStream, Cache); |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 747 | }; |
| 748 | } |
| 749 | |
Teresa Johnson | 3f212b8 | 2016-09-21 19:12:05 +0000 | [diff] [blame] | 750 | // Given the original \p Path to an output file, replace any path |
| 751 | // prefix matching \p OldPrefix with \p NewPrefix. Also, create the |
| 752 | // resulting directory if it does not yet exist. |
| 753 | std::string lto::getThinLTOOutputFile(const std::string &Path, |
| 754 | const std::string &OldPrefix, |
| 755 | const std::string &NewPrefix) { |
| 756 | if (OldPrefix.empty() && NewPrefix.empty()) |
| 757 | return Path; |
| 758 | SmallString<128> NewPath(Path); |
| 759 | llvm::sys::path::replace_path_prefix(NewPath, OldPrefix, NewPrefix); |
| 760 | StringRef ParentPath = llvm::sys::path::parent_path(NewPath.str()); |
| 761 | if (!ParentPath.empty()) { |
| 762 | // Make sure the new directory exists, creating it if necessary. |
| 763 | if (std::error_code EC = llvm::sys::fs::create_directories(ParentPath)) |
| 764 | llvm::errs() << "warning: could not create directory '" << ParentPath |
| 765 | << "': " << EC.message() << '\n'; |
| 766 | } |
| 767 | return NewPath.str(); |
| 768 | } |
| 769 | |
Benjamin Kramer | ffd3715 | 2016-11-19 20:44:26 +0000 | [diff] [blame] | 770 | namespace { |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 771 | class WriteIndexesThinBackend : public ThinBackendProc { |
| 772 | std::string OldPrefix, NewPrefix; |
| 773 | bool ShouldEmitImportsFiles; |
| 774 | |
| 775 | std::string LinkedObjectsFileName; |
| 776 | std::unique_ptr<llvm::raw_fd_ostream> LinkedObjectsFile; |
| 777 | |
| 778 | public: |
Mehdi Amini | 767e145 | 2016-09-06 03:23:45 +0000 | [diff] [blame] | 779 | WriteIndexesThinBackend( |
| 780 | Config &Conf, ModuleSummaryIndex &CombinedIndex, |
| 781 | const StringMap<GVSummaryMapTy> &ModuleToDefinedGVSummaries, |
| 782 | std::string OldPrefix, std::string NewPrefix, bool ShouldEmitImportsFiles, |
| 783 | std::string LinkedObjectsFileName) |
Mehdi Amini | 18b9111 | 2016-08-19 06:10:03 +0000 | [diff] [blame] | 784 | : ThinBackendProc(Conf, CombinedIndex, ModuleToDefinedGVSummaries), |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 785 | OldPrefix(OldPrefix), NewPrefix(NewPrefix), |
| 786 | ShouldEmitImportsFiles(ShouldEmitImportsFiles), |
| 787 | LinkedObjectsFileName(LinkedObjectsFileName) {} |
| 788 | |
Mehdi Amini | adc0e26 | 2016-08-23 21:30:12 +0000 | [diff] [blame] | 789 | Error start( |
Peter Collingbourne | 1a0720e | 2016-12-14 01:17:59 +0000 | [diff] [blame] | 790 | unsigned Task, BitcodeModule BM, |
Mehdi Amini | adc0e26 | 2016-08-23 21:30:12 +0000 | [diff] [blame] | 791 | const FunctionImporter::ImportMapTy &ImportList, |
| 792 | const FunctionImporter::ExportSetTy &ExportList, |
| 793 | const std::map<GlobalValue::GUID, GlobalValue::LinkageTypes> &ResolvedODR, |
Peter Collingbourne | 1a0720e | 2016-12-14 01:17:59 +0000 | [diff] [blame] | 794 | MapVector<StringRef, BitcodeModule> &ModuleMap) override { |
| 795 | StringRef ModulePath = BM.getModuleIdentifier(); |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 796 | std::string NewModulePath = |
| 797 | getThinLTOOutputFile(ModulePath, OldPrefix, NewPrefix); |
| 798 | |
| 799 | std::error_code EC; |
| 800 | if (!LinkedObjectsFileName.empty()) { |
| 801 | if (!LinkedObjectsFile) { |
| 802 | LinkedObjectsFile = llvm::make_unique<raw_fd_ostream>( |
| 803 | LinkedObjectsFileName, EC, sys::fs::OpenFlags::F_None); |
| 804 | if (EC) |
| 805 | return errorCodeToError(EC); |
| 806 | } |
| 807 | *LinkedObjectsFile << NewModulePath << '\n'; |
| 808 | } |
| 809 | |
| 810 | std::map<std::string, GVSummaryMapTy> ModuleToSummariesForIndex; |
| 811 | gatherImportedSummariesForModule(ModulePath, ModuleToDefinedGVSummaries, |
Mehdi Amini | cdbcbf7 | 2016-08-16 05:46:05 +0000 | [diff] [blame] | 812 | ImportList, ModuleToSummariesForIndex); |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 813 | |
| 814 | raw_fd_ostream OS(NewModulePath + ".thinlto.bc", EC, |
| 815 | sys::fs::OpenFlags::F_None); |
| 816 | if (EC) |
| 817 | return errorCodeToError(EC); |
| 818 | WriteIndexToFile(CombinedIndex, OS, &ModuleToSummariesForIndex); |
| 819 | |
| 820 | if (ShouldEmitImportsFiles) |
Mehdi Amini | cdbcbf7 | 2016-08-16 05:46:05 +0000 | [diff] [blame] | 821 | return errorCodeToError( |
| 822 | EmitImportsFiles(ModulePath, NewModulePath + ".imports", ImportList)); |
Mehdi Amini | 41af430 | 2016-11-11 04:28:40 +0000 | [diff] [blame] | 823 | return Error::success(); |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 824 | } |
| 825 | |
Mehdi Amini | 41af430 | 2016-11-11 04:28:40 +0000 | [diff] [blame] | 826 | Error wait() override { return Error::success(); } |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 827 | }; |
Benjamin Kramer | ffd3715 | 2016-11-19 20:44:26 +0000 | [diff] [blame] | 828 | } // end anonymous namespace |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 829 | |
| 830 | ThinBackend lto::createWriteIndexesThinBackend(std::string OldPrefix, |
| 831 | std::string NewPrefix, |
| 832 | bool ShouldEmitImportsFiles, |
| 833 | std::string LinkedObjectsFile) { |
| 834 | return [=](Config &Conf, ModuleSummaryIndex &CombinedIndex, |
Mehdi Amini | 767e145 | 2016-09-06 03:23:45 +0000 | [diff] [blame] | 835 | const StringMap<GVSummaryMapTy> &ModuleToDefinedGVSummaries, |
Peter Collingbourne | 80186a5 | 2016-09-23 21:33:43 +0000 | [diff] [blame] | 836 | AddStreamFn AddStream, NativeObjectCache Cache) { |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 837 | return llvm::make_unique<WriteIndexesThinBackend>( |
Mehdi Amini | 18b9111 | 2016-08-19 06:10:03 +0000 | [diff] [blame] | 838 | Conf, CombinedIndex, ModuleToDefinedGVSummaries, OldPrefix, NewPrefix, |
| 839 | ShouldEmitImportsFiles, LinkedObjectsFile); |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 840 | }; |
| 841 | } |
| 842 | |
Peter Collingbourne | 80186a5 | 2016-09-23 21:33:43 +0000 | [diff] [blame] | 843 | Error LTO::runThinLTO(AddStreamFn AddStream, NativeObjectCache Cache, |
| 844 | bool HasRegularLTO) { |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 845 | if (ThinLTO.ModuleMap.empty()) |
Mehdi Amini | 41af430 | 2016-11-11 04:28:40 +0000 | [diff] [blame] | 846 | return Error::success(); |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 847 | |
| 848 | if (Conf.CombinedIndexHook && !Conf.CombinedIndexHook(ThinLTO.CombinedIndex)) |
Mehdi Amini | 41af430 | 2016-11-11 04:28:40 +0000 | [diff] [blame] | 849 | return Error::success(); |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 850 | |
| 851 | // Collect for each module the list of function it defines (GUID -> |
| 852 | // Summary). |
| 853 | StringMap<std::map<GlobalValue::GUID, GlobalValueSummary *>> |
| 854 | ModuleToDefinedGVSummaries(ThinLTO.ModuleMap.size()); |
| 855 | ThinLTO.CombinedIndex.collectDefinedGVSummariesPerModule( |
| 856 | ModuleToDefinedGVSummaries); |
Teresa Johnson | 620c140 | 2016-09-20 23:07:17 +0000 | [diff] [blame] | 857 | // Create entries for any modules that didn't have any GV summaries |
| 858 | // (either they didn't have any GVs to start with, or we suppressed |
| 859 | // generation of the summaries because they e.g. had inline assembly |
| 860 | // uses that couldn't be promoted/renamed on export). This is so |
| 861 | // InProcessThinBackend::start can still launch a backend thread, which |
| 862 | // is passed the map of summaries for the module, without any special |
| 863 | // handling for this case. |
| 864 | for (auto &Mod : ThinLTO.ModuleMap) |
| 865 | if (!ModuleToDefinedGVSummaries.count(Mod.first)) |
| 866 | ModuleToDefinedGVSummaries.try_emplace(Mod.first); |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 867 | |
| 868 | StringMap<FunctionImporter::ImportMapTy> ImportLists( |
| 869 | ThinLTO.ModuleMap.size()); |
| 870 | StringMap<FunctionImporter::ExportSetTy> ExportLists( |
| 871 | ThinLTO.ModuleMap.size()); |
Mehdi Amini | adc0e26 | 2016-08-23 21:30:12 +0000 | [diff] [blame] | 872 | StringMap<std::map<GlobalValue::GUID, GlobalValue::LinkageTypes>> ResolvedODR; |
Mehdi Amini | adc0e26 | 2016-08-23 21:30:12 +0000 | [diff] [blame] | 873 | |
Teresa Johnson | 002af9b | 2016-10-31 22:12:21 +0000 | [diff] [blame] | 874 | if (Conf.OptLevel > 0) { |
Mehdi Amini | f39ce99 | 2017-01-20 23:34:12 +0000 | [diff] [blame] | 875 | // Compute "dead" symbols, we don't want to import/export these! |
| 876 | DenseSet<GlobalValue::GUID> GUIDPreservedSymbols; |
| 877 | for (auto &Res : GlobalResolutions) { |
| 878 | if (Res.second.VisibleOutsideThinLTO && |
| 879 | // IRName will be defined if we have seen the prevailing copy of |
| 880 | // this value. If not, no need to preserve any ThinLTO copies. |
| 881 | !Res.second.IRName.empty()) |
| 882 | GUIDPreservedSymbols.insert(GlobalValue::getGUID(Res.second.IRName)); |
| 883 | } |
| 884 | |
| 885 | auto DeadSymbols = |
| 886 | computeDeadSymbols(ThinLTO.CombinedIndex, GUIDPreservedSymbols); |
| 887 | |
Teresa Johnson | 002af9b | 2016-10-31 22:12:21 +0000 | [diff] [blame] | 888 | ComputeCrossModuleImport(ThinLTO.CombinedIndex, ModuleToDefinedGVSummaries, |
Teresa Johnson | 6c475a7 | 2017-01-05 21:34:18 +0000 | [diff] [blame] | 889 | ImportLists, ExportLists, &DeadSymbols); |
Teresa Johnson | 002af9b | 2016-10-31 22:12:21 +0000 | [diff] [blame] | 890 | |
| 891 | std::set<GlobalValue::GUID> ExportedGUIDs; |
| 892 | for (auto &Res : GlobalResolutions) { |
Teresa Johnson | 6c475a7 | 2017-01-05 21:34:18 +0000 | [diff] [blame] | 893 | // First check if the symbol was flagged as having external references. |
| 894 | if (Res.second.Partition != GlobalResolution::External) |
| 895 | continue; |
| 896 | // IRName will be defined if we have seen the prevailing copy of |
| 897 | // this value. If not, no need to mark as exported from a ThinLTO |
| 898 | // partition (and we can't get the GUID). |
| 899 | if (Res.second.IRName.empty()) |
| 900 | continue; |
| 901 | auto GUID = GlobalValue::getGUID(Res.second.IRName); |
| 902 | // Mark exported unless index-based analysis determined it to be dead. |
| 903 | if (!DeadSymbols.count(GUID)) |
Teresa Johnson | 002af9b | 2016-10-31 22:12:21 +0000 | [diff] [blame] | 904 | ExportedGUIDs.insert(GlobalValue::getGUID(Res.second.IRName)); |
| 905 | } |
| 906 | |
| 907 | auto isPrevailing = [&](GlobalValue::GUID GUID, |
| 908 | const GlobalValueSummary *S) { |
| 909 | return ThinLTO.PrevailingModuleForGUID[GUID] == S->modulePath(); |
| 910 | }; |
| 911 | auto isExported = [&](StringRef ModuleIdentifier, GlobalValue::GUID GUID) { |
| 912 | const auto &ExportList = ExportLists.find(ModuleIdentifier); |
| 913 | return (ExportList != ExportLists.end() && |
| 914 | ExportList->second.count(GUID)) || |
| 915 | ExportedGUIDs.count(GUID); |
| 916 | }; |
| 917 | thinLTOInternalizeAndPromoteInIndex(ThinLTO.CombinedIndex, isExported); |
| 918 | |
| 919 | auto recordNewLinkage = [&](StringRef ModuleIdentifier, |
| 920 | GlobalValue::GUID GUID, |
| 921 | GlobalValue::LinkageTypes NewLinkage) { |
| 922 | ResolvedODR[ModuleIdentifier][GUID] = NewLinkage; |
| 923 | }; |
| 924 | |
| 925 | thinLTOResolveWeakForLinkerInIndex(ThinLTO.CombinedIndex, isPrevailing, |
| 926 | recordNewLinkage); |
| 927 | } |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 928 | |
Peter Collingbourne | 80186a5 | 2016-09-23 21:33:43 +0000 | [diff] [blame] | 929 | std::unique_ptr<ThinBackendProc> BackendProc = |
| 930 | ThinLTO.Backend(Conf, ThinLTO.CombinedIndex, ModuleToDefinedGVSummaries, |
| 931 | AddStream, Cache); |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 932 | |
Davide Italiano | 6309895 | 2017-01-04 20:37:57 +0000 | [diff] [blame] | 933 | // Task numbers start at ParallelCodeGenParallelismLevel if an LTO |
| 934 | // module is present, as tasks 0 through ParallelCodeGenParallelismLevel-1 |
| 935 | // are reserved for parallel code generation partitions. |
Teresa Johnson | 8dd61ae | 2016-09-16 13:54:19 +0000 | [diff] [blame] | 936 | unsigned Task = |
| 937 | HasRegularLTO ? RegularLTO.ParallelCodeGenParallelismLevel : 0; |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 938 | for (auto &Mod : ThinLTO.ModuleMap) { |
Mehdi Amini | cdbcbf7 | 2016-08-16 05:46:05 +0000 | [diff] [blame] | 939 | if (Error E = BackendProc->start(Task, Mod.second, ImportLists[Mod.first], |
Mehdi Amini | adc0e26 | 2016-08-23 21:30:12 +0000 | [diff] [blame] | 940 | ExportLists[Mod.first], |
| 941 | ResolvedODR[Mod.first], ThinLTO.ModuleMap)) |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 942 | return E; |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 943 | ++Task; |
Teresa Johnson | 9ba95f9 | 2016-08-11 14:58:12 +0000 | [diff] [blame] | 944 | } |
| 945 | |
| 946 | return BackendProc->wait(); |
Teresa Johnson | df6edc5 | 2016-05-23 22:54:06 +0000 | [diff] [blame] | 947 | } |