1 //===-- gold-plugin.cpp - Plugin to gold for Link Time Optimization ------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // This is a gold plugin for LLVM. It provides an LLVM implementation of the
10 // interface described in http://gcc.gnu.org/wiki/whopr/driver .
12 //===----------------------------------------------------------------------===//
14 #include "llvm/ADT/ScopeExit.h"
15 #include "llvm/ADT/Statistic.h"
16 #include "llvm/Bitcode/BitcodeReader.h"
17 #include "llvm/Bitcode/BitcodeWriter.h"
18 #include "llvm/CodeGen/CommandFlags.h"
19 #include "llvm/Config/config.h" // plugin-api.h requires HAVE_STDINT_H
20 #include "llvm/Config/llvm-config.h"
21 #include "llvm/IR/Constants.h"
22 #include "llvm/IR/DiagnosticPrinter.h"
23 #include "llvm/LTO/LTO.h"
24 #include "llvm/Object/Error.h"
25 #include "llvm/Remarks/HotnessThresholdParser.h"
26 #include "llvm/Support/CachePruning.h"
27 #include "llvm/Support/Caching.h"
28 #include "llvm/Support/CommandLine.h"
29 #include "llvm/Support/FileSystem.h"
30 #include "llvm/Support/ManagedStatic.h"
31 #include "llvm/Support/MemoryBuffer.h"
32 #include "llvm/Support/Path.h"
33 #include "llvm/Support/TargetSelect.h"
34 #include "llvm/Support/Threading.h"
35 #include "llvm/Support/TimeProfiler.h"
36 #include "llvm/Support/raw_ostream.h"
37 #include "llvm/TargetParser/Host.h"
40 #include <plugin-api.h>
42 #include <system_error>
46 // FIXME: remove this declaration when we stop maintaining Ubuntu Quantal and
47 // Precise and Debian Wheezy (binutils 2.23 is required)
50 #define LDPT_GET_SYMBOLS_V3 28
52 // FIXME: Remove when binutils 2.31 (containing gold 1.16) is the minimum
54 #define LDPT_GET_WRAP_SYMBOLS 32
59 static codegen::RegisterCodeGenFlags CodeGenFlags
;
61 // FIXME: Remove when binutils 2.31 (containing gold 1.16) is the minimum
63 typedef enum ld_plugin_status (*ld_plugin_get_wrap_symbols
)(
64 uint64_t *num_symbols
, const char ***wrap_symbol_list
);
66 static ld_plugin_status
discard_message(int level
, const char *format
, ...) {
67 // Die loudly. Recent versions of Gold pass ld_plugin_message as the first
68 // callback in the transfer vector. This should never be called.
72 static ld_plugin_release_input_file release_input_file
= nullptr;
73 static ld_plugin_get_input_file get_input_file
= nullptr;
74 static ld_plugin_message message
= discard_message
;
75 static ld_plugin_get_wrap_symbols get_wrap_symbols
= nullptr;
81 std::vector
<ld_plugin_symbol
> syms
;
86 /// RAII wrapper to manage opening and releasing of a ld_plugin_input_file.
87 struct PluginInputFile
{
89 std::unique_ptr
<ld_plugin_input_file
> File
;
91 PluginInputFile(void *Handle
) : Handle(Handle
) {
92 File
= std::make_unique
<ld_plugin_input_file
>();
93 if (get_input_file(Handle
, File
.get()) != LDPS_OK
)
94 message(LDPL_FATAL
, "Failed to get file information");
97 // File would have been reset to nullptr if we moved this object
100 if (release_input_file(Handle
) != LDPS_OK
)
101 message(LDPL_FATAL
, "Failed to release file information");
104 ld_plugin_input_file
&file() { return *File
; }
106 PluginInputFile(PluginInputFile
&&RHS
) = default;
107 PluginInputFile
&operator=(PluginInputFile
&&RHS
) = default;
110 struct ResolutionInfo
{
111 bool CanOmitFromDynSym
= true;
112 bool DefaultVisibility
= true;
113 bool CanInline
= true;
114 bool IsUsedInRegularObj
= false;
119 static ld_plugin_add_symbols add_symbols
= nullptr;
120 static ld_plugin_get_symbols get_symbols
= nullptr;
121 static ld_plugin_add_input_file add_input_file
= nullptr;
122 static ld_plugin_set_extra_library_path set_extra_library_path
= nullptr;
123 static ld_plugin_get_view get_view
= nullptr;
124 static bool IsExecutable
= false;
125 static bool SplitSections
= true;
126 static std::optional
<Reloc::Model
> RelocationModel
;
127 static std::string output_name
= "";
128 static std::list
<claimed_file
> Modules
;
129 static DenseMap
<int, void *> FDToLeaderHandle
;
130 static StringMap
<ResolutionInfo
> ResInfo
;
131 static std::vector
<std::string
> Cleanup
;
141 static OutputType TheOutputType
= OT_NORMAL
;
142 static unsigned OptLevel
= 2;
143 // Currently only affects ThinLTO, where the default is the max cores in the
144 // system. See llvm::get_threadpool_strategy() for acceptable values.
145 static std::string Parallelism
;
146 // Default regular LTO codegen parallelism (number of partitions).
147 static unsigned ParallelCodeGenParallelismLevel
= 1;
149 static bool DisableVerify
= true;
151 static bool DisableVerify
= false;
153 static std::string obj_path
;
154 static std::string extra_library_path
;
155 static std::string triple
;
156 static std::string mcpu
;
157 // When the thinlto plugin option is specified, only read the function
158 // the information from intermediate files and write a combined
159 // global index for the ThinLTO backends.
160 static bool thinlto
= false;
161 // If false, all ThinLTO backend compilations through code gen are performed
162 // using multiple threads in the gold-plugin, before handing control back to
163 // gold. If true, write individual backend index files which reflect
164 // the import decisions, and exit afterwards. The assumption is
165 // that the build system will launch the backend processes.
166 static bool thinlto_index_only
= false;
167 // If non-empty, holds the name of a file in which to write the list of
168 // oject files gold selected for inclusion in the link after symbol
169 // resolution (i.e. they had selected symbols). This will only be non-empty
170 // in the thinlto_index_only case. It is used to identify files, which may
171 // have originally been within archive libraries specified via
172 // --start-lib/--end-lib pairs, that should be included in the final
173 // native link process (since intervening function importing and inlining
174 // may change the symbol resolution detected in the final link and which
175 // files to include out of --start-lib/--end-lib libraries as a result).
176 static std::string thinlto_linked_objects_file
;
177 // If true, when generating individual index files for distributed backends,
178 // also generate a "${bitcodefile}.imports" file at the same location for each
179 // bitcode file, listing the files it imports from in plain text. This is to
180 // support distributed build file staging.
181 static bool thinlto_emit_imports_files
= false;
182 // Option to control where files for a distributed backend (the individual
183 // index files and optional imports files) are created.
184 // If specified, expects a string of the form "oldprefix:newprefix", and
185 // instead of generating these files in the same directory path as the
186 // corresponding bitcode file, will use a path formed by replacing the
187 // bitcode file's path prefix matching oldprefix with newprefix.
188 static std::string thinlto_prefix_replace
;
189 // Option to control the name of modules encoded in the individual index
190 // files for a distributed backend. This enables the use of minimized
191 // bitcode files for the thin link, assuming the name of the full bitcode
192 // file used in the backend differs just in some part of the file suffix.
193 // If specified, expects a string of the form "oldsuffix:newsuffix".
194 static std::string thinlto_object_suffix_replace
;
195 // Optional path to a directory for caching ThinLTO objects.
196 static std::string cache_dir
;
197 // Optional pruning policy for ThinLTO caches.
198 static std::string cache_policy
;
199 // Additional options to pass into the code generator.
200 // Note: This array will contain all plugin options which are not claimed
201 // as plugin exclusive to pass to the code generator.
202 static std::vector
<const char *> extra
;
203 // Sample profile file path
204 static std::string sample_profile
;
205 // Debug new pass manager
206 static bool debug_pass_manager
= false;
207 // Directory to store the .dwo files.
208 static std::string dwo_dir
;
209 /// Statistics output filename.
210 static std::string stats_file
;
211 // Asserts that LTO link has whole program visibility
212 static bool whole_program_visibility
= false;
214 // Optimization remarks filename, accepted passes and hotness options
215 static std::string RemarksFilename
;
216 static std::string RemarksPasses
;
217 static bool RemarksWithHotness
= false;
218 static std::optional
<uint64_t> RemarksHotnessThreshold
= 0;
219 static std::string RemarksFormat
;
221 // Context sensitive PGO options.
222 static std::string cs_profile_path
;
223 static bool cs_pgo_gen
= false;
225 // Time trace options.
226 static std::string time_trace_file
;
227 static unsigned time_trace_granularity
= 500;
229 static void process_plugin_option(const char *opt_
)
233 llvm::StringRef opt
= opt_
;
235 if (opt
.consume_front("mcpu=")) {
236 mcpu
= std::string(opt
);
237 } else if (opt
.consume_front("extra-library-path=")) {
238 extra_library_path
= std::string(opt
);
239 } else if (opt
.consume_front("mtriple=")) {
240 triple
= std::string(opt
);
241 } else if (opt
.consume_front("obj-path=")) {
242 obj_path
= std::string(opt
);
243 } else if (opt
== "emit-llvm") {
244 TheOutputType
= OT_BC_ONLY
;
245 } else if (opt
== "save-temps") {
246 TheOutputType
= OT_SAVE_TEMPS
;
247 } else if (opt
== "disable-output") {
248 TheOutputType
= OT_DISABLE
;
249 } else if (opt
== "emit-asm") {
250 TheOutputType
= OT_ASM_ONLY
;
251 } else if (opt
== "thinlto") {
253 } else if (opt
== "thinlto-index-only") {
254 thinlto_index_only
= true;
255 } else if (opt
.consume_front("thinlto-index-only=")) {
256 thinlto_index_only
= true;
257 thinlto_linked_objects_file
= std::string(opt
);
258 } else if (opt
== "thinlto-emit-imports-files") {
259 thinlto_emit_imports_files
= true;
260 } else if (opt
.consume_front("thinlto-prefix-replace=")) {
261 thinlto_prefix_replace
= std::string(opt
);
262 if (thinlto_prefix_replace
.find(';') == std::string::npos
)
263 message(LDPL_FATAL
, "thinlto-prefix-replace expects 'old;new' format");
264 } else if (opt
.consume_front("thinlto-object-suffix-replace=")) {
265 thinlto_object_suffix_replace
= std::string(opt
);
266 if (thinlto_object_suffix_replace
.find(';') == std::string::npos
)
268 "thinlto-object-suffix-replace expects 'old;new' format");
269 } else if (opt
.consume_front("cache-dir=")) {
270 cache_dir
= std::string(opt
);
271 } else if (opt
.consume_front("cache-policy=")) {
272 cache_policy
= std::string(opt
);
273 } else if (opt
.size() == 2 && opt
[0] == 'O') {
274 if (opt
[1] < '0' || opt
[1] > '3')
275 message(LDPL_FATAL
, "Optimization level must be between 0 and 3");
276 OptLevel
= opt
[1] - '0';
277 } else if (opt
.consume_front("jobs=")) {
278 Parallelism
= std::string(opt
);
279 if (!get_threadpool_strategy(opt
))
280 message(LDPL_FATAL
, "Invalid parallelism level: %s",
281 Parallelism
.c_str());
282 } else if (opt
.consume_front("lto-partitions=")) {
283 if (opt
.getAsInteger(10, ParallelCodeGenParallelismLevel
))
284 message(LDPL_FATAL
, "Invalid codegen partition level: %s", opt_
+ 5);
285 } else if (opt
== "disable-verify") {
286 DisableVerify
= true;
287 } else if (opt
.consume_front("sample-profile=")) {
288 sample_profile
= std::string(opt
);
289 } else if (opt
== "cs-profile-generate") {
291 } else if (opt
.consume_front("cs-profile-path=")) {
292 cs_profile_path
= std::string(opt
);
293 } else if (opt
== "new-pass-manager") {
294 // We always use the new pass manager.
295 } else if (opt
== "debug-pass-manager") {
296 debug_pass_manager
= true;
297 } else if (opt
== "whole-program-visibility") {
298 whole_program_visibility
= true;
299 } else if (opt
.consume_front("dwo_dir=")) {
300 dwo_dir
= std::string(opt
);
301 } else if (opt
.consume_front("opt-remarks-filename=")) {
302 RemarksFilename
= std::string(opt
);
303 } else if (opt
.consume_front("opt-remarks-passes=")) {
304 RemarksPasses
= std::string(opt
);
305 } else if (opt
== "opt-remarks-with-hotness") {
306 RemarksWithHotness
= true;
307 } else if (opt
.consume_front("opt-remarks-hotness-threshold=")) {
308 auto ResultOrErr
= remarks::parseHotnessThresholdOption(opt
);
310 message(LDPL_FATAL
, "Invalid remarks hotness threshold: %s",
313 RemarksHotnessThreshold
= *ResultOrErr
;
314 } else if (opt
.consume_front("opt-remarks-format=")) {
315 RemarksFormat
= std::string(opt
);
316 } else if (opt
.consume_front("stats-file=")) {
317 stats_file
= std::string(opt
);
318 } else if (opt
.consume_front("time-trace=")) {
319 time_trace_file
= std::string(opt
);
320 } else if (opt
.consume_front("time-trace-granularity=")) {
321 unsigned Granularity
;
322 if (opt
.getAsInteger(10, Granularity
))
323 message(LDPL_FATAL
, "Invalid time trace granularity: %s", opt
.data());
325 time_trace_granularity
= Granularity
;
327 // Save this option to pass to the code generator.
328 // ParseCommandLineOptions() expects argv[0] to be program name. Lazily
331 extra
.push_back("LLVMgold");
333 extra
.push_back(opt_
);
338 static ld_plugin_status
claim_file_hook(const ld_plugin_input_file
*file
,
340 static ld_plugin_status
all_symbols_read_hook(void);
341 static ld_plugin_status
cleanup_hook(void);
343 extern "C" ld_plugin_status
onload(ld_plugin_tv
*tv
);
344 ld_plugin_status
onload(ld_plugin_tv
*tv
) {
345 InitializeAllTargetInfos();
346 InitializeAllTargets();
347 InitializeAllTargetMCs();
348 InitializeAllAsmParsers();
349 InitializeAllAsmPrinters();
351 // We're given a pointer to the first transfer vector. We read through them
352 // until we find one where tv_tag == LDPT_NULL. The REGISTER_* tagged values
353 // contain pointers to functions that we need to call to register our own
354 // hooks. The others are addresses of functions we can use to call into gold
357 bool registeredClaimFile
= false;
358 bool RegisteredAllSymbolsRead
= false;
360 for (; tv
->tv_tag
!= LDPT_NULL
; ++tv
) {
361 // Cast tv_tag to int to allow values not in "enum ld_plugin_tag", like, for
362 // example, LDPT_GET_SYMBOLS_V3 when building against an older plugin-api.h
364 switch (static_cast<int>(tv
->tv_tag
)) {
365 case LDPT_OUTPUT_NAME
:
366 output_name
= tv
->tv_u
.tv_string
;
368 case LDPT_LINKER_OUTPUT
:
369 switch (tv
->tv_u
.tv_val
) {
371 IsExecutable
= false;
372 SplitSections
= false;
374 case LDPO_DYN
: // .so
375 IsExecutable
= false;
376 RelocationModel
= Reloc::PIC_
;
378 case LDPO_PIE
: // position independent executable
380 RelocationModel
= Reloc::PIC_
;
382 case LDPO_EXEC
: // .exe
384 RelocationModel
= Reloc::Static
;
387 message(LDPL_ERROR
, "Unknown output file type %d", tv
->tv_u
.tv_val
);
392 options::process_plugin_option(tv
->tv_u
.tv_string
);
394 case LDPT_REGISTER_CLAIM_FILE_HOOK
: {
395 ld_plugin_register_claim_file callback
;
396 callback
= tv
->tv_u
.tv_register_claim_file
;
398 if (callback(claim_file_hook
) != LDPS_OK
)
401 registeredClaimFile
= true;
403 case LDPT_REGISTER_ALL_SYMBOLS_READ_HOOK
: {
404 ld_plugin_register_all_symbols_read callback
;
405 callback
= tv
->tv_u
.tv_register_all_symbols_read
;
407 if (callback(all_symbols_read_hook
) != LDPS_OK
)
410 RegisteredAllSymbolsRead
= true;
412 case LDPT_REGISTER_CLEANUP_HOOK
: {
413 ld_plugin_register_cleanup callback
;
414 callback
= tv
->tv_u
.tv_register_cleanup
;
416 if (callback(cleanup_hook
) != LDPS_OK
)
419 case LDPT_GET_INPUT_FILE
:
420 get_input_file
= tv
->tv_u
.tv_get_input_file
;
422 case LDPT_RELEASE_INPUT_FILE
:
423 release_input_file
= tv
->tv_u
.tv_release_input_file
;
425 case LDPT_ADD_SYMBOLS
:
426 add_symbols
= tv
->tv_u
.tv_add_symbols
;
428 case LDPT_GET_SYMBOLS_V2
:
429 // Do not override get_symbols_v3 with get_symbols_v2.
431 get_symbols
= tv
->tv_u
.tv_get_symbols
;
433 case LDPT_GET_SYMBOLS_V3
:
434 get_symbols
= tv
->tv_u
.tv_get_symbols
;
436 case LDPT_ADD_INPUT_FILE
:
437 add_input_file
= tv
->tv_u
.tv_add_input_file
;
439 case LDPT_SET_EXTRA_LIBRARY_PATH
:
440 set_extra_library_path
= tv
->tv_u
.tv_set_extra_library_path
;
443 get_view
= tv
->tv_u
.tv_get_view
;
446 message
= tv
->tv_u
.tv_message
;
448 case LDPT_GET_WRAP_SYMBOLS
:
449 // FIXME: When binutils 2.31 (containing gold 1.16) is the minimum
450 // required version, this should be changed to:
451 // get_wrap_symbols = tv->tv_u.tv_get_wrap_symbols;
452 #pragma GCC diagnostic push
453 #pragma GCC diagnostic ignored "-Wcast-function-type"
454 get_wrap_symbols
= (ld_plugin_get_wrap_symbols
)tv
->tv_u
.tv_message
;
455 #pragma GCC diagnostic pop
462 if (!registeredClaimFile
) {
463 message(LDPL_ERROR
, "register_claim_file not passed to LLVMgold.");
467 message(LDPL_ERROR
, "add_symbols not passed to LLVMgold.");
471 if (!RegisteredAllSymbolsRead
)
474 if (!get_input_file
) {
475 message(LDPL_ERROR
, "get_input_file not passed to LLVMgold.");
478 if (!release_input_file
) {
479 message(LDPL_ERROR
, "release_input_file not passed to LLVMgold.");
486 static void diagnosticHandler(const DiagnosticInfo
&DI
) {
487 std::string ErrStorage
;
489 raw_string_ostream
OS(ErrStorage
);
490 DiagnosticPrinterRawOStream
DP(OS
);
493 ld_plugin_level Level
;
494 switch (DI
.getSeverity()) {
499 Level
= LDPL_WARNING
;
506 message(Level
, "LLVM gold plugin: %s", ErrStorage
.c_str());
509 static void check(Error E
, std::string Msg
= "LLVM gold plugin") {
510 handleAllErrors(std::move(E
), [&](ErrorInfoBase
&EIB
) -> Error
{
511 message(LDPL_FATAL
, "%s: %s", Msg
.c_str(), EIB
.message().c_str());
512 return Error::success();
516 template <typename T
> static T
check(Expected
<T
> E
) {
518 return std::move(*E
);
519 check(E
.takeError());
523 /// Called by gold to see whether this file is one that our plugin can handle.
524 /// We'll try to open it and register all the symbols with add_symbol if
526 static ld_plugin_status
claim_file_hook(const ld_plugin_input_file
*file
,
528 MemoryBufferRef BufferRef
;
529 std::unique_ptr
<MemoryBuffer
> Buffer
;
532 if (get_view(file
->handle
, &view
) != LDPS_OK
) {
533 message(LDPL_ERROR
, "Failed to get a view of %s", file
->name
);
537 MemoryBufferRef(StringRef((const char *)view
, file
->filesize
), "");
540 // Gold has found what might be IR part-way inside of a file, such as
543 offset
= file
->offset
;
545 ErrorOr
<std::unique_ptr
<MemoryBuffer
>> BufferOrErr
=
546 MemoryBuffer::getOpenFileSlice(sys::fs::convertFDToNativeFile(file
->fd
),
547 file
->name
, file
->filesize
, offset
);
548 if (std::error_code EC
= BufferOrErr
.getError()) {
549 message(LDPL_ERROR
, EC
.message().c_str());
552 Buffer
= std::move(BufferOrErr
.get());
553 BufferRef
= Buffer
->getMemBufferRef();
558 Expected
<std::unique_ptr
<InputFile
>> ObjOrErr
= InputFile::create(BufferRef
);
560 handleAllErrors(ObjOrErr
.takeError(), [&](const ErrorInfoBase
&EI
) {
561 std::error_code EC
= EI
.convertToErrorCode();
562 if (EC
== object::object_error::invalid_file_type
||
563 EC
== object::object_error::bitcode_section_not_found
)
567 "LLVM gold plugin has failed to create LTO module: %s",
568 EI
.message().c_str());
571 return *claimed
? LDPS_ERR
: LDPS_OK
;
574 std::unique_ptr
<InputFile
> Obj
= std::move(*ObjOrErr
);
576 Modules
.emplace_back();
577 claimed_file
&cf
= Modules
.back();
579 cf
.handle
= file
->handle
;
580 // Keep track of the first handle for each file descriptor, since there are
581 // multiple in the case of an archive. This is used later in the case of
582 // ThinLTO parallel backends to ensure that each file is only opened and
585 FDToLeaderHandle
.insert(std::make_pair(file
->fd
, file
->handle
)).first
;
586 cf
.leader_handle
= LeaderHandle
->second
;
587 // Save the filesize since for parallel ThinLTO backends we can only
588 // invoke get_input_file once per archive (only for the leader handle).
589 cf
.filesize
= file
->filesize
;
590 // In the case of an archive library, all but the first member must have a
591 // non-zero offset, which we can append to the file name to obtain a
593 cf
.name
= file
->name
;
595 cf
.name
+= ".llvm." + std::to_string(file
->offset
) + "." +
596 sys::path::filename(Obj
->getSourceFileName()).str();
598 for (auto &Sym
: Obj
->symbols()) {
599 cf
.syms
.push_back(ld_plugin_symbol());
600 ld_plugin_symbol
&sym
= cf
.syms
.back();
601 sym
.version
= nullptr;
602 StringRef Name
= Sym
.getName();
603 sym
.name
= strdup(Name
.str().c_str());
605 ResolutionInfo
&Res
= ResInfo
[Name
];
607 Res
.CanOmitFromDynSym
&= Sym
.canBeOmittedFromSymbolTable();
609 sym
.visibility
= LDPV_DEFAULT
;
610 GlobalValue::VisibilityTypes Vis
= Sym
.getVisibility();
611 if (Vis
!= GlobalValue::DefaultVisibility
)
612 Res
.DefaultVisibility
= false;
614 case GlobalValue::DefaultVisibility
:
616 case GlobalValue::HiddenVisibility
:
617 sym
.visibility
= LDPV_HIDDEN
;
619 case GlobalValue::ProtectedVisibility
:
620 sym
.visibility
= LDPV_PROTECTED
;
624 if (Sym
.isUndefined()) {
625 sym
.def
= LDPK_UNDEF
;
627 sym
.def
= LDPK_WEAKUNDEF
;
628 } else if (Sym
.isCommon())
629 sym
.def
= LDPK_COMMON
;
630 else if (Sym
.isWeak())
631 sym
.def
= LDPK_WEAKDEF
;
636 sym
.comdat_key
= nullptr;
637 int CI
= Sym
.getComdatIndex();
639 // Not setting comdat_key for nodeduplicate ensuress we don't deduplicate.
640 std::pair
<StringRef
, Comdat::SelectionKind
> C
= Obj
->getComdatTable()[CI
];
641 if (C
.second
!= Comdat::NoDeduplicate
)
642 sym
.comdat_key
= strdup(C
.first
.str().c_str());
645 sym
.resolution
= LDPR_UNKNOWN
;
648 if (!cf
.syms
.empty()) {
649 if (add_symbols(cf
.handle
, cf
.syms
.size(), cf
.syms
.data()) != LDPS_OK
) {
650 message(LDPL_ERROR
, "Unable to add symbols!");
655 // Handle any --wrap options passed to gold, which are than passed
656 // along to the plugin.
657 if (get_wrap_symbols
) {
658 const char **wrap_symbols
;
660 if (get_wrap_symbols(&count
, &wrap_symbols
) != LDPS_OK
) {
661 message(LDPL_ERROR
, "Unable to get wrap symbols!");
664 for (uint64_t i
= 0; i
< count
; i
++) {
665 StringRef Name
= wrap_symbols
[i
];
666 ResolutionInfo
&Res
= ResInfo
[Name
];
667 ResolutionInfo
&WrapRes
= ResInfo
["__wrap_" + Name
.str()];
668 ResolutionInfo
&RealRes
= ResInfo
["__real_" + Name
.str()];
669 // Tell LTO not to inline symbols that will be overwritten.
670 Res
.CanInline
= false;
671 RealRes
.CanInline
= false;
672 // Tell LTO not to eliminate symbols that will be used after renaming.
673 Res
.IsUsedInRegularObj
= true;
674 WrapRes
.IsUsedInRegularObj
= true;
681 static void freeSymName(ld_plugin_symbol
&Sym
) {
683 free(Sym
.comdat_key
);
685 Sym
.comdat_key
= nullptr;
688 /// Helper to get a file's symbols and a view into it via gold callbacks.
689 static const void *getSymbolsAndView(claimed_file
&F
) {
690 ld_plugin_status status
= get_symbols(F
.handle
, F
.syms
.size(), F
.syms
.data());
691 if (status
== LDPS_NO_SYMS
)
694 if (status
!= LDPS_OK
)
695 message(LDPL_FATAL
, "Failed to get symbol information");
698 if (get_view(F
.handle
, &View
) != LDPS_OK
)
699 message(LDPL_FATAL
, "Failed to get a view of file");
704 /// Parse the thinlto-object-suffix-replace option into the \p OldSuffix and
705 /// \p NewSuffix strings, if it was specified.
706 static void getThinLTOOldAndNewSuffix(std::string
&OldSuffix
,
707 std::string
&NewSuffix
) {
708 assert(options::thinlto_object_suffix_replace
.empty() ||
709 options::thinlto_object_suffix_replace
.find(';') != StringRef::npos
);
710 StringRef SuffixReplace
= options::thinlto_object_suffix_replace
;
711 auto Split
= SuffixReplace
.split(';');
712 OldSuffix
= std::string(Split
.first
);
713 NewSuffix
= std::string(Split
.second
);
716 /// Given the original \p Path to an output file, replace any filename
717 /// suffix matching \p OldSuffix with \p NewSuffix.
718 static std::string
getThinLTOObjectFileName(StringRef Path
, StringRef OldSuffix
,
719 StringRef NewSuffix
) {
720 if (Path
.consume_back(OldSuffix
))
721 return (Path
+ NewSuffix
).str();
722 return std::string(Path
);
725 // Returns true if S is valid as a C language identifier.
726 static bool isValidCIdentifier(StringRef S
) {
727 return !S
.empty() && (isAlpha(S
[0]) || S
[0] == '_') &&
728 llvm::all_of(llvm::drop_begin(S
),
729 [](char C
) { return C
== '_' || isAlnum(C
); });
732 static bool isUndefined(ld_plugin_symbol
&Sym
) {
733 return Sym
.def
== LDPK_UNDEF
|| Sym
.def
== LDPK_WEAKUNDEF
;
736 static void addModule(LTO
&Lto
, claimed_file
&F
, const void *View
,
737 StringRef Filename
) {
738 MemoryBufferRef
BufferRef(StringRef((const char *)View
, F
.filesize
),
740 Expected
<std::unique_ptr
<InputFile
>> ObjOrErr
= InputFile::create(BufferRef
);
743 message(LDPL_FATAL
, "Could not read bitcode from file : %s",
744 toString(ObjOrErr
.takeError()).c_str());
747 std::unique_ptr
<InputFile
> Input
= std::move(ObjOrErr
.get());
748 auto InputFileSyms
= Input
->symbols();
749 assert(InputFileSyms
.size() == F
.syms
.size());
750 std::vector
<SymbolResolution
> Resols(F
.syms
.size());
751 for (ld_plugin_symbol
&Sym
: F
.syms
) {
752 const InputFile::Symbol
&InpSym
= InputFileSyms
[SymNum
];
753 SymbolResolution
&R
= Resols
[SymNum
++];
755 ld_plugin_symbol_resolution Resolution
=
756 (ld_plugin_symbol_resolution
)Sym
.resolution
;
758 ResolutionInfo
&Res
= ResInfo
[Sym
.name
];
760 switch (Resolution
) {
762 llvm_unreachable("Unexpected resolution");
764 case LDPR_RESOLVED_IR
:
765 case LDPR_RESOLVED_EXEC
:
766 case LDPR_PREEMPTED_IR
:
767 case LDPR_PREEMPTED_REG
:
771 case LDPR_RESOLVED_DYN
:
772 R
.ExportDynamic
= true;
775 case LDPR_PREVAILING_DEF_IRONLY
:
776 R
.Prevailing
= !isUndefined(Sym
);
779 case LDPR_PREVAILING_DEF
:
780 R
.Prevailing
= !isUndefined(Sym
);
781 R
.VisibleToRegularObj
= true;
784 case LDPR_PREVAILING_DEF_IRONLY_EXP
:
785 R
.Prevailing
= !isUndefined(Sym
);
786 // Identify symbols exported dynamically, and that therefore could be
787 // referenced by a shared library not visible to the linker.
788 R
.ExportDynamic
= true;
789 if (!Res
.CanOmitFromDynSym
)
790 R
.VisibleToRegularObj
= true;
794 // If the symbol has a C identifier section name, we need to mark
795 // it as visible to a regular object so that LTO will keep it around
796 // to ensure the linker generates special __start_<secname> and
797 // __stop_<secname> symbols which may be used elsewhere.
798 if (isValidCIdentifier(InpSym
.getSectionName()))
799 R
.VisibleToRegularObj
= true;
801 if (Resolution
!= LDPR_RESOLVED_DYN
&& Resolution
!= LDPR_UNDEF
&&
802 (IsExecutable
|| !Res
.DefaultVisibility
))
803 R
.FinalDefinitionInLinkageUnit
= true;
806 R
.LinkerRedefined
= true;
808 if (Res
.IsUsedInRegularObj
)
809 R
.VisibleToRegularObj
= true;
814 check(Lto
.add(std::move(Input
), Resols
),
815 std::string("Failed to link module ") + F
.name
);
818 static void recordFile(const std::string
&Filename
, bool TempOutFile
) {
819 if (add_input_file(Filename
.c_str()) != LDPS_OK
)
821 "Unable to add .o file to the link. File left behind in: %s",
824 Cleanup
.push_back(Filename
);
827 /// Return the desired output filename given a base input name, a flag
828 /// indicating whether a temp file should be generated, and an optional task id.
829 /// The new filename generated is returned in \p NewFilename.
830 static int getOutputFileName(StringRef InFilename
, bool TempOutFile
,
831 SmallString
<128> &NewFilename
, int TaskID
) {
835 sys::fs::createTemporaryFile("lto-llvm", "o", FD
, NewFilename
);
837 message(LDPL_FATAL
, "Could not create temporary file: %s",
838 EC
.message().c_str());
840 NewFilename
= InFilename
;
842 NewFilename
+= utostr(TaskID
);
844 sys::fs::openFileForWrite(NewFilename
, FD
, sys::fs::CD_CreateAlways
);
846 message(LDPL_FATAL
, "Could not open file %s: %s", NewFilename
.c_str(),
847 EC
.message().c_str());
852 /// Parse the thinlto_prefix_replace option into the \p OldPrefix and
853 /// \p NewPrefix strings, if it was specified.
854 static void getThinLTOOldAndNewPrefix(std::string
&OldPrefix
,
855 std::string
&NewPrefix
) {
856 StringRef PrefixReplace
= options::thinlto_prefix_replace
;
857 assert(PrefixReplace
.empty() || PrefixReplace
.find(';') != StringRef::npos
);
858 auto Split
= PrefixReplace
.split(';');
859 OldPrefix
= std::string(Split
.first
);
860 NewPrefix
= std::string(Split
.second
);
863 /// Creates instance of LTO.
864 /// OnIndexWrite is callback to let caller know when LTO writes index files.
865 /// LinkedObjectsFile is an output stream to write the list of object files for
866 /// the final ThinLTO linking. Can be nullptr.
867 static std::unique_ptr
<LTO
> createLTO(IndexWriteCallback OnIndexWrite
,
868 raw_fd_ostream
*LinkedObjectsFile
) {
872 Conf
.CPU
= options::mcpu
;
873 Conf
.Options
= codegen::InitTargetOptionsFromCodeGenFlags(Triple());
875 // Disable the new X86 relax relocations since gold might not support them.
876 // FIXME: Check the gold version or add a new option to enable them.
877 Conf
.Options
.MCOptions
.X86RelaxRelocations
= false;
879 // Toggle function/data sections.
880 if (!codegen::getExplicitFunctionSections())
881 Conf
.Options
.FunctionSections
= SplitSections
;
882 if (!codegen::getExplicitDataSections())
883 Conf
.Options
.DataSections
= SplitSections
;
885 Conf
.MAttrs
= codegen::getMAttrs();
886 Conf
.RelocModel
= RelocationModel
;
887 Conf
.CodeModel
= codegen::getExplicitCodeModel();
888 std::optional
<CodeGenOptLevel
> CGOptLevelOrNone
=
889 CodeGenOpt::getLevel(options::OptLevel
);
890 assert(CGOptLevelOrNone
&& "Invalid optimization level");
891 Conf
.CGOptLevel
= *CGOptLevelOrNone
;
892 Conf
.DisableVerify
= options::DisableVerify
;
893 Conf
.OptLevel
= options::OptLevel
;
894 Conf
.PTO
.LoopVectorization
= options::OptLevel
> 1;
895 Conf
.PTO
.SLPVectorization
= options::OptLevel
> 1;
896 Conf
.AlwaysEmitRegularLTOObj
= !options::obj_path
.empty();
898 if (options::thinlto_index_only
) {
899 std::string OldPrefix
, NewPrefix
;
900 getThinLTOOldAndNewPrefix(OldPrefix
, NewPrefix
);
901 Backend
= createWriteIndexesThinBackend(
902 llvm::hardware_concurrency(options::Parallelism
), OldPrefix
, NewPrefix
,
903 // TODO: Add support for optional native object path in
904 // thinlto_prefix_replace option to match lld.
905 /*NativeObjectPrefix=*/"", options::thinlto_emit_imports_files
,
906 LinkedObjectsFile
, OnIndexWrite
);
908 Backend
= createInProcessThinBackend(
909 llvm::heavyweight_hardware_concurrency(options::Parallelism
));
912 Conf
.OverrideTriple
= options::triple
;
913 Conf
.DefaultTriple
= sys::getDefaultTargetTriple();
915 Conf
.DiagHandler
= diagnosticHandler
;
917 switch (options::TheOutputType
) {
918 case options::OT_NORMAL
:
921 case options::OT_DISABLE
:
922 Conf
.PreOptModuleHook
= [](size_t Task
, const Module
&M
) { return false; };
925 case options::OT_BC_ONLY
:
926 Conf
.PostInternalizeModuleHook
= [](size_t Task
, const Module
&M
) {
928 SmallString
<128> TaskFilename
;
929 getOutputFileName(output_name
, /* TempOutFile */ false, TaskFilename
,
931 raw_fd_ostream
OS(TaskFilename
, EC
, sys::fs::OpenFlags::OF_None
);
933 message(LDPL_FATAL
, "Failed to write the output file.");
934 WriteBitcodeToFile(M
, OS
, /* ShouldPreserveUseListOrder */ false);
939 case options::OT_SAVE_TEMPS
:
940 check(Conf
.addSaveTemps(output_name
+ ".",
941 /* UseInputModulePath */ true));
943 case options::OT_ASM_ONLY
:
944 Conf
.CGFileType
= CodeGenFileType::AssemblyFile
;
945 Conf
.Options
.MCOptions
.AsmVerbose
= true;
949 if (!options::sample_profile
.empty())
950 Conf
.SampleProfile
= options::sample_profile
;
952 if (!options::cs_profile_path
.empty())
953 Conf
.CSIRProfile
= options::cs_profile_path
;
954 Conf
.RunCSIRInstr
= options::cs_pgo_gen
;
956 Conf
.DwoDir
= options::dwo_dir
;
958 // Set up optimization remarks handling.
959 Conf
.RemarksFilename
= options::RemarksFilename
;
960 Conf
.RemarksPasses
= options::RemarksPasses
;
961 Conf
.RemarksWithHotness
= options::RemarksWithHotness
;
962 Conf
.RemarksHotnessThreshold
= options::RemarksHotnessThreshold
;
963 Conf
.RemarksFormat
= options::RemarksFormat
;
965 // Debug new pass manager if requested
966 Conf
.DebugPassManager
= options::debug_pass_manager
;
968 Conf
.HasWholeProgramVisibility
= options::whole_program_visibility
;
970 Conf
.StatsFile
= options::stats_file
;
972 Conf
.TimeTraceEnabled
= !options::time_trace_file
.empty();
973 Conf
.TimeTraceGranularity
= options::time_trace_granularity
;
975 return std::make_unique
<LTO
>(std::move(Conf
), Backend
,
976 options::ParallelCodeGenParallelismLevel
);
979 // Write empty files that may be expected by a distributed build
980 // system when invoked with thinlto_index_only. This is invoked when
981 // the linker has decided not to include the given module in the
982 // final link. Frequently the distributed build system will want to
983 // confirm that all expected outputs are created based on all of the
984 // modules provided to the linker.
985 // If SkipModule is true then .thinlto.bc should contain just
986 // SkipModuleByDistributedBackend flag which requests distributed backend
987 // to skip the compilation of the corresponding module and produce an empty
989 static void writeEmptyDistributedBuildOutputs(const std::string
&ModulePath
,
990 const std::string
&OldPrefix
,
991 const std::string
&NewPrefix
,
993 std::string NewModulePath
=
994 getThinLTOOutputFile(ModulePath
, OldPrefix
, NewPrefix
);
997 raw_fd_ostream
OS(NewModulePath
+ ".thinlto.bc", EC
,
998 sys::fs::OpenFlags::OF_None
);
1000 message(LDPL_FATAL
, "Failed to write '%s': %s",
1001 (NewModulePath
+ ".thinlto.bc").c_str(), EC
.message().c_str());
1004 ModuleSummaryIndex
Index(/*HaveGVs*/ false);
1005 Index
.setSkipModuleByDistributedBackend();
1006 writeIndexToFile(Index
, OS
, nullptr);
1009 if (options::thinlto_emit_imports_files
) {
1010 raw_fd_ostream
OS(NewModulePath
+ ".imports", EC
,
1011 sys::fs::OpenFlags::OF_None
);
1013 message(LDPL_FATAL
, "Failed to write '%s': %s",
1014 (NewModulePath
+ ".imports").c_str(), EC
.message().c_str());
1018 // Creates and returns output stream with a list of object files for final
1019 // linking of distributed ThinLTO.
1020 static std::unique_ptr
<raw_fd_ostream
> CreateLinkedObjectsFile() {
1021 if (options::thinlto_linked_objects_file
.empty())
1023 assert(options::thinlto_index_only
);
1025 auto LinkedObjectsFile
= std::make_unique
<raw_fd_ostream
>(
1026 options::thinlto_linked_objects_file
, EC
, sys::fs::OpenFlags::OF_None
);
1028 message(LDPL_FATAL
, "Failed to create '%s': %s",
1029 options::thinlto_linked_objects_file
.c_str(), EC
.message().c_str());
1030 return LinkedObjectsFile
;
1033 /// Runs LTO and return a list of pairs <FileName, IsTemporary>.
1034 static std::vector
<std::pair
<SmallString
<128>, bool>> runLTO() {
1035 // Map to own RAII objects that manage the file opening and releasing
1036 // interfaces with gold. This is needed only for ThinLTO mode, since
1037 // unlike regular LTO, where addModule will result in the opened file
1038 // being merged into a new combined module, we need to keep these files open
1039 // through Lto->run().
1040 DenseMap
<void *, std::unique_ptr
<PluginInputFile
>> HandleToInputFile
;
1042 // Owns string objects and tells if index file was already created.
1043 StringMap
<bool> ObjectToIndexFileState
;
1045 std::unique_ptr
<raw_fd_ostream
> LinkedObjects
= CreateLinkedObjectsFile();
1046 std::unique_ptr
<LTO
> Lto
= createLTO(
1047 [&ObjectToIndexFileState
](const std::string
&Identifier
) {
1048 ObjectToIndexFileState
[Identifier
] = true;
1050 LinkedObjects
.get());
1052 std::string OldPrefix
, NewPrefix
;
1053 if (options::thinlto_index_only
)
1054 getThinLTOOldAndNewPrefix(OldPrefix
, NewPrefix
);
1056 std::string OldSuffix
, NewSuffix
;
1057 getThinLTOOldAndNewSuffix(OldSuffix
, NewSuffix
);
1059 for (claimed_file
&F
: Modules
) {
1060 if (options::thinlto
) {
1061 auto [It
, Inserted
] = HandleToInputFile
.try_emplace(F
.leader_handle
);
1063 It
->second
= std::make_unique
<PluginInputFile
>(F
.handle
);
1065 // In case we are thin linking with a minimized bitcode file, ensure
1066 // the module paths encoded in the index reflect where the backends
1067 // will locate the full bitcode files for compiling/importing.
1068 std::string Identifier
=
1069 getThinLTOObjectFileName(F
.name
, OldSuffix
, NewSuffix
);
1070 auto ObjFilename
= ObjectToIndexFileState
.insert({Identifier
, false});
1071 assert(ObjFilename
.second
);
1072 if (const void *View
= getSymbolsAndView(F
))
1073 addModule(*Lto
, F
, View
, ObjFilename
.first
->first());
1074 else if (options::thinlto_index_only
) {
1075 ObjFilename
.first
->second
= true;
1076 writeEmptyDistributedBuildOutputs(Identifier
, OldPrefix
, NewPrefix
,
1077 /* SkipModule */ true);
1081 SmallString
<128> Filename
;
1082 // Note that getOutputFileName will append a unique ID for each task
1083 if (!options::obj_path
.empty())
1084 Filename
= options::obj_path
;
1085 else if (options::TheOutputType
== options::OT_SAVE_TEMPS
)
1086 Filename
= output_name
+ ".lto.o";
1087 else if (options::TheOutputType
== options::OT_ASM_ONLY
)
1088 Filename
= output_name
;
1089 bool SaveTemps
= !Filename
.empty();
1091 size_t MaxTasks
= Lto
->getMaxTasks();
1092 std::vector
<std::pair
<SmallString
<128>, bool>> Files(MaxTasks
);
1096 const Twine
&ModuleName
) -> std::unique_ptr
<CachedFileStream
> {
1097 Files
[Task
].second
= !SaveTemps
;
1098 int FD
= getOutputFileName(Filename
, /* TempOutFile */ !SaveTemps
,
1099 Files
[Task
].first
, Task
);
1100 return std::make_unique
<CachedFileStream
>(
1101 std::make_unique
<llvm::raw_fd_ostream
>(FD
, true));
1104 auto AddBuffer
= [&](size_t Task
, const Twine
&moduleName
,
1105 std::unique_ptr
<MemoryBuffer
> MB
) {
1106 *AddStream(Task
, moduleName
)->OS
<< MB
->getBuffer();
1110 if (!options::cache_dir
.empty())
1111 Cache
= check(localCache("ThinLTO", "Thin", options::cache_dir
, AddBuffer
));
1113 check(Lto
->run(AddStream
, Cache
));
1115 // Write empty output files that may be expected by the distributed build
1117 if (options::thinlto_index_only
)
1118 for (auto &Identifier
: ObjectToIndexFileState
)
1119 if (!Identifier
.getValue())
1120 writeEmptyDistributedBuildOutputs(std::string(Identifier
.getKey()),
1121 OldPrefix
, NewPrefix
,
1122 /* SkipModule */ false);
1127 /// gold informs us that all symbols have been read. At this point, we use
1128 /// get_symbols to see if any of our definitions have been overridden by a
1129 /// native object file. Then, perform optimization and codegen.
1130 static ld_plugin_status
allSymbolsReadHook() {
1131 if (Modules
.empty())
1134 if (unsigned NumOpts
= options::extra
.size())
1135 cl::ParseCommandLineOptions(NumOpts
, &options::extra
[0]);
1137 // Initialize time trace profiler
1138 if (!options::time_trace_file
.empty())
1139 llvm::timeTraceProfilerInitialize(options::time_trace_granularity
,
1140 options::extra
.size() ? options::extra
[0]
1142 auto FinalizeTimeTrace
= llvm::make_scope_exit([&]() {
1143 if (!llvm::timeTraceProfilerEnabled())
1145 assert(!options::time_trace_file
.empty());
1146 check(llvm::timeTraceProfilerWrite(options::time_trace_file
, output_name
));
1147 llvm::timeTraceProfilerCleanup();
1150 std::vector
<std::pair
<SmallString
<128>, bool>> Files
= runLTO();
1152 if (options::TheOutputType
== options::OT_DISABLE
||
1153 options::TheOutputType
== options::OT_BC_ONLY
||
1154 options::TheOutputType
== options::OT_ASM_ONLY
)
1157 if (options::thinlto_index_only
) {
1163 for (const auto &F
: Files
)
1164 if (!F
.first
.empty())
1165 recordFile(std::string(F
.first
.str()), F
.second
);
1167 if (!options::extra_library_path
.empty() &&
1168 set_extra_library_path(options::extra_library_path
.c_str()) != LDPS_OK
)
1169 message(LDPL_FATAL
, "Unable to set the extra library path.");
1174 static ld_plugin_status
all_symbols_read_hook(void) {
1175 ld_plugin_status Ret
= allSymbolsReadHook();
1178 if (options::TheOutputType
== options::OT_BC_ONLY
||
1179 options::TheOutputType
== options::OT_ASM_ONLY
||
1180 options::TheOutputType
== options::OT_DISABLE
) {
1181 if (options::TheOutputType
== options::OT_DISABLE
) {
1182 // Remove the output file here since ld.bfd creates the output file
1184 std::error_code EC
= sys::fs::remove(output_name
);
1186 message(LDPL_ERROR
, "Failed to delete '%s': %s", output_name
.c_str(),
1187 EC
.message().c_str());
1195 static ld_plugin_status
cleanup_hook(void) {
1196 for (std::string
&Name
: Cleanup
) {
1197 std::error_code EC
= sys::fs::remove(Name
);
1199 message(LDPL_ERROR
, "Failed to delete '%s': %s", Name
.c_str(),
1200 EC
.message().c_str());
1204 if (!options::cache_dir
.empty()) {
1205 CachePruningPolicy policy
= check(parseCachePruningPolicy(options::cache_policy
));
1206 pruneCache(options::cache_dir
, policy
);