2017-02-02 23:58:14 +00:00
|
|
|
//===- LTO.cpp ------------------------------------------------------------===//
|
|
|
|
//
|
2019-01-19 08:50:56 +00:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2017-02-02 23:58:14 +00:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "LTO.h"
|
|
|
|
#include "Config.h"
|
|
|
|
#include "InputFiles.h"
|
|
|
|
#include "Symbols.h"
|
2019-01-30 20:46:18 +00:00
|
|
|
#include "lld/Common/Args.h"
|
[lld] unified COFF and ELF error handling on new Common/ErrorHandler
Summary:
The COFF linker and the ELF linker have long had similar but separate
Error.h and Error.cpp files to implement error handling. This change
introduces new error handling code in Common/ErrorHandler.h, changes the
COFF and ELF linkers to use it, and removes the old, separate
implementations.
Reviewers: ruiu
Reviewed By: ruiu
Subscribers: smeenai, jyknight, emaste, sdardis, nemanjai, nhaehnle, mgorny, javed.absar, kbarton, fedor.sergeev, llvm-commits
Differential Revision: https://reviews.llvm.org/D39259
llvm-svn: 316624
2017-10-25 22:28:38 +00:00
|
|
|
#include "lld/Common/ErrorHandler.h"
|
2018-05-22 20:20:25 +00:00
|
|
|
#include "lld/Common/Strings.h"
|
2017-10-02 21:00:41 +00:00
|
|
|
#include "lld/Common/TargetOptionsCommandFlags.h"
|
2017-02-02 23:58:14 +00:00
|
|
|
#include "llvm/ADT/STLExtras.h"
|
|
|
|
#include "llvm/ADT/SmallString.h"
|
|
|
|
#include "llvm/ADT/StringRef.h"
|
|
|
|
#include "llvm/ADT/Twine.h"
|
|
|
|
#include "llvm/IR/DiagnosticPrinter.h"
|
2017-09-08 00:50:50 +00:00
|
|
|
#include "llvm/LTO/Caching.h"
|
2017-02-02 23:58:14 +00:00
|
|
|
#include "llvm/LTO/Config.h"
|
|
|
|
#include "llvm/LTO/LTO.h"
|
|
|
|
#include "llvm/Object/SymbolicFile.h"
|
|
|
|
#include "llvm/Support/CodeGen.h"
|
|
|
|
#include "llvm/Support/Error.h"
|
|
|
|
#include "llvm/Support/FileSystem.h"
|
|
|
|
#include "llvm/Support/MemoryBuffer.h"
|
|
|
|
#include "llvm/Support/raw_ostream.h"
|
|
|
|
#include <algorithm>
|
|
|
|
#include <cstddef>
|
|
|
|
#include <memory>
|
|
|
|
#include <string>
|
|
|
|
#include <system_error>
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
using namespace llvm::object;
|
|
|
|
|
|
|
|
using namespace lld;
|
|
|
|
using namespace lld::coff;
|
|
|
|
|
|
|
|
static std::unique_ptr<lto::LTO> createLTO() {
|
2018-05-22 20:20:25 +00:00
|
|
|
lto::Config C;
|
2019-02-01 02:24:50 +00:00
|
|
|
C.Options = initTargetOptionsFromCodeGenFlags();
|
2018-03-14 20:25:41 +00:00
|
|
|
|
|
|
|
// Always emit a section per function/datum with LTO. LLVM LTO should get most
|
|
|
|
// of the benefit of linker GC, but there are still opportunities for ICF.
|
2018-05-22 20:20:25 +00:00
|
|
|
C.Options.FunctionSections = true;
|
|
|
|
C.Options.DataSections = true;
|
2018-03-14 20:25:41 +00:00
|
|
|
|
2017-10-11 00:46:58 +00:00
|
|
|
// Use static reloc model on 32-bit x86 because it usually results in more
|
|
|
|
// compact code, and because there are also known code generation bugs when
|
|
|
|
// using the PIC model (see PR34306).
|
|
|
|
if (Config->Machine == COFF::IMAGE_FILE_MACHINE_I386)
|
2018-05-22 20:20:25 +00:00
|
|
|
C.RelocModel = Reloc::Static;
|
2017-10-11 00:46:58 +00:00
|
|
|
else
|
2018-05-22 20:20:25 +00:00
|
|
|
C.RelocModel = Reloc::PIC_;
|
|
|
|
C.DisableVerify = true;
|
|
|
|
C.DiagHandler = diagnosticHandler;
|
|
|
|
C.OptLevel = Config->LTOO;
|
2019-02-01 02:24:50 +00:00
|
|
|
C.CPU = getCPUStr();
|
|
|
|
C.MAttrs = getMAttrs();
|
2019-01-30 20:46:18 +00:00
|
|
|
C.CGOptLevel = args::getCGOptLevel(Config->LTOO);
|
Set MAttrs in LTO mode
Summary: Without this patch, MAttrs are not set.
Patch by Yin Ma
Reviewers: espindola, MaskRay, ruiu, pcc
Reviewed By: MaskRay, pcc
Subscribers: pcc, emaste, sbc100, inglorion, arichardson, aheejin, steven_wu, llvm-commits
Differential Revision: https://reviews.llvm.org/D53446
llvm-svn: 345884
2018-11-01 20:02:49 +00:00
|
|
|
|
2017-02-08 18:36:41 +00:00
|
|
|
if (Config->SaveTemps)
|
2018-05-22 20:20:25 +00:00
|
|
|
checkError(C.addSaveTemps(std::string(Config->OutputFile) + ".",
|
|
|
|
/*UseInputModulePath*/ true));
|
2017-02-02 23:58:14 +00:00
|
|
|
lto::ThinBackend Backend;
|
2018-05-22 20:20:25 +00:00
|
|
|
if (Config->ThinLTOJobs != 0)
|
|
|
|
Backend = lto::createInProcessThinBackend(Config->ThinLTOJobs);
|
|
|
|
return llvm::make_unique<lto::LTO>(std::move(C), Backend,
|
2017-02-02 23:58:14 +00:00
|
|
|
Config->LTOPartitions);
|
|
|
|
}
|
|
|
|
|
|
|
|
BitcodeCompiler::BitcodeCompiler() : LTOObj(createLTO()) {}
|
|
|
|
|
|
|
|
BitcodeCompiler::~BitcodeCompiler() = default;
|
|
|
|
|
2017-11-03 22:48:47 +00:00
|
|
|
static void undefine(Symbol *S) { replaceSymbol<Undefined>(S, S->getName()); }
|
2017-02-02 23:58:14 +00:00
|
|
|
|
|
|
|
void BitcodeCompiler::add(BitcodeFile &F) {
|
|
|
|
lto::InputFile &Obj = *F.Obj;
|
|
|
|
unsigned SymNum = 0;
|
2017-11-03 21:21:47 +00:00
|
|
|
std::vector<Symbol *> SymBodies = F.getSymbols();
|
2017-02-02 23:58:14 +00:00
|
|
|
std::vector<lto::SymbolResolution> Resols(SymBodies.size());
|
|
|
|
|
|
|
|
// Provide a resolution to the LTO API for each symbol.
|
|
|
|
for (const lto::InputFile::Symbol &ObjSym : Obj.symbols()) {
|
2017-11-03 21:21:47 +00:00
|
|
|
Symbol *Sym = SymBodies[SymNum];
|
2017-02-02 23:58:14 +00:00
|
|
|
lto::SymbolResolution &R = Resols[SymNum];
|
|
|
|
++SymNum;
|
|
|
|
|
|
|
|
// Ideally we shouldn't check for SF_Undefined but currently IRObjectFile
|
|
|
|
// reports two symbols for module ASM defined. Without this check, lld
|
|
|
|
// flags an undefined in IR with a definition in ASM as prevailing.
|
|
|
|
// Once IRObjectFile is fixed to report only one symbol this hack can
|
|
|
|
// be removed.
|
2017-10-31 16:10:24 +00:00
|
|
|
R.Prevailing = !ObjSym.isUndefined() && Sym->getFile() == &F;
|
2017-02-02 23:58:14 +00:00
|
|
|
R.VisibleToRegularObj = Sym->IsUsedInRegularObj;
|
|
|
|
if (R.Prevailing)
|
|
|
|
undefine(Sym);
|
|
|
|
}
|
|
|
|
checkError(LTOObj->add(std::move(F.Obj), Resols));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Merge all the bitcode files we have seen, codegen the result
|
2017-02-06 20:47:55 +00:00
|
|
|
// and return the resulting objects.
|
|
|
|
std::vector<StringRef> BitcodeCompiler::compile() {
|
2017-02-02 23:58:14 +00:00
|
|
|
unsigned MaxTasks = LTOObj->getMaxTasks();
|
2018-05-22 20:20:25 +00:00
|
|
|
Buf.resize(MaxTasks);
|
2017-09-08 00:50:50 +00:00
|
|
|
Files.resize(MaxTasks);
|
|
|
|
|
|
|
|
// The /lldltocache option specifies the path to a directory in which to cache
|
|
|
|
// native object files for ThinLTO incremental builds. If a path was
|
|
|
|
// specified, configure LTO to use it as the cache directory.
|
|
|
|
lto::NativeObjectCache Cache;
|
|
|
|
if (!Config->LTOCache.empty())
|
2018-02-20 20:21:59 +00:00
|
|
|
Cache = check(lto::localCache(
|
|
|
|
Config->LTOCache, [&](size_t Task, std::unique_ptr<MemoryBuffer> MB) {
|
|
|
|
Files[Task] = std::move(MB);
|
|
|
|
}));
|
2017-09-08 00:50:50 +00:00
|
|
|
|
|
|
|
checkError(LTOObj->run(
|
|
|
|
[&](size_t Task) {
|
|
|
|
return llvm::make_unique<lto::NativeObjectStream>(
|
2018-05-22 20:20:25 +00:00
|
|
|
llvm::make_unique<raw_svector_ostream>(Buf[Task]));
|
2017-09-08 00:50:50 +00:00
|
|
|
},
|
|
|
|
Cache));
|
|
|
|
|
|
|
|
if (!Config->LTOCache.empty())
|
|
|
|
pruneCache(Config->LTOCache, Config->LTOCachePolicy);
|
2017-02-02 23:58:14 +00:00
|
|
|
|
2017-02-06 20:47:55 +00:00
|
|
|
std::vector<StringRef> Ret;
|
2017-02-08 18:36:41 +00:00
|
|
|
for (unsigned I = 0; I != MaxTasks; ++I) {
|
2018-05-22 20:20:25 +00:00
|
|
|
if (Buf[I].empty())
|
2017-02-08 18:36:41 +00:00
|
|
|
continue;
|
|
|
|
if (Config->SaveTemps) {
|
|
|
|
if (I == 0)
|
2018-05-22 20:20:25 +00:00
|
|
|
saveBuffer(Buf[I], Config->OutputFile + ".lto.obj");
|
2017-02-08 18:36:41 +00:00
|
|
|
else
|
2018-05-22 20:20:25 +00:00
|
|
|
saveBuffer(Buf[I], Config->OutputFile + Twine(I) + ".lto.obj");
|
2017-02-08 18:36:41 +00:00
|
|
|
}
|
2018-05-22 20:20:25 +00:00
|
|
|
Ret.emplace_back(Buf[I].data(), Buf[I].size());
|
2017-02-08 18:36:41 +00:00
|
|
|
}
|
2017-09-08 00:50:50 +00:00
|
|
|
|
|
|
|
for (std::unique_ptr<MemoryBuffer> &File : Files)
|
|
|
|
if (File)
|
|
|
|
Ret.push_back(File->getBuffer());
|
|
|
|
|
2017-02-02 23:58:14 +00:00
|
|
|
return Ret;
|
|
|
|
}
|