1 //===- LTO.cpp ------------------------------------------------------------===//
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
13 #include "InputFiles.h"
15 #include "llvm/ADT/STLExtras.h"
16 #include "llvm/ADT/SmallString.h"
17 #include "llvm/ADT/StringRef.h"
18 #include "llvm/ADT/Twine.h"
19 #include "llvm/CodeGen/CommandFlags.h"
20 #include "llvm/IR/DiagnosticPrinter.h"
21 #include "llvm/LTO/Config.h"
22 #include "llvm/LTO/LTO.h"
23 #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"
34 #include <system_error>
38 using namespace llvm::object;
39 using namespace llvm::ELF;
42 using namespace lld::elf;
44 // This is for use when debugging LTO.
45 static void saveBuffer(StringRef Buffer, const Twine &Path) {
47 raw_fd_ostream OS(Path.str(), EC, sys::fs::OpenFlags::F_None);
49 error(EC, "cannot create " + Path);
53 static void diagnosticHandler(const DiagnosticInfo &DI) {
54 SmallString<128> ErrStorage;
55 raw_svector_ostream OS(ErrStorage);
56 DiagnosticPrinterRawOStream DP(OS);
61 static void checkError(Error E) {
62 handleAllErrors(std::move(E), [&](ErrorInfoBase &EIB) -> Error {
64 return Error::success();
68 static std::unique_ptr<lto::LTO> createLTO() {
71 // LLD supports the new relocations.
72 Conf.Options = InitTargetOptionsFromCodeGenFlags();
73 Conf.Options.RelaxELFRelocations = true;
75 Conf.RelocModel = Config->Pic ? Reloc::PIC_ : Reloc::Static;
76 Conf.DisableVerify = Config->DisableVerify;
77 Conf.DiagHandler = diagnosticHandler;
78 Conf.OptLevel = Config->LTOO;
80 // Set up a custom pipeline if we've been asked to.
81 Conf.OptPipeline = Config->LTONewPmPasses;
82 Conf.AAPipeline = Config->LTOAAPipeline;
84 if (Config->SaveTemps)
85 checkError(Conf.addSaveTemps(std::string(Config->OutputFile) + ".",
86 /*UseInputModulePath*/ true));
88 lto::ThinBackend Backend;
89 if (Config->ThinLTOJobs != -1u)
90 Backend = lto::createInProcessThinBackend(Config->ThinLTOJobs);
91 return llvm::make_unique<lto::LTO>(std::move(Conf), Backend,
92 Config->LTOPartitions);
95 BitcodeCompiler::BitcodeCompiler() : LTOObj(createLTO()) {}
97 BitcodeCompiler::~BitcodeCompiler() = default;
99 template <class ELFT> static void undefine(Symbol *S) {
100 replaceBody<Undefined<ELFT>>(S, S->body()->getName(), /*IsLocal=*/false,
101 STV_DEFAULT, S->body()->Type, nullptr);
104 template <class ELFT> void BitcodeCompiler::add(BitcodeFile &F) {
105 lto::InputFile &Obj = *F.Obj;
107 std::vector<Symbol *> Syms = F.getSymbols();
108 std::vector<lto::SymbolResolution> Resols(Syms.size());
110 // 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];
115 SymbolBody *B = Sym->body();
117 // 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
123 !(ObjSym.getFlags() & object::BasicSymbolRef::SF_Undefined) &&
126 R.VisibleToRegularObj =
127 Sym->IsUsedInRegularObj || (R.Prevailing && Sym->includeInDynsym());
131 checkError(LTOObj->add(std::move(F.Obj), Resols));
134 // Merge all the bitcode files we have seen, codegen the result
135 // and return the resulting ObjectFile(s).
136 std::vector<InputFile *> BitcodeCompiler::compile() {
137 std::vector<InputFile *> Ret;
138 unsigned MaxTasks = LTOObj->getMaxTasks();
139 Buff.resize(MaxTasks);
141 checkError(LTOObj->run([&](size_t Task) {
142 return llvm::make_unique<lto::NativeObjectStream>(
143 llvm::make_unique<raw_svector_ostream>(Buff[Task]));
146 for (unsigned I = 0; I != MaxTasks; ++I) {
149 if (Config->SaveTemps) {
151 saveBuffer(Buff[I], Config->OutputFile + ".lto.o");
153 saveBuffer(Buff[I], Config->OutputFile + Twine(I) + ".lto.o");
155 InputFile *Obj = createObjectFile(MemoryBufferRef(Buff[I], "lto.tmp"));
161 template void BitcodeCompiler::template add<ELF32LE>(BitcodeFile &);
162 template void BitcodeCompiler::template add<ELF32BE>(BitcodeFile &);
163 template void BitcodeCompiler::template add<ELF64LE>(BitcodeFile &);
164 template void BitcodeCompiler::template add<ELF64BE>(BitcodeFile &);