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
|
|
|
|
2024-09-21 10:11:37 -07:00
|
|
|
static std::string getThinLTOOutputFile(Ctx &ctx, StringRef modulePath) {
|
|
|
|
return lto::getThinLTOOutputFile(modulePath, ctx.arg.thinLTOPrefixReplaceOld,
|
|
|
|
ctx.arg.thinLTOPrefixReplaceNew);
|
2018-05-07 22:11:24 +00:00
|
|
|
}
|
|
|
|
|
2024-09-21 10:11:37 -07:00
|
|
|
static lto::Config createConfig(Ctx &ctx) {
|
2018-05-07 23:24:07 +00:00
|
|
|
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;
|
2024-09-21 10:11:37 -07:00
|
|
|
for (StringRef C : ctx.arg.mllvmOpts)
|
2022-09-15 21:01:56 -07:00
|
|
|
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
|
|
|
|
2020-04-07 06:48:18 -07:00
|
|
|
// Check if basic block sections must be used.
|
2024-10-07 09:22:36 -07:00
|
|
|
// Allowed values for --lto-basic-block-sections are "all",
|
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.
|
2024-09-21 10:11:37 -07:00
|
|
|
if (!ctx.arg.ltoBasicBlockSections.empty()) {
|
|
|
|
if (ctx.arg.ltoBasicBlockSections == "all") {
|
2020-04-07 06:48:18 -07:00
|
|
|
c.Options.BBSections = BasicBlockSection::All;
|
2024-09-21 10:11:37 -07:00
|
|
|
} else if (ctx.arg.ltoBasicBlockSections == "labels") {
|
2024-09-25 22:03:10 -07:00
|
|
|
c.Options.BBAddrMap = true;
|
2024-11-06 22:19:31 -08:00
|
|
|
Warn(ctx)
|
|
|
|
<< "'--lto-basic-block-sections=labels' is deprecated; Please use "
|
|
|
|
"'--lto-basic-block-address-map' instead";
|
2024-09-21 10:11:37 -07:00
|
|
|
} else if (ctx.arg.ltoBasicBlockSections == "none") {
|
2020-04-07 06:48:18 -07:00
|
|
|
c.Options.BBSections = BasicBlockSection::None;
|
|
|
|
} else {
|
|
|
|
ErrorOr<std::unique_ptr<MemoryBuffer>> MBOrErr =
|
2024-09-21 10:11:37 -07:00
|
|
|
MemoryBuffer::getFile(ctx.arg.ltoBasicBlockSections.str());
|
2020-04-07 06:48:18 -07:00
|
|
|
if (!MBOrErr) {
|
2024-11-06 22:04:52 -08:00
|
|
|
ErrAlways(ctx) << "cannot open " << ctx.arg.ltoBasicBlockSections << ":"
|
|
|
|
<< MBOrErr.getError().message();
|
2020-04-07 06:48:18 -07:00
|
|
|
} else {
|
|
|
|
c.Options.BBSectionsFuncListBuf = std::move(*MBOrErr);
|
|
|
|
}
|
|
|
|
c.Options.BBSections = BasicBlockSection::List;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-25 22:03:10 -07:00
|
|
|
c.Options.BBAddrMap = ctx.arg.ltoBBAddrMap;
|
|
|
|
|
2020-06-01 23:17:29 -07:00
|
|
|
c.Options.UniqueBasicBlockSectionNames =
|
2024-09-21 10:11:37 -07:00
|
|
|
ctx.arg.ltoUniqueBasicBlockSectionNames;
|
2020-04-07 06:48:18 -07:00
|
|
|
|
2019-07-20 21:59:47 +00:00
|
|
|
if (auto relocModel = getRelocModelFromCMModel())
|
|
|
|
c.RelocModel = *relocModel;
|
2024-09-21 10:11:37 -07:00
|
|
|
else if (ctx.arg.relocatable)
|
2022-12-02 23:12:36 -08:00
|
|
|
c.RelocModel = std::nullopt;
|
2024-09-21 10:11:37 -07:00
|
|
|
else if (ctx.arg.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();
|
2024-09-21 10:11:37 -07:00
|
|
|
c.DisableVerify = ctx.arg.disableVerify;
|
2018-05-07 23:24:07 +00:00
|
|
|
c.DiagHandler = diagnosticHandler;
|
2024-09-21 10:11:37 -07:00
|
|
|
c.OptLevel = ctx.arg.ltoo;
|
2019-02-01 02:24:50 +00:00
|
|
|
c.CPU = getCPUStr();
|
|
|
|
c.MAttrs = getMAttrs();
|
2024-09-21 10:11:37 -07:00
|
|
|
c.CGOptLevel = ctx.arg.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.
|
2024-09-21 10:11:37 -07:00
|
|
|
c.OptPipeline = std::string(ctx.arg.ltoNewPmPasses);
|
|
|
|
c.AAPipeline = std::string(ctx.arg.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.
|
2024-09-21 10:11:37 -07:00
|
|
|
c.RemarksFilename = std::string(ctx.arg.optRemarksFilename);
|
|
|
|
c.RemarksPasses = std::string(ctx.arg.optRemarksPasses);
|
|
|
|
c.RemarksWithHotness = ctx.arg.optRemarksWithHotness;
|
|
|
|
c.RemarksHotnessThreshold = ctx.arg.optRemarksHotnessThreshold;
|
|
|
|
c.RemarksFormat = std::string(ctx.arg.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.
|
2024-09-21 10:11:37 -07:00
|
|
|
c.StatsFile = std::string(ctx.arg.optStatsFilename);
|
2022-03-17 11:53:44 +08:00
|
|
|
|
2024-09-21 10:11:37 -07:00
|
|
|
c.SampleProfile = std::string(ctx.arg.ltoSampleProfile);
|
|
|
|
for (StringRef pluginFn : ctx.arg.passPlugins)
|
2022-03-24 07:52:16 +01:00
|
|
|
c.PassPlugins.push_back(std::string(pluginFn));
|
2024-09-21 10:11:37 -07:00
|
|
|
c.DebugPassManager = ctx.arg.ltoDebugPassManager;
|
|
|
|
c.DwoDir = std::string(ctx.arg.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
|
|
|
|
2024-09-21 10:11:37 -07:00
|
|
|
c.HasWholeProgramVisibility = ctx.arg.ltoWholeProgramVisibility;
|
2023-07-13 19:02:52 -07:00
|
|
|
c.ValidateAllVtablesHaveTypeInfos =
|
2024-09-21 10:11:37 -07:00
|
|
|
ctx.arg.ltoValidateAllVtablesHaveTypeInfos;
|
2023-07-13 19:02:52 -07:00
|
|
|
c.AllVtablesHaveTypeInfos = ctx.ltoAllVtablesHaveTypeInfos;
|
2024-09-21 10:11:37 -07:00
|
|
|
c.AlwaysEmitRegularLTOObj = !ctx.arg.ltoObjPath.empty();
|
Re-apply "[NFCI][LTO][lld] Optimize away symbol copies within LTO global resolution in ELF" (#107792)
Fix the use-after-free bug and re-apply
https://github.com/llvm/llvm-project/pull/106193
* Without the fix, the string referenced by `objSym.Name` could be
destroyed even if string saver keeps a copy of the referenced string.
This caused use-after-free.
* The fix ([latest
commit](https://github.com/llvm/llvm-project/pull/107792/commits/9776ed44cfb26172480145aed8f59ba78a6fa2ea))
updates `objSym.Name` to reference (via `StringRef`) the string saver's
copy.
Test:
1. For `lld/test/ELF/lto/asmundef.ll`, its test failure is reproducible
with `-DLLVM_USE_SANITIZER=Address` and gone with the fix.
3. Run all tests by following
https://github.com/google/sanitizers/wiki/SanitizerBotReproduceBuild#try-local-changes.
* Without the fix, `ELF/lto/asmundef.ll` aborted the multi-stage test at
`@@@BUILD_STEP stage2/asan_ubsan check@@@`, defined
[here](https://github.com/llvm/llvm-zorg/blob/main/zorg/buildbot/builders/sanitizers/buildbot_fast.sh#L30)
* With the fix, the [multi-stage
test](https://github.com/llvm/llvm-zorg/blob/main/zorg/buildbot/builders/sanitizers/buildbot_fast.sh)
pass stage2 {asan, ubsan, masan}. This is also the test used by
https://lab.llvm.org/buildbot/#/builders/169
**Original commit message**
`StringMap<T>` creates a [copy of the
string](https://github.com/llvm/llvm-project/blob/d4c519e7b2ac21350ec08b23eda44bf4a2d3c974/llvm/include/llvm/ADT/StringMapEntry.h#L55-L58)
for entry insertions and intentionally keep copies [since the
implementation optimizes string memory
usage](https://github.com/llvm/llvm-project/blob/d4c519e7b2ac21350ec08b23eda44bf4a2d3c974/llvm/include/llvm/ADT/StringMap.h#L124).
On the other hand, linker keeps copies of symbol names [1] in
`lld::elf::parseFiles` [2] before invoking `compileBitcodeFiles` [3].
This change proposes to optimize away string copies inside
[LTO::GlobalResolutions](https://github.com/llvm/llvm-project/blob/24e791b4164986a1ca7776e3ae0292ef20d20c47/llvm/include/llvm/LTO/LTO.h#L409),
which will make LTO indexing more memory efficient for ELF. There are
similar opportunities for other (COFF, wasm, MachO) formats.
The optimization takes place for lld (ELF) only. For the rest of use
cases (gold plugin, `llvm-lto2`, etc), LTO owns a string saver to keep
copies and use global resolution key for de-duplication.
Together with @kazutakahirata's work to make `ComputeCrossModuleImport`
more memory efficient, we see a ~20% peak memory usage reduction in a
binary where peak memory usage needs to go down. Thanks to the
optimization in
https://github.com/llvm/llvm-project/commit/329ba523ccbbe68a12434926c92fd9a86494d958,
the max (as opposed to the sum) of `ComputeCrossModuleImport` or
`GlobalResolution` shows up in peak memory usage.
* Regarding correctness, the set of
[resolved](https://github.com/llvm/llvm-project/blob/80c47ad3aec9d7f22e1b1bdc88960a91b66f89f1/llvm/lib/LTO/LTO.cpp#L739)
[per-module
symbols](https://github.com/llvm/llvm-project/blob/80c47ad3aec9d7f22e1b1bdc88960a91b66f89f1/llvm/include/llvm/LTO/LTO.h#L188-L191)
is a subset of
[llvm::lto::InputFile::Symbols](https://github.com/llvm/llvm-project/blob/80c47ad3aec9d7f22e1b1bdc88960a91b66f89f1/llvm/include/llvm/LTO/LTO.h#L120).
And bitcode symbol parsing saves symbol name when iterating
`obj->symbols` in `BitcodeFile::parse` already. This change updates
`BitcodeFile::parseLazy` to keep copies of per-module undefined symbols.
* Presumably the undefined symbols in a LTO unit (copied in this patch
in linker unique saver) is a small set compared with the set of symbols
in global-resolution (copied before this patch), making this a
worthwhile trade-off. Benchmarking this change alone shows measurable
memory savings across various benchmarks.
[1] ELF
https://github.com/llvm/llvm-project/blob/1cea5c2138bef3d8fec75508df6dbb858e6e3560/lld/ELF/InputFiles.cpp#L1748
[2]
https://github.com/llvm/llvm-project/blob/ef7b18a53c0d186dcda1e322be6035407fdedb55/lld/ELF/Driver.cpp#L2863
[3]
https://github.com/llvm/llvm-project/blob/ef7b18a53c0d186dcda1e322be6035407fdedb55/lld/ELF/Driver.cpp#L2995
2024-09-09 11:16:58 -07:00
|
|
|
c.KeepSymbolNameCopies = false;
|
2020-01-24 12:24:18 -08:00
|
|
|
|
2024-09-21 10:11:37 -07:00
|
|
|
for (const llvm::StringRef &name : ctx.arg.thinLTOModulesToCompile)
|
2020-05-21 13:19:44 -07:00
|
|
|
c.ThinLTOModulesToCompile.emplace_back(name);
|
|
|
|
|
2024-09-21 10:11:37 -07:00
|
|
|
c.TimeTraceEnabled = ctx.arg.timeTraceEnabled;
|
|
|
|
c.TimeTraceGranularity = ctx.arg.timeTraceGranularity;
|
2020-01-28 16:05:13 +00:00
|
|
|
|
2024-09-21 10:11:37 -07:00
|
|
|
c.CSIRProfile = std::string(ctx.arg.ltoCSProfileFile);
|
|
|
|
c.RunCSIRInstr = ctx.arg.ltoCSProfileGenerate;
|
|
|
|
c.PGOWarnMismatch = ctx.arg.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
|
|
|
|
2024-09-21 10:11:37 -07:00
|
|
|
if (ctx.arg.emitLLVM) {
|
|
|
|
c.PreCodeGenModuleHook = [&ctx](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 =
|
2024-09-21 10:11:37 -07:00
|
|
|
openLTOOutputFile(ctx.arg.outputFile))
|
2018-12-14 21:58:49 +00:00
|
|
|
WriteBitcodeToFile(m, *os, false);
|
|
|
|
return false;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2024-09-21 10:11:37 -07:00
|
|
|
if (ctx.arg.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
|
|
|
|
2024-09-21 10:11:37 -07:00
|
|
|
if (!ctx.arg.saveTempsArgs.empty())
|
2024-11-16 22:28:54 -08:00
|
|
|
checkError(ctx.e, c.addSaveTemps(ctx.arg.outputFile.str() + ".",
|
|
|
|
/*UseInputModulePath*/ true,
|
|
|
|
ctx.arg.saveTempsArgs));
|
2018-05-07 23:24:07 +00:00
|
|
|
return c;
|
|
|
|
}
|
2016-03-22 20:52:10 +00:00
|
|
|
|
2024-09-21 10:11:37 -07:00
|
|
|
BitcodeCompiler::BitcodeCompiler(Ctx &ctx) : ctx(ctx) {
|
2019-07-16 05:50:45 +00:00
|
|
|
// Initialize indexFile.
|
2024-09-21 10:11:37 -07:00
|
|
|
if (!ctx.arg.thinLTOIndexOnlyArg.empty())
|
|
|
|
indexFile = openFile(ctx.arg.thinLTOIndexOnlyArg);
|
2018-09-11 14:37:27 +00:00
|
|
|
|
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); };
|
2024-09-21 10:11:37 -07:00
|
|
|
if (ctx.arg.thinLTOIndexOnly) {
|
2018-05-07 17:59:43 +00:00
|
|
|
backend = lto::createWriteIndexesThinBackend(
|
2024-10-07 08:16:46 -07:00
|
|
|
llvm::hardware_concurrency(ctx.arg.thinLTOJobs),
|
2024-09-21 10:11:37 -07:00
|
|
|
std::string(ctx.arg.thinLTOPrefixReplaceOld),
|
|
|
|
std::string(ctx.arg.thinLTOPrefixReplaceNew),
|
|
|
|
std::string(ctx.arg.thinLTOPrefixReplaceNativeObject),
|
|
|
|
ctx.arg.thinLTOEmitImportsFiles, indexFile.get(), onIndexWrite);
|
2020-03-27 10:20:39 -04:00
|
|
|
} else {
|
|
|
|
backend = lto::createInProcessThinBackend(
|
2024-09-21 10:11:37 -07:00
|
|
|
llvm::heavyweight_hardware_concurrency(ctx.arg.thinLTOJobs),
|
|
|
|
onIndexWrite, ctx.arg.thinLTOEmitIndexFiles,
|
|
|
|
ctx.arg.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};
|
2024-09-21 10:11:37 -07:00
|
|
|
ltoObj = std::make_unique<lto::LTO>(createConfig(ctx), backend,
|
|
|
|
ctx.arg.ltoPartitions,
|
|
|
|
ltoModes[ctx.arg.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;
|
2024-09-23 10:33:43 -07:00
|
|
|
for (Symbol *sym : ctx.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;
|
2024-09-21 22:54:37 -07:00
|
|
|
bool isExec = !ctx.arg.shared && !ctx.arg.relocatable;
|
2018-05-02 21:40:07 +00:00
|
|
|
|
2024-09-21 22:54:37 -07:00
|
|
|
if (ctx.arg.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.
|
2024-09-21 22:54:37 -07:00
|
|
|
r.VisibleToRegularObj = ctx.arg.relocatable || sym->isUsedInRegularObj ||
|
2022-04-19 18:04:17 -07:00
|
|
|
sym->referencedAfterWrap ||
|
2024-12-08 22:40:14 -08:00
|
|
|
(r.Prevailing && sym->isExported) ||
|
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.
|
2024-12-08 09:17:38 -08:00
|
|
|
r.ExportDynamic = sym->computeBinding(ctx) != STB_LOCAL &&
|
2024-12-08 22:40:14 -08:00
|
|
|
(ctx.arg.exportDynamic || sym->isExported);
|
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
|
|
|
}
|
2024-11-16 22:28:54 -08:00
|
|
|
checkError(ctx.e, 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.
|
2024-09-29 14:45:00 -07:00
|
|
|
static void thinLTOCreateEmptyIndexFiles(Ctx &ctx) {
|
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 =
|
2024-10-03 23:06:18 -07:00
|
|
|
replaceThinLTOSuffix(ctx, getThinLTOOutputFile(ctx, 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);
|
2024-09-21 22:54:37 -07:00
|
|
|
if (ctx.arg.thinLTOEmitImportsFiles)
|
2019-05-02 14:05:20 +00:00
|
|
|
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).
|
2024-11-16 23:50:34 -08:00
|
|
|
SmallVector<std::unique_ptr<InputFile>, 0> 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;
|
2024-09-21 10:11:37 -07:00
|
|
|
if (!ctx.arg.thinLTOCacheDir.empty())
|
|
|
|
cache = check(localCache("ThinLTO", "Thin", ctx.arg.thinLTOCacheDir,
|
2022-11-22 13:46:42 -08:00
|
|
|
[&](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())
|
2024-11-16 22:28:54 -08:00
|
|
|
checkError(ctx.e, ltoObj->run(
|
|
|
|
[&](size_t task, const Twine &moduleName) {
|
|
|
|
buf[task].first = moduleName.str();
|
|
|
|
return std::make_unique<CachedFileStream>(
|
|
|
|
std::make_unique<raw_svector_ostream>(
|
|
|
|
buf[task].second));
|
|
|
|
},
|
|
|
|
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.
|
2024-09-21 10:11:37 -07:00
|
|
|
if (ctx.arg.thinLTOModulesToCompile.empty()) {
|
2020-05-21 13:19:44 -07:00
|
|
|
for (StringRef s : thinIndices) {
|
2024-09-21 10:11:37 -07:00
|
|
|
std::string path = getThinLTOOutputFile(ctx, s);
|
2020-05-21 13:19:44 -07:00
|
|
|
openFile(path + ".thinlto.bc");
|
2024-09-21 10:11:37 -07:00
|
|
|
if (ctx.arg.thinLTOEmitImportsFiles)
|
2020-05-21 13:19:44 -07:00
|
|
|
openFile(path + ".imports");
|
|
|
|
}
|
2016-09-29 00:40:08 +00:00
|
|
|
}
|
2017-03-01 23:00:10 +00:00
|
|
|
|
2024-09-21 10:11:37 -07:00
|
|
|
if (ctx.arg.thinLTOEmitIndexFiles)
|
2024-09-29 14:45:00 -07:00
|
|
|
thinLTOCreateEmptyIndexFiles(ctx);
|
2018-05-07 22:11:24 +00:00
|
|
|
|
2024-09-21 10:11:37 -07:00
|
|
|
if (ctx.arg.thinLTOIndexOnly) {
|
|
|
|
if (!ctx.arg.ltoObjPath.empty())
|
|
|
|
saveBuffer(buf[0].second, ctx.arg.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
|
|
|
|
2024-09-21 10:11:37 -07:00
|
|
|
if (!ctx.arg.thinLTOCacheDir.empty())
|
|
|
|
pruneCache(ctx.arg.thinLTOCacheDir, ctx.arg.thinLTOCachePolicy, files);
|
2018-05-08 20:12:07 +00:00
|
|
|
|
2024-09-21 10:11:37 -07:00
|
|
|
if (!ctx.arg.ltoObjPath.empty()) {
|
|
|
|
saveBuffer(buf[0].second, ctx.arg.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)
|
2024-09-21 10:11:37 -07:00
|
|
|
saveBuffer(buf[i].second, ctx.arg.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
|
|
|
}
|
|
|
|
|
2024-09-21 10:11:37 -07:00
|
|
|
bool savePrelink = ctx.arg.saveTempsArgs.contains("prelink");
|
2024-11-16 23:50:34 -08:00
|
|
|
SmallVector<std::unique_ptr<InputFile>, 0> ret;
|
2024-09-21 10:11:37 -07:00
|
|
|
const char *ext = ctx.arg.ltoEmitAsm ? ".s" : ".o";
|
[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
|
|
|
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 =
|
2024-11-16 22:34:12 -08:00
|
|
|
ctx.saver.save(Twine(ctx.arg.outputFile) + ".lto" +
|
|
|
|
(i == 0 ? Twine("") : Twine('.') + Twine(i)) + ext);
|
[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
|
|
|
} 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);
|
2024-09-21 10:11:37 -07:00
|
|
|
StringRef outputFileBaseName = sys::path::filename(ctx.arg.outputFile);
|
[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
|
|
|
SmallString<256> path;
|
|
|
|
sys::path::append(path, directory,
|
|
|
|
outputFileBaseName + ".lto." + baseName + ext);
|
|
|
|
sys::path::remove_dots(path, true);
|
2024-11-16 22:34:12 -08:00
|
|
|
ltoObjName = ctx.saver.save(path.str());
|
[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
|
|
|
}
|
2024-09-21 10:11:37 -07:00
|
|
|
if (savePrelink || ctx.arg.ltoEmitAsm)
|
[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, ltoObjName);
|
2024-09-21 10:11:37 -07:00
|
|
|
if (!ctx.arg.ltoEmitAsm)
|
2024-10-06 18:09:52 -07:00
|
|
|
ret.push_back(createObjFile(ctx, MemoryBufferRef(objBuf, ltoObjName)));
|
[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
|
|
|
}
|
2016-09-29 00:40:08 +00:00
|
|
|
return ret;
|
2016-03-23 21:19:27 +00:00
|
|
|
}
|