| 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" |
| 12 | #include "Error.h" |
| 13 | #include "InputFiles.h" |
| 14 | #include "Symbols.h" |
| Bob Haarman | 35989d6 | 2017-02-02 23:49:16 +0000 | [diff] [blame] | 15 | #include "lld/Core/TargetOptionsCommandFlags.h" |
| Eugene Zelenko | 22886a2 | 2016-11-05 01:00:56 +0000 | [diff] [blame] | 16 | #include "llvm/ADT/STLExtras.h" |
| Rui Ueyama | 8c6a5aa | 2016-11-05 22:37:59 +0000 | [diff] [blame] | 17 | #include "llvm/ADT/SmallString.h" |
| Eugene Zelenko | 22886a2 | 2016-11-05 01:00:56 +0000 | [diff] [blame] | 18 | #include "llvm/ADT/StringRef.h" |
| 19 | #include "llvm/ADT/Twine.h" |
| Zachary Turner | 264b5d9 | 2017-06-07 03:48:56 +0000 | [diff] [blame] | 20 | #include "llvm/BinaryFormat/ELF.h" |
| Davide Italiano | 786d8e3 | 2016-09-29 00:40:08 +0000 | [diff] [blame] | 21 | #include "llvm/IR/DiagnosticPrinter.h" |
| Peter Collingbourne | e02775f | 2017-03-01 23:00:10 +0000 | [diff] [blame] | 22 | #include "llvm/LTO/Caching.h" |
| Eugene Zelenko | 22886a2 | 2016-11-05 01:00:56 +0000 | [diff] [blame] | 23 | #include "llvm/LTO/Config.h" |
| Davide Italiano | 786d8e3 | 2016-09-29 00:40:08 +0000 | [diff] [blame] | 24 | #include "llvm/LTO/LTO.h" |
| Eugene Zelenko | 22886a2 | 2016-11-05 01:00:56 +0000 | [diff] [blame] | 25 | #include "llvm/Object/SymbolicFile.h" |
| 26 | #include "llvm/Support/CodeGen.h" |
| Eugene Zelenko | 22886a2 | 2016-11-05 01:00:56 +0000 | [diff] [blame] | 27 | #include "llvm/Support/Error.h" |
| 28 | #include "llvm/Support/FileSystem.h" |
| 29 | #include "llvm/Support/MemoryBuffer.h" |
| 30 | #include "llvm/Support/raw_ostream.h" |
| 31 | #include <algorithm> |
| 32 | #include <cstddef> |
| 33 | #include <memory> |
| 34 | #include <string> |
| 35 | #include <system_error> |
| 36 | #include <vector> |
| Rui Ueyama | 2599248 | 2016-03-22 20:52:10 +0000 | [diff] [blame] | 37 | |
| 38 | using namespace llvm; |
| 39 | using namespace llvm::object; |
| 40 | using namespace llvm::ELF; |
| 41 | |
| 42 | using namespace lld; |
| 43 | using namespace lld::elf; |
| 44 | |
| 45 | // This is for use when debugging LTO. |
| Rui Ueyama | 48da5cf | 2016-07-15 02:17:13 +0000 | [diff] [blame] | 46 | static void saveBuffer(StringRef Buffer, const Twine &Path) { |
| Rui Ueyama | 2599248 | 2016-03-22 20:52:10 +0000 | [diff] [blame] | 47 | std::error_code EC; |
| Rui Ueyama | 48da5cf | 2016-07-15 02:17:13 +0000 | [diff] [blame] | 48 | raw_fd_ostream OS(Path.str(), EC, sys::fs::OpenFlags::F_None); |
| Rui Ueyama | f8292e9 | 2016-07-15 02:01:03 +0000 | [diff] [blame] | 49 | if (EC) |
| Rui Ueyama | c8d3a83 | 2017-01-12 22:18:04 +0000 | [diff] [blame] | 50 | error("cannot create " + Path + ": " + EC.message()); |
| Rui Ueyama | 2599248 | 2016-03-22 20:52:10 +0000 | [diff] [blame] | 51 | OS << Buffer; |
| 52 | } |
| 53 | |
| Davide Italiano | 786d8e3 | 2016-09-29 00:40:08 +0000 | [diff] [blame] | 54 | static void diagnosticHandler(const DiagnosticInfo &DI) { |
| 55 | SmallString<128> ErrStorage; |
| 56 | raw_svector_ostream OS(ErrStorage); |
| 57 | DiagnosticPrinterRawOStream DP(OS); |
| 58 | DI.print(DP); |
| Rui Ueyama | d31e13f | 2016-09-29 21:00:23 +0000 | [diff] [blame] | 59 | warn(ErrStorage); |
| Rui Ueyama | 2599248 | 2016-03-22 20:52:10 +0000 | [diff] [blame] | 60 | } |
| 61 | |
| Rui Ueyama | 6c5cbff | 2016-09-29 21:00:26 +0000 | [diff] [blame] | 62 | static void checkError(Error E) { |
| Mehdi Amini | c1edf56 | 2016-11-11 04:29:25 +0000 | [diff] [blame] | 63 | handleAllErrors(std::move(E), [&](ErrorInfoBase &EIB) -> Error { |
| Rui Ueyama | 6c5cbff | 2016-09-29 21:00:26 +0000 | [diff] [blame] | 64 | error(EIB.message()); |
| 65 | return Error::success(); |
| 66 | }); |
| 67 | } |
| 68 | |
| Davide Italiano | 786d8e3 | 2016-09-29 00:40:08 +0000 | [diff] [blame] | 69 | static std::unique_ptr<lto::LTO> createLTO() { |
| 70 | lto::Config Conf; |
| Davide Italiano | d26c4a1 | 2016-05-15 19:29:38 +0000 | [diff] [blame] | 71 | |
| Davide Italiano | 786d8e3 | 2016-09-29 00:40:08 +0000 | [diff] [blame] | 72 | // LLD supports the new relocations. |
| 73 | Conf.Options = InitTargetOptionsFromCodeGenFlags(); |
| 74 | Conf.Options.RelaxELFRelocations = true; |
| Davide Italiano | df24d5b | 2016-06-02 22:58:11 +0000 | [diff] [blame] | 75 | |
| Davide Italiano | 1f4e29c | 2017-07-24 19:38:13 +0000 | [diff] [blame^] | 76 | // Always emit a section per function with LTO. |
| 77 | Conf.Options.FunctionSections = true; |
| 78 | |
| Evgeniy Stepanov | f12ac5b | 2017-05-22 21:11:44 +0000 | [diff] [blame] | 79 | if (Config->Relocatable) |
| 80 | Conf.RelocModel = None; |
| 81 | else if (Config->Pic) |
| 82 | Conf.RelocModel = Reloc::PIC_; |
| 83 | else |
| 84 | Conf.RelocModel = Reloc::Static; |
| Martell Malone | 6b43b7a | 2017-02-28 23:43:26 +0000 | [diff] [blame] | 85 | Conf.CodeModel = GetCodeModelFromCMModel(); |
| Davide Italiano | 786d8e3 | 2016-09-29 00:40:08 +0000 | [diff] [blame] | 86 | Conf.DisableVerify = Config->DisableVerify; |
| 87 | Conf.DiagHandler = diagnosticHandler; |
| Davide Italiano | 3bfa081 | 2016-11-26 05:37:04 +0000 | [diff] [blame] | 88 | Conf.OptLevel = Config->LTOO; |
| Davide Italiano | df24d5b | 2016-06-02 22:58:11 +0000 | [diff] [blame] | 89 | |
| Davide Italiano | 786d8e3 | 2016-09-29 00:40:08 +0000 | [diff] [blame] | 90 | // Set up a custom pipeline if we've been asked to. |
| Davide Italiano | 3bfa081 | 2016-11-26 05:37:04 +0000 | [diff] [blame] | 91 | Conf.OptPipeline = Config->LTONewPmPasses; |
| 92 | Conf.AAPipeline = Config->LTOAAPipeline; |
| Rui Ueyama | 2599248 | 2016-03-22 20:52:10 +0000 | [diff] [blame] | 93 | |
| Davide Italiano | db4b0a7 | 2017-02-13 17:49:18 +0000 | [diff] [blame] | 94 | // Set up optimization remarks if we've been asked to. |
| 95 | Conf.RemarksFilename = Config->OptRemarksFilename; |
| 96 | Conf.RemarksWithHotness = Config->OptRemarksWithHotness; |
| 97 | |
| Rui Ueyama | 2599248 | 2016-03-22 20:52:10 +0000 | [diff] [blame] | 98 | if (Config->SaveTemps) |
| Rui Ueyama | 6c5cbff | 2016-09-29 21:00:26 +0000 | [diff] [blame] | 99 | checkError(Conf.addSaveTemps(std::string(Config->OutputFile) + ".", |
| George Rimar | a4c7e74 | 2016-10-20 08:36:42 +0000 | [diff] [blame] | 100 | /*UseInputModulePath*/ true)); |
| Davide Italiano | 786d8e3 | 2016-09-29 00:40:08 +0000 | [diff] [blame] | 101 | |
| Davide Italiano | 7a7b35a | 2016-10-10 18:12:53 +0000 | [diff] [blame] | 102 | lto::ThinBackend Backend; |
| Davide Italiano | 3bfa081 | 2016-11-26 05:37:04 +0000 | [diff] [blame] | 103 | if (Config->ThinLTOJobs != -1u) |
| 104 | Backend = lto::createInProcessThinBackend(Config->ThinLTOJobs); |
| Davide Italiano | b6e6e4a | 2016-10-10 23:12:14 +0000 | [diff] [blame] | 105 | return llvm::make_unique<lto::LTO>(std::move(Conf), Backend, |
| Davide Italiano | 3bfa081 | 2016-11-26 05:37:04 +0000 | [diff] [blame] | 106 | Config->LTOPartitions); |
| Rui Ueyama | 2599248 | 2016-03-22 20:52:10 +0000 | [diff] [blame] | 107 | } |
| 108 | |
| Davide Italiano | 3bfa081 | 2016-11-26 05:37:04 +0000 | [diff] [blame] | 109 | BitcodeCompiler::BitcodeCompiler() : LTOObj(createLTO()) {} |
| Rafael Espindola | ae605c1 | 2016-04-21 20:35:25 +0000 | [diff] [blame] | 110 | |
| Eugene Zelenko | 22886a2 | 2016-11-05 01:00:56 +0000 | [diff] [blame] | 111 | BitcodeCompiler::~BitcodeCompiler() = default; |
| Rui Ueyama | 412c802 | 2016-04-22 21:16:18 +0000 | [diff] [blame] | 112 | |
| Peter Smith | 3a52eb0 | 2017-02-01 10:26:03 +0000 | [diff] [blame] | 113 | static void undefine(Symbol *S) { |
| 114 | replaceBody<Undefined>(S, S->body()->getName(), /*IsLocal=*/false, |
| 115 | STV_DEFAULT, S->body()->Type, nullptr); |
| Peter Collingbourne | 0ef3874 | 2016-05-12 19:46:14 +0000 | [diff] [blame] | 116 | } |
| 117 | |
| Peter Smith | 3a52eb0 | 2017-02-01 10:26:03 +0000 | [diff] [blame] | 118 | void BitcodeCompiler::add(BitcodeFile &F) { |
| Davide Italiano | 786d8e3 | 2016-09-29 00:40:08 +0000 | [diff] [blame] | 119 | lto::InputFile &Obj = *F.Obj; |
| Davide Italiano | 786d8e3 | 2016-09-29 00:40:08 +0000 | [diff] [blame] | 120 | unsigned SymNum = 0; |
| 121 | std::vector<Symbol *> Syms = F.getSymbols(); |
| 122 | std::vector<lto::SymbolResolution> Resols(Syms.size()); |
| Davide Italiano | 334fce9 | 2016-05-11 01:07:22 +0000 | [diff] [blame] | 123 | |
| Davide Italiano | 786d8e3 | 2016-09-29 00:40:08 +0000 | [diff] [blame] | 124 | // Provide a resolution to the LTO API for each symbol. |
| 125 | for (const lto::InputFile::Symbol &ObjSym : Obj.symbols()) { |
| 126 | Symbol *Sym = Syms[SymNum]; |
| 127 | lto::SymbolResolution &R = Resols[SymNum]; |
| 128 | ++SymNum; |
| 129 | SymbolBody *B = Sym->body(); |
| Davide Italiano | 86f2bd5 | 2016-03-29 21:46:35 +0000 | [diff] [blame] | 130 | |
| Davide Italiano | 786d8e3 | 2016-09-29 00:40:08 +0000 | [diff] [blame] | 131 | // Ideally we shouldn't check for SF_Undefined but currently IRObjectFile |
| 132 | // reports two symbols for module ASM defined. Without this check, lld |
| 133 | // flags an undefined in IR with a definition in ASM as prevailing. |
| 134 | // Once IRObjectFile is fixed to report only one symbol this hack can |
| 135 | // be removed. |
| Peter Collingbourne | 0d56b95 | 2017-03-28 22:31:35 +0000 | [diff] [blame] | 136 | R.Prevailing = !ObjSym.isUndefined() && B->File == &F; |
| Peter Collingbourne | 3ad1c1e | 2016-05-05 17:13:49 +0000 | [diff] [blame] | 137 | |
| Davide Italiano | 786d8e3 | 2016-09-29 00:40:08 +0000 | [diff] [blame] | 138 | R.VisibleToRegularObj = |
| 139 | Sym->IsUsedInRegularObj || (R.Prevailing && Sym->includeInDynsym()); |
| 140 | if (R.Prevailing) |
| Peter Smith | 3a52eb0 | 2017-02-01 10:26:03 +0000 | [diff] [blame] | 141 | undefine(Sym); |
| Dmitry Mikulin | db3b87b | 2017-06-05 16:24:25 +0000 | [diff] [blame] | 142 | R.LinkerRedefined = Config->RenamedSymbols.count(Sym); |
| Rui Ueyama | 2599248 | 2016-03-22 20:52:10 +0000 | [diff] [blame] | 143 | } |
| Davide Italiano | 3bfa081 | 2016-11-26 05:37:04 +0000 | [diff] [blame] | 144 | checkError(LTOObj->add(std::move(F.Obj), Resols)); |
| Davide Italiano | bc17663 | 2016-04-15 22:38:10 +0000 | [diff] [blame] | 145 | } |
| 146 | |
| Rui Ueyama | 2599248 | 2016-03-22 20:52:10 +0000 | [diff] [blame] | 147 | // Merge all the bitcode files we have seen, codegen the result |
| Davide Italiano | 786d8e3 | 2016-09-29 00:40:08 +0000 | [diff] [blame] | 148 | // and return the resulting ObjectFile(s). |
| Rui Ueyama | 38dbd3e | 2016-09-14 00:05:51 +0000 | [diff] [blame] | 149 | std::vector<InputFile *> BitcodeCompiler::compile() { |
| Davide Italiano | 786d8e3 | 2016-09-29 00:40:08 +0000 | [diff] [blame] | 150 | std::vector<InputFile *> Ret; |
| Davide Italiano | 3bfa081 | 2016-11-26 05:37:04 +0000 | [diff] [blame] | 151 | unsigned MaxTasks = LTOObj->getMaxTasks(); |
| Davide Italiano | 786d8e3 | 2016-09-29 00:40:08 +0000 | [diff] [blame] | 152 | Buff.resize(MaxTasks); |
| Peter Collingbourne | e02775f | 2017-03-01 23:00:10 +0000 | [diff] [blame] | 153 | Files.resize(MaxTasks); |
| Davide Italiano | 828ac541 | 2016-03-28 15:44:21 +0000 | [diff] [blame] | 154 | |
| Peter Collingbourne | e02775f | 2017-03-01 23:00:10 +0000 | [diff] [blame] | 155 | // The --thinlto-cache-dir option specifies the path to a directory in which |
| 156 | // to cache native object files for ThinLTO incremental builds. If a path was |
| 157 | // specified, configure LTO to use it as the cache directory. |
| 158 | lto::NativeObjectCache Cache; |
| 159 | if (!Config->ThinLTOCacheDir.empty()) |
| Peter Collingbourne | 128423f | 2017-03-17 00:34:07 +0000 | [diff] [blame] | 160 | Cache = check( |
| 161 | lto::localCache(Config->ThinLTOCacheDir, |
| 162 | [&](size_t Task, std::unique_ptr<MemoryBuffer> MB) { |
| 163 | Files[Task] = std::move(MB); |
| 164 | })); |
| Peter Collingbourne | e02775f | 2017-03-01 23:00:10 +0000 | [diff] [blame] | 165 | |
| 166 | checkError(LTOObj->run( |
| 167 | [&](size_t Task) { |
| 168 | return llvm::make_unique<lto::NativeObjectStream>( |
| 169 | llvm::make_unique<raw_svector_ostream>(Buff[Task])); |
| 170 | }, |
| 171 | Cache)); |
| Rafael Espindola | abf6c65 | 2016-04-17 23:20:08 +0000 | [diff] [blame] | 172 | |
| Peter Collingbourne | ee59e43 | 2017-03-17 02:24:16 +0000 | [diff] [blame] | 173 | if (!Config->ThinLTOCacheDir.empty()) |
| 174 | pruneCache(Config->ThinLTOCacheDir, Config->ThinLTOCachePolicy); |
| 175 | |
| Davide Italiano | 786d8e3 | 2016-09-29 00:40:08 +0000 | [diff] [blame] | 176 | for (unsigned I = 0; I != MaxTasks; ++I) { |
| 177 | if (Buff[I].empty()) |
| 178 | continue; |
| 179 | if (Config->SaveTemps) { |
| Peter Collingbourne | d22ec64 | 2017-01-26 02:18:28 +0000 | [diff] [blame] | 180 | if (I == 0) |
| Davide Italiano | 786d8e3 | 2016-09-29 00:40:08 +0000 | [diff] [blame] | 181 | saveBuffer(Buff[I], Config->OutputFile + ".lto.o"); |
| 182 | else |
| 183 | saveBuffer(Buff[I], Config->OutputFile + Twine(I) + ".lto.o"); |
| 184 | } |
| Rui Ueyama | 55518e7 | 2016-10-28 20:57:25 +0000 | [diff] [blame] | 185 | InputFile *Obj = createObjectFile(MemoryBufferRef(Buff[I], "lto.tmp")); |
| Davide Italiano | 786d8e3 | 2016-09-29 00:40:08 +0000 | [diff] [blame] | 186 | Ret.push_back(Obj); |
| 187 | } |
| Peter Collingbourne | e02775f | 2017-03-01 23:00:10 +0000 | [diff] [blame] | 188 | |
| 189 | for (std::unique_ptr<MemoryBuffer> &File : Files) |
| 190 | if (File) |
| 191 | Ret.push_back(createObjectFile(*File)); |
| 192 | |
| Davide Italiano | 786d8e3 | 2016-09-29 00:40:08 +0000 | [diff] [blame] | 193 | return Ret; |
| Rui Ueyama | 961f2ff | 2016-03-23 21:19:27 +0000 | [diff] [blame] | 194 | } |