blob: 3a536271db4cd7b4839f863e9039128c6c78c294 [file] [log] [blame]
Rui Ueyama25992482016-03-22 20:52:10 +00001//===- 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 Haarman35989d62017-02-02 23:49:16 +000015#include "lld/Core/TargetOptionsCommandFlags.h"
Eugene Zelenko22886a22016-11-05 01:00:56 +000016#include "llvm/ADT/STLExtras.h"
Rui Ueyama8c6a5aa2016-11-05 22:37:59 +000017#include "llvm/ADT/SmallString.h"
Eugene Zelenko22886a22016-11-05 01:00:56 +000018#include "llvm/ADT/StringRef.h"
19#include "llvm/ADT/Twine.h"
Zachary Turner264b5d92017-06-07 03:48:56 +000020#include "llvm/BinaryFormat/ELF.h"
Davide Italiano786d8e32016-09-29 00:40:08 +000021#include "llvm/IR/DiagnosticPrinter.h"
Peter Collingbournee02775f2017-03-01 23:00:10 +000022#include "llvm/LTO/Caching.h"
Eugene Zelenko22886a22016-11-05 01:00:56 +000023#include "llvm/LTO/Config.h"
Davide Italiano786d8e32016-09-29 00:40:08 +000024#include "llvm/LTO/LTO.h"
Eugene Zelenko22886a22016-11-05 01:00:56 +000025#include "llvm/Object/SymbolicFile.h"
26#include "llvm/Support/CodeGen.h"
Eugene Zelenko22886a22016-11-05 01:00:56 +000027#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 Ueyama25992482016-03-22 20:52:10 +000037
38using namespace llvm;
39using namespace llvm::object;
40using namespace llvm::ELF;
41
42using namespace lld;
43using namespace lld::elf;
44
45// This is for use when debugging LTO.
Rui Ueyama48da5cf2016-07-15 02:17:13 +000046static void saveBuffer(StringRef Buffer, const Twine &Path) {
Rui Ueyama25992482016-03-22 20:52:10 +000047 std::error_code EC;
Rui Ueyama48da5cf2016-07-15 02:17:13 +000048 raw_fd_ostream OS(Path.str(), EC, sys::fs::OpenFlags::F_None);
Rui Ueyamaf8292e92016-07-15 02:01:03 +000049 if (EC)
Rui Ueyamac8d3a832017-01-12 22:18:04 +000050 error("cannot create " + Path + ": " + EC.message());
Rui Ueyama25992482016-03-22 20:52:10 +000051 OS << Buffer;
52}
53
Davide Italiano786d8e32016-09-29 00:40:08 +000054static void diagnosticHandler(const DiagnosticInfo &DI) {
55 SmallString<128> ErrStorage;
56 raw_svector_ostream OS(ErrStorage);
57 DiagnosticPrinterRawOStream DP(OS);
58 DI.print(DP);
Rui Ueyamad31e13f2016-09-29 21:00:23 +000059 warn(ErrStorage);
Rui Ueyama25992482016-03-22 20:52:10 +000060}
61
Rui Ueyama6c5cbff2016-09-29 21:00:26 +000062static void checkError(Error E) {
Mehdi Aminic1edf562016-11-11 04:29:25 +000063 handleAllErrors(std::move(E), [&](ErrorInfoBase &EIB) -> Error {
Rui Ueyama6c5cbff2016-09-29 21:00:26 +000064 error(EIB.message());
65 return Error::success();
66 });
67}
68
Davide Italiano786d8e32016-09-29 00:40:08 +000069static std::unique_ptr<lto::LTO> createLTO() {
70 lto::Config Conf;
Davide Italianod26c4a12016-05-15 19:29:38 +000071
Davide Italiano786d8e32016-09-29 00:40:08 +000072 // LLD supports the new relocations.
73 Conf.Options = InitTargetOptionsFromCodeGenFlags();
74 Conf.Options.RelaxELFRelocations = true;
Davide Italianodf24d5b2016-06-02 22:58:11 +000075
Evgeniy Stepanovf12ac5b2017-05-22 21:11:44 +000076 if (Config->Relocatable)
77 Conf.RelocModel = None;
78 else if (Config->Pic)
79 Conf.RelocModel = Reloc::PIC_;
80 else
81 Conf.RelocModel = Reloc::Static;
Martell Malone6b43b7a2017-02-28 23:43:26 +000082 Conf.CodeModel = GetCodeModelFromCMModel();
Davide Italiano786d8e32016-09-29 00:40:08 +000083 Conf.DisableVerify = Config->DisableVerify;
84 Conf.DiagHandler = diagnosticHandler;
Davide Italiano3bfa0812016-11-26 05:37:04 +000085 Conf.OptLevel = Config->LTOO;
Davide Italianodf24d5b2016-06-02 22:58:11 +000086
Davide Italiano786d8e32016-09-29 00:40:08 +000087 // Set up a custom pipeline if we've been asked to.
Davide Italiano3bfa0812016-11-26 05:37:04 +000088 Conf.OptPipeline = Config->LTONewPmPasses;
89 Conf.AAPipeline = Config->LTOAAPipeline;
Rui Ueyama25992482016-03-22 20:52:10 +000090
Davide Italianodb4b0a72017-02-13 17:49:18 +000091 // Set up optimization remarks if we've been asked to.
92 Conf.RemarksFilename = Config->OptRemarksFilename;
93 Conf.RemarksWithHotness = Config->OptRemarksWithHotness;
94
Rui Ueyama25992482016-03-22 20:52:10 +000095 if (Config->SaveTemps)
Rui Ueyama6c5cbff2016-09-29 21:00:26 +000096 checkError(Conf.addSaveTemps(std::string(Config->OutputFile) + ".",
George Rimara4c7e742016-10-20 08:36:42 +000097 /*UseInputModulePath*/ true));
Davide Italiano786d8e32016-09-29 00:40:08 +000098
Davide Italiano7a7b35a2016-10-10 18:12:53 +000099 lto::ThinBackend Backend;
Davide Italiano3bfa0812016-11-26 05:37:04 +0000100 if (Config->ThinLTOJobs != -1u)
101 Backend = lto::createInProcessThinBackend(Config->ThinLTOJobs);
Davide Italianob6e6e4a2016-10-10 23:12:14 +0000102 return llvm::make_unique<lto::LTO>(std::move(Conf), Backend,
Davide Italiano3bfa0812016-11-26 05:37:04 +0000103 Config->LTOPartitions);
Rui Ueyama25992482016-03-22 20:52:10 +0000104}
105
Davide Italiano3bfa0812016-11-26 05:37:04 +0000106BitcodeCompiler::BitcodeCompiler() : LTOObj(createLTO()) {}
Rafael Espindolaae605c12016-04-21 20:35:25 +0000107
Eugene Zelenko22886a22016-11-05 01:00:56 +0000108BitcodeCompiler::~BitcodeCompiler() = default;
Rui Ueyama412c8022016-04-22 21:16:18 +0000109
Peter Smith3a52eb02017-02-01 10:26:03 +0000110static void undefine(Symbol *S) {
111 replaceBody<Undefined>(S, S->body()->getName(), /*IsLocal=*/false,
112 STV_DEFAULT, S->body()->Type, nullptr);
Peter Collingbourne0ef38742016-05-12 19:46:14 +0000113}
114
Peter Smith3a52eb02017-02-01 10:26:03 +0000115void BitcodeCompiler::add(BitcodeFile &F) {
Davide Italiano786d8e32016-09-29 00:40:08 +0000116 lto::InputFile &Obj = *F.Obj;
Davide Italiano786d8e32016-09-29 00:40:08 +0000117 unsigned SymNum = 0;
118 std::vector<Symbol *> Syms = F.getSymbols();
119 std::vector<lto::SymbolResolution> Resols(Syms.size());
Davide Italiano334fce92016-05-11 01:07:22 +0000120
Davide Italiano786d8e32016-09-29 00:40:08 +0000121 // Provide a resolution to the LTO API for each symbol.
122 for (const lto::InputFile::Symbol &ObjSym : Obj.symbols()) {
123 Symbol *Sym = Syms[SymNum];
124 lto::SymbolResolution &R = Resols[SymNum];
125 ++SymNum;
126 SymbolBody *B = Sym->body();
Davide Italiano86f2bd52016-03-29 21:46:35 +0000127
Davide Italiano786d8e32016-09-29 00:40:08 +0000128 // Ideally we shouldn't check for SF_Undefined but currently IRObjectFile
129 // reports two symbols for module ASM defined. Without this check, lld
130 // flags an undefined in IR with a definition in ASM as prevailing.
131 // Once IRObjectFile is fixed to report only one symbol this hack can
132 // be removed.
Peter Collingbourne0d56b952017-03-28 22:31:35 +0000133 R.Prevailing = !ObjSym.isUndefined() && B->File == &F;
Peter Collingbourne3ad1c1e2016-05-05 17:13:49 +0000134
Davide Italiano786d8e32016-09-29 00:40:08 +0000135 R.VisibleToRegularObj =
136 Sym->IsUsedInRegularObj || (R.Prevailing && Sym->includeInDynsym());
137 if (R.Prevailing)
Peter Smith3a52eb02017-02-01 10:26:03 +0000138 undefine(Sym);
Dmitry Mikulindb3b87b2017-06-05 16:24:25 +0000139 R.LinkerRedefined = Config->RenamedSymbols.count(Sym);
Rui Ueyama25992482016-03-22 20:52:10 +0000140 }
Davide Italiano3bfa0812016-11-26 05:37:04 +0000141 checkError(LTOObj->add(std::move(F.Obj), Resols));
Davide Italianobc176632016-04-15 22:38:10 +0000142}
143
Rui Ueyama25992482016-03-22 20:52:10 +0000144// Merge all the bitcode files we have seen, codegen the result
Davide Italiano786d8e32016-09-29 00:40:08 +0000145// and return the resulting ObjectFile(s).
Rui Ueyama38dbd3e2016-09-14 00:05:51 +0000146std::vector<InputFile *> BitcodeCompiler::compile() {
Davide Italiano786d8e32016-09-29 00:40:08 +0000147 std::vector<InputFile *> Ret;
Davide Italiano3bfa0812016-11-26 05:37:04 +0000148 unsigned MaxTasks = LTOObj->getMaxTasks();
Davide Italiano786d8e32016-09-29 00:40:08 +0000149 Buff.resize(MaxTasks);
Peter Collingbournee02775f2017-03-01 23:00:10 +0000150 Files.resize(MaxTasks);
Davide Italiano828ac5412016-03-28 15:44:21 +0000151
Peter Collingbournee02775f2017-03-01 23:00:10 +0000152 // The --thinlto-cache-dir option specifies the path to a directory in which
153 // to cache native object files for ThinLTO incremental builds. If a path was
154 // specified, configure LTO to use it as the cache directory.
155 lto::NativeObjectCache Cache;
156 if (!Config->ThinLTOCacheDir.empty())
Peter Collingbourne128423f2017-03-17 00:34:07 +0000157 Cache = check(
158 lto::localCache(Config->ThinLTOCacheDir,
159 [&](size_t Task, std::unique_ptr<MemoryBuffer> MB) {
160 Files[Task] = std::move(MB);
161 }));
Peter Collingbournee02775f2017-03-01 23:00:10 +0000162
163 checkError(LTOObj->run(
164 [&](size_t Task) {
165 return llvm::make_unique<lto::NativeObjectStream>(
166 llvm::make_unique<raw_svector_ostream>(Buff[Task]));
167 },
168 Cache));
Rafael Espindolaabf6c652016-04-17 23:20:08 +0000169
Peter Collingbourneee59e432017-03-17 02:24:16 +0000170 if (!Config->ThinLTOCacheDir.empty())
171 pruneCache(Config->ThinLTOCacheDir, Config->ThinLTOCachePolicy);
172
Davide Italiano786d8e32016-09-29 00:40:08 +0000173 for (unsigned I = 0; I != MaxTasks; ++I) {
174 if (Buff[I].empty())
175 continue;
176 if (Config->SaveTemps) {
Peter Collingbourned22ec642017-01-26 02:18:28 +0000177 if (I == 0)
Davide Italiano786d8e32016-09-29 00:40:08 +0000178 saveBuffer(Buff[I], Config->OutputFile + ".lto.o");
179 else
180 saveBuffer(Buff[I], Config->OutputFile + Twine(I) + ".lto.o");
181 }
Rui Ueyama55518e72016-10-28 20:57:25 +0000182 InputFile *Obj = createObjectFile(MemoryBufferRef(Buff[I], "lto.tmp"));
Davide Italiano786d8e32016-09-29 00:40:08 +0000183 Ret.push_back(Obj);
184 }
Peter Collingbournee02775f2017-03-01 23:00:10 +0000185
186 for (std::unique_ptr<MemoryBuffer> &File : Files)
187 if (File)
188 Ret.push_back(createObjectFile(*File));
189
Davide Italiano786d8e32016-09-29 00:40:08 +0000190 return Ret;
Rui Ueyama961f2ff2016-03-23 21:19:27 +0000191}