1 //===-lto.cpp - LLVM Link Time Optimizer ----------------------------------===//
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 file implements the Link Time Optimization library. This library is
10 // intended to be used by linker to optimize code at link time.
12 //===----------------------------------------------------------------------===//
14 #include "llvm-c/lto.h"
15 #include "llvm/ADT/STLExtras.h"
16 #include "llvm/Bitcode/BitcodeReader.h"
17 #include "llvm/CodeGen/CommandFlags.inc"
18 #include "llvm/IR/DiagnosticInfo.h"
19 #include "llvm/IR/DiagnosticPrinter.h"
20 #include "llvm/IR/LLVMContext.h"
21 #include "llvm/LTO/LTO.h"
22 #include "llvm/LTO/legacy/LTOCodeGenerator.h"
23 #include "llvm/LTO/legacy/LTOModule.h"
24 #include "llvm/LTO/legacy/ThinLTOCodeGenerator.h"
25 #include "llvm/Support/MemoryBuffer.h"
26 #include "llvm/Support/Signals.h"
27 #include "llvm/Support/TargetSelect.h"
28 #include "llvm/Support/raw_ostream.h"
30 // extra command-line flags needed for LTOCodeGenerator
33 cl::desc("Optimization level. [-O0, -O1, -O2, or -O3] "
40 DisableInline("disable-inlining", cl::init(false),
41 cl::desc("Do not run the inliner pass"));
44 DisableGVNLoadPRE("disable-gvn-loadpre", cl::init(false),
45 cl::desc("Do not run the GVN load PRE pass"));
47 static cl::opt
<bool> DisableLTOVectorization(
48 "disable-lto-vectorization", cl::init(false),
49 cl::desc("Do not run loop or slp vectorization during LTO"));
51 static cl::opt
<bool> EnableFreestanding(
52 "lto-freestanding", cl::init(false),
53 cl::desc("Enable Freestanding (disable builtins / TLI) during LTO"));
56 static bool VerifyByDefault
= false;
58 static bool VerifyByDefault
= true;
61 static cl::opt
<bool> DisableVerify(
62 "disable-llvm-verifier", cl::init(!VerifyByDefault
),
63 cl::desc("Don't run the LLVM verifier during the optimization pipeline"));
65 // Holds most recent error string.
66 // *** Not thread safe ***
67 static std::string sLastErrorString
;
69 // Holds the initialization state of the LTO module.
70 // *** Not thread safe ***
71 static bool initialized
= false;
73 // Holds the command-line option parsing state of the LTO module.
74 static bool parsedOptions
= false;
76 static LLVMContext
*LTOContext
= nullptr;
78 struct LTOToolDiagnosticHandler
: public DiagnosticHandler
{
79 bool handleDiagnostics(const DiagnosticInfo
&DI
) override
{
80 if (DI
.getSeverity() != DS_Error
) {
81 DiagnosticPrinterRawOStream
DP(errs());
86 sLastErrorString
= "";
88 raw_string_ostream
Stream(sLastErrorString
);
89 DiagnosticPrinterRawOStream
DP(Stream
);
96 // Initialize the configured targets if they have not been initialized.
97 static void lto_initialize() {
100 // Dialog box on crash disabling doesn't work across DLL boundaries, so do
102 llvm::sys::DisableSystemDialogsOnCrash();
105 InitializeAllTargetInfos();
106 InitializeAllTargets();
107 InitializeAllTargetMCs();
108 InitializeAllAsmParsers();
109 InitializeAllAsmPrinters();
110 InitializeAllDisassemblers();
112 static LLVMContext Context
;
113 LTOContext
= &Context
;
114 LTOContext
->setDiagnosticHandler(
115 llvm::make_unique
<LTOToolDiagnosticHandler
>(), true);
122 static void handleLibLTODiagnostic(lto_codegen_diagnostic_severity_t Severity
,
123 const char *Msg
, void *) {
124 sLastErrorString
= Msg
;
127 // This derived class owns the native object file. This helps implement the
128 // libLTO API semantics, which require that the code generator owns the object
130 struct LibLTOCodeGenerator
: LTOCodeGenerator
{
131 LibLTOCodeGenerator() : LTOCodeGenerator(*LTOContext
) { init(); }
132 LibLTOCodeGenerator(std::unique_ptr
<LLVMContext
> Context
)
133 : LTOCodeGenerator(*Context
), OwnedContext(std::move(Context
)) {
137 // Reset the module first in case MergedModule is created in OwnedContext.
138 // Module must be destructed before its context gets destructed.
139 ~LibLTOCodeGenerator() { resetMergedModule(); }
141 void init() { setDiagnosticHandler(handleLibLTODiagnostic
, nullptr); }
143 std::unique_ptr
<MemoryBuffer
> NativeObjectFile
;
144 std::unique_ptr
<LLVMContext
> OwnedContext
;
149 DEFINE_SIMPLE_CONVERSION_FUNCTIONS(LibLTOCodeGenerator
, lto_code_gen_t
)
150 DEFINE_SIMPLE_CONVERSION_FUNCTIONS(ThinLTOCodeGenerator
, thinlto_code_gen_t
)
151 DEFINE_SIMPLE_CONVERSION_FUNCTIONS(LTOModule
, lto_module_t
)
153 // Convert the subtarget features into a string to pass to LTOCodeGenerator.
154 static void lto_add_attrs(lto_code_gen_t cg
) {
155 LTOCodeGenerator
*CG
= unwrap(cg
);
158 for (unsigned i
= 0; i
< MAttrs
.size(); ++i
) {
161 attrs
.append(MAttrs
[i
]);
167 if (OptLevel
< '0' || OptLevel
> '3')
168 report_fatal_error("Optimization level must be between 0 and 3");
169 CG
->setOptLevel(OptLevel
- '0');
170 CG
->setFreestanding(EnableFreestanding
);
173 extern const char* lto_get_version() {
174 return LTOCodeGenerator::getVersionString();
177 const char* lto_get_error_message() {
178 return sLastErrorString
.c_str();
181 bool lto_module_is_object_file(const char* path
) {
182 return LTOModule::isBitcodeFile(StringRef(path
));
185 bool lto_module_is_object_file_for_target(const char* path
,
186 const char* target_triplet_prefix
) {
187 ErrorOr
<std::unique_ptr
<MemoryBuffer
>> Buffer
= MemoryBuffer::getFile(path
);
190 return LTOModule::isBitcodeForTarget(Buffer
->get(),
191 StringRef(target_triplet_prefix
));
194 bool lto_module_has_objc_category(const void *mem
, size_t length
) {
195 std::unique_ptr
<MemoryBuffer
> Buffer(LTOModule::makeBuffer(mem
, length
));
199 ErrorOr
<bool> Result
= expectedToErrorOrAndEmitErrors(
200 Ctx
, llvm::isBitcodeContainingObjCCategory(*Buffer
));
201 return Result
&& *Result
;
204 bool lto_module_is_object_file_in_memory(const void* mem
, size_t length
) {
205 return LTOModule::isBitcodeFile(mem
, length
);
209 lto_module_is_object_file_in_memory_for_target(const void* mem
,
211 const char* target_triplet_prefix
) {
212 std::unique_ptr
<MemoryBuffer
> buffer(LTOModule::makeBuffer(mem
, length
));
215 return LTOModule::isBitcodeForTarget(buffer
.get(),
216 StringRef(target_triplet_prefix
));
219 lto_module_t
lto_module_create(const char* path
) {
221 llvm::TargetOptions Options
= InitTargetOptionsFromCodeGenFlags();
222 ErrorOr
<std::unique_ptr
<LTOModule
>> M
=
223 LTOModule::createFromFile(*LTOContext
, StringRef(path
), Options
);
226 return wrap(M
->release());
229 lto_module_t
lto_module_create_from_fd(int fd
, const char *path
, size_t size
) {
231 llvm::TargetOptions Options
= InitTargetOptionsFromCodeGenFlags();
232 ErrorOr
<std::unique_ptr
<LTOModule
>> M
= LTOModule::createFromOpenFile(
233 *LTOContext
, fd
, StringRef(path
), size
, Options
);
236 return wrap(M
->release());
239 lto_module_t
lto_module_create_from_fd_at_offset(int fd
, const char *path
,
244 llvm::TargetOptions Options
= InitTargetOptionsFromCodeGenFlags();
245 ErrorOr
<std::unique_ptr
<LTOModule
>> M
= LTOModule::createFromOpenFileSlice(
246 *LTOContext
, fd
, StringRef(path
), map_size
, offset
, Options
);
249 return wrap(M
->release());
252 lto_module_t
lto_module_create_from_memory(const void* mem
, size_t length
) {
254 llvm::TargetOptions Options
= InitTargetOptionsFromCodeGenFlags();
255 ErrorOr
<std::unique_ptr
<LTOModule
>> M
=
256 LTOModule::createFromBuffer(*LTOContext
, mem
, length
, Options
);
259 return wrap(M
->release());
262 lto_module_t
lto_module_create_from_memory_with_path(const void* mem
,
266 llvm::TargetOptions Options
= InitTargetOptionsFromCodeGenFlags();
267 ErrorOr
<std::unique_ptr
<LTOModule
>> M
= LTOModule::createFromBuffer(
268 *LTOContext
, mem
, length
, Options
, StringRef(path
));
271 return wrap(M
->release());
274 lto_module_t
lto_module_create_in_local_context(const void *mem
, size_t length
,
277 llvm::TargetOptions Options
= InitTargetOptionsFromCodeGenFlags();
279 // Create a local context. Ownership will be transferred to LTOModule.
280 std::unique_ptr
<LLVMContext
> Context
= llvm::make_unique
<LLVMContext
>();
281 Context
->setDiagnosticHandler(llvm::make_unique
<LTOToolDiagnosticHandler
>(),
284 ErrorOr
<std::unique_ptr
<LTOModule
>> M
= LTOModule::createInLocalContext(
285 std::move(Context
), mem
, length
, Options
, StringRef(path
));
288 return wrap(M
->release());
291 lto_module_t
lto_module_create_in_codegen_context(const void *mem
,
296 llvm::TargetOptions Options
= InitTargetOptionsFromCodeGenFlags();
297 ErrorOr
<std::unique_ptr
<LTOModule
>> M
= LTOModule::createFromBuffer(
298 unwrap(cg
)->getContext(), mem
, length
, Options
, StringRef(path
));
299 return wrap(M
->release());
302 void lto_module_dispose(lto_module_t mod
) { delete unwrap(mod
); }
304 const char* lto_module_get_target_triple(lto_module_t mod
) {
305 return unwrap(mod
)->getTargetTriple().c_str();
308 void lto_module_set_target_triple(lto_module_t mod
, const char *triple
) {
309 return unwrap(mod
)->setTargetTriple(StringRef(triple
));
312 unsigned int lto_module_get_num_symbols(lto_module_t mod
) {
313 return unwrap(mod
)->getSymbolCount();
316 const char* lto_module_get_symbol_name(lto_module_t mod
, unsigned int index
) {
317 return unwrap(mod
)->getSymbolName(index
).data();
320 lto_symbol_attributes
lto_module_get_symbol_attribute(lto_module_t mod
,
321 unsigned int index
) {
322 return unwrap(mod
)->getSymbolAttributes(index
);
325 const char* lto_module_get_linkeropts(lto_module_t mod
) {
326 return unwrap(mod
)->getLinkerOpts().data();
329 void lto_codegen_set_diagnostic_handler(lto_code_gen_t cg
,
330 lto_diagnostic_handler_t diag_handler
,
332 unwrap(cg
)->setDiagnosticHandler(diag_handler
, ctxt
);
335 static lto_code_gen_t
createCodeGen(bool InLocalContext
) {
338 TargetOptions Options
= InitTargetOptionsFromCodeGenFlags();
340 LibLTOCodeGenerator
*CodeGen
=
341 InLocalContext
? new LibLTOCodeGenerator(make_unique
<LLVMContext
>())
342 : new LibLTOCodeGenerator();
343 CodeGen
->setTargetOptions(Options
);
344 return wrap(CodeGen
);
347 lto_code_gen_t
lto_codegen_create(void) {
348 return createCodeGen(/* InLocalContext */ false);
351 lto_code_gen_t
lto_codegen_create_in_local_context(void) {
352 return createCodeGen(/* InLocalContext */ true);
355 void lto_codegen_dispose(lto_code_gen_t cg
) { delete unwrap(cg
); }
357 bool lto_codegen_add_module(lto_code_gen_t cg
, lto_module_t mod
) {
358 return !unwrap(cg
)->addModule(unwrap(mod
));
361 void lto_codegen_set_module(lto_code_gen_t cg
, lto_module_t mod
) {
362 unwrap(cg
)->setModule(std::unique_ptr
<LTOModule
>(unwrap(mod
)));
365 bool lto_codegen_set_debug_model(lto_code_gen_t cg
, lto_debug_model debug
) {
366 unwrap(cg
)->setDebugInfo(debug
);
370 bool lto_codegen_set_pic_model(lto_code_gen_t cg
, lto_codegen_model model
) {
372 case LTO_CODEGEN_PIC_MODEL_STATIC
:
373 unwrap(cg
)->setCodePICModel(Reloc::Static
);
375 case LTO_CODEGEN_PIC_MODEL_DYNAMIC
:
376 unwrap(cg
)->setCodePICModel(Reloc::PIC_
);
378 case LTO_CODEGEN_PIC_MODEL_DYNAMIC_NO_PIC
:
379 unwrap(cg
)->setCodePICModel(Reloc::DynamicNoPIC
);
381 case LTO_CODEGEN_PIC_MODEL_DEFAULT
:
382 unwrap(cg
)->setCodePICModel(None
);
385 sLastErrorString
= "Unknown PIC model";
389 void lto_codegen_set_cpu(lto_code_gen_t cg
, const char *cpu
) {
390 return unwrap(cg
)->setCpu(cpu
);
393 void lto_codegen_set_assembler_path(lto_code_gen_t cg
, const char *path
) {
394 // In here only for backwards compatibility. We use MC now.
397 void lto_codegen_set_assembler_args(lto_code_gen_t cg
, const char **args
,
399 // In here only for backwards compatibility. We use MC now.
402 void lto_codegen_add_must_preserve_symbol(lto_code_gen_t cg
,
403 const char *symbol
) {
404 unwrap(cg
)->addMustPreserveSymbol(symbol
);
407 static void maybeParseOptions(lto_code_gen_t cg
) {
408 if (!parsedOptions
) {
409 unwrap(cg
)->parseCodeGenDebugOptions();
411 parsedOptions
= true;
415 bool lto_codegen_write_merged_modules(lto_code_gen_t cg
, const char *path
) {
416 maybeParseOptions(cg
);
417 return !unwrap(cg
)->writeMergedModules(path
);
420 const void *lto_codegen_compile(lto_code_gen_t cg
, size_t *length
) {
421 maybeParseOptions(cg
);
422 LibLTOCodeGenerator
*CG
= unwrap(cg
);
423 CG
->NativeObjectFile
=
424 CG
->compile(DisableVerify
, DisableInline
, DisableGVNLoadPRE
,
425 DisableLTOVectorization
);
426 if (!CG
->NativeObjectFile
)
428 *length
= CG
->NativeObjectFile
->getBufferSize();
429 return CG
->NativeObjectFile
->getBufferStart();
432 bool lto_codegen_optimize(lto_code_gen_t cg
) {
433 maybeParseOptions(cg
);
434 return !unwrap(cg
)->optimize(DisableVerify
, DisableInline
, DisableGVNLoadPRE
,
435 DisableLTOVectorization
);
438 const void *lto_codegen_compile_optimized(lto_code_gen_t cg
, size_t *length
) {
439 maybeParseOptions(cg
);
440 LibLTOCodeGenerator
*CG
= unwrap(cg
);
441 CG
->NativeObjectFile
= CG
->compileOptimized();
442 if (!CG
->NativeObjectFile
)
444 *length
= CG
->NativeObjectFile
->getBufferSize();
445 return CG
->NativeObjectFile
->getBufferStart();
448 bool lto_codegen_compile_to_file(lto_code_gen_t cg
, const char **name
) {
449 maybeParseOptions(cg
);
450 return !unwrap(cg
)->compile_to_file(
451 name
, DisableVerify
, DisableInline
, DisableGVNLoadPRE
,
452 DisableLTOVectorization
);
455 void lto_codegen_debug_options(lto_code_gen_t cg
, const char *opt
) {
456 unwrap(cg
)->setCodeGenDebugOptions(opt
);
459 unsigned int lto_api_version() { return LTO_API_VERSION
; }
461 void lto_codegen_set_should_internalize(lto_code_gen_t cg
,
462 bool ShouldInternalize
) {
463 unwrap(cg
)->setShouldInternalize(ShouldInternalize
);
466 void lto_codegen_set_should_embed_uselists(lto_code_gen_t cg
,
467 lto_bool_t ShouldEmbedUselists
) {
468 unwrap(cg
)->setShouldEmbedUselists(ShouldEmbedUselists
);
473 thinlto_code_gen_t
thinlto_create_codegen(void) {
475 ThinLTOCodeGenerator
*CodeGen
= new ThinLTOCodeGenerator();
476 CodeGen
->setTargetOptions(InitTargetOptionsFromCodeGenFlags());
477 CodeGen
->setFreestanding(EnableFreestanding
);
479 if (OptLevel
.getNumOccurrences()) {
480 if (OptLevel
< '0' || OptLevel
> '3')
481 report_fatal_error("Optimization level must be between 0 and 3");
482 CodeGen
->setOptLevel(OptLevel
- '0');
485 CodeGen
->setCodeGenOptLevel(CodeGenOpt::None
);
488 CodeGen
->setCodeGenOptLevel(CodeGenOpt::Less
);
491 CodeGen
->setCodeGenOptLevel(CodeGenOpt::Default
);
494 CodeGen
->setCodeGenOptLevel(CodeGenOpt::Aggressive
);
498 return wrap(CodeGen
);
501 void thinlto_codegen_dispose(thinlto_code_gen_t cg
) { delete unwrap(cg
); }
503 void thinlto_codegen_add_module(thinlto_code_gen_t cg
, const char *Identifier
,
504 const char *Data
, int Length
) {
505 unwrap(cg
)->addModule(Identifier
, StringRef(Data
, Length
));
508 void thinlto_codegen_process(thinlto_code_gen_t cg
) { unwrap(cg
)->run(); }
510 unsigned int thinlto_module_get_num_objects(thinlto_code_gen_t cg
) {
511 return unwrap(cg
)->getProducedBinaries().size();
513 LTOObjectBuffer
thinlto_module_get_object(thinlto_code_gen_t cg
,
514 unsigned int index
) {
515 assert(index
< unwrap(cg
)->getProducedBinaries().size() && "Index overflow");
516 auto &MemBuffer
= unwrap(cg
)->getProducedBinaries()[index
];
517 return LTOObjectBuffer
{MemBuffer
->getBufferStart(),
518 MemBuffer
->getBufferSize()};
521 unsigned int thinlto_module_get_num_object_files(thinlto_code_gen_t cg
) {
522 return unwrap(cg
)->getProducedBinaryFiles().size();
524 const char *thinlto_module_get_object_file(thinlto_code_gen_t cg
,
525 unsigned int index
) {
526 assert(index
< unwrap(cg
)->getProducedBinaryFiles().size() &&
528 return unwrap(cg
)->getProducedBinaryFiles()[index
].c_str();
531 void thinlto_codegen_disable_codegen(thinlto_code_gen_t cg
,
532 lto_bool_t disable
) {
533 unwrap(cg
)->disableCodeGen(disable
);
536 void thinlto_codegen_set_codegen_only(thinlto_code_gen_t cg
,
537 lto_bool_t CodeGenOnly
) {
538 unwrap(cg
)->setCodeGenOnly(CodeGenOnly
);
541 void thinlto_debug_options(const char *const *options
, int number
) {
542 // if options were requested, set them
543 if (number
&& options
) {
544 std::vector
<const char *> CodegenArgv(1, "libLTO");
545 for (auto Arg
: ArrayRef
<const char *>(options
, number
))
546 CodegenArgv
.push_back(Arg
);
547 cl::ParseCommandLineOptions(CodegenArgv
.size(), CodegenArgv
.data());
551 lto_bool_t
lto_module_is_thinlto(lto_module_t mod
) {
552 return unwrap(mod
)->isThinLTO();
555 void thinlto_codegen_add_must_preserve_symbol(thinlto_code_gen_t cg
,
556 const char *Name
, int Length
) {
557 unwrap(cg
)->preserveSymbol(StringRef(Name
, Length
));
560 void thinlto_codegen_add_cross_referenced_symbol(thinlto_code_gen_t cg
,
561 const char *Name
, int Length
) {
562 unwrap(cg
)->crossReferenceSymbol(StringRef(Name
, Length
));
565 void thinlto_codegen_set_cpu(thinlto_code_gen_t cg
, const char *cpu
) {
566 return unwrap(cg
)->setCpu(cpu
);
569 void thinlto_codegen_set_cache_dir(thinlto_code_gen_t cg
,
570 const char *cache_dir
) {
571 return unwrap(cg
)->setCacheDir(cache_dir
);
574 void thinlto_codegen_set_cache_pruning_interval(thinlto_code_gen_t cg
,
576 return unwrap(cg
)->setCachePruningInterval(interval
);
579 void thinlto_codegen_set_cache_entry_expiration(thinlto_code_gen_t cg
,
580 unsigned expiration
) {
581 return unwrap(cg
)->setCacheEntryExpiration(expiration
);
584 void thinlto_codegen_set_final_cache_size_relative_to_available_space(
585 thinlto_code_gen_t cg
, unsigned Percentage
) {
586 return unwrap(cg
)->setMaxCacheSizeRelativeToAvailableSpace(Percentage
);
589 void thinlto_codegen_set_cache_size_bytes(
590 thinlto_code_gen_t cg
, unsigned MaxSizeBytes
) {
591 return unwrap(cg
)->setCacheMaxSizeBytes(MaxSizeBytes
);
594 void thinlto_codegen_set_cache_size_megabytes(
595 thinlto_code_gen_t cg
, unsigned MaxSizeMegabytes
) {
596 uint64_t MaxSizeBytes
= MaxSizeMegabytes
;
597 MaxSizeBytes
*= 1024 * 1024;
598 return unwrap(cg
)->setCacheMaxSizeBytes(MaxSizeBytes
);
601 void thinlto_codegen_set_cache_size_files(
602 thinlto_code_gen_t cg
, unsigned MaxSizeFiles
) {
603 return unwrap(cg
)->setCacheMaxSizeFiles(MaxSizeFiles
);
606 void thinlto_codegen_set_savetemps_dir(thinlto_code_gen_t cg
,
607 const char *save_temps_dir
) {
608 return unwrap(cg
)->setSaveTempsDir(save_temps_dir
);
611 void thinlto_set_generated_objects_dir(thinlto_code_gen_t cg
,
612 const char *save_temps_dir
) {
613 unwrap(cg
)->setGeneratedObjectsDirectory(save_temps_dir
);
616 lto_bool_t
thinlto_codegen_set_pic_model(thinlto_code_gen_t cg
,
617 lto_codegen_model model
) {
619 case LTO_CODEGEN_PIC_MODEL_STATIC
:
620 unwrap(cg
)->setCodePICModel(Reloc::Static
);
622 case LTO_CODEGEN_PIC_MODEL_DYNAMIC
:
623 unwrap(cg
)->setCodePICModel(Reloc::PIC_
);
625 case LTO_CODEGEN_PIC_MODEL_DYNAMIC_NO_PIC
:
626 unwrap(cg
)->setCodePICModel(Reloc::DynamicNoPIC
);
628 case LTO_CODEGEN_PIC_MODEL_DEFAULT
:
629 unwrap(cg
)->setCodePICModel(None
);
632 sLastErrorString
= "Unknown PIC model";
636 DEFINE_SIMPLE_CONVERSION_FUNCTIONS(lto::InputFile
, lto_input_t
)
638 lto_input_t
lto_input_create(const void *buffer
, size_t buffer_size
, const char *path
) {
639 return wrap(LTOModule::createInputFile(buffer
, buffer_size
, path
, sLastErrorString
));
642 void lto_input_dispose(lto_input_t input
) {
643 delete unwrap(input
);
646 extern unsigned lto_input_get_num_dependent_libraries(lto_input_t input
) {
647 return LTOModule::getDependentLibraryCount(unwrap(input
));
650 extern const char *lto_input_get_dependent_library(lto_input_t input
,
653 return LTOModule::getDependentLibrary(unwrap(input
), index
, size
);