2009-02-03 08:13:24 +01:00
|
|
|
//===-- gold-plugin.cpp - Plugin to gold for Link Time Optimization ------===//
|
|
|
|
//
|
2019-01-19 09:50:56 +01: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
|
2009-02-03 08:13:24 +01:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This is a gold plugin for LLVM. It provides an LLVM implementation of the
|
|
|
|
// interface described in http://gcc.gnu.org/wiki/whopr/driver .
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2017-02-02 18:33:53 +01:00
|
|
|
#include "llvm/ADT/Statistic.h"
|
2016-11-11 06:34:58 +01:00
|
|
|
#include "llvm/Bitcode/BitcodeReader.h"
|
|
|
|
#include "llvm/Bitcode/BitcodeWriter.h"
|
2020-03-04 00:47:43 +01:00
|
|
|
#include "llvm/CodeGen/CommandFlags.h"
|
2016-04-18 11:17:29 +02:00
|
|
|
#include "llvm/Config/config.h" // plugin-api.h requires HAVE_STDINT_H
|
2014-09-09 22:08:22 +02:00
|
|
|
#include "llvm/IR/Constants.h"
|
2015-01-10 01:07:30 +01:00
|
|
|
#include "llvm/IR/DiagnosticPrinter.h"
|
2016-08-24 17:11:47 +02:00
|
|
|
#include "llvm/LTO/Caching.h"
|
[ThinLTO/gold] Handle bitcode archives
Summary:
Several changes were required for ThinLTO links involving bitcode
archive static libraries. With this patch clang/llvm bootstraps with
ThinLTO and gold.
The first is that the gold callbacks get_input_file and
release_input_file can normally be used to get file information for
each constituent bitcode file within an archive. However, these
interfaces lock the underlying file and can't be for each archive
constituent for ThinLTO backends where we get all the input files up
front and don't release any until after the backend threads complete.
However, it is sufficient to only get and release once per file, and
then each consituent bitcode file can be accessed via get_view. This
required saving some information to identify which file handle is the
"leader" for each claimed file sharing the same file descriptor, and
other information so that get_input_file isn't necessary later when
processing the backends.
Second, the module paths in the index need to distinguish between
different constituent bitcode files within the same archive file,
otherwise they will all end up with the same archive file path.
Do this by appending the offset within the archive for the start of the
bitcode file, returned by get_input_file when we claim each bitcode file,
and saving that along with the file handle.
Third, rather than have the function importer try to load a file based
on the module path identifier (which now contains a suffix to
distinguish different bitcode files within an archive), use a custom
module loader. This is the same approach taken in libLTO, and I am using
the support refactored into the new LTO.h header in r270509. The module
loader parses the bitcode files out of the memory buffers returned from
gold via the get_view callback and saved in a map. This also means that
we call the function importer directly, rather than add it to the pass
pipeline (which was in the plan to do already for other reasons).
Reviewers: pcc, joker.eph
Subscribers: llvm-commits, joker.eph
Differential Revision: http://reviews.llvm.org/D20559
llvm-svn: 270814
2016-05-26 03:46:41 +02:00
|
|
|
#include "llvm/LTO/LTO.h"
|
2017-03-29 01:35:34 +02:00
|
|
|
#include "llvm/Object/Error.h"
|
2017-09-19 01:24:55 +02:00
|
|
|
#include "llvm/Support/CachePruning.h"
|
2016-08-11 16:58:12 +02:00
|
|
|
#include "llvm/Support/CommandLine.h"
|
2017-03-29 01:35:34 +02:00
|
|
|
#include "llvm/Support/FileSystem.h"
|
2020-05-20 11:55:37 +02:00
|
|
|
#include "llvm/Support/Host.h"
|
2014-11-25 21:52:49 +01:00
|
|
|
#include "llvm/Support/ManagedStatic.h"
|
2012-12-04 11:44:52 +01:00
|
|
|
#include "llvm/Support/MemoryBuffer.h"
|
2016-05-17 16:45:30 +02:00
|
|
|
#include "llvm/Support/Path.h"
|
2014-06-19 23:14:13 +02:00
|
|
|
#include "llvm/Support/TargetSelect.h"
|
2020-03-27 15:20:39 +01:00
|
|
|
#include "llvm/Support/Threading.h"
|
2015-12-09 20:45:55 +01:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2009-02-03 08:13:24 +01:00
|
|
|
#include <list>
|
2016-08-11 16:58:12 +02:00
|
|
|
#include <map>
|
2014-01-13 09:04:33 +01:00
|
|
|
#include <plugin-api.h>
|
2016-08-11 16:58:12 +02:00
|
|
|
#include <string>
|
2014-06-12 19:38:55 +02:00
|
|
|
#include <system_error>
|
2016-05-27 16:27:24 +02:00
|
|
|
#include <utility>
|
2009-02-03 08:13:24 +01:00
|
|
|
#include <vector>
|
|
|
|
|
2014-02-11 18:30:18 +01:00
|
|
|
// FIXME: remove this declaration when we stop maintaining Ubuntu Quantal and
|
|
|
|
// Precise and Debian Wheezy (binutils 2.23 is required)
|
2016-03-04 01:23:29 +01:00
|
|
|
#define LDPO_PIE 3
|
|
|
|
|
|
|
|
#define LDPT_GET_SYMBOLS_V3 28
|
2014-02-11 18:30:18 +01:00
|
|
|
|
2018-03-14 14:26:18 +01:00
|
|
|
// FIXME: Remove when binutils 2.31 (containing gold 1.16) is the minimum
|
|
|
|
// required version.
|
|
|
|
#define LDPT_GET_WRAP_SYMBOLS 32
|
|
|
|
|
2009-02-03 08:13:24 +01:00
|
|
|
using namespace llvm;
|
2016-08-11 16:58:12 +02:00
|
|
|
using namespace lto;
|
2009-02-03 08:13:24 +01:00
|
|
|
|
2020-03-04 00:47:43 +01:00
|
|
|
static codegen::RegisterCodeGenFlags CodeGenFlags;
|
|
|
|
|
2018-03-14 14:26:18 +01:00
|
|
|
// FIXME: Remove when binutils 2.31 (containing gold 1.16) is the minimum
|
|
|
|
// required version.
|
|
|
|
typedef enum ld_plugin_status (*ld_plugin_get_wrap_symbols)(
|
|
|
|
uint64_t *num_symbols, const char ***wrap_symbol_list);
|
|
|
|
|
2015-12-16 17:34:06 +01:00
|
|
|
static ld_plugin_status discard_message(int level, const char *format, ...) {
|
|
|
|
// Die loudly. Recent versions of Gold pass ld_plugin_message as the first
|
|
|
|
// callback in the transfer vector. This should never be called.
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
|
|
|
|
static ld_plugin_release_input_file release_input_file = nullptr;
|
|
|
|
static ld_plugin_get_input_file get_input_file = nullptr;
|
|
|
|
static ld_plugin_message message = discard_message;
|
2018-03-14 14:26:18 +01:00
|
|
|
static ld_plugin_get_wrap_symbols get_wrap_symbols = nullptr;
|
2015-12-16 17:34:06 +01:00
|
|
|
|
2009-02-03 08:13:24 +01:00
|
|
|
namespace {
|
2014-06-20 03:37:35 +02:00
|
|
|
struct claimed_file {
|
|
|
|
void *handle;
|
[ThinLTO/gold] Handle bitcode archives
Summary:
Several changes were required for ThinLTO links involving bitcode
archive static libraries. With this patch clang/llvm bootstraps with
ThinLTO and gold.
The first is that the gold callbacks get_input_file and
release_input_file can normally be used to get file information for
each constituent bitcode file within an archive. However, these
interfaces lock the underlying file and can't be for each archive
constituent for ThinLTO backends where we get all the input files up
front and don't release any until after the backend threads complete.
However, it is sufficient to only get and release once per file, and
then each consituent bitcode file can be accessed via get_view. This
required saving some information to identify which file handle is the
"leader" for each claimed file sharing the same file descriptor, and
other information so that get_input_file isn't necessary later when
processing the backends.
Second, the module paths in the index need to distinguish between
different constituent bitcode files within the same archive file,
otherwise they will all end up with the same archive file path.
Do this by appending the offset within the archive for the start of the
bitcode file, returned by get_input_file when we claim each bitcode file,
and saving that along with the file handle.
Third, rather than have the function importer try to load a file based
on the module path identifier (which now contains a suffix to
distinguish different bitcode files within an archive), use a custom
module loader. This is the same approach taken in libLTO, and I am using
the support refactored into the new LTO.h header in r270509. The module
loader parses the bitcode files out of the memory buffers returned from
gold via the get_view callback and saved in a map. This also means that
we call the function importer directly, rather than add it to the pass
pipeline (which was in the plan to do already for other reasons).
Reviewers: pcc, joker.eph
Subscribers: llvm-commits, joker.eph
Differential Revision: http://reviews.llvm.org/D20559
llvm-svn: 270814
2016-05-26 03:46:41 +02:00
|
|
|
void *leader_handle;
|
2014-06-20 03:37:35 +02:00
|
|
|
std::vector<ld_plugin_symbol> syms;
|
[ThinLTO/gold] Handle bitcode archives
Summary:
Several changes were required for ThinLTO links involving bitcode
archive static libraries. With this patch clang/llvm bootstraps with
ThinLTO and gold.
The first is that the gold callbacks get_input_file and
release_input_file can normally be used to get file information for
each constituent bitcode file within an archive. However, these
interfaces lock the underlying file and can't be for each archive
constituent for ThinLTO backends where we get all the input files up
front and don't release any until after the backend threads complete.
However, it is sufficient to only get and release once per file, and
then each consituent bitcode file can be accessed via get_view. This
required saving some information to identify which file handle is the
"leader" for each claimed file sharing the same file descriptor, and
other information so that get_input_file isn't necessary later when
processing the backends.
Second, the module paths in the index need to distinguish between
different constituent bitcode files within the same archive file,
otherwise they will all end up with the same archive file path.
Do this by appending the offset within the archive for the start of the
bitcode file, returned by get_input_file when we claim each bitcode file,
and saving that along with the file handle.
Third, rather than have the function importer try to load a file based
on the module path identifier (which now contains a suffix to
distinguish different bitcode files within an archive), use a custom
module loader. This is the same approach taken in libLTO, and I am using
the support refactored into the new LTO.h header in r270509. The module
loader parses the bitcode files out of the memory buffers returned from
gold via the get_view callback and saved in a map. This also means that
we call the function importer directly, rather than add it to the pass
pipeline (which was in the plan to do already for other reasons).
Reviewers: pcc, joker.eph
Subscribers: llvm-commits, joker.eph
Differential Revision: http://reviews.llvm.org/D20559
llvm-svn: 270814
2016-05-26 03:46:41 +02:00
|
|
|
off_t filesize;
|
|
|
|
std::string name;
|
2014-06-20 03:37:35 +02:00
|
|
|
};
|
2015-12-10 15:19:35 +01:00
|
|
|
|
2015-12-16 17:34:06 +01:00
|
|
|
/// RAII wrapper to manage opening and releasing of a ld_plugin_input_file.
|
|
|
|
struct PluginInputFile {
|
2015-12-16 22:37:48 +01:00
|
|
|
void *Handle;
|
2016-03-04 18:06:02 +01:00
|
|
|
std::unique_ptr<ld_plugin_input_file> File;
|
2015-12-16 17:34:06 +01:00
|
|
|
|
2015-12-16 22:37:48 +01:00
|
|
|
PluginInputFile(void *Handle) : Handle(Handle) {
|
2019-08-15 17:54:37 +02:00
|
|
|
File = std::make_unique<ld_plugin_input_file>();
|
2016-03-04 18:06:02 +01:00
|
|
|
if (get_input_file(Handle, File.get()) != LDPS_OK)
|
2015-12-16 17:34:06 +01:00
|
|
|
message(LDPL_FATAL, "Failed to get file information");
|
|
|
|
}
|
|
|
|
~PluginInputFile() {
|
2016-03-04 18:06:02 +01:00
|
|
|
// File would have been reset to nullptr if we moved this object
|
|
|
|
// to a new owner.
|
|
|
|
if (File)
|
|
|
|
if (release_input_file(Handle) != LDPS_OK)
|
|
|
|
message(LDPL_FATAL, "Failed to release file information");
|
2015-12-16 17:34:06 +01:00
|
|
|
}
|
2016-03-04 18:06:02 +01:00
|
|
|
|
|
|
|
ld_plugin_input_file &file() { return *File; }
|
|
|
|
|
|
|
|
PluginInputFile(PluginInputFile &&RHS) = default;
|
|
|
|
PluginInputFile &operator=(PluginInputFile &&RHS) = default;
|
2015-12-16 17:34:06 +01:00
|
|
|
};
|
|
|
|
|
2015-12-10 15:19:35 +01:00
|
|
|
struct ResolutionInfo {
|
2016-08-11 16:58:12 +02:00
|
|
|
bool CanOmitFromDynSym = true;
|
|
|
|
bool DefaultVisibility = true;
|
2018-03-14 14:26:18 +01:00
|
|
|
bool CanInline = true;
|
|
|
|
bool IsUsedInRegularObj = false;
|
2015-12-10 15:19:35 +01:00
|
|
|
};
|
2016-03-04 18:06:02 +01:00
|
|
|
|
2014-06-20 03:37:35 +02:00
|
|
|
}
|
2009-02-03 08:13:24 +01:00
|
|
|
|
2014-07-29 23:46:05 +02:00
|
|
|
static ld_plugin_add_symbols add_symbols = nullptr;
|
|
|
|
static ld_plugin_get_symbols get_symbols = nullptr;
|
|
|
|
static ld_plugin_add_input_file add_input_file = nullptr;
|
|
|
|
static ld_plugin_set_extra_library_path set_extra_library_path = nullptr;
|
|
|
|
static ld_plugin_get_view get_view = nullptr;
|
2016-08-11 16:58:12 +02:00
|
|
|
static bool IsExecutable = false;
|
2018-07-12 22:35:58 +02:00
|
|
|
static bool SplitSections = true;
|
2017-05-22 23:11:35 +02:00
|
|
|
static Optional<Reloc::Model> RelocationModel = None;
|
2014-06-20 03:37:35 +02:00
|
|
|
static std::string output_name = "";
|
|
|
|
static std::list<claimed_file> Modules;
|
[ThinLTO/gold] Handle bitcode archives
Summary:
Several changes were required for ThinLTO links involving bitcode
archive static libraries. With this patch clang/llvm bootstraps with
ThinLTO and gold.
The first is that the gold callbacks get_input_file and
release_input_file can normally be used to get file information for
each constituent bitcode file within an archive. However, these
interfaces lock the underlying file and can't be for each archive
constituent for ThinLTO backends where we get all the input files up
front and don't release any until after the backend threads complete.
However, it is sufficient to only get and release once per file, and
then each consituent bitcode file can be accessed via get_view. This
required saving some information to identify which file handle is the
"leader" for each claimed file sharing the same file descriptor, and
other information so that get_input_file isn't necessary later when
processing the backends.
Second, the module paths in the index need to distinguish between
different constituent bitcode files within the same archive file,
otherwise they will all end up with the same archive file path.
Do this by appending the offset within the archive for the start of the
bitcode file, returned by get_input_file when we claim each bitcode file,
and saving that along with the file handle.
Third, rather than have the function importer try to load a file based
on the module path identifier (which now contains a suffix to
distinguish different bitcode files within an archive), use a custom
module loader. This is the same approach taken in libLTO, and I am using
the support refactored into the new LTO.h header in r270509. The module
loader parses the bitcode files out of the memory buffers returned from
gold via the get_view callback and saved in a map. This also means that
we call the function importer directly, rather than add it to the pass
pipeline (which was in the plan to do already for other reasons).
Reviewers: pcc, joker.eph
Subscribers: llvm-commits, joker.eph
Differential Revision: http://reviews.llvm.org/D20559
llvm-svn: 270814
2016-05-26 03:46:41 +02:00
|
|
|
static DenseMap<int, void *> FDToLeaderHandle;
|
2015-12-10 15:19:35 +01:00
|
|
|
static StringMap<ResolutionInfo> ResInfo;
|
2014-06-20 03:37:35 +02:00
|
|
|
static std::vector<std::string> Cleanup;
|
2009-02-03 08:13:24 +01:00
|
|
|
|
2009-10-28 19:55:55 +01:00
|
|
|
namespace options {
|
2014-11-24 22:18:14 +01:00
|
|
|
enum OutputType {
|
|
|
|
OT_NORMAL,
|
|
|
|
OT_DISABLE,
|
|
|
|
OT_BC_ONLY,
|
2019-01-03 00:48:00 +01:00
|
|
|
OT_ASM_ONLY,
|
2014-11-24 22:18:14 +01:00
|
|
|
OT_SAVE_TEMPS
|
|
|
|
};
|
|
|
|
static OutputType TheOutputType = OT_NORMAL;
|
2015-03-19 23:01:00 +01:00
|
|
|
static unsigned OptLevel = 2;
|
[Support] On Windows, ensure hardware_concurrency() extends to all CPU sockets and all NUMA groups
The goal of this patch is to maximize CPU utilization on multi-socket or high core count systems, so that parallel computations such as LLD/ThinLTO can use all hardware threads in the system. Before this patch, on Windows, a maximum of 64 hardware threads could be used at most, in some cases dispatched only on one CPU socket.
== Background ==
Windows doesn't have a flat cpu_set_t like Linux. Instead, it projects hardware CPUs (or NUMA nodes) to applications through a concept of "processor groups". A "processor" is the smallest unit of execution on a CPU, that is, an hyper-thread if SMT is active; a core otherwise. There's a limit of 32-bit processors on older 32-bit versions of Windows, which later was raised to 64-processors with 64-bit versions of Windows. This limit comes from the affinity mask, which historically is represented by the sizeof(void*). Consequently, the concept of "processor groups" was introduced for dealing with systems with more than 64 hyper-threads.
By default, the Windows OS assigns only one "processor group" to each starting application, in a round-robin manner. If the application wants to use more processors, it needs to programmatically enable it, by assigning threads to other "processor groups". This also means that affinity cannot cross "processor group" boundaries; one can only specify a "preferred" group on start-up, but the application is free to allocate more groups if it wants to.
This creates a peculiar situation, where newer CPUs like the AMD EPYC 7702P (64-cores, 128-hyperthreads) are projected by the OS as two (2) "processor groups". This means that by default, an application can only use half of the cores. This situation could only get worse in the years to come, as dies with more cores will appear on the market.
== The problem ==
The heavyweight_hardware_concurrency() API was introduced so that only *one hardware thread per core* was used. Once that API returns, that original intention is lost, only the number of threads is retained. Consider a situation, on Windows, where the system has 2 CPU sockets, 18 cores each, each core having 2 hyper-threads, for a total of 72 hyper-threads. Both heavyweight_hardware_concurrency() and hardware_concurrency() currently return 36, because on Windows they are simply wrappers over std::thread::hardware_concurrency() -- which can only return processors from the current "processor group".
== The changes in this patch ==
To solve this situation, we capture (and retain) the initial intention until the point of usage, through a new ThreadPoolStrategy class. The number of threads to use is deferred as late as possible, until the moment where the std::threads are created (ThreadPool in the case of ThinLTO).
When using hardware_concurrency(), setting ThreadCount to 0 now means to use all the possible hardware CPU (SMT) threads. Providing a ThreadCount above to the maximum number of threads will have no effect, the maximum will be used instead.
The heavyweight_hardware_concurrency() is similar to hardware_concurrency(), except that only one thread per hardware *core* will be used.
When LLVM_ENABLE_THREADS is OFF, the threading APIs will always return 1, to ensure any caller loops will be exercised at least once.
Differential Revision: https://reviews.llvm.org/D71775
2020-02-14 04:49:57 +01:00
|
|
|
// Currently only affects ThinLTO, where the default is the max cores in the
|
2020-03-27 15:20:39 +01:00
|
|
|
// system. See llvm::get_threadpool_strategy() for acceptable values.
|
|
|
|
static std::string Parallelism;
|
2016-09-23 22:35:19 +02:00
|
|
|
// Default regular LTO codegen parallelism (number of partitions).
|
|
|
|
static unsigned ParallelCodeGenParallelismLevel = 1;
|
2015-09-16 20:06:45 +02:00
|
|
|
#ifdef NDEBUG
|
|
|
|
static bool DisableVerify = true;
|
|
|
|
#else
|
|
|
|
static bool DisableVerify = false;
|
|
|
|
#endif
|
2013-08-12 23:07:31 +02:00
|
|
|
static std::string obj_path;
|
2010-06-23 22:20:59 +02:00
|
|
|
static std::string extra_library_path;
|
2010-08-09 23:09:46 +02:00
|
|
|
static std::string triple;
|
2010-08-11 02:15:13 +02:00
|
|
|
static std::string mcpu;
|
2015-10-04 16:33:43 +02:00
|
|
|
// When the thinlto plugin option is specified, only read the function
|
|
|
|
// the information from intermediate files and write a combined
|
|
|
|
// global index for the ThinLTO backends.
|
|
|
|
static bool thinlto = false;
|
2016-03-04 18:06:02 +01:00
|
|
|
// If false, all ThinLTO backend compilations through code gen are performed
|
|
|
|
// using multiple threads in the gold-plugin, before handing control back to
|
2016-05-10 15:48:23 +02:00
|
|
|
// gold. If true, write individual backend index files which reflect
|
|
|
|
// the import decisions, and exit afterwards. The assumption is
|
2016-03-04 18:06:02 +01:00
|
|
|
// that the build system will launch the backend processes.
|
|
|
|
static bool thinlto_index_only = false;
|
2016-07-22 20:20:22 +02:00
|
|
|
// If non-empty, holds the name of a file in which to write the list of
|
|
|
|
// oject files gold selected for inclusion in the link after symbol
|
|
|
|
// resolution (i.e. they had selected symbols). This will only be non-empty
|
|
|
|
// in the thinlto_index_only case. It is used to identify files, which may
|
|
|
|
// have originally been within archive libraries specified via
|
|
|
|
// --start-lib/--end-lib pairs, that should be included in the final
|
|
|
|
// native link process (since intervening function importing and inlining
|
|
|
|
// may change the symbol resolution detected in the final link and which
|
|
|
|
// files to include out of --start-lib/--end-lib libraries as a result).
|
|
|
|
static std::string thinlto_linked_objects_file;
|
2016-05-10 17:54:09 +02:00
|
|
|
// If true, when generating individual index files for distributed backends,
|
|
|
|
// also generate a "${bitcodefile}.imports" file at the same location for each
|
|
|
|
// bitcode file, listing the files it imports from in plain text. This is to
|
|
|
|
// support distributed build file staging.
|
|
|
|
static bool thinlto_emit_imports_files = false;
|
2016-05-17 16:45:30 +02:00
|
|
|
// Option to control where files for a distributed backend (the individual
|
|
|
|
// index files and optional imports files) are created.
|
|
|
|
// If specified, expects a string of the form "oldprefix:newprefix", and
|
|
|
|
// instead of generating these files in the same directory path as the
|
|
|
|
// corresponding bitcode file, will use a path formed by replacing the
|
|
|
|
// bitcode file's path prefix matching oldprefix with newprefix.
|
|
|
|
static std::string thinlto_prefix_replace;
|
[ThinLTO] Add support for emitting minimized bitcode for thin link
Summary:
The cumulative size of the bitcode files for a very large application
can be huge, particularly with -g. In a distributed build environment,
all of these files must be sent to the remote build node that performs
the thin link step, and this can exceed size limits.
The thin link actually only needs the summary along with a bitcode
symbol table. Until we have a proper bitcode symbol table, simply
stripping the debug metadata results in significant size reduction.
Add support for an option to additionally emit minimized bitcode
modules, just for use in the thin link step, which for now just strips
all debug metadata. I plan to add a cc1 option so this can be invoked
easily during the compile step.
However, care must be taken to ensure that these minimized thin link
bitcode files produce the same index as with the original bitcode files,
as these original bitcode files will be used in the backends.
Specifically:
1) The module hash used for caching is typically produced by hashing the
written bitcode, and we want to include the hash that would correspond
to the original bitcode file. This is because we want to ensure that
changes in the stripped portions affect caching. Added plumbing to emit
the same module hash in the minimized thin link bitcode file.
2) The module paths in the index are constructed from the module ID of
each thin linked bitcode, and typically is automatically generated from
the input file path. This is the path used for finding the modules to
import from, and obviously we need this to point to the original bitcode
files. Added gold-plugin support to take a suffix replacement during the
thin link that is used to override the identifier on the MemoryBufferRef
constructed from the loaded thin link bitcode file. The assumption is
that the build system can specify that the minimized bitcode file has a
name that is similar but uses a different suffix (e.g. out.thinlink.bc
instead of out.o).
Added various tests to ensure that we get identical index files out of
the thin link step.
Reviewers: mehdi_amini, pcc
Subscribers: Prazek, llvm-commits
Differential Revision: https://reviews.llvm.org/D31027
llvm-svn: 298638
2017-03-23 20:47:39 +01:00
|
|
|
// Option to control the name of modules encoded in the individual index
|
|
|
|
// files for a distributed backend. This enables the use of minimized
|
|
|
|
// bitcode files for the thin link, assuming the name of the full bitcode
|
|
|
|
// file used in the backend differs just in some part of the file suffix.
|
|
|
|
// If specified, expects a string of the form "oldsuffix:newsuffix".
|
|
|
|
static std::string thinlto_object_suffix_replace;
|
2016-08-24 17:11:47 +02:00
|
|
|
// Optional path to a directory for caching ThinLTO objects.
|
|
|
|
static std::string cache_dir;
|
2017-09-19 01:24:55 +02:00
|
|
|
// Optional pruning policy for ThinLTO caches.
|
|
|
|
static std::string cache_policy;
|
2009-10-28 19:55:55 +01:00
|
|
|
// Additional options to pass into the code generator.
|
2010-06-03 19:10:17 +02:00
|
|
|
// Note: This array will contain all plugin options which are not claimed
|
2009-10-28 19:55:55 +01:00
|
|
|
// as plugin exclusive to pass to the code generator.
|
2014-07-29 21:17:44 +02:00
|
|
|
static std::vector<const char *> extra;
|
2016-12-16 17:48:46 +01:00
|
|
|
// Sample profile file path
|
|
|
|
static std::string sample_profile;
|
2017-10-05 03:48:42 +02:00
|
|
|
// New pass manager
|
|
|
|
static bool new_pass_manager = false;
|
2018-04-05 05:16:57 +02:00
|
|
|
// Debug new pass manager
|
|
|
|
static bool debug_pass_manager = false;
|
2018-04-13 07:03:28 +02:00
|
|
|
// Directory to store the .dwo files.
|
|
|
|
static std::string dwo_dir;
|
2018-04-20 12:18:36 +02:00
|
|
|
/// Statistics output filename.
|
|
|
|
static std::string stats_file;
|
2020-01-24 21:24:18 +01:00
|
|
|
// Asserts that LTO link has whole program visibility
|
|
|
|
static bool whole_program_visibility = false;
|
2009-10-28 19:55:55 +01:00
|
|
|
|
2019-03-12 22:22:27 +01:00
|
|
|
// Optimization remarks filename, accepted passes and hotness options
|
2019-06-14 18:20:51 +02:00
|
|
|
static std::string RemarksFilename;
|
|
|
|
static std::string RemarksPasses;
|
|
|
|
static bool RemarksWithHotness = false;
|
2019-06-17 18:06:00 +02:00
|
|
|
static std::string RemarksFormat;
|
2018-04-18 15:25:23 +02:00
|
|
|
|
2019-02-27 18:24:33 +01:00
|
|
|
// Context sensitive PGO options.
|
|
|
|
static std::string cs_profile_path;
|
|
|
|
static bool cs_pgo_gen = false;
|
|
|
|
|
2015-08-05 23:16:02 +02:00
|
|
|
static void process_plugin_option(const char *opt_)
|
2009-10-28 19:55:55 +01:00
|
|
|
{
|
2014-07-29 23:46:05 +02:00
|
|
|
if (opt_ == nullptr)
|
2009-10-28 19:55:55 +01:00
|
|
|
return;
|
2010-06-07 18:45:22 +02:00
|
|
|
llvm::StringRef opt = opt_;
|
2009-10-28 19:55:55 +01:00
|
|
|
|
2020-04-24 19:41:28 +02:00
|
|
|
if (opt.consume_front("mcpu=")) {
|
|
|
|
mcpu = std::string(opt);
|
|
|
|
} else if (opt.consume_front("extra-library-path=")) {
|
|
|
|
extra_library_path = std::string(opt);
|
|
|
|
} else if (opt.consume_front("mtriple=")) {
|
|
|
|
triple = std::string(opt);
|
|
|
|
} else if (opt.consume_front("obj-path=")) {
|
|
|
|
obj_path = std::string(opt);
|
2010-06-07 18:45:22 +02:00
|
|
|
} else if (opt == "emit-llvm") {
|
2014-11-24 22:18:14 +01:00
|
|
|
TheOutputType = OT_BC_ONLY;
|
2014-10-30 00:54:45 +01:00
|
|
|
} else if (opt == "save-temps") {
|
2014-11-24 22:18:14 +01:00
|
|
|
TheOutputType = OT_SAVE_TEMPS;
|
|
|
|
} else if (opt == "disable-output") {
|
|
|
|
TheOutputType = OT_DISABLE;
|
2019-01-03 00:48:00 +01:00
|
|
|
} else if (opt == "emit-asm") {
|
|
|
|
TheOutputType = OT_ASM_ONLY;
|
2015-10-04 16:33:43 +02:00
|
|
|
} else if (opt == "thinlto") {
|
|
|
|
thinlto = true;
|
2016-03-04 18:06:02 +01:00
|
|
|
} else if (opt == "thinlto-index-only") {
|
|
|
|
thinlto_index_only = true;
|
2020-04-24 19:41:28 +02:00
|
|
|
} else if (opt.consume_front("thinlto-index-only=")) {
|
2016-07-22 20:20:22 +02:00
|
|
|
thinlto_index_only = true;
|
2020-04-24 19:41:28 +02:00
|
|
|
thinlto_linked_objects_file = std::string(opt);
|
2016-05-10 17:54:09 +02:00
|
|
|
} else if (opt == "thinlto-emit-imports-files") {
|
|
|
|
thinlto_emit_imports_files = true;
|
2020-04-24 19:41:28 +02:00
|
|
|
} else if (opt.consume_front("thinlto-prefix-replace=")) {
|
|
|
|
thinlto_prefix_replace = std::string(opt);
|
2016-11-30 11:01:11 +01:00
|
|
|
if (thinlto_prefix_replace.find(';') == std::string::npos)
|
2016-05-17 20:43:22 +02:00
|
|
|
message(LDPL_FATAL, "thinlto-prefix-replace expects 'old;new' format");
|
2020-04-24 19:41:28 +02:00
|
|
|
} else if (opt.consume_front("thinlto-object-suffix-replace=")) {
|
|
|
|
thinlto_object_suffix_replace = std::string(opt);
|
[ThinLTO] Add support for emitting minimized bitcode for thin link
Summary:
The cumulative size of the bitcode files for a very large application
can be huge, particularly with -g. In a distributed build environment,
all of these files must be sent to the remote build node that performs
the thin link step, and this can exceed size limits.
The thin link actually only needs the summary along with a bitcode
symbol table. Until we have a proper bitcode symbol table, simply
stripping the debug metadata results in significant size reduction.
Add support for an option to additionally emit minimized bitcode
modules, just for use in the thin link step, which for now just strips
all debug metadata. I plan to add a cc1 option so this can be invoked
easily during the compile step.
However, care must be taken to ensure that these minimized thin link
bitcode files produce the same index as with the original bitcode files,
as these original bitcode files will be used in the backends.
Specifically:
1) The module hash used for caching is typically produced by hashing the
written bitcode, and we want to include the hash that would correspond
to the original bitcode file. This is because we want to ensure that
changes in the stripped portions affect caching. Added plumbing to emit
the same module hash in the minimized thin link bitcode file.
2) The module paths in the index are constructed from the module ID of
each thin linked bitcode, and typically is automatically generated from
the input file path. This is the path used for finding the modules to
import from, and obviously we need this to point to the original bitcode
files. Added gold-plugin support to take a suffix replacement during the
thin link that is used to override the identifier on the MemoryBufferRef
constructed from the loaded thin link bitcode file. The assumption is
that the build system can specify that the minimized bitcode file has a
name that is similar but uses a different suffix (e.g. out.thinlink.bc
instead of out.o).
Added various tests to ensure that we get identical index files out of
the thin link step.
Reviewers: mehdi_amini, pcc
Subscribers: Prazek, llvm-commits
Differential Revision: https://reviews.llvm.org/D31027
llvm-svn: 298638
2017-03-23 20:47:39 +01:00
|
|
|
if (thinlto_object_suffix_replace.find(';') == std::string::npos)
|
|
|
|
message(LDPL_FATAL,
|
|
|
|
"thinlto-object-suffix-replace expects 'old;new' format");
|
2020-04-24 19:41:28 +02:00
|
|
|
} else if (opt.consume_front("cache-dir=")) {
|
|
|
|
cache_dir = std::string(opt);
|
|
|
|
} else if (opt.consume_front("cache-policy=")) {
|
|
|
|
cache_policy = std::string(opt);
|
2015-03-19 23:01:00 +01:00
|
|
|
} else if (opt.size() == 2 && opt[0] == 'O') {
|
|
|
|
if (opt[1] < '0' || opt[1] > '3')
|
2015-09-01 22:40:22 +02:00
|
|
|
message(LDPL_FATAL, "Optimization level must be between 0 and 3");
|
2015-03-19 23:01:00 +01:00
|
|
|
OptLevel = opt[1] - '0';
|
2020-04-24 19:41:28 +02:00
|
|
|
} else if (opt.consume_front("jobs=")) {
|
|
|
|
Parallelism = std::string(opt);
|
|
|
|
if (!get_threadpool_strategy(opt))
|
|
|
|
message(LDPL_FATAL, "Invalid parallelism level: %s",
|
|
|
|
Parallelism.c_str());
|
|
|
|
} else if (opt.consume_front("lto-partitions=")) {
|
|
|
|
if (opt.getAsInteger(10, ParallelCodeGenParallelismLevel))
|
2016-09-23 22:35:19 +02:00
|
|
|
message(LDPL_FATAL, "Invalid codegen partition level: %s", opt_ + 5);
|
2015-09-16 20:06:45 +02:00
|
|
|
} else if (opt == "disable-verify") {
|
|
|
|
DisableVerify = true;
|
2020-04-24 19:41:28 +02:00
|
|
|
} else if (opt.consume_front("sample-profile=")) {
|
|
|
|
sample_profile = std::string(opt);
|
2019-02-27 18:24:33 +01:00
|
|
|
} else if (opt == "cs-profile-generate") {
|
|
|
|
cs_pgo_gen = true;
|
2020-04-24 19:41:28 +02:00
|
|
|
} else if (opt.consume_front("cs-profile-path=")) {
|
|
|
|
cs_profile_path = std::string(opt);
|
2017-10-05 03:48:42 +02:00
|
|
|
} else if (opt == "new-pass-manager") {
|
|
|
|
new_pass_manager = true;
|
2018-04-05 05:16:57 +02:00
|
|
|
} else if (opt == "debug-pass-manager") {
|
|
|
|
debug_pass_manager = true;
|
2020-01-24 21:24:18 +01:00
|
|
|
} else if (opt == "whole-program-visibility") {
|
|
|
|
whole_program_visibility = true;
|
2020-04-24 19:41:28 +02:00
|
|
|
} else if (opt.consume_front("dwo_dir=")) {
|
|
|
|
dwo_dir = std::string(opt);
|
|
|
|
} else if (opt.consume_front("opt-remarks-filename=")) {
|
|
|
|
RemarksFilename = std::string(opt);
|
|
|
|
} else if (opt.consume_front("opt-remarks-passes=")) {
|
|
|
|
RemarksPasses = std::string(opt);
|
2018-04-18 15:25:23 +02:00
|
|
|
} else if (opt == "opt-remarks-with-hotness") {
|
2019-06-14 18:20:51 +02:00
|
|
|
RemarksWithHotness = true;
|
2020-04-24 19:41:28 +02:00
|
|
|
} else if (opt.consume_front("opt-remarks-format=")) {
|
|
|
|
RemarksFormat = std::string(opt);
|
|
|
|
} else if (opt.consume_front("stats-file=")) {
|
|
|
|
stats_file = std::string(opt);
|
2009-10-28 19:55:55 +01:00
|
|
|
} else {
|
|
|
|
// Save this option to pass to the code generator.
|
2014-08-21 22:28:55 +02:00
|
|
|
// ParseCommandLineOptions() expects argv[0] to be program name. Lazily
|
|
|
|
// add that.
|
|
|
|
if (extra.empty())
|
|
|
|
extra.push_back("LLVMgold");
|
|
|
|
|
2014-07-29 21:17:44 +02:00
|
|
|
extra.push_back(opt_);
|
2009-10-28 19:55:55 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-04-16 02:42:57 +02:00
|
|
|
static ld_plugin_status claim_file_hook(const ld_plugin_input_file *file,
|
|
|
|
int *claimed);
|
|
|
|
static ld_plugin_status all_symbols_read_hook(void);
|
|
|
|
static ld_plugin_status cleanup_hook(void);
|
2009-02-03 08:13:24 +01:00
|
|
|
|
|
|
|
extern "C" ld_plugin_status onload(ld_plugin_tv *tv);
|
|
|
|
ld_plugin_status onload(ld_plugin_tv *tv) {
|
2014-07-04 01:28:03 +02:00
|
|
|
InitializeAllTargetInfos();
|
|
|
|
InitializeAllTargets();
|
|
|
|
InitializeAllTargetMCs();
|
|
|
|
InitializeAllAsmParsers();
|
|
|
|
InitializeAllAsmPrinters();
|
|
|
|
|
2009-02-03 08:13:24 +01:00
|
|
|
// We're given a pointer to the first transfer vector. We read through them
|
|
|
|
// until we find one where tv_tag == LDPT_NULL. The REGISTER_* tagged values
|
|
|
|
// contain pointers to functions that we need to call to register our own
|
|
|
|
// hooks. The others are addresses of functions we can use to call into gold
|
|
|
|
// for services.
|
|
|
|
|
|
|
|
bool registeredClaimFile = false;
|
2014-06-19 23:14:13 +02:00
|
|
|
bool RegisteredAllSymbolsRead = false;
|
2009-02-03 08:13:24 +01:00
|
|
|
|
|
|
|
for (; tv->tv_tag != LDPT_NULL; ++tv) {
|
2016-03-04 01:23:29 +01:00
|
|
|
// Cast tv_tag to int to allow values not in "enum ld_plugin_tag", like, for
|
|
|
|
// example, LDPT_GET_SYMBOLS_V3 when building against an older plugin-api.h
|
|
|
|
// header.
|
|
|
|
switch (static_cast<int>(tv->tv_tag)) {
|
|
|
|
case LDPT_OUTPUT_NAME:
|
|
|
|
output_name = tv->tv_u.tv_string;
|
|
|
|
break;
|
|
|
|
case LDPT_LINKER_OUTPUT:
|
|
|
|
switch (tv->tv_u.tv_val) {
|
|
|
|
case LDPO_REL: // .o
|
2017-05-22 23:11:35 +02:00
|
|
|
IsExecutable = false;
|
2018-07-12 22:35:58 +02:00
|
|
|
SplitSections = false;
|
2017-05-22 23:11:35 +02:00
|
|
|
break;
|
2016-03-04 01:23:29 +01:00
|
|
|
case LDPO_DYN: // .so
|
2016-08-11 16:58:12 +02:00
|
|
|
IsExecutable = false;
|
|
|
|
RelocationModel = Reloc::PIC_;
|
|
|
|
break;
|
2016-03-04 01:23:29 +01:00
|
|
|
case LDPO_PIE: // position independent executable
|
2016-08-11 16:58:12 +02:00
|
|
|
IsExecutable = true;
|
2016-03-04 01:23:29 +01:00
|
|
|
RelocationModel = Reloc::PIC_;
|
2011-04-07 23:11:00 +02:00
|
|
|
break;
|
2016-03-04 01:23:29 +01:00
|
|
|
case LDPO_EXEC: // .exe
|
2016-08-11 16:58:12 +02:00
|
|
|
IsExecutable = true;
|
2016-03-04 01:23:29 +01:00
|
|
|
RelocationModel = Reloc::Static;
|
2009-02-03 08:13:24 +01:00
|
|
|
break;
|
|
|
|
default:
|
2016-03-04 01:23:29 +01:00
|
|
|
message(LDPL_ERROR, "Unknown output file type %d", tv->tv_u.tv_val);
|
|
|
|
return LDPS_ERR;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case LDPT_OPTION:
|
|
|
|
options::process_plugin_option(tv->tv_u.tv_string);
|
|
|
|
break;
|
|
|
|
case LDPT_REGISTER_CLAIM_FILE_HOOK: {
|
|
|
|
ld_plugin_register_claim_file callback;
|
|
|
|
callback = tv->tv_u.tv_register_claim_file;
|
|
|
|
|
|
|
|
if (callback(claim_file_hook) != LDPS_OK)
|
|
|
|
return LDPS_ERR;
|
|
|
|
|
|
|
|
registeredClaimFile = true;
|
|
|
|
} break;
|
|
|
|
case LDPT_REGISTER_ALL_SYMBOLS_READ_HOOK: {
|
|
|
|
ld_plugin_register_all_symbols_read callback;
|
|
|
|
callback = tv->tv_u.tv_register_all_symbols_read;
|
|
|
|
|
|
|
|
if (callback(all_symbols_read_hook) != LDPS_OK)
|
|
|
|
return LDPS_ERR;
|
|
|
|
|
|
|
|
RegisteredAllSymbolsRead = true;
|
|
|
|
} break;
|
|
|
|
case LDPT_REGISTER_CLEANUP_HOOK: {
|
|
|
|
ld_plugin_register_cleanup callback;
|
|
|
|
callback = tv->tv_u.tv_register_cleanup;
|
|
|
|
|
|
|
|
if (callback(cleanup_hook) != LDPS_OK)
|
|
|
|
return LDPS_ERR;
|
|
|
|
} break;
|
|
|
|
case LDPT_GET_INPUT_FILE:
|
|
|
|
get_input_file = tv->tv_u.tv_get_input_file;
|
|
|
|
break;
|
|
|
|
case LDPT_RELEASE_INPUT_FILE:
|
|
|
|
release_input_file = tv->tv_u.tv_release_input_file;
|
|
|
|
break;
|
|
|
|
case LDPT_ADD_SYMBOLS:
|
|
|
|
add_symbols = tv->tv_u.tv_add_symbols;
|
|
|
|
break;
|
|
|
|
case LDPT_GET_SYMBOLS_V2:
|
|
|
|
// Do not override get_symbols_v3 with get_symbols_v2.
|
|
|
|
if (!get_symbols)
|
|
|
|
get_symbols = tv->tv_u.tv_get_symbols;
|
|
|
|
break;
|
|
|
|
case LDPT_GET_SYMBOLS_V3:
|
|
|
|
get_symbols = tv->tv_u.tv_get_symbols;
|
|
|
|
break;
|
|
|
|
case LDPT_ADD_INPUT_FILE:
|
|
|
|
add_input_file = tv->tv_u.tv_add_input_file;
|
|
|
|
break;
|
|
|
|
case LDPT_SET_EXTRA_LIBRARY_PATH:
|
|
|
|
set_extra_library_path = tv->tv_u.tv_set_extra_library_path;
|
|
|
|
break;
|
|
|
|
case LDPT_GET_VIEW:
|
|
|
|
get_view = tv->tv_u.tv_get_view;
|
|
|
|
break;
|
|
|
|
case LDPT_MESSAGE:
|
|
|
|
message = tv->tv_u.tv_message;
|
|
|
|
break;
|
2018-03-14 14:26:18 +01:00
|
|
|
case LDPT_GET_WRAP_SYMBOLS:
|
|
|
|
// FIXME: When binutils 2.31 (containing gold 1.16) is the minimum
|
|
|
|
// required version, this should be changed to:
|
|
|
|
// get_wrap_symbols = tv->tv_u.tv_get_wrap_symbols;
|
|
|
|
get_wrap_symbols =
|
2018-03-14 15:00:57 +01:00
|
|
|
(ld_plugin_get_wrap_symbols)tv->tv_u.tv_message;
|
2018-03-14 14:26:18 +01:00
|
|
|
break;
|
2016-03-04 01:23:29 +01:00
|
|
|
default:
|
|
|
|
break;
|
2009-02-03 08:13:24 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-02-18 09:30:15 +01:00
|
|
|
if (!registeredClaimFile) {
|
2014-07-30 02:38:58 +02:00
|
|
|
message(LDPL_ERROR, "register_claim_file not passed to LLVMgold.");
|
2009-02-18 18:49:06 +01:00
|
|
|
return LDPS_ERR;
|
|
|
|
}
|
2009-02-18 09:30:15 +01:00
|
|
|
if (!add_symbols) {
|
2014-07-30 02:38:58 +02:00
|
|
|
message(LDPL_ERROR, "add_symbols not passed to LLVMgold.");
|
2009-02-18 18:49:06 +01:00
|
|
|
return LDPS_ERR;
|
|
|
|
}
|
2009-02-03 08:13:24 +01:00
|
|
|
|
2014-06-20 00:20:07 +02:00
|
|
|
if (!RegisteredAllSymbolsRead)
|
|
|
|
return LDPS_OK;
|
2014-06-19 23:14:13 +02:00
|
|
|
|
2014-08-21 22:28:55 +02:00
|
|
|
if (!get_input_file) {
|
|
|
|
message(LDPL_ERROR, "get_input_file not passed to LLVMgold.");
|
|
|
|
return LDPS_ERR;
|
2014-06-20 00:54:47 +02:00
|
|
|
}
|
2014-08-21 22:28:55 +02:00
|
|
|
if (!release_input_file) {
|
2016-03-30 14:20:53 +02:00
|
|
|
message(LDPL_ERROR, "release_input_file not passed to LLVMgold.");
|
2014-08-21 22:28:55 +02:00
|
|
|
return LDPS_ERR;
|
2014-06-26 22:43:27 +02:00
|
|
|
}
|
|
|
|
|
2009-02-03 08:13:24 +01:00
|
|
|
return LDPS_OK;
|
|
|
|
}
|
|
|
|
|
2015-11-19 11:43:44 +01:00
|
|
|
static void diagnosticHandler(const DiagnosticInfo &DI) {
|
2015-01-10 01:07:30 +01:00
|
|
|
std::string ErrStorage;
|
|
|
|
{
|
|
|
|
raw_string_ostream OS(ErrStorage);
|
|
|
|
DiagnosticPrinterRawOStream DP(OS);
|
|
|
|
DI.print(DP);
|
|
|
|
}
|
2015-03-02 20:08:03 +01:00
|
|
|
ld_plugin_level Level;
|
|
|
|
switch (DI.getSeverity()) {
|
|
|
|
case DS_Error:
|
2018-11-02 00:34:12 +01:00
|
|
|
Level = LDPL_FATAL;
|
|
|
|
break;
|
2015-03-02 20:08:03 +01:00
|
|
|
case DS_Warning:
|
|
|
|
Level = LDPL_WARNING;
|
|
|
|
break;
|
|
|
|
case DS_Note:
|
2015-03-04 19:51:45 +01:00
|
|
|
case DS_Remark:
|
2015-03-02 20:08:03 +01:00
|
|
|
Level = LDPL_INFO;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
message(Level, "LLVM gold plugin: %s", ErrStorage.c_str());
|
2015-01-10 01:07:30 +01:00
|
|
|
}
|
|
|
|
|
2016-08-11 16:58:12 +02:00
|
|
|
static void check(Error E, std::string Msg = "LLVM gold plugin") {
|
2016-11-11 07:04:30 +01:00
|
|
|
handleAllErrors(std::move(E), [&](ErrorInfoBase &EIB) -> Error {
|
2016-08-11 16:58:12 +02:00
|
|
|
message(LDPL_FATAL, "%s: %s", Msg.c_str(), EIB.message().c_str());
|
|
|
|
return Error::success();
|
|
|
|
});
|
2015-11-19 11:43:44 +01:00
|
|
|
}
|
|
|
|
|
2016-08-11 16:58:12 +02:00
|
|
|
template <typename T> static T check(Expected<T> E) {
|
|
|
|
if (E)
|
|
|
|
return std::move(*E);
|
|
|
|
check(E.takeError());
|
|
|
|
return T();
|
2015-12-10 15:19:35 +01:00
|
|
|
}
|
|
|
|
|
2014-07-06 16:31:22 +02:00
|
|
|
/// Called by gold to see whether this file is one that our plugin can handle.
|
|
|
|
/// We'll try to open it and register all the symbols with add_symbol if
|
|
|
|
/// possible.
|
2010-04-16 02:42:57 +02:00
|
|
|
static ld_plugin_status claim_file_hook(const ld_plugin_input_file *file,
|
|
|
|
int *claimed) {
|
2014-08-27 22:25:55 +02:00
|
|
|
MemoryBufferRef BufferRef;
|
|
|
|
std::unique_ptr<MemoryBuffer> Buffer;
|
2011-04-07 23:11:00 +02:00
|
|
|
if (get_view) {
|
2014-08-21 22:28:55 +02:00
|
|
|
const void *view;
|
2011-04-07 23:11:00 +02:00
|
|
|
if (get_view(file->handle, &view) != LDPS_OK) {
|
2014-07-30 02:38:58 +02:00
|
|
|
message(LDPL_ERROR, "Failed to get a view of %s", file->name);
|
2011-04-07 23:11:00 +02:00
|
|
|
return LDPS_ERR;
|
|
|
|
}
|
2015-08-05 23:16:02 +02:00
|
|
|
BufferRef =
|
|
|
|
MemoryBufferRef(StringRef((const char *)view, file->filesize), "");
|
2011-09-12 23:47:50 +02:00
|
|
|
} else {
|
2011-09-16 01:13:00 +02:00
|
|
|
int64_t offset = 0;
|
2009-02-05 05:14:23 +01:00
|
|
|
// Gold has found what might be IR part-way inside of a file, such as
|
|
|
|
// an .a archive.
|
2011-09-12 23:47:50 +02:00
|
|
|
if (file->offset) {
|
|
|
|
offset = file->offset;
|
|
|
|
}
|
2014-07-06 19:43:13 +02:00
|
|
|
ErrorOr<std::unique_ptr<MemoryBuffer>> BufferOrErr =
|
2019-07-23 09:41:17 +02:00
|
|
|
MemoryBuffer::getOpenFileSlice(sys::fs::convertFDToNativeFile(file->fd),
|
|
|
|
file->name, file->filesize, offset);
|
2014-07-06 19:43:13 +02:00
|
|
|
if (std::error_code EC = BufferOrErr.getError()) {
|
2014-07-30 02:38:58 +02:00
|
|
|
message(LDPL_ERROR, EC.message().c_str());
|
2011-09-12 23:47:50 +02:00
|
|
|
return LDPS_ERR;
|
|
|
|
}
|
2014-08-27 22:25:55 +02:00
|
|
|
Buffer = std::move(BufferOrErr.get());
|
|
|
|
BufferRef = Buffer->getMemBufferRef();
|
2011-02-08 23:40:47 +01:00
|
|
|
}
|
2011-09-12 23:47:50 +02:00
|
|
|
|
2016-08-11 15:03:56 +02:00
|
|
|
*claimed = 1;
|
2016-08-11 14:56:40 +02:00
|
|
|
|
2016-08-11 16:58:12 +02:00
|
|
|
Expected<std::unique_ptr<InputFile>> ObjOrErr = InputFile::create(BufferRef);
|
|
|
|
if (!ObjOrErr) {
|
|
|
|
handleAllErrors(ObjOrErr.takeError(), [&](const ErrorInfoBase &EI) {
|
|
|
|
std::error_code EC = EI.convertToErrorCode();
|
|
|
|
if (EC == object::object_error::invalid_file_type ||
|
|
|
|
EC == object::object_error::bitcode_section_not_found)
|
|
|
|
*claimed = 0;
|
|
|
|
else
|
2017-03-31 06:47:07 +02:00
|
|
|
message(LDPL_FATAL,
|
2016-08-11 16:58:12 +02:00
|
|
|
"LLVM gold plugin has failed to create LTO module: %s",
|
|
|
|
EI.message().c_str());
|
|
|
|
});
|
|
|
|
|
|
|
|
return *claimed ? LDPS_ERR : LDPS_OK;
|
2011-09-09 02:14:04 +02:00
|
|
|
}
|
2016-08-11 16:58:12 +02:00
|
|
|
|
|
|
|
std::unique_ptr<InputFile> Obj = std::move(*ObjOrErr);
|
2009-02-03 08:13:24 +01:00
|
|
|
|
2017-07-10 17:31:53 +02:00
|
|
|
Modules.emplace_back();
|
2009-02-03 08:13:24 +01:00
|
|
|
claimed_file &cf = Modules.back();
|
2010-08-09 23:09:46 +02:00
|
|
|
|
2009-02-03 08:13:24 +01:00
|
|
|
cf.handle = file->handle;
|
[ThinLTO/gold] Handle bitcode archives
Summary:
Several changes were required for ThinLTO links involving bitcode
archive static libraries. With this patch clang/llvm bootstraps with
ThinLTO and gold.
The first is that the gold callbacks get_input_file and
release_input_file can normally be used to get file information for
each constituent bitcode file within an archive. However, these
interfaces lock the underlying file and can't be for each archive
constituent for ThinLTO backends where we get all the input files up
front and don't release any until after the backend threads complete.
However, it is sufficient to only get and release once per file, and
then each consituent bitcode file can be accessed via get_view. This
required saving some information to identify which file handle is the
"leader" for each claimed file sharing the same file descriptor, and
other information so that get_input_file isn't necessary later when
processing the backends.
Second, the module paths in the index need to distinguish between
different constituent bitcode files within the same archive file,
otherwise they will all end up with the same archive file path.
Do this by appending the offset within the archive for the start of the
bitcode file, returned by get_input_file when we claim each bitcode file,
and saving that along with the file handle.
Third, rather than have the function importer try to load a file based
on the module path identifier (which now contains a suffix to
distinguish different bitcode files within an archive), use a custom
module loader. This is the same approach taken in libLTO, and I am using
the support refactored into the new LTO.h header in r270509. The module
loader parses the bitcode files out of the memory buffers returned from
gold via the get_view callback and saved in a map. This also means that
we call the function importer directly, rather than add it to the pass
pipeline (which was in the plan to do already for other reasons).
Reviewers: pcc, joker.eph
Subscribers: llvm-commits, joker.eph
Differential Revision: http://reviews.llvm.org/D20559
llvm-svn: 270814
2016-05-26 03:46:41 +02:00
|
|
|
// Keep track of the first handle for each file descriptor, since there are
|
|
|
|
// multiple in the case of an archive. This is used later in the case of
|
|
|
|
// ThinLTO parallel backends to ensure that each file is only opened and
|
|
|
|
// released once.
|
|
|
|
auto LeaderHandle =
|
|
|
|
FDToLeaderHandle.insert(std::make_pair(file->fd, file->handle)).first;
|
|
|
|
cf.leader_handle = LeaderHandle->second;
|
|
|
|
// Save the filesize since for parallel ThinLTO backends we can only
|
|
|
|
// invoke get_input_file once per archive (only for the leader handle).
|
|
|
|
cf.filesize = file->filesize;
|
|
|
|
// In the case of an archive library, all but the first member must have a
|
|
|
|
// non-zero offset, which we can append to the file name to obtain a
|
|
|
|
// unique name.
|
|
|
|
cf.name = file->name;
|
|
|
|
if (file->offset)
|
|
|
|
cf.name += ".llvm." + std::to_string(file->offset) + "." +
|
2016-08-11 16:58:12 +02:00
|
|
|
sys::path::filename(Obj->getSourceFileName()).str();
|
2009-02-03 08:13:24 +01:00
|
|
|
|
2014-08-21 22:28:55 +02:00
|
|
|
for (auto &Sym : Obj->symbols()) {
|
2009-02-03 08:13:24 +01:00
|
|
|
cf.syms.push_back(ld_plugin_symbol());
|
|
|
|
ld_plugin_symbol &sym = cf.syms.back();
|
2014-07-29 23:46:05 +02:00
|
|
|
sym.version = nullptr;
|
2016-08-11 16:58:12 +02:00
|
|
|
StringRef Name = Sym.getName();
|
|
|
|
sym.name = strdup(Name.str().c_str());
|
2009-02-03 08:13:24 +01:00
|
|
|
|
2016-08-11 16:58:12 +02:00
|
|
|
ResolutionInfo &Res = ResInfo[Name];
|
2014-08-21 22:28:55 +02:00
|
|
|
|
2016-08-11 16:58:12 +02:00
|
|
|
Res.CanOmitFromDynSym &= Sym.canBeOmittedFromSymbolTable();
|
2015-12-10 15:19:35 +01:00
|
|
|
|
2014-08-21 22:28:55 +02:00
|
|
|
sym.visibility = LDPV_DEFAULT;
|
2016-08-11 16:58:12 +02:00
|
|
|
GlobalValue::VisibilityTypes Vis = Sym.getVisibility();
|
|
|
|
if (Vis != GlobalValue::DefaultVisibility)
|
|
|
|
Res.DefaultVisibility = false;
|
|
|
|
switch (Vis) {
|
|
|
|
case GlobalValue::DefaultVisibility:
|
|
|
|
break;
|
|
|
|
case GlobalValue::HiddenVisibility:
|
|
|
|
sym.visibility = LDPV_HIDDEN;
|
|
|
|
break;
|
|
|
|
case GlobalValue::ProtectedVisibility:
|
|
|
|
sym.visibility = LDPV_PROTECTED;
|
|
|
|
break;
|
2009-02-03 08:13:24 +01:00
|
|
|
}
|
|
|
|
|
2017-03-29 00:31:35 +02:00
|
|
|
if (Sym.isUndefined()) {
|
2014-08-21 22:28:55 +02:00
|
|
|
sym.def = LDPK_UNDEF;
|
2017-03-29 00:31:35 +02:00
|
|
|
if (Sym.isWeak())
|
2009-04-24 18:55:21 +02:00
|
|
|
sym.def = LDPK_WEAKUNDEF;
|
2017-03-29 00:31:35 +02:00
|
|
|
} else if (Sym.isCommon())
|
2016-08-11 16:58:12 +02:00
|
|
|
sym.def = LDPK_COMMON;
|
2017-03-29 00:31:35 +02:00
|
|
|
else if (Sym.isWeak())
|
2016-08-11 16:58:12 +02:00
|
|
|
sym.def = LDPK_WEAKDEF;
|
|
|
|
else
|
2014-08-21 22:28:55 +02:00
|
|
|
sym.def = LDPK_DEF;
|
2009-02-03 08:13:24 +01:00
|
|
|
|
|
|
|
sym.size = 0;
|
2014-08-21 22:28:55 +02:00
|
|
|
sym.comdat_key = nullptr;
|
2017-03-31 06:47:07 +02:00
|
|
|
int CI = Sym.getComdatIndex();
|
2016-10-25 14:02:03 +02:00
|
|
|
if (CI != -1) {
|
|
|
|
StringRef C = Obj->getComdatTable()[CI];
|
2016-10-17 20:51:02 +02:00
|
|
|
sym.comdat_key = strdup(C.str().c_str());
|
2016-10-25 14:02:03 +02:00
|
|
|
}
|
2009-02-03 08:13:24 +01:00
|
|
|
|
|
|
|
sym.resolution = LDPR_UNKNOWN;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!cf.syms.empty()) {
|
2015-08-05 23:16:02 +02:00
|
|
|
if (add_symbols(cf.handle, cf.syms.size(), cf.syms.data()) != LDPS_OK) {
|
2014-07-30 02:38:58 +02:00
|
|
|
message(LDPL_ERROR, "Unable to add symbols!");
|
2009-02-03 08:13:24 +01:00
|
|
|
return LDPS_ERR;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-14 14:26:18 +01:00
|
|
|
// Handle any --wrap options passed to gold, which are than passed
|
|
|
|
// along to the plugin.
|
|
|
|
if (get_wrap_symbols) {
|
|
|
|
const char **wrap_symbols;
|
|
|
|
uint64_t count = 0;
|
|
|
|
if (get_wrap_symbols(&count, &wrap_symbols) != LDPS_OK) {
|
|
|
|
message(LDPL_ERROR, "Unable to get wrap symbols!");
|
|
|
|
return LDPS_ERR;
|
|
|
|
}
|
|
|
|
for (uint64_t i = 0; i < count; i++) {
|
|
|
|
StringRef Name = wrap_symbols[i];
|
|
|
|
ResolutionInfo &Res = ResInfo[Name];
|
|
|
|
ResolutionInfo &WrapRes = ResInfo["__wrap_" + Name.str()];
|
|
|
|
ResolutionInfo &RealRes = ResInfo["__real_" + Name.str()];
|
|
|
|
// Tell LTO not to inline symbols that will be overwritten.
|
|
|
|
Res.CanInline = false;
|
|
|
|
RealRes.CanInline = false;
|
|
|
|
// Tell LTO not to eliminate symbols that will be used after renaming.
|
|
|
|
Res.IsUsedInRegularObj = true;
|
|
|
|
WrapRes.IsUsedInRegularObj = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-21 22:28:55 +02:00
|
|
|
return LDPS_OK;
|
|
|
|
}
|
|
|
|
|
2014-12-23 19:18:37 +01:00
|
|
|
static void freeSymName(ld_plugin_symbol &Sym) {
|
|
|
|
free(Sym.name);
|
|
|
|
free(Sym.comdat_key);
|
|
|
|
Sym.name = nullptr;
|
|
|
|
Sym.comdat_key = nullptr;
|
|
|
|
}
|
|
|
|
|
2016-03-04 17:36:06 +01:00
|
|
|
/// Helper to get a file's symbols and a view into it via gold callbacks.
|
|
|
|
static const void *getSymbolsAndView(claimed_file &F) {
|
2016-03-08 15:02:46 +01:00
|
|
|
ld_plugin_status status = get_symbols(F.handle, F.syms.size(), F.syms.data());
|
2016-03-04 01:23:29 +01:00
|
|
|
if (status == LDPS_NO_SYMS)
|
|
|
|
return nullptr;
|
2015-10-04 16:33:43 +02:00
|
|
|
|
2016-03-04 01:23:29 +01:00
|
|
|
if (status != LDPS_OK)
|
2015-10-04 16:33:43 +02:00
|
|
|
message(LDPL_FATAL, "Failed to get symbol information");
|
|
|
|
|
|
|
|
const void *View;
|
|
|
|
if (get_view(F.handle, &View) != LDPS_OK)
|
|
|
|
message(LDPL_FATAL, "Failed to get a view of file");
|
|
|
|
|
2016-03-04 17:36:06 +01:00
|
|
|
return View;
|
|
|
|
}
|
|
|
|
|
[ThinLTO] Add support for emitting minimized bitcode for thin link
Summary:
The cumulative size of the bitcode files for a very large application
can be huge, particularly with -g. In a distributed build environment,
all of these files must be sent to the remote build node that performs
the thin link step, and this can exceed size limits.
The thin link actually only needs the summary along with a bitcode
symbol table. Until we have a proper bitcode symbol table, simply
stripping the debug metadata results in significant size reduction.
Add support for an option to additionally emit minimized bitcode
modules, just for use in the thin link step, which for now just strips
all debug metadata. I plan to add a cc1 option so this can be invoked
easily during the compile step.
However, care must be taken to ensure that these minimized thin link
bitcode files produce the same index as with the original bitcode files,
as these original bitcode files will be used in the backends.
Specifically:
1) The module hash used for caching is typically produced by hashing the
written bitcode, and we want to include the hash that would correspond
to the original bitcode file. This is because we want to ensure that
changes in the stripped portions affect caching. Added plumbing to emit
the same module hash in the minimized thin link bitcode file.
2) The module paths in the index are constructed from the module ID of
each thin linked bitcode, and typically is automatically generated from
the input file path. This is the path used for finding the modules to
import from, and obviously we need this to point to the original bitcode
files. Added gold-plugin support to take a suffix replacement during the
thin link that is used to override the identifier on the MemoryBufferRef
constructed from the loaded thin link bitcode file. The assumption is
that the build system can specify that the minimized bitcode file has a
name that is similar but uses a different suffix (e.g. out.thinlink.bc
instead of out.o).
Added various tests to ensure that we get identical index files out of
the thin link step.
Reviewers: mehdi_amini, pcc
Subscribers: Prazek, llvm-commits
Differential Revision: https://reviews.llvm.org/D31027
llvm-svn: 298638
2017-03-23 20:47:39 +01:00
|
|
|
/// Parse the thinlto-object-suffix-replace option into the \p OldSuffix and
|
|
|
|
/// \p NewSuffix strings, if it was specified.
|
|
|
|
static void getThinLTOOldAndNewSuffix(std::string &OldSuffix,
|
|
|
|
std::string &NewSuffix) {
|
|
|
|
assert(options::thinlto_object_suffix_replace.empty() ||
|
|
|
|
options::thinlto_object_suffix_replace.find(";") != StringRef::npos);
|
|
|
|
StringRef SuffixReplace = options::thinlto_object_suffix_replace;
|
2020-01-29 01:49:54 +01:00
|
|
|
auto Split = SuffixReplace.split(';');
|
2020-01-29 02:12:23 +01:00
|
|
|
OldSuffix = std::string(Split.first);
|
|
|
|
NewSuffix = std::string(Split.second);
|
[ThinLTO] Add support for emitting minimized bitcode for thin link
Summary:
The cumulative size of the bitcode files for a very large application
can be huge, particularly with -g. In a distributed build environment,
all of these files must be sent to the remote build node that performs
the thin link step, and this can exceed size limits.
The thin link actually only needs the summary along with a bitcode
symbol table. Until we have a proper bitcode symbol table, simply
stripping the debug metadata results in significant size reduction.
Add support for an option to additionally emit minimized bitcode
modules, just for use in the thin link step, which for now just strips
all debug metadata. I plan to add a cc1 option so this can be invoked
easily during the compile step.
However, care must be taken to ensure that these minimized thin link
bitcode files produce the same index as with the original bitcode files,
as these original bitcode files will be used in the backends.
Specifically:
1) The module hash used for caching is typically produced by hashing the
written bitcode, and we want to include the hash that would correspond
to the original bitcode file. This is because we want to ensure that
changes in the stripped portions affect caching. Added plumbing to emit
the same module hash in the minimized thin link bitcode file.
2) The module paths in the index are constructed from the module ID of
each thin linked bitcode, and typically is automatically generated from
the input file path. This is the path used for finding the modules to
import from, and obviously we need this to point to the original bitcode
files. Added gold-plugin support to take a suffix replacement during the
thin link that is used to override the identifier on the MemoryBufferRef
constructed from the loaded thin link bitcode file. The assumption is
that the build system can specify that the minimized bitcode file has a
name that is similar but uses a different suffix (e.g. out.thinlink.bc
instead of out.o).
Added various tests to ensure that we get identical index files out of
the thin link step.
Reviewers: mehdi_amini, pcc
Subscribers: Prazek, llvm-commits
Differential Revision: https://reviews.llvm.org/D31027
llvm-svn: 298638
2017-03-23 20:47:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Given the original \p Path to an output file, replace any filename
|
|
|
|
/// suffix matching \p OldSuffix with \p NewSuffix.
|
2017-08-10 21:28:00 +02:00
|
|
|
static std::string getThinLTOObjectFileName(StringRef Path, StringRef OldSuffix,
|
|
|
|
StringRef NewSuffix) {
|
2018-08-22 04:11:36 +02:00
|
|
|
if (Path.consume_back(OldSuffix))
|
|
|
|
return (Path + NewSuffix).str();
|
2020-01-28 20:23:46 +01:00
|
|
|
return std::string(Path);
|
[ThinLTO] Add support for emitting minimized bitcode for thin link
Summary:
The cumulative size of the bitcode files for a very large application
can be huge, particularly with -g. In a distributed build environment,
all of these files must be sent to the remote build node that performs
the thin link step, and this can exceed size limits.
The thin link actually only needs the summary along with a bitcode
symbol table. Until we have a proper bitcode symbol table, simply
stripping the debug metadata results in significant size reduction.
Add support for an option to additionally emit minimized bitcode
modules, just for use in the thin link step, which for now just strips
all debug metadata. I plan to add a cc1 option so this can be invoked
easily during the compile step.
However, care must be taken to ensure that these minimized thin link
bitcode files produce the same index as with the original bitcode files,
as these original bitcode files will be used in the backends.
Specifically:
1) The module hash used for caching is typically produced by hashing the
written bitcode, and we want to include the hash that would correspond
to the original bitcode file. This is because we want to ensure that
changes in the stripped portions affect caching. Added plumbing to emit
the same module hash in the minimized thin link bitcode file.
2) The module paths in the index are constructed from the module ID of
each thin linked bitcode, and typically is automatically generated from
the input file path. This is the path used for finding the modules to
import from, and obviously we need this to point to the original bitcode
files. Added gold-plugin support to take a suffix replacement during the
thin link that is used to override the identifier on the MemoryBufferRef
constructed from the loaded thin link bitcode file. The assumption is
that the build system can specify that the minimized bitcode file has a
name that is similar but uses a different suffix (e.g. out.thinlink.bc
instead of out.o).
Added various tests to ensure that we get identical index files out of
the thin link step.
Reviewers: mehdi_amini, pcc
Subscribers: Prazek, llvm-commits
Differential Revision: https://reviews.llvm.org/D31027
llvm-svn: 298638
2017-03-23 20:47:39 +01:00
|
|
|
}
|
|
|
|
|
2017-07-25 21:42:32 +02:00
|
|
|
// Returns true if S is valid as a C language identifier.
|
|
|
|
static bool isValidCIdentifier(StringRef S) {
|
2017-10-04 13:00:30 +02:00
|
|
|
return !S.empty() && (isAlpha(S[0]) || S[0] == '_') &&
|
|
|
|
std::all_of(S.begin() + 1, S.end(),
|
|
|
|
[](char C) { return C == '_' || isAlnum(C); });
|
2017-07-25 21:42:32 +02:00
|
|
|
}
|
|
|
|
|
2017-12-15 10:18:21 +01:00
|
|
|
static bool isUndefined(ld_plugin_symbol &Sym) {
|
|
|
|
return Sym.def == LDPK_UNDEF || Sym.def == LDPK_WEAKUNDEF;
|
|
|
|
}
|
|
|
|
|
[ThinLTO] Add support for emitting minimized bitcode for thin link
Summary:
The cumulative size of the bitcode files for a very large application
can be huge, particularly with -g. In a distributed build environment,
all of these files must be sent to the remote build node that performs
the thin link step, and this can exceed size limits.
The thin link actually only needs the summary along with a bitcode
symbol table. Until we have a proper bitcode symbol table, simply
stripping the debug metadata results in significant size reduction.
Add support for an option to additionally emit minimized bitcode
modules, just for use in the thin link step, which for now just strips
all debug metadata. I plan to add a cc1 option so this can be invoked
easily during the compile step.
However, care must be taken to ensure that these minimized thin link
bitcode files produce the same index as with the original bitcode files,
as these original bitcode files will be used in the backends.
Specifically:
1) The module hash used for caching is typically produced by hashing the
written bitcode, and we want to include the hash that would correspond
to the original bitcode file. This is because we want to ensure that
changes in the stripped portions affect caching. Added plumbing to emit
the same module hash in the minimized thin link bitcode file.
2) The module paths in the index are constructed from the module ID of
each thin linked bitcode, and typically is automatically generated from
the input file path. This is the path used for finding the modules to
import from, and obviously we need this to point to the original bitcode
files. Added gold-plugin support to take a suffix replacement during the
thin link that is used to override the identifier on the MemoryBufferRef
constructed from the loaded thin link bitcode file. The assumption is
that the build system can specify that the minimized bitcode file has a
name that is similar but uses a different suffix (e.g. out.thinlink.bc
instead of out.o).
Added various tests to ensure that we get identical index files out of
the thin link step.
Reviewers: mehdi_amini, pcc
Subscribers: Prazek, llvm-commits
Differential Revision: https://reviews.llvm.org/D31027
llvm-svn: 298638
2017-03-23 20:47:39 +01:00
|
|
|
static void addModule(LTO &Lto, claimed_file &F, const void *View,
|
|
|
|
StringRef Filename) {
|
|
|
|
MemoryBufferRef BufferRef(StringRef((const char *)View, F.filesize),
|
|
|
|
Filename);
|
2016-08-11 16:58:12 +02:00
|
|
|
Expected<std::unique_ptr<InputFile>> ObjOrErr = InputFile::create(BufferRef);
|
2015-11-21 22:55:48 +01:00
|
|
|
|
2016-08-11 16:58:12 +02:00
|
|
|
if (!ObjOrErr)
|
2014-09-18 23:28:49 +02:00
|
|
|
message(LDPL_FATAL, "Could not read bitcode from file : %s",
|
2016-08-11 16:58:12 +02:00
|
|
|
toString(ObjOrErr.takeError()).c_str());
|
2014-08-21 22:28:55 +02:00
|
|
|
|
2014-12-09 17:13:59 +01:00
|
|
|
unsigned SymNum = 0;
|
2017-07-25 21:42:32 +02:00
|
|
|
std::unique_ptr<InputFile> Input = std::move(ObjOrErr.get());
|
|
|
|
auto InputFileSyms = Input->symbols();
|
|
|
|
assert(InputFileSyms.size() == F.syms.size());
|
2016-08-11 16:58:12 +02:00
|
|
|
std::vector<SymbolResolution> Resols(F.syms.size());
|
2016-09-14 04:55:16 +02:00
|
|
|
for (ld_plugin_symbol &Sym : F.syms) {
|
2017-07-25 21:42:32 +02:00
|
|
|
const InputFile::Symbol &InpSym = InputFileSyms[SymNum];
|
2016-09-14 04:55:16 +02:00
|
|
|
SymbolResolution &R = Resols[SymNum++];
|
2014-12-09 17:13:59 +01:00
|
|
|
|
2014-08-21 22:28:55 +02:00
|
|
|
ld_plugin_symbol_resolution Resolution =
|
|
|
|
(ld_plugin_symbol_resolution)Sym.resolution;
|
|
|
|
|
2015-12-10 15:19:35 +01:00
|
|
|
ResolutionInfo &Res = ResInfo[Sym.name];
|
2014-09-09 22:08:22 +02:00
|
|
|
|
2014-08-21 22:28:55 +02:00
|
|
|
switch (Resolution) {
|
|
|
|
case LDPR_UNKNOWN:
|
|
|
|
llvm_unreachable("Unexpected resolution");
|
|
|
|
|
|
|
|
case LDPR_RESOLVED_IR:
|
|
|
|
case LDPR_RESOLVED_EXEC:
|
|
|
|
case LDPR_RESOLVED_DYN:
|
2015-12-10 15:19:35 +01:00
|
|
|
case LDPR_PREEMPTED_IR:
|
|
|
|
case LDPR_PREEMPTED_REG:
|
2015-01-14 14:53:50 +01:00
|
|
|
case LDPR_UNDEF:
|
|
|
|
break;
|
|
|
|
|
2016-08-11 16:58:12 +02:00
|
|
|
case LDPR_PREVAILING_DEF_IRONLY:
|
2017-12-15 10:18:21 +01:00
|
|
|
R.Prevailing = !isUndefined(Sym);
|
2014-08-21 22:28:55 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case LDPR_PREVAILING_DEF:
|
2017-12-15 10:18:21 +01:00
|
|
|
R.Prevailing = !isUndefined(Sym);
|
2016-08-11 16:58:12 +02:00
|
|
|
R.VisibleToRegularObj = true;
|
2014-08-21 22:28:55 +02:00
|
|
|
break;
|
|
|
|
|
2016-08-11 16:58:12 +02:00
|
|
|
case LDPR_PREVAILING_DEF_IRONLY_EXP:
|
2017-12-15 10:18:21 +01:00
|
|
|
R.Prevailing = !isUndefined(Sym);
|
2016-08-11 16:58:12 +02:00
|
|
|
if (!Res.CanOmitFromDynSym)
|
|
|
|
R.VisibleToRegularObj = true;
|
2014-08-21 22:28:55 +02:00
|
|
|
break;
|
|
|
|
}
|
2016-08-11 16:58:12 +02:00
|
|
|
|
2017-07-25 21:42:32 +02:00
|
|
|
// If the symbol has a C identifier section name, we need to mark
|
|
|
|
// it as visible to a regular object so that LTO will keep it around
|
|
|
|
// to ensure the linker generates special __start_<secname> and
|
|
|
|
// __stop_<secname> symbols which may be used elsewhere.
|
|
|
|
if (isValidCIdentifier(InpSym.getSectionName()))
|
|
|
|
R.VisibleToRegularObj = true;
|
|
|
|
|
2016-08-11 16:58:12 +02:00
|
|
|
if (Resolution != LDPR_RESOLVED_DYN && Resolution != LDPR_UNDEF &&
|
|
|
|
(IsExecutable || !Res.DefaultVisibility))
|
|
|
|
R.FinalDefinitionInLinkageUnit = true;
|
|
|
|
|
2018-03-14 14:26:18 +01:00
|
|
|
if (!Res.CanInline)
|
|
|
|
R.LinkerRedefined = true;
|
|
|
|
|
|
|
|
if (Res.IsUsedInRegularObj)
|
|
|
|
R.VisibleToRegularObj = true;
|
|
|
|
|
2014-12-23 19:18:37 +01:00
|
|
|
freeSymName(Sym);
|
2013-10-18 21:32:06 +02:00
|
|
|
}
|
2011-02-20 19:28:29 +01:00
|
|
|
|
2017-07-25 21:42:32 +02:00
|
|
|
check(Lto.add(std::move(Input), Resols),
|
2016-08-11 16:58:12 +02:00
|
|
|
std::string("Failed to link module ") + F.name);
|
2014-10-30 00:54:45 +01:00
|
|
|
}
|
|
|
|
|
2017-08-10 21:28:00 +02:00
|
|
|
static void recordFile(const std::string &Filename, bool TempOutFile) {
|
2016-03-04 17:36:06 +01:00
|
|
|
if (add_input_file(Filename.c_str()) != LDPS_OK)
|
|
|
|
message(LDPL_FATAL,
|
|
|
|
"Unable to add .o file to the link. File left behind in: %s",
|
|
|
|
Filename.c_str());
|
|
|
|
if (TempOutFile)
|
2017-08-10 21:28:00 +02:00
|
|
|
Cleanup.push_back(Filename);
|
2016-03-04 17:36:06 +01:00
|
|
|
}
|
2014-08-21 22:28:55 +02:00
|
|
|
|
2016-08-17 08:23:09 +02:00
|
|
|
/// Return the desired output filename given a base input name, a flag
|
|
|
|
/// indicating whether a temp file should be generated, and an optional task id.
|
|
|
|
/// The new filename generated is returned in \p NewFilename.
|
2017-08-10 21:28:00 +02:00
|
|
|
static int getOutputFileName(StringRef InFilename, bool TempOutFile,
|
2017-08-10 19:38:41 +02:00
|
|
|
SmallString<128> &NewFilename, int TaskID) {
|
|
|
|
int FD = -1;
|
2016-03-04 17:36:06 +01:00
|
|
|
if (TempOutFile) {
|
|
|
|
std::error_code EC =
|
2017-08-10 19:38:41 +02:00
|
|
|
sys::fs::createTemporaryFile("lto-llvm", "o", FD, NewFilename);
|
2016-03-04 17:36:06 +01:00
|
|
|
if (EC)
|
|
|
|
message(LDPL_FATAL, "Could not create temporary file: %s",
|
|
|
|
EC.message().c_str());
|
|
|
|
} else {
|
|
|
|
NewFilename = InFilename;
|
2017-01-26 03:07:05 +01:00
|
|
|
if (TaskID > 0)
|
2016-03-04 17:36:06 +01:00
|
|
|
NewFilename += utostr(TaskID);
|
2017-08-10 19:38:41 +02:00
|
|
|
std::error_code EC =
|
2018-06-07 22:37:22 +02:00
|
|
|
sys::fs::openFileForWrite(NewFilename, FD, sys::fs::CD_CreateAlways);
|
2017-08-10 19:38:41 +02:00
|
|
|
if (EC)
|
|
|
|
message(LDPL_FATAL, "Could not open file %s: %s", NewFilename.c_str(),
|
|
|
|
EC.message().c_str());
|
2016-03-04 17:36:06 +01:00
|
|
|
}
|
2017-08-10 19:38:41 +02:00
|
|
|
return FD;
|
2016-03-04 17:36:06 +01:00
|
|
|
}
|
|
|
|
|
2018-04-06 23:14:33 +02:00
|
|
|
static CodeGenOpt::Level getCGOptLevel() {
|
|
|
|
switch (options::OptLevel) {
|
|
|
|
case 0:
|
|
|
|
return CodeGenOpt::None;
|
|
|
|
case 1:
|
|
|
|
return CodeGenOpt::Less;
|
|
|
|
case 2:
|
|
|
|
return CodeGenOpt::Default;
|
|
|
|
case 3:
|
|
|
|
return CodeGenOpt::Aggressive;
|
|
|
|
}
|
|
|
|
llvm_unreachable("Invalid optimization level");
|
|
|
|
}
|
|
|
|
|
2016-05-17 16:45:30 +02:00
|
|
|
/// Parse the thinlto_prefix_replace option into the \p OldPrefix and
|
|
|
|
/// \p NewPrefix strings, if it was specified.
|
|
|
|
static void getThinLTOOldAndNewPrefix(std::string &OldPrefix,
|
|
|
|
std::string &NewPrefix) {
|
|
|
|
StringRef PrefixReplace = options::thinlto_prefix_replace;
|
2016-05-17 20:43:22 +02:00
|
|
|
assert(PrefixReplace.empty() || PrefixReplace.find(";") != StringRef::npos);
|
2020-01-29 02:12:23 +01:00
|
|
|
auto Split = PrefixReplace.split(';');
|
|
|
|
OldPrefix = std::string(Split.first);
|
|
|
|
NewPrefix = std::string(Split.second);
|
2016-05-17 16:45:30 +02:00
|
|
|
}
|
|
|
|
|
2018-02-22 20:06:15 +01:00
|
|
|
/// Creates instance of LTO.
|
|
|
|
/// OnIndexWrite is callback to let caller know when LTO writes index files.
|
|
|
|
/// LinkedObjectsFile is an output stream to write the list of object files for
|
|
|
|
/// the final ThinLTO linking. Can be nullptr.
|
|
|
|
static std::unique_ptr<LTO> createLTO(IndexWriteCallback OnIndexWrite,
|
|
|
|
raw_fd_ostream *LinkedObjectsFile) {
|
2016-08-11 16:58:12 +02:00
|
|
|
Config Conf;
|
|
|
|
ThinBackend Backend;
|
2016-05-17 16:45:30 +02:00
|
|
|
|
2016-08-11 16:58:12 +02:00
|
|
|
Conf.CPU = options::mcpu;
|
2020-03-04 00:47:43 +01:00
|
|
|
Conf.Options = codegen::InitTargetOptionsFromCodeGenFlags();
|
[ThinLTO/gold] Handle bitcode archives
Summary:
Several changes were required for ThinLTO links involving bitcode
archive static libraries. With this patch clang/llvm bootstraps with
ThinLTO and gold.
The first is that the gold callbacks get_input_file and
release_input_file can normally be used to get file information for
each constituent bitcode file within an archive. However, these
interfaces lock the underlying file and can't be for each archive
constituent for ThinLTO backends where we get all the input files up
front and don't release any until after the backend threads complete.
However, it is sufficient to only get and release once per file, and
then each consituent bitcode file can be accessed via get_view. This
required saving some information to identify which file handle is the
"leader" for each claimed file sharing the same file descriptor, and
other information so that get_input_file isn't necessary later when
processing the backends.
Second, the module paths in the index need to distinguish between
different constituent bitcode files within the same archive file,
otherwise they will all end up with the same archive file path.
Do this by appending the offset within the archive for the start of the
bitcode file, returned by get_input_file when we claim each bitcode file,
and saving that along with the file handle.
Third, rather than have the function importer try to load a file based
on the module path identifier (which now contains a suffix to
distinguish different bitcode files within an archive), use a custom
module loader. This is the same approach taken in libLTO, and I am using
the support refactored into the new LTO.h header in r270509. The module
loader parses the bitcode files out of the memory buffers returned from
gold via the get_view callback and saved in a map. This also means that
we call the function importer directly, rather than add it to the pass
pipeline (which was in the plan to do already for other reasons).
Reviewers: pcc, joker.eph
Subscribers: llvm-commits, joker.eph
Differential Revision: http://reviews.llvm.org/D20559
llvm-svn: 270814
2016-05-26 03:46:41 +02:00
|
|
|
|
2016-08-11 16:58:12 +02:00
|
|
|
// Disable the new X86 relax relocations since gold might not support them.
|
|
|
|
// FIXME: Check the gold version or add a new option to enable them.
|
|
|
|
Conf.Options.RelaxELFRelocations = false;
|
|
|
|
|
2018-07-12 22:35:58 +02:00
|
|
|
// Toggle function/data sections.
|
2020-03-04 00:47:43 +01:00
|
|
|
if (!codegen::getExplicitFunctionSections())
|
2018-12-05 18:46:24 +01:00
|
|
|
Conf.Options.FunctionSections = SplitSections;
|
2020-03-04 00:47:43 +01:00
|
|
|
if (!codegen::getExplicitDataSections())
|
2018-12-05 18:46:24 +01:00
|
|
|
Conf.Options.DataSections = SplitSections;
|
2017-07-26 01:32:50 +02:00
|
|
|
|
2020-03-04 00:47:43 +01:00
|
|
|
Conf.MAttrs = codegen::getMAttrs();
|
2020-03-18 11:38:44 +01:00
|
|
|
Conf.RelocModel = RelocationModel;
|
2020-03-04 00:47:43 +01:00
|
|
|
Conf.CodeModel = codegen::getExplicitCodeModel();
|
2018-04-06 23:14:33 +02:00
|
|
|
Conf.CGOptLevel = getCGOptLevel();
|
2016-08-11 16:58:12 +02:00
|
|
|
Conf.DisableVerify = options::DisableVerify;
|
|
|
|
Conf.OptLevel = options::OptLevel;
|
2020-01-10 05:58:31 +01:00
|
|
|
Conf.PTO.LoopVectorization = options::OptLevel > 1;
|
|
|
|
Conf.PTO.SLPVectorization = options::OptLevel > 1;
|
[LTO] Suppress emission of empty combined module by default
Summary:
That unless the user requested an output object (--lto-obj-path), the an
unused empty combined module is not emitted.
This changed is helpful for some target (ex. RISCV-V) which encoded the
ABI info in IR module flags (target-abi). Empty unused module has no ABI
info so the linker would get the linking error during merging
incompatible ABIs.
Reviewers: tejohnson, espindola, MaskRay
Subscribers: emaste, inglorion, arichardson, hiraditya, simoncook, MaskRay, steven_wu, dexonsmith, PkmX, dang, lenary, s.egerton, luismarques, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D78988
2020-04-28 05:04:36 +02:00
|
|
|
Conf.AlwaysEmitRegularLTOObj = !options::obj_path.empty();
|
2020-01-10 05:58:31 +01:00
|
|
|
|
2016-08-11 15:03:56 +02:00
|
|
|
if (options::thinlto_index_only) {
|
|
|
|
std::string OldPrefix, NewPrefix;
|
|
|
|
getThinLTOOldAndNewPrefix(OldPrefix, NewPrefix);
|
2018-02-22 20:06:15 +01:00
|
|
|
Backend = createWriteIndexesThinBackend(OldPrefix, NewPrefix,
|
|
|
|
options::thinlto_emit_imports_files,
|
|
|
|
LinkedObjectsFile, OnIndexWrite);
|
2020-03-27 15:20:39 +01:00
|
|
|
} else {
|
|
|
|
Backend = createInProcessThinBackend(
|
|
|
|
llvm::heavyweight_hardware_concurrency(options::Parallelism));
|
2016-08-11 16:58:12 +02:00
|
|
|
}
|
2016-08-11 15:03:56 +02:00
|
|
|
|
2016-08-11 16:58:12 +02:00
|
|
|
Conf.OverrideTriple = options::triple;
|
|
|
|
Conf.DefaultTriple = sys::getDefaultTargetTriple();
|
2016-08-11 14:56:40 +02:00
|
|
|
|
2016-08-11 16:58:12 +02:00
|
|
|
Conf.DiagHandler = diagnosticHandler;
|
2016-08-11 15:03:56 +02:00
|
|
|
|
2016-08-11 16:58:12 +02:00
|
|
|
switch (options::TheOutputType) {
|
|
|
|
case options::OT_NORMAL:
|
|
|
|
break;
|
2016-08-11 15:03:56 +02:00
|
|
|
|
2016-08-11 16:58:12 +02:00
|
|
|
case options::OT_DISABLE:
|
2016-08-22 18:41:58 +02:00
|
|
|
Conf.PreOptModuleHook = [](size_t Task, const Module &M) { return false; };
|
2016-08-11 16:58:12 +02:00
|
|
|
break;
|
2016-08-11 15:03:56 +02:00
|
|
|
|
2016-08-11 16:58:12 +02:00
|
|
|
case options::OT_BC_ONLY:
|
2016-08-22 18:41:58 +02:00
|
|
|
Conf.PostInternalizeModuleHook = [](size_t Task, const Module &M) {
|
2016-08-11 16:58:12 +02:00
|
|
|
std::error_code EC;
|
2019-08-05 07:43:48 +02:00
|
|
|
raw_fd_ostream OS(output_name, EC, sys::fs::OpenFlags::OF_None);
|
2016-08-11 16:58:12 +02:00
|
|
|
if (EC)
|
|
|
|
message(LDPL_FATAL, "Failed to write the output file.");
|
2018-02-14 20:11:32 +01:00
|
|
|
WriteBitcodeToFile(M, OS, /* ShouldPreserveUseListOrder */ false);
|
2016-08-11 16:58:12 +02:00
|
|
|
return false;
|
|
|
|
};
|
|
|
|
break;
|
|
|
|
|
|
|
|
case options::OT_SAVE_TEMPS:
|
2016-08-18 02:12:33 +02:00
|
|
|
check(Conf.addSaveTemps(output_name + ".",
|
|
|
|
/* UseInputModulePath */ true));
|
2016-08-11 16:58:12 +02:00
|
|
|
break;
|
2019-01-03 00:48:00 +01:00
|
|
|
case options::OT_ASM_ONLY:
|
2019-11-14 00:17:46 +01:00
|
|
|
Conf.CGFileType = CGFT_AssemblyFile;
|
2019-01-03 00:48:00 +01:00
|
|
|
break;
|
2016-05-10 15:48:23 +02:00
|
|
|
}
|
2015-10-04 16:33:43 +02:00
|
|
|
|
2016-12-16 17:48:46 +01:00
|
|
|
if (!options::sample_profile.empty())
|
|
|
|
Conf.SampleProfile = options::sample_profile;
|
|
|
|
|
2019-02-27 18:24:33 +01:00
|
|
|
if (!options::cs_profile_path.empty())
|
|
|
|
Conf.CSIRProfile = options::cs_profile_path;
|
|
|
|
Conf.RunCSIRInstr = options::cs_pgo_gen;
|
|
|
|
|
2018-04-13 07:03:28 +02:00
|
|
|
Conf.DwoDir = options::dwo_dir;
|
|
|
|
|
2018-04-18 15:25:23 +02:00
|
|
|
// Set up optimization remarks handling.
|
2019-06-14 18:20:51 +02:00
|
|
|
Conf.RemarksFilename = options::RemarksFilename;
|
|
|
|
Conf.RemarksPasses = options::RemarksPasses;
|
|
|
|
Conf.RemarksWithHotness = options::RemarksWithHotness;
|
2019-06-17 18:06:00 +02:00
|
|
|
Conf.RemarksFormat = options::RemarksFormat;
|
2018-04-18 15:25:23 +02:00
|
|
|
|
2017-10-05 03:48:42 +02:00
|
|
|
// Use new pass manager if set in driver
|
|
|
|
Conf.UseNewPM = options::new_pass_manager;
|
2018-04-05 05:16:57 +02:00
|
|
|
// Debug new pass manager if requested
|
|
|
|
Conf.DebugPassManager = options::debug_pass_manager;
|
2017-10-05 03:48:42 +02:00
|
|
|
|
2020-01-24 21:24:18 +01:00
|
|
|
Conf.HasWholeProgramVisibility = options::whole_program_visibility;
|
|
|
|
|
2018-04-20 12:18:36 +02:00
|
|
|
Conf.StatsFile = options::stats_file;
|
2019-08-15 17:54:37 +02:00
|
|
|
return std::make_unique<LTO>(std::move(Conf), Backend,
|
2016-09-23 22:35:19 +02:00
|
|
|
options::ParallelCodeGenParallelismLevel);
|
2016-05-10 15:48:23 +02:00
|
|
|
}
|
2016-03-04 18:06:02 +01:00
|
|
|
|
2016-09-21 21:12:05 +02:00
|
|
|
// Write empty files that may be expected by a distributed build
|
|
|
|
// system when invoked with thinlto_index_only. This is invoked when
|
|
|
|
// the linker has decided not to include the given module in the
|
|
|
|
// final link. Frequently the distributed build system will want to
|
|
|
|
// confirm that all expected outputs are created based on all of the
|
|
|
|
// modules provided to the linker.
|
2018-02-17 00:38:22 +01:00
|
|
|
// If SkipModule is true then .thinlto.bc should contain just
|
|
|
|
// SkipModuleByDistributedBackend flag which requests distributed backend
|
|
|
|
// to skip the compilation of the corresponding module and produce an empty
|
|
|
|
// object file.
|
2018-01-30 22:19:26 +01:00
|
|
|
static void writeEmptyDistributedBuildOutputs(const std::string &ModulePath,
|
|
|
|
const std::string &OldPrefix,
|
2018-02-17 00:38:22 +01:00
|
|
|
const std::string &NewPrefix,
|
|
|
|
bool SkipModule) {
|
2016-09-21 21:12:05 +02:00
|
|
|
std::string NewModulePath =
|
|
|
|
getThinLTOOutputFile(ModulePath, OldPrefix, NewPrefix);
|
|
|
|
std::error_code EC;
|
|
|
|
{
|
|
|
|
raw_fd_ostream OS(NewModulePath + ".thinlto.bc", EC,
|
2019-08-05 07:43:48 +02:00
|
|
|
sys::fs::OpenFlags::OF_None);
|
2016-09-21 21:12:05 +02:00
|
|
|
if (EC)
|
|
|
|
message(LDPL_FATAL, "Failed to write '%s': %s",
|
|
|
|
(NewModulePath + ".thinlto.bc").c_str(), EC.message().c_str());
|
2018-02-17 00:38:22 +01:00
|
|
|
|
|
|
|
if (SkipModule) {
|
2018-06-07 00:22:01 +02:00
|
|
|
ModuleSummaryIndex Index(/*HaveGVs*/ false);
|
2018-02-17 00:38:22 +01:00
|
|
|
Index.setSkipModuleByDistributedBackend();
|
|
|
|
WriteIndexToFile(Index, OS, nullptr);
|
|
|
|
}
|
2016-09-21 21:12:05 +02:00
|
|
|
}
|
|
|
|
if (options::thinlto_emit_imports_files) {
|
|
|
|
raw_fd_ostream OS(NewModulePath + ".imports", EC,
|
2019-08-05 07:43:48 +02:00
|
|
|
sys::fs::OpenFlags::OF_None);
|
2016-09-21 21:12:05 +02:00
|
|
|
if (EC)
|
|
|
|
message(LDPL_FATAL, "Failed to write '%s': %s",
|
|
|
|
(NewModulePath + ".imports").c_str(), EC.message().c_str());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-22 20:06:15 +01:00
|
|
|
// Creates and returns output stream with a list of object files for final
|
|
|
|
// linking of distributed ThinLTO.
|
|
|
|
static std::unique_ptr<raw_fd_ostream> CreateLinkedObjectsFile() {
|
|
|
|
if (options::thinlto_linked_objects_file.empty())
|
|
|
|
return nullptr;
|
|
|
|
assert(options::thinlto_index_only);
|
|
|
|
std::error_code EC;
|
2019-08-15 17:54:37 +02:00
|
|
|
auto LinkedObjectsFile = std::make_unique<raw_fd_ostream>(
|
2019-08-05 07:43:48 +02:00
|
|
|
options::thinlto_linked_objects_file, EC, sys::fs::OpenFlags::OF_None);
|
2018-02-22 20:06:15 +01:00
|
|
|
if (EC)
|
|
|
|
message(LDPL_FATAL, "Failed to create '%s': %s",
|
|
|
|
options::thinlto_linked_objects_file.c_str(), EC.message().c_str());
|
|
|
|
return LinkedObjectsFile;
|
|
|
|
}
|
|
|
|
|
2018-02-22 20:06:05 +01:00
|
|
|
/// Runs LTO and return a list of pairs <FileName, IsTemporary>.
|
|
|
|
static std::vector<std::pair<SmallString<128>, bool>> runLTO() {
|
2016-08-20 03:24:07 +02:00
|
|
|
// Map to own RAII objects that manage the file opening and releasing
|
|
|
|
// interfaces with gold. This is needed only for ThinLTO mode, since
|
|
|
|
// unlike regular LTO, where addModule will result in the opened file
|
|
|
|
// being merged into a new combined module, we need to keep these files open
|
|
|
|
// through Lto->run().
|
|
|
|
DenseMap<void *, std::unique_ptr<PluginInputFile>> HandleToInputFile;
|
|
|
|
|
2018-01-30 22:19:26 +01:00
|
|
|
// Owns string objects and tells if index file was already created.
|
|
|
|
StringMap<bool> ObjectToIndexFileState;
|
|
|
|
|
2018-02-22 20:06:15 +01:00
|
|
|
std::unique_ptr<raw_fd_ostream> LinkedObjects = CreateLinkedObjectsFile();
|
|
|
|
std::unique_ptr<LTO> Lto = createLTO(
|
|
|
|
[&ObjectToIndexFileState](const std::string &Identifier) {
|
2018-01-30 22:19:26 +01:00
|
|
|
ObjectToIndexFileState[Identifier] = true;
|
2018-02-22 20:06:15 +01:00
|
|
|
},
|
|
|
|
LinkedObjects.get());
|
2015-12-09 20:49:40 +01:00
|
|
|
|
2016-09-21 21:12:05 +02:00
|
|
|
std::string OldPrefix, NewPrefix;
|
|
|
|
if (options::thinlto_index_only)
|
|
|
|
getThinLTOOldAndNewPrefix(OldPrefix, NewPrefix);
|
|
|
|
|
[ThinLTO] Add support for emitting minimized bitcode for thin link
Summary:
The cumulative size of the bitcode files for a very large application
can be huge, particularly with -g. In a distributed build environment,
all of these files must be sent to the remote build node that performs
the thin link step, and this can exceed size limits.
The thin link actually only needs the summary along with a bitcode
symbol table. Until we have a proper bitcode symbol table, simply
stripping the debug metadata results in significant size reduction.
Add support for an option to additionally emit minimized bitcode
modules, just for use in the thin link step, which for now just strips
all debug metadata. I plan to add a cc1 option so this can be invoked
easily during the compile step.
However, care must be taken to ensure that these minimized thin link
bitcode files produce the same index as with the original bitcode files,
as these original bitcode files will be used in the backends.
Specifically:
1) The module hash used for caching is typically produced by hashing the
written bitcode, and we want to include the hash that would correspond
to the original bitcode file. This is because we want to ensure that
changes in the stripped portions affect caching. Added plumbing to emit
the same module hash in the minimized thin link bitcode file.
2) The module paths in the index are constructed from the module ID of
each thin linked bitcode, and typically is automatically generated from
the input file path. This is the path used for finding the modules to
import from, and obviously we need this to point to the original bitcode
files. Added gold-plugin support to take a suffix replacement during the
thin link that is used to override the identifier on the MemoryBufferRef
constructed from the loaded thin link bitcode file. The assumption is
that the build system can specify that the minimized bitcode file has a
name that is similar but uses a different suffix (e.g. out.thinlink.bc
instead of out.o).
Added various tests to ensure that we get identical index files out of
the thin link step.
Reviewers: mehdi_amini, pcc
Subscribers: Prazek, llvm-commits
Differential Revision: https://reviews.llvm.org/D31027
llvm-svn: 298638
2017-03-23 20:47:39 +01:00
|
|
|
std::string OldSuffix, NewSuffix;
|
|
|
|
getThinLTOOldAndNewSuffix(OldSuffix, NewSuffix);
|
|
|
|
|
2014-08-21 22:28:55 +02:00
|
|
|
for (claimed_file &F : Modules) {
|
2016-08-20 03:24:07 +02:00
|
|
|
if (options::thinlto && !HandleToInputFile.count(F.leader_handle))
|
|
|
|
HandleToInputFile.insert(std::make_pair(
|
2019-08-15 17:54:37 +02:00
|
|
|
F.leader_handle, std::make_unique<PluginInputFile>(F.handle)));
|
[ThinLTO] Add support for emitting minimized bitcode for thin link
Summary:
The cumulative size of the bitcode files for a very large application
can be huge, particularly with -g. In a distributed build environment,
all of these files must be sent to the remote build node that performs
the thin link step, and this can exceed size limits.
The thin link actually only needs the summary along with a bitcode
symbol table. Until we have a proper bitcode symbol table, simply
stripping the debug metadata results in significant size reduction.
Add support for an option to additionally emit minimized bitcode
modules, just for use in the thin link step, which for now just strips
all debug metadata. I plan to add a cc1 option so this can be invoked
easily during the compile step.
However, care must be taken to ensure that these minimized thin link
bitcode files produce the same index as with the original bitcode files,
as these original bitcode files will be used in the backends.
Specifically:
1) The module hash used for caching is typically produced by hashing the
written bitcode, and we want to include the hash that would correspond
to the original bitcode file. This is because we want to ensure that
changes in the stripped portions affect caching. Added plumbing to emit
the same module hash in the minimized thin link bitcode file.
2) The module paths in the index are constructed from the module ID of
each thin linked bitcode, and typically is automatically generated from
the input file path. This is the path used for finding the modules to
import from, and obviously we need this to point to the original bitcode
files. Added gold-plugin support to take a suffix replacement during the
thin link that is used to override the identifier on the MemoryBufferRef
constructed from the loaded thin link bitcode file. The assumption is
that the build system can specify that the minimized bitcode file has a
name that is similar but uses a different suffix (e.g. out.thinlink.bc
instead of out.o).
Added various tests to ensure that we get identical index files out of
the thin link step.
Reviewers: mehdi_amini, pcc
Subscribers: Prazek, llvm-commits
Differential Revision: https://reviews.llvm.org/D31027
llvm-svn: 298638
2017-03-23 20:47:39 +01:00
|
|
|
// In case we are thin linking with a minimized bitcode file, ensure
|
|
|
|
// the module paths encoded in the index reflect where the backends
|
|
|
|
// will locate the full bitcode files for compiling/importing.
|
|
|
|
std::string Identifier =
|
|
|
|
getThinLTOObjectFileName(F.name, OldSuffix, NewSuffix);
|
2018-01-30 22:19:26 +01:00
|
|
|
auto ObjFilename = ObjectToIndexFileState.insert({Identifier, false});
|
[ThinLTO] Add support for emitting minimized bitcode for thin link
Summary:
The cumulative size of the bitcode files for a very large application
can be huge, particularly with -g. In a distributed build environment,
all of these files must be sent to the remote build node that performs
the thin link step, and this can exceed size limits.
The thin link actually only needs the summary along with a bitcode
symbol table. Until we have a proper bitcode symbol table, simply
stripping the debug metadata results in significant size reduction.
Add support for an option to additionally emit minimized bitcode
modules, just for use in the thin link step, which for now just strips
all debug metadata. I plan to add a cc1 option so this can be invoked
easily during the compile step.
However, care must be taken to ensure that these minimized thin link
bitcode files produce the same index as with the original bitcode files,
as these original bitcode files will be used in the backends.
Specifically:
1) The module hash used for caching is typically produced by hashing the
written bitcode, and we want to include the hash that would correspond
to the original bitcode file. This is because we want to ensure that
changes in the stripped portions affect caching. Added plumbing to emit
the same module hash in the minimized thin link bitcode file.
2) The module paths in the index are constructed from the module ID of
each thin linked bitcode, and typically is automatically generated from
the input file path. This is the path used for finding the modules to
import from, and obviously we need this to point to the original bitcode
files. Added gold-plugin support to take a suffix replacement during the
thin link that is used to override the identifier on the MemoryBufferRef
constructed from the loaded thin link bitcode file. The assumption is
that the build system can specify that the minimized bitcode file has a
name that is similar but uses a different suffix (e.g. out.thinlink.bc
instead of out.o).
Added various tests to ensure that we get identical index files out of
the thin link step.
Reviewers: mehdi_amini, pcc
Subscribers: Prazek, llvm-commits
Differential Revision: https://reviews.llvm.org/D31027
llvm-svn: 298638
2017-03-23 20:47:39 +01:00
|
|
|
assert(ObjFilename.second);
|
2018-01-30 22:19:26 +01:00
|
|
|
if (const void *View = getSymbolsAndView(F))
|
|
|
|
addModule(*Lto, F, View, ObjFilename.first->first());
|
2018-02-17 00:38:22 +01:00
|
|
|
else if (options::thinlto_index_only) {
|
|
|
|
ObjFilename.first->second = true;
|
|
|
|
writeEmptyDistributedBuildOutputs(Identifier, OldPrefix, NewPrefix,
|
|
|
|
/* SkipModule */ true);
|
|
|
|
}
|
2014-08-21 22:28:55 +02:00
|
|
|
}
|
|
|
|
|
2016-08-11 16:58:12 +02:00
|
|
|
SmallString<128> Filename;
|
2016-08-17 08:23:09 +02:00
|
|
|
// Note that getOutputFileName will append a unique ID for each task
|
2016-08-11 16:58:12 +02:00
|
|
|
if (!options::obj_path.empty())
|
|
|
|
Filename = options::obj_path;
|
|
|
|
else if (options::TheOutputType == options::OT_SAVE_TEMPS)
|
|
|
|
Filename = output_name + ".o";
|
2019-01-03 00:48:00 +01:00
|
|
|
else if (options::TheOutputType == options::OT_ASM_ONLY)
|
|
|
|
Filename = output_name;
|
2016-08-11 16:58:12 +02:00
|
|
|
bool SaveTemps = !Filename.empty();
|
|
|
|
|
2017-01-26 03:07:05 +01:00
|
|
|
size_t MaxTasks = Lto->getMaxTasks();
|
2018-02-22 20:06:05 +01:00
|
|
|
std::vector<std::pair<SmallString<128>, bool>> Files(MaxTasks);
|
2016-08-11 16:58:12 +02:00
|
|
|
|
2016-09-23 23:33:43 +02:00
|
|
|
auto AddStream =
|
|
|
|
[&](size_t Task) -> std::unique_ptr<lto::NativeObjectStream> {
|
2018-02-22 20:06:05 +01:00
|
|
|
Files[Task].second = !SaveTemps;
|
2018-02-17 00:38:22 +01:00
|
|
|
int FD = getOutputFileName(Filename, /* TempOutFile */ !SaveTemps,
|
2018-02-22 20:06:05 +01:00
|
|
|
Files[Task].first, Task);
|
2019-08-15 17:54:37 +02:00
|
|
|
return std::make_unique<lto::NativeObjectStream>(
|
|
|
|
std::make_unique<llvm::raw_fd_ostream>(FD, true));
|
2016-08-11 16:58:12 +02:00
|
|
|
};
|
|
|
|
|
2018-02-20 21:21:53 +01:00
|
|
|
auto AddBuffer = [&](size_t Task, std::unique_ptr<MemoryBuffer> MB) {
|
2018-02-20 20:51:30 +01:00
|
|
|
*AddStream(Task)->OS << MB->getBuffer();
|
2017-03-17 01:34:07 +01:00
|
|
|
};
|
2016-09-23 23:33:43 +02:00
|
|
|
|
|
|
|
NativeObjectCache Cache;
|
|
|
|
if (!options::cache_dir.empty())
|
2017-03-17 01:34:07 +01:00
|
|
|
Cache = check(localCache(options::cache_dir, AddBuffer));
|
2016-09-23 23:33:43 +02:00
|
|
|
|
|
|
|
check(Lto->run(AddStream, Cache));
|
2009-02-03 08:13:24 +01:00
|
|
|
|
2018-01-30 22:19:26 +01:00
|
|
|
// Write empty output files that may be expected by the distributed build
|
|
|
|
// system.
|
|
|
|
if (options::thinlto_index_only)
|
|
|
|
for (auto &Identifier : ObjectToIndexFileState)
|
|
|
|
if (!Identifier.getValue())
|
2020-01-28 20:23:46 +01:00
|
|
|
writeEmptyDistributedBuildOutputs(std::string(Identifier.getKey()),
|
|
|
|
OldPrefix, NewPrefix,
|
|
|
|
/* SkipModule */ false);
|
2018-01-30 22:19:26 +01:00
|
|
|
|
2018-02-22 20:06:05 +01:00
|
|
|
return Files;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// gold informs us that all symbols have been read. At this point, we use
|
|
|
|
/// get_symbols to see if any of our definitions have been overridden by a
|
|
|
|
/// native object file. Then, perform optimization and codegen.
|
|
|
|
static ld_plugin_status allSymbolsReadHook() {
|
|
|
|
if (Modules.empty())
|
|
|
|
return LDPS_OK;
|
|
|
|
|
|
|
|
if (unsigned NumOpts = options::extra.size())
|
|
|
|
cl::ParseCommandLineOptions(NumOpts, &options::extra[0]);
|
|
|
|
|
|
|
|
std::vector<std::pair<SmallString<128>, bool>> Files = runLTO();
|
|
|
|
|
2016-08-11 16:58:12 +02:00
|
|
|
if (options::TheOutputType == options::OT_DISABLE ||
|
2019-01-03 00:48:00 +01:00
|
|
|
options::TheOutputType == options::OT_BC_ONLY ||
|
|
|
|
options::TheOutputType == options::OT_ASM_ONLY)
|
2014-11-24 22:18:14 +01:00
|
|
|
return LDPS_OK;
|
|
|
|
|
2016-08-11 16:58:12 +02:00
|
|
|
if (options::thinlto_index_only) {
|
2018-04-19 18:55:13 +02:00
|
|
|
llvm_shutdown();
|
2016-08-11 16:58:12 +02:00
|
|
|
cleanup_hook();
|
|
|
|
exit(0);
|
2010-05-13 15:39:31 +02:00
|
|
|
}
|
2013-10-16 14:47:04 +02:00
|
|
|
|
2018-02-22 20:06:05 +01:00
|
|
|
for (const auto &F : Files)
|
|
|
|
if (!F.first.empty())
|
2020-01-28 20:23:46 +01:00
|
|
|
recordFile(std::string(F.first.str()), F.second);
|
2009-02-03 08:13:24 +01:00
|
|
|
|
2010-06-23 22:20:59 +02:00
|
|
|
if (!options::extra_library_path.empty() &&
|
2014-08-21 22:28:55 +02:00
|
|
|
set_extra_library_path(options::extra_library_path.c_str()) != LDPS_OK)
|
|
|
|
message(LDPL_FATAL, "Unable to set the extra library path.");
|
2013-08-12 23:07:31 +02:00
|
|
|
|
2009-02-03 08:13:24 +01:00
|
|
|
return LDPS_OK;
|
|
|
|
}
|
|
|
|
|
2014-08-11 21:06:54 +02:00
|
|
|
static ld_plugin_status all_symbols_read_hook(void) {
|
2016-08-11 16:58:12 +02:00
|
|
|
ld_plugin_status Ret = allSymbolsReadHook();
|
2014-11-25 21:52:49 +01:00
|
|
|
llvm_shutdown();
|
|
|
|
|
2014-11-24 22:18:14 +01:00
|
|
|
if (options::TheOutputType == options::OT_BC_ONLY ||
|
2019-01-03 00:48:00 +01:00
|
|
|
options::TheOutputType == options::OT_ASM_ONLY ||
|
2015-02-12 19:21:50 +01:00
|
|
|
options::TheOutputType == options::OT_DISABLE) {
|
2016-03-20 21:12:33 +01:00
|
|
|
if (options::TheOutputType == options::OT_DISABLE) {
|
2015-02-12 19:21:50 +01:00
|
|
|
// Remove the output file here since ld.bfd creates the output file
|
|
|
|
// early.
|
2016-03-20 21:12:33 +01:00
|
|
|
std::error_code EC = sys::fs::remove(output_name);
|
|
|
|
if (EC)
|
|
|
|
message(LDPL_ERROR, "Failed to delete '%s': %s", output_name.c_str(),
|
|
|
|
EC.message().c_str());
|
|
|
|
}
|
2014-08-11 21:06:54 +02:00
|
|
|
exit(0);
|
2015-02-12 19:21:50 +01:00
|
|
|
}
|
2014-08-11 21:06:54 +02:00
|
|
|
|
|
|
|
return Ret;
|
|
|
|
}
|
|
|
|
|
2010-04-16 02:42:57 +02:00
|
|
|
static ld_plugin_status cleanup_hook(void) {
|
2014-07-30 03:52:40 +02:00
|
|
|
for (std::string &Name : Cleanup) {
|
|
|
|
std::error_code EC = sys::fs::remove(Name);
|
2013-06-17 20:38:18 +02:00
|
|
|
if (EC)
|
2014-07-30 03:52:40 +02:00
|
|
|
message(LDPL_ERROR, "Failed to delete '%s': %s", Name.c_str(),
|
2014-07-30 02:38:58 +02:00
|
|
|
EC.message().c_str());
|
2013-06-17 20:38:18 +02:00
|
|
|
}
|
2009-02-03 08:13:24 +01:00
|
|
|
|
2017-09-19 01:24:55 +02:00
|
|
|
// Prune cache
|
2018-02-22 21:57:05 +01:00
|
|
|
if (!options::cache_dir.empty()) {
|
2017-09-19 01:24:55 +02:00
|
|
|
CachePruningPolicy policy = check(parseCachePruningPolicy(options::cache_policy));
|
|
|
|
pruneCache(options::cache_dir, policy);
|
|
|
|
}
|
|
|
|
|
2009-02-03 08:13:24 +01:00
|
|
|
return LDPS_OK;
|
|
|
|
}
|