Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:43:01

0001 /*===-- llvm-c/lto.h - LTO Public C Interface ---------------------*- C -*-===*\
0002 |*                                                                            *|
0003 |* Part of the LLVM Project, under the Apache License v2.0 with LLVM          *|
0004 |* Exceptions.                                                                *|
0005 |* See https://llvm.org/LICENSE.txt for license information.                  *|
0006 |* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception                    *|
0007 |*                                                                            *|
0008 |*===----------------------------------------------------------------------===*|
0009 |*                                                                            *|
0010 |* This header provides public interface to an abstract link time optimization*|
0011 |* library.  LLVM provides an implementation of this interface for use with   *|
0012 |* llvm bitcode files.                                                        *|
0013 |*                                                                            *|
0014 \*===----------------------------------------------------------------------===*/
0015 
0016 #ifndef LLVM_C_LTO_H
0017 #define LLVM_C_LTO_H
0018 
0019 #include "llvm-c/ExternC.h"
0020 
0021 #ifdef __cplusplus
0022 #include <cstddef>
0023 #else
0024 #include <stddef.h>
0025 #endif
0026 #include <sys/types.h>
0027 
0028 #ifndef __cplusplus
0029 #if !defined(_MSC_VER)
0030 #include <stdbool.h>
0031 typedef bool lto_bool_t;
0032 #else
0033 /* MSVC in particular does not have anything like _Bool or bool in C, but we can
0034    at least make sure the type is the same size.  The implementation side will
0035    use C++ bool. */
0036 typedef unsigned char lto_bool_t;
0037 #endif
0038 #else
0039 typedef bool lto_bool_t;
0040 #endif
0041 
0042 /**
0043  * @defgroup LLVMCLTO LTO
0044  * @ingroup LLVMC
0045  *
0046  * @{
0047  */
0048 
0049 #define LTO_API_VERSION 29
0050 
0051 /**
0052  * \since prior to LTO_API_VERSION=3
0053  */
0054 typedef enum {
0055     LTO_SYMBOL_ALIGNMENT_MASK              = 0x0000001F, /* log2 of alignment */
0056     LTO_SYMBOL_PERMISSIONS_MASK            = 0x000000E0,
0057     LTO_SYMBOL_PERMISSIONS_CODE            = 0x000000A0,
0058     LTO_SYMBOL_PERMISSIONS_DATA            = 0x000000C0,
0059     LTO_SYMBOL_PERMISSIONS_RODATA          = 0x00000080,
0060     LTO_SYMBOL_DEFINITION_MASK             = 0x00000700,
0061     LTO_SYMBOL_DEFINITION_REGULAR          = 0x00000100,
0062     LTO_SYMBOL_DEFINITION_TENTATIVE        = 0x00000200,
0063     LTO_SYMBOL_DEFINITION_WEAK             = 0x00000300,
0064     LTO_SYMBOL_DEFINITION_UNDEFINED        = 0x00000400,
0065     LTO_SYMBOL_DEFINITION_WEAKUNDEF        = 0x00000500,
0066     LTO_SYMBOL_SCOPE_MASK                  = 0x00003800,
0067     LTO_SYMBOL_SCOPE_INTERNAL              = 0x00000800,
0068     LTO_SYMBOL_SCOPE_HIDDEN                = 0x00001000,
0069     LTO_SYMBOL_SCOPE_PROTECTED             = 0x00002000,
0070     LTO_SYMBOL_SCOPE_DEFAULT               = 0x00001800,
0071     LTO_SYMBOL_SCOPE_DEFAULT_CAN_BE_HIDDEN = 0x00002800,
0072     LTO_SYMBOL_COMDAT                      = 0x00004000,
0073     LTO_SYMBOL_ALIAS                       = 0x00008000
0074 } lto_symbol_attributes;
0075 
0076 /**
0077  * \since prior to LTO_API_VERSION=3
0078  */
0079 typedef enum {
0080     LTO_DEBUG_MODEL_NONE         = 0,
0081     LTO_DEBUG_MODEL_DWARF        = 1
0082 } lto_debug_model;
0083 
0084 /**
0085  * \since prior to LTO_API_VERSION=3
0086  */
0087 typedef enum {
0088     LTO_CODEGEN_PIC_MODEL_STATIC         = 0,
0089     LTO_CODEGEN_PIC_MODEL_DYNAMIC        = 1,
0090     LTO_CODEGEN_PIC_MODEL_DYNAMIC_NO_PIC = 2,
0091     LTO_CODEGEN_PIC_MODEL_DEFAULT        = 3
0092 } lto_codegen_model;
0093 
0094 /** opaque reference to a loaded object module */
0095 typedef struct LLVMOpaqueLTOModule *lto_module_t;
0096 
0097 /** opaque reference to a code generator */
0098 typedef struct LLVMOpaqueLTOCodeGenerator *lto_code_gen_t;
0099 
0100 /** opaque reference to a thin code generator */
0101 typedef struct LLVMOpaqueThinLTOCodeGenerator *thinlto_code_gen_t;
0102 
0103 LLVM_C_EXTERN_C_BEGIN
0104 
0105 /**
0106  * Returns a printable string.
0107  *
0108  * \since prior to LTO_API_VERSION=3
0109  */
0110 extern const char*
0111 lto_get_version(void);
0112 
0113 /**
0114  * Returns the last error string or NULL if last operation was successful.
0115  *
0116  * \since prior to LTO_API_VERSION=3
0117  */
0118 extern const char*
0119 lto_get_error_message(void);
0120 
0121 /**
0122  * Checks if a file is a loadable object file.
0123  *
0124  * \since prior to LTO_API_VERSION=3
0125  */
0126 extern lto_bool_t
0127 lto_module_is_object_file(const char* path);
0128 
0129 /**
0130  * Checks if a file is a loadable object compiled for requested target.
0131  *
0132  * \since prior to LTO_API_VERSION=3
0133  */
0134 extern lto_bool_t
0135 lto_module_is_object_file_for_target(const char* path,
0136                                      const char* target_triple_prefix);
0137 
0138 /**
0139  * Return true if \p Buffer contains a bitcode file with ObjC code (category
0140  * or class) in it.
0141  *
0142  * \since LTO_API_VERSION=20
0143  */
0144 extern lto_bool_t
0145 lto_module_has_objc_category(const void *mem, size_t length);
0146 
0147 /**
0148  * Checks if a buffer is a loadable object file.
0149  *
0150  * \since prior to LTO_API_VERSION=3
0151  */
0152 extern lto_bool_t lto_module_is_object_file_in_memory(const void *mem,
0153                                                       size_t length);
0154 
0155 /**
0156  * Checks if a buffer is a loadable object compiled for requested target.
0157  *
0158  * \since prior to LTO_API_VERSION=3
0159  */
0160 extern lto_bool_t
0161 lto_module_is_object_file_in_memory_for_target(const void* mem, size_t length,
0162                                               const char* target_triple_prefix);
0163 
0164 /**
0165  * Loads an object file from disk.
0166  * Returns NULL on error (check lto_get_error_message() for details).
0167  *
0168  * \since prior to LTO_API_VERSION=3
0169  */
0170 extern lto_module_t
0171 lto_module_create(const char* path);
0172 
0173 /**
0174  * Loads an object file from memory.
0175  * Returns NULL on error (check lto_get_error_message() for details).
0176  *
0177  * \since prior to LTO_API_VERSION=3
0178  */
0179 extern lto_module_t
0180 lto_module_create_from_memory(const void* mem, size_t length);
0181 
0182 /**
0183  * Loads an object file from memory with an extra path argument.
0184  * Returns NULL on error (check lto_get_error_message() for details).
0185  *
0186  * \since LTO_API_VERSION=9
0187  */
0188 extern lto_module_t
0189 lto_module_create_from_memory_with_path(const void* mem, size_t length,
0190                                         const char *path);
0191 
0192 /**
0193  * Loads an object file in its own context.
0194  *
0195  * Loads an object file in its own LLVMContext.  This function call is
0196  * thread-safe.  However, modules created this way should not be merged into an
0197  * lto_code_gen_t using \a lto_codegen_add_module().
0198  *
0199  * Returns NULL on error (check lto_get_error_message() for details).
0200  *
0201  * \since LTO_API_VERSION=11
0202  */
0203 extern lto_module_t
0204 lto_module_create_in_local_context(const void *mem, size_t length,
0205                                    const char *path);
0206 
0207 /**
0208  * Loads an object file in the codegen context.
0209  *
0210  * Loads an object file into the same context as \c cg.  The module is safe to
0211  * add using \a lto_codegen_add_module().
0212  *
0213  * Returns NULL on error (check lto_get_error_message() for details).
0214  *
0215  * \since LTO_API_VERSION=11
0216  */
0217 extern lto_module_t
0218 lto_module_create_in_codegen_context(const void *mem, size_t length,
0219                                      const char *path, lto_code_gen_t cg);
0220 
0221 /**
0222  * Loads an object file from disk. The seek point of fd is not preserved.
0223  * Returns NULL on error (check lto_get_error_message() for details).
0224  *
0225  * \since LTO_API_VERSION=5
0226  */
0227 extern lto_module_t
0228 lto_module_create_from_fd(int fd, const char *path, size_t file_size);
0229 
0230 /**
0231  * Loads an object file from disk. The seek point of fd is not preserved.
0232  * Returns NULL on error (check lto_get_error_message() for details).
0233  *
0234  * \since LTO_API_VERSION=5
0235  */
0236 extern lto_module_t
0237 lto_module_create_from_fd_at_offset(int fd, const char *path, size_t file_size,
0238                                     size_t map_size, off_t offset);
0239 
0240 /**
0241  * Frees all memory internally allocated by the module.
0242  * Upon return the lto_module_t is no longer valid.
0243  *
0244  * \since prior to LTO_API_VERSION=3
0245  */
0246 extern void
0247 lto_module_dispose(lto_module_t mod);
0248 
0249 /**
0250  * Returns triple string which the object module was compiled under.
0251  *
0252  * \since prior to LTO_API_VERSION=3
0253  */
0254 extern const char*
0255 lto_module_get_target_triple(lto_module_t mod);
0256 
0257 /**
0258  * Sets triple string with which the object will be codegened.
0259  *
0260  * \since LTO_API_VERSION=4
0261  */
0262 extern void
0263 lto_module_set_target_triple(lto_module_t mod, const char *triple);
0264 
0265 /**
0266  * Returns the number of symbols in the object module.
0267  *
0268  * \since prior to LTO_API_VERSION=3
0269  */
0270 extern unsigned int
0271 lto_module_get_num_symbols(lto_module_t mod);
0272 
0273 /**
0274  * Returns the name of the ith symbol in the object module.
0275  *
0276  * \since prior to LTO_API_VERSION=3
0277  */
0278 extern const char*
0279 lto_module_get_symbol_name(lto_module_t mod, unsigned int index);
0280 
0281 /**
0282  * Returns the attributes of the ith symbol in the object module.
0283  *
0284  * \since prior to LTO_API_VERSION=3
0285  */
0286 extern lto_symbol_attributes
0287 lto_module_get_symbol_attribute(lto_module_t mod, unsigned int index);
0288 
0289 /**
0290  * Returns the module's linker options.
0291  *
0292  * The linker options may consist of multiple flags. It is the linker's
0293  * responsibility to split the flags using a platform-specific mechanism.
0294  *
0295  * \since LTO_API_VERSION=16
0296  */
0297 extern const char*
0298 lto_module_get_linkeropts(lto_module_t mod);
0299 
0300 /**
0301  * If targeting mach-o on darwin, this function gets the CPU type and subtype
0302  * that will end up being encoded in the mach-o header. These are the values
0303  * that can be found in mach/machine.h.
0304  *
0305  * \p out_cputype and \p out_cpusubtype must be non-NULL.
0306  *
0307  * Returns true on error (check lto_get_error_message() for details).
0308  *
0309  * \since LTO_API_VERSION=27
0310  */
0311 extern lto_bool_t lto_module_get_macho_cputype(lto_module_t mod,
0312                                                unsigned int *out_cputype,
0313                                                unsigned int *out_cpusubtype);
0314 
0315 /**
0316  * This function can be used by the linker to check if a given module has
0317  * any constructor or destructor functions.
0318  *
0319  * Returns true if the module has either the @llvm.global_ctors or the
0320  * @llvm.global_dtors symbol. Otherwise returns false.
0321  *
0322  * \since LTO_API_VERSION=29
0323  */
0324 extern lto_bool_t lto_module_has_ctor_dtor(lto_module_t mod);
0325 /**
0326  * Diagnostic severity.
0327  *
0328  * \since LTO_API_VERSION=7
0329  */
0330 typedef enum {
0331   LTO_DS_ERROR = 0,
0332   LTO_DS_WARNING = 1,
0333   LTO_DS_REMARK = 3, // Added in LTO_API_VERSION=10.
0334   LTO_DS_NOTE = 2
0335 } lto_codegen_diagnostic_severity_t;
0336 
0337 /**
0338  * Diagnostic handler type.
0339  * \p severity defines the severity.
0340  * \p diag is the actual diagnostic.
0341  * The diagnostic is not prefixed by any of severity keyword, e.g., 'error: '.
0342  * \p ctxt is used to pass the context set with the diagnostic handler.
0343  *
0344  * \since LTO_API_VERSION=7
0345  */
0346 typedef void (*lto_diagnostic_handler_t)(
0347     lto_codegen_diagnostic_severity_t severity, const char *diag, void *ctxt);
0348 
0349 /**
0350  * Set a diagnostic handler and the related context (void *).
0351  * This is more general than lto_get_error_message, as the diagnostic handler
0352  * can be called at anytime within lto.
0353  *
0354  * \since LTO_API_VERSION=7
0355  */
0356 extern void lto_codegen_set_diagnostic_handler(lto_code_gen_t,
0357                                                lto_diagnostic_handler_t,
0358                                                void *);
0359 
0360 /**
0361  * Instantiates a code generator.
0362  * Returns NULL on error (check lto_get_error_message() for details).
0363  *
0364  * All modules added using \a lto_codegen_add_module() must have been created
0365  * in the same context as the codegen.
0366  *
0367  * \since prior to LTO_API_VERSION=3
0368  */
0369 extern lto_code_gen_t
0370 lto_codegen_create(void);
0371 
0372 /**
0373  * Instantiate a code generator in its own context.
0374  *
0375  * Instantiates a code generator in its own context.  Modules added via \a
0376  * lto_codegen_add_module() must have all been created in the same context,
0377  * using \a lto_module_create_in_codegen_context().
0378  *
0379  * \since LTO_API_VERSION=11
0380  */
0381 extern lto_code_gen_t
0382 lto_codegen_create_in_local_context(void);
0383 
0384 /**
0385  * Frees all code generator and all memory it internally allocated.
0386  * Upon return the lto_code_gen_t is no longer valid.
0387  *
0388  * \since prior to LTO_API_VERSION=3
0389  */
0390 extern void
0391 lto_codegen_dispose(lto_code_gen_t);
0392 
0393 /**
0394  * Add an object module to the set of modules for which code will be generated.
0395  * Returns true on error (check lto_get_error_message() for details).
0396  *
0397  * \c cg and \c mod must both be in the same context.  See \a
0398  * lto_codegen_create_in_local_context() and \a
0399  * lto_module_create_in_codegen_context().
0400  *
0401  * \since prior to LTO_API_VERSION=3
0402  */
0403 extern lto_bool_t
0404 lto_codegen_add_module(lto_code_gen_t cg, lto_module_t mod);
0405 
0406 /**
0407  * Sets the object module for code generation. This will transfer the ownership
0408  * of the module to the code generator.
0409  *
0410  * \c cg and \c mod must both be in the same context.
0411  *
0412  * \since LTO_API_VERSION=13
0413  */
0414 extern void
0415 lto_codegen_set_module(lto_code_gen_t cg, lto_module_t mod);
0416 
0417 /**
0418  * Sets if debug info should be generated.
0419  * Returns true on error (check lto_get_error_message() for details).
0420  *
0421  * \since prior to LTO_API_VERSION=3
0422  */
0423 extern lto_bool_t
0424 lto_codegen_set_debug_model(lto_code_gen_t cg, lto_debug_model);
0425 
0426 /**
0427  * Sets which PIC code model to generated.
0428  * Returns true on error (check lto_get_error_message() for details).
0429  *
0430  * \since prior to LTO_API_VERSION=3
0431  */
0432 extern lto_bool_t
0433 lto_codegen_set_pic_model(lto_code_gen_t cg, lto_codegen_model);
0434 
0435 /**
0436  * Sets the cpu to generate code for.
0437  *
0438  * \since LTO_API_VERSION=4
0439  */
0440 extern void
0441 lto_codegen_set_cpu(lto_code_gen_t cg, const char *cpu);
0442 
0443 /**
0444  * Sets the location of the assembler tool to run. If not set, libLTO
0445  * will use gcc to invoke the assembler.
0446  *
0447  * \since LTO_API_VERSION=3
0448  */
0449 extern void
0450 lto_codegen_set_assembler_path(lto_code_gen_t cg, const char* path);
0451 
0452 /**
0453  * Sets extra arguments that libLTO should pass to the assembler.
0454  *
0455  * \since LTO_API_VERSION=4
0456  */
0457 extern void
0458 lto_codegen_set_assembler_args(lto_code_gen_t cg, const char **args,
0459                                int nargs);
0460 
0461 /**
0462  * Adds to a list of all global symbols that must exist in the final generated
0463  * code. If a function is not listed there, it might be inlined into every usage
0464  * and optimized away.
0465  *
0466  * \since prior to LTO_API_VERSION=3
0467  */
0468 extern void
0469 lto_codegen_add_must_preserve_symbol(lto_code_gen_t cg, const char* symbol);
0470 
0471 /**
0472  * Writes a new object file at the specified path that contains the
0473  * merged contents of all modules added so far.
0474  * Returns true on error (check lto_get_error_message() for details).
0475  *
0476  * \since LTO_API_VERSION=5
0477  */
0478 extern lto_bool_t
0479 lto_codegen_write_merged_modules(lto_code_gen_t cg, const char* path);
0480 
0481 /**
0482  * Generates code for all added modules into one native object file.
0483  * This calls lto_codegen_optimize then lto_codegen_compile_optimized.
0484  *
0485  * On success returns a pointer to a generated mach-o/ELF buffer and
0486  * length set to the buffer size.  The buffer is owned by the
0487  * lto_code_gen_t and will be freed when lto_codegen_dispose()
0488  * is called, or lto_codegen_compile() is called again.
0489  * On failure, returns NULL (check lto_get_error_message() for details).
0490  *
0491  * \since prior to LTO_API_VERSION=3
0492  */
0493 extern const void*
0494 lto_codegen_compile(lto_code_gen_t cg, size_t* length);
0495 
0496 /**
0497  * Generates code for all added modules into one native object file.
0498  * This calls lto_codegen_optimize then lto_codegen_compile_optimized (instead
0499  * of returning a generated mach-o/ELF buffer, it writes to a file).
0500  *
0501  * The name of the file is written to name. Returns true on error.
0502  *
0503  * \since LTO_API_VERSION=5
0504  */
0505 extern lto_bool_t
0506 lto_codegen_compile_to_file(lto_code_gen_t cg, const char** name);
0507 
0508 /**
0509  * Runs optimization for the merged module. Returns true on error.
0510  *
0511  * \since LTO_API_VERSION=12
0512  */
0513 extern lto_bool_t
0514 lto_codegen_optimize(lto_code_gen_t cg);
0515 
0516 /**
0517  * Generates code for the optimized merged module into one native object file.
0518  * It will not run any IR optimizations on the merged module.
0519  *
0520  * On success returns a pointer to a generated mach-o/ELF buffer and length set
0521  * to the buffer size.  The buffer is owned by the lto_code_gen_t and will be
0522  * freed when lto_codegen_dispose() is called, or
0523  * lto_codegen_compile_optimized() is called again. On failure, returns NULL
0524  * (check lto_get_error_message() for details).
0525  *
0526  * \since LTO_API_VERSION=12
0527  */
0528 extern const void*
0529 lto_codegen_compile_optimized(lto_code_gen_t cg, size_t* length);
0530 
0531 /**
0532  * Returns the runtime API version.
0533  *
0534  * \since LTO_API_VERSION=12
0535  */
0536 extern unsigned int
0537 lto_api_version(void);
0538 
0539 /**
0540  * Parses options immediately, making them available as early as possible. For
0541  * example during executing codegen::InitTargetOptionsFromCodeGenFlags. Since
0542  * parsing shud only happen once, only one of lto_codegen_debug_options or
0543  * lto_set_debug_options should be called.
0544  *
0545  * This function takes one or more options separated by spaces.
0546  * Warning: passing file paths through this function may confuse the argument
0547  * parser if the paths contain spaces.
0548  *
0549  * \since LTO_API_VERSION=28
0550  */
0551 extern void lto_set_debug_options(const char *const *options, int number);
0552 
0553 /**
0554  * Sets options to help debug codegen bugs. Since parsing shud only happen once,
0555  * only one of lto_codegen_debug_options or lto_set_debug_options
0556  * should be called.
0557  *
0558  * This function takes one or more options separated by spaces.
0559  * Warning: passing file paths through this function may confuse the argument
0560  * parser if the paths contain spaces.
0561  *
0562  * \since prior to LTO_API_VERSION=3
0563  */
0564 extern void
0565 lto_codegen_debug_options(lto_code_gen_t cg, const char *);
0566 
0567 /**
0568  * Same as the previous function, but takes every option separately through an
0569  * array.
0570  *
0571  * \since prior to LTO_API_VERSION=26
0572  */
0573 extern void lto_codegen_debug_options_array(lto_code_gen_t cg,
0574                                             const char *const *, int number);
0575 
0576 /**
0577  * Initializes LLVM disassemblers.
0578  * FIXME: This doesn't really belong here.
0579  *
0580  * \since LTO_API_VERSION=5
0581  */
0582 extern void
0583 lto_initialize_disassembler(void);
0584 
0585 /**
0586  * Sets if we should run internalize pass during optimization and code
0587  * generation.
0588  *
0589  * \since LTO_API_VERSION=14
0590  */
0591 extern void
0592 lto_codegen_set_should_internalize(lto_code_gen_t cg,
0593                                    lto_bool_t ShouldInternalize);
0594 
0595 /**
0596  * Set whether to embed uselists in bitcode.
0597  *
0598  * Sets whether \a lto_codegen_write_merged_modules() should embed uselists in
0599  * output bitcode.  This should be turned on for all -save-temps output.
0600  *
0601  * \since LTO_API_VERSION=15
0602  */
0603 extern void
0604 lto_codegen_set_should_embed_uselists(lto_code_gen_t cg,
0605                                       lto_bool_t ShouldEmbedUselists);
0606 
0607 /** Opaque reference to an LTO input file */
0608 typedef struct LLVMOpaqueLTOInput *lto_input_t;
0609 
0610 /**
0611   * Creates an LTO input file from a buffer. The path
0612   * argument is used for diagnotics as this function
0613   * otherwise does not know which file the given buffer
0614   * is associated with.
0615   *
0616   * \since LTO_API_VERSION=24
0617   */
0618 extern lto_input_t lto_input_create(const void *buffer,
0619                                     size_t buffer_size,
0620                                     const char *path);
0621 
0622 /**
0623   * Frees all memory internally allocated by the LTO input file.
0624   * Upon return the lto_module_t is no longer valid.
0625   *
0626   * \since LTO_API_VERSION=24
0627   */
0628 extern void lto_input_dispose(lto_input_t input);
0629 
0630 /**
0631   * Returns the number of dependent library specifiers
0632   * for the given LTO input file.
0633   *
0634   * \since LTO_API_VERSION=24
0635   */
0636 extern unsigned lto_input_get_num_dependent_libraries(lto_input_t input);
0637 
0638 /**
0639   * Returns the ith dependent library specifier
0640   * for the given LTO input file. The returned
0641   * string is not null-terminated.
0642   *
0643   * \since LTO_API_VERSION=24
0644   */
0645 extern const char * lto_input_get_dependent_library(lto_input_t input,
0646                                                     size_t index,
0647                                                     size_t *size);
0648 
0649 /**
0650  * Returns the list of libcall symbols that can be generated by LTO
0651  * that might not be visible from the symbol table of bitcode files.
0652  *
0653  * \since prior to LTO_API_VERSION=25
0654  */
0655 extern const char *const *lto_runtime_lib_symbols_list(size_t *size);
0656 
0657 /**
0658  * @} // endgoup LLVMCLTO
0659  * @defgroup LLVMCTLTO ThinLTO
0660  * @ingroup LLVMC
0661  *
0662  * @{
0663  */
0664 
0665 /**
0666  * Type to wrap a single object returned by ThinLTO.
0667  *
0668  * \since LTO_API_VERSION=18
0669  */
0670 typedef struct {
0671   const char *Buffer;
0672   size_t Size;
0673 } LTOObjectBuffer;
0674 
0675 /**
0676  * Instantiates a ThinLTO code generator.
0677  * Returns NULL on error (check lto_get_error_message() for details).
0678  *
0679  *
0680  * The ThinLTOCodeGenerator is not intended to be reuse for multiple
0681  * compilation: the model is that the client adds modules to the generator and
0682  * ask to perform the ThinLTO optimizations / codegen, and finally destroys the
0683  * codegenerator.
0684  *
0685  * \since LTO_API_VERSION=18
0686  */
0687 extern thinlto_code_gen_t thinlto_create_codegen(void);
0688 
0689 /**
0690  * Frees the generator and all memory it internally allocated.
0691  * Upon return the thinlto_code_gen_t is no longer valid.
0692  *
0693  * \since LTO_API_VERSION=18
0694  */
0695 extern void thinlto_codegen_dispose(thinlto_code_gen_t cg);
0696 
0697 /**
0698  * Add a module to a ThinLTO code generator. Identifier has to be unique among
0699  * all the modules in a code generator. The data buffer stays owned by the
0700  * client, and is expected to be available for the entire lifetime of the
0701  * thinlto_code_gen_t it is added to.
0702  *
0703  * On failure, returns NULL (check lto_get_error_message() for details).
0704  *
0705  *
0706  * \since LTO_API_VERSION=18
0707  */
0708 extern void thinlto_codegen_add_module(thinlto_code_gen_t cg,
0709                                        const char *identifier, const char *data,
0710                                        int length);
0711 
0712 /**
0713  * Optimize and codegen all the modules added to the codegenerator using
0714  * ThinLTO. Resulting objects are accessible using thinlto_module_get_object().
0715  *
0716  * \since LTO_API_VERSION=18
0717  */
0718 extern void thinlto_codegen_process(thinlto_code_gen_t cg);
0719 
0720 /**
0721  * Returns the number of object files produced by the ThinLTO CodeGenerator.
0722  *
0723  * It usually matches the number of input files, but this is not a guarantee of
0724  * the API and may change in future implementation, so the client should not
0725  * assume it.
0726  *
0727  * \since LTO_API_VERSION=18
0728  */
0729 extern unsigned int thinlto_module_get_num_objects(thinlto_code_gen_t cg);
0730 
0731 /**
0732  * Returns a reference to the ith object file produced by the ThinLTO
0733  * CodeGenerator.
0734  *
0735  * Client should use \p thinlto_module_get_num_objects() to get the number of
0736  * available objects.
0737  *
0738  * \since LTO_API_VERSION=18
0739  */
0740 extern LTOObjectBuffer thinlto_module_get_object(thinlto_code_gen_t cg,
0741                                                  unsigned int index);
0742 
0743 /**
0744  * Returns the number of object files produced by the ThinLTO CodeGenerator.
0745  *
0746  * It usually matches the number of input files, but this is not a guarantee of
0747  * the API and may change in future implementation, so the client should not
0748  * assume it.
0749  *
0750  * \since LTO_API_VERSION=21
0751  */
0752 unsigned int thinlto_module_get_num_object_files(thinlto_code_gen_t cg);
0753 
0754 /**
0755  * Returns the path to the ith object file produced by the ThinLTO
0756  * CodeGenerator.
0757  *
0758  * Client should use \p thinlto_module_get_num_object_files() to get the number
0759  * of available objects.
0760  *
0761  * \since LTO_API_VERSION=21
0762  */
0763 const char *thinlto_module_get_object_file(thinlto_code_gen_t cg,
0764                                            unsigned int index);
0765 
0766 /**
0767  * Sets which PIC code model to generate.
0768  * Returns true on error (check lto_get_error_message() for details).
0769  *
0770  * \since LTO_API_VERSION=18
0771  */
0772 extern lto_bool_t thinlto_codegen_set_pic_model(thinlto_code_gen_t cg,
0773                                                 lto_codegen_model);
0774 
0775 /**
0776  * Sets the path to a directory to use as a storage for temporary bitcode files.
0777  * The intention is to make the bitcode files available for debugging at various
0778  * stage of the pipeline.
0779  *
0780  * \since LTO_API_VERSION=18
0781  */
0782 extern void thinlto_codegen_set_savetemps_dir(thinlto_code_gen_t cg,
0783                                               const char *save_temps_dir);
0784 
0785 /**
0786  * Set the path to a directory where to save generated object files. This
0787  * path can be used by a linker to request on-disk files instead of in-memory
0788  * buffers. When set, results are available through
0789  * thinlto_module_get_object_file() instead of thinlto_module_get_object().
0790  *
0791  * \since LTO_API_VERSION=21
0792  */
0793 void thinlto_set_generated_objects_dir(thinlto_code_gen_t cg,
0794                                        const char *save_temps_dir);
0795 
0796 /**
0797  * Sets the cpu to generate code for.
0798  *
0799  * \since LTO_API_VERSION=18
0800  */
0801 extern void thinlto_codegen_set_cpu(thinlto_code_gen_t cg, const char *cpu);
0802 
0803 /**
0804  * Disable CodeGen, only run the stages till codegen and stop. The output will
0805  * be bitcode.
0806  *
0807  * \since LTO_API_VERSION=19
0808  */
0809 extern void thinlto_codegen_disable_codegen(thinlto_code_gen_t cg,
0810                                             lto_bool_t disable);
0811 
0812 /**
0813  * Perform CodeGen only: disable all other stages.
0814  *
0815  * \since LTO_API_VERSION=19
0816  */
0817 extern void thinlto_codegen_set_codegen_only(thinlto_code_gen_t cg,
0818                                              lto_bool_t codegen_only);
0819 
0820 /**
0821  * Parse -mllvm style debug options.
0822  *
0823  * \since LTO_API_VERSION=18
0824  */
0825 extern void thinlto_debug_options(const char *const *options, int number);
0826 
0827 /**
0828  * Test if a module has support for ThinLTO linking.
0829  *
0830  * \since LTO_API_VERSION=18
0831  */
0832 extern lto_bool_t lto_module_is_thinlto(lto_module_t mod);
0833 
0834 /**
0835  * Adds a symbol to the list of global symbols that must exist in the final
0836  * generated code. If a function is not listed there, it might be inlined into
0837  * every usage and optimized away. For every single module, the functions
0838  * referenced from code outside of the ThinLTO modules need to be added here.
0839  *
0840  * \since LTO_API_VERSION=18
0841  */
0842 extern void thinlto_codegen_add_must_preserve_symbol(thinlto_code_gen_t cg,
0843                                                      const char *name,
0844                                                      int length);
0845 
0846 /**
0847  * Adds a symbol to the list of global symbols that are cross-referenced between
0848  * ThinLTO files. If the ThinLTO CodeGenerator can ensure that every
0849  * references from a ThinLTO module to this symbol is optimized away, then
0850  * the symbol can be discarded.
0851  *
0852  * \since LTO_API_VERSION=18
0853  */
0854 extern void thinlto_codegen_add_cross_referenced_symbol(thinlto_code_gen_t cg,
0855                                                         const char *name,
0856                                                         int length);
0857 
0858 /**
0859  * @} // endgoup LLVMCTLTO
0860  * @defgroup LLVMCTLTO_CACHING ThinLTO Cache Control
0861  * @ingroup LLVMCTLTO
0862  *
0863  * These entry points control the ThinLTO cache. The cache is intended to
0864  * support incremental builds, and thus needs to be persistent across builds.
0865  * The client enables the cache by supplying a path to an existing directory.
0866  * The code generator will use this to store objects files that may be reused
0867  * during a subsequent build.
0868  * To avoid filling the disk space, a few knobs are provided:
0869  *  - The pruning interval limits the frequency at which the garbage collector
0870  *    will try to scan the cache directory to prune expired entries.
0871  *    Setting to a negative number disables the pruning.
0872  *  - The pruning expiration time indicates to the garbage collector how old an
0873  *    entry needs to be to be removed.
0874  *  - Finally, the garbage collector can be instructed to prune the cache until
0875  *    the occupied space goes below a threshold.
0876  * @{
0877  */
0878 
0879 /**
0880  * Sets the path to a directory to use as a cache storage for incremental build.
0881  * Setting this activates caching.
0882  *
0883  * \since LTO_API_VERSION=18
0884  */
0885 extern void thinlto_codegen_set_cache_dir(thinlto_code_gen_t cg,
0886                                           const char *cache_dir);
0887 
0888 /**
0889  * Sets the cache pruning interval (in seconds). A negative value disables the
0890  * pruning. An unspecified default value will be applied, and a value of 0 will
0891  * force prunning to occur.
0892  *
0893  * \since LTO_API_VERSION=18
0894  */
0895 extern void thinlto_codegen_set_cache_pruning_interval(thinlto_code_gen_t cg,
0896                                                        int interval);
0897 
0898 /**
0899  * Sets the maximum cache size that can be persistent across build, in terms of
0900  * percentage of the available space on the disk. Set to 100 to indicate
0901  * no limit, 50 to indicate that the cache size will not be left over half the
0902  * available space. A value over 100 will be reduced to 100, a value of 0 will
0903  * be ignored. An unspecified default value will be applied.
0904  *
0905  * The formula looks like:
0906  *  AvailableSpace = FreeSpace + ExistingCacheSize
0907  *  NewCacheSize = AvailableSpace * P/100
0908  *
0909  * \since LTO_API_VERSION=18
0910  */
0911 extern void thinlto_codegen_set_final_cache_size_relative_to_available_space(
0912     thinlto_code_gen_t cg, unsigned percentage);
0913 
0914 /**
0915  * Sets the expiration (in seconds) for an entry in the cache. An unspecified
0916  * default value will be applied. A value of 0 will be ignored.
0917  *
0918  * \since LTO_API_VERSION=18
0919  */
0920 extern void thinlto_codegen_set_cache_entry_expiration(thinlto_code_gen_t cg,
0921                                                        unsigned expiration);
0922 
0923 /**
0924  * Sets the maximum size of the cache directory (in bytes). A value over the
0925  * amount of available space on the disk will be reduced to the amount of
0926  * available space. An unspecified default value will be applied. A value of 0
0927  * will be ignored.
0928  *
0929  * \since LTO_API_VERSION=22
0930  */
0931 extern void thinlto_codegen_set_cache_size_bytes(thinlto_code_gen_t cg,
0932                                                  unsigned max_size_bytes);
0933 
0934 /**
0935  * Same as thinlto_codegen_set_cache_size_bytes, except the maximum size is in
0936  * megabytes (2^20 bytes).
0937  *
0938  * \since LTO_API_VERSION=23
0939  */
0940 extern void
0941 thinlto_codegen_set_cache_size_megabytes(thinlto_code_gen_t cg,
0942                                          unsigned max_size_megabytes);
0943 
0944 /**
0945  * Sets the maximum number of files in the cache directory. An unspecified
0946  * default value will be applied. A value of 0 will be ignored.
0947  *
0948  * \since LTO_API_VERSION=22
0949  */
0950 extern void thinlto_codegen_set_cache_size_files(thinlto_code_gen_t cg,
0951                                                  unsigned max_size_files);
0952 
0953 /**
0954  * @} // endgroup LLVMCTLTO_CACHING
0955  */
0956 
0957 LLVM_C_EXTERN_C_END
0958 
0959 #endif /* LLVM_C_LTO_H */