Rui Ueyama | 2599248 | 2016-03-22 20:52:10 | [diff] [blame] | 1 | //===- LTO.cpp ------------------------------------------------------------===// |
| 2 | // |
| 3 | // The LLVM Linker |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
| 10 | #include "LTO.h" |
| 11 | #include "Config.h" |
Rui Ueyama | 2599248 | 2016-03-22 20:52:10 | [diff] [blame] | 12 | #include "InputFiles.h" |
Rui Ueyama | 3f85170 | 2017-10-02 21:00:41 | [diff] [blame] | 13 | #include "LinkerScript.h" |
Rafael Espindola | 4b075bb | 2017-07-26 23:39:10 | [diff] [blame] | 14 | #include "SymbolTable.h" |
Rui Ueyama | 2599248 | 2016-03-22 20:52:10 | [diff] [blame] | 15 | #include "Symbols.h" |
Bob Haarman | b8a59c8 | 2017-10-25 22:28:38 | [diff] [blame] | 16 | #include "lld/Common/ErrorHandler.h" |
Rui Ueyama | 3f85170 | 2017-10-02 21:00:41 | [diff] [blame] | 17 | #include "lld/Common/TargetOptionsCommandFlags.h" |
Eugene Zelenko | 22886a2 | 2016-11-05 01:00:56 | [diff] [blame] | 18 | #include "llvm/ADT/STLExtras.h" |
Rui Ueyama | 8c6a5aa | 2016-11-05 22:37:59 | [diff] [blame] | 19 | #include "llvm/ADT/SmallString.h" |
Eugene Zelenko | 22886a2 | 2016-11-05 01:00:56 | [diff] [blame] | 20 | #include "llvm/ADT/StringRef.h" |
| 21 | #include "llvm/ADT/Twine.h" |
Zachary Turner | 264b5d9 | 2017-06-07 03:48:56 | [diff] [blame] | 22 | #include "llvm/BinaryFormat/ELF.h" |
Rumeet Dhindsa | d366e36 | 2018-05-02 21:40:07 | [diff] [blame] | 23 | #include "llvm/Bitcode/BitcodeReader.h" |
| 24 | #include "llvm/Bitcode/BitcodeWriter.h" |
Davide Italiano | 786d8e3 | 2016-09-29 00:40:08 | [diff] [blame] | 25 | #include "llvm/IR/DiagnosticPrinter.h" |
Peter Collingbourne | e02775f | 2017-03-01 23:00:10 | [diff] [blame] | 26 | #include "llvm/LTO/Caching.h" |
Eugene Zelenko | 22886a2 | 2016-11-05 01:00:56 | [diff] [blame] | 27 | #include "llvm/LTO/Config.h" |
Davide Italiano | 786d8e3 | 2016-09-29 00:40:08 | [diff] [blame] | 28 | #include "llvm/LTO/LTO.h" |
Eugene Zelenko | 22886a2 | 2016-11-05 01:00:56 | [diff] [blame] | 29 | #include "llvm/Object/SymbolicFile.h" |
| 30 | #include "llvm/Support/CodeGen.h" |
Eugene Zelenko | 22886a2 | 2016-11-05 01:00:56 | [diff] [blame] | 31 | #include "llvm/Support/Error.h" |
| 32 | #include "llvm/Support/FileSystem.h" |
| 33 | #include "llvm/Support/MemoryBuffer.h" |
Eugene Zelenko | 22886a2 | 2016-11-05 01:00:56 | [diff] [blame] | 34 | #include <algorithm> |
| 35 | #include <cstddef> |
| 36 | #include <memory> |
| 37 | #include <string> |
| 38 | #include <system_error> |
| 39 | #include <vector> |
Rui Ueyama | 2599248 | 2016-03-22 20:52:10 | [diff] [blame] | 40 | |
| 41 | using namespace llvm; |
| 42 | using namespace llvm::object; |
| 43 | using namespace llvm::ELF; |
| 44 | |
| 45 | using namespace lld; |
| 46 | using namespace lld::elf; |
| 47 | |
Rui Ueyama | 66a9f25 | 2018-05-07 17:46:28 | [diff] [blame] | 48 | // Creates an empty file to store a list of object files for final |
Rumeet Dhindsa | d366e36 | 2018-05-02 21:40:07 | [diff] [blame] | 49 | // linking of distributed ThinLTO. |
Rui Ueyama | 66a9f25 | 2018-05-07 17:46:28 | [diff] [blame] | 50 | static std::unique_ptr<raw_fd_ostream> openFile(StringRef File) { |
Rumeet Dhindsa | d366e36 | 2018-05-02 21:40:07 | [diff] [blame] | 51 | std::error_code EC; |
Rui Ueyama | 66a9f25 | 2018-05-07 17:46:28 | [diff] [blame] | 52 | auto Ret = |
| 53 | llvm::make_unique<raw_fd_ostream>(File, EC, sys::fs::OpenFlags::F_None); |
Rui Ueyama | d54f1c2 | 2018-05-07 22:11:24 | [diff] [blame] | 54 | if (EC) { |
| 55 | error("cannot open " + File + ": " + EC.message()); |
| 56 | return nullptr; |
| 57 | } |
Rui Ueyama | 66a9f25 | 2018-05-07 17:46:28 | [diff] [blame] | 58 | return Ret; |
Rumeet Dhindsa | d366e36 | 2018-05-02 21:40:07 | [diff] [blame] | 59 | } |
| 60 | |
Rui Ueyama | d54f1c2 | 2018-05-07 22:11:24 | [diff] [blame] | 61 | static std::string getThinLTOOutputFile(StringRef ModulePath) { |
| 62 | return lto::getThinLTOOutputFile(ModulePath, |
| 63 | Config->ThinLTOPrefixReplace.first, |
Rumeet Dhindsa | 4fb5119 | 2018-05-07 23:14:12 | [diff] [blame] | 64 | Config->ThinLTOPrefixReplace.second); |
Rui Ueyama | d54f1c2 | 2018-05-07 22:11:24 | [diff] [blame] | 65 | } |
| 66 | |
Rui Ueyama | 397dffd | 2018-05-07 23:24:07 | [diff] [blame] | 67 | static lto::Config createConfig() { |
| 68 | lto::Config C; |
Davide Italiano | d26c4a1 | 2016-05-15 19:29:38 | [diff] [blame] | 69 | |
Peter Collingbourne | 82a7f14 | 2018-08-06 20:12:12 | [diff] [blame] | 70 | // LLD supports the new relocations and address-significance tables. |
Rui Ueyama | 397dffd | 2018-05-07 23:24:07 | [diff] [blame] | 71 | C.Options = InitTargetOptionsFromCodeGenFlags(); |
| 72 | C.Options.RelaxELFRelocations = true; |
Peter Collingbourne | 82a7f14 | 2018-08-06 20:12:12 | [diff] [blame] | 73 | C.Options.EmitAddrsig = true; |
Davide Italiano | df24d5b | 2016-06-02 22:58:11 | [diff] [blame] | 74 | |
Davide Italiano | 957f120 | 2017-07-24 20:15:07 | [diff] [blame] | 75 | // Always emit a section per function/datum with LTO. |
Rui Ueyama | 397dffd | 2018-05-07 23:24:07 | [diff] [blame] | 76 | C.Options.FunctionSections = true; |
| 77 | C.Options.DataSections = true; |
Davide Italiano | 1f4e29c | 2017-07-24 19:38:13 | [diff] [blame] | 78 | |
Evgeniy Stepanov | f12ac5b | 2017-05-22 21:11:44 | [diff] [blame] | 79 | if (Config->Relocatable) |
Rui Ueyama | 397dffd | 2018-05-07 23:24:07 | [diff] [blame] | 80 | C.RelocModel = None; |
Evgeniy Stepanov | f12ac5b | 2017-05-22 21:11:44 | [diff] [blame] | 81 | else if (Config->Pic) |
Rui Ueyama | 397dffd | 2018-05-07 23:24:07 | [diff] [blame] | 82 | C.RelocModel = Reloc::PIC_; |
Evgeniy Stepanov | f12ac5b | 2017-05-22 21:11:44 | [diff] [blame] | 83 | else |
Rui Ueyama | 397dffd | 2018-05-07 23:24:07 | [diff] [blame] | 84 | C.RelocModel = Reloc::Static; |
| 85 | |
| 86 | C.CodeModel = GetCodeModelFromCMModel(); |
| 87 | C.DisableVerify = Config->DisableVerify; |
| 88 | C.DiagHandler = diagnosticHandler; |
| 89 | C.OptLevel = Config->LTOO; |
| 90 | C.CPU = GetCPUStr(); |
Fangrui Song | ccfc841 | 2018-11-01 20:02:49 | [diff] [blame] | 91 | C.MAttrs = GetMAttrs(); |
Davide Italiano | df24d5b | 2016-06-02 22:58:11 | [diff] [blame] | 92 | |
Davide Italiano | 786d8e3 | 2016-09-29 00:40:08 | [diff] [blame] | 93 | // Set up a custom pipeline if we've been asked to. |
Rui Ueyama | 397dffd | 2018-05-07 23:24:07 | [diff] [blame] | 94 | C.OptPipeline = Config->LTONewPmPasses; |
| 95 | C.AAPipeline = Config->LTOAAPipeline; |
Rui Ueyama | 2599248 | 2016-03-22 20:52:10 | [diff] [blame] | 96 | |
Davide Italiano | db4b0a7 | 2017-02-13 17:49:18 | [diff] [blame] | 97 | // Set up optimization remarks if we've been asked to. |
Rui Ueyama | 397dffd | 2018-05-07 23:24:07 | [diff] [blame] | 98 | C.RemarksFilename = Config->OptRemarksFilename; |
| 99 | C.RemarksWithHotness = Config->OptRemarksWithHotness; |
| 100 | |
| 101 | C.SampleProfile = Config->LTOSampleProfile; |
| 102 | C.UseNewPM = Config->LTONewPassManager; |
| 103 | C.DebugPassManager = Config->LTODebugPassManager; |
Yunlian Jiang | 496fb3e | 2018-07-16 17:55:48 | [diff] [blame] | 104 | C.DwoDir = Config->DwoDir; |
Davide Italiano | db4b0a7 | 2017-02-13 17:49:18 | [diff] [blame] | 105 | |
Rui Ueyama | 9f49990 | 2018-12-14 21:58:49 | [diff] [blame] | 106 | if (Config->EmitLLVM) { |
| 107 | C.PostInternalizeModuleHook = [](size_t Task, const Module &M) { |
| 108 | if (std::unique_ptr<raw_fd_ostream> OS = openFile(Config->OutputFile)) |
| 109 | WriteBitcodeToFile(M, *OS, false); |
| 110 | return false; |
| 111 | }; |
| 112 | } |
| 113 | |
Rui Ueyama | 2599248 | 2016-03-22 20:52:10 | [diff] [blame] | 114 | if (Config->SaveTemps) |
Rui Ueyama | 397dffd | 2018-05-07 23:24:07 | [diff] [blame] | 115 | checkError(C.addSaveTemps(Config->OutputFile.str() + ".", |
| 116 | /*UseInputModulePath*/ true)); |
| 117 | return C; |
| 118 | } |
Davide Italiano | 786d8e3 | 2016-09-29 00:40:08 | [diff] [blame] | 119 | |
Rui Ueyama | 397dffd | 2018-05-07 23:24:07 | [diff] [blame] | 120 | BitcodeCompiler::BitcodeCompiler() { |
Rui Ueyama | 98e4a5c | 2018-09-11 14:37:27 | [diff] [blame] | 121 | // Initialize IndexFile. |
| 122 | if (!Config->ThinLTOIndexOnlyArg.empty()) |
| 123 | IndexFile = openFile(Config->ThinLTOIndexOnlyArg); |
| 124 | |
Rui Ueyama | 397dffd | 2018-05-07 23:24:07 | [diff] [blame] | 125 | // Initialize LTOObj. |
Davide Italiano | 7a7b35a | 2016-10-10 18:12:53 | [diff] [blame] | 126 | lto::ThinBackend Backend; |
Rumeet Dhindsa | d366e36 | 2018-05-02 21:40:07 | [diff] [blame] | 127 | if (Config->ThinLTOIndexOnly) { |
Rui Ueyama | 98e4a5c | 2018-09-11 14:37:27 | [diff] [blame] | 128 | auto OnIndexWrite = [&](StringRef S) { ThinIndices.erase(S); }; |
Rui Ueyama | 4454b3d | 2018-05-07 17:59:43 | [diff] [blame] | 129 | Backend = lto::createWriteIndexesThinBackend( |
| 130 | Config->ThinLTOPrefixReplace.first, Config->ThinLTOPrefixReplace.second, |
Rumeet Dhindsa | 1888326 | 2018-05-08 20:12:07 | [diff] [blame] | 131 | Config->ThinLTOEmitImportsFiles, IndexFile.get(), OnIndexWrite); |
Rui Ueyama | 397dffd | 2018-05-07 23:24:07 | [diff] [blame] | 132 | } else if (Config->ThinLTOJobs != -1U) { |
| 133 | Backend = lto::createInProcessThinBackend(Config->ThinLTOJobs); |
Rumeet Dhindsa | d366e36 | 2018-05-02 21:40:07 | [diff] [blame] | 134 | } |
| 135 | |
Rui Ueyama | 397dffd | 2018-05-07 23:24:07 | [diff] [blame] | 136 | LTOObj = llvm::make_unique<lto::LTO>(createConfig(), Backend, |
Rui Ueyama | 66a9f25 | 2018-05-07 17:46:28 | [diff] [blame] | 137 | Config->LTOPartitions); |
Rui Ueyama | 2599248 | 2016-03-22 20:52:10 | [diff] [blame] | 138 | |
Rui Ueyama | 397dffd | 2018-05-07 23:24:07 | [diff] [blame] | 139 | // Initialize UsedStartStop. |
Rui Ueyama | f52496e | 2017-11-03 21:21:47 | [diff] [blame] | 140 | for (Symbol *Sym : Symtab->getSymbols()) { |
Rui Ueyama | 98e4a5c | 2018-09-11 14:37:27 | [diff] [blame] | 141 | StringRef S = Sym->getName(); |
Rafael Espindola | 4b075bb | 2017-07-26 23:39:10 | [diff] [blame] | 142 | for (StringRef Prefix : {"__start_", "__stop_"}) |
Rui Ueyama | 98e4a5c | 2018-09-11 14:37:27 | [diff] [blame] | 143 | if (S.startswith(Prefix)) |
| 144 | UsedStartStop.insert(S.substr(Prefix.size())); |
Rafael Espindola | 4b075bb | 2017-07-26 23:39:10 | [diff] [blame] | 145 | } |
| 146 | } |
Rafael Espindola | ae605c1 | 2016-04-21 20:35:25 | [diff] [blame] | 147 | |
Eugene Zelenko | 22886a2 | 2016-11-05 01:00:56 | [diff] [blame] | 148 | BitcodeCompiler::~BitcodeCompiler() = default; |
Rui Ueyama | 412c802b | 2016-04-22 21:16:18 | [diff] [blame] | 149 | |
Rui Ueyama | f52496e | 2017-11-03 21:21:47 | [diff] [blame] | 150 | static void undefine(Symbol *S) { |
Rafael Espindola | bec3765 | 2017-11-17 01:37:50 | [diff] [blame] | 151 | replaceSymbol<Undefined>(S, nullptr, S->getName(), STB_GLOBAL, STV_DEFAULT, |
| 152 | S->Type); |
Peter Collingbourne | 0ef3874 | 2016-05-12 19:46:14 | [diff] [blame] | 153 | } |
| 154 | |
Peter Smith | 3a52eb0 | 2017-02-01 10:26:03 | [diff] [blame] | 155 | void BitcodeCompiler::add(BitcodeFile &F) { |
Davide Italiano | 786d8e3 | 2016-09-29 00:40:08 | [diff] [blame] | 156 | lto::InputFile &Obj = *F.Obj; |
Rui Ueyama | d31b54b | 2018-05-08 17:50:54 | [diff] [blame] | 157 | bool IsExec = !Config->Shared && !Config->Relocatable; |
Rumeet Dhindsa | d366e36 | 2018-05-02 21:40:07 | [diff] [blame] | 158 | |
Rumeet Dhindsa | 1888326 | 2018-05-08 20:12:07 | [diff] [blame] | 159 | if (Config->ThinLTOIndexOnly) |
Rui Ueyama | 98e4a5c | 2018-09-11 14:37:27 | [diff] [blame] | 160 | ThinIndices.insert(Obj.getName()); |
Rumeet Dhindsa | d366e36 | 2018-05-02 21:40:07 | [diff] [blame] | 161 | |
Rui Ueyama | 2f9fa42 | 2018-05-08 17:50:43 | [diff] [blame] | 162 | ArrayRef<Symbol *> Syms = F.getSymbols(); |
Rui Ueyama | d31b54b | 2018-05-08 17:50:54 | [diff] [blame] | 163 | ArrayRef<lto::InputFile::Symbol> ObjSyms = Obj.symbols(); |
Davide Italiano | 786d8e3 | 2016-09-29 00:40:08 | [diff] [blame] | 164 | std::vector<lto::SymbolResolution> Resols(Syms.size()); |
Davide Italiano | 334fce9 | 2016-05-11 01:07:22 | [diff] [blame] | 165 | |
Davide Italiano | 786d8e3 | 2016-09-29 00:40:08 | [diff] [blame] | 166 | // Provide a resolution to the LTO API for each symbol. |
Rui Ueyama | d31b54b | 2018-05-08 17:50:54 | [diff] [blame] | 167 | for (size_t I = 0, E = Syms.size(); I != E; ++I) { |
| 168 | Symbol *Sym = Syms[I]; |
| 169 | const lto::InputFile::Symbol &ObjSym = ObjSyms[I]; |
| 170 | lto::SymbolResolution &R = Resols[I]; |
Davide Italiano | 86f2bd5 | 2016-03-29 21:46:35 | [diff] [blame] | 171 | |
Davide Italiano | 786d8e3 | 2016-09-29 00:40:08 | [diff] [blame] | 172 | // Ideally we shouldn't check for SF_Undefined but currently IRObjectFile |
| 173 | // reports two symbols for module ASM defined. Without this check, lld |
| 174 | // flags an undefined in IR with a definition in ASM as prevailing. |
| 175 | // Once IRObjectFile is fixed to report only one symbol this hack can |
| 176 | // be removed. |
Rafael Espindola | dfebd36 | 2017-11-29 22:47:35 | [diff] [blame] | 177 | R.Prevailing = !ObjSym.isUndefined() && Sym->File == &F; |
Peter Collingbourne | 3ad1c1e | 2016-05-05 17:13:49 | [diff] [blame] | 178 | |
George Rimar | 3a1af22 | 2017-08-22 08:36:54 | [diff] [blame] | 179 | // We ask LTO to preserve following global symbols: |
| 180 | // 1) All symbols when doing relocatable link, so that them can be used |
| 181 | // for doing final link. |
| 182 | // 2) Symbols that are used in regular objects. |
| 183 | // 3) C named sections if we have corresponding __start_/__stop_ symbol. |
| 184 | // 4) Symbols that are defined in bitcode files and used for dynamic linking. |
| 185 | R.VisibleToRegularObj = Config->Relocatable || Sym->IsUsedInRegularObj || |
Rafael Espindola | affe720 | 2017-07-25 22:51:05 | [diff] [blame] | 186 | (R.Prevailing && Sym->includeInDynsym()) || |
Rafael Espindola | 4b075bb | 2017-07-26 23:39:10 | [diff] [blame] | 187 | UsedStartStop.count(ObjSym.getSectionName()); |
Dmitry Mikulin | c84e0ee | 2018-02-07 00:49:51 | [diff] [blame] | 188 | const auto *DR = dyn_cast<Defined>(Sym); |
Rafael Espindola | c6df38c | 2018-01-16 16:49:05 | [diff] [blame] | 189 | R.FinalDefinitionInLinkageUnit = |
Rui Ueyama | d31b54b | 2018-05-08 17:50:54 | [diff] [blame] | 190 | (IsExec || Sym->Visibility != STV_DEFAULT) && DR && |
Dmitry Mikulin | c84e0ee | 2018-02-07 00:49:51 | [diff] [blame] | 191 | // Skip absolute symbols from ELF objects, otherwise PC-rel relocations |
| 192 | // will be generated by for them, triggering linker errors. |
| 193 | // Symbol section is always null for bitcode symbols, hence the check |
Dmitry Mikulin | 8ddd922 | 2018-02-08 04:25:52 | [diff] [blame] | 194 | // for isElf(). Skip linker script defined symbols as well: they have |
| 195 | // no File defined. |
| 196 | !(DR->Section == nullptr && (!Sym->File || Sym->File->isElf())); |
Rafael Espindola | c6df38c | 2018-01-16 16:49:05 | [diff] [blame] | 197 | |
Davide Italiano | 786d8e3 | 2016-09-29 00:40:08 | [diff] [blame] | 198 | if (R.Prevailing) |
Peter Smith | 3a52eb0 | 2017-02-01 10:26:03 | [diff] [blame] | 199 | undefine(Sym); |
George Rimar | d28c26b | 2017-09-25 09:31:43 | [diff] [blame] | 200 | |
George Rimar | c4ccfb5 | 2018-01-30 09:04:27 | [diff] [blame] | 201 | // We tell LTO to not apply interprocedural optimization for wrapped |
| 202 | // (with --wrap) symbols because otherwise LTO would inline them while |
| 203 | // their values are still not final. |
| 204 | R.LinkerRedefined = !Sym->CanInline; |
Rui Ueyama | 2599248 | 2016-03-22 20:52:10 | [diff] [blame] | 205 | } |
Davide Italiano | 3bfa081 | 2016-11-26 05:37:04 | [diff] [blame] | 206 | checkError(LTOObj->add(std::move(F.Obj), Resols)); |
Davide Italiano | bc17663 | 2016-04-15 22:38:10 | [diff] [blame] | 207 | } |
| 208 | |
Rui Ueyama | f06d494 | 2018-05-17 18:27:12 | [diff] [blame] | 209 | static void createEmptyIndex(StringRef ModulePath) { |
| 210 | std::string Path = replaceThinLTOSuffix(getThinLTOOutputFile(ModulePath)); |
Rui Ueyama | f06d494 | 2018-05-17 18:27:12 | [diff] [blame] | 211 | std::unique_ptr<raw_fd_ostream> OS = openFile(Path + ".thinlto.bc"); |
| 212 | if (!OS) |
| 213 | return; |
| 214 | |
Teresa Johnson | 566b402 | 2018-06-06 22:22:13 | [diff] [blame] | 215 | ModuleSummaryIndex M(/*HaveGVs*/ false); |
Rui Ueyama | f06d494 | 2018-05-17 18:27:12 | [diff] [blame] | 216 | M.setSkipModuleByDistributedBackend(); |
| 217 | WriteIndexToFile(M, *OS); |
| 218 | |
| 219 | if (Config->ThinLTOEmitImportsFiles) |
| 220 | openFile(Path + ".imports"); |
| 221 | } |
| 222 | |
Rui Ueyama | 2599248 | 2016-03-22 20:52:10 | [diff] [blame] | 223 | // Merge all the bitcode files we have seen, codegen the result |
Davide Italiano | 786d8e3 | 2016-09-29 00:40:08 | [diff] [blame] | 224 | // and return the resulting ObjectFile(s). |
Rui Ueyama | 38dbd3e | 2016-09-14 00:05:51 | [diff] [blame] | 225 | std::vector<InputFile *> BitcodeCompiler::compile() { |
Davide Italiano | 3bfa081 | 2016-11-26 05:37:04 | [diff] [blame] | 226 | unsigned MaxTasks = LTOObj->getMaxTasks(); |
Rui Ueyama | f06d494 | 2018-05-17 18:27:12 | [diff] [blame] | 227 | Buf.resize(MaxTasks); |
Peter Collingbourne | e02775f | 2017-03-01 23:00:10 | [diff] [blame] | 228 | Files.resize(MaxTasks); |
Davide Italiano | 828ac541 | 2016-03-28 15:44:21 | [diff] [blame] | 229 | |
Peter Collingbourne | e02775f | 2017-03-01 23:00:10 | [diff] [blame] | 230 | // The --thinlto-cache-dir option specifies the path to a directory in which |
| 231 | // to cache native object files for ThinLTO incremental builds. If a path was |
| 232 | // specified, configure LTO to use it as the cache directory. |
| 233 | lto::NativeObjectCache Cache; |
| 234 | if (!Config->ThinLTOCacheDir.empty()) |
Peter Collingbourne | 128423f | 2017-03-17 00:34:07 | [diff] [blame] | 235 | Cache = check( |
| 236 | lto::localCache(Config->ThinLTOCacheDir, |
Teresa Johnson | 2c2ed3c | 2018-02-20 20:21:59 | [diff] [blame] | 237 | [&](size_t Task, std::unique_ptr<MemoryBuffer> MB) { |
| 238 | Files[Task] = std::move(MB); |
| 239 | })); |
Peter Collingbourne | e02775f | 2017-03-01 23:00:10 | [diff] [blame] | 240 | |
| 241 | checkError(LTOObj->run( |
| 242 | [&](size_t Task) { |
| 243 | return llvm::make_unique<lto::NativeObjectStream>( |
Rui Ueyama | f06d494 | 2018-05-17 18:27:12 | [diff] [blame] | 244 | llvm::make_unique<raw_svector_ostream>(Buf[Task])); |
Peter Collingbourne | e02775f | 2017-03-01 23:00:10 | [diff] [blame] | 245 | }, |
| 246 | Cache)); |
Rafael Espindola | abf6c65 | 2016-04-17 23:20:08 | [diff] [blame] | 247 | |
Rumeet Dhindsa | 1888326 | 2018-05-08 20:12:07 | [diff] [blame] | 248 | // Emit empty index files for non-indexed files |
Rui Ueyama | 98e4a5c | 2018-09-11 14:37:27 | [diff] [blame] | 249 | for (StringRef S : ThinIndices) { |
| 250 | std::string Path = getThinLTOOutputFile(S); |
| 251 | openFile(Path + ".thinlto.bc"); |
| 252 | if (Config->ThinLTOEmitImportsFiles) |
| 253 | openFile(Path + ".imports"); |
Davide Italiano | 786d8e3 | 2016-09-29 00:40:08 | [diff] [blame] | 254 | } |
Peter Collingbourne | e02775f | 2017-03-01 23:00:10 | [diff] [blame] | 255 | |
Rui Ueyama | d54f1c2 | 2018-05-07 22:11:24 | [diff] [blame] | 256 | // If LazyObjFile has not been added to link, emit empty index files. |
| 257 | // This is needed because this is what GNU gold plugin does and we have a |
| 258 | // distributed build system that depends on that behavior. |
| 259 | if (Config->ThinLTOIndexOnly) { |
Rui Ueyama | f06d494 | 2018-05-17 18:27:12 | [diff] [blame] | 260 | for (LazyObjFile *F : LazyObjFiles) |
| 261 | if (!F->AddedToLink && isBitcode(F->MB)) |
| 262 | createEmptyIndex(F->getName()); |
Rui Ueyama | d54f1c2 | 2018-05-07 22:11:24 | [diff] [blame] | 263 | |
Rumeet Dhindsa | b5b7d6e | 2018-05-08 22:37:57 | [diff] [blame] | 264 | if (!Config->LTOObjPath.empty()) |
Rui Ueyama | f06d494 | 2018-05-17 18:27:12 | [diff] [blame] | 265 | saveBuffer(Buf[0], Config->LTOObjPath); |
Rumeet Dhindsa | 1888326 | 2018-05-08 20:12:07 | [diff] [blame] | 266 | |
Rui Ueyama | d54f1c2 | 2018-05-07 22:11:24 | [diff] [blame] | 267 | // ThinLTO with index only option is required to generate only the index |
| 268 | // files. After that, we exit from linker and ThinLTO backend runs in a |
| 269 | // distributed environment. |
Rui Ueyama | 554adb2 | 2018-05-07 22:11:34 | [diff] [blame] | 270 | if (IndexFile) |
| 271 | IndexFile->close(); |
| 272 | return {}; |
Rui Ueyama | d54f1c2 | 2018-05-07 22:11:24 | [diff] [blame] | 273 | } |
Rui Ueyama | f06d494 | 2018-05-17 18:27:12 | [diff] [blame] | 274 | |
Rumeet Dhindsa | 1888326 | 2018-05-08 20:12:07 | [diff] [blame] | 275 | if (!Config->ThinLTOCacheDir.empty()) |
| 276 | pruneCache(Config->ThinLTOCacheDir, Config->ThinLTOCachePolicy); |
| 277 | |
| 278 | std::vector<InputFile *> Ret; |
| 279 | for (unsigned I = 0; I != MaxTasks; ++I) { |
Rui Ueyama | f06d494 | 2018-05-17 18:27:12 | [diff] [blame] | 280 | if (Buf[I].empty()) |
Rumeet Dhindsa | 1888326 | 2018-05-08 20:12:07 | [diff] [blame] | 281 | continue; |
| 282 | if (Config->SaveTemps) { |
| 283 | if (I == 0) |
Rui Ueyama | f06d494 | 2018-05-17 18:27:12 | [diff] [blame] | 284 | saveBuffer(Buf[I], Config->OutputFile + ".lto.o"); |
Rumeet Dhindsa | 1888326 | 2018-05-08 20:12:07 | [diff] [blame] | 285 | else |
Rui Ueyama | f06d494 | 2018-05-17 18:27:12 | [diff] [blame] | 286 | saveBuffer(Buf[I], Config->OutputFile + Twine(I) + ".lto.o"); |
Rumeet Dhindsa | 1888326 | 2018-05-08 20:12:07 | [diff] [blame] | 287 | } |
Rui Ueyama | f06d494 | 2018-05-17 18:27:12 | [diff] [blame] | 288 | InputFile *Obj = createObjectFile(MemoryBufferRef(Buf[I], "lto.tmp")); |
Rumeet Dhindsa | 1888326 | 2018-05-08 20:12:07 | [diff] [blame] | 289 | Ret.push_back(Obj); |
| 290 | } |
Rumeet Dhindsa | d366e36 | 2018-05-02 21:40:07 | [diff] [blame] | 291 | |
Peter Collingbourne | e02775f | 2017-03-01 23:00:10 | [diff] [blame] | 292 | for (std::unique_ptr<MemoryBuffer> &File : Files) |
| 293 | if (File) |
| 294 | Ret.push_back(createObjectFile(*File)); |
Davide Italiano | 786d8e3 | 2016-09-29 00:40:08 | [diff] [blame] | 295 | return Ret; |
Rui Ueyama | 961f2ff | 2016-03-23 21:19:27 | [diff] [blame] | 296 | } |