2016-03-23 04:52:10 +08:00
|
|
|
//===- LTO.cpp ------------------------------------------------------------===//
|
|
|
|
//
|
2019-01-19 16:50:56 +08: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
|
2016-03-23 04:52:10 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "LTO.h"
|
|
|
|
#include "Config.h"
|
|
|
|
#include "InputFiles.h"
|
2017-10-03 05:00:41 +08:00
|
|
|
#include "LinkerScript.h"
|
2017-07-27 07:39:10 +08:00
|
|
|
#include "SymbolTable.h"
|
2016-03-23 04:52:10 +08:00
|
|
|
#include "Symbols.h"
|
2019-01-31 04:46:18 +08: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-26 06:28:38 +08:00
|
|
|
#include "lld/Common/ErrorHandler.h"
|
2017-10-03 05:00:41 +08:00
|
|
|
#include "lld/Common/TargetOptionsCommandFlags.h"
|
2016-11-05 09:00:56 +08:00
|
|
|
#include "llvm/ADT/STLExtras.h"
|
2016-11-06 06:37:59 +08:00
|
|
|
#include "llvm/ADT/SmallString.h"
|
2016-11-05 09:00:56 +08:00
|
|
|
#include "llvm/ADT/StringRef.h"
|
|
|
|
#include "llvm/ADT/Twine.h"
|
2017-06-07 11:48:56 +08:00
|
|
|
#include "llvm/BinaryFormat/ELF.h"
|
2018-05-03 05:40:07 +08:00
|
|
|
#include "llvm/Bitcode/BitcodeReader.h"
|
|
|
|
#include "llvm/Bitcode/BitcodeWriter.h"
|
2016-09-29 08:40:08 +08:00
|
|
|
#include "llvm/IR/DiagnosticPrinter.h"
|
2016-11-05 09:00:56 +08:00
|
|
|
#include "llvm/LTO/Config.h"
|
2016-09-29 08:40:08 +08:00
|
|
|
#include "llvm/LTO/LTO.h"
|
2016-11-05 09:00:56 +08:00
|
|
|
#include "llvm/Object/SymbolicFile.h"
|
2021-10-19 09:40:57 +08:00
|
|
|
#include "llvm/Support/Caching.h"
|
2016-11-05 09:00:56 +08:00
|
|
|
#include "llvm/Support/CodeGen.h"
|
|
|
|
#include "llvm/Support/Error.h"
|
|
|
|
#include "llvm/Support/FileSystem.h"
|
|
|
|
#include "llvm/Support/MemoryBuffer.h"
|
|
|
|
#include <algorithm>
|
|
|
|
#include <cstddef>
|
|
|
|
#include <memory>
|
|
|
|
#include <string>
|
|
|
|
#include <system_error>
|
|
|
|
#include <vector>
|
2016-03-23 04:52:10 +08:00
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
using namespace llvm::object;
|
|
|
|
using namespace llvm::ELF;
|
2020-05-15 13:18:58 +08:00
|
|
|
using namespace lld;
|
|
|
|
using namespace lld::elf;
|
2016-03-23 04:52:10 +08:00
|
|
|
|
2018-05-08 01:46:28 +08:00
|
|
|
// Creates an empty file to store a list of object files for final
|
2018-05-03 05:40:07 +08:00
|
|
|
// linking of distributed ThinLTO.
|
2018-05-08 01:46:28 +08:00
|
|
|
static std::unique_ptr<raw_fd_ostream> openFile(StringRef file) {
|
2018-05-03 05:40:07 +08:00
|
|
|
std::error_code ec;
|
2018-05-08 01:46:28 +08:00
|
|
|
auto ret =
|
2019-08-15 06:28:17 +08:00
|
|
|
std::make_unique<raw_fd_ostream>(file, ec, sys::fs::OpenFlags::OF_None);
|
2018-05-08 06:11:24 +08:00
|
|
|
if (ec) {
|
|
|
|
error("cannot open " + file + ": " + ec.message());
|
|
|
|
return nullptr;
|
|
|
|
}
|
2018-05-08 01:46:28 +08:00
|
|
|
return ret;
|
2018-05-03 05:40:07 +08:00
|
|
|
}
|
|
|
|
|
2021-02-18 00:52:24 +08:00
|
|
|
// The merged bitcode after LTO is large. Try opening a file stream that
|
Flush bitcode incrementally for LTO output
Bitcode writer does not flush buffer until the end by default. This is
fine to small bitcode files. When -flto,--plugin-opt=emit-llvm,-gmlt are
used, the final bitcode file is large, for example, >8G. Keeping all
data in memory consumes a lot of memory.
This change allows bitcode writer flush data to disk early when buffered
data size is above some threshold. This is only enabled when lld emits
LLVM bitcode.
One issue to address is backpatching bitcode: subblock length, function
body indexes, meta data indexes need to backfill. If buffer can be
flushed partially, we introduced raw_fd_stream that supports
read/seek/write, and enables backpatching bitcode flushed in disk.
Reviewed-by: tejohnson, MaskRay
Differential Revision: https://reviews.llvm.org/D86905
2020-09-13 03:35:17 +08:00
|
|
|
// supports reading, seeking and writing. Such a file allows BitcodeWriter to
|
2021-02-18 00:52:24 +08:00
|
|
|
// flush buffered data to reduce memory consumption. If this fails, open a file
|
Flush bitcode incrementally for LTO output
Bitcode writer does not flush buffer until the end by default. This is
fine to small bitcode files. When -flto,--plugin-opt=emit-llvm,-gmlt are
used, the final bitcode file is large, for example, >8G. Keeping all
data in memory consumes a lot of memory.
This change allows bitcode writer flush data to disk early when buffered
data size is above some threshold. This is only enabled when lld emits
LLVM bitcode.
One issue to address is backpatching bitcode: subblock length, function
body indexes, meta data indexes need to backfill. If buffer can be
flushed partially, we introduced raw_fd_stream that supports
read/seek/write, and enables backpatching bitcode flushed in disk.
Reviewed-by: tejohnson, MaskRay
Differential Revision: https://reviews.llvm.org/D86905
2020-09-13 03:35:17 +08:00
|
|
|
// stream that supports only write.
|
|
|
|
static std::unique_ptr<raw_fd_ostream> openLTOOutputFile(StringRef file) {
|
|
|
|
std::error_code ec;
|
|
|
|
std::unique_ptr<raw_fd_ostream> fs =
|
|
|
|
std::make_unique<raw_fd_stream>(file, ec);
|
|
|
|
if (!ec)
|
|
|
|
return fs;
|
|
|
|
return openFile(file);
|
|
|
|
}
|
|
|
|
|
2018-05-08 06:11:24 +08:00
|
|
|
static std::string getThinLTOOutputFile(StringRef modulePath) {
|
2020-01-29 03:23:46 +08:00
|
|
|
return lto::getThinLTOOutputFile(
|
|
|
|
std::string(modulePath), std::string(config->thinLTOPrefixReplace.first),
|
|
|
|
std::string(config->thinLTOPrefixReplace.second));
|
2018-05-08 06:11:24 +08:00
|
|
|
}
|
|
|
|
|
2018-05-08 07:24:07 +08:00
|
|
|
static lto::Config createConfig() {
|
|
|
|
lto::Config c;
|
2016-05-16 03:29:38 +08:00
|
|
|
|
2018-08-07 04:12:12 +08:00
|
|
|
// LLD supports the new relocations and address-significance tables.
|
2019-02-01 10:24:50 +08:00
|
|
|
c.Options = initTargetOptionsFromCodeGenFlags();
|
2018-05-08 07:24:07 +08:00
|
|
|
c.Options.RelaxELFRelocations = true;
|
2018-08-07 04:12:12 +08:00
|
|
|
c.Options.EmitAddrsig = true;
|
2016-05-16 03:29:38 +08:00
|
|
|
|
2017-07-25 04:15:07 +08:00
|
|
|
// Always emit a section per function/datum with LTO.
|
2018-05-08 07:24:07 +08:00
|
|
|
c.Options.FunctionSections = true;
|
|
|
|
c.Options.DataSections = true;
|
2017-07-25 03:38:13 +08:00
|
|
|
|
2020-04-07 21:48:18 +08:00
|
|
|
// Check if basic block sections must be used.
|
2020-08-01 02:14:49 +08:00
|
|
|
// Allowed values for --lto-basic-block-sections are "all", "labels",
|
2020-04-07 21:48:18 +08:00
|
|
|
// "<file name specifying basic block ids>", or none. This is the equivalent
|
2020-06-02 14:17:29 +08:00
|
|
|
// of -fbasic-block-sections= flag in clang.
|
2020-04-07 21:48:18 +08:00
|
|
|
if (!config->ltoBasicBlockSections.empty()) {
|
|
|
|
if (config->ltoBasicBlockSections == "all") {
|
|
|
|
c.Options.BBSections = BasicBlockSection::All;
|
|
|
|
} else if (config->ltoBasicBlockSections == "labels") {
|
|
|
|
c.Options.BBSections = BasicBlockSection::Labels;
|
|
|
|
} else if (config->ltoBasicBlockSections == "none") {
|
|
|
|
c.Options.BBSections = BasicBlockSection::None;
|
|
|
|
} else {
|
|
|
|
ErrorOr<std::unique_ptr<MemoryBuffer>> MBOrErr =
|
|
|
|
MemoryBuffer::getFile(config->ltoBasicBlockSections.str());
|
|
|
|
if (!MBOrErr) {
|
|
|
|
error("cannot open " + config->ltoBasicBlockSections + ":" +
|
|
|
|
MBOrErr.getError().message());
|
|
|
|
} else {
|
|
|
|
c.Options.BBSectionsFuncListBuf = std::move(*MBOrErr);
|
|
|
|
}
|
|
|
|
c.Options.BBSections = BasicBlockSection::List;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-02 14:17:29 +08:00
|
|
|
c.Options.UniqueBasicBlockSectionNames =
|
|
|
|
config->ltoUniqueBasicBlockSectionNames;
|
2020-04-07 21:48:18 +08:00
|
|
|
|
2019-07-21 05:59:47 +08:00
|
|
|
if (auto relocModel = getRelocModelFromCMModel())
|
|
|
|
c.RelocModel = *relocModel;
|
|
|
|
else if (config->relocatable)
|
2018-05-08 07:24:07 +08:00
|
|
|
c.RelocModel = None;
|
2017-05-23 05:11:44 +08:00
|
|
|
else if (config->isPic)
|
2018-05-08 07:24:07 +08:00
|
|
|
c.RelocModel = Reloc::PIC_;
|
2017-05-23 05:11:44 +08:00
|
|
|
else
|
2018-05-08 07:24:07 +08:00
|
|
|
c.RelocModel = Reloc::Static;
|
|
|
|
|
2019-02-01 10:24:50 +08:00
|
|
|
c.CodeModel = getCodeModelFromCMModel();
|
2018-05-08 07:24:07 +08:00
|
|
|
c.DisableVerify = config->disableVerify;
|
|
|
|
c.DiagHandler = diagnosticHandler;
|
|
|
|
c.OptLevel = config->ltoo;
|
2019-02-01 10:24:50 +08:00
|
|
|
c.CPU = getCPUStr();
|
|
|
|
c.MAttrs = getMAttrs();
|
2019-01-31 04:46:18 +08:00
|
|
|
c.CGOptLevel = args::getCGOptLevel(config->ltoo);
|
2016-05-16 03:29:38 +08:00
|
|
|
|
2020-01-10 12:58:31 +08:00
|
|
|
c.PTO.LoopVectorization = c.OptLevel > 1;
|
|
|
|
c.PTO.SLPVectorization = c.OptLevel > 1;
|
|
|
|
|
2016-09-29 08:40:08 +08:00
|
|
|
// Set up a custom pipeline if we've been asked to.
|
2020-01-29 03:23:46 +08:00
|
|
|
c.OptPipeline = std::string(config->ltoNewPmPasses);
|
|
|
|
c.AAPipeline = std::string(config->ltoAAPipeline);
|
2016-03-23 04:52:10 +08:00
|
|
|
|
2017-02-14 01:49:18 +08:00
|
|
|
// Set up optimization remarks if we've been asked to.
|
2020-01-29 03:23:46 +08:00
|
|
|
c.RemarksFilename = std::string(config->optRemarksFilename);
|
|
|
|
c.RemarksPasses = std::string(config->optRemarksPasses);
|
2018-05-08 07:24:07 +08:00
|
|
|
c.RemarksWithHotness = config->optRemarksWithHotness;
|
2020-11-18 02:37:59 +08:00
|
|
|
c.RemarksHotnessThreshold = config->optRemarksHotnessThreshold;
|
2020-01-29 03:23:46 +08:00
|
|
|
c.RemarksFormat = std::string(config->optRemarksFormat);
|
[Coding style change] Rename variables so that they start with a lowercase letter
This patch is mechanically generated by clang-llvm-rename tool that I wrote
using Clang Refactoring Engine just for creating this patch. You can see the
source code of the tool at https://reviews.llvm.org/D64123. There's no manual
post-processing; you can generate the same patch by re-running the tool against
lld's code base.
Here is the main discussion thread to change the LLVM coding style:
https://lists.llvm.org/pipermail/llvm-dev/2019-February/130083.html
In the discussion thread, I proposed we use lld as a testbed for variable
naming scheme change, and this patch does that.
I chose to rename variables so that they are in camelCase, just because that
is a minimal change to make variables to start with a lowercase letter.
Note to downstream patch maintainers: if you are maintaining a downstream lld
repo, just rebasing ahead of this commit would cause massive merge conflicts
because this patch essentially changes every line in the lld subdirectory. But
there's a remedy.
clang-llvm-rename tool is a batch tool, so you can rename variables in your
downstream repo with the tool. Given that, here is how to rebase your repo to
a commit after the mass renaming:
1. rebase to the commit just before the mass variable renaming,
2. apply the tool to your downstream repo to mass-rename variables locally, and
3. rebase again to the head.
Most changes made by the tool should be identical for a downstream repo and
for the head, so at the step 3, almost all changes should be merged and
disappear. I'd expect that there would be some lines that you need to merge by
hand, but that shouldn't be too many.
Differential Revision: https://reviews.llvm.org/D64121
llvm-svn: 365595
2019-07-10 13:00:37 +08:00
|
|
|
|
2020-01-29 03:23:46 +08:00
|
|
|
c.SampleProfile = std::string(config->ltoSampleProfile);
|
2018-05-08 07:24:07 +08:00
|
|
|
c.UseNewPM = config->ltoNewPassManager;
|
|
|
|
c.DebugPassManager = config->ltoDebugPassManager;
|
2020-01-29 03:23:46 +08:00
|
|
|
c.DwoDir = std::string(config->dwoDir);
|
[Coding style change] Rename variables so that they start with a lowercase letter
This patch is mechanically generated by clang-llvm-rename tool that I wrote
using Clang Refactoring Engine just for creating this patch. You can see the
source code of the tool at https://reviews.llvm.org/D64123. There's no manual
post-processing; you can generate the same patch by re-running the tool against
lld's code base.
Here is the main discussion thread to change the LLVM coding style:
https://lists.llvm.org/pipermail/llvm-dev/2019-February/130083.html
In the discussion thread, I proposed we use lld as a testbed for variable
naming scheme change, and this patch does that.
I chose to rename variables so that they are in camelCase, just because that
is a minimal change to make variables to start with a lowercase letter.
Note to downstream patch maintainers: if you are maintaining a downstream lld
repo, just rebasing ahead of this commit would cause massive merge conflicts
because this patch essentially changes every line in the lld subdirectory. But
there's a remedy.
clang-llvm-rename tool is a batch tool, so you can rename variables in your
downstream repo with the tool. Given that, here is how to rebase your repo to
a commit after the mass renaming:
1. rebase to the commit just before the mass variable renaming,
2. apply the tool to your downstream repo to mass-rename variables locally, and
3. rebase again to the head.
Most changes made by the tool should be identical for a downstream repo and
for the head, so at the step 3, almost all changes should be merged and
disappear. I'd expect that there would be some lines that you need to merge by
hand, but that shouldn't be too many.
Differential Revision: https://reviews.llvm.org/D64121
llvm-svn: 365595
2019-07-10 13:00:37 +08:00
|
|
|
|
2020-01-25 04:24:18 +08:00
|
|
|
c.HasWholeProgramVisibility = config->ltoWholeProgramVisibility;
|
[LTO] Suppress emission of empty combined module by default
Summary:
That unless the user requested an output object (--lto-obj-path), the an
unused empty combined module is not emitted.
This changed is helpful for some target (ex. RISCV-V) which encoded the
ABI info in IR module flags (target-abi). Empty unused module has no ABI
info so the linker would get the linking error during merging
incompatible ABIs.
Reviewers: tejohnson, espindola, MaskRay
Subscribers: emaste, inglorion, arichardson, hiraditya, simoncook, MaskRay, steven_wu, dexonsmith, PkmX, dang, lenary, s.egerton, luismarques, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D78988
2020-04-28 11:04:36 +08:00
|
|
|
c.AlwaysEmitRegularLTOObj = !config->ltoObjPath.empty();
|
2020-01-25 04:24:18 +08:00
|
|
|
|
2020-05-22 04:19:44 +08:00
|
|
|
for (const llvm::StringRef &name : config->thinLTOModulesToCompile)
|
|
|
|
c.ThinLTOModulesToCompile.emplace_back(name);
|
|
|
|
|
2020-01-29 00:05:13 +08:00
|
|
|
c.TimeTraceEnabled = config->timeTraceEnabled;
|
|
|
|
c.TimeTraceGranularity = config->timeTraceGranularity;
|
|
|
|
|
2020-01-29 03:23:46 +08:00
|
|
|
c.CSIRProfile = std::string(config->ltoCSProfileFile);
|
2019-03-12 06:51:38 +08:00
|
|
|
c.RunCSIRInstr = config->ltoCSProfileGenerate;
|
2021-08-12 00:45:55 +08:00
|
|
|
c.PGOWarnMismatch = config->ltoPGOWarnMismatch;
|
[Coding style change] Rename variables so that they start with a lowercase letter
This patch is mechanically generated by clang-llvm-rename tool that I wrote
using Clang Refactoring Engine just for creating this patch. You can see the
source code of the tool at https://reviews.llvm.org/D64123. There's no manual
post-processing; you can generate the same patch by re-running the tool against
lld's code base.
Here is the main discussion thread to change the LLVM coding style:
https://lists.llvm.org/pipermail/llvm-dev/2019-February/130083.html
In the discussion thread, I proposed we use lld as a testbed for variable
naming scheme change, and this patch does that.
I chose to rename variables so that they are in camelCase, just because that
is a minimal change to make variables to start with a lowercase letter.
Note to downstream patch maintainers: if you are maintaining a downstream lld
repo, just rebasing ahead of this commit would cause massive merge conflicts
because this patch essentially changes every line in the lld subdirectory. But
there's a remedy.
clang-llvm-rename tool is a batch tool, so you can rename variables in your
downstream repo with the tool. Given that, here is how to rebase your repo to
a commit after the mass renaming:
1. rebase to the commit just before the mass variable renaming,
2. apply the tool to your downstream repo to mass-rename variables locally, and
3. rebase again to the head.
Most changes made by the tool should be identical for a downstream repo and
for the head, so at the step 3, almost all changes should be merged and
disappear. I'd expect that there would be some lines that you need to merge by
hand, but that shouldn't be too many.
Differential Revision: https://reviews.llvm.org/D64121
llvm-svn: 365595
2019-07-10 13:00:37 +08:00
|
|
|
|
2018-12-15 05:58:49 +08:00
|
|
|
if (config->emitLLVM) {
|
|
|
|
c.PostInternalizeModuleHook = [](size_t task, const Module &m) {
|
Flush bitcode incrementally for LTO output
Bitcode writer does not flush buffer until the end by default. This is
fine to small bitcode files. When -flto,--plugin-opt=emit-llvm,-gmlt are
used, the final bitcode file is large, for example, >8G. Keeping all
data in memory consumes a lot of memory.
This change allows bitcode writer flush data to disk early when buffered
data size is above some threshold. This is only enabled when lld emits
LLVM bitcode.
One issue to address is backpatching bitcode: subblock length, function
body indexes, meta data indexes need to backfill. If buffer can be
flushed partially, we introduced raw_fd_stream that supports
read/seek/write, and enables backpatching bitcode flushed in disk.
Reviewed-by: tejohnson, MaskRay
Differential Revision: https://reviews.llvm.org/D86905
2020-09-13 03:35:17 +08:00
|
|
|
if (std::unique_ptr<raw_fd_ostream> os =
|
|
|
|
openLTOOutputFile(config->outputFile))
|
2018-12-15 05:58:49 +08:00
|
|
|
WriteBitcodeToFile(m, *os, false);
|
|
|
|
return false;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
[lld] Support --lto-emit-asm and --plugin-opt=emit-asm
Summary: The switch --plugin-opt=emit-asm can be used with the gold linker to dump the final assembly code generated by LTO in a user-friendly way. Unfortunately it doesn't work with lld. I'm hooking it up with lld. With that switch, lld emits assembly code into the output file (specified by -o) and if there are multiple input files, each of their assembly code will be emitted into a separate file named by suffixing the output file name with a unique number, respectively. The linking then stops after generating those assembly files.
Reviewers: espindola, wenlei, tejohnson, MaskRay, grimar
Reviewed By: tejohnson, MaskRay, grimar
Subscribers: pcc, emaste, inglorion, arichardson, hiraditya, MaskRay, steven_wu, dexonsmith, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D77231
2020-04-02 01:01:23 +08:00
|
|
|
if (config->ltoEmitAsm)
|
|
|
|
c.CGFileType = CGFT_AssemblyFile;
|
|
|
|
|
2016-03-23 04:52:10 +08:00
|
|
|
if (config->saveTemps)
|
2018-05-08 07:24:07 +08:00
|
|
|
checkError(c.addSaveTemps(config->outputFile.str() + ".",
|
|
|
|
/*UseInputModulePath*/ true));
|
|
|
|
return c;
|
|
|
|
}
|
2016-03-23 04:52:10 +08:00
|
|
|
|
2018-05-08 07:24:07 +08:00
|
|
|
BitcodeCompiler::BitcodeCompiler() {
|
2019-07-16 13:50:45 +08:00
|
|
|
// Initialize indexFile.
|
2018-09-11 22:37:27 +08:00
|
|
|
if (!config->thinLTOIndexOnlyArg.empty())
|
|
|
|
indexFile = openFile(config->thinLTOIndexOnlyArg);
|
|
|
|
|
2019-07-16 13:50:45 +08:00
|
|
|
// Initialize ltoObj.
|
2016-10-11 02:12:53 +08:00
|
|
|
lto::ThinBackend backend;
|
2018-05-03 05:40:07 +08:00
|
|
|
if (config->thinLTOIndexOnly) {
|
2018-09-11 22:37:27 +08:00
|
|
|
auto onIndexWrite = [&](StringRef s) { thinIndices.erase(s); };
|
2018-05-08 01:59:43 +08:00
|
|
|
backend = lto::createWriteIndexesThinBackend(
|
2020-01-29 03:23:46 +08:00
|
|
|
std::string(config->thinLTOPrefixReplace.first),
|
|
|
|
std::string(config->thinLTOPrefixReplace.second),
|
2018-05-09 04:12:07 +08:00
|
|
|
config->thinLTOEmitImportsFiles, indexFile.get(), onIndexWrite);
|
2020-03-27 22:20:39 +08:00
|
|
|
} else {
|
|
|
|
backend = lto::createInProcessThinBackend(
|
|
|
|
llvm::heavyweight_hardware_concurrency(config->thinLTOJobs));
|
2018-05-03 05:40:07 +08:00
|
|
|
}
|
|
|
|
|
2019-08-15 06:28:17 +08:00
|
|
|
ltoObj = std::make_unique<lto::LTO>(createConfig(), backend,
|
2018-05-08 01:46:28 +08:00
|
|
|
config->ltoPartitions);
|
|
|
|
|
2019-07-16 13:50:45 +08:00
|
|
|
// Initialize usedStartStop.
|
2019-11-21 03:16:15 +08:00
|
|
|
for (Symbol *sym : symtab->symbols()) {
|
2018-09-11 22:37:27 +08:00
|
|
|
StringRef s = sym->getName();
|
2017-07-27 07:39:10 +08:00
|
|
|
for (StringRef prefix : {"__start_", "__stop_"})
|
2018-09-11 22:37:27 +08:00
|
|
|
if (s.startswith(prefix))
|
|
|
|
usedStartStop.insert(s.substr(prefix.size()));
|
2019-11-21 03:16:15 +08:00
|
|
|
}
|
2017-07-27 07:39:10 +08:00
|
|
|
}
|
2016-09-29 08:40:08 +08:00
|
|
|
|
2016-11-05 09:00:56 +08:00
|
|
|
BitcodeCompiler::~BitcodeCompiler() = default;
|
2016-04-23 05:16:18 +08:00
|
|
|
|
2017-02-01 18:26:03 +08:00
|
|
|
void BitcodeCompiler::add(BitcodeFile &f) {
|
2016-09-29 08:40:08 +08:00
|
|
|
lto::InputFile &obj = *f.obj;
|
2018-05-09 01:50:54 +08:00
|
|
|
bool isExec = !config->shared && !config->relocatable;
|
2018-05-03 05:40:07 +08:00
|
|
|
|
2018-05-09 04:12:07 +08:00
|
|
|
if (config->thinLTOIndexOnly)
|
2018-09-11 22:37:27 +08:00
|
|
|
thinIndices.insert(obj.getName());
|
2018-05-03 05:40:07 +08:00
|
|
|
|
2018-05-09 01:50:43 +08:00
|
|
|
ArrayRef<Symbol *> syms = f.getSymbols();
|
2018-05-09 01:50:54 +08:00
|
|
|
ArrayRef<lto::InputFile::Symbol> objSyms = obj.symbols();
|
2016-09-29 08:40:08 +08:00
|
|
|
std::vector<lto::SymbolResolution> resols(syms.size());
|
|
|
|
|
|
|
|
// Provide a resolution to the LTO API for each symbol.
|
2018-05-09 01:50:54 +08:00
|
|
|
for (size_t i = 0, e = syms.size(); i != e; ++i) {
|
|
|
|
Symbol *sym = syms[i];
|
|
|
|
const lto::InputFile::Symbol &objSym = objSyms[i];
|
|
|
|
lto::SymbolResolution &r = resols[i];
|
2016-09-29 08:40:08 +08:00
|
|
|
|
|
|
|
// 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-11-30 06:47:35 +08:00
|
|
|
r.Prevailing = !objSym.isUndefined() && sym->file == &f;
|
2016-09-29 08:40:08 +08:00
|
|
|
|
2017-08-22 16:36:54 +08:00
|
|
|
// We ask LTO to preserve following global symbols:
|
|
|
|
// 1) All symbols when doing relocatable link, so that them can be used
|
|
|
|
// for doing final link.
|
|
|
|
// 2) Symbols that are used in regular objects.
|
|
|
|
// 3) C named sections if we have corresponding __start_/__stop_ symbol.
|
|
|
|
// 4) Symbols that are defined in bitcode files and used for dynamic linking.
|
|
|
|
r.VisibleToRegularObj = config->relocatable || sym->isUsedInRegularObj ||
|
2017-07-26 06:51:05 +08:00
|
|
|
(r.Prevailing && sym->includeInDynsym()) ||
|
2017-07-27 07:39:10 +08:00
|
|
|
usedStartStop.count(objSym.getSectionName());
|
2020-12-31 07:56:53 +08:00
|
|
|
// Identify symbols exported dynamically, and that therefore could be
|
|
|
|
// referenced by a shared library not visible to the linker.
|
2021-03-10 14:33:47 +08:00
|
|
|
r.ExportDynamic = sym->computeBinding() != STB_LOCAL &&
|
|
|
|
(sym->isExportDynamic(sym->kind(), sym->visibility) ||
|
|
|
|
sym->exportDynamic || sym->inDynamicList);
|
2018-02-07 08:49:51 +08:00
|
|
|
const auto *dr = dyn_cast<Defined>(sym);
|
2018-01-17 00:49:05 +08:00
|
|
|
r.FinalDefinitionInLinkageUnit =
|
2018-05-09 01:50:54 +08:00
|
|
|
(isExec || sym->visibility != STV_DEFAULT) && dr &&
|
2018-02-07 08:49:51 +08:00
|
|
|
// Skip absolute symbols from ELF objects, otherwise PC-rel relocations
|
|
|
|
// will be generated by for them, triggering linker errors.
|
|
|
|
// Symbol section is always null for bitcode symbols, hence the check
|
2018-02-08 12:25:52 +08:00
|
|
|
// for isElf(). Skip linker script defined symbols as well: they have
|
|
|
|
// no File defined.
|
|
|
|
!(dr->section == nullptr && (!sym->file || sym->file->isElf()));
|
2018-01-17 00:49:05 +08:00
|
|
|
|
2016-09-29 08:40:08 +08:00
|
|
|
if (r.Prevailing)
|
2019-05-20 11:36:33 +08:00
|
|
|
sym->replace(Undefined{nullptr, sym->getName(), STB_GLOBAL, STV_DEFAULT,
|
|
|
|
sym->type});
|
2017-09-25 17:31:43 +08:00
|
|
|
|
2018-01-30 17:04:27 +08:00
|
|
|
// We tell LTO to not apply interprocedural optimization for wrapped
|
|
|
|
// (with --wrap) symbols because otherwise LTO would inline them while
|
|
|
|
// their values are still not final.
|
|
|
|
r.LinkerRedefined = !sym->canInline;
|
2016-07-15 10:17:13 +08:00
|
|
|
}
|
2016-11-26 13:37:04 +08:00
|
|
|
checkError(ltoObj->add(std::move(f.obj), resols));
|
2016-04-16 06:38:10 +08:00
|
|
|
}
|
|
|
|
|
2019-05-02 22:05:20 +08:00
|
|
|
// If LazyObjFile has not been added to link, emit empty index files.
|
|
|
|
// This is needed because this is what GNU gold plugin does and we have a
|
|
|
|
// distributed build system that depends on that behavior.
|
2019-05-07 18:40:26 +08:00
|
|
|
static void thinLTOCreateEmptyIndexFiles() {
|
2019-05-02 22:05:20 +08:00
|
|
|
for (LazyObjFile *f : lazyObjFiles) {
|
2021-11-27 02:58:50 +08:00
|
|
|
if (f->extracted || !isBitcode(f->mb))
|
2019-05-02 22:05:20 +08:00
|
|
|
continue;
|
|
|
|
std::string path = replaceThinLTOSuffix(getThinLTOOutputFile(f->getName()));
|
|
|
|
std::unique_ptr<raw_fd_ostream> os = openFile(path + ".thinlto.bc");
|
|
|
|
if (!os)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
ModuleSummaryIndex m(/*HaveGVs*/ false);
|
|
|
|
m.setSkipModuleByDistributedBackend();
|
|
|
|
WriteIndexToFile(m, *os);
|
|
|
|
if (config->thinLTOEmitImportsFiles)
|
|
|
|
openFile(path + ".imports");
|
|
|
|
}
|
2018-05-18 02:27:12 +08:00
|
|
|
}
|
|
|
|
|
2016-03-23 04:52:10 +08:00
|
|
|
// Merge all the bitcode files we have seen, codegen the result
|
2016-09-29 08:40:08 +08:00
|
|
|
// and return the resulting ObjectFile(s).
|
2016-09-14 08:05:51 +08:00
|
|
|
std::vector<InputFile *> BitcodeCompiler::compile() {
|
2016-11-26 13:37:04 +08:00
|
|
|
unsigned maxTasks = ltoObj->getMaxTasks();
|
2018-05-18 02:27:12 +08:00
|
|
|
buf.resize(maxTasks);
|
2017-03-02 07:00:10 +08:00
|
|
|
files.resize(maxTasks);
|
|
|
|
|
|
|
|
// The --thinlto-cache-dir 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.
|
2021-11-05 03:59:59 +08:00
|
|
|
FileCache cache;
|
2017-03-02 07:00:10 +08:00
|
|
|
if (!config->thinLTOCacheDir.empty())
|
2021-10-19 09:40:57 +08:00
|
|
|
cache =
|
|
|
|
check(localCache("ThinLTO", "Thin", config->thinLTOCacheDir,
|
|
|
|
[&](size_t task, std::unique_ptr<MemoryBuffer> mb) {
|
|
|
|
files[task] = std::move(mb);
|
|
|
|
}));
|
2017-03-02 07:00:10 +08:00
|
|
|
|
2019-05-07 18:40:26 +08:00
|
|
|
if (!bitcodeFiles.empty())
|
|
|
|
checkError(ltoObj->run(
|
|
|
|
[&](size_t task) {
|
2021-11-05 03:59:59 +08:00
|
|
|
return std::make_unique<CachedFileStream>(
|
2019-08-15 06:28:17 +08:00
|
|
|
std::make_unique<raw_svector_ostream>(buf[task]));
|
2019-05-07 18:40:26 +08:00
|
|
|
},
|
|
|
|
cache));
|
2016-04-18 07:20:08 +08:00
|
|
|
|
2020-05-22 04:19:44 +08:00
|
|
|
// Emit empty index files for non-indexed files but not in single-module mode.
|
|
|
|
if (config->thinLTOModulesToCompile.empty()) {
|
|
|
|
for (StringRef s : thinIndices) {
|
|
|
|
std::string path = getThinLTOOutputFile(s);
|
|
|
|
openFile(path + ".thinlto.bc");
|
|
|
|
if (config->thinLTOEmitImportsFiles)
|
|
|
|
openFile(path + ".imports");
|
|
|
|
}
|
2016-09-29 08:40:08 +08:00
|
|
|
}
|
2017-03-02 07:00:10 +08:00
|
|
|
|
2018-05-08 06:11:24 +08:00
|
|
|
if (config->thinLTOIndexOnly) {
|
2019-05-02 22:05:20 +08:00
|
|
|
thinLTOCreateEmptyIndexFiles();
|
2018-05-08 06:11:24 +08:00
|
|
|
|
2018-05-09 06:37:57 +08:00
|
|
|
if (!config->ltoObjPath.empty())
|
2018-05-18 02:27:12 +08:00
|
|
|
saveBuffer(buf[0], config->ltoObjPath);
|
2018-05-09 04:12:07 +08:00
|
|
|
|
2018-05-08 06:11:24 +08:00
|
|
|
// ThinLTO with index only option is required to generate only the index
|
|
|
|
// files. After that, we exit from linker and ThinLTO backend runs in a
|
|
|
|
// distributed environment.
|
2018-05-08 06:11:34 +08:00
|
|
|
if (indexFile)
|
|
|
|
indexFile->close();
|
|
|
|
return {};
|
2018-05-08 06:11:24 +08:00
|
|
|
}
|
2018-05-18 02:27:12 +08:00
|
|
|
|
2018-05-09 04:12:07 +08:00
|
|
|
if (!config->thinLTOCacheDir.empty())
|
|
|
|
pruneCache(config->thinLTOCacheDir, config->thinLTOCachePolicy);
|
|
|
|
|
Output ELF files after ThinLTO is run.
Summary:
The gold linker allowed you to output the ELF files after LTO was run. It did
it by using the 'obj-path' option. This replicates that behavior.
Reviewers: espindola, ruiu, MaskRay, pcc
Reviewed By: MaskRay, pcc
Subscribers: grimar, emaste, inglorion, arichardson, steven_wu, dexonsmith, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D56046
llvm-svn: 354917
2019-02-27 03:29:14 +08:00
|
|
|
if (!config->ltoObjPath.empty()) {
|
|
|
|
saveBuffer(buf[0], config->ltoObjPath);
|
|
|
|
for (unsigned i = 1; i != maxTasks; ++i)
|
|
|
|
saveBuffer(buf[i], config->ltoObjPath + Twine(i));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (config->saveTemps) {
|
[LTO] Suppress emission of empty combined module by default
Summary:
That unless the user requested an output object (--lto-obj-path), the an
unused empty combined module is not emitted.
This changed is helpful for some target (ex. RISCV-V) which encoded the
ABI info in IR module flags (target-abi). Empty unused module has no ABI
info so the linker would get the linking error during merging
incompatible ABIs.
Reviewers: tejohnson, espindola, MaskRay
Subscribers: emaste, inglorion, arichardson, hiraditya, simoncook, MaskRay, steven_wu, dexonsmith, PkmX, dang, lenary, s.egerton, luismarques, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D78988
2020-04-28 11:04:36 +08:00
|
|
|
if (!buf[0].empty())
|
|
|
|
saveBuffer(buf[0], config->outputFile + ".lto.o");
|
Output ELF files after ThinLTO is run.
Summary:
The gold linker allowed you to output the ELF files after LTO was run. It did
it by using the 'obj-path' option. This replicates that behavior.
Reviewers: espindola, ruiu, MaskRay, pcc
Reviewed By: MaskRay, pcc
Subscribers: grimar, emaste, inglorion, arichardson, steven_wu, dexonsmith, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D56046
llvm-svn: 354917
2019-02-27 03:29:14 +08:00
|
|
|
for (unsigned i = 1; i != maxTasks; ++i)
|
|
|
|
saveBuffer(buf[i], config->outputFile + Twine(i) + ".lto.o");
|
2018-05-09 04:12:07 +08:00
|
|
|
}
|
2018-05-03 05:40:07 +08:00
|
|
|
|
[lld] Support --lto-emit-asm and --plugin-opt=emit-asm
Summary: The switch --plugin-opt=emit-asm can be used with the gold linker to dump the final assembly code generated by LTO in a user-friendly way. Unfortunately it doesn't work with lld. I'm hooking it up with lld. With that switch, lld emits assembly code into the output file (specified by -o) and if there are multiple input files, each of their assembly code will be emitted into a separate file named by suffixing the output file name with a unique number, respectively. The linking then stops after generating those assembly files.
Reviewers: espindola, wenlei, tejohnson, MaskRay, grimar
Reviewed By: tejohnson, MaskRay, grimar
Subscribers: pcc, emaste, inglorion, arichardson, hiraditya, MaskRay, steven_wu, dexonsmith, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D77231
2020-04-02 01:01:23 +08:00
|
|
|
if (config->ltoEmitAsm) {
|
|
|
|
saveBuffer(buf[0], config->outputFile);
|
|
|
|
for (unsigned i = 1; i != maxTasks; ++i)
|
|
|
|
saveBuffer(buf[i], config->outputFile + Twine(i));
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
Output ELF files after ThinLTO is run.
Summary:
The gold linker allowed you to output the ELF files after LTO was run. It did
it by using the 'obj-path' option. This replicates that behavior.
Reviewers: espindola, ruiu, MaskRay, pcc
Reviewed By: MaskRay, pcc
Subscribers: grimar, emaste, inglorion, arichardson, steven_wu, dexonsmith, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D56046
llvm-svn: 354917
2019-02-27 03:29:14 +08:00
|
|
|
std::vector<InputFile *> ret;
|
|
|
|
for (unsigned i = 0; i != maxTasks; ++i)
|
|
|
|
if (!buf[i].empty())
|
|
|
|
ret.push_back(createObjectFile(MemoryBufferRef(buf[i], "lto.tmp")));
|
|
|
|
|
2017-03-02 07:00:10 +08:00
|
|
|
for (std::unique_ptr<MemoryBuffer> &file : files)
|
|
|
|
if (file)
|
|
|
|
ret.push_back(createObjectFile(*file));
|
2016-09-29 08:40:08 +08:00
|
|
|
return ret;
|
2016-03-24 05:19:27 +08:00
|
|
|
}
|