2016-03-22 20:52:10 +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
|
2016-03-22 20:52:10 +00:00
|
|
|
//
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
|
|
#include "LTO.h"
|
|
|
|
|
#include "Config.h"
|
|
|
|
|
#include "InputFiles.h"
|
2017-07-26 23:39:10 +00:00
|
|
|
#include "SymbolTable.h"
|
2016-03-22 20:52:10 +00:00
|
|
|
#include "Symbols.h"
|
2019-01-30 20:46:18 +00:00
|
|
|
#include "lld/Common/Args.h"
|
[ELF] --save-temps --lto-emit-asm: derive ELF/asm file names from bitcode file names
Port COFF's https://reviews.llvm.org/D78221 and
https://reviews.llvm.org/D137217 to ELF. For the in-process ThinLTO
link, `ld.lld --save-temps a.o d/b.o -o out` will create
ELF relocatable files `out.lto.a.o`/`d/out.lto.b.o` instead of
`out1.lto.o`/`out2.lto.o`. Deriving the LTO-generated relocatable file
name from bitcode file names helps debugging.
The relocatable file name from the first regular LTO partition does not
change: `out.lto.o`. The second, if present due to `--lto-partition=`,
changes from `out1.lto.o` to `lto.1.o`.
For an archive member, e.g. `d/a.a(coll.o at 8)`,
the relocatable file is `d/out.lto.a.a(coll.o at 8).o`.
`--lto-emit-asm` file names are changed similarly. `--lto-emit-asm -o
out` now creates `out.lto.s` instead of `out`, therefore the
`--lto-emit-asm -o -` idiom no longer works. However, I think this new
behavior (which matches COFF) is better since keeping or removing
`--lto-emit-asm` will dump different files, instead of overwriting the
`-o` output file from an executable/shared object to an assembly file.
Reviewers: rnk, igorkudrin, xur-llvm, teresajohnson, ZequanWu
Reviewed By: teresajohnson
Pull Request: https://github.com/llvm/llvm-project/pull/78835
2024-01-23 11:38:15 -08:00
|
|
|
#include "lld/Common/CommonLinkerContext.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"
|
2023-09-28 00:06:48 +02:00
|
|
|
#include "lld/Common/Filesystem.h"
|
2022-02-07 21:53:34 -08:00
|
|
|
#include "lld/Common/Strings.h"
|
2017-10-02 21:00:41 +00:00
|
|
|
#include "lld/Common/TargetOptionsCommandFlags.h"
|
2016-11-05 22:37:59 +00:00
|
|
|
#include "llvm/ADT/SmallString.h"
|
2016-11-05 01:00:56 +00:00
|
|
|
#include "llvm/ADT/StringRef.h"
|
|
|
|
|
#include "llvm/ADT/Twine.h"
|
2017-06-07 03:48:56 +00:00
|
|
|
#include "llvm/BinaryFormat/ELF.h"
|
2018-05-02 21:40:07 +00:00
|
|
|
#include "llvm/Bitcode/BitcodeWriter.h"
|
2016-11-05 01:00:56 +00:00
|
|
|
#include "llvm/LTO/Config.h"
|
2016-09-29 00:40:08 +00:00
|
|
|
#include "llvm/LTO/LTO.h"
|
2021-10-18 18:40:57 -07:00
|
|
|
#include "llvm/Support/Caching.h"
|
2016-11-05 01:00:56 +00:00
|
|
|
#include "llvm/Support/CodeGen.h"
|
|
|
|
|
#include "llvm/Support/Error.h"
|
|
|
|
|
#include "llvm/Support/FileSystem.h"
|
|
|
|
|
#include "llvm/Support/MemoryBuffer.h"
|
[ELF] --save-temps --lto-emit-asm: derive ELF/asm file names from bitcode file names
Port COFF's https://reviews.llvm.org/D78221 and
https://reviews.llvm.org/D137217 to ELF. For the in-process ThinLTO
link, `ld.lld --save-temps a.o d/b.o -o out` will create
ELF relocatable files `out.lto.a.o`/`d/out.lto.b.o` instead of
`out1.lto.o`/`out2.lto.o`. Deriving the LTO-generated relocatable file
name from bitcode file names helps debugging.
The relocatable file name from the first regular LTO partition does not
change: `out.lto.o`. The second, if present due to `--lto-partition=`,
changes from `out1.lto.o` to `lto.1.o`.
For an archive member, e.g. `d/a.a(coll.o at 8)`,
the relocatable file is `d/out.lto.a.a(coll.o at 8).o`.
`--lto-emit-asm` file names are changed similarly. `--lto-emit-asm -o
out` now creates `out.lto.s` instead of `out`, therefore the
`--lto-emit-asm -o -` idiom no longer works. However, I think this new
behavior (which matches COFF) is better since keeping or removing
`--lto-emit-asm` will dump different files, instead of overwriting the
`-o` output file from an executable/shared object to an assembly file.
Reviewers: rnk, igorkudrin, xur-llvm, teresajohnson, ZequanWu
Reviewed By: teresajohnson
Pull Request: https://github.com/llvm/llvm-project/pull/78835
2024-01-23 11:38:15 -08:00
|
|
|
#include "llvm/Support/Path.h"
|
2016-11-05 01:00:56 +00:00
|
|
|
#include <algorithm>
|
|
|
|
|
#include <cstddef>
|
|
|
|
|
#include <memory>
|
|
|
|
|
#include <string>
|
|
|
|
|
#include <system_error>
|
|
|
|
|
#include <vector>
|
2016-03-22 20:52:10 +00:00
|
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
using namespace llvm::object;
|
|
|
|
|
using namespace llvm::ELF;
|
2020-05-14 22:18:58 -07:00
|
|
|
using namespace lld;
|
|
|
|
|
using namespace lld::elf;
|
2016-03-22 20:52:10 +00:00
|
|
|
|
2018-05-07 22:11:24 +00:00
|
|
|
static std::string getThinLTOOutputFile(StringRef modulePath) {
|
2023-04-26 20:43:11 -07:00
|
|
|
return lto::getThinLTOOutputFile(modulePath, config->thinLTOPrefixReplaceOld,
|
|
|
|
|
config->thinLTOPrefixReplaceNew);
|
2018-05-07 22:11:24 +00:00
|
|
|
}
|
|
|
|
|
|
2018-05-07 23:24:07 +00:00
|
|
|
static lto::Config createConfig() {
|
|
|
|
|
lto::Config c;
|
2016-05-15 19:29:38 +00:00
|
|
|
|
2018-08-06 20:12:12 +00:00
|
|
|
// LLD supports the new relocations and address-significance tables.
|
2019-02-01 02:24:50 +00:00
|
|
|
c.Options = initTargetOptionsFromCodeGenFlags();
|
2018-08-06 20:12:12 +00:00
|
|
|
c.Options.EmitAddrsig = true;
|
2022-09-15 21:01:56 -07:00
|
|
|
for (StringRef C : config->mllvmOpts)
|
|
|
|
|
c.MllvmArgs.emplace_back(C.str());
|
2016-05-15 19:29:38 +00:00
|
|
|
|
2017-07-24 20:15:07 +00:00
|
|
|
// Always emit a section per function/datum with LTO.
|
2018-05-07 23:24:07 +00:00
|
|
|
c.Options.FunctionSections = true;
|
|
|
|
|
c.Options.DataSections = true;
|
2017-07-24 19:38:13 +00:00
|
|
|
|
[SHT_LLVM_BB_ADDR_MAP] Allow basic-block-sections and labels be used together by decoupling the handling of the two features. (#74128)
Today `-split-machine-functions` and `-fbasic-block-sections={all,list}`
cannot be combined with `-basic-block-sections=labels` (the labels
option will be ignored).
The inconsistency comes from the way basic block address map -- the
underlying mechanism for basic block labels -- encodes basic block
addresses
(https://lists.llvm.org/pipermail/llvm-dev/2020-July/143512.html).
Specifically, basic block offsets are computed relative to the function
begin symbol. This relies on functions being contiguous which is not the
case for MFS and basic block section binaries. This means Propeller
cannot use binary profiles collected from these binaries, which limits
the applicability of Propeller for iterative optimization.
To make the `SHT_LLVM_BB_ADDR_MAP` feature work with basic block section
binaries, we propose modifying the encoding of this section as follows.
First let us review the current encoding which emits the address of each
function and its number of basic blocks, followed by basic block entries
for each basic block.
| | |
|--|--|
| Address of the function | Function Address |
| Number of basic blocks in this function | NumBlocks |
| BB entry 1
| BB entry 2
| ...
| BB entry #NumBlocks
To make this work for basic block sections, we treat each basic block
section similar to a function, except that basic block sections of the
same function must be encapsulated in the same structure so we can map
all of them to their single function.
We modify the encoding to first emit the number of basic block sections
(BB ranges) in the function. Then we emit the address map of each basic
block section section as before: the base address of the section, its
number of blocks, and BB entries for its basic block. The first section
in the BB address map is always the function entry section.
| | |
|--|--|
| Number of sections for this function | NumBBRanges |
| Section 1 begin address | BaseAddress[1] |
| Number of basic blocks in section 1 | NumBlocks[1] |
| BB entries for Section 1
|..................|
| Section #NumBBRanges begin address | BaseAddress[NumBBRanges] |
| Number of basic blocks in section #NumBBRanges |
NumBlocks[NumBBRanges] |
| BB entries for Section #NumBBRanges
The encoding of basic block entries remains as before with the minor
change that each basic block offset is now computed relative to the
begin symbol of its containing BB section.
This patch adds a new boolean codegen option `-basic-block-address-map`.
Correspondingly, the front-end flag `-fbasic-block-address-map` and LLD
flag `--lto-basic-block-address-map` are introduced.
Analogously, we add a new TargetOption field `BBAddrMap`. This means BB
address maps are either generated for all functions in the compiling
unit, or for none (depending on `TargetOptions::BBAddrMap`).
This patch keeps the functionality of the old
`-fbasic-block-sections=labels` option but does not remove it. A
subsequent patch will remove the obsolete option.
We refactor the `BasicBlockSections` pass by separating the BB address
map and BB sections handing to their own functions (named
`handleBBAddrMap` and `handleBBSections`). `handleBBSections` renumbers
basic blocks and places them in their assigned sections.
`handleBBAddrMap` is invoked after `handleBBSections` (if requested) and
only renumbers the blocks.
- New tests added:
- Two tests basic-block-address-map-with-basic-block-sections.ll and
basic-block-address-map-with-mfs.ll to exercise the combination of
`-basic-block-address-map` with `-basic-block-sections=list` and
'-split-machine-functions`.
- A driver sanity test for the `-fbasic-block-address-map` option
(basic-block-address-map.c).
- An LLD test for testing the `--lto-basic-block-address-map` option.
This reuses the LLVM IR from `lld/test/ELF/lto/basic-block-sections.ll`.
- Renamed and modified the two existing codegen tests for basic block
address map (`basic-block-sections-labels-functions-sections.ll` and
`basic-block-sections-labels.ll`)
- Removed `SHT_LLVM_BB_ADDR_MAP_V0` tests. Full deprecation of
`SHT_LLVM_BB_ADDR_MAP_V0` and `SHT_LLVM_BB_ADDR_MAP` version less than 2
will happen in a separate PR in a few months.
2024-02-01 17:50:46 -08:00
|
|
|
c.Options.BBAddrMap = config->ltoBBAddrMap;
|
|
|
|
|
|
2020-04-07 06:48:18 -07:00
|
|
|
// Check if basic block sections must be used.
|
2020-07-31 11:14:49 -07:00
|
|
|
// Allowed values for --lto-basic-block-sections are "all", "labels",
|
2020-04-07 06:48:18 -07:00
|
|
|
// "<file name specifying basic block ids>", or none. This is the equivalent
|
2020-06-01 23:17:29 -07:00
|
|
|
// of -fbasic-block-sections= flag in clang.
|
2020-04-07 06:48:18 -07: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-01 23:17:29 -07:00
|
|
|
c.Options.UniqueBasicBlockSectionNames =
|
|
|
|
|
config->ltoUniqueBasicBlockSectionNames;
|
2020-04-07 06:48:18 -07:00
|
|
|
|
2019-07-20 21:59:47 +00:00
|
|
|
if (auto relocModel = getRelocModelFromCMModel())
|
|
|
|
|
c.RelocModel = *relocModel;
|
|
|
|
|
else if (config->relocatable)
|
2022-12-02 23:12:36 -08:00
|
|
|
c.RelocModel = std::nullopt;
|
2017-05-22 21:11:44 +00:00
|
|
|
else if (config->isPic)
|
2018-05-07 23:24:07 +00:00
|
|
|
c.RelocModel = Reloc::PIC_;
|
2017-05-22 21:11:44 +00:00
|
|
|
else
|
2018-05-07 23:24:07 +00:00
|
|
|
c.RelocModel = Reloc::Static;
|
|
|
|
|
|
2019-02-01 02:24:50 +00:00
|
|
|
c.CodeModel = getCodeModelFromCMModel();
|
2018-05-07 23:24:07 +00:00
|
|
|
c.DisableVerify = config->disableVerify;
|
|
|
|
|
c.DiagHandler = diagnosticHandler;
|
|
|
|
|
c.OptLevel = config->ltoo;
|
2019-02-01 02:24:50 +00:00
|
|
|
c.CPU = getCPUStr();
|
|
|
|
|
c.MAttrs = getMAttrs();
|
2023-02-15 17:12:47 +00:00
|
|
|
c.CGOptLevel = config->ltoCgo;
|
2016-05-15 19:29:38 +00:00
|
|
|
|
2020-01-09 20:58:31 -08:00
|
|
|
c.PTO.LoopVectorization = c.OptLevel > 1;
|
|
|
|
|
c.PTO.SLPVectorization = c.OptLevel > 1;
|
|
|
|
|
|
2016-09-29 00:40:08 +00:00
|
|
|
// Set up a custom pipeline if we've been asked to.
|
2020-01-28 20:23:46 +01:00
|
|
|
c.OptPipeline = std::string(config->ltoNewPmPasses);
|
|
|
|
|
c.AAPipeline = std::string(config->ltoAAPipeline);
|
2016-03-22 20:52:10 +00:00
|
|
|
|
2017-02-13 17:49:18 +00:00
|
|
|
// Set up optimization remarks if we've been asked to.
|
2020-01-28 20:23:46 +01:00
|
|
|
c.RemarksFilename = std::string(config->optRemarksFilename);
|
|
|
|
|
c.RemarksPasses = std::string(config->optRemarksPasses);
|
2018-05-07 23:24:07 +00:00
|
|
|
c.RemarksWithHotness = config->optRemarksWithHotness;
|
2020-11-17 10:37:59 -08:00
|
|
|
c.RemarksHotnessThreshold = config->optRemarksHotnessThreshold;
|
2020-01-28 20:23:46 +01: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 05:00:37 +00:00
|
|
|
|
2022-03-17 11:53:44 +08:00
|
|
|
// Set up output file to emit statistics.
|
|
|
|
|
c.StatsFile = std::string(config->optStatsFilename);
|
|
|
|
|
|
2020-01-28 20:23:46 +01:00
|
|
|
c.SampleProfile = std::string(config->ltoSampleProfile);
|
2022-03-24 07:52:16 +01:00
|
|
|
for (StringRef pluginFn : config->passPlugins)
|
|
|
|
|
c.PassPlugins.push_back(std::string(pluginFn));
|
2018-05-07 23:24:07 +00:00
|
|
|
c.DebugPassManager = config->ltoDebugPassManager;
|
2020-01-28 20:23:46 +01: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 05:00:37 +00:00
|
|
|
|
2020-01-24 12:24:18 -08:00
|
|
|
c.HasWholeProgramVisibility = config->ltoWholeProgramVisibility;
|
2023-07-13 19:02:52 -07:00
|
|
|
c.ValidateAllVtablesHaveTypeInfos =
|
|
|
|
|
config->ltoValidateAllVtablesHaveTypeInfos;
|
|
|
|
|
c.AllVtablesHaveTypeInfos = ctx.ltoAllVtablesHaveTypeInfos;
|
[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-27 20:04:36 -07:00
|
|
|
c.AlwaysEmitRegularLTOObj = !config->ltoObjPath.empty();
|
2020-01-24 12:24:18 -08:00
|
|
|
|
2020-05-21 13:19:44 -07:00
|
|
|
for (const llvm::StringRef &name : config->thinLTOModulesToCompile)
|
|
|
|
|
c.ThinLTOModulesToCompile.emplace_back(name);
|
|
|
|
|
|
2020-01-28 16:05:13 +00:00
|
|
|
c.TimeTraceEnabled = config->timeTraceEnabled;
|
|
|
|
|
c.TimeTraceGranularity = config->timeTraceGranularity;
|
|
|
|
|
|
2020-01-28 20:23:46 +01:00
|
|
|
c.CSIRProfile = std::string(config->ltoCSProfileFile);
|
2019-03-11 22:51:38 +00:00
|
|
|
c.RunCSIRInstr = config->ltoCSProfileGenerate;
|
2021-08-11 09:45:55 -07: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 05:00:37 +00:00
|
|
|
|
2018-12-14 21:58:49 +00: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-12 19:35:17 +00:00
|
|
|
if (std::unique_ptr<raw_fd_ostream> os =
|
|
|
|
|
openLTOOutputFile(config->outputFile))
|
2018-12-14 21:58:49 +00:00
|
|
|
WriteBitcodeToFile(m, *os, false);
|
|
|
|
|
return false;
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-09 05:33:38 -05:00
|
|
|
if (config->ltoEmitAsm) {
|
2023-09-14 14:10:14 -07:00
|
|
|
c.CGFileType = CodeGenFileType::AssemblyFile;
|
2023-01-09 05:33:38 -05:00
|
|
|
c.Options.MCOptions.AsmVerbose = true;
|
|
|
|
|
}
|
[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-01 10:01:23 -07:00
|
|
|
|
2022-06-01 10:46:55 -07:00
|
|
|
if (!config->saveTempsArgs.empty())
|
2018-05-07 23:24:07 +00:00
|
|
|
checkError(c.addSaveTemps(config->outputFile.str() + ".",
|
2022-06-01 10:46:55 -07:00
|
|
|
/*UseInputModulePath*/ true,
|
|
|
|
|
config->saveTempsArgs));
|
2018-05-07 23:24:07 +00:00
|
|
|
return c;
|
|
|
|
|
}
|
2016-03-22 20:52:10 +00:00
|
|
|
|
2018-05-07 23:24:07 +00:00
|
|
|
BitcodeCompiler::BitcodeCompiler() {
|
2019-07-16 05:50:45 +00:00
|
|
|
// Initialize indexFile.
|
2018-09-11 14:37:27 +00:00
|
|
|
if (!config->thinLTOIndexOnlyArg.empty())
|
|
|
|
|
indexFile = openFile(config->thinLTOIndexOnlyArg);
|
|
|
|
|
|
2019-07-16 05:50:45 +00:00
|
|
|
// Initialize ltoObj.
|
2016-10-10 18:12:53 +00:00
|
|
|
lto::ThinBackend backend;
|
2022-06-01 10:49:36 -07:00
|
|
|
auto onIndexWrite = [&](StringRef s) { thinIndices.erase(s); };
|
2018-05-02 21:40:07 +00:00
|
|
|
if (config->thinLTOIndexOnly) {
|
2018-05-07 17:59:43 +00:00
|
|
|
backend = lto::createWriteIndexesThinBackend(
|
2023-04-04 09:57:53 -07:00
|
|
|
std::string(config->thinLTOPrefixReplaceOld),
|
|
|
|
|
std::string(config->thinLTOPrefixReplaceNew),
|
|
|
|
|
std::string(config->thinLTOPrefixReplaceNativeObject),
|
2018-05-08 20:12:07 +00:00
|
|
|
config->thinLTOEmitImportsFiles, indexFile.get(), onIndexWrite);
|
2020-03-27 10:20:39 -04:00
|
|
|
} else {
|
|
|
|
|
backend = lto::createInProcessThinBackend(
|
2022-06-01 10:49:36 -07:00
|
|
|
llvm::heavyweight_hardware_concurrency(config->thinLTOJobs),
|
|
|
|
|
onIndexWrite, config->thinLTOEmitIndexFiles,
|
|
|
|
|
config->thinLTOEmitImportsFiles);
|
2018-05-02 21:40:07 +00:00
|
|
|
}
|
|
|
|
|
|
2023-07-18 16:13:58 -07:00
|
|
|
constexpr llvm::lto::LTO::LTOKind ltoModes[3] =
|
|
|
|
|
{llvm::lto::LTO::LTOKind::LTOK_UnifiedThin,
|
|
|
|
|
llvm::lto::LTO::LTOKind::LTOK_UnifiedRegular,
|
|
|
|
|
llvm::lto::LTO::LTOKind::LTOK_Default};
|
|
|
|
|
ltoObj = std::make_unique<lto::LTO>(
|
|
|
|
|
createConfig(), backend, config->ltoPartitions,
|
|
|
|
|
ltoModes[config->ltoKind]);
|
2018-05-07 17:46:28 +00:00
|
|
|
|
2019-07-16 05:50:45 +00:00
|
|
|
// Initialize usedStartStop.
|
2022-10-01 12:06:33 -07:00
|
|
|
if (ctx.bitcodeFiles.empty())
|
2021-12-23 01:52:54 -08:00
|
|
|
return;
|
2022-10-01 14:46:49 -07:00
|
|
|
for (Symbol *sym : symtab.getSymbols()) {
|
2021-12-26 18:11:45 -08:00
|
|
|
if (sym->isPlaceholder())
|
|
|
|
|
continue;
|
2018-09-11 14:37:27 +00:00
|
|
|
StringRef s = sym->getName();
|
2017-07-26 23:39:10 +00:00
|
|
|
for (StringRef prefix : {"__start_", "__stop_"})
|
2023-06-05 14:36:19 -07:00
|
|
|
if (s.starts_with(prefix))
|
2018-09-11 14:37:27 +00:00
|
|
|
usedStartStop.insert(s.substr(prefix.size()));
|
2019-11-20 11:16:15 -08:00
|
|
|
}
|
2017-07-26 23:39:10 +00:00
|
|
|
}
|
2016-09-29 00:40:08 +00:00
|
|
|
|
2016-11-05 01:00:56 +00:00
|
|
|
BitcodeCompiler::~BitcodeCompiler() = default;
|
2016-04-22 21:16:18 +00:00
|
|
|
|
2017-02-01 10:26:03 +00:00
|
|
|
void BitcodeCompiler::add(BitcodeFile &f) {
|
2016-09-29 00:40:08 +00:00
|
|
|
lto::InputFile &obj = *f.obj;
|
2018-05-08 17:50:54 +00:00
|
|
|
bool isExec = !config->shared && !config->relocatable;
|
2018-05-02 21:40:07 +00:00
|
|
|
|
2022-06-01 10:49:36 -07:00
|
|
|
if (config->thinLTOEmitIndexFiles)
|
2018-09-11 14:37:27 +00:00
|
|
|
thinIndices.insert(obj.getName());
|
2018-05-02 21:40:07 +00:00
|
|
|
|
2018-05-08 17:50:43 +00:00
|
|
|
ArrayRef<Symbol *> syms = f.getSymbols();
|
2018-05-08 17:50:54 +00:00
|
|
|
ArrayRef<lto::InputFile::Symbol> objSyms = obj.symbols();
|
2016-09-29 00:40:08 +00:00
|
|
|
std::vector<lto::SymbolResolution> resols(syms.size());
|
|
|
|
|
|
|
|
|
|
// Provide a resolution to the LTO API for each symbol.
|
2018-05-08 17:50:54 +00: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 00:40:08 +00: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-29 22:47:35 +00:00
|
|
|
r.Prevailing = !objSym.isUndefined() && sym->file == &f;
|
2016-09-29 00:40:08 +00:00
|
|
|
|
2017-08-22 08:36:54 +00: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.
|
2022-04-19 18:04:17 -07:00
|
|
|
// 4) Symbols that are defined in bitcode files and used for dynamic
|
|
|
|
|
// linking.
|
|
|
|
|
// 5) Symbols that will be referenced after linker wrapping is performed.
|
2017-08-22 08:36:54 +00:00
|
|
|
r.VisibleToRegularObj = config->relocatable || sym->isUsedInRegularObj ||
|
2022-04-19 18:04:17 -07:00
|
|
|
sym->referencedAfterWrap ||
|
2017-07-25 22:51:05 +00:00
|
|
|
(r.Prevailing && sym->includeInDynsym()) ||
|
2017-07-26 23:39:10 +00:00
|
|
|
usedStartStop.count(objSym.getSectionName());
|
2020-12-30 15:56:53 -08:00
|
|
|
// Identify symbols exported dynamically, and that therefore could be
|
|
|
|
|
// referenced by a shared library not visible to the linker.
|
2022-02-24 11:31:58 -08:00
|
|
|
r.ExportDynamic =
|
|
|
|
|
sym->computeBinding() != STB_LOCAL &&
|
|
|
|
|
(config->exportDynamic || sym->exportDynamic || sym->inDynamicList);
|
2018-02-07 00:49:51 +00:00
|
|
|
const auto *dr = dyn_cast<Defined>(sym);
|
2018-01-16 16:49:05 +00:00
|
|
|
r.FinalDefinitionInLinkageUnit =
|
2022-09-04 17:27:35 -07:00
|
|
|
(isExec || sym->visibility() != STV_DEFAULT) && dr &&
|
2018-02-07 00:49:51 +00: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 04:25:52 +00:00
|
|
|
// for isElf(). Skip linker script defined symbols as well: they have
|
|
|
|
|
// no File defined.
|
2024-01-22 09:09:46 -08:00
|
|
|
!(dr->section == nullptr &&
|
|
|
|
|
(sym->file->isInternal() || sym->file->isElf()));
|
2018-01-16 16:49:05 +00:00
|
|
|
|
2016-09-29 00:40:08 +00:00
|
|
|
if (r.Prevailing)
|
2024-01-22 09:09:46 -08:00
|
|
|
Undefined(ctx.internalFile, StringRef(), STB_GLOBAL, STV_DEFAULT,
|
|
|
|
|
sym->type)
|
2022-09-28 13:11:31 -07:00
|
|
|
.overwrite(*sym);
|
2017-09-25 09:31:43 +00:00
|
|
|
|
2018-01-30 09:04:27 +00: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.
|
2022-02-05 12:00:34 -08:00
|
|
|
r.LinkerRedefined = sym->scriptDefined;
|
2016-07-15 02:17:13 +00:00
|
|
|
}
|
2016-11-26 05:37:04 +00:00
|
|
|
checkError(ltoObj->add(std::move(f.obj), resols));
|
2016-04-15 22:38:10 +00:00
|
|
|
}
|
|
|
|
|
|
2019-05-02 14:05:20 +00: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 10:40:26 +00:00
|
|
|
static void thinLTOCreateEmptyIndexFiles() {
|
2022-09-12 17:35:51 -07:00
|
|
|
DenseSet<StringRef> linkedBitCodeFiles;
|
2022-10-01 12:06:33 -07:00
|
|
|
for (BitcodeFile *f : ctx.bitcodeFiles)
|
2022-09-12 17:35:51 -07:00
|
|
|
linkedBitCodeFiles.insert(f->getName());
|
|
|
|
|
|
2022-10-01 12:06:33 -07:00
|
|
|
for (BitcodeFile *f : ctx.lazyBitcodeFiles) {
|
2021-12-22 17:41:50 -08:00
|
|
|
if (!f->lazy)
|
2019-05-02 14:05:20 +00:00
|
|
|
continue;
|
2022-09-12 17:35:51 -07:00
|
|
|
if (linkedBitCodeFiles.contains(f->getName()))
|
|
|
|
|
continue;
|
2022-10-01 06:24:32 +00:00
|
|
|
std::string path =
|
|
|
|
|
replaceThinLTOSuffix(getThinLTOOutputFile(f->obj->getName()));
|
2019-05-02 14:05:20 +00:00
|
|
|
std::unique_ptr<raw_fd_ostream> os = openFile(path + ".thinlto.bc");
|
|
|
|
|
if (!os)
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
ModuleSummaryIndex m(/*HaveGVs*/ false);
|
|
|
|
|
m.setSkipModuleByDistributedBackend();
|
2022-01-31 16:46:11 -08:00
|
|
|
writeIndexToFile(m, *os);
|
2019-05-02 14:05:20 +00:00
|
|
|
if (config->thinLTOEmitImportsFiles)
|
|
|
|
|
openFile(path + ".imports");
|
|
|
|
|
}
|
2018-05-17 18:27:12 +00:00
|
|
|
}
|
|
|
|
|
|
2016-03-22 20:52:10 +00:00
|
|
|
// Merge all the bitcode files we have seen, codegen the result
|
2016-09-29 00:40:08 +00:00
|
|
|
// and return the resulting ObjectFile(s).
|
2016-09-14 00:05:51 +00:00
|
|
|
std::vector<InputFile *> BitcodeCompiler::compile() {
|
2016-11-26 05:37:04 +00:00
|
|
|
unsigned maxTasks = ltoObj->getMaxTasks();
|
2018-05-17 18:27:12 +00:00
|
|
|
buf.resize(maxTasks);
|
2017-03-01 23:00:10 +00:00
|
|
|
files.resize(maxTasks);
|
[ELF] --save-temps --lto-emit-asm: derive ELF/asm file names from bitcode file names
Port COFF's https://reviews.llvm.org/D78221 and
https://reviews.llvm.org/D137217 to ELF. For the in-process ThinLTO
link, `ld.lld --save-temps a.o d/b.o -o out` will create
ELF relocatable files `out.lto.a.o`/`d/out.lto.b.o` instead of
`out1.lto.o`/`out2.lto.o`. Deriving the LTO-generated relocatable file
name from bitcode file names helps debugging.
The relocatable file name from the first regular LTO partition does not
change: `out.lto.o`. The second, if present due to `--lto-partition=`,
changes from `out1.lto.o` to `lto.1.o`.
For an archive member, e.g. `d/a.a(coll.o at 8)`,
the relocatable file is `d/out.lto.a.a(coll.o at 8).o`.
`--lto-emit-asm` file names are changed similarly. `--lto-emit-asm -o
out` now creates `out.lto.s` instead of `out`, therefore the
`--lto-emit-asm -o -` idiom no longer works. However, I think this new
behavior (which matches COFF) is better since keeping or removing
`--lto-emit-asm` will dump different files, instead of overwriting the
`-o` output file from an executable/shared object to an assembly file.
Reviewers: rnk, igorkudrin, xur-llvm, teresajohnson, ZequanWu
Reviewed By: teresajohnson
Pull Request: https://github.com/llvm/llvm-project/pull/78835
2024-01-23 11:38:15 -08:00
|
|
|
filenames.resize(maxTasks);
|
2017-03-01 23:00:10 +00:00
|
|
|
|
|
|
|
|
// 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-04 12:59:59 -07:00
|
|
|
FileCache cache;
|
2017-03-01 23:00:10 +00:00
|
|
|
if (!config->thinLTOCacheDir.empty())
|
2022-11-22 13:46:42 -08:00
|
|
|
cache = check(localCache("ThinLTO", "Thin", config->thinLTOCacheDir,
|
|
|
|
|
[&](size_t task, const Twine &moduleName,
|
|
|
|
|
std::unique_ptr<MemoryBuffer> mb) {
|
|
|
|
|
files[task] = std::move(mb);
|
[ELF] --save-temps --lto-emit-asm: derive ELF/asm file names from bitcode file names
Port COFF's https://reviews.llvm.org/D78221 and
https://reviews.llvm.org/D137217 to ELF. For the in-process ThinLTO
link, `ld.lld --save-temps a.o d/b.o -o out` will create
ELF relocatable files `out.lto.a.o`/`d/out.lto.b.o` instead of
`out1.lto.o`/`out2.lto.o`. Deriving the LTO-generated relocatable file
name from bitcode file names helps debugging.
The relocatable file name from the first regular LTO partition does not
change: `out.lto.o`. The second, if present due to `--lto-partition=`,
changes from `out1.lto.o` to `lto.1.o`.
For an archive member, e.g. `d/a.a(coll.o at 8)`,
the relocatable file is `d/out.lto.a.a(coll.o at 8).o`.
`--lto-emit-asm` file names are changed similarly. `--lto-emit-asm -o
out` now creates `out.lto.s` instead of `out`, therefore the
`--lto-emit-asm -o -` idiom no longer works. However, I think this new
behavior (which matches COFF) is better since keeping or removing
`--lto-emit-asm` will dump different files, instead of overwriting the
`-o` output file from an executable/shared object to an assembly file.
Reviewers: rnk, igorkudrin, xur-llvm, teresajohnson, ZequanWu
Reviewed By: teresajohnson
Pull Request: https://github.com/llvm/llvm-project/pull/78835
2024-01-23 11:38:15 -08:00
|
|
|
filenames[task] = moduleName.str();
|
2022-11-22 13:46:42 -08:00
|
|
|
}));
|
2017-03-01 23:00:10 +00:00
|
|
|
|
2022-10-01 12:06:33 -07:00
|
|
|
if (!ctx.bitcodeFiles.empty())
|
2019-05-07 10:40:26 +00:00
|
|
|
checkError(ltoObj->run(
|
2022-11-22 13:46:42 -08:00
|
|
|
[&](size_t task, const Twine &moduleName) {
|
[ELF] --save-temps --lto-emit-asm: derive ELF/asm file names from bitcode file names
Port COFF's https://reviews.llvm.org/D78221 and
https://reviews.llvm.org/D137217 to ELF. For the in-process ThinLTO
link, `ld.lld --save-temps a.o d/b.o -o out` will create
ELF relocatable files `out.lto.a.o`/`d/out.lto.b.o` instead of
`out1.lto.o`/`out2.lto.o`. Deriving the LTO-generated relocatable file
name from bitcode file names helps debugging.
The relocatable file name from the first regular LTO partition does not
change: `out.lto.o`. The second, if present due to `--lto-partition=`,
changes from `out1.lto.o` to `lto.1.o`.
For an archive member, e.g. `d/a.a(coll.o at 8)`,
the relocatable file is `d/out.lto.a.a(coll.o at 8).o`.
`--lto-emit-asm` file names are changed similarly. `--lto-emit-asm -o
out` now creates `out.lto.s` instead of `out`, therefore the
`--lto-emit-asm -o -` idiom no longer works. However, I think this new
behavior (which matches COFF) is better since keeping or removing
`--lto-emit-asm` will dump different files, instead of overwriting the
`-o` output file from an executable/shared object to an assembly file.
Reviewers: rnk, igorkudrin, xur-llvm, teresajohnson, ZequanWu
Reviewed By: teresajohnson
Pull Request: https://github.com/llvm/llvm-project/pull/78835
2024-01-23 11:38:15 -08:00
|
|
|
buf[task].first = moduleName.str();
|
2021-11-04 12:59:59 -07:00
|
|
|
return std::make_unique<CachedFileStream>(
|
[ELF] --save-temps --lto-emit-asm: derive ELF/asm file names from bitcode file names
Port COFF's https://reviews.llvm.org/D78221 and
https://reviews.llvm.org/D137217 to ELF. For the in-process ThinLTO
link, `ld.lld --save-temps a.o d/b.o -o out` will create
ELF relocatable files `out.lto.a.o`/`d/out.lto.b.o` instead of
`out1.lto.o`/`out2.lto.o`. Deriving the LTO-generated relocatable file
name from bitcode file names helps debugging.
The relocatable file name from the first regular LTO partition does not
change: `out.lto.o`. The second, if present due to `--lto-partition=`,
changes from `out1.lto.o` to `lto.1.o`.
For an archive member, e.g. `d/a.a(coll.o at 8)`,
the relocatable file is `d/out.lto.a.a(coll.o at 8).o`.
`--lto-emit-asm` file names are changed similarly. `--lto-emit-asm -o
out` now creates `out.lto.s` instead of `out`, therefore the
`--lto-emit-asm -o -` idiom no longer works. However, I think this new
behavior (which matches COFF) is better since keeping or removing
`--lto-emit-asm` will dump different files, instead of overwriting the
`-o` output file from an executable/shared object to an assembly file.
Reviewers: rnk, igorkudrin, xur-llvm, teresajohnson, ZequanWu
Reviewed By: teresajohnson
Pull Request: https://github.com/llvm/llvm-project/pull/78835
2024-01-23 11:38:15 -08:00
|
|
|
std::make_unique<raw_svector_ostream>(buf[task].second));
|
2019-05-07 10:40:26 +00:00
|
|
|
},
|
|
|
|
|
cache));
|
2016-04-17 23:20:08 +00:00
|
|
|
|
2020-05-21 13:19:44 -07: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 00:40:08 +00:00
|
|
|
}
|
2017-03-01 23:00:10 +00:00
|
|
|
|
2022-06-01 10:49:36 -07:00
|
|
|
if (config->thinLTOEmitIndexFiles)
|
2019-05-02 14:05:20 +00:00
|
|
|
thinLTOCreateEmptyIndexFiles();
|
2018-05-07 22:11:24 +00:00
|
|
|
|
2022-06-01 10:49:36 -07:00
|
|
|
if (config->thinLTOIndexOnly) {
|
2018-05-08 22:37:57 +00:00
|
|
|
if (!config->ltoObjPath.empty())
|
[ELF] --save-temps --lto-emit-asm: derive ELF/asm file names from bitcode file names
Port COFF's https://reviews.llvm.org/D78221 and
https://reviews.llvm.org/D137217 to ELF. For the in-process ThinLTO
link, `ld.lld --save-temps a.o d/b.o -o out` will create
ELF relocatable files `out.lto.a.o`/`d/out.lto.b.o` instead of
`out1.lto.o`/`out2.lto.o`. Deriving the LTO-generated relocatable file
name from bitcode file names helps debugging.
The relocatable file name from the first regular LTO partition does not
change: `out.lto.o`. The second, if present due to `--lto-partition=`,
changes from `out1.lto.o` to `lto.1.o`.
For an archive member, e.g. `d/a.a(coll.o at 8)`,
the relocatable file is `d/out.lto.a.a(coll.o at 8).o`.
`--lto-emit-asm` file names are changed similarly. `--lto-emit-asm -o
out` now creates `out.lto.s` instead of `out`, therefore the
`--lto-emit-asm -o -` idiom no longer works. However, I think this new
behavior (which matches COFF) is better since keeping or removing
`--lto-emit-asm` will dump different files, instead of overwriting the
`-o` output file from an executable/shared object to an assembly file.
Reviewers: rnk, igorkudrin, xur-llvm, teresajohnson, ZequanWu
Reviewed By: teresajohnson
Pull Request: https://github.com/llvm/llvm-project/pull/78835
2024-01-23 11:38:15 -08:00
|
|
|
saveBuffer(buf[0].second, config->ltoObjPath);
|
2018-05-08 20:12:07 +00:00
|
|
|
|
2018-05-07 22:11:24 +00: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-07 22:11:34 +00:00
|
|
|
if (indexFile)
|
|
|
|
|
indexFile->close();
|
|
|
|
|
return {};
|
2018-05-07 22:11:24 +00:00
|
|
|
}
|
2018-05-17 18:27:12 +00:00
|
|
|
|
2018-05-08 20:12:07 +00:00
|
|
|
if (!config->thinLTOCacheDir.empty())
|
2022-10-10 13:26:56 +01:00
|
|
|
pruneCache(config->thinLTOCacheDir, config->thinLTOCachePolicy, files);
|
2018-05-08 20:12:07 +00:00
|
|
|
|
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-26 19:29:14 +00:00
|
|
|
if (!config->ltoObjPath.empty()) {
|
[ELF] --save-temps --lto-emit-asm: derive ELF/asm file names from bitcode file names
Port COFF's https://reviews.llvm.org/D78221 and
https://reviews.llvm.org/D137217 to ELF. For the in-process ThinLTO
link, `ld.lld --save-temps a.o d/b.o -o out` will create
ELF relocatable files `out.lto.a.o`/`d/out.lto.b.o` instead of
`out1.lto.o`/`out2.lto.o`. Deriving the LTO-generated relocatable file
name from bitcode file names helps debugging.
The relocatable file name from the first regular LTO partition does not
change: `out.lto.o`. The second, if present due to `--lto-partition=`,
changes from `out1.lto.o` to `lto.1.o`.
For an archive member, e.g. `d/a.a(coll.o at 8)`,
the relocatable file is `d/out.lto.a.a(coll.o at 8).o`.
`--lto-emit-asm` file names are changed similarly. `--lto-emit-asm -o
out` now creates `out.lto.s` instead of `out`, therefore the
`--lto-emit-asm -o -` idiom no longer works. However, I think this new
behavior (which matches COFF) is better since keeping or removing
`--lto-emit-asm` will dump different files, instead of overwriting the
`-o` output file from an executable/shared object to an assembly file.
Reviewers: rnk, igorkudrin, xur-llvm, teresajohnson, ZequanWu
Reviewed By: teresajohnson
Pull Request: https://github.com/llvm/llvm-project/pull/78835
2024-01-23 11:38:15 -08:00
|
|
|
saveBuffer(buf[0].second, config->ltoObjPath);
|
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-26 19:29:14 +00:00
|
|
|
for (unsigned i = 1; i != maxTasks; ++i)
|
[ELF] --save-temps --lto-emit-asm: derive ELF/asm file names from bitcode file names
Port COFF's https://reviews.llvm.org/D78221 and
https://reviews.llvm.org/D137217 to ELF. For the in-process ThinLTO
link, `ld.lld --save-temps a.o d/b.o -o out` will create
ELF relocatable files `out.lto.a.o`/`d/out.lto.b.o` instead of
`out1.lto.o`/`out2.lto.o`. Deriving the LTO-generated relocatable file
name from bitcode file names helps debugging.
The relocatable file name from the first regular LTO partition does not
change: `out.lto.o`. The second, if present due to `--lto-partition=`,
changes from `out1.lto.o` to `lto.1.o`.
For an archive member, e.g. `d/a.a(coll.o at 8)`,
the relocatable file is `d/out.lto.a.a(coll.o at 8).o`.
`--lto-emit-asm` file names are changed similarly. `--lto-emit-asm -o
out` now creates `out.lto.s` instead of `out`, therefore the
`--lto-emit-asm -o -` idiom no longer works. However, I think this new
behavior (which matches COFF) is better since keeping or removing
`--lto-emit-asm` will dump different files, instead of overwriting the
`-o` output file from an executable/shared object to an assembly file.
Reviewers: rnk, igorkudrin, xur-llvm, teresajohnson, ZequanWu
Reviewed By: teresajohnson
Pull Request: https://github.com/llvm/llvm-project/pull/78835
2024-01-23 11:38:15 -08:00
|
|
|
saveBuffer(buf[i].second, config->ltoObjPath + Twine(i));
|
[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-01 10:01:23 -07:00
|
|
|
}
|
|
|
|
|
|
[ELF] --save-temps --lto-emit-asm: derive ELF/asm file names from bitcode file names
Port COFF's https://reviews.llvm.org/D78221 and
https://reviews.llvm.org/D137217 to ELF. For the in-process ThinLTO
link, `ld.lld --save-temps a.o d/b.o -o out` will create
ELF relocatable files `out.lto.a.o`/`d/out.lto.b.o` instead of
`out1.lto.o`/`out2.lto.o`. Deriving the LTO-generated relocatable file
name from bitcode file names helps debugging.
The relocatable file name from the first regular LTO partition does not
change: `out.lto.o`. The second, if present due to `--lto-partition=`,
changes from `out1.lto.o` to `lto.1.o`.
For an archive member, e.g. `d/a.a(coll.o at 8)`,
the relocatable file is `d/out.lto.a.a(coll.o at 8).o`.
`--lto-emit-asm` file names are changed similarly. `--lto-emit-asm -o
out` now creates `out.lto.s` instead of `out`, therefore the
`--lto-emit-asm -o -` idiom no longer works. However, I think this new
behavior (which matches COFF) is better since keeping or removing
`--lto-emit-asm` will dump different files, instead of overwriting the
`-o` output file from an executable/shared object to an assembly file.
Reviewers: rnk, igorkudrin, xur-llvm, teresajohnson, ZequanWu
Reviewed By: teresajohnson
Pull Request: https://github.com/llvm/llvm-project/pull/78835
2024-01-23 11:38:15 -08:00
|
|
|
bool savePrelink = config->saveTempsArgs.contains("prelink");
|
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-26 19:29:14 +00:00
|
|
|
std::vector<InputFile *> ret;
|
[ELF] --save-temps --lto-emit-asm: derive ELF/asm file names from bitcode file names
Port COFF's https://reviews.llvm.org/D78221 and
https://reviews.llvm.org/D137217 to ELF. For the in-process ThinLTO
link, `ld.lld --save-temps a.o d/b.o -o out` will create
ELF relocatable files `out.lto.a.o`/`d/out.lto.b.o` instead of
`out1.lto.o`/`out2.lto.o`. Deriving the LTO-generated relocatable file
name from bitcode file names helps debugging.
The relocatable file name from the first regular LTO partition does not
change: `out.lto.o`. The second, if present due to `--lto-partition=`,
changes from `out1.lto.o` to `lto.1.o`.
For an archive member, e.g. `d/a.a(coll.o at 8)`,
the relocatable file is `d/out.lto.a.a(coll.o at 8).o`.
`--lto-emit-asm` file names are changed similarly. `--lto-emit-asm -o
out` now creates `out.lto.s` instead of `out`, therefore the
`--lto-emit-asm -o -` idiom no longer works. However, I think this new
behavior (which matches COFF) is better since keeping or removing
`--lto-emit-asm` will dump different files, instead of overwriting the
`-o` output file from an executable/shared object to an assembly file.
Reviewers: rnk, igorkudrin, xur-llvm, teresajohnson, ZequanWu
Reviewed By: teresajohnson
Pull Request: https://github.com/llvm/llvm-project/pull/78835
2024-01-23 11:38:15 -08:00
|
|
|
const char *ext = config->ltoEmitAsm ? ".s" : ".o";
|
|
|
|
|
for (unsigned i = 0; i != maxTasks; ++i) {
|
|
|
|
|
StringRef bitcodeFilePath;
|
|
|
|
|
StringRef objBuf;
|
|
|
|
|
if (files[i]) {
|
|
|
|
|
// When files[i] is not null, we get the native relocatable file from the
|
|
|
|
|
// cache. filenames[i] contains the original BitcodeFile's identifier.
|
|
|
|
|
objBuf = files[i]->getBuffer();
|
|
|
|
|
bitcodeFilePath = filenames[i];
|
|
|
|
|
} else {
|
|
|
|
|
// Get the native relocatable file after in-process LTO compilation.
|
|
|
|
|
objBuf = buf[i].second;
|
|
|
|
|
bitcodeFilePath = buf[i].first;
|
|
|
|
|
}
|
|
|
|
|
if (objBuf.empty())
|
|
|
|
|
continue;
|
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-26 19:29:14 +00:00
|
|
|
|
[ELF] --save-temps --lto-emit-asm: derive ELF/asm file names from bitcode file names
Port COFF's https://reviews.llvm.org/D78221 and
https://reviews.llvm.org/D137217 to ELF. For the in-process ThinLTO
link, `ld.lld --save-temps a.o d/b.o -o out` will create
ELF relocatable files `out.lto.a.o`/`d/out.lto.b.o` instead of
`out1.lto.o`/`out2.lto.o`. Deriving the LTO-generated relocatable file
name from bitcode file names helps debugging.
The relocatable file name from the first regular LTO partition does not
change: `out.lto.o`. The second, if present due to `--lto-partition=`,
changes from `out1.lto.o` to `lto.1.o`.
For an archive member, e.g. `d/a.a(coll.o at 8)`,
the relocatable file is `d/out.lto.a.a(coll.o at 8).o`.
`--lto-emit-asm` file names are changed similarly. `--lto-emit-asm -o
out` now creates `out.lto.s` instead of `out`, therefore the
`--lto-emit-asm -o -` idiom no longer works. However, I think this new
behavior (which matches COFF) is better since keeping or removing
`--lto-emit-asm` will dump different files, instead of overwriting the
`-o` output file from an executable/shared object to an assembly file.
Reviewers: rnk, igorkudrin, xur-llvm, teresajohnson, ZequanWu
Reviewed By: teresajohnson
Pull Request: https://github.com/llvm/llvm-project/pull/78835
2024-01-23 11:38:15 -08:00
|
|
|
// If the input bitcode file is path/to/x.o and -o specifies a.out, the
|
|
|
|
|
// corresponding native relocatable file path will look like:
|
|
|
|
|
// path/to/a.out.lto.x.o.
|
|
|
|
|
StringRef ltoObjName;
|
|
|
|
|
if (bitcodeFilePath == "ld-temp.o") {
|
|
|
|
|
ltoObjName =
|
|
|
|
|
saver().save(Twine(config->outputFile) + ".lto" +
|
|
|
|
|
(i == 0 ? Twine("") : Twine('.') + Twine(i)) + ext);
|
|
|
|
|
} else {
|
|
|
|
|
StringRef directory = sys::path::parent_path(bitcodeFilePath);
|
|
|
|
|
// For an archive member, which has an identifier like "d/a.a(coll.o at
|
|
|
|
|
// 8)" (see BitcodeFile::BitcodeFile), use the filename; otherwise, use
|
|
|
|
|
// the stem (d/a.o => a).
|
|
|
|
|
StringRef baseName = bitcodeFilePath.ends_with(")")
|
|
|
|
|
? sys::path::filename(bitcodeFilePath)
|
|
|
|
|
: sys::path::stem(bitcodeFilePath);
|
|
|
|
|
StringRef outputFileBaseName = sys::path::filename(config->outputFile);
|
|
|
|
|
SmallString<256> path;
|
|
|
|
|
sys::path::append(path, directory,
|
|
|
|
|
outputFileBaseName + ".lto." + baseName + ext);
|
|
|
|
|
sys::path::remove_dots(path, true);
|
|
|
|
|
ltoObjName = saver().save(path.str());
|
|
|
|
|
}
|
|
|
|
|
if (savePrelink || config->ltoEmitAsm)
|
|
|
|
|
saveBuffer(buf[i].second, ltoObjName);
|
|
|
|
|
if (!config->ltoEmitAsm)
|
|
|
|
|
ret.push_back(createObjFile(MemoryBufferRef(objBuf, ltoObjName)));
|
|
|
|
|
}
|
2016-09-29 00:40:08 +00:00
|
|
|
return ret;
|
2016-03-23 21:19:27 +00:00
|
|
|
}
|