1/*===-- llvm-c/lto.h - LTO Public C Interface ---------------------*- C -*-===*\
2|* *|
3|* Part of the LLVM Project, under the Apache License v2.0 with LLVM *|
4|* Exceptions. *|
5|* See https://llvm.org/LICENSE.txt for license information. *|
6|* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception *|
7|* *|
8|*===----------------------------------------------------------------------===*|
9|* *|
10|* This header provides public interface to an abstract link time optimization*|
11|* library. LLVM provides an implementation of this interface for use with *|
12|* llvm bitcode files. *|
13|* *|
14\*===----------------------------------------------------------------------===*/
15
16#ifndef LLVM_C_LTO_H
17#define LLVM_C_LTO_H
18
19#include "llvm-c/ExternC.h"
20#include "llvm-c/Visibility.h"
21
22#ifdef __cplusplus
23#include <cstddef>
24#else
25#include <stddef.h>
26#endif
27#include <sys/types.h>
28
29#ifndef __cplusplus
30#if !defined(_MSC_VER)
31#include <stdbool.h>
32typedef bool lto_bool_t;
33#else
34/* MSVC in particular does not have anything like _Bool or bool in C, but we can
35 at least make sure the type is the same size. The implementation side will
36 use C++ bool. */
37typedef unsigned char lto_bool_t;
38#endif
39#else
40typedef bool lto_bool_t;
41#endif
42
43/**
44 * @defgroup LLVMCLTO LTO
45 * @ingroup LLVMC
46 *
47 * @{
48 */
49
50#define LTO_API_VERSION 30
51
52/**
53 * \since prior to LTO_API_VERSION=3
54 */
55typedef enum {
56 LTO_SYMBOL_ALIGNMENT_MASK = 0x0000001F, /* log2 of alignment */
57 LTO_SYMBOL_PERMISSIONS_MASK = 0x000000E0,
58 LTO_SYMBOL_PERMISSIONS_CODE = 0x000000A0,
59 LTO_SYMBOL_PERMISSIONS_DATA = 0x000000C0,
60 LTO_SYMBOL_PERMISSIONS_RODATA = 0x00000080,
61 LTO_SYMBOL_DEFINITION_MASK = 0x00000700,
62 LTO_SYMBOL_DEFINITION_REGULAR = 0x00000100,
63 LTO_SYMBOL_DEFINITION_TENTATIVE = 0x00000200,
64 LTO_SYMBOL_DEFINITION_WEAK = 0x00000300,
65 LTO_SYMBOL_DEFINITION_UNDEFINED = 0x00000400,
66 LTO_SYMBOL_DEFINITION_WEAKUNDEF = 0x00000500,
67 LTO_SYMBOL_SCOPE_MASK = 0x00003800,
68 LTO_SYMBOL_SCOPE_INTERNAL = 0x00000800,
69 LTO_SYMBOL_SCOPE_HIDDEN = 0x00001000,
70 LTO_SYMBOL_SCOPE_PROTECTED = 0x00002000,
71 LTO_SYMBOL_SCOPE_DEFAULT = 0x00001800,
72 LTO_SYMBOL_SCOPE_DEFAULT_CAN_BE_HIDDEN = 0x00002800,
73 LTO_SYMBOL_COMDAT = 0x00004000,
74 LTO_SYMBOL_ALIAS = 0x00008000
75} lto_symbol_attributes;
76
77/**
78 * \since prior to LTO_API_VERSION=3
79 */
80typedef enum {
81 LTO_DEBUG_MODEL_NONE = 0,
82 LTO_DEBUG_MODEL_DWARF = 1
83} lto_debug_model;
84
85/**
86 * \since prior to LTO_API_VERSION=3
87 */
88typedef enum {
89 LTO_CODEGEN_PIC_MODEL_STATIC = 0,
90 LTO_CODEGEN_PIC_MODEL_DYNAMIC = 1,
91 LTO_CODEGEN_PIC_MODEL_DYNAMIC_NO_PIC = 2,
92 LTO_CODEGEN_PIC_MODEL_DEFAULT = 3
93} lto_codegen_model;
94
95/** opaque reference to a loaded object module */
96typedef struct LLVMOpaqueLTOModule *lto_module_t;
97
98/** opaque reference to a code generator */
99typedef struct LLVMOpaqueLTOCodeGenerator *lto_code_gen_t;
100
101/** opaque reference to a thin code generator */
102typedef struct LLVMOpaqueThinLTOCodeGenerator *thinlto_code_gen_t;
103
104LLVM_C_EXTERN_C_BEGIN
105
106/**
107 * Returns a printable string.
108 *
109 * \since prior to LTO_API_VERSION=3
110 */
111LLVM_C_ABI extern const char *lto_get_version(void);
112
113/**
114 * Returns the last error string or NULL if last operation was successful.
115 *
116 * \since prior to LTO_API_VERSION=3
117 */
118LLVM_C_ABI extern const char *lto_get_error_message(void);
119
120/**
121 * Checks if a file is a loadable object file.
122 *
123 * \since prior to LTO_API_VERSION=3
124 */
125LLVM_C_ABI extern lto_bool_t lto_module_is_object_file(const char *path);
126
127/**
128 * Checks if a file is a loadable object compiled for requested target.
129 *
130 * \since prior to LTO_API_VERSION=3
131 */
132LLVM_C_ABI extern lto_bool_t
133lto_module_is_object_file_for_target(const char *path,
134 const char *target_triple_prefix);
135
136/**
137 * Return true if \p Buffer contains a bitcode file with ObjC code (category
138 * or class) in it.
139 *
140 * \since LTO_API_VERSION=20
141 */
142LLVM_C_ABI extern lto_bool_t lto_module_has_objc_category(const void *mem,
143 size_t length);
144
145/**
146 * Checks if a buffer is a loadable object file.
147 *
148 * \since prior to LTO_API_VERSION=3
149 */
150LLVM_C_ABI extern lto_bool_t
151lto_module_is_object_file_in_memory(const void *mem, size_t length);
152
153/**
154 * Checks if a buffer is a loadable object compiled for requested target.
155 *
156 * \since prior to LTO_API_VERSION=3
157 */
158LLVM_C_ABI extern lto_bool_t lto_module_is_object_file_in_memory_for_target(
159 const void *mem, size_t length, const char *target_triple_prefix);
160
161/**
162 * Loads an object file from disk.
163 * Returns NULL on error (check lto_get_error_message() for details).
164 *
165 * \since prior to LTO_API_VERSION=3
166 */
167LLVM_C_ABI extern lto_module_t lto_module_create(const char *path);
168
169/**
170 * Loads an object file from memory.
171 * Returns NULL on error (check lto_get_error_message() for details).
172 *
173 * \since prior to LTO_API_VERSION=3
174 */
175LLVM_C_ABI extern lto_module_t lto_module_create_from_memory(const void *mem,
176 size_t length);
177
178/**
179 * Loads an object file from memory with an extra path argument.
180 * Returns NULL on error (check lto_get_error_message() for details).
181 *
182 * \since LTO_API_VERSION=9
183 */
184LLVM_C_ABI extern lto_module_t
185lto_module_create_from_memory_with_path(const void *mem, size_t length,
186 const char *path);
187
188/**
189 * Loads an object file in its own context.
190 *
191 * Loads an object file in its own LLVMContext. This function call is
192 * thread-safe. However, modules created this way should not be merged into an
193 * lto_code_gen_t using \a lto_codegen_add_module().
194 *
195 * Returns NULL on error (check lto_get_error_message() for details).
196 *
197 * \since LTO_API_VERSION=11
198 */
199LLVM_C_ABI extern lto_module_t
200lto_module_create_in_local_context(const void *mem, size_t length,
201 const char *path);
202
203/**
204 * Loads an object file in the codegen context.
205 *
206 * Loads an object file into the same context as \c cg. The module is safe to
207 * add using \a lto_codegen_add_module().
208 *
209 * Returns NULL on error (check lto_get_error_message() for details).
210 *
211 * \since LTO_API_VERSION=11
212 */
213LLVM_C_ABI extern lto_module_t
214lto_module_create_in_codegen_context(const void *mem, size_t length,
215 const char *path, lto_code_gen_t cg);
216
217/**
218 * Loads an object file from disk. The seek point of fd is not preserved.
219 * Returns NULL on error (check lto_get_error_message() for details).
220 *
221 * \since LTO_API_VERSION=5
222 */
223LLVM_C_ABI extern lto_module_t
224lto_module_create_from_fd(int fd, const char *path, size_t file_size);
225
226/**
227 * Loads an object file from disk. The seek point of fd is not preserved.
228 * Returns NULL on error (check lto_get_error_message() for details).
229 *
230 * \since LTO_API_VERSION=5
231 */
232LLVM_C_ABI extern lto_module_t
233lto_module_create_from_fd_at_offset(int fd, const char *path, size_t file_size,
234 size_t map_size, off_t offset);
235
236/**
237 * Frees all memory internally allocated by the module.
238 * Upon return the lto_module_t is no longer valid.
239 *
240 * \since prior to LTO_API_VERSION=3
241 */
242LLVM_C_ABI extern void lto_module_dispose(lto_module_t mod);
243
244/**
245 * Returns triple string which the object module was compiled under.
246 *
247 * \since prior to LTO_API_VERSION=3
248 */
249LLVM_C_ABI extern const char *lto_module_get_target_triple(lto_module_t mod);
250
251/**
252 * Sets triple string with which the object will be codegened.
253 *
254 * \since LTO_API_VERSION=4
255 */
256LLVM_C_ABI extern void lto_module_set_target_triple(lto_module_t mod,
257 const char *triple);
258
259/**
260 * Returns the number of symbols in the object module.
261 *
262 * \since prior to LTO_API_VERSION=3
263 */
264LLVM_C_ABI extern unsigned int lto_module_get_num_symbols(lto_module_t mod);
265
266/**
267 * Returns the name of the ith symbol in the object module.
268 *
269 * \since prior to LTO_API_VERSION=3
270 */
271LLVM_C_ABI extern const char *lto_module_get_symbol_name(lto_module_t mod,
272 unsigned int index);
273
274/**
275 * Returns the attributes of the ith symbol in the object module.
276 *
277 * \since prior to LTO_API_VERSION=3
278 */
279LLVM_C_ABI extern lto_symbol_attributes
280lto_module_get_symbol_attribute(lto_module_t mod, unsigned int index);
281
282/**
283 * Returns the number of asm undefined symbols in the object module.
284 *
285 * \since prior to LTO_API_VERSION=30
286 */
287LLVM_C_ABI extern unsigned int
288lto_module_get_num_asm_undef_symbols(lto_module_t mod);
289
290/**
291 * Returns the name of the ith asm undefined symbol in the object module.
292 *
293 * \since prior to LTO_API_VERSION=30
294 */
295LLVM_C_ABI extern const char *
296lto_module_get_asm_undef_symbol_name(lto_module_t mod, unsigned int index);
297
298/**
299 * Returns the module's linker options.
300 *
301 * The linker options may consist of multiple flags. It is the linker's
302 * responsibility to split the flags using a platform-specific mechanism.
303 *
304 * \since LTO_API_VERSION=16
305 */
306LLVM_C_ABI extern const char *lto_module_get_linkeropts(lto_module_t mod);
307
308/**
309 * If targeting mach-o on darwin, this function gets the CPU type and subtype
310 * that will end up being encoded in the mach-o header. These are the values
311 * that can be found in mach/machine.h.
312 *
313 * \p out_cputype and \p out_cpusubtype must be non-NULL.
314 *
315 * Returns true on error (check lto_get_error_message() for details).
316 *
317 * \since LTO_API_VERSION=27
318 */
319LLVM_C_ABI extern lto_bool_t
320lto_module_get_macho_cputype(lto_module_t mod, unsigned int *out_cputype,
321 unsigned int *out_cpusubtype);
322
323/**
324 * This function can be used by the linker to check if a given module has
325 * any constructor or destructor functions.
326 *
327 * Returns true if the module has either the @llvm.global_ctors or the
328 * @llvm.global_dtors symbol. Otherwise returns false.
329 *
330 * \since LTO_API_VERSION=29
331 */
332LLVM_C_ABI extern lto_bool_t lto_module_has_ctor_dtor(lto_module_t mod);
333/**
334 * Diagnostic severity.
335 *
336 * \since LTO_API_VERSION=7
337 */
338typedef enum {
339 LTO_DS_ERROR = 0,
340 LTO_DS_WARNING = 1,
341 LTO_DS_REMARK = 3, // Added in LTO_API_VERSION=10.
342 LTO_DS_NOTE = 2
343} lto_codegen_diagnostic_severity_t;
344
345/**
346 * Diagnostic handler type.
347 * \p severity defines the severity.
348 * \p diag is the actual diagnostic.
349 * The diagnostic is not prefixed by any of severity keyword, e.g., 'error: '.
350 * \p ctxt is used to pass the context set with the diagnostic handler.
351 *
352 * \since LTO_API_VERSION=7
353 */
354typedef void (*lto_diagnostic_handler_t)(
355 lto_codegen_diagnostic_severity_t severity, const char *diag, void *ctxt);
356
357/**
358 * Set a diagnostic handler and the related context (void *).
359 * This is more general than lto_get_error_message, as the diagnostic handler
360 * can be called at anytime within lto.
361 *
362 * \since LTO_API_VERSION=7
363 */
364LLVM_C_ABI extern void
365lto_codegen_set_diagnostic_handler(lto_code_gen_t, lto_diagnostic_handler_t,
366 void *);
367
368/**
369 * Instantiates a code generator.
370 * Returns NULL on error (check lto_get_error_message() for details).
371 *
372 * All modules added using \a lto_codegen_add_module() must have been created
373 * in the same context as the codegen.
374 *
375 * \since prior to LTO_API_VERSION=3
376 */
377LLVM_C_ABI extern lto_code_gen_t lto_codegen_create(void);
378
379/**
380 * Instantiate a code generator in its own context.
381 *
382 * Instantiates a code generator in its own context. Modules added via \a
383 * lto_codegen_add_module() must have all been created in the same context,
384 * using \a lto_module_create_in_codegen_context().
385 *
386 * \since LTO_API_VERSION=11
387 */
388LLVM_C_ABI extern lto_code_gen_t lto_codegen_create_in_local_context(void);
389
390/**
391 * Frees all code generator and all memory it internally allocated.
392 * Upon return the lto_code_gen_t is no longer valid.
393 *
394 * \since prior to LTO_API_VERSION=3
395 */
396LLVM_C_ABI extern void lto_codegen_dispose(lto_code_gen_t);
397
398/**
399 * Add an object module to the set of modules for which code will be generated.
400 * Returns true on error (check lto_get_error_message() for details).
401 *
402 * \c cg and \c mod must both be in the same context. See \a
403 * lto_codegen_create_in_local_context() and \a
404 * lto_module_create_in_codegen_context().
405 *
406 * \since prior to LTO_API_VERSION=3
407 */
408LLVM_C_ABI extern lto_bool_t lto_codegen_add_module(lto_code_gen_t cg,
409 lto_module_t mod);
410
411/**
412 * Sets the object module for code generation. This will transfer the ownership
413 * of the module to the code generator.
414 *
415 * \c cg and \c mod must both be in the same context.
416 *
417 * \since LTO_API_VERSION=13
418 */
419LLVM_C_ABI extern void lto_codegen_set_module(lto_code_gen_t cg,
420 lto_module_t mod);
421
422/**
423 * Sets if debug info should be generated.
424 * Returns true on error (check lto_get_error_message() for details).
425 *
426 * \since prior to LTO_API_VERSION=3
427 */
428LLVM_C_ABI extern lto_bool_t lto_codegen_set_debug_model(lto_code_gen_t cg,
429 lto_debug_model);
430
431/**
432 * Sets which PIC code model to generated.
433 * Returns true on error (check lto_get_error_message() for details).
434 *
435 * \since prior to LTO_API_VERSION=3
436 */
437LLVM_C_ABI extern lto_bool_t lto_codegen_set_pic_model(lto_code_gen_t cg,
438 lto_codegen_model);
439
440/**
441 * Sets the cpu to generate code for.
442 *
443 * \since LTO_API_VERSION=4
444 */
445LLVM_C_ABI extern void lto_codegen_set_cpu(lto_code_gen_t cg, const char *cpu);
446
447/**
448 * Sets the location of the assembler tool to run. If not set, libLTO
449 * will use gcc to invoke the assembler.
450 *
451 * \since LTO_API_VERSION=3
452 */
453LLVM_C_ABI extern void lto_codegen_set_assembler_path(lto_code_gen_t cg,
454 const char *path);
455
456/**
457 * Sets extra arguments that libLTO should pass to the assembler.
458 *
459 * \since LTO_API_VERSION=4
460 */
461LLVM_C_ABI extern void
462lto_codegen_set_assembler_args(lto_code_gen_t cg, const char **args, int nargs);
463
464/**
465 * Adds to a list of all global symbols that must exist in the final generated
466 * code. If a function is not listed there, it might be inlined into every usage
467 * and optimized away.
468 *
469 * \since prior to LTO_API_VERSION=3
470 */
471LLVM_C_ABI extern void lto_codegen_add_must_preserve_symbol(lto_code_gen_t cg,
472 const char *symbol);
473
474/**
475 * Writes a new object file at the specified path that contains the
476 * merged contents of all modules added so far.
477 * Returns true on error (check lto_get_error_message() for details).
478 *
479 * \since LTO_API_VERSION=5
480 */
481LLVM_C_ABI extern lto_bool_t lto_codegen_write_merged_modules(lto_code_gen_t cg,
482 const char *path);
483
484/**
485 * Generates code for all added modules into one native object file.
486 * This calls lto_codegen_optimize then lto_codegen_compile_optimized.
487 *
488 * On success returns a pointer to a generated mach-o/ELF buffer and
489 * length set to the buffer size. The buffer is owned by the
490 * lto_code_gen_t and will be freed when lto_codegen_dispose()
491 * is called, or lto_codegen_compile() is called again.
492 * On failure, returns NULL (check lto_get_error_message() for details).
493 *
494 * \since prior to LTO_API_VERSION=3
495 */
496LLVM_C_ABI extern const void *lto_codegen_compile(lto_code_gen_t cg,
497 size_t *length);
498
499/**
500 * Generates code for all added modules into one native object file.
501 * This calls lto_codegen_optimize then lto_codegen_compile_optimized (instead
502 * of returning a generated mach-o/ELF buffer, it writes to a file).
503 *
504 * The name of the file is written to name. Returns true on error.
505 *
506 * \since LTO_API_VERSION=5
507 */
508LLVM_C_ABI extern lto_bool_t lto_codegen_compile_to_file(lto_code_gen_t cg,
509 const char **name);
510
511/**
512 * Runs optimization for the merged module. Returns true on error.
513 *
514 * \since LTO_API_VERSION=12
515 */
516LLVM_C_ABI extern lto_bool_t lto_codegen_optimize(lto_code_gen_t cg);
517
518/**
519 * Generates code for the optimized merged module into one native object file.
520 * It will not run any IR optimizations on the merged module.
521 *
522 * On success returns a pointer to a generated mach-o/ELF buffer and length set
523 * to the buffer size. The buffer is owned by the lto_code_gen_t and will be
524 * freed when lto_codegen_dispose() is called, or
525 * lto_codegen_compile_optimized() is called again. On failure, returns NULL
526 * (check lto_get_error_message() for details).
527 *
528 * \since LTO_API_VERSION=12
529 */
530LLVM_C_ABI extern const void *lto_codegen_compile_optimized(lto_code_gen_t cg,
531 size_t *length);
532
533/**
534 * Returns the runtime API version.
535 *
536 * \since LTO_API_VERSION=12
537 */
538LLVM_C_ABI extern unsigned int lto_api_version(void);
539
540/**
541 * Parses options immediately, making them available as early as possible. For
542 * example during executing codegen::InitTargetOptionsFromCodeGenFlags. Since
543 * parsing shud only happen once, only one of lto_codegen_debug_options or
544 * lto_set_debug_options should be called.
545 *
546 * This function takes one or more options separated by spaces.
547 * Warning: passing file paths through this function may confuse the argument
548 * parser if the paths contain spaces.
549 *
550 * \since LTO_API_VERSION=28
551 */
552LLVM_C_ABI extern void lto_set_debug_options(const char *const *options,
553 int number);
554
555/**
556 * Sets options to help debug codegen bugs. Since parsing shud only happen once,
557 * only one of lto_codegen_debug_options or lto_set_debug_options
558 * should be called.
559 *
560 * This function takes one or more options separated by spaces.
561 * Warning: passing file paths through this function may confuse the argument
562 * parser if the paths contain spaces.
563 *
564 * \since prior to LTO_API_VERSION=3
565 */
566LLVM_C_ABI extern void lto_codegen_debug_options(lto_code_gen_t cg,
567 const char *);
568
569/**
570 * Same as the previous function, but takes every option separately through an
571 * array.
572 *
573 * \since prior to LTO_API_VERSION=26
574 */
575LLVM_C_ABI extern void lto_codegen_debug_options_array(lto_code_gen_t cg,
576 const char *const *,
577 int number);
578
579/**
580 * Initializes LLVM disassemblers.
581 * FIXME: This doesn't really belong here.
582 *
583 * \since LTO_API_VERSION=5
584 */
585LLVM_C_ABI extern void lto_initialize_disassembler(void);
586
587/**
588 * Sets if we should run internalize pass during optimization and code
589 * generation.
590 *
591 * \since LTO_API_VERSION=14
592 */
593LLVM_C_ABI extern void
594lto_codegen_set_should_internalize(lto_code_gen_t cg,
595 lto_bool_t ShouldInternalize);
596
597/**
598 * Set whether to embed uselists in bitcode.
599 *
600 * Sets whether \a lto_codegen_write_merged_modules() should embed uselists in
601 * output bitcode. This should be turned on for all -save-temps output.
602 *
603 * \since LTO_API_VERSION=15
604 */
605LLVM_C_ABI extern void
606lto_codegen_set_should_embed_uselists(lto_code_gen_t cg,
607 lto_bool_t ShouldEmbedUselists);
608
609/** Opaque reference to an LTO input file */
610typedef struct LLVMOpaqueLTOInput *lto_input_t;
611
612/**
613 * Creates an LTO input file from a buffer. The path
614 * argument is used for diagnotics as this function
615 * otherwise does not know which file the given buffer
616 * is associated with.
617 *
618 * \since LTO_API_VERSION=24
619 */
620LLVM_C_ABI extern lto_input_t
621lto_input_create(const void *buffer, size_t buffer_size, const char *path);
622
623/**
624 * Frees all memory internally allocated by the LTO input file.
625 * Upon return the lto_module_t is no longer valid.
626 *
627 * \since LTO_API_VERSION=24
628 */
629LLVM_C_ABI extern void lto_input_dispose(lto_input_t input);
630
631/**
632 * Returns the number of dependent library specifiers
633 * for the given LTO input file.
634 *
635 * \since LTO_API_VERSION=24
636 */
637LLVM_C_ABI extern unsigned
638lto_input_get_num_dependent_libraries(lto_input_t input);
639
640/**
641 * Returns the ith dependent library specifier
642 * for the given LTO input file. The returned
643 * string is not null-terminated.
644 *
645 * \since LTO_API_VERSION=24
646 */
647LLVM_C_ABI extern const char *
648lto_input_get_dependent_library(lto_input_t input, size_t index, size_t *size);
649
650/**
651 * Returns the list of libcall symbols that can be generated by LTO
652 * that might not be visible from the symbol table of bitcode files.
653 *
654 * \since prior to LTO_API_VERSION=25
655 */
656LLVM_C_ABI extern const char *const *lto_runtime_lib_symbols_list(size_t *size);
657
658/**
659 * @} // endgoup LLVMCLTO
660 * @defgroup LLVMCTLTO ThinLTO
661 * @ingroup LLVMC
662 *
663 * @{
664 */
665
666/**
667 * Type to wrap a single object returned by ThinLTO.
668 *
669 * \since LTO_API_VERSION=18
670 */
671typedef struct {
672 const char *Buffer;
673 size_t Size;
674} LTOObjectBuffer;
675
676/**
677 * Instantiates a ThinLTO code generator.
678 * Returns NULL on error (check lto_get_error_message() for details).
679 *
680 *
681 * The ThinLTOCodeGenerator is not intended to be reuse for multiple
682 * compilation: the model is that the client adds modules to the generator and
683 * ask to perform the ThinLTO optimizations / codegen, and finally destroys the
684 * codegenerator.
685 *
686 * \since LTO_API_VERSION=18
687 */
688LLVM_C_ABI extern thinlto_code_gen_t thinlto_create_codegen(void);
689
690/**
691 * Frees the generator and all memory it internally allocated.
692 * Upon return the thinlto_code_gen_t is no longer valid.
693 *
694 * \since LTO_API_VERSION=18
695 */
696LLVM_C_ABI extern void thinlto_codegen_dispose(thinlto_code_gen_t cg);
697
698/**
699 * Add a module to a ThinLTO code generator. Identifier has to be unique among
700 * all the modules in a code generator. The data buffer stays owned by the
701 * client, and is expected to be available for the entire lifetime of the
702 * thinlto_code_gen_t it is added to.
703 *
704 * On failure, returns NULL (check lto_get_error_message() for details).
705 *
706 *
707 * \since LTO_API_VERSION=18
708 */
709LLVM_C_ABI extern void thinlto_codegen_add_module(thinlto_code_gen_t cg,
710 const char *identifier,
711 const char *data, int length);
712
713/**
714 * Optimize and codegen all the modules added to the codegenerator using
715 * ThinLTO. Resulting objects are accessible using thinlto_module_get_object().
716 *
717 * \since LTO_API_VERSION=18
718 */
719LLVM_C_ABI extern void thinlto_codegen_process(thinlto_code_gen_t cg);
720
721/**
722 * Returns the number of object files produced by the ThinLTO CodeGenerator.
723 *
724 * It usually matches the number of input files, but this is not a guarantee of
725 * the API and may change in future implementation, so the client should not
726 * assume it.
727 *
728 * \since LTO_API_VERSION=18
729 */
730LLVM_C_ABI extern unsigned int
731thinlto_module_get_num_objects(thinlto_code_gen_t cg);
732
733/**
734 * Returns a reference to the ith object file produced by the ThinLTO
735 * CodeGenerator.
736 *
737 * Client should use \p thinlto_module_get_num_objects() to get the number of
738 * available objects.
739 *
740 * \since LTO_API_VERSION=18
741 */
742LLVM_C_ABI extern LTOObjectBuffer
743thinlto_module_get_object(thinlto_code_gen_t cg, unsigned int index);
744
745/**
746 * Returns the number of object files produced by the ThinLTO CodeGenerator.
747 *
748 * It usually matches the number of input files, but this is not a guarantee of
749 * the API and may change in future implementation, so the client should not
750 * assume it.
751 *
752 * \since LTO_API_VERSION=21
753 */
754LLVM_C_ABI unsigned int
755thinlto_module_get_num_object_files(thinlto_code_gen_t cg);
756
757/**
758 * Returns the path to the ith object file produced by the ThinLTO
759 * CodeGenerator.
760 *
761 * Client should use \p thinlto_module_get_num_object_files() to get the number
762 * of available objects.
763 *
764 * \since LTO_API_VERSION=21
765 */
766LLVM_C_ABI const char *thinlto_module_get_object_file(thinlto_code_gen_t cg,
767 unsigned int index);
768
769/**
770 * Sets which PIC code model to generate.
771 * Returns true on error (check lto_get_error_message() for details).
772 *
773 * \since LTO_API_VERSION=18
774 */
775LLVM_C_ABI extern lto_bool_t
776thinlto_codegen_set_pic_model(thinlto_code_gen_t cg, lto_codegen_model);
777
778/**
779 * Sets the path to a directory to use as a storage for temporary bitcode files.
780 * The intention is to make the bitcode files available for debugging at various
781 * stage of the pipeline.
782 *
783 * \since LTO_API_VERSION=18
784 */
785LLVM_C_ABI extern void
786thinlto_codegen_set_savetemps_dir(thinlto_code_gen_t cg,
787 const char *save_temps_dir);
788
789/**
790 * Set the path to a directory where to save generated object files. This
791 * path can be used by a linker to request on-disk files instead of in-memory
792 * buffers. When set, results are available through
793 * thinlto_module_get_object_file() instead of thinlto_module_get_object().
794 *
795 * \since LTO_API_VERSION=21
796 */
797LLVM_C_ABI void thinlto_set_generated_objects_dir(thinlto_code_gen_t cg,
798 const char *save_temps_dir);
799
800/**
801 * Sets the cpu to generate code for.
802 *
803 * \since LTO_API_VERSION=18
804 */
805LLVM_C_ABI extern void thinlto_codegen_set_cpu(thinlto_code_gen_t cg,
806 const char *cpu);
807
808/**
809 * Disable CodeGen, only run the stages till codegen and stop. The output will
810 * be bitcode.
811 *
812 * \since LTO_API_VERSION=19
813 */
814LLVM_C_ABI extern void thinlto_codegen_disable_codegen(thinlto_code_gen_t cg,
815 lto_bool_t disable);
816
817/**
818 * Perform CodeGen only: disable all other stages.
819 *
820 * \since LTO_API_VERSION=19
821 */
822LLVM_C_ABI extern void
823thinlto_codegen_set_codegen_only(thinlto_code_gen_t cg,
824 lto_bool_t codegen_only);
825
826/**
827 * Parse -mllvm style debug options.
828 *
829 * \since LTO_API_VERSION=18
830 */
831LLVM_C_ABI extern void thinlto_debug_options(const char *const *options,
832 int number);
833
834/**
835 * Test if a module has support for ThinLTO linking.
836 *
837 * \since LTO_API_VERSION=18
838 */
839LLVM_C_ABI extern lto_bool_t lto_module_is_thinlto(lto_module_t mod);
840
841/**
842 * Adds a symbol to the list of global symbols that must exist in the final
843 * generated code. If a function is not listed there, it might be inlined into
844 * every usage and optimized away. For every single module, the functions
845 * referenced from code outside of the ThinLTO modules need to be added here.
846 *
847 * \since LTO_API_VERSION=18
848 */
849LLVM_C_ABI extern void
850thinlto_codegen_add_must_preserve_symbol(thinlto_code_gen_t cg,
851 const char *name, int length);
852
853/**
854 * Adds a symbol to the list of global symbols that are cross-referenced between
855 * ThinLTO files. If the ThinLTO CodeGenerator can ensure that every
856 * references from a ThinLTO module to this symbol is optimized away, then
857 * the symbol can be discarded.
858 *
859 * \since LTO_API_VERSION=18
860 */
861LLVM_C_ABI extern void
862thinlto_codegen_add_cross_referenced_symbol(thinlto_code_gen_t cg,
863 const char *name, int length);
864
865/**
866 * @} // endgoup LLVMCTLTO
867 * @defgroup LLVMCTLTO_CACHING ThinLTO Cache Control
868 * @ingroup LLVMCTLTO
869 *
870 * These entry points control the ThinLTO cache. The cache is intended to
871 * support incremental builds, and thus needs to be persistent across builds.
872 * The client enables the cache by supplying a path to an existing directory.
873 * The code generator will use this to store objects files that may be reused
874 * during a subsequent build.
875 * To avoid filling the disk space, a few knobs are provided:
876 * - The pruning interval limits the frequency at which the garbage collector
877 * will try to scan the cache directory to prune expired entries.
878 * Setting to a negative number disables the pruning.
879 * - The pruning expiration time indicates to the garbage collector how old an
880 * entry needs to be to be removed.
881 * - Finally, the garbage collector can be instructed to prune the cache until
882 * the occupied space goes below a threshold.
883 * @{
884 */
885
886/**
887 * Sets the path to a directory to use as a cache storage for incremental build.
888 * Setting this activates caching.
889 *
890 * \since LTO_API_VERSION=18
891 */
892LLVM_C_ABI extern void thinlto_codegen_set_cache_dir(thinlto_code_gen_t cg,
893 const char *cache_dir);
894
895/**
896 * Sets the cache pruning interval (in seconds). A negative value disables the
897 * pruning. An unspecified default value will be applied, and a value of 0 will
898 * force prunning to occur.
899 *
900 * \since LTO_API_VERSION=18
901 */
902LLVM_C_ABI extern void
903thinlto_codegen_set_cache_pruning_interval(thinlto_code_gen_t cg, int interval);
904
905/**
906 * Sets the maximum cache size that can be persistent across build, in terms of
907 * percentage of the available space on the disk. Set to 100 to indicate
908 * no limit, 50 to indicate that the cache size will not be left over half the
909 * available space. A value over 100 will be reduced to 100, a value of 0 will
910 * be ignored. An unspecified default value will be applied.
911 *
912 * The formula looks like:
913 * AvailableSpace = FreeSpace + ExistingCacheSize
914 * NewCacheSize = AvailableSpace * P/100
915 *
916 * \since LTO_API_VERSION=18
917 */
918LLVM_C_ABI extern void
919thinlto_codegen_set_final_cache_size_relative_to_available_space(
920 thinlto_code_gen_t cg, unsigned percentage);
921
922/**
923 * Sets the expiration (in seconds) for an entry in the cache. An unspecified
924 * default value will be applied. A value of 0 will be ignored.
925 *
926 * \since LTO_API_VERSION=18
927 */
928LLVM_C_ABI extern void
929thinlto_codegen_set_cache_entry_expiration(thinlto_code_gen_t cg,
930 unsigned expiration);
931
932/**
933 * Sets the maximum size of the cache directory (in bytes). A value over the
934 * amount of available space on the disk will be reduced to the amount of
935 * available space. An unspecified default value will be applied. A value of 0
936 * will be ignored.
937 *
938 * \since LTO_API_VERSION=22
939 */
940LLVM_C_ABI extern void
941thinlto_codegen_set_cache_size_bytes(thinlto_code_gen_t cg,
942 unsigned max_size_bytes);
943
944/**
945 * Same as thinlto_codegen_set_cache_size_bytes, except the maximum size is in
946 * megabytes (2^20 bytes).
947 *
948 * \since LTO_API_VERSION=23
949 */
950LLVM_C_ABI extern void
951thinlto_codegen_set_cache_size_megabytes(thinlto_code_gen_t cg,
952 unsigned max_size_megabytes);
953
954/**
955 * Sets the maximum number of files in the cache directory. An unspecified
956 * default value will be applied. A value of 0 will be ignored.
957 *
958 * \since LTO_API_VERSION=22
959 */
960LLVM_C_ABI extern void
961thinlto_codegen_set_cache_size_files(thinlto_code_gen_t cg,
962 unsigned max_size_files);
963
964/**
965 * @} // endgroup LLVMCTLTO_CACHING
966 */
967
968LLVM_C_EXTERN_C_END
969
970#endif /* LLVM_C_LTO_H */
971