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