blob: 8d4ad91394779332103b70a4a38708dbe97a0dfc [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"
Eugene Zelenko22886a22016-11-05 01:00:56 +000015#include "llvm/ADT/STLExtras.h"
Rui Ueyama8c6a5aa2016-11-05 22:37:59 +000016#include "llvm/ADT/SmallString.h"
Eugene Zelenko22886a22016-11-05 01:00:56 +000017#include "llvm/ADT/StringRef.h"
18#include "llvm/ADT/Twine.h"
Davide Italiano8eca2822016-04-01 00:35:29 +000019#include "llvm/CodeGen/CommandFlags.h"
Davide Italiano786d8e32016-09-29 00:40:08 +000020#include "llvm/IR/DiagnosticPrinter.h"
Eugene Zelenko22886a22016-11-05 01:00:56 +000021#include "llvm/LTO/Config.h"
Davide Italiano786d8e32016-09-29 00:40:08 +000022#include "llvm/LTO/LTO.h"
Eugene Zelenko22886a22016-11-05 01:00:56 +000023#include "llvm/Object/SymbolicFile.h"
24#include "llvm/Support/CodeGen.h"
25#include "llvm/Support/ELF.h"
26#include "llvm/Support/Error.h"
27#include "llvm/Support/FileSystem.h"
28#include "llvm/Support/MemoryBuffer.h"
29#include "llvm/Support/raw_ostream.h"
30#include <algorithm>
31#include <cstddef>
32#include <memory>
33#include <string>
34#include <system_error>
35#include <vector>
Rui Ueyama25992482016-03-22 20:52:10 +000036
37using namespace llvm;
38using namespace llvm::object;
39using namespace llvm::ELF;
40
41using namespace lld;
42using namespace lld::elf;
43
44// This is for use when debugging LTO.
Rui Ueyama48da5cf2016-07-15 02:17:13 +000045static void saveBuffer(StringRef Buffer, const Twine &Path) {
Rui Ueyama25992482016-03-22 20:52:10 +000046 std::error_code EC;
Rui Ueyama48da5cf2016-07-15 02:17:13 +000047 raw_fd_ostream OS(Path.str(), EC, sys::fs::OpenFlags::F_None);
Rui Ueyamaf8292e92016-07-15 02:01:03 +000048 if (EC)
49 error(EC, "cannot create " + Path);
Rui Ueyama25992482016-03-22 20:52:10 +000050 OS << Buffer;
51}
52
Davide Italiano786d8e32016-09-29 00:40:08 +000053static void diagnosticHandler(const DiagnosticInfo &DI) {
54 SmallString<128> ErrStorage;
55 raw_svector_ostream OS(ErrStorage);
56 DiagnosticPrinterRawOStream DP(OS);
57 DI.print(DP);
Rui Ueyamad31e13f2016-09-29 21:00:23 +000058 warn(ErrStorage);
Rui Ueyama25992482016-03-22 20:52:10 +000059}
60
Rui Ueyama6c5cbff2016-09-29 21:00:26 +000061static void checkError(Error E) {
Mehdi Aminic1edf562016-11-11 04:29:25 +000062 handleAllErrors(std::move(E), [&](ErrorInfoBase &EIB) -> Error {
Rui Ueyama6c5cbff2016-09-29 21:00:26 +000063 error(EIB.message());
64 return Error::success();
65 });
66}
67
Davide Italiano786d8e32016-09-29 00:40:08 +000068static std::unique_ptr<lto::LTO> createLTO() {
69 lto::Config Conf;
Davide Italianod26c4a12016-05-15 19:29:38 +000070
Davide Italiano786d8e32016-09-29 00:40:08 +000071 // LLD supports the new relocations.
72 Conf.Options = InitTargetOptionsFromCodeGenFlags();
73 Conf.Options.RelaxELFRelocations = true;
Davide Italianodf24d5b2016-06-02 22:58:11 +000074
Davide Italiano786d8e32016-09-29 00:40:08 +000075 Conf.RelocModel = Config->Pic ? Reloc::PIC_ : Reloc::Static;
76 Conf.DisableVerify = Config->DisableVerify;
77 Conf.DiagHandler = diagnosticHandler;
Davide Italiano3bfa0812016-11-26 05:37:04 +000078 Conf.OptLevel = Config->LTOO;
Davide Italianodf24d5b2016-06-02 22:58:11 +000079
Davide Italiano786d8e32016-09-29 00:40:08 +000080 // Set up a custom pipeline if we've been asked to.
Davide Italiano3bfa0812016-11-26 05:37:04 +000081 Conf.OptPipeline = Config->LTONewPmPasses;
82 Conf.AAPipeline = Config->LTOAAPipeline;
Rui Ueyama25992482016-03-22 20:52:10 +000083
84 if (Config->SaveTemps)
Rui Ueyama6c5cbff2016-09-29 21:00:26 +000085 checkError(Conf.addSaveTemps(std::string(Config->OutputFile) + ".",
George Rimara4c7e742016-10-20 08:36:42 +000086 /*UseInputModulePath*/ true));
Davide Italiano786d8e32016-09-29 00:40:08 +000087
Davide Italiano7a7b35a2016-10-10 18:12:53 +000088 lto::ThinBackend Backend;
Davide Italiano3bfa0812016-11-26 05:37:04 +000089 if (Config->ThinLTOJobs != -1u)
90 Backend = lto::createInProcessThinBackend(Config->ThinLTOJobs);
Davide Italianob6e6e4a2016-10-10 23:12:14 +000091 return llvm::make_unique<lto::LTO>(std::move(Conf), Backend,
Davide Italiano3bfa0812016-11-26 05:37:04 +000092 Config->LTOPartitions);
Rui Ueyama25992482016-03-22 20:52:10 +000093}
94
Davide Italiano3bfa0812016-11-26 05:37:04 +000095BitcodeCompiler::BitcodeCompiler() : LTOObj(createLTO()) {}
Rafael Espindolaae605c12016-04-21 20:35:25 +000096
Eugene Zelenko22886a22016-11-05 01:00:56 +000097BitcodeCompiler::~BitcodeCompiler() = default;
Rui Ueyama412c8022016-04-22 21:16:18 +000098
Peter Collingbourne0ef38742016-05-12 19:46:14 +000099static void undefine(Symbol *S) {
Rui Ueyama434b5612016-07-17 03:11:46 +0000100 replaceBody<Undefined>(S, S->body()->getName(), STV_DEFAULT, S->body()->Type,
101 nullptr);
Peter Collingbourne0ef38742016-05-12 19:46:14 +0000102}
103
Rui Ueyama25992482016-03-22 20:52:10 +0000104void BitcodeCompiler::add(BitcodeFile &F) {
Davide Italiano786d8e32016-09-29 00:40:08 +0000105 lto::InputFile &Obj = *F.Obj;
Davide Italiano786d8e32016-09-29 00:40:08 +0000106 unsigned SymNum = 0;
107 std::vector<Symbol *> Syms = F.getSymbols();
108 std::vector<lto::SymbolResolution> Resols(Syms.size());
Davide Italiano334fce92016-05-11 01:07:22 +0000109
Davide Italiano786d8e32016-09-29 00:40:08 +0000110 // Provide a resolution to the LTO API for each symbol.
111 for (const lto::InputFile::Symbol &ObjSym : Obj.symbols()) {
112 Symbol *Sym = Syms[SymNum];
113 lto::SymbolResolution &R = Resols[SymNum];
114 ++SymNum;
115 SymbolBody *B = Sym->body();
Davide Italiano86f2bd52016-03-29 21:46:35 +0000116
Davide Italiano786d8e32016-09-29 00:40:08 +0000117 // Ideally we shouldn't check for SF_Undefined but currently IRObjectFile
118 // reports two symbols for module ASM defined. Without this check, lld
119 // flags an undefined in IR with a definition in ASM as prevailing.
120 // Once IRObjectFile is fixed to report only one symbol this hack can
121 // be removed.
122 R.Prevailing =
123 !(ObjSym.getFlags() & object::BasicSymbolRef::SF_Undefined) &&
124 B->File == &F;
Peter Collingbourne3ad1c1e2016-05-05 17:13:49 +0000125
Davide Italiano786d8e32016-09-29 00:40:08 +0000126 R.VisibleToRegularObj =
127 Sym->IsUsedInRegularObj || (R.Prevailing && Sym->includeInDynsym());
128 if (R.Prevailing)
129 undefine(Sym);
Rui Ueyama25992482016-03-22 20:52:10 +0000130 }
Davide Italiano3bfa0812016-11-26 05:37:04 +0000131 checkError(LTOObj->add(std::move(F.Obj), Resols));
Davide Italianobc176632016-04-15 22:38:10 +0000132}
133
Rui Ueyama25992482016-03-22 20:52:10 +0000134// Merge all the bitcode files we have seen, codegen the result
Davide Italiano786d8e32016-09-29 00:40:08 +0000135// and return the resulting ObjectFile(s).
Rui Ueyama38dbd3e2016-09-14 00:05:51 +0000136std::vector<InputFile *> BitcodeCompiler::compile() {
Davide Italiano786d8e32016-09-29 00:40:08 +0000137 std::vector<InputFile *> Ret;
Davide Italiano3bfa0812016-11-26 05:37:04 +0000138 unsigned MaxTasks = LTOObj->getMaxTasks();
Davide Italiano786d8e32016-09-29 00:40:08 +0000139 Buff.resize(MaxTasks);
Davide Italiano828ac5412016-03-28 15:44:21 +0000140
Davide Italiano3bfa0812016-11-26 05:37:04 +0000141 checkError(LTOObj->run([&](size_t Task) {
Davide Italiano786d8e32016-09-29 00:40:08 +0000142 return llvm::make_unique<lto::NativeObjectStream>(
Eugene Zelenko22886a22016-11-05 01:00:56 +0000143 llvm::make_unique<raw_svector_ostream>(Buff[Task]));
Rui Ueyama5b8a3b32016-09-29 22:50:37 +0000144 }));
Rafael Espindolaabf6c652016-04-17 23:20:08 +0000145
Davide Italiano786d8e32016-09-29 00:40:08 +0000146 for (unsigned I = 0; I != MaxTasks; ++I) {
147 if (Buff[I].empty())
148 continue;
149 if (Config->SaveTemps) {
150 if (MaxTasks == 1)
151 saveBuffer(Buff[I], Config->OutputFile + ".lto.o");
152 else
153 saveBuffer(Buff[I], Config->OutputFile + Twine(I) + ".lto.o");
154 }
Rui Ueyama55518e72016-10-28 20:57:25 +0000155 InputFile *Obj = createObjectFile(MemoryBufferRef(Buff[I], "lto.tmp"));
Davide Italiano786d8e32016-09-29 00:40:08 +0000156 Ret.push_back(Obj);
157 }
158 return Ret;
Rui Ueyama961f2ff2016-03-23 21:19:27 +0000159}